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 global
15 * linked client list, the focus history is remembered through a global
16 * stack list. 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(M, C) ((M) == (C->m) && (C->tags & M->tagset[M->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 ((int)((1LL << 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 *);
126 int by, btx; /* bar geometry */
127 int wx, wy, ww, wh; /* window area */
128 unsigned int seltags;
130 unsigned int tagset[2];
139 const char *instance;
145 /* function declarations */
146 static void applyrules(Client *c);
147 static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h);
148 static void arrange(void);
149 static void attach(Client *c);
150 static void attachstack(Client *c);
151 static void buttonpress(XEvent *e);
152 static void checkotherwm(void);
153 static void cleanup(void);
154 static void cleanupmons(void);
155 static void clearurgent(Client *c);
156 static void configure(Client *c);
157 static void configurenotify(XEvent *e);
158 static void configurerequest(XEvent *e);
159 static void destroynotify(XEvent *e);
160 static void detach(Client *c);
161 static void detachstack(Client *c);
162 static void die(const char *errstr, ...);
163 static void drawbar(Monitor *m);
164 static void drawbars();
165 static void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
166 static void drawtext(const char *text, unsigned long col[ColLast], Bool invert);
167 static void enternotify(XEvent *e);
168 static void expose(XEvent *e);
169 static void focus(Client *c);
170 static void focusin(XEvent *e);
171 static void focusstack(const Arg *arg);
172 static Client *getclient(Window w);
173 static unsigned long getcolor(const char *colstr);
174 static long getstate(Window w);
175 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
176 static void grabbuttons(Client *c, Bool focused);
177 static void grabkeys(void);
178 static void initfont(const char *fontstr);
179 static Bool isprotodel(Client *c);
180 static void keypress(XEvent *e);
181 static void killclient(const Arg *arg);
182 static void manage(Window w, XWindowAttributes *wa);
183 static void mappingnotify(XEvent *e);
184 static void maprequest(XEvent *e);
185 static void monocle(Monitor *m);
186 static void movemouse(const Arg *arg);
187 static Client *nexttiled(Monitor *m, Client *c);
188 static void propertynotify(XEvent *e);
189 static void quit(const Arg *arg);
190 static void resize(Client *c, int x, int y, int w, int h);
191 static void resizemouse(const Arg *arg);
192 static void restack(Monitor *m);
193 static void run(void);
194 static void scan(void);
195 static void setclientstate(Client *c, long state);
196 static void setlayout(const Arg *arg);
197 static void setmfact(const Arg *arg);
198 static void setup(void);
199 static void showhide(Client *c);
200 static void sigchld(int signal);
201 static void spawn(const Arg *arg);
202 static void tag(const Arg *arg);
203 static int textnw(const char *text, unsigned int len);
204 static void tile(Monitor *);
205 static void togglebar(const Arg *arg);
206 static void togglefloating(const Arg *arg);
207 static void toggletag(const Arg *arg);
208 static void toggleview(const Arg *arg);
209 static void unmanage(Client *c);
210 static void unmapnotify(XEvent *e);
211 static void updategeom(void);
212 static void updatebars(void);
213 static void updatenumlockmask(void);
214 static void updatesizehints(Client *c);
215 static void updatestatus(void);
216 static void updatetitle(Client *c);
217 static void updatewmhints(Client *c);
218 static void view(const Arg *arg);
219 static int xerror(Display *dpy, XErrorEvent *ee);
220 static int xerrordummy(Display *dpy, XErrorEvent *ee);
221 static int xerrorstart(Display *dpy, XErrorEvent *ee);
222 static void zoom(const Arg *arg);
224 static void focusmon(const Arg *arg);
225 static void tagmon(const Arg *arg);
226 #endif /* XINERAMA */
229 static char stext[256];
231 static int sx, sy, sw, sh; /* X display screen geometry x, y, width, height */
232 static int bh, blw = 0; /* bar geometry */
233 static int (*xerrorxlib)(Display *, XErrorEvent *);
234 static unsigned int numlockmask = 0;
235 static void (*handler[LASTEvent]) (XEvent *) = {
236 [ButtonPress] = buttonpress,
237 [ConfigureRequest] = configurerequest,
238 [ConfigureNotify] = configurenotify,
239 [DestroyNotify] = destroynotify,
240 [EnterNotify] = enternotify,
243 [KeyPress] = keypress,
244 [MappingNotify] = mappingnotify,
245 [MapRequest] = maprequest,
246 [PropertyNotify] = propertynotify,
247 [UnmapNotify] = unmapnotify
249 static Atom wmatom[WMLast], netatom[NetLast];
251 static Bool running = True;
252 static Client *clients = NULL;
253 static Client *sel = NULL;
254 static Client *stack = NULL;
255 static Cursor cursor[CurLast];
258 static Layout *lt[] = { NULL, NULL };
259 static Monitor *mons = NULL, *selmon = NULL;
261 /* configuration, allows nested code to access above variables */
264 /* compile-time check if all tags fit into an unsigned int bit array. */
265 struct NumTags { char limitexceeded[sizeof(unsigned int) * 8 < LENGTH(tags) ? -1 : 1]; };
267 /* function implementations */
269 applyrules(Client *c) {
272 XClassHint ch = { 0 };
275 c->isfloating = c->tags = 0;
276 if(XGetClassHint(dpy, c->win, &ch)) {
277 for(i = 0; i < LENGTH(rules); i++) {
279 if((!r->title || strstr(c->name, r->title))
280 && (!r->class || (ch.res_class && strstr(ch.res_class, r->class)))
281 && (!r->instance || (ch.res_name && strstr(ch.res_name, r->instance)))) {
282 c->isfloating = r->isfloating;
291 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->m->tagset[c->m->seltags];
295 applysizehints(Client *c, int *x, int *y, int *w, int *h) {
298 /* set minimum possible */
306 if(*x + *w + 2 * c->bw < sx)
308 if(*y + *h + 2 * c->bw < sy)
315 if(resizehints || c->isfloating) {
316 /* see last two sentences in ICCCM 4.1.2.3 */
317 baseismin = c->basew == c->minw && c->baseh == c->minh;
319 if(!baseismin) { /* temporarily remove base dimensions */
324 /* adjust for aspect limits */
325 if(c->mina > 0 && c->maxa > 0) {
326 if(c->maxa < (float)*w / *h)
328 else if(c->mina < (float)*h / *w)
332 if(baseismin) { /* increment calculation requires this */
337 /* adjust for increment value */
343 /* restore base dimensions */
347 *w = MAX(*w, c->minw);
348 *h = MAX(*h, c->minh);
351 *w = MIN(*w, c->maxw);
354 *h = MIN(*h, c->maxh);
356 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
365 for(m = mons; m; m = m->next) {
366 if(lt[m->sellt]->arrange)
367 lt[m->sellt]->arrange(m);
379 attachstack(Client *c) {
385 buttonpress(XEvent *e) {
386 unsigned int i, x, click;
389 XButtonPressedEvent *ev = &e->xbutton;
392 if(ev->window == selmon->barwin) {
397 while(ev->x >= x && ++i < LENGTH(tags));
398 if(i < LENGTH(tags)) {
402 else if(ev->x < x + blw)
404 else if(ev->x > selmon->wx + selmon->ww - TEXTW(stext))
405 click = ClkStatusText;
409 else if((c = getclient(ev->window))) {
411 click = ClkClientWin;
414 for(i = 0; i < LENGTH(buttons); i++)
415 if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
416 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
417 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
423 xerrorxlib = XSetErrorHandler(xerrorstart);
425 /* this causes an error if some other window manager is running */
426 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
429 die("dwm: another window manager is already running\n");
430 XSetErrorHandler(xerror);
437 Layout foo = { "", NULL };
440 lt[selmon->sellt] = &foo;
444 XFreeFontSet(dpy, dc.font.set);
446 XFreeFont(dpy, dc.font.xfont);
447 XUngrabKey(dpy, AnyKey, AnyModifier, root);
448 XFreePixmap(dpy, dc.drawable);
450 XFreeCursor(dpy, cursor[CurNormal]);
451 XFreeCursor(dpy, cursor[CurResize]);
452 XFreeCursor(dpy, cursor[CurMove]);
455 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
464 XUnmapWindow(dpy, mons->barwin);
465 XDestroyWindow(dpy, mons->barwin);
472 clearurgent(Client *c) {
476 if(!(wmh = XGetWMHints(dpy, c->win)))
478 wmh->flags &= ~XUrgencyHint;
479 XSetWMHints(dpy, c->win, wmh);
484 configure(Client *c) {
487 ce.type = ConfigureNotify;
495 ce.border_width = c->bw;
497 ce.override_redirect = False;
498 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
502 configurenotify(XEvent *e) {
504 XConfigureEvent *ev = &e->xconfigure;
506 if(ev->window == root && (ev->width != sw || ev->height != sh)) {
511 XFreePixmap(dpy, dc.drawable);
512 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
514 for(m = mons; m; m = m->next)
515 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
521 configurerequest(XEvent *e) {
523 XConfigureRequestEvent *ev = &e->xconfigurerequest;
526 if((c = getclient(ev->window))) {
527 if(ev->value_mask & CWBorderWidth)
528 c->bw = ev->border_width;
529 else if(c->isfloating || !lt[selmon->sellt]->arrange) {
530 if(ev->value_mask & CWX)
532 if(ev->value_mask & CWY)
534 if(ev->value_mask & CWWidth)
536 if(ev->value_mask & CWHeight)
538 if((c->x - sx + c->w) > sw && c->isfloating)
539 c->x = sx + (sw / 2 - c->w / 2); /* center in x direction */
540 if((c->y - sy + c->h) > sh && c->isfloating)
541 c->y = sy + (sh / 2 - c->h / 2); /* center in y direction */
542 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
544 if(ISVISIBLE((c->m), c))
545 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
553 wc.width = ev->width;
554 wc.height = ev->height;
555 wc.border_width = ev->border_width;
556 wc.sibling = ev->above;
557 wc.stack_mode = ev->detail;
558 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
564 destroynotify(XEvent *e) {
566 XDestroyWindowEvent *ev = &e->xdestroywindow;
568 if((c = getclient(ev->window)))
576 for(tc = &clients; *tc && *tc != c; tc = &(*tc)->next);
581 detachstack(Client *c) {
584 for(tc = &stack; *tc && *tc != c; tc = &(*tc)->snext);
589 die(const char *errstr, ...) {
592 va_start(ap, errstr);
593 vfprintf(stderr, errstr, ap);
599 drawbar(Monitor *m) {
601 unsigned int i, occ = 0, urg = 0;
605 for(c = clients; c; c = c->next) {
617 dc.w = TEXTW(m->symbol);
618 drawtext(NULL, selmon == m ? dc.sel : dc.norm, False);
622 #endif /* XINERAMA */
624 for(i = 0; i < LENGTH(tags); i++) {
625 dc.w = TEXTW(tags[i]);
626 col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
627 drawtext(tags[i], col, urg & 1 << i);
628 drawsquare(m == selmon && sel && sel->tags & 1 << i,
629 occ & 1 << i, urg & 1 << i, col);
634 drawtext(lt[m->sellt]->symbol, dc.norm, False);
646 drawtext(stext, dc.norm, False);
647 if((dc.w = dc.x - x) > bh) {
650 drawtext(sel->name, dc.sel, False);
651 drawsquare(sel->isfixed, sel->isfloating, False, dc.sel);
654 drawtext(NULL, dc.norm, False);
660 drawtext(NULL, dc.norm, False);
662 XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
670 for(m = mons; m; m = m->next)
675 drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
678 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
680 gcv.foreground = col[invert ? ColBG : ColFG];
681 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
682 x = (dc.font.ascent + dc.font.descent + 2) / 4;
686 r.width = r.height = x + 1;
687 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
690 r.width = r.height = x;
691 XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
696 drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
698 int i, x, y, h, len, olen;
699 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
701 XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
702 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
706 h = dc.font.ascent + dc.font.descent;
707 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
709 /* shorten text if necessary */
710 for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
713 memcpy(buf, text, len);
715 for(i = len; i && i > len - 3; buf[--i] = '.');
716 XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
718 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
720 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
724 enternotify(XEvent *e) {
726 XCrossingEvent *ev = &e->xcrossing;
728 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
730 if((c = getclient(ev->window)))
739 XExposeEvent *ev = &e->xexpose;
742 for(m = mons; m; m = m->next)
743 if(ev->window == m->barwin) {
751 if(!c || !ISVISIBLE((c->m), c))
752 for(c = stack; c && !ISVISIBLE(selmon, c); c = c->snext);
753 if(sel && sel != c) {
754 grabbuttons(sel, False);
755 XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
762 grabbuttons(c, True);
763 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
764 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
767 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
773 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
774 XFocusChangeEvent *ev = &e->xfocus;
776 if(sel && ev->window != sel->win)
777 XSetInputFocus(dpy, sel->win, RevertToPointerRoot, CurrentTime);
782 focusmon(const Arg *arg) {
786 for(i = 0, m = mons; m; m = m->next, i++)
794 #endif /* XINERAMA */
797 focusstack(const Arg *arg) {
798 Client *c = NULL, *i;
803 for(c = sel->next; c && !ISVISIBLE(selmon, c); c = c->next);
805 for(c = clients; c && !ISVISIBLE(selmon, c); c = c->next);
808 for(i = clients; i != sel; i = i->next)
809 if(ISVISIBLE(selmon, i))
812 for(; i; i = i->next)
813 if(ISVISIBLE(selmon, i))
823 getclient(Window w) {
826 for(c = clients; c && c->win != w; c = c->next);
831 getcolor(const char *colstr) {
832 Colormap cmap = DefaultColormap(dpy, screen);
835 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
836 die("error, cannot allocate color '%s'\n", colstr);
844 unsigned char *p = NULL;
845 unsigned long n, extra;
848 status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
849 &real, &format, &n, &extra, (unsigned char **)&p);
850 if(status != Success)
859 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
864 if(!text || size == 0)
867 XGetTextProperty(dpy, w, &name, atom);
870 if(name.encoding == XA_STRING)
871 strncpy(text, (char *)name.value, size - 1);
873 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
875 strncpy(text, *list, size - 1);
876 XFreeStringList(list);
879 text[size - 1] = '\0';
885 grabbuttons(Client *c, Bool focused) {
889 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
890 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
892 for(i = 0; i < LENGTH(buttons); i++)
893 if(buttons[i].click == ClkClientWin)
894 for(j = 0; j < LENGTH(modifiers); j++)
895 XGrabButton(dpy, buttons[i].button,
896 buttons[i].mask | modifiers[j],
897 c->win, False, BUTTONMASK,
898 GrabModeAsync, GrabModeSync, None, None);
900 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
901 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
910 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
913 XUngrabKey(dpy, AnyKey, AnyModifier, root);
914 for(i = 0; i < LENGTH(keys); i++) {
915 if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
916 for(j = 0; j < LENGTH(modifiers); j++)
917 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
918 True, GrabModeAsync, GrabModeAsync);
924 initfont(const char *fontstr) {
925 char *def, **missing;
929 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
932 fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
933 XFreeStringList(missing);
936 XFontSetExtents *font_extents;
937 XFontStruct **xfonts;
939 dc.font.ascent = dc.font.descent = 0;
940 font_extents = XExtentsOfFontSet(dc.font.set);
941 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
942 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
943 dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
944 dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
949 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
950 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
951 die("error, cannot load font: '%s'\n", fontstr);
952 dc.font.ascent = dc.font.xfont->ascent;
953 dc.font.descent = dc.font.xfont->descent;
955 dc.font.height = dc.font.ascent + dc.font.descent;
959 isprotodel(Client *c) {
964 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
965 for(i = 0; !ret && i < n; i++)
966 if(protocols[i] == wmatom[WMDelete])
974 keypress(XEvent *e) {
980 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
981 for(i = 0; i < LENGTH(keys); i++)
982 if(keysym == keys[i].keysym
983 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
985 keys[i].func(&(keys[i].arg));
989 killclient(const Arg *arg) {
994 if(isprotodel(sel)) {
995 ev.type = ClientMessage;
996 ev.xclient.window = sel->win;
997 ev.xclient.message_type = wmatom[WMProtocols];
998 ev.xclient.format = 32;
999 ev.xclient.data.l[0] = wmatom[WMDelete];
1000 ev.xclient.data.l[1] = CurrentTime;
1001 XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
1004 XKillClient(dpy, sel->win);
1008 manage(Window w, XWindowAttributes *wa) {
1010 Client *c, *t = NULL;
1011 Window trans = None;
1014 if(!(c = malloc(sizeof(Client))))
1015 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1025 c->oldbw = wa->border_width;
1026 if(c->w == sw && c->h == sh) {
1032 if(c->x + WIDTH(c) > sx + sw)
1033 c->x = sx + sw - WIDTH(c);
1034 if(c->y + HEIGHT(c) > sy + sh)
1035 c->y = sy + sh - HEIGHT(c);
1036 c->x = MAX(c->x, sx);
1037 /* only fix client y-offset, if the client center might cover the bar */
1038 c->y = MAX(c->y, ((selmon->by == 0) && (c->x + (c->w / 2) >= selmon->wx)
1039 && (c->x + (c->w / 2) < selmon->wx + selmon->ww)) ? bh : sy);
1043 wc.border_width = c->bw;
1044 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1045 XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
1046 configure(c); /* propagates border_width, if size doesn't change */
1048 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1049 grabbuttons(c, False);
1051 if(XGetTransientForHint(dpy, w, &trans))
1052 t = getclient(trans);
1058 c->isfloating = trans != None || c->isfixed;
1060 XRaiseWindow(dpy, c->win);
1063 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1064 XMapWindow(dpy, c->win);
1065 setclientstate(c, NormalState);
1070 mappingnotify(XEvent *e) {
1071 XMappingEvent *ev = &e->xmapping;
1073 XRefreshKeyboardMapping(ev);
1074 if(ev->request == MappingKeyboard)
1079 maprequest(XEvent *e) {
1080 static XWindowAttributes wa;
1081 XMapRequestEvent *ev = &e->xmaprequest;
1083 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1085 if(wa.override_redirect)
1087 if(!getclient(ev->window))
1088 manage(ev->window, &wa);
1092 monocle(Monitor *m) {
1095 for(c = nexttiled(m, clients); c; c = nexttiled(m, c->next))
1096 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw);
1100 movemouse(const Arg *arg) {
1101 int x, y, ocx, ocy, di, nx, ny;
1112 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1113 None, cursor[CurMove], CurrentTime) != GrabSuccess)
1115 XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui);
1117 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1119 case ConfigureRequest:
1122 handler[ev.type](&ev);
1125 nx = ocx + (ev.xmotion.x - x);
1126 ny = ocy + (ev.xmotion.y - y);
1127 if(snap && nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1128 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1129 if(abs(selmon->wx - nx) < snap)
1131 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1132 nx = selmon->wx + selmon->ww - WIDTH(c);
1133 if(abs(selmon->wy - ny) < snap)
1135 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1136 ny = selmon->wy + selmon->wh - HEIGHT(c);
1137 if(!c->isfloating && lt[selmon->sellt]->arrange
1138 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1139 togglefloating(NULL);
1141 if(!lt[selmon->sellt]->arrange || c->isfloating)
1142 resize(c, nx, ny, c->w, c->h);
1146 while(ev.type != ButtonRelease);
1147 XUngrabPointer(dpy, CurrentTime);
1151 nexttiled(Monitor *m, Client *c) {
1153 for(; c && (c->isfloating || !ISVISIBLE(m, c)); c = c->next);
1158 propertynotify(XEvent *e) {
1161 XPropertyEvent *ev = &e->xproperty;
1163 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1165 else if(ev->state == PropertyDelete)
1166 return; /* ignore */
1167 else if((c = getclient(ev->window))) {
1170 case XA_WM_TRANSIENT_FOR:
1171 XGetTransientForHint(dpy, c->win, &trans);
1172 if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
1175 case XA_WM_NORMAL_HINTS:
1183 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1192 quit(const Arg *arg) {
1197 resize(Client *c, int x, int y, int w, int h) {
1200 if(applysizehints(c, &x, &y, &w, &h)) {
1203 c->w = wc.width = w;
1204 c->h = wc.height = h;
1205 wc.border_width = c->bw;
1206 XConfigureWindow(dpy, c->win,
1207 CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1214 resizemouse(const Arg *arg) {
1225 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1226 None, cursor[CurResize], CurrentTime) != GrabSuccess)
1228 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1230 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1232 case ConfigureRequest:
1235 handler[ev.type](&ev);
1238 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1239 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1241 if(snap && nw >= selmon->wx && nw <= selmon->wx + selmon->ww
1242 && nh >= selmon->wy && nh <= selmon->wy + selmon->wh) {
1243 if(!c->isfloating && lt[selmon->sellt]->arrange
1244 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1245 togglefloating(NULL);
1247 if(!lt[selmon->sellt]->arrange || c->isfloating)
1248 resize(c, c->x, c->y, nw, nh);
1252 while(ev.type != ButtonRelease);
1253 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1254 XUngrabPointer(dpy, CurrentTime);
1255 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1259 restack(Monitor *m) {
1267 if(m == selmon && (sel->isfloating || !lt[m->sellt]->arrange))
1268 XRaiseWindow(dpy, sel->win);
1269 if(lt[m->sellt]->arrange) {
1270 wc.stack_mode = Below;
1271 wc.sibling = m->barwin;
1272 for(c = stack; c; c = c->snext)
1273 if(!c->isfloating && ISVISIBLE(m, c)) {
1274 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1275 wc.sibling = c->win;
1279 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1286 /* main event loop */
1288 while(running && !XNextEvent(dpy, &ev)) {
1289 if(handler[ev.type])
1290 (handler[ev.type])(&ev); /* call handler */
1296 unsigned int i, num;
1297 Window d1, d2, *wins = NULL;
1298 XWindowAttributes wa;
1300 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1301 for(i = 0; i < num; i++) {
1302 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1303 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1305 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1306 manage(wins[i], &wa);
1308 for(i = 0; i < num; i++) { /* now the transients */
1309 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1311 if(XGetTransientForHint(dpy, wins[i], &d1)
1312 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1313 manage(wins[i], &wa);
1321 setclientstate(Client *c, long state) {
1322 long data[] = {state, None};
1324 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1325 PropModeReplace, (unsigned char *)data, 2);
1329 setlayout(const Arg *arg) {
1330 if(!arg || !arg->v || arg->v != lt[selmon->sellt])
1333 lt[selmon->sellt] = (Layout *)arg->v;
1340 /* arg > 1.0 will set mfact absolutly */
1342 setmfact(const Arg *arg) {
1345 if(!arg || !lt[selmon->sellt]->arrange)
1347 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1348 if(f < 0.1 || f > 0.9)
1358 XSetWindowAttributes wa;
1361 screen = DefaultScreen(dpy);
1362 root = RootWindow(dpy, screen);
1366 sw = DisplayWidth(dpy, screen);
1367 sh = DisplayHeight(dpy, screen);
1368 bh = dc.h = dc.font.height + 2;
1369 lt[0] = &layouts[0];
1370 lt[1] = &layouts[1 % LENGTH(layouts)];
1374 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1375 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1376 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1377 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1378 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1381 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1382 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1383 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1385 /* init appearance */
1386 dc.norm[ColBorder] = getcolor(normbordercolor);
1387 dc.norm[ColBG] = getcolor(normbgcolor);
1388 dc.norm[ColFG] = getcolor(normfgcolor);
1389 dc.sel[ColBorder] = getcolor(selbordercolor);
1390 dc.sel[ColBG] = getcolor(selbgcolor);
1391 dc.sel[ColFG] = getcolor(selfgcolor);
1392 dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
1393 dc.gc = XCreateGC(dpy, root, 0, NULL);
1394 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1396 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
1399 for(blw = i = 0; LENGTH(layouts) > 1 && i < LENGTH(layouts); i++) {
1400 w = TEXTW(layouts[i].symbol);
1406 /* EWMH support per view */
1407 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1408 PropModeReplace, (unsigned char *) netatom, NetLast);
1410 /* select for events */
1411 wa.cursor = cursor[CurNormal];
1412 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
1413 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
1414 |PropertyChangeMask;
1415 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1416 XSelectInput(dpy, root, wa.event_mask);
1422 showhide(Client *c) {
1425 if(ISVISIBLE((c->m), c)) { /* show clients top down */
1426 XMoveWindow(dpy, c->win, c->x, c->y);
1427 if(!lt[c->m->sellt]->arrange || c->isfloating)
1428 resize(c, c->x, c->y, c->w, c->h);
1431 else { /* hide clients bottom up */
1433 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
1439 sigchld(int signal) {
1440 while(0 < waitpid(-1, NULL, WNOHANG));
1444 spawn(const Arg *arg) {
1445 signal(SIGCHLD, sigchld);
1448 close(ConnectionNumber(dpy));
1450 execvp(((char **)arg->v)[0], (char **)arg->v);
1451 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1458 tag(const Arg *arg) {
1459 if(sel && arg->ui & TAGMASK) {
1460 sel->tags = arg->ui & TAGMASK;
1467 tagmon(const Arg *arg) {
1471 for(i = 0, m = mons; m; m = m->next, i++)
1478 #endif /* XINERAMA */
1481 textnw(const char *text, unsigned int len) {
1485 XmbTextExtents(dc.font.set, text, len, NULL, &r);
1488 return XTextWidth(dc.font.xfont, text, len);
1497 for(n = 0, c = nexttiled(m, clients); c; c = nexttiled(m, c->next), n++);
1502 c = nexttiled(m, clients);
1503 mw = m->mfact * m->ww;
1504 resize(c, m->wx, m->wy, (n == 1 ? m->ww : mw) - 2 * c->bw, m->wh - 2 * c->bw);
1510 x = (m->wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : m->wx + mw;
1512 w = (m->wx + mw > c->x + c->w) ? m->wx + m->ww - x : m->ww - mw;
1517 for(i = 0, c = nexttiled(m, c->next); c; c = nexttiled(m, c->next), i++) {
1518 resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
1519 ? m->wy + m->wh - y - 2 * c->bw : h - 2 * c->bw));
1521 y = c->y + HEIGHT(c);
1526 togglebar(const Arg *arg) {
1527 selmon->showbar = !selmon->showbar;
1529 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1534 togglefloating(const Arg *arg) {
1537 sel->isfloating = !sel->isfloating || sel->isfixed;
1539 resize(sel, sel->x, sel->y, sel->w, sel->h);
1544 toggletag(const Arg *arg) {
1550 mask = sel->tags ^ (arg->ui & TAGMASK);
1558 toggleview(const Arg *arg) {
1559 unsigned int mask = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1562 selmon->tagset[selmon->seltags] = mask;
1568 unmanage(Client *c) {
1571 wc.border_width = c->oldbw;
1572 /* The server grab construct avoids race conditions. */
1574 XSetErrorHandler(xerrordummy);
1575 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1580 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1581 setclientstate(c, WithdrawnState);
1584 XSetErrorHandler(xerror);
1590 unmapnotify(XEvent *e) {
1592 XUnmapEvent *ev = &e->xunmap;
1594 if((c = getclient(ev->window)))
1601 XSetWindowAttributes wa;
1603 wa.override_redirect = True;
1604 wa.background_pixmap = ParentRelative;
1605 wa.event_mask = ButtonPressMask|ExposureMask;
1607 for(m = mons; m; m = m->next) {
1608 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1610 CopyFromParent, DefaultVisual(dpy, screen),
1611 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1612 XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
1613 XMapRaised(dpy, m->barwin);
1621 Monitor *newmons = NULL, *m;
1624 XineramaScreenInfo *info = NULL;
1626 if(XineramaIsActive(dpy))
1627 info = XineramaQueryScreens(dpy, &n);
1629 /* allocate monitor(s) for the new geometry setup */
1630 for(i = 0; i < n; i++) {
1631 m = (Monitor *)malloc(sizeof(Monitor));
1636 /* initialise monitor(s) */
1638 if(XineramaIsActive(dpy)) {
1639 for(i = 0, m = newmons; m; m = m->next, i++) {
1640 m->screen_number = info[i].screen_number;
1641 m->wx = info[i].x_org;
1642 m->wy = info[i].y_org;
1643 m->ww = info[i].width;
1644 m->wh = info[i].height;
1650 /* default monitor setup */
1652 m->screen_number = 0;
1659 /* bar geometry setup */
1660 for(m = newmons; m; m = m->next) {
1661 /* TODO: consider removing the following values from config.h */
1664 m->tagset[0] = m->tagset[1] = 1;
1666 m->showbar = showbar;
1670 m->by = m->topbar ? m->wy : m->wy + m->wh;
1671 m->wy = m->topbar ? m->wy + bh : m->wy;
1675 /* reassign all clients with same screen number */
1676 for(c = clients; c; c = c->next)
1677 if(c->m->screen_number == m->screen_number)
1681 /* reassign left over clients with disappeared screen number */
1682 for(c = clients; c; c = c->next)
1683 if(c->m->screen_number >= n)
1686 /* select focused monitor */
1692 if(XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui))
1693 for(m = newmons; m; m = m->next)
1694 if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh)) {
1700 /* final assignment of new monitors */
1706 updatenumlockmask(void) {
1708 XModifierKeymap *modmap;
1711 modmap = XGetModifierMapping(dpy);
1712 for(i = 0; i < 8; i++)
1713 for(j = 0; j < modmap->max_keypermod; j++)
1714 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1715 == XKeysymToKeycode(dpy, XK_Num_Lock))
1716 numlockmask = (1 << i);
1717 XFreeModifiermap(modmap);
1721 updatesizehints(Client *c) {
1725 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
1726 /* size is uninitialized, ensure that size.flags aren't used */
1728 if(size.flags & PBaseSize) {
1729 c->basew = size.base_width;
1730 c->baseh = size.base_height;
1732 else if(size.flags & PMinSize) {
1733 c->basew = size.min_width;
1734 c->baseh = size.min_height;
1737 c->basew = c->baseh = 0;
1738 if(size.flags & PResizeInc) {
1739 c->incw = size.width_inc;
1740 c->inch = size.height_inc;
1743 c->incw = c->inch = 0;
1744 if(size.flags & PMaxSize) {
1745 c->maxw = size.max_width;
1746 c->maxh = size.max_height;
1749 c->maxw = c->maxh = 0;
1750 if(size.flags & PMinSize) {
1751 c->minw = size.min_width;
1752 c->minh = size.min_height;
1754 else if(size.flags & PBaseSize) {
1755 c->minw = size.base_width;
1756 c->minh = size.base_height;
1759 c->minw = c->minh = 0;
1760 if(size.flags & PAspect) {
1761 c->mina = (float)size.min_aspect.y / (float)size.min_aspect.x;
1762 c->maxa = (float)size.max_aspect.x / (float)size.max_aspect.y;
1765 c->maxa = c->mina = 0.0;
1766 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
1767 && c->maxw == c->minw && c->maxh == c->minh);
1771 updatetitle(Client *c) {
1772 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
1773 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
1778 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
1779 strcpy(stext, "dwm-"VERSION);
1784 updatewmhints(Client *c) {
1787 if((wmh = XGetWMHints(dpy, c->win))) {
1788 if(c == sel && wmh->flags & XUrgencyHint) {
1789 wmh->flags &= ~XUrgencyHint;
1790 XSetWMHints(dpy, c->win, wmh);
1793 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
1800 view(const Arg *arg) {
1801 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
1803 selmon->seltags ^= 1; /* toggle sel tagset */
1804 if(arg->ui & TAGMASK)
1805 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
1809 /* There's no way to check accesses to destroyed windows, thus those cases are
1810 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1811 * default error handler, which may call exit. */
1813 xerror(Display *dpy, XErrorEvent *ee) {
1814 if(ee->error_code == BadWindow
1815 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
1816 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
1817 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
1818 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
1819 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
1820 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
1821 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
1822 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
1824 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
1825 ee->request_code, ee->error_code);
1826 return xerrorxlib(dpy, ee); /* may call exit */
1830 xerrordummy(Display *dpy, XErrorEvent *ee) {
1834 /* Startup Error handler to check if another window manager
1835 * is already running. */
1837 xerrorstart(Display *dpy, XErrorEvent *ee) {
1843 zoom(const Arg *arg) {
1846 if(!lt[selmon->sellt]->arrange || lt[selmon->sellt]->arrange == monocle || (sel && sel->isfloating))
1848 if(c == nexttiled(selmon, clients))
1849 if(!c || !(c = nexttiled(selmon, c->next)))
1858 main(int argc, char *argv[]) {
1859 if(argc == 2 && !strcmp("-v", argv[1]))
1860 die("dwm-"VERSION", © 2006-2009 dwm engineers, see LICENSE for details\n");
1862 die("usage: dwm [-v]\n");
1864 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
1865 fputs("warning: no locale support\n", stderr);
1867 if(!(dpy = XOpenDisplay(NULL)))
1868 die("dwm: cannot open display\n");