summaryrefslogtreecommitdiff
path: root/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp73
1 files changed, 62 insertions, 11 deletions
diff --git a/main.cpp b/main.cpp
index b5fa9fa..d81924e 100644
--- a/main.cpp
+++ b/main.cpp
@@ -4,6 +4,9 @@
#include <iostream>
#include <map>
#include <vector>
+#include <unistd.h>
+#include <cstring>
+#include <algorithm>
#include "structs.h"
#include "config.h"
@@ -36,16 +39,66 @@ static int OnXError(Display* display, XErrorEvent* e);
void tile(int frameID, int x, int y, int w, int h);
+//Keybind commands
+void exit(const KeyArg arg)
+{
+ keepGoing = false;
+}
+void spawn(const KeyArg arg)
+{
+ if(fork() == 0)
+ {
+ execvp((char*)arg.str[0], (char**)arg.str);
+ exit(0);
+ }
+}
+void toggle(const KeyArg arg)
+{
+ nextDir = nextDir = (nextDir==horizontal)? vertical : horizontal;
+}
+void kill(const KeyArg arg)
+{
+ Window w;
+ int revertToReturn;
+ XGetInputFocus(dpy, &w, &revertToReturn);
+ Atom* supported_protocols;
+ int num_supported_protocols;
+ if (XGetWMProtocols(dpy,
+ w,
+ &supported_protocols,
+ &num_supported_protocols) &&
+ (std::find(supported_protocols,
+ supported_protocols + num_supported_protocols,
+ XInternAtom(dpy, "WM_DELETE_WINDOW", false)) !=
+ supported_protocols + num_supported_protocols)) {
+ // 1. Construct message.
+ XEvent msg;
+ memset(&msg, 0, sizeof(msg));
+ msg.xclient.type = ClientMessage;
+ msg.xclient.message_type = XInternAtom(dpy, "WM_PROTOCOLS", false);
+ msg.xclient.window = w;
+ msg.xclient.format = 32;
+ msg.xclient.data.l[0] = XInternAtom(dpy, "WM_DELETE_WINDOW", false);
+ // 2. Send message to window to be closed.
+ cout << "Nice kill\n";
+ XSendEvent(dpy, w, false, 0, &msg);
+ } else {
+ cout << "Mean kill\n";
+ XKillClient(dpy, w);
+ }
+}
+
void keyPress(XKeyEvent e)
{
if(e.same_screen!=1) return;
//TODO: Make this into a for loop
- switch(XLookupKeysym(&e, 1))
+ KeySym keysym = XLookupKeysym(&e, 1);
+ for(int i = 0; i < sizeof(keyBinds)/sizeof(keyBinds[0]); i++)
{
- case XK_E: keepGoing = false; break;
- case XK_Return: std::system("alacritty &"); break;
- case XK_D: std::system("rofi -i -show drun &"); break;
- case XK_T: nextDir = (nextDir==horizontal)? vertical : horizontal; break;
+ if(keyBinds[i].keysym == keysym && keyBinds[i].modifiers == e.state)
+ {
+ keyBinds[i].function(keyBinds[i].arg);
+ }
}
}
@@ -180,9 +233,9 @@ void tile(int frameID, int x, int y, int w, int h)
{
if(frameID == 0)
{
- printf("\nTILING ROOT:\n");
+ //printf("\nTILING ROOT:\n");
}
- printf("Tiling frame - ID: %i\n\tx: %i, y: %i, w: %i, h: %i\n", frameID, x, y, w, h);
+ //printf("Tiling frame - ID: %i\n\tx: %i, y: %i, w: %i, h: %i\n", frameID, x, y, w, h);
int i = 0;
vector<int>& subFrameIDs = frames.find(frameID)->second.subFrameIDs;
TileDir dir = frames.find(frameID)->second.dir;
@@ -200,7 +253,7 @@ void tile(int frameID, int x, int y, int w, int h)
continue;
}
Client c = clients.find(f.cID)->second;
- printf("Arranging client with frame ID %i, client ID %i:\n\tx: %i, y: %i, w: %i, h: %i\n", fID, c.ID, wX, wY, wW, wH);
+ //printf("Arranging client with frame ID %i, client ID %i:\n\tx: %i, y: %i, w: %i, h: %i\n", fID, c.ID, wX, wY, wW, wH);
XMoveWindow(dpy, c.w,
wX, wY);
XResizeWindow(dpy, c.w,
@@ -208,7 +261,7 @@ void tile(int frameID, int x, int y, int w, int h)
}
if(frameID == 0)
{
- printf("DONE TILING ROOT\n\n");
+ //printf("DONE TILING ROOT\n\n");
}
}
@@ -229,7 +282,6 @@ int main(int argc, char** argv)
XGrabKey(dpy, XKeysymToKeycode(dpy, keyBinds[i].keysym), keyBinds[i].modifiers, root, false, GrabModeAsync, GrabModeAsync);
}
-
vector<int> v;
Frame rootFrame = {0, noID, false, noID, horizontal, v};
currFrameID++;
@@ -260,7 +312,6 @@ int main(int argc, char** argv)
if(e.xcrossing.window == root)
break;
XSetInputFocus(dpy, e.xcrossing.window, RevertToNone, CurrentTime);
-
break;
default:
//cout << "Unhandled event, code: " << e.type << "!\n";