From 0d788bb718ea46576da6f47e714e426519784e71 Mon Sep 17 00:00:00 2001 From: Michael Richters Date: Wed, 11 Nov 2020 11:38:05 -0600 Subject: [PATCH] Fix Qukeys minimum prior interval problem The code for guarding against integer overflow on the prior interval timestamp was in the wrong place, and wouldn't get executed on cycles when the keyboard was idle, leading to a very slim chance of getting the wrong qukey value if all keys were idle long enough (65 seconds). Also fixed the same problem in the first quarter-second after the keyboard power on. Not likely to ever be observed, but costs nothing extra to fix. Signed-off-by: Michael Richters --- src/kaleidoscope/plugin/Qukeys.cpp | 16 ++++++++-------- src/kaleidoscope/plugin/Qukeys.h | 5 ++++- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/kaleidoscope/plugin/Qukeys.cpp b/src/kaleidoscope/plugin/Qukeys.cpp index e9a9637e..67643acc 100644 --- a/src/kaleidoscope/plugin/Qukeys.cpp +++ b/src/kaleidoscope/plugin/Qukeys.cpp @@ -118,6 +118,14 @@ EventHandlerResult Qukeys::beforeReportingState() { } } + // Next, if there hasn't been a keypress in a while, update the prior keypress + // timestamp to avoid integer overflow issues: + if (Runtime.hasTimeExpired(prior_keypress_timestamp_, + minimum_prior_interval_)) { + prior_keypress_timestamp_ = + Runtime.millisAtCycleStart() - (minimum_prior_interval_ + 1); + } + // If any events get flushed from the queue, stop there; we can only safely // send the one report per cycle. if (processQueue()) { @@ -134,14 +142,6 @@ EventHandlerResult Qukeys::beforeReportingState() { queue_head_.primary_key : queue_head_.alternate_key; flushEvent(event_key); } - - // Last, if there hasn't been a keypress in a while, update the prior keypress - // timestamp to avoid integer overflow issues: - if (Runtime.hasTimeExpired(prior_keypress_timestamp_, - minimum_prior_interval_)) { - prior_keypress_timestamp_ = - Runtime.millisAtCycleStart() - (minimum_prior_interval_ + 1); - } return EventHandlerResult::OK; } diff --git a/src/kaleidoscope/plugin/Qukeys.h b/src/kaleidoscope/plugin/Qukeys.h index e4b5d320..78ad0e52 100644 --- a/src/kaleidoscope/plugin/Qukeys.h +++ b/src/kaleidoscope/plugin/Qukeys.h @@ -202,7 +202,10 @@ class Qukeys : public kaleidoscope::Plugin { uint8_t minimum_prior_interval_{75}; // Timestamp of the keypress event immediately prior to the queue head event. - uint16_t prior_keypress_timestamp_{0}; + // The initial value is 256 to ensure that it won't trigger an error if a + // qukey is pressed before `minimum_prior_interval_` milliseconds after the + // keyboard powers on, and that value can only be as high as 255. + uint16_t prior_keypress_timestamp_{256}; // This is a guard against re-processing events when qukeys flushes them from // its event queue. We can't just use an "injected" key state flag, because