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>
47 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
48 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
49 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
50 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
51 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
52 #define LENGTH(X) (sizeof X / sizeof X[0])
53 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
54 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
55 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
56 #define TAGMASK ((1 << LENGTH(tags)) - 1)
57 #define TEXTW(X) (textnw(X, strlen(X)) + dc.font.height)
60 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
61 enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
62 enum { NetSupported, NetWMName, NetWMState,
63 NetWMFullscreen, NetActiveWindow, NetWMWindowType,
64 NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */
65 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
66 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
67 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
80 void (*func)(const Arg *arg);
84 typedef struct Monitor Monitor;
85 typedef struct Client Client;
90 int oldx, oldy, oldw, oldh;
91 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
94 Bool isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
103 unsigned long norm[ColLast];
104 unsigned long sel[ColLast];
114 } DC; /* draw context */
119 void (*func)(const Arg *);
125 void (*arrange)(Monitor *);
133 int by; /* bar geometry */
134 int mx, my, mw, mh; /* screen size */
135 int wx, wy, ww, wh; /* window area */
136 unsigned int seltags;
138 unsigned int tagset[2];
151 const char *instance;
158 /* function declarations */
159 static void applyrules(Client *c);
160 static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
161 static void arrange(Monitor *m);
162 static void arrangemon(Monitor *m);
163 static void attach(Client *c);
164 static void attachstack(Client *c);
165 static void buttonpress(XEvent *e);
166 static void checkotherwm(void);
167 static void cleanup(void);
168 static void cleanupmon(Monitor *mon);
169 static void clearurgent(Client *c);
170 static void clientmessage(XEvent *e);
171 static void configure(Client *c);
172 static void configurenotify(XEvent *e);
173 static void configurerequest(XEvent *e);
174 static Monitor *createmon(void);
175 static void destroynotify(XEvent *e);
176 static void detach(Client *c);
177 static void detachstack(Client *c);
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 updateclientlist(void);
242 static void updatenumlockmask(void);
243 static void updatesizehints(Client *c);
244 static void updatestatus(void);
245 static void updatewindowtype(Client *c);
246 static void updatetitle(Client *c);
247 static void updatewmhints(Client *c);
248 static void view(const Arg *arg);
249 static Client *wintoclient(Window w);
250 static Monitor *wintomon(Window w);
251 static int xerror(Display *dpy, XErrorEvent *ee);
252 static int xerrordummy(Display *dpy, XErrorEvent *ee);
253 static int xerrorstart(Display *dpy, XErrorEvent *ee);
254 static void zoom(const Arg *arg);
257 static const char broken[] = "broken";
258 static char stext[256];
260 static int sw, sh; /* X display screen geometry width, height */
261 static int bh, blw = 0; /* bar geometry */
262 static int (*xerrorxlib)(Display *, XErrorEvent *);
263 static unsigned int numlockmask = 0;
264 static void (*handler[LASTEvent]) (XEvent *) = {
265 [ButtonPress] = buttonpress,
266 [ClientMessage] = clientmessage,
267 [ConfigureRequest] = configurerequest,
268 [ConfigureNotify] = configurenotify,
269 [DestroyNotify] = destroynotify,
270 [EnterNotify] = enternotify,
273 [KeyPress] = keypress,
274 [MappingNotify] = mappingnotify,
275 [MapRequest] = maprequest,
276 [MotionNotify] = motionnotify,
277 [PropertyNotify] = propertynotify,
278 [UnmapNotify] = unmapnotify
280 static Atom wmatom[WMLast], netatom[NetLast];
281 static Bool running = True;
282 static Cursor cursor[CurLast];
285 static Monitor *mons = NULL, *selmon = NULL;
288 /* configuration, allows nested code to access above variables */
291 /* compile-time check if all tags fit into an unsigned int bit array. */
292 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
294 /* function implementations */
296 applyrules(Client *c) {
297 const char *class, *instance;
301 XClassHint ch = { NULL, NULL };
304 c->isfloating = c->tags = 0;
305 XGetClassHint(dpy, c->win, &ch);
306 class = ch.res_class ? ch.res_class : broken;
307 instance = ch.res_name ? ch.res_name : broken;
309 for(i = 0; i < LENGTH(rules); i++) {
311 if((!r->title || strstr(c->name, r->title))
312 && (!r->class || strstr(class, r->class))
313 && (!r->instance || strstr(instance, r->instance)))
315 c->isfloating = r->isfloating;
317 for(m = mons; m && m->num != r->monitor; m = m->next);
326 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
330 applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
334 /* set minimum possible */
342 if(*x + *w + 2 * c->bw < 0)
344 if(*y + *h + 2 * c->bw < 0)
348 if(*x >= m->wx + m->ww)
349 *x = m->wx + m->ww - WIDTH(c);
350 if(*y >= m->wy + m->wh)
351 *y = m->wy + m->wh - HEIGHT(c);
352 if(*x + *w + 2 * c->bw <= m->wx)
354 if(*y + *h + 2 * c->bw <= m->wy)
361 if(resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
362 /* see last two sentences in ICCCM 4.1.2.3 */
363 baseismin = c->basew == c->minw && c->baseh == c->minh;
364 if(!baseismin) { /* temporarily remove base dimensions */
368 /* adjust for aspect limits */
369 if(c->mina > 0 && c->maxa > 0) {
370 if(c->maxa < (float)*w / *h)
371 *w = *h * c->maxa + 0.5;
372 else if(c->mina < (float)*h / *w)
373 *h = *w * c->mina + 0.5;
375 if(baseismin) { /* increment calculation requires this */
379 /* adjust for increment value */
384 /* restore base dimensions */
385 *w = MAX(*w + c->basew, c->minw);
386 *h = MAX(*h + c->baseh, c->minh);
388 *w = MIN(*w, c->maxw);
390 *h = MIN(*h, c->maxh);
392 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
396 arrange(Monitor *m) {
399 else for(m = mons; m; m = m->next)
404 } else for(m = mons; m; m = m->next)
409 arrangemon(Monitor *m) {
410 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
411 if(m->lt[m->sellt]->arrange)
412 m->lt[m->sellt]->arrange(m);
417 c->next = c->mon->clients;
422 attachstack(Client *c) {
423 c->snext = c->mon->stack;
428 buttonpress(XEvent *e) {
429 unsigned int i, x, click;
433 XButtonPressedEvent *ev = &e->xbutton;
436 /* focus monitor if necessary */
437 if((m = wintomon(ev->window)) && m != selmon) {
438 unfocus(selmon->sel, True);
442 if(ev->window == selmon->barwin) {
446 while(ev->x >= x && ++i < LENGTH(tags));
447 if(i < LENGTH(tags)) {
451 else if(ev->x < x + blw)
453 else if(ev->x > selmon->ww - TEXTW(stext))
454 click = ClkStatusText;
458 else if((c = wintoclient(ev->window))) {
460 click = ClkClientWin;
462 for(i = 0; i < LENGTH(buttons); i++)
463 if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
464 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
465 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
470 xerrorxlib = XSetErrorHandler(xerrorstart);
471 /* this causes an error if some other window manager is running */
472 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
474 XSetErrorHandler(xerror);
481 Layout foo = { "", NULL };
485 selmon->lt[selmon->sellt] = &foo;
486 for(m = mons; m; m = m->next)
488 unmanage(m->stack, False);
490 XFreeFontSet(dpy, dc.font.set);
492 XFreeFont(dpy, dc.font.xfont);
493 XUngrabKey(dpy, AnyKey, AnyModifier, root);
494 XFreePixmap(dpy, dc.drawable);
496 XFreeCursor(dpy, cursor[CurNormal]);
497 XFreeCursor(dpy, cursor[CurResize]);
498 XFreeCursor(dpy, cursor[CurMove]);
502 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
503 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
507 cleanupmon(Monitor *mon) {
513 for(m = mons; m && m->next != mon; m = m->next);
516 XUnmapWindow(dpy, mon->barwin);
517 XDestroyWindow(dpy, mon->barwin);
522 clearurgent(Client *c) {
526 if(!(wmh = XGetWMHints(dpy, c->win)))
528 wmh->flags &= ~XUrgencyHint;
529 XSetWMHints(dpy, c->win, wmh);
534 clientmessage(XEvent *e) {
535 XClientMessageEvent *cme = &e->xclient;
536 Client *c = wintoclient(cme->window);
540 if(cme->message_type == netatom[NetWMState]) {
541 if(cme->data.l[1] == netatom[NetWMFullscreen] || cme->data.l[2] == netatom[NetWMFullscreen])
542 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
543 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
545 else if(cme->message_type == netatom[NetActiveWindow]) {
547 c->mon->seltags ^= 1;
548 c->mon->tagset[c->mon->seltags] = c->tags;
555 configure(Client *c) {
558 ce.type = ConfigureNotify;
566 ce.border_width = c->bw;
568 ce.override_redirect = False;
569 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
573 configurenotify(XEvent *e) {
575 XConfigureEvent *ev = &e->xconfigure;
578 // TODO: updategeom handling sucks, needs to be simplified
579 if(ev->window == root) {
580 dirty = (sw != ev->width || sh != ev->height);
583 if(updategeom() || dirty) {
585 XFreePixmap(dpy, dc.drawable);
586 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
588 for(m = mons; m; m = m->next)
589 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
597 configurerequest(XEvent *e) {
600 XConfigureRequestEvent *ev = &e->xconfigurerequest;
603 if((c = wintoclient(ev->window))) {
604 if(ev->value_mask & CWBorderWidth)
605 c->bw = ev->border_width;
606 else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
608 if(ev->value_mask & CWX) {
610 c->x = m->mx + ev->x;
612 if(ev->value_mask & CWY) {
614 c->y = m->my + ev->y;
616 if(ev->value_mask & CWWidth) {
620 if(ev->value_mask & CWHeight) {
624 if((c->x + c->w) > m->mx + m->mw && c->isfloating)
625 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
626 if((c->y + c->h) > m->my + m->mh && c->isfloating)
627 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
628 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
631 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
639 wc.width = ev->width;
640 wc.height = ev->height;
641 wc.border_width = ev->border_width;
642 wc.sibling = ev->above;
643 wc.stack_mode = ev->detail;
644 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
653 if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
654 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
655 m->tagset[0] = m->tagset[1] = 1;
657 m->nmaster = nmaster;
658 m->showbar = showbar;
660 m->lt[0] = &layouts[0];
661 m->lt[1] = &layouts[1 % LENGTH(layouts)];
662 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
667 destroynotify(XEvent *e) {
669 XDestroyWindowEvent *ev = &e->xdestroywindow;
671 if((c = wintoclient(ev->window)))
679 for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
684 detachstack(Client *c) {
687 for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
690 if(c == c->mon->sel) {
691 for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
701 if(!(m = selmon->next))
704 else if(selmon == mons)
705 for(m = mons; m->next; m = m->next);
707 for(m = mons; m->next != selmon; m = m->next);
712 drawbar(Monitor *m) {
714 unsigned int i, occ = 0, urg = 0;
718 for(c = m->clients; c; c = c->next) {
724 for(i = 0; i < LENGTH(tags); i++) {
725 dc.w = TEXTW(tags[i]);
726 col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
727 drawtext(tags[i], col, urg & 1 << i);
728 drawsquare(m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
729 occ & 1 << i, urg & 1 << i, col);
732 dc.w = blw = TEXTW(m->ltsymbol);
733 drawtext(m->ltsymbol, dc.norm, False);
736 if(m == selmon) { /* status is only drawn on selected monitor */
743 drawtext(stext, dc.norm, False);
747 if((dc.w = dc.x - x) > bh) {
750 col = m == selmon ? dc.sel : dc.norm;
751 drawtext(m->sel->name, col, False);
752 drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
755 drawtext(NULL, dc.norm, False);
757 XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
765 for(m = mons; m; m = m->next)
770 drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
773 XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
774 x = (dc.font.ascent + dc.font.descent + 2) / 4;
776 XFillRectangle(dpy, dc.drawable, dc.gc, dc.x+1, dc.y+1, x+1, x+1);
778 XDrawRectangle(dpy, dc.drawable, dc.gc, dc.x+1, dc.y+1, x, x);
782 drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
784 int i, x, y, h, len, olen;
786 XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
787 XFillRectangle(dpy, dc.drawable, dc.gc, dc.x, dc.y, dc.w, dc.h);
791 h = dc.font.ascent + dc.font.descent;
792 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
794 /* shorten text if necessary */
795 for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
798 memcpy(buf, text, len);
800 for(i = len; i && i > len - 3; buf[--i] = '.');
801 XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
803 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
805 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
809 enternotify(XEvent *e) {
812 XCrossingEvent *ev = &e->xcrossing;
814 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
816 c = wintoclient(ev->window);
817 m = c ? c->mon : wintomon(ev->window);
819 unfocus(selmon->sel, True);
822 else if(!c || c == selmon->sel)
830 XExposeEvent *ev = &e->xexpose;
832 if(ev->count == 0 && (m = wintomon(ev->window)))
838 if(!c || !ISVISIBLE(c))
839 for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
840 /* was if(selmon->sel) */
841 if(selmon->sel && selmon->sel != c)
842 unfocus(selmon->sel, False);
850 grabbuttons(c, True);
851 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
855 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
856 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
863 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
864 XFocusChangeEvent *ev = &e->xfocus;
866 if(selmon->sel && ev->window != selmon->sel->win)
867 setfocus(selmon->sel);
871 focusmon(const Arg *arg) {
876 if((m = dirtomon(arg->i)) == selmon)
878 unfocus(selmon->sel, False); /* s/True/False/ fixes input focus issues
879 in gedit and anjuta */
885 focusstack(const Arg *arg) {
886 Client *c = NULL, *i;
891 for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
893 for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
896 for(i = selmon->clients; i != selmon->sel; i = i->next)
900 for(; i; i = i->next)
911 getatomprop(Client *c, Atom prop) {
914 unsigned char *p = NULL;
915 Atom da, atom = None;
917 if(XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
918 &da, &di, &dl, &dl, &p) == Success && p) {
926 getcolor(const char *colstr) {
927 Colormap cmap = DefaultColormap(dpy, screen);
930 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
931 die("error, cannot allocate color '%s'\n", colstr);
936 getrootptr(int *x, int *y) {
941 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
948 unsigned char *p = NULL;
949 unsigned long n, extra;
952 if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
953 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
962 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
967 if(!text || size == 0)
970 XGetTextProperty(dpy, w, &name, atom);
973 if(name.encoding == XA_STRING)
974 strncpy(text, (char *)name.value, size - 1);
976 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
977 strncpy(text, *list, size - 1);
978 XFreeStringList(list);
981 text[size - 1] = '\0';
987 grabbuttons(Client *c, Bool focused) {
991 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
992 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
994 for(i = 0; i < LENGTH(buttons); i++)
995 if(buttons[i].click == ClkClientWin)
996 for(j = 0; j < LENGTH(modifiers); j++)
997 XGrabButton(dpy, buttons[i].button,
998 buttons[i].mask | modifiers[j],
999 c->win, False, BUTTONMASK,
1000 GrabModeAsync, GrabModeSync, None, None);
1003 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
1004 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1010 updatenumlockmask();
1013 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1016 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1017 for(i = 0; i < LENGTH(keys); i++)
1018 if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
1019 for(j = 0; j < LENGTH(modifiers); j++)
1020 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
1021 True, GrabModeAsync, GrabModeAsync);
1026 incnmaster(const Arg *arg) {
1027 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
1032 initfont(const char *fontstr) {
1033 char *def, **missing;
1036 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
1039 fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
1040 XFreeStringList(missing);
1043 XFontStruct **xfonts;
1046 dc.font.ascent = dc.font.descent = 0;
1047 XExtentsOfFontSet(dc.font.set);
1048 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
1050 dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
1051 dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
1056 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
1057 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
1058 die("error, cannot load font: '%s'\n", fontstr);
1059 dc.font.ascent = dc.font.xfont->ascent;
1060 dc.font.descent = dc.font.xfont->descent;
1062 dc.font.height = dc.font.ascent + dc.font.descent;
1067 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
1069 if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1070 && unique[n].width == info->width && unique[n].height == info->height)
1074 #endif /* XINERAMA */
1077 keypress(XEvent *e) {
1083 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1084 for(i = 0; i < LENGTH(keys); i++)
1085 if(keysym == keys[i].keysym
1086 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1088 keys[i].func(&(keys[i].arg));
1092 killclient(const Arg *arg) {
1095 if(!sendevent(selmon->sel, wmatom[WMDelete])) {
1097 XSetErrorHandler(xerrordummy);
1098 XSetCloseDownMode(dpy, DestroyAll);
1099 XKillClient(dpy, selmon->sel->win);
1101 XSetErrorHandler(xerror);
1107 manage(Window w, XWindowAttributes *wa) {
1108 Client *c, *t = NULL;
1109 Window trans = None;
1112 if(!(c = calloc(1, sizeof(Client))))
1113 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1116 if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1125 c->x = c->oldx = wa->x;
1126 c->y = c->oldy = wa->y;
1127 c->w = c->oldw = wa->width;
1128 c->h = c->oldh = wa->height;
1129 c->oldbw = wa->border_width;
1131 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1132 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1133 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1134 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1135 c->x = MAX(c->x, c->mon->mx);
1136 /* only fix client y-offset, if the client center might cover the bar */
1137 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1138 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1141 wc.border_width = c->bw;
1142 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1143 XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
1144 configure(c); /* propagates border_width, if size doesn't change */
1145 updatewindowtype(c);
1148 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1149 grabbuttons(c, False);
1151 c->isfloating = c->oldstate = trans != None || c->isfixed;
1153 XRaiseWindow(dpy, c->win);
1156 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1157 (unsigned char *) &(c->win), 1);
1158 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1159 setclientstate(c, NormalState);
1160 if (c->mon == selmon)
1161 unfocus(selmon->sel, False);
1164 XMapWindow(dpy, c->win);
1169 mappingnotify(XEvent *e) {
1170 XMappingEvent *ev = &e->xmapping;
1172 XRefreshKeyboardMapping(ev);
1173 if(ev->request == MappingKeyboard)
1178 maprequest(XEvent *e) {
1179 static XWindowAttributes wa;
1180 XMapRequestEvent *ev = &e->xmaprequest;
1182 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1184 if(wa.override_redirect)
1186 if(!wintoclient(ev->window))
1187 manage(ev->window, &wa);
1191 monocle(Monitor *m) {
1195 for(c = m->clients; c; c = c->next)
1198 if(n > 0) /* override layout symbol */
1199 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1200 for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
1201 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
1205 motionnotify(XEvent *e) {
1206 static Monitor *mon = NULL;
1208 XMotionEvent *ev = &e->xmotion;
1210 if(ev->window != root)
1212 if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1213 unfocus(selmon->sel, True);
1221 movemouse(const Arg *arg) {
1222 int x, y, ocx, ocy, nx, ny;
1227 if(!(c = selmon->sel))
1229 if(c->isfullscreen) /* no support moving fullscreen windows by mouse */
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)
1265 resize(c, nx, ny, c->w, c->h, True);
1268 } while(ev.type != ButtonRelease);
1269 XUngrabPointer(dpy, CurrentTime);
1270 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1278 nexttiled(Client *c) {
1279 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1292 propertynotify(XEvent *e) {
1295 XPropertyEvent *ev = &e->xproperty;
1297 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1299 else if(ev->state == PropertyDelete)
1300 return; /* ignore */
1301 else if((c = wintoclient(ev->window))) {
1304 case XA_WM_TRANSIENT_FOR:
1305 if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1306 (c->isfloating = (wintoclient(trans)) != NULL))
1309 case XA_WM_NORMAL_HINTS:
1317 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1319 if(c == c->mon->sel)
1322 if(ev->atom == netatom[NetWMWindowType])
1323 updatewindowtype(c);
1328 quit(const Arg *arg) {
1333 recttomon(int x, int y, int w, int h) {
1334 Monitor *m, *r = selmon;
1337 for(m = mons; m; m = m->next)
1338 if((a = INTERSECT(x, y, w, h, m)) > area) {
1346 resize(Client *c, int x, int y, int w, int h, Bool interact) {
1347 if(applysizehints(c, &x, &y, &w, &h, interact))
1348 resizeclient(c, x, y, w, h);
1352 resizeclient(Client *c, int x, int y, int w, int h) {
1355 c->oldx = c->x; c->x = wc.x = x;
1356 c->oldy = c->y; c->y = wc.y = y;
1357 c->oldw = c->w; c->w = wc.width = w;
1358 c->oldh = c->h; c->h = wc.height = h;
1359 wc.border_width = c->bw;
1360 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1366 resizemouse(const Arg *arg) {
1373 if(!(c = selmon->sel))
1375 if(c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1380 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1381 None, cursor[CurResize], CurrentTime) != GrabSuccess)
1383 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1385 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1387 case ConfigureRequest:
1390 handler[ev.type](&ev);
1393 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1394 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1395 if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1396 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1398 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1399 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1400 togglefloating(NULL);
1402 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1403 resize(c, c->x, c->y, nw, nh, True);
1406 } while(ev.type != ButtonRelease);
1407 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1408 XUngrabPointer(dpy, CurrentTime);
1409 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1410 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1418 restack(Monitor *m) {
1426 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1427 XRaiseWindow(dpy, m->sel->win);
1428 if(m->lt[m->sellt]->arrange) {
1429 wc.stack_mode = Below;
1430 wc.sibling = m->barwin;
1431 for(c = m->stack; c; c = c->snext)
1432 if(!c->isfloating && ISVISIBLE(c)) {
1433 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1434 wc.sibling = c->win;
1438 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1444 /* main event loop */
1446 while(running && !XNextEvent(dpy, &ev))
1447 if(handler[ev.type])
1448 handler[ev.type](&ev); /* call handler */
1453 unsigned int i, num;
1454 Window d1, d2, *wins = NULL;
1455 XWindowAttributes wa;
1457 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1458 for(i = 0; i < num; i++) {
1459 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1460 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1462 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1463 manage(wins[i], &wa);
1465 for(i = 0; i < num; i++) { /* now the transients */
1466 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1468 if(XGetTransientForHint(dpy, wins[i], &d1)
1469 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1470 manage(wins[i], &wa);
1478 sendmon(Client *c, Monitor *m) {
1485 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1493 setclientstate(Client *c, long state) {
1494 long data[] = { state, None };
1496 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1497 PropModeReplace, (unsigned char *)data, 2);
1501 sendevent(Client *c, Atom proto) {
1504 Bool exists = False;
1507 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1508 while(!exists && n--)
1509 exists = protocols[n] == proto;
1513 ev.type = ClientMessage;
1514 ev.xclient.window = c->win;
1515 ev.xclient.message_type = wmatom[WMProtocols];
1516 ev.xclient.format = 32;
1517 ev.xclient.data.l[0] = proto;
1518 ev.xclient.data.l[1] = CurrentTime;
1519 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1525 setfocus(Client *c) {
1526 if(!c->neverfocus) {
1527 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1528 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1529 XA_WINDOW, 32, PropModeReplace,
1530 (unsigned char *) &(c->win), 1);
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);
1617 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1619 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1620 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1621 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1622 /* init appearance */
1623 dc.norm[ColBorder] = getcolor(normbordercolor);
1624 dc.norm[ColBG] = getcolor(normbgcolor);
1625 dc.norm[ColFG] = getcolor(normfgcolor);
1626 dc.sel[ColBorder] = getcolor(selbordercolor);
1627 dc.sel[ColBG] = getcolor(selbgcolor);
1628 dc.sel[ColFG] = getcolor(selfgcolor);
1629 dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
1630 dc.gc = XCreateGC(dpy, root, 0, NULL);
1631 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1633 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
1637 /* EWMH support per view */
1638 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1639 PropModeReplace, (unsigned char *) netatom, NetLast);
1640 XDeleteProperty(dpy, root, netatom[NetClientList]);
1641 /* select for events */
1642 wa.cursor = cursor[CurNormal];
1643 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
1644 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1645 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1646 XSelectInput(dpy, root, wa.event_mask);
1651 showhide(Client *c) {
1654 if(ISVISIBLE(c)) { /* show clients top down */
1655 XMoveWindow(dpy, c->win, c->x, c->y);
1656 if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1657 resize(c, c->x, c->y, c->w, c->h, False);
1660 else { /* hide clients bottom up */
1662 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1667 sigchld(int unused) {
1668 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1669 die("Can't install SIGCHLD handler");
1670 while(0 < waitpid(-1, NULL, WNOHANG));
1674 spawn(const Arg *arg) {
1677 close(ConnectionNumber(dpy));
1679 execvp(((char **)arg->v)[0], (char **)arg->v);
1680 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1687 tag(const Arg *arg) {
1688 if(selmon->sel && arg->ui & TAGMASK) {
1689 selmon->sel->tags = arg->ui & TAGMASK;
1696 tagmon(const Arg *arg) {
1697 if(!selmon->sel || !mons->next)
1699 sendmon(selmon->sel, dirtomon(arg->i));
1703 textnw(const char *text, unsigned int len) {
1707 XmbTextExtents(dc.font.set, text, len, NULL, &r);
1710 return XTextWidth(dc.font.xfont, text, len);
1715 unsigned int i, n, h, mw, my, ty;
1718 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1723 mw = m->nmaster ? m->ww * m->mfact : 0;
1726 for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1727 if(i < m->nmaster) {
1728 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1729 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False);
1733 h = (m->wh - ty) / (n - i);
1734 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), False);
1740 togglebar(const Arg *arg) {
1741 selmon->showbar = !selmon->showbar;
1742 updatebarpos(selmon);
1743 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1748 togglefloating(const Arg *arg) {
1751 if(selmon->sel->isfullscreen) /* no support for fullscreen windows */
1753 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1754 if(selmon->sel->isfloating)
1755 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1756 selmon->sel->w, selmon->sel->h, False);
1761 toggletag(const Arg *arg) {
1762 unsigned int newtags;
1766 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1768 selmon->sel->tags = newtags;
1775 toggleview(const Arg *arg) {
1776 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1779 selmon->tagset[selmon->seltags] = newtagset;
1786 unfocus(Client *c, Bool setfocus) {
1789 grabbuttons(c, False);
1790 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
1792 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1793 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1798 unmanage(Client *c, Bool destroyed) {
1799 Monitor *m = c->mon;
1802 /* The server grab construct avoids race conditions. */
1806 wc.border_width = c->oldbw;
1808 XSetErrorHandler(xerrordummy);
1809 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1810 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1811 setclientstate(c, WithdrawnState);
1813 XSetErrorHandler(xerror);
1823 unmapnotify(XEvent *e) {
1825 XUnmapEvent *ev = &e->xunmap;
1827 if((c = wintoclient(ev->window))) {
1829 setclientstate(c, WithdrawnState);
1838 XSetWindowAttributes wa = {
1839 .override_redirect = True,
1840 .background_pixmap = ParentRelative,
1841 .event_mask = ButtonPressMask|ExposureMask
1843 for(m = mons; m; m = m->next) {
1846 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1847 CopyFromParent, DefaultVisual(dpy, screen),
1848 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1849 XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
1850 XMapRaised(dpy, m->barwin);
1855 updatebarpos(Monitor *m) {
1860 m->by = m->topbar ? m->wy : m->wy + m->wh;
1861 m->wy = m->topbar ? m->wy + bh : m->wy;
1868 updateclientlist() {
1872 XDeleteProperty(dpy, root, netatom[NetClientList]);
1873 for(m = mons; m; m = m->next)
1874 for(c = m->clients; c; c = c->next)
1875 XChangeProperty(dpy, root, netatom[NetClientList],
1876 XA_WINDOW, 32, PropModeAppend,
1877 (unsigned char *) &(c->win), 1);
1885 if(XineramaIsActive(dpy)) {
1889 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1890 XineramaScreenInfo *unique = NULL;
1892 for(n = 0, m = mons; m; m = m->next, n++);
1893 /* only consider unique geometries as separate screens */
1894 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
1895 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
1896 for(i = 0, j = 0; i < nn; i++)
1897 if(isuniquegeom(unique, j, &info[i]))
1898 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1902 for(i = 0; i < (nn - n); i++) { /* new monitors available */
1903 for(m = mons; m && m->next; m = m->next);
1905 m->next = createmon();
1909 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
1911 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1912 || unique[i].width != m->mw || unique[i].height != m->mh))
1916 m->mx = m->wx = unique[i].x_org;
1917 m->my = m->wy = unique[i].y_org;
1918 m->mw = m->ww = unique[i].width;
1919 m->mh = m->wh = unique[i].height;
1923 else { /* less monitors available nn < n */
1924 for(i = nn; i < n; i++) {
1925 for(m = mons; m && m->next; m = m->next);
1929 m->clients = c->next;
1943 #endif /* XINERAMA */
1944 /* default monitor setup */
1948 if(mons->mw != sw || mons->mh != sh) {
1950 mons->mw = mons->ww = sw;
1951 mons->mh = mons->wh = sh;
1957 selmon = wintomon(root);
1963 updatenumlockmask(void) {
1965 XModifierKeymap *modmap;
1968 modmap = XGetModifierMapping(dpy);
1969 for(i = 0; i < 8; i++)
1970 for(j = 0; j < modmap->max_keypermod; j++)
1971 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1972 == XKeysymToKeycode(dpy, XK_Num_Lock))
1973 numlockmask = (1 << i);
1974 XFreeModifiermap(modmap);
1978 updatesizehints(Client *c) {
1982 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
1983 /* size is uninitialized, ensure that size.flags aren't used */
1985 if(size.flags & PBaseSize) {
1986 c->basew = size.base_width;
1987 c->baseh = size.base_height;
1989 else if(size.flags & PMinSize) {
1990 c->basew = size.min_width;
1991 c->baseh = size.min_height;
1994 c->basew = c->baseh = 0;
1995 if(size.flags & PResizeInc) {
1996 c->incw = size.width_inc;
1997 c->inch = size.height_inc;
2000 c->incw = c->inch = 0;
2001 if(size.flags & PMaxSize) {
2002 c->maxw = size.max_width;
2003 c->maxh = size.max_height;
2006 c->maxw = c->maxh = 0;
2007 if(size.flags & PMinSize) {
2008 c->minw = size.min_width;
2009 c->minh = size.min_height;
2011 else if(size.flags & PBaseSize) {
2012 c->minw = size.base_width;
2013 c->minh = size.base_height;
2016 c->minw = c->minh = 0;
2017 if(size.flags & PAspect) {
2018 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2019 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2022 c->maxa = c->mina = 0.0;
2023 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
2024 && c->maxw == c->minw && c->maxh == c->minh);
2028 updatetitle(Client *c) {
2029 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2030 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2031 if(c->name[0] == '\0') /* hack to mark broken clients */
2032 strcpy(c->name, broken);
2036 updatestatus(void) {
2037 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2038 strcpy(stext, "dwm-"VERSION);
2043 updatewindowtype(Client *c) {
2044 Atom state = getatomprop(c, netatom[NetWMState]);
2045 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2047 if(state == netatom[NetWMFullscreen])
2048 setfullscreen(c, True);
2049 if(wtype == netatom[NetWMWindowTypeDialog])
2050 c->isfloating = True;
2054 updatewmhints(Client *c) {
2057 if((wmh = XGetWMHints(dpy, c->win))) {
2058 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
2059 wmh->flags &= ~XUrgencyHint;
2060 XSetWMHints(dpy, c->win, wmh);
2063 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
2064 if(wmh->flags & InputHint)
2065 c->neverfocus = !wmh->input;
2067 c->neverfocus = False;
2073 view(const Arg *arg) {
2074 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2076 selmon->seltags ^= 1; /* toggle sel tagset */
2077 if(arg->ui & TAGMASK)
2078 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2084 wintoclient(Window w) {
2088 for(m = mons; m; m = m->next)
2089 for(c = m->clients; c; c = c->next)
2096 wintomon(Window w) {
2101 if(w == root && getrootptr(&x, &y))
2102 return recttomon(x, y, 1, 1);
2103 for(m = mons; m; m = m->next)
2106 if((c = wintoclient(w)))
2111 /* There's no way to check accesses to destroyed windows, thus those cases are
2112 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2113 * default error handler, which may call exit. */
2115 xerror(Display *dpy, XErrorEvent *ee) {
2116 if(ee->error_code == BadWindow
2117 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2118 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2119 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2120 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2121 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2122 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2123 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2124 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2126 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2127 ee->request_code, ee->error_code);
2128 return xerrorxlib(dpy, ee); /* may call exit */
2132 xerrordummy(Display *dpy, XErrorEvent *ee) {
2136 /* Startup Error handler to check if another window manager
2137 * is already running. */
2139 xerrorstart(Display *dpy, XErrorEvent *ee) {
2140 die("dwm: another window manager is already running\n");
2145 zoom(const Arg *arg) {
2146 Client *c = selmon->sel;
2148 if(!selmon->lt[selmon->sellt]->arrange
2149 || (selmon->sel && selmon->sel->isfloating))
2151 if(c == nexttiled(selmon->clients))
2152 if(!c || !(c = nexttiled(c->next)))
2158 main(int argc, char *argv[]) {
2159 if(argc == 2 && !strcmp("-v", argv[1]))
2160 die("dwm-"VERSION", © 2006-2012 dwm engineers, see LICENSE for details\n");
2162 die("usage: dwm [-v]\n");
2163 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2164 fputs("warning: no locale support\n", stderr);
2165 if(!(dpy = XOpenDisplay(NULL)))
2166 die("dwm: cannot open display\n");
2173 return EXIT_SUCCESS;