diff --git a/src/kaleidoscope/Runtime.cpp b/src/kaleidoscope/Runtime.cpp index 4cb790b3..4e06f577 100644 --- a/src/kaleidoscope/Runtime.cpp +++ b/src/kaleidoscope/Runtime.cpp @@ -21,7 +21,6 @@ namespace kaleidoscope { uint32_t Runtime_::millis_at_cycle_start_; -Key Runtime_::active_keys_[kaleidoscope_internal::device.numKeys()]; Runtime_::Runtime_(void) { } @@ -43,10 +42,6 @@ Runtime_::setup(void) { device().setup(); - // Clear the active keys array (all keys idle at start) - for (auto key_addr : KeyAddr::all()) { - updateActiveKey(key_addr, Key_Transparent); - } Layer.setup(); } diff --git a/src/kaleidoscope/Runtime.h b/src/kaleidoscope/Runtime.h index deb4021b..7a4c0218 100644 --- a/src/kaleidoscope/Runtime.h +++ b/src/kaleidoscope/Runtime.h @@ -128,16 +128,8 @@ class Runtime_ { return kaleidoscope::Hooks::onFocusEvent(command); } - static Key activeKey(KeyAddr key_addr) { - return active_keys_[key_addr.toInt()]; - } - static void updateActiveKey(KeyAddr key_addr, Key key) { - active_keys_[key_addr.toInt()] = key; - } - private: static uint32_t millis_at_cycle_start_; - static Key active_keys_[kaleidoscope_internal::device.numKeys()]; }; extern kaleidoscope::Runtime_ Runtime; diff --git a/src/kaleidoscope/key_events.cpp b/src/kaleidoscope/key_events.cpp index 6f7ce5f3..f6198877 100644 --- a/src/kaleidoscope/key_events.cpp +++ b/src/kaleidoscope/key_events.cpp @@ -123,9 +123,9 @@ void handleKeyswitchEvent(Key mappedKey, KeyAddr key_addr, uint8_t keyState) { // Update the active keys cache if the key toggled on or off. if (key_addr.isValid()) { if (keyToggledOn(keyState)) { - Runtime.updateActiveKey(key_addr, mappedKey); + Layer.updateLiveKeymap(key_addr, mappedKey); } else if (keyToggledOff(keyState)) { - Runtime.updateActiveKey(key_addr, Key_Transparent); + Layer.updateLiveKeymap(key_addr, Key_Transparent); } } diff --git a/src/kaleidoscope/layers.cpp b/src/kaleidoscope/layers.cpp index 2ada0ffd..fdbafb8b 100644 --- a/src/kaleidoscope/layers.cpp +++ b/src/kaleidoscope/layers.cpp @@ -42,17 +42,27 @@ uint32_t Layer_::layer_state_; uint8_t Layer_::active_layer_count_ = 1; int8_t Layer_::active_layers_[31]; +Key Layer_::live_keymap_[Runtime.device().numKeys()]; uint8_t Layer_::active_layer_keymap_[Runtime.device().numKeys()]; Layer_::GetKeyFunction Layer_::getKey = &Layer_::getKeyFromPROGMEM; + + + void Layer_::setup() { // Explicitly set layer 0's state to 1 bitSet(layer_state_, 0); // Update the active layer cache (every entry will be `0` to start) Layer.updateActiveLayers(); + + Layer.setupLiveKeymap(); + + } + + void Layer_::handleKeymapKeyswitchEvent(Key keymapEntry, uint8_t keyState) { if (keymapEntry.getKeyCode() >= LAYER_MOVE_OFFSET) { if (keyToggledOn(keyState)) { @@ -119,15 +129,7 @@ Key Layer_::getKeyFromPROGMEM(uint8_t layer, KeyAddr key_addr) { } // Deprecated -void Layer_::updateLiveCompositeKeymap(KeyAddr key_addr) { - // We could update the active keys cache here (as commented below), but I - // think that's unlikely to serve whatever purpose the caller had in - // mind. `Layer.lookup()` will still give the correct result, and without a - // `Key` value is specified, this function no longer serves a purpose. - - // int8_t layer = active_layer_keymap_[key_addr.toInt()]; - // Runtime.updateActiveKey(key_addr, (*getKey)(layer, key_addr)); -} +void Layer_::updateLiveCompositeKeymap(KeyAddr key_addr) { } void Layer_::updateActiveLayers(void) { memset(active_layer_keymap_, 0, Runtime.device().numKeys()); @@ -233,6 +235,25 @@ void Layer_::forEachActiveLayer(forEachHandler h) { } } + +void Layer_::setupLiveKeymap() { + // Clear the active keys array (all keys idle at start) + for (auto key_addr : KeyAddr::all()) { + Layer.updateLiveKeymap(key_addr, Key_Transparent); + } + + +} +Key Layer_::lookupOnLiveKeymap(KeyAddr key_addr) { + return live_keymap_[key_addr.toInt()]; + } +void Layer_::updateLiveKeymap(KeyAddr key_addr, Key key) { + live_keymap_[key_addr.toInt()] = key; +} + + + + } kaleidoscope::Layer_ Layer; diff --git a/src/kaleidoscope/layers.h b/src/kaleidoscope/layers.h index 5e6ba05a..cba18b45 100644 --- a/src/kaleidoscope/layers.h +++ b/src/kaleidoscope/layers.h @@ -20,7 +20,6 @@ #include "kaleidoscope/key_defs.h" #include "kaleidoscope/keymaps.h" #include "kaleidoscope/device/device.h" -#include "kaleidoscope/Runtime.h" #include "kaleidoscope_internal/device.h" #include "kaleidoscope_internal/sketch_exploration/sketch_exploration.h" #include "kaleidoscope_internal/shortname.h" @@ -53,7 +52,7 @@ class Layer_ { Layer_() {} void setup(); - + /* There are two lookup functions, because we have two caches, and different * parts of the firmware will want to use either this or that (or perhaps * both, in rare cases). @@ -85,17 +84,22 @@ class Layer_ { */ static Key lookup(KeyAddr key_addr) { // First check the active keys array - Key key = Runtime.activeKey(key_addr); + Key key = lookupOnLiveKeymap(key_addr); // If that entry is clear, look up the entry from the active keymap layers if (key == Key_Transparent) { key = lookupOnActiveLayer(key_addr); } return key; } + + + static Key lookupOnLiveKeymap(KeyAddr key_addr); + static Key lookupOnActiveLayer(KeyAddr key_addr) { uint8_t layer = active_layer_keymap_[key_addr.toInt()]; return (*getKey)(layer, key_addr); } + static uint8_t lookupActiveLayer(KeyAddr key_addr) { return active_layer_keymap_[key_addr.toInt()]; } @@ -133,13 +137,19 @@ class Layer_ { DEPRECATED(LAYER_UPDATELIVECOMPOSITEKEYMAP) static void updateLiveCompositeKeymap(KeyAddr key_addr, Key mappedKey) { - Runtime.updateActiveKey(key_addr, mappedKey); + updateLiveKeymap(key_addr, mappedKey); } DEPRECATED(LAYER_UPDATELIVECOMPOSITEKEYMAP) static void updateLiveCompositeKeymap(KeyAddr key_addr); + + static void updateActiveLayers(void); + static void updateLiveKeymap(KeyAddr key_addr, Key key); + + private: + static void setupLiveKeymap(); using forEachHandler = void(*)(uint8_t index, uint8_t layer); public: @@ -149,7 +159,10 @@ class Layer_ { static uint32_t layer_state_; static uint8_t active_layer_count_; static int8_t active_layers_[31]; + static Key live_keymap_[kaleidoscope_internal::device.numKeys()]; static uint8_t active_layer_keymap_[kaleidoscope_internal::device.numKeys()]; + + }; } diff --git a/src/kaleidoscope/plugin/ShapeShifter.cpp b/src/kaleidoscope/plugin/ShapeShifter.cpp index b2058daa..aa3e9f9e 100644 --- a/src/kaleidoscope/plugin/ShapeShifter.cpp +++ b/src/kaleidoscope/plugin/ShapeShifter.cpp @@ -17,6 +17,7 @@ #include #include "kaleidoscope/keyswitch_state.h" +#include "kaleidoscope/layers.h" namespace kaleidoscope { namespace plugin { @@ -50,7 +51,7 @@ EventHandlerResult ShapeShifter::onKeyswitchEvent(Key &mapped_key, KeyAddr key_a bool shift_detected = false; for (KeyAddr k : KeyAddr::all()) { - if (Runtime.activeKey(k).isKeyboardShift()) + if (Layer.lookupOnLiveKeymap(k).isKeyboardShift()) shift_detected = true; } if (! shift_detected) diff --git a/src/kaleidoscope/plugin/TapDance.cpp b/src/kaleidoscope/plugin/TapDance.cpp index 829080e5..0f044fa3 100644 --- a/src/kaleidoscope/plugin/TapDance.cpp +++ b/src/kaleidoscope/plugin/TapDance.cpp @@ -46,7 +46,7 @@ void TapDance::actionKeys(uint8_t tap_count, break; { KeyAddr td_addr = event_queue_.addr(0); - bool key_released = (Runtime.activeKey(td_addr) == Key_Transparent); + bool key_released = (Layer.lookupOnLiveKeymap(td_addr) == Key_Transparent); handleKeyswitchEvent(key, td_addr, IS_PRESSED | INJECTED); if (key_released) release_addr_ = td_addr;