mousekey.c 16 KB

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