diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index 3d821d14..c38d82dc 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -94,13 +94,51 @@ uint32_t CKeybindManager::stringToModMask(std::string mods) { return modMask; } +void CKeybindManager::updateXKBTranslationState() { + if (m_pXKBTranslationState) { + xkb_keymap_unref(xkb_state_get_keymap(m_pXKBTranslationState)); + xkb_state_unref(m_pXKBTranslationState); + + m_pXKBTranslationState = nullptr; + } + + const auto FILEPATH = g_pConfigManager->getString("input:kb_file"); + const auto RULES = g_pConfigManager->getString("input:kb_rules"); + const auto MODEL = g_pConfigManager->getString("input:kb_model"); + const auto LAYOUT = g_pConfigManager->getString("input:kb_layout"); + const auto VARIANT = g_pConfigManager->getString("input:kb_variant"); + const auto OPTIONS = g_pConfigManager->getString("input:kb_options"); + + xkb_rule_names rules = { + .rules = RULES.c_str(), + .model = MODEL.c_str(), + .layout = LAYOUT.c_str(), + .variant = VARIANT.c_str(), + .options = OPTIONS.c_str()}; + + const auto PCONTEXT = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + + const auto PKEYMAP = FILEPATH == "" ? xkb_keymap_new_from_names(PCONTEXT, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS) : xkb_keymap_new_from_file(PCONTEXT, fopen(FILEPATH.c_str(), "r"), XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS); + + xkb_context_unref(PCONTEXT); + m_pXKBTranslationState = xkb_state_new(PKEYMAP); +} + bool CKeybindManager::onKeyEvent(wlr_keyboard_key_event* e, SKeyboard* pKeyboard) { if (pKeyboard->isVirtual) return true; + if (!m_pXKBTranslationState) { + Debug::log(ERR, "BUG THIS: m_pXKBTranslationState NULL!"); + updateXKBTranslationState(); + + if (!m_pXKBTranslationState) + return true; + } + const auto KEYCODE = e->keycode + 8; // Because to xkbcommon it's +8 from libinput - const xkb_keysym_t keysym = xkb_state_key_get_one_sym(wlr_keyboard_from_input_device(pKeyboard->keyboard)->xkb_state, KEYCODE); + const xkb_keysym_t keysym = xkb_state_key_get_one_sym(m_pXKBTranslationState, KEYCODE); const auto MODS = g_pInputManager->accumulateModsFromAllKBs(); diff --git a/src/managers/KeybindManager.hpp b/src/managers/KeybindManager.hpp index c8c09df6..29ebc7a1 100644 --- a/src/managers/KeybindManager.hpp +++ b/src/managers/KeybindManager.hpp @@ -69,6 +69,10 @@ private: bool handleInternalKeybinds(xkb_keysym_t); bool handleVT(xkb_keysym_t); + xkb_state* m_pXKBTranslationState = nullptr; + + void updateXKBTranslationState(); + // -------------- Dispatchers -------------- // static void killActive(std::string); static void spawn(std::string); diff --git a/src/managers/input/InputManager.cpp b/src/managers/input/InputManager.cpp index 87869da1..5241a0a0 100644 --- a/src/managers/input/InputManager.cpp +++ b/src/managers/input/InputManager.cpp @@ -507,6 +507,8 @@ void CInputManager::newVirtualKeyboard(wlr_input_device* keyboard) { void CInputManager::setKeyboardLayout() { for (auto& k : m_lKeyboards) applyConfigToKeyboard(&k); + + g_pKeybindManager->updateXKBTranslationState(); } void CInputManager::applyConfigToKeyboard(SKeyboard* pKeyboard) {