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