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 "utilities/Util.hpp"
#include <algorithm>
@ -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) {

View File

@ -4,13 +4,14 @@
#include <vector>
#include "windowManager.hpp"
#include <map>
namespace KeybindManager {
inline std::vector<Keybind> 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);
};

View File

@ -1,5 +1,6 @@
#include "ConfigManager.hpp"
#include "../windowManager.hpp"
#include "../KeybindManager.hpp"
#include <sys/stat.h>
#include <sys/types.h>
@ -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 <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) {
// 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);
}
}
}

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() {
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;