-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstackwm.cc
More file actions
211 lines (173 loc) · 4.95 KB
/
stackwm.cc
File metadata and controls
211 lines (173 loc) · 4.95 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <X11/Xlib.h>
#include <cassert>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <sstream>
// config.
constexpr const char* terminal = "stterm &";
constexpr const char* browser = "firefox &";
void quit();
void close_window();
void start_terminal();
void start_browser();
void start_dmenu();
void swap_stack_head();
constexpr auto mod_key = Mod4Mask;
static std::map<std::string, void (*)()> keys{
{"q", quit}, {"w", close_window}, {"x", swap_stack_head},
{"c", start_terminal}, {"b", start_browser}, {"p", start_dmenu},
};
// logging.
void log_info(const std::string& msg) { std::cerr << msg << "." << std::endl; }
[[noreturn]] void log_fatal(const std::string& msg) {
std::cerr << "died: " << msg << "." << std::endl;
exit(1);
}
using StreamFuncP = std::unique_ptr<std::ostringstream,
std::function<void(std::ostringstream*)>>;
StreamFuncP log_stream(void (*level)(const std::string&)) {
return StreamFuncP(new std::ostringstream,
[=](std::ostringstream* stream) { level(stream->str()); });
}
#define LOG(level) *(log_stream(log_##level))
// code.
static Display* display;
static Window root_window;
static int screen_width;
static int screen_height;
static std::list<Window> stack;
void push(Window w) { stack.push_front(w); }
void pop(Window w) { stack.remove(w); }
void refocus() {
if (stack.empty()) {
return;
}
Window w = stack.front();
XRaiseWindow(display, w);
XMoveResizeWindow(display, w, 0, 0, screen_width, screen_height);
XSetInputFocus(display, w, RevertToPointerRoot, CurrentTime);
}
// KeyPress handlers.
unsigned int keycode(const std::string& name) {
return XKeysymToKeycode(display, XStringToKeysym(name.c_str()));
}
void quit() { XCloseDisplay(display); }
void start_terminal() { system(terminal); }
void start_browser() { system(browser); }
void start_dmenu() { system("dmenu_run"); }
void close_window() {
if (stack.empty()) {
return;
}
XKillClient(display, stack.front());
}
void swap_stack_head() {
if (stack.size() < 2) {
return;
}
Window a = stack.front();
stack.pop_front();
Window b = stack.front();
stack.pop_front();
stack.push_front(a);
stack.push_front(b);
refocus();
}
// XEvent handlers.
void on_keypress(const XKeyEvent& e) {
for (const auto& key_and_func : keys) {
if (e.keycode == keycode(key_and_func.first)) {
key_and_func.second();
}
}
}
void on_configurerequest(const XConfigureRequestEvent& e) {
XWindowChanges changes;
changes.x = e.x;
changes.y = e.y;
changes.width = e.width;
changes.height = e.height;
changes.border_width = e.border_width;
changes.sibling = e.above;
changes.stack_mode = e.detail;
XConfigureWindow(display, e.window, e.value_mask, &changes);
}
void on_maprequest(const XMapRequestEvent& e) {
XMapWindow(display, e.window);
push(e.window);
refocus();
}
void on_unmapnotify(const XUnmapEvent& e) {
pop(e.window);
refocus();
}
void on_destroynotify(const XDestroyWindowEvent& e) {
pop(e.window);
refocus();
}
void init_keys();
void on_mappingnotify(XMappingEvent& e) {
XRefreshKeyboardMapping(&e);
if (e.request == MappingKeyboard) {
init_keys();
}
}
// init.
int x_error_handler(Display* d, XErrorEvent* e) {
char buf[100];
XGetErrorText(d, e->error_code, buf, sizeof(buf));
LOG(fatal) << "XError: " << buf;
return 0;
}
void init_keys() {
LOG(info) << "grabbing keys";
XUngrabKey(display, AnyKey, AnyModifier, root_window);
for (const auto& key_and_func : keys) {
XGrabKey(display, keycode(key_and_func.first), mod_key,
DefaultRootWindow(display), True, GrabModeAsync, GrabModeAsync);
}
LOG(info) << "grabbed keys";
}
void init_x() {
display = XOpenDisplay(NULL);
if (!display) {
LOG(fatal) << "failed to open the display";
}
root_window = DefaultRootWindow(display);
screen_width = DisplayWidth(display, DefaultScreen(display));
screen_height = DisplayHeight(display, DefaultScreen(display));
XSelectInput(display, root_window,
SubstructureNotifyMask | SubstructureRedirectMask);
XSetErrorHandler(x_error_handler);
init_keys();
XSync(display, False);
LOG(info) << "setup complete";
}
// main event loop.
int main() {
init_x();
XEvent e;
for (;;) {
XSync(display, False);
XNextEvent(display, &e);
#define HANDLE(event, code) \
case event: \
LOG(info) << "starting " << #event; \
code; \
LOG(info) << "finished " << #event; \
break
switch (e.type) {
HANDLE(KeyPress, on_keypress(e.xkey));
HANDLE(ConfigureRequest, on_configurerequest(e.xconfigurerequest));
HANDLE(MapRequest, on_maprequest(e.xmaprequest));
HANDLE(UnmapNotify, on_unmapnotify(e.xunmap));
HANDLE(DestroyNotify, on_destroynotify(e.xdestroywindow));
HANDLE(MappingNotify, on_mappingnotify(e.xmapping));
}
#undef HANDLE
}
return 0;
}