Better mod parsing

This commit is contained in:
vaxerski 2021-12-06 15:04:47 +01:00
parent efcb0f1a9e
commit b897a1b64e
7 changed files with 21 additions and 47 deletions

View file

@ -50,25 +50,16 @@ uint32_t KeybindManager::getKeyCodeFromName(std::string name) {
return 0; return 0;
} }
unsigned int KeybindManager::modToMask(MODS mod) { unsigned int KeybindManager::modToMask(std::string mod) {
switch(mod) {
case MOD_NONE:
return 0;
case MOD_SUPER:
return XCB_MOD_MASK_4;
case MOD_SHIFT:
return XCB_MOD_MASK_SHIFT;
case MOD_SHIFTSUPER:
return XCB_MOD_MASK_4 | XCB_MOD_MASK_SHIFT;
case MOD_SHIFTCTRL:
return XCB_MOD_MASK_SHIFT | XCB_MOD_MASK_CONTROL;
case MOD_CTRL:
return XCB_MOD_MASK_CONTROL;
case MOD_CTRLSUPER:
return XCB_MOD_MASK_CONTROL | XCB_MOD_MASK_4;
}
return 0; unsigned int sum = 0;
if (CONTAINS(mod, "SUPER") || CONTAINS(mod, "MOD4")) sum |= XCB_MOD_MASK_4;
if (CONTAINS(mod, "SHIFT")) sum |= XCB_MOD_MASK_SHIFT;
if (CONTAINS(mod, "CTRL")) sum |= XCB_MOD_MASK_CONTROL;
if (CONTAINS(mod, "ALT") || CONTAINS(mod, "MOD1")) sum |= XCB_MOD_MASK_1;
return sum;
} }
xcb_keysym_t KeybindManager::getKeysymFromKeycode(xcb_keycode_t keycode) { xcb_keysym_t KeybindManager::getKeysymFromKeycode(xcb_keycode_t keycode) {

View file

@ -9,7 +9,7 @@
namespace KeybindManager { namespace KeybindManager {
inline std::vector<Keybind> keybinds; inline std::vector<Keybind> keybinds;
unsigned int modToMask(MODS); unsigned int modToMask(std::string);
Keybind* findKeybindByKey(int mod, xcb_keysym_t keysym); Keybind* findKeybindByKey(int mod, xcb_keysym_t keysym);
xcb_keysym_t getKeysymFromKeycode(xcb_keycode_t keycode); xcb_keysym_t getKeysymFromKeycode(xcb_keycode_t keycode);

View file

@ -97,15 +97,6 @@ void handleBind(const std::string& command, const std::string& value) {
const auto COMMAND = valueCopy; const auto COMMAND = valueCopy;
MODS mod = MOD_NONE;
if (MOD == "SUPER") mod = MOD_SUPER;
else if (MOD == "SHIFT") mod = MOD_SHIFT;
else if (MOD == "SUPERSHIFT" || MOD == "SHIFTSUPER") mod = MOD_SHIFTSUPER;
else if (MOD == "SUPERCTRL" || MOD == "CTRLSUPER") mod = MOD_CTRLSUPER;
else if (MOD == "CTRL") mod = MOD_CTRL;
else if (MOD == "CTRLSHIFT" || MOD == "SHIFTCTRL") mod = MOD_SHIFTCTRL;
Dispatcher dispatcher = nullptr; Dispatcher dispatcher = nullptr;
if (HANDLER == "exec") dispatcher = KeybindManager::call; if (HANDLER == "exec") dispatcher = KeybindManager::call;
if (HANDLER == "killactive") dispatcher = KeybindManager::killactive; if (HANDLER == "killactive") dispatcher = KeybindManager::killactive;
@ -117,7 +108,7 @@ void handleBind(const std::string& command, const std::string& value) {
if (HANDLER == "togglefloating") dispatcher = KeybindManager::toggleActiveWindowFloating; if (HANDLER == "togglefloating") dispatcher = KeybindManager::toggleActiveWindowFloating;
if (dispatcher) if (dispatcher)
KeybindManager::keybinds.push_back(Keybind(mod, KEY, COMMAND, dispatcher)); KeybindManager::keybinds.push_back(Keybind(KeybindManager::modToMask(MOD), KEY, COMMAND, dispatcher));
} }
void handleRawExec(const std::string& command, const std::string& args) { void handleRawExec(const std::string& command, const std::string& args) {
@ -359,7 +350,7 @@ void ConfigManager::applyKeybindsToX() {
for (auto& keybind : KeybindManager::keybinds) { for (auto& keybind : KeybindManager::keybinds) {
xcb_grab_key(g_pWindowManager->DisplayConnection, 1, g_pWindowManager->Screen->root, xcb_grab_key(g_pWindowManager->DisplayConnection, 1, g_pWindowManager->Screen->root,
KeybindManager::modToMask(keybind.getMod()), KeybindManager::getKeycodeFromKeysym(keybind.getKeysym()), keybind.getMod(), KeybindManager::getKeycodeFromKeysym(keybind.getKeysym()),
XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC); XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
} }
@ -369,12 +360,12 @@ void ConfigManager::applyKeybindsToX() {
xcb_grab_button(g_pWindowManager->DisplayConnection, 0, xcb_grab_button(g_pWindowManager->DisplayConnection, 0,
g_pWindowManager->Screen->root, XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE, g_pWindowManager->Screen->root, XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE,
XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, g_pWindowManager->Screen->root, XCB_NONE, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, g_pWindowManager->Screen->root, XCB_NONE,
1, KeybindManager::modToMask(MOD_SUPER)); 1, KeybindManager::modToMask("SUPER"));
xcb_grab_button(g_pWindowManager->DisplayConnection, 0, xcb_grab_button(g_pWindowManager->DisplayConnection, 0,
g_pWindowManager->Screen->root, XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE, g_pWindowManager->Screen->root, XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE,
XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, g_pWindowManager->Screen->root, XCB_NONE, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, g_pWindowManager->Screen->root, XCB_NONE,
3, KeybindManager::modToMask(MOD_SUPER)); 3, KeybindManager::modToMask("SUPER"));
xcb_flush(g_pWindowManager->DisplayConnection); xcb_flush(g_pWindowManager->DisplayConnection);
} }

View file

@ -77,4 +77,6 @@
#define ALPHA(c) ((double)(((c) >> 24) & 0xff) / 255.0) #define ALPHA(c) ((double)(((c) >> 24) & 0xff) / 255.0)
#define RED(c) ((double)(((c) >> 16) & 0xff) / 255.0) #define RED(c) ((double)(((c) >> 16) & 0xff) / 255.0)
#define GREEN(c) ((double)(((c) >> 8) & 0xff) / 255.0) #define GREEN(c) ((double)(((c) >> 8) & 0xff) / 255.0)
#define BLUE(c) ((double)(((c)) & 0xff) / 255.0) #define BLUE(c) ((double)(((c)) & 0xff) / 255.0)
#define CONTAINS(s, f) s.find(f) != std::string::npos

View file

@ -394,7 +394,7 @@ void Events::eventKeyPress(xcb_generic_event_t* event) {
const auto KEYSYM = KeybindManager::getKeysymFromKeycode(E->detail); const auto KEYSYM = KeybindManager::getKeysymFromKeycode(E->detail);
for (auto& keybind : KeybindManager::keybinds) { for (auto& keybind : KeybindManager::keybinds) {
if (keybind.getKeysym() != 0 && keybind.getKeysym() == KEYSYM && KeybindManager::modToMask(keybind.getMod()) == E->state) { if (keybind.getKeysym() != 0 && keybind.getKeysym() == KEYSYM && keybind.getMod() == E->state) {
keybind.getDispatcher()(keybind.getCommand()); keybind.getDispatcher()(keybind.getCommand());
return; return;
// TODO: fix duplicating keybinds // TODO: fix duplicating keybinds

View file

@ -1,6 +1,6 @@
#include "Keybind.hpp" #include "Keybind.hpp"
Keybind::Keybind(MODS mod, xcb_keysym_t keysym, std::string comm, Dispatcher disp) { Keybind::Keybind(unsigned int mod, xcb_keysym_t keysym, std::string comm, Dispatcher disp) {
this->m_iMod = mod; this->m_iMod = mod;
this->m_Keysym = keysym; this->m_Keysym = keysym;
this->m_szCommand = comm; this->m_szCommand = comm;

View file

@ -3,22 +3,12 @@
typedef void (*Dispatcher)(std::string); typedef void (*Dispatcher)(std::string);
enum MODS {
MOD_NONE = 0,
MOD_SUPER,
MOD_SHIFT,
MOD_SHIFTSUPER,
MOD_SHIFTCTRL,
MOD_CTRL,
MOD_CTRLSUPER
};
class Keybind { class Keybind {
public: public:
Keybind(MODS, xcb_keysym_t, std::string, Dispatcher); Keybind(unsigned int, xcb_keysym_t, std::string, Dispatcher);
~Keybind(); ~Keybind();
EXPOSED_MEMBER(Mod, MODS, i); EXPOSED_MEMBER(Mod, unsigned int, i);
EXPOSED_MEMBER(Keysym, xcb_keysym_t,); EXPOSED_MEMBER(Keysym, xcb_keysym_t,);
EXPOSED_MEMBER(Command, std::string, sz); EXPOSED_MEMBER(Command, std::string, sz);
EXPOSED_MEMBER(Dispatcher, Dispatcher, p); EXPOSED_MEMBER(Dispatcher, Dispatcher, p);