From 7eb2a1023b7b5b25daf8ee836627ab78da2233ca Mon Sep 17 00:00:00 2001 From: vaxerski <43317083+vaxerski@users.noreply.github.com> Date: Thu, 24 Mar 2022 21:05:34 +0100 Subject: [PATCH] Added keyboard configuration --- example/hyprland.conf | 8 ++++++++ src/config/ConfigManager.cpp | 9 +++++++++ src/managers/InputManager.cpp | 37 +++++++++++++++++++++++++++++++++++ src/managers/InputManager.hpp | 2 ++ 4 files changed, 56 insertions(+) diff --git a/example/hyprland.conf b/example/hyprland.conf index 524ac94b..903aab32 100644 --- a/example/hyprland.conf +++ b/example/hyprland.conf @@ -6,6 +6,14 @@ monitor=,1280x720@60,0x0,0.5,1 workspace=DP-1,1 +input { + kb_layout=en + kb_variant= + kb_model= + kb_options= + kb_rules= +} + general { max_fps=240 sensitivity=0.25 diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index b29edacc..f4f1548e 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -27,6 +27,12 @@ CConfigManager::CConfigManager() { configValues["animations:windows"].intValue = 1; configValues["animations:borders"].intValue = 1; configValues["animations:fadein"].intValue = 1; + + configValues["input:kb_layout"].strValue = "en"; + configValues["input:kb_variant"].strValue = ""; + configValues["input:kb_options"].strValue = ""; + configValues["input:kb_rules"].strValue = ""; + configValues["input:kb_model"].strValue = ""; } void CConfigManager::init() { @@ -312,6 +318,9 @@ void CConfigManager::loadConfigLoadVars() { for (auto& m : g_pCompositor->m_lMonitors) g_pLayoutManager->getCurrentLayout()->recalculateMonitor(m.ID); + // Update the keyboard layout to the cfg'd one + g_pInputManager->setKeyboardLayout(); + // Calculate the mod mask for main_mod configValues["general:main_mod_internal"].intValue = g_pKeybindManager->stringToModMask(configValues["general:main_mod"].strValue); } diff --git a/src/managers/InputManager.cpp b/src/managers/InputManager.cpp index b1b47749..95ca49ae 100644 --- a/src/managers/InputManager.cpp +++ b/src/managers/InputManager.cpp @@ -144,6 +144,43 @@ void CInputManager::newKeyboard(wlr_input_device* keyboard) { wlr_seat_set_keyboard(g_pCompositor->m_sSeat.seat, keyboard->keyboard); Debug::log(LOG, "New keyboard created, pointers Hypr: %x and WLR: %x", PNEWKEYBOARD, keyboard); + + setKeyboardLayout(); +} + +void CInputManager::setKeyboardLayout() { + + 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 CONTEXT = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + const auto KEYMAP = xkb_keymap_new_from_names(CONTEXT, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS); + + if (!KEYMAP) { + Debug::log(ERR, "Keyboard layout %s with variant %s (rules: %s, model: %s, options: %s) couldn't have been loaded.", rules.layout, rules.variant, rules.rules, rules.model, rules.options); + xkb_context_unref(CONTEXT); + return; + } + + // TODO: configure devices one by one + for (auto& k : m_lKeyboards) + wlr_keyboard_set_keymap(k.keyboard->keyboard, KEYMAP); + + xkb_keymap_unref(KEYMAP); + xkb_context_unref(CONTEXT); + + Debug::log(LOG, "Set the keyboard layout to %s and variant to %s", rules.layout, rules.variant); } void CInputManager::newMouse(wlr_input_device* mouse) { diff --git a/src/managers/InputManager.hpp b/src/managers/InputManager.hpp index 8b958c49..f0abe183 100644 --- a/src/managers/InputManager.hpp +++ b/src/managers/InputManager.hpp @@ -22,6 +22,8 @@ public: Vector2D getMouseCoordsInternal(); void refocus(); + void setKeyboardLayout(); + // for dragging floating windows CWindow* currentlyDraggedWindow = nullptr;