Xinqi Bao's Git

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