Skip to content

Commit

Permalink
Cache explicitly configured AutoShift key
Browse files Browse the repository at this point in the history
Avoid searching for the mapping again if it is already known.

Signed-off-by: Marco Herrn <[email protected]>
  • Loading branch information
hupfdule committed Apr 13, 2023
1 parent f298751 commit 37a4f96
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,16 @@ bool AutoShift::isExplicitlyMapped(Key key) {
for (uint8_t i{0}; i < explicitmappings_count_; ++i) {
LongPress mappedKey = cloneFromProgmem(explicitmappings_[i]);
if (mappedKey.key == key) {
// cache the mapped key to not have to search it again
mapped_key_.key = mappedKey.key;
mapped_key_.alternate_key = mappedKey.alternate_key;
return true;
}
}

// If no matches were found, return false
// If no matches were found, clear mapped_key_ and return false
mapped_key_.key = Key_Transparent;
mapped_key_.alternate_key = Key_Transparent;
return false;
}

Expand Down Expand Up @@ -209,17 +214,10 @@ void AutoShift::flushEvent(bool is_long_press) {
event.key = Runtime.lookupKey(event.addr);

// If we have an explicit mapping for that key, apply that.
bool mapped= false;
for (uint8_t i{0}; i < explicitmappings_count_; ++i) {
LongPress mappedKey = cloneFromProgmem(explicitmappings_[i]);
if (mappedKey.key == event.key) {
event.key = mappedKey.alternate_key;
mapped= true;
}
}

// If there was no explicit mapping, just add the shift modifier
if (!mapped) {
if (mapped_key_.key != Key_Transparent) {
event.key = mapped_key_.alternate_key;
} else {
// If there was no explicit mapping, just add the shift modifier
// event.key = longpresses[event.key]
uint8_t flags = event.key.getFlags();
flags ^= SHIFT_HELD;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ class AutoShift : public Plugin {
// An array of LongPress objects in PROGMEM.
LongPress const *explicitmappings_{nullptr};
uint8_t explicitmappings_count_{0};

// A cache of the current explicit config key values, so we
// don't have to keep looking them up from PROGMEM.
LongPress mapped_key_ = { .key = Key_Transparent, .alternate_key = Key_Transparent };
};

// =============================================================================
Expand Down

0 comments on commit 37a4f96

Please sign in to comment.