You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

680 lines
24 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  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. static inline int8_t times_inv_sqrt2(int8_t x) {
  26. // 181/256 (0.70703125) is used as an approximation for 1/sqrt(2)
  27. // because it is close to the exact value which is 0.707106781
  28. const int16_t n = x * 181;
  29. const uint16_t d = 256;
  30. // To ensure that the integer result is rounded accurately after
  31. // division, check the sign of the numerator:
  32. // If negative, subtract half of the denominator before dividing
  33. // Otherwise, add half of the denominator before dividing
  34. return n < 0 ? (n - d / 2) / d : (n + d / 2) / d;
  35. }
  36. static report_mouse_t mouse_report = {0};
  37. static void mousekey_debug(void);
  38. static uint8_t mousekey_accel = 0;
  39. static uint8_t mousekey_repeat = 0;
  40. static uint8_t mousekey_wheel_repeat = 0;
  41. #ifdef MOUSEKEY_INERTIA
  42. static uint8_t mousekey_frame = 0; // track whether gesture is inactive, first frame, or repeating
  43. static int8_t mousekey_x_dir = 0; // -1 / 0 / 1 = left / neutral / right
  44. static int8_t mousekey_y_dir = 0; // -1 / 0 / 0 = up / neutral / down
  45. static int8_t mousekey_x_inertia = 0; // current velocity, limit +/- MOUSEKEY_TIME_TO_MAX
  46. static int8_t mousekey_y_inertia = 0; // ...
  47. #endif
  48. #ifdef MK_KINETIC_SPEED
  49. static uint16_t mouse_timer = 0;
  50. #endif
  51. #ifndef MK_3_SPEED
  52. static uint16_t last_timer_c = 0;
  53. static uint16_t last_timer_w = 0;
  54. /*
  55. * Mouse keys acceleration algorithm
  56. * http://en.wikipedia.org/wiki/Mouse_keys
  57. *
  58. * speed = delta * max_speed * (repeat / time_to_max)**((1000+curve)/1000)
  59. */
  60. /* milliseconds between the initial key press and first repeated motion event (0-2550) */
  61. uint8_t mk_delay = MOUSEKEY_DELAY / 10;
  62. /* milliseconds between repeated motion events (0-255) */
  63. uint8_t mk_interval = MOUSEKEY_INTERVAL;
  64. /* steady speed (in action_delta units) applied each event (0-255) */
  65. uint8_t mk_max_speed = MOUSEKEY_MAX_SPEED;
  66. /* number of events (count) accelerating to steady speed (0-255) */
  67. uint8_t mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
  68. /* ramp used to reach maximum pointer speed (NOT SUPPORTED) */
  69. // int8_t mk_curve = 0;
  70. /* wheel params */
  71. /* milliseconds between the initial key press and first repeated motion event (0-2550) */
  72. uint8_t mk_wheel_delay = MOUSEKEY_WHEEL_DELAY / 10;
  73. /* milliseconds between repeated motion events (0-255) */
  74. # ifdef MK_KINETIC_SPEED
  75. uint16_t mk_wheel_interval = 1000U / MOUSEKEY_WHEEL_INITIAL_MOVEMENTS;
  76. # else
  77. uint8_t mk_wheel_interval = MOUSEKEY_WHEEL_INTERVAL;
  78. # endif
  79. uint8_t mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
  80. uint8_t mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
  81. # ifndef MK_COMBINED
  82. # ifndef MK_KINETIC_SPEED
  83. # ifndef MOUSEKEY_INERTIA
  84. /* Default accelerated mode */
  85. static uint8_t move_unit(void) {
  86. uint16_t unit;
  87. if (mousekey_accel & (1 << 0)) {
  88. unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed) / 4;
  89. } else if (mousekey_accel & (1 << 1)) {
  90. unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed) / 2;
  91. } else if (mousekey_accel & (1 << 2)) {
  92. unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed);
  93. } else if (mousekey_repeat == 0) {
  94. unit = MOUSEKEY_MOVE_DELTA;
  95. } else if (mousekey_repeat >= mk_time_to_max) {
  96. unit = MOUSEKEY_MOVE_DELTA * mk_max_speed;
  97. } else {
  98. unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * mousekey_repeat) / mk_time_to_max;
  99. }
  100. return (unit > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : (unit == 0 ? 1 : unit));
  101. }
  102. # else // MOUSEKEY_INERTIA mode
  103. static int8_t move_unit(uint8_t axis) {
  104. int16_t unit;
  105. // handle X or Y axis
  106. int8_t inertia, dir;
  107. if (axis) {
  108. inertia = mousekey_y_inertia;
  109. dir = mousekey_y_dir;
  110. } else {
  111. inertia = mousekey_x_inertia;
  112. dir = mousekey_x_dir;
  113. }
  114. if (mousekey_frame < 2) { // first frame(s): initial keypress moves one pixel
  115. mousekey_frame = 1;
  116. unit = dir * MOUSEKEY_MOVE_DELTA;
  117. } else { // acceleration
  118. // linear acceleration (is here for reference, but doesn't feel as good during use)
  119. // unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * inertia) / mk_time_to_max;
  120. // x**2 acceleration (quadratic, more precise for short movements)
  121. int16_t percent = (inertia << 8) / mk_time_to_max;
  122. percent = ((int32_t)percent * percent) >> 8;
  123. if (inertia < 0) percent = -percent;
  124. // unit = sign(inertia) + (percent of max speed)
  125. if (inertia > 0)
  126. unit = 1;
  127. else if (inertia < 0)
  128. unit = -1;
  129. else
  130. unit = 0;
  131. unit = unit + ((mk_max_speed * percent) >> 8);
  132. }
  133. if (unit > MOUSEKEY_MOVE_MAX)
  134. unit = MOUSEKEY_MOVE_MAX;
  135. else if (unit < -MOUSEKEY_MOVE_MAX)
  136. unit = -MOUSEKEY_MOVE_MAX;
  137. return unit;
  138. }
  139. # endif // end MOUSEKEY_INERTIA mode
  140. static uint8_t wheel_unit(void) {
  141. uint16_t unit;
  142. if (mousekey_accel & (1 << 0)) {
  143. unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed) / 4;
  144. } else if (mousekey_accel & (1 << 1)) {
  145. unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed) / 2;
  146. } else if (mousekey_accel & (1 << 2)) {
  147. unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed);
  148. } else if (mousekey_wheel_repeat == 0) {
  149. unit = MOUSEKEY_WHEEL_DELTA;
  150. } else if (mousekey_wheel_repeat >= mk_wheel_time_to_max) {
  151. unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed;
  152. } else {
  153. unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_wheel_repeat) / mk_wheel_time_to_max;
  154. }
  155. return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : (unit == 0 ? 1 : unit));
  156. }
  157. # else /* #ifndef MK_KINETIC_SPEED */
  158. /*
  159. * Kinetic movement acceleration algorithm
  160. *
  161. * current speed = I + A * T/50 + A * (T/50)^2 * 1/2 | maximum B
  162. *
  163. * T: time since the mouse movement started
  164. * E: mouse events per second (set through MOUSEKEY_INTERVAL, UHK sends 250, the
  165. * pro micro on my Signum 3.0 sends only 125!)
  166. * I: initial speed at time 0
  167. * A: acceleration
  168. * B: base mouse travel speed
  169. */
  170. const uint16_t mk_accelerated_speed = MOUSEKEY_ACCELERATED_SPEED;
  171. const uint16_t mk_base_speed = MOUSEKEY_BASE_SPEED;
  172. const uint16_t mk_decelerated_speed = MOUSEKEY_DECELERATED_SPEED;
  173. const uint16_t mk_initial_speed = MOUSEKEY_INITIAL_SPEED;
  174. static uint8_t move_unit(void) {
  175. uint16_t speed = mk_initial_speed;
  176. if (mousekey_accel & (1 << 0)) {
  177. speed = mk_decelerated_speed;
  178. } else if (mousekey_accel & (1 << 2)) {
  179. speed = mk_accelerated_speed;
  180. } else if (mousekey_repeat && mouse_timer) {
  181. const uint16_t time_elapsed = timer_elapsed(mouse_timer) / 50;
  182. speed = mk_initial_speed + MOUSEKEY_MOVE_DELTA * time_elapsed + (MOUSEKEY_MOVE_DELTA * time_elapsed * time_elapsed) / 2;
  183. if (speed > mk_base_speed) {
  184. speed = mk_base_speed;
  185. }
  186. }
  187. /* convert speed to USB mouse speed 1 to 127 */
  188. speed = (uint8_t)(speed / (1000U / mk_interval));
  189. if (speed > MOUSEKEY_MOVE_MAX) {
  190. speed = MOUSEKEY_MOVE_MAX;
  191. } else if (speed < 1) {
  192. speed = 1;
  193. }
  194. return speed;
  195. }
  196. static uint8_t wheel_unit(void) {
  197. uint16_t speed = MOUSEKEY_WHEEL_INITIAL_MOVEMENTS;
  198. if (mousekey_accel & (1 << 0)) {
  199. speed = MOUSEKEY_WHEEL_DECELERATED_MOVEMENTS;
  200. } else if (mousekey_accel & (1 << 2)) {
  201. speed = MOUSEKEY_WHEEL_ACCELERATED_MOVEMENTS;
  202. } else if (mousekey_wheel_repeat && mouse_timer) {
  203. if (mk_wheel_interval != MOUSEKEY_WHEEL_BASE_MOVEMENTS) {
  204. const uint16_t time_elapsed = timer_elapsed(mouse_timer) / 50;
  205. speed = MOUSEKEY_WHEEL_INITIAL_MOVEMENTS + 1 * time_elapsed + (1 * time_elapsed * time_elapsed) / 2;
  206. }
  207. if (speed > MOUSEKEY_WHEEL_BASE_MOVEMENTS) {
  208. speed = MOUSEKEY_WHEEL_BASE_MOVEMENTS;
  209. }
  210. }
  211. mk_wheel_interval = 1000U / speed;
  212. return 1;
  213. }
  214. # endif /* #ifndef MK_KINETIC_SPEED */
  215. # else /* #ifndef MK_COMBINED */
  216. /* Combined mode */
  217. static uint8_t move_unit(void) {
  218. uint16_t unit;
  219. if (mousekey_accel & (1 << 0)) {
  220. unit = 1;
  221. } else if (mousekey_accel & (1 << 1)) {
  222. unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed) / 2;
  223. } else if (mousekey_accel & (1 << 2)) {
  224. unit = MOUSEKEY_MOVE_MAX;
  225. } else if (mousekey_repeat == 0) {
  226. unit = MOUSEKEY_MOVE_DELTA;
  227. } else if (mousekey_repeat >= mk_time_to_max) {
  228. unit = MOUSEKEY_MOVE_DELTA * mk_max_speed;
  229. } else {
  230. unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * mousekey_repeat) / mk_time_to_max;
  231. }
  232. return (unit > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : (unit == 0 ? 1 : unit));
  233. }
  234. static uint8_t wheel_unit(void) {
  235. uint16_t unit;
  236. if (mousekey_accel & (1 << 0)) {
  237. unit = 1;
  238. } else if (mousekey_accel & (1 << 1)) {
  239. unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed) / 2;
  240. } else if (mousekey_accel & (1 << 2)) {
  241. unit = MOUSEKEY_WHEEL_MAX;
  242. } else if (mousekey_repeat == 0) {
  243. unit = MOUSEKEY_WHEEL_DELTA;
  244. } else if (mousekey_repeat >= mk_wheel_time_to_max) {
  245. unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed;
  246. } else {
  247. unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_repeat) / mk_wheel_time_to_max;
  248. }
  249. return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : (unit == 0 ? 1 : unit));
  250. }
  251. # endif /* #ifndef MK_COMBINED */
  252. # ifdef MOUSEKEY_INERTIA
  253. static int8_t calc_inertia(int8_t direction, int8_t velocity) {
  254. // simulate acceleration and deceleration
  255. // deceleration
  256. if ((direction > -1) && (velocity < 0))
  257. velocity = (velocity + 1) * (256 - MOUSEKEY_FRICTION) / 256;
  258. else if ((direction < 1) && (velocity > 0))
  259. velocity = velocity * (256 - MOUSEKEY_FRICTION) / 256;
  260. // acceleration
  261. if ((direction > 0) && (velocity < mk_time_to_max))
  262. velocity++;
  263. else if ((direction < 0) && (velocity > -mk_time_to_max))
  264. velocity--;
  265. return velocity;
  266. }
  267. # endif
  268. void mousekey_task(void) {
  269. // report cursor and scroll movement independently
  270. report_mouse_t tmpmr = mouse_report;
  271. mouse_report.x = 0;
  272. mouse_report.y = 0;
  273. mouse_report.v = 0;
  274. mouse_report.h = 0;
  275. # ifdef MOUSEKEY_INERTIA
  276. // if an animation is in progress and it's time for the next frame
  277. if ((mousekey_frame) && timer_elapsed(last_timer_c) > ((mousekey_frame > 1) ? mk_interval : mk_delay * 10)) {
  278. mousekey_x_inertia = calc_inertia(mousekey_x_dir, mousekey_x_inertia);
  279. mousekey_y_inertia = calc_inertia(mousekey_y_dir, mousekey_y_inertia);
  280. mouse_report.x = move_unit(0);
  281. mouse_report.y = move_unit(1);
  282. // prevent sticky "drift"
  283. if ((!mousekey_x_dir) && (!mousekey_x_inertia)) tmpmr.x = 0;
  284. if ((!mousekey_y_dir) && (!mousekey_y_inertia)) tmpmr.y = 0;
  285. if (mousekey_frame < 2) mousekey_frame++;
  286. }
  287. // reset if not moving and no movement keys are held
  288. if ((!mousekey_x_dir) && (!mousekey_y_dir) && (!mousekey_x_inertia) && (!mousekey_y_inertia)) {
  289. mousekey_frame = 0;
  290. tmpmr.x = 0;
  291. tmpmr.y = 0;
  292. }
  293. # else // default acceleration
  294. if ((tmpmr.x || tmpmr.y) && timer_elapsed(last_timer_c) > (mousekey_repeat ? mk_interval : mk_delay * 10)) {
  295. if (mousekey_repeat != UINT8_MAX) mousekey_repeat++;
  296. if (tmpmr.x != 0) mouse_report.x = move_unit() * ((tmpmr.x > 0) ? 1 : -1);
  297. if (tmpmr.y != 0) mouse_report.y = move_unit() * ((tmpmr.y > 0) ? 1 : -1);
  298. /* diagonal move [1/sqrt(2)] */
  299. if (mouse_report.x && mouse_report.y) {
  300. mouse_report.x = times_inv_sqrt2(mouse_report.x);
  301. if (mouse_report.x == 0) {
  302. mouse_report.x = 1;
  303. }
  304. mouse_report.y = times_inv_sqrt2(mouse_report.y);
  305. if (mouse_report.y == 0) {
  306. mouse_report.y = 1;
  307. }
  308. }
  309. }
  310. # endif // MOUSEKEY_INERTIA or not
  311. if ((tmpmr.v || tmpmr.h) && timer_elapsed(last_timer_w) > (mousekey_wheel_repeat ? mk_wheel_interval : mk_wheel_delay * 10)) {
  312. if (mousekey_wheel_repeat != UINT8_MAX) mousekey_wheel_repeat++;
  313. if (tmpmr.v != 0) mouse_report.v = wheel_unit() * ((tmpmr.v > 0) ? 1 : -1);
  314. if (tmpmr.h != 0) mouse_report.h = wheel_unit() * ((tmpmr.h > 0) ? 1 : -1);
  315. /* diagonal move [1/sqrt(2)] */
  316. if (mouse_report.v && mouse_report.h) {
  317. mouse_report.v = times_inv_sqrt2(mouse_report.v);
  318. if (mouse_report.v == 0) {
  319. mouse_report.v = 1;
  320. }
  321. mouse_report.h = times_inv_sqrt2(mouse_report.h);
  322. if (mouse_report.h == 0) {
  323. mouse_report.h = 1;
  324. }
  325. }
  326. }
  327. if (has_mouse_report_changed(&mouse_report, &tmpmr) || should_mousekey_report_send(&mouse_report)) {
  328. mousekey_send();
  329. }
  330. // save the state for later
  331. memcpy(&mouse_report, &tmpmr, sizeof(tmpmr));
  332. }
  333. void mousekey_on(uint8_t code) {
  334. # ifdef MK_KINETIC_SPEED
  335. if (mouse_timer == 0) {
  336. mouse_timer = timer_read();
  337. }
  338. # endif
  339. # ifndef MOUSEKEY_INERTIA
  340. // If mouse report is not zero, the current mousekey press is overlapping
  341. // with another. Restart acceleration for smoother directional transition.
  342. if (mouse_report.x || mouse_report.y || mouse_report.h || mouse_report.v) {
  343. # ifdef MK_KINETIC_SPEED
  344. mouse_timer = timer_read() - (MOUSEKEY_INTERVAL << 2);
  345. # else
  346. mousekey_repeat = MOUSEKEY_MOVE_DELTA;
  347. mousekey_wheel_repeat = MOUSEKEY_WHEEL_DELTA;
  348. # endif
  349. }
  350. # endif // ifndef MOUSEKEY_INERTIA
  351. # ifdef MOUSEKEY_INERTIA
  352. // initial keypress sets impulse and activates first frame of movement
  353. if ((code == KC_MS_UP) || (code == KC_MS_DOWN)) {
  354. mousekey_y_dir = (code == KC_MS_DOWN) ? 1 : -1;
  355. if (mousekey_frame < 2) mouse_report.y = move_unit(1);
  356. } else if ((code == KC_MS_LEFT) || (code == KC_MS_RIGHT)) {
  357. mousekey_x_dir = (code == KC_MS_RIGHT) ? 1 : -1;
  358. if (mousekey_frame < 2) mouse_report.x = move_unit(0);
  359. }
  360. # else // no inertia
  361. if (code == KC_MS_UP)
  362. mouse_report.y = move_unit() * -1;
  363. else if (code == KC_MS_DOWN)
  364. mouse_report.y = move_unit();
  365. else if (code == KC_MS_LEFT)
  366. mouse_report.x = move_unit() * -1;
  367. else if (code == KC_MS_RIGHT)
  368. mouse_report.x = move_unit();
  369. # endif // inertia or not
  370. else if (code == KC_MS_WH_UP)
  371. mouse_report.v = wheel_unit();
  372. else if (code == KC_MS_WH_DOWN)
  373. mouse_report.v = wheel_unit() * -1;
  374. else if (code == KC_MS_WH_LEFT)
  375. mouse_report.h = wheel_unit() * -1;
  376. else if (code == KC_MS_WH_RIGHT)
  377. mouse_report.h = wheel_unit();
  378. else if (IS_MOUSEKEY_BUTTON(code))
  379. mouse_report.buttons |= 1 << (code - KC_MS_BTN1);
  380. else if (code == KC_MS_ACCEL0)
  381. mousekey_accel |= (1 << 0);
  382. else if (code == KC_MS_ACCEL1)
  383. mousekey_accel |= (1 << 1);
  384. else if (code == KC_MS_ACCEL2)
  385. mousekey_accel |= (1 << 2);
  386. }
  387. void mousekey_off(uint8_t code) {
  388. # ifdef MOUSEKEY_INERTIA
  389. // key release clears impulse unless opposite direction is held
  390. if ((code == KC_MS_UP) && (mousekey_y_dir < 1))
  391. mousekey_y_dir = 0;
  392. else if ((code == KC_MS_DOWN) && (mousekey_y_dir > -1))
  393. mousekey_y_dir = 0;
  394. else if ((code == KC_MS_LEFT) && (mousekey_x_dir < 1))
  395. mousekey_x_dir = 0;
  396. else if ((code == KC_MS_RIGHT) && (mousekey_x_dir > -1))
  397. mousekey_x_dir = 0;
  398. # else // no inertia
  399. if (code == KC_MS_UP && mouse_report.y < 0)
  400. mouse_report.y = 0;
  401. else if (code == KC_MS_DOWN && mouse_report.y > 0)
  402. mouse_report.y = 0;
  403. else if (code == KC_MS_LEFT && mouse_report.x < 0)
  404. mouse_report.x = 0;
  405. else if (code == KC_MS_RIGHT && mouse_report.x > 0)
  406. mouse_report.x = 0;
  407. # endif // inertia or not
  408. else if (code == KC_MS_WH_UP && mouse_report.v > 0)
  409. mouse_report.v = 0;
  410. else if (code == KC_MS_WH_DOWN && mouse_report.v < 0)
  411. mouse_report.v = 0;
  412. else if (code == KC_MS_WH_LEFT && mouse_report.h < 0)
  413. mouse_report.h = 0;
  414. else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0)
  415. mouse_report.h = 0;
  416. else if (IS_MOUSEKEY_BUTTON(code))
  417. mouse_report.buttons &= ~(1 << (code - KC_MS_BTN1));
  418. else if (code == KC_MS_ACCEL0)
  419. mousekey_accel &= ~(1 << 0);
  420. else if (code == KC_MS_ACCEL1)
  421. mousekey_accel &= ~(1 << 1);
  422. else if (code == KC_MS_ACCEL2)
  423. mousekey_accel &= ~(1 << 2);
  424. if (mouse_report.x == 0 && mouse_report.y == 0) {
  425. mousekey_repeat = 0;
  426. # ifdef MK_KINETIC_SPEED
  427. mouse_timer = 0;
  428. # endif /* #ifdef MK_KINETIC_SPEED */
  429. }
  430. if (mouse_report.v == 0 && mouse_report.h == 0) mousekey_wheel_repeat = 0;
  431. }
  432. #else /* #ifndef MK_3_SPEED */
  433. enum { mkspd_unmod, mkspd_0, mkspd_1, mkspd_2, mkspd_COUNT };
  434. # ifndef MK_MOMENTARY_ACCEL
  435. static uint8_t mk_speed = mkspd_1;
  436. # else
  437. static uint8_t mk_speed = mkspd_unmod;
  438. static uint8_t mkspd_DEFAULT = mkspd_unmod;
  439. # endif
  440. static uint16_t last_timer_c = 0;
  441. static uint16_t last_timer_w = 0;
  442. uint16_t c_offsets[mkspd_COUNT] = {MK_C_OFFSET_UNMOD, MK_C_OFFSET_0, MK_C_OFFSET_1, MK_C_OFFSET_2};
  443. uint16_t c_intervals[mkspd_COUNT] = {MK_C_INTERVAL_UNMOD, MK_C_INTERVAL_0, MK_C_INTERVAL_1, MK_C_INTERVAL_2};
  444. uint16_t w_offsets[mkspd_COUNT] = {MK_W_OFFSET_UNMOD, MK_W_OFFSET_0, MK_W_OFFSET_1, MK_W_OFFSET_2};
  445. uint16_t w_intervals[mkspd_COUNT] = {MK_W_INTERVAL_UNMOD, MK_W_INTERVAL_0, MK_W_INTERVAL_1, MK_W_INTERVAL_2};
  446. void mousekey_task(void) {
  447. // report cursor and scroll movement independently
  448. report_mouse_t tmpmr = mouse_report;
  449. mouse_report.x = 0;
  450. mouse_report.y = 0;
  451. mouse_report.v = 0;
  452. mouse_report.h = 0;
  453. if ((tmpmr.x || tmpmr.y) && timer_elapsed(last_timer_c) > c_intervals[mk_speed]) {
  454. mouse_report.x = tmpmr.x;
  455. mouse_report.y = tmpmr.y;
  456. }
  457. if ((tmpmr.h || tmpmr.v) && timer_elapsed(last_timer_w) > w_intervals[mk_speed]) {
  458. mouse_report.v = tmpmr.v;
  459. mouse_report.h = tmpmr.h;
  460. }
  461. if (has_mouse_report_changed(&mouse_report, &tmpmr) || should_mousekey_report_send(&mouse_report)) {
  462. mousekey_send();
  463. }
  464. memcpy(&mouse_report, &tmpmr, sizeof(tmpmr));
  465. }
  466. void adjust_speed(void) {
  467. uint16_t const c_offset = c_offsets[mk_speed];
  468. uint16_t const w_offset = w_offsets[mk_speed];
  469. if (mouse_report.x > 0) mouse_report.x = c_offset;
  470. if (mouse_report.x < 0) mouse_report.x = c_offset * -1;
  471. if (mouse_report.y > 0) mouse_report.y = c_offset;
  472. if (mouse_report.y < 0) mouse_report.y = c_offset * -1;
  473. if (mouse_report.h > 0) mouse_report.h = w_offset;
  474. if (mouse_report.h < 0) mouse_report.h = w_offset * -1;
  475. if (mouse_report.v > 0) mouse_report.v = w_offset;
  476. if (mouse_report.v < 0) mouse_report.v = w_offset * -1;
  477. // adjust for diagonals
  478. if (mouse_report.x && mouse_report.y) {
  479. mouse_report.x = times_inv_sqrt2(mouse_report.x);
  480. if (mouse_report.x == 0) {
  481. mouse_report.x = 1;
  482. }
  483. mouse_report.y = times_inv_sqrt2(mouse_report.y);
  484. if (mouse_report.y == 0) {
  485. mouse_report.y = 1;
  486. }
  487. }
  488. if (mouse_report.h && mouse_report.v) {
  489. mouse_report.h = times_inv_sqrt2(mouse_report.h);
  490. mouse_report.v = times_inv_sqrt2(mouse_report.v);
  491. }
  492. }
  493. void mousekey_on(uint8_t code) {
  494. uint16_t const c_offset = c_offsets[mk_speed];
  495. uint16_t const w_offset = w_offsets[mk_speed];
  496. uint8_t const old_speed = mk_speed;
  497. if (code == KC_MS_UP)
  498. mouse_report.y = c_offset * -1;
  499. else if (code == KC_MS_DOWN)
  500. mouse_report.y = c_offset;
  501. else if (code == KC_MS_LEFT)
  502. mouse_report.x = c_offset * -1;
  503. else if (code == KC_MS_RIGHT)
  504. mouse_report.x = c_offset;
  505. else if (code == KC_MS_WH_UP)
  506. mouse_report.v = w_offset;
  507. else if (code == KC_MS_WH_DOWN)
  508. mouse_report.v = w_offset * -1;
  509. else if (code == KC_MS_WH_LEFT)
  510. mouse_report.h = w_offset * -1;
  511. else if (code == KC_MS_WH_RIGHT)
  512. mouse_report.h = w_offset;
  513. else if (IS_MOUSEKEY_BUTTON(code))
  514. mouse_report.buttons |= 1 << (code - KC_MS_BTN1);
  515. else if (code == KC_MS_ACCEL0)
  516. mk_speed = mkspd_0;
  517. else if (code == KC_MS_ACCEL1)
  518. mk_speed = mkspd_1;
  519. else if (code == KC_MS_ACCEL2)
  520. mk_speed = mkspd_2;
  521. if (mk_speed != old_speed) adjust_speed();
  522. }
  523. void mousekey_off(uint8_t code) {
  524. # ifdef MK_MOMENTARY_ACCEL
  525. uint8_t const old_speed = mk_speed;
  526. # endif
  527. if (code == KC_MS_UP && mouse_report.y < 0)
  528. mouse_report.y = 0;
  529. else if (code == KC_MS_DOWN && mouse_report.y > 0)
  530. mouse_report.y = 0;
  531. else if (code == KC_MS_LEFT && mouse_report.x < 0)
  532. mouse_report.x = 0;
  533. else if (code == KC_MS_RIGHT && mouse_report.x > 0)
  534. mouse_report.x = 0;
  535. else if (code == KC_MS_WH_UP && mouse_report.v > 0)
  536. mouse_report.v = 0;
  537. else if (code == KC_MS_WH_DOWN && mouse_report.v < 0)
  538. mouse_report.v = 0;
  539. else if (code == KC_MS_WH_LEFT && mouse_report.h < 0)
  540. mouse_report.h = 0;
  541. else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0)
  542. mouse_report.h = 0;
  543. else if (IS_MOUSEKEY_BUTTON(code))
  544. mouse_report.buttons &= ~(1 << (code - KC_MS_BTN1));
  545. # ifdef MK_MOMENTARY_ACCEL
  546. else if (code == KC_MS_ACCEL0)
  547. mk_speed = mkspd_DEFAULT;
  548. else if (code == KC_MS_ACCEL1)
  549. mk_speed = mkspd_DEFAULT;
  550. else if (code == KC_MS_ACCEL2)
  551. mk_speed = mkspd_DEFAULT;
  552. if (mk_speed != old_speed) adjust_speed();
  553. # endif
  554. }
  555. #endif /* #ifndef MK_3_SPEED */
  556. void mousekey_send(void) {
  557. mousekey_debug();
  558. uint16_t time = timer_read();
  559. if (mouse_report.x || mouse_report.y) last_timer_c = time;
  560. if (mouse_report.v || mouse_report.h) last_timer_w = time;
  561. host_mouse_send(&mouse_report);
  562. }
  563. void mousekey_clear(void) {
  564. mouse_report = (report_mouse_t){};
  565. mousekey_repeat = 0;
  566. mousekey_wheel_repeat = 0;
  567. mousekey_accel = 0;
  568. #ifdef MOUSEKEY_INERTIA
  569. mousekey_frame = 0;
  570. mousekey_x_inertia = 0;
  571. mousekey_y_inertia = 0;
  572. mousekey_x_dir = 0;
  573. mousekey_y_dir = 0;
  574. #endif
  575. }
  576. static void mousekey_debug(void) {
  577. if (!debug_mouse) return;
  578. print("mousekey [btn|x y v h](rep/acl): [");
  579. print_hex8(mouse_report.buttons);
  580. print("|");
  581. print_decs(mouse_report.x);
  582. print(" ");
  583. print_decs(mouse_report.y);
  584. print(" ");
  585. print_decs(mouse_report.v);
  586. print(" ");
  587. print_decs(mouse_report.h);
  588. print("](");
  589. print_dec(mousekey_repeat);
  590. print("/");
  591. print_dec(mousekey_accel);
  592. print(")\n");
  593. }
  594. report_mouse_t mousekey_get_report(void) {
  595. return mouse_report;
  596. }
  597. bool should_mousekey_report_send(report_mouse_t *mouse_report) {
  598. return mouse_report->x || mouse_report->y || mouse_report->v || mouse_report->h;
  599. }