mousekey.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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 "keycode.h"
  19. #include "host.h"
  20. #include "timer.h"
  21. #include "print.h"
  22. #include "debug.h"
  23. #include "mousekey.h"
  24. inline int8_t times_inv_sqrt2(int8_t x) {
  25. // 181/256 is pretty close to 1/sqrt(2)
  26. // 0.70703125 0.707106781
  27. // 1 too small for x=99 and x=198
  28. // This ends up being a mult and discard lower 8 bits
  29. return (x * 181) >> 8;
  30. }
  31. static report_mouse_t mouse_report = {0};
  32. static void mousekey_debug(void);
  33. static uint8_t mousekey_accel = 0;
  34. static uint8_t mousekey_repeat = 0;
  35. static uint8_t mousekey_wheel_repeat = 0;
  36. #ifdef MK_KINETIC_SPEED
  37. static uint16_t mouse_timer = 0;
  38. #endif
  39. #ifndef MK_3_SPEED
  40. static uint16_t last_timer_c = 0;
  41. static uint16_t last_timer_w = 0;
  42. /*
  43. * Mouse keys acceleration algorithm
  44. * http://en.wikipedia.org/wiki/Mouse_keys
  45. *
  46. * speed = delta * max_speed * (repeat / time_to_max)**((1000+curve)/1000)
  47. */
  48. /* milliseconds between the initial key press and first repeated motion event (0-2550) */
  49. uint8_t mk_delay = MOUSEKEY_DELAY / 10;
  50. /* milliseconds between repeated motion events (0-255) */
  51. uint8_t mk_interval = MOUSEKEY_INTERVAL;
  52. /* steady speed (in action_delta units) applied each event (0-255) */
  53. uint8_t mk_max_speed = MOUSEKEY_MAX_SPEED;
  54. /* number of events (count) accelerating to steady speed (0-255) */
  55. uint8_t mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
  56. /* ramp used to reach maximum pointer speed (NOT SUPPORTED) */
  57. // int8_t mk_curve = 0;
  58. /* wheel params */
  59. /* milliseconds between the initial key press and first repeated motion event (0-2550) */
  60. uint8_t mk_wheel_delay = MOUSEKEY_WHEEL_DELAY / 10;
  61. /* milliseconds between repeated motion events (0-255) */
  62. uint8_t mk_wheel_interval = MOUSEKEY_WHEEL_INTERVAL;
  63. uint8_t mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
  64. uint8_t mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
  65. # ifndef MK_COMBINED
  66. static uint8_t move_unit(void) {
  67. uint16_t unit;
  68. if (mousekey_accel & (1 << 0)) {
  69. unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed) / 4;
  70. } else if (mousekey_accel & (1 << 1)) {
  71. unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed) / 2;
  72. } else if (mousekey_accel & (1 << 2)) {
  73. unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed);
  74. } else if (mousekey_repeat == 0) {
  75. unit = MOUSEKEY_MOVE_DELTA;
  76. } else if (mousekey_repeat >= mk_time_to_max) {
  77. unit = MOUSEKEY_MOVE_DELTA * mk_max_speed;
  78. } else {
  79. unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * mousekey_repeat) / mk_time_to_max;
  80. }
  81. return (unit > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : (unit == 0 ? 1 : unit));
  82. }
  83. static uint8_t wheel_unit(void) {
  84. uint16_t unit;
  85. if (mousekey_accel & (1 << 0)) {
  86. unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed) / 4;
  87. } else if (mousekey_accel & (1 << 1)) {
  88. unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed) / 2;
  89. } else if (mousekey_accel & (1 << 2)) {
  90. unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed);
  91. } else if (mousekey_wheel_repeat == 0) {
  92. unit = MOUSEKEY_WHEEL_DELTA;
  93. } else if (mousekey_wheel_repeat >= mk_wheel_time_to_max) {
  94. unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed;
  95. } else {
  96. unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_wheel_repeat) / mk_wheel_time_to_max;
  97. }
  98. return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : (unit == 0 ? 1 : unit));
  99. }
  100. # else /* #ifndef MK_COMBINED */
  101. # ifndef MK_KINETIC_SPEED
  102. /*
  103. * Kinetic movement acceleration algorithm
  104. *
  105. * current speed = I + A * T/50 + A * 0.5 * T^2 | maximum B
  106. *
  107. * T: time since the mouse movement started
  108. * E: mouse events per second (set through MOUSEKEY_INTERVAL, UHK sends 250, the
  109. * pro micro on my Signum 3.0 sends only 125!)
  110. * I: initial speed at time 0
  111. * A: acceleration
  112. * B: base mouse travel speed
  113. */
  114. const uint16_t mk_accelerated_speed = MOUSEKEY_ACCELERATED_SPEED;
  115. const uint16_t mk_base_speed = MOUSEKEY_BASE_SPEED;
  116. const uint16_t mk_decelerated_speed = MOUSEKEY_DECELERATED_SPEED;
  117. const uint16_t mk_initial_speed = MOUSEKEY_INITIAL_SPEED;
  118. static uint8_t move_unit(void) {
  119. float speed = mk_initial_speed;
  120. if (mousekey_accel & ((1 << 0) | (1 << 2))) {
  121. speed = mousekey_accel & (1 << 2) ? mk_accelerated_speed : mk_decelerated_speed;
  122. } else if (mousekey_repeat && mouse_timer) {
  123. const float time_elapsed = timer_elapsed(mouse_timer) / 50;
  124. speed = mk_initial_speed + MOUSEKEY_MOVE_DELTA * time_elapsed + MOUSEKEY_MOVE_DELTA * 0.5 * time_elapsed * time_elapsed;
  125. speed = speed > mk_base_speed ? mk_base_speed : speed;
  126. }
  127. /* convert speed to USB mouse speed 1 to 127 */
  128. speed = (uint8_t)(speed / (1000.0f / mk_interval));
  129. speed = speed < 1 ? 1 : speed;
  130. return speed > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : speed;
  131. }
  132. float mk_wheel_interval = 1000.0f / MOUSEKEY_WHEEL_INITIAL_MOVEMENTS;
  133. static uint8_t wheel_unit(void) {
  134. float speed = MOUSEKEY_WHEEL_INITIAL_MOVEMENTS;
  135. if (mousekey_accel & ((1 << 0) | (1 << 2))) {
  136. speed = mousekey_accel & (1 << 2) ? MOUSEKEY_WHEEL_ACCELERATED_MOVEMENTS : MOUSEKEY_WHEEL_DECELERATED_MOVEMENTS;
  137. } else if (mousekey_repeat && mouse_timer) {
  138. if (mk_wheel_interval != MOUSEKEY_WHEEL_BASE_MOVEMENTS) {
  139. const float time_elapsed = timer_elapsed(mouse_timer) / 50;
  140. speed = MOUSEKEY_WHEEL_INITIAL_MOVEMENTS + 1 * time_elapsed + 1 * 0.5 * time_elapsed * time_elapsed;
  141. }
  142. speed = speed > MOUSEKEY_WHEEL_BASE_MOVEMENTS ? MOUSEKEY_WHEEL_BASE_MOVEMENTS : speed;
  143. }
  144. mk_wheel_interval = 1000.0f / speed;
  145. return 1;
  146. }
  147. # else /* #ifndef MK_KINETIC_SPEED */
  148. static uint8_t move_unit(void) {
  149. uint16_t unit;
  150. if (mousekey_accel & (1 << 0)) {
  151. unit = 1;
  152. } else if (mousekey_accel & (1 << 1)) {
  153. unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed) / 2;
  154. } else if (mousekey_accel & (1 << 2)) {
  155. unit = MOUSEKEY_MOVE_MAX;
  156. } else if (mousekey_repeat == 0) {
  157. unit = MOUSEKEY_MOVE_DELTA;
  158. } else if (mousekey_repeat >= mk_time_to_max) {
  159. unit = MOUSEKEY_MOVE_DELTA * mk_max_speed;
  160. } else {
  161. unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * mousekey_repeat) / mk_time_to_max;
  162. }
  163. return (unit > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : (unit == 0 ? 1 : unit));
  164. }
  165. static uint8_t wheel_unit(void) {
  166. uint16_t unit;
  167. if (mousekey_accel & (1 << 0)) {
  168. unit = 1;
  169. } else if (mousekey_accel & (1 << 1)) {
  170. unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed) / 2;
  171. } else if (mousekey_accel & (1 << 2)) {
  172. unit = MOUSEKEY_WHEEL_MAX;
  173. } else if (mousekey_repeat == 0) {
  174. unit = MOUSEKEY_WHEEL_DELTA;
  175. } else if (mousekey_repeat >= mk_wheel_time_to_max) {
  176. unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed;
  177. } else {
  178. unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_repeat) / mk_wheel_time_to_max;
  179. }
  180. return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : (unit == 0 ? 1 : unit));
  181. }
  182. # endif /* #ifndef MK_KINETIC_SPEED */
  183. # endif /* #ifndef MK_COMBINED */
  184. void mousekey_task(void) {
  185. // report cursor and scroll movement independently
  186. report_mouse_t const tmpmr = mouse_report;
  187. mouse_report.x = 0;
  188. mouse_report.y = 0;
  189. mouse_report.v = 0;
  190. mouse_report.h = 0;
  191. if ((tmpmr.x || tmpmr.y) && timer_elapsed(last_timer_c) > (mousekey_repeat ? mk_interval : mk_delay * 10)) {
  192. if (mousekey_repeat != UINT8_MAX) mousekey_repeat++;
  193. if (tmpmr.x != 0) mouse_report.x = move_unit() * ((tmpmr.x > 0) ? 1 : -1);
  194. if (tmpmr.y != 0) mouse_report.y = move_unit() * ((tmpmr.y > 0) ? 1 : -1);
  195. /* diagonal move [1/sqrt(2)] */
  196. if (mouse_report.x && mouse_report.y) {
  197. mouse_report.x = times_inv_sqrt2(mouse_report.x);
  198. if (mouse_report.x == 0) {
  199. mouse_report.x = 1;
  200. }
  201. mouse_report.y = times_inv_sqrt2(mouse_report.y);
  202. if (mouse_report.y == 0) {
  203. mouse_report.y = 1;
  204. }
  205. }
  206. }
  207. if ((tmpmr.v || tmpmr.h) && timer_elapsed(last_timer_w) > (mousekey_wheel_repeat ? mk_wheel_interval : mk_wheel_delay * 10)) {
  208. if (mousekey_wheel_repeat != UINT8_MAX) mousekey_wheel_repeat++;
  209. if (tmpmr.v != 0) mouse_report.v = wheel_unit() * ((tmpmr.v > 0) ? 1 : -1);
  210. if (tmpmr.h != 0) mouse_report.h = wheel_unit() * ((tmpmr.h > 0) ? 1 : -1);
  211. /* diagonal move [1/sqrt(2)] */
  212. if (mouse_report.v && mouse_report.h) {
  213. mouse_report.v = times_inv_sqrt2(mouse_report.v);
  214. if (mouse_report.v == 0) {
  215. mouse_report.v = 1;
  216. }
  217. mouse_report.h = times_inv_sqrt2(mouse_report.h);
  218. if (mouse_report.h == 0) {
  219. mouse_report.h = 1;
  220. }
  221. }
  222. }
  223. if (mouse_report.x || mouse_report.y || mouse_report.v || mouse_report.h) mousekey_send();
  224. mouse_report = tmpmr;
  225. }
  226. void mousekey_on(uint8_t code) {
  227. # ifdef MK_KINETIC_SPEED
  228. if (mouse_timer == 0) {
  229. mouse_timer = timer_read();
  230. }
  231. # endif /* #ifdef MK_KINETIC_SPEED */
  232. if (code == KC_MS_UP)
  233. mouse_report.y = move_unit() * -1;
  234. else if (code == KC_MS_DOWN)
  235. mouse_report.y = move_unit();
  236. else if (code == KC_MS_LEFT)
  237. mouse_report.x = move_unit() * -1;
  238. else if (code == KC_MS_RIGHT)
  239. mouse_report.x = move_unit();
  240. else if (code == KC_MS_WH_UP)
  241. mouse_report.v = wheel_unit();
  242. else if (code == KC_MS_WH_DOWN)
  243. mouse_report.v = wheel_unit() * -1;
  244. else if (code == KC_MS_WH_LEFT)
  245. mouse_report.h = wheel_unit() * -1;
  246. else if (code == KC_MS_WH_RIGHT)
  247. mouse_report.h = wheel_unit();
  248. else if (code == KC_MS_BTN1)
  249. mouse_report.buttons |= MOUSE_BTN1;
  250. else if (code == KC_MS_BTN2)
  251. mouse_report.buttons |= MOUSE_BTN2;
  252. else if (code == KC_MS_BTN3)
  253. mouse_report.buttons |= MOUSE_BTN3;
  254. else if (code == KC_MS_BTN4)
  255. mouse_report.buttons |= MOUSE_BTN4;
  256. else if (code == KC_MS_BTN5)
  257. mouse_report.buttons |= MOUSE_BTN5;
  258. else if (code == KC_MS_ACCEL0)
  259. mousekey_accel |= (1 << 0);
  260. else if (code == KC_MS_ACCEL1)
  261. mousekey_accel |= (1 << 1);
  262. else if (code == KC_MS_ACCEL2)
  263. mousekey_accel |= (1 << 2);
  264. }
  265. void mousekey_off(uint8_t code) {
  266. if (code == KC_MS_UP && mouse_report.y < 0)
  267. mouse_report.y = 0;
  268. else if (code == KC_MS_DOWN && mouse_report.y > 0)
  269. mouse_report.y = 0;
  270. else if (code == KC_MS_LEFT && mouse_report.x < 0)
  271. mouse_report.x = 0;
  272. else if (code == KC_MS_RIGHT && mouse_report.x > 0)
  273. mouse_report.x = 0;
  274. else if (code == KC_MS_WH_UP && mouse_report.v > 0)
  275. mouse_report.v = 0;
  276. else if (code == KC_MS_WH_DOWN && mouse_report.v < 0)
  277. mouse_report.v = 0;
  278. else if (code == KC_MS_WH_LEFT && mouse_report.h < 0)
  279. mouse_report.h = 0;
  280. else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0)
  281. mouse_report.h = 0;
  282. else if (code == KC_MS_BTN1)
  283. mouse_report.buttons &= ~MOUSE_BTN1;
  284. else if (code == KC_MS_BTN2)
  285. mouse_report.buttons &= ~MOUSE_BTN2;
  286. else if (code == KC_MS_BTN3)
  287. mouse_report.buttons &= ~MOUSE_BTN3;
  288. else if (code == KC_MS_BTN4)
  289. mouse_report.buttons &= ~MOUSE_BTN4;
  290. else if (code == KC_MS_BTN5)
  291. mouse_report.buttons &= ~MOUSE_BTN5;
  292. else if (code == KC_MS_ACCEL0)
  293. mousekey_accel &= ~(1 << 0);
  294. else if (code == KC_MS_ACCEL1)
  295. mousekey_accel &= ~(1 << 1);
  296. else if (code == KC_MS_ACCEL2)
  297. mousekey_accel &= ~(1 << 2);
  298. if (mouse_report.x == 0 && mouse_report.y == 0) {
  299. mousekey_repeat = 0;
  300. # ifdef MK_KINETIC_SPEED
  301. mouse_timer = 0;
  302. # endif /* #ifdef MK_KINETIC_SPEED */
  303. }
  304. if (mouse_report.v == 0 && mouse_report.h == 0) mousekey_wheel_repeat = 0;
  305. }
  306. #else /* #ifndef MK_3_SPEED */
  307. enum { mkspd_unmod, mkspd_0, mkspd_1, mkspd_2, mkspd_COUNT };
  308. # ifndef MK_MOMENTARY_ACCEL
  309. static uint8_t mk_speed = mkspd_1;
  310. # else
  311. static uint8_t mk_speed = mkspd_unmod;
  312. static uint8_t mkspd_DEFAULT = mkspd_unmod;
  313. # endif
  314. static uint16_t last_timer_c = 0;
  315. static uint16_t last_timer_w = 0;
  316. uint16_t c_offsets[mkspd_COUNT] = {MK_C_OFFSET_UNMOD, MK_C_OFFSET_0, MK_C_OFFSET_1, MK_C_OFFSET_2};
  317. uint16_t c_intervals[mkspd_COUNT] = {MK_C_INTERVAL_UNMOD, MK_C_INTERVAL_0, MK_C_INTERVAL_1, MK_C_INTERVAL_2};
  318. uint16_t w_offsets[mkspd_COUNT] = {MK_W_OFFSET_UNMOD, MK_W_OFFSET_0, MK_W_OFFSET_1, MK_W_OFFSET_2};
  319. uint16_t w_intervals[mkspd_COUNT] = {MK_W_INTERVAL_UNMOD, MK_W_INTERVAL_0, MK_W_INTERVAL_1, MK_W_INTERVAL_2};
  320. void mousekey_task(void) {
  321. // report cursor and scroll movement independently
  322. report_mouse_t const tmpmr = mouse_report;
  323. mouse_report.x = 0;
  324. mouse_report.y = 0;
  325. mouse_report.v = 0;
  326. mouse_report.h = 0;
  327. if ((tmpmr.x || tmpmr.y) && timer_elapsed(last_timer_c) > c_intervals[mk_speed]) {
  328. mouse_report.x = tmpmr.x;
  329. mouse_report.y = tmpmr.y;
  330. }
  331. if ((tmpmr.h || tmpmr.v) && timer_elapsed(last_timer_w) > w_intervals[mk_speed]) {
  332. mouse_report.v = tmpmr.v;
  333. mouse_report.h = tmpmr.h;
  334. }
  335. if (mouse_report.x || mouse_report.y || mouse_report.v || mouse_report.h) mousekey_send();
  336. mouse_report = tmpmr;
  337. }
  338. void adjust_speed(void) {
  339. uint16_t const c_offset = c_offsets[mk_speed];
  340. uint16_t const w_offset = w_offsets[mk_speed];
  341. if (mouse_report.x > 0) mouse_report.x = c_offset;
  342. if (mouse_report.x < 0) mouse_report.x = c_offset * -1;
  343. if (mouse_report.y > 0) mouse_report.y = c_offset;
  344. if (mouse_report.y < 0) mouse_report.y = c_offset * -1;
  345. if (mouse_report.h > 0) mouse_report.h = w_offset;
  346. if (mouse_report.h < 0) mouse_report.h = w_offset * -1;
  347. if (mouse_report.v > 0) mouse_report.v = w_offset;
  348. if (mouse_report.v < 0) mouse_report.v = w_offset * -1;
  349. // adjust for diagonals
  350. if (mouse_report.x && mouse_report.y) {
  351. mouse_report.x = times_inv_sqrt2(mouse_report.x);
  352. if (mouse_report.x == 0) {
  353. mouse_report.x = 1;
  354. }
  355. mouse_report.y = times_inv_sqrt2(mouse_report.y);
  356. if (mouse_report.y == 0) {
  357. mouse_report.y = 1;
  358. }
  359. }
  360. if (mouse_report.h && mouse_report.v) {
  361. mouse_report.h = times_inv_sqrt2(mouse_report.h);
  362. mouse_report.v = times_inv_sqrt2(mouse_report.v);
  363. }
  364. }
  365. void mousekey_on(uint8_t code) {
  366. uint16_t const c_offset = c_offsets[mk_speed];
  367. uint16_t const w_offset = w_offsets[mk_speed];
  368. uint8_t const old_speed = mk_speed;
  369. if (code == KC_MS_UP)
  370. mouse_report.y = c_offset * -1;
  371. else if (code == KC_MS_DOWN)
  372. mouse_report.y = c_offset;
  373. else if (code == KC_MS_LEFT)
  374. mouse_report.x = c_offset * -1;
  375. else if (code == KC_MS_RIGHT)
  376. mouse_report.x = c_offset;
  377. else if (code == KC_MS_WH_UP)
  378. mouse_report.v = w_offset;
  379. else if (code == KC_MS_WH_DOWN)
  380. mouse_report.v = w_offset * -1;
  381. else if (code == KC_MS_WH_LEFT)
  382. mouse_report.h = w_offset * -1;
  383. else if (code == KC_MS_WH_RIGHT)
  384. mouse_report.h = w_offset;
  385. else if (code == KC_MS_BTN1)
  386. mouse_report.buttons |= MOUSE_BTN1;
  387. else if (code == KC_MS_BTN2)
  388. mouse_report.buttons |= MOUSE_BTN2;
  389. else if (code == KC_MS_BTN3)
  390. mouse_report.buttons |= MOUSE_BTN3;
  391. else if (code == KC_MS_BTN4)
  392. mouse_report.buttons |= MOUSE_BTN4;
  393. else if (code == KC_MS_BTN5)
  394. mouse_report.buttons |= MOUSE_BTN5;
  395. else if (code == KC_MS_ACCEL0)
  396. mk_speed = mkspd_0;
  397. else if (code == KC_MS_ACCEL1)
  398. mk_speed = mkspd_1;
  399. else if (code == KC_MS_ACCEL2)
  400. mk_speed = mkspd_2;
  401. if (mk_speed != old_speed) adjust_speed();
  402. }
  403. void mousekey_off(uint8_t code) {
  404. # ifdef MK_MOMENTARY_ACCEL
  405. uint8_t const old_speed = mk_speed;
  406. # endif
  407. if (code == KC_MS_UP && mouse_report.y < 0)
  408. mouse_report.y = 0;
  409. else if (code == KC_MS_DOWN && mouse_report.y > 0)
  410. mouse_report.y = 0;
  411. else if (code == KC_MS_LEFT && mouse_report.x < 0)
  412. mouse_report.x = 0;
  413. else if (code == KC_MS_RIGHT && mouse_report.x > 0)
  414. mouse_report.x = 0;
  415. else if (code == KC_MS_WH_UP && mouse_report.v > 0)
  416. mouse_report.v = 0;
  417. else if (code == KC_MS_WH_DOWN && mouse_report.v < 0)
  418. mouse_report.v = 0;
  419. else if (code == KC_MS_WH_LEFT && mouse_report.h < 0)
  420. mouse_report.h = 0;
  421. else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0)
  422. mouse_report.h = 0;
  423. else if (code == KC_MS_BTN1)
  424. mouse_report.buttons &= ~MOUSE_BTN1;
  425. else if (code == KC_MS_BTN2)
  426. mouse_report.buttons &= ~MOUSE_BTN2;
  427. else if (code == KC_MS_BTN3)
  428. mouse_report.buttons &= ~MOUSE_BTN3;
  429. else if (code == KC_MS_BTN4)
  430. mouse_report.buttons &= ~MOUSE_BTN4;
  431. else if (code == KC_MS_BTN5)
  432. mouse_report.buttons &= ~MOUSE_BTN5;
  433. # ifdef MK_MOMENTARY_ACCEL
  434. else if (code == KC_MS_ACCEL0)
  435. mk_speed = mkspd_DEFAULT;
  436. else if (code == KC_MS_ACCEL1)
  437. mk_speed = mkspd_DEFAULT;
  438. else if (code == KC_MS_ACCEL2)
  439. mk_speed = mkspd_DEFAULT;
  440. if (mk_speed != old_speed) adjust_speed();
  441. # endif
  442. }
  443. #endif /* #ifndef MK_3_SPEED */
  444. void mousekey_send(void) {
  445. mousekey_debug();
  446. uint16_t time = timer_read();
  447. if (mouse_report.x || mouse_report.y) last_timer_c = time;
  448. if (mouse_report.v || mouse_report.h) last_timer_w = time;
  449. host_mouse_send(&mouse_report);
  450. }
  451. void mousekey_clear(void) {
  452. mouse_report = (report_mouse_t){};
  453. mousekey_repeat = 0;
  454. mousekey_wheel_repeat = 0;
  455. mousekey_accel = 0;
  456. }
  457. static void mousekey_debug(void) {
  458. if (!debug_mouse) return;
  459. print("mousekey [btn|x y v h](rep/acl): [");
  460. phex(mouse_report.buttons);
  461. print("|");
  462. print_decs(mouse_report.x);
  463. print(" ");
  464. print_decs(mouse_report.y);
  465. print(" ");
  466. print_decs(mouse_report.v);
  467. print(" ");
  468. print_decs(mouse_report.h);
  469. print("](");
  470. print_dec(mousekey_repeat);
  471. print("/");
  472. print_dec(mousekey_accel);
  473. print(")\n");
  474. }