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.

201 lines
7.5 KiB

  1. // Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "transport_sync.h"
  4. #include "transactions.h"
  5. #include <string.h>
  6. #ifdef UNICODE_COMMON_ENABLE
  7. # include "process_unicode_common.h"
  8. extern unicode_config_t unicode_config;
  9. # include "keyrecords/unicode.h"
  10. #endif
  11. #ifdef AUDIO_ENABLE
  12. # include "audio.h"
  13. extern audio_config_t audio_config;
  14. extern bool delayed_tasks_run;
  15. #endif
  16. #if defined(POINTING_DEVICE_ENABLE) && defined(KEYBOARD_handwired_tractyl_manuform)
  17. extern bool tap_toggling;
  18. #endif
  19. #ifdef SWAP_HANDS_ENABLE
  20. extern bool swap_hands;
  21. #endif
  22. extern userspace_config_t userspace_config;
  23. uint16_t transport_keymap_config = 0;
  24. uint32_t transport_userspace_config = 0, transport_user_state = 0;
  25. user_runtime_config_t user_state;
  26. void user_state_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
  27. if (initiator2target_buffer_size == sizeof(transport_user_state)) {
  28. memcpy(&transport_user_state, initiator2target_buffer, initiator2target_buffer_size);
  29. }
  30. }
  31. void user_keymap_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
  32. if (initiator2target_buffer_size == sizeof(transport_keymap_config)) {
  33. memcpy(&transport_keymap_config, initiator2target_buffer, initiator2target_buffer_size);
  34. }
  35. }
  36. void user_config_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
  37. if (initiator2target_buffer_size == sizeof(transport_userspace_config)) {
  38. memcpy(&transport_userspace_config, initiator2target_buffer, initiator2target_buffer_size);
  39. }
  40. }
  41. #ifdef CUSTOM_OLED_DRIVER
  42. # include "oled/oled_stuff.h"
  43. void keylogger_string_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
  44. if (initiator2target_buffer_size == (OLED_KEYLOGGER_LENGTH+1)) {
  45. memcpy(&oled_keylog_str, initiator2target_buffer, initiator2target_buffer_size);
  46. }
  47. }
  48. #endif
  49. void keyboard_post_init_transport_sync(void) {
  50. // Register keyboard state sync split transaction
  51. transaction_register_rpc(RPC_ID_USER_STATE_SYNC, user_state_sync);
  52. transaction_register_rpc(RPC_ID_USER_KEYMAP_SYNC, user_keymap_sync);
  53. transaction_register_rpc(RPC_ID_USER_CONFIG_SYNC, user_config_sync);
  54. #ifdef CUSTOM_OLED_DRIVER
  55. transaction_register_rpc(RPC_ID_USER_OLED_KEYLOG_STR, keylogger_string_sync);
  56. #endif
  57. }
  58. void user_transport_update(void) {
  59. if (is_keyboard_master()) {
  60. transport_keymap_config = keymap_config.raw;
  61. transport_userspace_config = userspace_config.raw;
  62. #ifdef AUDIO_ENABLE
  63. user_state.audio_enable = is_audio_on();
  64. user_state.audio_clicky_enable = is_clicky_on();
  65. #endif
  66. #if defined(POINTING_DEVICE_ENABLE) && defined(POINTING_DEVICE_AUTO_MOUSE_ENABLE)
  67. user_state.tap_toggling = get_auto_mouse_toggle();
  68. #endif
  69. #ifdef UNICODE_COMMON_ENABLE
  70. user_state.unicode_mode = unicode_config.input_mode;
  71. user_state.unicode_typing_mode = unicode_typing_mode;
  72. #endif
  73. #ifdef SWAP_HANDS_ENABLE
  74. user_state.swap_hands = swap_hands;
  75. #endif
  76. user_state.host_driver_disabled = get_keyboard_lock();
  77. transport_user_state = user_state.raw;
  78. } else {
  79. keymap_config.raw = transport_keymap_config;
  80. userspace_config.raw = transport_userspace_config;
  81. user_state.raw = transport_user_state;
  82. #ifdef UNICODE_COMMON_ENABLE
  83. unicode_config.input_mode = user_state.unicode_mode;
  84. unicode_typing_mode = user_state.unicode_typing_mode;
  85. #endif
  86. #if defined(POINTING_DEVICE_ENABLE) && defined(POINTING_DEVICE_AUTO_MOUSE_ENABLE)
  87. if (get_auto_mouse_toggle() != user_state.tap_toggling) {
  88. auto_mouse_toggle();
  89. }
  90. #endif
  91. #ifdef SWAP_HANDS_ENABLE
  92. swap_hands = user_state.swap_hands;
  93. #endif
  94. set_keyboard_lock(user_state.host_driver_disabled);
  95. }
  96. }
  97. void user_transport_sync(void) {
  98. if (is_keyboard_master()) {
  99. // Keep track of the last state, so that we can tell if we need to propagate to slave
  100. static uint16_t last_keymap = 0;
  101. static uint32_t last_config = 0, last_sync[4], last_user_state = 0;
  102. bool needs_sync = false;
  103. #ifdef CUSTOM_OLED_DRIVER
  104. static char keylog_temp[OLED_KEYLOGGER_LENGTH + 1] = {0};
  105. #endif
  106. // Check if the state values are different
  107. if (memcmp(&transport_user_state, &last_user_state, sizeof(transport_user_state))) {
  108. needs_sync = true;
  109. memcpy(&last_user_state, &transport_user_state, sizeof(transport_user_state));
  110. }
  111. // Send to slave every 500ms regardless of state change
  112. if (timer_elapsed32(last_sync[0]) > 250) {
  113. needs_sync = true;
  114. }
  115. // Perform the sync if requested
  116. if (needs_sync) {
  117. if (transaction_rpc_send(RPC_ID_USER_STATE_SYNC, sizeof(user_state), &user_state)) {
  118. last_sync[0] = timer_read32();
  119. }
  120. needs_sync = false;
  121. }
  122. // Check if the state values are different
  123. if (memcmp(&transport_keymap_config, &last_keymap, sizeof(transport_keymap_config))) {
  124. needs_sync = true;
  125. memcpy(&last_keymap, &transport_keymap_config, sizeof(transport_keymap_config));
  126. }
  127. // Send to slave every 500ms regardless of state change
  128. if (timer_elapsed32(last_sync[1]) > 250) {
  129. needs_sync = true;
  130. }
  131. // Perform the sync if requested
  132. if (needs_sync) {
  133. if (transaction_rpc_send(RPC_ID_USER_KEYMAP_SYNC, sizeof(transport_keymap_config), &transport_keymap_config)) {
  134. last_sync[1] = timer_read32();
  135. }
  136. needs_sync = false;
  137. }
  138. // Check if the state values are different
  139. if (memcmp(&user_state, &last_config, sizeof(transport_userspace_config))) {
  140. needs_sync = true;
  141. memcpy(&last_config, &user_state, sizeof(transport_userspace_config));
  142. }
  143. // Send to slave every 500ms regardless of state change
  144. if (timer_elapsed32(last_sync[2]) > 250) {
  145. needs_sync = true;
  146. }
  147. // Perform the sync if requested
  148. if (needs_sync) {
  149. if (transaction_rpc_send(RPC_ID_USER_CONFIG_SYNC, sizeof(transport_userspace_config), &transport_userspace_config)) {
  150. last_sync[2] = timer_read32();
  151. }
  152. needs_sync = false;
  153. }
  154. #ifdef CUSTOM_OLED_DRIVER
  155. // Check if the state values are different
  156. if (memcmp(&oled_keylog_str, &keylog_temp, OLED_KEYLOGGER_LENGTH + 1)) {
  157. needs_sync = true;
  158. memcpy(&keylog_temp, &oled_keylog_str, OLED_KEYLOGGER_LENGTH + 1);
  159. }
  160. if (timer_elapsed32(last_sync[3]) > 250) {
  161. needs_sync = true;
  162. }
  163. // Perform the sync if requested
  164. if (needs_sync) {
  165. if (transaction_rpc_send(RPC_ID_USER_OLED_KEYLOG_STR, OLED_KEYLOGGER_LENGTH + 1, &oled_keylog_str)) {
  166. last_sync[3] = timer_read32();
  167. }
  168. needs_sync = false;
  169. }
  170. #endif
  171. }
  172. }
  173. void housekeeping_task_transport_sync(void) {
  174. // Update kb_state so we can send to slave
  175. user_transport_update();
  176. // Data sync from master to slave
  177. user_transport_sync();
  178. }