From aa1500fea32db04c9e4fe72786ebd7e3479b6a8a Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Sat, 4 Jan 2025 15:14:05 +1300 Subject: feat: Commands restructure + home manager module now makes config Commands module is now a separate libraray that the flake includes. The home manager module will now auto generate the config and has options for different things such as keybinds, gaps, workspaces, etc. --- src/IPC.h | 2 +- src/commands.cpp | 335 ------------------------------------------------------- src/commands.h | 89 --------------- src/config.cpp | 5 +- src/config.h | 2 +- src/keybinds.cpp | 5 +- src/keybinds.h | 2 +- src/main.cpp | 3 +- 8 files changed, 11 insertions(+), 432 deletions(-) delete mode 100644 src/commands.cpp delete mode 100644 src/commands.h (limited to 'src') diff --git a/src/IPC.h b/src/IPC.h index e0bbcee..0705c2b 100644 --- a/src/IPC.h +++ b/src/IPC.h @@ -3,7 +3,7 @@ #include #include -#include "commands.h" +#include #include "config.h" #include "util.h" diff --git a/src/commands.cpp b/src/commands.cpp deleted file mode 100644 index 5688da4..0000000 --- a/src/commands.cpp +++ /dev/null @@ -1,335 +0,0 @@ -#include "commands.h" -#include "error.h" -#include "util.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -using std::cout, std::endl, std::string, std::vector; - -const void CommandsModule::echo(const CommandArg* argv) -{ - cout << argv[0].str << endl; -} - -CommandsModule::CommandsModule() -{ - addCommand("echo", &CommandsModule::echo, 1, {STR_REST}, this); -} -CommandsModule::~CommandsModule() -{ - for(Command c : commandList) - { - if(c.argc > 0) - delete[] c.argTypes; - } -} - -void CommandsModule::addCommand(Command c) -{ - if(lookupCommand(c.name) != nullptr) - { - cout << "Duplicate command: " << c.name << endl; - } - 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 -{ - NameMatches(string s): s_{s} {} - bool operator()(Command c) { return (c.name == s_); } - string s_; -}; - -Command* CommandsModule::lookupCommand(string name) -{ - auto elem = std::find_if(commandList.begin(), commandList.end(), NameMatches(name)); - if (elem != commandList.end()) - { - int i = elem - commandList.begin(); - return &(commandList[i]); - } - else - { - return nullptr; - } -} - -vector CommandsModule::splitCommand(string command) -{ - vector v; - string arg = ""; - bool inQuotes = false; - bool escapeNext = true; - char quoteType; - for(int i = 0; i < command.size(); i++) - { - if(escapeNext) - { - arg += command[i]; - escapeNext = false; - } - else if(command[i] == '\\') - { - escapeNext = true; - } - else if(inQuotes) - { - if(command[i] == quoteType) - { - if(arg != "") - { - v.push_back(arg); - arg = ""; - } - inQuotes = false; - } - else - { - arg += command[i]; - } - } - else - { - if(command[i] == ' ') - { - if(arg != "") - { - v.push_back(arg); - arg = ""; - } - } - else if(command[i] == '"' || command[i] == '\'') - { - inQuotes = true; - quoteType = command[i]; - } - else - { - arg += command[i]; - } - } - } - if(arg != "") - v.push_back(arg); - return v; -} - -CommandArg* CommandsModule::getCommandArgs(vector& split, const CommandArgType* argTypes, const int argc) -{ - CommandArg* args = new CommandArg[argc]; - for(int i = 1; i < argc + 1; i++) - { - switch(argTypes[i-1]) - { - case STR: args[i-1].str = (char*)split[i].c_str(); break; - case NUM: - { - try - { - args[i-1].num = std::stoi(split[i]); - break; - } - catch(std::invalid_argument e) - { - delete[] args; - throw Err(CMD_ERR_WRONG_ARGS, split[i] + " is not a number!"); - } - } - case MOVDIR: - { - if(lowercase(split[i]) == "up") - args[i-1].dir = UP; - else if(lowercase(split[i]) == "down") - args[i-1].dir = DOWN; - else if(lowercase(split[i]) == "left") - args[i-1].dir = LEFT; - else if(lowercase(split[i]) == "right") - args[i-1].dir = RIGHT; - else - { - delete[] args; - throw Err(CMD_ERR_WRONG_ARGS, split[i] + " is not a direction!"); - } - break; - } - case STR_REST: - { - string rest = ""; - for(int j = i; j < split.size(); j++) - { - rest += split[j]; - if(j != split.size() - 1) - rest += " "; - } - args[i-1].str = new char[rest.size()]; - strncpy(args[i-1].str, rest.c_str(), rest.size()); - return args; - } - case NUM_ARR_REST: - { - int* rest = new int[split.size() - i]; - for(int j = 0; j < split.size() - i; j++) - { - try - { - rest[j] = std::stoi(split[j + i]); - } - catch(std::invalid_argument e) - { - delete[] rest; - delete[] args; - throw Err(CMD_ERR_WRONG_ARGS, split[i] + " is not a number!"); - } - } - args[i-1].numArr = {rest, (int) split.size() - i}; - return args; - } - default: cout << "UH OH SOMETHING IS VERY WRONG" << endl; - } - } - return args; -} - -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, "wrong number of arguments"); - CommandArg* args; - try - { - args = getCommandArgs(split, cmd->argTypes, cmd->argc); - } - catch(Err e) - { - throw e; - } - try - { - if(cmd->module == nullptr) - cmd->staticFunc(args); - else - cmd->func(*cmd->module, args); - } - catch (Err e) - { - for(int i = 0; i < cmd->argc; i++) - { - if(cmd->argTypes[i] == STR_REST) - delete[] args[i].str; - } - delete[] args; - throw e; - } - for(int i = 0; i < cmd->argc; i++) - { - if(cmd->argTypes[i] == STR_REST) - delete[] args[i].str; - } - delete[] args; -} - -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, "wrong number of arguments"); - CommandArg* args; - try - { - args = getCommandArgs(split, cmd->argTypes, cmd->argc); - } - catch(Err e) - { - return e; - } - for(int i = 0; i < cmd->argc; i++) - { - if(cmd->argTypes[i] == STR_REST || cmd->argTypes[i] == NUM_ARR_REST) - delete[] args[i].str; - } - delete[] args; - return Err(NOERR, ""); -} diff --git a/src/commands.h b/src/commands.h deleted file mode 100644 index af4a4a0..0000000 --- a/src/commands.h +++ /dev/null @@ -1,89 +0,0 @@ -#pragma once - -#include "error.h" - -#include -#include -#include -#include - -enum MoveDir - { - UP, - RIGHT, - DOWN, - LEFT - }; -enum CommandArgType - { - STR, - NUM, - MOVDIR, - STR_REST, - NUM_ARR_REST - }; - -struct NumArr -{ - int* arr; - int size; -}; -typedef union -{ - char* str; - int num; - NumArr numArr; - MoveDir dir; -} CommandArg; - -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 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); - void runCommand(std::vector split); - std::vector checkCommand(std::string command); - Err checkCommand(std::vector split); -}; - -// YES I KNOW THIS IS BAD -// but it needs to be done this way -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, 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/src/config.cpp b/src/config.cpp index 925e6ea..df195aa 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -1,6 +1,7 @@ #include "config.h" -#include "commands.h" -#include "error.h" +#include +//TODO: FIX THIS +#include #include diff --git a/src/config.h b/src/config.h index 452db9c..b04753f 100644 --- a/src/config.h +++ b/src/config.h @@ -1,8 +1,8 @@ #pragma once -#include "commands.h" #include #include +#include #include #include diff --git a/src/keybinds.cpp b/src/keybinds.cpp index 235f8c1..127a534 100644 --- a/src/keybinds.cpp +++ b/src/keybinds.cpp @@ -7,8 +7,9 @@ #include #include -#include "commands.h" -#include "error.h" +#include +//TODO: FIX THIS +#include #include "keybinds.h" #include "util.h" diff --git a/src/keybinds.h b/src/keybinds.h index a742240..5fd4fba 100644 --- a/src/keybinds.h +++ b/src/keybinds.h @@ -8,7 +8,7 @@ #include #include -#include "commands.h" +#include #include "config.h" #include "util.h" diff --git a/src/main.cpp b/src/main.cpp index da1ae9b..3e01b4d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -36,7 +36,8 @@ #include "config.h" #include "util.h" #include "ewmh.h" -#include "error.h" +//TODO: FIX THIS +#include using std::cout; using std::string; -- cgit v1.2.3 From e9c351d109439ca27c472f0d8d5f8a530ce24033 Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Sat, 4 Jan 2025 20:28:39 +1300 Subject: revert: Added the commands library back to the source --- src/IPC.h | 2 +- src/commands.cpp | 372 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/commands.h | 91 ++++++++++++++ src/config.cpp | 5 +- src/config.h | 2 +- src/keybinds.cpp | 5 +- src/keybinds.h | 2 +- 7 files changed, 470 insertions(+), 9 deletions(-) create mode 100644 src/commands.cpp create mode 100644 src/commands.h (limited to 'src') diff --git a/src/IPC.h b/src/IPC.h index 0705c2b..e0bbcee 100644 --- a/src/IPC.h +++ b/src/IPC.h @@ -3,7 +3,7 @@ #include #include -#include +#include "commands.h" #include "config.h" #include "util.h" diff --git a/src/commands.cpp b/src/commands.cpp new file mode 100644 index 0000000..caa82eb --- /dev/null +++ b/src/commands.cpp @@ -0,0 +1,372 @@ +#include "commands.h" +#include "error.h" +#include "util.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using std::cout, std::endl, std::string, std::vector; + +const void CommandsModule::echo(const CommandArg* argv) +{ + cout << argv[0].str << endl; +} + +const void CommandsModule::loadFile(const CommandArg* argv) +{ + std::vector errs; + + string path = cwd + "/" + std::string(argv[0].str); + + std::ifstream file(path); + if(!file.good()) + throw Err(CMD_ERR_NON_FATAL, "File '" + std::string(argv[0].str) + "' doesn't exist"); + + string prevCWD = cwd; + cwd = std::filesystem::path(path).parent_path(); + + cout << "Loading file '" << argv[0].str << "'" << endl; + + int line = 0; + for(string cmd; std::getline(file, cmd);) + { + line++; + if(cmd.size() == 0) + continue; + if(cmd.at(0) == '#') + continue; + try + { + this->runCommand(cmd); + } + catch (Err e) + { + throw Err(e.code, "Error in file '" + std::string(argv[0].str) + "' (line " + std::to_string(line) + "): " + std::to_string(e.code) + "\n\tMessage: " + e.message); + } + } + + cwd = prevCWD; +} + +CommandsModule::CommandsModule() +{ + addCommand("echo", &CommandsModule::echo, 1, {STR_REST}, this); + addCommand("loadFile", &CommandsModule::loadFile, 1, {STR_REST}, this); + cwd = std::filesystem::current_path(); +} +CommandsModule::~CommandsModule() +{ + for(Command c : commandList) + { + if(c.argc > 0) + delete[] c.argTypes; + } +} + +void CommandsModule::addCommand(Command c) +{ + if(lookupCommand(c.name) != nullptr) + { + cout << "Duplicate command: " << c.name << endl; + } + 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 +{ + NameMatches(string s): s_{s} {} + bool operator()(Command c) { return (c.name == s_); } + string s_; +}; + +Command* CommandsModule::lookupCommand(string name) +{ + auto elem = std::find_if(commandList.begin(), commandList.end(), NameMatches(name)); + if (elem != commandList.end()) + { + int i = elem - commandList.begin(); + return &(commandList[i]); + } + else + { + return nullptr; + } +} + +vector CommandsModule::splitCommand(string command) +{ + vector v; + string arg = ""; + bool inQuotes = false; + bool escapeNext = true; + char quoteType; + for(int i = 0; i < command.size(); i++) + { + if(escapeNext) + { + arg += command[i]; + escapeNext = false; + } + else if(command[i] == '\\') + { + escapeNext = true; + } + else if(inQuotes) + { + if(command[i] == quoteType) + { + if(arg != "") + { + v.push_back(arg); + arg = ""; + } + inQuotes = false; + } + else + { + arg += command[i]; + } + } + else + { + if(command[i] == ' ') + { + if(arg != "") + { + v.push_back(arg); + arg = ""; + } + } + else if(command[i] == '"' || command[i] == '\'') + { + inQuotes = true; + quoteType = command[i]; + } + else + { + arg += command[i]; + } + } + } + if(arg != "") + v.push_back(arg); + return v; +} + +CommandArg* CommandsModule::getCommandArgs(vector& split, const CommandArgType* argTypes, const int argc) +{ + CommandArg* args = new CommandArg[argc]; + for(int i = 1; i < argc + 1; i++) + { + switch(argTypes[i-1]) + { + case STR: args[i-1].str = (char*)split[i].c_str(); break; + case NUM: + { + try + { + args[i-1].num = std::stoi(split[i]); + break; + } + catch(std::invalid_argument e) + { + delete[] args; + throw Err(CMD_ERR_WRONG_ARGS, split[i] + " is not a number!"); + } + } + case MOVDIR: + { + if(lowercase(split[i]) == "up") + args[i-1].dir = UP; + else if(lowercase(split[i]) == "down") + args[i-1].dir = DOWN; + else if(lowercase(split[i]) == "left") + args[i-1].dir = LEFT; + else if(lowercase(split[i]) == "right") + args[i-1].dir = RIGHT; + else + { + delete[] args; + throw Err(CMD_ERR_WRONG_ARGS, split[i] + " is not a direction!"); + } + break; + } + case STR_REST: + { + string rest = ""; + for(int j = i; j < split.size(); j++) + { + rest += split[j]; + if(j != split.size() - 1) + rest += " "; + } + args[i-1].str = new char[rest.size()]; + strncpy(args[i-1].str, rest.c_str(), rest.size()); + return args; + } + case NUM_ARR_REST: + { + int* rest = new int[split.size() - i]; + for(int j = 0; j < split.size() - i; j++) + { + try + { + rest[j] = std::stoi(split[j + i]); + } + catch(std::invalid_argument e) + { + delete[] rest; + delete[] args; + throw Err(CMD_ERR_WRONG_ARGS, split[i] + " is not a number!"); + } + } + args[i-1].numArr = {rest, (int) split.size() - i}; + return args; + } + default: cout << "UH OH SOMETHING IS VERY WRONG" << endl; + } + } + return args; +} + +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, "wrong number of arguments"); + CommandArg* args; + try + { + args = getCommandArgs(split, cmd->argTypes, cmd->argc); + } + catch(Err e) + { + throw e; + } + try + { + if(cmd->module == nullptr) + cmd->staticFunc(args); + else + cmd->func(*cmd->module, args); + } + catch (Err e) + { + for(int i = 0; i < cmd->argc; i++) + { + if(cmd->argTypes[i] == STR_REST) + delete[] args[i].str; + } + delete[] args; + throw e; + } + for(int i = 0; i < cmd->argc; i++) + { + if(cmd->argTypes[i] == STR_REST) + delete[] args[i].str; + } + delete[] args; +} + +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, "wrong number of arguments"); + CommandArg* args; + try + { + args = getCommandArgs(split, cmd->argTypes, cmd->argc); + } + catch(Err e) + { + return e; + } + for(int i = 0; i < cmd->argc; i++) + { + if(cmd->argTypes[i] == STR_REST || cmd->argTypes[i] == NUM_ARR_REST) + delete[] args[i].str; + } + delete[] args; + return Err(NOERR, ""); +} diff --git a/src/commands.h b/src/commands.h new file mode 100644 index 0000000..4825f1c --- /dev/null +++ b/src/commands.h @@ -0,0 +1,91 @@ +#pragma once + +#include "error.h" + +#include +#include +#include +#include + +enum MoveDir + { + UP, + RIGHT, + DOWN, + LEFT + }; +enum CommandArgType + { + STR, + NUM, + MOVDIR, + STR_REST, + NUM_ARR_REST + }; + +struct NumArr +{ + int* arr; + int size; +}; +typedef union +{ + char* str; + int num; + NumArr numArr; + MoveDir dir; +} CommandArg; + +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 echo(const CommandArg* argv); + const void loadFile(const CommandArg* argv); + std::string cwd; +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); + void runCommand(std::vector split); + std::vector checkCommand(std::string command); + Err checkCommand(std::vector split); +}; + +// YES I KNOW THIS IS BAD +// but it needs to be done this way +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, 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/src/config.cpp b/src/config.cpp index df195aa..925e6ea 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -1,7 +1,6 @@ #include "config.h" -#include -//TODO: FIX THIS -#include +#include "commands.h" +#include "error.h" #include diff --git a/src/config.h b/src/config.h index b04753f..55e4fd3 100644 --- a/src/config.h +++ b/src/config.h @@ -2,7 +2,7 @@ #include #include -#include +#include "commands.h" #include #include diff --git a/src/keybinds.cpp b/src/keybinds.cpp index 127a534..235f8c1 100644 --- a/src/keybinds.cpp +++ b/src/keybinds.cpp @@ -7,9 +7,8 @@ #include #include -#include -//TODO: FIX THIS -#include +#include "commands.h" +#include "error.h" #include "keybinds.h" #include "util.h" diff --git a/src/keybinds.h b/src/keybinds.h index 5fd4fba..a742240 100644 --- a/src/keybinds.h +++ b/src/keybinds.h @@ -8,7 +8,7 @@ #include #include -#include +#include "commands.h" #include "config.h" #include "util.h" -- cgit v1.2.3 From 63005f5fc81e9b641f07eadb5f07bc7c532d40c7 Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Sat, 4 Jan 2025 20:50:44 +1300 Subject: feat: Added new command line option --config (-c) The new options allows you to specify the location of the config file in the arguments to the program. --- src/main.cpp | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index 3e01b4d..291151f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,7 +5,6 @@ #include #include -#include #include #include @@ -36,8 +35,7 @@ #include "config.h" #include "util.h" #include "ewmh.h" -//TODO: FIX THIS -#include +#include "error.h" using std::cout; using std::string; @@ -1030,13 +1028,15 @@ int main(int argc, char** argv) { int versionFlag = 0; bool immediateExit = false; + string configLocation = ""; while(1) { static option long_options[] = {{"version", no_argument, &versionFlag, 1}, + {"config", required_argument, NULL, 'c'}, {0, 0, 0, 0}}; int optionIndex; - char c = getopt_long(argc, argv, "v", long_options, &optionIndex); + char c = getopt_long(argc, argv, "c:v", long_options, &optionIndex); if(c == -1) break; @@ -1048,8 +1048,12 @@ int main(int argc, char** argv) break; //Option had arg break; + case 'c': + configLocation = string(optarg); + break; case 'v': versionFlag = 1; + break; case '?': //Error?? break; @@ -1124,16 +1128,19 @@ int main(int argc, char** argv) std::vector cfgErr; cout << "Registered commands" << endl; - - char* confDir = getenv("XDG_CONFIG_HOME"); - if(confDir != NULL) - { - cfgErr = cfg.loadFromFile(string(confDir) + "/YATwm/config"); - } + + if(configLocation != "") + cfgErr = cfg.loadFromFile(configLocation); else { - string home = getenv("HOME"); - cfgErr = cfg.loadFromFile(home + "/.config/YATwm/config"); + char* confDir = getenv("XDG_CONFIG_HOME"); + if(confDir != NULL) + cfgErr = cfg.loadFromFile(string(confDir) + "/YATwm/config"); + else + { + string home = getenv("HOME"); + cfgErr = cfg.loadFromFile(home + "/.config/YATwm/config"); + } } cout << "Done config" << endl; @@ -1234,7 +1241,6 @@ int main(int argc, char** argv) } if(ready == -1) { - cout << "E" << endl; log("ERROR"); } } -- cgit v1.2.3 From 74c58cdf74c4921071da93c4cbfaf22f672242a5 Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Thu, 6 Mar 2025 21:26:04 +1300 Subject: feat: Added the "S" modifier to the emacs bind mode You can now use S as a modifier for shift when binding with emacs mode if you're unable to capatilise the key to be bound. --- src/config.cpp | 2 +- src/keybinds.cpp | 55 +++++++++++++++++++++++++++++++------------------------ src/main.cpp | 1 + 3 files changed, 33 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/config.cpp b/src/config.cpp index 925e6ea..df8a43d 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -57,7 +57,7 @@ Config::Config(CommandsModule& commandsModule) 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); + commandsModule.addCommand("addWorkspace", &Config::addWorkspaceCmd, 2, {STR, NUM_ARR_REST}, this); commandsModule.addCommand("swapmods", &Config::swapSuperAltCmd, 0, {}, this); } diff --git a/src/keybinds.cpp b/src/keybinds.cpp index 235f8c1..1f60a75 100644 --- a/src/keybinds.cpp +++ b/src/keybinds.cpp @@ -168,48 +168,55 @@ const Keybind KeybindsModule::emacsBindMode(string bindString) Keybind bind; bind.modifiers = 0; - const std::regex keyRegex("^(?:([CMs])-)?(?:([CMs])-)?(?:([CMs])-)?([^\\s]|(SPC|ESC|RET|))$"); + //cout << "Adding keybind: '" << bindString << "'" << endl; + + const std::regex keyRegex("^((?:[CMSs]-)*)([^\\s]|(?:SPC|ESC|RET))$"); std::smatch keyMatch; if(std::regex_match(bindString, keyMatch, keyRegex)) { - for (int i = 1; i < 3; i++) + std::ssub_match modifierMatch = keyMatch[1]; + for(std::string modifier : split(modifierMatch, '-')) { - std::ssub_match modifierMatch = keyMatch[i]; - if(modifierMatch.matched) + if(modifier == "s") + { + //cout << "\tModifier: s" << endl; + bind.modifiers |= cfg.swapSuperAlt?Mod1Mask:Mod4Mask; + } + else if(modifier == "M") + { + //cout << "\tModifier: M" << endl; + bind.modifiers |= cfg.swapSuperAlt?Mod4Mask:Mod1Mask; + } + else if(modifier == "C") + { + //cout << "\tModifier: C" << endl; + bind.modifiers |= ControlMask; + } + else if(modifier == "S") { - 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; - } + //cout << "\tModifier: S" << endl; + bind.modifiers |= ShiftMask; } } } - KeySym keySym = XStringToKeysym(keyMatch[4].str().c_str()); + KeySym keySym = XStringToKeysym(keyMatch[2].str().c_str()); + //cout << "\tKey: " << keyMatch[2].str() << endl; - if(isUpper(keyMatch[4].str().c_str()) && keySym != NoSymbol) + if(isUpper(keyMatch[2].str().c_str()) && keySym != NoSymbol) { bind.modifiers |= ShiftMask; } if(keySym == NoSymbol) { - if(keyMatch[4].str() == "RET") + if(keyMatch[2].str() == "RET") keySym = XK_Return; - else if(keyMatch[4].str() == "ESC") + else if(keyMatch[2].str() == "ESC") keySym = XK_Escape; - else if(keyMatch[4].str() == "SPC") + else if(keyMatch[2].str() == "SPC") keySym = XK_space; - else if(keyMatch[4].str() == "-") + else if(keyMatch[2].str() == "-") keySym = XK_minus; - else if(keyMatch[4].str() == "+") + else if(keyMatch[2].str() == "+") keySym = XK_plus; else throw Err(CFG_ERR_KEYBIND, "Keybind '" + bindString + "' is invalid"); diff --git a/src/main.cpp b/src/main.cpp index 291151f..45e4eba 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include -- cgit v1.2.3