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.

86 lines
3.2 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 "tractyl_manuform.h"
  17. #include "transactions.h"
  18. #include <string.h>
  19. kb_config_data_t kb_config;
  20. kb_mouse_report_t sync_mouse_report;
  21. void kb_pointer_sync_handler(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
  22. if (target2initiator_buffer_size == sizeof(sync_mouse_report)) {
  23. memcpy(target2initiator_buffer, &sync_mouse_report, sizeof(sync_mouse_report));
  24. }
  25. sync_mouse_report.x = 0;
  26. sync_mouse_report.y = 0;
  27. }
  28. void kb_config_sync_handler(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
  29. if (initiator2target_buffer_size == sizeof(kb_config)) {
  30. memcpy(&kb_config, initiator2target_buffer, sizeof(kb_config));
  31. }
  32. static uint16_t cpi = 0;
  33. // Check if the state values are different
  34. if (cpi != kb_config.device_cpi) {
  35. cpi = kb_config.device_cpi;
  36. }
  37. }
  38. void keyboard_pre_init_sync(void) {
  39. memset(&kb_config, 0, sizeof(kb_config));
  40. memset(&sync_mouse_report, 0, sizeof(sync_mouse_report));
  41. }
  42. void keyboard_post_init_sync(void) {
  43. // Register keyboard state sync split transaction
  44. transaction_register_rpc(RPC_ID_KB_CONFIG_SYNC, kb_config_sync_handler);
  45. transaction_register_rpc(RPC_ID_POINTER_STATE_SYNC, kb_pointer_sync_handler);
  46. }
  47. void housekeeping_task_sync(void) {
  48. if (is_keyboard_master()) {
  49. // Keep track of the last state, so that we can tell if we need to propagate to slave
  50. static kb_config_data_t last_kb_config;
  51. static uint32_t last_sync = 0;
  52. bool needs_sync = false;
  53. // Check if the state values are different
  54. if (memcmp(&kb_config, &last_kb_config, sizeof(kb_config))) {
  55. needs_sync = true;
  56. memcpy(&last_kb_config, &kb_config, sizeof(kb_config));
  57. }
  58. // Send to slave every 500ms regardless of state change
  59. if (timer_elapsed32(last_sync) > 500) {
  60. needs_sync = true;
  61. }
  62. // Perform the sync if requested
  63. if (needs_sync) {
  64. if (transaction_rpc_send(RPC_ID_KB_CONFIG_SYNC, sizeof(kb_config), &kb_config)) {
  65. last_sync = timer_read32();
  66. }
  67. }
  68. }
  69. }
  70. void trackball_set_cpi(uint16_t cpi) {
  71. kb_config.device_cpi = cpi;
  72. if (!is_keyboard_left()) {
  73. pointing_device_set_cpi(cpi);
  74. }
  75. }