From da3b5b2131d2b4ff5cb127e92090fca568376835 Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Wed, 24 May 2023 10:28:49 +1200 Subject: in-progress: Config refactor started, changed all existing keybind command args and added in the new files, still many errors --- keybinds.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 keybinds.cpp (limited to 'keybinds.cpp') diff --git a/keybinds.cpp b/keybinds.cpp new file mode 100644 index 0000000..57f7003 --- /dev/null +++ b/keybinds.cpp @@ -0,0 +1,45 @@ +#include + +#include "keybinds.h" + +using std::string, std::cin, std::cout, std::endl; + +KeybindsModule::KeybindsModule(CommandsModule& commandsModule) + :commandsModule(commandsModule) +{ + 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::bind(const CommandArg* argv) +{ + Err e = commandsModule.checkCommand(argv[1].str); + if(e.code != NOERR) + { + 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) + { + string key; + cout << "> "; + std::getline(std::cin, key); + commandsModule.runCommand(binds.find(key)->second); + } +} + +const void KeybindsModule::exit(const CommandArg* argv) +{ + exitNow = true; + cout << "Exiting..." << endl; +} -- cgit v1.2.3 From 27137ec9d29c36df8117869773203b243849896c Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Fri, 26 May 2023 21:46:19 +1200 Subject: feat: Made keybinds work (I hope) Note: the config file reloading keybinds isn't quite working, though, need to ungrab the keys --- keybinds.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 13 deletions(-) (limited to 'keybinds.cpp') diff --git a/keybinds.cpp b/keybinds.cpp index 57f7003..ee6892a 100644 --- a/keybinds.cpp +++ b/keybinds.cpp @@ -1,21 +1,41 @@ +#include +#include #include +#include +#include +#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 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) -- cgit v1.2.3 From 01bc6a33eb235cd10851e2c31b99e6840603ca7d Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Thu, 1 Jun 2023 12:53:13 +1200 Subject: IT WORKS!!!!!! The new config commands system works, finally able to run this as a test and it works!!!!!. Still more to be done but at least it works. --- keybinds.cpp | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) (limited to 'keybinds.cpp') diff --git a/keybinds.cpp b/keybinds.cpp index ee6892a..30b3fd0 100644 --- a/keybinds.cpp +++ b/keybinds.cpp @@ -10,17 +10,14 @@ using std::string, std::cout, std::endl; -KeybindsModule::KeybindsModule(CommandsModule& commandsModule, Display* dpy, Window root) - :commandsModule(commandsModule) +KeybindsModule::KeybindsModule(CommandsModule& commandsModule, Globals& globals) + :commandsModule(commandsModule), + globals(globals) { - 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); - exitNow = false; } const void KeybindsModule::handleKeypress(XKeyEvent e) @@ -29,7 +26,7 @@ const void KeybindsModule::handleKeypress(XKeyEvent e) //updateMousePos(); for(Keybind bind : binds) { - if(bind.modifiers == e.state && bind.key == e.keycode) + if(bind.modifiers == e.state && bind.key == XKeycodeToKeysym(globals.dpy, e.keycode, 0)) { commandsModule.runCommand(bind.command); } @@ -46,6 +43,7 @@ const void KeybindsModule::bind(const CommandArg* argv) } std::vector keys = split(argv[0].str, '+'); Keybind bind; + bind.modifiers = 0; for(string key : keys) { if(key == "mod") @@ -54,7 +52,7 @@ const void KeybindsModule::bind(const CommandArg* argv) } else if(key == "alt") { - bind.modifiers |= Mod1Mask; + bind.modifiers |= Mod4Mask; } else if(key == "shift") { @@ -66,7 +64,8 @@ const void KeybindsModule::bind(const CommandArg* argv) } else { - bind.key = XKeysymToKeycode(dpy, XStringToKeysym(key.c_str())); + 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!"); @@ -74,12 +73,8 @@ const void KeybindsModule::bind(const CommandArg* argv) } } } - XGrabKey(dpy, bind.key, bind.modifiers, root, false, GrabModeAsync, GrabModeAsync); + 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::exit(const CommandArg* argv) -{ - exitNow = true; - cout << "Exiting..." << endl; -} -- cgit v1.2.3 From e9cc5756dbb0a2d079a7b5e3438d79945f819df5 Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Sat, 3 Jun 2023 21:30:05 +1200 Subject: feat: Keybind updates Re added the ability to swap super and mod as a config parameter (the ~swapmods~) command). Finally fixed keybinds sometimes not working because of numlock, bitwise & with the modifiers I actually care about. --- keybinds.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'keybinds.cpp') diff --git a/keybinds.cpp b/keybinds.cpp index 30b3fd0..85e9e4e 100644 --- a/keybinds.cpp +++ b/keybinds.cpp @@ -10,9 +10,10 @@ using std::string, std::cout, std::endl; -KeybindsModule::KeybindsModule(CommandsModule& commandsModule, Globals& globals) +KeybindsModule::KeybindsModule(CommandsModule& commandsModule, Config& cfg, Globals& globals) :commandsModule(commandsModule), - globals(globals) + globals(globals), + cfg(cfg) { CommandArgType* bindArgs = new CommandArgType[2]; bindArgs[0] = STR; @@ -23,10 +24,15 @@ KeybindsModule::KeybindsModule(CommandsModule& commandsModule, Globals& globals) 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 && bind.key == XKeycodeToKeysym(globals.dpy, e.keycode, 0)) + if(bind.modifiers == (e.state & masks) && bind.key == XKeycodeToKeysym(globals.dpy, e.keycode, 0)) { commandsModule.runCommand(bind.command); } @@ -48,11 +54,11 @@ const void KeybindsModule::bind(const CommandArg* argv) { if(key == "mod") { - bind.modifiers |= Mod1Mask; + bind.modifiers |= Mod4Mask >> 3 * cfg.swapSuperAlt; } else if(key == "alt") { - bind.modifiers |= Mod4Mask; + bind.modifiers |= Mod1Mask << 3 * cfg.swapSuperAlt; } else if(key == "shift") { @@ -74,7 +80,13 @@ const void KeybindsModule::bind(const CommandArg* argv) } } bind.command = argv[1].str; + cout << bind.modifiers << endl; 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); +} -- cgit v1.2.3 From 0b539b0b0278f2d7c2b7629e6d28d8463cba2688 Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Mon, 5 Jun 2023 20:35:32 +1200 Subject: Added a very basic config, and fixed some stuff NOTE: for some reason toggling doesn't work anymore --- keybinds.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'keybinds.cpp') diff --git a/keybinds.cpp b/keybinds.cpp index 85e9e4e..6e4abfd 100644 --- a/keybinds.cpp +++ b/keybinds.cpp @@ -10,7 +10,7 @@ using std::string, std::cout, std::endl; -KeybindsModule::KeybindsModule(CommandsModule& commandsModule, Config& cfg, Globals& globals) +KeybindsModule::KeybindsModule(CommandsModule& commandsModule, Config& cfg, Globals& globals, void (*updateMousePos)()) :commandsModule(commandsModule), globals(globals), cfg(cfg) @@ -19,6 +19,7 @@ KeybindsModule::KeybindsModule(CommandsModule& commandsModule, Config& cfg, Glob bindArgs[0] = STR; bindArgs[1] = STR_REST; commandsModule.addCommand("bind", &KeybindsModule::bind, 2, bindArgs, this); + this->updateMousePos = updateMousePos; } const void KeybindsModule::handleKeypress(XKeyEvent e) @@ -27,7 +28,7 @@ const void KeybindsModule::handleKeypress(XKeyEvent e) //cout << "Key Pressed" << endl; //cout << "\tState: " << e.state << endl; //cout << "\tCode: " << XKeysymToString(XKeycodeToKeysym(globals.dpy, e.keycode, 0)) << endl; - //updateMousePos(); + updateMousePos(); const unsigned int masks = ShiftMask | ControlMask | Mod1Mask | Mod4Mask; for(Keybind bind : binds) @@ -80,7 +81,6 @@ const void KeybindsModule::bind(const CommandArg* argv) } } bind.command = argv[1].str; - cout << bind.modifiers << endl; KeyCode c = XKeysymToKeycode(globals.dpy, bind.key); XGrabKey(globals.dpy, c, bind.modifiers, globals.root, False, GrabModeAsync, GrabModeAsync); binds.push_back(bind); -- cgit v1.2.3 From 5f54adae7bc4edaf2c18383efe13ded233255509 Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Fri, 16 Jun 2023 00:05:28 +1200 Subject: feat: Uses a backup config file and respects XDG Uses config files in the order of `$XDG_CONFIG_HOME/YATwm/config`, `$HOME/.config/YATwm/config`, then `/etc/YATwm/config` --- keybinds.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'keybinds.cpp') diff --git a/keybinds.cpp b/keybinds.cpp index 6e4abfd..3a431f0 100644 --- a/keybinds.cpp +++ b/keybinds.cpp @@ -33,7 +33,7 @@ const void KeybindsModule::handleKeypress(XKeyEvent e) const unsigned int masks = ShiftMask | ControlMask | Mod1Mask | Mod4Mask; for(Keybind bind : binds) { - if(bind.modifiers == (e.state & masks) && bind.key == XKeycodeToKeysym(globals.dpy, e.keycode, 0)) + if(bind.modifiers == (e.state & masks) && bind.key == XLookupKeysym(&e, 0)) { commandsModule.runCommand(bind.command); } @@ -89,4 +89,5 @@ const void KeybindsModule::bind(const CommandArg* argv) const void KeybindsModule::clearKeybinds() { XUngrabButton(globals.dpy, AnyKey, AnyModifier, globals.root); + binds = std::vector(); } -- cgit v1.2.3 From 557faa2603d33d6f6a8b7baa9c9f7891bfcb8d30 Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Sun, 18 Jun 2023 21:31:41 +1200 Subject: feat: Updated readme Updated the readme and added a default config file. --- keybinds.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'keybinds.cpp') diff --git a/keybinds.cpp b/keybinds.cpp index 3a431f0..64d3fc8 100644 --- a/keybinds.cpp +++ b/keybinds.cpp @@ -15,10 +15,7 @@ KeybindsModule::KeybindsModule(CommandsModule& commandsModule, Config& cfg, Glob globals(globals), cfg(cfg) { - CommandArgType* bindArgs = new CommandArgType[2]; - bindArgs[0] = STR; - bindArgs[1] = STR_REST; - commandsModule.addCommand("bind", &KeybindsModule::bind, 2, bindArgs, this); + commandsModule.addCommand("bind", &KeybindsModule::bind, 2, {STR, STR_REST}, this); this->updateMousePos = updateMousePos; } -- cgit v1.2.3