1 /* See LICENSE file for copyright and license details.
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.
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
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
19 * Keys and tagging rules are organized as arrays and defined in config.h.
21 * To understand everything else, start reading main().
31 #include <sys/types.h>
33 #include <X11/cursorfont.h>
34 #include <X11/keysym.h>
35 #include <X11/Xatom.h>
37 #include <X11/Xproto.h>
38 #include <X11/Xutil.h>
40 #include <X11/extensions/Xinerama.h>
45 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
46 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask))
47 #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
48 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
49 #define LENGTH(X) (sizeof X / sizeof X[0])
50 #define MAX(A, B) ((A) > (B) ? (A) : (B))
51 #define MIN(A, B) ((A) < (B) ? (A) : (B))
52 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
53 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
54 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
55 #define TAGMASK ((1 << LENGTH(tags)) - 1)
56 #define TEXTW(X) (textnw(X, strlen(X)) + dc.font.height)
59 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
60 enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
61 enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
62 enum { WMProtocols, WMDelete, WMState, WMLast }; /* default atoms */
63 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
64 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
77 void (*func)(const Arg *arg);
81 typedef struct Monitor Monitor;
82 typedef struct Client Client;
87 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
90 Bool isfixed, isfloating, isurgent;
99 unsigned long norm[ColLast];
100 unsigned long sel[ColLast];
110 } DC; /* draw context */
115 void (*func)(const Arg *);
121 void (*arrange)(Monitor *);
128 int by; /* bar geometry */
129 int mx, my, mw, mh; /* screen size */
130 int wx, wy, ww, wh; /* window area */
131 unsigned int seltags;
133 unsigned int tagset[2];
146 const char *instance;
153 /* function declarations */
154 static void applyrules(Client *c);
155 static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
156 static void arrange(Monitor *m);
157 static void arrangemon(Monitor *m);
158 static void attach(Client *c);
159 static void attachstack(Client *c);
160 static void buttonpress(XEvent *e);
161 static void checkotherwm(void);
162 static void cleanup(void);
163 static void cleanupmon(Monitor *mon);
164 static void clearurgent(Client *c);
165 static void configure(Client *c);
166 static void configurenotify(XEvent *e);
167 static void configurerequest(XEvent *e);
168 static Monitor *createmon(void);
169 static void destroynotify(XEvent *e);
170 static void detach(Client *c);
171 static void detachstack(Client *c);
172 static void die(const char *errstr, ...);
173 static Monitor *dirtomon(int dir);
174 static void drawbar(Monitor *m);
175 static void drawbars(void);
176 static void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
177 static void drawtext(const char *text, unsigned long col[ColLast], Bool invert);
178 static void enternotify(XEvent *e);
179 static void expose(XEvent *e);
180 static void focus(Client *c);
181 static void focusin(XEvent *e);
182 static void focusmon(const Arg *arg);
183 static void focusstack(const Arg *arg);
184 static unsigned long getcolor(const char *colstr);
185 static Bool getrootptr(int *x, int *y);
186 static long getstate(Window w);
187 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
188 static void grabbuttons(Client *c, Bool focused);
189 static void grabkeys(void);
190 static void initfont(const char *fontstr);
191 static Bool isprotodel(Client *c);
192 static void keypress(XEvent *e);
193 static void killclient(const Arg *arg);
194 static void manage(Window w, XWindowAttributes *wa);
195 static void mappingnotify(XEvent *e);
196 static void maprequest(XEvent *e);
197 static void monocle(Monitor *m);
198 static void movemouse(const Arg *arg);
199 static Client *nexttiled(Client *c);
200 static Monitor *ptrtomon(int x, int y);
201 static void propertynotify(XEvent *e);
202 static void quit(const Arg *arg);
203 static void resize(Client *c, int x, int y, int w, int h, Bool interact);
204 static void resizemouse(const Arg *arg);
205 static void restack(Monitor *m);
206 static void run(void);
207 static void scan(void);
208 static void sendmon(Client *c, Monitor *m);
209 static void setclientstate(Client *c, long state);
210 static void setlayout(const Arg *arg);
211 static void setmfact(const Arg *arg);
212 static void setup(void);
213 static void showhide(Client *c);
214 static void sigchld(int unused);
215 static void spawn(const Arg *arg);
216 static void tag(const Arg *arg);
217 static void tagmon(const Arg *arg);
218 static int textnw(const char *text, unsigned int len);
219 static void tile(Monitor *);
220 static void togglebar(const Arg *arg);
221 static void togglefloating(const Arg *arg);
222 static void toggletag(const Arg *arg);
223 static void toggleview(const Arg *arg);
224 static void unfocus(Client *c);
225 static void unmanage(Client *c, Bool destroyed);
226 static void unmapnotify(XEvent *e);
227 static Bool updategeom(void);
228 static void updatebarpos(Monitor *m);
229 static void updatebars(void);
230 static void updatenumlockmask(void);
231 static void updatesizehints(Client *c);
232 static void updatestatus(void);
233 static void updatetitle(Client *c);
234 static void updatewmhints(Client *c);
235 static void view(const Arg *arg);
236 static Client *wintoclient(Window w);
237 static Monitor *wintomon(Window w);
238 static int xerror(Display *dpy, XErrorEvent *ee);
239 static int xerrordummy(Display *dpy, XErrorEvent *ee);
240 static int xerrorstart(Display *dpy, XErrorEvent *ee);
241 static void zoom(const Arg *arg);
244 static const char broken[] = "broken";
245 static char stext[256];
247 static int sw, sh; /* X display screen geometry width, height */
248 static int bh, blw = 0; /* bar geometry */
249 static int (*xerrorxlib)(Display *, XErrorEvent *);
250 static unsigned int numlockmask = 0;
251 static void (*handler[LASTEvent]) (XEvent *) = {
252 [ButtonPress] = buttonpress,
253 [ConfigureRequest] = configurerequest,
254 [ConfigureNotify] = configurenotify,
255 [DestroyNotify] = destroynotify,
256 [EnterNotify] = enternotify,
259 [KeyPress] = keypress,
260 [MappingNotify] = mappingnotify,
261 [MapRequest] = maprequest,
262 [PropertyNotify] = propertynotify,
263 [UnmapNotify] = unmapnotify
265 static Atom wmatom[WMLast], netatom[NetLast];
267 static Bool running = True;
268 static Cursor cursor[CurLast];
271 static Monitor *mons = NULL, *selmon = NULL;
274 /* configuration, allows nested code to access above variables */
277 /* compile-time check if all tags fit into an unsigned int bit array. */
278 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
280 /* function implementations */
282 applyrules(Client *c) {
283 const char *class, *instance;
287 XClassHint ch = { 0 };
290 c->isfloating = c->tags = 0;
291 if(XGetClassHint(dpy, c->win, &ch)) {
292 class = ch.res_class ? ch.res_class : broken;
293 instance = ch.res_name ? ch.res_name : broken;
294 for(i = 0; i < LENGTH(rules); i++) {
296 if((!r->title || strstr(c->name, r->title))
297 && (!r->class || strstr(class, r->class))
298 && (!r->instance || strstr(instance, r->instance)))
300 c->isfloating = r->isfloating;
302 for(m = mons; m && m->num != r->monitor; m = m->next);
312 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
316 applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
320 /* set minimum possible */
328 if(*x + *w + 2 * c->bw < 0)
330 if(*y + *h + 2 * c->bw < 0)
334 if(*x > m->mx + m->mw)
335 *x = m->mx + m->mw - WIDTH(c);
336 if(*y > m->my + m->mh)
337 *y = m->my + m->mh - HEIGHT(c);
338 if(*x + *w + 2 * c->bw < m->mx)
340 if(*y + *h + 2 * c->bw < m->my)
347 if(resizehints || c->isfloating) {
348 /* see last two sentences in ICCCM 4.1.2.3 */
349 baseismin = c->basew == c->minw && c->baseh == c->minh;
350 if(!baseismin) { /* temporarily remove base dimensions */
354 /* adjust for aspect limits */
355 if(c->mina > 0 && c->maxa > 0) {
356 if(c->maxa < (float)*w / *h)
357 *w = *h * c->maxa + 0.5;
358 else if(c->mina < (float)*h / *w)
359 *h = *w * c->mina + 0.5;
361 if(baseismin) { /* increment calculation requires this */
365 /* adjust for increment value */
370 /* restore base dimensions */
373 *w = MAX(*w, c->minw);
374 *h = MAX(*h, c->minh);
376 *w = MIN(*w, c->maxw);
378 *h = MIN(*h, c->maxh);
380 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
384 arrange(Monitor *m) {
387 else for(m = mons; m; m = m->next)
392 else for(m = mons; m; m = m->next)
397 arrangemon(Monitor *m) {
398 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
399 if(m->lt[m->sellt]->arrange)
400 m->lt[m->sellt]->arrange(m);
406 c->next = c->mon->clients;
411 attachstack(Client *c) {
412 c->snext = c->mon->stack;
417 buttonpress(XEvent *e) {
418 unsigned int i, x, click;
422 XButtonPressedEvent *ev = &e->xbutton;
425 /* focus monitor if necessary */
426 if((m = wintomon(ev->window)) && m != selmon) {
427 unfocus(selmon->sel);
431 if(ev->window == selmon->barwin) {
435 } while(ev->x >= x && ++i < LENGTH(tags));
436 if(i < LENGTH(tags)) {
440 else if(ev->x < x + blw)
442 else if(ev->x > selmon->wx + selmon->ww - TEXTW(stext))
443 click = ClkStatusText;
447 else if((c = wintoclient(ev->window))) {
449 click = ClkClientWin;
451 for(i = 0; i < LENGTH(buttons); i++)
452 if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
453 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
454 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
460 xerrorxlib = XSetErrorHandler(xerrorstart);
461 /* this causes an error if some other window manager is running */
462 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
465 die("dwm: another window manager is already running\n");
466 XSetErrorHandler(xerror);
473 Layout foo = { "", NULL };
477 selmon->lt[selmon->sellt] = &foo;
478 for(m = mons; m; m = m->next)
480 unmanage(m->stack, False);
482 XFreeFontSet(dpy, dc.font.set);
484 XFreeFont(dpy, dc.font.xfont);
485 XUngrabKey(dpy, AnyKey, AnyModifier, root);
486 XFreePixmap(dpy, dc.drawable);
488 XFreeCursor(dpy, cursor[CurNormal]);
489 XFreeCursor(dpy, cursor[CurResize]);
490 XFreeCursor(dpy, cursor[CurMove]);
494 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
498 cleanupmon(Monitor *mon) {
504 for(m = mons; m && m->next != mon; m = m->next);
507 XUnmapWindow(dpy, mon->barwin);
508 XDestroyWindow(dpy, mon->barwin);
513 clearurgent(Client *c) {
517 if(!(wmh = XGetWMHints(dpy, c->win)))
519 wmh->flags &= ~XUrgencyHint;
520 XSetWMHints(dpy, c->win, wmh);
525 configure(Client *c) {
528 ce.type = ConfigureNotify;
536 ce.border_width = c->bw;
538 ce.override_redirect = False;
539 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
543 configurenotify(XEvent *e) {
545 XConfigureEvent *ev = &e->xconfigure;
547 if(ev->window == root) {
552 XFreePixmap(dpy, dc.drawable);
553 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
555 for(m = mons; m; m = m->next)
556 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
563 configurerequest(XEvent *e) {
566 XConfigureRequestEvent *ev = &e->xconfigurerequest;
569 if((c = wintoclient(ev->window))) {
570 if(ev->value_mask & CWBorderWidth)
571 c->bw = ev->border_width;
572 else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
574 if(ev->value_mask & CWX)
575 c->x = m->mx + ev->x;
576 if(ev->value_mask & CWY)
577 c->y = m->my + ev->y;
578 if(ev->value_mask & CWWidth)
580 if(ev->value_mask & CWHeight)
582 if((c->x + c->w) > m->mx + m->mw && c->isfloating)
583 c->x = m->mx + (m->mw / 2 - c->w / 2); /* center in x direction */
584 if((c->y + c->h) > m->my + m->mh && c->isfloating)
585 c->y = m->my + (m->mh / 2 - c->h / 2); /* center in y direction */
586 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
589 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
597 wc.width = ev->width;
598 wc.height = ev->height;
599 wc.border_width = ev->border_width;
600 wc.sibling = ev->above;
601 wc.stack_mode = ev->detail;
602 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
611 if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
612 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
613 m->tagset[0] = m->tagset[1] = 1;
615 m->showbar = showbar;
617 m->lt[0] = &layouts[0];
618 m->lt[1] = &layouts[1 % LENGTH(layouts)];
619 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
624 destroynotify(XEvent *e) {
626 XDestroyWindowEvent *ev = &e->xdestroywindow;
628 if((c = wintoclient(ev->window)))
636 for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
641 detachstack(Client *c) {
644 for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
647 if(c == c->mon->sel) {
648 for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
654 die(const char *errstr, ...) {
657 va_start(ap, errstr);
658 vfprintf(stderr, errstr, ap);
668 if(!(m = selmon->next))
673 for(m = mons; m->next; m = m->next);
675 for(m = mons; m->next != selmon; m = m->next);
681 drawbar(Monitor *m) {
683 unsigned int i, occ = 0, urg = 0;
687 for(c = m->clients; c; c = c->next) {
693 for(i = 0; i < LENGTH(tags); i++) {
694 dc.w = TEXTW(tags[i]);
695 col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
696 drawtext(tags[i], col, urg & 1 << i);
697 drawsquare(m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
698 occ & 1 << i, urg & 1 << i, col);
701 dc.w = blw = TEXTW(m->ltsymbol);
702 drawtext(m->ltsymbol, dc.norm, False);
705 if(m == selmon) { /* status is only drawn on selected monitor */
712 drawtext(stext, dc.norm, False);
716 if((dc.w = dc.x - x) > bh) {
719 col = m == selmon ? dc.sel : dc.norm;
720 drawtext(m->sel->name, col, False);
721 drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
724 drawtext(NULL, dc.norm, False);
726 XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
734 for(m = mons; m; m = m->next)
739 drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
742 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
744 gcv.foreground = col[invert ? ColBG : ColFG];
745 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
746 x = (dc.font.ascent + dc.font.descent + 2) / 4;
750 r.width = r.height = x + 1;
751 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
754 r.width = r.height = x;
755 XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
760 drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
762 int i, x, y, h, len, olen;
763 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
765 XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
766 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
770 h = dc.font.ascent + dc.font.descent;
771 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
773 /* shorten text if necessary */
774 for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
777 memcpy(buf, text, len);
779 for(i = len; i && i > len - 3; buf[--i] = '.');
780 XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
782 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
784 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
788 enternotify(XEvent *e) {
791 XCrossingEvent *ev = &e->xcrossing;
793 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
795 if((m = wintomon(ev->window)) && m != selmon) {
796 unfocus(selmon->sel);
799 if((c = wintoclient(ev->window)))
808 XExposeEvent *ev = &e->xexpose;
810 if(ev->count == 0 && (m = wintomon(ev->window)))
816 if(!c || !ISVISIBLE(c))
817 for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
819 unfocus(selmon->sel);
827 grabbuttons(c, True);
828 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
829 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
832 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
838 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
839 XFocusChangeEvent *ev = &e->xfocus;
841 if(selmon->sel && ev->window != selmon->sel->win)
842 XSetInputFocus(dpy, selmon->sel->win, RevertToPointerRoot, CurrentTime);
846 focusmon(const Arg *arg) {
851 if((m = dirtomon(arg->i)) == selmon)
853 unfocus(selmon->sel);
859 focusstack(const Arg *arg) {
860 Client *c = NULL, *i;
865 for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
867 for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
870 for(i = selmon->clients; i != selmon->sel; i = i->next)
874 for(; i; i = i->next)
885 getcolor(const char *colstr) {
886 Colormap cmap = DefaultColormap(dpy, screen);
889 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
890 die("error, cannot allocate color '%s'\n", colstr);
895 getrootptr(int *x, int *y) {
900 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
907 unsigned char *p = NULL;
908 unsigned long n, extra;
911 status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
912 &real, &format, &n, &extra, (unsigned char **)&p);
913 if(status != Success)
922 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
927 if(!text || size == 0)
930 XGetTextProperty(dpy, w, &name, atom);
933 if(name.encoding == XA_STRING)
934 strncpy(text, (char *)name.value, size - 1);
936 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
937 strncpy(text, *list, size - 1);
938 XFreeStringList(list);
941 text[size - 1] = '\0';
947 grabbuttons(Client *c, Bool focused) {
951 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
952 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
954 for(i = 0; i < LENGTH(buttons); i++)
955 if(buttons[i].click == ClkClientWin)
956 for(j = 0; j < LENGTH(modifiers); j++)
957 XGrabButton(dpy, buttons[i].button,
958 buttons[i].mask | modifiers[j],
959 c->win, False, BUTTONMASK,
960 GrabModeAsync, GrabModeSync, None, None);
963 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
964 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
973 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
976 XUngrabKey(dpy, AnyKey, AnyModifier, root);
977 for(i = 0; i < LENGTH(keys); i++) {
978 if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
979 for(j = 0; j < LENGTH(modifiers); j++)
980 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
981 True, GrabModeAsync, GrabModeAsync);
987 initfont(const char *fontstr) {
988 char *def, **missing;
992 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
995 fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
996 XFreeStringList(missing);
999 XFontSetExtents *font_extents;
1000 XFontStruct **xfonts;
1003 dc.font.ascent = dc.font.descent = 0;
1004 font_extents = XExtentsOfFontSet(dc.font.set);
1005 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
1006 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
1007 dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
1008 dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
1013 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
1014 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
1015 die("error, cannot load font: '%s'\n", fontstr);
1016 dc.font.ascent = dc.font.xfont->ascent;
1017 dc.font.descent = dc.font.xfont->descent;
1019 dc.font.height = dc.font.ascent + dc.font.descent;
1023 isprotodel(Client *c) {
1028 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1029 for(i = 0; !ret && i < n; i++)
1030 if(protocols[i] == wmatom[WMDelete])
1039 isuniquegeom(XineramaScreenInfo *unique, size_t len, XineramaScreenInfo *info) {
1042 for(i = 0; i < len; i++)
1043 if(unique[i].x_org == info->x_org && unique[i].y_org == info->y_org
1044 && unique[i].width == info->width && unique[i].height == info->height)
1048 #endif /* XINERAMA */
1051 keypress(XEvent *e) {
1057 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1058 for(i = 0; i < LENGTH(keys); i++)
1059 if(keysym == keys[i].keysym
1060 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1062 keys[i].func(&(keys[i].arg));
1066 killclient(const Arg *arg) {
1071 if(isprotodel(selmon->sel)) {
1072 ev.type = ClientMessage;
1073 ev.xclient.window = selmon->sel->win;
1074 ev.xclient.message_type = wmatom[WMProtocols];
1075 ev.xclient.format = 32;
1076 ev.xclient.data.l[0] = wmatom[WMDelete];
1077 ev.xclient.data.l[1] = CurrentTime;
1078 XSendEvent(dpy, selmon->sel->win, False, NoEventMask, &ev);
1082 XSetErrorHandler(xerrordummy);
1083 XSetCloseDownMode(dpy, DestroyAll);
1084 XKillClient(dpy, selmon->sel->win);
1086 XSetErrorHandler(xerror);
1092 manage(Window w, XWindowAttributes *wa) {
1094 Client *c, *t = NULL;
1095 Window trans = None;
1098 if(!(c = malloc(sizeof(Client))))
1099 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1103 if(XGetTransientForHint(dpy, w, &trans))
1104 t = wintoclient(trans);
1114 c->x = wa->x + c->mon->wx;
1115 c->y = wa->y + c->mon->wy;
1118 c->oldbw = wa->border_width;
1119 if(c->w == c->mon->mw && c->h == c->mon->mh) {
1125 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1126 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1127 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1128 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1129 c->x = MAX(c->x, c->mon->mx);
1130 /* only fix client y-offset, if the client center might cover the bar */
1131 c->y = MAX(c->y, ((c->mon->by == 0) && (c->x + (c->w / 2) >= c->mon->wx)
1132 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1135 wc.border_width = c->bw;
1136 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1137 XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
1138 configure(c); /* propagates border_width, if size doesn't change */
1140 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1141 grabbuttons(c, False);
1143 c->isfloating = trans != None || c->isfixed;
1145 XRaiseWindow(dpy, c->win);
1148 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1149 XMapWindow(dpy, c->win);
1150 setclientstate(c, NormalState);
1155 mappingnotify(XEvent *e) {
1156 XMappingEvent *ev = &e->xmapping;
1158 XRefreshKeyboardMapping(ev);
1159 if(ev->request == MappingKeyboard)
1164 maprequest(XEvent *e) {
1165 static XWindowAttributes wa;
1166 XMapRequestEvent *ev = &e->xmaprequest;
1168 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1170 if(wa.override_redirect)
1172 if(!wintoclient(ev->window))
1173 manage(ev->window, &wa);
1177 monocle(Monitor *m) {
1181 for(c = m->clients; c; c = c->next)
1184 if(n > 0) /* override layout symbol */
1185 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1186 for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
1187 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
1191 movemouse(const Arg *arg) {
1192 int x, y, ocx, ocy, nx, ny;
1197 if(!(c = selmon->sel))
1202 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1203 None, cursor[CurMove], CurrentTime) != GrabSuccess)
1205 if(!getrootptr(&x, &y))
1208 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1210 case ConfigureRequest:
1213 handler[ev.type](&ev);
1216 nx = ocx + (ev.xmotion.x - x);
1217 ny = ocy + (ev.xmotion.y - y);
1218 if(snap && nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1219 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1220 if(abs(selmon->wx - nx) < snap)
1222 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1223 nx = selmon->wx + selmon->ww - WIDTH(c);
1224 if(abs(selmon->wy - ny) < snap)
1226 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1227 ny = selmon->wy + selmon->wh - HEIGHT(c);
1228 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1229 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1230 togglefloating(NULL);
1232 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1233 resize(c, nx, ny, c->w, c->h, True);
1236 } while(ev.type != ButtonRelease);
1237 XUngrabPointer(dpy, CurrentTime);
1238 if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
1246 nexttiled(Client *c) {
1247 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1252 ptrtomon(int x, int y) {
1255 for(m = mons; m; m = m->next)
1256 if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh))
1262 propertynotify(XEvent *e) {
1265 XPropertyEvent *ev = &e->xproperty;
1267 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1269 else if(ev->state == PropertyDelete)
1270 return; /* ignore */
1271 else if((c = wintoclient(ev->window))) {
1274 case XA_WM_TRANSIENT_FOR:
1275 XGetTransientForHint(dpy, c->win, &trans);
1276 if(!c->isfloating && (c->isfloating = (wintoclient(trans) != NULL)))
1279 case XA_WM_NORMAL_HINTS:
1287 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1289 if(c == c->mon->sel)
1296 quit(const Arg *arg) {
1301 resize(Client *c, int x, int y, int w, int h, Bool interact) {
1304 if(applysizehints(c, &x, &y, &w, &h, interact)) {
1307 c->w = wc.width = w;
1308 c->h = wc.height = h;
1309 wc.border_width = c->bw;
1310 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1317 resizemouse(const Arg *arg) {
1324 if(!(c = selmon->sel))
1329 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1330 None, cursor[CurResize], CurrentTime) != GrabSuccess)
1332 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1334 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1336 case ConfigureRequest:
1339 handler[ev.type](&ev);
1342 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1343 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1344 if(snap && nw >= selmon->wx && nw <= selmon->wx + selmon->ww
1345 && nh >= selmon->wy && nh <= selmon->wy + selmon->wh)
1347 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1348 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1349 togglefloating(NULL);
1351 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1352 resize(c, c->x, c->y, nw, nh, True);
1355 } while(ev.type != ButtonRelease);
1356 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1357 XUngrabPointer(dpy, CurrentTime);
1358 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1359 if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
1367 restack(Monitor *m) {
1375 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1376 XRaiseWindow(dpy, m->sel->win);
1377 if(m->lt[m->sellt]->arrange) {
1378 wc.stack_mode = Below;
1379 wc.sibling = m->barwin;
1380 for(c = m->stack; c; c = c->snext)
1381 if(!c->isfloating && ISVISIBLE(c)) {
1382 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1383 wc.sibling = c->win;
1387 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1393 static const char *evname[LASTEvent] = {
1394 [ButtonPress] = "buttonpress",
1395 [ConfigureRequest] = "configurerequest",
1396 [ConfigureNotify] = "configurenotify",
1397 [DestroyNotify] = "destroynotify",
1398 [EnterNotify] = "enternotify",
1399 [Expose] = "expose",
1400 [FocusIn] = "focusin",
1401 [KeyPress] = "keypress",
1402 [MappingNotify] = "mappingnotify",
1403 [MapRequest] = "maprequest",
1404 [PropertyNotify] = "propertynotify",
1405 [UnmapNotify] = "unmapnotify"
1407 /* main event loop */
1409 while(running && !XNextEvent(dpy, &ev)) {
1410 D fprintf(stderr, "run event %s %ld\n", evname[ev.type], ev.xany.window);
1411 if(handler[ev.type])
1412 handler[ev.type](&ev); /* call handler */
1418 unsigned int i, num;
1419 Window d1, d2, *wins = NULL;
1420 XWindowAttributes wa;
1422 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1423 for(i = 0; i < num; i++) {
1424 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1425 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1427 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1428 manage(wins[i], &wa);
1430 for(i = 0; i < num; i++) { /* now the transients */
1431 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1433 if(XGetTransientForHint(dpy, wins[i], &d1)
1434 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1435 manage(wins[i], &wa);
1443 sendmon(Client *c, Monitor *m) {
1450 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1458 setclientstate(Client *c, long state) {
1459 long data[] = { state, None };
1461 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1462 PropModeReplace, (unsigned char *)data, 2);
1466 setlayout(const Arg *arg) {
1467 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1470 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1471 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1478 /* arg > 1.0 will set mfact absolutly */
1480 setmfact(const Arg *arg) {
1483 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1485 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1486 if(f < 0.1 || f > 0.9)
1494 XSetWindowAttributes wa;
1496 /* clean up any zombies immediately */
1500 screen = DefaultScreen(dpy);
1501 root = RootWindow(dpy, screen);
1503 sw = DisplayWidth(dpy, screen);
1504 sh = DisplayHeight(dpy, screen);
1505 bh = dc.h = dc.font.height + 2;
1508 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1509 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1510 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1511 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1512 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1514 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1515 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1516 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1517 /* init appearance */
1518 dc.norm[ColBorder] = getcolor(normbordercolor);
1519 dc.norm[ColBG] = getcolor(normbgcolor);
1520 dc.norm[ColFG] = getcolor(normfgcolor);
1521 dc.sel[ColBorder] = getcolor(selbordercolor);
1522 dc.sel[ColBG] = getcolor(selbgcolor);
1523 dc.sel[ColFG] = getcolor(selfgcolor);
1524 dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
1525 dc.gc = XCreateGC(dpy, root, 0, NULL);
1526 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1528 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
1532 /* EWMH support per view */
1533 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1534 PropModeReplace, (unsigned char *) netatom, NetLast);
1535 /* select for events */
1536 wa.cursor = cursor[CurNormal];
1537 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
1538 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
1539 |PropertyChangeMask;
1540 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1541 XSelectInput(dpy, root, wa.event_mask);
1546 showhide(Client *c) {
1549 if(ISVISIBLE(c)) { /* show clients top down */
1550 XMoveWindow(dpy, c->win, c->x, c->y);
1551 if(!c->mon->lt[c->mon->sellt]->arrange || c->isfloating)
1552 resize(c, c->x, c->y, c->w, c->h, False);
1555 else { /* hide clients bottom up */
1557 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
1563 sigchld(int unused) {
1564 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1565 die("Can't install SIGCHLD handler");
1566 while(0 < waitpid(-1, NULL, WNOHANG));
1570 spawn(const Arg *arg) {
1573 close(ConnectionNumber(dpy));
1575 execvp(((char **)arg->v)[0], (char **)arg->v);
1576 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1583 tag(const Arg *arg) {
1584 if(selmon->sel && arg->ui & TAGMASK) {
1585 selmon->sel->tags = arg->ui & TAGMASK;
1591 tagmon(const Arg *arg) {
1592 if(!selmon->sel || !mons->next)
1594 sendmon(selmon->sel, dirtomon(arg->i));
1598 textnw(const char *text, unsigned int len) {
1602 XmbTextExtents(dc.font.set, text, len, NULL, &r);
1605 return XTextWidth(dc.font.xfont, text, len);
1614 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1618 c = nexttiled(m->clients);
1619 mw = m->mfact * m->ww;
1620 resize(c, m->wx, m->wy, (n == 1 ? m->ww : mw) - 2 * c->bw, m->wh - 2 * c->bw, False);
1624 x = (m->wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : m->wx + mw;
1626 w = (m->wx + mw > c->x + c->w) ? m->wx + m->ww - x : m->ww - mw;
1630 for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
1631 resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
1632 ? m->wy + m->wh - y - 2 * c->bw : h - 2 * c->bw), False);
1634 y = c->y + HEIGHT(c);
1639 togglebar(const Arg *arg) {
1640 selmon->showbar = !selmon->showbar;
1641 updatebarpos(selmon);
1642 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1647 togglefloating(const Arg *arg) {
1650 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1651 if(selmon->sel->isfloating)
1652 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1653 selmon->sel->w, selmon->sel->h, False);
1658 toggletag(const Arg *arg) {
1659 unsigned int newtags;
1663 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1665 selmon->sel->tags = newtags;
1671 toggleview(const Arg *arg) {
1672 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1675 selmon->tagset[selmon->seltags] = newtagset;
1681 unfocus(Client *c) {
1684 grabbuttons(c, False);
1685 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
1686 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1690 unmanage(Client *c, Bool destroyed) {
1691 Monitor *m = c->mon;
1694 /* The server grab construct avoids race conditions. */
1698 wc.border_width = c->oldbw;
1700 XSetErrorHandler(xerrordummy);
1701 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1702 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1703 setclientstate(c, WithdrawnState);
1705 XSetErrorHandler(xerror);
1714 unmapnotify(XEvent *e) {
1716 XUnmapEvent *ev = &e->xunmap;
1718 if((c = wintoclient(ev->window)))
1725 XSetWindowAttributes wa;
1727 wa.override_redirect = True;
1728 wa.background_pixmap = ParentRelative;
1729 wa.event_mask = ButtonPressMask|ExposureMask;
1730 for(m = mons; m; m = m->next) {
1731 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1732 CopyFromParent, DefaultVisual(dpy, screen),
1733 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1734 XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
1735 XMapRaised(dpy, m->barwin);
1740 updatebarpos(Monitor *m) {
1745 m->by = m->topbar ? m->wy : m->wy + m->wh;
1746 m->wy = m->topbar ? m->wy + bh : m->wy;
1757 if(XineramaIsActive(dpy)) {
1761 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1762 XineramaScreenInfo *unique = NULL;
1764 info = XineramaQueryScreens(dpy, &nn);
1765 for(n = 0, m = mons; m; m = m->next, n++);
1766 /* only consider unique geometries as separate screens */
1767 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
1768 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
1769 for(i = 0, j = 0; i < nn; i++)
1770 if(isuniquegeom(unique, j, &info[i]))
1771 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1775 for(i = 0; i < (nn - n); i++) { /* new monitors available */
1776 for(m = mons; m && m->next; m = m->next);
1778 m->next = createmon();
1782 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
1784 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1785 || unique[i].width != m->mw || unique[i].height != m->mh))
1789 m->mx = m->wx = unique[i].x_org;
1790 m->my = m->wy = unique[i].y_org;
1791 m->mw = m->ww = unique[i].width;
1792 m->mh = m->wh = unique[i].height;
1796 else { /* less monitors available nn < n */
1797 for(i = nn; i < n; i++) {
1798 for(m = mons; m && m->next; m = m->next);
1802 m->clients = c->next;
1816 #endif /* XINERAMA */
1817 /* default monitor setup */
1821 if(mons->mw != sw || mons->mh != sh) {
1823 mons->mw = mons->ww = sw;
1824 mons->mh = mons->wh = sh;
1830 selmon = wintomon(root);
1836 updatenumlockmask(void) {
1838 XModifierKeymap *modmap;
1841 modmap = XGetModifierMapping(dpy);
1842 for(i = 0; i < 8; i++)
1843 for(j = 0; j < modmap->max_keypermod; j++)
1844 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1845 == XKeysymToKeycode(dpy, XK_Num_Lock))
1846 numlockmask = (1 << i);
1847 XFreeModifiermap(modmap);
1851 updatesizehints(Client *c) {
1855 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
1856 /* size is uninitialized, ensure that size.flags aren't used */
1858 if(size.flags & PBaseSize) {
1859 c->basew = size.base_width;
1860 c->baseh = size.base_height;
1862 else if(size.flags & PMinSize) {
1863 c->basew = size.min_width;
1864 c->baseh = size.min_height;
1867 c->basew = c->baseh = 0;
1868 if(size.flags & PResizeInc) {
1869 c->incw = size.width_inc;
1870 c->inch = size.height_inc;
1873 c->incw = c->inch = 0;
1874 if(size.flags & PMaxSize) {
1875 c->maxw = size.max_width;
1876 c->maxh = size.max_height;
1879 c->maxw = c->maxh = 0;
1880 if(size.flags & PMinSize) {
1881 c->minw = size.min_width;
1882 c->minh = size.min_height;
1884 else if(size.flags & PBaseSize) {
1885 c->minw = size.base_width;
1886 c->minh = size.base_height;
1889 c->minw = c->minh = 0;
1890 if(size.flags & PAspect) {
1891 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
1892 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
1895 c->maxa = c->mina = 0.0;
1896 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
1897 && c->maxw == c->minw && c->maxh == c->minh);
1901 updatetitle(Client *c) {
1902 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
1903 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
1904 if(c->name[0] == '\0') /* hack to mark broken clients */
1905 strcpy(c->name, broken);
1909 updatestatus(void) {
1910 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
1911 strcpy(stext, "dwm-"VERSION);
1916 updatewmhints(Client *c) {
1919 if((wmh = XGetWMHints(dpy, c->win))) {
1920 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
1921 wmh->flags &= ~XUrgencyHint;
1922 XSetWMHints(dpy, c->win, wmh);
1925 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
1931 view(const Arg *arg) {
1932 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
1934 selmon->seltags ^= 1; /* toggle sel tagset */
1935 if(arg->ui & TAGMASK)
1936 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
1941 wintoclient(Window w) {
1945 for(m = mons; m; m = m->next)
1946 for(c = m->clients; c; c = c->next)
1953 wintomon(Window w) {
1958 if(w == root && getrootptr(&x, &y))
1959 return ptrtomon(x, y);
1960 for(m = mons; m; m = m->next)
1963 if((c = wintoclient(w)))
1968 /* There's no way to check accesses to destroyed windows, thus those cases are
1969 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1970 * default error handler, which may call exit. */
1972 xerror(Display *dpy, XErrorEvent *ee) {
1973 if(ee->error_code == BadWindow
1974 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
1975 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
1976 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
1977 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
1978 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
1979 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
1980 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
1981 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
1983 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
1984 ee->request_code, ee->error_code);
1985 return xerrorxlib(dpy, ee); /* may call exit */
1989 xerrordummy(Display *dpy, XErrorEvent *ee) {
1993 /* Startup Error handler to check if another window manager
1994 * is already running. */
1996 xerrorstart(Display *dpy, XErrorEvent *ee) {
2002 zoom(const Arg *arg) {
2003 Client *c = selmon->sel;
2005 if(!selmon->lt[selmon->sellt]->arrange
2006 || selmon->lt[selmon->sellt]->arrange == monocle
2007 || (selmon->sel && selmon->sel->isfloating))
2009 if(c == nexttiled(selmon->clients))
2010 if(!c || !(c = nexttiled(c->next)))
2019 main(int argc, char *argv[]) {
2020 if(argc == 2 && !strcmp("-v", argv[1]))
2021 die("dwm-"VERSION", © 2006-2009 dwm engineers, see LICENSE for details\n");
2023 die("usage: dwm [-v]\n");
2024 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2025 fputs("warning: no locale support\n", stderr);
2026 if(!(dpy = XOpenDisplay(NULL)))
2027 die("dwm: cannot open display\n");