Xinqi Bao's Git

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