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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <stdint.h>
  2. #include <util/delay.h>
  3. #include "usb_keycodes.h"
  4. #include "host.h"
  5. #include "timer.h"
  6. #include "print.h"
  7. #include "debug.h"
  8. #include "mousekey.h"
  9. static report_mouse_t report;
  10. static report_mouse_t report_prev;
  11. static uint8_t mousekey_repeat = 0;
  12. static void mousekey_debug(void);
  13. /*
  14. * TODO: fix acceleration algorithm
  15. * see wikipedia http://en.wikipedia.org/wiki/Mouse_keys
  16. */
  17. #ifndef MOUSEKEY_DELAY_TIME
  18. # define MOUSEKEY_DELAY_TIME 255
  19. #endif
  20. // acceleration parameters
  21. uint8_t mousekey_move_unit = 2;
  22. uint8_t mousekey_resolution = 5;
  23. static inline uint8_t move_unit(void)
  24. {
  25. uint16_t unit = 5 + mousekey_repeat*2;
  26. return (unit > 63 ? 63 : unit);
  27. }
  28. void mousekey_decode(uint8_t code)
  29. {
  30. if (code == KB_MS_UP) report.y = -move_unit();
  31. else if (code == KB_MS_DOWN) report.y = move_unit();
  32. else if (code == KB_MS_LEFT) report.x = -move_unit();
  33. else if (code == KB_MS_RIGHT) report.x = move_unit();
  34. else if (code == KB_MS_BTN1) report.buttons |= MOUSE_BTN1;
  35. else if (code == KB_MS_BTN2) report.buttons |= MOUSE_BTN2;
  36. else if (code == KB_MS_BTN3) report.buttons |= MOUSE_BTN3;
  37. else if (code == KB_MS_BTN4) report.buttons |= MOUSE_BTN4;
  38. else if (code == KB_MS_BTN5) report.buttons |= MOUSE_BTN5;
  39. else if (code == KB_MS_WH_UP) report.v += 1;
  40. else if (code == KB_MS_WH_DOWN) report.v -= 1;
  41. else if (code == KB_MS_WH_LEFT) report.h -= 1;
  42. else if (code == KB_MS_WH_RIGHT)report.h += 1;
  43. }
  44. bool mousekey_changed(void)
  45. {
  46. return (report.buttons != report_prev.buttons ||
  47. report.x || report.y || report.v || report.h);
  48. }
  49. void mousekey_send(void)
  50. {
  51. static uint16_t last_timer = 0;
  52. if (!mousekey_changed()) {
  53. mousekey_repeat = 0;
  54. mousekey_clear_report();
  55. return;
  56. }
  57. // send immediately when buttun state is changed
  58. if (report.buttons == report_prev.buttons) {
  59. if (timer_elapsed(last_timer) < 5) {
  60. mousekey_clear_report();
  61. return;
  62. }
  63. }
  64. if (mousekey_repeat != 0xFF) {
  65. mousekey_repeat++;
  66. }
  67. if (report.x && report.y) {
  68. report.x *= 0.7;
  69. report.y *= 0.7;
  70. }
  71. mousekey_debug();
  72. host_mouse_send(&report);
  73. report_prev = report;
  74. last_timer = timer_read();
  75. mousekey_clear_report();
  76. }
  77. void mousekey_clear_report(void)
  78. {
  79. report.buttons = 0;
  80. report.x = 0;
  81. report.y = 0;
  82. report.v = 0;
  83. report.h = 0;
  84. }
  85. static void mousekey_debug(void)
  86. {
  87. if (!debug_mouse) return;
  88. print("mousekey[btn|x y v h]: ");
  89. phex(report.buttons); print("|");
  90. phex(report.x); print(" ");
  91. phex(report.y); print(" ");
  92. phex(report.v); print(" ");
  93. phex(report.h);
  94. phex(mousekey_repeat);
  95. print("\n");
  96. }