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.3KB

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