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.

179 lines
6.4 KiB

2 years ago
  1. /* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  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 QMK_KEYBOARD_H
  17. #include "pointing_device.h"
  18. #include "transactions.h"
  19. #include <string.h>
  20. #ifdef MOUSEKEY_ENABLE
  21. # include "mousekey.h"
  22. #endif
  23. // typedef struct {
  24. // uint16_t device_cpi;
  25. // } kb_config_data_t;
  26. kb_config_data_t kb_config;
  27. static report_mouse_t shared_mouse_report;
  28. extern const pointing_device_driver_t pointing_device_driver;
  29. void kb_pointer_sync_handler(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
  30. shared_mouse_report = pointing_device_driver.get_report(shared_mouse_report);
  31. memcpy(target2initiator_buffer, &shared_mouse_report, sizeof(report_mouse_t));
  32. shared_mouse_report.x = 0;
  33. shared_mouse_report.y = 0;
  34. shared_mouse_report.h = 0;
  35. shared_mouse_report.v = 0;
  36. }
  37. void kb_config_sync_handler(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
  38. if (initiator2target_buffer_size == sizeof(kb_config)) {
  39. memcpy(&kb_config, initiator2target_buffer, sizeof(kb_config));
  40. }
  41. static uint16_t cpi = 0;
  42. // Check if the state values are different
  43. if (cpi != kb_config.device_cpi) {
  44. cpi = kb_config.device_cpi;
  45. if (!is_keyboard_left()) {
  46. pointing_device_set_cpi(cpi);
  47. }
  48. }
  49. }
  50. void keyboard_pre_init_sync(void) {
  51. memset(&kb_config, 0, sizeof(kb_config));
  52. memset(&shared_mouse_report, 0, sizeof(shared_mouse_report));
  53. }
  54. void keyboard_post_init_sync(void) {
  55. // Register keyboard state sync split transaction
  56. transaction_register_rpc(RPC_ID_KB_CONFIG_SYNC, kb_config_sync_handler);
  57. transaction_register_rpc(RPC_ID_POINTER_STATE_SYNC, kb_pointer_sync_handler);
  58. }
  59. void housekeeping_task_sync(void) {
  60. if (is_keyboard_master()) {
  61. // Keep track of the last state, so that we can tell if we need to propagate to slave
  62. static kb_config_data_t last_kb_config;
  63. static uint32_t last_sync = 0;
  64. bool needs_sync = false;
  65. // Check if the state values are different
  66. if (memcmp(&kb_config, &last_kb_config, sizeof(kb_config))) {
  67. needs_sync = true;
  68. memcpy(&last_kb_config, &kb_config, sizeof(kb_config));
  69. }
  70. // Send to slave every 500ms regardless of state change
  71. if (timer_elapsed32(last_sync) > 500) {
  72. needs_sync = true;
  73. }
  74. // Perform the sync if requested
  75. if (needs_sync) {
  76. if (transaction_rpc_send(RPC_ID_KB_CONFIG_SYNC, sizeof(kb_config), &kb_config)) {
  77. last_sync = timer_read32();
  78. }
  79. }
  80. }
  81. }
  82. void trackball_set_cpi(uint16_t cpi) {
  83. kb_config.device_cpi = cpi;
  84. if (!is_keyboard_left()) {
  85. pointing_device_set_cpi(cpi);
  86. }
  87. }
  88. void pointing_device_task(void) {
  89. if (!is_keyboard_master()) {
  90. return;
  91. }
  92. #if defined(POINTING_DEVICE_TASK_THROTTLE)
  93. static uint32_t last_exec = 0;
  94. if (timer_elapsed32(last_exec) < 1) {
  95. return;
  96. }
  97. last_exec = timer_read32();
  98. #endif
  99. report_mouse_t local_report = pointing_device_get_report();
  100. // Gather report info
  101. #ifdef POINTING_DEVICE_MOTION_PIN
  102. if (!readPin(POINTING_DEVICE_MOTION_PIN))
  103. #endif
  104. #if defined(POINTING_DEVICE_COMBINED)
  105. local_report = pointing_device_driver.get_report(local_report);
  106. transaction_rpc_recv(RPC_ID_POINTER_STATE_SYNC, sizeof(report_mouse_t), &shared_mouse_report);
  107. local_report.x = local_report.x | shared_mouse_report.x;
  108. local_report.y = local_report.y | shared_mouse_report.y;
  109. local_report.h = local_report.h | shared_mouse_report.h;
  110. local_report.v = local_report.v | shared_mouse_report.v;
  111. #elif defined(POINTING_DEVICE_LEFT)
  112. if (is_keyboard_left()) {
  113. local_report = pointing_device_driver.get_report(local_report);
  114. } else {
  115. transaction_rpc_recv(RPC_ID_POINTER_STATE_SYNC, sizeof(report_mouse_t), &local_report);
  116. }
  117. #elif defined(POINTING_DEVICE_RIGHT)
  118. if (!is_keyboard_left()) {
  119. local_report = pointing_device_driver.get_report(local_report);
  120. } else {
  121. transaction_rpc_recv(RPC_ID_POINTER_STATE_SYNC, sizeof(report_mouse_t), &local_report);
  122. }
  123. #else
  124. # error "You need to define the side(s) the pointing device is on. POINTING_DEVICE_COMBINED / POINTING_DEVICE_LEFT / POINTING_DEVICE_RIGHT"
  125. #endif
  126. // Support rotation of the sensor data
  127. #if defined(POINTING_DEVICE_ROTATION_90) || defined(POINTING_DEVICE_ROTATION_180) || defined(POINTING_DEVICE_ROTATION_270)
  128. int8_t x = local_report.x, y = local_report.y;
  129. # if defined(POINTING_DEVICE_ROTATION_90)
  130. local_report.x = y;
  131. local_report.y = -x;
  132. # elif defined(POINTING_DEVICE_ROTATION_180)
  133. local_report.x = -x;
  134. local_report.y = -y;
  135. # elif defined(POINTING_DEVICE_ROTATION_270)
  136. local_report.x = -y;
  137. local_report.y = x;
  138. # else
  139. # error "How the heck did you get here?!"
  140. # endif
  141. #endif
  142. // Support Inverting the X and Y Axises
  143. #if defined(POINTING_DEVICE_INVERT_X)
  144. local_report.x = -local_report.x;
  145. #endif
  146. #if defined(POINTING_DEVICE_INVERT_Y)
  147. local_report.y = -local_report.y;
  148. #endif
  149. // allow kb to intercept and modify report
  150. local_report = pointing_device_task_kb(local_report);
  151. // combine with mouse report to ensure that the combined is sent correctly
  152. #ifdef MOUSEKEY_ENABLE
  153. report_mouse_t mousekey_report = mousekey_get_report();
  154. local_report.buttons = local_report.buttons | mousekey_report.buttons;
  155. #endif
  156. #if defined(POINTING_DEVICE_COMBINED)
  157. local_report.buttons = local_report.buttons | shared_mouse_report.buttons;
  158. #endif
  159. pointing_device_set_report(local_report);
  160. pointing_device_send();
  161. }