From 66b5c5a1437f81e9cea94e1a27e48fbf74d8a56d Mon Sep 17 00:00:00 2001 From: vaxerski <43317083+vaxerski@users.noreply.github.com> Date: Thu, 21 Apr 2022 15:50:52 +0200 Subject: [PATCH] static and functional dispatchers --- src/config/ConfigManager.cpp | 8 +++++++ src/managers/KeybindManager.cpp | 39 +++++++++++++++++++++------------ src/managers/KeybindManager.hpp | 30 +++++++++++++++---------- 3 files changed, 51 insertions(+), 26 deletions(-) diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index 74bc2fab1..db5f5a8ae 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -203,6 +203,14 @@ void CConfigManager::handleBind(const std::string& command, const std::string& v const auto COMMAND = valueCopy; + const auto DISPATCHER = g_pKeybindManager->m_mDispatchers.find(HANDLER); + + if (DISPATCHER == g_pKeybindManager->m_mDispatchers.end()) { + Debug::log(ERR, "Invalid dispatcher!"); + parseError = "Invalid dispatcher, requested \"" + HANDLER + "\" does not exist"; + return; + } + if (KEY != "") g_pKeybindManager->addKeybind(SKeybind{KEY, MOD, HANDLER, COMMAND}); } diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index 69535ec8f..3f91e7f26 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -1,5 +1,22 @@ #include "KeybindManager.hpp" +CKeybindManager::CKeybindManager() { + // initialize all dispatchers + + m_mDispatchers["exec"] = spawn; + m_mDispatchers["killactive"] = killActive; + m_mDispatchers["togglefloating"] = toggleActiveFloating; + m_mDispatchers["workspace"] = changeworkspace; + m_mDispatchers["fullscreen"] = fullscreenActive; + m_mDispatchers["movetoworkspace"] = moveActiveToWorkspace; + m_mDispatchers["pseudo"] = toggleActivePseudo; + m_mDispatchers["movefocus"] = moveFocusTo; + m_mDispatchers["movewindow"] = moveActiveTo; + m_mDispatchers["togglegroup"] = toggleGroup; + m_mDispatchers["changegroupactive"] = changeGroupActive; + m_mDispatchers["splitratio"] = alterSplitRatio; +} + void CKeybindManager::addKeybind(SKeybind kb) { m_dKeybinds.push_back(kb); } @@ -50,21 +67,15 @@ bool CKeybindManager::handleKeybinds(const uint32_t& modmask, const xkb_keysym_t if (key != KBKEY && key != KBKEYUPPER) continue; - // yes. - if (k.handler == "exec") { spawn(k.arg); } - else if (k.handler == "killactive") { killActive(k.arg); } - else if (k.handler == "togglefloating") { toggleActiveFloating(k.arg); } - else if (k.handler == "workspace") { changeworkspace(k.arg); } - else if (k.handler == "fullscreen") { fullscreenActive(k.arg); } - else if (k.handler == "movetoworkspace") { moveActiveToWorkspace(k.arg); } - else if (k.handler == "pseudo") { toggleActivePseudo(k.arg); } - else if (k.handler == "movefocus") { moveFocusTo(k.arg); } - else if (k.handler == "movewindow") { moveActiveTo(k.arg); } - else if (k.handler == "togglegroup") { toggleGroup(k.arg); } - else if (k.handler == "changegroupactive") { changeGroupActive(k.arg); } - else if (k.handler == "splitratio") { alterSplitRatio(k.arg); } - else { + + const auto DISPATCHER = m_mDispatchers.find(k.handler); + + // Should never happen, as we check in the ConfigManager, but oh well + if (DISPATCHER == m_mDispatchers.end()) { Debug::log(ERR, "Inavlid handler in a keybind! (handler %s does not exist)", k.handler.c_str()); + } else { + // call the dispatcher + DISPATCHER->second(k.arg); } found = true; diff --git a/src/managers/KeybindManager.hpp b/src/managers/KeybindManager.hpp index 30e038238..e262081e1 100644 --- a/src/managers/KeybindManager.hpp +++ b/src/managers/KeybindManager.hpp @@ -3,6 +3,8 @@ #include "../defines.hpp" #include #include "../Compositor.hpp" +#include +#include struct SKeybind { std::string key = 0; @@ -13,29 +15,33 @@ struct SKeybind { class CKeybindManager { public: + CKeybindManager(); + bool handleKeybinds(const uint32_t&, const xkb_keysym_t&); void addKeybind(SKeybind); uint32_t stringToModMask(std::string); void clearKeybinds(); + std::unordered_map> m_mDispatchers; + private: std::deque m_dKeybinds; bool handleInternalKeybinds(xkb_keysym_t); // -------------- Dispatchers -------------- // - void killActive(std::string); - void spawn(std::string); - void toggleActiveFloating(std::string); - void toggleActivePseudo(std::string); - void changeworkspace(std::string); - void fullscreenActive(std::string); - void moveActiveToWorkspace(std::string); - void moveFocusTo(std::string); - void moveActiveTo(std::string); - void toggleGroup(std::string); - void changeGroupActive(std::string); - void alterSplitRatio(std::string); + static void killActive(std::string); + static void spawn(std::string); + static void toggleActiveFloating(std::string); + static void toggleActivePseudo(std::string); + static void changeworkspace(std::string); + static void fullscreenActive(std::string); + static void moveActiveToWorkspace(std::string); + static void moveFocusTo(std::string); + static void moveActiveTo(std::string); + static void toggleGroup(std::string); + static void changeGroupActive(std::string); + static void alterSplitRatio(std::string); }; inline std::unique_ptr g_pKeybindManager; \ No newline at end of file