Xinqi Bao's Git

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