Xinqi Bao's Git

applied Ivan Delalande's NET_SUPPORTING_WM_CHECK patch for gtk3 compatibility
[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 }; /* color schemes */
64 enum { NetSupported, NetWMName, NetWMState, NetWMCheck,
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;
267 static Display *dpy;
268 static Drw *drw;
269 static Monitor *mons, *selmon;
270 static Window root, wmcheckwin;
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 < LENGTH(colors); i++)
487 free(scheme[i]);
488 XDestroyWindow(dpy, wmcheckwin);
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 Client *c;
569 XConfigureEvent *ev = &e->xconfigure;
570 int dirty;
571
572 /* TODO: updategeom handling sucks, needs to be simplified */
573 if (ev->window == root) {
574 dirty = (sw != ev->width || sh != ev->height);
575 sw = ev->width;
576 sh = ev->height;
577 if (updategeom() || dirty) {
578 drw_resize(drw, sw, bh);
579 updatebars();
580 for (m = mons; m; m = m->next) {
581 for (c = m->clients; c; c = c->next)
582 if (c->isfullscreen)
583 resizeclient(c, m->mx, m->my, m->mw, m->mh);
584 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
585 }
586 focus(NULL);
587 arrange(NULL);
588 }
589 }
590 }
591
592 void
593 configurerequest(XEvent *e)
594 {
595 Client *c;
596 Monitor *m;
597 XConfigureRequestEvent *ev = &e->xconfigurerequest;
598 XWindowChanges wc;
599
600 if ((c = wintoclient(ev->window))) {
601 if (ev->value_mask & CWBorderWidth)
602 c->bw = ev->border_width;
603 else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
604 m = c->mon;
605 if (ev->value_mask & CWX) {
606 c->oldx = c->x;
607 c->x = m->mx + ev->x;
608 }
609 if (ev->value_mask & CWY) {
610 c->oldy = c->y;
611 c->y = m->my + ev->y;
612 }
613 if (ev->value_mask & CWWidth) {
614 c->oldw = c->w;
615 c->w = ev->width;
616 }
617 if (ev->value_mask & CWHeight) {
618 c->oldh = c->h;
619 c->h = ev->height;
620 }
621 if ((c->x + c->w) > m->mx + m->mw && c->isfloating)
622 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
623 if ((c->y + c->h) > m->my + m->mh && c->isfloating)
624 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
625 if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
626 configure(c);
627 if (ISVISIBLE(c))
628 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
629 } else
630 configure(c);
631 } else {
632 wc.x = ev->x;
633 wc.y = ev->y;
634 wc.width = ev->width;
635 wc.height = ev->height;
636 wc.border_width = ev->border_width;
637 wc.sibling = ev->above;
638 wc.stack_mode = ev->detail;
639 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
640 }
641 XSync(dpy, False);
642 }
643
644 Monitor *
645 createmon(void)
646 {
647 Monitor *m;
648
649 m = ecalloc(1, sizeof(Monitor));
650 m->tagset[0] = m->tagset[1] = 1;
651 m->mfact = mfact;
652 m->nmaster = nmaster;
653 m->showbar = showbar;
654 m->topbar = topbar;
655 m->lt[0] = &layouts[0];
656 m->lt[1] = &layouts[1 % LENGTH(layouts)];
657 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
658 return m;
659 }
660
661 void
662 destroynotify(XEvent *e)
663 {
664 Client *c;
665 XDestroyWindowEvent *ev = &e->xdestroywindow;
666
667 if ((c = wintoclient(ev->window)))
668 unmanage(c, 1);
669 }
670
671 void
672 detach(Client *c)
673 {
674 Client **tc;
675
676 for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
677 *tc = c->next;
678 }
679
680 void
681 detachstack(Client *c)
682 {
683 Client **tc, *t;
684
685 for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
686 *tc = c->snext;
687
688 if (c == c->mon->sel) {
689 for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
690 c->mon->sel = t;
691 }
692 }
693
694 Monitor *
695 dirtomon(int dir)
696 {
697 Monitor *m = NULL;
698
699 if (dir > 0) {
700 if (!(m = selmon->next))
701 m = mons;
702 } else if (selmon == mons)
703 for (m = mons; m->next; m = m->next);
704 else
705 for (m = mons; m->next != selmon; m = m->next);
706 return m;
707 }
708
709 void
710 drawbar(Monitor *m)
711 {
712 int x, w, sw = 0;
713 int boxs = drw->fonts->h / 9;
714 int boxw = drw->fonts->h / 6 + 2;
715 unsigned int i, occ = 0, urg = 0;
716 Client *c;
717
718 /* draw status first so it can be overdrawn by tags later */
719 if (m == selmon) { /* status is only drawn on selected monitor */
720 drw_setscheme(drw, scheme[SchemeNorm]);
721 sw = TEXTW(stext) - lrpad + 2; /* 2px right padding */
722 drw_text(drw, m->ww - sw, 0, sw, bh, 0, stext, 0);
723 }
724
725 for (c = m->clients; c; c = c->next) {
726 occ |= c->tags;
727 if (c->isurgent)
728 urg |= c->tags;
729 }
730 x = 0;
731 for (i = 0; i < LENGTH(tags); i++) {
732 w = TEXTW(tags[i]);
733 drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]);
734 drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i);
735 if (occ & 1 << i)
736 drw_rect(drw, x + boxs, boxs, boxw, boxw,
737 m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
738 urg & 1 << i);
739 x += w;
740 }
741 w = blw = TEXTW(m->ltsymbol);
742 drw_setscheme(drw, scheme[SchemeNorm]);
743 x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0);
744
745 if ((w = m->ww - sw - x) > bh) {
746 if (m->sel) {
747 drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]);
748 drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->name, 0);
749 if (m->sel->isfloating)
750 drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
751 } else {
752 drw_setscheme(drw, scheme[SchemeNorm]);
753 drw_rect(drw, x, 0, w, bh, 1, 1);
754 }
755 }
756 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
757 }
758
759 void
760 drawbars(void)
761 {
762 Monitor *m;
763
764 for (m = mons; m; m = m->next)
765 drawbar(m);
766 }
767
768 void
769 enternotify(XEvent *e)
770 {
771 Client *c;
772 Monitor *m;
773 XCrossingEvent *ev = &e->xcrossing;
774
775 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
776 return;
777 c = wintoclient(ev->window);
778 m = c ? c->mon : wintomon(ev->window);
779 if (m != selmon) {
780 unfocus(selmon->sel, 1);
781 selmon = m;
782 } else if (!c || c == selmon->sel)
783 return;
784 focus(c);
785 }
786
787 void
788 expose(XEvent *e)
789 {
790 Monitor *m;
791 XExposeEvent *ev = &e->xexpose;
792
793 if (ev->count == 0 && (m = wintomon(ev->window)))
794 drawbar(m);
795 }
796
797 void
798 focus(Client *c)
799 {
800 if (!c || !ISVISIBLE(c))
801 for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
802 /* was if (selmon->sel) */
803 if (selmon->sel && selmon->sel != c)
804 unfocus(selmon->sel, 0);
805 if (c) {
806 if (c->mon != selmon)
807 selmon = c->mon;
808 if (c->isurgent)
809 clearurgent(c);
810 detachstack(c);
811 attachstack(c);
812 grabbuttons(c, 1);
813 XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel);
814 setfocus(c);
815 } else {
816 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
817 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
818 }
819 selmon->sel = c;
820 drawbars();
821 }
822
823 /* there are some broken focus acquiring clients */
824 void
825 focusin(XEvent *e)
826 {
827 XFocusChangeEvent *ev = &e->xfocus;
828
829 if (selmon->sel && ev->window != selmon->sel->win)
830 setfocus(selmon->sel);
831 }
832
833 void
834 focusmon(const Arg *arg)
835 {
836 Monitor *m;
837
838 if (!mons->next)
839 return;
840 if ((m = dirtomon(arg->i)) == selmon)
841 return;
842 unfocus(selmon->sel, 0); /* s/1/0/ fixes input focus issues
843 in gedit and anjuta */
844 selmon = m;
845 focus(NULL);
846 }
847
848 void
849 focusstack(const Arg *arg)
850 {
851 Client *c = NULL, *i;
852
853 if (!selmon->sel)
854 return;
855 if (arg->i > 0) {
856 for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
857 if (!c)
858 for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
859 } else {
860 for (i = selmon->clients; i != selmon->sel; i = i->next)
861 if (ISVISIBLE(i))
862 c = i;
863 if (!c)
864 for (; i; i = i->next)
865 if (ISVISIBLE(i))
866 c = i;
867 }
868 if (c) {
869 focus(c);
870 restack(selmon);
871 }
872 }
873
874 Atom
875 getatomprop(Client *c, Atom prop)
876 {
877 int di;
878 unsigned long dl;
879 unsigned char *p = NULL;
880 Atom da, atom = None;
881
882 if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
883 &da, &di, &dl, &dl, &p) == Success && p) {
884 atom = *(Atom *)p;
885 XFree(p);
886 }
887 return atom;
888 }
889
890 int
891 getrootptr(int *x, int *y)
892 {
893 int di;
894 unsigned int dui;
895 Window dummy;
896
897 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
898 }
899
900 long
901 getstate(Window w)
902 {
903 int format;
904 long result = -1;
905 unsigned char *p = NULL;
906 unsigned long n, extra;
907 Atom real;
908
909 if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
910 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
911 return -1;
912 if (n != 0)
913 result = *p;
914 XFree(p);
915 return result;
916 }
917
918 int
919 gettextprop(Window w, Atom atom, char *text, unsigned int size)
920 {
921 char **list = NULL;
922 int n;
923 XTextProperty name;
924
925 if (!text || size == 0)
926 return 0;
927 text[0] = '\0';
928 XGetTextProperty(dpy, w, &name, atom);
929 if (!name.nitems)
930 return 0;
931 if (name.encoding == XA_STRING)
932 strncpy(text, (char *)name.value, size - 1);
933 else {
934 if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
935 strncpy(text, *list, size - 1);
936 XFreeStringList(list);
937 }
938 }
939 text[size - 1] = '\0';
940 XFree(name.value);
941 return 1;
942 }
943
944 void
945 grabbuttons(Client *c, int focused)
946 {
947 updatenumlockmask();
948 {
949 unsigned int i, j;
950 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
951 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
952 if (focused) {
953 for (i = 0; i < LENGTH(buttons); i++)
954 if (buttons[i].click == ClkClientWin)
955 for (j = 0; j < LENGTH(modifiers); j++)
956 XGrabButton(dpy, buttons[i].button,
957 buttons[i].mask | modifiers[j],
958 c->win, False, BUTTONMASK,
959 GrabModeAsync, GrabModeSync, None, None);
960 } else
961 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
962 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
963 }
964 }
965
966 void
967 grabkeys(void)
968 {
969 updatenumlockmask();
970 {
971 unsigned int i, j;
972 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
973 KeyCode code;
974
975 XUngrabKey(dpy, AnyKey, AnyModifier, root);
976 for (i = 0; i < LENGTH(keys); i++)
977 if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
978 for (j = 0; j < LENGTH(modifiers); j++)
979 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
980 True, GrabModeAsync, GrabModeAsync);
981 }
982 }
983
984 void
985 incnmaster(const Arg *arg)
986 {
987 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
988 arrange(selmon);
989 }
990
991 #ifdef XINERAMA
992 static int
993 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
994 {
995 while (n--)
996 if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
997 && unique[n].width == info->width && unique[n].height == info->height)
998 return 0;
999 return 1;
1000 }
1001 #endif /* XINERAMA */
1002
1003 void
1004 keypress(XEvent *e)
1005 {
1006 unsigned int i;
1007 KeySym keysym;
1008 XKeyEvent *ev;
1009
1010 ev = &e->xkey;
1011 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1012 for (i = 0; i < LENGTH(keys); i++)
1013 if (keysym == keys[i].keysym
1014 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1015 && keys[i].func)
1016 keys[i].func(&(keys[i].arg));
1017 }
1018
1019 void
1020 killclient(const Arg *arg)
1021 {
1022 if (!selmon->sel)
1023 return;
1024 if (!sendevent(selmon->sel, wmatom[WMDelete])) {
1025 XGrabServer(dpy);
1026 XSetErrorHandler(xerrordummy);
1027 XSetCloseDownMode(dpy, DestroyAll);
1028 XKillClient(dpy, selmon->sel->win);
1029 XSync(dpy, False);
1030 XSetErrorHandler(xerror);
1031 XUngrabServer(dpy);
1032 }
1033 }
1034
1035 void
1036 manage(Window w, XWindowAttributes *wa)
1037 {
1038 Client *c, *t = NULL;
1039 Window trans = None;
1040 XWindowChanges wc;
1041
1042 c = ecalloc(1, sizeof(Client));
1043 c->win = w;
1044 /* geometry */
1045 c->x = c->oldx = wa->x;
1046 c->y = c->oldy = wa->y;
1047 c->w = c->oldw = wa->width;
1048 c->h = c->oldh = wa->height;
1049 c->oldbw = wa->border_width;
1050
1051 updatetitle(c);
1052 if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1053 c->mon = t->mon;
1054 c->tags = t->tags;
1055 } else {
1056 c->mon = selmon;
1057 applyrules(c);
1058 }
1059
1060 if (c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1061 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1062 if (c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1063 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1064 c->x = MAX(c->x, c->mon->mx);
1065 /* only fix client y-offset, if the client center might cover the bar */
1066 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1067 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1068 c->bw = borderpx;
1069
1070 wc.border_width = c->bw;
1071 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1072 XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel);
1073 configure(c); /* propagates border_width, if size doesn't change */
1074 updatewindowtype(c);
1075 updatesizehints(c);
1076 updatewmhints(c);
1077 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1078 grabbuttons(c, 0);
1079 if (!c->isfloating)
1080 c->isfloating = c->oldstate = trans != None || c->isfixed;
1081 if (c->isfloating)
1082 XRaiseWindow(dpy, c->win);
1083 attach(c);
1084 attachstack(c);
1085 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1086 (unsigned char *) &(c->win), 1);
1087 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1088 setclientstate(c, NormalState);
1089 if (c->mon == selmon)
1090 unfocus(selmon->sel, 0);
1091 c->mon->sel = c;
1092 arrange(c->mon);
1093 XMapWindow(dpy, c->win);
1094 focus(NULL);
1095 }
1096
1097 void
1098 mappingnotify(XEvent *e)
1099 {
1100 XMappingEvent *ev = &e->xmapping;
1101
1102 XRefreshKeyboardMapping(ev);
1103 if (ev->request == MappingKeyboard)
1104 grabkeys();
1105 }
1106
1107 void
1108 maprequest(XEvent *e)
1109 {
1110 static XWindowAttributes wa;
1111 XMapRequestEvent *ev = &e->xmaprequest;
1112
1113 if (!XGetWindowAttributes(dpy, ev->window, &wa))
1114 return;
1115 if (wa.override_redirect)
1116 return;
1117 if (!wintoclient(ev->window))
1118 manage(ev->window, &wa);
1119 }
1120
1121 void
1122 monocle(Monitor *m)
1123 {
1124 unsigned int n = 0;
1125 Client *c;
1126
1127 for (c = m->clients; c; c = c->next)
1128 if (ISVISIBLE(c))
1129 n++;
1130 if (n > 0) /* override layout symbol */
1131 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1132 for (c = nexttiled(m->clients); c; c = nexttiled(c->next))
1133 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
1134 }
1135
1136 void
1137 motionnotify(XEvent *e)
1138 {
1139 static Monitor *mon = NULL;
1140 Monitor *m;
1141 XMotionEvent *ev = &e->xmotion;
1142
1143 if (ev->window != root)
1144 return;
1145 if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1146 unfocus(selmon->sel, 1);
1147 selmon = m;
1148 focus(NULL);
1149 }
1150 mon = m;
1151 }
1152
1153 void
1154 movemouse(const Arg *arg)
1155 {
1156 int x, y, ocx, ocy, nx, ny;
1157 Client *c;
1158 Monitor *m;
1159 XEvent ev;
1160 Time lasttime = 0;
1161
1162 if (!(c = selmon->sel))
1163 return;
1164 if (c->isfullscreen) /* no support moving fullscreen windows by mouse */
1165 return;
1166 restack(selmon);
1167 ocx = c->x;
1168 ocy = c->y;
1169 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1170 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1171 return;
1172 if (!getrootptr(&x, &y))
1173 return;
1174 do {
1175 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1176 switch(ev.type) {
1177 case ConfigureRequest:
1178 case Expose:
1179 case MapRequest:
1180 handler[ev.type](&ev);
1181 break;
1182 case MotionNotify:
1183 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1184 continue;
1185 lasttime = ev.xmotion.time;
1186
1187 nx = ocx + (ev.xmotion.x - x);
1188 ny = ocy + (ev.xmotion.y - y);
1189 if (nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1190 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1191 if (abs(selmon->wx - nx) < snap)
1192 nx = selmon->wx;
1193 else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1194 nx = selmon->wx + selmon->ww - WIDTH(c);
1195 if (abs(selmon->wy - ny) < snap)
1196 ny = selmon->wy;
1197 else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1198 ny = selmon->wy + selmon->wh - HEIGHT(c);
1199 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1200 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1201 togglefloating(NULL);
1202 }
1203 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1204 resize(c, nx, ny, c->w, c->h, 1);
1205 break;
1206 }
1207 } while (ev.type != ButtonRelease);
1208 XUngrabPointer(dpy, CurrentTime);
1209 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1210 sendmon(c, m);
1211 selmon = m;
1212 focus(NULL);
1213 }
1214 }
1215
1216 Client *
1217 nexttiled(Client *c)
1218 {
1219 for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1220 return c;
1221 }
1222
1223 void
1224 pop(Client *c)
1225 {
1226 detach(c);
1227 attach(c);
1228 focus(c);
1229 arrange(c->mon);
1230 }
1231
1232 void
1233 propertynotify(XEvent *e)
1234 {
1235 Client *c;
1236 Window trans;
1237 XPropertyEvent *ev = &e->xproperty;
1238
1239 if ((ev->window == root) && (ev->atom == XA_WM_NAME))
1240 updatestatus();
1241 else if (ev->state == PropertyDelete)
1242 return; /* ignore */
1243 else if ((c = wintoclient(ev->window))) {
1244 switch(ev->atom) {
1245 default: break;
1246 case XA_WM_TRANSIENT_FOR:
1247 if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1248 (c->isfloating = (wintoclient(trans)) != NULL))
1249 arrange(c->mon);
1250 break;
1251 case XA_WM_NORMAL_HINTS:
1252 updatesizehints(c);
1253 break;
1254 case XA_WM_HINTS:
1255 updatewmhints(c);
1256 drawbars();
1257 break;
1258 }
1259 if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1260 updatetitle(c);
1261 if (c == c->mon->sel)
1262 drawbar(c->mon);
1263 }
1264 if (ev->atom == netatom[NetWMWindowType])
1265 updatewindowtype(c);
1266 }
1267 }
1268
1269 void
1270 quit(const Arg *arg)
1271 {
1272 running = 0;
1273 }
1274
1275 Monitor *
1276 recttomon(int x, int y, int w, int h)
1277 {
1278 Monitor *m, *r = selmon;
1279 int a, area = 0;
1280
1281 for (m = mons; m; m = m->next)
1282 if ((a = INTERSECT(x, y, w, h, m)) > area) {
1283 area = a;
1284 r = m;
1285 }
1286 return r;
1287 }
1288
1289 void
1290 resize(Client *c, int x, int y, int w, int h, int interact)
1291 {
1292 if (applysizehints(c, &x, &y, &w, &h, interact))
1293 resizeclient(c, x, y, w, h);
1294 }
1295
1296 void
1297 resizeclient(Client *c, int x, int y, int w, int h)
1298 {
1299 XWindowChanges wc;
1300
1301 c->oldx = c->x; c->x = wc.x = x;
1302 c->oldy = c->y; c->y = wc.y = y;
1303 c->oldw = c->w; c->w = wc.width = w;
1304 c->oldh = c->h; c->h = wc.height = h;
1305 wc.border_width = c->bw;
1306 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1307 configure(c);
1308 XSync(dpy, False);
1309 }
1310
1311 void
1312 resizemouse(const Arg *arg)
1313 {
1314 int ocx, ocy, nw, nh;
1315 Client *c;
1316 Monitor *m;
1317 XEvent ev;
1318 Time lasttime = 0;
1319
1320 if (!(c = selmon->sel))
1321 return;
1322 if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1323 return;
1324 restack(selmon);
1325 ocx = c->x;
1326 ocy = c->y;
1327 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1328 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1329 return;
1330 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1331 do {
1332 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1333 switch(ev.type) {
1334 case ConfigureRequest:
1335 case Expose:
1336 case MapRequest:
1337 handler[ev.type](&ev);
1338 break;
1339 case MotionNotify:
1340 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1341 continue;
1342 lasttime = ev.xmotion.time;
1343
1344 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1345 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1346 if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1347 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1348 {
1349 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1350 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1351 togglefloating(NULL);
1352 }
1353 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1354 resize(c, c->x, c->y, nw, nh, 1);
1355 break;
1356 }
1357 } while (ev.type != ButtonRelease);
1358 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1359 XUngrabPointer(dpy, CurrentTime);
1360 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1361 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1362 sendmon(c, m);
1363 selmon = m;
1364 focus(NULL);
1365 }
1366 }
1367
1368 void
1369 restack(Monitor *m)
1370 {
1371 Client *c;
1372 XEvent ev;
1373 XWindowChanges wc;
1374
1375 drawbar(m);
1376 if (!m->sel)
1377 return;
1378 if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
1379 XRaiseWindow(dpy, m->sel->win);
1380 if (m->lt[m->sellt]->arrange) {
1381 wc.stack_mode = Below;
1382 wc.sibling = m->barwin;
1383 for (c = m->stack; c; c = c->snext)
1384 if (!c->isfloating && ISVISIBLE(c)) {
1385 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1386 wc.sibling = c->win;
1387 }
1388 }
1389 XSync(dpy, False);
1390 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1391 }
1392
1393 void
1394 run(void)
1395 {
1396 XEvent ev;
1397 /* main event loop */
1398 XSync(dpy, False);
1399 while (running && !XNextEvent(dpy, &ev))
1400 if (handler[ev.type])
1401 handler[ev.type](&ev); /* call handler */
1402 }
1403
1404 void
1405 scan(void)
1406 {
1407 unsigned int i, num;
1408 Window d1, d2, *wins = NULL;
1409 XWindowAttributes wa;
1410
1411 if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1412 for (i = 0; i < num; i++) {
1413 if (!XGetWindowAttributes(dpy, wins[i], &wa)
1414 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1415 continue;
1416 if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1417 manage(wins[i], &wa);
1418 }
1419 for (i = 0; i < num; i++) { /* now the transients */
1420 if (!XGetWindowAttributes(dpy, wins[i], &wa))
1421 continue;
1422 if (XGetTransientForHint(dpy, wins[i], &d1)
1423 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1424 manage(wins[i], &wa);
1425 }
1426 if (wins)
1427 XFree(wins);
1428 }
1429 }
1430
1431 void
1432 sendmon(Client *c, Monitor *m)
1433 {
1434 if (c->mon == m)
1435 return;
1436 unfocus(c, 1);
1437 detach(c);
1438 detachstack(c);
1439 c->mon = m;
1440 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1441 attach(c);
1442 attachstack(c);
1443 focus(NULL);
1444 arrange(NULL);
1445 }
1446
1447 void
1448 setclientstate(Client *c, long state)
1449 {
1450 long data[] = { state, None };
1451
1452 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1453 PropModeReplace, (unsigned char *)data, 2);
1454 }
1455
1456 int
1457 sendevent(Client *c, Atom proto)
1458 {
1459 int n;
1460 Atom *protocols;
1461 int exists = 0;
1462 XEvent ev;
1463
1464 if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1465 while (!exists && n--)
1466 exists = protocols[n] == proto;
1467 XFree(protocols);
1468 }
1469 if (exists) {
1470 ev.type = ClientMessage;
1471 ev.xclient.window = c->win;
1472 ev.xclient.message_type = wmatom[WMProtocols];
1473 ev.xclient.format = 32;
1474 ev.xclient.data.l[0] = proto;
1475 ev.xclient.data.l[1] = CurrentTime;
1476 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1477 }
1478 return exists;
1479 }
1480
1481 void
1482 setfocus(Client *c)
1483 {
1484 if (!c->neverfocus) {
1485 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1486 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1487 XA_WINDOW, 32, PropModeReplace,
1488 (unsigned char *) &(c->win), 1);
1489 }
1490 sendevent(c, wmatom[WMTakeFocus]);
1491 }
1492
1493 void
1494 setfullscreen(Client *c, int fullscreen)
1495 {
1496 if (fullscreen && !c->isfullscreen) {
1497 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1498 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1499 c->isfullscreen = 1;
1500 c->oldstate = c->isfloating;
1501 c->oldbw = c->bw;
1502 c->bw = 0;
1503 c->isfloating = 1;
1504 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1505 XRaiseWindow(dpy, c->win);
1506 } else if (!fullscreen && c->isfullscreen){
1507 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1508 PropModeReplace, (unsigned char*)0, 0);
1509 c->isfullscreen = 0;
1510 c->isfloating = c->oldstate;
1511 c->bw = c->oldbw;
1512 c->x = c->oldx;
1513 c->y = c->oldy;
1514 c->w = c->oldw;
1515 c->h = c->oldh;
1516 resizeclient(c, c->x, c->y, c->w, c->h);
1517 arrange(c->mon);
1518 }
1519 }
1520
1521 void
1522 setlayout(const Arg *arg)
1523 {
1524 if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1525 selmon->sellt ^= 1;
1526 if (arg && arg->v)
1527 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1528 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1529 if (selmon->sel)
1530 arrange(selmon);
1531 else
1532 drawbar(selmon);
1533 }
1534
1535 /* arg > 1.0 will set mfact absolutly */
1536 void
1537 setmfact(const Arg *arg)
1538 {
1539 float f;
1540
1541 if (!arg || !selmon->lt[selmon->sellt]->arrange)
1542 return;
1543 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1544 if (f < 0.1 || f > 0.9)
1545 return;
1546 selmon->mfact = f;
1547 arrange(selmon);
1548 }
1549
1550 void
1551 setup(void)
1552 {
1553 int i;
1554 XSetWindowAttributes wa;
1555 Atom utf8string;
1556
1557 /* clean up any zombies immediately */
1558 sigchld(0);
1559
1560 /* init screen */
1561 screen = DefaultScreen(dpy);
1562 sw = DisplayWidth(dpy, screen);
1563 sh = DisplayHeight(dpy, screen);
1564 root = RootWindow(dpy, screen);
1565 drw = drw_create(dpy, screen, root, sw, sh);
1566 if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
1567 die("no fonts could be loaded.");
1568 lrpad = drw->fonts->h;
1569 bh = drw->fonts->h + 2;
1570 updategeom();
1571 /* init atoms */
1572 utf8string = XInternAtom(dpy, "UTF8_STRING", False);
1573 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1574 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1575 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1576 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1577 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1578 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1579 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1580 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1581 netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
1582 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1583 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1584 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1585 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1586 /* init cursors */
1587 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1588 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1589 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1590 /* init appearance */
1591 scheme = ecalloc(LENGTH(colors), sizeof(Scm));
1592 for (i = 0; i < LENGTH(colors); i++)
1593 scheme[i] = drw_scm_create(drw, colors[i], 3);
1594 /* init bars */
1595 updatebars();
1596 updatestatus();
1597 /* supporting window for NetWMCheck */
1598 wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
1599 XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
1600 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1601 XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8,
1602 PropModeReplace, (unsigned char *) "dwm", 4);
1603 XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32,
1604 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1605 /* EWMH support per view */
1606 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1607 PropModeReplace, (unsigned char *) netatom, NetLast);
1608 XDeleteProperty(dpy, root, netatom[NetClientList]);
1609 /* select for events */
1610 wa.cursor = cursor[CurNormal]->cursor;
1611 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
1612 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1613 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1614 XSelectInput(dpy, root, wa.event_mask);
1615 grabkeys();
1616 focus(NULL);
1617 }
1618
1619 void
1620 showhide(Client *c)
1621 {
1622 if (!c)
1623 return;
1624 if (ISVISIBLE(c)) {
1625 /* show clients top down */
1626 XMoveWindow(dpy, c->win, c->x, c->y);
1627 if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1628 resize(c, c->x, c->y, c->w, c->h, 0);
1629 showhide(c->snext);
1630 } else {
1631 /* hide clients bottom up */
1632 showhide(c->snext);
1633 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1634 }
1635 }
1636
1637 void
1638 sigchld(int unused)
1639 {
1640 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1641 die("can't install SIGCHLD handler:");
1642 while (0 < waitpid(-1, NULL, WNOHANG));
1643 }
1644
1645 void
1646 spawn(const Arg *arg)
1647 {
1648 if (arg->v == dmenucmd)
1649 dmenumon[0] = '0' + selmon->num;
1650 if (fork() == 0) {
1651 if (dpy)
1652 close(ConnectionNumber(dpy));
1653 setsid();
1654 execvp(((char **)arg->v)[0], (char **)arg->v);
1655 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1656 perror(" failed");
1657 exit(EXIT_SUCCESS);
1658 }
1659 }
1660
1661 void
1662 tag(const Arg *arg)
1663 {
1664 if (selmon->sel && arg->ui & TAGMASK) {
1665 selmon->sel->tags = arg->ui & TAGMASK;
1666 focus(NULL);
1667 arrange(selmon);
1668 }
1669 }
1670
1671 void
1672 tagmon(const Arg *arg)
1673 {
1674 if (!selmon->sel || !mons->next)
1675 return;
1676 sendmon(selmon->sel, dirtomon(arg->i));
1677 }
1678
1679 void
1680 tile(Monitor *m)
1681 {
1682 unsigned int i, n, h, mw, my, ty;
1683 Client *c;
1684
1685 for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1686 if (n == 0)
1687 return;
1688
1689 if (n > m->nmaster)
1690 mw = m->nmaster ? m->ww * m->mfact : 0;
1691 else
1692 mw = m->ww;
1693 for (i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1694 if (i < m->nmaster) {
1695 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1696 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), 0);
1697 my += HEIGHT(c);
1698 } else {
1699 h = (m->wh - ty) / (n - i);
1700 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), 0);
1701 ty += HEIGHT(c);
1702 }
1703 }
1704
1705 void
1706 togglebar(const Arg *arg)
1707 {
1708 selmon->showbar = !selmon->showbar;
1709 updatebarpos(selmon);
1710 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1711 arrange(selmon);
1712 }
1713
1714 void
1715 togglefloating(const Arg *arg)
1716 {
1717 if (!selmon->sel)
1718 return;
1719 if (selmon->sel->isfullscreen) /* no support for fullscreen windows */
1720 return;
1721 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1722 if (selmon->sel->isfloating)
1723 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1724 selmon->sel->w, selmon->sel->h, 0);
1725 arrange(selmon);
1726 }
1727
1728 void
1729 toggletag(const Arg *arg)
1730 {
1731 unsigned int newtags;
1732
1733 if (!selmon->sel)
1734 return;
1735 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1736 if (newtags) {
1737 selmon->sel->tags = newtags;
1738 focus(NULL);
1739 arrange(selmon);
1740 }
1741 }
1742
1743 void
1744 toggleview(const Arg *arg)
1745 {
1746 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1747
1748 if (newtagset) {
1749 selmon->tagset[selmon->seltags] = newtagset;
1750 focus(NULL);
1751 arrange(selmon);
1752 }
1753 }
1754
1755 void
1756 unfocus(Client *c, int setfocus)
1757 {
1758 if (!c)
1759 return;
1760 grabbuttons(c, 0);
1761 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
1762 if (setfocus) {
1763 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1764 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1765 }
1766 }
1767
1768 void
1769 unmanage(Client *c, int destroyed)
1770 {
1771 Monitor *m = c->mon;
1772 XWindowChanges wc;
1773
1774 /* The server grab construct avoids race conditions. */
1775 detach(c);
1776 detachstack(c);
1777 if (!destroyed) {
1778 wc.border_width = c->oldbw;
1779 XGrabServer(dpy);
1780 XSetErrorHandler(xerrordummy);
1781 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1782 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1783 setclientstate(c, WithdrawnState);
1784 XSync(dpy, False);
1785 XSetErrorHandler(xerror);
1786 XUngrabServer(dpy);
1787 }
1788 free(c);
1789 focus(NULL);
1790 updateclientlist();
1791 arrange(m);
1792 }
1793
1794 void
1795 unmapnotify(XEvent *e)
1796 {
1797 Client *c;
1798 XUnmapEvent *ev = &e->xunmap;
1799
1800 if ((c = wintoclient(ev->window))) {
1801 if (ev->send_event)
1802 setclientstate(c, WithdrawnState);
1803 else
1804 unmanage(c, 0);
1805 }
1806 }
1807
1808 void
1809 updatebars(void)
1810 {
1811 Monitor *m;
1812 XSetWindowAttributes wa = {
1813 .override_redirect = True,
1814 .background_pixmap = ParentRelative,
1815 .event_mask = ButtonPressMask|ExposureMask
1816 };
1817 for (m = mons; m; m = m->next) {
1818 if (m->barwin)
1819 continue;
1820 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1821 CopyFromParent, DefaultVisual(dpy, screen),
1822 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1823 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
1824 XMapRaised(dpy, m->barwin);
1825 }
1826 }
1827
1828 void
1829 updatebarpos(Monitor *m)
1830 {
1831 m->wy = m->my;
1832 m->wh = m->mh;
1833 if (m->showbar) {
1834 m->wh -= bh;
1835 m->by = m->topbar ? m->wy : m->wy + m->wh;
1836 m->wy = m->topbar ? m->wy + bh : m->wy;
1837 } else
1838 m->by = -bh;
1839 }
1840
1841 void
1842 updateclientlist()
1843 {
1844 Client *c;
1845 Monitor *m;
1846
1847 XDeleteProperty(dpy, root, netatom[NetClientList]);
1848 for (m = mons; m; m = m->next)
1849 for (c = m->clients; c; c = c->next)
1850 XChangeProperty(dpy, root, netatom[NetClientList],
1851 XA_WINDOW, 32, PropModeAppend,
1852 (unsigned char *) &(c->win), 1);
1853 }
1854
1855 int
1856 updategeom(void)
1857 {
1858 int dirty = 0;
1859
1860 #ifdef XINERAMA
1861 if (XineramaIsActive(dpy)) {
1862 int i, j, n, nn;
1863 Client *c;
1864 Monitor *m;
1865 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1866 XineramaScreenInfo *unique = NULL;
1867
1868 for (n = 0, m = mons; m; m = m->next, n++);
1869 /* only consider unique geometries as separate screens */
1870 unique = ecalloc(nn, sizeof(XineramaScreenInfo));
1871 for (i = 0, j = 0; i < nn; i++)
1872 if (isuniquegeom(unique, j, &info[i]))
1873 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1874 XFree(info);
1875 nn = j;
1876 if (n <= nn) {
1877 for (i = 0; i < (nn - n); i++) { /* new monitors available */
1878 for (m = mons; m && m->next; m = m->next);
1879 if (m)
1880 m->next = createmon();
1881 else
1882 mons = createmon();
1883 }
1884 for (i = 0, m = mons; i < nn && m; m = m->next, i++)
1885 if (i >= n
1886 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1887 || unique[i].width != m->mw || unique[i].height != m->mh))
1888 {
1889 dirty = 1;
1890 m->num = i;
1891 m->mx = m->wx = unique[i].x_org;
1892 m->my = m->wy = unique[i].y_org;
1893 m->mw = m->ww = unique[i].width;
1894 m->mh = m->wh = unique[i].height;
1895 updatebarpos(m);
1896 }
1897 } else {
1898 /* less monitors available nn < n */
1899 for (i = nn; i < n; i++) {
1900 for (m = mons; m && m->next; m = m->next);
1901 while (m->clients) {
1902 dirty = 1;
1903 c = m->clients;
1904 m->clients = c->next;
1905 detachstack(c);
1906 c->mon = mons;
1907 attach(c);
1908 attachstack(c);
1909 }
1910 if (m == selmon)
1911 selmon = mons;
1912 cleanupmon(m);
1913 }
1914 }
1915 free(unique);
1916 } else
1917 #endif /* XINERAMA */
1918 /* default monitor setup */
1919 {
1920 if (!mons)
1921 mons = createmon();
1922 if (mons->mw != sw || mons->mh != sh) {
1923 dirty = 1;
1924 mons->mw = mons->ww = sw;
1925 mons->mh = mons->wh = sh;
1926 updatebarpos(mons);
1927 }
1928 }
1929 if (dirty) {
1930 selmon = mons;
1931 selmon = wintomon(root);
1932 }
1933 return dirty;
1934 }
1935
1936 void
1937 updatenumlockmask(void)
1938 {
1939 unsigned int i, j;
1940 XModifierKeymap *modmap;
1941
1942 numlockmask = 0;
1943 modmap = XGetModifierMapping(dpy);
1944 for (i = 0; i < 8; i++)
1945 for (j = 0; j < modmap->max_keypermod; j++)
1946 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1947 == XKeysymToKeycode(dpy, XK_Num_Lock))
1948 numlockmask = (1 << i);
1949 XFreeModifiermap(modmap);
1950 }
1951
1952 void
1953 updatesizehints(Client *c)
1954 {
1955 long msize;
1956 XSizeHints size;
1957
1958 if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
1959 /* size is uninitialized, ensure that size.flags aren't used */
1960 size.flags = PSize;
1961 if (size.flags & PBaseSize) {
1962 c->basew = size.base_width;
1963 c->baseh = size.base_height;
1964 } else if (size.flags & PMinSize) {
1965 c->basew = size.min_width;
1966 c->baseh = size.min_height;
1967 } else
1968 c->basew = c->baseh = 0;
1969 if (size.flags & PResizeInc) {
1970 c->incw = size.width_inc;
1971 c->inch = size.height_inc;
1972 } else
1973 c->incw = c->inch = 0;
1974 if (size.flags & PMaxSize) {
1975 c->maxw = size.max_width;
1976 c->maxh = size.max_height;
1977 } else
1978 c->maxw = c->maxh = 0;
1979 if (size.flags & PMinSize) {
1980 c->minw = size.min_width;
1981 c->minh = size.min_height;
1982 } else if (size.flags & PBaseSize) {
1983 c->minw = size.base_width;
1984 c->minh = size.base_height;
1985 } else
1986 c->minw = c->minh = 0;
1987 if (size.flags & PAspect) {
1988 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
1989 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
1990 } else
1991 c->maxa = c->mina = 0.0;
1992 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
1993 && c->maxw == c->minw && c->maxh == c->minh);
1994 }
1995
1996 void
1997 updatetitle(Client *c)
1998 {
1999 if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2000 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2001 if (c->name[0] == '\0') /* hack to mark broken clients */
2002 strcpy(c->name, broken);
2003 }
2004
2005 void
2006 updatestatus(void)
2007 {
2008 if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2009 strcpy(stext, "dwm-"VERSION);
2010 drawbar(selmon);
2011 }
2012
2013 void
2014 updatewindowtype(Client *c)
2015 {
2016 Atom state = getatomprop(c, netatom[NetWMState]);
2017 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2018
2019 if (state == netatom[NetWMFullscreen])
2020 setfullscreen(c, 1);
2021 if (wtype == netatom[NetWMWindowTypeDialog])
2022 c->isfloating = 1;
2023 }
2024
2025 void
2026 updatewmhints(Client *c)
2027 {
2028 XWMHints *wmh;
2029
2030 if ((wmh = XGetWMHints(dpy, c->win))) {
2031 if (c == selmon->sel && wmh->flags & XUrgencyHint) {
2032 wmh->flags &= ~XUrgencyHint;
2033 XSetWMHints(dpy, c->win, wmh);
2034 } else
2035 c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
2036 if (wmh->flags & InputHint)
2037 c->neverfocus = !wmh->input;
2038 else
2039 c->neverfocus = 0;
2040 XFree(wmh);
2041 }
2042 }
2043
2044 void
2045 view(const Arg *arg)
2046 {
2047 if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2048 return;
2049 selmon->seltags ^= 1; /* toggle sel tagset */
2050 if (arg->ui & TAGMASK)
2051 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2052 focus(NULL);
2053 arrange(selmon);
2054 }
2055
2056 Client *
2057 wintoclient(Window w)
2058 {
2059 Client *c;
2060 Monitor *m;
2061
2062 for (m = mons; m; m = m->next)
2063 for (c = m->clients; c; c = c->next)
2064 if (c->win == w)
2065 return c;
2066 return NULL;
2067 }
2068
2069 Monitor *
2070 wintomon(Window w)
2071 {
2072 int x, y;
2073 Client *c;
2074 Monitor *m;
2075
2076 if (w == root && getrootptr(&x, &y))
2077 return recttomon(x, y, 1, 1);
2078 for (m = mons; m; m = m->next)
2079 if (w == m->barwin)
2080 return m;
2081 if ((c = wintoclient(w)))
2082 return c->mon;
2083 return selmon;
2084 }
2085
2086 /* There's no way to check accesses to destroyed windows, thus those cases are
2087 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2088 * default error handler, which may call exit. */
2089 int
2090 xerror(Display *dpy, XErrorEvent *ee)
2091 {
2092 if (ee->error_code == BadWindow
2093 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2094 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2095 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2096 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2097 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2098 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2099 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2100 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2101 return 0;
2102 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2103 ee->request_code, ee->error_code);
2104 return xerrorxlib(dpy, ee); /* may call exit */
2105 }
2106
2107 int
2108 xerrordummy(Display *dpy, XErrorEvent *ee)
2109 {
2110 return 0;
2111 }
2112
2113 /* Startup Error handler to check if another window manager
2114 * is already running. */
2115 int
2116 xerrorstart(Display *dpy, XErrorEvent *ee)
2117 {
2118 die("dwm: another window manager is already running");
2119 return -1;
2120 }
2121
2122 void
2123 zoom(const Arg *arg)
2124 {
2125 Client *c = selmon->sel;
2126
2127 if (!selmon->lt[selmon->sellt]->arrange
2128 || (selmon->sel && selmon->sel->isfloating))
2129 return;
2130 if (c == nexttiled(selmon->clients))
2131 if (!c || !(c = nexttiled(c->next)))
2132 return;
2133 pop(c);
2134 }
2135
2136 int
2137 main(int argc, char *argv[])
2138 {
2139 if (argc == 2 && !strcmp("-v", argv[1]))
2140 die("dwm-"VERSION);
2141 else if (argc != 1)
2142 die("usage: dwm [-v]");
2143 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2144 fputs("warning: no locale support\n", stderr);
2145 if (!(dpy = XOpenDisplay(NULL)))
2146 die("dwm: cannot open display");
2147 checkotherwm();
2148 setup();
2149 scan();
2150 run();
2151 cleanup();
2152 XCloseDisplay(dpy);
2153 return EXIT_SUCCESS;
2154 }