Xinqi Bao's Git

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