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.

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