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) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
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, isfullscreen;
100 unsigned long norm[ColLast];
101 unsigned long sel[ColLast];
111 } DC; /* draw context */
116 void (*func)(const Arg *);
122 void (*arrange)(Monitor *);
130 int by; /* bar geometry */
131 int mx, my, mw, mh; /* screen size */
132 int wx, wy, ww, wh; /* window area */
133 unsigned int seltags;
135 unsigned int tagset[2];
148 const char *instance;
155 /* function declarations */
156 static void applyrules(Client *c);
157 static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
158 static void arrange(Monitor *m);
159 static void arrangemon(Monitor *m);
160 static void attach(Client *c);
161 static void attachstack(Client *c);
162 static void buttonpress(XEvent *e);
163 static void checkotherwm(void);
164 static void cleanup(void);
165 static void cleanupmon(Monitor *mon);
166 static void clearurgent(Client *c);
167 static void clientmessage(XEvent *e);
168 static void configure(Client *c);
169 static void configurenotify(XEvent *e);
170 static void configurerequest(XEvent *e);
171 static Monitor *createmon(void);
172 static void destroynotify(XEvent *e);
173 static void detach(Client *c);
174 static void detachstack(Client *c);
175 static void die(const char *errstr, ...);
176 static Monitor *dirtomon(int dir);
177 static void drawbar(Monitor *m);
178 static void drawbars(void);
179 static void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
180 static void drawtext(const char *text, unsigned long col[ColLast], Bool invert);
181 static void enternotify(XEvent *e);
182 static void expose(XEvent *e);
183 static void focus(Client *c);
184 static void focusin(XEvent *e);
185 static void focusmon(const Arg *arg);
186 static void focusstack(const Arg *arg);
187 static unsigned long getcolor(const char *colstr);
188 static Bool getrootptr(int *x, int *y);
189 static long getstate(Window w);
190 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
191 static void grabbuttons(Client *c, Bool focused);
192 static void grabkeys(void);
193 static void incnmaster(const Arg *arg);
194 static void initfont(const char *fontstr);
195 static void keypress(XEvent *e);
196 static void killclient(const Arg *arg);
197 static void manage(Window w, XWindowAttributes *wa);
198 static void mappingnotify(XEvent *e);
199 static void maprequest(XEvent *e);
200 static void monocle(Monitor *m);
201 static void movemouse(const Arg *arg);
202 static Client *nexttiled(Client *c);
203 static void pop(Client *);
204 static void propertynotify(XEvent *e);
205 static Monitor *ptrtomon(int x, int y);
206 static void quit(const Arg *arg);
207 static void resize(Client *c, int x, int y, int w, int h, Bool interact);
208 static void resizeclient(Client *c, int x, int y, int w, int h);
209 static void resizemouse(const Arg *arg);
210 static void restack(Monitor *m);
211 static void run(void);
212 static void scan(void);
213 static Bool sendevent(Client *c, Atom proto);
214 static void sendmon(Client *c, Monitor *m);
215 static void setclientstate(Client *c, long state);
216 static void setfocus(Client *c);
217 static void setlayout(const Arg *arg);
218 static void setmfact(const Arg *arg);
219 static void setup(void);
220 static void showhide(Client *c);
221 static void sigchld(int unused);
222 static void spawn(const Arg *arg);
223 static void tag(const Arg *arg);
224 static void tagmon(const Arg *arg);
225 static int textnw(const char *text, unsigned int len);
226 static void tile(Monitor *);
227 static void togglebar(const Arg *arg);
228 static void togglefloating(const Arg *arg);
229 static void toggletag(const Arg *arg);
230 static void toggleview(const Arg *arg);
231 static void unfocus(Client *c, Bool setfocus);
232 static void unmanage(Client *c, Bool destroyed);
233 static void unmapnotify(XEvent *e);
234 static Bool updategeom(void);
235 static void updatebarpos(Monitor *m);
236 static void updatebars(void);
237 static void updatenumlockmask(void);
238 static void updatesizehints(Client *c);
239 static void updatestatus(void);
240 static void updatetitle(Client *c);
241 static void updatewmhints(Client *c);
242 static void view(const Arg *arg);
243 static Client *wintoclient(Window w);
244 static Monitor *wintomon(Window w);
245 static int xerror(Display *dpy, XErrorEvent *ee);
246 static int xerrordummy(Display *dpy, XErrorEvent *ee);
247 static int xerrorstart(Display *dpy, XErrorEvent *ee);
248 static void zoom(const Arg *arg);
251 static const char broken[] = "broken";
252 static char stext[256];
254 static int sw, sh; /* X display screen geometry width, height */
255 static int bh, blw = 0; /* bar geometry */
256 static int (*xerrorxlib)(Display *, XErrorEvent *);
257 static unsigned int numlockmask = 0;
258 static void (*handler[LASTEvent]) (XEvent *) = {
259 [ButtonPress] = buttonpress,
260 [ClientMessage] = clientmessage,
261 [ConfigureRequest] = configurerequest,
262 [ConfigureNotify] = configurenotify,
263 [DestroyNotify] = destroynotify,
264 [EnterNotify] = enternotify,
267 [KeyPress] = keypress,
268 [MappingNotify] = mappingnotify,
269 [MapRequest] = maprequest,
270 [PropertyNotify] = propertynotify,
271 [UnmapNotify] = unmapnotify
273 static Atom wmatom[WMLast], netatom[NetLast];
274 static Bool running = True;
275 static Cursor cursor[CurLast];
278 static Monitor *mons = NULL, *selmon = NULL;
281 /* configuration, allows nested code to access above variables */
284 /* compile-time check if all tags fit into an unsigned int bit array. */
285 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
287 /* function implementations */
289 applyrules(Client *c) {
290 const char *class, *instance;
294 XClassHint ch = { NULL, NULL };
297 c->isfloating = c->tags = 0;
298 XGetClassHint(dpy, c->win, &ch);
299 class = ch.res_class ? ch.res_class : broken;
300 instance = ch.res_name ? ch.res_name : broken;
302 for(i = 0; i < LENGTH(rules); i++) {
304 if((!r->title || strstr(c->name, r->title))
305 && (!r->class || strstr(class, r->class))
306 && (!r->instance || strstr(instance, r->instance)))
308 c->isfloating = r->isfloating;
310 for(m = mons; m && m->num != r->monitor; m = m->next);
319 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
323 applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
327 /* set minimum possible */
335 if(*x + *w + 2 * c->bw < 0)
337 if(*y + *h + 2 * c->bw < 0)
341 if(*x > m->mx + m->mw)
342 *x = m->mx + m->mw - WIDTH(c);
343 if(*y > m->my + m->mh)
344 *y = m->my + m->mh - HEIGHT(c);
345 if(*x + *w + 2 * c->bw < m->mx)
347 if(*y + *h + 2 * c->bw < m->my)
354 if(resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
355 /* see last two sentences in ICCCM 4.1.2.3 */
356 baseismin = c->basew == c->minw && c->baseh == c->minh;
357 if(!baseismin) { /* temporarily remove base dimensions */
361 /* adjust for aspect limits */
362 if(c->mina > 0 && c->maxa > 0) {
363 if(c->maxa < (float)*w / *h)
364 *w = *h * c->maxa + 0.5;
365 else if(c->mina < (float)*h / *w)
366 *h = *w * c->mina + 0.5;
368 if(baseismin) { /* increment calculation requires this */
372 /* adjust for increment value */
377 /* restore base dimensions */
378 *w = MAX(*w + c->basew, c->minw);
379 *h = MAX(*h + c->baseh, c->minh);
381 *w = MIN(*w, c->maxw);
383 *h = MIN(*h, c->maxh);
385 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
389 arrange(Monitor *m) {
392 else for(m = mons; m; m = m->next)
396 else for(m = mons; m; m = m->next)
401 arrangemon(Monitor *m) {
402 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
403 if(m->lt[m->sellt]->arrange)
404 m->lt[m->sellt]->arrange(m);
410 c->next = c->mon->clients;
415 attachstack(Client *c) {
416 c->snext = c->mon->stack;
421 buttonpress(XEvent *e) {
422 unsigned int i, x, click;
426 XButtonPressedEvent *ev = &e->xbutton;
429 /* focus monitor if necessary */
430 if((m = wintomon(ev->window)) && m != selmon) {
431 unfocus(selmon->sel, True);
435 if(ev->window == selmon->barwin) {
439 while(ev->x >= x && ++i < LENGTH(tags));
440 if(i < LENGTH(tags)) {
444 else if(ev->x < x + blw)
446 else if(ev->x > selmon->ww - TEXTW(stext))
447 click = ClkStatusText;
451 else if((c = wintoclient(ev->window))) {
453 click = ClkClientWin;
455 for(i = 0; i < LENGTH(buttons); i++)
456 if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
457 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
458 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
463 xerrorxlib = XSetErrorHandler(xerrorstart);
464 /* this causes an error if some other window manager is running */
465 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
467 XSetErrorHandler(xerror);
474 Layout foo = { "", NULL };
478 selmon->lt[selmon->sellt] = &foo;
479 for(m = mons; m; m = m->next)
481 unmanage(m->stack, False);
483 XFreeFontSet(dpy, dc.font.set);
485 XFreeFont(dpy, dc.font.xfont);
486 XUngrabKey(dpy, AnyKey, AnyModifier, root);
487 XFreePixmap(dpy, dc.drawable);
489 XFreeCursor(dpy, cursor[CurNormal]);
490 XFreeCursor(dpy, cursor[CurResize]);
491 XFreeCursor(dpy, cursor[CurMove]);
495 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
499 cleanupmon(Monitor *mon) {
505 for(m = mons; m && m->next != mon; m = m->next);
508 XUnmapWindow(dpy, mon->barwin);
509 XDestroyWindow(dpy, mon->barwin);
514 clearurgent(Client *c) {
518 if(!(wmh = XGetWMHints(dpy, c->win)))
520 wmh->flags &= ~XUrgencyHint;
521 XSetWMHints(dpy, c->win, wmh);
526 clientmessage(XEvent *e) {
527 XClientMessageEvent *cme = &e->xclient;
528 Client *c = wintoclient(cme->window);
532 if(cme->message_type == netatom[NetWMState] && cme->data.l[1] == netatom[NetWMFullscreen]) {
533 if(cme->data.l[0] && !c->isfullscreen) {
534 XChangeProperty(dpy, cme->window, netatom[NetWMState], XA_ATOM, 32,
535 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
536 c->isfullscreen = True;
537 c->oldstate = c->isfloating;
540 c->isfloating = True;
541 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
542 XRaiseWindow(dpy, c->win);
545 XChangeProperty(dpy, cme->window, netatom[NetWMState], XA_ATOM, 32,
546 PropModeReplace, (unsigned char*)0, 0);
547 c->isfullscreen = False;
548 c->isfloating = c->oldstate;
554 resizeclient(c, c->x, c->y, c->w, c->h);
558 else if(cme->message_type == netatom[NetActiveWindow]) {
560 c->mon->seltags ^= 1;
561 c->mon->tagset[c->mon->seltags] = c->tags;
568 configure(Client *c) {
571 ce.type = ConfigureNotify;
579 ce.border_width = c->bw;
581 ce.override_redirect = False;
582 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
586 configurenotify(XEvent *e) {
588 XConfigureEvent *ev = &e->xconfigure;
591 if(ev->window == root) {
592 dirty = (sw != ev->width);
595 if(updategeom() || dirty) {
597 XFreePixmap(dpy, dc.drawable);
598 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
600 for(m = mons; m; m = m->next)
601 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
609 configurerequest(XEvent *e) {
612 XConfigureRequestEvent *ev = &e->xconfigurerequest;
615 if((c = wintoclient(ev->window))) {
616 if(ev->value_mask & CWBorderWidth)
617 c->bw = ev->border_width;
618 else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
620 if(ev->value_mask & CWX) {
622 c->x = m->mx + ev->x;
624 if(ev->value_mask & CWY) {
626 c->y = m->my + ev->y;
628 if(ev->value_mask & CWWidth) {
632 if(ev->value_mask & CWHeight) {
636 if((c->x + c->w) > m->mx + m->mw && c->isfloating)
637 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
638 if((c->y + c->h) > m->my + m->mh && c->isfloating)
639 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
640 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
643 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
651 wc.width = ev->width;
652 wc.height = ev->height;
653 wc.border_width = ev->border_width;
654 wc.sibling = ev->above;
655 wc.stack_mode = ev->detail;
656 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
665 if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
666 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
667 m->tagset[0] = m->tagset[1] = 1;
669 m->nmaster = nmaster;
670 m->showbar = showbar;
672 m->lt[0] = &layouts[0];
673 m->lt[1] = &layouts[1 % LENGTH(layouts)];
674 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
679 destroynotify(XEvent *e) {
681 XDestroyWindowEvent *ev = &e->xdestroywindow;
683 if((c = wintoclient(ev->window)))
691 for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
696 detachstack(Client *c) {
699 for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
702 if(c == c->mon->sel) {
703 for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
709 die(const char *errstr, ...) {
712 va_start(ap, errstr);
713 vfprintf(stderr, errstr, ap);
723 if(!(m = selmon->next))
726 else if(selmon == mons)
727 for(m = mons; m->next; m = m->next);
729 for(m = mons; m->next != selmon; m = m->next);
734 drawbar(Monitor *m) {
736 unsigned int i, occ = 0, urg = 0;
740 for(c = m->clients; c; c = c->next) {
746 for(i = 0; i < LENGTH(tags); i++) {
747 dc.w = TEXTW(tags[i]);
748 col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
749 drawtext(tags[i], col, urg & 1 << i);
750 drawsquare(m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
751 occ & 1 << i, urg & 1 << i, col);
754 dc.w = blw = TEXTW(m->ltsymbol);
755 drawtext(m->ltsymbol, dc.norm, False);
758 if(m == selmon) { /* status is only drawn on selected monitor */
765 drawtext(stext, dc.norm, False);
769 if((dc.w = dc.x - x) > bh) {
772 col = m == selmon ? dc.sel : dc.norm;
773 drawtext(m->sel->name, col, False);
774 drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
777 drawtext(NULL, dc.norm, False);
779 XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
787 for(m = mons; m; m = m->next)
792 drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
795 XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
796 x = (dc.font.ascent + dc.font.descent + 2) / 4;
798 XFillRectangle(dpy, dc.drawable, dc.gc, dc.x+1, dc.y+1, x+1, x+1);
800 XDrawRectangle(dpy, dc.drawable, dc.gc, dc.x+1, dc.y+1, x, x);
804 drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
806 int i, x, y, h, len, olen;
808 XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
809 XFillRectangle(dpy, dc.drawable, dc.gc, dc.x, dc.y, dc.w, dc.h);
813 h = dc.font.ascent + dc.font.descent;
814 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
816 /* shorten text if necessary */
817 for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
820 memcpy(buf, text, len);
822 for(i = len; i && i > len - 3; buf[--i] = '.');
823 XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
825 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
827 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
831 enternotify(XEvent *e) {
834 XCrossingEvent *ev = &e->xcrossing;
836 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
838 c = wintoclient(ev->window);
839 m = c ? c->mon : wintomon(ev->window);
841 unfocus(selmon->sel, True);
844 else if(!c || c == selmon->sel)
852 XExposeEvent *ev = &e->xexpose;
854 if(ev->count == 0 && (m = wintomon(ev->window)))
860 if(!c || !ISVISIBLE(c))
861 for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
862 /* was if(selmon->sel) */
863 if(selmon->sel && selmon->sel != c)
864 unfocus(selmon->sel, False);
872 grabbuttons(c, True);
873 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
877 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
883 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
884 XFocusChangeEvent *ev = &e->xfocus;
886 if(selmon->sel && ev->window != selmon->sel->win)
887 setfocus(selmon->sel);
891 focusmon(const Arg *arg) {
896 if((m = dirtomon(arg->i)) == selmon)
898 unfocus(selmon->sel, True);
904 focusstack(const Arg *arg) {
905 Client *c = NULL, *i;
910 for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
912 for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
915 for(i = selmon->clients; i != selmon->sel; i = i->next)
919 for(; i; i = i->next)
930 getcolor(const char *colstr) {
931 Colormap cmap = DefaultColormap(dpy, screen);
934 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
935 die("error, cannot allocate color '%s'\n", colstr);
940 getrootptr(int *x, int *y) {
945 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
952 unsigned char *p = NULL;
953 unsigned long n, extra;
956 if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
957 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
966 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
971 if(!text || size == 0)
974 XGetTextProperty(dpy, w, &name, atom);
977 if(name.encoding == XA_STRING)
978 strncpy(text, (char *)name.value, size - 1);
980 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
981 strncpy(text, *list, size - 1);
982 XFreeStringList(list);
985 text[size - 1] = '\0';
991 grabbuttons(Client *c, Bool focused) {
995 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
996 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
998 for(i = 0; i < LENGTH(buttons); i++)
999 if(buttons[i].click == ClkClientWin)
1000 for(j = 0; j < LENGTH(modifiers); j++)
1001 XGrabButton(dpy, buttons[i].button,
1002 buttons[i].mask | modifiers[j],
1003 c->win, False, BUTTONMASK,
1004 GrabModeAsync, GrabModeSync, None, None);
1007 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
1008 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1014 updatenumlockmask();
1017 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1020 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1021 for(i = 0; i < LENGTH(keys); i++)
1022 if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
1023 for(j = 0; j < LENGTH(modifiers); j++)
1024 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
1025 True, GrabModeAsync, GrabModeAsync);
1030 incnmaster(const Arg *arg) {
1031 selmon->nmaster = MAX(selmon->nmaster + arg->i, 1);
1036 initfont(const char *fontstr) {
1037 char *def, **missing;
1040 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
1043 fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
1044 XFreeStringList(missing);
1047 XFontStruct **xfonts;
1050 dc.font.ascent = dc.font.descent = 0;
1051 XExtentsOfFontSet(dc.font.set);
1052 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
1054 dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
1055 dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
1060 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
1061 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
1062 die("error, cannot load font: '%s'\n", fontstr);
1063 dc.font.ascent = dc.font.xfont->ascent;
1064 dc.font.descent = dc.font.xfont->descent;
1066 dc.font.height = dc.font.ascent + dc.font.descent;
1071 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
1073 if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1074 && unique[n].width == info->width && unique[n].height == info->height)
1078 #endif /* XINERAMA */
1081 keypress(XEvent *e) {
1087 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1088 for(i = 0; i < LENGTH(keys); i++)
1089 if(keysym == keys[i].keysym
1090 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1092 keys[i].func(&(keys[i].arg));
1096 killclient(const Arg *arg) {
1099 if(!sendevent(selmon->sel, wmatom[WMDelete])) {
1101 XSetErrorHandler(xerrordummy);
1102 XSetCloseDownMode(dpy, DestroyAll);
1103 XKillClient(dpy, selmon->sel->win);
1105 XSetErrorHandler(xerror);
1111 manage(Window w, XWindowAttributes *wa) {
1112 Client *c, *t = NULL;
1113 Window trans = None;
1116 if(!(c = calloc(1, sizeof(Client))))
1117 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1120 if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1129 c->x = c->oldx = wa->x;
1130 c->y = c->oldy = wa->y;
1131 c->w = c->oldw = wa->width;
1132 c->h = c->oldh = wa->height;
1133 c->oldbw = wa->border_width;
1134 if(c->w == c->mon->mw && c->h == c->mon->mh) {
1135 c->isfloating = True; // regression with flash, XXXX
1141 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1142 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1143 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1144 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1145 c->x = MAX(c->x, c->mon->mx);
1146 /* only fix client y-offset, if the client center might cover the bar */
1147 c->y = MAX(c->y, ((c->mon->by == 0) && (c->x + (c->w / 2) >= c->mon->wx)
1148 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1151 wc.border_width = c->bw;
1152 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1153 XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
1154 configure(c); /* propagates border_width, if size doesn't change */
1157 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1158 grabbuttons(c, False);
1160 c->isfloating = c->oldstate = trans != None || c->isfixed;
1162 XRaiseWindow(dpy, c->win);
1165 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1166 setclientstate(c, NormalState);
1167 if (c->mon == selmon)
1168 unfocus(selmon->sel, False);
1171 XMapWindow(dpy, c->win);
1176 mappingnotify(XEvent *e) {
1177 XMappingEvent *ev = &e->xmapping;
1179 XRefreshKeyboardMapping(ev);
1180 if(ev->request == MappingKeyboard)
1185 maprequest(XEvent *e) {
1186 static XWindowAttributes wa;
1187 XMapRequestEvent *ev = &e->xmaprequest;
1189 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1191 if(wa.override_redirect)
1193 if(!wintoclient(ev->window))
1194 manage(ev->window, &wa);
1198 monocle(Monitor *m) {
1202 for(c = m->clients; c; c = c->next)
1205 if(n > 0) /* override layout symbol */
1206 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1207 for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
1208 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
1212 movemouse(const Arg *arg) {
1213 int x, y, ocx, ocy, nx, ny;
1218 if(!(c = selmon->sel))
1223 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1224 None, cursor[CurMove], CurrentTime) != GrabSuccess)
1226 if(!getrootptr(&x, &y))
1229 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1231 case ConfigureRequest:
1234 handler[ev.type](&ev);
1237 nx = ocx + (ev.xmotion.x - x);
1238 ny = ocy + (ev.xmotion.y - y);
1239 if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1240 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1241 if(abs(selmon->wx - nx) < snap)
1243 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1244 nx = selmon->wx + selmon->ww - WIDTH(c);
1245 if(abs(selmon->wy - ny) < snap)
1247 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1248 ny = selmon->wy + selmon->wh - HEIGHT(c);
1249 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1250 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1251 togglefloating(NULL);
1253 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1254 resize(c, nx, ny, c->w, c->h, True);
1257 } while(ev.type != ButtonRelease);
1258 XUngrabPointer(dpy, CurrentTime);
1259 if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
1267 nexttiled(Client *c) {
1268 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1281 propertynotify(XEvent *e) {
1284 XPropertyEvent *ev = &e->xproperty;
1286 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1288 else if(ev->state == PropertyDelete)
1289 return; /* ignore */
1290 else if((c = wintoclient(ev->window))) {
1293 case XA_WM_TRANSIENT_FOR:
1294 if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1295 (c->isfloating = (wintoclient(trans)) != NULL))
1298 case XA_WM_NORMAL_HINTS:
1306 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1308 if(c == c->mon->sel)
1315 ptrtomon(int x, int y) {
1318 for(m = mons; m; m = m->next)
1319 if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh))
1325 quit(const Arg *arg) {
1330 resize(Client *c, int x, int y, int w, int h, Bool interact) {
1331 if(applysizehints(c, &x, &y, &w, &h, interact))
1332 resizeclient(c, x, y, w, h);
1336 resizeclient(Client *c, int x, int y, int w, int h) {
1339 c->oldx = c->x; c->x = wc.x = x;
1340 c->oldy = c->y; c->y = wc.y = y;
1341 c->oldw = c->w; c->w = wc.width = w;
1342 c->oldh = c->h; c->h = wc.height = h;
1343 wc.border_width = c->bw;
1344 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1350 resizemouse(const Arg *arg) {
1357 if(!(c = selmon->sel))
1362 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1363 None, cursor[CurResize], CurrentTime) != GrabSuccess)
1365 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1367 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1369 case ConfigureRequest:
1372 handler[ev.type](&ev);
1375 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1376 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1377 if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1378 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1380 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1381 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1382 togglefloating(NULL);
1384 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1385 resize(c, c->x, c->y, nw, nh, True);
1388 } while(ev.type != ButtonRelease);
1389 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1390 XUngrabPointer(dpy, CurrentTime);
1391 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1392 if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
1400 restack(Monitor *m) {
1408 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1409 XRaiseWindow(dpy, m->sel->win);
1410 if(m->lt[m->sellt]->arrange) {
1411 wc.stack_mode = Below;
1412 wc.sibling = m->barwin;
1413 for(c = m->stack; c; c = c->snext)
1414 if(!c->isfloating && ISVISIBLE(c)) {
1415 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1416 wc.sibling = c->win;
1420 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1426 /* main event loop */
1428 while(running && !XNextEvent(dpy, &ev))
1429 if(handler[ev.type])
1430 handler[ev.type](&ev); /* call handler */
1435 unsigned int i, num;
1436 Window d1, d2, *wins = NULL;
1437 XWindowAttributes wa;
1439 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1440 for(i = 0; i < num; i++) {
1441 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1442 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1444 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1445 manage(wins[i], &wa);
1447 for(i = 0; i < num; i++) { /* now the transients */
1448 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1450 if(XGetTransientForHint(dpy, wins[i], &d1)
1451 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1452 manage(wins[i], &wa);
1460 sendmon(Client *c, Monitor *m) {
1467 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1475 setclientstate(Client *c, long state) {
1476 long data[] = { state, None };
1478 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1479 PropModeReplace, (unsigned char *)data, 2);
1483 sendevent(Client *c, Atom proto) {
1486 Bool exists = False;
1489 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1490 while(!exists && n--)
1491 exists = protocols[n] == proto;
1495 ev.type = ClientMessage;
1496 ev.xclient.window = c->win;
1497 ev.xclient.message_type = wmatom[WMProtocols];
1498 ev.xclient.format = 32;
1499 ev.xclient.data.l[0] = proto;
1500 ev.xclient.data.l[1] = CurrentTime;
1501 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1507 setfocus(Client *c) {
1509 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1510 sendevent(c, wmatom[WMTakeFocus]);
1514 setlayout(const Arg *arg) {
1515 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1518 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1519 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1526 /* arg > 1.0 will set mfact absolutly */
1528 setmfact(const Arg *arg) {
1531 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1533 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1534 if(f < 0.1 || f > 0.9)
1542 XSetWindowAttributes wa;
1544 /* clean up any zombies immediately */
1548 screen = DefaultScreen(dpy);
1549 root = RootWindow(dpy, screen);
1551 sw = DisplayWidth(dpy, screen);
1552 sh = DisplayHeight(dpy, screen);
1553 bh = dc.h = dc.font.height + 2;
1556 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1557 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1558 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1559 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1560 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1561 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1562 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1563 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1564 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1566 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1567 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1568 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1569 /* init appearance */
1570 dc.norm[ColBorder] = getcolor(normbordercolor);
1571 dc.norm[ColBG] = getcolor(normbgcolor);
1572 dc.norm[ColFG] = getcolor(normfgcolor);
1573 dc.sel[ColBorder] = getcolor(selbordercolor);
1574 dc.sel[ColBG] = getcolor(selbgcolor);
1575 dc.sel[ColFG] = getcolor(selfgcolor);
1576 dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
1577 dc.gc = XCreateGC(dpy, root, 0, NULL);
1578 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1580 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
1584 /* EWMH support per view */
1585 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1586 PropModeReplace, (unsigned char *) netatom, NetLast);
1587 /* select for events */
1588 wa.cursor = cursor[CurNormal];
1589 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
1590 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
1591 |PropertyChangeMask;
1592 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1593 XSelectInput(dpy, root, wa.event_mask);
1598 showhide(Client *c) {
1601 if(ISVISIBLE(c)) { /* show clients top down */
1602 XMoveWindow(dpy, c->win, c->x, c->y);
1603 if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1604 resize(c, c->x, c->y, c->w, c->h, False);
1607 else { /* hide clients bottom up */
1609 XMoveWindow(dpy, c->win, c->w * -2, c->y);
1614 sigchld(int unused) {
1615 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1616 die("Can't install SIGCHLD handler");
1617 while(0 < waitpid(-1, NULL, WNOHANG));
1621 spawn(const Arg *arg) {
1624 close(ConnectionNumber(dpy));
1626 execvp(((char **)arg->v)[0], (char **)arg->v);
1627 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1634 tag(const Arg *arg) {
1635 if(selmon->sel && arg->ui & TAGMASK) {
1636 selmon->sel->tags = arg->ui & TAGMASK;
1643 tagmon(const Arg *arg) {
1644 if(!selmon->sel || !mons->next)
1646 sendmon(selmon->sel, dirtomon(arg->i));
1650 textnw(const char *text, unsigned int len) {
1654 XmbTextExtents(dc.font.set, text, len, NULL, &r);
1657 return XTextWidth(dc.font.xfont, text, len);
1662 unsigned int i, n, mw, mh, tw, th;
1665 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1669 mh = (n > m->nmaster) ? m->wh / m->nmaster : m->wh / n;
1670 mw = (n > m->nmaster) ? m->ww * m->mfact : m->ww;
1671 th = (n > m->nmaster) ? m->wh / (n - m->nmaster) : 0;
1674 for(i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1676 resize(c, m->wx, m->wy + (i*mh), mw - (2*c->bw), mh - (2*c->bw), False);
1678 resize(c, m->wx + mw, m->wy + ((i - m->nmaster)*th), tw - (2*c->bw), th - (2*c->bw), False);
1682 togglebar(const Arg *arg) {
1683 selmon->showbar = !selmon->showbar;
1684 updatebarpos(selmon);
1685 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1690 togglefloating(const Arg *arg) {
1693 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1694 if(selmon->sel->isfloating)
1695 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1696 selmon->sel->w, selmon->sel->h, False);
1701 toggletag(const Arg *arg) {
1702 unsigned int newtags;
1706 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1708 selmon->sel->tags = newtags;
1715 toggleview(const Arg *arg) {
1716 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1719 selmon->tagset[selmon->seltags] = newtagset;
1726 unfocus(Client *c, Bool setfocus) {
1729 grabbuttons(c, False);
1730 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
1732 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1736 unmanage(Client *c, Bool destroyed) {
1737 Monitor *m = c->mon;
1740 /* The server grab construct avoids race conditions. */
1744 wc.border_width = c->oldbw;
1746 XSetErrorHandler(xerrordummy);
1747 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1748 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1749 setclientstate(c, WithdrawnState);
1751 XSetErrorHandler(xerror);
1760 unmapnotify(XEvent *e) {
1762 XUnmapEvent *ev = &e->xunmap;
1764 if((c = wintoclient(ev->window))) {
1766 setclientstate(c, WithdrawnState);
1775 XSetWindowAttributes wa = {
1776 .override_redirect = True,
1777 .background_pixmap = ParentRelative,
1778 .event_mask = ButtonPressMask|ExposureMask
1780 for(m = mons; m; m = m->next) {
1781 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1782 CopyFromParent, DefaultVisual(dpy, screen),
1783 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1784 XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
1785 XMapRaised(dpy, m->barwin);
1790 updatebarpos(Monitor *m) {
1795 m->by = m->topbar ? m->wy : m->wy + m->wh;
1796 m->wy = m->topbar ? m->wy + bh : m->wy;
1807 if(XineramaIsActive(dpy)) {
1811 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1812 XineramaScreenInfo *unique = NULL;
1814 for(n = 0, m = mons; m; m = m->next, n++);
1815 /* only consider unique geometries as separate screens */
1816 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
1817 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
1818 for(i = 0, j = 0; i < nn; i++)
1819 if(isuniquegeom(unique, j, &info[i]))
1820 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1824 for(i = 0; i < (nn - n); i++) { /* new monitors available */
1825 for(m = mons; m && m->next; m = m->next);
1827 m->next = createmon();
1831 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
1833 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1834 || unique[i].width != m->mw || unique[i].height != m->mh))
1838 m->mx = m->wx = unique[i].x_org;
1839 m->my = m->wy = unique[i].y_org;
1840 m->mw = m->ww = unique[i].width;
1841 m->mh = m->wh = unique[i].height;
1845 else { /* less monitors available nn < n */
1846 for(i = nn; i < n; i++) {
1847 for(m = mons; m && m->next; m = m->next);
1851 m->clients = c->next;
1865 #endif /* XINERAMA */
1866 /* default monitor setup */
1870 if(mons->mw != sw || mons->mh != sh) {
1872 mons->mw = mons->ww = sw;
1873 mons->mh = mons->wh = sh;
1879 selmon = wintomon(root);
1885 updatenumlockmask(void) {
1887 XModifierKeymap *modmap;
1890 modmap = XGetModifierMapping(dpy);
1891 for(i = 0; i < 8; i++)
1892 for(j = 0; j < modmap->max_keypermod; j++)
1893 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1894 == XKeysymToKeycode(dpy, XK_Num_Lock))
1895 numlockmask = (1 << i);
1896 XFreeModifiermap(modmap);
1900 updatesizehints(Client *c) {
1904 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
1905 /* size is uninitialized, ensure that size.flags aren't used */
1907 if(size.flags & PBaseSize) {
1908 c->basew = size.base_width;
1909 c->baseh = size.base_height;
1911 else if(size.flags & PMinSize) {
1912 c->basew = size.min_width;
1913 c->baseh = size.min_height;
1916 c->basew = c->baseh = 0;
1917 if(size.flags & PResizeInc) {
1918 c->incw = size.width_inc;
1919 c->inch = size.height_inc;
1922 c->incw = c->inch = 0;
1923 if(size.flags & PMaxSize) {
1924 c->maxw = size.max_width;
1925 c->maxh = size.max_height;
1928 c->maxw = c->maxh = 0;
1929 if(size.flags & PMinSize) {
1930 c->minw = size.min_width;
1931 c->minh = size.min_height;
1933 else if(size.flags & PBaseSize) {
1934 c->minw = size.base_width;
1935 c->minh = size.base_height;
1938 c->minw = c->minh = 0;
1939 if(size.flags & PAspect) {
1940 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
1941 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
1944 c->maxa = c->mina = 0.0;
1945 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
1946 && c->maxw == c->minw && c->maxh == c->minh);
1950 updatetitle(Client *c) {
1951 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
1952 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
1953 if(c->name[0] == '\0') /* hack to mark broken clients */
1954 strcpy(c->name, broken);
1958 updatestatus(void) {
1959 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
1960 strcpy(stext, "dwm-"VERSION);
1965 updatewmhints(Client *c) {
1968 if((wmh = XGetWMHints(dpy, c->win))) {
1969 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
1970 wmh->flags &= ~XUrgencyHint;
1971 XSetWMHints(dpy, c->win, wmh);
1974 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
1975 if(wmh->flags & InputHint)
1976 c->neverfocus = !wmh->input;
1978 c->neverfocus = False;
1984 view(const Arg *arg) {
1985 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
1987 selmon->seltags ^= 1; /* toggle sel tagset */
1988 if(arg->ui & TAGMASK)
1989 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
1995 wintoclient(Window w) {
1999 for(m = mons; m; m = m->next)
2000 for(c = m->clients; c; c = c->next)
2007 wintomon(Window w) {
2012 if(w == root && getrootptr(&x, &y))
2013 return ptrtomon(x, y);
2014 for(m = mons; m; m = m->next)
2017 if((c = wintoclient(w)))
2022 /* There's no way to check accesses to destroyed windows, thus those cases are
2023 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2024 * default error handler, which may call exit. */
2026 xerror(Display *dpy, XErrorEvent *ee) {
2027 if(ee->error_code == BadWindow
2028 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2029 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2030 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2031 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2032 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2033 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2034 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2035 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2037 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2038 ee->request_code, ee->error_code);
2039 return xerrorxlib(dpy, ee); /* may call exit */
2043 xerrordummy(Display *dpy, XErrorEvent *ee) {
2047 /* Startup Error handler to check if another window manager
2048 * is already running. */
2050 xerrorstart(Display *dpy, XErrorEvent *ee) {
2051 die("dwm: another window manager is already running\n");
2056 zoom(const Arg *arg) {
2057 Client *c = selmon->sel;
2059 if(!selmon->lt[selmon->sellt]->arrange
2060 || (selmon->sel && selmon->sel->isfloating))
2062 if(c == nexttiled(selmon->clients))
2063 if(!c || !(c = nexttiled(c->next)))
2069 main(int argc, char *argv[]) {
2070 if(argc == 2 && !strcmp("-v", argv[1]))
2071 die("dwm-"VERSION", © 2006-2011 dwm engineers, see LICENSE for details\n");
2073 die("usage: dwm [-v]\n");
2074 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2075 fputs("warning: no locale support\n", stderr);
2076 if(!(dpy = XOpenDisplay(NULL)))
2077 die("dwm: cannot open display\n");
2084 return EXIT_SUCCESS;