From e0c7388e5d25c94ce038f3e473ddeb980349110d Mon Sep 17 00:00:00 2001 From: Ilya Zhuravlev Date: Sat, 3 Jul 2021 19:48:01 -0400 Subject: [PATCH] vial/tap-dance: allow complex keycodes --- quantum/dynamic_keymap.c | 6 +- quantum/vial.c | 116 +++++++++++++++++++-------------- quantum/vial.h | 5 +- tmk_core/common/action_layer.c | 12 ++++ 4 files changed, 82 insertions(+), 57 deletions(-) diff --git a/quantum/dynamic_keymap.c b/quantum/dynamic_keymap.c index 71d3ad7b37..d7658c2084 100644 --- a/quantum/dynamic_keymap.c +++ b/quantum/dynamic_keymap.c @@ -341,9 +341,7 @@ void dynamic_keymap_set_buffer(uint16_t offset, uint16_t size, uint8_t *data) { } } -#ifdef VIAL_ENCODERS_ENABLE extern uint16_t g_vial_magic_keycode_override; -#endif // This overrides the one in quantum/keymap_common.c uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key) { @@ -351,10 +349,8 @@ uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key) { /* Disable any keycode processing while unlocking */ if (vial_unlock_in_progress) return KC_NO; -#endif -#ifdef VIAL_ENCODERS_ENABLE - if (key.row == VIAL_ENCODER_MATRIX_MAGIC && key.col == VIAL_ENCODER_MATRIX_MAGIC) + if (key.row == VIAL_MATRIX_MAGIC && key.col == VIAL_MATRIX_MAGIC) return g_vial_magic_keycode_override; #endif diff --git a/quantum/vial.c b/quantum/vial.c index 65bfffbdd5..754832cb1c 100644 --- a/quantum/vial.c +++ b/quantum/vial.c @@ -224,37 +224,51 @@ void vial_handle_cmd(uint8_t *msg, uint8_t length) { } } -#ifdef VIAL_ENCODERS_ENABLE uint16_t g_vial_magic_keycode_override; -static void exec_keycode(uint16_t keycode) { -#ifdef VIAL_ENCODER_SIMPLE_TAP - register_code16(keycode); -#if VIAL_ENCODER_KEYCODE_DELAY > 0 - wait_ms(VIAL_ENCODER_KEYCODE_DELAY); -#endif - unregister_code16(keycode); -#else +static void vial_keycode_down(uint16_t keycode) { g_vial_magic_keycode_override = keycode; - keyrecord_t record = {.event = (keyevent_t){.key = { VIAL_ENCODER_MATRIX_MAGIC, VIAL_ENCODER_MATRIX_MAGIC }, .pressed = true, .time = (timer_read() | 1)}}; - - if (keycode <= QK_MODS_MAX) + if (keycode <= QK_MODS_MAX) { register_code16(keycode); - else - process_record_quantum_helper(keycode, &record); + } else { + action_exec((keyevent_t){ + .key = (keypos_t){.row = VIAL_MATRIX_MAGIC, .col = VIAL_MATRIX_MAGIC}, .pressed = 1, .time = (timer_read() | 1) /* time should not be 0 */ + }); + } +} + +static void vial_keycode_up(uint16_t keycode) { + g_vial_magic_keycode_override = keycode; + + if (keycode <= QK_MODS_MAX) { + unregister_code16(keycode); + } else { + action_exec((keyevent_t){ + .key = (keypos_t){.row = VIAL_MATRIX_MAGIC, .col = VIAL_MATRIX_MAGIC}, .pressed = 0, .time = (timer_read() | 1) /* time should not be 0 */ + }); + } +} + +static void vial_keycode_tap(uint16_t keycode) { + vial_keycode_down(keycode); + +#if TAP_CODE_DELAY > 0 + wait_ms(TAP_CODE_DELAY); +#endif + + vial_keycode_up(keycode); +} + +#ifdef VIAL_ENCODERS_ENABLE +static void exec_keycode(uint16_t keycode) { + vial_keycode_down(keycode); #if VIAL_ENCODER_KEYCODE_DELAY > 0 wait_ms(VIAL_ENCODER_KEYCODE_DELAY); #endif - record.event.time = timer_read() | 1; - record.event.pressed = false; - if (keycode <= QK_MODS_MAX) - unregister_code16(keycode); - else - process_record_quantum_helper(keycode, &record); -#endif + vial_keycode_up(keycode); } bool vial_encoder_update(uint8_t index, bool clockwise) { @@ -315,11 +329,11 @@ static void on_dance(qk_tap_dance_state_t *state, void *user_data) { uint16_t kc = td_entry.on_tap; if (kc) { if (state->count == 3) { - tap_code16(kc); - tap_code16(kc); - tap_code16(kc); + vial_keycode_tap(kc); + vial_keycode_tap(kc); + vial_keycode_tap(kc); } else if (state->count > 3) { - tap_code16(kc); + vial_keycode_tap(kc); } } } @@ -332,45 +346,45 @@ static void on_dance_finished(qk_tap_dance_state_t *state, void *user_data) { switch (dance_state[index]) { case SINGLE_TAP: { if (td_entry.on_tap) - register_code16(td_entry.on_tap); + vial_keycode_down(td_entry.on_tap); break; } case SINGLE_HOLD: { if (td_entry.on_hold) - register_code16(td_entry.on_hold); + vial_keycode_down(td_entry.on_hold); else if (td_entry.on_tap) - register_code16(td_entry.on_tap); + vial_keycode_down(td_entry.on_tap); break; } case DOUBLE_TAP: { if (td_entry.on_double_tap) { - register_code16(td_entry.on_double_tap); + vial_keycode_down(td_entry.on_double_tap); } else if (td_entry.on_tap) { - tap_code16(td_entry.on_tap); - register_code16(td_entry.on_tap); + vial_keycode_tap(td_entry.on_tap); + vial_keycode_down(td_entry.on_tap); } break; } case DOUBLE_HOLD: { if (td_entry.on_tap_hold) { - register_code16(td_entry.on_tap_hold); + vial_keycode_down(td_entry.on_tap_hold); } else { if (td_entry.on_tap) { - tap_code16(td_entry.on_tap); + vial_keycode_tap(td_entry.on_tap); if (td_entry.on_hold) - register_code16(td_entry.on_hold); + vial_keycode_down(td_entry.on_hold); else - register_code16(td_entry.on_tap); + vial_keycode_down(td_entry.on_tap); } else if (td_entry.on_hold) { - register_code16(td_entry.on_hold); + vial_keycode_down(td_entry.on_hold); } } break; } case DOUBLE_SINGLE_TAP: { if (td_entry.on_tap) { - tap_code16(td_entry.on_tap); - register_code16(td_entry.on_tap); + vial_keycode_tap(td_entry.on_tap); + vial_keycode_down(td_entry.on_tap); } break; } @@ -381,50 +395,52 @@ static void on_dance_reset(qk_tap_dance_state_t *state, void *user_data) { uint8_t index = (uintptr_t)user_data; if (dynamic_keymap_get_tap_dance(index, &td_entry) != 0) return; - switch (dance_state[index]) { + uint8_t st = dance_state[index]; + state->count = 0; + dance_state[index] = 0; + switch (st) { case SINGLE_TAP: { if (td_entry.on_tap) - unregister_code16(td_entry.on_tap); + vial_keycode_up(td_entry.on_tap); break; } case SINGLE_HOLD: { if (td_entry.on_hold) - unregister_code16(td_entry.on_hold); + vial_keycode_up(td_entry.on_hold); else if (td_entry.on_tap) - unregister_code16(td_entry.on_tap); + vial_keycode_up(td_entry.on_tap); break; } case DOUBLE_TAP: { if (td_entry.on_double_tap) { - unregister_code16(td_entry.on_double_tap); + vial_keycode_up(td_entry.on_double_tap); } else if (td_entry.on_tap) { - unregister_code16(td_entry.on_tap); + vial_keycode_up(td_entry.on_tap); } break; } case DOUBLE_HOLD: { if (td_entry.on_tap_hold) { - unregister_code16(td_entry.on_tap_hold); + vial_keycode_up(td_entry.on_tap_hold); } else { if (td_entry.on_tap) { if (td_entry.on_hold) - unregister_code16(td_entry.on_hold); + vial_keycode_up(td_entry.on_hold); else - unregister_code16(td_entry.on_tap); + vial_keycode_up(td_entry.on_tap); } else if (td_entry.on_hold) { - unregister_code16(td_entry.on_hold); + vial_keycode_up(td_entry.on_hold); } } break; } case DOUBLE_SINGLE_TAP: { if (td_entry.on_tap) { - unregister_code16(td_entry.on_tap); + vial_keycode_up(td_entry.on_tap); } break; } } - dance_state[index] = 0; } qk_tap_dance_action_t tap_dance_actions[VIAL_TAP_DANCE_ENTRIES]; diff --git a/quantum/vial.h b/quantum/vial.h index e5c24f1c0f..84ae0476b8 100644 --- a/quantum/vial.h +++ b/quantum/vial.h @@ -54,8 +54,9 @@ enum { dynamic_vial_tap_dance_set = 0x02, }; -/* Fake encoder position in keyboard matrix, can't use 255 as that is immediately rejected by IS_NOEVENT */ -#define VIAL_ENCODER_MATRIX_MAGIC 254 +/* Fake position in keyboard matrix, can't use 255 as that is immediately rejected by IS_NOEVENT + used to send arbitrary keycodes thru process_record_quantum_helper */ +#define VIAL_MATRIX_MAGIC 254 #ifdef TAP_DANCE_ENABLE diff --git a/tmk_core/common/action_layer.c b/tmk_core/common/action_layer.c index af2d7d964b..3b2bf3096f 100644 --- a/tmk_core/common/action_layer.c +++ b/tmk_core/common/action_layer.c @@ -10,6 +10,10 @@ # include "nodebug.h" #endif +#ifdef VIAL_ENABLE +#include "vial.h" +#endif + /** \brief Default Layer State */ layer_state_t default_layer_state = 0; @@ -192,6 +196,10 @@ uint8_t source_layers_cache[(MATRIX_ROWS * MATRIX_COLS + 7) / 8][MAX_LAYER_BITS] * Updates the cached keys when changing layers */ void update_source_layers_cache(keypos_t key, uint8_t layer) { +#ifdef VIAL_ENABLE + if (key.row == VIAL_MATRIX_MAGIC) return; +#endif + const uint8_t key_number = key.col + (key.row * MATRIX_COLS); const uint8_t storage_row = key_number / 8; const uint8_t storage_bit = key_number % 8; @@ -206,6 +214,10 @@ void update_source_layers_cache(keypos_t key, uint8_t layer) { * reads the cached keys stored when the layer was changed */ uint8_t read_source_layers_cache(keypos_t key) { +#ifdef VIAL_ENABLE + if (key.row == VIAL_MATRIX_MAGIC) return 0; +#endif + const uint8_t key_number = key.col + (key.row * MATRIX_COLS); const uint8_t storage_row = key_number / 8; const uint8_t storage_bit = key_number % 8;