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.

173 lines
6.6 KiB

  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 "transport_sync.h"
  17. #include "transactions.h"
  18. #include <string.h>
  19. #ifdef UNICODE_ENABLE
  20. extern unicode_config_t unicode_config;
  21. #endif
  22. #ifdef AUDIO_ENABLE
  23. # include "audio.h"
  24. extern audio_config_t audio_config;
  25. extern bool delayed_tasks_run;
  26. #endif
  27. #if defined(POINTING_DEVICE_ENABLE) && defined(KEYBOARD_handwired_tractyl_manuform)
  28. extern bool tap_toggling;
  29. #endif
  30. #ifdef SWAP_HANDS_ENABLE
  31. extern bool swap_hands;
  32. #endif
  33. extern userspace_config_t userspace_config;
  34. extern bool host_driver_disabled;
  35. uint16_t transport_keymap_config = 0;
  36. uint32_t transport_userspace_config = 0, transport_user_state = 0;
  37. user_runtime_config_t user_state;
  38. void user_state_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
  39. if (initiator2target_buffer_size == sizeof(transport_user_state)) {
  40. memcpy(&transport_user_state, initiator2target_buffer, initiator2target_buffer_size);
  41. }
  42. }
  43. void user_keymap_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
  44. if (initiator2target_buffer_size == sizeof(transport_keymap_config)) {
  45. memcpy(&transport_keymap_config, initiator2target_buffer, initiator2target_buffer_size);
  46. }
  47. }
  48. void user_config_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
  49. if (initiator2target_buffer_size == sizeof(transport_userspace_config)) {
  50. memcpy(&transport_userspace_config, initiator2target_buffer, initiator2target_buffer_size);
  51. }
  52. }
  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. }
  59. void user_transport_update(void) {
  60. if (is_keyboard_master()) {
  61. transport_keymap_config = keymap_config.raw;
  62. transport_userspace_config = userspace_config.raw;
  63. #ifdef AUDIO_ENABLE
  64. user_state.audio_enable = is_audio_on();
  65. user_state.audio_clicky_enable = is_clicky_on();
  66. #endif
  67. #if defined(POINTING_DEVICE_ENABLE) && defined(KEYBOARD_handwired_tractyl_manuform)
  68. user_state.tap_toggling = tap_toggling;
  69. #endif
  70. #ifdef UNICODE_ENABLE
  71. user_state.unicode_mode = unicode_config.input_mode;
  72. #endif
  73. #ifdef SWAP_HANDS_ENABLE
  74. user_state.swap_hands = swap_hands;
  75. #endif
  76. user_state.host_driver_disabled = host_driver_disabled;
  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_ENABLE
  83. unicode_config.input_mode = user_state.unicode_mode;
  84. #endif
  85. #if defined(POINTING_DEVICE_ENABLE) && defined(KEYBOARD_handwired_tractyl_manuform)
  86. tap_toggling = user_state.tap_toggling;
  87. #endif
  88. #ifdef SWAP_HANDS_ENABLE
  89. swap_hands = user_state.swap_hands;
  90. #endif
  91. host_driver_disabled = user_state.host_driver_disabled;
  92. }
  93. }
  94. void user_transport_sync(void) {
  95. if (is_keyboard_master()) {
  96. // Keep track of the last state, so that we can tell if we need to propagate to slave
  97. static uint16_t last_keymap = 0;
  98. static uint32_t last_config = 0, last_sync[3], last_user_state = 0;
  99. bool needs_sync = false;
  100. // Check if the state values are different
  101. if (memcmp(&transport_user_state, &last_user_state, sizeof(transport_user_state))) {
  102. needs_sync = true;
  103. memcpy(&last_user_state, &transport_user_state, sizeof(transport_user_state));
  104. }
  105. // Send to slave every 500ms regardless of state change
  106. if (timer_elapsed32(last_sync[0]) > 250) {
  107. needs_sync = true;
  108. }
  109. // Perform the sync if requested
  110. if (needs_sync) {
  111. if (transaction_rpc_send(RPC_ID_USER_STATE_SYNC, sizeof(user_state), &user_state)) {
  112. last_sync[0] = timer_read32();
  113. }
  114. needs_sync = false;
  115. }
  116. // Check if the state values are different
  117. if (memcmp(&transport_keymap_config, &last_keymap, sizeof(transport_keymap_config))) {
  118. needs_sync = true;
  119. memcpy(&last_keymap, &transport_keymap_config, sizeof(transport_keymap_config));
  120. }
  121. // Send to slave every 500ms regardless of state change
  122. if (timer_elapsed32(last_sync[1]) > 250) {
  123. needs_sync = true;
  124. }
  125. // Perform the sync if requested
  126. if (needs_sync) {
  127. if (transaction_rpc_send(RPC_ID_USER_KEYMAP_SYNC, sizeof(transport_keymap_config), &transport_keymap_config)) {
  128. last_sync[1] = timer_read32();
  129. }
  130. needs_sync = false;
  131. }
  132. // Check if the state values are different
  133. if (memcmp(&user_state, &last_config, sizeof(transport_userspace_config))) {
  134. needs_sync = true;
  135. memcpy(&last_config, &user_state, sizeof(transport_userspace_config));
  136. }
  137. // Send to slave every 500ms regardless of state change
  138. if (timer_elapsed32(last_sync[2]) > 250) {
  139. needs_sync = true;
  140. }
  141. // Perform the sync if requested
  142. if (needs_sync) {
  143. if (transaction_rpc_send(RPC_ID_USER_CONFIG_SYNC, sizeof(transport_userspace_config), &transport_userspace_config)) {
  144. last_sync[2] = timer_read32();
  145. }
  146. }
  147. }
  148. }
  149. void housekeeping_task_user(void) {
  150. // Update kb_state so we can send to slave
  151. user_transport_update();
  152. // Data sync from master to slave
  153. user_transport_sync();
  154. }