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.

169 lines
4.3 KiB

  1. /* Copyright 2020 Alexander Tulloh
  2. *
  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. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "oddball.h"
  17. #include "pointing_device.h"
  18. #include "optical_sensor/optical_sensor.h"
  19. #define CLAMP_HID(value) value < -127 ? -127 : value > 127 ? 127 : value
  20. static bool scroll_pressed;
  21. static bool mouse_buttons_dirty;
  22. static int8_t scroll_h;
  23. static int8_t scroll_v;
  24. void pointing_device_init(void){
  25. if(!is_keyboard_master())
  26. return;
  27. optical_sensor_init();
  28. // read config from EEPROM and update if needed
  29. config_oddball_t kb_config;
  30. kb_config.raw = eeconfig_read_kb();
  31. if(!kb_config.cpi) {
  32. kb_config.cpi = CPI_2;
  33. eeconfig_update_kb(kb_config.raw);
  34. }
  35. optical_sensor_set_config((config_optical_sensor_t){ kb_config.cpi });
  36. }
  37. void pointing_device_task(void){
  38. if(!is_keyboard_master())
  39. return;
  40. report_mouse_t mouse_report = pointing_device_get_report();
  41. report_optical_sensor_t sensor_report = optical_sensor_get_report();
  42. int8_t clamped_x = CLAMP_HID(sensor_report.x);
  43. int8_t clamped_y = CLAMP_HID(sensor_report.y);
  44. if(scroll_pressed) {
  45. // accumulate scroll
  46. scroll_h += clamped_x;
  47. scroll_v += clamped_y;
  48. int8_t scaled_scroll_h = scroll_h / SCROLL_DIVIDER;
  49. int8_t scaled_scroll_v = scroll_v / SCROLL_DIVIDER;
  50. // clear accumulated scroll on assignment
  51. if(scaled_scroll_h != 0){
  52. mouse_report.h = -scaled_scroll_h;
  53. scroll_h = 0;
  54. }
  55. if(scaled_scroll_v != 0){
  56. mouse_report.v = -scaled_scroll_v;
  57. scroll_v = 0;
  58. }
  59. }
  60. else {
  61. mouse_report.x = -clamped_x;
  62. mouse_report.y = clamped_y;
  63. }
  64. pointing_device_set_report(mouse_report);
  65. // only send report on change as even sending report with no change is treated as movement
  66. if(mouse_buttons_dirty ||
  67. mouse_report.x != 0 ||
  68. mouse_report.y != 0 ||
  69. mouse_report.h != 0 ||
  70. mouse_report.v != 0){
  71. mouse_buttons_dirty = false;
  72. pointing_device_send();
  73. }
  74. }
  75. static void on_cpi_button(uint16_t cpi, keyrecord_t *record) {
  76. if(!record->event.pressed)
  77. return;
  78. optical_sensor_set_config((config_optical_sensor_t){ cpi });
  79. config_oddball_t kb_config;
  80. kb_config.cpi = cpi;
  81. eeconfig_update_kb(kb_config.raw);
  82. }
  83. static void on_mouse_button(uint8_t mouse_button, keyrecord_t *record) {
  84. report_mouse_t report = pointing_device_get_report();
  85. if(record->event.pressed)
  86. report.buttons |= mouse_button;
  87. else
  88. report.buttons &= ~mouse_button;
  89. pointing_device_set_report(report);
  90. mouse_buttons_dirty = true;
  91. }
  92. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  93. if(!process_record_user(keycode, record))
  94. return false;
  95. // handle mouse drag and scroll
  96. switch (keycode) {
  97. case KC_BTN1:
  98. on_mouse_button(MOUSE_BTN1, record);
  99. return false;
  100. case KC_BTN2:
  101. on_mouse_button(MOUSE_BTN2, record);
  102. return false;
  103. case KC_BTN3:
  104. on_mouse_button(MOUSE_BTN3, record);
  105. return false;
  106. case KC_BTN4:
  107. on_mouse_button(MOUSE_BTN4, record);
  108. return false;
  109. case KC_BTN5:
  110. on_mouse_button(MOUSE_BTN5, record);
  111. return false;
  112. case KC_SCROLL:
  113. scroll_pressed = record->event.pressed;
  114. return false;
  115. case KC_CPI_1:
  116. on_cpi_button(CPI_1, record);
  117. return false;
  118. case KC_CPI_2:
  119. on_cpi_button(CPI_2, record);
  120. return false;
  121. case KC_CPI_3:
  122. on_cpi_button(CPI_3, record);
  123. return false;
  124. default:
  125. return true;
  126. }
  127. }