Xinqi Bao's Git

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