Keyboard firmwares for Atmel AVR and Cortex-M
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.

mousekey.c 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. Copyright 2011 Jun Wako <[email protected]>
  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. static report_mouse_t mouse_report = {};
  22. static uint8_t mousekey_repeat = 0;
  23. static uint8_t mousekey_accel = 0;
  24. static void mousekey_debug(void);
  25. /*
  26. * Mouse keys acceleration algorithm
  27. * http://en.wikipedia.org/wiki/Mouse_keys
  28. *
  29. * speed = delta * max_speed * (repeat / time_to_max)**((1000+curve)/1000)
  30. */
  31. /* milliseconds between the initial key press and first repeated motion event (0-2550) */
  32. uint8_t mk_delay = MOUSEKEY_DELAY/10;
  33. /* milliseconds between repeated motion events (0-255) */
  34. uint8_t mk_interval = MOUSEKEY_INTERVAL;
  35. /* steady speed (in action_delta units) applied each event (0-255) */
  36. uint8_t mk_max_speed = MOUSEKEY_MAX_SPEED;
  37. /* number of events (count) accelerating to steady speed (0-255) */
  38. uint8_t mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
  39. /* ramp used to reach maximum pointer speed (NOT SUPPORTED) */
  40. //int8_t mk_curve = 0;
  41. /* wheel params */
  42. uint8_t mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
  43. uint8_t mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
  44. static uint16_t last_timer = 0;
  45. static uint8_t move_unit(void)
  46. {
  47. uint16_t unit;
  48. if (mousekey_accel & (1<<0)) {
  49. unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed)/4;
  50. } else if (mousekey_accel & (1<<1)) {
  51. unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed)/2;
  52. } else if (mousekey_accel & (1<<2)) {
  53. unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed);
  54. } else if (mousekey_repeat == 0) {
  55. unit = MOUSEKEY_MOVE_DELTA;
  56. } else if (mousekey_repeat >= mk_time_to_max) {
  57. unit = MOUSEKEY_MOVE_DELTA * mk_max_speed;
  58. } else {
  59. unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * mousekey_repeat) / mk_time_to_max;
  60. }
  61. return (unit > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : (unit == 0 ? 1 : unit));
  62. }
  63. static uint8_t wheel_unit(void)
  64. {
  65. uint16_t unit;
  66. if (mousekey_accel & (1<<0)) {
  67. unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed)/4;
  68. } else if (mousekey_accel & (1<<1)) {
  69. unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed)/2;
  70. } else if (mousekey_accel & (1<<2)) {
  71. unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed);
  72. } else if (mousekey_repeat == 0) {
  73. unit = MOUSEKEY_WHEEL_DELTA;
  74. } else if (mousekey_repeat >= mk_wheel_time_to_max) {
  75. unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed;
  76. } else {
  77. unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_repeat) / mk_wheel_time_to_max;
  78. }
  79. return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : (unit == 0 ? 1 : unit));
  80. }
  81. void mousekey_task(void)
  82. {
  83. if (timer_elapsed(last_timer) < (mousekey_repeat ? mk_interval : mk_delay*10))
  84. return;
  85. if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0)
  86. return;
  87. if (mousekey_repeat != UINT8_MAX)
  88. mousekey_repeat++;
  89. if (mouse_report.x > 0) mouse_report.x = move_unit();
  90. if (mouse_report.x < 0) mouse_report.x = move_unit() * -1;
  91. if (mouse_report.y > 0) mouse_report.y = move_unit();
  92. if (mouse_report.y < 0) mouse_report.y = move_unit() * -1;
  93. /* diagonal move [1/sqrt(2) = 0.7] */
  94. if (mouse_report.x && mouse_report.y) {
  95. mouse_report.x *= 0.7;
  96. mouse_report.y *= 0.7;
  97. }
  98. if (mouse_report.v > 0) mouse_report.v = wheel_unit();
  99. if (mouse_report.v < 0) mouse_report.v = wheel_unit() * -1;
  100. if (mouse_report.h > 0) mouse_report.h = wheel_unit();
  101. if (mouse_report.h < 0) mouse_report.h = wheel_unit() * -1;
  102. mousekey_send();
  103. }
  104. void mousekey_on(uint8_t code)
  105. {
  106. if (code == KC_MS_UP) mouse_report.y = move_unit() * -1;
  107. else if (code == KC_MS_DOWN) mouse_report.y = move_unit();
  108. else if (code == KC_MS_LEFT) mouse_report.x = move_unit() * -1;
  109. else if (code == KC_MS_RIGHT) mouse_report.x = move_unit();
  110. else if (code == KC_MS_WH_UP) mouse_report.v = wheel_unit();
  111. else if (code == KC_MS_WH_DOWN) mouse_report.v = wheel_unit() * -1;
  112. else if (code == KC_MS_WH_LEFT) mouse_report.h = wheel_unit() * -1;
  113. else if (code == KC_MS_WH_RIGHT) mouse_report.h = wheel_unit();
  114. else if (code == KC_MS_BTN1) mouse_report.buttons |= MOUSE_BTN1;
  115. else if (code == KC_MS_BTN2) mouse_report.buttons |= MOUSE_BTN2;
  116. else if (code == KC_MS_BTN3) mouse_report.buttons |= MOUSE_BTN3;
  117. else if (code == KC_MS_BTN4) mouse_report.buttons |= MOUSE_BTN4;
  118. else if (code == KC_MS_BTN5) mouse_report.buttons |= MOUSE_BTN5;
  119. else if (code == KC_MS_ACCEL0) mousekey_accel |= (1<<0);
  120. else if (code == KC_MS_ACCEL1) mousekey_accel |= (1<<1);
  121. else if (code == KC_MS_ACCEL2) mousekey_accel |= (1<<2);
  122. }
  123. void mousekey_off(uint8_t code)
  124. {
  125. if (code == KC_MS_UP && mouse_report.y < 0) mouse_report.y = 0;
  126. else if (code == KC_MS_DOWN && mouse_report.y > 0) mouse_report.y = 0;
  127. else if (code == KC_MS_LEFT && mouse_report.x < 0) mouse_report.x = 0;
  128. else if (code == KC_MS_RIGHT && mouse_report.x > 0) mouse_report.x = 0;
  129. else if (code == KC_MS_WH_UP && mouse_report.v > 0) mouse_report.v = 0;
  130. else if (code == KC_MS_WH_DOWN && mouse_report.v < 0) mouse_report.v = 0;
  131. else if (code == KC_MS_WH_LEFT && mouse_report.h < 0) mouse_report.h = 0;
  132. else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0) mouse_report.h = 0;
  133. else if (code == KC_MS_BTN1) mouse_report.buttons &= ~MOUSE_BTN1;
  134. else if (code == KC_MS_BTN2) mouse_report.buttons &= ~MOUSE_BTN2;
  135. else if (code == KC_MS_BTN3) mouse_report.buttons &= ~MOUSE_BTN3;
  136. else if (code == KC_MS_BTN4) mouse_report.buttons &= ~MOUSE_BTN4;
  137. else if (code == KC_MS_BTN5) mouse_report.buttons &= ~MOUSE_BTN5;
  138. else if (code == KC_MS_ACCEL0) mousekey_accel &= ~(1<<0);
  139. else if (code == KC_MS_ACCEL1) mousekey_accel &= ~(1<<1);
  140. else if (code == KC_MS_ACCEL2) mousekey_accel &= ~(1<<2);
  141. if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0)
  142. mousekey_repeat = 0;
  143. }
  144. void mousekey_send(void)
  145. {
  146. mousekey_debug();
  147. host_mouse_send(&mouse_report);
  148. last_timer = timer_read();
  149. }
  150. void mousekey_clear(void)
  151. {
  152. mouse_report = (report_mouse_t){};
  153. mousekey_repeat = 0;
  154. mousekey_accel = 0;
  155. }
  156. static void mousekey_debug(void)
  157. {
  158. if (!debug_mouse) return;
  159. print("mousekey [btn|x y v h](rep/acl): [");
  160. phex(mouse_report.buttons); print("|");
  161. print_decs(mouse_report.x); print(" ");
  162. print_decs(mouse_report.y); print(" ");
  163. print_decs(mouse_report.v); print(" ");
  164. print_decs(mouse_report.h); print("](");
  165. print_dec(mousekey_repeat); print("/");
  166. print_dec(mousekey_accel); print(")\n");
  167. }