Skip to content

Commit

Permalink
Grabbing key from configured bindings
Browse files Browse the repository at this point in the history
- Grabbing key from configured bindings on root window
- Grabbing key from configured bindings on every framed client
  • Loading branch information
corecaps committed Feb 11, 2024
1 parent 40b6664 commit 5039c31
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 34 deletions.
8 changes: 8 additions & 0 deletions inc/Config/Binding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,17 @@ class Binding {
const std::string &getCommandName() const;
const std::string &getArgs() const;

private:
public:
unsigned int getModMask() const;

int getKeyCode() const;

private:
std::string mod_;
unsigned int modMask_;
std::string key_;
int keyCode_;
std::string commandName_;
std::string args_;
CommandBase *command_;
Expand Down
18 changes: 3 additions & 15 deletions src/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include "Layouts/LayoutManager.hpp"
#include "Group.hpp"
#include "Logger.hpp"
#include "Config/ConfigHandler.hpp"
#include "Config/ConfigDataBindings.hpp"

Client::Client(Display *display,
Window root,
Expand Down Expand Up @@ -138,21 +140,7 @@ Client_Err Client::frame() {
// GrabModeAsync,
// None,
// None);
XGrabKey(
display_,
XKeysymToKeycode(display_, XK_2),
Mod1Mask,
window_,
false,
GrabModeAsync,
GrabModeAsync);
XGrabKey(display_,
XKeysymToKeycode(display_, XK_1),
Mod1Mask ,
window_,
false,
GrabModeAsync,
GrabModeAsync);
ConfigHandler::GetInstance().getConfigData<ConfigDataBindings>()->grabKeys(display_, window_);
this->framed = true;
this->group_->AddClient(window_,this);
return YGG_CLI_NO_ERROR;
Expand Down
29 changes: 24 additions & 5 deletions src/Config/Binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,47 @@
#include "Logger.hpp"
#include "Commands/FocusGroup.hpp"
#include "Commands/Spawn.hpp"
extern "C" {
#include <X11/Xlib.h>

}

void Binding::init(std::string Mod, std::string Key, std::string Command, std::string Args) {
mod_ = Mod;
key_ = Key;
commandName_ = Command;
args_ = Args;
Logger::GetInstance()->Log("New Binding registered.\n===============================\tModKey:[" + mod_ + "]\tKey [" + key_ + "]\tCommand Name [" + commandName_ + "]\tArguments:[" + args_ +"]", L_INFO);
if (commandName_ == "FocusGroup") {
command_ = new FocusGroup();
} else if (commandName_ == "Spawn") {
command_ = new Spawn();
} else {
Logger::GetInstance()->Log("Unknown command : " + commandName_, L_WARNING);
}
if (mod_ == "Mod4") {
modMask_ = Mod4Mask;
} else if (mod_ == "Mod1") {
modMask_ = Mod1Mask;
} else {
Logger::GetInstance()->Log("Unknown mod : " + mod_, L_WARNING);
}
keyCode_ = XKeysymToKeycode(XOpenDisplay(NULL), XStringToKeysym(key_.c_str()));
Logger::GetInstance()->Log("New Binding registered.\n=======================\tModKey:[" + mod_ + "] Key [" + key_ + "] Command Name [" + commandName_ + "] Arguments:[" + args_ +"] ModMask ["+std::to_string(modMask_)+"] Keycode [" + std::to_string(keyCode_) + "]", L_INFO);
}
void Binding::execute() {
command_->execute(args_);
}
Binding::Binding() {

}
Binding::Binding() :
mod_(""),
modMask_(0),
key_(""),
keyCode_(0),
commandName_(""),
args_(""),
command_(nullptr) {}
const std::string &Binding::getMod() const { return mod_; }
const std::string &Binding::getKey() const { return key_; }
const std::string &Binding::getCommandName() const { return commandName_; }
const std::string &Binding::getArgs() const { return args_; }
const std::string &Binding::getArgs() const { return args_; }
unsigned int Binding::getModMask() const { return modMask_; }
int Binding::getKeyCode() const { return keyCode_; }
7 changes: 7 additions & 0 deletions src/Config/ConfigDataBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,10 @@ ConfigDataBindings::~ConfigDataBindings() {
delete binding;
}
}

void ConfigDataBindings::grabKeys(Display *display, Window window) {
for (auto &binding : bindings_) {
XGrabKey(display, binding->getKeyCode(), binding->getModMask(), window, true, GrabModeAsync, GrabModeAsync);
}
XFlush(display);
}
16 changes: 2 additions & 14 deletions src/WindowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "Config/ConfigDataBars.hpp"
#include "Config/ConfigHandler.hpp"
#include "Config/ConfigDataGroups.hpp"
#include "Config/ConfigDataBindings.hpp"

bool WindowManager::wm_detected_;
WindowManager * WindowManager::instance_ = nullptr;
Expand Down Expand Up @@ -70,6 +71,7 @@ WindowManager::~WindowManager() {
}
void WindowManager::Init() {
selectEventOnRoot();
ConfigHandler::GetInstance().getConfigData<ConfigDataBindings>()->grabKeys(display_, root_);
if (wm_detected_) {
throw std::runtime_error("Another window manager is already running.");
}
Expand All @@ -87,20 +89,6 @@ void WindowManager::selectEventOnRoot() const {
root_,
SubstructureRedirectMask | SubstructureNotifyMask | FocusChangeMask);
XSetErrorHandler(&WindowManager::OnXError);
XGrabKey(display_,
XKeysymToKeycode(display_, XK_1),
Mod1Mask ,
root_,
false,
GrabModeAsync,
GrabModeAsync);
XGrabKey(display_,
XKeysymToKeycode(display_, XK_2),
Mod1Mask ,
root_,
false,
GrabModeAsync,
GrabModeAsync);
XSync(display_, false);
}
void WindowManager::getTopLevelWindows() {
Expand Down

0 comments on commit 5039c31

Please sign in to comment.