Skip to content

Commit

Permalink
Yaft: use indices instead of pointers for modifier keys
Browse files Browse the repository at this point in the history
  • Loading branch information
timower committed Feb 9, 2021
1 parent 4e1898d commit bcc2487
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
43 changes: 22 additions & 21 deletions apps/yaft/keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,9 @@ Keyboard::initKeyMap() {
// Setup the keymap.
keys.clear();

shiftKey = nullptr;
altKey = nullptr;
ctrlKey = nullptr;
shiftKey = -1;
altKey = -1;
ctrlKey = -1;

int y = startHeight;
int rowNum = 0;
Expand All @@ -295,18 +295,18 @@ Keyboard::initKeyMap() {
for (const auto& key : row) {
const auto keyWidth = baseKeyWidth * key.width;

keys.emplace_back(
key, Rect{ { x, y }, { x + keyWidth - 1, y + key_height - 1 } });

// Store the modifier keys for later.
if (key.code == Shift) {
shiftKey = &keys.back();
shiftKey = keys.size();
} else if (key.code == Alt) {
altKey = &keys.back();
altKey = keys.size();
} else if (key.code == Ctrl) {
ctrlKey = &keys.back();
ctrlKey = keys.size();
}

keys.emplace_back(
key, Rect{ { x, y }, { x + keyWidth - 1, y + key_height - 1 } });

x += keyWidth;
}

Expand Down Expand Up @@ -406,9 +406,9 @@ Keyboard::sendKeyDown(const Key& key) const {
}

// Lookup modifier state.
bool shift = shiftKey == nullptr ? false : shiftKey->isDown();
bool alt = altKey == nullptr ? false : altKey->isDown();
bool ctrl = ctrlKey == nullptr ? false : ctrlKey->isDown();
bool shift = shiftKey == -1 ? false : keys.at(shiftKey).isDown();
bool alt = altKey == -1 ? false : keys.at(altKey).isDown();
bool ctrl = ctrlKey == -1 ? false : keys.at(ctrlKey).isDown();

bool appCursor = (term->mode & MODE_APP_CURSOR) != 0;

Expand Down Expand Up @@ -552,18 +552,19 @@ handleKeyEvent(Keyboard& kb, const Event& ev) {

// Clear sticky keys.
if (!isModifier(key->info.code)) {
for (auto* key : { kb.shiftKey, kb.altKey, kb.ctrlKey }) {
if (key == nullptr) {
for (auto keyIdx : { kb.shiftKey, kb.altKey, kb.ctrlKey }) {
if (keyIdx == -1) {
continue;
}

key->nextRepeat = time_source::now() + repeat_delay;
if (key->stuck) {
key->stuck = false;
kb.drawKey(*key);
kb.fb->doUpdate(key->keyRect,
rmlib::fb::Waveform::DU,
rmlib::fb::UpdateFlags::None);
auto& key = kb.keys[keyIdx];

key.nextRepeat = time_source::now() + repeat_delay;
if (key.stuck) {
key.stuck = false;
kb.drawKey(key);
kb.fb->doUpdate(
key.keyRect, rmlib::fb::Waveform::DU, rmlib::fb::UpdateFlags::None);
}
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions apps/yaft/keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ struct Keyboard {
rmlib::Point lastMousePos;

// Pointers for tracking modifier state.
Key* shiftKey = nullptr;
Key* altKey = nullptr;
Key* ctrlKey = nullptr;
int shiftKey = -1;
int altKey = -1;
int ctrlKey = -1;

std::vector<Key> keys;

Expand Down

0 comments on commit bcc2487

Please sign in to comment.