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, NetClientList, 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 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 if(ev->window == root) {
579 dirty = (sw != ev->width);
582 if(updategeom() || dirty) {
584 XFreePixmap(dpy, dc.drawable);
585 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
587 for(m = mons; m; m = m->next)
588 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
596 configurerequest(XEvent *e) {
599 XConfigureRequestEvent *ev = &e->xconfigurerequest;
602 if((c = wintoclient(ev->window))) {
603 if(ev->value_mask & CWBorderWidth)
604 c->bw = ev->border_width;
605 else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
607 if(ev->value_mask & CWX) {
609 c->x = m->mx + ev->x;
611 if(ev->value_mask & CWY) {
613 c->y = m->my + ev->y;
615 if(ev->value_mask & CWWidth) {
619 if(ev->value_mask & CWHeight) {
623 if((c->x + c->w) > m->mx + m->mw && c->isfloating)
624 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
625 if((c->y + c->h) > m->my + m->mh && c->isfloating)
626 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
627 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
630 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
638 wc.width = ev->width;
639 wc.height = ev->height;
640 wc.border_width = ev->border_width;
641 wc.sibling = ev->above;
642 wc.stack_mode = ev->detail;
643 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
652 if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
653 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
654 m->tagset[0] = m->tagset[1] = 1;
656 m->nmaster = nmaster;
657 m->showbar = showbar;
659 m->lt[0] = &layouts[0];
660 m->lt[1] = &layouts[1 % LENGTH(layouts)];
661 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
666 destroynotify(XEvent *e) {
668 XDestroyWindowEvent *ev = &e->xdestroywindow;
670 if((c = wintoclient(ev->window)))
678 for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
683 detachstack(Client *c) {
686 for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
689 if(c == c->mon->sel) {
690 for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
696 die(const char *errstr, ...) {
699 va_start(ap, errstr);
700 vfprintf(stderr, errstr, ap);
710 if(!(m = selmon->next))
713 else if(selmon == mons)
714 for(m = mons; m->next; m = m->next);
716 for(m = mons; m->next != selmon; m = m->next);
721 drawbar(Monitor *m) {
723 unsigned int i, occ = 0, urg = 0;
727 for(c = m->clients; c; c = c->next) {
733 for(i = 0; i < LENGTH(tags); i++) {
734 dc.w = TEXTW(tags[i]);
735 col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
736 drawtext(tags[i], col, urg & 1 << i);
737 drawsquare(m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
738 occ & 1 << i, urg & 1 << i, col);
741 dc.w = blw = TEXTW(m->ltsymbol);
742 drawtext(m->ltsymbol, dc.norm, False);
745 if(m == selmon) { /* status is only drawn on selected monitor */
752 drawtext(stext, dc.norm, False);
756 if((dc.w = dc.x - x) > bh) {
759 col = m == selmon ? dc.sel : dc.norm;
760 drawtext(m->sel->name, col, False);
761 drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
764 drawtext(NULL, dc.norm, False);
766 XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
774 for(m = mons; m; m = m->next)
779 drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
782 XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
783 x = (dc.font.ascent + dc.font.descent + 2) / 4;
785 XFillRectangle(dpy, dc.drawable, dc.gc, dc.x+1, dc.y+1, x+1, x+1);
787 XDrawRectangle(dpy, dc.drawable, dc.gc, dc.x+1, dc.y+1, x, x);
791 drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
793 int i, x, y, h, len, olen;
795 XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
796 XFillRectangle(dpy, dc.drawable, dc.gc, dc.x, dc.y, dc.w, dc.h);
800 h = dc.font.ascent + dc.font.descent;
801 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
803 /* shorten text if necessary */
804 for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
807 memcpy(buf, text, len);
809 for(i = len; i && i > len - 3; buf[--i] = '.');
810 XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
812 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
814 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
818 enternotify(XEvent *e) {
821 XCrossingEvent *ev = &e->xcrossing;
823 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
825 c = wintoclient(ev->window);
826 m = c ? c->mon : wintomon(ev->window);
828 unfocus(selmon->sel, True);
831 else if(!c || c == selmon->sel)
839 XExposeEvent *ev = &e->xexpose;
841 if(ev->count == 0 && (m = wintomon(ev->window)))
847 if(!c || !ISVISIBLE(c))
848 for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
849 /* was if(selmon->sel) */
850 if(selmon->sel && selmon->sel != c)
851 unfocus(selmon->sel, False);
859 grabbuttons(c, True);
860 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
864 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
865 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
872 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
873 XFocusChangeEvent *ev = &e->xfocus;
875 if(selmon->sel && ev->window != selmon->sel->win)
876 setfocus(selmon->sel);
880 focusmon(const Arg *arg) {
885 if((m = dirtomon(arg->i)) == selmon)
887 unfocus(selmon->sel, True);
893 focusstack(const Arg *arg) {
894 Client *c = NULL, *i;
899 for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
901 for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
904 for(i = selmon->clients; i != selmon->sel; i = i->next)
908 for(; i; i = i->next)
919 getatomprop(Client *c, Atom prop) {
922 unsigned char *p = NULL;
923 Atom da, atom = None;
925 if(XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
926 &da, &di, &dl, &dl, &p) == Success && p) {
934 getcolor(const char *colstr) {
935 Colormap cmap = DefaultColormap(dpy, screen);
938 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
939 die("error, cannot allocate color '%s'\n", colstr);
944 getrootptr(int *x, int *y) {
949 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
956 unsigned char *p = NULL;
957 unsigned long n, extra;
960 if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
961 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
970 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
975 if(!text || size == 0)
978 XGetTextProperty(dpy, w, &name, atom);
981 if(name.encoding == XA_STRING)
982 strncpy(text, (char *)name.value, size - 1);
984 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
985 strncpy(text, *list, size - 1);
986 XFreeStringList(list);
989 text[size - 1] = '\0';
995 grabbuttons(Client *c, Bool focused) {
999 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1000 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1002 for(i = 0; i < LENGTH(buttons); i++)
1003 if(buttons[i].click == ClkClientWin)
1004 for(j = 0; j < LENGTH(modifiers); j++)
1005 XGrabButton(dpy, buttons[i].button,
1006 buttons[i].mask | modifiers[j],
1007 c->win, False, BUTTONMASK,
1008 GrabModeAsync, GrabModeSync, None, None);
1011 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
1012 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1018 updatenumlockmask();
1021 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1024 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1025 for(i = 0; i < LENGTH(keys); i++)
1026 if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
1027 for(j = 0; j < LENGTH(modifiers); j++)
1028 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
1029 True, GrabModeAsync, GrabModeAsync);
1034 incnmaster(const Arg *arg) {
1035 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
1040 initfont(const char *fontstr) {
1041 char *def, **missing;
1044 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
1047 fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
1048 XFreeStringList(missing);
1051 XFontStruct **xfonts;
1054 dc.font.ascent = dc.font.descent = 0;
1055 XExtentsOfFontSet(dc.font.set);
1056 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
1058 dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
1059 dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
1064 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
1065 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
1066 die("error, cannot load font: '%s'\n", fontstr);
1067 dc.font.ascent = dc.font.xfont->ascent;
1068 dc.font.descent = dc.font.xfont->descent;
1070 dc.font.height = dc.font.ascent + dc.font.descent;
1075 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
1077 if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1078 && unique[n].width == info->width && unique[n].height == info->height)
1082 #endif /* XINERAMA */
1085 keypress(XEvent *e) {
1091 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1092 for(i = 0; i < LENGTH(keys); i++)
1093 if(keysym == keys[i].keysym
1094 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1096 keys[i].func(&(keys[i].arg));
1100 killclient(const Arg *arg) {
1103 if(!sendevent(selmon->sel, wmatom[WMDelete])) {
1105 XSetErrorHandler(xerrordummy);
1106 XSetCloseDownMode(dpy, DestroyAll);
1107 XKillClient(dpy, selmon->sel->win);
1109 XSetErrorHandler(xerror);
1115 manage(Window w, XWindowAttributes *wa) {
1116 Client *c, *t = NULL;
1117 Window trans = None;
1120 if(!(c = calloc(1, sizeof(Client))))
1121 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1124 if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1133 c->x = c->oldx = wa->x;
1134 c->y = c->oldy = wa->y;
1135 c->w = c->oldw = wa->width;
1136 c->h = c->oldh = wa->height;
1137 c->oldbw = wa->border_width;
1139 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1140 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1141 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1142 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1143 c->x = MAX(c->x, c->mon->mx);
1144 /* only fix client y-offset, if the client center might cover the bar */
1145 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1146 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1149 wc.border_width = c->bw;
1150 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1151 XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
1152 configure(c); /* propagates border_width, if size doesn't change */
1153 updatewindowtype(c);
1156 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1157 grabbuttons(c, False);
1159 c->isfloating = c->oldstate = trans != None || c->isfixed;
1161 XRaiseWindow(dpy, c->win);
1164 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1165 (unsigned char *) &(c->win), 1);
1166 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1167 setclientstate(c, NormalState);
1168 if (c->mon == selmon)
1169 unfocus(selmon->sel, False);
1172 XMapWindow(dpy, c->win);
1177 mappingnotify(XEvent *e) {
1178 XMappingEvent *ev = &e->xmapping;
1180 XRefreshKeyboardMapping(ev);
1181 if(ev->request == MappingKeyboard)
1186 maprequest(XEvent *e) {
1187 static XWindowAttributes wa;
1188 XMapRequestEvent *ev = &e->xmaprequest;
1190 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1192 if(wa.override_redirect)
1194 if(!wintoclient(ev->window))
1195 manage(ev->window, &wa);
1199 monocle(Monitor *m) {
1203 for(c = m->clients; c; c = c->next)
1206 if(n > 0) /* override layout symbol */
1207 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1208 for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
1209 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
1213 motionnotify(XEvent *e) {
1214 static Monitor *mon = NULL;
1216 XMotionEvent *ev = &e->xmotion;
1218 if(ev->window != root)
1220 if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1221 unfocus(selmon->sel, True);
1229 movemouse(const Arg *arg) {
1230 int x, y, ocx, ocy, nx, ny;
1235 if(!(c = selmon->sel))
1240 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1241 None, cursor[CurMove], CurrentTime) != GrabSuccess)
1243 if(!getrootptr(&x, &y))
1246 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1248 case ConfigureRequest:
1251 handler[ev.type](&ev);
1254 nx = ocx + (ev.xmotion.x - x);
1255 ny = ocy + (ev.xmotion.y - y);
1256 if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1257 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1258 if(abs(selmon->wx - nx) < snap)
1260 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1261 nx = selmon->wx + selmon->ww - WIDTH(c);
1262 if(abs(selmon->wy - ny) < snap)
1264 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1265 ny = selmon->wy + selmon->wh - HEIGHT(c);
1266 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1267 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1268 togglefloating(NULL);
1270 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating) {
1272 setfullscreen(c, False);
1273 resize(c, nx, ny, c->w, c->h, True);
1277 } while(ev.type != ButtonRelease);
1278 XUngrabPointer(dpy, CurrentTime);
1279 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1287 nexttiled(Client *c) {
1288 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1301 propertynotify(XEvent *e) {
1304 XPropertyEvent *ev = &e->xproperty;
1306 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1308 else if(ev->state == PropertyDelete)
1309 return; /* ignore */
1310 else if((c = wintoclient(ev->window))) {
1313 case XA_WM_TRANSIENT_FOR:
1314 if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1315 (c->isfloating = (wintoclient(trans)) != NULL))
1318 case XA_WM_NORMAL_HINTS:
1326 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1328 if(c == c->mon->sel)
1331 if(ev->atom == netatom[NetWMWindowType])
1332 updatewindowtype(c);
1337 quit(const Arg *arg) {
1342 recttomon(int x, int y, int w, int h) {
1343 Monitor *m, *r = selmon;
1346 for(m = mons; m; m = m->next)
1347 if((a = INTERSECT(x, y, w, h, m)) > area) {
1355 resize(Client *c, int x, int y, int w, int h, Bool interact) {
1356 if(applysizehints(c, &x, &y, &w, &h, interact))
1357 resizeclient(c, x, y, w, h);
1361 resizeclient(Client *c, int x, int y, int w, int h) {
1364 c->oldx = c->x; c->x = wc.x = x;
1365 c->oldy = c->y; c->y = wc.y = y;
1366 c->oldw = c->w; c->w = wc.width = w;
1367 c->oldh = c->h; c->h = wc.height = h;
1368 wc.border_width = c->bw;
1369 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1375 resizemouse(const Arg *arg) {
1382 if(!(c = selmon->sel))
1387 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1388 None, cursor[CurResize], CurrentTime) != GrabSuccess)
1390 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1392 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1394 case ConfigureRequest:
1397 handler[ev.type](&ev);
1400 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1401 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1402 if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1403 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1405 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1406 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1407 togglefloating(NULL);
1409 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating) {
1411 setfullscreen(c, False);
1412 resize(c, c->x, c->y, nw, nh, True);
1416 } while(ev.type != ButtonRelease);
1417 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1418 XUngrabPointer(dpy, CurrentTime);
1419 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1420 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1428 restack(Monitor *m) {
1436 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1437 XRaiseWindow(dpy, m->sel->win);
1438 if(m->lt[m->sellt]->arrange) {
1439 wc.stack_mode = Below;
1440 wc.sibling = m->barwin;
1441 for(c = m->stack; c; c = c->snext)
1442 if(!c->isfloating && ISVISIBLE(c)) {
1443 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1444 wc.sibling = c->win;
1448 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1454 /* main event loop */
1456 while(running && !XNextEvent(dpy, &ev))
1457 if(handler[ev.type])
1458 handler[ev.type](&ev); /* call handler */
1463 unsigned int i, num;
1464 Window d1, d2, *wins = NULL;
1465 XWindowAttributes wa;
1467 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1468 for(i = 0; i < num; i++) {
1469 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1470 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1472 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1473 manage(wins[i], &wa);
1475 for(i = 0; i < num; i++) { /* now the transients */
1476 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1478 if(XGetTransientForHint(dpy, wins[i], &d1)
1479 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1480 manage(wins[i], &wa);
1488 sendmon(Client *c, Monitor *m) {
1495 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1503 setclientstate(Client *c, long state) {
1504 long data[] = { state, None };
1506 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1507 PropModeReplace, (unsigned char *)data, 2);
1511 sendevent(Client *c, Atom proto) {
1514 Bool exists = False;
1517 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1518 while(!exists && n--)
1519 exists = protocols[n] == proto;
1523 ev.type = ClientMessage;
1524 ev.xclient.window = c->win;
1525 ev.xclient.message_type = wmatom[WMProtocols];
1526 ev.xclient.format = 32;
1527 ev.xclient.data.l[0] = proto;
1528 ev.xclient.data.l[1] = CurrentTime;
1529 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1535 setfocus(Client *c) {
1536 if(!c->neverfocus) {
1537 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1538 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1539 XA_WINDOW, 32, PropModeReplace,
1540 (unsigned char *) &(c->win), 1);
1542 sendevent(c, wmatom[WMTakeFocus]);
1546 setfullscreen(Client *c, Bool fullscreen) {
1548 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1549 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1550 c->isfullscreen = True;
1551 c->oldstate = c->isfloating;
1554 c->isfloating = True;
1555 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1556 XRaiseWindow(dpy, c->win);
1559 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1560 PropModeReplace, (unsigned char*)0, 0);
1561 c->isfullscreen = False;
1562 c->isfloating = c->oldstate;
1568 resizeclient(c, c->x, c->y, c->w, c->h);
1574 setlayout(const Arg *arg) {
1575 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1578 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1579 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1586 /* arg > 1.0 will set mfact absolutly */
1588 setmfact(const Arg *arg) {
1591 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1593 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1594 if(f < 0.1 || f > 0.9)
1602 XSetWindowAttributes wa;
1604 /* clean up any zombies immediately */
1608 screen = DefaultScreen(dpy);
1609 root = RootWindow(dpy, screen);
1611 sw = DisplayWidth(dpy, screen);
1612 sh = DisplayHeight(dpy, screen);
1613 bh = dc.h = dc.font.height + 2;
1616 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1617 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1618 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1619 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1620 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1621 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1622 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1623 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1624 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1625 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1626 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1627 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1629 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1630 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1631 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1632 /* init appearance */
1633 dc.norm[ColBorder] = getcolor(normbordercolor);
1634 dc.norm[ColBG] = getcolor(normbgcolor);
1635 dc.norm[ColFG] = getcolor(normfgcolor);
1636 dc.sel[ColBorder] = getcolor(selbordercolor);
1637 dc.sel[ColBG] = getcolor(selbgcolor);
1638 dc.sel[ColFG] = getcolor(selfgcolor);
1639 dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
1640 dc.gc = XCreateGC(dpy, root, 0, NULL);
1641 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1643 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
1647 /* EWMH support per view */
1648 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1649 PropModeReplace, (unsigned char *) netatom, NetLast);
1650 XDeleteProperty(dpy, root, netatom[NetClientList]);
1651 /* select for events */
1652 wa.cursor = cursor[CurNormal];
1653 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
1654 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1655 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1656 XSelectInput(dpy, root, wa.event_mask);
1661 showhide(Client *c) {
1664 if(ISVISIBLE(c)) { /* show clients top down */
1665 XMoveWindow(dpy, c->win, c->x, c->y);
1666 if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1667 resize(c, c->x, c->y, c->w, c->h, False);
1670 else { /* hide clients bottom up */
1672 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1677 sigchld(int unused) {
1678 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1679 die("Can't install SIGCHLD handler");
1680 while(0 < waitpid(-1, NULL, WNOHANG));
1684 spawn(const Arg *arg) {
1687 close(ConnectionNumber(dpy));
1689 execvp(((char **)arg->v)[0], (char **)arg->v);
1690 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1697 tag(const Arg *arg) {
1698 if(selmon->sel && arg->ui & TAGMASK) {
1699 selmon->sel->tags = arg->ui & TAGMASK;
1706 tagmon(const Arg *arg) {
1707 if(!selmon->sel || !mons->next)
1709 sendmon(selmon->sel, dirtomon(arg->i));
1713 textnw(const char *text, unsigned int len) {
1717 XmbTextExtents(dc.font.set, text, len, NULL, &r);
1720 return XTextWidth(dc.font.xfont, text, len);
1725 unsigned int i, n, h, mw, my, ty;
1728 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1733 mw = m->nmaster ? m->ww * m->mfact : 0;
1736 for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1737 if(i < m->nmaster) {
1738 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1739 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False);
1743 h = (m->wh - ty) / (n - i);
1744 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), False);
1750 togglebar(const Arg *arg) {
1751 selmon->showbar = !selmon->showbar;
1752 updatebarpos(selmon);
1753 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1758 togglefloating(const Arg *arg) {
1761 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1762 if(selmon->sel->isfloating)
1763 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1764 selmon->sel->w, selmon->sel->h, False);
1765 else if(selmon->sel->isfullscreen)
1766 setfullscreen(selmon->sel, False);
1771 toggletag(const Arg *arg) {
1772 unsigned int newtags;
1776 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1778 selmon->sel->tags = newtags;
1785 toggleview(const Arg *arg) {
1786 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1789 selmon->tagset[selmon->seltags] = newtagset;
1796 unfocus(Client *c, Bool setfocus) {
1799 grabbuttons(c, False);
1800 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
1802 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1803 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1808 unmanage(Client *c, Bool destroyed) {
1809 Monitor *m = c->mon;
1812 /* The server grab construct avoids race conditions. */
1816 wc.border_width = c->oldbw;
1818 XSetErrorHandler(xerrordummy);
1819 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1820 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1821 setclientstate(c, WithdrawnState);
1823 XSetErrorHandler(xerror);
1833 unmapnotify(XEvent *e) {
1835 XUnmapEvent *ev = &e->xunmap;
1837 if((c = wintoclient(ev->window))) {
1839 setclientstate(c, WithdrawnState);
1848 XSetWindowAttributes wa = {
1849 .override_redirect = True,
1850 .background_pixmap = ParentRelative,
1851 .event_mask = ButtonPressMask|ExposureMask
1853 for(m = mons; m; m = m->next) {
1856 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1857 CopyFromParent, DefaultVisual(dpy, screen),
1858 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1859 XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
1860 XMapRaised(dpy, m->barwin);
1865 updatebarpos(Monitor *m) {
1870 m->by = m->topbar ? m->wy : m->wy + m->wh;
1871 m->wy = m->topbar ? m->wy + bh : m->wy;
1878 updateclientlist() {
1882 XDeleteProperty(dpy, root, netatom[NetClientList]);
1883 for(m = mons; m; m = m->next)
1884 for(c = m->clients; c; c = c->next)
1885 XChangeProperty(dpy, root, netatom[NetClientList],
1886 XA_WINDOW, 32, PropModeAppend,
1887 (unsigned char *) &(c->win), 1);
1895 if(XineramaIsActive(dpy)) {
1899 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1900 XineramaScreenInfo *unique = NULL;
1902 for(n = 0, m = mons; m; m = m->next, n++);
1903 /* only consider unique geometries as separate screens */
1904 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
1905 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
1906 for(i = 0, j = 0; i < nn; i++)
1907 if(isuniquegeom(unique, j, &info[i]))
1908 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1912 for(i = 0; i < (nn - n); i++) { /* new monitors available */
1913 for(m = mons; m && m->next; m = m->next);
1915 m->next = createmon();
1919 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
1921 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1922 || unique[i].width != m->mw || unique[i].height != m->mh))
1926 m->mx = m->wx = unique[i].x_org;
1927 m->my = m->wy = unique[i].y_org;
1928 m->mw = m->ww = unique[i].width;
1929 m->mh = m->wh = unique[i].height;
1933 else { /* less monitors available nn < n */
1934 for(i = nn; i < n; i++) {
1935 for(m = mons; m && m->next; m = m->next);
1939 m->clients = c->next;
1953 #endif /* XINERAMA */
1954 /* default monitor setup */
1958 if(mons->mw != sw || mons->mh != sh) {
1960 mons->mw = mons->ww = sw;
1961 mons->mh = mons->wh = sh;
1967 selmon = wintomon(root);
1973 updatenumlockmask(void) {
1975 XModifierKeymap *modmap;
1978 modmap = XGetModifierMapping(dpy);
1979 for(i = 0; i < 8; i++)
1980 for(j = 0; j < modmap->max_keypermod; j++)
1981 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1982 == XKeysymToKeycode(dpy, XK_Num_Lock))
1983 numlockmask = (1 << i);
1984 XFreeModifiermap(modmap);
1988 updatesizehints(Client *c) {
1992 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
1993 /* size is uninitialized, ensure that size.flags aren't used */
1995 if(size.flags & PBaseSize) {
1996 c->basew = size.base_width;
1997 c->baseh = size.base_height;
1999 else if(size.flags & PMinSize) {
2000 c->basew = size.min_width;
2001 c->baseh = size.min_height;
2004 c->basew = c->baseh = 0;
2005 if(size.flags & PResizeInc) {
2006 c->incw = size.width_inc;
2007 c->inch = size.height_inc;
2010 c->incw = c->inch = 0;
2011 if(size.flags & PMaxSize) {
2012 c->maxw = size.max_width;
2013 c->maxh = size.max_height;
2016 c->maxw = c->maxh = 0;
2017 if(size.flags & PMinSize) {
2018 c->minw = size.min_width;
2019 c->minh = size.min_height;
2021 else if(size.flags & PBaseSize) {
2022 c->minw = size.base_width;
2023 c->minh = size.base_height;
2026 c->minw = c->minh = 0;
2027 if(size.flags & PAspect) {
2028 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2029 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2032 c->maxa = c->mina = 0.0;
2033 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
2034 && c->maxw == c->minw && c->maxh == c->minh);
2038 updatetitle(Client *c) {
2039 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2040 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2041 if(c->name[0] == '\0') /* hack to mark broken clients */
2042 strcpy(c->name, broken);
2046 updatestatus(void) {
2047 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2048 strcpy(stext, "dwm-"VERSION);
2053 updatewindowtype(Client *c) {
2054 Atom state = getatomprop(c, netatom[NetWMState]);
2055 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2057 if(state == netatom[NetWMFullscreen])
2058 setfullscreen(c, True);
2059 if(wtype == netatom[NetWMWindowTypeDialog])
2060 c->isfloating = True;
2064 updatewmhints(Client *c) {
2067 if((wmh = XGetWMHints(dpy, c->win))) {
2068 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
2069 wmh->flags &= ~XUrgencyHint;
2070 XSetWMHints(dpy, c->win, wmh);
2073 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
2074 if(wmh->flags & InputHint)
2075 c->neverfocus = !wmh->input;
2077 c->neverfocus = False;
2083 view(const Arg *arg) {
2084 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2086 selmon->seltags ^= 1; /* toggle sel tagset */
2087 if(arg->ui & TAGMASK)
2088 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2094 wintoclient(Window w) {
2098 for(m = mons; m; m = m->next)
2099 for(c = m->clients; c; c = c->next)
2106 wintomon(Window w) {
2111 if(w == root && getrootptr(&x, &y))
2112 return recttomon(x, y, 1, 1);
2113 for(m = mons; m; m = m->next)
2116 if((c = wintoclient(w)))
2121 /* There's no way to check accesses to destroyed windows, thus those cases are
2122 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2123 * default error handler, which may call exit. */
2125 xerror(Display *dpy, XErrorEvent *ee) {
2126 if(ee->error_code == BadWindow
2127 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2128 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2129 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2130 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2131 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2132 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2133 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2134 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2136 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2137 ee->request_code, ee->error_code);
2138 return xerrorxlib(dpy, ee); /* may call exit */
2142 xerrordummy(Display *dpy, XErrorEvent *ee) {
2146 /* Startup Error handler to check if another window manager
2147 * is already running. */
2149 xerrorstart(Display *dpy, XErrorEvent *ee) {
2150 die("dwm: another window manager is already running\n");
2155 zoom(const Arg *arg) {
2156 Client *c = selmon->sel;
2158 if(!selmon->lt[selmon->sellt]->arrange
2159 || (selmon->sel && selmon->sel->isfloating))
2161 if(c == nexttiled(selmon->clients))
2162 if(!c || !(c = nexttiled(c->next)))
2168 main(int argc, char *argv[]) {
2169 if(argc == 2 && !strcmp("-v", argv[1]))
2170 die("dwm-"VERSION", © 2006-2012 dwm engineers, see LICENSE for details\n");
2172 die("usage: dwm [-v]\n");
2173 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2174 fputs("warning: no locale support\n", stderr);
2175 if(!(dpy = XOpenDisplay(NULL)))
2176 die("dwm: cannot open display\n");
2183 return EXIT_SUCCESS;