mousekey.c 18 KB

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