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>
39 #include <X11/XKBlib.h>
40 #include <fontconfig/fontconfig.h>
41 #include <X11/Xft/Xft.h>
43 #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 MAX(A, B) ((A) > (B) ? (A) : (B))
54 #define MIN(A, B) ((A) < (B) ? (A) : (B))
55 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
56 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
57 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
58 #define TAGMASK ((1 << LENGTH(tags)) - 1)
59 #define TEXTW(X) (textnw(X, strlen(X)) + dc.font.height)
62 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
63 enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
64 enum { NetSupported, NetWMName, NetWMState,
65 NetWMFullscreen, NetActiveWindow, NetWMWindowType,
66 NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */
67 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
68 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
69 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
82 void (*func)(const Arg *arg);
86 typedef struct Monitor Monitor;
87 typedef struct Client Client;
92 int oldx, oldy, oldw, oldh;
93 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
96 Bool isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
105 XftColor norm[ColLast];
106 XftColor sel[ColLast];
115 } DC; /* draw context */
120 void (*func)(const Arg *);
126 void (*arrange)(Monitor *);
134 int by; /* bar geometry */
135 int mx, my, mw, mh; /* screen size */
136 int wx, wy, ww, wh; /* window area */
137 unsigned int seltags;
139 unsigned int tagset[2];
152 const char *instance;
159 /* function declarations */
160 static void applyrules(Client *c);
161 static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
162 static void arrange(Monitor *m);
163 static void arrangemon(Monitor *m);
164 static void attach(Client *c);
165 static void attachstack(Client *c);
166 static void buttonpress(XEvent *e);
167 static void checkotherwm(void);
168 static void cleanup(void);
169 static void cleanupmon(Monitor *mon);
170 static void clearurgent(Client *c);
171 static void clientmessage(XEvent *e);
172 static void configure(Client *c);
173 static void configurenotify(XEvent *e);
174 static void configurerequest(XEvent *e);
175 static Monitor *createmon(void);
176 static void destroynotify(XEvent *e);
177 static void detach(Client *c);
178 static void detachstack(Client *c);
179 static void die(const char *errstr, ...);
180 static Monitor *dirtomon(int dir);
181 static void drawbar(Monitor *m);
182 static void drawbars(void);
183 static void drawsquare(Bool filled, Bool empty, Bool invert, XftColor col[ColLast]);
184 static void drawtext(const char *text, XftColor col[ColLast], Bool invert);
185 static void enternotify(XEvent *e);
186 static void expose(XEvent *e);
187 static void focus(Client *c);
188 static void focusin(XEvent *e);
189 static void focusmon(const Arg *arg);
190 static void focusstack(const Arg *arg);
191 static XftColor getcolor(const char *colstr);
192 static Bool getrootptr(int *x, int *y);
193 static long getstate(Window w);
194 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
195 static void grabbuttons(Client *c, Bool focused);
196 static void grabkeys(void);
197 static void incnmaster(const Arg *arg);
198 static void initfont(const char *fontstr);
199 static void keypress(XEvent *e);
200 static void killclient(const Arg *arg);
201 static void manage(Window w, XWindowAttributes *wa);
202 static void mappingnotify(XEvent *e);
203 static void maprequest(XEvent *e);
204 static void monocle(Monitor *m);
205 static void motionnotify(XEvent *e);
206 static void movemouse(const Arg *arg);
207 static Client *nexttiled(Client *c);
208 static void pop(Client *);
209 static void propertynotify(XEvent *e);
210 static void quit(const Arg *arg);
211 static Monitor *recttomon(int x, int y, int w, int h);
212 static void resize(Client *c, int x, int y, int w, int h, Bool interact);
213 static void resizeclient(Client *c, int x, int y, int w, int h);
214 static void resizemouse(const Arg *arg);
215 static void restack(Monitor *m);
216 static void run(void);
217 static void scan(void);
218 static Bool sendevent(Client *c, Atom proto);
219 static void sendmon(Client *c, Monitor *m);
220 static void setclientstate(Client *c, long state);
221 static void setfocus(Client *c);
222 static void setfullscreen(Client *c, Bool fullscreen);
223 static void setlayout(const Arg *arg);
224 static void setmfact(const Arg *arg);
225 static void setup(void);
226 static void showhide(Client *c);
227 static void sigchld(int unused);
228 static void spawn(const Arg *arg);
229 static void tag(const Arg *arg);
230 static void tagmon(const Arg *arg);
231 static int textnw(const char *text, unsigned int len);
232 static void tile(Monitor *);
233 static void togglebar(const Arg *arg);
234 static void togglefloating(const Arg *arg);
235 static void toggletag(const Arg *arg);
236 static void toggleview(const Arg *arg);
237 static void unfocus(Client *c, Bool setfocus);
238 static void unmanage(Client *c, Bool destroyed);
239 static void unmapnotify(XEvent *e);
240 static Bool updategeom(void);
241 static void updatebarpos(Monitor *m);
242 static void updatebars(void);
243 static void updateclientlist(void);
244 static void updatenumlockmask(void);
245 static void updatesizehints(Client *c);
246 static void updatestatus(void);
247 static void updatewindowtype(Client *c);
248 static void updatetitle(Client *c);
249 static void updatewmhints(Client *c);
250 static void view(const Arg *arg);
251 static Client *wintoclient(Window w);
252 static Monitor *wintomon(Window w);
253 static int xerror(Display *dpy, XErrorEvent *ee);
254 static int xerrordummy(Display *dpy, XErrorEvent *ee);
255 static int xerrorstart(Display *dpy, XErrorEvent *ee);
256 static void zoom(const Arg *arg);
259 static const char broken[] = "broken";
260 static char stext[256];
262 static int sw, sh; /* X display screen geometry width, height */
263 static int bh, blw = 0; /* bar geometry */
264 static int (*xerrorxlib)(Display *, XErrorEvent *);
265 static unsigned int numlockmask = 0;
266 static void (*handler[LASTEvent]) (XEvent *) = {
267 [ButtonPress] = buttonpress,
268 [ClientMessage] = clientmessage,
269 [ConfigureRequest] = configurerequest,
270 [ConfigureNotify] = configurenotify,
271 [DestroyNotify] = destroynotify,
272 [EnterNotify] = enternotify,
275 [KeyPress] = keypress,
276 [MappingNotify] = mappingnotify,
277 [MapRequest] = maprequest,
278 [MotionNotify] = motionnotify,
279 [PropertyNotify] = propertynotify,
280 [UnmapNotify] = unmapnotify
282 static Atom wmatom[WMLast], netatom[NetLast];
283 static Bool running = True;
284 static Cursor cursor[CurLast];
287 static Monitor *mons = NULL, *selmon = NULL;
290 /* configuration, allows nested code to access above variables */
293 /* compile-time check if all tags fit into an unsigned int bit array. */
294 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
296 /* function implementations */
298 applyrules(Client *c) {
299 const char *class, *instance;
303 XClassHint ch = { NULL, NULL };
306 c->isfloating = c->tags = 0;
307 XGetClassHint(dpy, c->win, &ch);
308 class = ch.res_class ? ch.res_class : broken;
309 instance = ch.res_name ? ch.res_name : broken;
311 for(i = 0; i < LENGTH(rules); i++) {
313 if((!r->title || strstr(c->name, r->title))
314 && (!r->class || strstr(class, r->class))
315 && (!r->instance || strstr(instance, r->instance)))
317 c->isfloating = r->isfloating;
319 for(m = mons; m && m->num != r->monitor; m = m->next);
328 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
332 applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
336 /* set minimum possible */
344 if(*x + *w + 2 * c->bw < 0)
346 if(*y + *h + 2 * c->bw < 0)
350 if(*x >= m->wx + m->ww)
351 *x = m->wx + m->ww - WIDTH(c);
352 if(*y >= m->wy + m->wh)
353 *y = m->wy + m->wh - HEIGHT(c);
354 if(*x + *w + 2 * c->bw <= m->wx)
356 if(*y + *h + 2 * c->bw <= m->wy)
363 if(resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
364 /* see last two sentences in ICCCM 4.1.2.3 */
365 baseismin = c->basew == c->minw && c->baseh == c->minh;
366 if(!baseismin) { /* temporarily remove base dimensions */
370 /* adjust for aspect limits */
371 if(c->mina > 0 && c->maxa > 0) {
372 if(c->maxa < (float)*w / *h)
373 *w = *h * c->maxa + 0.5;
374 else if(c->mina < (float)*h / *w)
375 *h = *w * c->mina + 0.5;
377 if(baseismin) { /* increment calculation requires this */
381 /* adjust for increment value */
386 /* restore base dimensions */
387 *w = MAX(*w + c->basew, c->minw);
388 *h = MAX(*h + c->baseh, c->minh);
390 *w = MIN(*w, c->maxw);
392 *h = MIN(*h, c->maxh);
394 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
398 arrange(Monitor *m) {
401 else for(m = mons; m; m = m->next)
406 } else for(m = mons; m; m = m->next)
411 arrangemon(Monitor *m) {
412 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
413 if(m->lt[m->sellt]->arrange)
414 m->lt[m->sellt]->arrange(m);
419 c->next = c->mon->clients;
424 attachstack(Client *c) {
425 c->snext = c->mon->stack;
430 buttonpress(XEvent *e) {
431 unsigned int i, x, click;
435 XButtonPressedEvent *ev = &e->xbutton;
438 /* focus monitor if necessary */
439 if((m = wintomon(ev->window)) && m != selmon) {
440 unfocus(selmon->sel, True);
444 if(ev->window == selmon->barwin) {
448 while(ev->x >= x && ++i < LENGTH(tags));
449 if(i < LENGTH(tags)) {
453 else if(ev->x < x + blw)
455 else if(ev->x > selmon->ww - TEXTW(stext))
456 click = ClkStatusText;
460 else if((c = wintoclient(ev->window))) {
462 click = ClkClientWin;
464 for(i = 0; i < LENGTH(buttons); i++)
465 if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
466 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
467 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
472 xerrorxlib = XSetErrorHandler(xerrorstart);
473 /* this causes an error if some other window manager is running */
474 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
476 XSetErrorHandler(xerror);
483 Layout foo = { "", NULL };
487 selmon->lt[selmon->sellt] = &foo;
488 for(m = mons; m; m = m->next)
490 unmanage(m->stack, False);
491 XUngrabKey(dpy, AnyKey, AnyModifier, root);
492 XFreePixmap(dpy, dc.drawable);
494 XFreeCursor(dpy, cursor[CurNormal]);
495 XFreeCursor(dpy, cursor[CurResize]);
496 XFreeCursor(dpy, cursor[CurMove]);
500 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
501 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
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 // TODO: updategeom handling sucks, needs to be simplified
577 if(ev->window == root) {
578 dirty = (sw != ev->width || sh != ev->height);
581 if(updategeom() || dirty) {
583 XFreePixmap(dpy, dc.drawable);
584 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
586 for(m = mons; m; m = m->next)
587 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
595 configurerequest(XEvent *e) {
598 XConfigureRequestEvent *ev = &e->xconfigurerequest;
601 if((c = wintoclient(ev->window))) {
602 if(ev->value_mask & CWBorderWidth)
603 c->bw = ev->border_width;
604 else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
606 if(ev->value_mask & CWX) {
608 c->x = m->mx + ev->x;
610 if(ev->value_mask & CWY) {
612 c->y = m->my + ev->y;
614 if(ev->value_mask & CWWidth) {
618 if(ev->value_mask & CWHeight) {
622 if((c->x + c->w) > m->mx + m->mw && c->isfloating)
623 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
624 if((c->y + c->h) > m->my + m->mh && c->isfloating)
625 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
626 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
629 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
637 wc.width = ev->width;
638 wc.height = ev->height;
639 wc.border_width = ev->border_width;
640 wc.sibling = ev->above;
641 wc.stack_mode = ev->detail;
642 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
651 if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
652 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
653 m->tagset[0] = m->tagset[1] = 1;
655 m->nmaster = nmaster;
656 m->showbar = showbar;
658 m->lt[0] = &layouts[0];
659 m->lt[1] = &layouts[1 % LENGTH(layouts)];
660 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
665 destroynotify(XEvent *e) {
667 XDestroyWindowEvent *ev = &e->xdestroywindow;
669 if((c = wintoclient(ev->window)))
677 for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
682 detachstack(Client *c) {
685 for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
688 if(c == c->mon->sel) {
689 for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
695 die(const char *errstr, ...) {
698 va_start(ap, errstr);
699 vfprintf(stderr, errstr, ap);
709 if(!(m = selmon->next))
712 else if(selmon == mons)
713 for(m = mons; m->next; m = m->next);
715 for(m = mons; m->next != selmon; m = m->next);
720 drawbar(Monitor *m) {
722 unsigned int i, occ = 0, urg = 0;
726 for(c = m->clients; c; c = c->next) {
732 for(i = 0; i < LENGTH(tags); i++) {
733 dc.w = TEXTW(tags[i]);
734 col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
735 drawtext(tags[i], col, urg & 1 << i);
736 drawsquare(m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
737 occ & 1 << i, urg & 1 << i, col);
740 dc.w = blw = TEXTW(m->ltsymbol);
741 drawtext(m->ltsymbol, dc.norm, False);
744 if(m == selmon) { /* status is only drawn on selected monitor */
751 drawtext(stext, dc.norm, False);
755 if((dc.w = dc.x - x) > bh) {
758 col = m == selmon ? dc.sel : dc.norm;
759 drawtext(m->sel->name, col, False);
760 drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
763 drawtext(NULL, dc.norm, False);
765 XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
773 for(m = mons; m; m = m->next)
778 drawsquare(Bool filled, Bool empty, Bool invert, XftColor col[ColLast]) {
781 XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG].pixel);
782 x = (dc.font.ascent + dc.font.descent + 2) / 4;
784 XFillRectangle(dpy, dc.drawable, dc.gc, dc.x+1, dc.y+1, x+1, x+1);
786 XDrawRectangle(dpy, dc.drawable, dc.gc, dc.x+1, dc.y+1, x, x);
790 drawtext(const char *text, XftColor col[ColLast], Bool invert) {
792 int i, x, y, h, len, olen;
795 XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG].pixel);
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] = '.');
811 d = XftDrawCreate(dpy, dc.drawable, DefaultVisual(dpy, screen), DefaultColormap(dpy,screen));
813 XftDrawStringUtf8(d, &col[invert ? ColBG : ColFG], dc.font.xfont, x, y, (XftChar8 *) 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].pixel);
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, False); /* s/True/False/ fixes input focus issues
888 in gedit and anjuta */
894 focusstack(const Arg *arg) {
895 Client *c = NULL, *i;
900 for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
902 for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
905 for(i = selmon->clients; i != selmon->sel; i = i->next)
909 for(; i; i = i->next)
920 getatomprop(Client *c, Atom prop) {
923 unsigned char *p = NULL;
924 Atom da, atom = None;
926 if(XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
927 &da, &di, &dl, &dl, &p) == Success && p) {
935 getcolor(const char *colstr) {
938 if(!XftColorAllocName(dpy, DefaultVisual(dpy, screen), DefaultColormap(dpy, screen), colstr, &color))
939 die("error, cannot allocate color '%s'\n", colstr);
945 getrootptr(int *x, int *y) {
950 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
957 unsigned char *p = NULL;
958 unsigned long n, extra;
961 if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
962 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
971 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
976 if(!text || size == 0)
979 XGetTextProperty(dpy, w, &name, atom);
982 if(name.encoding == XA_STRING)
983 strncpy(text, (char *)name.value, size - 1);
985 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
986 strncpy(text, *list, size - 1);
987 XFreeStringList(list);
990 text[size - 1] = '\0';
996 grabbuttons(Client *c, Bool focused) {
1000 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1001 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1003 for(i = 0; i < LENGTH(buttons); i++)
1004 if(buttons[i].click == ClkClientWin)
1005 for(j = 0; j < LENGTH(modifiers); j++)
1006 XGrabButton(dpy, buttons[i].button,
1007 buttons[i].mask | modifiers[j],
1008 c->win, False, BUTTONMASK,
1009 GrabModeAsync, GrabModeSync, None, None);
1012 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
1013 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1019 updatenumlockmask();
1022 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1025 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1026 for(i = 0; i < LENGTH(keys); i++)
1027 if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
1028 for(j = 0; j < LENGTH(modifiers); j++)
1029 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
1030 True, GrabModeAsync, GrabModeAsync);
1035 incnmaster(const Arg *arg) {
1036 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
1041 initfont(const char *fontstr) {
1043 if(!(dc.font.xfont = XftFontOpenName(dpy,screen,fontstr))
1044 && !(dc.font.xfont = XftFontOpenName(dpy,screen,"fixed")))
1045 die("error, cannot load font: '%s'\n", fontstr);
1047 dc.font.ascent = dc.font.xfont->ascent;
1048 dc.font.descent = dc.font.xfont->descent;
1049 dc.font.height = dc.font.ascent + dc.font.descent;
1054 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
1056 if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1057 && unique[n].width == info->width && unique[n].height == info->height)
1061 #endif /* XINERAMA */
1064 keypress(XEvent *e) {
1070 keysym = XkbKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0, 0);
1071 for(i = 0; i < LENGTH(keys); i++)
1072 if(keysym == keys[i].keysym
1073 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1075 keys[i].func(&(keys[i].arg));
1079 killclient(const Arg *arg) {
1082 if(!sendevent(selmon->sel, wmatom[WMDelete])) {
1084 XSetErrorHandler(xerrordummy);
1085 XSetCloseDownMode(dpy, DestroyAll);
1086 XKillClient(dpy, selmon->sel->win);
1088 XSetErrorHandler(xerror);
1094 manage(Window w, XWindowAttributes *wa) {
1095 Client *c, *t = NULL;
1096 Window trans = None;
1099 if(!(c = calloc(1, sizeof(Client))))
1100 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1103 if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1112 c->x = c->oldx = wa->x;
1113 c->y = c->oldy = wa->y;
1114 c->w = c->oldw = wa->width;
1115 c->h = c->oldh = wa->height;
1116 c->oldbw = wa->border_width;
1118 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1119 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1120 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1121 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1122 c->x = MAX(c->x, c->mon->mx);
1123 /* only fix client y-offset, if the client center might cover the bar */
1124 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1125 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1128 wc.border_width = c->bw;
1129 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1130 XSetWindowBorder(dpy, w, dc.norm[ColBorder].pixel);
1131 configure(c); /* propagates border_width, if size doesn't change */
1132 updatewindowtype(c);
1135 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1136 grabbuttons(c, False);
1138 c->isfloating = c->oldstate = trans != None || c->isfixed;
1140 XRaiseWindow(dpy, c->win);
1143 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1144 (unsigned char *) &(c->win), 1);
1145 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1146 setclientstate(c, NormalState);
1147 if (c->mon == selmon)
1148 unfocus(selmon->sel, False);
1151 XMapWindow(dpy, c->win);
1156 mappingnotify(XEvent *e) {
1157 XMappingEvent *ev = &e->xmapping;
1159 XRefreshKeyboardMapping(ev);
1160 if(ev->request == MappingKeyboard)
1165 maprequest(XEvent *e) {
1166 static XWindowAttributes wa;
1167 XMapRequestEvent *ev = &e->xmaprequest;
1169 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1171 if(wa.override_redirect)
1173 if(!wintoclient(ev->window))
1174 manage(ev->window, &wa);
1178 monocle(Monitor *m) {
1182 for(c = m->clients; c; c = c->next)
1185 if(n > 0) /* override layout symbol */
1186 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1187 for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
1188 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
1192 motionnotify(XEvent *e) {
1193 static Monitor *mon = NULL;
1195 XMotionEvent *ev = &e->xmotion;
1197 if(ev->window != root)
1199 if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1200 unfocus(selmon->sel, True);
1208 movemouse(const Arg *arg) {
1209 int x, y, ocx, ocy, nx, ny;
1214 if(!(c = selmon->sel))
1216 if(c->isfullscreen) /* no support moving fullscreen windows by mouse */
1221 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1222 None, cursor[CurMove], CurrentTime) != GrabSuccess)
1224 if(!getrootptr(&x, &y))
1227 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1229 case ConfigureRequest:
1232 handler[ev.type](&ev);
1235 nx = ocx + (ev.xmotion.x - x);
1236 ny = ocy + (ev.xmotion.y - y);
1237 if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1238 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1239 if(abs(selmon->wx - nx) < snap)
1241 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1242 nx = selmon->wx + selmon->ww - WIDTH(c);
1243 if(abs(selmon->wy - ny) < snap)
1245 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1246 ny = selmon->wy + selmon->wh - HEIGHT(c);
1247 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1248 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1249 togglefloating(NULL);
1251 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1252 resize(c, nx, ny, c->w, c->h, True);
1255 } while(ev.type != ButtonRelease);
1256 XUngrabPointer(dpy, CurrentTime);
1257 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1265 nexttiled(Client *c) {
1266 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1279 propertynotify(XEvent *e) {
1282 XPropertyEvent *ev = &e->xproperty;
1284 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1286 else if(ev->state == PropertyDelete)
1287 return; /* ignore */
1288 else if((c = wintoclient(ev->window))) {
1291 case XA_WM_TRANSIENT_FOR:
1292 if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1293 (c->isfloating = (wintoclient(trans)) != NULL))
1296 case XA_WM_NORMAL_HINTS:
1304 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1306 if(c == c->mon->sel)
1309 if(ev->atom == netatom[NetWMWindowType])
1310 updatewindowtype(c);
1315 quit(const Arg *arg) {
1320 recttomon(int x, int y, int w, int h) {
1321 Monitor *m, *r = selmon;
1324 for(m = mons; m; m = m->next)
1325 if((a = INTERSECT(x, y, w, h, m)) > area) {
1333 resize(Client *c, int x, int y, int w, int h, Bool interact) {
1334 if(applysizehints(c, &x, &y, &w, &h, interact))
1335 resizeclient(c, x, y, w, h);
1339 resizeclient(Client *c, int x, int y, int w, int h) {
1342 c->oldx = c->x; c->x = wc.x = x;
1343 c->oldy = c->y; c->y = wc.y = y;
1344 c->oldw = c->w; c->w = wc.width = w;
1345 c->oldh = c->h; c->h = wc.height = h;
1346 wc.border_width = c->bw;
1347 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1353 resizemouse(const Arg *arg) {
1360 if(!(c = selmon->sel))
1362 if(c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1367 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1368 None, cursor[CurResize], CurrentTime) != GrabSuccess)
1370 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1372 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1374 case ConfigureRequest:
1377 handler[ev.type](&ev);
1380 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1381 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1382 if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1383 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1385 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1386 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1387 togglefloating(NULL);
1389 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1390 resize(c, c->x, c->y, nw, nh, True);
1393 } while(ev.type != ButtonRelease);
1394 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1395 XUngrabPointer(dpy, CurrentTime);
1396 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1397 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1405 restack(Monitor *m) {
1413 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1414 XRaiseWindow(dpy, m->sel->win);
1415 if(m->lt[m->sellt]->arrange) {
1416 wc.stack_mode = Below;
1417 wc.sibling = m->barwin;
1418 for(c = m->stack; c; c = c->snext)
1419 if(!c->isfloating && ISVISIBLE(c)) {
1420 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1421 wc.sibling = c->win;
1425 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1431 /* main event loop */
1433 while(running && !XNextEvent(dpy, &ev))
1434 if(handler[ev.type])
1435 handler[ev.type](&ev); /* call handler */
1440 unsigned int i, num;
1441 Window d1, d2, *wins = NULL;
1442 XWindowAttributes wa;
1444 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1445 for(i = 0; i < num; i++) {
1446 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1447 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1449 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1450 manage(wins[i], &wa);
1452 for(i = 0; i < num; i++) { /* now the transients */
1453 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1455 if(XGetTransientForHint(dpy, wins[i], &d1)
1456 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1457 manage(wins[i], &wa);
1465 sendmon(Client *c, Monitor *m) {
1472 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1480 setclientstate(Client *c, long state) {
1481 long data[] = { state, None };
1483 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1484 PropModeReplace, (unsigned char *)data, 2);
1488 sendevent(Client *c, Atom proto) {
1491 Bool exists = False;
1494 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1495 while(!exists && n--)
1496 exists = protocols[n] == proto;
1500 ev.type = ClientMessage;
1501 ev.xclient.window = c->win;
1502 ev.xclient.message_type = wmatom[WMProtocols];
1503 ev.xclient.format = 32;
1504 ev.xclient.data.l[0] = proto;
1505 ev.xclient.data.l[1] = CurrentTime;
1506 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1512 setfocus(Client *c) {
1513 if(!c->neverfocus) {
1514 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1515 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1516 XA_WINDOW, 32, PropModeReplace,
1517 (unsigned char *) &(c->win), 1);
1519 sendevent(c, wmatom[WMTakeFocus]);
1523 setfullscreen(Client *c, Bool fullscreen) {
1525 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1526 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1527 c->isfullscreen = True;
1528 c->oldstate = c->isfloating;
1531 c->isfloating = True;
1532 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1533 XRaiseWindow(dpy, c->win);
1536 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1537 PropModeReplace, (unsigned char*)0, 0);
1538 c->isfullscreen = False;
1539 c->isfloating = c->oldstate;
1545 resizeclient(c, c->x, c->y, c->w, c->h);
1551 setlayout(const Arg *arg) {
1552 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1555 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1556 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1563 /* arg > 1.0 will set mfact absolutly */
1565 setmfact(const Arg *arg) {
1568 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1570 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1571 if(f < 0.1 || f > 0.9)
1579 XSetWindowAttributes wa;
1581 /* clean up any zombies immediately */
1585 screen = DefaultScreen(dpy);
1586 root = RootWindow(dpy, screen);
1588 sw = DisplayWidth(dpy, screen);
1589 sh = DisplayHeight(dpy, screen);
1590 bh = dc.h = dc.font.height + 2;
1593 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1594 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1595 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1596 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1597 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1598 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1599 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1600 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1601 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1602 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1603 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1604 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1606 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1607 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1608 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1609 /* init appearance */
1610 dc.norm[ColBorder] = getcolor(normbordercolor);
1611 dc.norm[ColBG] = getcolor(normbgcolor);
1612 dc.norm[ColFG] = getcolor(normfgcolor);
1613 dc.sel[ColBorder] = getcolor(selbordercolor);
1614 dc.sel[ColBG] = getcolor(selbgcolor);
1615 dc.sel[ColFG] = getcolor(selfgcolor);
1616 dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
1617 dc.gc = XCreateGC(dpy, root, 0, NULL);
1618 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1622 /* EWMH support per view */
1623 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1624 PropModeReplace, (unsigned char *) netatom, NetLast);
1625 XDeleteProperty(dpy, root, netatom[NetClientList]);
1626 /* select for events */
1627 wa.cursor = cursor[CurNormal];
1628 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
1629 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1630 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1631 XSelectInput(dpy, root, wa.event_mask);
1636 showhide(Client *c) {
1639 if(ISVISIBLE(c)) { /* show clients top down */
1640 XMoveWindow(dpy, c->win, c->x, c->y);
1641 if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1642 resize(c, c->x, c->y, c->w, c->h, False);
1645 else { /* hide clients bottom up */
1647 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1652 sigchld(int unused) {
1653 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1654 die("Can't install SIGCHLD handler");
1655 while(0 < waitpid(-1, NULL, WNOHANG));
1659 spawn(const Arg *arg) {
1662 close(ConnectionNumber(dpy));
1664 execvp(((char **)arg->v)[0], (char **)arg->v);
1665 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1672 tag(const Arg *arg) {
1673 if(selmon->sel && arg->ui & TAGMASK) {
1674 selmon->sel->tags = arg->ui & TAGMASK;
1681 tagmon(const Arg *arg) {
1682 if(!selmon->sel || !mons->next)
1684 sendmon(selmon->sel, dirtomon(arg->i));
1688 textnw(const char *text, unsigned int len) {
1690 XftTextExtentsUtf8(dpy, dc.font.xfont, (XftChar8 *) text, len, &ext);
1696 unsigned int i, n, h, mw, my, ty;
1699 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1704 mw = m->nmaster ? m->ww * m->mfact : 0;
1707 for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1708 if(i < m->nmaster) {
1709 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1710 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False);
1714 h = (m->wh - ty) / (n - i);
1715 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), False);
1721 togglebar(const Arg *arg) {
1722 selmon->showbar = !selmon->showbar;
1723 updatebarpos(selmon);
1724 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1729 togglefloating(const Arg *arg) {
1732 if(selmon->sel->isfullscreen) /* no support for fullscreen windows */
1734 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1735 if(selmon->sel->isfloating)
1736 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1737 selmon->sel->w, selmon->sel->h, False);
1742 toggletag(const Arg *arg) {
1743 unsigned int newtags;
1747 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1749 selmon->sel->tags = newtags;
1756 toggleview(const Arg *arg) {
1757 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1760 selmon->tagset[selmon->seltags] = newtagset;
1767 unfocus(Client *c, Bool setfocus) {
1770 grabbuttons(c, False);
1771 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder].pixel);
1773 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1774 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1779 unmanage(Client *c, Bool destroyed) {
1780 Monitor *m = c->mon;
1783 /* The server grab construct avoids race conditions. */
1787 wc.border_width = c->oldbw;
1789 XSetErrorHandler(xerrordummy);
1790 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1791 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1792 setclientstate(c, WithdrawnState);
1794 XSetErrorHandler(xerror);
1804 unmapnotify(XEvent *e) {
1806 XUnmapEvent *ev = &e->xunmap;
1808 if((c = wintoclient(ev->window))) {
1810 setclientstate(c, WithdrawnState);
1819 XSetWindowAttributes wa = {
1820 .override_redirect = True,
1821 .background_pixmap = ParentRelative,
1822 .event_mask = ButtonPressMask|ExposureMask
1824 for(m = mons; m; m = m->next) {
1827 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1828 CopyFromParent, DefaultVisual(dpy, screen),
1829 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1830 XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
1831 XMapRaised(dpy, m->barwin);
1836 updatebarpos(Monitor *m) {
1841 m->by = m->topbar ? m->wy : m->wy + m->wh;
1842 m->wy = m->topbar ? m->wy + bh : m->wy;
1849 updateclientlist() {
1853 XDeleteProperty(dpy, root, netatom[NetClientList]);
1854 for(m = mons; m; m = m->next)
1855 for(c = m->clients; c; c = c->next)
1856 XChangeProperty(dpy, root, netatom[NetClientList],
1857 XA_WINDOW, 32, PropModeAppend,
1858 (unsigned char *) &(c->win), 1);
1866 if(XineramaIsActive(dpy)) {
1870 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1871 XineramaScreenInfo *unique = NULL;
1873 for(n = 0, m = mons; m; m = m->next, n++);
1874 /* only consider unique geometries as separate screens */
1875 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
1876 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
1877 for(i = 0, j = 0; i < nn; i++)
1878 if(isuniquegeom(unique, j, &info[i]))
1879 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1883 for(i = 0; i < (nn - n); i++) { /* new monitors available */
1884 for(m = mons; m && m->next; m = m->next);
1886 m->next = createmon();
1890 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
1892 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1893 || unique[i].width != m->mw || unique[i].height != m->mh))
1897 m->mx = m->wx = unique[i].x_org;
1898 m->my = m->wy = unique[i].y_org;
1899 m->mw = m->ww = unique[i].width;
1900 m->mh = m->wh = unique[i].height;
1904 else { /* less monitors available nn < n */
1905 for(i = nn; i < n; i++) {
1906 for(m = mons; m && m->next; m = m->next);
1910 m->clients = c->next;
1924 #endif /* XINERAMA */
1925 /* default monitor setup */
1929 if(mons->mw != sw || mons->mh != sh) {
1931 mons->mw = mons->ww = sw;
1932 mons->mh = mons->wh = sh;
1938 selmon = wintomon(root);
1944 updatenumlockmask(void) {
1946 XModifierKeymap *modmap;
1949 modmap = XGetModifierMapping(dpy);
1950 for(i = 0; i < 8; i++)
1951 for(j = 0; j < modmap->max_keypermod; j++)
1952 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1953 == XKeysymToKeycode(dpy, XK_Num_Lock))
1954 numlockmask = (1 << i);
1955 XFreeModifiermap(modmap);
1959 updatesizehints(Client *c) {
1963 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
1964 /* size is uninitialized, ensure that size.flags aren't used */
1966 if(size.flags & PBaseSize) {
1967 c->basew = size.base_width;
1968 c->baseh = size.base_height;
1970 else if(size.flags & PMinSize) {
1971 c->basew = size.min_width;
1972 c->baseh = size.min_height;
1975 c->basew = c->baseh = 0;
1976 if(size.flags & PResizeInc) {
1977 c->incw = size.width_inc;
1978 c->inch = size.height_inc;
1981 c->incw = c->inch = 0;
1982 if(size.flags & PMaxSize) {
1983 c->maxw = size.max_width;
1984 c->maxh = size.max_height;
1987 c->maxw = c->maxh = 0;
1988 if(size.flags & PMinSize) {
1989 c->minw = size.min_width;
1990 c->minh = size.min_height;
1992 else if(size.flags & PBaseSize) {
1993 c->minw = size.base_width;
1994 c->minh = size.base_height;
1997 c->minw = c->minh = 0;
1998 if(size.flags & PAspect) {
1999 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2000 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2003 c->maxa = c->mina = 0.0;
2004 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
2005 && c->maxw == c->minw && c->maxh == c->minh);
2009 updatetitle(Client *c) {
2010 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2011 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2012 if(c->name[0] == '\0') /* hack to mark broken clients */
2013 strcpy(c->name, broken);
2017 updatestatus(void) {
2018 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2019 strcpy(stext, "dwm-"VERSION);
2024 updatewindowtype(Client *c) {
2025 Atom state = getatomprop(c, netatom[NetWMState]);
2026 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2028 if(state == netatom[NetWMFullscreen])
2029 setfullscreen(c, True);
2030 if(wtype == netatom[NetWMWindowTypeDialog])
2031 c->isfloating = True;
2035 updatewmhints(Client *c) {
2038 if((wmh = XGetWMHints(dpy, c->win))) {
2039 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
2040 wmh->flags &= ~XUrgencyHint;
2041 XSetWMHints(dpy, c->win, wmh);
2044 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
2045 if(wmh->flags & InputHint)
2046 c->neverfocus = !wmh->input;
2048 c->neverfocus = False;
2054 view(const Arg *arg) {
2055 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2057 selmon->seltags ^= 1; /* toggle sel tagset */
2058 if(arg->ui & TAGMASK)
2059 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2065 wintoclient(Window w) {
2069 for(m = mons; m; m = m->next)
2070 for(c = m->clients; c; c = c->next)
2077 wintomon(Window w) {
2082 if(w == root && getrootptr(&x, &y))
2083 return recttomon(x, y, 1, 1);
2084 for(m = mons; m; m = m->next)
2087 if((c = wintoclient(w)))
2092 /* There's no way to check accesses to destroyed windows, thus those cases are
2093 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2094 * default error handler, which may call exit. */
2096 xerror(Display *dpy, XErrorEvent *ee) {
2097 if(ee->error_code == BadWindow
2098 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2099 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2100 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2101 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2102 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2103 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2104 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2105 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2107 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2108 ee->request_code, ee->error_code);
2109 return xerrorxlib(dpy, ee); /* may call exit */
2113 xerrordummy(Display *dpy, XErrorEvent *ee) {
2117 /* Startup Error handler to check if another window manager
2118 * is already running. */
2120 xerrorstart(Display *dpy, XErrorEvent *ee) {
2121 die("dwm: another window manager is already running\n");
2126 zoom(const Arg *arg) {
2127 Client *c = selmon->sel;
2129 if(!selmon->lt[selmon->sellt]->arrange
2130 || (selmon->sel && selmon->sel->isfloating))
2132 if(c == nexttiled(selmon->clients))
2133 if(!c || !(c = nexttiled(c->next)))
2139 main(int argc, char *argv[]) {
2140 if(argc == 2 && !strcmp("-v", argv[1]))
2141 die("dwm-"VERSION", © 2006-2012 dwm engineers, see LICENSE for details\n");
2143 die("usage: dwm [-v]\n");
2144 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2145 fputs("warning: no locale support\n", stderr);
2146 if(!(dpy = XOpenDisplay(NULL)))
2147 die("dwm: cannot open display\n");
2154 return EXIT_SUCCESS;