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 6.4KB

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