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, usexkb;
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) {
1073 keysym = XkbKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0, 0);
1075 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1076 for(i = 0; i < LENGTH(keys); i++)
1077 if(keysym == keys[i].keysym
1078 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1080 keys[i].func(&(keys[i].arg));
1084 killclient(const Arg *arg) {
1087 if(!sendevent(selmon->sel, wmatom[WMDelete])) {
1089 XSetErrorHandler(xerrordummy);
1090 XSetCloseDownMode(dpy, DestroyAll);
1091 XKillClient(dpy, selmon->sel->win);
1093 XSetErrorHandler(xerror);
1099 manage(Window w, XWindowAttributes *wa) {
1100 Client *c, *t = NULL;
1101 Window trans = None;
1104 if(!(c = calloc(1, sizeof(Client))))
1105 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1108 if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1117 c->x = c->oldx = wa->x;
1118 c->y = c->oldy = wa->y;
1119 c->w = c->oldw = wa->width;
1120 c->h = c->oldh = wa->height;
1121 c->oldbw = wa->border_width;
1123 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1124 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1125 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1126 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1127 c->x = MAX(c->x, c->mon->mx);
1128 /* only fix client y-offset, if the client center might cover the bar */
1129 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1130 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1133 wc.border_width = c->bw;
1134 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1135 XSetWindowBorder(dpy, w, dc.norm[ColBorder].pixel);
1136 configure(c); /* propagates border_width, if size doesn't change */
1137 updatewindowtype(c);
1140 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1141 grabbuttons(c, False);
1143 c->isfloating = c->oldstate = trans != None || c->isfixed;
1145 XRaiseWindow(dpy, c->win);
1148 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1149 (unsigned char *) &(c->win), 1);
1150 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1151 setclientstate(c, NormalState);
1152 if (c->mon == selmon)
1153 unfocus(selmon->sel, False);
1156 XMapWindow(dpy, c->win);
1161 mappingnotify(XEvent *e) {
1162 XMappingEvent *ev = &e->xmapping;
1164 XRefreshKeyboardMapping(ev);
1165 if(ev->request == MappingKeyboard)
1170 maprequest(XEvent *e) {
1171 static XWindowAttributes wa;
1172 XMapRequestEvent *ev = &e->xmaprequest;
1174 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1176 if(wa.override_redirect)
1178 if(!wintoclient(ev->window))
1179 manage(ev->window, &wa);
1183 monocle(Monitor *m) {
1187 for(c = m->clients; c; c = c->next)
1190 if(n > 0) /* override layout symbol */
1191 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1192 for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
1193 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
1197 motionnotify(XEvent *e) {
1198 static Monitor *mon = NULL;
1200 XMotionEvent *ev = &e->xmotion;
1202 if(ev->window != root)
1204 if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1205 unfocus(selmon->sel, True);
1213 movemouse(const Arg *arg) {
1214 int x, y, ocx, ocy, nx, ny;
1219 if(!(c = selmon->sel))
1221 if(c->isfullscreen) /* no support moving fullscreen windows by mouse */
1226 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1227 None, cursor[CurMove], CurrentTime) != GrabSuccess)
1229 if(!getrootptr(&x, &y))
1232 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1234 case ConfigureRequest:
1237 handler[ev.type](&ev);
1240 nx = ocx + (ev.xmotion.x - x);
1241 ny = ocy + (ev.xmotion.y - y);
1242 if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1243 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1244 if(abs(selmon->wx - nx) < snap)
1246 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1247 nx = selmon->wx + selmon->ww - WIDTH(c);
1248 if(abs(selmon->wy - ny) < snap)
1250 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1251 ny = selmon->wy + selmon->wh - HEIGHT(c);
1252 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1253 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1254 togglefloating(NULL);
1256 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1257 resize(c, nx, ny, c->w, c->h, True);
1260 } while(ev.type != ButtonRelease);
1261 XUngrabPointer(dpy, CurrentTime);
1262 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1270 nexttiled(Client *c) {
1271 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1284 propertynotify(XEvent *e) {
1287 XPropertyEvent *ev = &e->xproperty;
1289 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1291 else if(ev->state == PropertyDelete)
1292 return; /* ignore */
1293 else if((c = wintoclient(ev->window))) {
1296 case XA_WM_TRANSIENT_FOR:
1297 if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1298 (c->isfloating = (wintoclient(trans)) != NULL))
1301 case XA_WM_NORMAL_HINTS:
1309 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1311 if(c == c->mon->sel)
1314 if(ev->atom == netatom[NetWMWindowType])
1315 updatewindowtype(c);
1320 quit(const Arg *arg) {
1325 recttomon(int x, int y, int w, int h) {
1326 Monitor *m, *r = selmon;
1329 for(m = mons; m; m = m->next)
1330 if((a = INTERSECT(x, y, w, h, m)) > area) {
1338 resize(Client *c, int x, int y, int w, int h, Bool interact) {
1339 if(applysizehints(c, &x, &y, &w, &h, interact))
1340 resizeclient(c, x, y, w, h);
1344 resizeclient(Client *c, int x, int y, int w, int h) {
1347 c->oldx = c->x; c->x = wc.x = x;
1348 c->oldy = c->y; c->y = wc.y = y;
1349 c->oldw = c->w; c->w = wc.width = w;
1350 c->oldh = c->h; c->h = wc.height = h;
1351 wc.border_width = c->bw;
1352 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1358 resizemouse(const Arg *arg) {
1365 if(!(c = selmon->sel))
1367 if(c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1372 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1373 None, cursor[CurResize], CurrentTime) != GrabSuccess)
1375 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1377 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1379 case ConfigureRequest:
1382 handler[ev.type](&ev);
1385 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1386 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1387 if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1388 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1390 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1391 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1392 togglefloating(NULL);
1394 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1395 resize(c, c->x, c->y, nw, nh, True);
1398 } while(ev.type != ButtonRelease);
1399 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1400 XUngrabPointer(dpy, CurrentTime);
1401 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1402 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1410 restack(Monitor *m) {
1418 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1419 XRaiseWindow(dpy, m->sel->win);
1420 if(m->lt[m->sellt]->arrange) {
1421 wc.stack_mode = Below;
1422 wc.sibling = m->barwin;
1423 for(c = m->stack; c; c = c->snext)
1424 if(!c->isfloating && ISVISIBLE(c)) {
1425 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1426 wc.sibling = c->win;
1430 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1436 /* main event loop */
1438 while(running && !XNextEvent(dpy, &ev))
1439 if(handler[ev.type])
1440 handler[ev.type](&ev); /* call handler */
1445 unsigned int i, num;
1446 Window d1, d2, *wins = NULL;
1447 XWindowAttributes wa;
1449 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1450 for(i = 0; i < num; i++) {
1451 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1452 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1454 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1455 manage(wins[i], &wa);
1457 for(i = 0; i < num; i++) { /* now the transients */
1458 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1460 if(XGetTransientForHint(dpy, wins[i], &d1)
1461 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1462 manage(wins[i], &wa);
1470 sendmon(Client *c, Monitor *m) {
1477 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1485 setclientstate(Client *c, long state) {
1486 long data[] = { state, None };
1488 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1489 PropModeReplace, (unsigned char *)data, 2);
1493 sendevent(Client *c, Atom proto) {
1496 Bool exists = False;
1499 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1500 while(!exists && n--)
1501 exists = protocols[n] == proto;
1505 ev.type = ClientMessage;
1506 ev.xclient.window = c->win;
1507 ev.xclient.message_type = wmatom[WMProtocols];
1508 ev.xclient.format = 32;
1509 ev.xclient.data.l[0] = proto;
1510 ev.xclient.data.l[1] = CurrentTime;
1511 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1517 setfocus(Client *c) {
1518 if(!c->neverfocus) {
1519 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1520 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1521 XA_WINDOW, 32, PropModeReplace,
1522 (unsigned char *) &(c->win), 1);
1524 sendevent(c, wmatom[WMTakeFocus]);
1528 setfullscreen(Client *c, Bool fullscreen) {
1530 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1531 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1532 c->isfullscreen = True;
1533 c->oldstate = c->isfloating;
1536 c->isfloating = True;
1537 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1538 XRaiseWindow(dpy, c->win);
1541 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1542 PropModeReplace, (unsigned char*)0, 0);
1543 c->isfullscreen = False;
1544 c->isfloating = c->oldstate;
1550 resizeclient(c, c->x, c->y, c->w, c->h);
1556 setlayout(const Arg *arg) {
1557 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1560 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1561 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1568 /* arg > 1.0 will set mfact absolutly */
1570 setmfact(const Arg *arg) {
1573 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1575 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1576 if(f < 0.1 || f > 0.9)
1584 XSetWindowAttributes wa;
1585 int dummy = 0, xkbmajor = XkbMajorVersion, xkbminor = XkbMinorVersion;
1587 /* clean up any zombies immediately */
1591 screen = DefaultScreen(dpy);
1592 root = RootWindow(dpy, screen);
1594 sw = DisplayWidth(dpy, screen);
1595 sh = DisplayHeight(dpy, screen);
1596 bh = dc.h = dc.font.height + 2;
1599 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1600 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1601 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1602 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1603 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1604 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1605 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1606 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1607 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1608 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1609 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1610 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1612 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1613 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1614 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1615 /* init appearance */
1616 dc.norm[ColBorder] = getcolor(normbordercolor);
1617 dc.norm[ColBG] = getcolor(normbgcolor);
1618 dc.norm[ColFG] = getcolor(normfgcolor);
1619 dc.sel[ColBorder] = getcolor(selbordercolor);
1620 dc.sel[ColBG] = getcolor(selbgcolor);
1621 dc.sel[ColFG] = getcolor(selfgcolor);
1622 dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
1623 dc.gc = XCreateGC(dpy, root, 0, NULL);
1624 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1628 /* EWMH support per view */
1629 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1630 PropModeReplace, (unsigned char *) netatom, NetLast);
1631 XDeleteProperty(dpy, root, netatom[NetClientList]);
1632 /* select for events */
1633 wa.cursor = cursor[CurNormal];
1634 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
1635 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1636 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1637 XSelectInput(dpy, root, wa.event_mask);
1639 usexkb = XkbQueryExtension(dpy, &dummy, &dummy, &dummy, &xkbmajor, &xkbminor);
1644 showhide(Client *c) {
1647 if(ISVISIBLE(c)) { /* show clients top down */
1648 XMoveWindow(dpy, c->win, c->x, c->y);
1649 if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1650 resize(c, c->x, c->y, c->w, c->h, False);
1653 else { /* hide clients bottom up */
1655 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1660 sigchld(int unused) {
1661 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1662 die("Can't install SIGCHLD handler");
1663 while(0 < waitpid(-1, NULL, WNOHANG));
1667 spawn(const Arg *arg) {
1670 close(ConnectionNumber(dpy));
1672 execvp(((char **)arg->v)[0], (char **)arg->v);
1673 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1680 tag(const Arg *arg) {
1681 if(selmon->sel && arg->ui & TAGMASK) {
1682 selmon->sel->tags = arg->ui & TAGMASK;
1689 tagmon(const Arg *arg) {
1690 if(!selmon->sel || !mons->next)
1692 sendmon(selmon->sel, dirtomon(arg->i));
1696 textnw(const char *text, unsigned int len) {
1698 XftTextExtentsUtf8(dpy, dc.font.xfont, (XftChar8 *) text, len, &ext);
1704 unsigned int i, n, h, mw, my, ty;
1707 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1712 mw = m->nmaster ? m->ww * m->mfact : 0;
1715 for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1716 if(i < m->nmaster) {
1717 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1718 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False);
1722 h = (m->wh - ty) / (n - i);
1723 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), False);
1729 togglebar(const Arg *arg) {
1730 selmon->showbar = !selmon->showbar;
1731 updatebarpos(selmon);
1732 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1737 togglefloating(const Arg *arg) {
1740 if(selmon->sel->isfullscreen) /* no support for fullscreen windows */
1742 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1743 if(selmon->sel->isfloating)
1744 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1745 selmon->sel->w, selmon->sel->h, False);
1750 toggletag(const Arg *arg) {
1751 unsigned int newtags;
1755 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1757 selmon->sel->tags = newtags;
1764 toggleview(const Arg *arg) {
1765 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1768 selmon->tagset[selmon->seltags] = newtagset;
1775 unfocus(Client *c, Bool setfocus) {
1778 grabbuttons(c, False);
1779 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder].pixel);
1781 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1782 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1787 unmanage(Client *c, Bool destroyed) {
1788 Monitor *m = c->mon;
1791 /* The server grab construct avoids race conditions. */
1795 wc.border_width = c->oldbw;
1797 XSetErrorHandler(xerrordummy);
1798 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1799 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1800 setclientstate(c, WithdrawnState);
1802 XSetErrorHandler(xerror);
1812 unmapnotify(XEvent *e) {
1814 XUnmapEvent *ev = &e->xunmap;
1816 if((c = wintoclient(ev->window))) {
1818 setclientstate(c, WithdrawnState);
1827 XSetWindowAttributes wa = {
1828 .override_redirect = True,
1829 .background_pixmap = ParentRelative,
1830 .event_mask = ButtonPressMask|ExposureMask
1832 for(m = mons; m; m = m->next) {
1835 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1836 CopyFromParent, DefaultVisual(dpy, screen),
1837 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1838 XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
1839 XMapRaised(dpy, m->barwin);
1844 updatebarpos(Monitor *m) {
1849 m->by = m->topbar ? m->wy : m->wy + m->wh;
1850 m->wy = m->topbar ? m->wy + bh : m->wy;
1857 updateclientlist() {
1861 XDeleteProperty(dpy, root, netatom[NetClientList]);
1862 for(m = mons; m; m = m->next)
1863 for(c = m->clients; c; c = c->next)
1864 XChangeProperty(dpy, root, netatom[NetClientList],
1865 XA_WINDOW, 32, PropModeAppend,
1866 (unsigned char *) &(c->win), 1);
1874 if(XineramaIsActive(dpy)) {
1878 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1879 XineramaScreenInfo *unique = NULL;
1881 for(n = 0, m = mons; m; m = m->next, n++);
1882 /* only consider unique geometries as separate screens */
1883 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
1884 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
1885 for(i = 0, j = 0; i < nn; i++)
1886 if(isuniquegeom(unique, j, &info[i]))
1887 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1891 for(i = 0; i < (nn - n); i++) { /* new monitors available */
1892 for(m = mons; m && m->next; m = m->next);
1894 m->next = createmon();
1898 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
1900 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1901 || unique[i].width != m->mw || unique[i].height != m->mh))
1905 m->mx = m->wx = unique[i].x_org;
1906 m->my = m->wy = unique[i].y_org;
1907 m->mw = m->ww = unique[i].width;
1908 m->mh = m->wh = unique[i].height;
1912 else { /* less monitors available nn < n */
1913 for(i = nn; i < n; i++) {
1914 for(m = mons; m && m->next; m = m->next);
1918 m->clients = c->next;
1932 #endif /* XINERAMA */
1933 /* default monitor setup */
1937 if(mons->mw != sw || mons->mh != sh) {
1939 mons->mw = mons->ww = sw;
1940 mons->mh = mons->wh = sh;
1946 selmon = wintomon(root);
1952 updatenumlockmask(void) {
1954 XModifierKeymap *modmap;
1957 modmap = XGetModifierMapping(dpy);
1958 for(i = 0; i < 8; i++)
1959 for(j = 0; j < modmap->max_keypermod; j++)
1960 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1961 == XKeysymToKeycode(dpy, XK_Num_Lock))
1962 numlockmask = (1 << i);
1963 XFreeModifiermap(modmap);
1967 updatesizehints(Client *c) {
1971 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
1972 /* size is uninitialized, ensure that size.flags aren't used */
1974 if(size.flags & PBaseSize) {
1975 c->basew = size.base_width;
1976 c->baseh = size.base_height;
1978 else if(size.flags & PMinSize) {
1979 c->basew = size.min_width;
1980 c->baseh = size.min_height;
1983 c->basew = c->baseh = 0;
1984 if(size.flags & PResizeInc) {
1985 c->incw = size.width_inc;
1986 c->inch = size.height_inc;
1989 c->incw = c->inch = 0;
1990 if(size.flags & PMaxSize) {
1991 c->maxw = size.max_width;
1992 c->maxh = size.max_height;
1995 c->maxw = c->maxh = 0;
1996 if(size.flags & PMinSize) {
1997 c->minw = size.min_width;
1998 c->minh = size.min_height;
2000 else if(size.flags & PBaseSize) {
2001 c->minw = size.base_width;
2002 c->minh = size.base_height;
2005 c->minw = c->minh = 0;
2006 if(size.flags & PAspect) {
2007 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2008 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2011 c->maxa = c->mina = 0.0;
2012 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
2013 && c->maxw == c->minw && c->maxh == c->minh);
2017 updatetitle(Client *c) {
2018 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2019 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2020 if(c->name[0] == '\0') /* hack to mark broken clients */
2021 strcpy(c->name, broken);
2025 updatestatus(void) {
2026 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2027 strcpy(stext, "dwm-"VERSION);
2032 updatewindowtype(Client *c) {
2033 Atom state = getatomprop(c, netatom[NetWMState]);
2034 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2036 if(state == netatom[NetWMFullscreen])
2037 setfullscreen(c, True);
2038 if(wtype == netatom[NetWMWindowTypeDialog])
2039 c->isfloating = True;
2043 updatewmhints(Client *c) {
2046 if((wmh = XGetWMHints(dpy, c->win))) {
2047 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
2048 wmh->flags &= ~XUrgencyHint;
2049 XSetWMHints(dpy, c->win, wmh);
2052 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
2053 if(wmh->flags & InputHint)
2054 c->neverfocus = !wmh->input;
2056 c->neverfocus = False;
2062 view(const Arg *arg) {
2063 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2065 selmon->seltags ^= 1; /* toggle sel tagset */
2066 if(arg->ui & TAGMASK)
2067 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2073 wintoclient(Window w) {
2077 for(m = mons; m; m = m->next)
2078 for(c = m->clients; c; c = c->next)
2085 wintomon(Window w) {
2090 if(w == root && getrootptr(&x, &y))
2091 return recttomon(x, y, 1, 1);
2092 for(m = mons; m; m = m->next)
2095 if((c = wintoclient(w)))
2100 /* There's no way to check accesses to destroyed windows, thus those cases are
2101 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2102 * default error handler, which may call exit. */
2104 xerror(Display *dpy, XErrorEvent *ee) {
2105 if(ee->error_code == BadWindow
2106 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2107 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2108 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2109 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2110 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2111 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2112 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2113 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2115 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2116 ee->request_code, ee->error_code);
2117 return xerrorxlib(dpy, ee); /* may call exit */
2121 xerrordummy(Display *dpy, XErrorEvent *ee) {
2125 /* Startup Error handler to check if another window manager
2126 * is already running. */
2128 xerrorstart(Display *dpy, XErrorEvent *ee) {
2129 die("dwm: another window manager is already running\n");
2134 zoom(const Arg *arg) {
2135 Client *c = selmon->sel;
2137 if(!selmon->lt[selmon->sellt]->arrange
2138 || (selmon->sel && selmon->sel->isfloating))
2140 if(c == nexttiled(selmon->clients))
2141 if(!c || !(c = nexttiled(c->next)))
2147 main(int argc, char *argv[]) {
2148 if(argc == 2 && !strcmp("-v", argv[1]))
2149 die("dwm-"VERSION", © 2006-2012 dwm engineers, see LICENSE for details\n");
2151 die("usage: dwm [-v]\n");
2152 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2153 fputs("warning: no locale support\n", stderr);
2154 if(!(dpy = XOpenDisplay(NULL)))
2155 die("dwm: cannot open display\n");
2162 return EXIT_SUCCESS;