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>
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)
58 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
59 enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
60 enum { NetSupported, NetWMName, NetWMState, NetLast }; /* EWMH atoms */
61 enum { WMProtocols, WMDelete, WMState, WMLast }; /* default atoms */
62 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
63 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
76 void (*func)(const Arg *arg);
80 typedef struct Monitor Monitor;
81 typedef struct Client Client;
86 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
89 Bool isfixed, isfloating, isurgent;
98 unsigned long norm[ColLast];
99 unsigned long sel[ColLast];
109 } DC; /* draw context */
114 void (*func)(const Arg *);
120 void (*arrange)(Monitor *);
127 int by; /* bar geometry */
128 int mx, my, mw, mh; /* screen size */
129 int wx, wy, ww, wh; /* window area */
130 unsigned int seltags;
132 unsigned int tagset[2];
145 const char *instance;
152 /* function declarations */
153 static void applyrules(Client *c);
154 static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
155 static void arrange(Monitor *m);
156 static void arrangemon(Monitor *m);
157 static void attach(Client *c);
158 static void attachstack(Client *c);
159 static void buttonpress(XEvent *e);
160 static void checkotherwm(void);
161 static void cleanup(void);
162 static void cleanupmon(Monitor *mon);
163 static void clearurgent(Client *c);
164 static void configure(Client *c);
165 static void configurenotify(XEvent *e);
166 static void configurerequest(XEvent *e);
167 static Monitor *createmon(void);
168 static void destroynotify(XEvent *e);
169 static void detach(Client *c);
170 static void detachstack(Client *c);
171 static void die(const char *errstr, ...);
172 static Monitor *dirtomon(int dir);
173 static void drawbar(Monitor *m);
174 static void drawbars(void);
175 static void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
176 static void drawtext(const char *text, unsigned long col[ColLast], Bool invert);
177 static void enternotify(XEvent *e);
178 static void expose(XEvent *e);
179 static void focus(Client *c);
180 static void focusin(XEvent *e);
181 static void focusmon(const Arg *arg);
182 static void focusstack(const Arg *arg);
183 static unsigned long getcolor(const char *colstr);
184 static Bool getrootptr(int *x, int *y);
185 static long getstate(Window w);
186 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
187 static void grabbuttons(Client *c, Bool focused);
188 static void grabkeys(void);
189 static void initfont(const char *fontstr);
190 static Bool isprotodel(Client *c);
191 static void keypress(XEvent *e);
192 static void killclient(const Arg *arg);
193 static void manage(Window w, XWindowAttributes *wa);
194 static void mappingnotify(XEvent *e);
195 static void maprequest(XEvent *e);
196 static void monocle(Monitor *m);
197 static void movemouse(const Arg *arg);
198 static Client *nexttiled(Client *c);
199 static Monitor *ptrtomon(int x, int y);
200 static void propertynotify(XEvent *e);
201 static void quit(const Arg *arg);
202 static void resize(Client *c, int x, int y, int w, int h, Bool interact);
203 static void resizemouse(const Arg *arg);
204 static void restack(Monitor *m);
205 static void run(void);
206 static void scan(void);
207 static void sendmon(Client *c, Monitor *m);
208 static void setclientstate(Client *c, long state);
209 static void setlayout(const Arg *arg);
210 static void setmfact(const Arg *arg);
211 static void setup(void);
212 static void showhide(Client *c);
213 static void sigchld(int unused);
214 static void spawn(const Arg *arg);
215 static void tag(const Arg *arg);
216 static void tagmon(const Arg *arg);
217 static int textnw(const char *text, unsigned int len);
218 static void tile(Monitor *);
219 static void togglebar(const Arg *arg);
220 static void togglefloating(const Arg *arg);
221 static void toggletag(const Arg *arg);
222 static void toggleview(const Arg *arg);
223 static void unfocus(Client *c);
224 static void unmanage(Client *c, Bool destroyed);
225 static void unmapnotify(XEvent *e);
226 static Bool updategeom(void);
227 static void updatebarpos(Monitor *m);
228 static void updatebars(void);
229 static void updatenumlockmask(void);
230 static void updatesizehints(Client *c);
231 static void updatestatus(void);
232 static void updatetitle(Client *c);
233 static void updatewmhints(Client *c);
234 static void view(const Arg *arg);
235 static Client *wintoclient(Window w);
236 static Monitor *wintomon(Window w);
237 static int xerror(Display *dpy, XErrorEvent *ee);
238 static int xerrordummy(Display *dpy, XErrorEvent *ee);
239 static int xerrorstart(Display *dpy, XErrorEvent *ee);
240 static void zoom(const Arg *arg);
243 static const char broken[] = "broken";
244 static char stext[256];
246 static int sw, sh; /* X display screen geometry width, height */
247 static int bh, blw = 0; /* bar geometry */
248 static int (*xerrorxlib)(Display *, XErrorEvent *);
249 static unsigned int numlockmask = 0;
250 static void (*handler[LASTEvent]) (XEvent *) = {
251 [ButtonPress] = buttonpress,
252 [ConfigureRequest] = configurerequest,
253 [ConfigureNotify] = configurenotify,
254 [DestroyNotify] = destroynotify,
255 [EnterNotify] = enternotify,
258 [KeyPress] = keypress,
259 [MappingNotify] = mappingnotify,
260 [MapRequest] = maprequest,
261 [PropertyNotify] = propertynotify,
262 [UnmapNotify] = unmapnotify
264 static Atom wmatom[WMLast], netatom[NetLast];
266 static Bool running = True;
267 static Cursor cursor[CurLast];
270 static Monitor *mons = NULL, *selmon = NULL;
273 /* configuration, allows nested code to access above variables */
276 /* compile-time check if all tags fit into an unsigned int bit array. */
277 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
279 /* function implementations */
281 applyrules(Client *c) {
282 const char *class, *instance;
286 XClassHint ch = { 0 };
289 c->isfloating = c->tags = 0;
290 if(XGetClassHint(dpy, c->win, &ch)) {
291 class = ch.res_class ? ch.res_class : broken;
292 instance = ch.res_name ? ch.res_name : broken;
293 for(i = 0; i < LENGTH(rules); i++) {
295 if((!r->title || strstr(c->name, r->title))
296 && (!r->class || strstr(class, r->class))
297 && (!r->instance || strstr(instance, r->instance)))
299 c->isfloating = r->isfloating;
301 for(m = mons; m && m->num != r->monitor; m = m->next);
311 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
315 applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
319 /* set minimum possible */
327 if(*x + *w + 2 * c->bw < 0)
329 if(*y + *h + 2 * c->bw < 0)
333 if(*x > m->mx + m->mw)
334 *x = m->mx + m->mw - WIDTH(c);
335 if(*y > m->my + m->mh)
336 *y = m->my + m->mh - HEIGHT(c);
337 if(*x + *w + 2 * c->bw < m->mx)
339 if(*y + *h + 2 * c->bw < m->my)
346 if(resizehints || c->isfloating) {
347 /* see last two sentences in ICCCM 4.1.2.3 */
348 baseismin = c->basew == c->minw && c->baseh == c->minh;
349 if(!baseismin) { /* temporarily remove base dimensions */
353 /* adjust for aspect limits */
354 if(c->mina > 0 && c->maxa > 0) {
355 if(c->maxa < (float)*w / *h)
356 *w = *h * c->maxa + 0.5;
357 else if(c->mina < (float)*h / *w)
358 *h = *w * c->mina + 0.5;
360 if(baseismin) { /* increment calculation requires this */
364 /* adjust for increment value */
369 /* restore base dimensions */
372 *w = MAX(*w, c->minw);
373 *h = MAX(*h, c->minh);
375 *w = MIN(*w, c->maxw);
377 *h = MIN(*h, c->maxh);
379 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
383 arrange(Monitor *m) {
386 else for(m = mons; m; m = m->next)
391 else for(m = mons; m; m = m->next)
396 arrangemon(Monitor *m) {
397 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
398 if(m->lt[m->sellt]->arrange)
399 m->lt[m->sellt]->arrange(m);
405 c->next = c->mon->clients;
410 attachstack(Client *c) {
411 c->snext = c->mon->stack;
416 buttonpress(XEvent *e) {
417 unsigned int i, x, click;
421 XButtonPressedEvent *ev = &e->xbutton;
424 /* focus monitor if necessary */
425 if((m = wintomon(ev->window)) && m != selmon) {
426 unfocus(selmon->sel);
430 if(ev->window == selmon->barwin) {
434 } while(ev->x >= x && ++i < LENGTH(tags));
435 if(i < LENGTH(tags)) {
439 else if(ev->x < x + blw)
441 else if(ev->x > selmon->wx + selmon->ww - TEXTW(stext))
442 click = ClkStatusText;
446 else if((c = wintoclient(ev->window))) {
448 click = ClkClientWin;
450 for(i = 0; i < LENGTH(buttons); i++)
451 if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
452 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
453 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
459 xerrorxlib = XSetErrorHandler(xerrorstart);
460 /* this causes an error if some other window manager is running */
461 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
464 die("dwm: another window manager is already running\n");
465 XSetErrorHandler(xerror);
472 Layout foo = { "", NULL };
476 selmon->lt[selmon->sellt] = &foo;
477 for(m = mons; m; m = m->next)
479 unmanage(m->stack, False);
481 XFreeFontSet(dpy, dc.font.set);
483 XFreeFont(dpy, dc.font.xfont);
484 XUngrabKey(dpy, AnyKey, AnyModifier, root);
485 XFreePixmap(dpy, dc.drawable);
487 XFreeCursor(dpy, cursor[CurNormal]);
488 XFreeCursor(dpy, cursor[CurResize]);
489 XFreeCursor(dpy, cursor[CurMove]);
493 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
497 cleanupmon(Monitor *mon) {
503 for(m = mons; m && m->next != mon; m = m->next);
506 XUnmapWindow(dpy, mon->barwin);
507 XDestroyWindow(dpy, mon->barwin);
512 clearurgent(Client *c) {
516 if(!(wmh = XGetWMHints(dpy, c->win)))
518 wmh->flags &= ~XUrgencyHint;
519 XSetWMHints(dpy, c->win, wmh);
524 configure(Client *c) {
527 ce.type = ConfigureNotify;
535 ce.border_width = c->bw;
537 ce.override_redirect = False;
538 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
542 configurenotify(XEvent *e) {
544 XConfigureEvent *ev = &e->xconfigure;
546 if(ev->window == root) {
551 XFreePixmap(dpy, dc.drawable);
552 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
554 for(m = mons; m; m = m->next)
555 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
562 configurerequest(XEvent *e) {
565 XConfigureRequestEvent *ev = &e->xconfigurerequest;
568 if((c = wintoclient(ev->window))) {
569 if(ev->value_mask & CWBorderWidth)
570 c->bw = ev->border_width;
571 else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
573 if(ev->value_mask & CWX)
574 c->x = m->mx + ev->x;
575 if(ev->value_mask & CWY)
576 c->y = m->my + ev->y;
577 if(ev->value_mask & CWWidth)
579 if(ev->value_mask & CWHeight)
581 if((c->x + c->w) > m->mx + m->mw && c->isfloating)
582 c->x = m->mx + (m->mw / 2 - c->w / 2); /* center in x direction */
583 if((c->y + c->h) > m->my + m->mh && c->isfloating)
584 c->y = m->my + (m->mh / 2 - c->h / 2); /* center in y direction */
585 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
588 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
596 wc.width = ev->width;
597 wc.height = ev->height;
598 wc.border_width = ev->border_width;
599 wc.sibling = ev->above;
600 wc.stack_mode = ev->detail;
601 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
610 if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
611 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
612 m->tagset[0] = m->tagset[1] = 1;
614 m->showbar = showbar;
616 m->lt[0] = &layouts[0];
617 m->lt[1] = &layouts[1 % LENGTH(layouts)];
618 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
623 destroynotify(XEvent *e) {
625 XDestroyWindowEvent *ev = &e->xdestroywindow;
627 if((c = wintoclient(ev->window)))
635 for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
640 detachstack(Client *c) {
643 for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
646 if(c == c->mon->sel) {
647 for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
653 die(const char *errstr, ...) {
656 va_start(ap, errstr);
657 vfprintf(stderr, errstr, ap);
667 if(!(m = selmon->next))
672 for(m = mons; m->next; m = m->next);
674 for(m = mons; m->next != selmon; m = m->next);
680 drawbar(Monitor *m) {
682 unsigned int i, occ = 0, urg = 0;
686 for(c = m->clients; c; c = c->next) {
692 for(i = 0; i < LENGTH(tags); i++) {
693 dc.w = TEXTW(tags[i]);
694 col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
695 drawtext(tags[i], col, urg & 1 << i);
696 drawsquare(m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
697 occ & 1 << i, urg & 1 << i, col);
700 dc.w = blw = TEXTW(m->ltsymbol);
701 drawtext(m->ltsymbol, dc.norm, False);
704 if(m == selmon) { /* status is only drawn on selected monitor */
711 drawtext(stext, dc.norm, False);
715 if((dc.w = dc.x - x) > bh) {
718 col = m == selmon ? dc.sel : dc.norm;
719 drawtext(m->sel->name, col, False);
720 drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
723 drawtext(NULL, dc.norm, False);
725 XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
733 for(m = mons; m; m = m->next)
738 drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
741 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
743 gcv.foreground = col[invert ? ColBG : ColFG];
744 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
745 x = (dc.font.ascent + dc.font.descent + 2) / 4;
749 r.width = r.height = x + 1;
750 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
753 r.width = r.height = x;
754 XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
759 drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
761 int i, x, y, h, len, olen;
762 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
764 XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
765 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
769 h = dc.font.ascent + dc.font.descent;
770 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
772 /* shorten text if necessary */
773 for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
776 memcpy(buf, text, len);
778 for(i = len; i && i > len - 3; buf[--i] = '.');
779 XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
781 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
783 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
787 enternotify(XEvent *e) {
790 XCrossingEvent *ev = &e->xcrossing;
792 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
794 if((m = wintomon(ev->window)) && m != selmon) {
795 unfocus(selmon->sel);
798 if((c = wintoclient(ev->window)))
807 XExposeEvent *ev = &e->xexpose;
809 if(ev->count == 0 && (m = wintomon(ev->window)))
815 if(!c || !ISVISIBLE(c))
816 for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
818 unfocus(selmon->sel);
826 grabbuttons(c, True);
827 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
828 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
831 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
837 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
838 XFocusChangeEvent *ev = &e->xfocus;
840 if(selmon->sel && ev->window != selmon->sel->win)
841 XSetInputFocus(dpy, selmon->sel->win, RevertToPointerRoot, CurrentTime);
845 focusmon(const Arg *arg) {
850 if((m = dirtomon(arg->i)) == selmon)
852 unfocus(selmon->sel);
858 focusstack(const Arg *arg) {
859 Client *c = NULL, *i;
864 for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
866 for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
869 for(i = selmon->clients; i != selmon->sel; i = i->next)
873 for(; i; i = i->next)
884 getcolor(const char *colstr) {
885 Colormap cmap = DefaultColormap(dpy, screen);
888 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
889 die("error, cannot allocate color '%s'\n", colstr);
894 getrootptr(int *x, int *y) {
899 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
906 unsigned char *p = NULL;
907 unsigned long n, extra;
910 status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
911 &real, &format, &n, &extra, (unsigned char **)&p);
912 if(status != Success)
921 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
926 if(!text || size == 0)
929 XGetTextProperty(dpy, w, &name, atom);
932 if(name.encoding == XA_STRING)
933 strncpy(text, (char *)name.value, size - 1);
935 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
936 strncpy(text, *list, size - 1);
937 XFreeStringList(list);
940 text[size - 1] = '\0';
946 grabbuttons(Client *c, Bool focused) {
950 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
951 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
953 for(i = 0; i < LENGTH(buttons); i++)
954 if(buttons[i].click == ClkClientWin)
955 for(j = 0; j < LENGTH(modifiers); j++)
956 XGrabButton(dpy, buttons[i].button,
957 buttons[i].mask | modifiers[j],
958 c->win, False, BUTTONMASK,
959 GrabModeAsync, GrabModeSync, None, None);
962 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
963 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
972 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
975 XUngrabKey(dpy, AnyKey, AnyModifier, root);
976 for(i = 0; i < LENGTH(keys); i++) {
977 if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
978 for(j = 0; j < LENGTH(modifiers); j++)
979 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
980 True, GrabModeAsync, GrabModeAsync);
986 initfont(const char *fontstr) {
987 char *def, **missing;
991 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
994 fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
995 XFreeStringList(missing);
998 XFontSetExtents *font_extents;
999 XFontStruct **xfonts;
1002 dc.font.ascent = dc.font.descent = 0;
1003 font_extents = XExtentsOfFontSet(dc.font.set);
1004 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
1005 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
1006 dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
1007 dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
1012 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
1013 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
1014 die("error, cannot load font: '%s'\n", fontstr);
1015 dc.font.ascent = dc.font.xfont->ascent;
1016 dc.font.descent = dc.font.xfont->descent;
1018 dc.font.height = dc.font.ascent + dc.font.descent;
1022 isprotodel(Client *c) {
1027 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1028 for(i = 0; !ret && i < n; i++)
1029 if(protocols[i] == wmatom[WMDelete])
1038 isuniquegeom(XineramaScreenInfo *unique, size_t len, XineramaScreenInfo *info) {
1041 for(i = 0; i < len; i++)
1042 if(unique[i].x_org == info->x_org && unique[i].y_org == info->y_org
1043 && unique[i].width == info->width && unique[i].height == info->height)
1047 #endif /* XINERAMA */
1050 keypress(XEvent *e) {
1056 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1057 for(i = 0; i < LENGTH(keys); i++)
1058 if(keysym == keys[i].keysym
1059 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1061 keys[i].func(&(keys[i].arg));
1065 killclient(const Arg *arg) {
1070 if(isprotodel(selmon->sel)) {
1071 ev.type = ClientMessage;
1072 ev.xclient.window = selmon->sel->win;
1073 ev.xclient.message_type = wmatom[WMProtocols];
1074 ev.xclient.format = 32;
1075 ev.xclient.data.l[0] = wmatom[WMDelete];
1076 ev.xclient.data.l[1] = CurrentTime;
1077 XSendEvent(dpy, selmon->sel->win, False, NoEventMask, &ev);
1081 XSetErrorHandler(xerrordummy);
1082 XSetCloseDownMode(dpy, DestroyAll);
1083 XKillClient(dpy, selmon->sel->win);
1085 XSetErrorHandler(xerror);
1091 manage(Window w, XWindowAttributes *wa) {
1093 Client *c, *t = NULL;
1094 Window trans = None;
1097 if(!(c = malloc(sizeof(Client))))
1098 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1102 if(XGetTransientForHint(dpy, w, &trans))
1103 t = wintoclient(trans);
1113 c->x = wa->x + c->mon->wx;
1114 c->y = wa->y + c->mon->wy;
1117 c->oldbw = wa->border_width;
1118 if(c->w == c->mon->mw && c->h == c->mon->mh) {
1124 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1125 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1126 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1127 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1128 c->x = MAX(c->x, c->mon->mx);
1129 /* only fix client y-offset, if the client center might cover the bar */
1130 c->y = MAX(c->y, ((c->mon->by == 0) && (c->x + (c->w / 2) >= c->mon->wx)
1131 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1134 wc.border_width = c->bw;
1135 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1136 XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
1137 configure(c); /* propagates border_width, if size doesn't change */
1139 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1140 grabbuttons(c, False);
1142 c->isfloating = trans != None || c->isfixed;
1144 XRaiseWindow(dpy, c->win);
1147 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1148 XMapWindow(dpy, c->win);
1149 setclientstate(c, NormalState);
1154 mappingnotify(XEvent *e) {
1155 XMappingEvent *ev = &e->xmapping;
1157 XRefreshKeyboardMapping(ev);
1158 if(ev->request == MappingKeyboard)
1163 maprequest(XEvent *e) {
1164 static XWindowAttributes wa;
1165 XMapRequestEvent *ev = &e->xmaprequest;
1167 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1169 if(wa.override_redirect)
1171 if(!wintoclient(ev->window))
1172 manage(ev->window, &wa);
1176 monocle(Monitor *m) {
1180 for(c = m->clients; c; c = c->next)
1183 if(n > 0) /* override layout symbol */
1184 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1185 for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
1186 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
1190 movemouse(const Arg *arg) {
1191 int x, y, ocx, ocy, nx, ny;
1196 if(!(c = selmon->sel))
1201 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1202 None, cursor[CurMove], CurrentTime) != GrabSuccess)
1204 if(!getrootptr(&x, &y))
1207 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1209 case ConfigureRequest:
1212 handler[ev.type](&ev);
1215 nx = ocx + (ev.xmotion.x - x);
1216 ny = ocy + (ev.xmotion.y - y);
1217 if(snap && nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1218 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1219 if(abs(selmon->wx - nx) < snap)
1221 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1222 nx = selmon->wx + selmon->ww - WIDTH(c);
1223 if(abs(selmon->wy - ny) < snap)
1225 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1226 ny = selmon->wy + selmon->wh - HEIGHT(c);
1227 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1228 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1229 togglefloating(NULL);
1231 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1232 resize(c, nx, ny, c->w, c->h, True);
1235 } while(ev.type != ButtonRelease);
1236 XUngrabPointer(dpy, CurrentTime);
1237 if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
1245 nexttiled(Client *c) {
1246 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1251 ptrtomon(int x, int y) {
1254 for(m = mons; m; m = m->next)
1255 if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh))
1261 propertynotify(XEvent *e) {
1264 XPropertyEvent *ev = &e->xproperty;
1266 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1268 else if(ev->state == PropertyDelete)
1269 return; /* ignore */
1270 else if((c = wintoclient(ev->window))) {
1273 case XA_WM_TRANSIENT_FOR:
1274 XGetTransientForHint(dpy, c->win, &trans);
1275 if(!c->isfloating && (c->isfloating = (wintoclient(trans) != NULL)))
1278 case XA_WM_NORMAL_HINTS:
1286 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1288 if(c == c->mon->sel)
1295 quit(const Arg *arg) {
1300 resize(Client *c, int x, int y, int w, int h, Bool interact) {
1303 if(applysizehints(c, &x, &y, &w, &h, interact)) {
1306 c->w = wc.width = w;
1307 c->h = wc.height = h;
1308 wc.border_width = c->bw;
1309 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1316 resizemouse(const Arg *arg) {
1323 if(!(c = selmon->sel))
1328 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1329 None, cursor[CurResize], CurrentTime) != GrabSuccess)
1331 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1333 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1335 case ConfigureRequest:
1338 handler[ev.type](&ev);
1341 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1342 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1343 if(snap && nw >= selmon->wx && nw <= selmon->wx + selmon->ww
1344 && nh >= selmon->wy && nh <= selmon->wy + selmon->wh)
1346 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1347 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1348 togglefloating(NULL);
1350 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1351 resize(c, c->x, c->y, nw, nh, True);
1354 } while(ev.type != ButtonRelease);
1355 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1356 XUngrabPointer(dpy, CurrentTime);
1357 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1358 if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
1366 restack(Monitor *m) {
1374 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1375 XRaiseWindow(dpy, m->sel->win);
1376 if(m->lt[m->sellt]->arrange) {
1377 wc.stack_mode = Below;
1378 wc.sibling = m->barwin;
1379 for(c = m->stack; c; c = c->snext)
1380 if(!c->isfloating && ISVISIBLE(c)) {
1381 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1382 wc.sibling = c->win;
1386 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1392 /* main event loop */
1394 while(running && !XNextEvent(dpy, &ev)) {
1395 if(handler[ev.type])
1396 handler[ev.type](&ev); /* call handler */
1402 unsigned int i, num;
1403 Window d1, d2, *wins = NULL;
1404 XWindowAttributes wa;
1406 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1407 for(i = 0; i < num; i++) {
1408 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1409 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1411 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1412 manage(wins[i], &wa);
1414 for(i = 0; i < num; i++) { /* now the transients */
1415 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1417 if(XGetTransientForHint(dpy, wins[i], &d1)
1418 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1419 manage(wins[i], &wa);
1427 sendmon(Client *c, Monitor *m) {
1434 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1442 setclientstate(Client *c, long state) {
1443 long data[] = { state, None };
1445 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1446 PropModeReplace, (unsigned char *)data, 2);
1450 setlayout(const Arg *arg) {
1451 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1454 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1455 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1462 /* arg > 1.0 will set mfact absolutly */
1464 setmfact(const Arg *arg) {
1467 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1469 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1470 if(f < 0.1 || f > 0.9)
1478 XSetWindowAttributes wa;
1480 /* clean up any zombies immediately */
1484 screen = DefaultScreen(dpy);
1485 root = RootWindow(dpy, screen);
1487 sw = DisplayWidth(dpy, screen);
1488 sh = DisplayHeight(dpy, screen);
1489 bh = dc.h = dc.font.height + 2;
1492 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1493 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1494 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1495 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1496 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1497 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1499 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1500 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1501 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1502 /* init appearance */
1503 dc.norm[ColBorder] = getcolor(normbordercolor);
1504 dc.norm[ColBG] = getcolor(normbgcolor);
1505 dc.norm[ColFG] = getcolor(normfgcolor);
1506 dc.sel[ColBorder] = getcolor(selbordercolor);
1507 dc.sel[ColBG] = getcolor(selbgcolor);
1508 dc.sel[ColFG] = getcolor(selfgcolor);
1509 dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
1510 dc.gc = XCreateGC(dpy, root, 0, NULL);
1511 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1513 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
1517 /* EWMH support per view */
1518 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1519 PropModeReplace, (unsigned char *) netatom, NetLast);
1520 /* select for events */
1521 wa.cursor = cursor[CurNormal];
1522 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
1523 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
1524 |PropertyChangeMask;
1525 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1526 XSelectInput(dpy, root, wa.event_mask);
1531 showhide(Client *c) {
1534 if(ISVISIBLE(c)) { /* show clients top down */
1535 XMoveWindow(dpy, c->win, c->x, c->y);
1536 if(!c->mon->lt[c->mon->sellt]->arrange || c->isfloating)
1537 resize(c, c->x, c->y, c->w, c->h, False);
1540 else { /* hide clients bottom up */
1542 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
1548 sigchld(int unused) {
1549 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1550 die("Can't install SIGCHLD handler");
1551 while(0 < waitpid(-1, NULL, WNOHANG));
1555 spawn(const Arg *arg) {
1558 close(ConnectionNumber(dpy));
1560 execvp(((char **)arg->v)[0], (char **)arg->v);
1561 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1568 tag(const Arg *arg) {
1569 if(selmon->sel && arg->ui & TAGMASK) {
1570 selmon->sel->tags = arg->ui & TAGMASK;
1576 tagmon(const Arg *arg) {
1577 if(!selmon->sel || !mons->next)
1579 sendmon(selmon->sel, dirtomon(arg->i));
1583 textnw(const char *text, unsigned int len) {
1587 XmbTextExtents(dc.font.set, text, len, NULL, &r);
1590 return XTextWidth(dc.font.xfont, text, len);
1599 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1603 c = nexttiled(m->clients);
1604 mw = m->mfact * m->ww;
1605 resize(c, m->wx, m->wy, (n == 1 ? m->ww : mw) - 2 * c->bw, m->wh - 2 * c->bw, False);
1609 x = (m->wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : m->wx + mw;
1611 w = (m->wx + mw > c->x + c->w) ? m->wx + m->ww - x : m->ww - mw;
1615 for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
1616 resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
1617 ? m->wy + m->wh - y - 2 * c->bw : h - 2 * c->bw), False);
1619 y = c->y + HEIGHT(c);
1624 togglebar(const Arg *arg) {
1625 selmon->showbar = !selmon->showbar;
1626 updatebarpos(selmon);
1627 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1632 togglefloating(const Arg *arg) {
1635 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1636 if(selmon->sel->isfloating)
1637 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1638 selmon->sel->w, selmon->sel->h, False);
1643 toggletag(const Arg *arg) {
1644 unsigned int newtags;
1648 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1650 selmon->sel->tags = newtags;
1656 toggleview(const Arg *arg) {
1657 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1660 selmon->tagset[selmon->seltags] = newtagset;
1666 unfocus(Client *c) {
1669 grabbuttons(c, False);
1670 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
1671 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1675 unmanage(Client *c, Bool destroyed) {
1676 Monitor *m = c->mon;
1679 /* The server grab construct avoids race conditions. */
1683 wc.border_width = c->oldbw;
1685 XSetErrorHandler(xerrordummy);
1686 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1687 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1688 setclientstate(c, WithdrawnState);
1690 XSetErrorHandler(xerror);
1699 unmapnotify(XEvent *e) {
1701 XUnmapEvent *ev = &e->xunmap;
1703 if((c = wintoclient(ev->window)))
1710 XSetWindowAttributes wa;
1712 wa.override_redirect = True;
1713 wa.background_pixmap = ParentRelative;
1714 wa.event_mask = ButtonPressMask|ExposureMask;
1715 for(m = mons; m; m = m->next) {
1716 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1717 CopyFromParent, DefaultVisual(dpy, screen),
1718 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1719 XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
1720 XMapRaised(dpy, m->barwin);
1725 updatebarpos(Monitor *m) {
1730 m->by = m->topbar ? m->wy : m->wy + m->wh;
1731 m->wy = m->topbar ? m->wy + bh : m->wy;
1742 if(XineramaIsActive(dpy)) {
1746 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1747 XineramaScreenInfo *unique = NULL;
1749 info = XineramaQueryScreens(dpy, &nn);
1750 for(n = 0, m = mons; m; m = m->next, n++);
1751 /* only consider unique geometries as separate screens */
1752 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
1753 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
1754 for(i = 0, j = 0; i < nn; i++)
1755 if(isuniquegeom(unique, j, &info[i]))
1756 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1760 for(i = 0; i < (nn - n); i++) { /* new monitors available */
1761 for(m = mons; m && m->next; m = m->next);
1763 m->next = createmon();
1767 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
1769 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1770 || unique[i].width != m->mw || unique[i].height != m->mh))
1774 m->mx = m->wx = unique[i].x_org;
1775 m->my = m->wy = unique[i].y_org;
1776 m->mw = m->ww = unique[i].width;
1777 m->mh = m->wh = unique[i].height;
1781 else { /* less monitors available nn < n */
1782 for(i = nn; i < n; i++) {
1783 for(m = mons; m && m->next; m = m->next);
1787 m->clients = c->next;
1801 #endif /* XINERAMA */
1802 /* default monitor setup */
1806 if(mons->mw != sw || mons->mh != sh) {
1808 mons->mw = mons->ww = sw;
1809 mons->mh = mons->wh = sh;
1815 selmon = wintomon(root);
1821 updatenumlockmask(void) {
1823 XModifierKeymap *modmap;
1826 modmap = XGetModifierMapping(dpy);
1827 for(i = 0; i < 8; i++)
1828 for(j = 0; j < modmap->max_keypermod; j++)
1829 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1830 == XKeysymToKeycode(dpy, XK_Num_Lock))
1831 numlockmask = (1 << i);
1832 XFreeModifiermap(modmap);
1836 updatesizehints(Client *c) {
1840 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
1841 /* size is uninitialized, ensure that size.flags aren't used */
1843 if(size.flags & PBaseSize) {
1844 c->basew = size.base_width;
1845 c->baseh = size.base_height;
1847 else if(size.flags & PMinSize) {
1848 c->basew = size.min_width;
1849 c->baseh = size.min_height;
1852 c->basew = c->baseh = 0;
1853 if(size.flags & PResizeInc) {
1854 c->incw = size.width_inc;
1855 c->inch = size.height_inc;
1858 c->incw = c->inch = 0;
1859 if(size.flags & PMaxSize) {
1860 c->maxw = size.max_width;
1861 c->maxh = size.max_height;
1864 c->maxw = c->maxh = 0;
1865 if(size.flags & PMinSize) {
1866 c->minw = size.min_width;
1867 c->minh = size.min_height;
1869 else if(size.flags & PBaseSize) {
1870 c->minw = size.base_width;
1871 c->minh = size.base_height;
1874 c->minw = c->minh = 0;
1875 if(size.flags & PAspect) {
1876 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
1877 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
1880 c->maxa = c->mina = 0.0;
1881 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
1882 && c->maxw == c->minw && c->maxh == c->minh);
1886 updatetitle(Client *c) {
1887 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
1888 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
1889 if(c->name[0] == '\0') /* hack to mark broken clients */
1890 strcpy(c->name, broken);
1894 updatestatus(void) {
1895 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
1896 strcpy(stext, "dwm-"VERSION);
1901 updatewmhints(Client *c) {
1904 if((wmh = XGetWMHints(dpy, c->win))) {
1905 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
1906 wmh->flags &= ~XUrgencyHint;
1907 XSetWMHints(dpy, c->win, wmh);
1910 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
1916 view(const Arg *arg) {
1917 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
1919 selmon->seltags ^= 1; /* toggle sel tagset */
1920 if(arg->ui & TAGMASK)
1921 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
1926 wintoclient(Window w) {
1930 for(m = mons; m; m = m->next)
1931 for(c = m->clients; c; c = c->next)
1938 wintomon(Window w) {
1943 if(w == root && getrootptr(&x, &y))
1944 return ptrtomon(x, y);
1945 for(m = mons; m; m = m->next)
1948 if((c = wintoclient(w)))
1953 /* There's no way to check accesses to destroyed windows, thus those cases are
1954 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1955 * default error handler, which may call exit. */
1957 xerror(Display *dpy, XErrorEvent *ee) {
1958 if(ee->error_code == BadWindow
1959 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
1960 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
1961 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
1962 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
1963 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
1964 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
1965 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
1966 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
1968 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
1969 ee->request_code, ee->error_code);
1970 return xerrorxlib(dpy, ee); /* may call exit */
1974 xerrordummy(Display *dpy, XErrorEvent *ee) {
1978 /* Startup Error handler to check if another window manager
1979 * is already running. */
1981 xerrorstart(Display *dpy, XErrorEvent *ee) {
1987 zoom(const Arg *arg) {
1988 Client *c = selmon->sel;
1990 if(!selmon->lt[selmon->sellt]->arrange
1991 || selmon->lt[selmon->sellt]->arrange == monocle
1992 || (selmon->sel && selmon->sel->isfloating))
1994 if(c == nexttiled(selmon->clients))
1995 if(!c || !(c = nexttiled(c->next)))
2004 main(int argc, char *argv[]) {
2005 if(argc == 2 && !strcmp("-v", argv[1]))
2006 die("dwm-"VERSION", © 2006-2010 dwm engineers, see LICENSE for details\n");
2008 die("usage: dwm [-v]\n");
2009 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2010 fputs("warning: no locale support\n", stderr);
2011 if(!(dpy = XOpenDisplay(NULL)))
2012 die("dwm: cannot open display\n");