Xinqi Bao's Git

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