diff options
| author | BossCode45 <human.cyborg42@gmail.com> | 2023-06-19 12:49:19 +1200 |
|---|---|---|
| committer | BossCode45 <human.cyborg42@gmail.com> | 2023-06-19 12:49:19 +1200 |
| commit | 8b4b487e685c72a478baf74aea2bf756988fe550 (patch) | |
| tree | 9a5ac8ffbd65942f4ce0117a5316f7db7f4eb918 /keybinds.cpp | |
| parent | 6655d5dfb24ca3fd36b02550c526bca1f5d924e9 (diff) | |
| parent | 185015b39b8db953b8034f29724ef03de09e7ea1 (diff) | |
| download | YATwm-8b4b487e685c72a478baf74aea2bf756988fe550.tar.gz YATwm-8b4b487e685c72a478baf74aea2bf756988fe550.zip | |
Merge branch 'config-refactor'
Merging in the new config system with commands
Diffstat (limited to 'keybinds.cpp')
| -rw-r--r-- | keybinds.cpp | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/keybinds.cpp b/keybinds.cpp new file mode 100644 index 0000000..64d3fc8 --- /dev/null +++ b/keybinds.cpp @@ -0,0 +1,90 @@ +#include <X11/X.h> +#include <X11/Xlib.h> +#include <iostream> +#include <sstream> +#include <vector> + +#include "error.h" +#include "keybinds.h" +#include "util.h" + +using std::string, std::cout, std::endl; + +KeybindsModule::KeybindsModule(CommandsModule& commandsModule, Config& cfg, Globals& globals, void (*updateMousePos)()) + :commandsModule(commandsModule), + globals(globals), + cfg(cfg) +{ + commandsModule.addCommand("bind", &KeybindsModule::bind, 2, {STR, STR_REST}, this); + this->updateMousePos = updateMousePos; +} + +const void KeybindsModule::handleKeypress(XKeyEvent e) +{ + if(e.same_screen!=1) return; + //cout << "Key Pressed" << endl; + //cout << "\tState: " << e.state << endl; + //cout << "\tCode: " << XKeysymToString(XKeycodeToKeysym(globals.dpy, e.keycode, 0)) << endl; + updateMousePos(); + + const unsigned int masks = ShiftMask | ControlMask | Mod1Mask | Mod4Mask; + for(Keybind bind : binds) + { + if(bind.modifiers == (e.state & masks) && bind.key == XLookupKeysym(&e, 0)) + { + commandsModule.runCommand(bind.command); + } + } +} + +const void KeybindsModule::bind(const CommandArg* argv) +{ + Err e = commandsModule.checkCommand(argv[1].str); + if(e.code != NOERR) + { + e.message = "Binding fail - " + e.message; + throw e; + } + std::vector<string> keys = split(argv[0].str, '+'); + Keybind bind; + bind.modifiers = 0; + for(string key : keys) + { + if(key == "mod") + { + bind.modifiers |= Mod4Mask >> 3 * cfg.swapSuperAlt; + } + else if(key == "alt") + { + bind.modifiers |= Mod1Mask << 3 * cfg.swapSuperAlt; + } + else if(key == "shift") + { + bind.modifiers |= ShiftMask; + } + else if(key == "control") + { + bind.modifiers |= ControlMask; + } + else + { + KeySym s = XStringToKeysym(key.c_str()); + bind.key = s; + if(bind.key == NoSymbol) + { + throw Err(CFG_ERR_KEYBIND, "Keybind '" + string(argv[0].str) + "' is invalid!"); + continue; + } + } + } + bind.command = argv[1].str; + KeyCode c = XKeysymToKeycode(globals.dpy, bind.key); + XGrabKey(globals.dpy, c, bind.modifiers, globals.root, False, GrabModeAsync, GrabModeAsync); + binds.push_back(bind); +} + +const void KeybindsModule::clearKeybinds() +{ + XUngrabButton(globals.dpy, AnyKey, AnyModifier, globals.root); + binds = std::vector<Keybind>(); +} |
