diff options
| author | BossCode45 <human.cyborg42@gmail.com> | 2023-05-26 21:46:19 +1200 |
|---|---|---|
| committer | BossCode45 <human.cyborg42@gmail.com> | 2023-05-26 21:46:19 +1200 |
| commit | 27137ec9d29c36df8117869773203b243849896c (patch) | |
| tree | 8f457bcc0862ecd176f5fc748b06f66e46082a2e /keybinds.cpp | |
| parent | da3b5b2131d2b4ff5cb127e92090fca568376835 (diff) | |
| download | YATwm-27137ec9d29c36df8117869773203b243849896c.tar.gz YATwm-27137ec9d29c36df8117869773203b243849896c.zip | |
feat: Made keybinds work (I hope)
Note: the config file reloading keybinds isn't quite working, though, need to ungrab the keys
Diffstat (limited to 'keybinds.cpp')
| -rw-r--r-- | keybinds.cpp | 66 |
1 files changed, 53 insertions, 13 deletions
diff --git a/keybinds.cpp b/keybinds.cpp index 57f7003..ee6892a 100644 --- a/keybinds.cpp +++ b/keybinds.cpp @@ -1,21 +1,41 @@ +#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::cin, std::cout, std::endl; +using std::string, std::cout, std::endl; -KeybindsModule::KeybindsModule(CommandsModule& commandsModule) +KeybindsModule::KeybindsModule(CommandsModule& commandsModule, Display* dpy, Window root) :commandsModule(commandsModule) { + this->dpy = dpy; + this->root = root; CommandArgType* bindArgs = new CommandArgType[2]; bindArgs[0] = STR; bindArgs[1] = STR_REST; commandsModule.addCommand("bind", &KeybindsModule::bind, 2, bindArgs, this); commandsModule.addCommand("exit", &KeybindsModule::exit, 0, {}, this); - commandsModule.addCommand("readBinds", &KeybindsModule::readBinds, 0, {}, this); exitNow = false; } +const void KeybindsModule::handleKeypress(XKeyEvent e) +{ + if(e.same_screen!=1) return; + //updateMousePos(); + for(Keybind bind : binds) + { + if(bind.modifiers == e.state && bind.key == e.keycode) + { + commandsModule.runCommand(bind.command); + } + } +} + const void KeybindsModule::bind(const CommandArg* argv) { Err e = commandsModule.checkCommand(argv[1].str); @@ -24,18 +44,38 @@ const void KeybindsModule::bind(const CommandArg* argv) e.message = "Binding fail - " + e.message; throw e; } - binds.insert({argv[0].str, argv[1].str}); -} -const void KeybindsModule::readBinds(const CommandArg* argv) -{ - cout << "Reading binds" << endl; - while(exitNow == false) + std::vector<string> keys = split(argv[0].str, '+'); + Keybind bind; + for(string key : keys) { - string key; - cout << "> "; - std::getline(std::cin, key); - commandsModule.runCommand(binds.find(key)->second); + if(key == "mod") + { + bind.modifiers |= Mod1Mask; + } + else if(key == "alt") + { + bind.modifiers |= Mod1Mask; + } + else if(key == "shift") + { + bind.modifiers |= ShiftMask; + } + else if(key == "control") + { + bind.modifiers |= ControlMask; + } + else + { + bind.key = XKeysymToKeycode(dpy, XStringToKeysym(key.c_str())); + if(bind.key == NoSymbol) + { + throw Err(CFG_ERR_KEYBIND, "Keybind '" + string(argv[0].str) + "' is invalid!"); + continue; + } + } } + XGrabKey(dpy, bind.key, bind.modifiers, root, false, GrabModeAsync, GrabModeAsync); + binds.push_back(bind); } const void KeybindsModule::exit(const CommandArg* argv) |
