From 638c3ac10003f66ef4af43f50ee365c9036da0fe Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Sat, 24 Jun 2023 20:26:29 +1200 Subject: feat: Added ; as a command separator --- commands.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++--- commands.h | 5 +++- keybinds.cpp | 11 +++++--- readme.html | 82 ++++++++++++++++++++++++++++++------------------------------ readme.org | 2 +- 5 files changed, 115 insertions(+), 50 deletions(-) diff --git a/commands.cpp b/commands.cpp index de7d81e..af523ad 100644 --- a/commands.cpp +++ b/commands.cpp @@ -13,8 +13,14 @@ using std::cout, std::endl, std::string, std::vector, std::tolower; +const void CommandsModule::echo(const CommandArg* argv) +{ + cout << argv[0].str << endl; +} + CommandsModule::CommandsModule() { + addCommand("echo", &CommandsModule::echo, 1, {STR_REST}, this); } CommandsModule::~CommandsModule() { @@ -215,11 +221,36 @@ CommandArg* CommandsModule::getCommandArgs(vector& split, const CommandA void CommandsModule::runCommand(string command) { vector split = splitCommand(command); + vector::const_iterator start = split.begin(); + int count = 0; + for(string s : split) + { + if(s == ";") + { + vector::const_iterator end = start + count; + vector partialCmd(start, end); + runCommand(partialCmd); + count = 0; + start = end + 1; + } + else + { + count++; + } + } + if(start != split.end()) + { + vector partialCmd(start, (vector::const_iterator)split.end()); + runCommand(partialCmd); + } +} +void CommandsModule::runCommand(vector split) +{ Command* cmd = lookupCommand(split[0]); if(cmd == nullptr) throw Err(CMD_ERR_NOT_FOUND, split[0] + " is not a valid command name"); if(cmd->argc > split.size() - 1) - throw Err(CMD_ERR_WRONG_ARGS, command + " is the wrong args"); + throw Err(CMD_ERR_WRONG_ARGS, "wrong number of arguments"); CommandArg* args; try { @@ -254,14 +285,42 @@ void CommandsModule::runCommand(string command) delete[] args; } -Err CommandsModule::checkCommand(string command) +vector CommandsModule::checkCommand(string command) { + vector errs; vector split = splitCommand(command); + vector::const_iterator start = split.begin(); + int count = 0; + for(string s : split) + { + if(s == ";") + { + vector::const_iterator end = start + count; + vector partialCmd(start, end); + errs.push_back(checkCommand(partialCmd)); + count = 0; + start = end + 1; + } + else + { + count++; + } + } + if(start != split.end()) + { + vector partialCmd(start, (vector::const_iterator)split.end()); + errs.push_back(checkCommand(partialCmd)); + } + return errs; +} + +Err CommandsModule::checkCommand(vector split) +{ Command* cmd = lookupCommand(split[0]); if(cmd == nullptr) return Err(CMD_ERR_NOT_FOUND, split[0] + " is not a valid command name"); if(cmd->argc > split.size()) - return Err(CMD_ERR_WRONG_ARGS, command + " is the wrong args"); + return Err(CMD_ERR_WRONG_ARGS, "wrong number of arguments"); CommandArg* args; try { diff --git a/commands.h b/commands.h index d7f2351..af4a4a0 100644 --- a/commands.h +++ b/commands.h @@ -51,6 +51,7 @@ private: std::vector commandList; std::vector splitCommand(std::string command); CommandArg* getCommandArgs(std::vector& args, const CommandArgType* argTypes, const int argc); + const void echo(const CommandArg* argv); public: CommandsModule(); ~CommandsModule(); @@ -63,7 +64,9 @@ public: void addCommand(Command c); Command* lookupCommand(std::string name); void runCommand(std::string command); - Err checkCommand(std::string command); + void runCommand(std::vector split); + std::vector checkCommand(std::string command); + Err checkCommand(std::vector split); }; // YES I KNOW THIS IS BAD diff --git a/keybinds.cpp b/keybinds.cpp index 64d3fc8..8448eed 100644 --- a/keybinds.cpp +++ b/keybinds.cpp @@ -39,11 +39,14 @@ const void KeybindsModule::handleKeypress(XKeyEvent e) const void KeybindsModule::bind(const CommandArg* argv) { - Err e = commandsModule.checkCommand(argv[1].str); - if(e.code != NOERR) + std::vector errs = commandsModule.checkCommand(argv[1].str); + for(Err e : errs) { - e.message = "Binding fail - " + e.message; - throw e; + if(e.code != NOERR) + { + e.message = "Binding fail - " + e.message; + throw e; + } } std::vector keys = split(argv[0].str, '+'); Keybind bind; diff --git a/readme.html b/readme.html index 1040470..1914ed8 100644 --- a/readme.html +++ b/readme.html @@ -3,7 +3,7 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - + YATwm @@ -200,36 +200,36 @@

Table of Contents

-
-

1. This config is best read in Emacs!

+
+

1. This config is best read in Emacs!

-
-

2. Disclaimer: This is still very much in beta

+
+

2. Disclaimer: This is still very much in beta

This only just works, multiple monitors aren't supported and floating windows cannot move and there is no resizing. Many features are just hacked together and are likely to break. However, it is just about usable so if you really want to try then go for it! (feel free to make an issue if you have any questions).
@@ -237,16 +237,16 @@ This only just works, multiple monitors aren't supported and floating windows ca

-
-

3. Usage instructions

+
+

3. Usage instructions

-
-

3.1. Installation

+
+

3.1. Installation

-
-

3.1.1. Pre reqs

+
+

3.1.1. Pre reqs

  • Xlib and g++ and libnotify to build the program
  • @@ -258,8 +258,8 @@ This only just works, multiple monitors aren't supported and floating windows ca
-
-

3.1.2. Installing and removing

+
+

3.1.2. Installing and removing

  • make i or make install to install
  • @@ -269,23 +269,23 @@ This only just works, multiple monitors aren't supported and floating windows ca
-
-

3.2. Config

+
+

3.2. Config

You can configure YATwm with the config file in $HOME/.config/YATwm/config or $XDG_CONFIG_HOME/YATwm/config if you have that set. I have provided an example config file in the project dir that has all the variables set to their defaults (this will also be installed to /etc/YATwm/config.
It should alert you with a notification if you have an error, and put the error your log file. If the whole file is missing then it will use the default in /etc/YATwm/config.

-
-

3.2.1. Syntax

+
+

3.2.1. Syntax

-The config file is a list of commands. Each command should be on a new line. For example, to set the gaps you would use the gaps command like this gaps 10 (make sure this is all there is on that line). This says to call the command gaps with the arguments of 10. Commands can have multiple arguments and these should be separated with a space, if you want a space in one of the arguments then wrap the arg in quotes, e.g. addWorkspace "1: A" 1, here the arguments are 1: A and 1. If you want to have a quote in your argument then make sure that arg is wrapped in quotes or escape it with \ (e.g. \'), to insert \ then use \\.
+The config file is a list of commands. Each command should be on a new line. For example, to set the gaps you would use the gaps command like this gaps 10 (make sure this is all there is on that line). This says to call the command gaps with the arguments of 10. Commands can have multiple arguments and these should be separated with a space, if you want a space in one of the arguments then wrap the arg in quotes, e.g. addWorkspace "1: A" 1, here the arguments are 1: A and 1. If you want to have a quote in your argument then make sure that arg is wrapped in quotes or escape it with \ (e.g. \'), to insert \ then use \\. If you want to have multiple commands on the same line, e.g. binding a key to multiple commands, then use ; as an argument on its own to separate them (tip: if you are using this for keybinds then enclose all the keybind commands in quotes, e.g. bind mod+l "spawn i3lock ; spawn systemctl suspend").

    -
  1. Command arg types
    +
  2. Command arg types
    • String: this is just some text, this can be wrapped in quotes if you want a space in it.
    • @@ -302,7 +302,7 @@ The config file is a list of commands. Each command should be on a new line. For
  3. -
  4. List of commands
    +
  5. List of commands
    • exit: shuts down YATwm
    • @@ -368,8 +368,8 @@ The config file is a list of commands. Each command should be on a new line. For
-
-

3.2.2. General

+
+

3.2.2. General

You can change either the inner gaps (padding around each window - so double it for space between windows), or the outer gaps (padding around the display - add to inner gaps to get space between window and screen edges).
@@ -379,8 +379,8 @@ YATwm also keeps a log file, the location of this file can be changed with the c

-
-

3.2.3. Workspaces

+
+

3.2.3. Workspaces

You can add workspace with the command addworkspace in the config file.
@@ -406,8 +406,8 @@ Defaults workspace are listed below (these are the args for the addworkspace com

-
-

3.2.4. Keybinds

+
+

3.2.4. Keybinds

Current keybinds (these can all be edited):
@@ -451,8 +451,8 @@ Commands are executed going down the list and multiple commands with the same ke

-
-

4. Credits

+
+

4. Credits

Catwm (https://github.com/pyknite/catwm)
@@ -465,7 +465,7 @@ basicwm (https://github

diff --git a/readme.org b/readme.org index 9a41662..82863b9 100644 --- a/readme.org +++ b/readme.org @@ -20,7 +20,7 @@ This only just works, multiple monitors aren't supported and floating windows ca You can configure YATwm with the config file in ~$HOME/.config/YATwm/config~ or ~$XDG_CONFIG_HOME/YATwm/config~ if you have that set. I have provided an example config file in the project dir that has all the variables set to their defaults (this will also be installed to ~/etc/YATwm/config~. It should alert you with a notification if you have an error, and put the error your log file. If the whole file is missing then it will use the default in ~/etc/YATwm/config~. *** Syntax -The config file is a list of commands. Each command should be on a new line. For example, to set the gaps you would use the ~gaps~ command like this ~gaps 10~ (make sure this is all there is on that line). This says to call the command ~gaps~ with the arguments of ~10~. Commands can have multiple arguments and these should be separated with a space, if you want a space in one of the arguments then wrap the arg in quotes, e.g. ~addWorkspace "1: A" 1~, here the arguments are ~1: A~ and ~1~. If you want to have a quote in your argument then make sure that arg is wrapped in quotes or escape it with ~\~ (e.g. ~\'~), to insert ~\~ then use ~\\~. +The config file is a list of commands. Each command should be on a new line. For example, to set the gaps you would use the ~gaps~ command like this ~gaps 10~ (make sure this is all there is on that line). This says to call the command ~gaps~ with the arguments of ~10~. Commands can have multiple arguments and these should be separated with a space, if you want a space in one of the arguments then wrap the arg in quotes, e.g. ~addWorkspace "1: A" 1~, here the arguments are ~1: A~ and ~1~. If you want to have a quote in your argument then make sure that arg is wrapped in quotes or escape it with ~\~ (e.g. ~\'~), to insert ~\~ then use ~\\~. If you want to have multiple commands on the same line, e.g. binding a key to multiple commands, then use ~;~ as an argument on its own to separate them (tip: if you are using this for keybinds then enclose *all* the keybind commands in quotes, e.g. ~bind mod+l "spawn i3lock ; spawn systemctl suspend"~). **** Command arg types - String: this is just some text, this can be wrapped in quotes if you want a space in it. - String rest: This will only ever be the final argument. This just takes the rest of the line as a string, so you can use spaces without needing quotes. -- cgit v1.2.3 From 37a2725da41e363fcdca12d0374b192cd03905d0 Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Wed, 28 Jun 2023 21:24:59 +1200 Subject: feat: Added key chording Probably a hacky mess but oh well. Key chords can be done by seperating binds in string with ` `. You can set the quit bind with `quitkey`, default mod+g. (Chords also exited when pressing unbound keys). --- keybinds.cpp | 144 +++++++++++++++++++++++++++++++++++++++++++++++++---------- keybinds.h | 24 +++++++++- main.cpp | 2 +- readme.html | 87 +++++++++++++++++++----------------- readme.org | 5 ++- util.cpp | 7 +++ util.h | 3 +- 7 files changed, 203 insertions(+), 69 deletions(-) diff --git a/keybinds.cpp b/keybinds.cpp index 8448eed..b51bf6e 100644 --- a/keybinds.cpp +++ b/keybinds.cpp @@ -2,53 +2,98 @@ #include #include #include +#include #include +#include "commands.h" #include "error.h" #include "keybinds.h" #include "util.h" using std::string, std::cout, std::endl; +bool Keybind::operator<(const Keybind &o) const { + if(key != o.key) + { + return key < o.key; + } + else return modifiers < o.modifiers; +} +bool Keybind::operator==(const Keybind &o) const { + return (key == o.key && modifiers == o.modifiers); +} + 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); + commandsModule.addCommand("quitkey", &KeybindsModule::quitKey, 1, {STR}, this); this->updateMousePos = updateMousePos; + keyMaps.insert({0, std::map()}); +} + +void KeybindsModule::changeMap(int newMapID) +{ + if(currentMapID == newMapID) + return; + if(currentMapID != 0) + XUngrabKeyboard(globals.dpy, CurrentTime); + XUngrabButton(globals.dpy, AnyKey, AnyModifier, globals.root); + currentMapID = newMapID; + if(newMapID == 0) + { + for(std::pair pair : getKeymap(currentMapID)) + { + Keybind bind = pair.first; + KeyCode c = XKeysymToKeycode(globals.dpy, bind.key); + XGrabKey(globals.dpy, c, bind.modifiers, globals.root, false, GrabModeAsync, GrabModeAsync); + } + } + else + { + XGrabKeyboard(globals.dpy, globals.root, false, GrabModeAsync, GrabModeAsync, CurrentTime); + } } 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; + // 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) + Keybind k = {XLookupKeysym(&e, 0), e.state & masks}; + if(k == exitBind) { - if(bind.modifiers == (e.state & masks) && bind.key == XLookupKeysym(&e, 0)) + changeMap(0); + } + else if(getKeymap(currentMapID).count(k) > 0) + { + KeyFunction& c = getKeymap(currentMapID).find(k)->second; + if(getKeymap(c.mapID).size() == 0) + { + commandsModule.runCommand(c.command); + changeMap(0); + } + else { - commandsModule.runCommand(bind.command); + XUngrabButton(globals.dpy, AnyKey, AnyModifier, globals.root); + changeMap(c.mapID); } } + else if(std::find(std::begin(ignoredKeys), std::end(ignoredKeys), e.keycode) == std::end(ignoredKeys)) + { + changeMap(0); + } } -const void KeybindsModule::bind(const CommandArg* argv) +Keybind KeybindsModule::getKeybind(string bindString) { - std::vector errs = commandsModule.checkCommand(argv[1].str); - for(Err e : errs) - { - if(e.code != NOERR) - { - e.message = "Binding fail - " + e.message; - throw e; - } - } - std::vector keys = split(argv[0].str, '+'); + std::vector keys = split(bindString, '+'); Keybind bind; bind.modifiers = 0; for(string key : keys) @@ -75,19 +120,72 @@ const void KeybindsModule::bind(const CommandArg* argv) bind.key = s; if(bind.key == NoSymbol) { - throw Err(CFG_ERR_KEYBIND, "Keybind '" + string(argv[0].str) + "' is invalid!"); + throw Err(CFG_ERR_KEYBIND, "Keybind '" + bindString + "' 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); + return bind; +} + +const void KeybindsModule::bind(const CommandArg* argv) +{ + std::vector errs = commandsModule.checkCommand(argv[1].str); + for(Err e : errs) + { + if(e.code != NOERR) + { + e.message = "Binding fail - " + e.message; + throw e; + } + } + std::vector keys = split(argv[0].str, ' '); + int currentBindingMap = 0; + for(int i = 0; i < keys.size() - 1; i++) + { + Keybind bind = getKeybind(keys[i]); + if(getKeymap(currentBindingMap).count(bind) > 0) + { + currentBindingMap = getKeymap(currentBindingMap).find(bind)->second.mapID; + } + else + { + KeyFunction newMap = {"", nextKeymapID}; + keyMaps.insert({nextKeymapID, std::map()}); + nextKeymapID++; + getKeymap(currentBindingMap).insert({bind, newMap}); + currentBindingMap = getKeymap(currentBindingMap).find(bind)->second.mapID; + } + } + Keybind bind = getKeybind(keys[keys.size() - 1]); + if(getKeymap(currentBindingMap).count(bind) <= 0) + { + KeyFunction function = {argv[1].str, nextKeymapID}; + keyMaps.insert({nextKeymapID, std::map()}); + nextKeymapID++; + getKeymap(currentBindingMap).insert({bind, function}); + if(currentBindingMap == currentMapID) + { + KeyCode c = XKeysymToKeycode(globals.dpy, bind.key); + XGrabKey(globals.dpy, c, bind.modifiers, globals.root, false, GrabModeAsync, GrabModeAsync); + } + } + else + { + throw Err(CFG_ERR_KEYBIND, "Bind is a keymap already!"); + } + // cout << "Added bind" << endl; + // cout << "\t" << argv[0].str << endl; +} + +const void KeybindsModule::quitKey(const CommandArg* argv) +{ + exitBind = getKeybind(argv[0].str); } const void KeybindsModule::clearKeybinds() { XUngrabButton(globals.dpy, AnyKey, AnyModifier, globals.root); - binds = std::vector(); + keyMaps = std::map>(); + keyMaps.insert({0, std::map()}); } diff --git a/keybinds.h b/keybinds.h index 686eaf8..5c220c2 100644 --- a/keybinds.h +++ b/keybinds.h @@ -2,6 +2,7 @@ #include #include + #include #include #include @@ -14,18 +15,37 @@ struct Keybind { KeySym key; unsigned int modifiers; + bool operator<(const Keybind &o) const; + bool operator==(const Keybind &o) const; +}; + +struct KeyFunction +{ std::string command; + int mapID; }; -class KeybindsModule { +#define getKeymap(X) \ + keyMaps.find(X)->second + +class KeybindsModule +{ public: KeybindsModule(CommandsModule& commandsModule, Config& cfg, Globals& globals, void (*updateMousePos)()); ~KeybindsModule() = default; const void bind(const CommandArg* argv); + const void quitKey(const CommandArg* argv); const void handleKeypress(XKeyEvent e); const void clearKeybinds(); private: - std::vector binds; + Keybind getKeybind(std::string bindString); + void changeMap(int newMapID); + std::map> keyMaps; + // Modifier keys to ignore when canceling a keymap + KeyCode ignoredKeys[8] = {50, 37, 133, 64, 62, 105, 134, 108}; + int currentMapID = 0; + int nextKeymapID = 1; + Keybind exitBind = {42, 0x40}; CommandsModule& commandsModule; Config& cfg; Globals& globals; diff --git a/main.cpp b/main.cpp index f6cc6e8..cc3d2bb 100644 --- a/main.cpp +++ b/main.cpp @@ -1117,7 +1117,7 @@ int main(int argc, char** argv) clientMessage(e.xclient); break; default: - //cout << "Unhandled event, code: " << evNames[e.type] << "!\n"; + // cout << "Unhandled event: " << getEventName(e.type) << endl; break; } } diff --git a/readme.html b/readme.html index 1914ed8..7879e4d 100644 --- a/readme.html +++ b/readme.html @@ -3,7 +3,7 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - + YATwm @@ -200,36 +200,36 @@

Table of Contents

-
-

1. This config is best read in Emacs!

+
+

1. This config is best read in Emacs!

-
-

2. Disclaimer: This is still very much in beta

+
+

2. Disclaimer: This is still very much in beta

This only just works, multiple monitors aren't supported and floating windows cannot move and there is no resizing. Many features are just hacked together and are likely to break. However, it is just about usable so if you really want to try then go for it! (feel free to make an issue if you have any questions).
@@ -237,16 +237,16 @@ This only just works, multiple monitors aren't supported and floating windows ca

-
-

3. Usage instructions

+
+

3. Usage instructions

-
-

3.1. Installation

+
+

3.1. Installation

-
-

3.1.1. Pre reqs

+
+

3.1.1. Pre reqs

  • Xlib and g++ and libnotify to build the program
  • @@ -258,8 +258,8 @@ This only just works, multiple monitors aren't supported and floating windows ca
-
-

3.1.2. Installing and removing

+
+

3.1.2. Installing and removing

  • make i or make install to install
  • @@ -269,23 +269,23 @@ This only just works, multiple monitors aren't supported and floating windows ca
-
-

3.2. Config

+
+

3.2. Config

You can configure YATwm with the config file in $HOME/.config/YATwm/config or $XDG_CONFIG_HOME/YATwm/config if you have that set. I have provided an example config file in the project dir that has all the variables set to their defaults (this will also be installed to /etc/YATwm/config.
It should alert you with a notification if you have an error, and put the error your log file. If the whole file is missing then it will use the default in /etc/YATwm/config.

-
-

3.2.1. Syntax

+
+

3.2.1. Syntax

The config file is a list of commands. Each command should be on a new line. For example, to set the gaps you would use the gaps command like this gaps 10 (make sure this is all there is on that line). This says to call the command gaps with the arguments of 10. Commands can have multiple arguments and these should be separated with a space, if you want a space in one of the arguments then wrap the arg in quotes, e.g. addWorkspace "1: A" 1, here the arguments are 1: A and 1. If you want to have a quote in your argument then make sure that arg is wrapped in quotes or escape it with \ (e.g. \'), to insert \ then use \\. If you want to have multiple commands on the same line, e.g. binding a key to multiple commands, then use ; as an argument on its own to separate them (tip: if you are using this for keybinds then enclose all the keybind commands in quotes, e.g. bind mod+l "spawn i3lock ; spawn systemctl suspend").

    -
  1. Command arg types
    +
  2. Command arg types
    • String: this is just some text, this can be wrapped in quotes if you want a space in it.
    • @@ -302,7 +302,7 @@ The config file is a list of commands. Each command should be on a new line. For
  3. -
  4. List of commands
    +
  5. List of commands
    • exit: shuts down YATwm
    • @@ -359,17 +359,21 @@ The config file is a list of commands. Each command should be on a new line. For
  6. bind: Binds a key to a command
      -
    • String: The key bind, modifiers and keys are separated with +, e.g. mod+x
    • +
    • String: The key bind, modifiers and keys are separated with +, e.g. mod+x. This can also be a key chord, where you have multiple binds, where when pressed in succession will execute the command (make sure to enclose this arg in quotes, and then separate the binds with spaces)
    • String rest: The command to run
  7. +
  8. quitkey: Sets the key to exit a key chord (note: pressing an unbound key also does this)
    +
      +
    • String: The key bind, modifiers and keys are separated with +, e.g. mod+g.
    • +
  9. wsDump: This is a command for testing, you probably don't want to use it
-
-

3.2.2. General

+
+

3.2.2. General

You can change either the inner gaps (padding around each window - so double it for space between windows), or the outer gaps (padding around the display - add to inner gaps to get space between window and screen edges).
@@ -379,8 +383,8 @@ YATwm also keeps a log file, the location of this file can be changed with the c

-
-

3.2.3. Workspaces

+
+

3.2.3. Workspaces

You can add workspace with the command addworkspace in the config file.
@@ -406,8 +410,8 @@ Defaults workspace are listed below (these are the args for the addworkspace com

-
-

3.2.4. Keybinds

+
+

3.2.4. Keybinds

Current keybinds (these can all be edited):
@@ -427,6 +431,7 @@ Current keybinds (these can all be edited):

  • mod + f : toggle fullscreen
  • mod + (num) : switch to workspace (num) - currently only for 1-10 but you can add more
  • mod + shift + (num) : move window to workspace (num) - currently only for 1-10 but you can add more
  • +
  • mod + g : exit key chord
  • (mod is super, and the direction keys are h, j, k, l - left, down, up, right respectively like vim)
    @@ -451,8 +456,8 @@ Commands are executed going down the list and multiple commands with the same ke

    -
    -

    4. Credits

    +
    +

    4. Credits

    Catwm (https://github.com/pyknite/catwm)
    @@ -465,7 +470,7 @@ basicwm (https://github

    diff --git a/readme.org b/readme.org index 82863b9..b8b6a6b 100644 --- a/readme.org +++ b/readme.org @@ -63,8 +63,10 @@ The config file is a list of commands. Each command should be on a new line. For - String: The name of the workspace - Number array rest: The monitor preferences. This is which monitor it should appear on, first (primary) monitor is one. E.g. ~2 1~ to appear on the second monitor first, but if that isn't plugged in then use the first. - bind: Binds a key to a command - - String: The key bind, modifiers and keys are separated with +, e.g. ~mod+x~ + - String: The key bind, modifiers and keys are separated with +, e.g. ~mod+x~. This can also be a key chord, where you have multiple binds, where when pressed in succession will execute the command (make sure to enclose this arg in quotes, and then separate the binds with spaces) - String rest: The command to run +- quitkey: Sets the key to exit a key chord (note: pressing an unbound key also does this) + - String: The key bind, modifiers and keys are separated with +, e.g. ~mod+g~. - wsDump: This is a command for testing, you probably don't want to use it *** General You can change either the inner gaps (padding around each window - so double it for space between windows), or the outer gaps (padding around the display - add to inner gaps to get space between window and screen edges). @@ -104,6 +106,7 @@ Current keybinds (these can all be edited): - ~mod + f~ : toggle fullscreen - ~mod + (num)~ : switch to workspace (num) - currently only for 1-10 but you can add more - ~mod + shift + (num)~ : move window to workspace (num) - currently only for 1-10 but you can add more +- ~mod + g~ : exit key chord (mod is super, and the direction keys are h, j, k, l - left, down, up, right respectively like vim) You can use the command ~swapmods~ to make ~mod~ act as ~alt~ and ~alt~ act as ~mod~. diff --git a/util.cpp b/util.cpp index dff3428..7fe5641 100644 --- a/util.cpp +++ b/util.cpp @@ -15,3 +15,10 @@ std::vector split (const string &s, char delim) { return result; } + +const string evNames[] = {"", "", "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", "NoExpose", "VisibilityNotify", "CreateNotify", "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest", "ReparentNotify", "ConfigureNotify", "ConfigureRequest", "GravityNotify", "ResizeRequest", "CirculateNotify", "CirculateRequest", "PropertyNotify", "SelectionClear", "SelectionRequest", "SelectionNotify", "ColormapNotify", "ClientMessage", "MappingNotify", "GenericEvent", "LASTEvent"}; + +string getEventName(int e) +{ + return evNames[e]; +} diff --git a/util.h b/util.h index bdc45d8..d29d596 100644 --- a/util.h +++ b/util.h @@ -4,7 +4,8 @@ #include #include -#define evNames {0, 0, "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", "NoExpose", "VisibilityNotify", "CreateNotify", "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest", "ReparentNotify", "ConfigureNotify", "ConfigureRequest", "GravityNotify", "ResizeRequest", "CirculateNotify", "CirculateRequest", "PropertyNotify", "SelectionClear", "SelectionRequest", "SelectionNotify", "ColormapNotify", "ClientMessage", "MappingNotify", "GenericEvent", "LASTEvent"}; + +std::string getEventName(int e); std::vector split (const std::string &s, char delim); -- cgit v1.2.3 From ea569d9c9c61eb26f7d325b41d8ac839dc470eec Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Tue, 15 Aug 2023 21:27:51 +1200 Subject: feat: Made the bind modes work Numlock still seems to mess with keybindings. Also switched from storing keybinds with KeySym to KeyCode --- commands.cpp | 10 ++----- keybinds.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----- keybinds.h | 8 +++++- util.cpp | 8 ++++++ util.h | 2 ++ 5 files changed, 98 insertions(+), 16 deletions(-) diff --git a/commands.cpp b/commands.cpp index af523ad..54fe891 100644 --- a/commands.cpp +++ b/commands.cpp @@ -1,5 +1,6 @@ #include "commands.h" #include "error.h" +#include "util.h" #include #include @@ -11,7 +12,7 @@ #include #include -using std::cout, std::endl, std::string, std::vector, std::tolower; +using std::cout, std::endl, std::string, std::vector; const void CommandsModule::echo(const CommandArg* argv) { @@ -135,13 +136,6 @@ vector CommandsModule::splitCommand(string command) return v; } -string lowercase(string s) -{ - string s2 = s; - std::transform(s2.begin(), s2.end(), s2.begin(), [](unsigned char c){ return std::tolower(c); }); - return s2; -} - CommandArg* CommandsModule::getCommandArgs(vector& split, const CommandArgType* argTypes, const int argc) { CommandArg* args = new CommandArg[argc]; diff --git a/keybinds.cpp b/keybinds.cpp index b51bf6e..88a43fe 100644 --- a/keybinds.cpp +++ b/keybinds.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -30,6 +31,14 @@ KeybindsModule::KeybindsModule(CommandsModule& commandsModule, Config& cfg, Glob { commandsModule.addCommand("bind", &KeybindsModule::bind, 2, {STR, STR_REST}, this); commandsModule.addCommand("quitkey", &KeybindsModule::quitKey, 1, {STR}, this); + commandsModule.addCommand("bindmode", &KeybindsModule::bindMode, 1, {STR}, this); + + bindModes = { + {"normal", &KeybindsModule::normalBindMode}, + {"emacs", &KeybindsModule::emacsBindMode} + }; + bindFunc = &KeybindsModule::normalBindMode; + this->updateMousePos = updateMousePos; keyMaps.insert({0, std::map()}); } @@ -47,8 +56,7 @@ void KeybindsModule::changeMap(int newMapID) for(std::pair pair : getKeymap(currentMapID)) { Keybind bind = pair.first; - KeyCode c = XKeysymToKeycode(globals.dpy, bind.key); - XGrabKey(globals.dpy, c, bind.modifiers, globals.root, false, GrabModeAsync, GrabModeAsync); + XGrabKey(globals.dpy, bind.key, bind.modifiers, globals.root, false, GrabModeAsync, GrabModeAsync); } } else @@ -66,7 +74,7 @@ const void KeybindsModule::handleKeypress(XKeyEvent e) updateMousePos(); const unsigned int masks = ShiftMask | ControlMask | Mod1Mask | Mod4Mask; - Keybind k = {XLookupKeysym(&e, 0), e.state & masks}; + Keybind k = {(KeyCode)e.keycode, e.state & masks}; if(k == exitBind) { changeMap(0); @@ -91,7 +99,28 @@ const void KeybindsModule::handleKeypress(XKeyEvent e) } } -Keybind KeybindsModule::getKeybind(string bindString) +bool isUpper(const std::string& s) { + return std::all_of(s.begin(), s.end(), [](unsigned char c){ return std::isupper(c); }); +} + +const void KeybindsModule::bindMode(const CommandArg* argv) +{ + if(bindModes.count(argv[0].str) < 1) + { + throw Err(CFG_ERR_KEYBIND, "Bind mode: " + string(argv[0].str) + " does not exist"); + } + else + { + bindFunc = bindModes.find(argv[0].str)->second; + } +} + +Keybind KeybindsModule::getKeybind(std::string bindString) +{ + return (this->*bindFunc)(bindString); +} + +const Keybind KeybindsModule::normalBindMode(string bindString) { std::vector keys = split(bindString, '+'); Keybind bind; @@ -116,15 +145,58 @@ Keybind KeybindsModule::getKeybind(string bindString) } else { + if(isUpper(key)) + { + bind.modifiers |= ShiftMask; + } + KeySym s = XStringToKeysym(key.c_str()); + if(s == NoSymbol) + { + throw Err(CFG_ERR_KEYBIND, "Keybind '" + bindString + "' is invalid!"); + } + bind.key = XKeysymToKeycode(globals.dpy, s); + } + } + if(!bind.key) + throw Err(CFG_ERR_KEYBIND, "Keybind '" + bindString + "' is invalid!"); + return bind; +} + +const Keybind KeybindsModule::emacsBindMode(string bindString) +{ + std::vector keys = split(bindString, '-'); + Keybind bind; + bind.modifiers = 0; + for(string key : keys) + { + if(key == "s") + { + bind.modifiers |= Mod4Mask >> 3 * cfg.swapSuperAlt; + } + else if(key == "M") + { + bind.modifiers |= Mod1Mask << 3 * cfg.swapSuperAlt; + } + else if(key == "C") + { + bind.modifiers |= ControlMask; + } + else + { + if(isUpper(key)) + { + bind.modifiers |= ShiftMask; + } KeySym s = XStringToKeysym(key.c_str()); - bind.key = s; - if(bind.key == NoSymbol) + if(s == NoSymbol) { throw Err(CFG_ERR_KEYBIND, "Keybind '" + bindString + "' is invalid!"); - continue; } + bind.key = XKeysymToKeycode(globals.dpy, s); } } + if(!bind.key) + throw Err(CFG_ERR_KEYBIND, "Keybind '" + bindString + "' is invalid!"); return bind; } diff --git a/keybinds.h b/keybinds.h index 5c220c2..a742240 100644 --- a/keybinds.h +++ b/keybinds.h @@ -13,7 +13,7 @@ #include "util.h" struct Keybind { - KeySym key; + KeyCode key; unsigned int modifiers; bool operator<(const Keybind &o) const; bool operator==(const Keybind &o) const; @@ -28,6 +28,7 @@ struct KeyFunction #define getKeymap(X) \ keyMaps.find(X)->second + class KeybindsModule { public: @@ -35,12 +36,17 @@ public: ~KeybindsModule() = default; const void bind(const CommandArg* argv); const void quitKey(const CommandArg* argv); + const void bindMode(const CommandArg* argv); const void handleKeypress(XKeyEvent e); const void clearKeybinds(); private: Keybind getKeybind(std::string bindString); void changeMap(int newMapID); std::map> keyMaps; + const Keybind emacsBindMode(std::string bindString); + const Keybind normalBindMode(std::string bindString); + std::map bindModes; + const Keybind(KeybindsModule::* bindFunc)(std::string bindString); // Modifier keys to ignore when canceling a keymap KeyCode ignoredKeys[8] = {50, 37, 133, 64, 62, 105, 134, 108}; int currentMapID = 0; diff --git a/util.cpp b/util.cpp index 7fe5641..58116d0 100644 --- a/util.cpp +++ b/util.cpp @@ -1,6 +1,7 @@ #include "util.h" #include +#include using std::string; @@ -22,3 +23,10 @@ string getEventName(int e) { return evNames[e]; } + +string lowercase(string s) +{ + string s2 = s; + std::transform(s2.begin(), s2.end(), s2.begin(), [](unsigned char c){ return std::tolower(c); }); + return s2; +} diff --git a/util.h b/util.h index d29d596..66ecf76 100644 --- a/util.h +++ b/util.h @@ -10,6 +10,8 @@ std::string getEventName(int e); std::vector split (const std::string &s, char delim); +std::string lowercase(std::string s); + struct Globals { Display*& dpy; -- cgit v1.2.3 From ba3ca53aef521467049a58aa766104041f987c5e Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Tue, 22 Aug 2023 11:51:15 +1200 Subject: feat: Made the emacs bind mode function better It now uses a regex to split out the different modifiers and keys. --- keybinds.cpp | 67 +++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/keybinds.cpp b/keybinds.cpp index 88a43fe..a4a78c0 100644 --- a/keybinds.cpp +++ b/keybinds.cpp @@ -5,13 +5,14 @@ #include #include #include +#include #include "commands.h" #include "error.h" #include "keybinds.h" #include "util.h" -using std::string, std::cout, std::endl; +using std::string; bool Keybind::operator<(const Keybind &o) const { if(key != o.key) @@ -164,39 +165,53 @@ const Keybind KeybindsModule::normalBindMode(string bindString) const Keybind KeybindsModule::emacsBindMode(string bindString) { - std::vector keys = split(bindString, '-'); Keybind bind; bind.modifiers = 0; - for(string key : keys) + + const std::regex keyRegex("^(?:([CMs])-)?(?:([CMs])-)?(?:([CMs])-)?([^\\s]+)$"); + std::smatch keyMatch; + if(std::regex_match(bindString, keyMatch, keyRegex)) { - if(key == "s") - { - bind.modifiers |= Mod4Mask >> 3 * cfg.swapSuperAlt; - } - else if(key == "M") - { - bind.modifiers |= Mod1Mask << 3 * cfg.swapSuperAlt; - } - else if(key == "C") + for (int i = 1; i < 3; i++) { - bind.modifiers |= ControlMask; - } - else - { - if(isUpper(key)) - { - bind.modifiers |= ShiftMask; - } - KeySym s = XStringToKeysym(key.c_str()); - if(s == NoSymbol) + std::ssub_match modifierMatch = keyMatch[i]; + if(modifierMatch.matched) { - throw Err(CFG_ERR_KEYBIND, "Keybind '" + bindString + "' is invalid!"); + std::string modifier = modifierMatch.str(); + if(modifier == "s") + { + bind.modifiers |= Mod4Mask >> 3 * cfg.swapSuperAlt; + } + else if(modifier == "M") + { + bind.modifiers |= Mod1Mask << 3 * cfg.swapSuperAlt; + } + else if(modifier == "C") + { + bind.modifiers |= ControlMask; + } } - bind.key = XKeysymToKeycode(globals.dpy, s); } } - if(!bind.key) - throw Err(CFG_ERR_KEYBIND, "Keybind '" + bindString + "' is invalid!"); + KeySym keySym = XStringToKeysym(keyMatch[4].str().c_str()); + + if(isUpper(keyMatch[4].str().c_str()) && keySym != NoSymbol) + { + bind.modifiers |= ShiftMask; + } + if(keySym == NoSymbol) + { + if(keyMatch[4].str() == "RET") + keySym = XK_Return; + if(keyMatch[4].str() == "ESC") + keySym = XK_Escape; + if(keyMatch[4].str() == "SPC") + keySym = XK_space; + else + throw Err(CFG_ERR_KEYBIND, "Keybind '" + bindString + "' is invalid"); + } + bind.key = XKeysymToKeycode(globals.dpy, keySym); + return bind; } -- cgit v1.2.3 From 6e5d3c8aec03d34b5d195cc920cc4c2f123a1cb2 Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Tue, 22 Aug 2023 11:58:07 +1200 Subject: fixup: Made regex work better --- keybinds.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keybinds.cpp b/keybinds.cpp index a4a78c0..2e4ebe2 100644 --- a/keybinds.cpp +++ b/keybinds.cpp @@ -168,7 +168,7 @@ const Keybind KeybindsModule::emacsBindMode(string bindString) Keybind bind; bind.modifiers = 0; - const std::regex keyRegex("^(?:([CMs])-)?(?:([CMs])-)?(?:([CMs])-)?([^\\s]+)$"); + const std::regex keyRegex("^(?:([CMs])-)?(?:([CMs])-)?(?:([CMs])-)?([^\s]|(SPC|ESC|RET|))$"); std::smatch keyMatch; if(std::regex_match(bindString, keyMatch, keyRegex)) { -- cgit v1.2.3 From d22b5d2363ed8a90960446a209482180f6d27fc0 Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Wed, 23 Aug 2023 21:32:50 +1200 Subject: feat: Added documentation Also fixed up the special cases for keybinds, such as SPC, ESC, RET, -, and +. --- keybinds.cpp | 10 +++++++--- readme.org | 9 ++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/keybinds.cpp b/keybinds.cpp index 2e4ebe2..c41452f 100644 --- a/keybinds.cpp +++ b/keybinds.cpp @@ -168,7 +168,7 @@ const Keybind KeybindsModule::emacsBindMode(string bindString) Keybind bind; bind.modifiers = 0; - const std::regex keyRegex("^(?:([CMs])-)?(?:([CMs])-)?(?:([CMs])-)?([^\s]|(SPC|ESC|RET|))$"); + const std::regex keyRegex("^(?:([CMs])-)?(?:([CMs])-)?(?:([CMs])-)?([^\\s]|(SPC|ESC|RET|))$"); std::smatch keyMatch; if(std::regex_match(bindString, keyMatch, keyRegex)) { @@ -203,10 +203,14 @@ const Keybind KeybindsModule::emacsBindMode(string bindString) { if(keyMatch[4].str() == "RET") keySym = XK_Return; - if(keyMatch[4].str() == "ESC") + else if(keyMatch[4].str() == "ESC") keySym = XK_Escape; - if(keyMatch[4].str() == "SPC") + else if(keyMatch[4].str() == "SPC") keySym = XK_space; + else if(keyMatch[4].str() == "-") + keySym = XK_minus; + else if(keyMatch[4].str() == "+") + keySym = XK_plus; else throw Err(CFG_ERR_KEYBIND, "Keybind '" + bindString + "' is invalid"); } diff --git a/readme.org b/readme.org index b8b6a6b..f6257da 100644 --- a/readme.org +++ b/readme.org @@ -63,10 +63,12 @@ The config file is a list of commands. Each command should be on a new line. For - String: The name of the workspace - Number array rest: The monitor preferences. This is which monitor it should appear on, first (primary) monitor is one. E.g. ~2 1~ to appear on the second monitor first, but if that isn't plugged in then use the first. - bind: Binds a key to a command - - String: The key bind, modifiers and keys are separated with +, e.g. ~mod+x~. This can also be a key chord, where you have multiple binds, where when pressed in succession will execute the command (make sure to enclose this arg in quotes, and then separate the binds with spaces) + - String: The key bind, structured according to the bind mode. This can also be a key chord, where you have multiple binds, where when pressed in succession will execute the command (make sure to enclose this arg in quotes, and then separate the binds with spaces) - String rest: The command to run - quitkey: Sets the key to exit a key chord (note: pressing an unbound key also does this) - - String: The key bind, modifiers and keys are separated with +, e.g. ~mod+g~. + - String: The key bind, structured according to the bind mode. +- bindmode: Sets the bind mode (Description of these further down) + - String: The bind mode to use - wsDump: This is a command for testing, you probably don't want to use it *** General You can change either the inner gaps (padding around each window - so double it for space between windows), or the outer gaps (padding around the display - add to inner gaps to get space between window and screen edges). @@ -117,7 +119,8 @@ bind mod+q kill bind mod+shift+x bashSpawn loginctl lock-session && systemctl suspend #+end_src Commands are executed going down the list and multiple commands with the same keybind and modifiers will all be executed - +**** Bind modes +The current two bind modes are ~normal~ and ~emacs~. The normal bind mode has the same syntax as i3, so key and modifiers separated with a '+', and use the xlib KeySym name for them. The emacs bind mode has the same syntax as emacs, so separate modifiers and keys with a '-', and the key comes last. It uses xlib keysyms, but for multi letter keys such as ~RET~, ~SPC~, ~ESC~, ~+~, ~-~ it has special logic to interpret how they would be written in emacs (note: if there are any other exceptions please let me know). * Credits Catwm (https://github.com/pyknite/catwm) -- cgit v1.2.3