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.

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