Skip to content

Instantly share code, notes, and snippets.

@drashna
Created January 9, 2024 01:46
Show Gist options
  • Save drashna/951176bfcdf12ce0b4f4f8b3c7d3dff0 to your computer and use it in GitHub Desktop.
Save drashna/951176bfcdf12ce0b4f4f8b3c7d3dff0 to your computer and use it in GitHub Desktop.
Custom Key log buffer sync
#pragma once
#define OLED_KEYLOGGER_LENGTH 5
#define SPLIT_TRANSACTION_IDS_USER RPC_ID_USER_OLED_KEYLOG_STR
char oled_keylog_str[OLED_KEYLOGGER_LENGTH + 1] = {0};
void add_keylog(uint16_t keycode, keyrecord_t *record) {
if (IS_QK_MOD_TAP(keycode)) {
if (record->tap.count) {
keycode = keycode_config(QK_MOD_TAP_GET_TAP_KEYCODE(keycode));
} else {
keycode = keycode_config(0xE0 + biton(QK_MOD_TAP_GET_MODS(keycode) & 0xF) + biton(QK_MOD_TAP_GET_MODS(keycode) & 0x10));
}
} else if (IS_QK_LAYER_TAP(keycode) && record->tap.count) {
keycode = keycode_config(QK_LAYER_TAP_GET_TAP_KEYCODE(keycode));
} else if (IS_QK_MODS(keycode)) {
keycode = keycode_config(QK_MODS_GET_BASIC_KEYCODE(keycode));
} else if (IS_QK_ONE_SHOT_MOD(keycode)) {
keycode = keycode_config(0xE0 + biton(QK_ONE_SHOT_MOD_GET_MODS(keycode) & 0xF) + biton(QK_ONE_SHOT_MOD_GET_MODS(keycode) & 0x10));
} else if (IS_QK_BASIC(keycode)) {
keycode = keycode_config(keycode);
}
if ((keycode == KC_BSPC) && mod_config(get_mods() | get_oneshot_mods()) & MOD_MASK_CTRL) {
memset(oled_keylog_str, ' ', OLED_KEYLOGGER_LENGTH);
oled_keylog_str[OLED_KEYLOGGER_LENGTH] = '\0';
return;
}
char code = 0;
if (keycode < ARRAY_SIZE(code_to_name)) {
code = pgm_read_byte(&code_to_name[keycode]);
} else {
if (keycode == UC_IRNY) {
code = 0xFD;
} else if (keycode == UC_CLUE) {
code = 0xFE;
}
}
if (code != 0) {
memmove(oled_keylog_str, oled_keylog_str + 1, OLED_KEYLOGGER_LENGTH - 1);
oled_keylog_str[(OLED_KEYLOGGER_LENGTH - 1)] = code;
}
}
bool process_record_user(uint16_t keycode, keyrecord_t *record) {
if (record->event.pressed) {
add_keylog(keycode, record);
}
return true;
}
_Static_assert(sizeof(oled_keylog_str) <= RPC_M2S_BUFFER_SIZE, "Keylog array larger than buffer size!");
void keylogger_string_sync(uint8_t initiator2target_buffer_size, const void *initiator2target_buffer, uint8_t target2initiator_buffer_size, void *target2initiator_buffer) {
if (initiator2target_buffer_size == (OLED_KEYLOGGER_LENGTH + 1)) {
memcpy(&oled_keylog_str, initiator2target_buffer, initiator2target_buffer_size);
}
}
void keyboard_post_init_user(void) {
transaction_register_rpc(RPC_ID_USER_OLED_KEYLOG_STR, keylogger_string_sync);
}
void housekeeping_task_user(void) {
if (is_keyboard_master()) {
bool needs_sync = false;
static uint32_t last_sync = 0, static char keylog_temp[OLED_KEYLOGGER_LENGTH + 1] = {0};
if (memcmp(&oled_keylog_str, &keylog_temp, OLED_KEYLOGGER_LENGTH + 1)) {
needs_sync = true;
memcpy(&keylog_temp, &oled_keylog_str, OLED_KEYLOGGER_LENGTH + 1);
}
if (timer_elapsed32(last_sync) > 250) {
needs_sync = true;
}
// Perform the sync if requested
if (needs_sync) {
if (transaction_rpc_send(RPC_ID_USER_OLED_KEYLOG_STR, OLED_KEYLOGGER_LENGTH + 1, &oled_keylog_str)) {
last_sync = timer_read32();
}
needs_sync = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment