-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwm.c
404 lines (370 loc) · 11.2 KB
/
wm.c
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
#include "wm.h"
#include "server.h"
#include <X11/X.h>
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <unistd.h>
/* Global structures */
Display *dpy;
Window root;
Atom WM_PROTOCOLS, WM_DELETE_WINDOW;
typedef struct {
Window *windows; // ordered windows, stored TOS+n...TOS
unsigned int num_windows;
} WinStack;
#define window_at(stack, n) (stack).windows[(stack).num_windows - (n)-1]
struct {
WinStack *win_stacks;
unsigned int num_win_stacks;
} stack_stack = {
.win_stacks = NULL, // ordered stacks, stored TOS+n...TOS
.num_win_stacks = 0,
};
#define win_stack_at(n) \
stack_stack.win_stacks[stack_stack.num_win_stacks - (n)-1]
Splits split_stack = {
.splits = NULL, // ordered splits, stored TOS...TOS+n
.num_splits = 0,
};
#define split_at(n) split_stack.splits[n]
unsigned int gap = 0;
/* Draw stack on given split */
void draw_stack(WinStack stack, Split split) {
for (unsigned int w = 0; w < stack.num_windows; w++) {
Window win = window_at(stack, w);
XMapWindow(dpy, win);
if (split.width > split.height) {
unsigned int width = split.width / stack.num_windows;
XMoveResizeWindow(dpy, win, split.x + w * width + gap, split.y + gap,
width - gap * 2, split.height - gap * 2);
} else {
unsigned int height = split.height / stack.num_windows;
XMoveResizeWindow(dpy, win, split.x + gap, split.y + w * height + gap,
split.width - gap * 2, height - gap * 2);
}
}
// make sure TOS window remains the focus
WinStack tos = win_stack_at(0);
if (tos.num_windows) {
Window win = window_at(tos, 0);
XRaiseWindow(dpy, win);
XSetInputFocus(dpy, win, RevertToPointerRoot, CurrentTime);
} else {
XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
}
}
/* Hide a stack */
void hide_stack(WinStack stack) {
for (unsigned int w = 0; w < stack.num_windows; w++) {
XUnmapWindow(dpy, stack.windows[w]);
}
}
/* Redraw all windows */
void draw_all() {
unsigned int s = 0;
// draw all visible
for (; s < stack_stack.num_win_stacks && s < split_stack.num_splits; s++) {
draw_stack(win_stack_at(s), split_at(s));
}
// hide all not visible
for (; s < stack_stack.num_win_stacks; s++) {
hide_stack(win_stack_at(s));
}
}
/* Return if a window exists, returning the location (NULL for split if it
* isn't visible)
*/
Bool find_window(Window win, WinStack **win_stack, Split **split) {
for (unsigned int s = 0; s < stack_stack.num_win_stacks; s++) {
*win_stack = &win_stack_at(s);
*split = s < split_stack.num_splits ? &split_at(s) : NULL;
for (unsigned int w = 0; w < (*win_stack)->num_windows; w++) {
if ((*win_stack)->windows[w] == win) {
return True;
}
}
}
return False;
}
void remove_window(Window win) {
for (unsigned int s = 0; s < stack_stack.num_win_stacks; s++) {
WinStack *win_stack = &win_stack_at(s);
for (unsigned int w = 0; w < win_stack->num_windows; w++) {
if (win_stack->windows[w] == win) {
Window *old = win_stack->windows;
win_stack->num_windows--;
win_stack->windows = malloc(win_stack->num_windows * sizeof(Window));
memcpy(win_stack->windows, old, w * sizeof(Window));
memcpy(win_stack->windows + w, old + w + 1,
(win_stack->num_windows - w) * sizeof(Window));
free(old);
}
}
}
draw_all();
}
/* X error handler */
int x_error(Display *dpy, XErrorEvent *err) {
const unsigned int error_msg_size = 1024;
char error_msg[error_msg_size];
error_msg[error_msg_size - 1] = '\0';
XGetErrorText(dpy, err->error_code, error_msg, error_msg_size - 1);
fprintf(stderr, "soswm: X error: %s\n", error_msg);
return 0;
}
/* Continuously handle X events */
void x_handler() {
while (XPending(dpy)) {
// check for new X events
XEvent e;
XNextEvent(dpy, &e);
switch (e.type) {
case ConfigureRequest: {
XConfigureRequestEvent req = e.xconfigurerequest;
XWindowChanges changes = {
.x = req.x,
.y = req.y,
.width = req.width,
.height = req.height,
.border_width = 0,
.sibling = req.above,
.stack_mode = req.detail,
};
XConfigureWindow(dpy, req.window, req.value_mask, &changes);
break;
}
case MapRequest: {
Window win = e.xmaprequest.window;
XMapWindow(dpy, win);
// if the window exists, redraw it; otherwise, add it
WinStack *win_stack;
Split *split;
if (find_window(win, &win_stack, &split)) {
if (split) {
draw_stack(*win_stack, *split);
}
} else {
if (!stack_stack.num_win_stacks) {
stack_stack.num_win_stacks = 1;
stack_stack.win_stacks = malloc(sizeof(WinStack));
stack_stack.win_stacks[0] = (WinStack){
.windows = NULL,
.num_windows = 0,
};
}
win_stack = &win_stack_at(0);
win_stack->num_windows++;
win_stack->windows = realloc(win_stack->windows,
win_stack->num_windows * sizeof(Window));
window_at(*win_stack, 0) = win;
draw_stack(*win_stack, split_at(0));
}
break;
}
case UnmapNotify: {
Window win = e.xunmap.window;
// delete the window if it should currently be visible
WinStack *win_stack;
Split *split;
if (find_window(win, &win_stack, &split) && split) {
remove_window(win);
}
break;
}
case DestroyNotify: {
remove_window(e.xdestroywindow.window);
break;
}
}
}
}
int main() {
// initialize display
if (!(dpy = XOpenDisplay(0))) {
fprintf(stderr, "soswm: Could not open display\n");
exit(1);
}
root = XDefaultRootWindow(dpy);
XSetErrorHandler(x_error);
XSelectInput(dpy, root, SubstructureNotifyMask | SubstructureRedirectMask);
// set default split
Screen *scr = XDefaultScreenOfDisplay(dpy);
split_stack = (Splits){
.splits = malloc(sizeof(Split)),
.num_splits = 1,
};
split_at(0) = (Split){
.width = XWidthOfScreen(scr),
.height = XHeightOfScreen(scr),
.x = 0,
.y = 0,
};
// initialize communication protocols
WM_PROTOCOLS = XInternAtom(dpy, "WM_PROTOCOLS", False);
WM_DELETE_WINDOW = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
// initialize and run server
server_init();
// run startup program
if (!fork()) {
const unsigned int startup_path_len = 64;
char startup_path[startup_path_len];
snprintf(startup_path, startup_path_len, "%s/.config/soswm/soswmrc",
getenv("HOME"));
execl(startup_path, "soswmrc", NULL);
exit(1);
}
// continuously accept from either X or server
fd_set set;
int x_fd = ConnectionNumber(dpy);
for (;;) {
FD_ZERO(&set);
FD_SET(x_fd, &set);
FD_SET(connection_socket, &set);
int max_fd = x_fd > connection_socket ? x_fd : connection_socket;
// wait for any change, always checking X after
select(max_fd + 1, &set, NULL, NULL, NULL);
if (FD_ISSET(connection_socket, &set)) {
server_handler();
}
x_handler();
}
return 0;
}
/* Interface functions */
void push_stack() {
stack_stack.num_win_stacks++;
stack_stack.win_stacks = realloc(
stack_stack.win_stacks, stack_stack.num_win_stacks * sizeof(WinStack));
win_stack_at(0) = (WinStack){
.windows = NULL,
.num_windows = 0,
};
draw_all();
}
void pop_window() {
if (stack_stack.num_win_stacks) {
WinStack tos = win_stack_at(0);
if (tos.num_windows) {
Window win = window_at(tos, 0);
int n;
Atom *protos;
// first, try to tell the window to close
if (XGetWMProtocols(dpy, win, &protos, &n)) {
while (n--) {
if (protos[n] == WM_DELETE_WINDOW) {
const int LONG_SIZE = 32;
XEvent e;
e.xclient = (XClientMessageEvent){
.type = ClientMessage,
.window = win,
.message_type = WM_PROTOCOLS,
.format = LONG_SIZE,
.data = {.l[0] = WM_DELETE_WINDOW}};
XSendEvent(dpy, win, False, NoEventMask, &e);
XFree(protos);
return;
}
}
XFree(protos);
}
// if the client has no deletion protocol, forcefully kill it
XKillClient(dpy, win);
}
}
}
void pop_stack() {
if (stack_stack.num_win_stacks && !win_stack_at(0).num_windows) {
free(win_stack_at(0).windows);
stack_stack.num_win_stacks--;
draw_all();
}
}
void roll_window(RollDirection dir) {
if (stack_stack.num_win_stacks) {
WinStack *tos = &win_stack_at(0);
if (tos->num_windows > 1) {
Window top = window_at(*tos, 0);
Window bottom = window_at(*tos, tos->num_windows - 1);
Window *old = tos->windows;
tos->windows = malloc(tos->num_windows * sizeof(Window));
if (dir == ROLL_TOP) {
memcpy(tos->windows + 1, old, (tos->num_windows - 1) * sizeof(Window));
window_at(*tos, tos->num_windows - 1) = top;
} else {
memcpy(tos->windows, old + 1, (tos->num_windows - 1) * sizeof(Window));
window_at(*tos, 0) = bottom;
}
free(old);
draw_all();
}
}
}
void roll_stack(RollDirection dir) {
if (stack_stack.num_win_stacks > 1) {
WinStack top = win_stack_at(0);
WinStack bottom = win_stack_at(stack_stack.num_win_stacks - 1);
WinStack *old = stack_stack.win_stacks;
stack_stack.win_stacks =
malloc(stack_stack.num_win_stacks * sizeof(WinStack));
if (dir == ROLL_TOP) {
memcpy(stack_stack.win_stacks + 1, old,
(stack_stack.num_win_stacks - 1) * sizeof(WinStack));
win_stack_at(stack_stack.num_win_stacks - 1) = top;
} else {
memcpy(stack_stack.win_stacks, old + 1,
(stack_stack.num_win_stacks - 1) * sizeof(WinStack));
win_stack_at(0) = bottom;
}
free(old);
draw_all();
}
}
void move_window(unsigned int n) {
if (stack_stack.num_win_stacks > 1 && win_stack_at(0).num_windows && n &&
n < stack_stack.num_win_stacks) {
WinStack *from = &win_stack_at(0);
WinStack *to = &win_stack_at(n);
Window win = window_at(*from, 0);
from->num_windows--;
to->num_windows++;
to->windows = realloc(to->windows, to->num_windows * sizeof(Window));
window_at(*to, 0) = win;
draw_all();
}
}
void swap_window(unsigned int n) {
if (stack_stack.num_win_stacks) {
WinStack win_stack = win_stack_at(0);
if (n > 0 && n < win_stack.num_windows) {
Window tos = window_at(win_stack, 0);
window_at(win_stack, 0) = window_at(win_stack, n);
window_at(win_stack, n) = tos;
draw_stack(win_stack, split_at(0));
}
}
}
void swap_stack(unsigned int n) {
if (n > 0 && n < stack_stack.num_win_stacks) {
WinStack tos = win_stack_at(0);
win_stack_at(0) = win_stack_at(n);
win_stack_at(n) = tos;
draw_all();
}
}
void set_gap(unsigned int n) {
gap = n;
draw_all();
}
void split_screen(Splits updated_split_stack) {
free(split_stack.splits);
split_stack = updated_split_stack;
draw_all();
}
void logout_wm() {
XCloseDisplay(dpy);
server_quit();
exit(0);
}