Xinqi Bao's Git

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