From 150d693fe794a01aab762a18d2d8a2c8bc54b43c Mon Sep 17 00:00:00 2001 From: Levizor Date: Tue, 9 Sep 2025 15:19:51 +0200 Subject: hyprctl: add an active layout index field in devices (#11531) --- src/devices/IKeyboard.cpp | 13 +++++++++++++ src/devices/IKeyboard.hpp | 35 ++++++++++++++++++----------------- 2 files changed, 31 insertions(+), 17 deletions(-) (limited to 'src/devices') diff --git a/src/devices/IKeyboard.cpp b/src/devices/IKeyboard.cpp index 4f2281b7..ad9e40d8 100644 --- a/src/devices/IKeyboard.cpp +++ b/src/devices/IKeyboard.cpp @@ -266,6 +266,19 @@ void IKeyboard::updateXKBTranslationState(xkb_keymap* const keymap) { xkb_context_unref(PCONTEXT); } +std::optional IKeyboard::getActiveLayoutIndex() { + const auto KEYMAP = m_xkbKeymap; + const auto STATE = m_xkbState; + const auto LAYOUTSNUM = xkb_keymap_num_layouts(KEYMAP); + + for (xkb_layout_index_t i = 0; i < LAYOUTSNUM; ++i) { + if (xkb_state_layout_index_is_active(STATE, i, XKB_STATE_LAYOUT_EFFECTIVE) == 1) + return i; + } + + return {}; +} + std::string IKeyboard::getActiveLayout() { const auto KEYMAP = m_xkbKeymap; const auto STATE = m_xkbState; diff --git a/src/devices/IKeyboard.hpp b/src/devices/IKeyboard.hpp index f4fb667b..c7832548 100644 --- a/src/devices/IKeyboard.hpp +++ b/src/devices/IKeyboard.hpp @@ -65,23 +65,24 @@ class IKeyboard : public IHID { std::string rules = ""; }; - void setKeymap(const SStringRuleNames& rules); - void updateXKBTranslationState(xkb_keymap* const keymap = nullptr); - std::string getActiveLayout(); - std::optional getLEDs(); - void updateLEDs(); - void updateLEDs(uint32_t leds); - uint32_t getModifiers(); - void updateModifiers(uint32_t depressed, uint32_t latched, uint32_t locked, uint32_t group); - bool updateModifiersState(); // rets whether changed - void updateXkbStateWithKey(uint32_t xkbKey, bool pressed); - void updateKeymapFD(); - bool getPressed(uint32_t key); - bool shareStates(); - - bool m_active = false; - bool m_enabled = true; - bool m_allowBinds = true; + void setKeymap(const SStringRuleNames& rules); + void updateXKBTranslationState(xkb_keymap* const keymap = nullptr); + std::optional getActiveLayoutIndex(); + std::string getActiveLayout(); + std::optional getLEDs(); + void updateLEDs(); + void updateLEDs(uint32_t leds); + uint32_t getModifiers(); + void updateModifiers(uint32_t depressed, uint32_t latched, uint32_t locked, uint32_t group); + bool updateModifiersState(); // rets whether changed + void updateXkbStateWithKey(uint32_t xkbKey, bool pressed); + void updateKeymapFD(); + bool getPressed(uint32_t key); + bool shareStates(); + + bool m_active = false; + bool m_enabled = true; + bool m_allowBinds = true; // permission flag: whether this keyboard is allowed to be processed bool m_allowed = true; -- cgit v1.2.3