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

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