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.

120 lines
4.5 KiB

  1. /* Copyright 2017 Joshua Broekhuijsen <snipeye+qmk@gmail.com>
  2. * Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  3. * Copyright 2021 Dasky (@daskygit)
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "pointing_device.h"
  19. #include <string.h>
  20. #ifdef MOUSEKEY_ENABLE
  21. # include "mousekey.h"
  22. #endif
  23. #if (defined(POINTING_DEVICE_ROTATION_90) + defined(POINTING_DEVICE_ROTATION_180) + defined(POINTING_DEVICE_ROTATION_270)) > 1
  24. # error More than one rotation selected. This is not supported.
  25. #endif
  26. static report_mouse_t mouseReport = {};
  27. extern const pointing_device_driver_t pointing_device_driver;
  28. __attribute__((weak)) bool has_mouse_report_changed(report_mouse_t new, report_mouse_t old) { return memcmp(&new, &old, sizeof(new)); }
  29. __attribute__((weak)) void pointing_device_init_kb(void) {}
  30. __attribute__((weak)) void pointing_device_init_user(void) {}
  31. __attribute__((weak)) report_mouse_t pointing_device_task_kb(report_mouse_t mouse_report) { return pointing_device_task_user(mouse_report); }
  32. __attribute__((weak)) report_mouse_t pointing_device_task_user(report_mouse_t mouse_report) { return mouse_report; }
  33. __attribute__((weak)) uint8_t pointing_device_handle_buttons(uint8_t buttons, bool pressed, pointing_device_buttons_t button) {
  34. if (pressed) {
  35. buttons |= 1 << (button);
  36. } else {
  37. buttons &= ~(1 << (button));
  38. }
  39. return buttons;
  40. }
  41. __attribute__((weak)) void pointing_device_init(void) {
  42. pointing_device_driver.init();
  43. #ifdef POINTING_DEVICE_MOTION_PIN
  44. setPinInputHigh(POINTING_DEVICE_MOTION_PIN);
  45. #endif
  46. pointing_device_init_kb();
  47. pointing_device_init_user();
  48. }
  49. __attribute__((weak)) void pointing_device_send(void) {
  50. static report_mouse_t old_report = {};
  51. // If you need to do other things, like debugging, this is the place to do it.
  52. if (has_mouse_report_changed(mouseReport, old_report)) {
  53. host_mouse_send(&mouseReport);
  54. }
  55. // send it and 0 it out except for buttons, so those stay until they are explicity over-ridden using update_pointing_device
  56. mouseReport.x = 0;
  57. mouseReport.y = 0;
  58. mouseReport.v = 0;
  59. mouseReport.h = 0;
  60. memcpy(&old_report, &mouseReport, sizeof(mouseReport));
  61. }
  62. __attribute__((weak)) void pointing_device_task(void) {
  63. // Gather report info
  64. #ifdef POINTING_DEVICE_MOTION_PIN
  65. if (!readPin(POINTING_DEVICE_MOTION_PIN))
  66. #endif
  67. mouseReport = pointing_device_driver.get_report(mouseReport);
  68. // Support rotation of the sensor data
  69. #if defined(POINTING_DEVICE_ROTATION_90) || defined(POINTING_DEVICE_ROTATION_180) || defined(POINTING_DEVICE_ROTATION_270)
  70. int8_t x = mouseReport.x, y = mouseReport.y;
  71. # if defined(POINTING_DEVICE_ROTATION_90)
  72. mouseReport.x = y;
  73. mouseReport.y = -x;
  74. # elif defined(POINTING_DEVICE_ROTATION_180)
  75. mouseReport.x = -x;
  76. mouseReport.y = -y;
  77. # elif defined(POINTING_DEVICE_ROTATION_270)
  78. mouseReport.x = -y;
  79. mouseReport.y = x;
  80. # else
  81. # error "How the heck did you get here?!"
  82. # endif
  83. #endif
  84. // Support Inverting the X and Y Axises
  85. #if defined(POINTING_DEVICE_INVERT_X)
  86. mouseReport.x = -mouseReport.x;
  87. #endif
  88. #if defined(POINTING_DEVICE_INVERT_Y)
  89. mouseReport.y = -mouseReport.y;
  90. #endif
  91. // allow kb to intercept and modify report
  92. mouseReport = pointing_device_task_kb(mouseReport);
  93. // combine with mouse report to ensure that the combined is sent correctly
  94. #ifdef MOUSEKEY_ENABLE
  95. report_mouse_t mousekey_report = mousekey_get_report();
  96. mouseReport.buttons = mouseReport.buttons | mousekey_report.buttons;
  97. #endif
  98. pointing_device_send();
  99. }
  100. report_mouse_t pointing_device_get_report(void) { return mouseReport; }
  101. void pointing_device_set_report(report_mouse_t newMouseReport) { mouseReport = newMouseReport; }
  102. uint16_t pointing_device_get_cpi(void) { return pointing_device_driver.get_cpi(); }
  103. void pointing_device_set_cpi(uint16_t cpi) { pointing_device_driver.set_cpi(cpi); }