From a5ceb5833a4af1cb9c769b706bbb3ffdf24bd73d Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sat, 21 Apr 2018 11:12:46 +0200 Subject: [PATCH] Use the triggering coordinates when injecting events When we activate a OneShot key, use the coordinates of the (last) physical key that triggered it. This is done by keeping an array of positions for each possible OneShot key. While this costs us 16 bytes of RAM and a bit of code, the benefit is that plugins ordered after OneShot will know where OneShot was triggered from. Fixes #13. Signed-off-by: Gergely Nagy --- src/Kaleidoscope/OneShot.cpp | 12 +++++++++++- src/Kaleidoscope/OneShot.h | 3 +++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Kaleidoscope/OneShot.cpp b/src/Kaleidoscope/OneShot.cpp index b73ebc20..f125242f 100644 --- a/src/Kaleidoscope/OneShot.cpp +++ b/src/Kaleidoscope/OneShot.cpp @@ -33,6 +33,7 @@ Key OneShot::prev_key_; bool OneShot::should_cancel_ = false; bool OneShot::should_cancel_stickies_ = false; bool OneShot::should_mask_on_interrupt_ = false; +uint8_t OneShot::positions_[16]; // --- helper macros ------ @@ -57,9 +58,15 @@ bool OneShot::should_mask_on_interrupt_ = false; #define hasTimedOut() (millis () - start_time_ >= time_out) +void OneShot::positionToCoords(uint8_t pos, byte *row, byte *col) { + *col = pos % COLS; + *row = (pos - *col) / COLS; +} + // ---- OneShot stuff ---- void OneShot::injectNormalKey(uint8_t idx, uint8_t key_state) { Key key; + byte row, col; if (idx < 8) { key.flags = Key_LeftControl.flags; @@ -69,7 +76,8 @@ void OneShot::injectNormalKey(uint8_t idx, uint8_t key_state) { key.keyCode = LAYER_SHIFT_OFFSET + idx - 8; } - handleKeyswitchEvent(key, UNKNOWN_KEYSWITCH_LOCATION, key_state | INJECTED); + positionToCoords(idx, &row, &col); + handleKeyswitchEvent(key, row, col, key_state | INJECTED); } void OneShot::activateOneShot(uint8_t idx) { @@ -100,6 +108,7 @@ Key OneShot::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_st } } else if (keyToggledOn(key_state)) { start_time_ = millis(); + positions_[idx] = row * COLS + col; setPressed(idx); setOneShot(idx); saveAsPrevious(mapped_key); @@ -138,6 +147,7 @@ Key OneShot::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_st } else { start_time_ = millis(); + positions_[idx] = row * COLS + col; setOneShot(idx); saveAsPrevious(mapped_key); diff --git a/src/Kaleidoscope/OneShot.h b/src/Kaleidoscope/OneShot.h index 1f4a8084..3c98c0a6 100644 --- a/src/Kaleidoscope/OneShot.h +++ b/src/Kaleidoscope/OneShot.h @@ -66,6 +66,9 @@ class OneShot : public KaleidoscopePlugin { static bool should_cancel_; static bool should_cancel_stickies_; static bool should_mask_on_interrupt_; + static uint8_t positions_[16]; + + static void positionToCoords(uint8_t pos, byte *row, byte *col); static void injectNormalKey(uint8_t idx, uint8_t key_state); static void activateOneShot(uint8_t idx);