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