From 4acf8981b34eaa6c3d9f823108daede4c6a75726 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Mon, 26 Feb 2024 02:28:33 +0000 Subject: [PATCH] core: fix repeated key events triggering presses fixes #66 --- src/core/hyprlock.cpp | 23 ++++++++++++++++++----- src/core/hyprlock.hpp | 4 +++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/core/hyprlock.cpp b/src/core/hyprlock.cpp index 564783e..f9019d6 100644 --- a/src/core/hyprlock.cpp +++ b/src/core/hyprlock.cpp @@ -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) { @@ -653,9 +650,25 @@ std::optional 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(); diff --git a/src/core/hyprlock.hpp b/src/core/hyprlock.hpp index c8d3db5..46eca86 100644 --- a/src/core/hyprlock.hpp +++ b/src/core/hyprlock.hpp @@ -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 passwordLastFailReason(); @@ -128,6 +128,8 @@ class CHyprlock { } m_sLoopState; std::vector> m_vTimers; + + std::vector m_vPressedKeys; }; inline std::unique_ptr g_pHyprlock; \ No newline at end of file