mousekey.c 24 KB

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