dwm
dynamic window manager
git clone https://9o.is/git/dwm.git
dwm.c
(60256B)
1 /* See LICENSE file for copyright and license details.
2 *
3 * dynamic window manager is designed like any other X client as well. It is
4 * driven through handling X events. In contrast to other X clients, a window
5 * manager selects for SubstructureRedirectMask on the root window, to receive
6 * events about window (dis-)appearance. Only one X connection at a time is
7 * allowed to select for this event mask.
8 *
9 * The event handlers of dwm are organized in an array which is accessed
10 * whenever a new event has been fetched. This allows event dispatching
11 * in O(1) time.
12 *
13 * Each child of the root window is called a client, except windows which have
14 * set the override_redirect flag. Clients are organized in a linked client
15 * list on each monitor, the focus history is remembered through a stack list
16 * on each monitor. Each client contains a bit array to indicate the tags of a
17 * client.
18 *
19 * Keys and tagging rules are organized as arrays and defined in config.h.
20 *
21 * To understand everything else, start reading main().
22 */
23 #include <errno.h>
24 #include <locale.h>
25 #include <signal.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <X11/cursorfont.h>
34 #include <X11/keysym.h>
35 #include <X11/Xatom.h>
36 #include <X11/Xlib.h>
37 #include <X11/Xproto.h>
38 #include <X11/Xutil.h>
39 #include <X11/XF86keysym.h>
40 #ifdef XINERAMA
41 #include <X11/extensions/Xinerama.h>
42 #endif /* XINERAMA */
43 #include <X11/Xft/Xft.h>
44
45 #include "drw.h"
46 #include "util.h"
47
48 /* macros */
49 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
50 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
51 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
52 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
53 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
54 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
55 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
56 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
57 #define TAGMASK ((1 << LENGTH(tags)) - 1)
58 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
59 #define TEXTWNP(X) (drw_fontset_getwidth(drw, (X)) + 2)
60
61 /* enums */
62 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
63 enum { SchemeNorm, SchemeSel }; /* color schemes */
64 enum { NetSupported, NetWMName, NetWMState, NetWMCheck,
65 NetWMFullscreen, NetActiveWindow, NetWMWindowType,
66 NetWMWindowTypeDialog, NetClientList, NetClientInfo, NetLast }; /* EWMH atoms */
67 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
68 enum { QubesVMName, QubesLast }; /* Qubes atoms */
69 enum { MyStatus, MyLast }; /* My atoms */
70 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle, ClkCounter,
71 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
72
73 typedef union {
74 int i;
75 unsigned int ui;
76 float f;
77 const void *v;
78 } Arg;
79
80 typedef struct {
81 unsigned int click;
82 unsigned int mask;
83 unsigned int button;
84 void (*func)(const Arg *arg);
85 const Arg arg;
86 } Button;
87
88 typedef struct Monitor Monitor;
89 typedef struct Client Client;
90 struct Client {
91 char vmname[256];
92 char name[256];
93 char mystatus[256];
94 float mina, maxa;
95 int x, y, w, h;
96 int oldx, oldy, oldw, oldh;
97 int basew, baseh, incw, inch, maxw, maxh, minw, minh, hintsvalid;
98 int bw, oldbw;
99 unsigned int tags;
100 int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
101 Client *next;
102 Client *snext;
103 Monitor *mon;
104 Window win;
105 };
106
107 typedef struct {
108 unsigned int mod;
109 KeySym keysym;
110 void (*func)(const Arg *);
111 const Arg arg;
112 } Key;
113
114 typedef struct {
115 const char *symbol;
116 void (*arrange)(Monitor *);
117 } Layout;
118
119 struct Monitor {
120 char ltsymbol[16];
121 float mfact;
122 int nmaster;
123 int num;
124 int by; /* bar geometry */
125 int mx, my, mw, mh; /* screen size */
126 int wx, wy, ww, wh; /* window area */
127 unsigned int seltags;
128 unsigned int sellt;
129 unsigned int tagset[2];
130 int showbar;
131 int topbar;
132 Client *clients;
133 Client *sel;
134 Client *stack;
135 Client *fg;
136 Monitor *next;
137 Window barwin;
138 const Layout *lt[2];
139 char ccounter[8];
140 };
141
142 typedef struct {
143 const char *class;
144 const char *instance;
145 const char *title;
146 unsigned int tags;
147 int isfloating;
148 int monitor;
149 } Rule;
150
151 /* function declarations */
152 static void applyrules(Client *c);
153 static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int *bw, int interact);
154 static void arrange(Monitor *m);
155 static void arrangeforegrounded(Monitor *m);
156 static void arrangemon(Monitor *m);
157 static void attach(Client *c);
158 static void attachstack(Client *c);
159 static void buttonpress(XEvent *e);
160 static void checkotherwm(void);
161 static void cleanup(void);
162 static void cleanupmon(Monitor *mon);
163 static void clientmessage(XEvent *e);
164 static void configure(Client *c);
165 static void configurenotify(XEvent *e);
166 static void configurerequest(XEvent *e);
167 static Monitor *createmon(void);
168 static void deck(Monitor *m);
169 static void destroynotify(XEvent *e);
170 static void detach(Client *c);
171 static void detachstack(Client *c);
172 //static Monitor *dirtomon(int dir);
173 static void drawbar(Monitor *m);
174 static void drawbars(void);
175 static void enternotify(XEvent *e);
176 static void expose(XEvent *e);
177 static void fgtoggle(const Arg *arg);
178 static void focus(Client *c);
179 static void focusin(XEvent *e);
180 //static void focusmon(const Arg *arg);
181 static void focusstack(const Arg *arg);
182 static void focusstacknum(const Arg *arg);
183 static Atom getatomprop(Client *c, Atom prop);
184 static int getrootptr(int *x, int *y);
185 static long getstate(Window w);
186 static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
187 static void grabbuttons(Client *c, int focused);
188 static void grabkeys(void);
189 static void incnmaster(const Arg *arg);
190 static void keypress(XEvent *e);
191 static void killclient(const Arg *arg);
192 static void manage(Window w, XWindowAttributes *wa);
193 static void mappingnotify(XEvent *e);
194 static void maprequest(XEvent *e);
195 static void monocle(Monitor *m);
196 static void motionnotify(XEvent *e);
197 static void movemouse(const Arg *arg);
198 static Client *nexttiled(Client *c);
199 static void pop(Client *c);
200 static void propertynotify(XEvent *e);
201 static void quit(const Arg *arg);
202 static Monitor *recttomon(int x, int y, int w, int h);
203 static void resize(Client *c, int x, int y, int w, int h, int bw, int interact);
204 static void resizeclient(Client *c, int x, int y, int w, int h, int bw);
205 static void resizemouse(const Arg *arg);
206 static void restack(Monitor *m);
207 static void run(void);
208 static void scan(void);
209 static int sendevent(Client *c, Atom proto);
210 static void sendmon(Client *c, Monitor *m);
211 static void setclientstate(Client *c, long state);
212 static void setclienttagprop(Client *c);
213 static void setfocus(Client *c);
214 static void setfullscreen(Client *c, int fullscreen);
215 static void setlayout(const Arg *arg);
216 static void setmfact(const Arg *arg);
217 static void setup(void);
218 static void seturgent(Client *c, int urg);
219 static void showhide(Client *c);
220 static void sighup(int unused);
221 static void sigterm(int unused);
222 static void spawn(const Arg *arg);
223 static void tag(const Arg *arg);
224 //static void tagmon(const Arg *arg);
225 static void tile(Monitor *m);
226 static void togglebar(const Arg *arg);
227 static void togglefloating(const Arg *arg);
228 static void toggletag(const Arg *arg);
229 static void toggleview(const Arg *arg);
230 static void unfocus(Client *c, int setfocus);
231 static void unmanage(Client *c, int destroyed);
232 static void unmapnotify(XEvent *e);
233 static void updatebarpos(Monitor *m);
234 static void updatebars(void);
235 static void updateclientlist(void);
236 static int updategeom(void);
237 static void updatenumlockmask(void);
238 static void updatesizehints(Client *c);
239 static void updatestatus(void);
240 static void updatetitle(Client *c);
241 static void updatewindowtype(Client *c);
242 static void updatewmhints(Client *c);
243 static void updatecentered(Client *c);
244 static void updatetagprop(Client *c);
245 static void view(const Arg *arg);
246 static void warpmouse(void);
247 static Client *wintoclient(Window w);
248 static Monitor *wintomon(Window w);
249 static int xerror(Display *dpy, XErrorEvent *ee);
250 static int xerrordummy(Display *dpy, XErrorEvent *ee);
251 static int xerrorstart(Display *dpy, XErrorEvent *ee);
252 static void zoom(const Arg *arg);
253
254 /* variables */
255 static const char broken[] = "broken";
256 static const char gui_domain[] = "sys-gui";
257 static char stext[256];
258 static int screen;
259 static int sw, sh; /* X display screen geometry width, height */
260 static int bh; /* bar height */
261 static int lrpad; /* sum of left and right padding for text */
262 static int (*xerrorxlib)(Display *, XErrorEvent *);
263 static unsigned int numlockmask = 0;
264 static void (*handler[LASTEvent]) (XEvent *) = {
265 [ButtonPress] = buttonpress,
266 [ClientMessage] = clientmessage,
267 [ConfigureRequest] = configurerequest,
268 [ConfigureNotify] = configurenotify,
269 [DestroyNotify] = destroynotify,
270 [EnterNotify] = enternotify,
271 [Expose] = expose,
272 [FocusIn] = focusin,
273 [KeyPress] = keypress,
274 [MappingNotify] = mappingnotify,
275 [MapRequest] = maprequest,
276 [MotionNotify] = motionnotify,
277 [PropertyNotify] = propertynotify,
278 [UnmapNotify] = unmapnotify
279 };
280 static Atom wmatom[WMLast], netatom[NetLast], qubesatom[QubesLast], myatom[MyLast];
281 static int restart = 0;
282 static int running = 1;
283 static Cur *cursor[CurLast];
284 static Clr **scheme;
285 static Display *dpy;
286 static Drw *drw;
287 static Monitor *mons, *selmon;
288 static Window root, wmcheckwin;
289
290 /* configuration, allows nested code to access above variables */
291 #include "config.h"
292
293 /* compile-time check if all tags fit into an unsigned int bit array. */
294 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
295
296 /* function implementations */
297 void
298 applyrules(Client *c)
299 {
300 const char *class, *instance;
301 unsigned int i;
302 const Rule *r;
303 Monitor *m;
304 XClassHint ch = { NULL, NULL };
305
306 /* rule matching */
307 c->isfloating = 0;
308 c->tags = 0;
309 XGetClassHint(dpy, c->win, &ch);
310 class = ch.res_class ? ch.res_class : broken;
311 instance = ch.res_name ? ch.res_name : broken;
312
313 for (i = 0; i < LENGTH(rules); i++) {
314 r = &rules[i];
315 if ((!r->title || strstr(c->name, r->title))
316 && (!r->class || strstr(class, r->class))
317 && (!r->instance || strstr(instance, r->instance)))
318 {
319 c->isfloating = r->isfloating;
320 c->tags |= r->tags;
321 for (m = mons; m && m->num != r->monitor; m = m->next);
322 if (m)
323 c->mon = m;
324 }
325 }
326 if (ch.res_class)
327 XFree(ch.res_class);
328 if (ch.res_name)
329 XFree(ch.res_name);
330 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
331 }
332
333 int
334 applysizehints(Client *c, int *x, int *y, int *w, int *h, int *bw, int interact)
335 {
336 int baseismin;
337 Monitor *m = c->mon;
338
339 /* set minimum possible */
340 *w = MAX(1, *w);
341 *h = MAX(1, *h);
342 if (interact) {
343 if (*x > sw)
344 *x = sw - WIDTH(c);
345 if (*y > sh)
346 *y = sh - HEIGHT(c);
347 if (*x + *w + 2 * *bw < 0)
348 *x = 0;
349 if (*y + *h + 2 * *bw < 0)
350 *y = 0;
351 } else {
352 if (*x >= m->wx + m->ww)
353 *x = m->wx + m->ww - WIDTH(c);
354 if (*y >= m->wy + m->wh)
355 *y = m->wy + m->wh - HEIGHT(c);
356 if (*x + *w + 2 * *bw <= m->wx)
357 *x = m->wx;
358 if (*y + *h + 2 * *bw <= m->wy)
359 *y = m->wy;
360 }
361 if (*h < bh)
362 *h = bh;
363 if (*w < bh)
364 *w = bh;
365 if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
366 if (!c->hintsvalid)
367 updatesizehints(c);
368 /* see last two sentences in ICCCM 4.1.2.3 */
369 baseismin = c->basew == c->minw && c->baseh == c->minh;
370 if (!baseismin) { /* temporarily remove base dimensions */
371 *w -= c->basew;
372 *h -= c->baseh;
373 }
374 /* adjust for aspect limits */
375 if (c->mina > 0 && c->maxa > 0) {
376 if (c->maxa < (float)*w / *h)
377 *w = *h * c->maxa + 0.5;
378 else if (c->mina < (float)*h / *w)
379 *h = *w * c->mina + 0.5;
380 }
381 if (baseismin) { /* increment calculation requires this */
382 *w -= c->basew;
383 *h -= c->baseh;
384 }
385 /* adjust for increment value */
386 if (c->incw)
387 *w -= *w % c->incw;
388 if (c->inch)
389 *h -= *h % c->inch;
390 /* restore base dimensions */
391 *w = MAX(*w + c->basew, c->minw);
392 *h = MAX(*h + c->baseh, c->minh);
393 if (c->maxw)
394 *w = MIN(*w, c->maxw);
395 if (c->maxh)
396 *h = MIN(*h, c->maxh);
397 }
398 return *x != c->x || *y != c->y || *w != c->w || *h != c->h || *bw != c->bw;
399 }
400
401 void
402 arrange(Monitor *m)
403 {
404 if (m)
405 showhide(m->stack);
406 else for (m = mons; m; m = m->next)
407 showhide(m->stack);
408 if (m) {
409 arrangemon(m);
410 restack(m);
411 } else for (m = mons; m; m = m->next)
412 arrangemon(m);
413 }
414
415 void
416 arrangeforegrounded (Monitor *m)
417 {
418 unsigned int x,y,w,h;
419 if (!selmon->fg) return;
420 x = m->mx + (m->mw - m->mw * fgw) / 2;
421 y = m->my + (m->mh - m->mh * fgh) / 2;
422 w = (m->mw * fgw) - (2 * (m->fg->bw));
423 h = (m->mh * fgh) - (2 * (m->fg->bw));
424 resize(selmon->fg, x, y, w, h, borderpx, 0);
425 }
426
427 void
428 arrangemon(Monitor *m)
429 {
430 Client *c;
431
432 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
433 if (m->lt[m->sellt]->arrange)
434 m->lt[m->sellt]->arrange(m);
435 else
436 /* <>< case; rather than providing an arrange function and upsetting other logic that tests for its presence, simply add borders here */
437 for (c = selmon->clients; c; c = c->next)
438 if (ISVISIBLE(c) && c->bw == 0)
439 resize(c, c->x, c->y, c->w - 2*borderpx, c->h - 2*borderpx, borderpx, 0);
440
441 if (m->fg)
442 arrangeforegrounded(m);
443 }
444
445 void
446 attach(Client *c)
447 {
448 c->next = c->mon->clients;
449 c->mon->clients = c;
450 }
451
452 void
453 attachstack(Client *c)
454 {
455 c->snext = c->mon->stack;
456 c->mon->stack = c;
457 }
458
459 void
460 buttonpress(XEvent *e)
461 {
462 unsigned int i, x, click, occ;
463 Arg arg = {0};
464 Client *c;
465 Monitor *m;
466 XButtonPressedEvent *ev = &e->xbutton;
467
468 click = ClkRootWin;
469 /* focus monitor if necessary */
470 if ((m = wintomon(ev->window)) && m != selmon) {
471 unfocus(selmon->sel, 1);
472 selmon = m;
473 focus(NULL);
474 }
475 if (ev->window == selmon->barwin) {
476 occ = 0;
477 for(c = m->clients; c; c=c->next)
478 occ |= c->tags == TAGMASK ? 0 : c->tags;
479
480 x = TEXTWNP(stext);
481 x += TEXTW(selmon->ccounter);
482 x += TEXTW(selmon->ltsymbol);
483 for (i = 0; i < LENGTH(tags); i++) {
484 if (!(occ & 1 << i || m->tagset[m->seltags] & 1 << i))
485 continue;
486 x += TEXTWNP(tags[i]);
487 }
488 x = selmon->ww - x;
489
490 if (ev->x < x)
491 click = ClkWinTitle;
492 else if (ev->x < (x += TEXTW(selmon->ccounter)))
493 click = ClkCounter;
494 else {
495 i = 0;
496 do {
497 if (!(occ & 1 << i || m->tagset[m->seltags] & 1 << i))
498 continue;
499 x += TEXTWNP(tags[i]);
500 } while (ev->x >= x && ++i < LENGTH(tags));
501
502 if (i < LENGTH(tags)) {
503 click = ClkTagBar;
504 arg.ui = 1 << i;
505 }
506 else if (ev->x < x + TEXTW(selmon->ltsymbol))
507 click = ClkLtSymbol;
508 else
509 click = ClkStatusText;
510 }
511 } else if ((c = wintoclient(ev->window))) {
512 focus(c);
513 restack(selmon);
514 XAllowEvents(dpy, ReplayPointer, CurrentTime);
515 click = ClkClientWin;
516 }
517 for (i = 0; i < LENGTH(buttons); i++)
518 if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
519 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
520 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
521 }
522
523 void
524 checkotherwm(void)
525 {
526 xerrorxlib = XSetErrorHandler(xerrorstart);
527 /* this causes an error if some other window manager is running */
528 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
529 XSync(dpy, False);
530 XSetErrorHandler(xerror);
531 XSync(dpy, False);
532 }
533
534 void
535 cleanup(void)
536 {
537 Arg a = {.ui = ~0};
538 Layout foo = { "", NULL };
539 Monitor *m;
540 size_t i;
541
542 view(&a);
543 selmon->lt[selmon->sellt] = &foo;
544 for (m = mons; m; m = m->next)
545 while (m->stack)
546 unmanage(m->stack, 0);
547 XUngrabKey(dpy, AnyKey, AnyModifier, root);
548 while (mons)
549 cleanupmon(mons);
550 for (i = 0; i < CurLast; i++)
551 drw_cur_free(drw, cursor[i]);
552 for (i = 0; i < LENGTH(colors); i++)
553 drw_scm_free(drw, scheme[i], 3);
554 free(scheme);
555 XDestroyWindow(dpy, wmcheckwin);
556 drw_free(drw);
557 XSync(dpy, False);
558 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
559 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
560 }
561
562 void
563 cleanupmon(Monitor *mon)
564 {
565 Monitor *m;
566
567 if (mon == mons)
568 mons = mons->next;
569 else {
570 for (m = mons; m && m->next != mon; m = m->next);
571 m->next = mon->next;
572 }
573 XUnmapWindow(dpy, mon->barwin);
574 XDestroyWindow(dpy, mon->barwin);
575 free(mon);
576 }
577
578 void
579 clientmessage(XEvent *e)
580 {
581 XClientMessageEvent *cme = &e->xclient;
582 Client *c = wintoclient(cme->window);
583 unsigned int i;
584
585 if (!c)
586 return;
587 if (cme->message_type == netatom[NetWMState]) {
588 if (cme->data.l[1] == netatom[NetWMFullscreen]
589 || cme->data.l[2] == netatom[NetWMFullscreen])
590 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
591 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
592 } else if (cme->message_type == netatom[NetActiveWindow]) {
593 for (i = 0; i < LENGTH(tags) && !((1 << i) & c->tags); i++);
594 if (i < LENGTH(tags)) {
595 const Arg a = {.ui = 1 << i};
596 view(&a);
597 focus(c);
598 arrange(selmon);
599 warpmouse();
600 }
601 }
602 }
603
604 void
605 configure(Client *c)
606 {
607 XConfigureEvent ce;
608
609 ce.type = ConfigureNotify;
610 ce.display = dpy;
611 ce.event = c->win;
612 ce.window = c->win;
613 ce.x = c->x;
614 ce.y = c->y;
615 ce.width = c->w;
616 ce.height = c->h;
617 ce.border_width = c->bw;
618 ce.above = None;
619 ce.override_redirect = False;
620 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
621 }
622
623 void
624 configurenotify(XEvent *e)
625 {
626 Monitor *m;
627 Client *c;
628 XConfigureEvent *ev = &e->xconfigure;
629 int dirty;
630
631 /* TODO: updategeom handling sucks, needs to be simplified */
632 if (ev->window == root) {
633 dirty = (sw != ev->width || sh != ev->height);
634 sw = ev->width;
635 sh = ev->height;
636 if (updategeom() || dirty) {
637 drw_resize(drw, sw, bh);
638 updatebars();
639 for (m = mons; m; m = m->next) {
640 for (c = m->clients; c; c = c->next)
641 if (c->isfullscreen)
642 resizeclient(c, m->mx, m->my, m->mw, m->mh, 0);
643 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
644 }
645 focus(NULL);
646 arrange(NULL);
647 }
648 }
649 }
650
651 void
652 configurerequest(XEvent *e)
653 {
654 Client *c;
655 Monitor *m;
656 XConfigureRequestEvent *ev = &e->xconfigurerequest;
657 XWindowChanges wc;
658
659 if ((c = wintoclient(ev->window))) {
660 if (ev->value_mask & CWBorderWidth)
661 c->bw = ev->border_width;
662 else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
663 m = c->mon;
664 if (ev->value_mask & CWX) {
665 c->oldx = c->x;
666 c->x = m->mx + ev->x;
667 }
668 if (ev->value_mask & CWY) {
669 c->oldy = c->y;
670 c->y = m->my + ev->y;
671 }
672 if (ev->value_mask & CWWidth) {
673 c->oldw = c->w;
674 c->w = ev->width;
675 }
676 if (ev->value_mask & CWHeight) {
677 c->oldh = c->h;
678 c->h = ev->height;
679 }
680 if ((c->x + c->w) > m->mx + m->mw && c->isfloating)
681 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
682 if ((c->y + c->h) > m->my + m->mh && c->isfloating)
683 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
684 if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
685 configure(c);
686 if (ISVISIBLE(c))
687 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
688 } else
689 configure(c);
690 } else {
691 wc.x = ev->x;
692 wc.y = ev->y;
693 wc.width = ev->width;
694 wc.height = ev->height;
695 wc.border_width = ev->border_width;
696 wc.sibling = ev->above;
697 wc.stack_mode = ev->detail;
698 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
699 }
700 XSync(dpy, False);
701 }
702
703 Monitor *
704 createmon(void)
705 {
706 Monitor *m;
707
708 m = ecalloc(1, sizeof(Monitor));
709 m->tagset[0] = m->tagset[1] = 1;
710 m->mfact = mfact;
711 m->nmaster = nmaster;
712 m->showbar = showbar;
713 m->topbar = topbar;
714 m->lt[0] = &layouts[0];
715 m->lt[1] = &layouts[1 % LENGTH(layouts)];
716 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
717 return m;
718 }
719
720 void
721 deck(Monitor *m)
722 {
723 unsigned int i, n, h, mw, my, bw;
724 Client *c;
725
726 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
727 if(n == 0)
728 return;
729
730 if (n == 1)
731 bw = 0;
732 else
733 bw = borderpx;
734
735 if(n > m->nmaster) {
736 mw = m->nmaster ? m->ww * m->mfact : 0;
737 }
738 else
739 mw = m->ww;
740 for (i = my = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
741 if (i < m->nmaster) {
742 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
743 resizeclient(c, m->wx, m->wy + my, mw - 2*bw, h - 2*bw, bw);
744 my += HEIGHT(c);
745 } else {
746 resizeclient(c, m->wx + mw, m->wy, m->ww - mw - 2*bw, m->wh - 2*bw, bw);
747 }
748 }
749
750 void
751 destroynotify(XEvent *e)
752 {
753 Client *c;
754 XDestroyWindowEvent *ev = &e->xdestroywindow;
755
756 if ((c = wintoclient(ev->window)))
757 unmanage(c, 1);
758 }
759
760 void
761 detach(Client *c)
762 {
763 Client **tc;
764
765 for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
766 *tc = c->next;
767 }
768
769 void
770 detachstack(Client *c)
771 {
772 Client **tc, *t;
773
774 for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
775 *tc = c->snext;
776
777 if (c == c->mon->sel) {
778 for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
779 c->mon->sel = t;
780 }
781 }
782
783 /*
784 Monitor *
785 dirtomon(int dir)
786 {
787 Monitor *m = NULL;
788
789 if (dir > 0) {
790 if (!(m = selmon->next))
791 m = mons;
792 } else if (selmon == mons)
793 for (m = mons; m->next; m = m->next);
794 else
795 for (m = mons; m->next != selmon; m = m->next);
796 return m;
797 }
798 */
799
800 void
801 drawbar(Monitor *m)
802 {
803 int x, w, tw = 0, cn, ct = 0;
804 int boxs = drw->fonts->h / 9;
805 int boxw = drw->fonts->h / 6 + 2;
806 unsigned int i, occ = 0, urg = 0;
807 Client *c;
808
809 if (!m->showbar)
810 return;
811
812 /* draw status first so it can be overdrawn by tags later */
813 if (m == selmon) { /* status is only drawn on selected monitor */
814 drw_setscheme(drw, scheme[SchemeNorm]);
815 tw = TEXTWNP(stext);
816 drw_text(drw, m->ww - tw, 0, tw, bh, 0, stext, 0);
817 }
818
819 for (c = m->clients; c; c = c->next) {
820 occ |= c->tags == TAGMASK ? 0 : c->tags;
821 if (c->isurgent)
822 urg |= c->tags;
823 if (c->tags & m->tagset[m->seltags]) {
824 ct++;
825 if (c == m->sel)
826 cn = ct;
827 }
828 }
829
830 if (ct > 0)
831 snprintf(m->ccounter, sizeof(m->ccounter), "%d/%d", cn & 0xFF, ct & 0xFF);
832 else
833 m->ccounter[0] = '\0';
834
835 tw += TEXTW(m->ccounter);
836 tw += TEXTW(m->ltsymbol);
837
838 for (i = 0; i < LENGTH(tags); i++) {
839 if(!(occ & 1 << i || m->tagset[m->seltags] & 1 << i))
840 continue;
841 tw += TEXTWNP(tags[i]);
842 }
843
844 if (m->sel && m->sel->mystatus[0] != '\0') {
845 tw += TEXTW(m->sel->mystatus);
846 tw += TEXTW("«");
847 }
848
849 x = 0;
850
851 if (m->sel) {
852 w = TEXTW(m->sel->vmname);
853 x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->vmname, 0);
854 }
855
856 if ((w = m->ww - tw - x) > bh) {
857 if (m->sel) {
858 drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->name, 0);
859 if (m->sel->isfloating)
860 drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
861 } else {
862 drw_setscheme(drw, scheme[SchemeNorm]);
863 drw_rect(drw, x, 0, w, bh, 1, 1);
864 }
865 x += w;
866 }
867
868 if (m->sel && m->sel->mystatus[0] != '\0') {
869 w = TEXTW(m->sel->mystatus);
870 x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->mystatus, 0);
871 w = TEXTW("«");
872 x = drw_text(drw, x, 0, w, bh, lrpad / 2, "«", 0);
873 }
874
875 w = TEXTW(m->ccounter);
876 x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ccounter, 0);
877
878 for (i = 0; i < LENGTH(tags); i++) {
879 if(!(occ & 1 << i || m->tagset[m->seltags] & 1 << i))
880 continue;
881 w = TEXTWNP(tags[i]);
882 drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]);
883 drw_text(drw, x, 0, w, bh, 0, tags[i], urg & 1 << i);
884 x += w;
885 }
886
887 drw_setscheme(drw, scheme[SchemeNorm]);
888 w = TEXTW(m->ltsymbol);
889 x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0);
890
891 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
892 }
893
894 void
895 drawbars(void)
896 {
897 Monitor *m;
898
899 for (m = mons; m; m = m->next)
900 drawbar(m);
901 }
902
903 void
904 enternotify(XEvent *e)
905 {
906 Client *c;
907 Monitor *m;
908 XCrossingEvent *ev = &e->xcrossing;
909
910 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
911 return;
912 c = wintoclient(ev->window);
913 m = c ? c->mon : wintomon(ev->window);
914 if (m != selmon) {
915 unfocus(selmon->sel, 1);
916 selmon = m;
917 } else if (!c || c == selmon->sel)
918 return;
919 focus(c);
920 }
921
922 void
923 expose(XEvent *e)
924 {
925 Monitor *m;
926 XExposeEvent *ev = &e->xexpose;
927
928 if (ev->count == 0 && (m = wintomon(ev->window)))
929 drawbar(m);
930 }
931
932 void
933 fgtoggle(const Arg *arg)
934 {
935 Client *c = selmon->fg;
936 if (c) {
937 selmon->fg = NULL;
938 focus(c);
939 } else {
940 selmon->fg = selmon->sel;
941 }
942 arrange(selmon);
943 }
944
945 void
946 focus(Client *c)
947 {
948 if (!c || !ISVISIBLE(c))
949 for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
950 if (selmon->sel && selmon->sel != c)
951 unfocus(selmon->sel, 0);
952 if (c) {
953 if (c->mon != selmon)
954 selmon = c->mon;
955 if (c->isurgent)
956 seturgent(c, 0);
957 detachstack(c);
958 attachstack(c);
959 grabbuttons(c, 1);
960 XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel);
961 setfocus(c);
962 } else {
963 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
964 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
965 }
966 selmon->sel = c;
967 drawbars();
968 }
969
970 /* there are some broken focus acquiring clients needing extra handling */
971 void
972 focusin(XEvent *e)
973 {
974 XFocusChangeEvent *ev = &e->xfocus;
975
976 if (selmon->sel && ev->window != selmon->sel->win)
977 setfocus(selmon->sel);
978 }
979
980 /*
981 void
982 focusmon(const Arg *arg)
983 {
984 Monitor *m;
985
986 if (!mons->next)
987 return;
988 if ((m = dirtomon(arg->i)) == selmon)
989 return;
990 unfocus(selmon->sel, 0);
991 selmon = m;
992 focus(NULL);
993 if (selmon->sel)
994 XWarpPointer(dpy, None, selmon->sel->win, 0, 0, 0, 0, selmon->sel->w/2, selmon->sel->h/2);
995 }
996 */
997
998 void
999 warpmouse()
1000 {
1001 if (selmon->sel)
1002 XWarpPointer(dpy, None, selmon->sel->win, 0, 0, 0, 0, selmon->sel->w/2, selmon->sel->h/2);
1003 }
1004
1005 void
1006 focusstack(const Arg *arg)
1007 {
1008 Client *c = NULL, *i;
1009
1010 if (!selmon->sel || (selmon->sel->isfullscreen && lockfullscreen))
1011 return;
1012
1013 if (arg->i > 0) {
1014 for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
1015 if (!c)
1016 for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
1017 } else {
1018 for (i = selmon->clients; i != selmon->sel; i = i->next)
1019 if (ISVISIBLE(i))
1020 c = i;
1021 if (!c)
1022 for (; i; i = i->next)
1023 if (ISVISIBLE(i))
1024 c = i;
1025 }
1026
1027 if (c && selmon->fg) {
1028 selmon->fg = c;
1029 arrange(selmon);
1030 focus(c);
1031 warpmouse();
1032 }
1033 else if (c) {
1034 focus(c);
1035 restack(selmon);
1036 warpmouse();
1037 }
1038 }
1039
1040 void
1041 focusstacknum(const Arg *arg)
1042 {
1043 Client *c = NULL;
1044 int j = 0;
1045
1046 if (!selmon->sel || (selmon->sel->isfullscreen && lockfullscreen))
1047 return;
1048
1049 if (arg->i > 0) {
1050 for (c = selmon->clients; c; c = c->next) {
1051 if (ISVISIBLE(c)) {
1052 if (j == arg->i - 1) {
1053 break;
1054 }
1055 j++;
1056 }
1057 }
1058 }
1059 if (c) {
1060 focus(c);
1061 restack(selmon);
1062 warpmouse();
1063 }
1064 }
1065
1066 Atom
1067 getatomprop(Client *c, Atom prop)
1068 {
1069 int di;
1070 unsigned long nitems, dl;
1071 unsigned char *p = NULL;
1072 Atom da, atom = None;
1073
1074 if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
1075 &da, &di, &nitems, &dl, &p) == Success && p) {
1076 if (nitems > 0)
1077 atom = *(Atom *)p;
1078 XFree(p);
1079 }
1080 return atom;
1081 }
1082
1083 int
1084 getrootptr(int *x, int *y)
1085 {
1086 int di;
1087 unsigned int dui;
1088 Window dummy;
1089
1090 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
1091 }
1092
1093 long
1094 getstate(Window w)
1095 {
1096 int format;
1097 long result = -1;
1098 unsigned char *p = NULL;
1099 unsigned long n, extra;
1100 Atom real;
1101
1102 if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
1103 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
1104 return -1;
1105 if (n != 0)
1106 result = *p;
1107 XFree(p);
1108 return result;
1109 }
1110
1111 int
1112 gettextprop(Window w, Atom atom, char *text, unsigned int size)
1113 {
1114 char **list = NULL;
1115 int n;
1116 XTextProperty name;
1117
1118 if (!text || size == 0)
1119 return 0;
1120 text[0] = '\0';
1121 if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems)
1122 return 0;
1123 if (name.encoding == XA_STRING) {
1124 strncpy(text, (char *)name.value, size - 1);
1125 } else if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
1126 strncpy(text, *list, size - 1);
1127 XFreeStringList(list);
1128 }
1129 text[size - 1] = '\0';
1130 XFree(name.value);
1131 return 1;
1132 }
1133
1134 void
1135 grabbuttons(Client *c, int focused)
1136 {
1137 updatenumlockmask();
1138 {
1139 unsigned int i, j;
1140 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1141 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1142 if (!focused)
1143 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
1144 BUTTONMASK, GrabModeSync, GrabModeSync, None, None);
1145 for (i = 0; i < LENGTH(buttons); i++)
1146 if (buttons[i].click == ClkClientWin)
1147 for (j = 0; j < LENGTH(modifiers); j++)
1148 XGrabButton(dpy, buttons[i].button,
1149 buttons[i].mask | modifiers[j],
1150 c->win, False, BUTTONMASK,
1151 GrabModeAsync, GrabModeSync, None, None);
1152 }
1153 }
1154
1155 void
1156 grabkeys(void)
1157 {
1158 updatenumlockmask();
1159 {
1160 unsigned int i, j, k;
1161 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1162 int start, end, skip;
1163 KeySym *syms;
1164
1165 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1166 XDisplayKeycodes(dpy, &start, &end);
1167 syms = XGetKeyboardMapping(dpy, start, end - start + 1, &skip);
1168 if (!syms)
1169 return;
1170 for (k = start; k <= end; k++)
1171 for (i = 0; i < LENGTH(keys); i++)
1172 /* skip modifier codes, we do that ourselves */
1173 if (keys[i].keysym == syms[(k - start) * skip])
1174 for (j = 0; j < LENGTH(modifiers); j++)
1175 XGrabKey(dpy, k,
1176 keys[i].mod | modifiers[j],
1177 root, True,
1178 GrabModeAsync, GrabModeAsync);
1179 XFree(syms);
1180 }
1181 }
1182
1183 void
1184 incnmaster(const Arg *arg)
1185 {
1186 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
1187 arrange(selmon);
1188 warpmouse();
1189 }
1190
1191 #ifdef XINERAMA
1192 static int
1193 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
1194 {
1195 while (n--)
1196 if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1197 && unique[n].width == info->width && unique[n].height == info->height)
1198 return 0;
1199 return 1;
1200 }
1201 #endif /* XINERAMA */
1202
1203 void
1204 keypress(XEvent *e)
1205 {
1206 unsigned int i;
1207 KeySym keysym;
1208 XKeyEvent *ev;
1209
1210 ev = &e->xkey;
1211 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1212 for (i = 0; i < LENGTH(keys); i++)
1213 if (keysym == keys[i].keysym
1214 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1215 && keys[i].func)
1216 keys[i].func(&(keys[i].arg));
1217 }
1218
1219 void
1220 killclient(const Arg *arg)
1221 {
1222 if (!selmon->sel)
1223 return;
1224 if (!sendevent(selmon->sel, wmatom[WMDelete])) {
1225 XGrabServer(dpy);
1226 XSetErrorHandler(xerrordummy);
1227 XSetCloseDownMode(dpy, DestroyAll);
1228 XKillClient(dpy, selmon->sel->win);
1229 XSync(dpy, False);
1230 XSetErrorHandler(xerror);
1231 XUngrabServer(dpy);
1232 }
1233 }
1234
1235 void
1236 manage(Window w, XWindowAttributes *wa)
1237 {
1238 Client *c, *t = NULL;
1239 Window trans = None;
1240 XWindowChanges wc;
1241
1242 c = ecalloc(1, sizeof(Client));
1243 c->win = w;
1244 /* geometry */
1245 c->x = c->oldx = wa->x;
1246 c->y = c->oldy = wa->y;
1247 c->w = c->oldw = wa->width;
1248 c->h = c->oldh = wa->height;
1249 c->oldbw = wa->border_width;
1250
1251 updatetitle(c);
1252 if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1253 c->mon = t->mon;
1254 c->tags = t->tags;
1255 } else {
1256 c->mon = selmon;
1257 applyrules(c);
1258 }
1259
1260 if (c->x + WIDTH(c) > c->mon->wx + c->mon->ww)
1261 c->x = c->mon->wx + c->mon->ww - WIDTH(c);
1262 if (c->y + HEIGHT(c) > c->mon->wy + c->mon->wh)
1263 c->y = c->mon->wy + c->mon->wh - HEIGHT(c);
1264 c->x = MAX(c->x, c->mon->wx);
1265 c->y = MAX(c->y, c->mon->wy);
1266 c->bw = borderpx;
1267
1268 wc.border_width = c->bw;
1269 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1270 XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel);
1271 configure(c); /* propagates border_width, if size doesn't change */
1272 updatewindowtype(c);
1273 updatesizehints(c);
1274 updatewmhints(c);
1275 updatecentered(c);
1276 updatetagprop(c);
1277 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1278 grabbuttons(c, 0);
1279 if (!c->isfloating)
1280 c->isfloating = c->oldstate = trans != None || c->isfixed;
1281 if (c->isfloating)
1282 XRaiseWindow(dpy, c->win);
1283 attach(c);
1284 attachstack(c);
1285 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1286 (unsigned char *) &(c->win), 1);
1287 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1288 setclientstate(c, NormalState);
1289 if (c->mon == selmon)
1290 unfocus(selmon->sel, 0);
1291 c->mon->sel = c;
1292 arrange(c->mon);
1293 XMapWindow(dpy, c->win);
1294 if (c && c->mon == selmon)
1295 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w/2, c->h/2);
1296 focus(NULL);
1297 }
1298
1299 void
1300 mappingnotify(XEvent *e)
1301 {
1302 XMappingEvent *ev = &e->xmapping;
1303
1304 XRefreshKeyboardMapping(ev);
1305 if (ev->request == MappingKeyboard)
1306 grabkeys();
1307 }
1308
1309 void
1310 maprequest(XEvent *e)
1311 {
1312 static XWindowAttributes wa;
1313 XMapRequestEvent *ev = &e->xmaprequest;
1314
1315 if (!XGetWindowAttributes(dpy, ev->window, &wa) || wa.override_redirect)
1316 return;
1317 if (!wintoclient(ev->window))
1318 manage(ev->window, &wa);
1319 }
1320
1321 void
1322 monocle(Monitor *m)
1323 {
1324 unsigned int n = 0;
1325 Client *c;
1326
1327 for (c = m->clients; c; c = c->next)
1328 if (ISVISIBLE(c))
1329 n++;
1330 for (c = nexttiled(m->clients); c; c = nexttiled(c->next))
1331 resizeclient(c, m->wx, m->wy, m->ww, m->wh, 0);
1332 }
1333
1334 void
1335 motionnotify(XEvent *e)
1336 {
1337 static Monitor *mon = NULL;
1338 Monitor *m;
1339 XMotionEvent *ev = &e->xmotion;
1340
1341 if (ev->window != root)
1342 return;
1343 if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1344 unfocus(selmon->sel, 1);
1345 selmon = m;
1346 focus(NULL);
1347 }
1348 mon = m;
1349 }
1350
1351 void
1352 movemouse(const Arg *arg)
1353 {
1354 int x, y, ocx, ocy, nx, ny;
1355 Client *c;
1356 Monitor *m;
1357 XEvent ev;
1358 Time lasttime = 0;
1359
1360 if (!(c = selmon->sel))
1361 return;
1362 if (c->isfullscreen) /* no support moving fullscreen windows by mouse */
1363 return;
1364 restack(selmon);
1365 ocx = c->x;
1366 ocy = c->y;
1367 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1368 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1369 return;
1370 if (!getrootptr(&x, &y))
1371 return;
1372 do {
1373 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1374 switch(ev.type) {
1375 case ConfigureRequest:
1376 case Expose:
1377 case MapRequest:
1378 handler[ev.type](&ev);
1379 break;
1380 case MotionNotify:
1381 if ((ev.xmotion.time - lasttime) <= (1000 / refreshrate))
1382 continue;
1383 lasttime = ev.xmotion.time;
1384
1385 nx = ocx + (ev.xmotion.x - x);
1386 ny = ocy + (ev.xmotion.y - y);
1387 if (abs(selmon->wx - nx) < snap)
1388 nx = selmon->wx;
1389 else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1390 nx = selmon->wx + selmon->ww - WIDTH(c);
1391 if (abs(selmon->wy - ny) < snap)
1392 ny = selmon->wy;
1393 else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1394 ny = selmon->wy + selmon->wh - HEIGHT(c);
1395 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1396 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1397 togglefloating(NULL);
1398 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1399 resize(c, nx, ny, c->w, c->h, c->bw, 1);
1400 break;
1401 }
1402 } while (ev.type != ButtonRelease);
1403 XUngrabPointer(dpy, CurrentTime);
1404 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1405 sendmon(c, m);
1406 selmon = m;
1407 focus(NULL);
1408 }
1409 }
1410
1411 Client *
1412 nexttiled(Client *c)
1413 {
1414 for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1415 return c;
1416 }
1417
1418 void
1419 pop(Client *c)
1420 {
1421 detach(c);
1422 attach(c);
1423 focus(c);
1424 arrange(c->mon);
1425 warpmouse();
1426 }
1427
1428 void
1429 propertynotify(XEvent *e)
1430 {
1431 Client *c;
1432 Window trans;
1433 XPropertyEvent *ev = &e->xproperty;
1434
1435 if ((ev->window == root) && (ev->atom == XA_WM_NAME))
1436 updatestatus();
1437 else if (ev->state == PropertyDelete)
1438 return; /* ignore */
1439 else if ((c = wintoclient(ev->window))) {
1440 switch(ev->atom) {
1441 default: break;
1442 case XA_WM_TRANSIENT_FOR:
1443 if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1444 (c->isfloating = (wintoclient(trans)) != NULL))
1445 arrange(c->mon);
1446 break;
1447 case XA_WM_NORMAL_HINTS:
1448 c->hintsvalid = 0;
1449 break;
1450 case XA_WM_HINTS:
1451 updatewmhints(c);
1452 drawbars();
1453 break;
1454 }
1455 if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName] ||
1456 ev->atom == qubesatom[QubesVMName] || ev->atom == myatom[MyStatus]) {
1457 updatetitle(c);
1458 if (c == c->mon->sel)
1459 drawbar(c->mon);
1460 }
1461 if (ev->atom == netatom[NetWMWindowType])
1462 updatewindowtype(c);
1463 }
1464 }
1465
1466 void
1467 quit(const Arg *arg)
1468 {
1469 if(arg->i) restart = 1;
1470 running = 0;
1471 }
1472
1473 Monitor *
1474 recttomon(int x, int y, int w, int h)
1475 {
1476 Monitor *m, *r = selmon;
1477 int a, area = 0;
1478
1479 for (m = mons; m; m = m->next)
1480 if ((a = INTERSECT(x, y, w, h, m)) > area) {
1481 area = a;
1482 r = m;
1483 }
1484 return r;
1485 }
1486
1487 void
1488 resize(Client *c, int x, int y, int w, int h, int bw, int interact)
1489 {
1490 if (applysizehints(c, &x, &y, &w, &h, &bw, interact))
1491 resizeclient(c, x, y, w, h, bw);
1492 }
1493
1494 void
1495 resizeclient(Client *c, int x, int y, int w, int h, int bw)
1496 {
1497 XWindowChanges wc;
1498
1499 c->oldx = c->x; c->x = wc.x = x;
1500 c->oldy = c->y; c->y = wc.y = y;
1501 c->oldw = c->w; c->w = wc.width = w;
1502 c->oldh = c->h; c->h = wc.height = h;
1503 c->oldbw = c->bw; c->bw = wc.border_width = bw;
1504 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1505 configure(c);
1506 XSync(dpy, False);
1507 }
1508
1509 void
1510 resizemouse(const Arg *arg)
1511 {
1512 int ocx, ocy, nw, nh;
1513 Client *c;
1514 Monitor *m;
1515 XEvent ev;
1516 Time lasttime = 0;
1517
1518 if (!(c = selmon->sel))
1519 return;
1520 if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1521 return;
1522 restack(selmon);
1523 ocx = c->x;
1524 ocy = c->y;
1525 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1526 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1527 return;
1528 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1529 do {
1530 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1531 switch(ev.type) {
1532 case ConfigureRequest:
1533 case Expose:
1534 case MapRequest:
1535 handler[ev.type](&ev);
1536 break;
1537 case MotionNotify:
1538 if ((ev.xmotion.time - lasttime) <= (1000 / refreshrate))
1539 continue;
1540 lasttime = ev.xmotion.time;
1541
1542 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1543 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1544 if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1545 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1546 {
1547 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1548 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1549 togglefloating(NULL);
1550 }
1551 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1552 resize(c, c->x, c->y, nw, nh, c->bw, 1);
1553 break;
1554 }
1555 } while (ev.type != ButtonRelease);
1556 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1557 XUngrabPointer(dpy, CurrentTime);
1558 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1559 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1560 sendmon(c, m);
1561 selmon = m;
1562 focus(NULL);
1563 }
1564 }
1565
1566 void
1567 restack(Monitor *m)
1568 {
1569 Client *c;
1570 XEvent ev;
1571 XWindowChanges wc;
1572
1573 drawbar(m);
1574 if (!m->sel)
1575 return;
1576 if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
1577 XRaiseWindow(dpy, m->sel->win);
1578 if (m->fg) {
1579 wc.stack_mode = Above;
1580 XConfigureWindow(dpy, m->fg->win, CWStackMode, &wc);
1581 }
1582 else if (m->lt[m->sellt]->arrange) {
1583 wc.stack_mode = Below;
1584 wc.sibling = m->barwin;
1585 for (c = m->stack; c; c = c->snext)
1586 if (!c->isfloating && ISVISIBLE(c)) {
1587 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1588 wc.sibling = c->win;
1589 }
1590 }
1591 XSync(dpy, False);
1592 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1593 }
1594
1595 void
1596 run(void)
1597 {
1598 XEvent ev;
1599 /* main event loop */
1600 XSync(dpy, False);
1601 while (running && !XNextEvent(dpy, &ev))
1602 if (handler[ev.type])
1603 handler[ev.type](&ev); /* call handler */
1604 }
1605
1606 void
1607 scan(void)
1608 {
1609 unsigned int i, num;
1610 Window d1, d2, *wins = NULL;
1611 XWindowAttributes wa;
1612
1613 if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1614 for (i = 0; i < num; i++) {
1615 if (!XGetWindowAttributes(dpy, wins[i], &wa)
1616 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1617 continue;
1618 if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1619 manage(wins[i], &wa);
1620 }
1621 for (i = 0; i < num; i++) { /* now the transients */
1622 if (!XGetWindowAttributes(dpy, wins[i], &wa))
1623 continue;
1624 if (XGetTransientForHint(dpy, wins[i], &d1)
1625 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1626 manage(wins[i], &wa);
1627 }
1628 if (wins)
1629 XFree(wins);
1630 }
1631 }
1632
1633 void
1634 sendmon(Client *c, Monitor *m)
1635 {
1636 if (c->mon == m)
1637 return;
1638 unfocus(c, 1);
1639 detach(c);
1640 detachstack(c);
1641 c->mon = m;
1642 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1643 attach(c);
1644 attachstack(c);
1645 setclienttagprop(c);
1646 focus(NULL);
1647 arrange(NULL);
1648 }
1649
1650 void
1651 setclientstate(Client *c, long state)
1652 {
1653 long data[] = { state, None };
1654
1655 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1656 PropModeReplace, (unsigned char *)data, 2);
1657 }
1658
1659 void
1660 setclienttagprop(Client *c)
1661 {
1662 long data[] = { (long) c->tags, (long) c->mon->num };
1663 XChangeProperty(dpy, c->win, netatom[NetClientInfo], XA_CARDINAL, 32,
1664 PropModeReplace, (unsigned char *) data, 2);
1665 }
1666
1667 int
1668 sendevent(Client *c, Atom proto)
1669 {
1670 int n;
1671 Atom *protocols;
1672 int exists = 0;
1673 XEvent ev;
1674
1675 if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1676 while (!exists && n--)
1677 exists = protocols[n] == proto;
1678 XFree(protocols);
1679 }
1680 if (exists) {
1681 ev.type = ClientMessage;
1682 ev.xclient.window = c->win;
1683 ev.xclient.message_type = wmatom[WMProtocols];
1684 ev.xclient.format = 32;
1685 ev.xclient.data.l[0] = proto;
1686 ev.xclient.data.l[1] = CurrentTime;
1687 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1688 }
1689 return exists;
1690 }
1691
1692 void
1693 setfocus(Client *c)
1694 {
1695 if (!c->neverfocus) {
1696 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1697 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1698 XA_WINDOW, 32, PropModeReplace,
1699 (unsigned char *) &(c->win), 1);
1700 }
1701 sendevent(c, wmatom[WMTakeFocus]);
1702 }
1703
1704 void
1705 setfullscreen(Client *c, int fullscreen)
1706 {
1707 if (fullscreen && !c->isfullscreen) {
1708 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1709 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1710 c->isfullscreen = 1;
1711 c->oldstate = c->isfloating;
1712 c->isfloating = 1;
1713 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh, 0);
1714 XRaiseWindow(dpy, c->win);
1715 } else if (!fullscreen && c->isfullscreen){
1716 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1717 PropModeReplace, (unsigned char*)0, 0);
1718 c->isfullscreen = 0;
1719 c->isfloating = c->oldstate;
1720 c->x = c->oldx;
1721 c->y = c->oldy;
1722 c->w = c->oldw;
1723 c->h = c->oldh;
1724 c->bw = c->oldbw;
1725 resizeclient(c, c->x, c->y, c->w, c->h, c->bw);
1726 arrange(c->mon);
1727 }
1728 }
1729
1730 void
1731 setlayout(const Arg *arg)
1732 {
1733 if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1734 selmon->sellt ^= 1;
1735 if (arg && arg->v)
1736 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1737 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1738 if (selmon->sel)
1739 arrange(selmon);
1740 else
1741 drawbar(selmon);
1742 }
1743
1744 /* arg > 1.0 will set mfact absolutely */
1745 void
1746 setmfact(const Arg *arg)
1747 {
1748 float f;
1749
1750 if (!arg || !selmon->lt[selmon->sellt]->arrange)
1751 return;
1752 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1753 if (f < 0.05 || f > 0.95)
1754 return;
1755 selmon->mfact = f;
1756 arrange(selmon);
1757 warpmouse();
1758 }
1759
1760 void
1761 setup(void)
1762 {
1763 int i;
1764 XSetWindowAttributes wa;
1765 Atom utf8string;
1766 struct sigaction sa, sa_term, sa_hup;
1767
1768 /* do not transform children into zombies when they terminate */
1769 sigemptyset(&sa.sa_mask);
1770 sa.sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT | SA_RESTART;
1771 sa.sa_handler = SIG_IGN;
1772 sigaction(SIGCHLD, &sa, NULL);
1773
1774 /* handle termination */
1775 sigemptyset(&sa_term.sa_mask);
1776 sa_term.sa_flags = SA_RESTART;
1777 sa_term.sa_handler = sigterm;
1778 sigaction(SIGTERM, &sa_term, NULL);
1779
1780 /* handle hangup */
1781 sigemptyset(&sa_hup.sa_mask);
1782 sa_hup.sa_flags = SA_RESTART;
1783 sa_hup.sa_handler = sighup;
1784 sigaction(SIGHUP, &sa_hup, NULL);
1785
1786 /* clean up any zombies (inherited from .xinitrc etc) immediately */
1787 while (waitpid(-1, NULL, WNOHANG) > 0);
1788
1789 /* init screen */
1790 screen = DefaultScreen(dpy);
1791 sw = DisplayWidth(dpy, screen);
1792 sh = DisplayHeight(dpy, screen);
1793 root = RootWindow(dpy, screen);
1794 drw = drw_create(dpy, screen, root, sw, sh);
1795 if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
1796 die("no fonts could be loaded.");
1797 lrpad = drw->fonts->h;
1798 bh = drw->fonts->h + 2;
1799 updategeom();
1800 /* init atoms */
1801 utf8string = XInternAtom(dpy, "UTF8_STRING", False);
1802 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1803 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1804 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1805 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1806 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1807 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1808 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1809 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1810 netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
1811 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1812 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1813 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1814 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1815 netatom[NetClientInfo] = XInternAtom(dpy, "_NET_CLIENT_INFO", False);
1816 /* init qubes atoms */
1817 qubesatom[QubesVMName] = XInternAtom(dpy, "_QUBES_VMNAME", False);
1818 /* init my atoms */
1819 myatom[MyStatus] = XInternAtom(dpy, "_MY_WIN_STATUS", False);
1820 /* init cursors */
1821 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1822 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1823 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1824 /* init appearance */
1825 scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
1826 for (i = 0; i < LENGTH(colors); i++)
1827 scheme[i] = drw_scm_create(drw, colors[i], 3);
1828 /* init bars */
1829 updatebars();
1830 updatestatus();
1831 /* supporting window for NetWMCheck */
1832 wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
1833 XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
1834 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1835 XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8,
1836 PropModeReplace, (unsigned char *) "dwm", 3);
1837 XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32,
1838 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1839 /* EWMH support per view */
1840 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1841 PropModeReplace, (unsigned char *) netatom, NetLast);
1842 XDeleteProperty(dpy, root, netatom[NetClientList]);
1843 XDeleteProperty(dpy, root, netatom[NetClientInfo]);
1844 /* select events */
1845 wa.cursor = cursor[CurNormal]->cursor;
1846 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
1847 |ButtonPressMask|PointerMotionMask|EnterWindowMask
1848 |LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1849 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1850 XSelectInput(dpy, root, wa.event_mask);
1851 grabkeys();
1852 focus(NULL);
1853 }
1854
1855 void
1856 seturgent(Client *c, int urg)
1857 {
1858 XWMHints *wmh;
1859
1860 c->isurgent = urg;
1861 if (!(wmh = XGetWMHints(dpy, c->win)))
1862 return;
1863 wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
1864 XSetWMHints(dpy, c->win, wmh);
1865 XFree(wmh);
1866 }
1867
1868 void
1869 showhide(Client *c)
1870 {
1871 if (!c)
1872 return;
1873 if (ISVISIBLE(c)) {
1874 /* show clients top down */
1875 XMoveWindow(dpy, c->win, c->x, c->y);
1876 if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1877 resize(c, c->x, c->y, c->w, c->h, c->bw, 0);
1878 showhide(c->snext);
1879 } else {
1880 /* hide clients bottom up */
1881 showhide(c->snext);
1882 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1883 }
1884 }
1885
1886 void
1887 sighup(int unused)
1888 {
1889 Arg a = {.i = 1};
1890 quit(&a);
1891 }
1892
1893 void
1894 sigterm(int unused)
1895 {
1896 Arg a = {.i = 0};
1897 quit(&a);
1898 }
1899
1900 void
1901 spawn(const Arg *arg)
1902 {
1903 struct sigaction sa;
1904
1905 if (arg->v == dmenu_launch || arg->v == dmenu_cmd)
1906 dmenumon[0] = '0' + selmon->num;
1907 if (fork() == 0) {
1908 if (dpy)
1909 close(ConnectionNumber(dpy));
1910 setsid();
1911
1912 sigemptyset(&sa.sa_mask);
1913 sa.sa_flags = 0;
1914 sa.sa_handler = SIG_DFL;
1915 sigaction(SIGCHLD, &sa, NULL);
1916
1917 execvp(((char **)arg->v)[0], (char **)arg->v);
1918 die("dwm: execvp '%s' failed:", ((char **)arg->v)[0]);
1919 }
1920 }
1921
1922 void
1923 tag(const Arg *arg)
1924 {
1925 if (selmon->sel && arg->ui & TAGMASK) {
1926 selmon->sel->tags = arg->ui & TAGMASK;
1927 setclienttagprop(selmon->sel);
1928 focus(NULL);
1929 arrange(selmon);
1930 warpmouse();
1931 }
1932 }
1933
1934 /*
1935 void
1936 tagmon(const Arg *arg)
1937 {
1938 if (!selmon->sel || !mons->next)
1939 return;
1940 sendmon(selmon->sel, dirtomon(arg->i));
1941 }
1942 */
1943
1944 void
1945 tile(Monitor *m)
1946 {
1947 unsigned int i, n, h, mw, my, ty, bw;
1948 Client *c;
1949
1950 for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1951 if (n == 0)
1952 return;
1953
1954 if (n == 1)
1955 bw = 0;
1956 else
1957 bw = borderpx;
1958 if (n > m->nmaster)
1959 mw = m->nmaster ? m->ww * m->mfact : 0;
1960 else
1961 mw = m->ww;
1962 for (i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1963 if (i < m->nmaster) {
1964 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1965 resizeclient(c, m->wx, m->wy + my, mw - 2*bw, h - 2*bw, bw);
1966 if (my + HEIGHT(c) < m->wh)
1967 my += HEIGHT(c);
1968 } else {
1969 h = (m->wh - ty) / (n - i);
1970 resizeclient(c, m->wx + mw, m->wy + ty, m->ww - mw - 2*bw, h - 2*bw, bw);
1971 if (ty + HEIGHT(c) < m->wh)
1972 ty += HEIGHT(c);
1973 }
1974 }
1975
1976 void
1977 togglebar(const Arg *arg)
1978 {
1979 selmon->showbar = !selmon->showbar;
1980 updatebarpos(selmon);
1981 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1982 arrange(selmon);
1983 }
1984
1985 void
1986 togglefloating(const Arg *arg)
1987 {
1988 if (!selmon->sel)
1989 return;
1990 if (selmon->sel->isfullscreen) /* no support for fullscreen windows */
1991 return;
1992 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1993 if (selmon->sel->isfloating)
1994 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1995 selmon->sel->w - 2 * (borderpx - selmon->sel->bw),
1996 selmon->sel->h - 2 * (borderpx - selmon->sel->bw),
1997 borderpx, 0);
1998
1999 arrange(selmon);
2000 }
2001
2002 void
2003 toggletag(const Arg *arg)
2004 {
2005 unsigned int newtags;
2006
2007 if (!selmon->sel)
2008 return;
2009 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
2010 if (newtags) {
2011 selmon->sel->tags = newtags;
2012 setclienttagprop(selmon->sel);
2013 focus(NULL);
2014 arrange(selmon);
2015 warpmouse();
2016 }
2017 }
2018
2019 void
2020 toggleview(const Arg *arg)
2021 {
2022 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
2023
2024 if (newtagset) {
2025 selmon->tagset[selmon->seltags] = newtagset;
2026 focus(NULL);
2027 arrange(selmon);
2028 warpmouse();
2029 }
2030 }
2031
2032 void
2033 unfocus(Client *c, int setfocus)
2034 {
2035 if (!c)
2036 return;
2037 grabbuttons(c, 0);
2038 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
2039 if (setfocus) {
2040 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
2041 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
2042 }
2043 }
2044
2045 void
2046 unmanage(Client *c, int destroyed)
2047 {
2048 Monitor *m = c->mon;
2049 XWindowChanges wc;
2050
2051 detach(c);
2052 detachstack(c);
2053
2054 if (!destroyed) {
2055 wc.border_width = c->oldbw;
2056 XGrabServer(dpy); /* avoid race conditions */
2057 XSetErrorHandler(xerrordummy);
2058 XSelectInput(dpy, c->win, NoEventMask);
2059 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
2060 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
2061 setclientstate(c, WithdrawnState);
2062 XSync(dpy, False);
2063 XSetErrorHandler(xerror);
2064 XUngrabServer(dpy);
2065 }
2066 free(c);
2067 focus(NULL);
2068 updateclientlist();
2069 arrange(m);
2070 if (m == selmon && m->sel)
2071 XWarpPointer(dpy, None, m->sel->win, 0, 0, 0, 0,
2072 m->sel->w/2, m->sel->h/2);
2073
2074 }
2075
2076 void
2077 unmapnotify(XEvent *e)
2078 {
2079 Client *c;
2080 XUnmapEvent *ev = &e->xunmap;
2081
2082 if ((c = wintoclient(ev->window))) {
2083 if (ev->send_event)
2084 setclientstate(c, WithdrawnState);
2085 else
2086 unmanage(c, 0);
2087 }
2088 }
2089
2090 void
2091 updatebars(void)
2092 {
2093 Monitor *m;
2094 XSetWindowAttributes wa = {
2095 .override_redirect = True,
2096 .background_pixmap = ParentRelative,
2097 .event_mask = ButtonPressMask|ExposureMask
2098 };
2099 XClassHint ch = {"dwm", "dwm"};
2100 for (m = mons; m; m = m->next) {
2101 if (m->barwin)
2102 continue;
2103 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
2104 CopyFromParent, DefaultVisual(dpy, screen),
2105 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
2106 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
2107 XMapRaised(dpy, m->barwin);
2108 XSetClassHint(dpy, m->barwin, &ch);
2109 }
2110 }
2111
2112 void
2113 updatebarpos(Monitor *m)
2114 {
2115 m->wy = m->my;
2116 m->wh = m->mh;
2117 if (m->showbar) {
2118 m->wh -= bh;
2119 m->by = m->topbar ? m->wy : m->wy + m->wh;
2120 m->wy = m->topbar ? m->wy + bh : m->wy;
2121 } else
2122 m->by = -bh;
2123 }
2124
2125 void
2126 updateclientlist(void)
2127 {
2128 Client *c;
2129 Monitor *m;
2130
2131 XDeleteProperty(dpy, root, netatom[NetClientList]);
2132 for (m = mons; m; m = m->next)
2133 for (c = m->clients; c; c = c->next)
2134 XChangeProperty(dpy, root, netatom[NetClientList],
2135 XA_WINDOW, 32, PropModeAppend,
2136 (unsigned char *) &(c->win), 1);
2137 }
2138
2139 int
2140 updategeom(void)
2141 {
2142 int dirty = 0;
2143
2144 #ifdef XINERAMA
2145 if (XineramaIsActive(dpy)) {
2146 int i, j, n, nn;
2147 Client *c;
2148 Monitor *m;
2149 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
2150 XineramaScreenInfo *unique = NULL;
2151
2152 for (n = 0, m = mons; m; m = m->next, n++);
2153 /* only consider unique geometries as separate screens */
2154 unique = ecalloc(nn, sizeof(XineramaScreenInfo));
2155 for (i = 0, j = 0; i < nn; i++)
2156 if (isuniquegeom(unique, j, &info[i]))
2157 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
2158 XFree(info);
2159 nn = j;
2160
2161 /* new monitors if nn > n */
2162 for (i = n; i < nn; i++) {
2163 for (m = mons; m && m->next; m = m->next);
2164 if (m)
2165 m->next = createmon();
2166 else
2167 mons = createmon();
2168 }
2169 for (i = 0, m = mons; i < nn && m; m = m->next, i++)
2170 if (i >= n
2171 || unique[i].x_org != m->mx || unique[i].y_org != m->my
2172 || unique[i].width != m->mw || unique[i].height != m->mh)
2173 {
2174 dirty = 1;
2175 m->num = i;
2176 m->mx = m->wx = unique[i].x_org;
2177 m->my = m->wy = unique[i].y_org;
2178 m->mw = m->ww = unique[i].width;
2179 m->mh = m->wh = unique[i].height;
2180 updatebarpos(m);
2181 }
2182 /* removed monitors if n > nn */
2183 for (i = nn; i < n; i++) {
2184 for (m = mons; m && m->next; m = m->next);
2185 while ((c = m->clients)) {
2186 dirty = 1;
2187 m->clients = c->next;
2188 detachstack(c);
2189 c->mon = mons;
2190 attach(c);
2191 attachstack(c);
2192 }
2193 if (m == selmon)
2194 selmon = mons;
2195 cleanupmon(m);
2196 }
2197 free(unique);
2198 } else
2199 #endif /* XINERAMA */
2200 { /* default monitor setup */
2201 if (!mons)
2202 mons = createmon();
2203 if (mons->mw != sw || mons->mh != sh) {
2204 dirty = 1;
2205 mons->mw = mons->ww = sw;
2206 mons->mh = mons->wh = sh;
2207 updatebarpos(mons);
2208 }
2209 }
2210 if (dirty) {
2211 selmon = mons;
2212 selmon = wintomon(root);
2213 }
2214 return dirty;
2215 }
2216
2217 void
2218 updatenumlockmask(void)
2219 {
2220 unsigned int i, j;
2221 XModifierKeymap *modmap;
2222
2223 numlockmask = 0;
2224 modmap = XGetModifierMapping(dpy);
2225 for (i = 0; i < 8; i++)
2226 for (j = 0; j < modmap->max_keypermod; j++)
2227 if (modmap->modifiermap[i * modmap->max_keypermod + j]
2228 == XKeysymToKeycode(dpy, XK_Num_Lock))
2229 numlockmask = (1 << i);
2230 XFreeModifiermap(modmap);
2231 }
2232
2233 void
2234 updatesizehints(Client *c)
2235 {
2236 long msize;
2237 XSizeHints size;
2238
2239 if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
2240 /* size is uninitialized, ensure that size.flags aren't used */
2241 size.flags = PSize;
2242 if (size.flags & PBaseSize) {
2243 c->basew = size.base_width;
2244 c->baseh = size.base_height;
2245 } else if (size.flags & PMinSize) {
2246 c->basew = size.min_width;
2247 c->baseh = size.min_height;
2248 } else
2249 c->basew = c->baseh = 0;
2250 if (size.flags & PResizeInc) {
2251 c->incw = size.width_inc;
2252 c->inch = size.height_inc;
2253 } else
2254 c->incw = c->inch = 0;
2255 if (size.flags & PMaxSize) {
2256 c->maxw = size.max_width;
2257 c->maxh = size.max_height;
2258 } else
2259 c->maxw = c->maxh = 0;
2260 if (size.flags & PMinSize) {
2261 c->minw = size.min_width;
2262 c->minh = size.min_height;
2263 } else if (size.flags & PBaseSize) {
2264 c->minw = size.base_width;
2265 c->minh = size.base_height;
2266 } else
2267 c->minw = c->minh = 0;
2268 if (size.flags & PAspect) {
2269 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2270 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2271 } else
2272 c->maxa = c->mina = 0.0;
2273 c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh);
2274 c->hintsvalid = 1;
2275 }
2276
2277 void
2278 updatestatus(void)
2279 {
2280 if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2281 strcpy(stext, "");
2282 drawbar(selmon);
2283 }
2284
2285 void
2286 updatetitle(Client *c)
2287 {
2288 if (!gettextprop(c->win, qubesatom[QubesVMName], c->vmname, sizeof c->vmname))
2289 snprintf(c->vmname, sizeof(c->vmname), "%s", gui_domain);
2290 if (!gettextprop(c->win, myatom[MyStatus], c->mystatus, sizeof c->mystatus))
2291 strcpy(c->mystatus, "");
2292 if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2293 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2294 if (c->name[0] == '\0') /* hack to mark broken clients */
2295 strcpy(c->name, broken);
2296 }
2297
2298 void
2299 updatewindowtype(Client *c)
2300 {
2301 Atom state = getatomprop(c, netatom[NetWMState]);
2302 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2303
2304 if (state == netatom[NetWMFullscreen])
2305 setfullscreen(c, 1);
2306 if (wtype == netatom[NetWMWindowTypeDialog])
2307 c->isfloating = 1;
2308 }
2309
2310 void
2311 updatewmhints(Client *c)
2312 {
2313 XWMHints *wmh;
2314
2315 if ((wmh = XGetWMHints(dpy, c->win))) {
2316 if (c == selmon->sel && wmh->flags & XUrgencyHint) {
2317 wmh->flags &= ~XUrgencyHint;
2318 XSetWMHints(dpy, c->win, wmh);
2319 } else
2320 c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
2321 if (wmh->flags & InputHint)
2322 c->neverfocus = !wmh->input;
2323 else
2324 c->neverfocus = 0;
2325 XFree(wmh);
2326 }
2327 }
2328
2329 void
2330 updatecentered(Client *c)
2331 {
2332 c->x = c->mon->mx + (c->mon->mw - WIDTH(c)) / 2;
2333 c->y = c->mon->my + (c->mon->mh - HEIGHT(c)) / 2;
2334 }
2335
2336 void
2337 updatetagprop(Client *c)
2338 {
2339 int format;
2340 unsigned long *data, n, extra;
2341 Monitor *m;
2342 Atom atom;
2343 if (XGetWindowProperty(dpy, c->win, netatom[NetClientInfo], 0L, 2L, False, XA_CARDINAL,
2344 &atom, &format, &n, &extra, (unsigned char **)&data) == Success && n == 2) {
2345 c->tags = *data;
2346 for (m = mons; m; m = m->next) {
2347 if (m->num == *(data+1)) {
2348 c->mon = m;
2349 break;
2350 }
2351 }
2352 }
2353 if (n > 0)
2354 XFree(data);
2355 setclienttagprop(c);
2356 }
2357
2358 void
2359 view(const Arg *arg)
2360 {
2361 if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2362 return;
2363 selmon->seltags ^= 1; /* toggle sel tagset */
2364 if (arg->ui & TAGMASK)
2365 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2366 focus(NULL);
2367 arrange(selmon);
2368 warpmouse();
2369 }
2370
2371 Client *
2372 wintoclient(Window w)
2373 {
2374 Client *c;
2375 Monitor *m;
2376
2377 for (m = mons; m; m = m->next)
2378 for (c = m->clients; c; c = c->next)
2379 if (c->win == w)
2380 return c;
2381 return NULL;
2382 }
2383
2384 Monitor *
2385 wintomon(Window w)
2386 {
2387 int x, y;
2388 Client *c;
2389 Monitor *m;
2390
2391 if (w == root && getrootptr(&x, &y))
2392 return recttomon(x, y, 1, 1);
2393 for (m = mons; m; m = m->next)
2394 if (w == m->barwin)
2395 return m;
2396 if ((c = wintoclient(w)))
2397 return c->mon;
2398 return selmon;
2399 }
2400
2401 /* There's no way to check accesses to destroyed windows, thus those cases are
2402 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2403 * default error handler, which may call exit. */
2404 int
2405 xerror(Display *dpy, XErrorEvent *ee)
2406 {
2407 if (ee->error_code == BadWindow
2408 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2409 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2410 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2411 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2412 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2413 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2414 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2415 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2416 return 0;
2417 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2418 ee->request_code, ee->error_code);
2419 return xerrorxlib(dpy, ee); /* may call exit */
2420 }
2421
2422 int
2423 xerrordummy(Display *dpy, XErrorEvent *ee)
2424 {
2425 return 0;
2426 }
2427
2428 /* Startup Error handler to check if another window manager
2429 * is already running. */
2430 int
2431 xerrorstart(Display *dpy, XErrorEvent *ee)
2432 {
2433 die("dwm: another window manager is already running");
2434 return -1;
2435 }
2436
2437 void
2438 zoom(const Arg *arg)
2439 {
2440 Client *c = selmon->sel;
2441
2442 if (!selmon->lt[selmon->sellt]->arrange || !c || c->isfloating)
2443 return;
2444 if (c == nexttiled(selmon->clients) && !(c = nexttiled(c->next)))
2445 return;
2446 pop(c);
2447 }
2448
2449 int
2450 main(int argc, char *argv[])
2451 {
2452 if (argc == 2 && !strcmp("-v", argv[1]))
2453 die("dwm-"VERSION);
2454 else if (argc != 1)
2455 die("usage: dwm [-v]");
2456 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2457 fputs("warning: no locale support\n", stderr);
2458 if (!(dpy = XOpenDisplay(NULL)))
2459 die("dwm: cannot open display");
2460 checkotherwm();
2461 setup();
2462 #ifdef __OpenBSD__
2463 if (pledge("stdio rpath proc exec", NULL) == -1)
2464 die("pledge");
2465 #endif /* __OpenBSD__ */
2466 scan();
2467 run();
2468 if(restart)
2469 execvp(argv[0], argv);
2470 cleanup();
2471 XCloseDisplay(dpy);
2472 return EXIT_SUCCESS;
2473 }