1 /* See LICENSE file for copyright and license details.
3 * dynamic window manager is designed like any other X client as well. It is
4 * driven through handling X events. In contrast to other X clients, a window
5 * manager selects for SubstructureRedirectMask on the root window, to receive
6 * events about window (dis-)appearance. Only one X connection at a time is
7 * allowed to select for this event mask.
9 * The event handlers of dwm are organized in an array which is accessed
10 * whenever a new event has been fetched. This allows event dispatching
13 * Each child of the root window is called a client, except windows which have
14 * set the override_redirect flag. Clients are organized in a linked client
15 * list on each monitor, the focus history is remembered through a stack list
16 * on each monitor. Each client contains a bit array to indicate the tags of a
19 * Keys and tagging rules are organized as arrays and defined in config.h.
21 * To understand everything else, start reading main().
31 #include <sys/types.h>
33 #include <X11/cursorfont.h>
34 #include <X11/keysym.h>
35 #include <X11/Xatom.h>
37 #include <X11/Xproto.h>
38 #include <X11/Xutil.h>
40 #include <X11/extensions/Xinerama.h>
44 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
45 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
46 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
47 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
48 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
49 #define LENGTH(X) (sizeof X / sizeof X[0])
50 #define MAX(A, B) ((A) > (B) ? (A) : (B))
51 #define MIN(A, B) ((A) < (B) ? (A) : (B))
52 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
53 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
54 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
55 #define TAGMASK ((1 << LENGTH(tags)) - 1)
56 #define TEXTW(X) (textnw(X, strlen(X)) + dc.font.height)
59 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
60 enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
61 enum { NetSupported, NetWMName, NetWMState,
62 NetWMFullscreen, NetActiveWindow, NetWMWindowType,
63 NetWMWindowTypeDialog, NetLast }; /* EWMH atoms */
64 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
65 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
66 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
79 void (*func)(const Arg *arg);
83 typedef struct Monitor Monitor;
84 typedef struct Client Client;
89 int oldx, oldy, oldw, oldh;
90 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
93 Bool isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
102 unsigned long norm[ColLast];
103 unsigned long sel[ColLast];
113 } DC; /* draw context */
118 void (*func)(const Arg *);
124 void (*arrange)(Monitor *);
132 int by; /* bar geometry */
133 int mx, my, mw, mh; /* screen size */
134 int wx, wy, ww, wh; /* window area */
135 unsigned int seltags;
137 unsigned int tagset[2];
150 const char *instance;
157 /* function declarations */
158 static void applyrules(Client *c);
159 static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
160 static void arrange(Monitor *m);
161 static void arrangemon(Monitor *m);
162 static void attach(Client *c);
163 static void attachstack(Client *c);
164 static void buttonpress(XEvent *e);
165 static void checkotherwm(void);
166 static void cleanup(void);
167 static void cleanupmon(Monitor *mon);
168 static void clearurgent(Client *c);
169 static void clientmessage(XEvent *e);
170 static void configure(Client *c);
171 static void configurenotify(XEvent *e);
172 static void configurerequest(XEvent *e);
173 static Monitor *createmon(void);
174 static void destroynotify(XEvent *e);
175 static void detach(Client *c);
176 static void detachstack(Client *c);
177 static void die(const char *errstr, ...);
178 static Monitor *dirtomon(int dir);
179 static void drawbar(Monitor *m);
180 static void drawbars(void);
181 static void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
182 static void drawtext(const char *text, unsigned long col[ColLast], Bool invert);
183 static void enternotify(XEvent *e);
184 static void expose(XEvent *e);
185 static void focus(Client *c);
186 static void focusin(XEvent *e);
187 static void focusmon(const Arg *arg);
188 static void focusstack(const Arg *arg);
189 static unsigned long getcolor(const char *colstr);
190 static Bool getrootptr(int *x, int *y);
191 static long getstate(Window w);
192 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
193 static void grabbuttons(Client *c, Bool focused);
194 static void grabkeys(void);
195 static void incnmaster(const Arg *arg);
196 static void initfont(const char *fontstr);
197 static void keypress(XEvent *e);
198 static void killclient(const Arg *arg);
199 static void manage(Window w, XWindowAttributes *wa);
200 static void mappingnotify(XEvent *e);
201 static void maprequest(XEvent *e);
202 static void monocle(Monitor *m);
203 static void movemouse(const Arg *arg);
204 static Client *nexttiled(Client *c);
205 static void pop(Client *);
206 static void propertynotify(XEvent *e);
207 static void quit(const Arg *arg);
208 static Monitor *recttomon(int x, int y, int w, int h);
209 static void resize(Client *c, int x, int y, int w, int h, Bool interact);
210 static void resizeclient(Client *c, int x, int y, int w, int h);
211 static void resizemouse(const Arg *arg);
212 static void restack(Monitor *m);
213 static void run(void);
214 static void scan(void);
215 static Bool sendevent(Client *c, Atom proto);
216 static void sendmon(Client *c, Monitor *m);
217 static void setclientstate(Client *c, long state);
218 static void setfocus(Client *c);
219 static void setfullscreen(Client *c, Bool fullscreen);
220 static void setlayout(const Arg *arg);
221 static void setmfact(const Arg *arg);
222 static void setup(void);
223 static void showhide(Client *c);
224 static void sigchld(int unused);
225 static void spawn(const Arg *arg);
226 static void tag(const Arg *arg);
227 static void tagmon(const Arg *arg);
228 static int textnw(const char *text, unsigned int len);
229 static void tile(Monitor *);
230 static void togglebar(const Arg *arg);
231 static void togglefloating(const Arg *arg);
232 static void toggletag(const Arg *arg);
233 static void toggleview(const Arg *arg);
234 static void unfocus(Client *c, Bool setfocus);
235 static void unmanage(Client *c, Bool destroyed);
236 static void unmapnotify(XEvent *e);
237 static Bool updategeom(void);
238 static void updatebarpos(Monitor *m);
239 static void updatebars(void);
240 static void updatenumlockmask(void);
241 static void updatesizehints(Client *c);
242 static void updatestatus(void);
243 static void updatewindowtype(Client *c);
244 static void updatetitle(Client *c);
245 static void updatewmhints(Client *c);
246 static void view(const Arg *arg);
247 static Client *wintoclient(Window w);
248 static Monitor *wintomon(Window w);
249 static int xerror(Display *dpy, XErrorEvent *ee);
250 static int xerrordummy(Display *dpy, XErrorEvent *ee);
251 static int xerrorstart(Display *dpy, XErrorEvent *ee);
252 static void zoom(const Arg *arg);
255 static const char broken[] = "broken";
256 static char stext[256];
258 static int sw, sh; /* X display screen geometry width, height */
259 static int bh, blw = 0; /* bar geometry */
260 static int (*xerrorxlib)(Display *, XErrorEvent *);
261 static unsigned int numlockmask = 0;
262 static void (*handler[LASTEvent]) (XEvent *) = {
263 [ButtonPress] = buttonpress,
264 [ClientMessage] = clientmessage,
265 [ConfigureRequest] = configurerequest,
266 [ConfigureNotify] = configurenotify,
267 [DestroyNotify] = destroynotify,
268 [EnterNotify] = enternotify,
271 [KeyPress] = keypress,
272 [MappingNotify] = mappingnotify,
273 [MapRequest] = maprequest,
274 [PropertyNotify] = propertynotify,
275 [UnmapNotify] = unmapnotify
277 static Atom wmatom[WMLast], netatom[NetLast];
278 static Bool running = True;
279 static Cursor cursor[CurLast];
282 static Monitor *mons = NULL, *selmon = NULL;
285 /* configuration, allows nested code to access above variables */
288 /* compile-time check if all tags fit into an unsigned int bit array. */
289 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
291 /* function implementations */
293 applyrules(Client *c) {
294 const char *class, *instance;
298 XClassHint ch = { NULL, NULL };
301 c->isfloating = c->tags = 0;
302 XGetClassHint(dpy, c->win, &ch);
303 class = ch.res_class ? ch.res_class : broken;
304 instance = ch.res_name ? ch.res_name : broken;
306 for(i = 0; i < LENGTH(rules); i++) {
308 if((!r->title || strstr(c->name, r->title))
309 && (!r->class || strstr(class, r->class))
310 && (!r->instance || strstr(instance, r->instance)))
312 c->isfloating = r->isfloating;
314 for(m = mons; m && m->num != r->monitor; m = m->next);
323 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
327 applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
331 /* set minimum possible */
339 if(*x + *w + 2 * c->bw < 0)
341 if(*y + *h + 2 * c->bw < 0)
345 if(*x >= m->wx + m->ww)
346 *x = m->wx + m->ww - WIDTH(c);
347 if(*y >= m->wy + m->wh)
348 *y = m->wy + m->wh - HEIGHT(c);
349 if(*x + *w + 2 * c->bw <= m->wx)
351 if(*y + *h + 2 * c->bw <= m->wy)
358 if(resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
359 /* see last two sentences in ICCCM 4.1.2.3 */
360 baseismin = c->basew == c->minw && c->baseh == c->minh;
361 if(!baseismin) { /* temporarily remove base dimensions */
365 /* adjust for aspect limits */
366 if(c->mina > 0 && c->maxa > 0) {
367 if(c->maxa < (float)*w / *h)
368 *w = *h * c->maxa + 0.5;
369 else if(c->mina < (float)*h / *w)
370 *h = *w * c->mina + 0.5;
372 if(baseismin) { /* increment calculation requires this */
376 /* adjust for increment value */
381 /* restore base dimensions */
382 *w = MAX(*w + c->basew, c->minw);
383 *h = MAX(*h + c->baseh, c->minh);
385 *w = MIN(*w, c->maxw);
387 *h = MIN(*h, c->maxh);
389 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
393 arrange(Monitor *m) {
396 else for(m = mons; m; m = m->next)
400 else for(m = mons; m; m = m->next)
405 arrangemon(Monitor *m) {
406 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
407 if(m->lt[m->sellt]->arrange)
408 m->lt[m->sellt]->arrange(m);
414 c->next = c->mon->clients;
419 attachstack(Client *c) {
420 c->snext = c->mon->stack;
425 buttonpress(XEvent *e) {
426 unsigned int i, x, click;
430 XButtonPressedEvent *ev = &e->xbutton;
433 /* focus monitor if necessary */
434 if((m = wintomon(ev->window)) && m != selmon) {
435 unfocus(selmon->sel, True);
439 if(ev->window == selmon->barwin) {
443 while(ev->x >= x && ++i < LENGTH(tags));
444 if(i < LENGTH(tags)) {
448 else if(ev->x < x + blw)
450 else if(ev->x > selmon->ww - TEXTW(stext))
451 click = ClkStatusText;
455 else if((c = wintoclient(ev->window))) {
457 click = ClkClientWin;
459 for(i = 0; i < LENGTH(buttons); i++)
460 if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
461 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
462 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
467 xerrorxlib = XSetErrorHandler(xerrorstart);
468 /* this causes an error if some other window manager is running */
469 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
471 XSetErrorHandler(xerror);
478 Layout foo = { "", NULL };
482 selmon->lt[selmon->sellt] = &foo;
483 for(m = mons; m; m = m->next)
485 unmanage(m->stack, False);
487 XFreeFontSet(dpy, dc.font.set);
489 XFreeFont(dpy, dc.font.xfont);
490 XUngrabKey(dpy, AnyKey, AnyModifier, root);
491 XFreePixmap(dpy, dc.drawable);
493 XFreeCursor(dpy, cursor[CurNormal]);
494 XFreeCursor(dpy, cursor[CurResize]);
495 XFreeCursor(dpy, cursor[CurMove]);
499 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
503 cleanupmon(Monitor *mon) {
509 for(m = mons; m && m->next != mon; m = m->next);
512 XUnmapWindow(dpy, mon->barwin);
513 XDestroyWindow(dpy, mon->barwin);
518 clearurgent(Client *c) {
522 if(!(wmh = XGetWMHints(dpy, c->win)))
524 wmh->flags &= ~XUrgencyHint;
525 XSetWMHints(dpy, c->win, wmh);
530 clientmessage(XEvent *e) {
531 XClientMessageEvent *cme = &e->xclient;
532 Client *c = wintoclient(cme->window);
536 if(cme->message_type == netatom[NetWMState]) {
537 if(cme->data.l[1] == netatom[NetWMFullscreen] || cme->data.l[2] == netatom[NetWMFullscreen])
538 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
539 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
541 else if(cme->message_type == netatom[NetActiveWindow]) {
543 c->mon->seltags ^= 1;
544 c->mon->tagset[c->mon->seltags] = c->tags;
551 configure(Client *c) {
554 ce.type = ConfigureNotify;
562 ce.border_width = c->bw;
564 ce.override_redirect = False;
565 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
569 configurenotify(XEvent *e) {
571 XConfigureEvent *ev = &e->xconfigure;
574 if(ev->window == root) {
575 dirty = (sw != ev->width);
578 if(updategeom() || dirty) {
580 XFreePixmap(dpy, dc.drawable);
581 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
583 for(m = mons; m; m = m->next)
584 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
592 configurerequest(XEvent *e) {
595 XConfigureRequestEvent *ev = &e->xconfigurerequest;
598 if((c = wintoclient(ev->window))) {
599 if(ev->value_mask & CWBorderWidth)
600 c->bw = ev->border_width;
601 else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
603 if(ev->value_mask & CWX) {
605 c->x = m->mx + ev->x;
607 if(ev->value_mask & CWY) {
609 c->y = m->my + ev->y;
611 if(ev->value_mask & CWWidth) {
615 if(ev->value_mask & CWHeight) {
619 if((c->x + c->w) > m->mx + m->mw && c->isfloating)
620 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
621 if((c->y + c->h) > m->my + m->mh && c->isfloating)
622 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
623 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
626 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
634 wc.width = ev->width;
635 wc.height = ev->height;
636 wc.border_width = ev->border_width;
637 wc.sibling = ev->above;
638 wc.stack_mode = ev->detail;
639 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
648 if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
649 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
650 m->tagset[0] = m->tagset[1] = 1;
652 m->nmaster = nmaster;
653 m->showbar = showbar;
655 m->lt[0] = &layouts[0];
656 m->lt[1] = &layouts[1 % LENGTH(layouts)];
657 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
662 destroynotify(XEvent *e) {
664 XDestroyWindowEvent *ev = &e->xdestroywindow;
666 if((c = wintoclient(ev->window)))
674 for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
679 detachstack(Client *c) {
682 for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
685 if(c == c->mon->sel) {
686 for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
692 die(const char *errstr, ...) {
695 va_start(ap, errstr);
696 vfprintf(stderr, errstr, ap);
706 if(!(m = selmon->next))
709 else if(selmon == mons)
710 for(m = mons; m->next; m = m->next);
712 for(m = mons; m->next != selmon; m = m->next);
717 drawbar(Monitor *m) {
719 unsigned int i, occ = 0, urg = 0;
723 for(c = m->clients; c; c = c->next) {
729 for(i = 0; i < LENGTH(tags); i++) {
730 dc.w = TEXTW(tags[i]);
731 col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
732 drawtext(tags[i], col, urg & 1 << i);
733 drawsquare(m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
734 occ & 1 << i, urg & 1 << i, col);
737 dc.w = blw = TEXTW(m->ltsymbol);
738 drawtext(m->ltsymbol, dc.norm, False);
741 if(m == selmon) { /* status is only drawn on selected monitor */
748 drawtext(stext, dc.norm, False);
752 if((dc.w = dc.x - x) > bh) {
755 col = m == selmon ? dc.sel : dc.norm;
756 drawtext(m->sel->name, col, False);
757 drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
760 drawtext(NULL, dc.norm, False);
762 XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
770 for(m = mons; m; m = m->next)
775 drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
778 XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
779 x = (dc.font.ascent + dc.font.descent + 2) / 4;
781 XFillRectangle(dpy, dc.drawable, dc.gc, dc.x+1, dc.y+1, x+1, x+1);
783 XDrawRectangle(dpy, dc.drawable, dc.gc, dc.x+1, dc.y+1, x, x);
787 drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
789 int i, x, y, h, len, olen;
791 XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
792 XFillRectangle(dpy, dc.drawable, dc.gc, dc.x, dc.y, dc.w, dc.h);
796 h = dc.font.ascent + dc.font.descent;
797 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
799 /* shorten text if necessary */
800 for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
803 memcpy(buf, text, len);
805 for(i = len; i && i > len - 3; buf[--i] = '.');
806 XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
808 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
810 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
814 enternotify(XEvent *e) {
817 XCrossingEvent *ev = &e->xcrossing;
819 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
821 c = wintoclient(ev->window);
822 m = c ? c->mon : wintomon(ev->window);
824 unfocus(selmon->sel, True);
827 else if(!c || c == selmon->sel)
835 XExposeEvent *ev = &e->xexpose;
837 if(ev->count == 0 && (m = wintomon(ev->window)))
843 if(!c || !ISVISIBLE(c))
844 for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
845 /* was if(selmon->sel) */
846 if(selmon->sel && selmon->sel != c)
847 unfocus(selmon->sel, False);
855 grabbuttons(c, True);
856 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
860 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
866 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
867 XFocusChangeEvent *ev = &e->xfocus;
869 if(selmon->sel && ev->window != selmon->sel->win)
870 setfocus(selmon->sel);
874 focusmon(const Arg *arg) {
879 if((m = dirtomon(arg->i)) == selmon)
881 unfocus(selmon->sel, True);
887 focusstack(const Arg *arg) {
888 Client *c = NULL, *i;
893 for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
895 for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
898 for(i = selmon->clients; i != selmon->sel; i = i->next)
902 for(; i; i = i->next)
913 getatomprop(Client *c, Atom prop) {
916 unsigned char *p = NULL;
917 Atom da, atom = None;
919 if(XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
920 &da, &di, &dl, &dl, &p) == Success && p) {
928 getcolor(const char *colstr) {
929 Colormap cmap = DefaultColormap(dpy, screen);
932 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
933 die("error, cannot allocate color '%s'\n", colstr);
938 getrootptr(int *x, int *y) {
943 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
950 unsigned char *p = NULL;
951 unsigned long n, extra;
954 if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
955 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
964 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
969 if(!text || size == 0)
972 XGetTextProperty(dpy, w, &name, atom);
975 if(name.encoding == XA_STRING)
976 strncpy(text, (char *)name.value, size - 1);
978 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
979 strncpy(text, *list, size - 1);
980 XFreeStringList(list);
983 text[size - 1] = '\0';
989 grabbuttons(Client *c, Bool focused) {
993 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
994 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
996 for(i = 0; i < LENGTH(buttons); i++)
997 if(buttons[i].click == ClkClientWin)
998 for(j = 0; j < LENGTH(modifiers); j++)
999 XGrabButton(dpy, buttons[i].button,
1000 buttons[i].mask | modifiers[j],
1001 c->win, False, BUTTONMASK,
1002 GrabModeAsync, GrabModeSync, None, None);
1005 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
1006 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1012 updatenumlockmask();
1015 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1018 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1019 for(i = 0; i < LENGTH(keys); i++)
1020 if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
1021 for(j = 0; j < LENGTH(modifiers); j++)
1022 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
1023 True, GrabModeAsync, GrabModeAsync);
1028 incnmaster(const Arg *arg) {
1029 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
1034 initfont(const char *fontstr) {
1035 char *def, **missing;
1038 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
1041 fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
1042 XFreeStringList(missing);
1045 XFontStruct **xfonts;
1048 dc.font.ascent = dc.font.descent = 0;
1049 XExtentsOfFontSet(dc.font.set);
1050 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
1052 dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
1053 dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
1058 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
1059 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
1060 die("error, cannot load font: '%s'\n", fontstr);
1061 dc.font.ascent = dc.font.xfont->ascent;
1062 dc.font.descent = dc.font.xfont->descent;
1064 dc.font.height = dc.font.ascent + dc.font.descent;
1069 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
1071 if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1072 && unique[n].width == info->width && unique[n].height == info->height)
1076 #endif /* XINERAMA */
1079 keypress(XEvent *e) {
1085 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1086 for(i = 0; i < LENGTH(keys); i++)
1087 if(keysym == keys[i].keysym
1088 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1090 keys[i].func(&(keys[i].arg));
1094 killclient(const Arg *arg) {
1097 if(!sendevent(selmon->sel, wmatom[WMDelete])) {
1099 XSetErrorHandler(xerrordummy);
1100 XSetCloseDownMode(dpy, DestroyAll);
1101 XKillClient(dpy, selmon->sel->win);
1103 XSetErrorHandler(xerror);
1109 manage(Window w, XWindowAttributes *wa) {
1110 Client *c, *t = NULL;
1111 Window trans = None;
1114 if(!(c = calloc(1, sizeof(Client))))
1115 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1118 if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1127 c->x = c->oldx = wa->x;
1128 c->y = c->oldy = wa->y;
1129 c->w = c->oldw = wa->width;
1130 c->h = c->oldh = wa->height;
1131 c->oldbw = wa->border_width;
1133 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1134 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1135 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1136 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1137 c->x = MAX(c->x, c->mon->mx);
1138 /* only fix client y-offset, if the client center might cover the bar */
1139 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1140 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1143 wc.border_width = c->bw;
1144 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1145 XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
1146 configure(c); /* propagates border_width, if size doesn't change */
1147 updatewindowtype(c);
1150 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1151 grabbuttons(c, False);
1153 c->isfloating = c->oldstate = trans != None || c->isfixed;
1155 XRaiseWindow(dpy, c->win);
1158 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1159 setclientstate(c, NormalState);
1160 if (c->mon == selmon)
1161 unfocus(selmon->sel, False);
1164 XMapWindow(dpy, c->win);
1169 mappingnotify(XEvent *e) {
1170 XMappingEvent *ev = &e->xmapping;
1172 XRefreshKeyboardMapping(ev);
1173 if(ev->request == MappingKeyboard)
1178 maprequest(XEvent *e) {
1179 static XWindowAttributes wa;
1180 XMapRequestEvent *ev = &e->xmaprequest;
1182 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1184 if(wa.override_redirect)
1186 if(!wintoclient(ev->window))
1187 manage(ev->window, &wa);
1191 monocle(Monitor *m) {
1195 for(c = m->clients; c; c = c->next)
1198 if(n > 0) /* override layout symbol */
1199 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1200 for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
1201 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
1205 movemouse(const Arg *arg) {
1206 int x, y, ocx, ocy, nx, ny;
1211 if(!(c = selmon->sel))
1216 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1217 None, cursor[CurMove], CurrentTime) != GrabSuccess)
1219 if(!getrootptr(&x, &y))
1222 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1224 case ConfigureRequest:
1227 handler[ev.type](&ev);
1230 nx = ocx + (ev.xmotion.x - x);
1231 ny = ocy + (ev.xmotion.y - y);
1232 if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1233 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1234 if(abs(selmon->wx - nx) < snap)
1236 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1237 nx = selmon->wx + selmon->ww - WIDTH(c);
1238 if(abs(selmon->wy - ny) < snap)
1240 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1241 ny = selmon->wy + selmon->wh - HEIGHT(c);
1242 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1243 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1244 togglefloating(NULL);
1246 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1247 resize(c, nx, ny, c->w, c->h, True);
1250 } while(ev.type != ButtonRelease);
1251 XUngrabPointer(dpy, CurrentTime);
1252 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1260 nexttiled(Client *c) {
1261 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1274 propertynotify(XEvent *e) {
1277 XPropertyEvent *ev = &e->xproperty;
1279 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1281 else if(ev->state == PropertyDelete)
1282 return; /* ignore */
1283 else if((c = wintoclient(ev->window))) {
1286 case XA_WM_TRANSIENT_FOR:
1287 if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1288 (c->isfloating = (wintoclient(trans)) != NULL))
1291 case XA_WM_NORMAL_HINTS:
1299 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1301 if(c == c->mon->sel)
1304 if(ev->atom == netatom[NetWMWindowType])
1305 updatewindowtype(c);
1310 quit(const Arg *arg) {
1315 recttomon(int x, int y, int w, int h) {
1316 Monitor *m, *r = selmon;
1319 for(m = mons; m; m = m->next)
1320 if((a = INTERSECT(x, y, w, h, m)) > area) {
1328 resize(Client *c, int x, int y, int w, int h, Bool interact) {
1329 if(applysizehints(c, &x, &y, &w, &h, interact))
1330 resizeclient(c, x, y, w, h);
1334 resizeclient(Client *c, int x, int y, int w, int h) {
1337 c->oldx = c->x; c->x = wc.x = x;
1338 c->oldy = c->y; c->y = wc.y = y;
1339 c->oldw = c->w; c->w = wc.width = w;
1340 c->oldh = c->h; c->h = wc.height = h;
1341 wc.border_width = c->bw;
1342 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1348 resizemouse(const Arg *arg) {
1355 if(!(c = selmon->sel))
1360 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1361 None, cursor[CurResize], CurrentTime) != GrabSuccess)
1363 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1365 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1367 case ConfigureRequest:
1370 handler[ev.type](&ev);
1373 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1374 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1375 if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1376 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1378 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1379 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1380 togglefloating(NULL);
1382 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1383 resize(c, c->x, c->y, nw, nh, True);
1386 } while(ev.type != ButtonRelease);
1387 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1388 XUngrabPointer(dpy, CurrentTime);
1389 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1390 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1398 restack(Monitor *m) {
1406 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1407 XRaiseWindow(dpy, m->sel->win);
1408 if(m->lt[m->sellt]->arrange) {
1409 wc.stack_mode = Below;
1410 wc.sibling = m->barwin;
1411 for(c = m->stack; c; c = c->snext)
1412 if(!c->isfloating && ISVISIBLE(c)) {
1413 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1414 wc.sibling = c->win;
1418 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1424 /* main event loop */
1426 while(running && !XNextEvent(dpy, &ev))
1427 if(handler[ev.type])
1428 handler[ev.type](&ev); /* call handler */
1433 unsigned int i, num;
1434 Window d1, d2, *wins = NULL;
1435 XWindowAttributes wa;
1437 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1438 for(i = 0; i < num; i++) {
1439 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1440 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1442 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1443 manage(wins[i], &wa);
1445 for(i = 0; i < num; i++) { /* now the transients */
1446 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1448 if(XGetTransientForHint(dpy, wins[i], &d1)
1449 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1450 manage(wins[i], &wa);
1458 sendmon(Client *c, Monitor *m) {
1465 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1473 setclientstate(Client *c, long state) {
1474 long data[] = { state, None };
1476 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1477 PropModeReplace, (unsigned char *)data, 2);
1481 sendevent(Client *c, Atom proto) {
1484 Bool exists = False;
1487 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1488 while(!exists && n--)
1489 exists = protocols[n] == proto;
1493 ev.type = ClientMessage;
1494 ev.xclient.window = c->win;
1495 ev.xclient.message_type = wmatom[WMProtocols];
1496 ev.xclient.format = 32;
1497 ev.xclient.data.l[0] = proto;
1498 ev.xclient.data.l[1] = CurrentTime;
1499 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1505 setfocus(Client *c) {
1507 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1508 sendevent(c, wmatom[WMTakeFocus]);
1512 setfullscreen(Client *c, Bool fullscreen) {
1514 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1515 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1516 c->isfullscreen = True;
1517 c->oldstate = c->isfloating;
1520 c->isfloating = True;
1521 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1522 XRaiseWindow(dpy, c->win);
1525 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1526 PropModeReplace, (unsigned char*)0, 0);
1527 c->isfullscreen = False;
1528 c->isfloating = c->oldstate;
1534 resizeclient(c, c->x, c->y, c->w, c->h);
1540 setlayout(const Arg *arg) {
1541 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1544 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1545 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1552 /* arg > 1.0 will set mfact absolutly */
1554 setmfact(const Arg *arg) {
1557 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1559 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1560 if(f < 0.1 || f > 0.9)
1568 XSetWindowAttributes wa;
1570 /* clean up any zombies immediately */
1574 screen = DefaultScreen(dpy);
1575 root = RootWindow(dpy, screen);
1577 sw = DisplayWidth(dpy, screen);
1578 sh = DisplayHeight(dpy, screen);
1579 bh = dc.h = dc.font.height + 2;
1582 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1583 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1584 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1585 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1586 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1587 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1588 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1589 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1590 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1591 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1592 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1594 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1595 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1596 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1597 /* init appearance */
1598 dc.norm[ColBorder] = getcolor(normbordercolor);
1599 dc.norm[ColBG] = getcolor(normbgcolor);
1600 dc.norm[ColFG] = getcolor(normfgcolor);
1601 dc.sel[ColBorder] = getcolor(selbordercolor);
1602 dc.sel[ColBG] = getcolor(selbgcolor);
1603 dc.sel[ColFG] = getcolor(selfgcolor);
1604 dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
1605 dc.gc = XCreateGC(dpy, root, 0, NULL);
1606 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1608 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
1612 /* EWMH support per view */
1613 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1614 PropModeReplace, (unsigned char *) netatom, NetLast);
1615 /* select for events */
1616 wa.cursor = cursor[CurNormal];
1617 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
1618 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
1619 |PropertyChangeMask;
1620 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1621 XSelectInput(dpy, root, wa.event_mask);
1626 showhide(Client *c) {
1629 if(ISVISIBLE(c)) { /* show clients top down */
1630 XMoveWindow(dpy, c->win, c->x, c->y);
1631 if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1632 resize(c, c->x, c->y, c->w, c->h, False);
1635 else { /* hide clients bottom up */
1637 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1642 sigchld(int unused) {
1643 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1644 die("Can't install SIGCHLD handler");
1645 while(0 < waitpid(-1, NULL, WNOHANG));
1649 spawn(const Arg *arg) {
1652 close(ConnectionNumber(dpy));
1654 execvp(((char **)arg->v)[0], (char **)arg->v);
1655 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1662 tag(const Arg *arg) {
1663 if(selmon->sel && arg->ui & TAGMASK) {
1664 selmon->sel->tags = arg->ui & TAGMASK;
1671 tagmon(const Arg *arg) {
1672 if(!selmon->sel || !mons->next)
1674 sendmon(selmon->sel, dirtomon(arg->i));
1678 textnw(const char *text, unsigned int len) {
1682 XmbTextExtents(dc.font.set, text, len, NULL, &r);
1685 return XTextWidth(dc.font.xfont, text, len);
1690 unsigned int i, n, h, mw, my, ty;
1693 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1698 mw = m->nmaster ? m->ww * m->mfact : 0;
1701 for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1702 if(i < m->nmaster) {
1703 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1704 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False);
1708 h = (m->wh - ty) / (n - i);
1709 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), False);
1715 togglebar(const Arg *arg) {
1716 selmon->showbar = !selmon->showbar;
1717 updatebarpos(selmon);
1718 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1723 togglefloating(const Arg *arg) {
1726 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1727 if(selmon->sel->isfloating)
1728 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1729 selmon->sel->w, selmon->sel->h, False);
1734 toggletag(const Arg *arg) {
1735 unsigned int newtags;
1739 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1741 selmon->sel->tags = newtags;
1748 toggleview(const Arg *arg) {
1749 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1752 selmon->tagset[selmon->seltags] = newtagset;
1759 unfocus(Client *c, Bool setfocus) {
1762 grabbuttons(c, False);
1763 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
1765 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1769 unmanage(Client *c, Bool destroyed) {
1770 Monitor *m = c->mon;
1773 /* The server grab construct avoids race conditions. */
1777 wc.border_width = c->oldbw;
1779 XSetErrorHandler(xerrordummy);
1780 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1781 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1782 setclientstate(c, WithdrawnState);
1784 XSetErrorHandler(xerror);
1793 unmapnotify(XEvent *e) {
1795 XUnmapEvent *ev = &e->xunmap;
1797 if((c = wintoclient(ev->window))) {
1799 setclientstate(c, WithdrawnState);
1808 XSetWindowAttributes wa = {
1809 .override_redirect = True,
1810 .background_pixmap = ParentRelative,
1811 .event_mask = ButtonPressMask|ExposureMask
1813 for(m = mons; m; m = m->next) {
1814 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1815 CopyFromParent, DefaultVisual(dpy, screen),
1816 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1817 XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
1818 XMapRaised(dpy, m->barwin);
1823 updatebarpos(Monitor *m) {
1828 m->by = m->topbar ? m->wy : m->wy + m->wh;
1829 m->wy = m->topbar ? m->wy + bh : m->wy;
1840 if(XineramaIsActive(dpy)) {
1844 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1845 XineramaScreenInfo *unique = NULL;
1847 for(n = 0, m = mons; m; m = m->next, n++);
1848 /* only consider unique geometries as separate screens */
1849 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
1850 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
1851 for(i = 0, j = 0; i < nn; i++)
1852 if(isuniquegeom(unique, j, &info[i]))
1853 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1857 for(i = 0; i < (nn - n); i++) { /* new monitors available */
1858 for(m = mons; m && m->next; m = m->next);
1860 m->next = createmon();
1864 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
1866 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1867 || unique[i].width != m->mw || unique[i].height != m->mh))
1871 m->mx = m->wx = unique[i].x_org;
1872 m->my = m->wy = unique[i].y_org;
1873 m->mw = m->ww = unique[i].width;
1874 m->mh = m->wh = unique[i].height;
1878 else { /* less monitors available nn < n */
1879 for(i = nn; i < n; i++) {
1880 for(m = mons; m && m->next; m = m->next);
1884 m->clients = c->next;
1898 #endif /* XINERAMA */
1899 /* default monitor setup */
1903 if(mons->mw != sw || mons->mh != sh) {
1905 mons->mw = mons->ww = sw;
1906 mons->mh = mons->wh = sh;
1912 selmon = wintomon(root);
1918 updatenumlockmask(void) {
1920 XModifierKeymap *modmap;
1923 modmap = XGetModifierMapping(dpy);
1924 for(i = 0; i < 8; i++)
1925 for(j = 0; j < modmap->max_keypermod; j++)
1926 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1927 == XKeysymToKeycode(dpy, XK_Num_Lock))
1928 numlockmask = (1 << i);
1929 XFreeModifiermap(modmap);
1933 updatesizehints(Client *c) {
1937 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
1938 /* size is uninitialized, ensure that size.flags aren't used */
1940 if(size.flags & PBaseSize) {
1941 c->basew = size.base_width;
1942 c->baseh = size.base_height;
1944 else if(size.flags & PMinSize) {
1945 c->basew = size.min_width;
1946 c->baseh = size.min_height;
1949 c->basew = c->baseh = 0;
1950 if(size.flags & PResizeInc) {
1951 c->incw = size.width_inc;
1952 c->inch = size.height_inc;
1955 c->incw = c->inch = 0;
1956 if(size.flags & PMaxSize) {
1957 c->maxw = size.max_width;
1958 c->maxh = size.max_height;
1961 c->maxw = c->maxh = 0;
1962 if(size.flags & PMinSize) {
1963 c->minw = size.min_width;
1964 c->minh = size.min_height;
1966 else if(size.flags & PBaseSize) {
1967 c->minw = size.base_width;
1968 c->minh = size.base_height;
1971 c->minw = c->minh = 0;
1972 if(size.flags & PAspect) {
1973 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
1974 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
1977 c->maxa = c->mina = 0.0;
1978 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
1979 && c->maxw == c->minw && c->maxh == c->minh);
1983 updatetitle(Client *c) {
1984 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
1985 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
1986 if(c->name[0] == '\0') /* hack to mark broken clients */
1987 strcpy(c->name, broken);
1991 updatestatus(void) {
1992 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
1993 strcpy(stext, "dwm-"VERSION);
1998 updatewindowtype(Client *c) {
1999 Atom state = getatomprop(c, netatom[NetWMState]);
2000 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2002 if(state == netatom[NetWMFullscreen])
2003 setfullscreen(c, True);
2005 if(wtype == netatom[NetWMWindowTypeDialog])
2006 c->isfloating = True;
2010 updatewmhints(Client *c) {
2013 if((wmh = XGetWMHints(dpy, c->win))) {
2014 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
2015 wmh->flags &= ~XUrgencyHint;
2016 XSetWMHints(dpy, c->win, wmh);
2019 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
2020 if(wmh->flags & InputHint)
2021 c->neverfocus = !wmh->input;
2023 c->neverfocus = False;
2029 view(const Arg *arg) {
2030 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2032 selmon->seltags ^= 1; /* toggle sel tagset */
2033 if(arg->ui & TAGMASK)
2034 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2040 wintoclient(Window w) {
2044 for(m = mons; m; m = m->next)
2045 for(c = m->clients; c; c = c->next)
2052 wintomon(Window w) {
2057 if(w == root && getrootptr(&x, &y))
2058 return recttomon(x, y, 1, 1);
2059 for(m = mons; m; m = m->next)
2062 if((c = wintoclient(w)))
2067 /* There's no way to check accesses to destroyed windows, thus those cases are
2068 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2069 * default error handler, which may call exit. */
2071 xerror(Display *dpy, XErrorEvent *ee) {
2072 if(ee->error_code == BadWindow
2073 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2074 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2075 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2076 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2077 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2078 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2079 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2080 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2082 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2083 ee->request_code, ee->error_code);
2084 return xerrorxlib(dpy, ee); /* may call exit */
2088 xerrordummy(Display *dpy, XErrorEvent *ee) {
2092 /* Startup Error handler to check if another window manager
2093 * is already running. */
2095 xerrorstart(Display *dpy, XErrorEvent *ee) {
2096 die("dwm: another window manager is already running\n");
2101 zoom(const Arg *arg) {
2102 Client *c = selmon->sel;
2104 if(!selmon->lt[selmon->sellt]->arrange
2105 || (selmon->sel && selmon->sel->isfloating))
2107 if(c == nexttiled(selmon->clients))
2108 if(!c || !(c = nexttiled(c->next)))
2114 main(int argc, char *argv[]) {
2115 if(argc == 2 && !strcmp("-v", argv[1]))
2116 die("dwm-"VERSION", © 2006-2011 dwm engineers, see LICENSE for details\n");
2118 die("usage: dwm [-v]\n");
2119 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2120 fputs("warning: no locale support\n", stderr);
2121 if(!(dpy = XOpenDisplay(NULL)))
2122 die("dwm: cannot open display\n");
2129 return EXIT_SUCCESS;