From 6b3dff404248bcf03e0bf0463a8c53d8918f7eec Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Wed, 31 May 2023 20:55:49 +1200 Subject: Added support for commands without a module Also made it easier to add commands by making the ~addCommand~ function accept a vector for ~argTypes~ and then convert it. --- commands.cpp | 19 ++++++++- commands.h | 73 ++++++++++++++++++++-------------- compile_commands.json | 108 +++++++++++++++++++++++++++++++++++++++++++++++++- config.cpp | 37 ++++------------- config.h | 1 - main.cpp | 2 + 6 files changed, 178 insertions(+), 62 deletions(-) diff --git a/commands.cpp b/commands.cpp index 322d160..549775e 100644 --- a/commands.cpp +++ b/commands.cpp @@ -47,6 +47,20 @@ void CommandsModule::addCommand(Command c) } commandList.push_back(c); } +void CommandsModule::addCommand(std::string name, const void (*func)(const CommandArg *), const int argc, CommandArgType *argTypes) +{ + Command c = {name, nullptr, func, argc, argTypes, nullptr}; + addCommand(c); +} +void CommandsModule::addCommand(std::string name, const void(*func)(const CommandArg*), const int argc, std::vector argTypes) +{ + CommandArgType* argTypesArr = new CommandArgType[argc]; + for(int i = 0; i < argc; i++) + { + argTypesArr[i] = argTypes[i]; + } + addCommand(name, func, argc, argTypesArr); +} struct NameMatches { @@ -145,7 +159,10 @@ void CommandsModule::runCommand(string command) CommandArg* args = getCommandArgs(split, cmd->argTypes, cmd->argc); try { - cmd->func(*cmd->module, args); + if(cmd->module == nullptr) + cmd->staticFunc(args); + else + cmd->func(*cmd->module, args); } catch (Err e) { diff --git a/commands.h b/commands.h index 2e364ac..92801bd 100644 --- a/commands.h +++ b/commands.h @@ -8,20 +8,20 @@ #include enum MoveDir -{ - UP, - RIGHT, - DOWN, - LEFT -}; + { + UP, + RIGHT, + DOWN, + LEFT + }; enum CommandArgType -{ - STR, - NUM, - MOVDIR, - STR_REST, - NUM_ARR_REST -}; + { + STR, + NUM, + MOVDIR, + STR_REST, + NUM_ARR_REST + }; struct NumArr { @@ -40,27 +40,32 @@ struct Command { const std::string name; const std::function func; + const std::function staticFunc; const int argc; CommandArgType* argTypes; std::any* module; }; class CommandsModule { - private: - std::vector commandList; - std::vector splitCommand(std::string command); - CommandArg* getCommandArgs(std::vector& args, const CommandArgType* argTypes, const int argc); - const void printHello(const CommandArg* argv); - const void echo(const CommandArg* argv); - public: - CommandsModule(); - ~CommandsModule(); - template - void addCommand(std::string name, const void(T::*func)(const CommandArg*), const int argc, CommandArgType* argTypes, T* module); - void addCommand(Command c); - Command* lookupCommand(std::string name); - void runCommand(std::string command); - Err checkCommand(std::string command); +private: + std::vector commandList; + std::vector splitCommand(std::string command); + CommandArg* getCommandArgs(std::vector& args, const CommandArgType* argTypes, const int argc); + const void printHello(const CommandArg* argv); + const void echo(const CommandArg* argv); +public: + CommandsModule(); + ~CommandsModule(); + template + void addCommand(std::string name, const void(T::*func)(const CommandArg*), const int argc, CommandArgType* argTypes, T* module); + void addCommand(std::string name, const void(*func)(const CommandArg*), const int argc, CommandArgType* argTypes); + template + void addCommand(std::string name, const void(T::*func)(const CommandArg*), const int argc, std::vector argTypes, T* module); + void addCommand(std::string name, const void(*func)(const CommandArg*), const int argc, std::vector argTypes); + void addCommand(Command c); + Command* lookupCommand(std::string name); + void runCommand(std::string command); + Err checkCommand(std::string command); }; // YES I KNOW THIS IS BAD @@ -68,6 +73,16 @@ class CommandsModule template void CommandsModule::addCommand(std::string name, const void(T::*func)(const CommandArg*), const int argc, CommandArgType* argTypes, T* module) { - Command c = {name, (const void*(std::any::*)(const CommandArg* argv)) func, argc, argTypes, (std::any*)module}; + Command c = {name, (const void*(std::any::*)(const CommandArg* argv)) func, nullptr, argc, argTypes, (std::any*)module}; addCommand(c); } +template +void CommandsModule::addCommand(std::string name, const void(T::*func)(const CommandArg*), const int argc, std::vector argTypes, T* module) +{ + CommandArgType* argTypesArr = new CommandArgType[argc]; + for(int i = 0; i < argc; i++) + { + argTypesArr[i] = argTypes[i]; + } + addCommand(name, func, argc, argTypesArr, module); +} diff --git a/compile_commands.json b/compile_commands.json index 6e04525..3997f42 100644 --- a/compile_commands.json +++ b/compile_commands.json @@ -3,10 +3,30 @@ "arguments": [ "/usr/bin/g++", "-std=c++17", - "-Iinclude", "-I/usr/include/gdk-pixbuf-2.0", "-I/usr/include/glib-2.0", - "-I/usr/lib/glib-2.0/include", + "-I/usr/lib64/glib-2.0/include", + "-I/usr/include/sysprof-4", + "-I/usr/include/libpng16", + "-I/usr/include/libmount", + "-I/usr/include/blkid", + "-pthread", + "-c", + "-o", + "ewmh.o", + "ewmh.cpp" + ], + "directory": "/home/boss/Documents/Coding/WM/YATwm", + "file": "/home/boss/Documents/Coding/WM/YATwm/ewmh.cpp", + "output": "/home/boss/Documents/Coding/WM/YATwm/ewmh.o" + }, + { + "arguments": [ + "/usr/bin/g++", + "-std=c++17", + "-I/usr/include/gdk-pixbuf-2.0", + "-I/usr/include/glib-2.0", + "-I/usr/lib64/glib-2.0/include", "-I/usr/include/sysprof-4", "-I/usr/include/libpng16", "-I/usr/include/libmount", @@ -20,5 +40,89 @@ "directory": "/home/boss/Documents/Coding/WM/YATwm", "file": "/home/boss/Documents/Coding/WM/YATwm/main.cpp", "output": "/home/boss/Documents/Coding/WM/YATwm/main.o" + }, + { + "arguments": [ + "/usr/bin/g++", + "-std=c++17", + "-I/usr/include/gdk-pixbuf-2.0", + "-I/usr/include/glib-2.0", + "-I/usr/lib64/glib-2.0/include", + "-I/usr/include/sysprof-4", + "-I/usr/include/libpng16", + "-I/usr/include/libmount", + "-I/usr/include/blkid", + "-pthread", + "-c", + "-o", + "util.o", + "util.cpp" + ], + "directory": "/home/boss/Documents/Coding/WM/YATwm", + "file": "/home/boss/Documents/Coding/WM/YATwm/util.cpp", + "output": "/home/boss/Documents/Coding/WM/YATwm/util.o" + }, + { + "arguments": [ + "/usr/bin/g++", + "-std=c++17", + "-I/usr/include/gdk-pixbuf-2.0", + "-I/usr/include/glib-2.0", + "-I/usr/lib64/glib-2.0/include", + "-I/usr/include/sysprof-4", + "-I/usr/include/libpng16", + "-I/usr/include/libmount", + "-I/usr/include/blkid", + "-pthread", + "-c", + "-o", + "config.o", + "config.cpp" + ], + "directory": "/home/boss/Documents/Coding/WM/YATwm", + "file": "/home/boss/Documents/Coding/WM/YATwm/config.cpp", + "output": "/home/boss/Documents/Coding/WM/YATwm/config.o" + }, + { + "arguments": [ + "/usr/bin/g++", + "-std=c++17", + "-I/usr/include/gdk-pixbuf-2.0", + "-I/usr/include/glib-2.0", + "-I/usr/lib64/glib-2.0/include", + "-I/usr/include/sysprof-4", + "-I/usr/include/libpng16", + "-I/usr/include/libmount", + "-I/usr/include/blkid", + "-pthread", + "-c", + "-o", + "keybinds.o", + "keybinds.cpp" + ], + "directory": "/home/boss/Documents/Coding/WM/YATwm", + "file": "/home/boss/Documents/Coding/WM/YATwm/keybinds.cpp", + "output": "/home/boss/Documents/Coding/WM/YATwm/keybinds.o" + }, + { + "arguments": [ + "/usr/bin/g++", + "-std=c++17", + "-I/usr/include/gdk-pixbuf-2.0", + "-I/usr/include/glib-2.0", + "-I/usr/lib64/glib-2.0/include", + "-I/usr/include/sysprof-4", + "-I/usr/include/libpng16", + "-I/usr/include/libmount", + "-I/usr/include/blkid", + "-pthread", + "-c", + "-o", + "commands.o", + "commands.cpp" + ], + "directory": "/home/boss/Documents/Coding/WM/YATwm", + "file": "/home/boss/Documents/Coding/WM/YATwm/commands.cpp", + "output": "/home/boss/Documents/Coding/WM/YATwm/commands.o" } ] diff --git a/config.cpp b/config.cpp index 422ec19..74a4a1d 100644 --- a/config.cpp +++ b/config.cpp @@ -41,18 +41,6 @@ const void Config::spawn_once(const CommandArg* argv) } } -const void Config::spawn(const CommandArg* argv) -{ - if(fork() == 0) - { - int null = open("/dev/null", O_WRONLY); - dup2(null, 0); - dup2(null, 1); - dup2(null, 2); - system(argv[0].str); - exit(0); - } -} const void Config::changeWS(const CommandArg* argv) { cout << "changeWS called" << endl; @@ -99,24 +87,14 @@ Config::Config(CommandsModule& commandsModule) : commandsModule(commandsModule) { //Register commands for keybinds - CommandArgType* spawnArgs = new CommandArgType[1]; - spawnArgs[0] = STR_REST; - commandsModule.addCommand("spawn", &Config::spawn, 1, spawnArgs, this); - commandsModule.addCommand("spawn_once", &Config::spawn_once, 1, spawnArgs, this); + commandsModule.addCommand("spawn_once", &Config::spawn_once, 1, {STR_REST}, this); commandsModule.addCommand("reload", &Config::reload, 0, {}, this); //Register commands for config - CommandArgType* gapsArgs = new CommandArgType[1]; - gapsArgs[0] = NUM; - commandsModule.addCommand("gaps", &Config::gapsCmd, 1, gapsArgs, this); - commandsModule.addCommand("outergaps", &Config::outerGapsCmd, 1, gapsArgs, this); - CommandArgType* logFileArgs = new CommandArgType[1]; - logFileArgs[0] = STR_REST; - commandsModule.addCommand("logfile", &Config::logFileCmd, 1, logFileArgs, this); - CommandArgType* addWorkspaceArgs = new CommandArgType[2]; - addWorkspaceArgs[0] = STR; - addWorkspaceArgs[1] = NUM_ARR_REST; - commandsModule.addCommand("addworkspace", &Config::addWorkspaceCmd, 2, addWorkspaceArgs, this); + commandsModule.addCommand("gaps", &Config::gapsCmd, 1, {NUM}, this); + commandsModule.addCommand("outergaps", &Config::outerGapsCmd, 1, {NUM}, this); + commandsModule.addCommand("logfile", &Config::logFileCmd, 1, {STR_REST}, this); + commandsModule.addCommand("addworkspace", &Config::addWorkspaceCmd, 2, {STR, NUM_ARR_REST}, this); } std::vector Config::reloadFile() @@ -128,7 +106,7 @@ std::vector Config::reloadFile() std::vector Config::loadFromFile(std::string path) { - std::vector ers; + std::vector errs; file = path; //Set defaults @@ -152,12 +130,13 @@ std::vector Config::loadFromFile(std::string path) } catch (Err e) { - ers.push_back({e.code, "Error in config (line " + std::to_string(line) + "): " + std::to_string(e.code) + "\n\tMessage: " + e.message}); + errs.push_back({e.code, "Error in config (line " + std::to_string(line) + "): " + std::to_string(e.code) + "\n\tMessage: " + e.message}); } line++; } loaded = true; + return errs; } Config::~Config() diff --git a/config.h b/config.h index 55e55aa..e6d6227 100644 --- a/config.h +++ b/config.h @@ -47,7 +47,6 @@ public: // Keybind Commands COMMAND(exit); - COMMAND(spawn); COMMAND(spawn_once); COMMAND(changeWS); COMMAND(wToWS); diff --git a/main.cpp b/main.cpp index 3f541a1..bf523fd 100644 --- a/main.cpp +++ b/main.cpp @@ -925,6 +925,8 @@ int main(int argc, char** argv) dpy = XOpenDisplay(nullptr); root = Window(DefaultRootWindow(dpy)); + // Adding commands + commandsModule.addCommand("spawn", spawn, 1, {STR_REST}); //Config std::string home = getenv("HOME"); -- cgit v1.2.3