static and functional dispatchers

This commit is contained in:
vaxerski 2022-04-21 15:50:52 +02:00
parent ab2c65e535
commit 66b5c5a143
3 changed files with 51 additions and 26 deletions

View file

@ -203,6 +203,14 @@ void CConfigManager::handleBind(const std::string& command, const std::string& v
const auto COMMAND = valueCopy; 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 != "") if (KEY != "")
g_pKeybindManager->addKeybind(SKeybind{KEY, MOD, HANDLER, COMMAND}); g_pKeybindManager->addKeybind(SKeybind{KEY, MOD, HANDLER, COMMAND});
} }

View file

@ -1,5 +1,22 @@
#include "KeybindManager.hpp" #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) { void CKeybindManager::addKeybind(SKeybind kb) {
m_dKeybinds.push_back(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) if (key != KBKEY && key != KBKEYUPPER)
continue; continue;
// yes.
if (k.handler == "exec") { spawn(k.arg); } const auto DISPATCHER = m_mDispatchers.find(k.handler);
else if (k.handler == "killactive") { killActive(k.arg); }
else if (k.handler == "togglefloating") { toggleActiveFloating(k.arg); } // Should never happen, as we check in the ConfigManager, but oh well
else if (k.handler == "workspace") { changeworkspace(k.arg); } if (DISPATCHER == m_mDispatchers.end()) {
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 {
Debug::log(ERR, "Inavlid handler in a keybind! (handler %s does not exist)", k.handler.c_str()); 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; found = true;

View file

@ -3,6 +3,8 @@
#include "../defines.hpp" #include "../defines.hpp"
#include <deque> #include <deque>
#include "../Compositor.hpp" #include "../Compositor.hpp"
#include <unordered_map>
#include <functional>
struct SKeybind { struct SKeybind {
std::string key = 0; std::string key = 0;
@ -13,29 +15,33 @@ struct SKeybind {
class CKeybindManager { class CKeybindManager {
public: public:
CKeybindManager();
bool handleKeybinds(const uint32_t&, const xkb_keysym_t&); bool handleKeybinds(const uint32_t&, const xkb_keysym_t&);
void addKeybind(SKeybind); void addKeybind(SKeybind);
uint32_t stringToModMask(std::string); uint32_t stringToModMask(std::string);
void clearKeybinds(); void clearKeybinds();
std::unordered_map<std::string, std::function<void(std::string)>> m_mDispatchers;
private: private:
std::deque<SKeybind> m_dKeybinds; std::deque<SKeybind> m_dKeybinds;
bool handleInternalKeybinds(xkb_keysym_t); bool handleInternalKeybinds(xkb_keysym_t);
// -------------- Dispatchers -------------- // // -------------- Dispatchers -------------- //
void killActive(std::string); static void killActive(std::string);
void spawn(std::string); static void spawn(std::string);
void toggleActiveFloating(std::string); static void toggleActiveFloating(std::string);
void toggleActivePseudo(std::string); static void toggleActivePseudo(std::string);
void changeworkspace(std::string); static void changeworkspace(std::string);
void fullscreenActive(std::string); static void fullscreenActive(std::string);
void moveActiveToWorkspace(std::string); static void moveActiveToWorkspace(std::string);
void moveFocusTo(std::string); static void moveFocusTo(std::string);
void moveActiveTo(std::string); static void moveActiveTo(std::string);
void toggleGroup(std::string); static void toggleGroup(std::string);
void changeGroupActive(std::string); static void changeGroupActive(std::string);
void alterSplitRatio(std::string); static void alterSplitRatio(std::string);
}; };
inline std::unique_ptr<CKeybindManager> g_pKeybindManager; inline std::unique_ptr<CKeybindManager> g_pKeybindManager;