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 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});
}

View File

@ -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;

View File

@ -3,6 +3,8 @@
#include "../defines.hpp"
#include <deque>
#include "../Compositor.hpp"
#include <unordered_map>
#include <functional>
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<std::string, std::function<void(std::string)>> m_mDispatchers;
private:
std::deque<SKeybind> 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<CKeybindManager> g_pKeybindManager;