transport_sync.c 7.9 KB

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