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, NetActiveWindow, NetLast }; /* EWMH atoms */
62 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, 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, neverfocus, 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 void keypress(XEvent *e);
194 static void killclient(const Arg *arg);
195 static void manage(Window w, XWindowAttributes *wa);
196 static void mappingnotify(XEvent *e);
197 static void maprequest(XEvent *e);
198 static void monocle(Monitor *m);
199 static void movemouse(const Arg *arg);
200 static Client *nexttiled(Client *c);
201 static Monitor *ptrtomon(int x, int y);
202 static void propertynotify(XEvent *e);
203 static void quit(const Arg *arg);
204 static void resize(Client *c, int x, int y, int w, int h, Bool interact);
205 static void resizeclient(Client *c, int x, int y, int w, int h);
206 static void resizemouse(const Arg *arg);
207 static void restack(Monitor *m);
208 static void run(void);
209 static void scan(void);
210 static Bool sendevent(Client *c, Atom proto);
211 static void sendmon(Client *c, Monitor *m);
212 static void setclientstate(Client *c, long state);
213 static void setfocus(Client *c);
214 static void setlayout(const Arg *arg);
215 static void setmfact(const Arg *arg);
216 static void setup(void);
217 static void showhide(Client *c);
218 static void sigchld(int unused);
219 static void spawn(const Arg *arg);
220 static void tag(const Arg *arg);
221 static void tagmon(const Arg *arg);
222 static int textnw(const char *text, unsigned int len);
223 static void tile(Monitor *);
224 static void togglebar(const Arg *arg);
225 static void togglefloating(const Arg *arg);
226 static void toggletag(const Arg *arg);
227 static void toggleview(const Arg *arg);
228 static void unfocus(Client *c, Bool setfocus);
229 static void unmanage(Client *c, Bool destroyed);
230 static void unmapnotify(XEvent *e);
231 static Bool updategeom(void);
232 static void updatebarpos(Monitor *m);
233 static void updatebars(void);
234 static void updatenumlockmask(void);
235 static void updatesizehints(Client *c);
236 static void updatestatus(void);
237 static void updatetitle(Client *c);
238 static void updatewmhints(Client *c);
239 static void view(const Arg *arg);
240 static Client *wintoclient(Window w);
241 static Monitor *wintomon(Window w);
242 static int xerror(Display *dpy, XErrorEvent *ee);
243 static int xerrordummy(Display *dpy, XErrorEvent *ee);
244 static int xerrorstart(Display *dpy, XErrorEvent *ee);
245 static void zoom(const Arg *arg);
248 static const char broken[] = "broken";
249 static char stext[256];
251 static int sw, sh; /* X display screen geometry width, height */
252 static int bh, blw = 0; /* bar geometry */
253 static int (*xerrorxlib)(Display *, XErrorEvent *);
254 static unsigned int numlockmask = 0;
255 static void (*handler[LASTEvent]) (XEvent *) = {
256 [ButtonPress] = buttonpress,
257 [ClientMessage] = clientmessage,
258 [ConfigureRequest] = configurerequest,
259 [ConfigureNotify] = configurenotify,
260 [DestroyNotify] = destroynotify,
261 [EnterNotify] = enternotify,
264 [KeyPress] = keypress,
265 [MappingNotify] = mappingnotify,
266 [MapRequest] = maprequest,
267 [PropertyNotify] = propertynotify,
268 [UnmapNotify] = unmapnotify
270 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);
461 xerrorxlib = XSetErrorHandler(xerrorstart);
462 /* this causes an error if some other window manager is running */
463 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
465 XSetErrorHandler(xerror);
472 Layout foo = { "", NULL };
476 selmon->lt[selmon->sellt] = &foo;
477 for(m = mons; m; m = m->next)
479 unmanage(m->stack, False);
481 XFreeFontSet(dpy, dc.font.set);
483 XFreeFont(dpy, dc.font.xfont);
484 XUngrabKey(dpy, AnyKey, AnyModifier, root);
485 XFreePixmap(dpy, dc.drawable);
487 XFreeCursor(dpy, cursor[CurNormal]);
488 XFreeCursor(dpy, cursor[CurResize]);
489 XFreeCursor(dpy, cursor[CurMove]);
493 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
497 cleanupmon(Monitor *mon) {
503 for(m = mons; m && m->next != mon; m = m->next);
506 XUnmapWindow(dpy, mon->barwin);
507 XDestroyWindow(dpy, mon->barwin);
512 clearurgent(Client *c) {
516 if(!(wmh = XGetWMHints(dpy, c->win)))
518 wmh->flags &= ~XUrgencyHint;
519 XSetWMHints(dpy, c->win, wmh);
524 configure(Client *c) {
527 ce.type = ConfigureNotify;
535 ce.border_width = c->bw;
537 ce.override_redirect = False;
538 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
542 configurenotify(XEvent *e) {
544 XConfigureEvent *ev = &e->xconfigure;
546 if(ev->window == root) {
551 XFreePixmap(dpy, dc.drawable);
552 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
554 for(m = mons; m; m = m->next)
555 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
562 configurerequest(XEvent *e) {
565 XConfigureRequestEvent *ev = &e->xconfigurerequest;
568 if((c = wintoclient(ev->window))) {
569 if(ev->value_mask & CWBorderWidth)
570 c->bw = ev->border_width;
571 else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
573 if(ev->value_mask & CWX)
574 c->x = m->mx + ev->x;
575 if(ev->value_mask & CWY)
576 c->y = m->my + ev->y;
577 if(ev->value_mask & CWWidth)
579 if(ev->value_mask & CWHeight)
581 if((c->x + c->w) > m->mx + m->mw && c->isfloating)
582 c->x = m->mx + (m->mw / 2 - c->w / 2); /* center in x direction */
583 if((c->y + c->h) > m->my + m->mh && c->isfloating)
584 c->y = m->my + (m->mh / 2 - c->h / 2); /* center in y direction */
585 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
588 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
596 wc.width = ev->width;
597 wc.height = ev->height;
598 wc.border_width = ev->border_width;
599 wc.sibling = ev->above;
600 wc.stack_mode = ev->detail;
601 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
610 if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
611 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
612 m->tagset[0] = m->tagset[1] = 1;
614 m->showbar = showbar;
616 m->lt[0] = &layouts[0];
617 m->lt[1] = &layouts[1 % LENGTH(layouts)];
618 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
623 destroynotify(XEvent *e) {
625 XDestroyWindowEvent *ev = &e->xdestroywindow;
627 if((c = wintoclient(ev->window)))
635 for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
640 detachstack(Client *c) {
643 for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
646 if(c == c->mon->sel) {
647 for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
653 die(const char *errstr, ...) {
656 va_start(ap, errstr);
657 vfprintf(stderr, errstr, ap);
667 if(!(m = selmon->next))
672 for(m = mons; m->next; m = m->next);
674 for(m = mons; m->next != selmon; m = m->next);
680 drawbar(Monitor *m) {
682 unsigned int i, occ = 0, urg = 0;
686 for(c = m->clients; c; c = c->next) {
692 for(i = 0; i < LENGTH(tags); i++) {
693 dc.w = TEXTW(tags[i]);
694 col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
695 drawtext(tags[i], col, urg & 1 << i);
696 drawsquare(m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
697 occ & 1 << i, urg & 1 << i, col);
700 dc.w = blw = TEXTW(m->ltsymbol);
701 drawtext(m->ltsymbol, dc.norm, False);
704 if(m == selmon) { /* status is only drawn on selected monitor */
711 drawtext(stext, dc.norm, False);
715 if((dc.w = dc.x - x) > bh) {
718 col = m == selmon ? dc.sel : dc.norm;
719 drawtext(m->sel->name, col, False);
720 drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
723 drawtext(NULL, dc.norm, False);
725 XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
733 for(m = mons; m; m = m->next)
738 drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
741 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
743 gcv.foreground = col[invert ? ColBG : ColFG];
744 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
745 x = (dc.font.ascent + dc.font.descent + 2) / 4;
749 r.width = r.height = x + 1;
750 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
753 r.width = r.height = x;
754 XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
759 drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
761 int i, x, y, h, len, olen;
762 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
764 XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
765 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
769 h = dc.font.ascent + dc.font.descent;
770 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
772 /* shorten text if necessary */
773 for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
776 memcpy(buf, text, len);
778 for(i = len; i && i > len - 3; buf[--i] = '.');
779 XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
781 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
783 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
787 enternotify(XEvent *e) {
789 XCrossingEvent *ev = &e->xcrossing;
791 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
793 if((m = wintomon(ev->window)) && m != selmon) {
794 unfocus(selmon->sel, True);
797 focus((wintoclient(ev->window)));
803 XExposeEvent *ev = &e->xexpose;
805 if(ev->count == 0 && (m = wintomon(ev->window)))
811 if(!c || !ISVISIBLE(c))
812 for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
813 /* was if(selmon->sel) */
814 if(selmon->sel && selmon->sel != c)
815 unfocus(selmon->sel, False);
823 grabbuttons(c, True);
824 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
828 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
834 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
835 XFocusChangeEvent *ev = &e->xfocus;
837 if(selmon->sel && ev->window != selmon->sel->win)
838 setfocus(selmon->sel);
842 focusmon(const Arg *arg) {
847 if((m = dirtomon(arg->i)) == selmon)
849 unfocus(selmon->sel, True);
855 focusstack(const Arg *arg) {
856 Client *c = NULL, *i;
861 for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
863 for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
866 for(i = selmon->clients; i != selmon->sel; i = i->next)
870 for(; i; i = i->next)
881 getcolor(const char *colstr) {
882 Colormap cmap = DefaultColormap(dpy, screen);
885 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
886 die("error, cannot allocate color '%s'\n", colstr);
891 getrootptr(int *x, int *y) {
896 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
903 unsigned char *p = NULL;
904 unsigned long n, extra;
907 if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
908 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
917 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
922 if(!text || size == 0)
925 XGetTextProperty(dpy, w, &name, atom);
928 if(name.encoding == XA_STRING)
929 strncpy(text, (char *)name.value, size - 1);
931 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
932 strncpy(text, *list, size - 1);
933 XFreeStringList(list);
936 text[size - 1] = '\0';
942 grabbuttons(Client *c, Bool focused) {
946 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
947 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
949 for(i = 0; i < LENGTH(buttons); i++)
950 if(buttons[i].click == ClkClientWin)
951 for(j = 0; j < LENGTH(modifiers); j++)
952 XGrabButton(dpy, buttons[i].button,
953 buttons[i].mask | modifiers[j],
954 c->win, False, BUTTONMASK,
955 GrabModeAsync, GrabModeSync, None, None);
958 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
959 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
968 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
971 XUngrabKey(dpy, AnyKey, AnyModifier, root);
972 for(i = 0; i < LENGTH(keys); i++) {
973 if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
974 for(j = 0; j < LENGTH(modifiers); j++)
975 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
976 True, GrabModeAsync, GrabModeAsync);
982 initfont(const char *fontstr) {
983 char *def, **missing;
987 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
990 fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
991 XFreeStringList(missing);
994 XFontStruct **xfonts;
997 dc.font.ascent = dc.font.descent = 0;
998 XExtentsOfFontSet(dc.font.set);
999 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
1001 dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
1002 dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
1007 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
1008 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
1009 die("error, cannot load font: '%s'\n", fontstr);
1010 dc.font.ascent = dc.font.xfont->ascent;
1011 dc.font.descent = dc.font.xfont->descent;
1013 dc.font.height = dc.font.ascent + dc.font.descent;
1018 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
1020 if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1021 && unique[n].width == info->width && unique[n].height == info->height)
1025 #endif /* XINERAMA */
1028 keypress(XEvent *e) {
1034 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1035 for(i = 0; i < LENGTH(keys); i++)
1036 if(keysym == keys[i].keysym
1037 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1039 keys[i].func(&(keys[i].arg));
1043 killclient(const Arg *arg) {
1046 if(!sendevent(selmon->sel, wmatom[WMDelete])) {
1048 XSetErrorHandler(xerrordummy);
1049 XSetCloseDownMode(dpy, DestroyAll);
1050 XKillClient(dpy, selmon->sel->win);
1052 XSetErrorHandler(xerror);
1058 manage(Window w, XWindowAttributes *wa) {
1059 Client *c, *t = NULL;
1060 Window trans = None;
1063 if(!(c = calloc(1, sizeof(Client))))
1064 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1067 if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1076 c->x = c->oldx = wa->x + c->mon->wx;
1077 c->y = c->oldy = wa->y + c->mon->wy;
1078 c->w = c->oldw = wa->width;
1079 c->h = c->oldh = wa->height;
1080 c->oldbw = wa->border_width;
1081 if(c->w == c->mon->mw && c->h == c->mon->mh) {
1082 c->isfloating = True;
1088 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1089 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1090 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1091 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1092 c->x = MAX(c->x, c->mon->mx);
1093 /* only fix client y-offset, if the client center might cover the bar */
1094 c->y = MAX(c->y, ((c->mon->by == 0) && (c->x + (c->w / 2) >= c->mon->wx)
1095 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1098 wc.border_width = c->bw;
1099 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1100 XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
1101 configure(c); /* propagates border_width, if size doesn't change */
1104 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1105 grabbuttons(c, False);
1107 c->isfloating = c->oldstate = trans != None || c->isfixed;
1109 XRaiseWindow(dpy, c->win);
1112 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1113 XMapWindow(dpy, c->win);
1114 setclientstate(c, NormalState);
1119 mappingnotify(XEvent *e) {
1120 XMappingEvent *ev = &e->xmapping;
1122 XRefreshKeyboardMapping(ev);
1123 if(ev->request == MappingKeyboard)
1128 maprequest(XEvent *e) {
1129 static XWindowAttributes wa;
1130 XMapRequestEvent *ev = &e->xmaprequest;
1132 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1134 if(wa.override_redirect)
1136 if(!wintoclient(ev->window))
1137 manage(ev->window, &wa);
1141 monocle(Monitor *m) {
1145 for(c = m->clients; c; c = c->next)
1148 if(n > 0) /* override layout symbol */
1149 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1150 for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
1151 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
1155 movemouse(const Arg *arg) {
1156 int x, y, ocx, ocy, nx, ny;
1161 if(!(c = selmon->sel))
1166 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1167 None, cursor[CurMove], CurrentTime) != GrabSuccess)
1169 if(!getrootptr(&x, &y))
1172 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1174 case ConfigureRequest:
1177 handler[ev.type](&ev);
1180 nx = ocx + (ev.xmotion.x - x);
1181 ny = ocy + (ev.xmotion.y - y);
1182 if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1183 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1184 if(abs(selmon->wx - nx) < snap)
1186 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1187 nx = selmon->wx + selmon->ww - WIDTH(c);
1188 if(abs(selmon->wy - ny) < snap)
1190 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1191 ny = selmon->wy + selmon->wh - HEIGHT(c);
1192 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1193 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1194 togglefloating(NULL);
1196 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1197 resize(c, nx, ny, c->w, c->h, True);
1200 } while(ev.type != ButtonRelease);
1201 XUngrabPointer(dpy, CurrentTime);
1202 if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
1210 nexttiled(Client *c) {
1211 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1216 ptrtomon(int x, int y) {
1219 for(m = mons; m; m = m->next)
1220 if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh))
1226 propertynotify(XEvent *e) {
1229 XPropertyEvent *ev = &e->xproperty;
1231 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1233 else if(ev->state == PropertyDelete)
1234 return; /* ignore */
1235 else if((c = wintoclient(ev->window))) {
1238 case XA_WM_TRANSIENT_FOR:
1239 if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1240 (c->isfloating = (wintoclient(trans)) != NULL))
1243 case XA_WM_NORMAL_HINTS:
1251 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1253 if(c == c->mon->sel)
1260 clientmessage(XEvent *e) {
1261 XClientMessageEvent *cme = &e->xclient;
1262 Client *c = wintoclient(cme->window);
1266 if(cme->message_type == netatom[NetWMState] && cme->data.l[1] == netatom[NetWMFullscreen]) {
1267 if(cme->data.l[0]) {
1268 XChangeProperty(dpy, cme->window, netatom[NetWMState], XA_ATOM, 32,
1269 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1270 c->oldstate = c->isfloating;
1273 c->isfloating = True;
1274 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1275 XRaiseWindow(dpy, c->win);
1278 XChangeProperty(dpy, cme->window, netatom[NetWMState], XA_ATOM, 32,
1279 PropModeReplace, (unsigned char*)0, 0);
1280 c->isfloating = c->oldstate;
1286 resizeclient(c, c->x, c->y, c->w, c->h);
1290 else if(cme->message_type == netatom[NetActiveWindow]) {
1292 Arg a = { .ui = c->tags };
1303 quit(const Arg *arg) {
1308 resize(Client *c, int x, int y, int w, int h, Bool interact) {
1309 if(applysizehints(c, &x, &y, &w, &h, interact))
1310 resizeclient(c, x, y, w, h);
1314 resizeclient(Client *c, int x, int y, int w, int h) {
1317 c->oldx = c->x; c->x = wc.x = x;
1318 c->oldy = c->y; c->y = wc.y = y;
1319 c->oldw = c->w; c->w = wc.width = w;
1320 c->oldh = c->h; c->h = wc.height = h;
1321 wc.border_width = c->bw;
1322 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1328 resizemouse(const Arg *arg) {
1335 if(!(c = selmon->sel))
1340 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1341 None, cursor[CurResize], CurrentTime) != GrabSuccess)
1343 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1345 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1347 case ConfigureRequest:
1350 handler[ev.type](&ev);
1353 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1354 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1355 if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1356 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1358 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1359 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1360 togglefloating(NULL);
1362 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1363 resize(c, c->x, c->y, nw, nh, True);
1366 } while(ev.type != ButtonRelease);
1367 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1368 XUngrabPointer(dpy, CurrentTime);
1369 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1370 if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
1378 restack(Monitor *m) {
1386 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1387 XRaiseWindow(dpy, m->sel->win);
1388 if(m->lt[m->sellt]->arrange) {
1389 wc.stack_mode = Below;
1390 wc.sibling = m->barwin;
1391 for(c = m->stack; c; c = c->snext)
1392 if(!c->isfloating && ISVISIBLE(c)) {
1393 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1394 wc.sibling = c->win;
1398 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1404 /* main event loop */
1406 while(running && !XNextEvent(dpy, &ev)) {
1407 if(handler[ev.type])
1408 handler[ev.type](&ev); /* call handler */
1414 unsigned int i, num;
1415 Window d1, d2, *wins = NULL;
1416 XWindowAttributes wa;
1418 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1419 for(i = 0; i < num; i++) {
1420 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1421 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1423 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1424 manage(wins[i], &wa);
1426 for(i = 0; i < num; i++) { /* now the transients */
1427 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1429 if(XGetTransientForHint(dpy, wins[i], &d1)
1430 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1431 manage(wins[i], &wa);
1439 sendmon(Client *c, Monitor *m) {
1446 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1454 setclientstate(Client *c, long state) {
1455 long data[] = { state, None };
1457 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1458 PropModeReplace, (unsigned char *)data, 2);
1462 sendevent(Client *c, Atom proto) {
1465 Bool exists = False;
1468 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1469 while(!exists && n--)
1470 exists = protocols[n] == proto;
1474 ev.type = ClientMessage;
1475 ev.xclient.window = c->win;
1476 ev.xclient.message_type = wmatom[WMProtocols];
1477 ev.xclient.format = 32;
1478 ev.xclient.data.l[0] = proto;
1479 ev.xclient.data.l[1] = CurrentTime;
1480 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1486 setfocus(Client *c) {
1488 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1489 sendevent(c, wmatom[WMTakeFocus]);
1493 setlayout(const Arg *arg) {
1494 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1497 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1498 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1505 /* arg > 1.0 will set mfact absolutly */
1507 setmfact(const Arg *arg) {
1510 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1512 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1513 if(f < 0.1 || f > 0.9)
1521 XSetWindowAttributes wa;
1523 /* clean up any zombies immediately */
1527 screen = DefaultScreen(dpy);
1528 root = RootWindow(dpy, screen);
1530 sw = DisplayWidth(dpy, screen);
1531 sh = DisplayHeight(dpy, screen);
1532 bh = dc.h = dc.font.height + 2;
1535 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1536 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1537 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1538 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1539 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1540 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1541 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1542 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1543 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1545 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1546 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1547 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1548 /* init appearance */
1549 dc.norm[ColBorder] = getcolor(normbordercolor);
1550 dc.norm[ColBG] = getcolor(normbgcolor);
1551 dc.norm[ColFG] = getcolor(normfgcolor);
1552 dc.sel[ColBorder] = getcolor(selbordercolor);
1553 dc.sel[ColBG] = getcolor(selbgcolor);
1554 dc.sel[ColFG] = getcolor(selfgcolor);
1555 dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
1556 dc.gc = XCreateGC(dpy, root, 0, NULL);
1557 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1559 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
1563 /* EWMH support per view */
1564 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1565 PropModeReplace, (unsigned char *) netatom, NetLast);
1566 /* select for events */
1567 wa.cursor = cursor[CurNormal];
1568 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
1569 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
1570 |PropertyChangeMask;
1571 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1572 XSelectInput(dpy, root, wa.event_mask);
1577 showhide(Client *c) {
1580 if(ISVISIBLE(c)) { /* show clients top down */
1581 XMoveWindow(dpy, c->win, c->x, c->y);
1582 if(!c->mon->lt[c->mon->sellt]->arrange || c->isfloating)
1583 resize(c, c->x, c->y, c->w, c->h, False);
1586 else { /* hide clients bottom up */
1588 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
1593 sigchld(int unused) {
1594 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1595 die("Can't install SIGCHLD handler");
1596 while(0 < waitpid(-1, NULL, WNOHANG));
1600 spawn(const Arg *arg) {
1603 close(ConnectionNumber(dpy));
1605 execvp(((char **)arg->v)[0], (char **)arg->v);
1606 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1613 tag(const Arg *arg) {
1614 if(selmon->sel && arg->ui & TAGMASK) {
1615 selmon->sel->tags = arg->ui & TAGMASK;
1621 tagmon(const Arg *arg) {
1622 if(!selmon->sel || !mons->next)
1624 sendmon(selmon->sel, dirtomon(arg->i));
1628 textnw(const char *text, unsigned int len) {
1632 XmbTextExtents(dc.font.set, text, len, NULL, &r);
1635 return XTextWidth(dc.font.xfont, text, len);
1644 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1648 c = nexttiled(m->clients);
1649 mw = m->mfact * m->ww;
1650 resize(c, m->wx, m->wy, (n == 1 ? m->ww : mw) - 2 * c->bw, m->wh - 2 * c->bw, False);
1654 x = (m->wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : m->wx + mw;
1656 w = (m->wx + mw > c->x + c->w) ? m->wx + m->ww - x : m->ww - mw;
1660 for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
1661 resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
1662 ? m->wy + m->wh - y - 2 * c->bw : h - 2 * c->bw), False);
1664 y = c->y + HEIGHT(c);
1669 togglebar(const Arg *arg) {
1670 selmon->showbar = !selmon->showbar;
1671 updatebarpos(selmon);
1672 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1677 togglefloating(const Arg *arg) {
1680 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1681 if(selmon->sel->isfloating)
1682 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1683 selmon->sel->w, selmon->sel->h, False);
1688 toggletag(const Arg *arg) {
1689 unsigned int newtags;
1693 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1695 selmon->sel->tags = newtags;
1701 toggleview(const Arg *arg) {
1702 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1705 selmon->tagset[selmon->seltags] = newtagset;
1711 unfocus(Client *c, Bool setfocus) {
1714 grabbuttons(c, False);
1715 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
1717 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1721 unmanage(Client *c, Bool destroyed) {
1722 Monitor *m = c->mon;
1725 /* The server grab construct avoids race conditions. */
1729 wc.border_width = c->oldbw;
1731 XSetErrorHandler(xerrordummy);
1732 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1733 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1734 setclientstate(c, WithdrawnState);
1736 XSetErrorHandler(xerror);
1745 unmapnotify(XEvent *e) {
1747 XUnmapEvent *ev = &e->xunmap;
1749 if((c = wintoclient(ev->window)))
1756 XSetWindowAttributes wa;
1758 wa.override_redirect = True;
1759 wa.background_pixmap = ParentRelative;
1760 wa.event_mask = ButtonPressMask|ExposureMask;
1761 for(m = mons; m; m = m->next) {
1762 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1763 CopyFromParent, DefaultVisual(dpy, screen),
1764 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1765 XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
1766 XMapRaised(dpy, m->barwin);
1771 updatebarpos(Monitor *m) {
1776 m->by = m->topbar ? m->wy : m->wy + m->wh;
1777 m->wy = m->topbar ? m->wy + bh : m->wy;
1788 if(XineramaIsActive(dpy)) {
1792 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1793 XineramaScreenInfo *unique = NULL;
1795 for(n = 0, m = mons; m; m = m->next, n++);
1796 /* only consider unique geometries as separate screens */
1797 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
1798 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
1799 for(i = 0, j = 0; i < nn; i++)
1800 if(isuniquegeom(unique, j, &info[i]))
1801 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1805 for(i = 0; i < (nn - n); i++) { /* new monitors available */
1806 for(m = mons; m && m->next; m = m->next);
1808 m->next = createmon();
1812 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
1814 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1815 || unique[i].width != m->mw || unique[i].height != m->mh))
1819 m->mx = m->wx = unique[i].x_org;
1820 m->my = m->wy = unique[i].y_org;
1821 m->mw = m->ww = unique[i].width;
1822 m->mh = m->wh = unique[i].height;
1826 else { /* less monitors available nn < n */
1827 for(i = nn; i < n; i++) {
1828 for(m = mons; m && m->next; m = m->next);
1832 m->clients = c->next;
1846 #endif /* XINERAMA */
1847 /* default monitor setup */
1851 if(mons->mw != sw || mons->mh != sh) {
1853 mons->mw = mons->ww = sw;
1854 mons->mh = mons->wh = sh;
1860 selmon = wintomon(root);
1866 updatenumlockmask(void) {
1868 XModifierKeymap *modmap;
1871 modmap = XGetModifierMapping(dpy);
1872 for(i = 0; i < 8; i++)
1873 for(j = 0; j < modmap->max_keypermod; j++)
1874 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1875 == XKeysymToKeycode(dpy, XK_Num_Lock))
1876 numlockmask = (1 << i);
1877 XFreeModifiermap(modmap);
1881 updatesizehints(Client *c) {
1885 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
1886 /* size is uninitialized, ensure that size.flags aren't used */
1888 if(size.flags & PBaseSize) {
1889 c->basew = size.base_width;
1890 c->baseh = size.base_height;
1892 else if(size.flags & PMinSize) {
1893 c->basew = size.min_width;
1894 c->baseh = size.min_height;
1897 c->basew = c->baseh = 0;
1898 if(size.flags & PResizeInc) {
1899 c->incw = size.width_inc;
1900 c->inch = size.height_inc;
1903 c->incw = c->inch = 0;
1904 if(size.flags & PMaxSize) {
1905 c->maxw = size.max_width;
1906 c->maxh = size.max_height;
1909 c->maxw = c->maxh = 0;
1910 if(size.flags & PMinSize) {
1911 c->minw = size.min_width;
1912 c->minh = size.min_height;
1914 else if(size.flags & PBaseSize) {
1915 c->minw = size.base_width;
1916 c->minh = size.base_height;
1919 c->minw = c->minh = 0;
1920 if(size.flags & PAspect) {
1921 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
1922 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
1925 c->maxa = c->mina = 0.0;
1926 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
1927 && c->maxw == c->minw && c->maxh == c->minh);
1931 updatetitle(Client *c) {
1932 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
1933 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
1934 if(c->name[0] == '\0') /* hack to mark broken clients */
1935 strcpy(c->name, broken);
1939 updatestatus(void) {
1940 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
1941 strcpy(stext, "dwm-"VERSION);
1946 updatewmhints(Client *c) {
1949 if((wmh = XGetWMHints(dpy, c->win))) {
1950 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
1951 wmh->flags &= ~XUrgencyHint;
1952 XSetWMHints(dpy, c->win, wmh);
1955 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
1956 if(wmh->flags & InputHint)
1957 c->neverfocus = !wmh->input;
1959 c->neverfocus = False;
1965 view(const Arg *arg) {
1966 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
1968 selmon->seltags ^= 1; /* toggle sel tagset */
1969 if(arg->ui & TAGMASK)
1970 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
1975 wintoclient(Window w) {
1979 for(m = mons; m; m = m->next)
1980 for(c = m->clients; c; c = c->next)
1987 wintomon(Window w) {
1992 if(w == root && getrootptr(&x, &y))
1993 return ptrtomon(x, y);
1994 for(m = mons; m; m = m->next)
1997 if((c = wintoclient(w)))
2002 /* There's no way to check accesses to destroyed windows, thus those cases are
2003 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2004 * default error handler, which may call exit. */
2006 xerror(Display *dpy, XErrorEvent *ee) {
2007 if(ee->error_code == BadWindow
2008 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2009 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2010 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2011 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2012 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2013 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2014 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2015 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2017 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2018 ee->request_code, ee->error_code);
2019 return xerrorxlib(dpy, ee); /* may call exit */
2023 xerrordummy(Display *dpy, XErrorEvent *ee) {
2027 /* Startup Error handler to check if another window manager
2028 * is already running. */
2030 xerrorstart(Display *dpy, XErrorEvent *ee) {
2031 die("dwm: another window manager is already running\n");
2036 zoom(const Arg *arg) {
2037 Client *c = selmon->sel;
2039 if(!selmon->lt[selmon->sellt]->arrange
2040 || selmon->lt[selmon->sellt]->arrange == monocle
2041 || (selmon->sel && selmon->sel->isfloating))
2043 if(c == nexttiled(selmon->clients))
2044 if(!c || !(c = nexttiled(c->next)))
2053 main(int argc, char *argv[]) {
2054 if(argc == 2 && !strcmp("-v", argv[1]))
2055 die("dwm-"VERSION", © 2006-2010 dwm engineers, see LICENSE for details\n");
2057 die("usage: dwm [-v]\n");
2058 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2059 fputs("warning: no locale support\n", stderr);
2060 if(!(dpy = XOpenDisplay(NULL)))
2061 die("dwm: cannot open display\n");