forked from qmk/qmk_firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointing_device_modes.c
640 lines (583 loc) · 19.5 KB
/
pointing_device_modes.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/* Copyright 2023 Alabastard (@Alabastard-64)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http:https://www.gnu.org/licenses/>.
*/
#ifdef POINTING_DEVICE_MODES_ENABLE
# include "pointing_device_modes.h"
// initialize local functions
static report_mouse_t process_pointing_mode(pointing_mode_t pointing_mode, report_mouse_t mouse_report);
static uint8_t get_pointing_mode_direction(void);
static uint8_t get_pointing_mode_divisor(void);
static uint8_t divisor_postprocess(uint8_t divisor);
// local variables
# if POINTING_MODES_DEVICE_CONTROL_COUNT > 1
static uint8_t current_device = POINTING_MODES_DEFAULT_DEVICE;
# else
static const uint8_t current_device = 0;
# if defined(POINTING_MODES_SINGLE_CONTROL)
static uint8_t selected_device = POINTING_MODES_DEFAULT_DEVICE;
# endif
# endif
static uint8_t current_direction = 0;
static uint8_t current_divisor = POINTING_DEFAULT_DIVISOR;
static uint8_t toggle_ids[] = {[0 ... POINTING_MODES_DEVICE_CONTROL_COUNT - 1] = POINTING_MODE_DEFAULT};
static pointing_mode_t pointing_modes[] = {[0 ... POINTING_MODES_DEVICE_CONTROL_COUNT - 1] = {.id = POINTING_MODE_DEFAULT}};
// set up clamping and divisor application functions
static inline int8_t clamp_int_16_to_8(int16_t value) {
if (value < INT8_MIN) {
return INT8_MIN;
} else if (value > INT8_MAX) {
return INT8_MAX;
} else {
return value;
}
}
static inline int16_t clamp_int_32_to_16(int32_t value) {
if (value < INT16_MIN) {
return INT16_MIN;
} else if (value > INT16_MAX) {
return INT16_MAX;
} else {
return value;
}
}
static inline uint16_t clamp_uint_16_to_8(uint16_t value) {
if (value > UINT8_MAX) return UINT8_MAX;
return value;
}
static int16_t divisor_multiply16(int16_t value) {
return clamp_int_32_to_16(value * (int16_t)current_divisor);
}
static int8_t divisor_divide8(int16_t value) {
return clamp_int_16_to_8(value / (int16_t)current_divisor);
}
static int16_t divisor_divide16(int16_t value) {
return value / (int16_t)current_divisor;
}
/**
* @brief divides value by current divisor return mouse x/y
*
* Intended for modifying pointing_mode x/y values
*
* @params value[in] value int16_t
*
* @return quotient clamped to mouse_xy_report_t range
*/
mouse_xy_report_t apply_divisor_xy(int16_t value) {
# ifdef MOUSE_EXTENDED_REPORT
return divisor_divide16(value);
# else
return divisor_divide8(value);
# endif
}
/**
* @brief divides value by current divisor return mouse h/v
*
* Intended for modifying pointing_mode x/y values
*
* @params value[in] value int16_t
*
* @return quotient clamped to int8_t range
*/
int8_t apply_divisor_hv(int16_t value) {
return divisor_divide8(value);
}
/**
* @brief multiplies mouse x/y value by current divisor
*
* Intended for collecting residuals by subtracting quotient * divisor from the dividend
* as modulo can be large and slow on some chips
*
* @params value[in] value mouse_xy_report_t mouse x/y axis value to multiply
*
* @return product clamped to int16_t range
*/
int16_t multiply_divisor_xy(mouse_xy_report_t value) {
return divisor_multiply16((int16_t)value);
}
/**
* @brief multiplies mouse h/v value by current divisor
*
* Intended for collecting residuals by subtracting quotient * divisor from the dividend
* as modulo can be large and slow on some chips
*
* @params value[in] value int8_t mouse h/v axis value to multiply
*
* @return modified and clamped value int16_t
*/
int16_t multiply_divisor_hv(int8_t value) {
return divisor_multiply16((int16_t)value);
}
/**
* @brief Return device id of current controlled device
*
* @return current device id [uint8_t]
*/
uint8_t get_pointing_mode_device(void) {
# ifdef POINTING_MODES_SINGLE_CONTROL
return selected_device;
# else
return current_device;
# endif
}
/**
* @brief Allow changing of active side
*
* will change which side (PM_LEFT_DEVICE, PM_RIGHT_DEVICE, etc.) is controlled by pointing mode framework
*
* NOTE: If mode is set above maximum device number device is set to zero (this allows cycling)
*
* @params[in] new side uint8_t
*/
void set_pointing_mode_device(uint8_t device) {
# if (POINTING_MODES_NUM_DEVICES > 1)
if (device > POINTING_MODES_NUM_DEVICES) device = 0;
# if POINTING_MODES_SINGLE_CONTROL
selected_device = device;
# else
current_device = device;
# endif
# else
;
# endif
}
/**
* @brief Reset pointing mode data
*
* Clear poiting mode and set to defaults
*/
void pointing_mode_reset(void) {
memset(&pointing_modes[current_device], 0, sizeof(pointing_mode_t));
pointing_modes[current_device].id = toggle_ids[current_device];
}
/**
* @brief set current pointing mode data
*
* @param[in] pointing_mode pointing_mode_t
*/
void set_pointing_mode(pointing_mode_t pointing_mode) {
// skip if same
if (!memcmp(&pointing_modes[current_device], &pointing_mode, sizeof(pointing_mode_t))) return;
memcpy(&pointing_modes[current_device], &pointing_mode, sizeof(pointing_mode_t));
}
/**
* @brief access current pointing mode id
*
* @return uint8_t current pointing mode id
*/
uint8_t get_pointing_mode_id(void) {
return pointing_modes[current_device].id;
}
/**
* @brief set pointing mode id
*
* @param[in] mode_id uint8_t
*/
void set_pointing_mode_id(uint8_t mode_id) {
if (get_pointing_mode_id() != mode_id) {
pointing_mode_reset();
pointing_modes[current_device].id = mode_id;
}
}
/**
* @brief Access current toggled pointing mode
*
* @return uint8_t toggle pointing mode
*/
uint8_t get_toggled_pointing_mode_id(void) {
return toggle_ids[current_device];
}
/**
* @brief Toggle pointing mode id on/off
*
* Will change tg_mode_id setting to POINTING_MODE_DEFAULT when toggling "off"
*
* @param[in] mode_id uint8_t
*/
void toggle_pointing_mode_id(uint8_t mode_id) {
if (get_toggled_pointing_mode_id() == mode_id) {
toggle_ids[current_device] = POINTING_MODE_DEFAULT;
} else {
toggle_ids[current_device] = mode_id;
}
if (get_pointing_mode_id() != get_toggled_pointing_mode_id()) pointing_mode_reset();
}
/**
* @brief Weak function to convert x/y axes to h/v
*
* The default uses accumulation based on inversion defines and
* halts cursor movement
*
* @params pointing_modes[in] pointing_mode_t
* @params mouse_report[in] report_mouse_t
*
* @return updated mouse_report report_mouse_t
*/
__attribute__((weak)) report_mouse_t pointing_modes_axes_conv(pointing_mode_t pointing_mode, report_mouse_t mouse_report) {
# ifdef POINTING_DEVICE_MODES_INVERT_X
pointing_mode.x -= mouse_report.x;
# else
pointing_mode.x += mouse_report.x;
# endif
# ifdef POINTING_DEVICE_MODES_INVERT_Y
pointing_mode.y -= mouse_report.y;
# else
pointing_mode.y += mouse_report.y;
# endif
set_pointing_mode(pointing_mode);
mouse_report.x = 0;
mouse_report.y = 0;
return mouse_report;
}
/**
* @brief Modifies divisor after
*
* @params pointing_modes[in] uint8_t
* @params direction[in] uint8_t
*
* @return divisor uint8_t
*/
static uint8_t divisor_postprocess(uint8_t divisor) {
if (!(pointing_mode_divisor_postprocess_user(&divisor) && pointing_mode_divisor_postprocess_kb(&divisor))) {
// never return without zero checking
return divisor ? divisor : POINTING_DEFAULT_DIVISOR;
}
// Modify divisor if precision is toggled
if (get_toggled_pointing_mode_id() == PM_PRECISION && !(get_pointing_mode_id() == PM_PRECISION)) {
divisor = clamp_uint_16_to_8(divisor * POINTING_PRECISION_DIVISOR);
}
// never return without zero checking
return divisor ? divisor : POINTING_DEFAULT_DIVISOR;
}
/**
* @brief override current divisor value
*
* Will only take effect until next cycle update or next call of this process
*
* @param[in] divisor uint8_t
*/
void pointing_mode_divisor_override(uint8_t divisor) {
current_divisor = divisor_postprocess(divisor);
}
/**
* @brief local function to get pointing mode divisor
*
* Will handle default divisors and call weak kb and user functions
*
* NOTE: Expects that pointing mode and direction has been updated
*
* @return divisor uint8_t
*/
static uint8_t get_pointing_mode_divisor(void) {
uint8_t divisor = 0;
// allow for user and keyboard overrides
divisor = get_pointing_mode_divisor_user(get_pointing_mode_id(), current_pointing_mode_direction());
if (divisor) return divisor_postprocess(divisor);
divisor = get_pointing_mode_divisor_kb(get_pointing_mode_id(), current_pointing_mode_direction());
if (divisor) return divisor_postprocess(divisor);
// built in divisors
switch (get_pointing_mode_id()) {
case PM_PRECISION:
divisor = POINTING_PRECISION_DIVISOR;
break;
case PM_DRAG:
divisor = POINTING_DRAG_DIVISOR;
break;
case PM_CARET:
divisor = current_pointing_mode_direction() < PD_LEFT ? POINTING_CARET_DIVISOR_V : POINTING_CARET_DIVISOR_H;
break;
case PM_HISTORY:
divisor = POINTING_HISTORY_DIVISOR;
break;
# ifdef EXTRAKEY_ENABLE
case PM_VOLUME:
divisor = POINTING_VOLUME_DIVISOR;
break;
# endif
}
return divisor_postprocess(divisor);
}
/**
* @brief local function to get single direction based on h/v
*
* Determines direction based on axis with largest magnitude
*
* NOTE: Defaults to PD_DOWN
*
* @return direction uint8_t
*/
static uint8_t get_pointing_mode_direction(void) {
if (abs(pointing_modes[current_device].x) > abs(pointing_modes[current_device].y)) {
if (pointing_modes[current_device].x > 0) {
return PD_RIGHT;
} else {
return PD_LEFT;
}
} else {
if (pointing_modes[current_device].y > 0) {
return PD_UP;
} else {
return PD_DOWN;
}
}
}
/**
* @brief update dependent parameters of pointing_mode
*
* Will update the direction and divisor values based on mode id and h, and v axis values
*
*/
void pointing_mode_update(void) {
current_direction = get_pointing_mode_direction();
current_divisor = get_pointing_mode_divisor();
}
/**
* @brief Access current pointing mode direction
*
* @return uint8_t direction
*/
uint8_t current_pointing_mode_direction(void) {
return current_direction;
}
/**
* @brief Access current pointing mode divisor
*
* @return uint8_t divisor
*/
uint8_t current_pointing_mode_divisor(void) {
return current_divisor;
}
/**
* @brief Tap keycodes based on mode axis values
*
* Will translate internal h & v axes into keycode taps.
* Uses input ordered DOWN, UP, LEFT, RIGHT and will output 1 keytap/divisor
* and will update the h and v values.
*
* NOTE: favours staying on axis and weakly favours the horizontal over the vertical axis
*
* @params kc_left[in] uint16_t keycode for negative h
* @params kc_down[in] uint16_t keycode for negative v
* @params kc_up[in] uint16_t keycode for positive v
* @params kc_right[in] uint16_t keycode for positive h
*/
void pointing_tap_codes(uint16_t kc_left, uint16_t kc_down, uint16_t kc_up, uint16_t kc_right) {
uint16_t kc_direction = 0;
int16_t count = 0;
switch (current_pointing_mode_direction()) {
case PD_DOWN:
kc_direction = kc_down;
break;
case PD_UP:
kc_direction = kc_up;
break;
case PD_LEFT:
kc_direction = kc_left;
break;
case PD_RIGHT:
kc_direction = kc_right;
break;
}
switch (current_pointing_mode_direction()) {
case PD_DOWN ... PD_UP:
count = divisor_divide16(pointing_modes[current_device].y);
if (!count) return; // exit if accumulated y is too low
pointing_modes[current_device].y -= divisor_multiply16(count);
pointing_modes[current_device].x = 0;
break;
case PD_LEFT ... PD_RIGHT:
count = divisor_divide16(pointing_modes[current_device].x);
if (!count) return; // exit if accumulated x is too low
pointing_modes[current_device].x -= divisor_multiply16(count);
pointing_modes[current_device].y = 0;
break;
}
// skip if KC_TRNS or KC_NO (but allow for axes update above)
if (kc_direction < 2) return;
// tap codes
uint8_t taps = abs(count);
do {
tap_code16_delay(kc_direction, POINTING_TAP_DELAY);
} while (--taps);
}
/**
* @brief Core function to handle pointing mode task
*
* Meant to be implemented in pointing_device_task
*
* @param[in] mouse_report report_mouse_t
*
* @return mouse_report report_mouse_t
*/
report_mouse_t pointing_device_modes_task(report_mouse_t mouse_report) {
// skip all processing if pointing mode is PM_NONE
if (get_pointing_mode_id() == PM_NONE) return mouse_report;
mouse_report = pointing_modes_axes_conv(pointing_modes[current_device], mouse_report);
pointing_mode_update();
mouse_report = process_pointing_mode(pointing_modes[current_device], mouse_report);
return mouse_report;
}
/**
* @brief Handle processing of pointing modes
*
* Takes in report_mouse_t and pointing_mode_t allowing manipulation of mouse_report
* and pointing_mode via set_pointing_mode
*
* @return mouse_report report_mouse_t
*/
static report_mouse_t process_pointing_mode(pointing_mode_t pointing_mode, report_mouse_t mouse_report) {
// allow overwrite of pointing modes by user, and kb
if (!(process_pointing_mode_user(pointing_mode, &mouse_report) && process_pointing_mode_kb(pointing_mode, &mouse_report))) {
return mouse_report;
}
// allow overwrite of modes using maps
# if (POINTING_MODE_MAP_COUNT > 0)
if (pointing_mode.id >= POINTING_MODE_MAP_START && pointing_mode.id <= (POINTING_MODE_MAP_START + POINTING_MODE_MAP_COUNT - 1)) {
pointing_tap_codes(POINTING_MODE_MAP(pointing_mode.id - POINTING_MODE_MAP_START));
return mouse_report;
}
# endif
switch (pointing_mode.id) {
// precision mode (reduce x y sensitivity temporarily)
case PM_PRECISION:
# ifdef POINTING_DEVICE_MODES_INVERT_X
mouse_report.x -= apply_divisor_xy(pointing_mode.x);
pointing_mode.x += multiply_divisor_xy(mouse_report.x);
# else
mouse_report.x += apply_divisor_xy(pointing_mode.x);
pointing_mode.x -= multiply_divisor_xy(mouse_report.x);
# endif
# ifdef POINTING_DEVICE_MODES_INVERT_Y
mouse_report.y -= apply_divisor_xy(pointing_mode.y);
pointing_mode.y += multiply_divisor_xy(mouse_report.y);
# else
mouse_report.y += apply_divisor_xy(pointing_mode.y);
pointing_mode.y -= multiply_divisor_xy(mouse_report.y);
# endif
set_pointing_mode(pointing_mode);
break;
// drag scroll mode (sets mouse axes to mouse_report h & v with divisor)
case PM_DRAG:
mouse_report.h = apply_divisor_hv(pointing_mode.x);
pointing_mode.x -= multiply_divisor_hv(mouse_report.h);
mouse_report.v = apply_divisor_hv(pointing_mode.y);
pointing_mode.y -= multiply_divisor_hv(mouse_report.v);
set_pointing_mode(pointing_mode);
break;
// caret mode (uses arrow keys to move cursor)
case PM_CARET:
pointing_tap_codes(KC_LEFT, KC_DOWN, KC_UP, KC_RIGHT);
break;
// history scroll mode (will scroll through undo/redo history)
case PM_HISTORY:
pointing_tap_codes(C(KC_Z), KC_NO, KC_NO, C(KC_Y));
break;
# ifdef EXTRAKEY_ENABLE
// volume scroll mode (adjusts audio volume)
case PM_VOLUME:
pointing_tap_codes(KC_NO, KC_VOLD, KC_VOLU, KC_NO);
break;
# endif
}
return mouse_report;
}
/**
* @brief User level processing of pointing device modes
*
* Takes pointing_mode_t struct, and pointer to the report_mouse_t struct used in pointing mode process allowing
* modification of mouse_report directly and pointing mode using set_pointing_mode(pointing_mode) and the returned
* bool controls the continued processing of pointing modes.
*
* NOTE: returning false will stop mode processing (for overwriting modes)
*
* @params pointing_modes[in] pointing_mode_t
* @params mouse_report[in] pointer to report_mouse_t
*
* @return bool continue processing flag
*/
__attribute__((weak)) bool process_pointing_mode_user(pointing_mode_t pointing_mode, report_mouse_t* mouse_report) {
return true; // continue processing
}
/**
* @brief Keyboard level processing of pointing device modes
*
* Takes pointing_mode_t struct, and pointer to the report_mouse_t struct used in pointing mode process allowing
* modification of mouse_report directly and pointing mode using set_pointing_mode(pointing_mode) and the returned
* bool controls the continued processing of pointing modes.
*
* NOTE: returning false will stop mode processing (for overwriting modes)
*
* @params pointing_modes[in] pointing_mode_t
* @params mouse_report[in] pointer to report_mouse_t
*
* @return bool continue processing flag
*/
__attribute__((weak)) bool process_pointing_mode_kb(pointing_mode_t pointing_mode, report_mouse_t* mouse_report) {
return true; // continue processing
}
/**
* @brief Weak function for user level adding of pointing mode divisors
*
* Takes in mode id and direction allowing for divisor values to be
* determined based on these parameters
*
* @params pointing_modes[in] uint8_t
* @params direction[in] uint8_t
*
* @return divisor uint8_t
*/
__attribute__((weak)) uint8_t get_pointing_mode_divisor_user(uint8_t mode_id, uint8_t direction) {
return 0; // continue processing
}
/**
* @brief Weak function for user level adding of pointing mode divisors
*
* Takes in mode id and direction allowing for divisor values to be
* determined based on these parameters
*
* @params pointing_modes[in] uint8_t
* @params direction[in] uint8_t
*
* @return divisor uint8_t
*/
__attribute__((weak)) uint8_t get_pointing_mode_divisor_kb(uint8_t mode_id, uint8_t direction) {
return 0; // continue processing
}
/**
* @brief Keyboard level modifying of divisors after being set and before use
*
* Allows modification the divisor after being set by get_pointing_mode_divisor stack before
* handing off to default post processing
*
* @params[in] pointer to divisor uint8_t
*
* @return bool continue processing flag
*/
__attribute__((weak)) bool pointing_mode_divisor_postprocess_kb(uint8_t* divisor) {
return true;
}
/**
* @brief User level modifying of divisors after being set and before use
*
* Allows modification the divisor after being set by get_pointing_mode_divisor stack before
* handing off to default post processing
*
* @params[in] pointer to divisor uint8_t
*
* @return bool continue processing flag
*/
__attribute__((weak)) bool pointing_mode_divisor_postprocess_user(uint8_t* divisor) {
return true;
}
#endif // POINTING_DEVICE_MODES_ENABLE