Xinqi Bao's Git

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