Xinqi Bao's Git

4a8ee32f0185f4130d8d0cf3b41b29bf0faf4147
[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 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 while(n--) {
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 n;
1018 Atom *protocols;
1019 Bool ret = False;
1020
1021 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1022 while(!ret && n--)
1023 ret = protocols[n] == wmatom[WMDelete];
1024 XFree(protocols);
1025 }
1026 return ret;
1027 }
1028
1029 #ifdef XINERAMA
1030 static Bool
1031 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
1032 while(n--)
1033 if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1034 && unique[n].width == info->width && unique[n].height == info->height)
1035 return False;
1036 return True;
1037 }
1038 #endif /* XINERAMA */
1039
1040 void
1041 keypress(XEvent *e) {
1042 unsigned int i;
1043 KeySym keysym;
1044 XKeyEvent *ev;
1045
1046 ev = &e->xkey;
1047 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1048 for(i = 0; i < LENGTH(keys); i++)
1049 if(keysym == keys[i].keysym
1050 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1051 && keys[i].func)
1052 keys[i].func(&(keys[i].arg));
1053 }
1054
1055 void
1056 killclient(const Arg *arg) {
1057 XEvent ev;
1058
1059 if(!selmon->sel)
1060 return;
1061 if(isprotodel(selmon->sel)) {
1062 ev.type = ClientMessage;
1063 ev.xclient.window = selmon->sel->win;
1064 ev.xclient.message_type = wmatom[WMProtocols];
1065 ev.xclient.format = 32;
1066 ev.xclient.data.l[0] = wmatom[WMDelete];
1067 ev.xclient.data.l[1] = CurrentTime;
1068 XSendEvent(dpy, selmon->sel->win, False, NoEventMask, &ev);
1069 }
1070 else {
1071 XGrabServer(dpy);
1072 XSetErrorHandler(xerrordummy);
1073 XSetCloseDownMode(dpy, DestroyAll);
1074 XKillClient(dpy, selmon->sel->win);
1075 XSync(dpy, False);
1076 XSetErrorHandler(xerror);
1077 XUngrabServer(dpy);
1078 }
1079 }
1080
1081 void
1082 manage(Window w, XWindowAttributes *wa) {
1083 Client *c, *t = NULL;
1084 Window trans = None;
1085 XWindowChanges wc;
1086
1087 if(!(c = calloc(1, sizeof(Client))))
1088 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1089 c->win = w;
1090 updatetitle(c);
1091 if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1092 c->mon = t->mon;
1093 c->tags = t->tags;
1094 }
1095 else {
1096 c->mon = selmon;
1097 applyrules(c);
1098 }
1099 /* geometry */
1100 c->x = c->oldx = wa->x + c->mon->wx;
1101 c->y = c->oldy = wa->y + c->mon->wy;
1102 c->w = c->oldw = wa->width;
1103 c->h = c->oldh = wa->height;
1104 c->oldbw = wa->border_width;
1105 if(c->w == c->mon->mw && c->h == c->mon->mh) {
1106 c->isfloating = True;
1107 c->x = c->mon->mx;
1108 c->y = c->mon->my;
1109 c->bw = 0;
1110 }
1111 else {
1112 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1113 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1114 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1115 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1116 c->x = MAX(c->x, c->mon->mx);
1117 /* only fix client y-offset, if the client center might cover the bar */
1118 c->y = MAX(c->y, ((c->mon->by == 0) && (c->x + (c->w / 2) >= c->mon->wx)
1119 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1120 c->bw = borderpx;
1121 }
1122 wc.border_width = c->bw;
1123 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1124 XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
1125 configure(c); /* propagates border_width, if size doesn't change */
1126 updatesizehints(c);
1127 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1128 grabbuttons(c, False);
1129 if(!c->isfloating)
1130 c->isfloating = c->oldstate = trans != None || c->isfixed;
1131 if(c->isfloating)
1132 XRaiseWindow(dpy, c->win);
1133 attach(c);
1134 attachstack(c);
1135 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1136 XMapWindow(dpy, c->win);
1137 setclientstate(c, NormalState);
1138 arrange(c->mon);
1139 }
1140
1141 void
1142 mappingnotify(XEvent *e) {
1143 XMappingEvent *ev = &e->xmapping;
1144
1145 XRefreshKeyboardMapping(ev);
1146 if(ev->request == MappingKeyboard)
1147 grabkeys();
1148 }
1149
1150 void
1151 maprequest(XEvent *e) {
1152 static XWindowAttributes wa;
1153 XMapRequestEvent *ev = &e->xmaprequest;
1154
1155 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1156 return;
1157 if(wa.override_redirect)
1158 return;
1159 if(!wintoclient(ev->window))
1160 manage(ev->window, &wa);
1161 }
1162
1163 void
1164 monocle(Monitor *m) {
1165 unsigned int n = 0;
1166 Client *c;
1167
1168 for(c = m->clients; c; c = c->next)
1169 if(ISVISIBLE(c))
1170 n++;
1171 if(n > 0) /* override layout symbol */
1172 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1173 for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
1174 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
1175 }
1176
1177 void
1178 movemouse(const Arg *arg) {
1179 int x, y, ocx, ocy, nx, ny;
1180 Client *c;
1181 Monitor *m;
1182 XEvent ev;
1183
1184 if(!(c = selmon->sel))
1185 return;
1186 restack(selmon);
1187 ocx = c->x;
1188 ocy = c->y;
1189 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1190 None, cursor[CurMove], CurrentTime) != GrabSuccess)
1191 return;
1192 if(!getrootptr(&x, &y))
1193 return;
1194 do {
1195 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1196 switch (ev.type) {
1197 case ConfigureRequest:
1198 case Expose:
1199 case MapRequest:
1200 handler[ev.type](&ev);
1201 break;
1202 case MotionNotify:
1203 nx = ocx + (ev.xmotion.x - x);
1204 ny = ocy + (ev.xmotion.y - y);
1205 if(snap && nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1206 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1207 if(abs(selmon->wx - nx) < snap)
1208 nx = selmon->wx;
1209 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1210 nx = selmon->wx + selmon->ww - WIDTH(c);
1211 if(abs(selmon->wy - ny) < snap)
1212 ny = selmon->wy;
1213 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1214 ny = selmon->wy + selmon->wh - HEIGHT(c);
1215 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1216 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1217 togglefloating(NULL);
1218 }
1219 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1220 resize(c, nx, ny, c->w, c->h, True);
1221 break;
1222 }
1223 } while(ev.type != ButtonRelease);
1224 XUngrabPointer(dpy, CurrentTime);
1225 if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
1226 sendmon(c, m);
1227 selmon = m;
1228 focus(NULL);
1229 }
1230 }
1231
1232 Client *
1233 nexttiled(Client *c) {
1234 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1235 return c;
1236 }
1237
1238 Monitor *
1239 ptrtomon(int x, int y) {
1240 Monitor *m;
1241
1242 for(m = mons; m; m = m->next)
1243 if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh))
1244 return m;
1245 return selmon;
1246 }
1247
1248 void
1249 propertynotify(XEvent *e) {
1250 Client *c;
1251 Window trans;
1252 XPropertyEvent *ev = &e->xproperty;
1253
1254 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1255 updatestatus();
1256 else if(ev->state == PropertyDelete)
1257 return; /* ignore */
1258 else if((c = wintoclient(ev->window))) {
1259 switch (ev->atom) {
1260 default: break;
1261 case XA_WM_TRANSIENT_FOR:
1262 if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1263 (c->isfloating = (wintoclient(trans)) != NULL))
1264 arrange(c->mon);
1265 break;
1266 case XA_WM_NORMAL_HINTS:
1267 updatesizehints(c);
1268 break;
1269 case XA_WM_HINTS:
1270 updatewmhints(c);
1271 drawbars();
1272 break;
1273 }
1274 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1275 updatetitle(c);
1276 if(c == c->mon->sel)
1277 drawbar(c->mon);
1278 }
1279 }
1280 }
1281
1282 void
1283 clientmessage(XEvent *e) {
1284 XClientMessageEvent *cme = &e->xclient;
1285 Client *c;
1286
1287 if((c = wintoclient(cme->window))
1288 && (cme->message_type == netatom[NetWMState] && cme->data.l[1] == netatom[NetWMFullscreen]))
1289 {
1290 if(cme->data.l[0]) {
1291 XChangeProperty(dpy, cme->window, netatom[NetWMState], XA_ATOM, 32,
1292 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1293 c->oldstate = c->isfloating;
1294 c->oldbw = c->bw;
1295 c->bw = 0;
1296 c->isfloating = True;
1297 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1298 XRaiseWindow(dpy, c->win);
1299 }
1300 else {
1301 XChangeProperty(dpy, cme->window, netatom[NetWMState], XA_ATOM, 32,
1302 PropModeReplace, (unsigned char*)0, 0);
1303 c->isfloating = c->oldstate;
1304 c->bw = c->oldbw;
1305 c->x = c->oldx;
1306 c->y = c->oldy;
1307 c->w = c->oldw;
1308 c->h = c->oldh;
1309 resizeclient(c, c->x, c->y, c->w, c->h);
1310 arrange(c->mon);
1311 }
1312 }
1313 }
1314
1315 void
1316 quit(const Arg *arg) {
1317 running = False;
1318 }
1319
1320 void
1321 resize(Client *c, int x, int y, int w, int h, Bool interact) {
1322 if(applysizehints(c, &x, &y, &w, &h, interact))
1323 resizeclient(c, x, y, w, h);
1324 }
1325
1326 void
1327 resizeclient(Client *c, int x, int y, int w, int h) {
1328 XWindowChanges wc;
1329
1330 c->oldx = c->x; c->x = wc.x = x;
1331 c->oldy = c->y; c->y = wc.y = y;
1332 c->oldw = c->w; c->w = wc.width = w;
1333 c->oldh = c->h; c->h = wc.height = h;
1334 wc.border_width = c->bw;
1335 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1336 configure(c);
1337 XSync(dpy, False);
1338 }
1339
1340 void
1341 resizemouse(const Arg *arg) {
1342 int ocx, ocy;
1343 int nw, nh;
1344 Client *c;
1345 Monitor *m;
1346 XEvent ev;
1347
1348 if(!(c = selmon->sel))
1349 return;
1350 restack(selmon);
1351 ocx = c->x;
1352 ocy = c->y;
1353 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1354 None, cursor[CurResize], CurrentTime) != GrabSuccess)
1355 return;
1356 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1357 do {
1358 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1359 switch(ev.type) {
1360 case ConfigureRequest:
1361 case Expose:
1362 case MapRequest:
1363 handler[ev.type](&ev);
1364 break;
1365 case MotionNotify:
1366 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1367 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1368 if(snap && c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1369 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1370 {
1371 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1372 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1373 togglefloating(NULL);
1374 }
1375 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1376 resize(c, c->x, c->y, nw, nh, True);
1377 break;
1378 }
1379 } while(ev.type != ButtonRelease);
1380 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1381 XUngrabPointer(dpy, CurrentTime);
1382 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1383 if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
1384 sendmon(c, m);
1385 selmon = m;
1386 focus(NULL);
1387 }
1388 }
1389
1390 void
1391 restack(Monitor *m) {
1392 Client *c;
1393 XEvent ev;
1394 XWindowChanges wc;
1395
1396 drawbar(m);
1397 if(!m->sel)
1398 return;
1399 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1400 XRaiseWindow(dpy, m->sel->win);
1401 if(m->lt[m->sellt]->arrange) {
1402 wc.stack_mode = Below;
1403 wc.sibling = m->barwin;
1404 for(c = m->stack; c; c = c->snext)
1405 if(!c->isfloating && ISVISIBLE(c)) {
1406 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1407 wc.sibling = c->win;
1408 }
1409 }
1410 XSync(dpy, False);
1411 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1412 }
1413
1414 void
1415 run(void) {
1416 XEvent ev;
1417 /* main event loop */
1418 XSync(dpy, False);
1419 while(running && !XNextEvent(dpy, &ev)) {
1420 if(handler[ev.type])
1421 handler[ev.type](&ev); /* call handler */
1422 }
1423 }
1424
1425 void
1426 scan(void) {
1427 unsigned int i, num;
1428 Window d1, d2, *wins = NULL;
1429 XWindowAttributes wa;
1430
1431 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1432 for(i = 0; i < num; i++) {
1433 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1434 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1435 continue;
1436 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1437 manage(wins[i], &wa);
1438 }
1439 for(i = 0; i < num; i++) { /* now the transients */
1440 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1441 continue;
1442 if(XGetTransientForHint(dpy, wins[i], &d1)
1443 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1444 manage(wins[i], &wa);
1445 }
1446 if(wins)
1447 XFree(wins);
1448 }
1449 }
1450
1451 void
1452 sendmon(Client *c, Monitor *m) {
1453 if(c->mon == m)
1454 return;
1455 unfocus(c, True);
1456 detach(c);
1457 detachstack(c);
1458 c->mon = m;
1459 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1460 attach(c);
1461 attachstack(c);
1462 focus(NULL);
1463 arrange(NULL);
1464 }
1465
1466 void
1467 setclientstate(Client *c, long state) {
1468 long data[] = { state, None };
1469
1470 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1471 PropModeReplace, (unsigned char *)data, 2);
1472 }
1473
1474 void
1475 setlayout(const Arg *arg) {
1476 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1477 selmon->sellt ^= 1;
1478 if(arg && arg->v)
1479 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1480 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1481 if(selmon->sel)
1482 arrange(selmon);
1483 else
1484 drawbar(selmon);
1485 }
1486
1487 /* arg > 1.0 will set mfact absolutly */
1488 void
1489 setmfact(const Arg *arg) {
1490 float f;
1491
1492 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1493 return;
1494 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1495 if(f < 0.1 || f > 0.9)
1496 return;
1497 selmon->mfact = f;
1498 arrange(selmon);
1499 }
1500
1501 void
1502 setup(void) {
1503 XSetWindowAttributes wa;
1504
1505 /* clean up any zombies immediately */
1506 sigchld(0);
1507
1508 /* init screen */
1509 screen = DefaultScreen(dpy);
1510 root = RootWindow(dpy, screen);
1511 initfont(font);
1512 sw = DisplayWidth(dpy, screen);
1513 sh = DisplayHeight(dpy, screen);
1514 bh = dc.h = dc.font.height + 2;
1515 updategeom();
1516 /* init atoms */
1517 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1518 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1519 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1520 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1521 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1522 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1523 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1524 /* init cursors */
1525 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1526 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1527 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1528 /* init appearance */
1529 dc.norm[ColBorder] = getcolor(normbordercolor);
1530 dc.norm[ColBG] = getcolor(normbgcolor);
1531 dc.norm[ColFG] = getcolor(normfgcolor);
1532 dc.sel[ColBorder] = getcolor(selbordercolor);
1533 dc.sel[ColBG] = getcolor(selbgcolor);
1534 dc.sel[ColFG] = getcolor(selfgcolor);
1535 dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
1536 dc.gc = XCreateGC(dpy, root, 0, NULL);
1537 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1538 if(!dc.font.set)
1539 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
1540 /* init bars */
1541 updatebars();
1542 updatestatus();
1543 /* EWMH support per view */
1544 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1545 PropModeReplace, (unsigned char *) netatom, NetLast);
1546 /* select for events */
1547 wa.cursor = cursor[CurNormal];
1548 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
1549 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
1550 |PropertyChangeMask;
1551 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1552 XSelectInput(dpy, root, wa.event_mask);
1553 grabkeys();
1554 }
1555
1556 void
1557 showhide(Client *c) {
1558 if(!c)
1559 return;
1560 if(ISVISIBLE(c)) { /* show clients top down */
1561 XMoveWindow(dpy, c->win, c->x, c->y);
1562 if(!c->mon->lt[c->mon->sellt]->arrange || c->isfloating)
1563 resize(c, c->x, c->y, c->w, c->h, False);
1564 showhide(c->snext);
1565 }
1566 else { /* hide clients bottom up */
1567 showhide(c->snext);
1568 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
1569 }
1570 }
1571
1572 void
1573 sigchld(int unused) {
1574 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1575 die("Can't install SIGCHLD handler");
1576 while(0 < waitpid(-1, NULL, WNOHANG));
1577 }
1578
1579 void
1580 spawn(const Arg *arg) {
1581 if(fork() == 0) {
1582 if(dpy)
1583 close(ConnectionNumber(dpy));
1584 setsid();
1585 execvp(((char **)arg->v)[0], (char **)arg->v);
1586 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1587 perror(" failed");
1588 exit(0);
1589 }
1590 }
1591
1592 void
1593 tag(const Arg *arg) {
1594 if(selmon->sel && arg->ui & TAGMASK) {
1595 selmon->sel->tags = arg->ui & TAGMASK;
1596 arrange(selmon);
1597 }
1598 }
1599
1600 void
1601 tagmon(const Arg *arg) {
1602 if(!selmon->sel || !mons->next)
1603 return;
1604 sendmon(selmon->sel, dirtomon(arg->i));
1605 }
1606
1607 int
1608 textnw(const char *text, unsigned int len) {
1609 XRectangle r;
1610
1611 if(dc.font.set) {
1612 XmbTextExtents(dc.font.set, text, len, NULL, &r);
1613 return r.width;
1614 }
1615 return XTextWidth(dc.font.xfont, text, len);
1616 }
1617
1618 void
1619 tile(Monitor *m) {
1620 int x, y, h, w, mw;
1621 unsigned int i, n;
1622 Client *c;
1623
1624 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1625 if(n == 0)
1626 return;
1627 /* master */
1628 c = nexttiled(m->clients);
1629 mw = m->mfact * m->ww;
1630 resize(c, m->wx, m->wy, (n == 1 ? m->ww : mw) - 2 * c->bw, m->wh - 2 * c->bw, False);
1631 if(--n == 0)
1632 return;
1633 /* tile stack */
1634 x = (m->wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : m->wx + mw;
1635 y = m->wy;
1636 w = (m->wx + mw > c->x + c->w) ? m->wx + m->ww - x : m->ww - mw;
1637 h = m->wh / n;
1638 if(h < bh)
1639 h = m->wh;
1640 for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
1641 resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
1642 ? m->wy + m->wh - y - 2 * c->bw : h - 2 * c->bw), False);
1643 if(h != m->wh)
1644 y = c->y + HEIGHT(c);
1645 }
1646 }
1647
1648 void
1649 togglebar(const Arg *arg) {
1650 selmon->showbar = !selmon->showbar;
1651 updatebarpos(selmon);
1652 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1653 arrange(selmon);
1654 }
1655
1656 void
1657 togglefloating(const Arg *arg) {
1658 if(!selmon->sel)
1659 return;
1660 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1661 if(selmon->sel->isfloating)
1662 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1663 selmon->sel->w, selmon->sel->h, False);
1664 arrange(selmon);
1665 }
1666
1667 void
1668 toggletag(const Arg *arg) {
1669 unsigned int newtags;
1670
1671 if(!selmon->sel)
1672 return;
1673 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1674 if(newtags) {
1675 selmon->sel->tags = newtags;
1676 arrange(selmon);
1677 }
1678 }
1679
1680 void
1681 toggleview(const Arg *arg) {
1682 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1683
1684 if(newtagset) {
1685 selmon->tagset[selmon->seltags] = newtagset;
1686 arrange(selmon);
1687 }
1688 }
1689
1690 void
1691 unfocus(Client *c, Bool setfocus) {
1692 if(!c)
1693 return;
1694 grabbuttons(c, False);
1695 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
1696 if(setfocus)
1697 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1698 }
1699
1700 void
1701 unmanage(Client *c, Bool destroyed) {
1702 Monitor *m = c->mon;
1703 XWindowChanges wc;
1704
1705 /* The server grab construct avoids race conditions. */
1706 detach(c);
1707 detachstack(c);
1708 if(!destroyed) {
1709 wc.border_width = c->oldbw;
1710 XGrabServer(dpy);
1711 XSetErrorHandler(xerrordummy);
1712 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1713 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1714 setclientstate(c, WithdrawnState);
1715 XSync(dpy, False);
1716 XSetErrorHandler(xerror);
1717 XUngrabServer(dpy);
1718 }
1719 free(c);
1720 focus(NULL);
1721 arrange(m);
1722 }
1723
1724 void
1725 unmapnotify(XEvent *e) {
1726 Client *c;
1727 XUnmapEvent *ev = &e->xunmap;
1728
1729 if((c = wintoclient(ev->window)))
1730 unmanage(c, False);
1731 }
1732
1733 void
1734 updatebars(void) {
1735 Monitor *m;
1736 XSetWindowAttributes wa;
1737
1738 wa.override_redirect = True;
1739 wa.background_pixmap = ParentRelative;
1740 wa.event_mask = ButtonPressMask|ExposureMask;
1741 for(m = mons; m; m = m->next) {
1742 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1743 CopyFromParent, DefaultVisual(dpy, screen),
1744 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1745 XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
1746 XMapRaised(dpy, m->barwin);
1747 }
1748 }
1749
1750 void
1751 updatebarpos(Monitor *m) {
1752 m->wy = m->my;
1753 m->wh = m->mh;
1754 if(m->showbar) {
1755 m->wh -= bh;
1756 m->by = m->topbar ? m->wy : m->wy + m->wh;
1757 m->wy = m->topbar ? m->wy + bh : m->wy;
1758 }
1759 else
1760 m->by = -bh;
1761 }
1762
1763 Bool
1764 updategeom(void) {
1765 Bool dirty = False;
1766
1767 #ifdef XINERAMA
1768 if(XineramaIsActive(dpy)) {
1769 int i, j, n, nn;
1770 Client *c;
1771 Monitor *m;
1772 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1773 XineramaScreenInfo *unique = NULL;
1774
1775 for(n = 0, m = mons; m; m = m->next, n++);
1776 /* only consider unique geometries as separate screens */
1777 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
1778 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
1779 for(i = 0, j = 0; i < nn; i++)
1780 if(isuniquegeom(unique, j, &info[i]))
1781 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1782 XFree(info);
1783 nn = j;
1784 if(n <= nn) {
1785 for(i = 0; i < (nn - n); i++) { /* new monitors available */
1786 for(m = mons; m && m->next; m = m->next);
1787 if(m)
1788 m->next = createmon();
1789 else
1790 mons = createmon();
1791 }
1792 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
1793 if(i >= n
1794 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1795 || unique[i].width != m->mw || unique[i].height != m->mh))
1796 {
1797 dirty = True;
1798 m->num = i;
1799 m->mx = m->wx = unique[i].x_org;
1800 m->my = m->wy = unique[i].y_org;
1801 m->mw = m->ww = unique[i].width;
1802 m->mh = m->wh = unique[i].height;
1803 updatebarpos(m);
1804 }
1805 }
1806 else { /* less monitors available nn < n */
1807 for(i = nn; i < n; i++) {
1808 for(m = mons; m && m->next; m = m->next);
1809 while(m->clients) {
1810 dirty = True;
1811 c = m->clients;
1812 m->clients = c->next;
1813 detachstack(c);
1814 c->mon = mons;
1815 attach(c);
1816 attachstack(c);
1817 }
1818 if(m == selmon)
1819 selmon = mons;
1820 cleanupmon(m);
1821 }
1822 }
1823 free(unique);
1824 }
1825 else
1826 #endif /* XINERAMA */
1827 /* default monitor setup */
1828 {
1829 if(!mons)
1830 mons = createmon();
1831 if(mons->mw != sw || mons->mh != sh) {
1832 dirty = True;
1833 mons->mw = mons->ww = sw;
1834 mons->mh = mons->wh = sh;
1835 updatebarpos(mons);
1836 }
1837 }
1838 if(dirty) {
1839 selmon = mons;
1840 selmon = wintomon(root);
1841 }
1842 return dirty;
1843 }
1844
1845 void
1846 updatenumlockmask(void) {
1847 unsigned int i, j;
1848 XModifierKeymap *modmap;
1849
1850 numlockmask = 0;
1851 modmap = XGetModifierMapping(dpy);
1852 for(i = 0; i < 8; i++)
1853 for(j = 0; j < modmap->max_keypermod; j++)
1854 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1855 == XKeysymToKeycode(dpy, XK_Num_Lock))
1856 numlockmask = (1 << i);
1857 XFreeModifiermap(modmap);
1858 }
1859
1860 void
1861 updatesizehints(Client *c) {
1862 long msize;
1863 XSizeHints size;
1864
1865 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
1866 /* size is uninitialized, ensure that size.flags aren't used */
1867 size.flags = PSize;
1868 if(size.flags & PBaseSize) {
1869 c->basew = size.base_width;
1870 c->baseh = size.base_height;
1871 }
1872 else if(size.flags & PMinSize) {
1873 c->basew = size.min_width;
1874 c->baseh = size.min_height;
1875 }
1876 else
1877 c->basew = c->baseh = 0;
1878 if(size.flags & PResizeInc) {
1879 c->incw = size.width_inc;
1880 c->inch = size.height_inc;
1881 }
1882 else
1883 c->incw = c->inch = 0;
1884 if(size.flags & PMaxSize) {
1885 c->maxw = size.max_width;
1886 c->maxh = size.max_height;
1887 }
1888 else
1889 c->maxw = c->maxh = 0;
1890 if(size.flags & PMinSize) {
1891 c->minw = size.min_width;
1892 c->minh = size.min_height;
1893 }
1894 else if(size.flags & PBaseSize) {
1895 c->minw = size.base_width;
1896 c->minh = size.base_height;
1897 }
1898 else
1899 c->minw = c->minh = 0;
1900 if(size.flags & PAspect) {
1901 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
1902 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
1903 }
1904 else
1905 c->maxa = c->mina = 0.0;
1906 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
1907 && c->maxw == c->minw && c->maxh == c->minh);
1908 }
1909
1910 void
1911 updatetitle(Client *c) {
1912 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
1913 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
1914 if(c->name[0] == '\0') /* hack to mark broken clients */
1915 strcpy(c->name, broken);
1916 }
1917
1918 void
1919 updatestatus(void) {
1920 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
1921 strcpy(stext, "dwm-"VERSION);
1922 drawbar(selmon);
1923 }
1924
1925 void
1926 updatewmhints(Client *c) {
1927 XWMHints *wmh;
1928
1929 if((wmh = XGetWMHints(dpy, c->win))) {
1930 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
1931 wmh->flags &= ~XUrgencyHint;
1932 XSetWMHints(dpy, c->win, wmh);
1933 }
1934 else
1935 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
1936 XFree(wmh);
1937 }
1938 }
1939
1940 void
1941 view(const Arg *arg) {
1942 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
1943 return;
1944 selmon->seltags ^= 1; /* toggle sel tagset */
1945 if(arg->ui & TAGMASK)
1946 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
1947 arrange(selmon);
1948 }
1949
1950 Client *
1951 wintoclient(Window w) {
1952 Client *c;
1953 Monitor *m;
1954
1955 for(m = mons; m; m = m->next)
1956 for(c = m->clients; c; c = c->next)
1957 if(c->win == w)
1958 return c;
1959 return NULL;
1960 }
1961
1962 Monitor *
1963 wintomon(Window w) {
1964 int x, y;
1965 Client *c;
1966 Monitor *m;
1967
1968 if(w == root && getrootptr(&x, &y))
1969 return ptrtomon(x, y);
1970 for(m = mons; m; m = m->next)
1971 if(w == m->barwin)
1972 return m;
1973 if((c = wintoclient(w)))
1974 return c->mon;
1975 return selmon;
1976 }
1977
1978 /* There's no way to check accesses to destroyed windows, thus those cases are
1979 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1980 * default error handler, which may call exit. */
1981 int
1982 xerror(Display *dpy, XErrorEvent *ee) {
1983 if(ee->error_code == BadWindow
1984 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
1985 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
1986 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
1987 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
1988 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
1989 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
1990 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
1991 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
1992 return 0;
1993 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
1994 ee->request_code, ee->error_code);
1995 return xerrorxlib(dpy, ee); /* may call exit */
1996 }
1997
1998 int
1999 xerrordummy(Display *dpy, XErrorEvent *ee) {
2000 return 0;
2001 }
2002
2003 /* Startup Error handler to check if another window manager
2004 * is already running. */
2005 int
2006 xerrorstart(Display *dpy, XErrorEvent *ee) {
2007 die("dwm: another window manager is already running\n");
2008 return -1;
2009 }
2010
2011 void
2012 zoom(const Arg *arg) {
2013 Client *c = selmon->sel;
2014
2015 if(!selmon->lt[selmon->sellt]->arrange
2016 || selmon->lt[selmon->sellt]->arrange == monocle
2017 || (selmon->sel && selmon->sel->isfloating))
2018 return;
2019 if(c == nexttiled(selmon->clients))
2020 if(!c || !(c = nexttiled(c->next)))
2021 return;
2022 detach(c);
2023 attach(c);
2024 focus(c);
2025 arrange(c->mon);
2026 }
2027
2028 int
2029 main(int argc, char *argv[]) {
2030 if(argc == 2 && !strcmp("-v", argv[1]))
2031 die("dwm-"VERSION", © 2006-2010 dwm engineers, see LICENSE for details\n");
2032 else if(argc != 1)
2033 die("usage: dwm [-v]\n");
2034 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2035 fputs("warning: no locale support\n", stderr);
2036 if(!(dpy = XOpenDisplay(NULL)))
2037 die("dwm: cannot open display\n");
2038 checkotherwm();
2039 setup();
2040 scan();
2041 run();
2042 cleanup();
2043 XCloseDisplay(dpy);
2044 return 0;
2045 }