From c9492cef89ce425bf1471bf44f77271044589b19 Mon Sep 17 00:00:00 2001 From: Ilya Zhuravlev Date: Sun, 4 Jul 2021 15:07:31 -0400 Subject: [PATCH] fix combo handling for interrupted combos --- quantum/process_keycode/process_combo.c | 58 ++++++++++++++++--------- quantum/process_keycode/process_combo.h | 9 ++-- 2 files changed, 44 insertions(+), 23 deletions(-) diff --git a/quantum/process_keycode/process_combo.c b/quantum/process_keycode/process_combo.c index 373c49260a..77122eada8 100644 --- a/quantum/process_keycode/process_combo.c +++ b/quantum/process_keycode/process_combo.c @@ -33,7 +33,6 @@ __attribute__((weak)) void process_combo_event(uint16_t combo_index, bool presse static uint16_t timer = 0; static uint16_t current_combo_index = 0; static bool drop_buffer = false; -static bool is_active = false; static bool b_combo_enable = true; // defaults to enabled static uint8_t buffer_size = 0; @@ -75,7 +74,19 @@ static inline void dump_key_buffer(bool emit) { buffer_size = 0; } -#define ALL_COMBO_KEYS_ARE_DOWN (((1 << count) - 1) == combo->state) +static void end_incomplete_combos(void) { +#ifndef COMBO_VARIABLE_LEN + for (current_combo_index = 0; current_combo_index < COMBO_COUNT; ++current_combo_index) { +#else + for (current_combo_index = 0; current_combo_index < COMBO_LEN; ++current_combo_index) { +#endif + combo_t *combo = &key_combos[current_combo_index]; + if (!(combo->state & COMBO_COMPLETE)) + combo->state = 0; + } +} + +#define ALL_COMBO_KEYS_ARE_DOWN (((1 << count) - 1) == (combo->state & ~COMBO_COMPLETE)) #define KEY_STATE_DOWN(key) \ do { \ combo->state |= (1 << key); \ @@ -84,6 +95,7 @@ static inline void dump_key_buffer(bool emit) { do { \ combo->state &= ~(1 << key); \ } while (0) +#define KEY_STATE(key) (combo->state & (1 << (key))) static bool process_single_combo(combo_t *combo, uint16_t keycode, keyrecord_t *record) { uint8_t count = 0; @@ -113,13 +125,15 @@ static bool process_single_combo(combo_t *combo, uint16_t keycode, keyrecord_t * /* Continue processing if not a combo key */ if (-1 == (int8_t)index) return false; - bool is_combo_active = is_active; + bool is_combo_active = true; if (record->event.pressed) { KEY_STATE_DOWN(index); if (is_combo_active) { if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was pressed */ + dump_key_buffer(false); + combo->state |= COMBO_COMPLETE; send_combo(combo->keycode, true); drop_buffer = true; } @@ -127,22 +141,31 @@ static bool process_single_combo(combo_t *combo, uint16_t keycode, keyrecord_t * } else { if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was released */ send_combo(combo->keycode, false); - } else { - /* continue processing without immediately returning */ - is_combo_active = false; } - KEY_STATE_UP(index); + /* don't consume the key unless it is a part of a completed combo */ + is_combo_active = false; + if (KEY_STATE(index)) { + KEY_STATE_UP(index); + + if (combo->state & COMBO_COMPLETE) { + /* Consume the key on release, only if the combo was complete */ + is_combo_active = true; + + /* if all keys are up, reset the combo */ + if (combo->state == COMBO_COMPLETE) + combo->state = 0; + } + } } return is_combo_active; } -#define NO_COMBO_KEYS_ARE_DOWN (0 == combo->state) +#define NO_COMBO_KEYS_ARE_DOWN (0 == (combo->state & ~COMBO_COMPLETE)) bool process_combo(uint16_t keycode, keyrecord_t *record) { bool is_combo_key = false; - drop_buffer = false; bool no_combo_keys_pressed = true; if (keycode == CMB_ON && record->event.pressed) { @@ -177,17 +200,12 @@ bool process_combo(uint16_t keycode, keyrecord_t *record) { /* buffer is only dropped when we complete a combo, so we refresh the timer * here */ timer = timer_read(); - dump_key_buffer(false); + drop_buffer = false; } else if (!is_combo_key) { /* if no combos claim the key we need to emit the keybuffer */ dump_key_buffer(true); - - // reset state if there are no combo keys pressed at all - if (no_combo_keys_pressed) { - timer = 0; - is_active = true; - } - } else if (record->event.pressed && is_active) { + end_incomplete_combos(); + } else if (record->event.pressed) { /* otherwise the key is consumed and placed in the buffer */ timer = timer_read(); @@ -204,19 +222,19 @@ bool process_combo(uint16_t keycode, keyrecord_t *record) { } void matrix_scan_combo(void) { - if (b_combo_enable && is_active && timer && timer_elapsed(timer) > COMBO_TERM) { + if (b_combo_enable && timer && timer_elapsed(timer) > COMBO_TERM) { /* This disables the combo, meaning key events for this * combo will be handled by the next processors in the chain */ - is_active = false; dump_key_buffer(true); + end_incomplete_combos(); } } void combo_enable(void) { b_combo_enable = true; } void combo_disable(void) { - b_combo_enable = is_active = false; + b_combo_enable = false; timer = 0; dump_key_buffer(true); } diff --git a/quantum/process_keycode/process_combo.h b/quantum/process_keycode/process_combo.h index 50a5de24c1..c3203dd0c8 100644 --- a/quantum/process_keycode/process_combo.h +++ b/quantum/process_keycode/process_combo.h @@ -25,11 +25,14 @@ #include #ifdef EXTRA_EXTRA_LONG_COMBOS -# define MAX_COMBO_LENGTH 32 +# define MAX_COMBO_LENGTH 31 +# define COMBO_COMPLETE 0x80000000u #elif EXTRA_LONG_COMBOS -# define MAX_COMBO_LENGTH 16 +# define MAX_COMBO_LENGTH 15 +# define COMBO_COMPLETE 0x8000u #else -# define MAX_COMBO_LENGTH 8 +# define MAX_COMBO_LENGTH 7 +# define COMBO_COMPLETE 0x80u #endif typedef struct {