From c680259b24257b3505d1350e4bde6166da3e9b78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Go=C5=82a=C5=9B?= Date: Wed, 29 Jul 2026 11:39:57 +0200 Subject: [PATCH] kbc.c: Fix delay 0 when typing slowly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The delay was implemented by choosing the next time the key can be sent. It was choosen as the time of sending the previous key + random. This causes slow writing to never trigger any delays as the actual time of a new keypress arriving was often longer than the random delay amount. This change makes it so that the first and every other keycode sent to the OS is delayed with a random delay from the previous one. The first key starting from an empty buffer arrives with a random delay. If the buffer is not empty after sending the keycode, another delay is armed. This causes the key buffer to overflow much faster as every key is delayed by AT LEAST the randomly chosen delay and there is no way to write any faster, so the buffer was expanded yet again. Signed-off-by: Filip Gołaś --- src/board/system76/common/kbc.c | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/board/system76/common/kbc.c b/src/board/system76/common/kbc.c index 06a75964f..22e168dcf 100644 --- a/src/board/system76/common/kbc.c +++ b/src/board/system76/common/kbc.c @@ -83,14 +83,20 @@ static const uint16_t kbc_typematic_period[32] = { }; // clang-format on -static uint8_t kbc_buffer[32] = { 0 }; +static uint8_t kbc_buffer[64] = { 0 }; static uint8_t kbc_buffer_head = 0; static uint8_t kbc_buffer_tail = 0; static bool kbc_buffer_privacy = false; -static uint32_t kbc_buffer_privacy_last_time = 0; +static uint32_t kbc_buffer_privacy_release_at = 0; static uint16_t kbc_buffer_privacy_delay = 0; +static void kbc_buffer_privacy_arm(uint32_t now) { + uint16_t max = (uint16_t)options_get(OPT_KB_PRIVACY_MAX_DELAY_MS); + kbc_buffer_privacy_delay = (max == 0) ? 0 : (uint16_t)(nondeterministic_rng() % max); + kbc_buffer_privacy_release_at = now + kbc_buffer_privacy_delay; +} + static bool kbc_buffer_pop(uint8_t *scancode) { if (kbc_buffer_head == kbc_buffer_tail) { return false; @@ -101,17 +107,24 @@ static bool kbc_buffer_pop(uint8_t *scancode) { } static bool kbc_buffer_push(uint8_t *scancodes, uint8_t len) { + uint8_t i; //TODO: make this test more efficient - for (uint8_t i = 0; i < len; i++) { + for (i = 0; i < len; i++) { if ((kbc_buffer_tail + i + 1U) % ARRAY_SIZE(kbc_buffer) == kbc_buffer_head) { + TRACE(" KB_Priv: buffer full, dropping %d scancodes\n", len); return false; } } - for (uint8_t i = 0; i < len; i++) { + bool was_empty = (kbc_buffer_head == kbc_buffer_tail); + for (i = 0; i < len; i++) { kbc_buffer[kbc_buffer_tail] = scancodes[i]; kbc_buffer_tail = (kbc_buffer_tail + 1U) % ARRAY_SIZE(kbc_buffer); } + + if (was_empty && options_get(OPT_KB_PRIVACY)) { + kbc_buffer_privacy_arm(time_get()); + } return true; } @@ -469,13 +482,14 @@ void kbc_event(struct Kbc *kbc) { if (state == KBC_STATE_NORMAL) { if (options_get(OPT_KB_PRIVACY)) { uint32_t time = time_get(); - if (time - kbc_buffer_privacy_last_time >= (uint32_t)kbc_buffer_privacy_delay) { + if (kbc_buffer_head != kbc_buffer_tail && + (int32_t)(time - kbc_buffer_privacy_release_at) >= 0) { TRACE("KB_Priv: delay of %d passed\n", kbc_buffer_privacy_delay); if (kbc_buffer_pop(&state_data)) { state = KBC_STATE_KEYBOARD; - kbc_buffer_privacy_last_time = time; - kbc_buffer_privacy_delay = (uint16_t)(nondeterministic_rng() % - options_get(OPT_KB_PRIVACY_MAX_DELAY_MS)); + if (kbc_buffer_head != kbc_buffer_tail) { + kbc_buffer_privacy_arm(time); + } } } } else {