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,
61 NetWMFullscreen, NetLast }; /* EWMH atoms */
62 enum { WMProtocols, WMDelete, WMState, WMLast }; /* default atoms */
63 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
64 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
77 void (*func)(const Arg *arg);
81 typedef struct Monitor Monitor;
82 typedef struct Client Client;
87 int oldx, oldy, oldw, oldh;
88 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
91 Bool isfixed, isfloating, isurgent, oldstate;
100 unsigned long norm[ColLast];
101 unsigned long sel[ColLast];
111 } DC; /* draw context */
116 void (*func)(const Arg *);
122 void (*arrange)(Monitor *);
129 int by; /* bar geometry */
130 int mx, my, mw, mh; /* screen size */
131 int wx, wy, ww, wh; /* window area */
132 unsigned int seltags;
134 unsigned int tagset[2];
147 const char *instance;
154 /* function declarations */
155 static void applyrules(Client *c);
156 static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
157 static void arrange(Monitor *m);
158 static void arrangemon(Monitor *m);
159 static void attach(Client *c);
160 static void attachstack(Client *c);
161 static void buttonpress(XEvent *e);
162 static void checkotherwm(void);
163 static void cleanup(void);
164 static void cleanupmon(Monitor *mon);
165 static void clearurgent(Client *c);
166 static void clientmessage(XEvent *e);
167 static void configure(Client *c);
168 static void configurenotify(XEvent *e);
169 static void configurerequest(XEvent *e);
170 static Monitor *createmon(void);
171 static void destroynotify(XEvent *e);
172 static void detach(Client *c);
173 static void detachstack(Client *c);
174 static void die(const char *errstr, ...);
175 static Monitor *dirtomon(int dir);
176 static void drawbar(Monitor *m);
177 static void drawbars(void);
178 static void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
179 static void drawtext(const char *text, unsigned long col[ColLast], Bool invert);
180 static void enternotify(XEvent *e);
181 static void expose(XEvent *e);
182 static void focus(Client *c);
183 static void focusin(XEvent *e);
184 static void focusmon(const Arg *arg);
185 static void focusstack(const Arg *arg);
186 static unsigned long getcolor(const char *colstr);
187 static Bool getrootptr(int *x, int *y);
188 static long getstate(Window w);
189 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
190 static void grabbuttons(Client *c, Bool focused);
191 static void grabkeys(void);
192 static void initfont(const char *fontstr);
193 static Bool isprotodel(Client *c);
194 static void keypress(XEvent *e);
195 static void killclient(const Arg *arg);
196 static void manage(Window w, XWindowAttributes *wa);
197 static void mappingnotify(XEvent *e);
198 static void maprequest(XEvent *e);
199 static void monocle(Monitor *m);
200 static void movemouse(const Arg *arg);
201 static Client *nexttiled(Client *c);
202 static Monitor *ptrtomon(int x, int y);
203 static void propertynotify(XEvent *e);
204 static void quit(const Arg *arg);
205 static void resize(Client *c, int x, int y, int w, int h, Bool interact);
206 static void resizeclient(Client *c, int x, int y, int w, int h);
207 static void resizemouse(const Arg *arg);
208 static void restack(Monitor *m);
209 static void run(void);
210 static void scan(void);
211 static void sendmon(Client *c, Monitor *m);
212 static void setclientstate(Client *c, long state);
213 static void setlayout(const Arg *arg);
214 static void setmfact(const Arg *arg);
215 static void setup(void);
216 static void showhide(Client *c);
217 static void sigchld(int unused);
218 static void spawn(const Arg *arg);
219 static void tag(const Arg *arg);
220 static void tagmon(const Arg *arg);
221 static int textnw(const char *text, unsigned int len);
222 static void tile(Monitor *);
223 static void togglebar(const Arg *arg);
224 static void togglefloating(const Arg *arg);
225 static void toggletag(const Arg *arg);
226 static void toggleview(const Arg *arg);
227 static void unfocus(Client *c, Bool setfocus);
228 static void unmanage(Client *c, Bool destroyed);
229 static void unmapnotify(XEvent *e);
230 static Bool updategeom(void);
231 static void updatebarpos(Monitor *m);
232 static void updatebars(void);
233 static void updatenumlockmask(void);
234 static void updatesizehints(Client *c);
235 static void updatestatus(void);
236 static void updatetitle(Client *c);
237 static void updatewmhints(Client *c);
238 static void view(const Arg *arg);
239 static Client *wintoclient(Window w);
240 static Monitor *wintomon(Window w);
241 static int xerror(Display *dpy, XErrorEvent *ee);
242 static int xerrordummy(Display *dpy, XErrorEvent *ee);
243 static int xerrorstart(Display *dpy, XErrorEvent *ee);
244 static void zoom(const Arg *arg);
247 static const char broken[] = "broken";
248 static char stext[256];
250 static int sw, sh; /* X display screen geometry width, height */
251 static int bh, blw = 0; /* bar geometry */
252 static int (*xerrorxlib)(Display *, XErrorEvent *);
253 static unsigned int numlockmask = 0;
254 static void (*handler[LASTEvent]) (XEvent *) = {
255 [ButtonPress] = buttonpress,
256 [ClientMessage] = clientmessage,
257 [ConfigureRequest] = configurerequest,
258 [ConfigureNotify] = configurenotify,
259 [DestroyNotify] = destroynotify,
260 [EnterNotify] = enternotify,
263 [KeyPress] = keypress,
264 [MappingNotify] = mappingnotify,
265 [MapRequest] = maprequest,
266 [PropertyNotify] = propertynotify,
267 [UnmapNotify] = unmapnotify
269 static Atom wmatom[WMLast], netatom[NetLast];
271 static Bool running = True;
272 static Cursor cursor[CurLast];
275 static Monitor *mons = NULL, *selmon = NULL;
278 /* configuration, allows nested code to access above variables */
281 /* compile-time check if all tags fit into an unsigned int bit array. */
282 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
284 /* function implementations */
286 applyrules(Client *c) {
287 const char *class, *instance;
291 XClassHint ch = { 0 };
294 c->isfloating = c->tags = 0;
295 if(XGetClassHint(dpy, c->win, &ch)) {
296 class = ch.res_class ? ch.res_class : broken;
297 instance = ch.res_name ? ch.res_name : broken;
298 for(i = 0; i < LENGTH(rules); i++) {
300 if((!r->title || strstr(c->name, r->title))
301 && (!r->class || strstr(class, r->class))
302 && (!r->instance || strstr(instance, r->instance)))
304 c->isfloating = r->isfloating;
306 for(m = mons; m && m->num != r->monitor; m = m->next);
316 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
320 applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
324 /* set minimum possible */
332 if(*x + *w + 2 * c->bw < 0)
334 if(*y + *h + 2 * c->bw < 0)
338 if(*x > m->mx + m->mw)
339 *x = m->mx + m->mw - WIDTH(c);
340 if(*y > m->my + m->mh)
341 *y = m->my + m->mh - HEIGHT(c);
342 if(*x + *w + 2 * c->bw < m->mx)
344 if(*y + *h + 2 * c->bw < m->my)
351 if(resizehints || c->isfloating) {
352 /* see last two sentences in ICCCM 4.1.2.3 */
353 baseismin = c->basew == c->minw && c->baseh == c->minh;
354 if(!baseismin) { /* temporarily remove base dimensions */
358 /* adjust for aspect limits */
359 if(c->mina > 0 && c->maxa > 0) {
360 if(c->maxa < (float)*w / *h)
361 *w = *h * c->maxa + 0.5;
362 else if(c->mina < (float)*h / *w)
363 *h = *w * c->mina + 0.5;
365 if(baseismin) { /* increment calculation requires this */
369 /* adjust for increment value */
374 /* restore base dimensions */
375 *w = MAX(*w + c->basew, c->minw);
376 *h = MAX(*h + c->baseh, c->minh);
378 *w = MIN(*w, c->maxw);
380 *h = MIN(*h, c->maxh);
382 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
386 arrange(Monitor *m) {
389 else for(m = mons; m; m = m->next)
394 else for(m = mons; m; m = m->next)
399 arrangemon(Monitor *m) {
400 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
401 if(m->lt[m->sellt]->arrange)
402 m->lt[m->sellt]->arrange(m);
408 c->next = c->mon->clients;
413 attachstack(Client *c) {
414 c->snext = c->mon->stack;
419 buttonpress(XEvent *e) {
420 unsigned int i, x, click;
424 XButtonPressedEvent *ev = &e->xbutton;
427 /* focus monitor if necessary */
428 if((m = wintomon(ev->window)) && m != selmon) {
429 unfocus(selmon->sel, True);
433 if(ev->window == selmon->barwin) {
437 } while(ev->x >= x && ++i < LENGTH(tags));
438 if(i < LENGTH(tags)) {
442 else if(ev->x < x + blw)
444 else if(ev->x > selmon->wx + selmon->ww - TEXTW(stext))
445 click = ClkStatusText;
449 else if((c = wintoclient(ev->window))) {
451 click = ClkClientWin;
453 for(i = 0; i < LENGTH(buttons); i++)
454 if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
455 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
456 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
462 xerrorxlib = XSetErrorHandler(xerrorstart);
463 /* this causes an error if some other window manager is running */
464 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
467 die("dwm: another window manager is already running\n");
468 XSetErrorHandler(xerror);
475 Layout foo = { "", NULL };
479 selmon->lt[selmon->sellt] = &foo;
480 for(m = mons; m; m = m->next)
482 unmanage(m->stack, False);
484 XFreeFontSet(dpy, dc.font.set);
486 XFreeFont(dpy, dc.font.xfont);
487 XUngrabKey(dpy, AnyKey, AnyModifier, root);
488 XFreePixmap(dpy, dc.drawable);
490 XFreeCursor(dpy, cursor[CurNormal]);
491 XFreeCursor(dpy, cursor[CurResize]);
492 XFreeCursor(dpy, cursor[CurMove]);
496 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
500 cleanupmon(Monitor *mon) {
506 for(m = mons; m && m->next != mon; m = m->next);
509 XUnmapWindow(dpy, mon->barwin);
510 XDestroyWindow(dpy, mon->barwin);
515 clearurgent(Client *c) {
519 if(!(wmh = XGetWMHints(dpy, c->win)))
521 wmh->flags &= ~XUrgencyHint;
522 XSetWMHints(dpy, c->win, wmh);
527 configure(Client *c) {
530 ce.type = ConfigureNotify;
538 ce.border_width = c->bw;
540 ce.override_redirect = False;
541 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
545 configurenotify(XEvent *e) {
547 XConfigureEvent *ev = &e->xconfigure;
549 if(ev->window == root) {
554 XFreePixmap(dpy, dc.drawable);
555 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
557 for(m = mons; m; m = m->next)
558 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
565 configurerequest(XEvent *e) {
568 XConfigureRequestEvent *ev = &e->xconfigurerequest;
571 if((c = wintoclient(ev->window))) {
572 if(ev->value_mask & CWBorderWidth)
573 c->bw = ev->border_width;
574 else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
576 if(ev->value_mask & CWX)
577 c->x = m->mx + ev->x;
578 if(ev->value_mask & CWY)
579 c->y = m->my + ev->y;
580 if(ev->value_mask & CWWidth)
582 if(ev->value_mask & CWHeight)
584 if((c->x + c->w) > m->mx + m->mw && c->isfloating)
585 c->x = m->mx + (m->mw / 2 - c->w / 2); /* center in x direction */
586 if((c->y + c->h) > m->my + m->mh && c->isfloating)
587 c->y = m->my + (m->mh / 2 - c->h / 2); /* center in y direction */
588 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
591 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
599 wc.width = ev->width;
600 wc.height = ev->height;
601 wc.border_width = ev->border_width;
602 wc.sibling = ev->above;
603 wc.stack_mode = ev->detail;
604 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
613 if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
614 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
615 m->tagset[0] = m->tagset[1] = 1;
617 m->showbar = showbar;
619 m->lt[0] = &layouts[0];
620 m->lt[1] = &layouts[1 % LENGTH(layouts)];
621 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
626 destroynotify(XEvent *e) {
628 XDestroyWindowEvent *ev = &e->xdestroywindow;
630 if((c = wintoclient(ev->window)))
638 for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
643 detachstack(Client *c) {
646 for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
649 if(c == c->mon->sel) {
650 for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
656 die(const char *errstr, ...) {
659 va_start(ap, errstr);
660 vfprintf(stderr, errstr, ap);
670 if(!(m = selmon->next))
675 for(m = mons; m->next; m = m->next);
677 for(m = mons; m->next != selmon; m = m->next);
683 drawbar(Monitor *m) {
685 unsigned int i, occ = 0, urg = 0;
689 for(c = m->clients; c; c = c->next) {
695 for(i = 0; i < LENGTH(tags); i++) {
696 dc.w = TEXTW(tags[i]);
697 col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
698 drawtext(tags[i], col, urg & 1 << i);
699 drawsquare(m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
700 occ & 1 << i, urg & 1 << i, col);
703 dc.w = blw = TEXTW(m->ltsymbol);
704 drawtext(m->ltsymbol, dc.norm, False);
707 if(m == selmon) { /* status is only drawn on selected monitor */
714 drawtext(stext, dc.norm, False);
718 if((dc.w = dc.x - x) > bh) {
721 col = m == selmon ? dc.sel : dc.norm;
722 drawtext(m->sel->name, col, False);
723 drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
726 drawtext(NULL, dc.norm, False);
728 XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
736 for(m = mons; m; m = m->next)
741 drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
744 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
746 gcv.foreground = col[invert ? ColBG : ColFG];
747 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
748 x = (dc.font.ascent + dc.font.descent + 2) / 4;
752 r.width = r.height = x + 1;
753 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
756 r.width = r.height = x;
757 XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
762 drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
764 int i, x, y, h, len, olen;
765 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
767 XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
768 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
772 h = dc.font.ascent + dc.font.descent;
773 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
775 /* shorten text if necessary */
776 for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
779 memcpy(buf, text, len);
781 for(i = len; i && i > len - 3; buf[--i] = '.');
782 XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
784 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
786 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
790 enternotify(XEvent *e) {
792 XCrossingEvent *ev = &e->xcrossing;
794 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
796 if((m = wintomon(ev->window)) && m != selmon) {
797 unfocus(selmon->sel, True);
800 focus((wintoclient(ev->window)));
806 XExposeEvent *ev = &e->xexpose;
808 if(ev->count == 0 && (m = wintomon(ev->window)))
814 if(!c || !ISVISIBLE(c))
815 for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
816 /* was if(selmon->sel) */
817 if(selmon->sel && selmon->sel != c)
818 unfocus(selmon->sel, False);
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, True);
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 if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
911 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
920 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
925 if(!text || size == 0)
928 XGetTextProperty(dpy, w, &name, atom);
931 if(name.encoding == XA_STRING)
932 strncpy(text, (char *)name.value, size - 1);
934 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
935 strncpy(text, *list, size - 1);
936 XFreeStringList(list);
939 text[size - 1] = '\0';
945 grabbuttons(Client *c, Bool focused) {
949 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
950 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
952 for(i = 0; i < LENGTH(buttons); i++)
953 if(buttons[i].click == ClkClientWin)
954 for(j = 0; j < LENGTH(modifiers); j++)
955 XGrabButton(dpy, buttons[i].button,
956 buttons[i].mask | modifiers[j],
957 c->win, False, BUTTONMASK,
958 GrabModeAsync, GrabModeSync, None, None);
961 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
962 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
971 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
974 XUngrabKey(dpy, AnyKey, AnyModifier, root);
975 for(i = 0; i < LENGTH(keys); i++) {
976 if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
977 for(j = 0; j < LENGTH(modifiers); j++)
978 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
979 True, GrabModeAsync, GrabModeAsync);
985 initfont(const char *fontstr) {
986 char *def, **missing;
990 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
993 fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
994 XFreeStringList(missing);
997 XFontSetExtents *font_extents;
998 XFontStruct **xfonts;
1001 dc.font.ascent = dc.font.descent = 0;
1002 font_extents = XExtentsOfFontSet(dc.font.set);
1003 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
1004 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
1005 dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
1006 dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
1011 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
1012 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
1013 die("error, cannot load font: '%s'\n", fontstr);
1014 dc.font.ascent = dc.font.xfont->ascent;
1015 dc.font.descent = dc.font.xfont->descent;
1017 dc.font.height = dc.font.ascent + dc.font.descent;
1021 isprotodel(Client *c) {
1026 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1027 for(i = 0; !ret && i < n; i++)
1028 if(protocols[i] == wmatom[WMDelete])
1037 isuniquegeom(XineramaScreenInfo *unique, size_t len, XineramaScreenInfo *info) {
1040 for(i = 0; i < len; i++)
1041 if(unique[i].x_org == info->x_org && unique[i].y_org == info->y_org
1042 && unique[i].width == info->width && unique[i].height == info->height)
1046 #endif /* XINERAMA */
1049 keypress(XEvent *e) {
1055 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1056 for(i = 0; i < LENGTH(keys); i++)
1057 if(keysym == keys[i].keysym
1058 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1060 keys[i].func(&(keys[i].arg));
1064 killclient(const Arg *arg) {
1069 if(isprotodel(selmon->sel)) {
1070 ev.type = ClientMessage;
1071 ev.xclient.window = selmon->sel->win;
1072 ev.xclient.message_type = wmatom[WMProtocols];
1073 ev.xclient.format = 32;
1074 ev.xclient.data.l[0] = wmatom[WMDelete];
1075 ev.xclient.data.l[1] = CurrentTime;
1076 XSendEvent(dpy, selmon->sel->win, False, NoEventMask, &ev);
1080 XSetErrorHandler(xerrordummy);
1081 XSetCloseDownMode(dpy, DestroyAll);
1082 XKillClient(dpy, selmon->sel->win);
1084 XSetErrorHandler(xerror);
1090 manage(Window w, XWindowAttributes *wa) {
1092 Client *c, *t = NULL;
1093 Window trans = None;
1096 if(!(c = malloc(sizeof(Client))))
1097 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1101 if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1110 c->x = c->oldx = wa->x + c->mon->wx;
1111 c->y = c->oldy = wa->y + c->mon->wy;
1112 c->w = c->oldw = wa->width;
1113 c->h = c->oldh = wa->height;
1114 c->oldbw = wa->border_width;
1115 if(c->w == c->mon->mw && c->h == c->mon->mh) {
1116 c->isfloating = True;
1122 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1123 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1124 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1125 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1126 c->x = MAX(c->x, c->mon->mx);
1127 /* only fix client y-offset, if the client center might cover the bar */
1128 c->y = MAX(c->y, ((c->mon->by == 0) && (c->x + (c->w / 2) >= c->mon->wx)
1129 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1132 wc.border_width = c->bw;
1133 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1134 XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
1135 configure(c); /* propagates border_width, if size doesn't change */
1137 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1138 grabbuttons(c, False);
1140 c->isfloating = c->oldstate = trans != None || c->isfixed;
1142 XRaiseWindow(dpy, c->win);
1145 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1146 XMapWindow(dpy, c->win);
1147 setclientstate(c, NormalState);
1152 mappingnotify(XEvent *e) {
1153 XMappingEvent *ev = &e->xmapping;
1155 XRefreshKeyboardMapping(ev);
1156 if(ev->request == MappingKeyboard)
1161 maprequest(XEvent *e) {
1162 static XWindowAttributes wa;
1163 XMapRequestEvent *ev = &e->xmaprequest;
1165 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1167 if(wa.override_redirect)
1169 if(!wintoclient(ev->window))
1170 manage(ev->window, &wa);
1174 monocle(Monitor *m) {
1178 for(c = m->clients; c; c = c->next)
1181 if(n > 0) /* override layout symbol */
1182 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1183 for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
1184 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
1188 movemouse(const Arg *arg) {
1189 int x, y, ocx, ocy, nx, ny;
1194 if(!(c = selmon->sel))
1199 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1200 None, cursor[CurMove], CurrentTime) != GrabSuccess)
1202 if(!getrootptr(&x, &y))
1205 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1207 case ConfigureRequest:
1210 handler[ev.type](&ev);
1213 nx = ocx + (ev.xmotion.x - x);
1214 ny = ocy + (ev.xmotion.y - y);
1215 if(snap && nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1216 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1217 if(abs(selmon->wx - nx) < snap)
1219 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1220 nx = selmon->wx + selmon->ww - WIDTH(c);
1221 if(abs(selmon->wy - ny) < snap)
1223 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1224 ny = selmon->wy + selmon->wh - HEIGHT(c);
1225 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1226 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1227 togglefloating(NULL);
1229 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1230 resize(c, nx, ny, c->w, c->h, True);
1233 } while(ev.type != ButtonRelease);
1234 XUngrabPointer(dpy, CurrentTime);
1235 if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
1243 nexttiled(Client *c) {
1244 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1249 ptrtomon(int x, int y) {
1252 for(m = mons; m; m = m->next)
1253 if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh))
1259 propertynotify(XEvent *e) {
1262 XPropertyEvent *ev = &e->xproperty;
1264 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1266 else if(ev->state == PropertyDelete)
1267 return; /* ignore */
1268 else if((c = wintoclient(ev->window))) {
1271 case XA_WM_TRANSIENT_FOR:
1272 if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1273 (c->isfloating = (wintoclient(trans)) != NULL))
1276 case XA_WM_NORMAL_HINTS:
1284 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1286 if(c == c->mon->sel)
1293 clientmessage(XEvent *e) {
1294 XClientMessageEvent *cme = &e->xclient;
1297 if((c = wintoclient(cme->window))
1298 && (cme->message_type == netatom[NetWMState] && cme->data.l[1] == netatom[NetWMFullscreen]))
1300 if(cme->data.l[0]) {
1301 XChangeProperty(dpy, cme->window, netatom[NetWMState], XA_ATOM, 32,
1302 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1303 c->oldstate = c->isfloating;
1306 c->isfloating = True;
1307 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1308 XRaiseWindow(dpy, c->win);
1311 XChangeProperty(dpy, cme->window, netatom[NetWMState], XA_ATOM, 32,
1312 PropModeReplace, (unsigned char*)0, 0);
1313 c->isfloating = c->oldstate;
1319 resizeclient(c, c->x, c->y, c->w, c->h);
1326 quit(const Arg *arg) {
1331 resize(Client *c, int x, int y, int w, int h, Bool interact) {
1332 if(applysizehints(c, &x, &y, &w, &h, interact))
1333 resizeclient(c, x, y, w, h);
1337 resizeclient(Client *c, int x, int y, int w, int h) {
1340 c->oldx = c->x; c->x = wc.x = x;
1341 c->oldy = c->y; c->y = wc.y = y;
1342 c->oldw = c->w; c->w = wc.width = w;
1343 c->oldh = c->h; c->h = wc.height = h;
1344 wc.border_width = c->bw;
1345 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1351 resizemouse(const Arg *arg) {
1358 if(!(c = selmon->sel))
1363 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1364 None, cursor[CurResize], CurrentTime) != GrabSuccess)
1366 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1368 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1370 case ConfigureRequest:
1373 handler[ev.type](&ev);
1376 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1377 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1378 if(snap && nw >= selmon->wx && nw <= selmon->wx + selmon->ww
1379 && nh >= selmon->wy && nh <= selmon->wy + selmon->wh)
1381 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1382 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1383 togglefloating(NULL);
1385 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1386 resize(c, c->x, c->y, nw, nh, True);
1389 } while(ev.type != ButtonRelease);
1390 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1391 XUngrabPointer(dpy, CurrentTime);
1392 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1393 if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
1401 restack(Monitor *m) {
1409 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1410 XRaiseWindow(dpy, m->sel->win);
1411 if(m->lt[m->sellt]->arrange) {
1412 wc.stack_mode = Below;
1413 wc.sibling = m->barwin;
1414 for(c = m->stack; c; c = c->snext)
1415 if(!c->isfloating && ISVISIBLE(c)) {
1416 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1417 wc.sibling = c->win;
1421 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1427 /* main event loop */
1429 while(running && !XNextEvent(dpy, &ev)) {
1430 if(handler[ev.type])
1431 handler[ev.type](&ev); /* call handler */
1437 unsigned int i, num;
1438 Window d1, d2, *wins = NULL;
1439 XWindowAttributes wa;
1441 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1442 for(i = 0; i < num; i++) {
1443 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1444 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1446 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1447 manage(wins[i], &wa);
1449 for(i = 0; i < num; i++) { /* now the transients */
1450 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1452 if(XGetTransientForHint(dpy, wins[i], &d1)
1453 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1454 manage(wins[i], &wa);
1462 sendmon(Client *c, Monitor *m) {
1469 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1477 setclientstate(Client *c, long state) {
1478 long data[] = { state, None };
1480 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1481 PropModeReplace, (unsigned char *)data, 2);
1485 setlayout(const Arg *arg) {
1486 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1489 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1490 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1497 /* arg > 1.0 will set mfact absolutly */
1499 setmfact(const Arg *arg) {
1502 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1504 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1505 if(f < 0.1 || f > 0.9)
1513 XSetWindowAttributes wa;
1515 /* clean up any zombies immediately */
1519 screen = DefaultScreen(dpy);
1520 root = RootWindow(dpy, screen);
1522 sw = DisplayWidth(dpy, screen);
1523 sh = DisplayHeight(dpy, screen);
1524 bh = dc.h = dc.font.height + 2;
1527 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1528 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1529 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1530 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1531 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1532 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1533 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1535 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1536 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1537 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1538 /* init appearance */
1539 dc.norm[ColBorder] = getcolor(normbordercolor);
1540 dc.norm[ColBG] = getcolor(normbgcolor);
1541 dc.norm[ColFG] = getcolor(normfgcolor);
1542 dc.sel[ColBorder] = getcolor(selbordercolor);
1543 dc.sel[ColBG] = getcolor(selbgcolor);
1544 dc.sel[ColFG] = getcolor(selfgcolor);
1545 dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
1546 dc.gc = XCreateGC(dpy, root, 0, NULL);
1547 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1549 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
1553 /* EWMH support per view */
1554 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1555 PropModeReplace, (unsigned char *) netatom, NetLast);
1556 /* select for events */
1557 wa.cursor = cursor[CurNormal];
1558 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
1559 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
1560 |PropertyChangeMask;
1561 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1562 XSelectInput(dpy, root, wa.event_mask);
1567 showhide(Client *c) {
1570 if(ISVISIBLE(c)) { /* show clients top down */
1571 XMoveWindow(dpy, c->win, c->x, c->y);
1572 if(!c->mon->lt[c->mon->sellt]->arrange || c->isfloating)
1573 resize(c, c->x, c->y, c->w, c->h, False);
1576 else { /* hide clients bottom up */
1578 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
1584 sigchld(int unused) {
1585 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1586 die("Can't install SIGCHLD handler");
1587 while(0 < waitpid(-1, NULL, WNOHANG));
1591 spawn(const Arg *arg) {
1594 close(ConnectionNumber(dpy));
1596 execvp(((char **)arg->v)[0], (char **)arg->v);
1597 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1604 tag(const Arg *arg) {
1605 if(selmon->sel && arg->ui & TAGMASK) {
1606 selmon->sel->tags = arg->ui & TAGMASK;
1612 tagmon(const Arg *arg) {
1613 if(!selmon->sel || !mons->next)
1615 sendmon(selmon->sel, dirtomon(arg->i));
1619 textnw(const char *text, unsigned int len) {
1623 XmbTextExtents(dc.font.set, text, len, NULL, &r);
1626 return XTextWidth(dc.font.xfont, text, len);
1635 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1639 c = nexttiled(m->clients);
1640 mw = m->mfact * m->ww;
1641 resize(c, m->wx, m->wy, (n == 1 ? m->ww : mw) - 2 * c->bw, m->wh - 2 * c->bw, False);
1645 x = (m->wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : m->wx + mw;
1647 w = (m->wx + mw > c->x + c->w) ? m->wx + m->ww - x : m->ww - mw;
1651 for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
1652 resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
1653 ? m->wy + m->wh - y - 2 * c->bw : h - 2 * c->bw), False);
1655 y = c->y + HEIGHT(c);
1660 togglebar(const Arg *arg) {
1661 selmon->showbar = !selmon->showbar;
1662 updatebarpos(selmon);
1663 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1668 togglefloating(const Arg *arg) {
1671 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1672 if(selmon->sel->isfloating)
1673 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1674 selmon->sel->w, selmon->sel->h, False);
1679 toggletag(const Arg *arg) {
1680 unsigned int newtags;
1684 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1686 selmon->sel->tags = newtags;
1692 toggleview(const Arg *arg) {
1693 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1696 selmon->tagset[selmon->seltags] = newtagset;
1702 unfocus(Client *c, Bool setfocus) {
1705 grabbuttons(c, False);
1706 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
1708 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1712 unmanage(Client *c, Bool destroyed) {
1713 Monitor *m = c->mon;
1716 /* The server grab construct avoids race conditions. */
1720 wc.border_width = c->oldbw;
1722 XSetErrorHandler(xerrordummy);
1723 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1724 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1725 setclientstate(c, WithdrawnState);
1727 XSetErrorHandler(xerror);
1736 unmapnotify(XEvent *e) {
1738 XUnmapEvent *ev = &e->xunmap;
1740 if((c = wintoclient(ev->window)))
1747 XSetWindowAttributes wa;
1749 wa.override_redirect = True;
1750 wa.background_pixmap = ParentRelative;
1751 wa.event_mask = ButtonPressMask|ExposureMask;
1752 for(m = mons; m; m = m->next) {
1753 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1754 CopyFromParent, DefaultVisual(dpy, screen),
1755 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1756 XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
1757 XMapRaised(dpy, m->barwin);
1762 updatebarpos(Monitor *m) {
1767 m->by = m->topbar ? m->wy : m->wy + m->wh;
1768 m->wy = m->topbar ? m->wy + bh : m->wy;
1779 if(XineramaIsActive(dpy)) {
1783 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1784 XineramaScreenInfo *unique = NULL;
1786 for(n = 0, m = mons; m; m = m->next, n++);
1787 /* only consider unique geometries as separate screens */
1788 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
1789 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
1790 for(i = 0, j = 0; i < nn; i++)
1791 if(isuniquegeom(unique, j, &info[i]))
1792 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1796 for(i = 0; i < (nn - n); i++) { /* new monitors available */
1797 for(m = mons; m && m->next; m = m->next);
1799 m->next = createmon();
1803 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
1805 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1806 || unique[i].width != m->mw || unique[i].height != m->mh))
1810 m->mx = m->wx = unique[i].x_org;
1811 m->my = m->wy = unique[i].y_org;
1812 m->mw = m->ww = unique[i].width;
1813 m->mh = m->wh = unique[i].height;
1817 else { /* less monitors available nn < n */
1818 for(i = nn; i < n; i++) {
1819 for(m = mons; m && m->next; m = m->next);
1823 m->clients = c->next;
1837 #endif /* XINERAMA */
1838 /* default monitor setup */
1842 if(mons->mw != sw || mons->mh != sh) {
1844 mons->mw = mons->ww = sw;
1845 mons->mh = mons->wh = sh;
1851 selmon = wintomon(root);
1857 updatenumlockmask(void) {
1859 XModifierKeymap *modmap;
1862 modmap = XGetModifierMapping(dpy);
1863 for(i = 0; i < 8; i++)
1864 for(j = 0; j < modmap->max_keypermod; j++)
1865 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1866 == XKeysymToKeycode(dpy, XK_Num_Lock))
1867 numlockmask = (1 << i);
1868 XFreeModifiermap(modmap);
1872 updatesizehints(Client *c) {
1876 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
1877 /* size is uninitialized, ensure that size.flags aren't used */
1879 if(size.flags & PBaseSize) {
1880 c->basew = size.base_width;
1881 c->baseh = size.base_height;
1883 else if(size.flags & PMinSize) {
1884 c->basew = size.min_width;
1885 c->baseh = size.min_height;
1888 c->basew = c->baseh = 0;
1889 if(size.flags & PResizeInc) {
1890 c->incw = size.width_inc;
1891 c->inch = size.height_inc;
1894 c->incw = c->inch = 0;
1895 if(size.flags & PMaxSize) {
1896 c->maxw = size.max_width;
1897 c->maxh = size.max_height;
1900 c->maxw = c->maxh = 0;
1901 if(size.flags & PMinSize) {
1902 c->minw = size.min_width;
1903 c->minh = size.min_height;
1905 else if(size.flags & PBaseSize) {
1906 c->minw = size.base_width;
1907 c->minh = size.base_height;
1910 c->minw = c->minh = 0;
1911 if(size.flags & PAspect) {
1912 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
1913 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
1916 c->maxa = c->mina = 0.0;
1917 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
1918 && c->maxw == c->minw && c->maxh == c->minh);
1922 updatetitle(Client *c) {
1923 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
1924 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
1925 if(c->name[0] == '\0') /* hack to mark broken clients */
1926 strcpy(c->name, broken);
1930 updatestatus(void) {
1931 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
1932 strcpy(stext, "dwm-"VERSION);
1937 updatewmhints(Client *c) {
1940 if((wmh = XGetWMHints(dpy, c->win))) {
1941 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
1942 wmh->flags &= ~XUrgencyHint;
1943 XSetWMHints(dpy, c->win, wmh);
1946 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
1952 view(const Arg *arg) {
1953 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
1955 selmon->seltags ^= 1; /* toggle sel tagset */
1956 if(arg->ui & TAGMASK)
1957 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
1962 wintoclient(Window w) {
1966 for(m = mons; m; m = m->next)
1967 for(c = m->clients; c; c = c->next)
1974 wintomon(Window w) {
1979 if(w == root && getrootptr(&x, &y))
1980 return ptrtomon(x, y);
1981 for(m = mons; m; m = m->next)
1984 if((c = wintoclient(w)))
1989 /* There's no way to check accesses to destroyed windows, thus those cases are
1990 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1991 * default error handler, which may call exit. */
1993 xerror(Display *dpy, XErrorEvent *ee) {
1994 if(ee->error_code == BadWindow
1995 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
1996 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
1997 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
1998 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
1999 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2000 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2001 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2002 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2004 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2005 ee->request_code, ee->error_code);
2006 return xerrorxlib(dpy, ee); /* may call exit */
2010 xerrordummy(Display *dpy, XErrorEvent *ee) {
2014 /* Startup Error handler to check if another window manager
2015 * is already running. */
2017 xerrorstart(Display *dpy, XErrorEvent *ee) {
2023 zoom(const Arg *arg) {
2024 Client *c = selmon->sel;
2026 if(!selmon->lt[selmon->sellt]->arrange
2027 || selmon->lt[selmon->sellt]->arrange == monocle
2028 || (selmon->sel && selmon->sel->isfloating))
2030 if(c == nexttiled(selmon->clients))
2031 if(!c || !(c = nexttiled(c->next)))
2040 main(int argc, char *argv[]) {
2041 if(argc == 2 && !strcmp("-v", argv[1]))
2042 die("dwm-"VERSION", © 2006-2010 dwm engineers, see LICENSE for details\n");
2044 die("usage: dwm [-v]\n");
2045 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2046 fputs("warning: no locale support\n", stderr);
2047 if(!(dpy = XOpenDisplay(NULL)))
2048 die("dwm: cannot open display\n");