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, 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 *);
124 const char *ltsymbol;
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(void);
156 static void attach(Client *c);
157 static void attachstack(Client *c);
158 static void buttonpress(XEvent *e);
159 static void checkotherwm(void);
160 static void cleanup(void);
161 static void cleanupmons(void);
162 static void clearurgent(Client *c);
163 static void configure(Client *c);
164 static void configurenotify(XEvent *e);
165 static void configurerequest(XEvent *e);
166 static void destroynotify(XEvent *e);
167 static void detach(Client *c);
168 static void detachstack(Client *c);
169 static void die(const char *errstr, ...);
170 static Monitor *dirtomon(int dir);
171 static void drawbar(Monitor *m);
172 static void drawbars(void);
173 static void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
174 static void drawtext(const char *text, unsigned long col[ColLast], Bool invert);
175 static void enternotify(XEvent *e);
176 static void expose(XEvent *e);
177 static void focus(Client *c);
178 static void focusin(XEvent *e);
179 static void focusmon(const Arg *arg);
180 static void focusstack(const Arg *arg);
181 static unsigned long getcolor(const char *colstr);
182 static Bool getrootptr(int *x, int *y);
183 static long getstate(Window w);
184 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
185 static void grabbuttons(Client *c, Bool focused);
186 static void grabkeys(void);
187 static void initfont(const char *fontstr);
188 static Bool isprotodel(Client *c);
189 static void keypress(XEvent *e);
190 static void killclient(const Arg *arg);
191 static void manage(Window w, XWindowAttributes *wa);
192 static void mappingnotify(XEvent *e);
193 static void maprequest(XEvent *e);
194 static void monocle(Monitor *m);
195 static void movemouse(const Arg *arg);
196 static Client *nexttiled(Client *c);
197 static Monitor *ptrtomon(int x, int y);
198 static void propertynotify(XEvent *e);
199 static void quit(const Arg *arg);
200 static void resize(Client *c, int x, int y, int w, int h, Bool interact);
201 static void resizemouse(const Arg *arg);
202 static void restack(Monitor *m);
203 static void run(void);
204 static void scan(void);
205 static void sendmon(Client *c, Monitor *m);
206 static void setclientstate(Client *c, long state);
207 static void setlayout(const Arg *arg);
208 static void setmfact(const Arg *arg);
209 static void setup(void);
210 static void showhide(Client *c);
211 static void sigchld(int unused);
212 static void spawn(const Arg *arg);
213 static void tag(const Arg *arg);
214 static void tagmon(const Arg *arg);
215 static int textnw(const char *text, unsigned int len);
216 static void tile(Monitor *);
217 static void togglebar(const Arg *arg);
218 static void togglefloating(const Arg *arg);
219 static void toggletag(const Arg *arg);
220 static void toggleview(const Arg *arg);
221 static void unfocus(Client *c);
222 static void unmanage(Client *c, Bool destroyed);
223 static void unmapnotify(XEvent *e);
224 static Bool updategeom(void);
225 static void updatebarpos(Monitor *m);
226 static void updatebars(void);
227 static void updatenumlockmask(void);
228 static void updatesizehints(Client *c);
229 static void updatestatus(void);
230 static void updatetitle(Client *c);
231 static void updatewmhints(Client *c);
232 static void view(const Arg *arg);
233 static Client *wintoclient(Window w);
234 static Monitor *wintomon(Window w);
235 static int xerror(Display *dpy, XErrorEvent *ee);
236 static int xerrordummy(Display *dpy, XErrorEvent *ee);
237 static int xerrorstart(Display *dpy, XErrorEvent *ee);
238 static void zoom(const Arg *arg);
241 static const char broken[] = "broken";
242 static char stext[256];
244 static int sw, sh; /* X display screen geometry width, height */
245 static int bh, blw = 0; /* bar geometry */
246 static int (*xerrorxlib)(Display *, XErrorEvent *);
247 static unsigned int numlockmask = 0;
248 static void (*handler[LASTEvent]) (XEvent *) = {
249 [ButtonPress] = buttonpress,
250 [ConfigureRequest] = configurerequest,
251 [ConfigureNotify] = configurenotify,
252 [DestroyNotify] = destroynotify,
253 [EnterNotify] = enternotify,
256 [KeyPress] = keypress,
257 [MappingNotify] = mappingnotify,
258 [MapRequest] = maprequest,
259 [PropertyNotify] = propertynotify,
260 [UnmapNotify] = unmapnotify
262 static Atom wmatom[WMLast], netatom[NetLast];
264 static Bool running = True;
265 static Cursor cursor[CurLast];
268 static Monitor *mons = NULL, *selmon = NULL;
271 /* configuration, allows nested code to access above variables */
274 /* compile-time check if all tags fit into an unsigned int bit array. */
275 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
277 /* function implementations */
279 applyrules(Client *c) {
280 const char *class, *instance;
284 XClassHint ch = { 0 };
287 c->isfloating = c->tags = 0;
288 if(XGetClassHint(dpy, c->win, &ch)) {
289 class = ch.res_class ? ch.res_class : broken;
290 instance = ch.res_name ? ch.res_name : broken;
291 for(i = 0; i < LENGTH(rules); i++) {
293 if((!r->title || strstr(c->name, r->title))
294 && (!r->class || strstr(class, r->class))
295 && (!r->instance || strstr(instance, r->instance)))
297 c->isfloating = r->isfloating;
299 for(m = mons; m && m->num != r->monitor; m = m->next);
309 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
313 applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
317 /* set minimum possible */
325 if(*x + *w + 2 * c->bw < 0)
327 if(*y + *h + 2 * c->bw < 0)
331 if(*x > m->mx + m->mw)
332 *x = m->mx + m->mw - WIDTH(c);
333 if(*y > m->my + m->mh)
334 *y = m->my + m->mh - HEIGHT(c);
335 if(*x + *w + 2 * c->bw < m->mx)
337 if(*y + *h + 2 * c->bw < m->my)
344 if(resizehints || c->isfloating) {
345 /* see last two sentences in ICCCM 4.1.2.3 */
346 baseismin = c->basew == c->minw && c->baseh == c->minh;
347 if(!baseismin) { /* temporarily remove base dimensions */
351 /* adjust for aspect limits */
352 if(c->mina > 0 && c->maxa > 0) {
353 if(c->maxa < (float)*w / *h)
354 *w = *h * c->maxa + 0.5;
355 else if(c->mina < (float)*h / *w)
356 *h = *w * c->mina + 0.5;
358 if(baseismin) { /* increment calculation requires this */
362 /* adjust for increment value */
367 /* restore base dimensions */
370 *w = MAX(*w, c->minw);
371 *h = MAX(*h, c->minh);
373 *w = MIN(*w, c->maxw);
375 *h = MIN(*h, c->maxh);
377 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
384 for(m = mons; m; m = m->next)
387 for(m = mons; m; m = m->next) {
388 m->ltsymbol = m->lt[m->sellt]->symbol;
389 if(m->lt[m->sellt]->arrange)
390 m->lt[m->sellt]->arrange(m);
397 c->next = c->mon->clients;
402 attachstack(Client *c) {
403 c->snext = c->mon->stack;
408 buttonpress(XEvent *e) {
409 unsigned int i, x, click;
413 XButtonPressedEvent *ev = &e->xbutton;
416 /* focus monitor if necessary */
417 if((m = wintomon(ev->window)) && m != selmon) {
418 unfocus(selmon->sel);
422 if(ev->window == selmon->barwin) {
426 } while(ev->x >= x && ++i < LENGTH(tags));
427 if(i < LENGTH(tags)) {
431 else if(ev->x < x + blw)
433 else if(ev->x > selmon->wx + selmon->ww - TEXTW(stext))
434 click = ClkStatusText;
438 else if((c = wintoclient(ev->window))) {
440 click = ClkClientWin;
442 for(i = 0; i < LENGTH(buttons); i++)
443 if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
444 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
445 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
451 xerrorxlib = XSetErrorHandler(xerrorstart);
452 /* this causes an error if some other window manager is running */
453 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
456 die("dwm: another window manager is already running\n");
457 XSetErrorHandler(xerror);
464 Layout foo = { "", NULL };
468 selmon->lt[selmon->sellt] = &foo;
469 for(m = mons; m; m = m->next)
471 unmanage(m->stack, False);
473 XFreeFontSet(dpy, dc.font.set);
475 XFreeFont(dpy, dc.font.xfont);
476 XUngrabKey(dpy, AnyKey, AnyModifier, root);
477 XFreePixmap(dpy, dc.drawable);
479 XFreeCursor(dpy, cursor[CurNormal]);
480 XFreeCursor(dpy, cursor[CurResize]);
481 XFreeCursor(dpy, cursor[CurMove]);
484 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
493 XUnmapWindow(dpy, mons->barwin);
494 XDestroyWindow(dpy, mons->barwin);
501 clearurgent(Client *c) {
505 if(!(wmh = XGetWMHints(dpy, c->win)))
507 wmh->flags &= ~XUrgencyHint;
508 XSetWMHints(dpy, c->win, wmh);
513 configure(Client *c) {
516 ce.type = ConfigureNotify;
524 ce.border_width = c->bw;
526 ce.override_redirect = False;
527 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
531 configurenotify(XEvent *e) {
533 XConfigureEvent *ev = &e->xconfigure;
535 if(ev->window == root) {
540 XFreePixmap(dpy, dc.drawable);
541 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
543 for(m = mons; m; m = m->next)
544 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
551 configurerequest(XEvent *e) {
554 XConfigureRequestEvent *ev = &e->xconfigurerequest;
557 if((c = wintoclient(ev->window))) {
558 if(ev->value_mask & CWBorderWidth)
559 c->bw = ev->border_width;
560 else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
562 if(ev->value_mask & CWX)
563 c->x = m->mx + ev->x;
564 if(ev->value_mask & CWY)
565 c->y = m->my + ev->y;
566 if(ev->value_mask & CWWidth)
568 if(ev->value_mask & CWHeight)
570 if((c->x + c->w) > m->mx + m->mw && c->isfloating)
571 c->x = m->mx + (m->mw / 2 - c->w / 2); /* center in x direction */
572 if((c->y + c->h) > m->my + m->mh && c->isfloating)
573 c->y = m->my + (m->mh / 2 - c->h / 2); /* center in y direction */
574 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
577 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
585 wc.width = ev->width;
586 wc.height = ev->height;
587 wc.border_width = ev->border_width;
588 wc.sibling = ev->above;
589 wc.stack_mode = ev->detail;
590 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
596 destroynotify(XEvent *e) {
598 XDestroyWindowEvent *ev = &e->xdestroywindow;
600 if((c = wintoclient(ev->window)))
608 for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
613 detachstack(Client *c) {
616 for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
619 if(c == c->mon->sel) {
620 for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
626 die(const char *errstr, ...) {
629 va_start(ap, errstr);
630 vfprintf(stderr, errstr, ap);
640 if(!(m = selmon->next))
645 for(m = mons; m->next; m = m->next);
647 for(m = mons; m->next != selmon; m = m->next);
653 drawbar(Monitor *m) {
655 unsigned int i, occ = 0, urg = 0;
659 for(c = m->clients; c; c = c->next) {
665 for(i = 0; i < LENGTH(tags); i++) {
666 dc.w = TEXTW(tags[i]);
667 col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
668 drawtext(tags[i], col, urg & 1 << i);
669 drawsquare(m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
670 occ & 1 << i, urg & 1 << i, col);
673 dc.w = blw = TEXTW(m->ltsymbol);
674 drawtext(m->ltsymbol, dc.norm, False);
677 if(m == selmon) { /* status is only drawn on selected monitor */
684 drawtext(stext, dc.norm, False);
688 if((dc.w = dc.x - x) > bh) {
691 col = m == selmon ? dc.sel : dc.norm;
692 drawtext(m->sel->name, col, False);
693 drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
696 drawtext(NULL, dc.norm, False);
698 XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
706 for(m = mons; m; m = m->next)
711 drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
714 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
716 gcv.foreground = col[invert ? ColBG : ColFG];
717 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
718 x = (dc.font.ascent + dc.font.descent + 2) / 4;
722 r.width = r.height = x + 1;
723 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
726 r.width = r.height = x;
727 XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
732 drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
734 int i, x, y, h, len, olen;
735 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
737 XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
738 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
742 h = dc.font.ascent + dc.font.descent;
743 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
745 /* shorten text if necessary */
746 for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
749 memcpy(buf, text, len);
751 for(i = len; i && i > len - 3; buf[--i] = '.');
752 XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
754 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
756 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
760 enternotify(XEvent *e) {
763 XCrossingEvent *ev = &e->xcrossing;
765 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
767 if((m = wintomon(ev->window)) && m != selmon) {
768 unfocus(selmon->sel);
771 if((c = wintoclient(ev->window)))
780 XExposeEvent *ev = &e->xexpose;
782 if(ev->count == 0 && (m = wintomon(ev->window)))
788 if(!c || !ISVISIBLE(c))
789 for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
791 unfocus(selmon->sel);
799 grabbuttons(c, True);
800 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
801 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
804 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
810 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
811 XFocusChangeEvent *ev = &e->xfocus;
813 if(selmon->sel && ev->window != selmon->sel->win)
814 XSetInputFocus(dpy, selmon->sel->win, RevertToPointerRoot, CurrentTime);
818 focusmon(const Arg *arg) {
823 m = dirtomon(arg->i);
824 unfocus(selmon->sel);
830 focusstack(const Arg *arg) {
831 Client *c = NULL, *i;
836 for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
838 for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
841 for(i = selmon->clients; i != selmon->sel; i = i->next)
845 for(; i; i = i->next)
856 getcolor(const char *colstr) {
857 Colormap cmap = DefaultColormap(dpy, screen);
860 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
861 die("error, cannot allocate color '%s'\n", colstr);
866 getrootptr(int *x, int *y) {
871 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
878 unsigned char *p = NULL;
879 unsigned long n, extra;
882 status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
883 &real, &format, &n, &extra, (unsigned char **)&p);
884 if(status != Success)
893 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
898 if(!text || size == 0)
901 XGetTextProperty(dpy, w, &name, atom);
904 if(name.encoding == XA_STRING)
905 strncpy(text, (char *)name.value, size - 1);
907 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
908 strncpy(text, *list, size - 1);
909 XFreeStringList(list);
912 text[size - 1] = '\0';
918 grabbuttons(Client *c, Bool focused) {
922 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
923 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
925 for(i = 0; i < LENGTH(buttons); i++)
926 if(buttons[i].click == ClkClientWin)
927 for(j = 0; j < LENGTH(modifiers); j++)
928 XGrabButton(dpy, buttons[i].button,
929 buttons[i].mask | modifiers[j],
930 c->win, False, BUTTONMASK,
931 GrabModeAsync, GrabModeSync, None, None);
934 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
935 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
944 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
947 XUngrabKey(dpy, AnyKey, AnyModifier, root);
948 for(i = 0; i < LENGTH(keys); i++) {
949 if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
950 for(j = 0; j < LENGTH(modifiers); j++)
951 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
952 True, GrabModeAsync, GrabModeAsync);
958 initfont(const char *fontstr) {
959 char *def, **missing;
963 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
966 fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
967 XFreeStringList(missing);
970 XFontSetExtents *font_extents;
971 XFontStruct **xfonts;
974 dc.font.ascent = dc.font.descent = 0;
975 font_extents = XExtentsOfFontSet(dc.font.set);
976 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
977 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
978 dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
979 dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
984 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
985 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
986 die("error, cannot load font: '%s'\n", fontstr);
987 dc.font.ascent = dc.font.xfont->ascent;
988 dc.font.descent = dc.font.xfont->descent;
990 dc.font.height = dc.font.ascent + dc.font.descent;
994 isprotodel(Client *c) {
999 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1000 for(i = 0; !ret && i < n; i++)
1001 if(protocols[i] == wmatom[WMDelete])
1009 keypress(XEvent *e) {
1015 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1016 for(i = 0; i < LENGTH(keys); i++)
1017 if(keysym == keys[i].keysym
1018 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1020 keys[i].func(&(keys[i].arg));
1024 killclient(const Arg *arg) {
1029 if(isprotodel(selmon->sel)) {
1030 ev.type = ClientMessage;
1031 ev.xclient.window = selmon->sel->win;
1032 ev.xclient.message_type = wmatom[WMProtocols];
1033 ev.xclient.format = 32;
1034 ev.xclient.data.l[0] = wmatom[WMDelete];
1035 ev.xclient.data.l[1] = CurrentTime;
1036 XSendEvent(dpy, selmon->sel->win, False, NoEventMask, &ev);
1040 XSetErrorHandler(xerrordummy);
1041 XSetCloseDownMode(dpy, DestroyAll);
1042 XKillClient(dpy, selmon->sel->win);
1044 XSetErrorHandler(xerror);
1050 manage(Window w, XWindowAttributes *wa) {
1052 Client *c, *t = NULL;
1053 Window trans = None;
1056 if(!(c = malloc(sizeof(Client))))
1057 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1061 if(XGetTransientForHint(dpy, w, &trans))
1062 t = wintoclient(trans);
1072 c->x = wa->x + c->mon->wx;
1073 c->y = wa->y + c->mon->wy;
1076 c->oldbw = wa->border_width;
1077 if(c->w == c->mon->mw && c->h == c->mon->mh) {
1083 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1084 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1085 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1086 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1087 c->x = MAX(c->x, c->mon->mx);
1088 /* only fix client y-offset, if the client center might cover the bar */
1089 c->y = MAX(c->y, ((c->mon->by == 0) && (c->x + (c->w / 2) >= c->mon->wx)
1090 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1093 wc.border_width = c->bw;
1094 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1095 XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
1096 configure(c); /* propagates border_width, if size doesn't change */
1098 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1099 grabbuttons(c, False);
1101 c->isfloating = trans != None || c->isfixed;
1103 XRaiseWindow(dpy, c->win);
1106 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1107 XMapWindow(dpy, c->win);
1108 setclientstate(c, NormalState);
1113 mappingnotify(XEvent *e) {
1114 XMappingEvent *ev = &e->xmapping;
1116 XRefreshKeyboardMapping(ev);
1117 if(ev->request == MappingKeyboard)
1122 maprequest(XEvent *e) {
1123 static XWindowAttributes wa;
1124 XMapRequestEvent *ev = &e->xmaprequest;
1126 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1128 if(wa.override_redirect)
1130 if(!wintoclient(ev->window))
1131 manage(ev->window, &wa);
1135 monocle(Monitor *m) {
1136 static char ntext[8];
1140 for(c = m->clients; c; c = c->next)
1143 if(n > 0) { /* override layout symbol */
1144 snprintf(ntext, sizeof ntext, "[%d]", n);
1145 m->ltsymbol = ntext;
1147 for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
1148 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
1152 movemouse(const Arg *arg) {
1153 int x, y, ocx, ocy, nx, ny;
1158 if(!(c = selmon->sel))
1163 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1164 None, cursor[CurMove], CurrentTime) != GrabSuccess)
1166 if(!getrootptr(&x, &y))
1169 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1171 case ConfigureRequest:
1174 handler[ev.type](&ev);
1177 nx = ocx + (ev.xmotion.x - x);
1178 ny = ocy + (ev.xmotion.y - y);
1179 if(snap && nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1180 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1181 if(abs(selmon->wx - nx) < snap)
1183 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1184 nx = selmon->wx + selmon->ww - WIDTH(c);
1185 if(abs(selmon->wy - ny) < snap)
1187 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1188 ny = selmon->wy + selmon->wh - HEIGHT(c);
1189 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1190 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1191 togglefloating(NULL);
1193 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1194 resize(c, nx, ny, c->w, c->h, True);
1197 } while(ev.type != ButtonRelease);
1198 XUngrabPointer(dpy, CurrentTime);
1199 if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
1207 nexttiled(Client *c) {
1208 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1213 ptrtomon(int x, int y) {
1216 for(m = mons; m; m = m->next)
1217 if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh))
1223 propertynotify(XEvent *e) {
1226 XPropertyEvent *ev = &e->xproperty;
1228 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1230 else if(ev->state == PropertyDelete)
1231 return; /* ignore */
1232 else if((c = wintoclient(ev->window))) {
1235 case XA_WM_TRANSIENT_FOR:
1236 XGetTransientForHint(dpy, c->win, &trans);
1237 if(!c->isfloating && (c->isfloating = (wintoclient(trans) != NULL)))
1240 case XA_WM_NORMAL_HINTS:
1248 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1250 if(c == c->mon->sel)
1257 quit(const Arg *arg) {
1262 resize(Client *c, int x, int y, int w, int h, Bool interact) {
1265 if(applysizehints(c, &x, &y, &w, &h, interact)) {
1268 c->w = wc.width = w;
1269 c->h = wc.height = h;
1270 wc.border_width = c->bw;
1271 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1278 resizemouse(const Arg *arg) {
1285 if(!(c = selmon->sel))
1290 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1291 None, cursor[CurResize], CurrentTime) != GrabSuccess)
1293 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1295 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1297 case ConfigureRequest:
1300 handler[ev.type](&ev);
1303 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1304 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1305 if(snap && nw >= selmon->wx && nw <= selmon->wx + selmon->ww
1306 && nh >= selmon->wy && nh <= selmon->wy + selmon->wh)
1308 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1309 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1310 togglefloating(NULL);
1312 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1313 resize(c, c->x, c->y, nw, nh, True);
1316 } while(ev.type != ButtonRelease);
1317 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1318 XUngrabPointer(dpy, CurrentTime);
1319 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1320 if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
1328 restack(Monitor *m) {
1336 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1337 XRaiseWindow(dpy, m->sel->win);
1338 if(m->lt[m->sellt]->arrange) {
1339 wc.stack_mode = Below;
1340 wc.sibling = m->barwin;
1341 for(c = m->stack; c; c = c->snext)
1342 if(!c->isfloating && ISVISIBLE(c)) {
1343 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1344 wc.sibling = c->win;
1348 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1355 /* main event loop */
1357 while(running && !XNextEvent(dpy, &ev))
1358 if(handler[ev.type])
1359 handler[ev.type](&ev); /* call handler */
1364 unsigned int i, num;
1365 Window d1, d2, *wins = NULL;
1366 XWindowAttributes wa;
1368 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1369 for(i = 0; i < num; i++) {
1370 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1371 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1373 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1374 manage(wins[i], &wa);
1376 for(i = 0; i < num; i++) { /* now the transients */
1377 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1379 if(XGetTransientForHint(dpy, wins[i], &d1)
1380 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1381 manage(wins[i], &wa);
1389 sendmon(Client *c, Monitor *m) {
1396 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1404 setclientstate(Client *c, long state) {
1405 long data[] = { state, None };
1407 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1408 PropModeReplace, (unsigned char *)data, 2);
1412 setlayout(const Arg *arg) {
1413 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1416 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1423 /* arg > 1.0 will set mfact absolutly */
1425 setmfact(const Arg *arg) {
1428 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1430 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1431 if(f < 0.1 || f > 0.9)
1439 XSetWindowAttributes wa;
1441 /* clean up any zombies immediately */
1445 screen = DefaultScreen(dpy);
1446 root = RootWindow(dpy, screen);
1448 sw = DisplayWidth(dpy, screen);
1449 sh = DisplayHeight(dpy, screen);
1450 bh = dc.h = dc.font.height + 2;
1453 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1454 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1455 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1456 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1457 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1459 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1460 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1461 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1462 /* init appearance */
1463 dc.norm[ColBorder] = getcolor(normbordercolor);
1464 dc.norm[ColBG] = getcolor(normbgcolor);
1465 dc.norm[ColFG] = getcolor(normfgcolor);
1466 dc.sel[ColBorder] = getcolor(selbordercolor);
1467 dc.sel[ColBG] = getcolor(selbgcolor);
1468 dc.sel[ColFG] = getcolor(selfgcolor);
1469 dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
1470 dc.gc = XCreateGC(dpy, root, 0, NULL);
1471 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1473 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
1477 /* EWMH support per view */
1478 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1479 PropModeReplace, (unsigned char *) netatom, NetLast);
1480 /* select for events */
1481 wa.cursor = cursor[CurNormal];
1482 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
1483 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
1484 |PropertyChangeMask;
1485 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1486 XSelectInput(dpy, root, wa.event_mask);
1491 showhide(Client *c) {
1494 if(ISVISIBLE(c)) { /* show clients top down */
1495 XMoveWindow(dpy, c->win, c->x, c->y);
1496 if(!c->mon->lt[c->mon->sellt]->arrange || c->isfloating)
1497 resize(c, c->x, c->y, c->w, c->h, False);
1500 else { /* hide clients bottom up */
1502 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
1508 sigchld(int unused) {
1509 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1510 die("Can't install SIGCHLD handler");
1511 while(0 < waitpid(-1, NULL, WNOHANG));
1515 spawn(const Arg *arg) {
1518 close(ConnectionNumber(dpy));
1520 execvp(((char **)arg->v)[0], (char **)arg->v);
1521 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1528 tag(const Arg *arg) {
1529 if(selmon->sel && arg->ui & TAGMASK) {
1530 selmon->sel->tags = arg->ui & TAGMASK;
1536 tagmon(const Arg *arg) {
1537 if(!selmon->sel || !mons->next)
1539 sendmon(selmon->sel, dirtomon(arg->i));
1543 textnw(const char *text, unsigned int len) {
1547 XmbTextExtents(dc.font.set, text, len, NULL, &r);
1550 return XTextWidth(dc.font.xfont, text, len);
1559 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1563 c = nexttiled(m->clients);
1564 mw = m->mfact * m->ww;
1565 resize(c, m->wx, m->wy, (n == 1 ? m->ww : mw) - 2 * c->bw, m->wh - 2 * c->bw, False);
1569 x = (m->wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : m->wx + mw;
1571 w = (m->wx + mw > c->x + c->w) ? m->wx + m->ww - x : m->ww - mw;
1575 for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
1576 resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
1577 ? m->wy + m->wh - y - 2 * c->bw : h - 2 * c->bw), False);
1579 y = c->y + HEIGHT(c);
1584 togglebar(const Arg *arg) {
1585 selmon->showbar = !selmon->showbar;
1586 updatebarpos(selmon);
1587 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1592 togglefloating(const Arg *arg) {
1595 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1596 if(selmon->sel->isfloating)
1597 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1598 selmon->sel->w, selmon->sel->h, False);
1603 toggletag(const Arg *arg) {
1604 unsigned int newtags;
1608 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1610 selmon->sel->tags = newtags;
1616 toggleview(const Arg *arg) {
1617 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1620 selmon->tagset[selmon->seltags] = newtagset;
1626 unfocus(Client *c) {
1629 grabbuttons(c, False);
1630 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
1631 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1635 unmanage(Client *c, Bool destroyed) {
1638 /* The server grab construct avoids race conditions. */
1642 wc.border_width = c->oldbw;
1644 XSetErrorHandler(xerrordummy);
1645 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1646 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1647 setclientstate(c, WithdrawnState);
1649 XSetErrorHandler(xerror);
1658 unmapnotify(XEvent *e) {
1660 XUnmapEvent *ev = &e->xunmap;
1662 if((c = wintoclient(ev->window)))
1669 XSetWindowAttributes wa;
1671 wa.override_redirect = True;
1672 wa.background_pixmap = ParentRelative;
1673 wa.event_mask = ButtonPressMask|ExposureMask;
1674 for(m = mons; m; m = m->next) {
1675 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1676 CopyFromParent, DefaultVisual(dpy, screen),
1677 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1678 XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
1679 XMapRaised(dpy, m->barwin);
1684 updatebarpos(Monitor *m) {
1689 m->by = m->topbar ? m->wy : m->wy + m->wh;
1690 m->wy = m->topbar ? m->wy + bh : m->wy;
1698 int i, j, nn = 1, n = 1;
1700 Monitor *newmons = NULL, *m = NULL, *tm;
1703 XineramaScreenInfo *info = NULL;
1706 if(XineramaIsActive(dpy))
1707 info = XineramaQueryScreens(dpy, &n);
1708 flags = (Bool *)malloc(sizeof(Bool) * n);
1709 for(i = 0; i < n; i++)
1711 /* next double-loop seeks any combination of retrieved Xinerama info
1712 * with existing monitors, this is used to avoid unnecessary
1713 * re-allocations of monitor structs */
1714 for(i = 0, nn = n; i < n; i++)
1715 for(j = 0, m = mons; m; m = m->next, j++)
1718 info[i].x_org == m->mx
1719 && info[i].y_org == m->my
1720 && info[i].width == m->mw
1721 && info[i].height == m->mh)
1725 if(nn == 0) { /* no need to re-allocate monitors */
1727 for(i = 0, m = mons; m; m = m->next, i++) {
1728 m->num = info[i].screen_number;
1729 if(info[i].x_org != m->mx
1730 || info[i].y_org != m->my
1731 || info[i].width != m->mw
1732 || info[i].height != m->mh)
1734 m->mx = m->wx = info[i].x_org;
1735 m->my = m->wy = info[i].y_org;
1736 m->mw = m->ww = info[i].width;
1737 m->mh = m->wh = info[i].height;
1746 /* next algorithm only considers unique geometries as separate screens */
1747 for(i = 0; i < n; i++)
1748 flags[i] = False; /* used for ignoring certain monitors */
1749 for(i = 0, nn = n; i < n; i++)
1750 for(j = 0; j < n; j++)
1751 if(i != j && !flags[i]) {
1753 info[i].x_org == info[j].x_org
1754 && info[i].y_org == info[j].y_org
1755 && info[i].width == info[j].width
1756 && info[i].height == info[j].height)
1760 #endif /* XINERAMA */
1761 /* allocate monitor(s) for the new geometry setup */
1762 for(i = 0; i < nn; i++) {
1763 if(!(m = (Monitor *)malloc(sizeof(Monitor))))
1764 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
1768 /* initialise monitor(s) */
1770 if(XineramaIsActive(dpy)) {
1771 for(i = 0, m = newmons; m && i < n; i++) {
1772 if(!flags[i]) { /* only use screens that aren't dublettes */
1773 m->num = info[i].screen_number;
1774 m->mx = m->wx = info[i].x_org;
1775 m->my = m->wy = info[i].y_org;
1776 m->mw = m->ww = info[i].width;
1777 m->mh = m->wh = info[i].height;
1785 #endif /* XINERAMA */
1786 /* default monitor setup */
1794 /* bar geometry setup */
1795 for(m = newmons; m; m = m->next) {
1796 m->sel = m->stack = m->clients = NULL;
1799 m->tagset[0] = m->tagset[1] = 1;
1801 m->showbar = showbar;
1803 m->lt[0] = &layouts[0];
1804 m->lt[1] = &layouts[1 % LENGTH(layouts)];
1805 m->ltsymbol = layouts[0].symbol;
1808 /* reassign left over clients of disappeared monitors */
1809 for(tm = mons; tm; tm = tm->next)
1810 while(tm->clients) {
1812 tm->clients = c->next;
1818 /* select focused monitor */
1820 selmon = mons = newmons;
1821 selmon = wintomon(root);
1826 updatenumlockmask(void) {
1828 XModifierKeymap *modmap;
1831 modmap = XGetModifierMapping(dpy);
1832 for(i = 0; i < 8; i++)
1833 for(j = 0; j < modmap->max_keypermod; j++)
1834 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1835 == XKeysymToKeycode(dpy, XK_Num_Lock))
1836 numlockmask = (1 << i);
1837 XFreeModifiermap(modmap);
1841 updatesizehints(Client *c) {
1845 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
1846 /* size is uninitialized, ensure that size.flags aren't used */
1848 if(size.flags & PBaseSize) {
1849 c->basew = size.base_width;
1850 c->baseh = size.base_height;
1852 else if(size.flags & PMinSize) {
1853 c->basew = size.min_width;
1854 c->baseh = size.min_height;
1857 c->basew = c->baseh = 0;
1858 if(size.flags & PResizeInc) {
1859 c->incw = size.width_inc;
1860 c->inch = size.height_inc;
1863 c->incw = c->inch = 0;
1864 if(size.flags & PMaxSize) {
1865 c->maxw = size.max_width;
1866 c->maxh = size.max_height;
1869 c->maxw = c->maxh = 0;
1870 if(size.flags & PMinSize) {
1871 c->minw = size.min_width;
1872 c->minh = size.min_height;
1874 else if(size.flags & PBaseSize) {
1875 c->minw = size.base_width;
1876 c->minh = size.base_height;
1879 c->minw = c->minh = 0;
1880 if(size.flags & PAspect) {
1881 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
1882 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
1885 c->maxa = c->mina = 0.0;
1886 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
1887 && c->maxw == c->minw && c->maxh == c->minh);
1891 updatetitle(Client *c) {
1892 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
1893 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
1894 if(c->name[0] == '\0') /* hack to mark broken clients */
1895 strcpy(c->name, broken);
1899 updatestatus(void) {
1900 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
1901 strcpy(stext, "dwm-"VERSION);
1906 updatewmhints(Client *c) {
1909 if((wmh = XGetWMHints(dpy, c->win))) {
1910 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
1911 wmh->flags &= ~XUrgencyHint;
1912 XSetWMHints(dpy, c->win, wmh);
1915 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
1921 view(const Arg *arg) {
1922 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
1924 selmon->seltags ^= 1; /* toggle sel tagset */
1925 if(arg->ui & TAGMASK)
1926 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
1931 wintoclient(Window w) {
1935 for(m = mons; m; m = m->next)
1936 for(c = m->clients; c; c = c->next)
1943 wintomon(Window w) {
1948 if(w == root && getrootptr(&x, &y))
1949 return ptrtomon(x, y);
1950 for(m = mons; m; m = m->next)
1953 if((c = wintoclient(w)))
1958 /* There's no way to check accesses to destroyed windows, thus those cases are
1959 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1960 * default error handler, which may call exit. */
1962 xerror(Display *dpy, XErrorEvent *ee) {
1963 if(ee->error_code == BadWindow
1964 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
1965 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
1966 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
1967 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
1968 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
1969 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
1970 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
1971 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
1973 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
1974 ee->request_code, ee->error_code);
1975 return xerrorxlib(dpy, ee); /* may call exit */
1979 xerrordummy(Display *dpy, XErrorEvent *ee) {
1983 /* Startup Error handler to check if another window manager
1984 * is already running. */
1986 xerrorstart(Display *dpy, XErrorEvent *ee) {
1992 zoom(const Arg *arg) {
1993 Client *c = selmon->sel;
1995 if(!selmon->lt[selmon->sellt]->arrange
1996 || selmon->lt[selmon->sellt]->arrange == monocle
1997 || (selmon->sel && selmon->sel->isfloating))
1999 if(c == nexttiled(selmon->clients))
2000 if(!c || !(c = nexttiled(c->next)))
2009 main(int argc, char *argv[]) {
2010 if(argc == 2 && !strcmp("-v", argv[1]))
2011 die("dwm-"VERSION", © 2006-2009 dwm engineers, see LICENSE for details\n");
2013 die("usage: dwm [-v]\n");
2014 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2015 fputs("warning: no locale support\n", stderr);
2016 if(!(dpy = XOpenDisplay(NULL)))
2017 die("dwm: cannot open display\n");