Parent directory

mousekey.c

24653 bytes
  1/*
  2 * Copyright 2011 Jun Wako <wakojun@gmail.com>
  3 *
  4 * This program is free software: you can redistribute it and/or modify
  5 * it under the terms of the GNU General Public License as published by
  6 * the Free Software Foundation, either version 2 of the License, or
  7 * (at your option) any later version.
  8 *
  9 * This program is distributed in the hope that it will be useful,
 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 * GNU General Public License for more details.
 13 *
 14 * You should have received a copy of the GNU General Public License
 15 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 16 */
 17
 18#include <stdint.h>
 19#include <string.h>
 20#include "keycode.h"
 21#include "host.h"
 22#include "timer.h"
 23#include "print.h"
 24#include "debug.h"
 25#include "mousekey.h"
 26
 27static inline int8_t times_inv_sqrt2(int8_t x) {
 28    // 181/256 (0.70703125) is used as an approximation for 1/sqrt(2)
 29    // because it is close to the exact value which is 0.707106781
 30    const int16_t  n = x * 181;
 31    const uint16_t d = 256;
 32
 33    // To ensure that the integer result is rounded accurately after
 34    // division, check the sign of the numerator:
 35    // If negative, subtract half of the denominator before dividing
 36    // Otherwise, add half of the denominator before dividing
 37    return n < 0 ? (n - d / 2) / d : (n + d / 2) / d;
 38}
 39
 40static report_mouse_t mouse_report = {0};
 41static void           mousekey_debug(void);
 42static uint8_t        mousekey_accel        = 0;
 43static uint8_t        mousekey_repeat       = 0;
 44static uint8_t        mousekey_wheel_repeat = 0;
 45#ifdef MOUSEKEY_INERTIA
 46static uint8_t mousekey_frame     = 0; // track whether gesture is inactive, first frame, or repeating
 47static int8_t  mousekey_x_dir     = 0; // -1 / 0 / 1 = left / neutral / right
 48static int8_t  mousekey_y_dir     = 0; // -1 / 0 / 0 = up / neutral / down
 49static int8_t  mousekey_x_inertia = 0; // current velocity, limit +/- MOUSEKEY_TIME_TO_MAX
 50static int8_t  mousekey_y_inertia = 0; // ...
 51#endif
 52#ifdef MK_KINETIC_SPEED
 53static uint16_t mouse_timer = 0;
 54#endif
 55
 56#ifndef MK_3_SPEED
 57
 58static uint16_t last_timer_c = 0;
 59static uint16_t last_timer_w = 0;
 60
 61/*
 62 * Mouse keys acceleration algorithm
 63 *  http://en.wikipedia.org/wiki/Mouse_keys
 64 *
 65 *  speed = delta * max_speed * (repeat / time_to_max)**((1000+curve)/1000)
 66 */
 67/* milliseconds between the initial key press and first repeated motion event (0-2550) */
 68uint8_t mk_delay = MOUSEKEY_DELAY / 10;
 69/* milliseconds between repeated motion events (0-255) */
 70uint8_t mk_interval = MOUSEKEY_INTERVAL;
 71/* steady speed (in action_delta units) applied each event (0-255) */
 72uint8_t mk_max_speed = MOUSEKEY_MAX_SPEED;
 73/* number of events (count) accelerating to steady speed (0-255) */
 74uint8_t mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
 75/* ramp used to reach maximum pointer speed (NOT SUPPORTED) */
 76// int8_t mk_curve = 0;
 77/* wheel params */
 78/* milliseconds between the initial key press and first repeated motion event (0-2550) */
 79uint8_t mk_wheel_delay = MOUSEKEY_WHEEL_DELAY / 10;
 80/* milliseconds between repeated motion events (0-255) */
 81#    ifdef MK_KINETIC_SPEED
 82uint16_t mk_wheel_interval = 1000U / MOUSEKEY_WHEEL_INITIAL_MOVEMENTS;
 83#    else
 84uint8_t mk_wheel_interval = MOUSEKEY_WHEEL_INTERVAL;
 85#    endif
 86uint8_t mk_wheel_max_speed   = MOUSEKEY_WHEEL_MAX_SPEED;
 87uint8_t mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
 88
 89#    ifndef MK_COMBINED
 90#        ifndef MK_KINETIC_SPEED
 91#            ifndef MOUSEKEY_INERTIA
 92
 93/* Default accelerated mode */
 94
 95static uint8_t move_unit(void) {
 96    uint16_t unit;
 97    if (mousekey_accel & (1 << 0)) {
 98        unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed) / 4;
 99    } else if (mousekey_accel & (1 << 1)) {
100        unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed) / 2;
101    } else if (mousekey_accel & (1 << 2)) {
102        unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed);
103    } else if (mousekey_repeat == 0) {
104        unit = MOUSEKEY_MOVE_DELTA;
105    } else if (mousekey_repeat >= mk_time_to_max) {
106        unit = MOUSEKEY_MOVE_DELTA * mk_max_speed;
107    } else {
108        unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * mousekey_repeat) / mk_time_to_max;
109    }
110    return (unit > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : (unit == 0 ? 1 : unit));
111}
112
113#            else // MOUSEKEY_INERTIA mode
114
115static int8_t move_unit(uint8_t axis) {
116    int16_t unit;
117
118    // handle X or Y axis
119    int8_t inertia, dir;
120    if (axis) {
121        inertia = mousekey_y_inertia;
122        dir     = mousekey_y_dir;
123    } else {
124        inertia = mousekey_x_inertia;
125        dir     = mousekey_x_dir;
126    }
127
128    if (mousekey_frame < 2) { // first frame(s): initial keypress moves one pixel
129        mousekey_frame = 1;
130        unit           = dir * MOUSEKEY_MOVE_DELTA;
131    } else { // acceleration
132        // linear acceleration (is here for reference, but doesn't feel as good during use)
133        // unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * inertia) / mk_time_to_max;
134
135        // x**2 acceleration (quadratic, more precise for short movements)
136        int16_t percent = (inertia << 8) / mk_time_to_max;
137        percent         = ((int32_t)percent * percent) >> 8;
138        if (inertia < 0) percent = -percent;
139
140        // unit = sign(inertia) + (percent of max speed)
141        if (inertia > 0)
142            unit = 1;
143        else if (inertia < 0)
144            unit = -1;
145        else
146            unit = 0;
147
148        unit = unit + ((mk_max_speed * percent) >> 8);
149    }
150
151    if (unit > MOUSEKEY_MOVE_MAX)
152        unit = MOUSEKEY_MOVE_MAX;
153    else if (unit < -MOUSEKEY_MOVE_MAX)
154        unit = -MOUSEKEY_MOVE_MAX;
155    return unit;
156}
157
158#            endif // end MOUSEKEY_INERTIA mode
159
160static uint8_t wheel_unit(void) {
161    uint16_t unit;
162    if (mousekey_accel & (1 << 0)) {
163        unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed) / 4;
164    } else if (mousekey_accel & (1 << 1)) {
165        unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed) / 2;
166    } else if (mousekey_accel & (1 << 2)) {
167        unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed);
168    } else if (mousekey_wheel_repeat == 0) {
169        unit = MOUSEKEY_WHEEL_DELTA;
170    } else if (mousekey_wheel_repeat >= mk_wheel_time_to_max) {
171        unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed;
172    } else {
173        unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_wheel_repeat) / mk_wheel_time_to_max;
174    }
175    return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : (unit == 0 ? 1 : unit));
176}
177
178#        else /* #ifndef MK_KINETIC_SPEED */
179
180/*
181 * Kinetic movement  acceleration algorithm
182 *
183 *  current speed = I + A * T/50 + A * (T/50)^2 * 1/2 | maximum B
184 *
185 * T: time since the mouse movement started
186 * E: mouse events per second (set through MOUSEKEY_INTERVAL, UHK sends 250, the
187 *    pro micro on my Signum 3.0 sends only 125!)
188 * I: initial speed at time 0
189 * A: acceleration
190 * B: base mouse travel speed
191 */
192const uint16_t mk_accelerated_speed = MOUSEKEY_ACCELERATED_SPEED;
193const uint16_t mk_base_speed        = MOUSEKEY_BASE_SPEED;
194const uint16_t mk_decelerated_speed = MOUSEKEY_DECELERATED_SPEED;
195const uint16_t mk_initial_speed     = MOUSEKEY_INITIAL_SPEED;
196
197static uint8_t move_unit(void) {
198    uint16_t speed = mk_initial_speed;
199
200    if (mousekey_accel & (1 << 0)) {
201        speed = mk_decelerated_speed;
202    } else if (mousekey_accel & (1 << 2)) {
203        speed = mk_accelerated_speed;
204    } else if (mousekey_repeat && mouse_timer) {
205        const uint16_t time_elapsed = timer_elapsed(mouse_timer) / 50;
206        speed                       = mk_initial_speed + MOUSEKEY_MOVE_DELTA * time_elapsed + (MOUSEKEY_MOVE_DELTA * time_elapsed * time_elapsed) / 2;
207        if (speed > mk_base_speed) {
208            speed = mk_base_speed;
209        }
210    }
211    /* convert speed to USB mouse speed 1 to 127 */
212    speed = (uint8_t)(speed / (1000U / mk_interval));
213
214    if (speed > MOUSEKEY_MOVE_MAX) {
215        speed = MOUSEKEY_MOVE_MAX;
216    } else if (speed < 1) {
217        speed = 1;
218    }
219    return speed;
220}
221
222static uint8_t wheel_unit(void) {
223    uint16_t speed = MOUSEKEY_WHEEL_INITIAL_MOVEMENTS;
224
225    if (mousekey_accel & (1 << 0)) {
226        speed = MOUSEKEY_WHEEL_DECELERATED_MOVEMENTS;
227    } else if (mousekey_accel & (1 << 2)) {
228        speed = MOUSEKEY_WHEEL_ACCELERATED_MOVEMENTS;
229    } else if (mousekey_wheel_repeat && mouse_timer) {
230        if (mk_wheel_interval != MOUSEKEY_WHEEL_BASE_MOVEMENTS) {
231            const uint16_t time_elapsed = timer_elapsed(mouse_timer) / 50;
232            speed                       = MOUSEKEY_WHEEL_INITIAL_MOVEMENTS + 1 * time_elapsed + (1 * time_elapsed * time_elapsed) / 2;
233        }
234        if (speed > MOUSEKEY_WHEEL_BASE_MOVEMENTS) {
235            speed = MOUSEKEY_WHEEL_BASE_MOVEMENTS;
236        }
237    }
238    mk_wheel_interval = 1000U / speed;
239    return 1;
240}
241
242#        endif /* #ifndef MK_KINETIC_SPEED */
243#    else      /* #ifndef MK_COMBINED */
244
245/* Combined mode */
246
247static uint8_t move_unit(void) {
248    uint16_t unit;
249    if (mousekey_accel & (1 << 0)) {
250        unit = 1;
251    } else if (mousekey_accel & (1 << 1)) {
252        unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed) / 2;
253    } else if (mousekey_accel & (1 << 2)) {
254        unit = MOUSEKEY_MOVE_MAX;
255    } else if (mousekey_repeat == 0) {
256        unit = MOUSEKEY_MOVE_DELTA;
257    } else if (mousekey_repeat >= mk_time_to_max) {
258        unit = MOUSEKEY_MOVE_DELTA * mk_max_speed;
259    } else {
260        unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * mousekey_repeat) / mk_time_to_max;
261    }
262    return (unit > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : (unit == 0 ? 1 : unit));
263}
264
265static uint8_t wheel_unit(void) {
266    uint16_t unit;
267    if (mousekey_accel & (1 << 0)) {
268        unit = 1;
269    } else if (mousekey_accel & (1 << 1)) {
270        unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed) / 2;
271    } else if (mousekey_accel & (1 << 2)) {
272        unit = MOUSEKEY_WHEEL_MAX;
273    } else if (mousekey_repeat == 0) {
274        unit = MOUSEKEY_WHEEL_DELTA;
275    } else if (mousekey_repeat >= mk_wheel_time_to_max) {
276        unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed;
277    } else {
278        unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_repeat) / mk_wheel_time_to_max;
279    }
280    return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : (unit == 0 ? 1 : unit));
281}
282
283#    endif /* #ifndef MK_COMBINED */
284
285#    ifdef MOUSEKEY_INERTIA
286
287static int8_t calc_inertia(int8_t direction, int8_t velocity) {
288    // simulate acceleration and deceleration
289
290    // deceleration
291    if ((direction > -1) && (velocity < 0))
292        velocity = (velocity + 1) * (256 - MOUSEKEY_FRICTION) / 256;
293    else if ((direction < 1) && (velocity > 0))
294        velocity = velocity * (256 - MOUSEKEY_FRICTION) / 256;
295
296    // acceleration
297    if ((direction > 0) && (velocity < mk_time_to_max))
298        velocity++;
299    else if ((direction < 0) && (velocity > -mk_time_to_max))
300        velocity--;
301
302    return velocity;
303}
304
305#    endif
306
307void mousekey_task(void) {
308    // report cursor and scroll movement independently
309    report_mouse_t tmpmr = mouse_report;
310
311    mouse_report.x = 0;
312    mouse_report.y = 0;
313    mouse_report.v = 0;
314    mouse_report.h = 0;
315
316#    ifdef MOUSEKEY_INERTIA
317
318    // if an animation is in progress and it's time for the next frame
319    if ((mousekey_frame) && timer_elapsed(last_timer_c) > ((mousekey_frame > 1) ? mk_interval : mk_delay * 10)) {
320        mousekey_x_inertia = calc_inertia(mousekey_x_dir, mousekey_x_inertia);
321        mousekey_y_inertia = calc_inertia(mousekey_y_dir, mousekey_y_inertia);
322
323        mouse_report.x = move_unit(0);
324        mouse_report.y = move_unit(1);
325
326        // prevent sticky "drift"
327        if ((!mousekey_x_dir) && (!mousekey_x_inertia)) tmpmr.x = 0;
328        if ((!mousekey_y_dir) && (!mousekey_y_inertia)) tmpmr.y = 0;
329
330        if (mousekey_frame < 2) mousekey_frame++;
331    }
332
333    // reset if not moving and no movement keys are held
334    if ((!mousekey_x_dir) && (!mousekey_y_dir) && (!mousekey_x_inertia) && (!mousekey_y_inertia)) {
335        mousekey_frame = 0;
336        tmpmr.x        = 0;
337        tmpmr.y        = 0;
338    }
339
340#    else // default acceleration
341
342    if ((tmpmr.x || tmpmr.y) && timer_elapsed(last_timer_c) > (mousekey_repeat ? mk_interval : mk_delay * 10)) {
343        if (mousekey_repeat != UINT8_MAX) mousekey_repeat++;
344        if (tmpmr.x != 0) mouse_report.x = move_unit() * ((tmpmr.x > 0) ? 1 : -1);
345        if (tmpmr.y != 0) mouse_report.y = move_unit() * ((tmpmr.y > 0) ? 1 : -1);
346
347        /* diagonal move [1/sqrt(2)] */
348        if (mouse_report.x && mouse_report.y) {
349            mouse_report.x = times_inv_sqrt2(mouse_report.x);
350            if (mouse_report.x == 0) {
351                mouse_report.x = 1;
352            }
353            mouse_report.y = times_inv_sqrt2(mouse_report.y);
354            if (mouse_report.y == 0) {
355                mouse_report.y = 1;
356            }
357        }
358    }
359
360#    endif // MOUSEKEY_INERTIA or not
361
362    if ((tmpmr.v || tmpmr.h) && timer_elapsed(last_timer_w) > (mousekey_wheel_repeat ? mk_wheel_interval : mk_wheel_delay * 10)) {
363        if (mousekey_wheel_repeat != UINT8_MAX) mousekey_wheel_repeat++;
364        if (tmpmr.v != 0) mouse_report.v = wheel_unit() * ((tmpmr.v > 0) ? 1 : -1);
365        if (tmpmr.h != 0) mouse_report.h = wheel_unit() * ((tmpmr.h > 0) ? 1 : -1);
366
367        /* diagonal move [1/sqrt(2)] */
368        if (mouse_report.v && mouse_report.h) {
369            mouse_report.v = times_inv_sqrt2(mouse_report.v);
370            if (mouse_report.v == 0) {
371                mouse_report.v = 1;
372            }
373            mouse_report.h = times_inv_sqrt2(mouse_report.h);
374            if (mouse_report.h == 0) {
375                mouse_report.h = 1;
376            }
377        }
378    }
379
380    if (has_mouse_report_changed(&mouse_report, &tmpmr) || should_mousekey_report_send(&mouse_report)) {
381        mousekey_send();
382    }
383    // save the state for later
384    memcpy(&mouse_report, &tmpmr, sizeof(tmpmr));
385}
386
387void mousekey_on(uint8_t code) {
388#    ifdef MK_KINETIC_SPEED
389    if (mouse_timer == 0) {
390        mouse_timer = timer_read();
391    }
392#    endif
393
394#    if defined(MOUSEKEY_OVERLAP_RESET) && !defined(MOUSEKEY_INERTIA)
395    // If mouse report is not zero, the current mousekey press is overlapping
396    // with another. Restart acceleration for smoother directional transition.
397    if (mouse_report.x || mouse_report.y || mouse_report.h || mouse_report.v) {
398#        ifdef MK_KINETIC_SPEED
399        mouse_timer = timer_read() - MOUSEKEY_OVERLAP_INTERVAL;
400#        else
401        mousekey_repeat       = MOUSEKEY_OVERLAP_MOVE_DELTA;
402        mousekey_wheel_repeat = MOUSEKEY_OVERLAP_WHEEL_DELTA;
403#        endif
404    }
405#    endif // defined(MOUSEKEY_OVERLAP_RESET) && !defined(MOUSEKEY_INERTIA)
406
407#    ifdef MOUSEKEY_INERTIA
408
409    // initial keypress sets impulse and activates first frame of movement
410    if ((code == QK_MOUSE_CURSOR_UP) || (code == QK_MOUSE_CURSOR_DOWN)) {
411        mousekey_y_dir = (code == QK_MOUSE_CURSOR_DOWN) ? 1 : -1;
412        if (mousekey_frame < 2) mouse_report.y = move_unit(1);
413    } else if ((code == QK_MOUSE_CURSOR_LEFT) || (code == QK_MOUSE_CURSOR_RIGHT)) {
414        mousekey_x_dir = (code == QK_MOUSE_CURSOR_RIGHT) ? 1 : -1;
415        if (mousekey_frame < 2) mouse_report.x = move_unit(0);
416    }
417
418#    else // no inertia
419
420    if (code == QK_MOUSE_CURSOR_UP)
421        mouse_report.y = move_unit() * -1;
422    else if (code == QK_MOUSE_CURSOR_DOWN)
423        mouse_report.y = move_unit();
424    else if (code == QK_MOUSE_CURSOR_LEFT)
425        mouse_report.x = move_unit() * -1;
426    else if (code == QK_MOUSE_CURSOR_RIGHT)
427        mouse_report.x = move_unit();
428
429#    endif // inertia or not
430
431    else if (code == QK_MOUSE_WHEEL_UP)
432        mouse_report.v = wheel_unit();
433    else if (code == QK_MOUSE_WHEEL_DOWN)
434        mouse_report.v = wheel_unit() * -1;
435    else if (code == QK_MOUSE_WHEEL_LEFT)
436        mouse_report.h = wheel_unit() * -1;
437    else if (code == QK_MOUSE_WHEEL_RIGHT)
438        mouse_report.h = wheel_unit();
439    else if (IS_MOUSEKEY_BUTTON(code))
440        mouse_report.buttons |= 1 << (code - QK_MOUSE_BUTTON_1);
441    else if (code == QK_MOUSE_ACCELERATION_0)
442        mousekey_accel |= (1 << 0);
443    else if (code == QK_MOUSE_ACCELERATION_1)
444        mousekey_accel |= (1 << 1);
445    else if (code == QK_MOUSE_ACCELERATION_2)
446        mousekey_accel |= (1 << 2);
447}
448
449void mousekey_off(uint8_t code) {
450#    ifdef MOUSEKEY_INERTIA
451
452    // key release clears impulse unless opposite direction is held
453    if ((code == QK_MOUSE_CURSOR_UP) && (mousekey_y_dir < 1))
454        mousekey_y_dir = 0;
455    else if ((code == QK_MOUSE_CURSOR_DOWN) && (mousekey_y_dir > -1))
456        mousekey_y_dir = 0;
457    else if ((code == QK_MOUSE_CURSOR_LEFT) && (mousekey_x_dir < 1))
458        mousekey_x_dir = 0;
459    else if ((code == QK_MOUSE_CURSOR_RIGHT) && (mousekey_x_dir > -1))
460        mousekey_x_dir = 0;
461
462#    else // no inertia
463
464    if (code == QK_MOUSE_CURSOR_UP && mouse_report.y < 0)
465        mouse_report.y = 0;
466    else if (code == QK_MOUSE_CURSOR_DOWN && mouse_report.y > 0)
467        mouse_report.y = 0;
468    else if (code == QK_MOUSE_CURSOR_LEFT && mouse_report.x < 0)
469        mouse_report.x = 0;
470    else if (code == QK_MOUSE_CURSOR_RIGHT && mouse_report.x > 0)
471        mouse_report.x = 0;
472
473#    endif // inertia or not
474
475    else if (code == QK_MOUSE_WHEEL_UP && mouse_report.v > 0)
476        mouse_report.v = 0;
477    else if (code == QK_MOUSE_WHEEL_DOWN && mouse_report.v < 0)
478        mouse_report.v = 0;
479    else if (code == QK_MOUSE_WHEEL_LEFT && mouse_report.h < 0)
480        mouse_report.h = 0;
481    else if (code == QK_MOUSE_WHEEL_RIGHT && mouse_report.h > 0)
482        mouse_report.h = 0;
483    else if (IS_MOUSEKEY_BUTTON(code))
484        mouse_report.buttons &= ~(1 << (code - QK_MOUSE_BUTTON_1));
485    else if (code == QK_MOUSE_ACCELERATION_0)
486        mousekey_accel &= ~(1 << 0);
487    else if (code == QK_MOUSE_ACCELERATION_1)
488        mousekey_accel &= ~(1 << 1);
489    else if (code == QK_MOUSE_ACCELERATION_2)
490        mousekey_accel &= ~(1 << 2);
491    if (mouse_report.x == 0 && mouse_report.y == 0) {
492        mousekey_repeat = 0;
493#    ifdef MK_KINETIC_SPEED
494        mouse_timer = 0;
495#    endif /* #ifdef MK_KINETIC_SPEED */
496    }
497    if (mouse_report.v == 0 && mouse_report.h == 0) mousekey_wheel_repeat = 0;
498}
499
500#else /* #ifndef MK_3_SPEED */
501
502enum { mkspd_unmod, mkspd_0, mkspd_1, mkspd_2, mkspd_COUNT };
503#    ifndef MK_MOMENTARY_ACCEL
504static uint8_t  mk_speed                 = mkspd_1;
505#    else
506static uint8_t mk_speed      = mkspd_unmod;
507static uint8_t mkspd_DEFAULT = mkspd_unmod;
508#    endif
509static uint16_t last_timer_c             = 0;
510static uint16_t last_timer_w             = 0;
511uint16_t        c_offsets[mkspd_COUNT]   = {MK_C_OFFSET_UNMOD, MK_C_OFFSET_0, MK_C_OFFSET_1, MK_C_OFFSET_2};
512uint16_t        c_intervals[mkspd_COUNT] = {MK_C_INTERVAL_UNMOD, MK_C_INTERVAL_0, MK_C_INTERVAL_1, MK_C_INTERVAL_2};
513uint16_t        w_offsets[mkspd_COUNT]   = {MK_W_OFFSET_UNMOD, MK_W_OFFSET_0, MK_W_OFFSET_1, MK_W_OFFSET_2};
514uint16_t        w_intervals[mkspd_COUNT] = {MK_W_INTERVAL_UNMOD, MK_W_INTERVAL_0, MK_W_INTERVAL_1, MK_W_INTERVAL_2};
515
516void mousekey_task(void) {
517    // report cursor and scroll movement independently
518    report_mouse_t tmpmr = mouse_report;
519    mouse_report.x       = 0;
520    mouse_report.y       = 0;
521    mouse_report.v       = 0;
522    mouse_report.h       = 0;
523
524    if ((tmpmr.x || tmpmr.y) && timer_elapsed(last_timer_c) > c_intervals[mk_speed]) {
525        mouse_report.x = tmpmr.x;
526        mouse_report.y = tmpmr.y;
527    }
528    if ((tmpmr.h || tmpmr.v) && timer_elapsed(last_timer_w) > w_intervals[mk_speed]) {
529        mouse_report.v = tmpmr.v;
530        mouse_report.h = tmpmr.h;
531    }
532
533    if (has_mouse_report_changed(&mouse_report, &tmpmr) || should_mousekey_report_send(&mouse_report)) {
534        mousekey_send();
535    }
536    memcpy(&mouse_report, &tmpmr, sizeof(tmpmr));
537}
538
539void adjust_speed(void) {
540    uint16_t const c_offset = c_offsets[mk_speed];
541    uint16_t const w_offset = w_offsets[mk_speed];
542    if (mouse_report.x > 0) mouse_report.x = c_offset;
543    if (mouse_report.x < 0) mouse_report.x = c_offset * -1;
544    if (mouse_report.y > 0) mouse_report.y = c_offset;
545    if (mouse_report.y < 0) mouse_report.y = c_offset * -1;
546    if (mouse_report.h > 0) mouse_report.h = w_offset;
547    if (mouse_report.h < 0) mouse_report.h = w_offset * -1;
548    if (mouse_report.v > 0) mouse_report.v = w_offset;
549    if (mouse_report.v < 0) mouse_report.v = w_offset * -1;
550    // adjust for diagonals
551    if (mouse_report.x && mouse_report.y) {
552        mouse_report.x = times_inv_sqrt2(mouse_report.x);
553        if (mouse_report.x == 0) {
554            mouse_report.x = 1;
555        }
556        mouse_report.y = times_inv_sqrt2(mouse_report.y);
557        if (mouse_report.y == 0) {
558            mouse_report.y = 1;
559        }
560    }
561    if (mouse_report.h && mouse_report.v) {
562        mouse_report.h = times_inv_sqrt2(mouse_report.h);
563        mouse_report.v = times_inv_sqrt2(mouse_report.v);
564    }
565}
566
567void mousekey_on(uint8_t code) {
568    uint16_t const c_offset  = c_offsets[mk_speed];
569    uint16_t const w_offset  = w_offsets[mk_speed];
570    uint8_t const  old_speed = mk_speed;
571    if (code == QK_MOUSE_CURSOR_UP)
572        mouse_report.y = c_offset * -1;
573    else if (code == QK_MOUSE_CURSOR_DOWN)
574        mouse_report.y = c_offset;
575    else if (code == QK_MOUSE_CURSOR_LEFT)
576        mouse_report.x = c_offset * -1;
577    else if (code == QK_MOUSE_CURSOR_RIGHT)
578        mouse_report.x = c_offset;
579    else if (code == QK_MOUSE_WHEEL_UP)
580        mouse_report.v = w_offset;
581    else if (code == QK_MOUSE_WHEEL_DOWN)
582        mouse_report.v = w_offset * -1;
583    else if (code == QK_MOUSE_WHEEL_LEFT)
584        mouse_report.h = w_offset * -1;
585    else if (code == QK_MOUSE_WHEEL_RIGHT)
586        mouse_report.h = w_offset;
587    else if (IS_MOUSEKEY_BUTTON(code))
588        mouse_report.buttons |= 1 << (code - QK_MOUSE_BUTTON_1);
589    else if (code == QK_MOUSE_ACCELERATION_0)
590        mk_speed = mkspd_0;
591    else if (code == QK_MOUSE_ACCELERATION_1)
592        mk_speed = mkspd_1;
593    else if (code == QK_MOUSE_ACCELERATION_2)
594        mk_speed = mkspd_2;
595    if (mk_speed != old_speed) adjust_speed();
596}
597
598void mousekey_off(uint8_t code) {
599#    ifdef MK_MOMENTARY_ACCEL
600    uint8_t const old_speed = mk_speed;
601#    endif
602    if (code == QK_MOUSE_CURSOR_UP && mouse_report.y < 0)
603        mouse_report.y = 0;
604    else if (code == QK_MOUSE_CURSOR_DOWN && mouse_report.y > 0)
605        mouse_report.y = 0;
606    else if (code == QK_MOUSE_CURSOR_LEFT && mouse_report.x < 0)
607        mouse_report.x = 0;
608    else if (code == QK_MOUSE_CURSOR_RIGHT && mouse_report.x > 0)
609        mouse_report.x = 0;
610    else if (code == QK_MOUSE_WHEEL_UP && mouse_report.v > 0)
611        mouse_report.v = 0;
612    else if (code == QK_MOUSE_WHEEL_DOWN && mouse_report.v < 0)
613        mouse_report.v = 0;
614    else if (code == QK_MOUSE_WHEEL_LEFT && mouse_report.h < 0)
615        mouse_report.h = 0;
616    else if (code == QK_MOUSE_WHEEL_RIGHT && mouse_report.h > 0)
617        mouse_report.h = 0;
618    else if (IS_MOUSEKEY_BUTTON(code))
619        mouse_report.buttons &= ~(1 << (code - QK_MOUSE_BUTTON_1));
620#    ifdef MK_MOMENTARY_ACCEL
621    else if (code == QK_MOUSE_ACCELERATION_0)
622        mk_speed = mkspd_DEFAULT;
623    else if (code == QK_MOUSE_ACCELERATION_1)
624        mk_speed = mkspd_DEFAULT;
625    else if (code == QK_MOUSE_ACCELERATION_2)
626        mk_speed = mkspd_DEFAULT;
627    if (mk_speed != old_speed) adjust_speed();
628#    endif
629}
630
631#endif /* #ifndef MK_3_SPEED */
632
633void mousekey_send(void) {
634    mousekey_debug();
635    uint16_t time = timer_read();
636    if (mouse_report.x || mouse_report.y) last_timer_c = time;
637    if (mouse_report.v || mouse_report.h) last_timer_w = time;
638    host_mouse_send(&mouse_report);
639}
640
641void mousekey_clear(void) {
642    mouse_report          = (report_mouse_t){};
643    mousekey_repeat       = 0;
644    mousekey_wheel_repeat = 0;
645    mousekey_accel        = 0;
646#ifdef MOUSEKEY_INERTIA
647    mousekey_frame     = 0;
648    mousekey_x_inertia = 0;
649    mousekey_y_inertia = 0;
650    mousekey_x_dir     = 0;
651    mousekey_y_dir     = 0;
652#endif
653}
654
655static void mousekey_debug(void) {
656    if (!debug_mouse) return;
657    print("mousekey [btn|x y v h](rep/acl): [");
658    print_hex8(mouse_report.buttons);
659    print("|");
660    print_decs(mouse_report.x);
661    print(" ");
662    print_decs(mouse_report.y);
663    print(" ");
664    print_decs(mouse_report.v);
665    print(" ");
666    print_decs(mouse_report.h);
667    print("](");
668    print_dec(mousekey_repeat);
669    print("/");
670    print_dec(mousekey_accel);
671    print(")\n");
672}
673
674report_mouse_t mousekey_get_report(void) {
675    return mouse_report;
676}
677
678bool should_mousekey_report_send(report_mouse_t *mouse_report) {
679    return mouse_report->x || mouse_report->y || mouse_report->v || mouse_report->h;
680}