-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasicWM.cpp
97 lines (82 loc) · 2.58 KB
/
basicWM.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "config.hpp"
#include "basicWM.hpp"
#include <X11/X.h>
#include <X11/Xlib.h>
#include <iostream>
#include <sstream>
#include <string>
#include <regex>
char* nameString = "basicWM v0.0.3";
void handleEvent() {
XNextEvent(display, &event);
if(event.type == KeyPress) {
for (cKeybind bind : keybinds) {
bind.processKeyPress(event);
}
}
else if (event.type == MapRequest) {
windowList.push_back(event.xmaprequest.window);
XMapWindow(display, event.xmaprequest.window);
}
}
void handleFocus() {
int x, y;
Window r, c;
int rx, ry;
unsigned int m;
if (XQueryPointer(display, root, &r, &c, &rx, &ry, &x, &y, &m)) {
focusedWindow = c;
}
XRaiseWindow(display, focusedWindow);
}
std::string generateBarString() {
/* Get name of currently focused window */
char* winName;
XFetchName(display, focusedWindow, &winName);
if (!winName) {
winName = "";
}
/* Replace things in barConfig string with what they should be replaced with */
std::string barString = std::regex_replace(barConfig, std::regex("WMNAME"), nameString);
barString = std::regex_replace(barString, std::regex("CURFOCUS"), winName);
return barString;
}
void loop() {
/* Event loop */
while (true) {
/* Draw bar */
std::string barString = generateBarString();
XClearWindow(display, root);
XSetForeground(display, gc, barBackground);
XFillRectangle(display, root, gc, 0,0, XDefaultScreenOfDisplay(display)->width, 16);
XSetForeground(display, gc, barText);
XDrawString(display, root, gc, 4, 12, barString.c_str(), barString.length());
handleFocus();
/* If there are X events to handle, handle them and dont delay */
if (XPending(display) > 0) {
handleEvent();
} else {
/* No current event, sleep */
usleep(10000);
}
}
}
/*Make an error handler that doesn't do anything,
This is done to stop the program exiting when you
attempt to kill a window when you have no focus*/
int errHandler(Display* disp, XErrorEvent* err) {return 0;}
int main() {
display = XOpenDisplay(NULL);
curScreen = DefaultScreen(display);
root = RootWindow(display, curScreen);
gc = XDefaultGC(display, curScreen);
XSetErrorHandler(&errHandler);
XSelectInput(display, root, SubstructureRedirectMask | SubstructureNotifyMask);
/* Register all keys with X */
for (cKeybind bind : keybinds) {
bind.registerKey();
}
/* Event loop */
loop();
return 0;
}