3b18b2b28a42c53078d50db5d7f66585e189434c
[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 static const char *evname[LASTEvent] = {
1410 [ButtonPress] = "buttonpress",
1411 [ClientMessage] = "clientmessage",
1412 [ConfigureRequest] = "configurerequest",
1413 [ConfigureNotify] = "configurenotify",
1414 [DestroyNotify] = "destroynotify",
1415 [EnterNotify] = "enternotify",
1416 [Expose] = "expose",
1417 [FocusIn] = "focusin",
1418 [KeyPress] = "keypress",
1419 [MappingNotify] = "mappingnotify",
1420 [MapRequest] = "maprequest",
1421 [PropertyNotify] = "propertynotify",
1422 [UnmapNotify] = "unmapnotify"
1423 };
1424 /* main event loop */
1425 XSync(dpy, False);
1426 while(running && !XNextEvent(dpy, &ev)) {
1427 if(handler[ev.type])
1428 handler[ev.type](&ev); /* call handler */
1429 }
1430 }
1431
1432 void
1433 scan(void) {
1434 unsigned int i, num;
1435 Window d1, d2, *wins = NULL;
1436 XWindowAttributes wa;
1437
1438 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1439 for(i = 0; i < num; i++) {
1440 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1441 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1442 continue;
1443 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1444 manage(wins[i], &wa);
1445 }
1446 for(i = 0; i < num; i++) { /* now the transients */
1447 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1448 continue;
1449 if(XGetTransientForHint(dpy, wins[i], &d1)
1450 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1451 manage(wins[i], &wa);
1452 }
1453 if(wins)
1454 XFree(wins);
1455 }
1456 }
1457
1458 void
1459 sendmon(Client *c, Monitor *m) {
1460 if(c->mon == m)
1461 return;
1462 unfocus(c);
1463 detach(c);
1464 detachstack(c);
1465 c->mon = m;
1466 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1467 attach(c);
1468 attachstack(c);
1469 focus(NULL);
1470 arrange(NULL);
1471 }
1472
1473 void
1474 setclientstate(Client *c, long state) {
1475 long data[] = { state, None };
1476
1477 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1478 PropModeReplace, (unsigned char *)data, 2);
1479 }
1480
1481 void
1482 setlayout(const Arg *arg) {
1483 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1484 selmon->sellt ^= 1;
1485 if(arg && arg->v)
1486 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1487 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1488 if(selmon->sel)
1489 arrange(selmon);
1490 else
1491 drawbar(selmon);
1492 }
1493
1494 /* arg > 1.0 will set mfact absolutly */
1495 void
1496 setmfact(const Arg *arg) {
1497 float f;
1498
1499 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1500 return;
1501 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1502 if(f < 0.1 || f > 0.9)
1503 return;
1504 selmon->mfact = f;
1505 arrange(selmon);
1506 }
1507
1508 void
1509 setup(void) {
1510 XSetWindowAttributes wa;
1511
1512 /* clean up any zombies immediately */
1513 sigchld(0);
1514
1515 /* init screen */
1516 screen = DefaultScreen(dpy);
1517 root = RootWindow(dpy, screen);
1518 initfont(font);
1519 sw = DisplayWidth(dpy, screen);
1520 sh = DisplayHeight(dpy, screen);
1521 bh = dc.h = dc.font.height + 2;
1522 updategeom();
1523 /* init atoms */
1524 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1525 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1526 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1527 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1528 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1529 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1530 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1531 /* init cursors */
1532 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1533 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1534 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1535 /* init appearance */
1536 dc.norm[ColBorder] = getcolor(normbordercolor);
1537 dc.norm[ColBG] = getcolor(normbgcolor);
1538 dc.norm[ColFG] = getcolor(normfgcolor);
1539 dc.sel[ColBorder] = getcolor(selbordercolor);
1540 dc.sel[ColBG] = getcolor(selbgcolor);
1541 dc.sel[ColFG] = getcolor(selfgcolor);
1542 dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
1543 dc.gc = XCreateGC(dpy, root, 0, NULL);
1544 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1545 if(!dc.font.set)
1546 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
1547 /* init bars */
1548 updatebars();
1549 updatestatus();
1550 /* EWMH support per view */
1551 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1552 PropModeReplace, (unsigned char *) netatom, NetLast);
1553 /* select for events */
1554 wa.cursor = cursor[CurNormal];
1555 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
1556 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
1557 |PropertyChangeMask;
1558 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1559 XSelectInput(dpy, root, wa.event_mask);
1560 grabkeys();
1561 }
1562
1563 void
1564 showhide(Client *c) {
1565 if(!c)
1566 return;
1567 if(ISVISIBLE(c)) { /* show clients top down */
1568 XMoveWindow(dpy, c->win, c->x, c->y);
1569 if(!c->mon->lt[c->mon->sellt]->arrange || c->isfloating)
1570 resize(c, c->x, c->y, c->w, c->h, False);
1571 showhide(c->snext);
1572 }
1573 else { /* hide clients bottom up */
1574 showhide(c->snext);
1575 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
1576 }
1577 }
1578
1579
1580 void
1581 sigchld(int unused) {
1582 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1583 die("Can't install SIGCHLD handler");
1584 while(0 < waitpid(-1, NULL, WNOHANG));
1585 }
1586
1587 void
1588 spawn(const Arg *arg) {
1589 if(fork() == 0) {
1590 if(dpy)
1591 close(ConnectionNumber(dpy));
1592 setsid();
1593 execvp(((char **)arg->v)[0], (char **)arg->v);
1594 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1595 perror(" failed");
1596 exit(0);
1597 }
1598 }
1599
1600 void
1601 tag(const Arg *arg) {
1602 if(selmon->sel && arg->ui & TAGMASK) {
1603 selmon->sel->tags = arg->ui & TAGMASK;
1604 arrange(selmon);
1605 }
1606 }
1607
1608 void
1609 tagmon(const Arg *arg) {
1610 if(!selmon->sel || !mons->next)
1611 return;
1612 sendmon(selmon->sel, dirtomon(arg->i));
1613 }
1614
1615 int
1616 textnw(const char *text, unsigned int len) {
1617 XRectangle r;
1618
1619 if(dc.font.set) {
1620 XmbTextExtents(dc.font.set, text, len, NULL, &r);
1621 return r.width;
1622 }
1623 return XTextWidth(dc.font.xfont, text, len);
1624 }
1625
1626 void
1627 tile(Monitor *m) {
1628 int x, y, h, w, mw;
1629 unsigned int i, n;
1630 Client *c;
1631
1632 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1633 if(n == 0)
1634 return;
1635 /* master */
1636 c = nexttiled(m->clients);
1637 mw = m->mfact * m->ww;
1638 resize(c, m->wx, m->wy, (n == 1 ? m->ww : mw) - 2 * c->bw, m->wh - 2 * c->bw, False);
1639 if(--n == 0)
1640 return;
1641 /* tile stack */
1642 x = (m->wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : m->wx + mw;
1643 y = m->wy;
1644 w = (m->wx + mw > c->x + c->w) ? m->wx + m->ww - x : m->ww - mw;
1645 h = m->wh / n;
1646 if(h < bh)
1647 h = m->wh;
1648 for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
1649 resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
1650 ? m->wy + m->wh - y - 2 * c->bw : h - 2 * c->bw), False);
1651 if(h != m->wh)
1652 y = c->y + HEIGHT(c);
1653 }
1654 }
1655
1656 void
1657 togglebar(const Arg *arg) {
1658 selmon->showbar = !selmon->showbar;
1659 updatebarpos(selmon);
1660 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1661 arrange(selmon);
1662 }
1663
1664 void
1665 togglefloating(const Arg *arg) {
1666 if(!selmon->sel)
1667 return;
1668 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1669 if(selmon->sel->isfloating)
1670 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1671 selmon->sel->w, selmon->sel->h, False);
1672 arrange(selmon);
1673 }
1674
1675 void
1676 toggletag(const Arg *arg) {
1677 unsigned int newtags;
1678
1679 if(!selmon->sel)
1680 return;
1681 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1682 if(newtags) {
1683 selmon->sel->tags = newtags;
1684 arrange(selmon);
1685 }
1686 }
1687
1688 void
1689 toggleview(const Arg *arg) {
1690 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1691
1692 if(newtagset) {
1693 selmon->tagset[selmon->seltags] = newtagset;
1694 arrange(selmon);
1695 }
1696 }
1697
1698 void
1699 unfocus(Client *c) {
1700 if(!c)
1701 return;
1702 grabbuttons(c, False);
1703 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
1704 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1705 }
1706
1707 void
1708 unmanage(Client *c, Bool destroyed) {
1709 Monitor *m = c->mon;
1710 XWindowChanges wc;
1711
1712 /* The server grab construct avoids race conditions. */
1713 detach(c);
1714 detachstack(c);
1715 if(!destroyed) {
1716 wc.border_width = c->oldbw;
1717 XGrabServer(dpy);
1718 XSetErrorHandler(xerrordummy);
1719 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1720 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1721 setclientstate(c, WithdrawnState);
1722 XSync(dpy, False);
1723 XSetErrorHandler(xerror);
1724 XUngrabServer(dpy);
1725 }
1726 free(c);
1727 focus(NULL);
1728 arrange(m);
1729 }
1730
1731 void
1732 unmapnotify(XEvent *e) {
1733 Client *c;
1734 XUnmapEvent *ev = &e->xunmap;
1735
1736 if((c = wintoclient(ev->window)))
1737 unmanage(c, False);
1738 }
1739
1740 void
1741 updatebars(void) {
1742 Monitor *m;
1743 XSetWindowAttributes wa;
1744
1745 wa.override_redirect = True;
1746 wa.background_pixmap = ParentRelative;
1747 wa.event_mask = ButtonPressMask|ExposureMask;
1748 for(m = mons; m; m = m->next) {
1749 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1750 CopyFromParent, DefaultVisual(dpy, screen),
1751 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1752 XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
1753 XMapRaised(dpy, m->barwin);
1754 }
1755 }
1756
1757 void
1758 updatebarpos(Monitor *m) {
1759 m->wy = m->my;
1760 m->wh = m->mh;
1761 if(m->showbar) {
1762 m->wh -= bh;
1763 m->by = m->topbar ? m->wy : m->wy + m->wh;
1764 m->wy = m->topbar ? m->wy + bh : m->wy;
1765 }
1766 else
1767 m->by = -bh;
1768 }
1769
1770 Bool
1771 updategeom(void) {
1772 Bool dirty = False;
1773
1774 #ifdef XINERAMA
1775 if(XineramaIsActive(dpy)) {
1776 int i, j, n, nn;
1777 Client *c;
1778 Monitor *m;
1779 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1780 XineramaScreenInfo *unique = NULL;
1781
1782 info = XineramaQueryScreens(dpy, &nn);
1783 for(n = 0, m = mons; m; m = m->next, n++);
1784 /* only consider unique geometries as separate screens */
1785 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
1786 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
1787 for(i = 0, j = 0; i < nn; i++)
1788 if(isuniquegeom(unique, j, &info[i]))
1789 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1790 XFree(info);
1791 nn = j;
1792 if(n <= nn) {
1793 for(i = 0; i < (nn - n); i++) { /* new monitors available */
1794 for(m = mons; m && m->next; m = m->next);
1795 if(m)
1796 m->next = createmon();
1797 else
1798 mons = createmon();
1799 }
1800 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
1801 if(i >= n
1802 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1803 || unique[i].width != m->mw || unique[i].height != m->mh))
1804 {
1805 dirty = True;
1806 m->num = i;
1807 m->mx = m->wx = unique[i].x_org;
1808 m->my = m->wy = unique[i].y_org;
1809 m->mw = m->ww = unique[i].width;
1810 m->mh = m->wh = unique[i].height;
1811 updatebarpos(m);
1812 }
1813 }
1814 else { /* less monitors available nn < n */
1815 for(i = nn; i < n; i++) {
1816 for(m = mons; m && m->next; m = m->next);
1817 while(m->clients) {
1818 dirty = True;
1819 c = m->clients;
1820 m->clients = c->next;
1821 detachstack(c);
1822 c->mon = mons;
1823 attach(c);
1824 attachstack(c);
1825 }
1826 if(m == selmon)
1827 selmon = mons;
1828 cleanupmon(m);
1829 }
1830 }
1831 free(unique);
1832 }
1833 else
1834 #endif /* XINERAMA */
1835 /* default monitor setup */
1836 {
1837 if(!mons)
1838 mons = createmon();
1839 if(mons->mw != sw || mons->mh != sh) {
1840 dirty = True;
1841 mons->mw = mons->ww = sw;
1842 mons->mh = mons->wh = sh;
1843 updatebarpos(mons);
1844 }
1845 }
1846 if(dirty) {
1847 selmon = mons;
1848 selmon = wintomon(root);
1849 }
1850 return dirty;
1851 }
1852
1853 void
1854 updatenumlockmask(void) {
1855 unsigned int i, j;
1856 XModifierKeymap *modmap;
1857
1858 numlockmask = 0;
1859 modmap = XGetModifierMapping(dpy);
1860 for(i = 0; i < 8; i++)
1861 for(j = 0; j < modmap->max_keypermod; j++)
1862 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1863 == XKeysymToKeycode(dpy, XK_Num_Lock))
1864 numlockmask = (1 << i);
1865 XFreeModifiermap(modmap);
1866 }
1867
1868 void
1869 updatesizehints(Client *c) {
1870 long msize;
1871 XSizeHints size;
1872
1873 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
1874 /* size is uninitialized, ensure that size.flags aren't used */
1875 size.flags = PSize;
1876 if(size.flags & PBaseSize) {
1877 c->basew = size.base_width;
1878 c->baseh = size.base_height;
1879 }
1880 else if(size.flags & PMinSize) {
1881 c->basew = size.min_width;
1882 c->baseh = size.min_height;
1883 }
1884 else
1885 c->basew = c->baseh = 0;
1886 if(size.flags & PResizeInc) {
1887 c->incw = size.width_inc;
1888 c->inch = size.height_inc;
1889 }
1890 else
1891 c->incw = c->inch = 0;
1892 if(size.flags & PMaxSize) {
1893 c->maxw = size.max_width;
1894 c->maxh = size.max_height;
1895 }
1896 else
1897 c->maxw = c->maxh = 0;
1898 if(size.flags & PMinSize) {
1899 c->minw = size.min_width;
1900 c->minh = size.min_height;
1901 }
1902 else if(size.flags & PBaseSize) {
1903 c->minw = size.base_width;
1904 c->minh = size.base_height;
1905 }
1906 else
1907 c->minw = c->minh = 0;
1908 if(size.flags & PAspect) {
1909 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
1910 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
1911 }
1912 else
1913 c->maxa = c->mina = 0.0;
1914 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
1915 && c->maxw == c->minw && c->maxh == c->minh);
1916 }
1917
1918 void
1919 updatetitle(Client *c) {
1920 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
1921 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
1922 if(c->name[0] == '\0') /* hack to mark broken clients */
1923 strcpy(c->name, broken);
1924 }
1925
1926 void
1927 updatestatus(void) {
1928 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
1929 strcpy(stext, "dwm-"VERSION);
1930 drawbar(selmon);
1931 }
1932
1933 void
1934 updatewmhints(Client *c) {
1935 XWMHints *wmh;
1936
1937 if((wmh = XGetWMHints(dpy, c->win))) {
1938 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
1939 wmh->flags &= ~XUrgencyHint;
1940 XSetWMHints(dpy, c->win, wmh);
1941 }
1942 else
1943 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
1944 XFree(wmh);
1945 }
1946 }
1947
1948 void
1949 view(const Arg *arg) {
1950 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
1951 return;
1952 selmon->seltags ^= 1; /* toggle sel tagset */
1953 if(arg->ui & TAGMASK)
1954 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
1955 arrange(selmon);
1956 }
1957
1958 Client *
1959 wintoclient(Window w) {
1960 Client *c;
1961 Monitor *m;
1962
1963 for(m = mons; m; m = m->next)
1964 for(c = m->clients; c; c = c->next)
1965 if(c->win == w)
1966 return c;
1967 return NULL;
1968 }
1969
1970 Monitor *
1971 wintomon(Window w) {
1972 int x, y;
1973 Client *c;
1974 Monitor *m;
1975
1976 if(w == root && getrootptr(&x, &y))
1977 return ptrtomon(x, y);
1978 for(m = mons; m; m = m->next)
1979 if(w == m->barwin)
1980 return m;
1981 if((c = wintoclient(w)))
1982 return c->mon;
1983 return selmon;
1984 }
1985
1986 /* There's no way to check accesses to destroyed windows, thus those cases are
1987 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1988 * default error handler, which may call exit. */
1989 int
1990 xerror(Display *dpy, XErrorEvent *ee) {
1991 if(ee->error_code == BadWindow
1992 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
1993 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
1994 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
1995 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
1996 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
1997 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
1998 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
1999 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2000 return 0;
2001 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2002 ee->request_code, ee->error_code);
2003 return xerrorxlib(dpy, ee); /* may call exit */
2004 }
2005
2006 int
2007 xerrordummy(Display *dpy, XErrorEvent *ee) {
2008 return 0;
2009 }
2010
2011 /* Startup Error handler to check if another window manager
2012 * is already running. */
2013 int
2014 xerrorstart(Display *dpy, XErrorEvent *ee) {
2015 otherwm = True;
2016 return -1;
2017 }
2018
2019 void
2020 zoom(const Arg *arg) {
2021 Client *c = selmon->sel;
2022
2023 if(!selmon->lt[selmon->sellt]->arrange
2024 || selmon->lt[selmon->sellt]->arrange == monocle
2025 || (selmon->sel && selmon->sel->isfloating))
2026 return;
2027 if(c == nexttiled(selmon->clients))
2028 if(!c || !(c = nexttiled(c->next)))
2029 return;
2030 detach(c);
2031 attach(c);
2032 focus(c);
2033 arrange(c->mon);
2034 }
2035
2036 int
2037 main(int argc, char *argv[]) {
2038 if(argc == 2 && !strcmp("-v", argv[1]))
2039 die("dwm-"VERSION", © 2006-2009 dwm engineers, see LICENSE for details\n");
2040 else if(argc != 1)
2041 die("usage: dwm [-v]\n");
2042 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2043 fputs("warning: no locale support\n", stderr);
2044 if(!(dpy = XOpenDisplay(NULL)))
2045 die("dwm: cannot open display\n");
2046 checkotherwm();
2047 setup();
2048 scan();
2049 run();
2050 cleanup();
2051 XCloseDisplay(dpy);
2052 return 0;
2053 }