-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow_manager.cpp
More file actions
529 lines (445 loc) · 17.5 KB
/
Copy pathwindow_manager.cpp
File metadata and controls
529 lines (445 loc) · 17.5 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#include "window_manager.hpp"
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
#include <glog/logging.h>
#include <algorithm>
#include "config.hpp"
#include "utils.hpp"
using std::find;
using std::max;
using std::pair;
using std::string;
using std::unique_ptr;
using std::vector;
bool WindowManager::wm_detected_;
unique_ptr<WindowManager> WindowManager::Create() {
Display* display = XOpenDisplay(nullptr);
if (display == nullptr) {
LOG(ERROR) << "Failed to open X display " << XDisplayName(nullptr);
return nullptr;
}
return unique_ptr<WindowManager>(new WindowManager(display));
}
WindowManager::WindowManager(Display* display) : display_(CHECK_NOTNULL(display)),
root_(DefaultRootWindow(display_)),
WM_PROTOCOLS(XInternAtom(display_, "WM_PROTOCOLS", false)),
WM_DELETE_WINDOW(XInternAtom(display_, "WM_DELETE_WINDOW", false)) {
}
WindowManager::~WindowManager() {
XCloseDisplay(display_);
}
void WindowManager::Run() {
// Initialization
wm_detected_ = false;
XSetErrorHandler(&WindowManager::OnWMDetected);
// Alt + Mouse buttons
XGrabButton(display_,
AnyButton,
Mod1Mask,
root_,
True,
ButtonPressMask | ButtonReleaseMask | PointerMotionMask | OwnerGrabButtonMask,
GrabModeAsync,
GrabModeAsync,
None,
None);
// Alt + Tab to switch active window
XGrabKey(display_,
XKeysymToKeycode(display_, XK_Tab),
Mod1Mask,
root_,
False,
GrabModeAsync,
GrabModeAsync);
// Alt + (Shift) + Enter for Terminal
XGrabKey(display_,
XKeysymToKeycode(display_, XK_Return),
Mod1Mask | ShiftMask,
root_,
False,
GrabModeAsync,
GrabModeAsync);
// Alt + Ctrl + Right for next workspace
XGrabKey(display_,
XKeysymToKeycode(display_, XK_Right),
Mod1Mask | ControlMask,
root_,
False,
GrabModeAsync,
GrabModeAsync);
// Alt + Ctrl + Left for previous workspace
XGrabKey(display_,
XKeysymToKeycode(display_, XK_Left),
Mod1Mask | ControlMask,
root_,
False,
GrabModeAsync,
GrabModeAsync);
// Alt + Shift + Ctrl + Right to move active window to next workpace
XGrabKey(display_,
XKeysymToKeycode(display_, XK_Right),
Mod1Mask | ControlMask | ShiftMask,
root_,
False,
GrabModeAsync,
GrabModeAsync);
// Alt + Shift + Ctrl + Left to move active window to previous workpace
XGrabKey(display_,
XKeysymToKeycode(display_, XK_Left),
Mod1Mask | ControlMask | ShiftMask,
root_,
False,
GrabModeAsync,
GrabModeAsync);
XSelectInput(display_, root_, SubstructureNotifyMask | SubstructureRedirectMask);
XSync(display_, false);
if (wm_detected_) {
LOG(ERROR) << "Detected another window manager on display " << XDisplayString(display_);
return;
}
XSetErrorHandler(&WindowManager::OnXError);
// Show mouse cursor
XDefineCursor(display_, root_, XCreateFontCursor(display_, XC_top_left_arrow));
// Create status bar window
status_bar_window_ = XCreateSimpleWindow(
display_,
root_,
0, 0,
DisplayWidth(display_, DefaultScreen(display_)), STATUS_BAR_HEIGHT,
STATUS_BAR_BORDER_WIDTH,
STATUS_BAR_BORDER_COLOR,
STATUS_BAR_BG_COLOR);
XMapWindow(display_, status_bar_window_);
// Create first workspace
workspaces_.push_back(vector<Window>());
// Main event loop
XEvent e;
while (true) {
XNextEvent(display_, &e);
// LOG(INFO) << "Received event: " << XEventCodeToString(e.type);
switch (e.type) {
case CreateNotify:
OnCreateNotify(e.xcreatewindow);
break;
case DestroyNotify:
OnDestroyNotify(e.xdestroywindow);
break;
case ReparentNotify:
OnReparentNotify(e.xreparent);
break;
case ConfigureRequest:
OnConfigureRequest(e.xconfigurerequest);
break;
case ConfigureNotify:
OnConfigureNotify(e.xconfigure);
break;
case MapRequest:
OnMapRequest(e.xmaprequest);
break;
case MapNotify:
OnMapNotify(e.xmap);
break;
case UnmapNotify:
OnUnmapNotify(e.xunmap);
break;
case ButtonPress:
OnButtonPress(e.xbutton);
break;
case ButtonRelease:
OnButtonRelease(e.xbutton);
break;
case MotionNotify:
// Skip any pending motion events
while (XCheckTypedWindowEvent(display_, e.xmotion.subwindow, MotionNotify, &e)) {
}
OnMotionNotify(e.xmotion);
break;
case KeyPress:
OnKeyPress(e.xkey);
break;
case KeyRelease:
OnKeyRelease(e.xkey);
break;
default:
LOG(WARNING) << "Ignored event: " << XEventCodeToString(e.type);
}
}
}
int WindowManager::OnWMDetected(Display* display, XErrorEvent* e) {
CHECK_EQ(static_cast<int>(e->error_code), BadAccess);
wm_detected_ = true;
return 0;
}
int WindowManager::OnXError(Display* display, XErrorEvent* e) {
char error_text[1024] = {0};
XGetErrorText(display, e->error_code, error_text, sizeof(error_text));
LOG(ERROR) << "Received X Error:"
<< "\n " << error_text << " (" << int(e->error_code) << ")"
<< "\n Request: " << XRequestCodeToString(e->request_code) << " (" << int(e->request_code) << ")"
<< "\n Resource ID: " << e->resourceid;
return 0;
}
void WindowManager::SetWindowBorder(const Window& w, unsigned int width, const char* color_str) {
XSetWindowBorderWidth(display_, w, width);
Colormap colormap = DefaultColormap(display_, DefaultScreen(display_));
XColor color;
CHECK(XAllocNamedColor(display_, colormap, color_str, &color, &color));
XSetWindowBorder(display_, w, color.pixel);
}
void WindowManager::FocusWindow(const Window& w) {
// Raise and change border on current window
SetWindowBorder(w, BORDER_WIDTH_ACTIVE, BORDER_COLOR_ACTIVE);
XRaiseWindow(display_, w);
active_window_ = w;
// Change border of all other windows to inactive
for (auto& workspace : workspaces_) {
for (auto& window : workspace) {
if (window == w)
continue;
SetWindowBorder(window, BORDER_WIDTH_INACTIVE, BORDER_COLOR_INACTIVE);
}
}
// Write window title to status bar
XTextProperty xtext;
XGetWMName(display_, w, &xtext);
WriteToStatusBar(string(reinterpret_cast<char*>(xtext.value)));
}
void WindowManager::WriteToStatusBar(const string message) {
// Write active workspace number and message
std::stringstream fmt;
fmt << "[" << active_workspace_ << "] " << message;
XClearWindow(display_, status_bar_window_);
XDrawString(display_,
status_bar_window_,
DefaultGC(display_, DefaultScreen(display_)),
16, 16,
fmt.str().c_str(),
fmt.str().length());
}
void WindowManager::SwitchWorkspace(const int workspace) {
CHECK(workspace >= 0);
CHECK(workspace < num_workspaces_);
// Create new workspace if needed
if (workspace == workspaces_.size()) {
workspaces_.push_back(vector<Window>());
}
// Hide windows of current workspace
for (auto& window : workspaces_[active_workspace_]) {
XWithdrawWindow(display_, window, DefaultScreen(display_));
}
// Show windows of new workspace
for (auto& window : workspaces_[workspace]) {
XMapWindow(display_, window);
}
active_workspace_ = workspace;
WriteToStatusBar("");
}
void WindowManager::CreateWorkspace() {
num_workspaces_++;
SwitchWorkspace(num_workspaces_ - 1);
}
void WindowManager::OnCreateNotify(const XCreateWindowEvent& e) {}
void WindowManager::OnDestroyNotify(const XDestroyWindowEvent& e) {
// Remove window from windows vector and switch active window
for (auto& workspace : workspaces_) {
auto it = find(workspace.begin(), workspace.end(), e.window);
if (it != workspace.end()) {
workspace.erase(it);
}
}
}
void WindowManager::OnReparentNotify(const XReparentEvent& e) {}
void WindowManager::OnConfigureRequest(const XConfigureRequestEvent& e) {
XWindowChanges wc;
wc.x = e.x;
wc.y = e.y;
wc.width = e.width;
wc.height = e.height;
wc.border_width = e.border_width;
wc.sibling = e.above;
wc.stack_mode = e.detail;
XConfigureWindow(display_, e.window, e.value_mask, &wc);
}
void WindowManager::OnConfigureNotify(const XConfigureEvent& e) {}
void WindowManager::OnMapRequest(const XMapRequestEvent& e) {
workspaces_[active_workspace_].push_back(e.window);
XMapWindow(display_, e.window);
XReparentWindow(display_, e.window, root_, 0, 0);
XMoveWindow(display_, e.window, 0, STATUS_BAR_HEIGHT);
FocusWindow(e.window);
LOG(INFO) << "Mapped window " << e.window;
}
void WindowManager::OnMapNotify(const XMapEvent& e) {}
void WindowManager::OnUnmapNotify(const XUnmapEvent& e) {}
void WindowManager::OnButtonPress(const XButtonEvent& e) {
if (e.subwindow == None)
return;
// Save initial cursor position
drag_start_pos_ = {e.x_root, e.y_root};
// Save initial window info
Window returned_root;
int x, y;
unsigned width, height, border_width, depth;
CHECK(XGetGeometry(
display_,
e.subwindow,
&returned_root,
&x,
&y,
&width,
&height,
&border_width,
&depth));
drag_start_frame_pos_ = {x, y};
drag_start_frame_size_ = {width, height};
// Raise window and change border to active
FocusWindow(e.subwindow);
// Alt
if (e.state & Mod1Mask) {
// Alt + Middle button to close window
if (e.button == Button2) {
// Try to gracefully kill client if the client supports the WM_DELETE_WINDOW behavior.
// Otherwise, kill it.
Atom* supported_protocols;
int num_supported_protocols;
if (XGetWMProtocols(display_, e.subwindow, &supported_protocols, &num_supported_protocols) &&
find(supported_protocols, supported_protocols + num_supported_protocols, WM_DELETE_WINDOW) != supported_protocols + num_supported_protocols) {
LOG(INFO) << "Gracefully deleting window " << e.subwindow;
XEvent msg = {0};
msg.xclient.type = ClientMessage;
msg.xclient.message_type = WM_PROTOCOLS;
msg.xclient.window = e.subwindow;
msg.xclient.format = 32;
msg.xclient.data.l[0] = WM_DELETE_WINDOW;
CHECK(XSendEvent(display_, e.subwindow, false, 0, &msg));
} else {
LOG(INFO) << "Killing window " << e.subwindow;
XKillClient(display_, e.subwindow);
}
}
}
}
void WindowManager::OnButtonRelease(const XButtonEvent& e) {}
void WindowManager::OnMotionNotify(const XMotionEvent& e) {
if (e.subwindow == None)
return;
const pair<int, int> drag_pos = {e.x_root, e.y_root};
const pair<int, int> delta = {drag_pos.first - drag_start_pos_.first,
drag_pos.second - drag_start_pos_.second};
// Alt
if (e.state & Mod1Mask) {
// Alt + Left button to move
if (e.state & Button1Mask) {
const pair<int, int> dest_frame_pos = {drag_start_frame_pos_.first + delta.first,
drag_start_frame_pos_.second + delta.second};
// Don't move window above status bar
if (dest_frame_pos.second < STATUS_BAR_HEIGHT)
return;
XMoveWindow(display_, e.subwindow, dest_frame_pos.first, dest_frame_pos.second);
}
// Alt + Right button to reisze
if (e.state & Button3Mask) {
const pair<int, int> size_delta = {max(delta.first, -drag_start_frame_size_.first),
max(delta.second, -drag_start_frame_size_.second)};
pair<int, int> dest_frame_size = {drag_start_frame_size_.first + size_delta.first,
drag_start_frame_size_.second + size_delta.second};
// Restrict minimum window size
dest_frame_size.first = max(dest_frame_size.first, MIN_WINDOW_WIDTH);
dest_frame_size.second = max(dest_frame_size.second, MIN_WINDOW_HEIGHT);
// Resize window
XResizeWindow(display_, e.subwindow, dest_frame_size.first, dest_frame_size.second);
}
}
}
void WindowManager::OnKeyPress(const XKeyEvent& e) {
// Alt
if (e.state & Mod1Mask) {
// Alt + Tab to switch active window to next one
if (e.keycode == XKeysymToKeycode(display_, XK_Tab)) {
// Don't switch if there are no windows or no active window
if (workspaces_.size() == 0 || active_window_ == 0)
return;
// Find next window in workspace
auto it = find(workspaces_[active_workspace_].begin(), workspaces_[active_workspace_].end(), active_window_);
if (it == workspaces_[active_workspace_].end()) {
it = workspaces_[active_workspace_].begin();
} else {
it++;
if (it == workspaces_[active_workspace_].end())
it = workspaces_[active_workspace_].begin();
}
FocusWindow(*it);
return;
}
// Alt + Shift
if (e.state & ShiftMask) {
// Alt + Shift + Enter to open terminal
if (e.keycode == XKeysymToKeycode(display_, XK_Return)) {
if (fork() == 0) {
char* argument_list[] = {"xterm", NULL};
execvp("xterm", argument_list);
exit(EXIT_SUCCESS);
}
return;
}
// Alt + Shift + Ctrl
if (e.state & ControlMask) {
// Alt + Shift + Ctrl + Right to move active window to next workspace
if (e.keycode == XKeysymToKeycode(display_, XK_Right)) {
if (active_window_ == 0)
return;
// Check if there is a next workspace
if (active_workspace_ == num_workspaces_ - 1)
return;
// Hide window
XWithdrawWindow(display_, active_window_, DefaultScreen(display_));
// Move window to next workspace
workspaces_[active_workspace_].erase(find(workspaces_[active_workspace_].begin(), workspaces_[active_workspace_].end(), active_window_));
workspaces_[active_workspace_ + 1].push_back(active_window_);
active_window_ = 0;
WriteToStatusBar("");
return;
}
// Alt + Shift + Ctrl + Left to move active window to previous workspace
if (e.keycode == XKeysymToKeycode(display_, XK_Left)) {
if (active_window_ == 0)
return;
// Check if there is a previous workspace
if (active_workspace_ == 0)
return;
// Hide window
XWithdrawWindow(display_, active_window_, DefaultScreen(display_));
// Move window to previous workspace
workspaces_[active_workspace_].erase(find(workspaces_[active_workspace_].begin(), workspaces_[active_workspace_].end(), active_window_));
workspaces_[active_workspace_ - 1].push_back(active_window_);
active_window_ = 0;
WriteToStatusBar("");
return;
}
}
}
// Alt + Ctrl
if (e.state & ControlMask) {
// Alt + Ctrl + Right to switch to next workspace
if (e.keycode == XKeysymToKeycode(display_, XK_Right)) {
// Switch to next workspace
if (active_workspace_ < num_workspaces_ - 1) {
SwitchWorkspace(active_workspace_ + 1);
} else {
// Create new workspace if on the last one
CreateWorkspace();
}
return;
}
// Alt + Ctrl + Left to switch to previous workspace
if (e.keycode == XKeysymToKeycode(display_, XK_Left)) {
// Switch to previous workspace if we are not on the first one
if (active_workspace_ == 0)
return;
SwitchWorkspace(active_workspace_ - 1);
return;
}
}
}
}
void WindowManager::OnKeyRelease(const XKeyEvent& e) {}