-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathshod.c
437 lines (385 loc) · 10.5 KB
/
shod.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
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
#include <sys/wait.h>
#include <err.h>
#include <limits.h>
#include <locale.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <X11/extensions/Xrandr.h>
#include "shod.h"
#define DOCK_TITLE "shod's dock"
#define WMNAME "shod"
static int (*xerrorxlib)(Display *, XErrorEvent *);
/* shared variables */
unsigned long clientmask = CWEventMask | CWColormap | CWBackPixel | CWBorderPixel;
XSetWindowAttributes clientswa = {
.event_mask = StructureNotifyMask | SubstructureRedirectMask
| ButtonReleaseMask | ButtonPressMask
| FocusChangeMask | Button1MotionMask
};
struct WM wm = { .running = 1 };
struct Dock dock;
/* show usage and exit */
static void
usage(void)
{
(void)fprintf(stderr, "usage: shod [-AcdhSstW] [file]\n");
exit(1);
}
/* call fork checking for error; exit on error */
static pid_t
efork(void)
{
pid_t pid;
if ((pid = fork()) < 0)
err(1, "fork");
return pid;
}
/* call execlp checking for error; exit on error */
static void
execshell(char *filename)
{
char *argv[3];
if ((argv[0] = getenv("SHELL")) == NULL)
argv[0] = "sh";
if (filename[0] == '-' && filename[1] == '\0')
argv[1] = NULL; /* read commands from stdin */
else
argv[1] = filename; /* read commands from file */
argv[2] = NULL;
if (execvp(argv[0], argv) == -1) {
err(1, "%s", argv[0]);
}
}
/* error handler */
static int
xerror(Display *dpy, XErrorEvent *e)
{
/* stolen from berry, which stole from katriawm, which stole from dwm lol */
/* There's no way to check accesses to destroyed windows, thus those
* cases are ignored (especially on UnmapNotify's). Other types of
* errors call Xlibs default error handler, which may call exit. */
if (e->error_code == BadWindow ||
(e->request_code == X_SetInputFocus && e->error_code == BadMatch) ||
(e->request_code == X_PolyText8 && e->error_code == BadDrawable) ||
(e->request_code == X_PolyFillRectangle && e->error_code == BadDrawable) ||
(e->request_code == X_PolySegment && e->error_code == BadDrawable) ||
(e->request_code == X_ConfigureWindow && e->error_code == BadMatch) ||
(e->request_code == X_ConfigureWindow && e->error_code == BadValue) ||
(e->request_code == X_GrabButton && e->error_code == BadAccess) ||
(e->request_code == X_GrabKey && e->error_code == BadAccess) ||
(e->request_code == X_CopyArea && e->error_code == BadDrawable) ||
(e->request_code == 139 && e->error_code == BadDrawable) ||
(e->request_code == 139 && e->error_code == 143))
return 0;
fprintf(stderr, "shod: ");
return xerrorxlib(dpy, e);
exit(1); /* unreached */
}
/* startup error handler to check if another window manager is already running */
static int
xerrorstart(Display *dpy, XErrorEvent *e)
{
(void)dpy;
(void)e;
errx(1, "another window manager is already running");
}
/* read command-line options */
static char *
getoptions(int argc, char *argv[])
{
int c;
while ((c = getopt(argc, argv, "AcdhSstW")) != -1) {
switch (c) {
case 'A' :
config.altkeysym = XK_Alt_L;
config.modifier = Mod1Mask;
break;
case 'c':
config.honorconfig = 1;
break;
case 'd':
config.floatdialog = 1;
break;
case 'h':
config.disablehidden = 1;
break;
case 'S':
config.sloppytiles = 1;
break;
case 's':
config.sloppyfocus = 1;
break;
case 't':
config.disablealttab = 1;
break;
case 'W' :
config.altkeysym = XK_Super_L;
config.modifier = Mod4Mask;
break;
default:
usage();
break;
}
}
argc -= optind;
argv += optind;
if (argc > 1)
usage();
return *argv;
}
/* initialize signals */
static void
initsignal(void)
{
struct sigaction sa;
/* remove zombies, we may inherit children when exec'ing shod in .xinitrc */
sa.sa_handler = SIG_IGN;
sa.sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT | SA_RESTART;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGCHLD, &sa, NULL) == -1)
err(1, "sigaction");
while (waitpid(-1, NULL, WNOHANG) > 0)
;
}
/* initialize cursors */
static void
initcursors(void)
{
wm.cursors[CURSOR_NORMAL] = XCreateFontCursor(dpy, XC_left_ptr);
wm.cursors[CURSOR_MOVE] = XCreateFontCursor(dpy, XC_fleur);
wm.cursors[CURSOR_NW] = XCreateFontCursor(dpy, XC_top_left_corner);
wm.cursors[CURSOR_NE] = XCreateFontCursor(dpy, XC_top_right_corner);
wm.cursors[CURSOR_SW] = XCreateFontCursor(dpy, XC_bottom_left_corner);
wm.cursors[CURSOR_SE] = XCreateFontCursor(dpy, XC_bottom_right_corner);
wm.cursors[CURSOR_N] = XCreateFontCursor(dpy, XC_top_side);
wm.cursors[CURSOR_S] = XCreateFontCursor(dpy, XC_bottom_side);
wm.cursors[CURSOR_W] = XCreateFontCursor(dpy, XC_left_side);
wm.cursors[CURSOR_E] = XCreateFontCursor(dpy, XC_right_side);
wm.cursors[CURSOR_V] = XCreateFontCursor(dpy, XC_sb_v_double_arrow);
wm.cursors[CURSOR_H] = XCreateFontCursor(dpy, XC_sb_h_double_arrow);
wm.cursors[CURSOR_HAND] = XCreateFontCursor(dpy, XC_hand2);
wm.cursors[CURSOR_PIRATE] = XCreateFontCursor(dpy, XC_pirate);
}
/* create dock window */
static void
initdock(void)
{
XSetWindowAttributes swa;
TAILQ_INIT(&dock.dappq);
dock.pix = None;
swa.event_mask = SubstructureNotifyMask | SubstructureRedirectMask;
swa.background_pixel = BlackPixel(dpy, screen);
swa.border_pixel = BlackPixel(dpy, screen);
swa.colormap = colormap;
dock.obj.win = XCreateWindow(
dpy, root,
0, 0, 1, 1, 0,
depth, InputOutput, visual,
clientmask, &swa
);
dock.state = MAXIMIZED;
dock.obj.class = dock_class;
settitle(dock.obj.win, DOCK_TITLE);
}
/* create dummy windows used for controlling focus and the layer of clients */
static void
initdummywindows(void)
{
int i;
XSetWindowAttributes swa;
for (i = 0; i < LAYER_LAST; i++) {
wm.layers[i].ncols = 0;
wm.layers[i].frame = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
XRaiseWindow(dpy, wm.layers[i].frame);
TAILQ_INSERT_HEAD(&wm.stackq, &wm.layers[i], raiseentry);
}
swa = clientswa;
swa.event_mask |= KeyPressMask;
wm.checkwin = wm.focuswin = wm.dragwin = wm.restackwin = XCreateWindow(
dpy, root,
- (2 * config.borderwidth + config.titlewidth),
- (2 * config.borderwidth + config.titlewidth),
2 * config.borderwidth + config.titlewidth,
2 * config.borderwidth + config.titlewidth,
0, depth, CopyFromParent, visual,
clientmask,
&swa
);
}
/* map and hide dummy windows */
static void
mapdummywins(void)
{
XMapWindow(dpy, wm.focuswin);
XSetInputFocus(dpy, wm.focuswin, RevertToParent, CurrentTime);
}
/* run stdin on sh */
static void
autostart(char *filename)
{
pid_t pid;
if (filename == NULL)
return;
if ((pid = efork()) == 0) {
if (efork() == 0)
execshell(filename);
exit(0);
}
waitpid(pid, NULL, 0);
}
static void
checkotherwm(void)
{
/*
* NOTE: Do we need to select SubstructureNotifyMask on the root window?
*
* We will always select StructureNotifyMask on the client windows we
* are requested to map, so selecting StructureNotifyMask on the root
* window seems redundant.
*
* I have removed this mask in the bitmask passed as the third parameter
* to XSelectInput(3) down here. If anything ever brake, just add it
* back.
*/
xerrorxlib = XSetErrorHandler(xerrorstart);
XSelectInput(
dpy, root,
SubstructureRedirectMask | /* so clients request us to map */
StructureNotifyMask | /* get changes on root configuration */
PropertyChangeMask | /* get changes on root properties */
ButtonPressMask /* to change monitor when clicking */
);
XSync(dpy, False);
(void)XSetErrorHandler(xerror);
XSync(dpy, False);
}
/* destroy dummy windows */
static void
cleandummywindows(void)
{
int i;
XDestroyWindow(dpy, wm.checkwin);
for (i = 0; i < LAYER_LAST; i++) {
XDestroyWindow(dpy, wm.layers[i].frame);
}
}
/* free cursors */
static void
cleancursors(void)
{
size_t i;
for (i = 0; i < CURSOR_LAST; i++) {
XFreeCursor(dpy, wm.cursors[i]);
}
}
/* clean window manager structures */
static void
cleanwm(void)
{
struct Monitor *mon;
struct Object *obj;
struct Container *c;
while ((c = TAILQ_FIRST(&wm.focusq)) != NULL)
containerdel(c);
while ((obj = TAILQ_FIRST(&wm.notifq)) != NULL)
(*obj->class->unmanage)(obj);
while ((obj = TAILQ_FIRST(&wm.barq)) != NULL)
(*obj->class->unmanage)(obj);
while ((obj = TAILQ_FIRST(&wm.splashq)) != NULL)
(*obj->class->unmanage)(obj);
while ((obj = TAILQ_FIRST(&dock.dappq)) != NULL)
(*obj->class->unmanage)(obj);
while ((mon = TAILQ_FIRST(&wm.monq)) != NULL)
mondel(mon);
if (dock.pix != None)
XFreePixmap(dpy, dock.pix);
XDestroyWindow(dpy, dock.obj.win);
}
/* shod window manager */
int
main(int argc, char *argv[])
{
XEvent ev;
char *filename, *wmname;
if (!setlocale(LC_ALL, "") || !XSupportsLocale())
warnx("warning: no locale support");
xinit();
checkotherwm();
moninit();
initdepth();
clientswa.colormap = colormap;
clientswa.border_pixel = BlackPixel(dpy, screen);
clientswa.background_pixel = BlackPixel(dpy, screen);
/* get configuration */
if ((wmname = strrchr(argv[0], '/')) != NULL)
wmname++;
else if (argv[0] != NULL && argv[0][0] != '\0')
wmname = argv[0];
else
wmname = WMNAME;
filename = getoptions(argc, argv);
/* check sloppy focus */
if (config.sloppyfocus || config.sloppytiles)
clientswa.event_mask |= EnterWindowMask;
/* initialize queues */
TAILQ_INIT(&wm.monq);
TAILQ_INIT(&wm.barq);
TAILQ_INIT(&wm.splashq);
TAILQ_INIT(&wm.notifq);
TAILQ_INIT(&wm.focusq);
TAILQ_INIT(&wm.stackq);
/* initialize */
initsignal();
initcursors();
initatoms();
initdummywindows();
initdock();
inittheme();
/* set up list of monitors */
monupdate();
wm.selmon = TAILQ_FIRST(&wm.monq);
/* initialize ewmh hints */
ewmhinit(wmname);
ewmhsetcurrentdesktop(0);
ewmhsetshowingdesktop(0);
ewmhsetclients();
ewmhsetactivewindow(None);
/* run sh script */
autostart(filename);
/* scan windows */
scan();
mapdummywins();
/* change default cursor */
XDefineCursor(dpy, root, wm.cursors[CURSOR_NORMAL]);
/* Set focus to root window */
XSetInputFocus(dpy, root, RevertToParent, CurrentTime);
/* set modifier key and grab alt key */
setmod();
/* run main event loop */
while (wm.running) {
(void)XNextEvent(dpy, &ev);
if (ev.type < LASTEvent && xevents[ev.type] != NULL)
(*xevents[ev.type])(&ev);
else if (wm.xrandr && ev.type == wm.xrandrev)
monevent(&ev);
if (wm.setclientlist)
ewmhsetclients();
wm.setclientlist = False;
}
/* clean up */
cleandummywindows();
cleancursors();
cleanwm();
cleantheme();
/* clear ewmh hints */
ewmhsetclients();
ewmhsetactivewindow(None);
/* close connection to server */
XUngrabPointer(dpy, CurrentTime);
XUngrabKeyboard(dpy, CurrentTime);
XCloseDisplay(dpy);
return 0;
}