mousekey.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. #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_wheel_repeat == 0) {
  89. unit = MOUSEKEY_WHEEL_DELTA;
  90. } else if (mousekey_wheel_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_wheel_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. mouse_report.x = 0;
  137. mouse_report.y = 0;
  138. mouse_report.v = 0;
  139. mouse_report.h = 0;
  140. if ((tmpmr.x || tmpmr.y) && timer_elapsed(last_timer_c) > (mousekey_repeat ? mk_interval : mk_delay * 10)) {
  141. if (mousekey_repeat != UINT8_MAX) mousekey_repeat++;
  142. if (tmpmr.x != 0) mouse_report.x = move_unit() * ((tmpmr.x > 0) ? 1 : -1);
  143. if (tmpmr.y != 0) mouse_report.y = move_unit() * ((tmpmr.y > 0) ? 1 : -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. }
  156. if ((tmpmr.v || tmpmr.h) && timer_elapsed(last_timer_w) > (mousekey_wheel_repeat ? mk_wheel_interval : mk_wheel_delay * 10)) {
  157. if (mousekey_wheel_repeat != UINT8_MAX) mousekey_wheel_repeat++;
  158. if (tmpmr.v != 0) mouse_report.v = wheel_unit() * ((tmpmr.v > 0) ? 1 : -1);
  159. if (tmpmr.h != 0) mouse_report.h = wheel_unit() * ((tmpmr.h > 0) ? 1 : -1);
  160. /* diagonal move [1/sqrt(2)] */
  161. if (mouse_report.v && mouse_report.h) {
  162. mouse_report.v = times_inv_sqrt2(mouse_report.v);
  163. if (mouse_report.v == 0) {
  164. mouse_report.v = 1;
  165. }
  166. mouse_report.h = times_inv_sqrt2(mouse_report.h);
  167. if (mouse_report.h == 0) {
  168. mouse_report.h = 1;
  169. }
  170. }
  171. }
  172. if (mouse_report.x || mouse_report.y || mouse_report.v || mouse_report.h) mousekey_send();
  173. mouse_report = tmpmr;
  174. }
  175. void mousekey_on(uint8_t code) {
  176. if (code == KC_MS_UP)
  177. mouse_report.y = move_unit() * -1;
  178. else if (code == KC_MS_DOWN)
  179. mouse_report.y = move_unit();
  180. else if (code == KC_MS_LEFT)
  181. mouse_report.x = move_unit() * -1;
  182. else if (code == KC_MS_RIGHT)
  183. mouse_report.x = move_unit();
  184. else if (code == KC_MS_WH_UP)
  185. mouse_report.v = wheel_unit();
  186. else if (code == KC_MS_WH_DOWN)
  187. mouse_report.v = wheel_unit() * -1;
  188. else if (code == KC_MS_WH_LEFT)
  189. mouse_report.h = wheel_unit() * -1;
  190. else if (code == KC_MS_WH_RIGHT)
  191. mouse_report.h = wheel_unit();
  192. else if (code == KC_MS_BTN1)
  193. mouse_report.buttons |= MOUSE_BTN1;
  194. else if (code == KC_MS_BTN2)
  195. mouse_report.buttons |= MOUSE_BTN2;
  196. else if (code == KC_MS_BTN3)
  197. mouse_report.buttons |= MOUSE_BTN3;
  198. else if (code == KC_MS_BTN4)
  199. mouse_report.buttons |= MOUSE_BTN4;
  200. else if (code == KC_MS_BTN5)
  201. mouse_report.buttons |= MOUSE_BTN5;
  202. else if (code == KC_MS_ACCEL0)
  203. mousekey_accel |= (1 << 0);
  204. else if (code == KC_MS_ACCEL1)
  205. mousekey_accel |= (1 << 1);
  206. else if (code == KC_MS_ACCEL2)
  207. mousekey_accel |= (1 << 2);
  208. }
  209. void mousekey_off(uint8_t code) {
  210. if (code == KC_MS_UP && mouse_report.y < 0)
  211. mouse_report.y = 0;
  212. else if (code == KC_MS_DOWN && mouse_report.y > 0)
  213. mouse_report.y = 0;
  214. else if (code == KC_MS_LEFT && mouse_report.x < 0)
  215. mouse_report.x = 0;
  216. else if (code == KC_MS_RIGHT && mouse_report.x > 0)
  217. mouse_report.x = 0;
  218. else if (code == KC_MS_WH_UP && mouse_report.v > 0)
  219. mouse_report.v = 0;
  220. else if (code == KC_MS_WH_DOWN && mouse_report.v < 0)
  221. mouse_report.v = 0;
  222. else if (code == KC_MS_WH_LEFT && mouse_report.h < 0)
  223. mouse_report.h = 0;
  224. else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0)
  225. mouse_report.h = 0;
  226. else if (code == KC_MS_BTN1)
  227. mouse_report.buttons &= ~MOUSE_BTN1;
  228. else if (code == KC_MS_BTN2)
  229. mouse_report.buttons &= ~MOUSE_BTN2;
  230. else if (code == KC_MS_BTN3)
  231. mouse_report.buttons &= ~MOUSE_BTN3;
  232. else if (code == KC_MS_BTN4)
  233. mouse_report.buttons &= ~MOUSE_BTN4;
  234. else if (code == KC_MS_BTN5)
  235. mouse_report.buttons &= ~MOUSE_BTN5;
  236. else if (code == KC_MS_ACCEL0)
  237. mousekey_accel &= ~(1 << 0);
  238. else if (code == KC_MS_ACCEL1)
  239. mousekey_accel &= ~(1 << 1);
  240. else if (code == KC_MS_ACCEL2)
  241. mousekey_accel &= ~(1 << 2);
  242. if (mouse_report.x == 0 && mouse_report.y == 0) mousekey_repeat = 0;
  243. if (mouse_report.v == 0 && mouse_report.h == 0) mousekey_wheel_repeat = 0;
  244. }
  245. #else /* #ifndef MK_3_SPEED */
  246. enum { mkspd_unmod, mkspd_0, mkspd_1, mkspd_2, mkspd_COUNT };
  247. # ifndef MK_MOMENTARY_ACCEL
  248. static uint8_t mk_speed = mkspd_1;
  249. # else
  250. static uint8_t mk_speed = mkspd_unmod;
  251. static uint8_t mkspd_DEFAULT = mkspd_unmod;
  252. # endif
  253. static uint16_t last_timer_c = 0;
  254. static uint16_t last_timer_w = 0;
  255. uint16_t c_offsets[mkspd_COUNT] = {MK_C_OFFSET_UNMOD, MK_C_OFFSET_0, MK_C_OFFSET_1, MK_C_OFFSET_2};
  256. uint16_t c_intervals[mkspd_COUNT] = {MK_C_INTERVAL_UNMOD, MK_C_INTERVAL_0, MK_C_INTERVAL_1, MK_C_INTERVAL_2};
  257. uint16_t w_offsets[mkspd_COUNT] = {MK_W_OFFSET_UNMOD, MK_W_OFFSET_0, MK_W_OFFSET_1, MK_W_OFFSET_2};
  258. uint16_t w_intervals[mkspd_COUNT] = {MK_W_INTERVAL_UNMOD, MK_W_INTERVAL_0, MK_W_INTERVAL_1, MK_W_INTERVAL_2};
  259. void mousekey_task(void) {
  260. // report cursor and scroll movement independently
  261. report_mouse_t const tmpmr = mouse_report;
  262. mouse_report.x = 0;
  263. mouse_report.y = 0;
  264. mouse_report.v = 0;
  265. mouse_report.h = 0;
  266. if ((tmpmr.x || tmpmr.y) && timer_elapsed(last_timer_c) > c_intervals[mk_speed]) {
  267. mouse_report.x = tmpmr.x;
  268. mouse_report.y = tmpmr.y;
  269. }
  270. if ((tmpmr.h || tmpmr.v) && timer_elapsed(last_timer_w) > w_intervals[mk_speed]) {
  271. mouse_report.v = tmpmr.v;
  272. mouse_report.h = tmpmr.h;
  273. }
  274. if (mouse_report.x || mouse_report.y || mouse_report.v || mouse_report.h) mousekey_send();
  275. mouse_report = tmpmr;
  276. }
  277. void adjust_speed(void) {
  278. uint16_t const c_offset = c_offsets[mk_speed];
  279. uint16_t const w_offset = w_offsets[mk_speed];
  280. if (mouse_report.x > 0) mouse_report.x = c_offset;
  281. if (mouse_report.x < 0) mouse_report.x = c_offset * -1;
  282. if (mouse_report.y > 0) mouse_report.y = c_offset;
  283. if (mouse_report.y < 0) mouse_report.y = c_offset * -1;
  284. if (mouse_report.h > 0) mouse_report.h = w_offset;
  285. if (mouse_report.h < 0) mouse_report.h = w_offset * -1;
  286. if (mouse_report.v > 0) mouse_report.v = w_offset;
  287. if (mouse_report.v < 0) mouse_report.v = w_offset * -1;
  288. // adjust for diagonals
  289. if (mouse_report.x && mouse_report.y) {
  290. mouse_report.x = times_inv_sqrt2(mouse_report.x);
  291. if (mouse_report.x == 0) {
  292. mouse_report.x = 1;
  293. }
  294. mouse_report.y = times_inv_sqrt2(mouse_report.y);
  295. if (mouse_report.y == 0) {
  296. mouse_report.y = 1;
  297. }
  298. }
  299. if (mouse_report.h && mouse_report.v) {
  300. mouse_report.h = times_inv_sqrt2(mouse_report.h);
  301. mouse_report.v = times_inv_sqrt2(mouse_report.v);
  302. }
  303. }
  304. void mousekey_on(uint8_t code) {
  305. uint16_t const c_offset = c_offsets[mk_speed];
  306. uint16_t const w_offset = w_offsets[mk_speed];
  307. uint8_t const old_speed = mk_speed;
  308. if (code == KC_MS_UP)
  309. mouse_report.y = c_offset * -1;
  310. else if (code == KC_MS_DOWN)
  311. mouse_report.y = c_offset;
  312. else if (code == KC_MS_LEFT)
  313. mouse_report.x = c_offset * -1;
  314. else if (code == KC_MS_RIGHT)
  315. mouse_report.x = c_offset;
  316. else if (code == KC_MS_WH_UP)
  317. mouse_report.v = w_offset;
  318. else if (code == KC_MS_WH_DOWN)
  319. mouse_report.v = w_offset * -1;
  320. else if (code == KC_MS_WH_LEFT)
  321. mouse_report.h = w_offset * -1;
  322. else if (code == KC_MS_WH_RIGHT)
  323. mouse_report.h = w_offset;
  324. else if (code == KC_MS_BTN1)
  325. mouse_report.buttons |= MOUSE_BTN1;
  326. else if (code == KC_MS_BTN2)
  327. mouse_report.buttons |= MOUSE_BTN2;
  328. else if (code == KC_MS_BTN3)
  329. mouse_report.buttons |= MOUSE_BTN3;
  330. else if (code == KC_MS_BTN4)
  331. mouse_report.buttons |= MOUSE_BTN4;
  332. else if (code == KC_MS_BTN5)
  333. mouse_report.buttons |= MOUSE_BTN5;
  334. else if (code == KC_MS_ACCEL0)
  335. mk_speed = mkspd_0;
  336. else if (code == KC_MS_ACCEL1)
  337. mk_speed = mkspd_1;
  338. else if (code == KC_MS_ACCEL2)
  339. mk_speed = mkspd_2;
  340. if (mk_speed != old_speed) adjust_speed();
  341. }
  342. void mousekey_off(uint8_t code) {
  343. # ifdef MK_MOMENTARY_ACCEL
  344. uint8_t const old_speed = mk_speed;
  345. # endif
  346. if (code == KC_MS_UP && mouse_report.y < 0)
  347. mouse_report.y = 0;
  348. else if (code == KC_MS_DOWN && mouse_report.y > 0)
  349. mouse_report.y = 0;
  350. else if (code == KC_MS_LEFT && mouse_report.x < 0)
  351. mouse_report.x = 0;
  352. else if (code == KC_MS_RIGHT && mouse_report.x > 0)
  353. mouse_report.x = 0;
  354. else if (code == KC_MS_WH_UP && mouse_report.v > 0)
  355. mouse_report.v = 0;
  356. else if (code == KC_MS_WH_DOWN && mouse_report.v < 0)
  357. mouse_report.v = 0;
  358. else if (code == KC_MS_WH_LEFT && mouse_report.h < 0)
  359. mouse_report.h = 0;
  360. else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0)
  361. mouse_report.h = 0;
  362. else if (code == KC_MS_BTN1)
  363. mouse_report.buttons &= ~MOUSE_BTN1;
  364. else if (code == KC_MS_BTN2)
  365. mouse_report.buttons &= ~MOUSE_BTN2;
  366. else if (code == KC_MS_BTN3)
  367. mouse_report.buttons &= ~MOUSE_BTN3;
  368. else if (code == KC_MS_BTN4)
  369. mouse_report.buttons &= ~MOUSE_BTN4;
  370. else if (code == KC_MS_BTN5)
  371. mouse_report.buttons &= ~MOUSE_BTN5;
  372. # ifdef MK_MOMENTARY_ACCEL
  373. else if (code == KC_MS_ACCEL0)
  374. mk_speed = mkspd_DEFAULT;
  375. else if (code == KC_MS_ACCEL1)
  376. mk_speed = mkspd_DEFAULT;
  377. else if (code == KC_MS_ACCEL2)
  378. mk_speed = mkspd_DEFAULT;
  379. if (mk_speed != old_speed) adjust_speed();
  380. # endif
  381. }
  382. #endif /* #ifndef MK_3_SPEED */
  383. void mousekey_send(void) {
  384. mousekey_debug();
  385. uint16_t time = timer_read();
  386. if (mouse_report.x || mouse_report.y) last_timer_c = time;
  387. if (mouse_report.v || mouse_report.h) last_timer_w = time;
  388. host_mouse_send(&mouse_report);
  389. }
  390. void mousekey_clear(void) {
  391. mouse_report = (report_mouse_t){};
  392. mousekey_repeat = 0;
  393. mousekey_wheel_repeat = 0;
  394. mousekey_accel = 0;
  395. }
  396. static void mousekey_debug(void) {
  397. if (!debug_mouse) return;
  398. print("mousekey [btn|x y v h](rep/acl): [");
  399. phex(mouse_report.buttons);
  400. print("|");
  401. print_decs(mouse_report.x);
  402. print(" ");
  403. print_decs(mouse_report.y);
  404. print(" ");
  405. print_decs(mouse_report.v);
  406. print(" ");
  407. print_decs(mouse_report.h);
  408. print("](");
  409. print_dec(mousekey_repeat);
  410. print("/");
  411. print_dec(mousekey_accel);
  412. print(")\n");
  413. }