mousekey.c 18 KB

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