Added keybinds to the config

This commit is contained in:
vaxerski 2021-11-21 13:11:51 +01:00
parent 5a5ecafd91
commit ca9821fec4
6 changed files with 105 additions and 24 deletions

View file

@ -1,4 +1,5 @@
#include "KeybindManager.hpp" #include "KeybindManager.hpp"
#include "utilities/Util.hpp"
#include <algorithm> #include <algorithm>
@ -11,33 +12,40 @@ Keybind* KeybindManager::findKeybindByKey(int mod, xcb_keysym_t keysym) {
return nullptr; return nullptr;
} }
void KeybindManager::reloadAllKeybinds() { uint32_t KeybindManager::getKeyCodeFromName(std::string name) {
KeybindManager::keybinds.clear(); if (name == "")
return 0;
// todo: config transform(name.begin(), name.end(), name.begin(), ::tolower);
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));
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 try {
KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0xff51 /* < */, "l", &KeybindManager::movewindow)); returnValue = returnValue.substr(returnValue.find_first_of('x') + 1);
KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0xff53 /* > */, "r", &KeybindManager::movewindow)); returnValue = returnValue.substr(0, returnValue.find_first_of(' '));
KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0xff52 /* ^ */, "t", &KeybindManager::movewindow));
KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0xff54 /* v */, "b", &KeybindManager::movewindow));
// Fullscreen return std::stoi(returnValue, nullptr, 16); // return hex to int
KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0x66 /* F */, "", &KeybindManager::toggleActiveWindowFullscreen)); } catch(...) { }
} else {
// workspace binds if (name == "return" || name == "enter") {
for (int i = 0; i < 10; ++i) { return 0xff0d;
// MOD + 1-9 } else if (name == "left") {
KeybindManager::keybinds.push_back(Keybind(MOD_SUPER, 0x31 + i, std::to_string(i + 1), &KeybindManager::changeworkspace)); 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) { unsigned int KeybindManager::modToMask(MODS mod) {
switch(mod) { switch(mod) {
case MOD_NONE: case MOD_NONE:

View file

@ -4,13 +4,14 @@
#include <vector> #include <vector>
#include "windowManager.hpp" #include "windowManager.hpp"
#include <map>
namespace KeybindManager { namespace KeybindManager {
inline std::vector<Keybind> keybinds; inline std::vector<Keybind> keybinds;
unsigned int modToMask(MODS); unsigned int modToMask(MODS);
Keybind* findKeybindByKey(int mod, xcb_keysym_t keysym); Keybind* findKeybindByKey(int mod, xcb_keysym_t keysym);
void reloadAllKeybinds();
xcb_keysym_t getKeysymFromKeycode(xcb_keycode_t keycode); xcb_keysym_t getKeysymFromKeycode(xcb_keycode_t keycode);
xcb_keycode_t getKeycodeFromKeysym(xcb_keysym_t keysym); xcb_keycode_t getKeycodeFromKeysym(xcb_keysym_t keysym);
@ -20,4 +21,7 @@ namespace KeybindManager {
void movewindow(std::string args); void movewindow(std::string args);
void changeworkspace(std::string args); void changeworkspace(std::string args);
void toggleActiveWindowFullscreen(std::string args); void toggleActiveWindowFullscreen(std::string args);
uint32_t getKeyCodeFromName(std::string);
}; };

View file

@ -1,5 +1,6 @@
#include "ConfigManager.hpp" #include "ConfigManager.hpp"
#include "../windowManager.hpp" #include "../windowManager.hpp"
#include "../KeybindManager.hpp"
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
@ -18,6 +19,40 @@ void ConfigManager::init() {
ConfigManager::loadConfigLoadVars(); ConfigManager::loadConfigLoadVars();
} }
void handleBind(const std::string& command, const std::string& value) {
// example:
// bind=SUPER,G,exec,dmenu_run <args>
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) { void parseLine(std::string& line) {
// first check if its not a comment // first check if its not a comment
const auto COMMENTSTART = line.find_first_of('#'); const auto COMMENTSTART = line.find_first_of('#');
@ -38,6 +73,11 @@ void parseLine(std::string& line) {
const auto COMMAND = line.substr(0, EQUALSPLACE); const auto COMMAND = line.substr(0, EQUALSPLACE);
const auto VALUE = line.substr(EQUALSPLACE + 1); const auto VALUE = line.substr(EQUALSPLACE + 1);
if (COMMAND == "bind") {
handleBind(COMMAND, VALUE);
return;
}
if (ConfigManager::configValues.find(COMMAND) == ConfigManager::configValues.end()) if (ConfigManager::configValues.find(COMMAND) == ConfigManager::configValues.end())
return; return;
@ -66,6 +106,8 @@ void parseLine(std::string& line) {
void ConfigManager::loadConfigLoadVars() { void ConfigManager::loadConfigLoadVars() {
Debug::log(LOG, "Reloading the config!"); Debug::log(LOG, "Reloading the config!");
KeybindManager::keybinds.clear();
const char* const ENVHOME = getenv("HOME"); const char* const ENVHOME = getenv("HOME");
const std::string CONFIGPATH = ENVHOME + (std::string) "/.config/hypr/hypr.conf"; const std::string CONFIGPATH = ENVHOME + (std::string) "/.config/hypr/hypr.conf";
@ -82,7 +124,13 @@ void ConfigManager::loadConfigLoadVars() {
if (ifs.is_open()) { if (ifs.is_open()) {
while (std::getline(ifs, line)) { while (std::getline(ifs, line)) {
// Read line by line. // Read line by line.
try {
parseLine(line); parseLine(line);
} catch(...) {
Debug::log(ERR, "Error reading line from config. Line:");
Debug::log(NONE, line);
}
} }
} }

16
src/utilities/Util.cpp Normal file
View file

@ -0,0 +1,16 @@
#include "Util.hpp"
// Execute a shell command and get the output
std::string exec(const char* cmd) {
std::array<char, 128> buffer;
std::string result;
const std::unique_ptr<FILE, decltype(&pclose)> 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;
}

6
src/utilities/Util.hpp Normal file
View file

@ -0,0 +1,6 @@
#pragma once
#include "../defines.hpp"
std::string exec(const char* cmd);

View file

@ -15,7 +15,6 @@ xcb_visualtype_t* CWindowManager::setupColors() {
} }
void CWindowManager::setupManager() { void CWindowManager::setupManager() {
KeybindManager::reloadAllKeybinds();
ConfigManager::init(); ConfigManager::init();
Values[0] = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE; Values[0] = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE;