From b6d6802c6d2a4aa5c7626834dd91c7fc695e9ef3 Mon Sep 17 00:00:00 2001 From: Bastian Winkler Date: Sun, 3 Nov 2019 22:41:18 +0100 Subject: [PATCH 1/3] Process leader mappings on key press --- quantum/process_keycode/process_leader.c | 26 ++++++++++++++++++++++++ quantum/process_keycode/process_leader.h | 3 +++ quantum/quantum.c | 4 ++++ 3 files changed, 33 insertions(+) diff --git a/quantum/process_keycode/process_leader.c b/quantum/process_keycode/process_leader.c index 58a615d85a59..408cc084f3ab 100644 --- a/quantum/process_keycode/process_leader.c +++ b/quantum/process_keycode/process_leader.c @@ -23,10 +23,16 @@ # define LEADER_TIMEOUT 300 # endif +# ifndef LEADER_ABORT +# define LEADER_ABORT KC_LEAD +# endif + __attribute__((weak)) void leader_start(void) {} __attribute__((weak)) void leader_end(void) {} +__attribute__((weak)) bool leader_process_user(uint16_t *leader_sequence, bool is_timeout) { return true; } + // Leader key stuff bool leading = false; uint16_t leader_time = 0; @@ -45,6 +51,14 @@ void qk_leader_start(void) { memset(leader_sequence, 0, sizeof(leader_sequence)); } +void matrix_scan_leader(void) { + if (leading && timer_elapsed(leader_time) > LEADER_TIMEOUT) { + leader_process_user(leader_sequence, true); + leading = false; + leader_end(); + } +} + bool process_leader(uint16_t keycode, keyrecord_t *record) { // Leader key set-up if (record->event.pressed) { @@ -55,9 +69,21 @@ bool process_leader(uint16_t keycode, keyrecord_t *record) { keycode = keycode & 0xFF; } # endif // LEADER_KEY_STRICT_KEY_PROCESSING + if (keycode == LEADER_ABORT) { + leading = false; + leader_end(); + return false; + } if (leader_sequence_size < (sizeof(leader_sequence) / sizeof(leader_sequence[0]))) { leader_sequence[leader_sequence_size] = keycode; leader_sequence_size++; +# ifdef LEADER_ON_KEY_PROCESSING + if (!leader_process_user(leader_sequence, false)) { + leading = false; + leader_end(); + return false; + } +# endif // LEADER_ON_KEY_PROCESSING } else { leading = false; leader_end(); diff --git a/quantum/process_keycode/process_leader.h b/quantum/process_keycode/process_leader.h index e0edf57b3272..cac3bea529d2 100644 --- a/quantum/process_keycode/process_leader.h +++ b/quantum/process_keycode/process_leader.h @@ -20,9 +20,12 @@ #include "quantum.h" bool process_leader(uint16_t keycode, keyrecord_t *record); +void matrix_scan_leader(void); void leader_start(void); void leader_end(void); +bool leader_process_user(uint16_t *leader_sequence, bool is_timeout); + void qk_leader_start(void); #define SEQ_ONE_KEY(key) if (leader_sequence[0] == (key) && leader_sequence[1] == 0 && leader_sequence[2] == 0 && leader_sequence[3] == 0 && leader_sequence[4] == 0) diff --git a/quantum/quantum.c b/quantum/quantum.c index 1f17c6ff71d4..3e6783e32056 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c @@ -982,6 +982,10 @@ void matrix_scan_quantum() { matrix_scan_combo(); #endif +#if defined(LEADER_ENABLE) && defined(LEADER_ON_KEY_PROCESSING) + matrix_scan_leader(); +#endif + #if defined(BACKLIGHT_ENABLE) # if defined(LED_MATRIX_ENABLE) led_matrix_task(); From 460279eb1235f070f54724e2137fb9a7d77cc0f8 Mon Sep 17 00:00:00 2001 From: Bastian Winkler Date: Wed, 6 Nov 2019 09:29:45 +0100 Subject: [PATCH 2/3] leader: Move on key processing guard to matrix_scan_leader --- quantum/process_keycode/process_leader.c | 2 ++ quantum/quantum.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/quantum/process_keycode/process_leader.c b/quantum/process_keycode/process_leader.c index 408cc084f3ab..849722293719 100644 --- a/quantum/process_keycode/process_leader.c +++ b/quantum/process_keycode/process_leader.c @@ -52,11 +52,13 @@ void qk_leader_start(void) { } void matrix_scan_leader(void) { +# ifdef LEADER_ON_KEY_PROCESSING if (leading && timer_elapsed(leader_time) > LEADER_TIMEOUT) { leader_process_user(leader_sequence, true); leading = false; leader_end(); } +# endif } bool process_leader(uint16_t keycode, keyrecord_t *record) { diff --git a/quantum/quantum.c b/quantum/quantum.c index 3e6783e32056..34866967074c 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c @@ -982,7 +982,7 @@ void matrix_scan_quantum() { matrix_scan_combo(); #endif -#if defined(LEADER_ENABLE) && defined(LEADER_ON_KEY_PROCESSING) +#if defined(LEADER_ENABLE) matrix_scan_leader(); #endif From 527df04253ce2a18ec787a8f858e2d5a4c1a07e7 Mon Sep 17 00:00:00 2001 From: Bastian Winkler Date: Wed, 6 Nov 2019 10:12:44 +0100 Subject: [PATCH 3/3] leader: Make LEADER_ABORT optional Otherwise it would break existing code that uses a leader sequence that contains KC_LEAD --- quantum/process_keycode/process_leader.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/quantum/process_keycode/process_leader.c b/quantum/process_keycode/process_leader.c index 849722293719..6f80c1fea457 100644 --- a/quantum/process_keycode/process_leader.c +++ b/quantum/process_keycode/process_leader.c @@ -23,10 +23,6 @@ # define LEADER_TIMEOUT 300 # endif -# ifndef LEADER_ABORT -# define LEADER_ABORT KC_LEAD -# endif - __attribute__((weak)) void leader_start(void) {} __attribute__((weak)) void leader_end(void) {} @@ -71,11 +67,13 @@ bool process_leader(uint16_t keycode, keyrecord_t *record) { keycode = keycode & 0xFF; } # endif // LEADER_KEY_STRICT_KEY_PROCESSING +# ifdef LEADER_ABORT if (keycode == LEADER_ABORT) { leading = false; leader_end(); return false; } +# endif // LEADER_ABORT if (leader_sequence_size < (sizeof(leader_sequence) / sizeof(leader_sequence[0]))) { leader_sequence[leader_sequence_size] = keycode; leader_sequence_size++; @@ -85,7 +83,7 @@ bool process_leader(uint16_t keycode, keyrecord_t *record) { leader_end(); return false; } -# endif // LEADER_ON_KEY_PROCESSING +# endif // LEADER_ON_KEY_PROCESSING } else { leading = false; leader_end();