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 INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
47 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
48 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
49 #define LENGTH(X) (sizeof X / sizeof X[0])
50 #define MAX(A, B) ((A) > (B) ? (A) : (B))
51 #define MIN(A, B) ((A) < (B) ? (A) : (B))
52 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
53 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
54 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
55 #define TAGMASK ((1 << LENGTH(tags)) - 1)
56 #define TEXTW(X) (textnw(X, strlen(X)) + dc.font.height)
59 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
60 enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
61 enum { NetSupported, NetWMName, NetWMState,
62 NetWMFullscreen, NetActiveWindow, NetWMWindowType,
63 NetWMWindowTypeDialog, NetLast }; /* EWMH atoms */
64 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
65 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
66 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
79 void (*func)(const Arg *arg);
83 typedef struct Monitor Monitor;
84 typedef struct Client Client;
89 int oldx, oldy, oldw, oldh;
90 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
93 Bool isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
102 unsigned long norm[ColLast];
103 unsigned long sel[ColLast];
113 } DC; /* draw context */
118 void (*func)(const Arg *);
124 void (*arrange)(Monitor *);
132 int by; /* bar geometry */
133 int mx, my, mw, mh; /* screen size */
134 int wx, wy, ww, wh; /* window area */
135 unsigned int seltags;
137 unsigned int tagset[2];
150 const char *instance;
157 /* function declarations */
158 static void applyrules(Client *c);
159 static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
160 static void arrange(Monitor *m);
161 static void arrangemon(Monitor *m);
162 static void attach(Client *c);
163 static void attachstack(Client *c);
164 static void buttonpress(XEvent *e);
165 static void checkotherwm(void);
166 static void cleanup(void);
167 static void cleanupmon(Monitor *mon);
168 static void clearurgent(Client *c);
169 static void clientmessage(XEvent *e);
170 static void configure(Client *c);
171 static void configurenotify(XEvent *e);
172 static void configurerequest(XEvent *e);
173 static Monitor *createmon(void);
174 static void destroynotify(XEvent *e);
175 static void detach(Client *c);
176 static void detachstack(Client *c);
177 static void die(const char *errstr, ...);
178 static Monitor *dirtomon(int dir);
179 static void drawbar(Monitor *m);
180 static void drawbars(void);
181 static void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
182 static void drawtext(const char *text, unsigned long col[ColLast], Bool invert);
183 static void enternotify(XEvent *e);
184 static void expose(XEvent *e);
185 static void focus(Client *c);
186 static void focusin(XEvent *e);
187 static void focusmon(const Arg *arg);
188 static void focusstack(const Arg *arg);
189 static unsigned long getcolor(const char *colstr);
190 static Bool getrootptr(int *x, int *y);
191 static long getstate(Window w);
192 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
193 static void grabbuttons(Client *c, Bool focused);
194 static void grabkeys(void);
195 static void incnmaster(const Arg *arg);
196 static void initfont(const char *fontstr);
197 static void keypress(XEvent *e);
198 static void killclient(const Arg *arg);
199 static void manage(Window w, XWindowAttributes *wa);
200 static void mappingnotify(XEvent *e);
201 static void maprequest(XEvent *e);
202 static void monocle(Monitor *m);
203 static void motionnotify(XEvent *e);
204 static void movemouse(const Arg *arg);
205 static Client *nexttiled(Client *c);
206 static void pop(Client *);
207 static void propertynotify(XEvent *e);
208 static void quit(const Arg *arg);
209 static Monitor *recttomon(int x, int y, int w, int h);
210 static void resize(Client *c, int x, int y, int w, int h, Bool interact);
211 static void resizeclient(Client *c, int x, int y, int w, int h);
212 static void resizemouse(const Arg *arg);
213 static void restack(Monitor *m);
214 static void run(void);
215 static void scan(void);
216 static Bool sendevent(Client *c, Atom proto);
217 static void sendmon(Client *c, Monitor *m);
218 static void setclientstate(Client *c, long state);
219 static void setfocus(Client *c);
220 static void setfullscreen(Client *c, Bool fullscreen);
221 static void setlayout(const Arg *arg);
222 static void setmfact(const Arg *arg);
223 static void setup(void);
224 static void showhide(Client *c);
225 static void sigchld(int unused);
226 static void spawn(const Arg *arg);
227 static void tag(const Arg *arg);
228 static void tagmon(const Arg *arg);
229 static int textnw(const char *text, unsigned int len);
230 static void tile(Monitor *);
231 static void togglebar(const Arg *arg);
232 static void togglefloating(const Arg *arg);
233 static void toggletag(const Arg *arg);
234 static void toggleview(const Arg *arg);
235 static void unfocus(Client *c, Bool setfocus);
236 static void unmanage(Client *c, Bool destroyed);
237 static void unmapnotify(XEvent *e);
238 static Bool updategeom(void);
239 static void updatebarpos(Monitor *m);
240 static void updatebars(void);
241 static void updatenumlockmask(void);
242 static void updatesizehints(Client *c);
243 static void updatestatus(void);
244 static void updatewindowtype(Client *c);
245 static void updatetitle(Client *c);
246 static void updatewmhints(Client *c);
247 static void view(const Arg *arg);
248 static Client *wintoclient(Window w);
249 static Monitor *wintomon(Window w);
250 static int xerror(Display *dpy, XErrorEvent *ee);
251 static int xerrordummy(Display *dpy, XErrorEvent *ee);
252 static int xerrorstart(Display *dpy, XErrorEvent *ee);
253 static void zoom(const Arg *arg);
256 static const char broken[] = "broken";
257 static char stext[256];
259 static int sw, sh; /* X display screen geometry width, height */
260 static int bh, blw = 0; /* bar geometry */
261 static int (*xerrorxlib)(Display *, XErrorEvent *);
262 static unsigned int numlockmask = 0;
263 static void (*handler[LASTEvent]) (XEvent *) = {
264 [ButtonPress] = buttonpress,
265 [ClientMessage] = clientmessage,
266 [ConfigureRequest] = configurerequest,
267 [ConfigureNotify] = configurenotify,
268 [DestroyNotify] = destroynotify,
269 [EnterNotify] = enternotify,
272 [KeyPress] = keypress,
273 [MappingNotify] = mappingnotify,
274 [MapRequest] = maprequest,
275 [MotionNotify] = motionnotify,
276 [PropertyNotify] = propertynotify,
277 [UnmapNotify] = unmapnotify
279 static Atom wmatom[WMLast], netatom[NetLast];
280 static Bool running = True;
281 static Cursor cursor[CurLast];
284 static Monitor *mons = NULL, *selmon = NULL;
287 /* configuration, allows nested code to access above variables */
290 /* compile-time check if all tags fit into an unsigned int bit array. */
291 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
293 /* function implementations */
295 applyrules(Client *c) {
296 const char *class, *instance;
300 XClassHint ch = { NULL, NULL };
303 c->isfloating = c->tags = 0;
304 XGetClassHint(dpy, c->win, &ch);
305 class = ch.res_class ? ch.res_class : broken;
306 instance = ch.res_name ? ch.res_name : broken;
308 for(i = 0; i < LENGTH(rules); i++) {
310 if((!r->title || strstr(c->name, r->title))
311 && (!r->class || strstr(class, r->class))
312 && (!r->instance || strstr(instance, r->instance)))
314 c->isfloating = r->isfloating;
316 for(m = mons; m && m->num != r->monitor; m = m->next);
325 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
329 applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
333 /* set minimum possible */
341 if(*x + *w + 2 * c->bw < 0)
343 if(*y + *h + 2 * c->bw < 0)
347 if(*x >= m->wx + m->ww)
348 *x = m->wx + m->ww - WIDTH(c);
349 if(*y >= m->wy + m->wh)
350 *y = m->wy + m->wh - HEIGHT(c);
351 if(*x + *w + 2 * c->bw <= m->wx)
353 if(*y + *h + 2 * c->bw <= m->wy)
360 if(resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
361 /* see last two sentences in ICCCM 4.1.2.3 */
362 baseismin = c->basew == c->minw && c->baseh == c->minh;
363 if(!baseismin) { /* temporarily remove base dimensions */
367 /* adjust for aspect limits */
368 if(c->mina > 0 && c->maxa > 0) {
369 if(c->maxa < (float)*w / *h)
370 *w = *h * c->maxa + 0.5;
371 else if(c->mina < (float)*h / *w)
372 *h = *w * c->mina + 0.5;
374 if(baseismin) { /* increment calculation requires this */
378 /* adjust for increment value */
383 /* restore base dimensions */
384 *w = MAX(*w + c->basew, c->minw);
385 *h = MAX(*h + c->baseh, c->minh);
387 *w = MIN(*w, c->maxw);
389 *h = MIN(*h, c->maxh);
391 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
395 arrange(Monitor *m) {
398 else for(m = mons; m; m = m->next)
403 } else for(m = mons; m; m = m->next)
408 arrangemon(Monitor *m) {
409 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
410 if(m->lt[m->sellt]->arrange)
411 m->lt[m->sellt]->arrange(m);
416 c->next = c->mon->clients;
421 attachstack(Client *c) {
422 c->snext = c->mon->stack;
427 buttonpress(XEvent *e) {
428 unsigned int i, x, click;
432 XButtonPressedEvent *ev = &e->xbutton;
435 /* focus monitor if necessary */
436 if((m = wintomon(ev->window)) && m != selmon) {
437 unfocus(selmon->sel, True);
441 if(ev->window == selmon->barwin) {
445 while(ev->x >= x && ++i < LENGTH(tags));
446 if(i < LENGTH(tags)) {
450 else if(ev->x < x + blw)
452 else if(ev->x > selmon->ww - TEXTW(stext))
453 click = ClkStatusText;
457 else if((c = wintoclient(ev->window))) {
459 click = ClkClientWin;
461 for(i = 0; i < LENGTH(buttons); i++)
462 if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
463 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
464 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
469 xerrorxlib = XSetErrorHandler(xerrorstart);
470 /* this causes an error if some other window manager is running */
471 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
473 XSetErrorHandler(xerror);
480 Layout foo = { "", NULL };
484 selmon->lt[selmon->sellt] = &foo;
485 for(m = mons; m; m = m->next)
487 unmanage(m->stack, False);
489 XFreeFontSet(dpy, dc.font.set);
491 XFreeFont(dpy, dc.font.xfont);
492 XUngrabKey(dpy, AnyKey, AnyModifier, root);
493 XFreePixmap(dpy, dc.drawable);
495 XFreeCursor(dpy, cursor[CurNormal]);
496 XFreeCursor(dpy, cursor[CurResize]);
497 XFreeCursor(dpy, cursor[CurMove]);
501 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
505 cleanupmon(Monitor *mon) {
511 for(m = mons; m && m->next != mon; m = m->next);
514 XUnmapWindow(dpy, mon->barwin);
515 XDestroyWindow(dpy, mon->barwin);
520 clearurgent(Client *c) {
524 if(!(wmh = XGetWMHints(dpy, c->win)))
526 wmh->flags &= ~XUrgencyHint;
527 XSetWMHints(dpy, c->win, wmh);
532 clientmessage(XEvent *e) {
533 XClientMessageEvent *cme = &e->xclient;
534 Client *c = wintoclient(cme->window);
538 if(cme->message_type == netatom[NetWMState]) {
539 if(cme->data.l[1] == netatom[NetWMFullscreen] || cme->data.l[2] == netatom[NetWMFullscreen])
540 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
541 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
543 else if(cme->message_type == netatom[NetActiveWindow]) {
545 c->mon->seltags ^= 1;
546 c->mon->tagset[c->mon->seltags] = c->tags;
553 configure(Client *c) {
556 ce.type = ConfigureNotify;
564 ce.border_width = c->bw;
566 ce.override_redirect = False;
567 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
571 configurenotify(XEvent *e) {
573 XConfigureEvent *ev = &e->xconfigure;
576 if(ev->window == root) {
577 dirty = (sw != ev->width);
580 if(updategeom() || dirty) {
582 XFreePixmap(dpy, dc.drawable);
583 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
585 for(m = mons; m; m = m->next)
586 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
594 configurerequest(XEvent *e) {
597 XConfigureRequestEvent *ev = &e->xconfigurerequest;
600 if((c = wintoclient(ev->window))) {
601 if(ev->value_mask & CWBorderWidth)
602 c->bw = ev->border_width;
603 else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
605 if(ev->value_mask & CWX) {
607 c->x = m->mx + ev->x;
609 if(ev->value_mask & CWY) {
611 c->y = m->my + ev->y;
613 if(ev->value_mask & CWWidth) {
617 if(ev->value_mask & CWHeight) {
621 if((c->x + c->w) > m->mx + m->mw && c->isfloating)
622 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
623 if((c->y + c->h) > m->my + m->mh && c->isfloating)
624 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
625 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
628 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
636 wc.width = ev->width;
637 wc.height = ev->height;
638 wc.border_width = ev->border_width;
639 wc.sibling = ev->above;
640 wc.stack_mode = ev->detail;
641 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
650 if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
651 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
652 m->tagset[0] = m->tagset[1] = 1;
654 m->nmaster = nmaster;
655 m->showbar = showbar;
657 m->lt[0] = &layouts[0];
658 m->lt[1] = &layouts[1 % LENGTH(layouts)];
659 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
664 destroynotify(XEvent *e) {
666 XDestroyWindowEvent *ev = &e->xdestroywindow;
668 if((c = wintoclient(ev->window)))
676 for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
681 detachstack(Client *c) {
684 for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
687 if(c == c->mon->sel) {
688 for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
694 die(const char *errstr, ...) {
697 va_start(ap, errstr);
698 vfprintf(stderr, errstr, ap);
708 if(!(m = selmon->next))
711 else if(selmon == mons)
712 for(m = mons; m->next; m = m->next);
714 for(m = mons; m->next != selmon; m = m->next);
719 drawbar(Monitor *m) {
721 unsigned int i, occ = 0, urg = 0;
725 for(c = m->clients; c; c = c->next) {
731 for(i = 0; i < LENGTH(tags); i++) {
732 dc.w = TEXTW(tags[i]);
733 col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
734 drawtext(tags[i], col, urg & 1 << i);
735 drawsquare(m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
736 occ & 1 << i, urg & 1 << i, col);
739 dc.w = blw = TEXTW(m->ltsymbol);
740 drawtext(m->ltsymbol, dc.norm, False);
743 if(m == selmon) { /* status is only drawn on selected monitor */
750 drawtext(stext, dc.norm, False);
754 if((dc.w = dc.x - x) > bh) {
757 col = m == selmon ? dc.sel : dc.norm;
758 drawtext(m->sel->name, col, False);
759 drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
762 drawtext(NULL, dc.norm, False);
764 XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
772 for(m = mons; m; m = m->next)
777 drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
780 XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
781 x = (dc.font.ascent + dc.font.descent + 2) / 4;
783 XFillRectangle(dpy, dc.drawable, dc.gc, dc.x+1, dc.y+1, x+1, x+1);
785 XDrawRectangle(dpy, dc.drawable, dc.gc, dc.x+1, dc.y+1, x, x);
789 drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
791 int i, x, y, h, len, olen;
793 XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
794 XFillRectangle(dpy, dc.drawable, dc.gc, dc.x, dc.y, dc.w, dc.h);
798 h = dc.font.ascent + dc.font.descent;
799 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
801 /* shorten text if necessary */
802 for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
805 memcpy(buf, text, len);
807 for(i = len; i && i > len - 3; buf[--i] = '.');
808 XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
810 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
812 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
816 enternotify(XEvent *e) {
819 XCrossingEvent *ev = &e->xcrossing;
821 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
823 c = wintoclient(ev->window);
824 m = c ? c->mon : wintomon(ev->window);
826 unfocus(selmon->sel, True);
829 else if(!c || c == selmon->sel)
837 XExposeEvent *ev = &e->xexpose;
839 if(ev->count == 0 && (m = wintomon(ev->window)))
845 if(!c || !ISVISIBLE(c))
846 for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
847 /* was if(selmon->sel) */
848 if(selmon->sel && selmon->sel != c)
849 unfocus(selmon->sel, False);
857 grabbuttons(c, True);
858 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
862 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
868 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
869 XFocusChangeEvent *ev = &e->xfocus;
871 if(selmon->sel && ev->window != selmon->sel->win)
872 setfocus(selmon->sel);
876 focusmon(const Arg *arg) {
881 if((m = dirtomon(arg->i)) == selmon)
883 unfocus(selmon->sel, True);
889 focusstack(const Arg *arg) {
890 Client *c = NULL, *i;
895 for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
897 for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
900 for(i = selmon->clients; i != selmon->sel; i = i->next)
904 for(; i; i = i->next)
915 getatomprop(Client *c, Atom prop) {
918 unsigned char *p = NULL;
919 Atom da, atom = None;
921 if(XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
922 &da, &di, &dl, &dl, &p) == Success && p) {
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, 0);
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;
1135 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1136 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1137 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1138 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1139 c->x = MAX(c->x, c->mon->mx);
1140 /* only fix client y-offset, if the client center might cover the bar */
1141 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1142 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1145 wc.border_width = c->bw;
1146 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1147 XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
1148 configure(c); /* propagates border_width, if size doesn't change */
1149 updatewindowtype(c);
1152 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1153 grabbuttons(c, False);
1155 c->isfloating = c->oldstate = trans != None || c->isfixed;
1157 XRaiseWindow(dpy, c->win);
1160 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1161 setclientstate(c, NormalState);
1162 if (c->mon == selmon)
1163 unfocus(selmon->sel, False);
1166 XMapWindow(dpy, c->win);
1171 mappingnotify(XEvent *e) {
1172 XMappingEvent *ev = &e->xmapping;
1174 XRefreshKeyboardMapping(ev);
1175 if(ev->request == MappingKeyboard)
1180 maprequest(XEvent *e) {
1181 static XWindowAttributes wa;
1182 XMapRequestEvent *ev = &e->xmaprequest;
1184 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1186 if(wa.override_redirect)
1188 if(!wintoclient(ev->window))
1189 manage(ev->window, &wa);
1193 monocle(Monitor *m) {
1197 for(c = m->clients; c; c = c->next)
1200 if(n > 0) /* override layout symbol */
1201 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1202 for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
1203 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
1207 motionnotify(XEvent *e) {
1208 static Monitor *mon = NULL;
1210 XMotionEvent *ev = &e->xmotion;
1212 if(ev->window != root)
1214 if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1215 unfocus(selmon->sel, True);
1223 movemouse(const Arg *arg) {
1224 int x, y, ocx, ocy, nx, ny;
1229 if(!(c = selmon->sel))
1234 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1235 None, cursor[CurMove], CurrentTime) != GrabSuccess)
1237 if(!getrootptr(&x, &y))
1240 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1242 case ConfigureRequest:
1245 handler[ev.type](&ev);
1248 nx = ocx + (ev.xmotion.x - x);
1249 ny = ocy + (ev.xmotion.y - y);
1250 if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1251 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1252 if(abs(selmon->wx - nx) < snap)
1254 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1255 nx = selmon->wx + selmon->ww - WIDTH(c);
1256 if(abs(selmon->wy - ny) < snap)
1258 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1259 ny = selmon->wy + selmon->wh - HEIGHT(c);
1260 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1261 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1262 togglefloating(NULL);
1264 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating) {
1266 setfullscreen(c, False);
1267 resize(c, nx, ny, c->w, c->h, True);
1271 } while(ev.type != ButtonRelease);
1272 XUngrabPointer(dpy, CurrentTime);
1273 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1281 nexttiled(Client *c) {
1282 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1295 propertynotify(XEvent *e) {
1298 XPropertyEvent *ev = &e->xproperty;
1300 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1302 else if(ev->state == PropertyDelete)
1303 return; /* ignore */
1304 else if((c = wintoclient(ev->window))) {
1307 case XA_WM_TRANSIENT_FOR:
1308 if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1309 (c->isfloating = (wintoclient(trans)) != NULL))
1312 case XA_WM_NORMAL_HINTS:
1320 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1322 if(c == c->mon->sel)
1325 if(ev->atom == netatom[NetWMWindowType])
1326 updatewindowtype(c);
1331 quit(const Arg *arg) {
1336 recttomon(int x, int y, int w, int h) {
1337 Monitor *m, *r = selmon;
1340 for(m = mons; m; m = m->next)
1341 if((a = INTERSECT(x, y, w, h, m)) > area) {
1349 resize(Client *c, int x, int y, int w, int h, Bool interact) {
1350 if(applysizehints(c, &x, &y, &w, &h, interact))
1351 resizeclient(c, x, y, w, h);
1355 resizeclient(Client *c, int x, int y, int w, int h) {
1358 c->oldx = c->x; c->x = wc.x = x;
1359 c->oldy = c->y; c->y = wc.y = y;
1360 c->oldw = c->w; c->w = wc.width = w;
1361 c->oldh = c->h; c->h = wc.height = h;
1362 wc.border_width = c->bw;
1363 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1369 resizemouse(const Arg *arg) {
1376 if(!(c = selmon->sel))
1381 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1382 None, cursor[CurResize], CurrentTime) != GrabSuccess)
1384 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1386 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1388 case ConfigureRequest:
1391 handler[ev.type](&ev);
1394 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1395 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1396 if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1397 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1399 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1400 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1401 togglefloating(NULL);
1403 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating) {
1405 setfullscreen(c, False);
1406 resize(c, c->x, c->y, nw, nh, True);
1410 } while(ev.type != ButtonRelease);
1411 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1412 XUngrabPointer(dpy, CurrentTime);
1413 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1414 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1422 restack(Monitor *m) {
1430 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1431 XRaiseWindow(dpy, m->sel->win);
1432 if(m->lt[m->sellt]->arrange) {
1433 wc.stack_mode = Below;
1434 wc.sibling = m->barwin;
1435 for(c = m->stack; c; c = c->snext)
1436 if(!c->isfloating && ISVISIBLE(c)) {
1437 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1438 wc.sibling = c->win;
1442 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1448 /* main event loop */
1450 while(running && !XNextEvent(dpy, &ev))
1451 if(handler[ev.type])
1452 handler[ev.type](&ev); /* call handler */
1457 unsigned int i, num;
1458 Window d1, d2, *wins = NULL;
1459 XWindowAttributes wa;
1461 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1462 for(i = 0; i < num; i++) {
1463 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1464 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1466 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1467 manage(wins[i], &wa);
1469 for(i = 0; i < num; i++) { /* now the transients */
1470 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1472 if(XGetTransientForHint(dpy, wins[i], &d1)
1473 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1474 manage(wins[i], &wa);
1482 sendmon(Client *c, Monitor *m) {
1489 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1497 setclientstate(Client *c, long state) {
1498 long data[] = { state, None };
1500 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1501 PropModeReplace, (unsigned char *)data, 2);
1505 sendevent(Client *c, Atom proto) {
1508 Bool exists = False;
1511 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1512 while(!exists && n--)
1513 exists = protocols[n] == proto;
1517 ev.type = ClientMessage;
1518 ev.xclient.window = c->win;
1519 ev.xclient.message_type = wmatom[WMProtocols];
1520 ev.xclient.format = 32;
1521 ev.xclient.data.l[0] = proto;
1522 ev.xclient.data.l[1] = CurrentTime;
1523 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1529 setfocus(Client *c) {
1531 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1532 sendevent(c, wmatom[WMTakeFocus]);
1536 setfullscreen(Client *c, Bool fullscreen) {
1538 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1539 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1540 c->isfullscreen = True;
1541 c->oldstate = c->isfloating;
1544 c->isfloating = True;
1545 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1546 XRaiseWindow(dpy, c->win);
1549 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1550 PropModeReplace, (unsigned char*)0, 0);
1551 c->isfullscreen = False;
1552 c->isfloating = c->oldstate;
1558 resizeclient(c, c->x, c->y, c->w, c->h);
1564 setlayout(const Arg *arg) {
1565 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1568 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1569 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1576 /* arg > 1.0 will set mfact absolutly */
1578 setmfact(const Arg *arg) {
1581 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1583 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1584 if(f < 0.1 || f > 0.9)
1592 XSetWindowAttributes wa;
1594 /* clean up any zombies immediately */
1598 screen = DefaultScreen(dpy);
1599 root = RootWindow(dpy, screen);
1601 sw = DisplayWidth(dpy, screen);
1602 sh = DisplayHeight(dpy, screen);
1603 bh = dc.h = dc.font.height + 2;
1606 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1607 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1608 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1609 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1610 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1611 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1612 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1613 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1614 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1615 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1616 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1618 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1619 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1620 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1621 /* init appearance */
1622 dc.norm[ColBorder] = getcolor(normbordercolor);
1623 dc.norm[ColBG] = getcolor(normbgcolor);
1624 dc.norm[ColFG] = getcolor(normfgcolor);
1625 dc.sel[ColBorder] = getcolor(selbordercolor);
1626 dc.sel[ColBG] = getcolor(selbgcolor);
1627 dc.sel[ColFG] = getcolor(selfgcolor);
1628 dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
1629 dc.gc = XCreateGC(dpy, root, 0, NULL);
1630 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1632 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
1636 /* EWMH support per view */
1637 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1638 PropModeReplace, (unsigned char *) netatom, NetLast);
1639 /* select for events */
1640 wa.cursor = cursor[CurNormal];
1641 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
1642 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1643 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1644 XSelectInput(dpy, root, wa.event_mask);
1649 showhide(Client *c) {
1652 if(ISVISIBLE(c)) { /* show clients top down */
1653 XMoveWindow(dpy, c->win, c->x, c->y);
1654 if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1655 resize(c, c->x, c->y, c->w, c->h, False);
1658 else { /* hide clients bottom up */
1660 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1665 sigchld(int unused) {
1666 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1667 die("Can't install SIGCHLD handler");
1668 while(0 < waitpid(-1, NULL, WNOHANG));
1672 spawn(const Arg *arg) {
1675 close(ConnectionNumber(dpy));
1677 execvp(((char **)arg->v)[0], (char **)arg->v);
1678 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1685 tag(const Arg *arg) {
1686 if(selmon->sel && arg->ui & TAGMASK) {
1687 selmon->sel->tags = arg->ui & TAGMASK;
1694 tagmon(const Arg *arg) {
1695 if(!selmon->sel || !mons->next)
1697 sendmon(selmon->sel, dirtomon(arg->i));
1701 textnw(const char *text, unsigned int len) {
1705 XmbTextExtents(dc.font.set, text, len, NULL, &r);
1708 return XTextWidth(dc.font.xfont, text, len);
1713 unsigned int i, n, h, mw, my, ty;
1716 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1721 mw = m->nmaster ? m->ww * m->mfact : 0;
1724 for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1725 if(i < m->nmaster) {
1726 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1727 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False);
1731 h = (m->wh - ty) / (n - i);
1732 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), False);
1738 togglebar(const Arg *arg) {
1739 selmon->showbar = !selmon->showbar;
1740 updatebarpos(selmon);
1741 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1746 togglefloating(const Arg *arg) {
1749 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1750 if(selmon->sel->isfloating)
1751 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1752 selmon->sel->w, selmon->sel->h, False);
1753 else if(selmon->sel->isfullscreen)
1754 setfullscreen(selmon->sel, False);
1759 toggletag(const Arg *arg) {
1760 unsigned int newtags;
1764 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1766 selmon->sel->tags = newtags;
1773 toggleview(const Arg *arg) {
1774 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1777 selmon->tagset[selmon->seltags] = newtagset;
1784 unfocus(Client *c, Bool setfocus) {
1787 grabbuttons(c, False);
1788 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
1790 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1794 unmanage(Client *c, Bool destroyed) {
1795 Monitor *m = c->mon;
1798 /* The server grab construct avoids race conditions. */
1802 wc.border_width = c->oldbw;
1804 XSetErrorHandler(xerrordummy);
1805 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1806 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1807 setclientstate(c, WithdrawnState);
1809 XSetErrorHandler(xerror);
1818 unmapnotify(XEvent *e) {
1820 XUnmapEvent *ev = &e->xunmap;
1822 if((c = wintoclient(ev->window))) {
1824 setclientstate(c, WithdrawnState);
1833 XSetWindowAttributes wa = {
1834 .override_redirect = True,
1835 .background_pixmap = ParentRelative,
1836 .event_mask = ButtonPressMask|ExposureMask
1838 for(m = mons; m; m = m->next) {
1841 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1842 CopyFromParent, DefaultVisual(dpy, screen),
1843 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1844 XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
1845 XMapRaised(dpy, m->barwin);
1850 updatebarpos(Monitor *m) {
1855 m->by = m->topbar ? m->wy : m->wy + m->wh;
1856 m->wy = m->topbar ? m->wy + bh : m->wy;
1867 if(XineramaIsActive(dpy)) {
1871 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1872 XineramaScreenInfo *unique = NULL;
1874 for(n = 0, m = mons; m; m = m->next, n++);
1875 /* only consider unique geometries as separate screens */
1876 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
1877 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
1878 for(i = 0, j = 0; i < nn; i++)
1879 if(isuniquegeom(unique, j, &info[i]))
1880 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1884 for(i = 0; i < (nn - n); i++) { /* new monitors available */
1885 for(m = mons; m && m->next; m = m->next);
1887 m->next = createmon();
1891 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
1893 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1894 || unique[i].width != m->mw || unique[i].height != m->mh))
1898 m->mx = m->wx = unique[i].x_org;
1899 m->my = m->wy = unique[i].y_org;
1900 m->mw = m->ww = unique[i].width;
1901 m->mh = m->wh = unique[i].height;
1905 else { /* less monitors available nn < n */
1906 for(i = nn; i < n; i++) {
1907 for(m = mons; m && m->next; m = m->next);
1911 m->clients = c->next;
1925 #endif /* XINERAMA */
1926 /* default monitor setup */
1930 if(mons->mw != sw || mons->mh != sh) {
1932 mons->mw = mons->ww = sw;
1933 mons->mh = mons->wh = sh;
1939 selmon = wintomon(root);
1945 updatenumlockmask(void) {
1947 XModifierKeymap *modmap;
1950 modmap = XGetModifierMapping(dpy);
1951 for(i = 0; i < 8; i++)
1952 for(j = 0; j < modmap->max_keypermod; j++)
1953 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1954 == XKeysymToKeycode(dpy, XK_Num_Lock))
1955 numlockmask = (1 << i);
1956 XFreeModifiermap(modmap);
1960 updatesizehints(Client *c) {
1964 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
1965 /* size is uninitialized, ensure that size.flags aren't used */
1967 if(size.flags & PBaseSize) {
1968 c->basew = size.base_width;
1969 c->baseh = size.base_height;
1971 else if(size.flags & PMinSize) {
1972 c->basew = size.min_width;
1973 c->baseh = size.min_height;
1976 c->basew = c->baseh = 0;
1977 if(size.flags & PResizeInc) {
1978 c->incw = size.width_inc;
1979 c->inch = size.height_inc;
1982 c->incw = c->inch = 0;
1983 if(size.flags & PMaxSize) {
1984 c->maxw = size.max_width;
1985 c->maxh = size.max_height;
1988 c->maxw = c->maxh = 0;
1989 if(size.flags & PMinSize) {
1990 c->minw = size.min_width;
1991 c->minh = size.min_height;
1993 else if(size.flags & PBaseSize) {
1994 c->minw = size.base_width;
1995 c->minh = size.base_height;
1998 c->minw = c->minh = 0;
1999 if(size.flags & PAspect) {
2000 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2001 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2004 c->maxa = c->mina = 0.0;
2005 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
2006 && c->maxw == c->minw && c->maxh == c->minh);
2010 updatetitle(Client *c) {
2011 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2012 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2013 if(c->name[0] == '\0') /* hack to mark broken clients */
2014 strcpy(c->name, broken);
2018 updatestatus(void) {
2019 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2020 strcpy(stext, "dwm-"VERSION);
2025 updatewindowtype(Client *c) {
2026 Atom state = getatomprop(c, netatom[NetWMState]);
2027 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2029 if(state == netatom[NetWMFullscreen])
2030 setfullscreen(c, True);
2031 if(wtype == netatom[NetWMWindowTypeDialog])
2032 c->isfloating = True;
2036 updatewmhints(Client *c) {
2039 if((wmh = XGetWMHints(dpy, c->win))) {
2040 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
2041 wmh->flags &= ~XUrgencyHint;
2042 XSetWMHints(dpy, c->win, wmh);
2045 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
2046 if(wmh->flags & InputHint)
2047 c->neverfocus = !wmh->input;
2049 c->neverfocus = False;
2055 view(const Arg *arg) {
2056 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2058 selmon->seltags ^= 1; /* toggle sel tagset */
2059 if(arg->ui & TAGMASK)
2060 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2066 wintoclient(Window w) {
2070 for(m = mons; m; m = m->next)
2071 for(c = m->clients; c; c = c->next)
2078 wintomon(Window w) {
2083 if(w == root && getrootptr(&x, &y))
2084 return recttomon(x, y, 1, 1);
2085 for(m = mons; m; m = m->next)
2088 if((c = wintoclient(w)))
2093 /* There's no way to check accesses to destroyed windows, thus those cases are
2094 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2095 * default error handler, which may call exit. */
2097 xerror(Display *dpy, XErrorEvent *ee) {
2098 if(ee->error_code == BadWindow
2099 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2100 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2101 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2102 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2103 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2104 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2105 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2106 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2108 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2109 ee->request_code, ee->error_code);
2110 return xerrorxlib(dpy, ee); /* may call exit */
2114 xerrordummy(Display *dpy, XErrorEvent *ee) {
2118 /* Startup Error handler to check if another window manager
2119 * is already running. */
2121 xerrorstart(Display *dpy, XErrorEvent *ee) {
2122 die("dwm: another window manager is already running\n");
2127 zoom(const Arg *arg) {
2128 Client *c = selmon->sel;
2130 if(!selmon->lt[selmon->sellt]->arrange
2131 || (selmon->sel && selmon->sel->isfloating))
2133 if(c == nexttiled(selmon->clients))
2134 if(!c || !(c = nexttiled(c->next)))
2140 main(int argc, char *argv[]) {
2141 if(argc == 2 && !strcmp("-v", argv[1]))
2142 die("dwm-"VERSION", © 2006-2012 dwm engineers, see LICENSE for details\n");
2144 die("usage: dwm [-v]\n");
2145 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2146 fputs("warning: no locale support\n", stderr);
2147 if(!(dpy = XOpenDisplay(NULL)))
2148 die("dwm: cannot open display\n");
2155 return EXIT_SUCCESS;