Xinqi Bao's Git

Added tag 5.8 for changeset 60ea8fed13ab
[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 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 clientmessage(XEvent *e);
166 static void configure(Client *c);
167 static void configurenotify(XEvent *e);
168 static void configurerequest(XEvent *e);
169 static Monitor *createmon(void);
170 static void destroynotify(XEvent *e);
171 static void detach(Client *c);
172 static void detachstack(Client *c);
173 static void die(const char *errstr, ...);
174 static Monitor *dirtomon(int dir);
175 static void drawbar(Monitor *m);
176 static void drawbars(void);
177 static void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
178 static void drawtext(const char *text, unsigned long col[ColLast], Bool invert);
179 static void enternotify(XEvent *e);
180 static void expose(XEvent *e);
181 static void focus(Client *c);
182 static void focusin(XEvent *e);
183 static void focusmon(const Arg *arg);
184 static void focusstack(const Arg *arg);
185 static unsigned long getcolor(const char *colstr);
186 static Bool getrootptr(int *x, int *y);
187 static long getstate(Window w);
188 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
189 static void grabbuttons(Client *c, Bool focused);
190 static void grabkeys(void);
191 static void initfont(const char *fontstr);
192 static Bool isprotodel(Client *c);
193 static void keypress(XEvent *e);
194 static void killclient(const Arg *arg);
195 static void manage(Window w, XWindowAttributes *wa);
196 static void mappingnotify(XEvent *e);
197 static void maprequest(XEvent *e);
198 static void monocle(Monitor *m);
199 static void movemouse(const Arg *arg);
200 static Client *nexttiled(Client *c);
201 static Monitor *ptrtomon(int x, int y);
202 static void propertynotify(XEvent *e);
203 static void quit(const Arg *arg);
204 static void resize(Client *c, int x, int y, int w, int h, Bool interact);
205 static void resizemouse(const Arg *arg);
206 static void restack(Monitor *m);
207 static void run(void);
208 static void scan(void);
209 static void sendmon(Client *c, Monitor *m);
210 static void setclientstate(Client *c, long state);
211 static void setlayout(const Arg *arg);
212 static void setmfact(const Arg *arg);
213 static void setup(void);
214 static void showhide(Client *c);
215 static void sigchld(int unused);
216 static void spawn(const Arg *arg);
217 static void tag(const Arg *arg);
218 static void tagmon(const Arg *arg);
219 static int textnw(const char *text, unsigned int len);
220 static void tile(Monitor *);
221 static void togglebar(const Arg *arg);
222 static void togglefloating(const Arg *arg);
223 static void toggletag(const Arg *arg);
224 static void toggleview(const Arg *arg);
225 static void unfocus(Client *c);
226 static void unmanage(Client *c, Bool destroyed);
227 static void unmapnotify(XEvent *e);
228 static Bool updategeom(void);
229 static void updatebarpos(Monitor *m);
230 static void updatebars(void);
231 static void updatenumlockmask(void);
232 static void updatesizehints(Client *c);
233 static void updatestatus(void);
234 static void updatetitle(Client *c);
235 static void updatewmhints(Client *c);
236 static void view(const Arg *arg);
237 static Client *wintoclient(Window w);
238 static Monitor *wintomon(Window w);
239 static int xerror(Display *dpy, XErrorEvent *ee);
240 static int xerrordummy(Display *dpy, XErrorEvent *ee);
241 static int xerrorstart(Display *dpy, XErrorEvent *ee);
242 static void zoom(const Arg *arg);
243
244 /* variables */
245 static const char broken[] = "broken";
246 static char stext[256];
247 static int screen;
248 static int sw, sh; /* X display screen geometry width, height */
249 static int bh, blw = 0; /* bar geometry */
250 static int (*xerrorxlib)(Display *, XErrorEvent *);
251 static unsigned int numlockmask = 0;
252 static void (*handler[LASTEvent]) (XEvent *) = {
253 [ButtonPress] = buttonpress,
254 [ClientMessage] = clientmessage,
255 [ConfigureRequest] = configurerequest,
256 [ConfigureNotify] = configurenotify,
257 [DestroyNotify] = destroynotify,
258 [EnterNotify] = enternotify,
259 [Expose] = expose,
260 [FocusIn] = focusin,
261 [KeyPress] = keypress,
262 [MappingNotify] = mappingnotify,
263 [MapRequest] = maprequest,
264 [PropertyNotify] = propertynotify,
265 [UnmapNotify] = unmapnotify
266 };
267 static Atom wmatom[WMLast], netatom[NetLast];
268 static Bool otherwm;
269 static Bool running = True;
270 static Cursor cursor[CurLast];
271 static Display *dpy;
272 static DC dc;
273 static Monitor *mons = NULL, *selmon = NULL;
274 static Window root;
275
276 /* configuration, allows nested code to access above variables */
277 #include "config.h"
278
279 /* compile-time check if all tags fit into an unsigned int bit array. */
280 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
281
282 /* function implementations */
283 void
284 applyrules(Client *c) {
285 const char *class, *instance;
286 unsigned int i;
287 const Rule *r;
288 Monitor *m;
289 XClassHint ch = { 0 };
290
291 /* rule matching */
292 c->isfloating = c->tags = 0;
293 if(XGetClassHint(dpy, c->win, &ch)) {
294 class = ch.res_class ? ch.res_class : broken;
295 instance = ch.res_name ? ch.res_name : broken;
296 for(i = 0; i < LENGTH(rules); i++) {
297 r = &rules[i];
298 if((!r->title || strstr(c->name, r->title))
299 && (!r->class || strstr(class, r->class))
300 && (!r->instance || strstr(instance, r->instance)))
301 {
302 c->isfloating = r->isfloating;
303 c->tags |= r->tags;
304 for(m = mons; m && m->num != r->monitor; m = m->next);
305 if(m)
306 c->mon = m;
307 }
308 }
309 if(ch.res_class)
310 XFree(ch.res_class);
311 if(ch.res_name)
312 XFree(ch.res_name);
313 }
314 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
315 }
316
317 Bool
318 applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
319 Bool baseismin;
320 Monitor *m = c->mon;
321
322 /* set minimum possible */
323 *w = MAX(1, *w);
324 *h = MAX(1, *h);
325 if(interact) {
326 if(*x > sw)
327 *x = sw - WIDTH(c);
328 if(*y > sh)
329 *y = sh - HEIGHT(c);
330 if(*x + *w + 2 * c->bw < 0)
331 *x = 0;
332 if(*y + *h + 2 * c->bw < 0)
333 *y = 0;
334 }
335 else {
336 if(*x > m->mx + m->mw)
337 *x = m->mx + m->mw - WIDTH(c);
338 if(*y > m->my + m->mh)
339 *y = m->my + m->mh - HEIGHT(c);
340 if(*x + *w + 2 * c->bw < m->mx)
341 *x = m->mx;
342 if(*y + *h + 2 * c->bw < m->my)
343 *y = m->my;
344 }
345 if(*h < bh)
346 *h = bh;
347 if(*w < bh)
348 *w = bh;
349 if(resizehints || c->isfloating) {
350 /* see last two sentences in ICCCM 4.1.2.3 */
351 baseismin = c->basew == c->minw && c->baseh == c->minh;
352 if(!baseismin) { /* temporarily remove base dimensions */
353 *w -= c->basew;
354 *h -= c->baseh;
355 }
356 /* adjust for aspect limits */
357 if(c->mina > 0 && c->maxa > 0) {
358 if(c->maxa < (float)*w / *h)
359 *w = *h * c->maxa + 0.5;
360 else if(c->mina < (float)*h / *w)
361 *h = *w * c->mina + 0.5;
362 }
363 if(baseismin) { /* increment calculation requires this */
364 *w -= c->basew;
365 *h -= c->baseh;
366 }
367 /* adjust for increment value */
368 if(c->incw)
369 *w -= *w % c->incw;
370 if(c->inch)
371 *h -= *h % c->inch;
372 /* restore base dimensions */
373 *w += c->basew;
374 *h += c->baseh;
375 *w = MAX(*w, c->minw);
376 *h = MAX(*h, 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);
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 Client *c;
792 Monitor *m;
793 XCrossingEvent *ev = &e->xcrossing;
794
795 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
796 return;
797 if((m = wintomon(ev->window)) && m != selmon) {
798 unfocus(selmon->sel);
799 selmon = m;
800 }
801 if((c = wintoclient(ev->window)))
802 focus(c);
803 else
804 focus(NULL);
805 }
806
807 void
808 expose(XEvent *e) {
809 Monitor *m;
810 XExposeEvent *ev = &e->xexpose;
811
812 if(ev->count == 0 && (m = wintomon(ev->window)))
813 drawbar(m);
814 }
815
816 void
817 focus(Client *c) {
818 if(!c || !ISVISIBLE(c))
819 for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
820 if(selmon->sel)
821 unfocus(selmon->sel);
822 if(c) {
823 if(c->mon != selmon)
824 selmon = c->mon;
825 if(c->isurgent)
826 clearurgent(c);
827 detachstack(c);
828 attachstack(c);
829 grabbuttons(c, True);
830 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
831 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
832 }
833 else
834 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
835 selmon->sel = c;
836 drawbars();
837 }
838
839 void
840 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
841 XFocusChangeEvent *ev = &e->xfocus;
842
843 if(selmon->sel && ev->window != selmon->sel->win)
844 XSetInputFocus(dpy, selmon->sel->win, RevertToPointerRoot, CurrentTime);
845 }
846
847 void
848 focusmon(const Arg *arg) {
849 Monitor *m = NULL;
850
851 if(!mons->next)
852 return;
853 if((m = dirtomon(arg->i)) == selmon)
854 return;
855 unfocus(selmon->sel);
856 selmon = m;
857 focus(NULL);
858 }
859
860 void
861 focusstack(const Arg *arg) {
862 Client *c = NULL, *i;
863
864 if(!selmon->sel)
865 return;
866 if(arg->i > 0) {
867 for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
868 if(!c)
869 for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
870 }
871 else {
872 for(i = selmon->clients; i != selmon->sel; i = i->next)
873 if(ISVISIBLE(i))
874 c = i;
875 if(!c)
876 for(; i; i = i->next)
877 if(ISVISIBLE(i))
878 c = i;
879 }
880 if(c) {
881 focus(c);
882 restack(selmon);
883 }
884 }
885
886 unsigned long
887 getcolor(const char *colstr) {
888 Colormap cmap = DefaultColormap(dpy, screen);
889 XColor color;
890
891 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
892 die("error, cannot allocate color '%s'\n", colstr);
893 return color.pixel;
894 }
895
896 Bool
897 getrootptr(int *x, int *y) {
898 int di;
899 unsigned int dui;
900 Window dummy;
901
902 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
903 }
904
905 long
906 getstate(Window w) {
907 int format, status;
908 long result = -1;
909 unsigned char *p = NULL;
910 unsigned long n, extra;
911 Atom real;
912
913 status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
914 &real, &format, &n, &extra, (unsigned char **)&p);
915 if(status != Success)
916 return -1;
917 if(n != 0)
918 result = *p;
919 XFree(p);
920 return result;
921 }
922
923 Bool
924 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
925 char **list = NULL;
926 int n;
927 XTextProperty name;
928
929 if(!text || size == 0)
930 return False;
931 text[0] = '\0';
932 XGetTextProperty(dpy, w, &name, atom);
933 if(!name.nitems)
934 return False;
935 if(name.encoding == XA_STRING)
936 strncpy(text, (char *)name.value, size - 1);
937 else {
938 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
939 strncpy(text, *list, size - 1);
940 XFreeStringList(list);
941 }
942 }
943 text[size - 1] = '\0';
944 XFree(name.value);
945 return True;
946 }
947
948 void
949 grabbuttons(Client *c, Bool focused) {
950 updatenumlockmask();
951 {
952 unsigned int i, j;
953 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
954 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
955 if(focused) {
956 for(i = 0; i < LENGTH(buttons); i++)
957 if(buttons[i].click == ClkClientWin)
958 for(j = 0; j < LENGTH(modifiers); j++)
959 XGrabButton(dpy, buttons[i].button,
960 buttons[i].mask | modifiers[j],
961 c->win, False, BUTTONMASK,
962 GrabModeAsync, GrabModeSync, None, None);
963 }
964 else
965 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
966 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
967 }
968 }
969
970 void
971 grabkeys(void) {
972 updatenumlockmask();
973 {
974 unsigned int i, j;
975 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
976 KeyCode code;
977
978 XUngrabKey(dpy, AnyKey, AnyModifier, root);
979 for(i = 0; i < LENGTH(keys); i++) {
980 if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
981 for(j = 0; j < LENGTH(modifiers); j++)
982 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
983 True, GrabModeAsync, GrabModeAsync);
984 }
985 }
986 }
987
988 void
989 initfont(const char *fontstr) {
990 char *def, **missing;
991 int i, n;
992
993 missing = NULL;
994 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
995 if(missing) {
996 while(n--)
997 fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
998 XFreeStringList(missing);
999 }
1000 if(dc.font.set) {
1001 XFontSetExtents *font_extents;
1002 XFontStruct **xfonts;
1003 char **font_names;
1004
1005 dc.font.ascent = dc.font.descent = 0;
1006 font_extents = XExtentsOfFontSet(dc.font.set);
1007 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
1008 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
1009 dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
1010 dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
1011 xfonts++;
1012 }
1013 }
1014 else {
1015 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
1016 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
1017 die("error, cannot load font: '%s'\n", fontstr);
1018 dc.font.ascent = dc.font.xfont->ascent;
1019 dc.font.descent = dc.font.xfont->descent;
1020 }
1021 dc.font.height = dc.font.ascent + dc.font.descent;
1022 }
1023
1024 Bool
1025 isprotodel(Client *c) {
1026 int i, n;
1027 Atom *protocols;
1028 Bool ret = False;
1029
1030 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1031 for(i = 0; !ret && i < n; i++)
1032 if(protocols[i] == wmatom[WMDelete])
1033 ret = True;
1034 XFree(protocols);
1035 }
1036 return ret;
1037 }
1038
1039 #ifdef XINERAMA
1040 static Bool
1041 isuniquegeom(XineramaScreenInfo *unique, size_t len, XineramaScreenInfo *info) {
1042 unsigned int i;
1043
1044 for(i = 0; i < len; i++)
1045 if(unique[i].x_org == info->x_org && unique[i].y_org == info->y_org
1046 && unique[i].width == info->width && unique[i].height == info->height)
1047 return False;
1048 return True;
1049 }
1050 #endif /* XINERAMA */
1051
1052 void
1053 keypress(XEvent *e) {
1054 unsigned int i;
1055 KeySym keysym;
1056 XKeyEvent *ev;
1057
1058 ev = &e->xkey;
1059 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1060 for(i = 0; i < LENGTH(keys); i++)
1061 if(keysym == keys[i].keysym
1062 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1063 && keys[i].func)
1064 keys[i].func(&(keys[i].arg));
1065 }
1066
1067 void
1068 killclient(const Arg *arg) {
1069 XEvent ev;
1070
1071 if(!selmon->sel)
1072 return;
1073 if(isprotodel(selmon->sel)) {
1074 ev.type = ClientMessage;
1075 ev.xclient.window = selmon->sel->win;
1076 ev.xclient.message_type = wmatom[WMProtocols];
1077 ev.xclient.format = 32;
1078 ev.xclient.data.l[0] = wmatom[WMDelete];
1079 ev.xclient.data.l[1] = CurrentTime;
1080 XSendEvent(dpy, selmon->sel->win, False, NoEventMask, &ev);
1081 }
1082 else {
1083 XGrabServer(dpy);
1084 XSetErrorHandler(xerrordummy);
1085 XSetCloseDownMode(dpy, DestroyAll);
1086 XKillClient(dpy, selmon->sel->win);
1087 XSync(dpy, False);
1088 XSetErrorHandler(xerror);
1089 XUngrabServer(dpy);
1090 }
1091 }
1092
1093 void
1094 manage(Window w, XWindowAttributes *wa) {
1095 static Client cz;
1096 Client *c, *t = NULL;
1097 Window trans = None;
1098 XWindowChanges wc;
1099
1100 if(!(c = malloc(sizeof(Client))))
1101 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1102 *c = cz;
1103 c->win = w;
1104 updatetitle(c);
1105 if(XGetTransientForHint(dpy, w, &trans))
1106 t = wintoclient(trans);
1107 if(t) {
1108 c->mon = t->mon;
1109 c->tags = t->tags;
1110 }
1111 else {
1112 c->mon = selmon;
1113 applyrules(c);
1114 }
1115 /* geometry */
1116 c->x = wa->x + c->mon->wx;
1117 c->y = wa->y + c->mon->wy;
1118 c->w = wa->width;
1119 c->h = wa->height;
1120 c->oldbw = wa->border_width;
1121 if(c->w == c->mon->mw && c->h == c->mon->mh) {
1122 c->x = c->mon->mx;
1123 c->y = c->mon->my;
1124 c->bw = 0;
1125 }
1126 else {
1127 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1128 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1129 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1130 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1131 c->x = MAX(c->x, c->mon->mx);
1132 /* only fix client y-offset, if the client center might cover the bar */
1133 c->y = MAX(c->y, ((c->mon->by == 0) && (c->x + (c->w / 2) >= c->mon->wx)
1134 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1135 c->bw = borderpx;
1136 }
1137 wc.border_width = c->bw;
1138 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1139 XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
1140 configure(c); /* propagates border_width, if size doesn't change */
1141 updatesizehints(c);
1142 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1143 grabbuttons(c, False);
1144 if(!c->isfloating)
1145 c->isfloating = trans != None || c->isfixed;
1146 if(c->isfloating)
1147 XRaiseWindow(dpy, c->win);
1148 attach(c);
1149 attachstack(c);
1150 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1151 XMapWindow(dpy, c->win);
1152 setclientstate(c, NormalState);
1153 arrange(c->mon);
1154 }
1155
1156 void
1157 mappingnotify(XEvent *e) {
1158 XMappingEvent *ev = &e->xmapping;
1159
1160 XRefreshKeyboardMapping(ev);
1161 if(ev->request == MappingKeyboard)
1162 grabkeys();
1163 }
1164
1165 void
1166 maprequest(XEvent *e) {
1167 static XWindowAttributes wa;
1168 XMapRequestEvent *ev = &e->xmaprequest;
1169
1170 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1171 return;
1172 if(wa.override_redirect)
1173 return;
1174 if(!wintoclient(ev->window))
1175 manage(ev->window, &wa);
1176 }
1177
1178 void
1179 monocle(Monitor *m) {
1180 unsigned int n = 0;
1181 Client *c;
1182
1183 for(c = m->clients; c; c = c->next)
1184 if(ISVISIBLE(c))
1185 n++;
1186 if(n > 0) /* override layout symbol */
1187 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1188 for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
1189 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
1190 }
1191
1192 void
1193 movemouse(const Arg *arg) {
1194 int x, y, ocx, ocy, nx, ny;
1195 Client *c;
1196 Monitor *m;
1197 XEvent ev;
1198
1199 if(!(c = selmon->sel))
1200 return;
1201 restack(selmon);
1202 ocx = c->x;
1203 ocy = c->y;
1204 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1205 None, cursor[CurMove], CurrentTime) != GrabSuccess)
1206 return;
1207 if(!getrootptr(&x, &y))
1208 return;
1209 do {
1210 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1211 switch (ev.type) {
1212 case ConfigureRequest:
1213 case Expose:
1214 case MapRequest:
1215 handler[ev.type](&ev);
1216 break;
1217 case MotionNotify:
1218 nx = ocx + (ev.xmotion.x - x);
1219 ny = ocy + (ev.xmotion.y - y);
1220 if(snap && nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1221 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1222 if(abs(selmon->wx - nx) < snap)
1223 nx = selmon->wx;
1224 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1225 nx = selmon->wx + selmon->ww - WIDTH(c);
1226 if(abs(selmon->wy - ny) < snap)
1227 ny = selmon->wy;
1228 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1229 ny = selmon->wy + selmon->wh - HEIGHT(c);
1230 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1231 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1232 togglefloating(NULL);
1233 }
1234 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1235 resize(c, nx, ny, c->w, c->h, True);
1236 break;
1237 }
1238 } while(ev.type != ButtonRelease);
1239 XUngrabPointer(dpy, CurrentTime);
1240 if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
1241 sendmon(c, m);
1242 selmon = m;
1243 focus(NULL);
1244 }
1245 }
1246
1247 Client *
1248 nexttiled(Client *c) {
1249 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1250 return c;
1251 }
1252
1253 Monitor *
1254 ptrtomon(int x, int y) {
1255 Monitor *m;
1256
1257 for(m = mons; m; m = m->next)
1258 if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh))
1259 return m;
1260 return selmon;
1261 }
1262
1263 void
1264 propertynotify(XEvent *e) {
1265 Client *c;
1266 Window trans;
1267 XPropertyEvent *ev = &e->xproperty;
1268
1269 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1270 updatestatus();
1271 else if(ev->state == PropertyDelete)
1272 return; /* ignore */
1273 else if((c = wintoclient(ev->window))) {
1274 switch (ev->atom) {
1275 default: break;
1276 case XA_WM_TRANSIENT_FOR:
1277 XGetTransientForHint(dpy, c->win, &trans);
1278 if(!c->isfloating && (c->isfloating = (wintoclient(trans) != NULL)))
1279 arrange(c->mon);
1280 break;
1281 case XA_WM_NORMAL_HINTS:
1282 updatesizehints(c);
1283 break;
1284 case XA_WM_HINTS:
1285 updatewmhints(c);
1286 drawbars();
1287 break;
1288 }
1289 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1290 updatetitle(c);
1291 if(c == c->mon->sel)
1292 drawbar(c->mon);
1293 }
1294 }
1295 }
1296
1297 void
1298 clientmessage(XEvent *e) {
1299 XClientMessageEvent *cme = &e->xclient;
1300
1301 if(cme->message_type == netatom[NetWMState] && cme->data.l[1] == netatom[NetWMFullscreen]) {
1302 if(cme->data.l[0])
1303 XChangeProperty(dpy, cme->window, netatom[NetWMState], XA_ATOM, 32,
1304 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1305 else
1306 XChangeProperty(dpy, cme->window, netatom[NetWMState], XA_ATOM, 32,
1307 PropModeReplace, (unsigned char*)0, 0);
1308 }
1309 }
1310
1311 void
1312 quit(const Arg *arg) {
1313 running = False;
1314 }
1315
1316 void
1317 resize(Client *c, int x, int y, int w, int h, Bool interact) {
1318 XWindowChanges wc;
1319
1320 if(applysizehints(c, &x, &y, &w, &h, interact)) {
1321 c->x = wc.x = x;
1322 c->y = wc.y = y;
1323 c->w = wc.width = w;
1324 c->h = wc.height = h;
1325 wc.border_width = c->bw;
1326 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1327 configure(c);
1328 XSync(dpy, False);
1329 }
1330 }
1331
1332 void
1333 resizemouse(const Arg *arg) {
1334 int ocx, ocy;
1335 int nw, nh;
1336 Client *c;
1337 Monitor *m;
1338 XEvent ev;
1339
1340 if(!(c = selmon->sel))
1341 return;
1342 restack(selmon);
1343 ocx = c->x;
1344 ocy = c->y;
1345 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1346 None, cursor[CurResize], CurrentTime) != GrabSuccess)
1347 return;
1348 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1349 do {
1350 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1351 switch(ev.type) {
1352 case ConfigureRequest:
1353 case Expose:
1354 case MapRequest:
1355 handler[ev.type](&ev);
1356 break;
1357 case MotionNotify:
1358 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1359 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1360 if(snap && nw >= selmon->wx && nw <= selmon->wx + selmon->ww
1361 && nh >= selmon->wy && nh <= selmon->wy + selmon->wh)
1362 {
1363 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1364 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1365 togglefloating(NULL);
1366 }
1367 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1368 resize(c, c->x, c->y, nw, nh, True);
1369 break;
1370 }
1371 } while(ev.type != ButtonRelease);
1372 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1373 XUngrabPointer(dpy, CurrentTime);
1374 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1375 if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
1376 sendmon(c, m);
1377 selmon = m;
1378 focus(NULL);
1379 }
1380 }
1381
1382 void
1383 restack(Monitor *m) {
1384 Client *c;
1385 XEvent ev;
1386 XWindowChanges wc;
1387
1388 drawbar(m);
1389 if(!m->sel)
1390 return;
1391 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1392 XRaiseWindow(dpy, m->sel->win);
1393 if(m->lt[m->sellt]->arrange) {
1394 wc.stack_mode = Below;
1395 wc.sibling = m->barwin;
1396 for(c = m->stack; c; c = c->snext)
1397 if(!c->isfloating && ISVISIBLE(c)) {
1398 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1399 wc.sibling = c->win;
1400 }
1401 }
1402 XSync(dpy, False);
1403 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1404 }
1405
1406 void
1407 run(void) {
1408 XEvent ev;
1409 /* main event loop */
1410 XSync(dpy, False);
1411 while(running && !XNextEvent(dpy, &ev)) {
1412 if(handler[ev.type])
1413 handler[ev.type](&ev); /* call handler */
1414 }
1415 }
1416
1417 void
1418 scan(void) {
1419 unsigned int i, num;
1420 Window d1, d2, *wins = NULL;
1421 XWindowAttributes wa;
1422
1423 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1424 for(i = 0; i < num; i++) {
1425 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1426 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1427 continue;
1428 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1429 manage(wins[i], &wa);
1430 }
1431 for(i = 0; i < num; i++) { /* now the transients */
1432 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1433 continue;
1434 if(XGetTransientForHint(dpy, wins[i], &d1)
1435 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1436 manage(wins[i], &wa);
1437 }
1438 if(wins)
1439 XFree(wins);
1440 }
1441 }
1442
1443 void
1444 sendmon(Client *c, Monitor *m) {
1445 if(c->mon == m)
1446 return;
1447 unfocus(c);
1448 detach(c);
1449 detachstack(c);
1450 c->mon = m;
1451 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1452 attach(c);
1453 attachstack(c);
1454 focus(NULL);
1455 arrange(NULL);
1456 }
1457
1458 void
1459 setclientstate(Client *c, long state) {
1460 long data[] = { state, None };
1461
1462 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1463 PropModeReplace, (unsigned char *)data, 2);
1464 }
1465
1466 void
1467 setlayout(const Arg *arg) {
1468 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1469 selmon->sellt ^= 1;
1470 if(arg && arg->v)
1471 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1472 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1473 if(selmon->sel)
1474 arrange(selmon);
1475 else
1476 drawbar(selmon);
1477 }
1478
1479 /* arg > 1.0 will set mfact absolutly */
1480 void
1481 setmfact(const Arg *arg) {
1482 float f;
1483
1484 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1485 return;
1486 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1487 if(f < 0.1 || f > 0.9)
1488 return;
1489 selmon->mfact = f;
1490 arrange(selmon);
1491 }
1492
1493 void
1494 setup(void) {
1495 XSetWindowAttributes wa;
1496
1497 /* clean up any zombies immediately */
1498 sigchld(0);
1499
1500 /* init screen */
1501 screen = DefaultScreen(dpy);
1502 root = RootWindow(dpy, screen);
1503 initfont(font);
1504 sw = DisplayWidth(dpy, screen);
1505 sh = DisplayHeight(dpy, screen);
1506 bh = dc.h = dc.font.height + 2;
1507 updategeom();
1508 /* init atoms */
1509 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1510 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1511 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1512 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1513 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1514 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1515 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1516 /* init cursors */
1517 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1518 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1519 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1520 /* init appearance */
1521 dc.norm[ColBorder] = getcolor(normbordercolor);
1522 dc.norm[ColBG] = getcolor(normbgcolor);
1523 dc.norm[ColFG] = getcolor(normfgcolor);
1524 dc.sel[ColBorder] = getcolor(selbordercolor);
1525 dc.sel[ColBG] = getcolor(selbgcolor);
1526 dc.sel[ColFG] = getcolor(selfgcolor);
1527 dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
1528 dc.gc = XCreateGC(dpy, root, 0, NULL);
1529 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1530 if(!dc.font.set)
1531 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
1532 /* init bars */
1533 updatebars();
1534 updatestatus();
1535 /* EWMH support per view */
1536 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1537 PropModeReplace, (unsigned char *) netatom, NetLast);
1538 /* select for events */
1539 wa.cursor = cursor[CurNormal];
1540 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
1541 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
1542 |PropertyChangeMask;
1543 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1544 XSelectInput(dpy, root, wa.event_mask);
1545 grabkeys();
1546 }
1547
1548 void
1549 showhide(Client *c) {
1550 if(!c)
1551 return;
1552 if(ISVISIBLE(c)) { /* show clients top down */
1553 XMoveWindow(dpy, c->win, c->x, c->y);
1554 if(!c->mon->lt[c->mon->sellt]->arrange || c->isfloating)
1555 resize(c, c->x, c->y, c->w, c->h, False);
1556 showhide(c->snext);
1557 }
1558 else { /* hide clients bottom up */
1559 showhide(c->snext);
1560 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
1561 }
1562 }
1563
1564
1565 void
1566 sigchld(int unused) {
1567 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1568 die("Can't install SIGCHLD handler");
1569 while(0 < waitpid(-1, NULL, WNOHANG));
1570 }
1571
1572 void
1573 spawn(const Arg *arg) {
1574 if(fork() == 0) {
1575 if(dpy)
1576 close(ConnectionNumber(dpy));
1577 setsid();
1578 execvp(((char **)arg->v)[0], (char **)arg->v);
1579 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1580 perror(" failed");
1581 exit(0);
1582 }
1583 }
1584
1585 void
1586 tag(const Arg *arg) {
1587 if(selmon->sel && arg->ui & TAGMASK) {
1588 selmon->sel->tags = arg->ui & TAGMASK;
1589 arrange(selmon);
1590 }
1591 }
1592
1593 void
1594 tagmon(const Arg *arg) {
1595 if(!selmon->sel || !mons->next)
1596 return;
1597 sendmon(selmon->sel, dirtomon(arg->i));
1598 }
1599
1600 int
1601 textnw(const char *text, unsigned int len) {
1602 XRectangle r;
1603
1604 if(dc.font.set) {
1605 XmbTextExtents(dc.font.set, text, len, NULL, &r);
1606 return r.width;
1607 }
1608 return XTextWidth(dc.font.xfont, text, len);
1609 }
1610
1611 void
1612 tile(Monitor *m) {
1613 int x, y, h, w, mw;
1614 unsigned int i, n;
1615 Client *c;
1616
1617 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1618 if(n == 0)
1619 return;
1620 /* master */
1621 c = nexttiled(m->clients);
1622 mw = m->mfact * m->ww;
1623 resize(c, m->wx, m->wy, (n == 1 ? m->ww : mw) - 2 * c->bw, m->wh - 2 * c->bw, False);
1624 if(--n == 0)
1625 return;
1626 /* tile stack */
1627 x = (m->wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : m->wx + mw;
1628 y = m->wy;
1629 w = (m->wx + mw > c->x + c->w) ? m->wx + m->ww - x : m->ww - mw;
1630 h = m->wh / n;
1631 if(h < bh)
1632 h = m->wh;
1633 for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
1634 resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
1635 ? m->wy + m->wh - y - 2 * c->bw : h - 2 * c->bw), False);
1636 if(h != m->wh)
1637 y = c->y + HEIGHT(c);
1638 }
1639 }
1640
1641 void
1642 togglebar(const Arg *arg) {
1643 selmon->showbar = !selmon->showbar;
1644 updatebarpos(selmon);
1645 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1646 arrange(selmon);
1647 }
1648
1649 void
1650 togglefloating(const Arg *arg) {
1651 if(!selmon->sel)
1652 return;
1653 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1654 if(selmon->sel->isfloating)
1655 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1656 selmon->sel->w, selmon->sel->h, False);
1657 arrange(selmon);
1658 }
1659
1660 void
1661 toggletag(const Arg *arg) {
1662 unsigned int newtags;
1663
1664 if(!selmon->sel)
1665 return;
1666 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1667 if(newtags) {
1668 selmon->sel->tags = newtags;
1669 arrange(selmon);
1670 }
1671 }
1672
1673 void
1674 toggleview(const Arg *arg) {
1675 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1676
1677 if(newtagset) {
1678 selmon->tagset[selmon->seltags] = newtagset;
1679 arrange(selmon);
1680 }
1681 }
1682
1683 void
1684 unfocus(Client *c) {
1685 if(!c)
1686 return;
1687 grabbuttons(c, False);
1688 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
1689 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1690 }
1691
1692 void
1693 unmanage(Client *c, Bool destroyed) {
1694 Monitor *m = c->mon;
1695 XWindowChanges wc;
1696
1697 /* The server grab construct avoids race conditions. */
1698 detach(c);
1699 detachstack(c);
1700 if(!destroyed) {
1701 wc.border_width = c->oldbw;
1702 XGrabServer(dpy);
1703 XSetErrorHandler(xerrordummy);
1704 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1705 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1706 setclientstate(c, WithdrawnState);
1707 XSync(dpy, False);
1708 XSetErrorHandler(xerror);
1709 XUngrabServer(dpy);
1710 }
1711 free(c);
1712 focus(NULL);
1713 arrange(m);
1714 }
1715
1716 void
1717 unmapnotify(XEvent *e) {
1718 Client *c;
1719 XUnmapEvent *ev = &e->xunmap;
1720
1721 if((c = wintoclient(ev->window)))
1722 unmanage(c, False);
1723 }
1724
1725 void
1726 updatebars(void) {
1727 Monitor *m;
1728 XSetWindowAttributes wa;
1729
1730 wa.override_redirect = True;
1731 wa.background_pixmap = ParentRelative;
1732 wa.event_mask = ButtonPressMask|ExposureMask;
1733 for(m = mons; m; m = m->next) {
1734 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1735 CopyFromParent, DefaultVisual(dpy, screen),
1736 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1737 XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
1738 XMapRaised(dpy, m->barwin);
1739 }
1740 }
1741
1742 void
1743 updatebarpos(Monitor *m) {
1744 m->wy = m->my;
1745 m->wh = m->mh;
1746 if(m->showbar) {
1747 m->wh -= bh;
1748 m->by = m->topbar ? m->wy : m->wy + m->wh;
1749 m->wy = m->topbar ? m->wy + bh : m->wy;
1750 }
1751 else
1752 m->by = -bh;
1753 }
1754
1755 Bool
1756 updategeom(void) {
1757 Bool dirty = False;
1758
1759 #ifdef XINERAMA
1760 if(XineramaIsActive(dpy)) {
1761 int i, j, n, nn;
1762 Client *c;
1763 Monitor *m;
1764 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1765 XineramaScreenInfo *unique = NULL;
1766
1767 info = XineramaQueryScreens(dpy, &nn);
1768 for(n = 0, m = mons; m; m = m->next, n++);
1769 /* only consider unique geometries as separate screens */
1770 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
1771 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
1772 for(i = 0, j = 0; i < nn; i++)
1773 if(isuniquegeom(unique, j, &info[i]))
1774 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1775 XFree(info);
1776 nn = j;
1777 if(n <= nn) {
1778 for(i = 0; i < (nn - n); i++) { /* new monitors available */
1779 for(m = mons; m && m->next; m = m->next);
1780 if(m)
1781 m->next = createmon();
1782 else
1783 mons = createmon();
1784 }
1785 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
1786 if(i >= n
1787 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1788 || unique[i].width != m->mw || unique[i].height != m->mh))
1789 {
1790 dirty = True;
1791 m->num = i;
1792 m->mx = m->wx = unique[i].x_org;
1793 m->my = m->wy = unique[i].y_org;
1794 m->mw = m->ww = unique[i].width;
1795 m->mh = m->wh = unique[i].height;
1796 updatebarpos(m);
1797 }
1798 }
1799 else { /* less monitors available nn < n */
1800 for(i = nn; i < n; i++) {
1801 for(m = mons; m && m->next; m = m->next);
1802 while(m->clients) {
1803 dirty = True;
1804 c = m->clients;
1805 m->clients = c->next;
1806 detachstack(c);
1807 c->mon = mons;
1808 attach(c);
1809 attachstack(c);
1810 }
1811 if(m == selmon)
1812 selmon = mons;
1813 cleanupmon(m);
1814 }
1815 }
1816 free(unique);
1817 }
1818 else
1819 #endif /* XINERAMA */
1820 /* default monitor setup */
1821 {
1822 if(!mons)
1823 mons = createmon();
1824 if(mons->mw != sw || mons->mh != sh) {
1825 dirty = True;
1826 mons->mw = mons->ww = sw;
1827 mons->mh = mons->wh = sh;
1828 updatebarpos(mons);
1829 }
1830 }
1831 if(dirty) {
1832 selmon = mons;
1833 selmon = wintomon(root);
1834 }
1835 return dirty;
1836 }
1837
1838 void
1839 updatenumlockmask(void) {
1840 unsigned int i, j;
1841 XModifierKeymap *modmap;
1842
1843 numlockmask = 0;
1844 modmap = XGetModifierMapping(dpy);
1845 for(i = 0; i < 8; i++)
1846 for(j = 0; j < modmap->max_keypermod; j++)
1847 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1848 == XKeysymToKeycode(dpy, XK_Num_Lock))
1849 numlockmask = (1 << i);
1850 XFreeModifiermap(modmap);
1851 }
1852
1853 void
1854 updatesizehints(Client *c) {
1855 long msize;
1856 XSizeHints size;
1857
1858 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
1859 /* size is uninitialized, ensure that size.flags aren't used */
1860 size.flags = PSize;
1861 if(size.flags & PBaseSize) {
1862 c->basew = size.base_width;
1863 c->baseh = size.base_height;
1864 }
1865 else if(size.flags & PMinSize) {
1866 c->basew = size.min_width;
1867 c->baseh = size.min_height;
1868 }
1869 else
1870 c->basew = c->baseh = 0;
1871 if(size.flags & PResizeInc) {
1872 c->incw = size.width_inc;
1873 c->inch = size.height_inc;
1874 }
1875 else
1876 c->incw = c->inch = 0;
1877 if(size.flags & PMaxSize) {
1878 c->maxw = size.max_width;
1879 c->maxh = size.max_height;
1880 }
1881 else
1882 c->maxw = c->maxh = 0;
1883 if(size.flags & PMinSize) {
1884 c->minw = size.min_width;
1885 c->minh = size.min_height;
1886 }
1887 else if(size.flags & PBaseSize) {
1888 c->minw = size.base_width;
1889 c->minh = size.base_height;
1890 }
1891 else
1892 c->minw = c->minh = 0;
1893 if(size.flags & PAspect) {
1894 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
1895 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
1896 }
1897 else
1898 c->maxa = c->mina = 0.0;
1899 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
1900 && c->maxw == c->minw && c->maxh == c->minh);
1901 }
1902
1903 void
1904 updatetitle(Client *c) {
1905 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
1906 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
1907 if(c->name[0] == '\0') /* hack to mark broken clients */
1908 strcpy(c->name, broken);
1909 }
1910
1911 void
1912 updatestatus(void) {
1913 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
1914 strcpy(stext, "dwm-"VERSION);
1915 drawbar(selmon);
1916 }
1917
1918 void
1919 updatewmhints(Client *c) {
1920 XWMHints *wmh;
1921
1922 if((wmh = XGetWMHints(dpy, c->win))) {
1923 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
1924 wmh->flags &= ~XUrgencyHint;
1925 XSetWMHints(dpy, c->win, wmh);
1926 }
1927 else
1928 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
1929 XFree(wmh);
1930 }
1931 }
1932
1933 void
1934 view(const Arg *arg) {
1935 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
1936 return;
1937 selmon->seltags ^= 1; /* toggle sel tagset */
1938 if(arg->ui & TAGMASK)
1939 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
1940 arrange(selmon);
1941 }
1942
1943 Client *
1944 wintoclient(Window w) {
1945 Client *c;
1946 Monitor *m;
1947
1948 for(m = mons; m; m = m->next)
1949 for(c = m->clients; c; c = c->next)
1950 if(c->win == w)
1951 return c;
1952 return NULL;
1953 }
1954
1955 Monitor *
1956 wintomon(Window w) {
1957 int x, y;
1958 Client *c;
1959 Monitor *m;
1960
1961 if(w == root && getrootptr(&x, &y))
1962 return ptrtomon(x, y);
1963 for(m = mons; m; m = m->next)
1964 if(w == m->barwin)
1965 return m;
1966 if((c = wintoclient(w)))
1967 return c->mon;
1968 return selmon;
1969 }
1970
1971 /* There's no way to check accesses to destroyed windows, thus those cases are
1972 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1973 * default error handler, which may call exit. */
1974 int
1975 xerror(Display *dpy, XErrorEvent *ee) {
1976 if(ee->error_code == BadWindow
1977 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
1978 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
1979 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
1980 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
1981 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
1982 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
1983 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
1984 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
1985 return 0;
1986 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
1987 ee->request_code, ee->error_code);
1988 return xerrorxlib(dpy, ee); /* may call exit */
1989 }
1990
1991 int
1992 xerrordummy(Display *dpy, XErrorEvent *ee) {
1993 return 0;
1994 }
1995
1996 /* Startup Error handler to check if another window manager
1997 * is already running. */
1998 int
1999 xerrorstart(Display *dpy, XErrorEvent *ee) {
2000 otherwm = True;
2001 return -1;
2002 }
2003
2004 void
2005 zoom(const Arg *arg) {
2006 Client *c = selmon->sel;
2007
2008 if(!selmon->lt[selmon->sellt]->arrange
2009 || selmon->lt[selmon->sellt]->arrange == monocle
2010 || (selmon->sel && selmon->sel->isfloating))
2011 return;
2012 if(c == nexttiled(selmon->clients))
2013 if(!c || !(c = nexttiled(c->next)))
2014 return;
2015 detach(c);
2016 attach(c);
2017 focus(c);
2018 arrange(c->mon);
2019 }
2020
2021 int
2022 main(int argc, char *argv[]) {
2023 if(argc == 2 && !strcmp("-v", argv[1]))
2024 die("dwm-"VERSION", © 2006-2010 dwm engineers, see LICENSE for details\n");
2025 else if(argc != 1)
2026 die("usage: dwm [-v]\n");
2027 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2028 fputs("warning: no locale support\n", stderr);
2029 if(!(dpy = XOpenDisplay(NULL)))
2030 die("dwm: cannot open display\n");
2031 checkotherwm();
2032 setup();
2033 scan();
2034 run();
2035 cleanup();
2036 XCloseDisplay(dpy);
2037 return 0;
2038 }