mousekey.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. if (mouse_timer == 0) {
  336. mouse_timer = timer_read();
  337. }
  338. # endif /* #ifdef MK_KINETIC_SPEED */
  339. # ifdef MOUSEKEY_INERTIA
  340. // initial keypress sets impulse and activates first frame of movement
  341. if ((code == KC_MS_UP) || (code == KC_MS_DOWN)) {
  342. mousekey_y_dir = (code == KC_MS_DOWN) ? 1 : -1;
  343. if (mousekey_frame < 2) mouse_report.y = move_unit(1);
  344. } else if ((code == KC_MS_LEFT) || (code == KC_MS_RIGHT)) {
  345. mousekey_x_dir = (code == KC_MS_RIGHT) ? 1 : -1;
  346. if (mousekey_frame < 2) mouse_report.x = move_unit(0);
  347. }
  348. # else // no inertia
  349. if (code == KC_MS_UP)
  350. mouse_report.y = move_unit() * -1;
  351. else if (code == KC_MS_DOWN)
  352. mouse_report.y = move_unit();
  353. else if (code == KC_MS_LEFT)
  354. mouse_report.x = move_unit() * -1;
  355. else if (code == KC_MS_RIGHT)
  356. mouse_report.x = move_unit();
  357. # endif // inertia or not
  358. else if (code == KC_MS_WH_UP)
  359. mouse_report.v = wheel_unit();
  360. else if (code == KC_MS_WH_DOWN)
  361. mouse_report.v = wheel_unit() * -1;
  362. else if (code == KC_MS_WH_LEFT)
  363. mouse_report.h = wheel_unit() * -1;
  364. else if (code == KC_MS_WH_RIGHT)
  365. mouse_report.h = wheel_unit();
  366. else if (IS_MOUSEKEY_BUTTON(code))
  367. mouse_report.buttons |= 1 << (code - KC_MS_BTN1);
  368. else if (code == KC_MS_ACCEL0)
  369. mousekey_accel |= (1 << 0);
  370. else if (code == KC_MS_ACCEL1)
  371. mousekey_accel |= (1 << 1);
  372. else if (code == KC_MS_ACCEL2)
  373. mousekey_accel |= (1 << 2);
  374. }
  375. void mousekey_off(uint8_t code) {
  376. # ifdef MOUSEKEY_INERTIA
  377. // key release clears impulse unless opposite direction is held
  378. if ((code == KC_MS_UP) && (mousekey_y_dir < 1))
  379. mousekey_y_dir = 0;
  380. else if ((code == KC_MS_DOWN) && (mousekey_y_dir > -1))
  381. mousekey_y_dir = 0;
  382. else if ((code == KC_MS_LEFT) && (mousekey_x_dir < 1))
  383. mousekey_x_dir = 0;
  384. else if ((code == KC_MS_RIGHT) && (mousekey_x_dir > -1))
  385. mousekey_x_dir = 0;
  386. # else // no inertia
  387. if (code == KC_MS_UP && mouse_report.y < 0)
  388. mouse_report.y = 0;
  389. else if (code == KC_MS_DOWN && mouse_report.y > 0)
  390. mouse_report.y = 0;
  391. else if (code == KC_MS_LEFT && mouse_report.x < 0)
  392. mouse_report.x = 0;
  393. else if (code == KC_MS_RIGHT && mouse_report.x > 0)
  394. mouse_report.x = 0;
  395. # endif // inertia or not
  396. else if (code == KC_MS_WH_UP && mouse_report.v > 0)
  397. mouse_report.v = 0;
  398. else if (code == KC_MS_WH_DOWN && mouse_report.v < 0)
  399. mouse_report.v = 0;
  400. else if (code == KC_MS_WH_LEFT && mouse_report.h < 0)
  401. mouse_report.h = 0;
  402. else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0)
  403. mouse_report.h = 0;
  404. else if (IS_MOUSEKEY_BUTTON(code))
  405. mouse_report.buttons &= ~(1 << (code - KC_MS_BTN1));
  406. else if (code == KC_MS_ACCEL0)
  407. mousekey_accel &= ~(1 << 0);
  408. else if (code == KC_MS_ACCEL1)
  409. mousekey_accel &= ~(1 << 1);
  410. else if (code == KC_MS_ACCEL2)
  411. mousekey_accel &= ~(1 << 2);
  412. if (mouse_report.x == 0 && mouse_report.y == 0) {
  413. mousekey_repeat = 0;
  414. # ifdef MK_KINETIC_SPEED
  415. mouse_timer = 0;
  416. # endif /* #ifdef MK_KINETIC_SPEED */
  417. }
  418. if (mouse_report.v == 0 && mouse_report.h == 0) mousekey_wheel_repeat = 0;
  419. }
  420. #else /* #ifndef MK_3_SPEED */
  421. enum { mkspd_unmod, mkspd_0, mkspd_1, mkspd_2, mkspd_COUNT };
  422. # ifndef MK_MOMENTARY_ACCEL
  423. static uint8_t mk_speed = mkspd_1;
  424. # else
  425. static uint8_t mk_speed = mkspd_unmod;
  426. static uint8_t mkspd_DEFAULT = mkspd_unmod;
  427. # endif
  428. static uint16_t last_timer_c = 0;
  429. static uint16_t last_timer_w = 0;
  430. uint16_t c_offsets[mkspd_COUNT] = {MK_C_OFFSET_UNMOD, MK_C_OFFSET_0, MK_C_OFFSET_1, MK_C_OFFSET_2};
  431. uint16_t c_intervals[mkspd_COUNT] = {MK_C_INTERVAL_UNMOD, MK_C_INTERVAL_0, MK_C_INTERVAL_1, MK_C_INTERVAL_2};
  432. uint16_t w_offsets[mkspd_COUNT] = {MK_W_OFFSET_UNMOD, MK_W_OFFSET_0, MK_W_OFFSET_1, MK_W_OFFSET_2};
  433. uint16_t w_intervals[mkspd_COUNT] = {MK_W_INTERVAL_UNMOD, MK_W_INTERVAL_0, MK_W_INTERVAL_1, MK_W_INTERVAL_2};
  434. void mousekey_task(void) {
  435. // report cursor and scroll movement independently
  436. report_mouse_t tmpmr = mouse_report;
  437. mouse_report.x = 0;
  438. mouse_report.y = 0;
  439. mouse_report.v = 0;
  440. mouse_report.h = 0;
  441. if ((tmpmr.x || tmpmr.y) && timer_elapsed(last_timer_c) > c_intervals[mk_speed]) {
  442. mouse_report.x = tmpmr.x;
  443. mouse_report.y = tmpmr.y;
  444. }
  445. if ((tmpmr.h || tmpmr.v) && timer_elapsed(last_timer_w) > w_intervals[mk_speed]) {
  446. mouse_report.v = tmpmr.v;
  447. mouse_report.h = tmpmr.h;
  448. }
  449. if (has_mouse_report_changed(&mouse_report, &tmpmr) || should_mousekey_report_send(&mouse_report)) {
  450. mousekey_send();
  451. }
  452. memcpy(&mouse_report, &tmpmr, sizeof(tmpmr));
  453. }
  454. void adjust_speed(void) {
  455. uint16_t const c_offset = c_offsets[mk_speed];
  456. uint16_t const w_offset = w_offsets[mk_speed];
  457. if (mouse_report.x > 0) mouse_report.x = c_offset;
  458. if (mouse_report.x < 0) mouse_report.x = c_offset * -1;
  459. if (mouse_report.y > 0) mouse_report.y = c_offset;
  460. if (mouse_report.y < 0) mouse_report.y = c_offset * -1;
  461. if (mouse_report.h > 0) mouse_report.h = w_offset;
  462. if (mouse_report.h < 0) mouse_report.h = w_offset * -1;
  463. if (mouse_report.v > 0) mouse_report.v = w_offset;
  464. if (mouse_report.v < 0) mouse_report.v = w_offset * -1;
  465. // adjust for diagonals
  466. if (mouse_report.x && mouse_report.y) {
  467. mouse_report.x = times_inv_sqrt2(mouse_report.x);
  468. if (mouse_report.x == 0) {
  469. mouse_report.x = 1;
  470. }
  471. mouse_report.y = times_inv_sqrt2(mouse_report.y);
  472. if (mouse_report.y == 0) {
  473. mouse_report.y = 1;
  474. }
  475. }
  476. if (mouse_report.h && mouse_report.v) {
  477. mouse_report.h = times_inv_sqrt2(mouse_report.h);
  478. mouse_report.v = times_inv_sqrt2(mouse_report.v);
  479. }
  480. }
  481. void mousekey_on(uint8_t code) {
  482. uint16_t const c_offset = c_offsets[mk_speed];
  483. uint16_t const w_offset = w_offsets[mk_speed];
  484. uint8_t const old_speed = mk_speed;
  485. if (code == KC_MS_UP)
  486. mouse_report.y = c_offset * -1;
  487. else if (code == KC_MS_DOWN)
  488. mouse_report.y = c_offset;
  489. else if (code == KC_MS_LEFT)
  490. mouse_report.x = c_offset * -1;
  491. else if (code == KC_MS_RIGHT)
  492. mouse_report.x = c_offset;
  493. else if (code == KC_MS_WH_UP)
  494. mouse_report.v = w_offset;
  495. else if (code == KC_MS_WH_DOWN)
  496. mouse_report.v = w_offset * -1;
  497. else if (code == KC_MS_WH_LEFT)
  498. mouse_report.h = w_offset * -1;
  499. else if (code == KC_MS_WH_RIGHT)
  500. mouse_report.h = w_offset;
  501. else if (IS_MOUSEKEY_BUTTON(code))
  502. mouse_report.buttons |= 1 << (code - KC_MS_BTN1);
  503. else if (code == KC_MS_ACCEL0)
  504. mk_speed = mkspd_0;
  505. else if (code == KC_MS_ACCEL1)
  506. mk_speed = mkspd_1;
  507. else if (code == KC_MS_ACCEL2)
  508. mk_speed = mkspd_2;
  509. if (mk_speed != old_speed) adjust_speed();
  510. }
  511. void mousekey_off(uint8_t code) {
  512. # ifdef MK_MOMENTARY_ACCEL
  513. uint8_t const old_speed = mk_speed;
  514. # endif
  515. if (code == KC_MS_UP && mouse_report.y < 0)
  516. mouse_report.y = 0;
  517. else if (code == KC_MS_DOWN && mouse_report.y > 0)
  518. mouse_report.y = 0;
  519. else if (code == KC_MS_LEFT && mouse_report.x < 0)
  520. mouse_report.x = 0;
  521. else if (code == KC_MS_RIGHT && mouse_report.x > 0)
  522. mouse_report.x = 0;
  523. else if (code == KC_MS_WH_UP && mouse_report.v > 0)
  524. mouse_report.v = 0;
  525. else if (code == KC_MS_WH_DOWN && mouse_report.v < 0)
  526. mouse_report.v = 0;
  527. else if (code == KC_MS_WH_LEFT && mouse_report.h < 0)
  528. mouse_report.h = 0;
  529. else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0)
  530. mouse_report.h = 0;
  531. else if (IS_MOUSEKEY_BUTTON(code))
  532. mouse_report.buttons &= ~(1 << (code - KC_MS_BTN1));
  533. # ifdef MK_MOMENTARY_ACCEL
  534. else if (code == KC_MS_ACCEL0)
  535. mk_speed = mkspd_DEFAULT;
  536. else if (code == KC_MS_ACCEL1)
  537. mk_speed = mkspd_DEFAULT;
  538. else if (code == KC_MS_ACCEL2)
  539. mk_speed = mkspd_DEFAULT;
  540. if (mk_speed != old_speed) adjust_speed();
  541. # endif
  542. }
  543. #endif /* #ifndef MK_3_SPEED */
  544. void mousekey_send(void) {
  545. mousekey_debug();
  546. uint16_t time = timer_read();
  547. if (mouse_report.x || mouse_report.y) last_timer_c = time;
  548. if (mouse_report.v || mouse_report.h) last_timer_w = time;
  549. host_mouse_send(&mouse_report);
  550. }
  551. void mousekey_clear(void) {
  552. mouse_report = (report_mouse_t){};
  553. mousekey_repeat = 0;
  554. mousekey_wheel_repeat = 0;
  555. mousekey_accel = 0;
  556. #ifdef MOUSEKEY_INERTIA
  557. mousekey_frame = 0;
  558. mousekey_x_inertia = 0;
  559. mousekey_y_inertia = 0;
  560. mousekey_x_dir = 0;
  561. mousekey_y_dir = 0;
  562. #endif
  563. }
  564. static void mousekey_debug(void) {
  565. if (!debug_mouse) return;
  566. print("mousekey [btn|x y v h](rep/acl): [");
  567. print_hex8(mouse_report.buttons);
  568. print("|");
  569. print_decs(mouse_report.x);
  570. print(" ");
  571. print_decs(mouse_report.y);
  572. print(" ");
  573. print_decs(mouse_report.v);
  574. print(" ");
  575. print_decs(mouse_report.h);
  576. print("](");
  577. print_dec(mousekey_repeat);
  578. print("/");
  579. print_dec(mousekey_accel);
  580. print(")\n");
  581. }
  582. report_mouse_t mousekey_get_report(void) {
  583. return mouse_report;
  584. }
  585. bool should_mousekey_report_send(report_mouse_t *mouse_report) {
  586. return mouse_report->x || mouse_report->y || mouse_report->v || mouse_report->h;
  587. }