Skip to content

Commit

Permalink
core: fix repeated key events triggering presses
Browse files Browse the repository at this point in the history
fixes #66
  • Loading branch information
vaxerski committed Feb 26, 2024
1 parent 94ac7fe commit 4acf898
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
23 changes: 18 additions & 5 deletions src/core/hyprlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,10 +563,7 @@ static void handleKeyboardKeymap(void* data, wl_keyboard* wl_keyboard, uint form
}

static void handleKeyboardKey(void* data, struct wl_keyboard* keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state) {
if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
return;

g_pHyprlock->onKey(key);
g_pHyprlock->onKey(key, state == WL_KEYBOARD_KEY_STATE_PRESSED);
}

static void handleKeyboardEnter(void* data, wl_keyboard* wl_keyboard, uint serial, wl_surface* surface, wl_array* keys) {
Expand Down Expand Up @@ -653,9 +650,25 @@ std::optional<std::string> CHyprlock::passwordLastFailReason() {
return m_sPasswordState.lastFailReason;
}

void CHyprlock::onKey(uint32_t key) {
void CHyprlock::onKey(uint32_t key, bool down) {
const auto SYM = xkb_state_key_get_one_sym(m_pXKBState, key + 8);

if (down && std::find(m_vPressedKeys.begin(), m_vPressedKeys.end(), SYM) != m_vPressedKeys.end()) {
Debug::log(ERR, "Invalid key down event (key already pressed?)");
return;
} else if (!down && std::find(m_vPressedKeys.begin(), m_vPressedKeys.end(), SYM) == m_vPressedKeys.end()) {
Debug::log(ERR, "Invalid key down event (stray release event?)");
return;
}

if (down)
m_vPressedKeys.push_back(SYM);
else
std::erase(m_vPressedKeys, SYM);

if (!down) // we dont care about up events
return;

if (m_sPasswordState.result) {
for (auto& o : m_vOutputs) {
o->sessionLockSurface->render();
Expand Down
4 changes: 3 additions & 1 deletion src/core/hyprlock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class CHyprlock {
void spawnAsync(const std::string& cmd);
std::string spawnSync(const std::string& cmd);

void onKey(uint32_t key);
void onKey(uint32_t key, bool down);
void onPasswordCheckTimer();
bool passwordCheckWaiting();
std::optional<std::string> passwordLastFailReason();
Expand Down Expand Up @@ -128,6 +128,8 @@ class CHyprlock {
} m_sLoopState;

std::vector<std::shared_ptr<CTimer>> m_vTimers;

std::vector<uint32_t> m_vPressedKeys;
};

inline std::unique_ptr<CHyprlock> g_pHyprlock;

0 comments on commit 4acf898

Please sign in to comment.