mousekey.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. Copyright 2011 Jun Wako <wakojun@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdint.h>
  15. #include "keycode.h"
  16. #include "host.h"
  17. #include "timer.h"
  18. #include "print.h"
  19. #include "debug.h"
  20. #include "mousekey.h"
  21. inline int8_t times_inv_sqrt2(int8_t x) {
  22. // 181/256 is pretty close to 1/sqrt(2)
  23. // 0.70703125 0.707106781
  24. // 1 too small for x=99 and x=198
  25. // This ends up being a mult and discard lower 8 bits
  26. return (x * 181) >> 8;
  27. }
  28. static report_mouse_t mouse_report = {0};
  29. static void mousekey_debug(void);
  30. static uint8_t mousekey_accel = 0;
  31. static uint8_t mousekey_repeat = 0;
  32. static uint16_t last_timer = 0;
  33. #ifndef MK_3_SPEED
  34. static uint16_t last_timer_c = 0;
  35. static uint16_t last_timer_w = 0;
  36. /*
  37. * Mouse keys acceleration algorithm
  38. * http://en.wikipedia.org/wiki/Mouse_keys
  39. *
  40. * speed = delta * max_speed * (repeat / time_to_max)**((1000+curve)/1000)
  41. */
  42. /* milliseconds between the initial key press and first repeated motion event (0-2550) */
  43. uint8_t mk_delay = MOUSEKEY_DELAY / 10;
  44. /* milliseconds between repeated motion events (0-255) */
  45. uint8_t mk_interval = MOUSEKEY_INTERVAL;
  46. /* steady speed (in action_delta units) applied each event (0-255) */
  47. uint8_t mk_max_speed = MOUSEKEY_MAX_SPEED;
  48. /* number of events (count) accelerating to steady speed (0-255) */
  49. uint8_t mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
  50. /* ramp used to reach maximum pointer speed (NOT SUPPORTED) */
  51. // int8_t mk_curve = 0;
  52. /* wheel params */
  53. /* milliseconds between the initial key press and first repeated motion event (0-2550) */
  54. uint8_t mk_wheel_delay = MOUSEKEY_WHEEL_DELAY / 10;
  55. /* milliseconds between repeated motion events (0-255) */
  56. uint8_t mk_wheel_interval = MOUSEKEY_WHEEL_INTERVAL;
  57. uint8_t mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
  58. uint8_t mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
  59. static uint8_t move_unit(void) {
  60. uint16_t unit;
  61. if (mousekey_accel & (1 << 0)) {
  62. unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed) / 4;
  63. } else if (mousekey_accel & (1 << 1)) {
  64. unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed) / 2;
  65. } else if (mousekey_accel & (1 << 2)) {
  66. unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed);
  67. } else if (mousekey_repeat == 0) {
  68. unit = MOUSEKEY_MOVE_DELTA;
  69. } else if (mousekey_repeat >= mk_time_to_max) {
  70. unit = MOUSEKEY_MOVE_DELTA * mk_max_speed;
  71. } else {
  72. unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * mousekey_repeat) / mk_time_to_max;
  73. }
  74. return (unit > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : (unit == 0 ? 1 : unit));
  75. }
  76. static uint8_t wheel_unit(void) {
  77. uint16_t unit;
  78. if (mousekey_accel & (1 << 0)) {
  79. unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed) / 4;
  80. } else if (mousekey_accel & (1 << 1)) {
  81. unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed) / 2;
  82. } else if (mousekey_accel & (1 << 2)) {
  83. unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed);
  84. } else if (mousekey_repeat == 0) {
  85. unit = MOUSEKEY_WHEEL_DELTA;
  86. } else if (mousekey_repeat >= mk_wheel_time_to_max) {
  87. unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed;
  88. } else {
  89. unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_repeat) / mk_wheel_time_to_max;
  90. }
  91. return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : (unit == 0 ? 1 : unit));
  92. }
  93. void mousekey_task(void) {
  94. // report cursor and scroll movement independently
  95. report_mouse_t const tmpmr = mouse_report;
  96. if ((mouse_report.x || mouse_report.y) && timer_elapsed(last_timer_c) > (mousekey_repeat ? mk_interval : mk_delay * 10)) {
  97. if (mousekey_repeat != UINT8_MAX) mousekey_repeat++;
  98. mouse_report.v = 0;
  99. mouse_report.h = 0;
  100. if (mouse_report.x > 0) mouse_report.x = move_unit();
  101. if (mouse_report.x < 0) mouse_report.x = move_unit() * -1;
  102. if (mouse_report.y > 0) mouse_report.y = move_unit();
  103. if (mouse_report.y < 0) mouse_report.y = move_unit() * -1;
  104. /* diagonal move [1/sqrt(2)] */
  105. if (mouse_report.x && mouse_report.y) {
  106. mouse_report.x = times_inv_sqrt2(mouse_report.x);
  107. if (mouse_report.x == 0) {
  108. mouse_report.x = 1;
  109. }
  110. mouse_report.y = times_inv_sqrt2(mouse_report.y);
  111. if (mouse_report.y == 0) {
  112. mouse_report.y = 1;
  113. }
  114. }
  115. mousekey_send();
  116. last_timer_c = last_timer;
  117. mouse_report = tmpmr;
  118. }
  119. if ((mouse_report.v || mouse_report.h) && timer_elapsed(last_timer_w) > (mousekey_repeat ? mk_wheel_interval : mk_wheel_delay * 10)) {
  120. if (mousekey_repeat != UINT8_MAX) mousekey_repeat++;
  121. mouse_report.x = 0;
  122. mouse_report.y = 0;
  123. if (mouse_report.v > 0) mouse_report.v = wheel_unit();
  124. if (mouse_report.v < 0) mouse_report.v = wheel_unit() * -1;
  125. if (mouse_report.h > 0) mouse_report.h = wheel_unit();
  126. if (mouse_report.h < 0) mouse_report.h = wheel_unit() * -1;
  127. /* diagonal move [1/sqrt(2)] */
  128. if (mouse_report.v && mouse_report.h) {
  129. mouse_report.v = times_inv_sqrt2(mouse_report.v);
  130. if (mouse_report.v == 0) {
  131. mouse_report.v = 1;
  132. }
  133. mouse_report.h = times_inv_sqrt2(mouse_report.h);
  134. if (mouse_report.h == 0) {
  135. mouse_report.h = 1;
  136. }
  137. }
  138. mousekey_send();
  139. last_timer_w = last_timer;
  140. mouse_report = tmpmr;
  141. }
  142. }
  143. void mousekey_on(uint8_t code) {
  144. if (code == KC_MS_UP)
  145. mouse_report.y = move_unit() * -1;
  146. else if (code == KC_MS_DOWN)
  147. mouse_report.y = move_unit();
  148. else if (code == KC_MS_LEFT)
  149. mouse_report.x = move_unit() * -1;
  150. else if (code == KC_MS_RIGHT)
  151. mouse_report.x = move_unit();
  152. else if (code == KC_MS_WH_UP)
  153. mouse_report.v = wheel_unit();
  154. else if (code == KC_MS_WH_DOWN)
  155. mouse_report.v = wheel_unit() * -1;
  156. else if (code == KC_MS_WH_LEFT)
  157. mouse_report.h = wheel_unit() * -1;
  158. else if (code == KC_MS_WH_RIGHT)
  159. mouse_report.h = wheel_unit();
  160. else if (code == KC_MS_BTN1)
  161. mouse_report.buttons |= MOUSE_BTN1;
  162. else if (code == KC_MS_BTN2)
  163. mouse_report.buttons |= MOUSE_BTN2;
  164. else if (code == KC_MS_BTN3)
  165. mouse_report.buttons |= MOUSE_BTN3;
  166. else if (code == KC_MS_BTN4)
  167. mouse_report.buttons |= MOUSE_BTN4;
  168. else if (code == KC_MS_BTN5)
  169. mouse_report.buttons |= MOUSE_BTN5;
  170. else if (code == KC_MS_ACCEL0)
  171. mousekey_accel |= (1 << 0);
  172. else if (code == KC_MS_ACCEL1)
  173. mousekey_accel |= (1 << 1);
  174. else if (code == KC_MS_ACCEL2)
  175. mousekey_accel |= (1 << 2);
  176. }
  177. void mousekey_off(uint8_t code) {
  178. if (code == KC_MS_UP && mouse_report.y < 0)
  179. mouse_report.y = 0;
  180. else if (code == KC_MS_DOWN && mouse_report.y > 0)
  181. mouse_report.y = 0;
  182. else if (code == KC_MS_LEFT && mouse_report.x < 0)
  183. mouse_report.x = 0;
  184. else if (code == KC_MS_RIGHT && mouse_report.x > 0)
  185. mouse_report.x = 0;
  186. else if (code == KC_MS_WH_UP && mouse_report.v > 0)
  187. mouse_report.v = 0;
  188. else if (code == KC_MS_WH_DOWN && mouse_report.v < 0)
  189. mouse_report.v = 0;
  190. else if (code == KC_MS_WH_LEFT && mouse_report.h < 0)
  191. mouse_report.h = 0;
  192. else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0)
  193. mouse_report.h = 0;
  194. else if (code == KC_MS_BTN1)
  195. mouse_report.buttons &= ~MOUSE_BTN1;
  196. else if (code == KC_MS_BTN2)
  197. mouse_report.buttons &= ~MOUSE_BTN2;
  198. else if (code == KC_MS_BTN3)
  199. mouse_report.buttons &= ~MOUSE_BTN3;
  200. else if (code == KC_MS_BTN4)
  201. mouse_report.buttons &= ~MOUSE_BTN4;
  202. else if (code == KC_MS_BTN5)
  203. mouse_report.buttons &= ~MOUSE_BTN5;
  204. else if (code == KC_MS_ACCEL0)
  205. mousekey_accel &= ~(1 << 0);
  206. else if (code == KC_MS_ACCEL1)
  207. mousekey_accel &= ~(1 << 1);
  208. else if (code == KC_MS_ACCEL2)
  209. mousekey_accel &= ~(1 << 2);
  210. if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0) mousekey_repeat = 0;
  211. }
  212. #else /* #ifndef MK_3_SPEED */
  213. enum { mkspd_unmod, mkspd_0, mkspd_1, mkspd_2, mkspd_COUNT };
  214. # ifndef MK_MOMENTARY_ACCEL
  215. static uint8_t mk_speed = mkspd_1;
  216. # else
  217. static uint8_t mk_speed = mkspd_unmod;
  218. static uint8_t mkspd_DEFAULT = mkspd_unmod;
  219. # endif
  220. static uint16_t last_timer_c = 0;
  221. static uint16_t last_timer_w = 0;
  222. uint16_t c_offsets[mkspd_COUNT] = {MK_C_OFFSET_UNMOD, MK_C_OFFSET_0, MK_C_OFFSET_1, MK_C_OFFSET_2};
  223. uint16_t c_intervals[mkspd_COUNT] = {MK_C_INTERVAL_UNMOD, MK_C_INTERVAL_0, MK_C_INTERVAL_1, MK_C_INTERVAL_2};
  224. uint16_t w_offsets[mkspd_COUNT] = {MK_W_OFFSET_UNMOD, MK_W_OFFSET_0, MK_W_OFFSET_1, MK_W_OFFSET_2};
  225. uint16_t w_intervals[mkspd_COUNT] = {MK_W_INTERVAL_UNMOD, MK_W_INTERVAL_0, MK_W_INTERVAL_1, MK_W_INTERVAL_2};
  226. void mousekey_task(void) {
  227. // report cursor and scroll movement independently
  228. report_mouse_t const tmpmr = mouse_report;
  229. if ((mouse_report.x || mouse_report.y) && timer_elapsed(last_timer_c) > c_intervals[mk_speed]) {
  230. mouse_report.h = 0;
  231. mouse_report.v = 0;
  232. mousekey_send();
  233. last_timer_c = last_timer;
  234. mouse_report = tmpmr;
  235. }
  236. if ((mouse_report.h || mouse_report.v) && timer_elapsed(last_timer_w) > w_intervals[mk_speed]) {
  237. mouse_report.x = 0;
  238. mouse_report.y = 0;
  239. mousekey_send();
  240. last_timer_w = last_timer;
  241. mouse_report = tmpmr;
  242. }
  243. }
  244. void adjust_speed(void) {
  245. uint16_t const c_offset = c_offsets[mk_speed];
  246. uint16_t const w_offset = w_offsets[mk_speed];
  247. if (mouse_report.x > 0) mouse_report.x = c_offset;
  248. if (mouse_report.x < 0) mouse_report.x = c_offset * -1;
  249. if (mouse_report.y > 0) mouse_report.y = c_offset;
  250. if (mouse_report.y < 0) mouse_report.y = c_offset * -1;
  251. if (mouse_report.h > 0) mouse_report.h = w_offset;
  252. if (mouse_report.h < 0) mouse_report.h = w_offset * -1;
  253. if (mouse_report.v > 0) mouse_report.v = w_offset;
  254. if (mouse_report.v < 0) mouse_report.v = w_offset * -1;
  255. // adjust for diagonals
  256. if (mouse_report.x && mouse_report.y) {
  257. mouse_report.x = times_inv_sqrt2(mouse_report.x);
  258. if (mouse_report.x == 0) {
  259. mouse_report.x = 1;
  260. }
  261. mouse_report.y = times_inv_sqrt2(mouse_report.y);
  262. if (mouse_report.y == 0) {
  263. mouse_report.y = 1;
  264. }
  265. }
  266. if (mouse_report.h && mouse_report.v) {
  267. mouse_report.h = times_inv_sqrt2(mouse_report.h);
  268. mouse_report.v = times_inv_sqrt2(mouse_report.v);
  269. }
  270. }
  271. void mousekey_on(uint8_t code) {
  272. uint16_t const c_offset = c_offsets[mk_speed];
  273. uint16_t const w_offset = w_offsets[mk_speed];
  274. uint8_t const old_speed = mk_speed;
  275. if (code == KC_MS_UP)
  276. mouse_report.y = c_offset * -1;
  277. else if (code == KC_MS_DOWN)
  278. mouse_report.y = c_offset;
  279. else if (code == KC_MS_LEFT)
  280. mouse_report.x = c_offset * -1;
  281. else if (code == KC_MS_RIGHT)
  282. mouse_report.x = c_offset;
  283. else if (code == KC_MS_WH_UP)
  284. mouse_report.v = w_offset;
  285. else if (code == KC_MS_WH_DOWN)
  286. mouse_report.v = w_offset * -1;
  287. else if (code == KC_MS_WH_LEFT)
  288. mouse_report.h = w_offset * -1;
  289. else if (code == KC_MS_WH_RIGHT)
  290. mouse_report.h = w_offset;
  291. else if (code == KC_MS_BTN1)
  292. mouse_report.buttons |= MOUSE_BTN1;
  293. else if (code == KC_MS_BTN2)
  294. mouse_report.buttons |= MOUSE_BTN2;
  295. else if (code == KC_MS_BTN3)
  296. mouse_report.buttons |= MOUSE_BTN3;
  297. else if (code == KC_MS_BTN4)
  298. mouse_report.buttons |= MOUSE_BTN4;
  299. else if (code == KC_MS_BTN5)
  300. mouse_report.buttons |= MOUSE_BTN5;
  301. else if (code == KC_MS_ACCEL0)
  302. mk_speed = mkspd_0;
  303. else if (code == KC_MS_ACCEL1)
  304. mk_speed = mkspd_1;
  305. else if (code == KC_MS_ACCEL2)
  306. mk_speed = mkspd_2;
  307. if (mk_speed != old_speed) adjust_speed();
  308. }
  309. void mousekey_off(uint8_t code) {
  310. # ifdef MK_MOMENTARY_ACCEL
  311. uint8_t const old_speed = mk_speed;
  312. # endif
  313. if (code == KC_MS_UP && mouse_report.y < 0)
  314. mouse_report.y = 0;
  315. else if (code == KC_MS_DOWN && mouse_report.y > 0)
  316. mouse_report.y = 0;
  317. else if (code == KC_MS_LEFT && mouse_report.x < 0)
  318. mouse_report.x = 0;
  319. else if (code == KC_MS_RIGHT && mouse_report.x > 0)
  320. mouse_report.x = 0;
  321. else if (code == KC_MS_WH_UP && mouse_report.v > 0)
  322. mouse_report.v = 0;
  323. else if (code == KC_MS_WH_DOWN && mouse_report.v < 0)
  324. mouse_report.v = 0;
  325. else if (code == KC_MS_WH_LEFT && mouse_report.h < 0)
  326. mouse_report.h = 0;
  327. else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0)
  328. mouse_report.h = 0;
  329. else if (code == KC_MS_BTN1)
  330. mouse_report.buttons &= ~MOUSE_BTN1;
  331. else if (code == KC_MS_BTN2)
  332. mouse_report.buttons &= ~MOUSE_BTN2;
  333. else if (code == KC_MS_BTN3)
  334. mouse_report.buttons &= ~MOUSE_BTN3;
  335. else if (code == KC_MS_BTN4)
  336. mouse_report.buttons &= ~MOUSE_BTN4;
  337. else if (code == KC_MS_BTN5)
  338. mouse_report.buttons &= ~MOUSE_BTN5;
  339. # ifdef MK_MOMENTARY_ACCEL
  340. else if (code == KC_MS_ACCEL0)
  341. mk_speed = mkspd_DEFAULT;
  342. else if (code == KC_MS_ACCEL1)
  343. mk_speed = mkspd_DEFAULT;
  344. else if (code == KC_MS_ACCEL2)
  345. mk_speed = mkspd_DEFAULT;
  346. if (mk_speed != old_speed) adjust_speed();
  347. # endif
  348. }
  349. #endif /* #ifndef MK_3_SPEED */
  350. void mousekey_send(void) {
  351. mousekey_debug();
  352. host_mouse_send(&mouse_report);
  353. last_timer = timer_read();
  354. }
  355. void mousekey_clear(void) {
  356. mouse_report = (report_mouse_t){};
  357. mousekey_repeat = 0;
  358. mousekey_accel = 0;
  359. }
  360. static void mousekey_debug(void) {
  361. if (!debug_mouse) return;
  362. print("mousekey [btn|x y v h](rep/acl): [");
  363. phex(mouse_report.buttons);
  364. print("|");
  365. print_decs(mouse_report.x);
  366. print(" ");
  367. print_decs(mouse_report.y);
  368. print(" ");
  369. print_decs(mouse_report.v);
  370. print(" ");
  371. print_decs(mouse_report.h);
  372. print("](");
  373. print_dec(mousekey_repeat);
  374. print("/");
  375. print_dec(mousekey_accel);
  376. print(")\n");
  377. }