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>
49 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
50 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
51 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
52 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
53 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
54 #define LENGTH(X) (sizeof X / sizeof X[0])
55 #define MAX(A, B) ((A) > (B) ? (A) : (B))
56 #define MIN(A, B) ((A) < (B) ? (A) : (B))
57 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
58 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
59 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
60 #define TAGMASK ((1 << LENGTH(tags)) - 1)
61 #define TEXTW(X) (textnw(X, strlen(X)) + dc.font.height)
64 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
65 enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
66 enum { NetSupported, NetWMName, NetWMState,
67 NetWMFullscreen, NetActiveWindow, NetWMWindowType,
68 NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */
69 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
70 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
71 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
84 void (*func)(const Arg *arg);
88 typedef struct Monitor Monitor;
89 typedef struct Client Client;
94 int oldx, oldy, oldw, oldh;
95 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
98 Bool isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
107 XftColor norm[ColLast];
108 XftColor sel[ColLast];
117 } DC; /* draw context */
122 void (*func)(const Arg *);
128 void (*arrange)(Monitor *);
136 int by; /* bar geometry */
137 int mx, my, mw, mh; /* screen size */
138 int wx, wy, ww, wh; /* window area */
139 unsigned int seltags;
141 unsigned int tagset[2];
154 const char *instance;
161 /* function declarations */
162 static void applyrules(Client *c);
163 static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
164 static void arrange(Monitor *m);
165 static void arrangemon(Monitor *m);
166 static void attach(Client *c);
167 static void attachstack(Client *c);
168 static void buttonpress(XEvent *e);
169 static void checkotherwm(void);
170 static void cleanup(void);
171 static void cleanupmon(Monitor *mon);
172 static void clearurgent(Client *c);
173 static void clientmessage(XEvent *e);
174 static void configure(Client *c);
175 static void configurenotify(XEvent *e);
176 static void configurerequest(XEvent *e);
177 static Monitor *createmon(void);
178 static void destroynotify(XEvent *e);
179 static void detach(Client *c);
180 static void detachstack(Client *c);
181 static void die(const char *errstr, ...);
182 static Monitor *dirtomon(int dir);
183 static void drawbar(Monitor *m);
184 static void drawbars(void);
185 static void drawsquare(Bool filled, Bool empty, Bool invert, XftColor col[ColLast]);
186 static void drawtext(const char *text, XftColor col[ColLast], Bool invert);
187 static void enternotify(XEvent *e);
188 static void expose(XEvent *e);
189 static void focus(Client *c);
190 static void focusin(XEvent *e);
191 static void focusmon(const Arg *arg);
192 static void focusstack(const Arg *arg);
193 static XftColor getcolor(const char *colstr);
194 static Bool getrootptr(int *x, int *y);
195 static long getstate(Window w);
196 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
197 static void grabbuttons(Client *c, Bool focused);
198 static void grabkeys(void);
199 static void incnmaster(const Arg *arg);
200 static void initfont(const char *fontstr);
201 static void keypress(XEvent *e);
202 static void killclient(const Arg *arg);
203 static void manage(Window w, XWindowAttributes *wa);
204 static void mappingnotify(XEvent *e);
205 static void maprequest(XEvent *e);
206 static void monocle(Monitor *m);
207 static void motionnotify(XEvent *e);
208 static void movemouse(const Arg *arg);
209 static Client *nexttiled(Client *c);
210 static void pop(Client *);
211 static void propertynotify(XEvent *e);
212 static void quit(const Arg *arg);
213 static Monitor *recttomon(int x, int y, int w, int h);
214 static void resize(Client *c, int x, int y, int w, int h, Bool interact);
215 static void resizeclient(Client *c, int x, int y, int w, int h);
216 static void resizemouse(const Arg *arg);
217 static void restack(Monitor *m);
218 static void run(void);
219 static void scan(void);
220 static Bool sendevent(Client *c, Atom proto);
221 static void sendmon(Client *c, Monitor *m);
222 static void setclientstate(Client *c, long state);
223 static void setfocus(Client *c);
224 static void setfullscreen(Client *c, Bool fullscreen);
225 static void setlayout(const Arg *arg);
226 static void setmfact(const Arg *arg);
227 static void setup(void);
228 static void showhide(Client *c);
229 static void sigchld(int unused);
230 static void spawn(const Arg *arg);
231 static void tag(const Arg *arg);
232 static void tagmon(const Arg *arg);
233 static int textnw(const char *text, unsigned int len);
234 static void tile(Monitor *);
235 static void togglebar(const Arg *arg);
236 static void togglefloating(const Arg *arg);
237 static void toggletag(const Arg *arg);
238 static void toggleview(const Arg *arg);
239 static void unfocus(Client *c, Bool setfocus);
240 static void unmanage(Client *c, Bool destroyed);
241 static void unmapnotify(XEvent *e);
242 static Bool updategeom(void);
243 static void updatebarpos(Monitor *m);
244 static void updatebars(void);
245 static void updateclientlist(void);
246 static void updatenumlockmask(void);
247 static void updatesizehints(Client *c);
248 static void updatestatus(void);
249 static void updatewindowtype(Client *c);
250 static void updatetitle(Client *c);
251 static void updatewmhints(Client *c);
252 static void view(const Arg *arg);
253 static Client *wintoclient(Window w);
254 static Monitor *wintomon(Window w);
255 static int xerror(Display *dpy, XErrorEvent *ee);
256 static int xerrordummy(Display *dpy, XErrorEvent *ee);
257 static int xerrorstart(Display *dpy, XErrorEvent *ee);
258 static void zoom(const Arg *arg);
261 static const char broken[] = "broken";
262 static char stext[256];
264 static int sw, sh; /* X display screen geometry width, height */
265 static int bh, blw = 0; /* bar geometry */
266 static int (*xerrorxlib)(Display *, XErrorEvent *);
267 static unsigned int numlockmask = 0;
268 static void (*handler[LASTEvent]) (XEvent *) = {
269 [ButtonPress] = buttonpress,
270 [ClientMessage] = clientmessage,
271 [ConfigureRequest] = configurerequest,
272 [ConfigureNotify] = configurenotify,
273 [DestroyNotify] = destroynotify,
274 [EnterNotify] = enternotify,
277 [KeyPress] = keypress,
278 [MappingNotify] = mappingnotify,
279 [MapRequest] = maprequest,
280 [MotionNotify] = motionnotify,
281 [PropertyNotify] = propertynotify,
282 [UnmapNotify] = unmapnotify
284 static Atom wmatom[WMLast], netatom[NetLast];
285 static Bool running = True;
286 static Cursor cursor[CurLast];
289 static Monitor *mons = NULL, *selmon = NULL;
292 /* configuration, allows nested code to access above variables */
295 /* compile-time check if all tags fit into an unsigned int bit array. */
296 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
298 /* function implementations */
300 applyrules(Client *c) {
301 const char *class, *instance;
305 XClassHint ch = { NULL, NULL };
308 c->isfloating = c->tags = 0;
309 XGetClassHint(dpy, c->win, &ch);
310 class = ch.res_class ? ch.res_class : broken;
311 instance = ch.res_name ? ch.res_name : broken;
313 for(i = 0; i < LENGTH(rules); i++) {
315 if((!r->title || strstr(c->name, r->title))
316 && (!r->class || strstr(class, r->class))
317 && (!r->instance || strstr(instance, r->instance)))
319 c->isfloating = r->isfloating;
321 for(m = mons; m && m->num != r->monitor; m = m->next);
330 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
334 applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
338 /* set minimum possible */
346 if(*x + *w + 2 * c->bw < 0)
348 if(*y + *h + 2 * c->bw < 0)
352 if(*x >= m->wx + m->ww)
353 *x = m->wx + m->ww - WIDTH(c);
354 if(*y >= m->wy + m->wh)
355 *y = m->wy + m->wh - HEIGHT(c);
356 if(*x + *w + 2 * c->bw <= m->wx)
358 if(*y + *h + 2 * c->bw <= m->wy)
365 if(resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
366 /* see last two sentences in ICCCM 4.1.2.3 */
367 baseismin = c->basew == c->minw && c->baseh == c->minh;
368 if(!baseismin) { /* temporarily remove base dimensions */
372 /* adjust for aspect limits */
373 if(c->mina > 0 && c->maxa > 0) {
374 if(c->maxa < (float)*w / *h)
375 *w = *h * c->maxa + 0.5;
376 else if(c->mina < (float)*h / *w)
377 *h = *w * c->mina + 0.5;
379 if(baseismin) { /* increment calculation requires this */
383 /* adjust for increment value */
388 /* restore base dimensions */
389 *w = MAX(*w + c->basew, c->minw);
390 *h = MAX(*h + c->baseh, c->minh);
392 *w = MIN(*w, c->maxw);
394 *h = MIN(*h, c->maxh);
396 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
400 arrange(Monitor *m) {
403 else for(m = mons; m; m = m->next)
408 } else for(m = mons; m; m = m->next)
413 arrangemon(Monitor *m) {
414 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
415 if(m->lt[m->sellt]->arrange)
416 m->lt[m->sellt]->arrange(m);
421 c->next = c->mon->clients;
426 attachstack(Client *c) {
427 c->snext = c->mon->stack;
432 buttonpress(XEvent *e) {
433 unsigned int i, x, click;
437 XButtonPressedEvent *ev = &e->xbutton;
440 /* focus monitor if necessary */
441 if((m = wintomon(ev->window)) && m != selmon) {
442 unfocus(selmon->sel, True);
446 if(ev->window == selmon->barwin) {
450 while(ev->x >= x && ++i < LENGTH(tags));
451 if(i < LENGTH(tags)) {
455 else if(ev->x < x + blw)
457 else if(ev->x > selmon->ww - TEXTW(stext))
458 click = ClkStatusText;
462 else if((c = wintoclient(ev->window))) {
464 click = ClkClientWin;
466 for(i = 0; i < LENGTH(buttons); i++)
467 if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
468 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
469 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
474 xerrorxlib = XSetErrorHandler(xerrorstart);
475 /* this causes an error if some other window manager is running */
476 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
478 XSetErrorHandler(xerror);
485 Layout foo = { "", NULL };
489 selmon->lt[selmon->sellt] = &foo;
490 for(m = mons; m; m = m->next)
492 unmanage(m->stack, False);
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);
697 die(const char *errstr, ...) {
700 va_start(ap, errstr);
701 vfprintf(stderr, errstr, ap);
711 if(!(m = selmon->next))
714 else if(selmon == mons)
715 for(m = mons; m->next; m = m->next);
717 for(m = mons; m->next != selmon; m = m->next);
722 drawbar(Monitor *m) {
724 unsigned int i, occ = 0, urg = 0;
728 for(c = m->clients; c; c = c->next) {
734 for(i = 0; i < LENGTH(tags); i++) {
735 dc.w = TEXTW(tags[i]);
736 col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
737 drawtext(tags[i], col, urg & 1 << i);
738 drawsquare(m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
739 occ & 1 << i, urg & 1 << i, col);
742 dc.w = blw = TEXTW(m->ltsymbol);
743 drawtext(m->ltsymbol, dc.norm, False);
746 if(m == selmon) { /* status is only drawn on selected monitor */
753 drawtext(stext, dc.norm, False);
757 if((dc.w = dc.x - x) > bh) {
760 col = m == selmon ? dc.sel : dc.norm;
761 drawtext(m->sel->name, col, False);
762 drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
765 drawtext(NULL, dc.norm, False);
767 XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
775 for(m = mons; m; m = m->next)
780 drawsquare(Bool filled, Bool empty, Bool invert, XftColor col[ColLast]) {
783 XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG].pixel);
784 x = (dc.font.ascent + dc.font.descent + 2) / 4;
786 XFillRectangle(dpy, dc.drawable, dc.gc, dc.x+1, dc.y+1, x+1, x+1);
788 XDrawRectangle(dpy, dc.drawable, dc.gc, dc.x+1, dc.y+1, x, x);
792 drawtext(const char *text, XftColor col[ColLast], Bool invert) {
794 int i, x, y, h, len, olen;
797 XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG].pixel);
798 XFillRectangle(dpy, dc.drawable, dc.gc, dc.x, dc.y, dc.w, dc.h);
802 h = dc.font.ascent + dc.font.descent;
803 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
805 /* shorten text if necessary */
806 for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
809 memcpy(buf, text, len);
811 for(i = len; i && i > len - 3; buf[--i] = '.');
813 d = XftDrawCreate(dpy, dc.drawable, DefaultVisual(dpy, screen), DefaultColormap(dpy,screen));
815 XftDrawStringUtf8(d, &col[invert ? ColBG : ColFG], dc.font.xfont, x, y, (XftChar8 *) buf, len);
820 enternotify(XEvent *e) {
823 XCrossingEvent *ev = &e->xcrossing;
825 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
827 c = wintoclient(ev->window);
828 m = c ? c->mon : wintomon(ev->window);
830 unfocus(selmon->sel, True);
833 else if(!c || c == selmon->sel)
841 XExposeEvent *ev = &e->xexpose;
843 if(ev->count == 0 && (m = wintomon(ev->window)))
849 if(!c || !ISVISIBLE(c))
850 for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
851 /* was if(selmon->sel) */
852 if(selmon->sel && selmon->sel != c)
853 unfocus(selmon->sel, False);
861 grabbuttons(c, True);
862 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder].pixel);
866 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
867 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
874 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
875 XFocusChangeEvent *ev = &e->xfocus;
877 if(selmon->sel && ev->window != selmon->sel->win)
878 setfocus(selmon->sel);
882 focusmon(const Arg *arg) {
887 if((m = dirtomon(arg->i)) == selmon)
889 unfocus(selmon->sel, False); /* s/True/False/ fixes input focus issues
890 in gedit and anjuta */
896 focusstack(const Arg *arg) {
897 Client *c = NULL, *i;
902 for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
904 for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
907 for(i = selmon->clients; i != selmon->sel; i = i->next)
911 for(; i; i = i->next)
922 getatomprop(Client *c, Atom prop) {
925 unsigned char *p = NULL;
926 Atom da, atom = None;
928 if(XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
929 &da, &di, &dl, &dl, &p) == Success && p) {
937 getcolor(const char *colstr) {
940 if(!XftColorAllocName(dpy, DefaultVisual(dpy, screen), DefaultColormap(dpy, screen), colstr, &color))
941 die("error, cannot allocate color '%s'\n", colstr);
947 getrootptr(int *x, int *y) {
952 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
959 unsigned char *p = NULL;
960 unsigned long n, extra;
963 if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
964 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
973 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
978 if(!text || size == 0)
981 XGetTextProperty(dpy, w, &name, atom);
984 if(name.encoding == XA_STRING)
985 strncpy(text, (char *)name.value, size - 1);
987 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
988 strncpy(text, *list, size - 1);
989 XFreeStringList(list);
992 text[size - 1] = '\0';
998 grabbuttons(Client *c, Bool focused) {
1002 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1003 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1005 for(i = 0; i < LENGTH(buttons); i++)
1006 if(buttons[i].click == ClkClientWin)
1007 for(j = 0; j < LENGTH(modifiers); j++)
1008 XGrabButton(dpy, buttons[i].button,
1009 buttons[i].mask | modifiers[j],
1010 c->win, False, BUTTONMASK,
1011 GrabModeAsync, GrabModeSync, None, None);
1014 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
1015 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1021 updatenumlockmask();
1024 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1027 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1028 for(i = 0; i < LENGTH(keys); i++)
1029 if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
1030 for(j = 0; j < LENGTH(modifiers); j++)
1031 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
1032 True, GrabModeAsync, GrabModeAsync);
1037 incnmaster(const Arg *arg) {
1038 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
1043 initfont(const char *fontstr) {
1045 if(!(dc.font.xfont = XftFontOpenName(dpy,screen,fontstr))
1046 && !(dc.font.xfont = XftFontOpenName(dpy,screen,"fixed")))
1047 die("error, cannot load font: '%s'\n", fontstr);
1049 dc.font.ascent = dc.font.xfont->ascent;
1050 dc.font.descent = dc.font.xfont->descent;
1051 dc.font.height = dc.font.ascent + dc.font.descent;
1056 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
1058 if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1059 && unique[n].width == info->width && unique[n].height == info->height)
1063 #endif /* XINERAMA */
1066 keypress(XEvent *e) {
1072 keysym = XkbKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0, 0);
1073 for(i = 0; i < LENGTH(keys); i++)
1074 if(keysym == keys[i].keysym
1075 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1077 keys[i].func(&(keys[i].arg));
1081 killclient(const Arg *arg) {
1084 if(!sendevent(selmon->sel, wmatom[WMDelete])) {
1086 XSetErrorHandler(xerrordummy);
1087 XSetCloseDownMode(dpy, DestroyAll);
1088 XKillClient(dpy, selmon->sel->win);
1090 XSetErrorHandler(xerror);
1096 manage(Window w, XWindowAttributes *wa) {
1097 Client *c, *t = NULL;
1098 Window trans = None;
1101 if(!(c = calloc(1, sizeof(Client))))
1102 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1105 if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1114 c->x = c->oldx = wa->x;
1115 c->y = c->oldy = wa->y;
1116 c->w = c->oldw = wa->width;
1117 c->h = c->oldh = wa->height;
1118 c->oldbw = wa->border_width;
1120 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1121 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1122 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1123 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1124 c->x = MAX(c->x, c->mon->mx);
1125 /* only fix client y-offset, if the client center might cover the bar */
1126 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1127 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1130 wc.border_width = c->bw;
1131 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1132 XSetWindowBorder(dpy, w, dc.norm[ColBorder].pixel);
1133 configure(c); /* propagates border_width, if size doesn't change */
1134 updatewindowtype(c);
1137 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1138 grabbuttons(c, False);
1140 c->isfloating = c->oldstate = trans != None || c->isfixed;
1142 XRaiseWindow(dpy, c->win);
1145 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1146 (unsigned char *) &(c->win), 1);
1147 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1148 setclientstate(c, NormalState);
1149 if (c->mon == selmon)
1150 unfocus(selmon->sel, False);
1153 XMapWindow(dpy, c->win);
1158 mappingnotify(XEvent *e) {
1159 XMappingEvent *ev = &e->xmapping;
1161 XRefreshKeyboardMapping(ev);
1162 if(ev->request == MappingKeyboard)
1167 maprequest(XEvent *e) {
1168 static XWindowAttributes wa;
1169 XMapRequestEvent *ev = &e->xmaprequest;
1171 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1173 if(wa.override_redirect)
1175 if(!wintoclient(ev->window))
1176 manage(ev->window, &wa);
1180 monocle(Monitor *m) {
1184 for(c = m->clients; c; c = c->next)
1187 if(n > 0) /* override layout symbol */
1188 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1189 for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
1190 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
1194 motionnotify(XEvent *e) {
1195 static Monitor *mon = NULL;
1197 XMotionEvent *ev = &e->xmotion;
1199 if(ev->window != root)
1201 if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1202 unfocus(selmon->sel, True);
1210 movemouse(const Arg *arg) {
1211 int x, y, ocx, ocy, nx, ny;
1216 if(!(c = selmon->sel))
1218 if(c->isfullscreen) /* no support moving fullscreen windows by mouse */
1223 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1224 None, cursor[CurMove], CurrentTime) != GrabSuccess)
1226 if(!getrootptr(&x, &y))
1229 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1231 case ConfigureRequest:
1234 handler[ev.type](&ev);
1237 nx = ocx + (ev.xmotion.x - x);
1238 ny = ocy + (ev.xmotion.y - y);
1239 if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1240 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1241 if(abs(selmon->wx - nx) < snap)
1243 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1244 nx = selmon->wx + selmon->ww - WIDTH(c);
1245 if(abs(selmon->wy - ny) < snap)
1247 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1248 ny = selmon->wy + selmon->wh - HEIGHT(c);
1249 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1250 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1251 togglefloating(NULL);
1253 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1254 resize(c, nx, ny, c->w, c->h, True);
1257 } while(ev.type != ButtonRelease);
1258 XUngrabPointer(dpy, CurrentTime);
1259 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1267 nexttiled(Client *c) {
1268 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1281 propertynotify(XEvent *e) {
1284 XPropertyEvent *ev = &e->xproperty;
1286 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1288 else if(ev->state == PropertyDelete)
1289 return; /* ignore */
1290 else if((c = wintoclient(ev->window))) {
1293 case XA_WM_TRANSIENT_FOR:
1294 if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1295 (c->isfloating = (wintoclient(trans)) != NULL))
1298 case XA_WM_NORMAL_HINTS:
1306 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1308 if(c == c->mon->sel)
1311 if(ev->atom == netatom[NetWMWindowType])
1312 updatewindowtype(c);
1317 quit(const Arg *arg) {
1322 recttomon(int x, int y, int w, int h) {
1323 Monitor *m, *r = selmon;
1326 for(m = mons; m; m = m->next)
1327 if((a = INTERSECT(x, y, w, h, m)) > area) {
1335 resize(Client *c, int x, int y, int w, int h, Bool interact) {
1336 if(applysizehints(c, &x, &y, &w, &h, interact))
1337 resizeclient(c, x, y, w, h);
1341 resizeclient(Client *c, int x, int y, int w, int h) {
1344 c->oldx = c->x; c->x = wc.x = x;
1345 c->oldy = c->y; c->y = wc.y = y;
1346 c->oldw = c->w; c->w = wc.width = w;
1347 c->oldh = c->h; c->h = wc.height = h;
1348 wc.border_width = c->bw;
1349 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1355 resizemouse(const Arg *arg) {
1362 if(!(c = selmon->sel))
1364 if(c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1369 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1370 None, cursor[CurResize], CurrentTime) != GrabSuccess)
1372 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1374 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1376 case ConfigureRequest:
1379 handler[ev.type](&ev);
1382 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1383 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1384 if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1385 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1387 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1388 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1389 togglefloating(NULL);
1391 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1392 resize(c, c->x, c->y, nw, nh, True);
1395 } while(ev.type != ButtonRelease);
1396 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1397 XUngrabPointer(dpy, CurrentTime);
1398 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1399 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1407 restack(Monitor *m) {
1415 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1416 XRaiseWindow(dpy, m->sel->win);
1417 if(m->lt[m->sellt]->arrange) {
1418 wc.stack_mode = Below;
1419 wc.sibling = m->barwin;
1420 for(c = m->stack; c; c = c->snext)
1421 if(!c->isfloating && ISVISIBLE(c)) {
1422 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1423 wc.sibling = c->win;
1427 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1433 /* main event loop */
1435 while(running && !XNextEvent(dpy, &ev))
1436 if(handler[ev.type])
1437 handler[ev.type](&ev); /* call handler */
1442 unsigned int i, num;
1443 Window d1, d2, *wins = NULL;
1444 XWindowAttributes wa;
1446 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1447 for(i = 0; i < num; i++) {
1448 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1449 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1451 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1452 manage(wins[i], &wa);
1454 for(i = 0; i < num; i++) { /* now the transients */
1455 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1457 if(XGetTransientForHint(dpy, wins[i], &d1)
1458 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1459 manage(wins[i], &wa);
1467 sendmon(Client *c, Monitor *m) {
1474 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1482 setclientstate(Client *c, long state) {
1483 long data[] = { state, None };
1485 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1486 PropModeReplace, (unsigned char *)data, 2);
1490 sendevent(Client *c, Atom proto) {
1493 Bool exists = False;
1496 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1497 while(!exists && n--)
1498 exists = protocols[n] == proto;
1502 ev.type = ClientMessage;
1503 ev.xclient.window = c->win;
1504 ev.xclient.message_type = wmatom[WMProtocols];
1505 ev.xclient.format = 32;
1506 ev.xclient.data.l[0] = proto;
1507 ev.xclient.data.l[1] = CurrentTime;
1508 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1514 setfocus(Client *c) {
1515 if(!c->neverfocus) {
1516 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1517 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1518 XA_WINDOW, 32, PropModeReplace,
1519 (unsigned char *) &(c->win), 1);
1521 sendevent(c, wmatom[WMTakeFocus]);
1525 setfullscreen(Client *c, Bool fullscreen) {
1527 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1528 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1529 c->isfullscreen = True;
1530 c->oldstate = c->isfloating;
1533 c->isfloating = True;
1534 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1535 XRaiseWindow(dpy, c->win);
1538 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1539 PropModeReplace, (unsigned char*)0, 0);
1540 c->isfullscreen = False;
1541 c->isfloating = c->oldstate;
1547 resizeclient(c, c->x, c->y, c->w, c->h);
1553 setlayout(const Arg *arg) {
1554 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1557 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1558 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1565 /* arg > 1.0 will set mfact absolutly */
1567 setmfact(const Arg *arg) {
1570 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1572 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1573 if(f < 0.1 || f > 0.9)
1581 XSetWindowAttributes wa;
1583 /* clean up any zombies immediately */
1587 screen = DefaultScreen(dpy);
1588 root = RootWindow(dpy, screen);
1590 sw = DisplayWidth(dpy, screen);
1591 sh = DisplayHeight(dpy, screen);
1592 bh = dc.h = dc.font.height + 2;
1595 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1596 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1597 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1598 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1599 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1600 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1601 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1602 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1603 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1604 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1605 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1606 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1608 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1609 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1610 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1611 /* init appearance */
1612 dc.norm[ColBorder] = getcolor(normbordercolor);
1613 dc.norm[ColBG] = getcolor(normbgcolor);
1614 dc.norm[ColFG] = getcolor(normfgcolor);
1615 dc.sel[ColBorder] = getcolor(selbordercolor);
1616 dc.sel[ColBG] = getcolor(selbgcolor);
1617 dc.sel[ColFG] = getcolor(selfgcolor);
1618 dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
1619 dc.gc = XCreateGC(dpy, root, 0, NULL);
1620 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1624 /* EWMH support per view */
1625 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1626 PropModeReplace, (unsigned char *) netatom, NetLast);
1627 XDeleteProperty(dpy, root, netatom[NetClientList]);
1628 /* select for events */
1629 wa.cursor = cursor[CurNormal];
1630 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
1631 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1632 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1633 XSelectInput(dpy, root, wa.event_mask);
1638 showhide(Client *c) {
1641 if(ISVISIBLE(c)) { /* show clients top down */
1642 XMoveWindow(dpy, c->win, c->x, c->y);
1643 if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1644 resize(c, c->x, c->y, c->w, c->h, False);
1647 else { /* hide clients bottom up */
1649 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1654 sigchld(int unused) {
1655 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1656 die("Can't install SIGCHLD handler");
1657 while(0 < waitpid(-1, NULL, WNOHANG));
1661 spawn(const Arg *arg) {
1664 close(ConnectionNumber(dpy));
1666 execvp(((char **)arg->v)[0], (char **)arg->v);
1667 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1674 tag(const Arg *arg) {
1675 if(selmon->sel && arg->ui & TAGMASK) {
1676 selmon->sel->tags = arg->ui & TAGMASK;
1683 tagmon(const Arg *arg) {
1684 if(!selmon->sel || !mons->next)
1686 sendmon(selmon->sel, dirtomon(arg->i));
1690 textnw(const char *text, unsigned int len) {
1692 XftTextExtentsUtf8(dpy, dc.font.xfont, (XftChar8 *) text, len, &ext);
1698 unsigned int i, n, h, mw, my, ty;
1701 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1706 mw = m->nmaster ? m->ww * m->mfact : 0;
1709 for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1710 if(i < m->nmaster) {
1711 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1712 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False);
1716 h = (m->wh - ty) / (n - i);
1717 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), False);
1723 togglebar(const Arg *arg) {
1724 selmon->showbar = !selmon->showbar;
1725 updatebarpos(selmon);
1726 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1731 togglefloating(const Arg *arg) {
1734 if(selmon->sel->isfullscreen) /* no support for fullscreen windows */
1736 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1737 if(selmon->sel->isfloating)
1738 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1739 selmon->sel->w, selmon->sel->h, False);
1744 toggletag(const Arg *arg) {
1745 unsigned int newtags;
1749 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1751 selmon->sel->tags = newtags;
1758 toggleview(const Arg *arg) {
1759 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1762 selmon->tagset[selmon->seltags] = newtagset;
1769 unfocus(Client *c, Bool setfocus) {
1772 grabbuttons(c, False);
1773 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder].pixel);
1775 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1776 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1781 unmanage(Client *c, Bool destroyed) {
1782 Monitor *m = c->mon;
1785 /* The server grab construct avoids race conditions. */
1789 wc.border_width = c->oldbw;
1791 XSetErrorHandler(xerrordummy);
1792 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1793 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1794 setclientstate(c, WithdrawnState);
1796 XSetErrorHandler(xerror);
1806 unmapnotify(XEvent *e) {
1808 XUnmapEvent *ev = &e->xunmap;
1810 if((c = wintoclient(ev->window))) {
1812 setclientstate(c, WithdrawnState);
1821 XSetWindowAttributes wa = {
1822 .override_redirect = True,
1823 .background_pixmap = ParentRelative,
1824 .event_mask = ButtonPressMask|ExposureMask
1826 for(m = mons; m; m = m->next) {
1829 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1830 CopyFromParent, DefaultVisual(dpy, screen),
1831 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1832 XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
1833 XMapRaised(dpy, m->barwin);
1838 updatebarpos(Monitor *m) {
1843 m->by = m->topbar ? m->wy : m->wy + m->wh;
1844 m->wy = m->topbar ? m->wy + bh : m->wy;
1851 updateclientlist() {
1855 XDeleteProperty(dpy, root, netatom[NetClientList]);
1856 for(m = mons; m; m = m->next)
1857 for(c = m->clients; c; c = c->next)
1858 XChangeProperty(dpy, root, netatom[NetClientList],
1859 XA_WINDOW, 32, PropModeAppend,
1860 (unsigned char *) &(c->win), 1);
1868 if(XineramaIsActive(dpy)) {
1872 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1873 XineramaScreenInfo *unique = NULL;
1875 for(n = 0, m = mons; m; m = m->next, n++);
1876 /* only consider unique geometries as separate screens */
1877 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
1878 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
1879 for(i = 0, j = 0; i < nn; i++)
1880 if(isuniquegeom(unique, j, &info[i]))
1881 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1885 for(i = 0; i < (nn - n); i++) { /* new monitors available */
1886 for(m = mons; m && m->next; m = m->next);
1888 m->next = createmon();
1892 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
1894 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1895 || unique[i].width != m->mw || unique[i].height != m->mh))
1899 m->mx = m->wx = unique[i].x_org;
1900 m->my = m->wy = unique[i].y_org;
1901 m->mw = m->ww = unique[i].width;
1902 m->mh = m->wh = unique[i].height;
1906 else { /* less monitors available nn < n */
1907 for(i = nn; i < n; i++) {
1908 for(m = mons; m && m->next; m = m->next);
1912 m->clients = c->next;
1926 #endif /* XINERAMA */
1927 /* default monitor setup */
1931 if(mons->mw != sw || mons->mh != sh) {
1933 mons->mw = mons->ww = sw;
1934 mons->mh = mons->wh = sh;
1940 selmon = wintomon(root);
1946 updatenumlockmask(void) {
1948 XModifierKeymap *modmap;
1951 modmap = XGetModifierMapping(dpy);
1952 for(i = 0; i < 8; i++)
1953 for(j = 0; j < modmap->max_keypermod; j++)
1954 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1955 == XKeysymToKeycode(dpy, XK_Num_Lock))
1956 numlockmask = (1 << i);
1957 XFreeModifiermap(modmap);
1961 updatesizehints(Client *c) {
1965 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
1966 /* size is uninitialized, ensure that size.flags aren't used */
1968 if(size.flags & PBaseSize) {
1969 c->basew = size.base_width;
1970 c->baseh = size.base_height;
1972 else if(size.flags & PMinSize) {
1973 c->basew = size.min_width;
1974 c->baseh = size.min_height;
1977 c->basew = c->baseh = 0;
1978 if(size.flags & PResizeInc) {
1979 c->incw = size.width_inc;
1980 c->inch = size.height_inc;
1983 c->incw = c->inch = 0;
1984 if(size.flags & PMaxSize) {
1985 c->maxw = size.max_width;
1986 c->maxh = size.max_height;
1989 c->maxw = c->maxh = 0;
1990 if(size.flags & PMinSize) {
1991 c->minw = size.min_width;
1992 c->minh = size.min_height;
1994 else if(size.flags & PBaseSize) {
1995 c->minw = size.base_width;
1996 c->minh = size.base_height;
1999 c->minw = c->minh = 0;
2000 if(size.flags & PAspect) {
2001 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2002 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2005 c->maxa = c->mina = 0.0;
2006 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
2007 && c->maxw == c->minw && c->maxh == c->minh);
2011 updatetitle(Client *c) {
2012 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2013 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2014 if(c->name[0] == '\0') /* hack to mark broken clients */
2015 strcpy(c->name, broken);
2019 updatestatus(void) {
2020 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2021 strcpy(stext, "dwm-"VERSION);
2026 updatewindowtype(Client *c) {
2027 Atom state = getatomprop(c, netatom[NetWMState]);
2028 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2030 if(state == netatom[NetWMFullscreen])
2031 setfullscreen(c, True);
2032 if(wtype == netatom[NetWMWindowTypeDialog])
2033 c->isfloating = True;
2037 updatewmhints(Client *c) {
2040 if((wmh = XGetWMHints(dpy, c->win))) {
2041 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
2042 wmh->flags &= ~XUrgencyHint;
2043 XSetWMHints(dpy, c->win, wmh);
2046 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
2047 if(wmh->flags & InputHint)
2048 c->neverfocus = !wmh->input;
2050 c->neverfocus = False;
2056 view(const Arg *arg) {
2057 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2059 selmon->seltags ^= 1; /* toggle sel tagset */
2060 if(arg->ui & TAGMASK)
2061 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2067 wintoclient(Window w) {
2071 for(m = mons; m; m = m->next)
2072 for(c = m->clients; c; c = c->next)
2079 wintomon(Window w) {
2084 if(w == root && getrootptr(&x, &y))
2085 return recttomon(x, y, 1, 1);
2086 for(m = mons; m; m = m->next)
2089 if((c = wintoclient(w)))
2094 /* There's no way to check accesses to destroyed windows, thus those cases are
2095 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2096 * default error handler, which may call exit. */
2098 xerror(Display *dpy, XErrorEvent *ee) {
2099 if(ee->error_code == BadWindow
2100 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2101 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2102 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2103 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2104 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2105 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2106 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2107 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2109 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2110 ee->request_code, ee->error_code);
2111 return xerrorxlib(dpy, ee); /* may call exit */
2115 xerrordummy(Display *dpy, XErrorEvent *ee) {
2119 /* Startup Error handler to check if another window manager
2120 * is already running. */
2122 xerrorstart(Display *dpy, XErrorEvent *ee) {
2123 die("dwm: another window manager is already running\n");
2128 zoom(const Arg *arg) {
2129 Client *c = selmon->sel;
2131 if(!selmon->lt[selmon->sellt]->arrange
2132 || (selmon->sel && selmon->sel->isfloating))
2134 if(c == nexttiled(selmon->clients))
2135 if(!c || !(c = nexttiled(c->next)))
2141 main(int argc, char *argv[]) {
2142 if(argc == 2 && !strcmp("-v", argv[1]))
2143 die("dwm-"VERSION", © 2006-2012 dwm engineers, see LICENSE for details\n");
2145 die("usage: dwm [-v]\n");
2146 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2147 fputs("warning: no locale support\n", stderr);
2148 if(!(dpy = XOpenDisplay(NULL)))
2149 die("dwm: cannot open display\n");
2156 return EXIT_SUCCESS;