From ca9821fec4d62605d5b36af3ac877cd0e583b627 Mon Sep 17 00:00:00 2001 From: vaxerski <43317083+vaxerski@users.noreply.github.com> Date: Sun, 21 Nov 2021 13:11:51 +0100 Subject: [PATCH] Added keybinds to the config --- src/KeybindManager.cpp | 48 +++++++++++++++++++-------------- src/KeybindManager.hpp | 6 ++++- src/config/ConfigManager.cpp | 52 ++++++++++++++++++++++++++++++++++-- src/utilities/Util.cpp | 16 +++++++++++ src/utilities/Util.hpp | 6 +++++ src/windowManager.cpp | 1 - 6 files changed, 105 insertions(+), 24 deletions(-) create mode 100644 src/utilities/Util.cpp create mode 100644 src/utilities/Util.hpp diff --git a/src/KeybindManager.cpp b/src/KeybindManager.cpp index 23add20..5aba8da 100644 --- a/src/KeybindManager.cpp +++ b/src/KeybindManager.cpp @@ -1,4 +1,5 @@ #include "KeybindManager.hpp" +#include "utilities/Util.hpp" #include @@ -11,31 +12,38 @@ Keybind* KeybindManager::findKeybindByKey(int mod, xcb_keysym_t keysym) { return nullptr; } -void KeybindManager::reloadAllKeybinds() { - KeybindManager::keybinds.clear(); +uint32_t KeybindManager::getKeyCodeFromName(std::string name) { + if (name == "") + return 0; - // todo: config - KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0x72 /* R */, "dmenu_run", &KeybindManager::call)); - KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0x71 /* Q */, "kitty", &KeybindManager::call)); - KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0xff0d /* Enter */, "xterm", &KeybindManager::call)); - KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0x62 /* G */, "google-chrome-stable", &KeybindManager::call)); + transform(name.begin(), name.end(), name.begin(), ::tolower); - KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0x63 /* C */, "", &KeybindManager::killactive)); + if (name.length() == 1) { + // key + std::string command = "xmodmap -pk | grep \"(" + name + ")\""; + std::string returnValue = exec(command.c_str()); - // move window - KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0xff51 /* < */, "l", &KeybindManager::movewindow)); - KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0xff53 /* > */, "r", &KeybindManager::movewindow)); - KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0xff52 /* ^ */, "t", &KeybindManager::movewindow)); - KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0xff54 /* v */, "b", &KeybindManager::movewindow)); + try { + returnValue = returnValue.substr(returnValue.find_first_of('x') + 1); + returnValue = returnValue.substr(0, returnValue.find_first_of(' ')); - // Fullscreen - KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0x66 /* F */, "", &KeybindManager::toggleActiveWindowFullscreen)); - - // workspace binds - for (int i = 0; i < 10; ++i) { - // MOD + 1-9 - KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0x31 + i, std::to_string(i + 1), &KeybindManager::changeworkspace)); + return std::stoi(returnValue, nullptr, 16); // return hex to int + } catch(...) { } + } else { + if (name == "return" || name == "enter") { + return 0xff0d; + } else if (name == "left") { + return 0xff51; + } else if (name == "right") { + return 0xff53; + } else if (name == "up") { + return 0xff52; + } else if (name == "down") { + return 0xff54; + } } + + return 0; } unsigned int KeybindManager::modToMask(MODS mod) { diff --git a/src/KeybindManager.hpp b/src/KeybindManager.hpp index 25f27a8..2dd444f 100644 --- a/src/KeybindManager.hpp +++ b/src/KeybindManager.hpp @@ -4,13 +4,14 @@ #include #include "windowManager.hpp" +#include + namespace KeybindManager { inline std::vector keybinds; unsigned int modToMask(MODS); Keybind* findKeybindByKey(int mod, xcb_keysym_t keysym); - void reloadAllKeybinds(); xcb_keysym_t getKeysymFromKeycode(xcb_keycode_t keycode); xcb_keycode_t getKeycodeFromKeysym(xcb_keysym_t keysym); @@ -20,4 +21,7 @@ namespace KeybindManager { void movewindow(std::string args); void changeworkspace(std::string args); void toggleActiveWindowFullscreen(std::string args); + + + uint32_t getKeyCodeFromName(std::string); }; \ No newline at end of file diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index 1a72b65..8f88c51 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -1,5 +1,6 @@ #include "ConfigManager.hpp" #include "../windowManager.hpp" +#include "../KeybindManager.hpp" #include #include @@ -18,6 +19,40 @@ void ConfigManager::init() { ConfigManager::loadConfigLoadVars(); } +void handleBind(const std::string& command, const std::string& value) { + + // example: + // bind=SUPER,G,exec,dmenu_run + + auto valueCopy = value; + + const auto MOD = valueCopy.substr(0, valueCopy.find_first_of(",")); + valueCopy = valueCopy.substr(valueCopy.find_first_of(",") + 1); + + const auto KEY = KeybindManager::getKeyCodeFromName(valueCopy.substr(0, valueCopy.find_first_of(","))); + valueCopy = valueCopy.substr(valueCopy.find_first_of(",") + 1); + + const auto HANDLER = valueCopy.substr(0, valueCopy.find_first_of(",")); + valueCopy = valueCopy.substr(valueCopy.find_first_of(",") + 1); + + const auto COMMAND = valueCopy; + + MODS mod = MOD_NONE; + + if (MOD == "SUPER") mod = MOD_SUPER; + else if (MOD == "SHIFT") mod = MOD_SHIFT; + + Dispatcher dispatcher = nullptr; + if (HANDLER == "exec") dispatcher = KeybindManager::call; + if (HANDLER == "killactive") dispatcher = KeybindManager::killactive; + if (HANDLER == "fullscreen") dispatcher = KeybindManager::toggleActiveWindowFullscreen; + if (HANDLER == "movewindow") dispatcher = KeybindManager::call; + if (HANDLER == "workspace") dispatcher = KeybindManager::changeworkspace; + + if (dispatcher) + KeybindManager::keybinds.push_back(Keybind(mod, KEY, COMMAND, dispatcher)); +} + void parseLine(std::string& line) { // first check if its not a comment const auto COMMENTSTART = line.find_first_of('#'); @@ -38,7 +73,12 @@ void parseLine(std::string& line) { const auto COMMAND = line.substr(0, EQUALSPLACE); const auto VALUE = line.substr(EQUALSPLACE + 1); - if (ConfigManager::configValues.find(COMMAND) == ConfigManager::configValues.end()) + if (COMMAND == "bind") { + handleBind(COMMAND, VALUE); + return; + } + + if (ConfigManager::configValues.find(COMMAND) == ConfigManager::configValues.end()) return; auto& CONFIGENTRY = ConfigManager::configValues.at(COMMAND); @@ -66,6 +106,8 @@ void parseLine(std::string& line) { void ConfigManager::loadConfigLoadVars() { Debug::log(LOG, "Reloading the config!"); + KeybindManager::keybinds.clear(); + const char* const ENVHOME = getenv("HOME"); const std::string CONFIGPATH = ENVHOME + (std::string) "/.config/hypr/hypr.conf"; @@ -82,7 +124,13 @@ void ConfigManager::loadConfigLoadVars() { if (ifs.is_open()) { while (std::getline(ifs, line)) { // Read line by line. - parseLine(line); + try { + parseLine(line); + } catch(...) { + Debug::log(ERR, "Error reading line from config. Line:"); + Debug::log(NONE, line); + } + } } diff --git a/src/utilities/Util.cpp b/src/utilities/Util.cpp new file mode 100644 index 0000000..163a9a9 --- /dev/null +++ b/src/utilities/Util.cpp @@ -0,0 +1,16 @@ +#include "Util.hpp" + +// Execute a shell command and get the output +std::string exec(const char* cmd) { + std::array buffer; + std::string result; + const std::unique_ptr pipe(popen(cmd, "r"), pclose); + if (!pipe) { + Debug::log(ERR, "Exec failed in pipe."); + return ""; + } + while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) { + result += buffer.data(); + } + return result; +} \ No newline at end of file diff --git a/src/utilities/Util.hpp b/src/utilities/Util.hpp new file mode 100644 index 0000000..750d1f1 --- /dev/null +++ b/src/utilities/Util.hpp @@ -0,0 +1,6 @@ +#pragma once + +#include "../defines.hpp" + +std::string exec(const char* cmd); + diff --git a/src/windowManager.cpp b/src/windowManager.cpp index 5719e14..36b9802 100644 --- a/src/windowManager.cpp +++ b/src/windowManager.cpp @@ -15,7 +15,6 @@ xcb_visualtype_t* CWindowManager::setupColors() { } void CWindowManager::setupManager() { - KeybindManager::reloadAllKeybinds(); ConfigManager::init(); Values[0] = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE;