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.

149 lines
3.8 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. extern const pointing_device_driver_t pointing_device_driver;
  19. static bool scroll_pressed;
  20. static bool mouse_buttons_dirty;
  21. static int8_t scroll_h;
  22. static int8_t scroll_v;
  23. void pointing_device_init_kb(void){
  24. if(!is_keyboard_master())
  25. return;
  26. // read config from EEPROM and update if needed
  27. config_oddball_t kb_config;
  28. kb_config.raw = eeconfig_read_kb();
  29. if(!kb_config.cpi) {
  30. kb_config.cpi = CPI_2;
  31. eeconfig_update_kb(kb_config.raw);
  32. }
  33. pointing_device_set_cpi(kb_config.cpi);
  34. }
  35. report_mouse_t pointing_device_task_kb(report_mouse_t mouse_report) {
  36. if (!is_keyboard_master()) return mouse_report;
  37. int8_t clamped_x = mouse_report.x, clamped_y = mouse_report.y;
  38. mouse_report.x = 0;
  39. mouse_report.y = 0;
  40. if (scroll_pressed) {
  41. // accumulate scroll
  42. scroll_h += clamped_x;
  43. scroll_v += clamped_y;
  44. int8_t scaled_scroll_h = scroll_h / SCROLL_DIVIDER;
  45. int8_t scaled_scroll_v = scroll_v / SCROLL_DIVIDER;
  46. // clear accumulated scroll on assignment
  47. if (scaled_scroll_h != 0) {
  48. mouse_report.h = -scaled_scroll_h;
  49. scroll_h = 0;
  50. }
  51. if (scaled_scroll_v != 0) {
  52. mouse_report.v = -scaled_scroll_v;
  53. scroll_v = 0;
  54. }
  55. } else {
  56. mouse_report.x = -clamped_x;
  57. mouse_report.y = clamped_y;
  58. }
  59. return mouse_report;
  60. }
  61. static void on_cpi_button(uint16_t cpi, keyrecord_t *record) {
  62. if(!record->event.pressed)
  63. return;
  64. pointing_device_set_cpi(cpi);
  65. config_oddball_t kb_config;
  66. kb_config.cpi = cpi;
  67. eeconfig_update_kb(kb_config.raw);
  68. }
  69. static void on_mouse_button(uint8_t mouse_button, keyrecord_t *record) {
  70. report_mouse_t report = pointing_device_get_report();
  71. if(record->event.pressed)
  72. report.buttons |= mouse_button;
  73. else
  74. report.buttons &= ~mouse_button;
  75. pointing_device_set_report(report);
  76. mouse_buttons_dirty = true;
  77. }
  78. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  79. if(!process_record_user(keycode, record))
  80. return false;
  81. // handle mouse drag and scroll
  82. switch (keycode) {
  83. case KC_BTN1:
  84. on_mouse_button(MOUSE_BTN1, record);
  85. return false;
  86. case KC_BTN2:
  87. on_mouse_button(MOUSE_BTN2, record);
  88. return false;
  89. case KC_BTN3:
  90. on_mouse_button(MOUSE_BTN3, record);
  91. return false;
  92. case KC_BTN4:
  93. on_mouse_button(MOUSE_BTN4, record);
  94. return false;
  95. case KC_BTN5:
  96. on_mouse_button(MOUSE_BTN5, record);
  97. return false;
  98. case KC_SCROLL:
  99. scroll_pressed = record->event.pressed;
  100. return false;
  101. case KC_CPI_1:
  102. on_cpi_button(CPI_1, record);
  103. return false;
  104. case KC_CPI_2:
  105. on_cpi_button(CPI_2, record);
  106. return false;
  107. case KC_CPI_3:
  108. on_cpi_button(CPI_3, record);
  109. return false;
  110. default:
  111. return true;
  112. }
  113. }