From 3da2111df6439ddf106a3b64ac6be5677dde9fbc Mon Sep 17 00:00:00 2001 From: Stefan Kerkmann Date: Mon, 1 Apr 2024 14:16:29 +0200 Subject: [PATCH 1/3] suspend: suppress wake up keypress Waking the host from suspend is done by pressing any key on the keyboard, the regular key codes assigned to the keys are not important and must not be sent - otherwise they usually end up in password prompts as ghost characters that have to be deleted again. This commit adds suppression for all keys pressed at the time of wake up. Once a key is released it functions as a regular key again. Signed-off-by: Stefan Kerkmann --- platforms/suspend.c | 20 ++++++++++++++++++-- platforms/suspend.h | 3 +++ quantum/keyboard.c | 4 +++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/platforms/suspend.c b/platforms/suspend.c index fea23cbd02b..0818a726ce1 100644 --- a/platforms/suspend.c +++ b/platforms/suspend.c @@ -4,6 +4,8 @@ #include "suspend.h" #include "matrix.h" +static matrix_row_t wakeup_matrix[MATRIX_ROWS]; + // TODO: Move to more correct location __attribute__((weak)) void matrix_power_up(void) {} __attribute__((weak)) void matrix_power_down(void) {} @@ -44,8 +46,22 @@ bool suspend_wakeup_condition(void) { matrix_power_up(); matrix_scan(); matrix_power_down(); + + bool wakeup = false; for (uint8_t r = 0; r < MATRIX_ROWS; r++) { - if (matrix_get_row(r)) return true; + wakeup_matrix[r] = matrix_get_row(r); + wakeup |= wakeup_matrix[r] != 0; + } + + return wakeup; +} + +bool keypress_is_wakeup_key(uint8_t row, uint8_t col) { + return (wakeup_matrix[row] & ((matrix_row_t)1 << col)); +} + +void wakeup_matrix_handle_key_event(uint8_t row, uint8_t col, bool pressed) { + if (!pressed) { + wakeup_matrix[row] &= ~((matrix_row_t)1 << col); } - return false; } diff --git a/platforms/suspend.h b/platforms/suspend.h index e4f7f39ddb8..762bef12dbc 100644 --- a/platforms/suspend.h +++ b/platforms/suspend.h @@ -14,6 +14,9 @@ void suspend_power_down_user(void); void suspend_power_down_kb(void); void suspend_power_down_quantum(void); +bool keypress_is_wakeup_key(uint8_t row, uint8_t col); +void wakeup_matrix_handle_key_event(uint8_t row, uint8_t col, bool pressed); + #ifndef USB_SUSPEND_WAKEUP_DELAY # define USB_SUSPEND_WAKEUP_DELAY 0 #endif diff --git a/quantum/keyboard.c b/quantum/keyboard.c index fccdaf29905..42087c75d38 100644 --- a/quantum/keyboard.c +++ b/quantum/keyboard.c @@ -32,6 +32,7 @@ along with this program. If not, see . #include "sendchar.h" #include "eeconfig.h" #include "action_layer.h" +#include "suspend.h" #ifdef BOOTMAGIC_ENABLE # include "bootmagic.h" #endif @@ -554,6 +555,7 @@ void switch_events(uint8_t row, uint8_t col, bool pressed) { #if defined(RGB_MATRIX_ENABLE) rgb_matrix_handle_key_event(row, col, pressed); #endif + wakeup_matrix_handle_key_event(row, col, pressed); } /** @@ -617,7 +619,7 @@ static bool matrix_task(void) { if (row_changes & col_mask) { const bool key_pressed = current_row & col_mask; - if (process_keypress) { + if (process_keypress && !keypress_is_wakeup_key(row, col)) { action_exec(MAKE_KEYEVENT(row, col, key_pressed)); } From f9dc545b831e4139eebb9d011bbd4e00bcc6a899 Mon Sep 17 00:00:00 2001 From: Stefan Kerkmann Date: Mon, 1 Apr 2024 18:00:36 +0200 Subject: [PATCH 2/3] suspend: update wake up matrix after wake up delay MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If USB_SUSPEND_WAKEUP_DELAY is set, the keyboard sleeps during wake up - which can be up to multiple seconds. To handle key presses and releases in that time frame we have to handle the following cases: 1. Key not pressed before suspend, and not pressed after wakeup → do nothing (normal case). 2. Key not pressed before suspend, but pressed after wakeup → set the wakeup_matrix bit to 1 (so that the press and release events would be suppressed). 3. Key pressed before suspend, but not pressed after wakeup → do nothing (the release event will be generated on the first matrix_task() call after the wakeup). 4. Key pressed before suspend, and still pressed after wakeup → do nothing (the release event will be generated some time later). Signed-off-by: Stefan Kerkmann Co-authored-by: Sergey Vlasov --- platforms/suspend.c | 19 ++++++++++++++++--- platforms/suspend.h | 1 + quantum/keyboard.c | 4 ++-- tmk_core/protocol/chibios/chibios.c | 3 +++ tmk_core/protocol/lufa/lufa.c | 3 +++ tmk_core/protocol/vusb/protocol.c | 3 +++ 6 files changed, 28 insertions(+), 5 deletions(-) diff --git a/platforms/suspend.c b/platforms/suspend.c index 0818a726ce1..4756796ea43 100644 --- a/platforms/suspend.c +++ b/platforms/suspend.c @@ -4,6 +4,7 @@ #include "suspend.h" #include "matrix.h" +extern matrix_row_t matrix_previous[MATRIX_ROWS]; static matrix_row_t wakeup_matrix[MATRIX_ROWS]; // TODO: Move to more correct location @@ -48,14 +49,26 @@ bool suspend_wakeup_condition(void) { matrix_power_down(); bool wakeup = false; - for (uint8_t r = 0; r < MATRIX_ROWS; r++) { - wakeup_matrix[r] = matrix_get_row(r); - wakeup |= wakeup_matrix[r] != 0; + for (uint8_t row = 0; row < MATRIX_ROWS; row++) { + wakeup_matrix[row] = matrix_get_row(row); + wakeup |= wakeup_matrix[row] != 0; } return wakeup; } +void update_matrix_state_after_wakeup(void) { + matrix_power_up(); + matrix_scan(); + matrix_power_down(); + + for (uint8_t row = 0; row < MATRIX_ROWS; row++) { + const matrix_row_t current_row = matrix_get_row(row); + wakeup_matrix[row] |= current_row & ~matrix_previous[row]; + matrix_previous[row] |= current_row; + } +} + bool keypress_is_wakeup_key(uint8_t row, uint8_t col) { return (wakeup_matrix[row] & ((matrix_row_t)1 << col)); } diff --git a/platforms/suspend.h b/platforms/suspend.h index 762bef12dbc..3a7f51f9f07 100644 --- a/platforms/suspend.h +++ b/platforms/suspend.h @@ -15,6 +15,7 @@ void suspend_power_down_kb(void); void suspend_power_down_quantum(void); bool keypress_is_wakeup_key(uint8_t row, uint8_t col); +void update_matrix_state_after_wakeup(void); void wakeup_matrix_handle_key_event(uint8_t row, uint8_t col, bool pressed); #ifndef USB_SUSPEND_WAKEUP_DELAY diff --git a/quantum/keyboard.c b/quantum/keyboard.c index 42087c75d38..7da56cb30ec 100644 --- a/quantum/keyboard.c +++ b/quantum/keyboard.c @@ -571,6 +571,8 @@ static inline void generate_tick_event(void) { } } +matrix_row_t matrix_previous[MATRIX_ROWS]; + /** * @brief This task scans the keyboards matrix and processes any key presses * that occur. @@ -584,8 +586,6 @@ static bool matrix_task(void) { return false; } - static matrix_row_t matrix_previous[MATRIX_ROWS]; - matrix_scan(); bool matrix_changed = false; for (uint8_t row = 0; row < MATRIX_ROWS && !matrix_changed; row++) { diff --git a/tmk_core/protocol/chibios/chibios.c b/tmk_core/protocol/chibios/chibios.c index cf948154f9b..7c78bf5afe3 100644 --- a/tmk_core/protocol/chibios/chibios.c +++ b/tmk_core/protocol/chibios/chibios.c @@ -193,6 +193,9 @@ void protocol_pre_task(void) { // // Pause for a while to let things settle... wait_ms(USB_SUSPEND_WAKEUP_DELAY); + // ...and then update the wakeup matrix again as the waking key + // might have been released during the delay + update_matrix_state_after_wakeup(); # endif } } diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 81da035f0c0..4f026319136 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -836,6 +836,9 @@ void protocol_pre_task(void) { // // Pause for a while to let things settle... wait_ms(USB_SUSPEND_WAKEUP_DELAY); + // ...and then update the wakeup matrix again as the waking key + // might have been released during the delay + update_matrix_state_after_wakeup(); # endif } } diff --git a/tmk_core/protocol/vusb/protocol.c b/tmk_core/protocol/vusb/protocol.c index 41ccf451fdb..b1851f1f1d0 100644 --- a/tmk_core/protocol/vusb/protocol.c +++ b/tmk_core/protocol/vusb/protocol.c @@ -145,6 +145,9 @@ void protocol_pre_task(void) { // // Pause for a while to let things settle... wait_ms(USB_SUSPEND_WAKEUP_DELAY); + // ...and then update the wakeup matrix again as the waking key + // might have been released during the delay + update_matrix_state_after_wakeup(); # endif } } From 564ee44a11711c191cc1b0d7b59b25eb2e76efa7 Mon Sep 17 00:00:00 2001 From: Stefan Kerkmann Date: Wed, 19 Mar 2025 20:28:28 +0100 Subject: [PATCH 3/3] keyboards: anavi: macropad8: disable snake and rgb_test effects ...to shrink the binary size. --- keyboards/anavi/macropad8/keyboard.json | 2 -- 1 file changed, 2 deletions(-) diff --git a/keyboards/anavi/macropad8/keyboard.json b/keyboards/anavi/macropad8/keyboard.json index d70d3fa0475..6fe56d4560d 100644 --- a/keyboards/anavi/macropad8/keyboard.json +++ b/keyboards/anavi/macropad8/keyboard.json @@ -21,11 +21,9 @@ "breathing": true, "rainbow_mood": true, "rainbow_swirl": true, - "snake": true, "knight": true, "christmas": true, "static_gradient": true, - "rgb_test": true, "alternating": true, "twinkle": true }