2021-12-30 10:17:34 +06:00
|
|
|
// Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2021-08-22 02:34:44 +06:00
|
|
|
|
|
|
|
#include "transport_sync.h"
|
|
|
|
#include "transactions.h"
|
|
|
|
#include <string.h>
|
|
|
|
|
2022-01-31 03:20:33 +06:00
|
|
|
#ifdef UNICODE_COMMON_ENABLE
|
2022-01-22 09:36:52 +06:00
|
|
|
# include "process_unicode_common.h"
|
2021-08-08 10:40:48 +06:00
|
|
|
extern unicode_config_t unicode_config;
|
2022-05-31 11:02:55 +06:00
|
|
|
# include "keyrecords/unicode.h"
|
2021-08-22 02:34:44 +06:00
|
|
|
#endif
|
|
|
|
#ifdef AUDIO_ENABLE
|
|
|
|
# include "audio.h"
|
2021-09-30 00:16:11 +06:00
|
|
|
extern audio_config_t audio_config;
|
|
|
|
extern bool delayed_tasks_run;
|
2021-08-22 02:34:44 +06:00
|
|
|
#endif
|
2022-08-29 01:55:19 +06:00
|
|
|
#if defined(OLED_ENABLE) && !defined(SPLIT_OLED_ENABLE) && defined(CUSTOM_OLED_DRIVER)
|
|
|
|
extern bool is_oled_enabled;
|
|
|
|
#endif
|
2021-08-22 02:34:44 +06:00
|
|
|
#if defined(POINTING_DEVICE_ENABLE) && defined(KEYBOARD_handwired_tractyl_manuform)
|
2021-08-08 10:40:48 +06:00
|
|
|
extern bool tap_toggling;
|
2021-08-22 02:34:44 +06:00
|
|
|
#endif
|
|
|
|
#ifdef SWAP_HANDS_ENABLE
|
2021-08-08 10:40:48 +06:00
|
|
|
extern bool swap_hands;
|
2021-08-22 02:34:44 +06:00
|
|
|
#endif
|
2021-12-30 10:17:34 +06:00
|
|
|
|
2021-08-08 10:40:48 +06:00
|
|
|
extern userspace_config_t userspace_config;
|
2021-10-16 11:30:23 +06:00
|
|
|
extern bool host_driver_disabled;
|
2021-08-08 10:40:48 +06:00
|
|
|
|
|
|
|
uint16_t transport_keymap_config = 0;
|
2021-10-16 11:30:23 +06:00
|
|
|
uint32_t transport_userspace_config = 0, transport_user_state = 0;
|
2021-08-08 10:40:48 +06:00
|
|
|
|
2021-07-01 13:42:32 +06:00
|
|
|
user_runtime_config_t user_state;
|
|
|
|
|
2021-08-08 10:40:48 +06:00
|
|
|
void user_state_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
|
2021-10-16 11:30:23 +06:00
|
|
|
if (initiator2target_buffer_size == sizeof(transport_user_state)) {
|
|
|
|
memcpy(&transport_user_state, initiator2target_buffer, initiator2target_buffer_size);
|
2021-07-01 13:42:32 +06:00
|
|
|
}
|
|
|
|
}
|
2021-08-08 10:40:48 +06:00
|
|
|
void user_keymap_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
|
|
|
|
if (initiator2target_buffer_size == sizeof(transport_keymap_config)) {
|
|
|
|
memcpy(&transport_keymap_config, initiator2target_buffer, initiator2target_buffer_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void user_config_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
|
|
|
|
if (initiator2target_buffer_size == sizeof(transport_userspace_config)) {
|
|
|
|
memcpy(&transport_userspace_config, initiator2target_buffer, initiator2target_buffer_size);
|
|
|
|
}
|
|
|
|
}
|
2021-07-01 13:42:32 +06:00
|
|
|
|
2022-03-07 12:12:17 +06:00
|
|
|
#ifdef CUSTOM_OLED_DRIVER
|
2022-01-31 03:20:33 +06:00
|
|
|
# include "oled/oled_stuff.h"
|
2022-01-22 09:36:52 +06:00
|
|
|
void keylogger_string_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
|
2023-03-01 01:14:48 +06:00
|
|
|
if (initiator2target_buffer_size == (OLED_KEYLOGGER_LENGTH)) {
|
2022-01-22 09:36:52 +06:00
|
|
|
memcpy(&keylog_str, initiator2target_buffer, initiator2target_buffer_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2021-12-30 10:17:34 +06:00
|
|
|
|
2021-07-01 13:42:32 +06:00
|
|
|
void keyboard_post_init_transport_sync(void) {
|
|
|
|
// Register keyboard state sync split transaction
|
2021-08-08 10:40:48 +06:00
|
|
|
transaction_register_rpc(RPC_ID_USER_STATE_SYNC, user_state_sync);
|
|
|
|
transaction_register_rpc(RPC_ID_USER_KEYMAP_SYNC, user_keymap_sync);
|
|
|
|
transaction_register_rpc(RPC_ID_USER_CONFIG_SYNC, user_config_sync);
|
2022-03-07 12:12:17 +06:00
|
|
|
#ifdef CUSTOM_OLED_DRIVER
|
2022-01-22 09:36:52 +06:00
|
|
|
transaction_register_rpc(RPC_ID_USER_KEYLOG_STR, keylogger_string_sync);
|
|
|
|
#endif
|
2021-07-01 13:42:32 +06:00
|
|
|
}
|
|
|
|
|
2021-08-08 10:40:48 +06:00
|
|
|
void user_transport_update(void) {
|
2021-07-01 13:42:32 +06:00
|
|
|
if (is_keyboard_master()) {
|
2021-08-08 10:40:48 +06:00
|
|
|
transport_keymap_config = keymap_config.raw;
|
|
|
|
transport_userspace_config = userspace_config.raw;
|
2021-08-22 02:34:44 +06:00
|
|
|
#ifdef AUDIO_ENABLE
|
2021-08-08 10:40:48 +06:00
|
|
|
user_state.audio_enable = is_audio_on();
|
|
|
|
user_state.audio_clicky_enable = is_clicky_on();
|
2021-08-22 02:34:44 +06:00
|
|
|
#endif
|
2022-08-29 01:55:19 +06:00
|
|
|
#if defined(OLED_ENABLE) && !defined(SPLIT_OLED_ENABLE) && defined(CUSTOM_OLED_DRIVER)
|
|
|
|
user_state.is_oled_enabled = is_oled_enabled;
|
|
|
|
#endif
|
2022-09-26 02:04:00 +06:00
|
|
|
#if defined(POINTING_DEVICE_ENABLE) && defined(POINTING_DEVICE_AUTO_MOUSE_ENABLE)
|
|
|
|
user_state.tap_toggling = get_auto_mouse_toggle();
|
2021-08-22 02:34:44 +06:00
|
|
|
#endif
|
2022-01-31 03:20:33 +06:00
|
|
|
#ifdef UNICODE_COMMON_ENABLE
|
2022-07-03 08:55:46 +06:00
|
|
|
user_state.unicode_mode = unicode_config.input_mode;
|
2022-11-30 01:43:42 +06:00
|
|
|
user_state.unicode_typing_mode = unicode_typing_mode;
|
2021-09-30 00:16:11 +06:00
|
|
|
#endif
|
2021-08-22 02:34:44 +06:00
|
|
|
#ifdef SWAP_HANDS_ENABLE
|
2021-08-08 10:40:48 +06:00
|
|
|
user_state.swap_hands = swap_hands;
|
2021-08-22 02:34:44 +06:00
|
|
|
#endif
|
2021-10-16 11:30:23 +06:00
|
|
|
user_state.host_driver_disabled = host_driver_disabled;
|
|
|
|
|
|
|
|
transport_user_state = user_state.raw;
|
2021-07-01 13:42:32 +06:00
|
|
|
} else {
|
2021-08-08 10:40:48 +06:00
|
|
|
keymap_config.raw = transport_keymap_config;
|
|
|
|
userspace_config.raw = transport_userspace_config;
|
2021-10-16 11:30:23 +06:00
|
|
|
user_state.raw = transport_user_state;
|
2022-01-31 03:20:33 +06:00
|
|
|
#ifdef UNICODE_COMMON_ENABLE
|
2021-08-08 10:40:48 +06:00
|
|
|
unicode_config.input_mode = user_state.unicode_mode;
|
2022-11-30 01:43:42 +06:00
|
|
|
unicode_typing_mode = user_state.unicode_typing_mode;
|
2021-08-22 02:34:44 +06:00
|
|
|
#endif
|
2022-08-29 01:55:19 +06:00
|
|
|
#if defined(OLED_ENABLE) && !defined(SPLIT_OLED_ENABLE) && defined(CUSTOM_OLED_DRIVER)
|
|
|
|
is_oled_enabled = user_state.is_oled_enabled;
|
|
|
|
#endif
|
2022-09-26 02:04:00 +06:00
|
|
|
#if defined(POINTING_DEVICE_ENABLE) && defined(POINTING_DEVICE_AUTO_MOUSE_ENABLE)
|
|
|
|
if (get_auto_mouse_toggle() != user_state.tap_toggling) {
|
|
|
|
auto_mouse_toggle();
|
|
|
|
}
|
2021-08-22 02:34:44 +06:00
|
|
|
#endif
|
|
|
|
#ifdef SWAP_HANDS_ENABLE
|
2021-08-08 10:40:48 +06:00
|
|
|
swap_hands = user_state.swap_hands;
|
2021-08-22 02:34:44 +06:00
|
|
|
#endif
|
2021-10-16 11:30:23 +06:00
|
|
|
host_driver_disabled = user_state.host_driver_disabled;
|
2021-07-01 13:42:32 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-08 10:40:48 +06:00
|
|
|
void user_transport_sync(void) {
|
2021-07-01 13:42:32 +06:00
|
|
|
if (is_keyboard_master()) {
|
|
|
|
// Keep track of the last state, so that we can tell if we need to propagate to slave
|
2022-01-22 09:36:52 +06:00
|
|
|
static uint16_t last_keymap = 0;
|
|
|
|
static uint32_t last_config = 0, last_sync[4], last_user_state = 0;
|
|
|
|
bool needs_sync = false;
|
2022-03-07 12:12:17 +06:00
|
|
|
#ifdef CUSTOM_OLED_DRIVER
|
2022-01-31 03:20:33 +06:00
|
|
|
static char keylog_temp[OLED_KEYLOGGER_LENGTH] = {0};
|
2022-01-22 09:36:52 +06:00
|
|
|
#endif
|
2021-07-01 13:42:32 +06:00
|
|
|
|
|
|
|
// Check if the state values are different
|
2021-10-16 11:30:23 +06:00
|
|
|
if (memcmp(&transport_user_state, &last_user_state, sizeof(transport_user_state))) {
|
2021-07-01 13:42:32 +06:00
|
|
|
needs_sync = true;
|
2021-10-16 11:30:23 +06:00
|
|
|
memcpy(&last_user_state, &transport_user_state, sizeof(transport_user_state));
|
2021-07-01 13:42:32 +06:00
|
|
|
}
|
|
|
|
// Send to slave every 500ms regardless of state change
|
2021-08-08 10:40:48 +06:00
|
|
|
if (timer_elapsed32(last_sync[0]) > 250) {
|
2021-07-01 13:42:32 +06:00
|
|
|
needs_sync = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the sync if requested
|
|
|
|
if (needs_sync) {
|
|
|
|
if (transaction_rpc_send(RPC_ID_USER_STATE_SYNC, sizeof(user_state), &user_state)) {
|
2021-08-08 10:40:48 +06:00
|
|
|
last_sync[0] = timer_read32();
|
|
|
|
}
|
|
|
|
needs_sync = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the state values are different
|
|
|
|
if (memcmp(&transport_keymap_config, &last_keymap, sizeof(transport_keymap_config))) {
|
|
|
|
needs_sync = true;
|
|
|
|
memcpy(&last_keymap, &transport_keymap_config, sizeof(transport_keymap_config));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send to slave every 500ms regardless of state change
|
|
|
|
if (timer_elapsed32(last_sync[1]) > 250) {
|
|
|
|
needs_sync = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the sync if requested
|
|
|
|
if (needs_sync) {
|
|
|
|
if (transaction_rpc_send(RPC_ID_USER_KEYMAP_SYNC, sizeof(transport_keymap_config), &transport_keymap_config)) {
|
|
|
|
last_sync[1] = timer_read32();
|
|
|
|
}
|
|
|
|
needs_sync = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the state values are different
|
|
|
|
if (memcmp(&user_state, &last_config, sizeof(transport_userspace_config))) {
|
|
|
|
needs_sync = true;
|
|
|
|
memcpy(&last_config, &user_state, sizeof(transport_userspace_config));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send to slave every 500ms regardless of state change
|
|
|
|
if (timer_elapsed32(last_sync[2]) > 250) {
|
|
|
|
needs_sync = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the sync if requested
|
|
|
|
if (needs_sync) {
|
|
|
|
if (transaction_rpc_send(RPC_ID_USER_CONFIG_SYNC, sizeof(transport_userspace_config), &transport_userspace_config)) {
|
|
|
|
last_sync[2] = timer_read32();
|
2021-07-01 13:42:32 +06:00
|
|
|
}
|
2022-01-22 09:36:52 +06:00
|
|
|
needs_sync = false;
|
|
|
|
}
|
|
|
|
|
2022-03-07 12:12:17 +06:00
|
|
|
#ifdef CUSTOM_OLED_DRIVER
|
2022-01-22 09:36:52 +06:00
|
|
|
// Check if the state values are different
|
|
|
|
if (memcmp(&keylog_str, &keylog_temp, OLED_KEYLOGGER_LENGTH)) {
|
|
|
|
needs_sync = true;
|
|
|
|
memcpy(&keylog_temp, &keylog_str, OLED_KEYLOGGER_LENGTH);
|
|
|
|
}
|
|
|
|
if (timer_elapsed32(last_sync[3]) > 250) {
|
|
|
|
needs_sync = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the sync if requested
|
|
|
|
if (needs_sync) {
|
|
|
|
if (transaction_rpc_send(RPC_ID_USER_KEYLOG_STR, OLED_KEYLOGGER_LENGTH, &keylog_str)) {
|
|
|
|
last_sync[3] = timer_read32();
|
|
|
|
}
|
|
|
|
needs_sync = false;
|
2021-07-01 13:42:32 +06:00
|
|
|
}
|
2022-01-22 09:36:52 +06:00
|
|
|
#endif
|
2021-07-01 13:42:32 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-03 08:55:46 +06:00
|
|
|
void housekeeping_task_transport_sync(void) {
|
2021-07-01 13:42:32 +06:00
|
|
|
// Update kb_state so we can send to slave
|
2021-08-08 10:40:48 +06:00
|
|
|
user_transport_update();
|
2021-07-01 13:42:32 +06:00
|
|
|
|
|
|
|
// Data sync from master to slave
|
2021-08-08 10:40:48 +06:00
|
|
|
user_transport_sync();
|
2021-07-01 13:42:32 +06:00
|
|
|
}
|