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