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>
47 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
48 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
49 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
50 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
51 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
52 #define LENGTH(X) (sizeof X / sizeof X[0])
53 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
54 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
55 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
56 #define TAGMASK ((1 << LENGTH(tags)) - 1)
57 #define TEXTW(X) (drw_font_getexts_width(drw->font, X, strlen(X)) + drw->font->h)
60 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
61 enum { SchemeNorm, SchemeSel, SchemeLast }; /* color schemes */
62 enum { NetSupported, NetWMName, NetWMState,
63 NetWMFullscreen, NetWMWindowOpacity, NetActiveWindow, NetWMWindowType,
64 NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */
65 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
66 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
67 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
80 void (*func)(const Arg *arg);
84 typedef struct Monitor Monitor;
85 typedef struct Client Client;
90 int oldx, oldy, oldw, oldh;
91 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
94 Bool isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
104 void (*func)(const Arg *);
110 void (*arrange)(Monitor *);
118 int by; /* bar geometry */
119 int mx, my, mw, mh; /* screen size */
120 int wx, wy, ww, wh; /* window area */
121 unsigned int seltags;
123 unsigned int tagset[2];
136 const char *instance;
143 /* function declarations */
144 static void applyrules(Client *c);
145 static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
146 static void arrange(Monitor *m);
147 static void arrangemon(Monitor *m);
148 static void attach(Client *c);
149 static void attachstack(Client *c);
150 static void buttonpress(XEvent *e);
151 static void checkotherwm(void);
152 static void cleanup(void);
153 static void cleanupmon(Monitor *mon);
154 static void clearurgent(Client *c);
155 static void clientmessage(XEvent *e);
156 static void configure(Client *c);
157 static void configurenotify(XEvent *e);
158 static void configurerequest(XEvent *e);
159 static Monitor *createmon(void);
160 static void destroynotify(XEvent *e);
161 static void detach(Client *c);
162 static void detachstack(Client *c);
163 static Monitor *dirtomon(int dir);
164 static void drawbar(Monitor *m);
165 static void drawbars(void);
166 static void enternotify(XEvent *e);
167 static void expose(XEvent *e);
168 static void focus(Client *c);
169 static void focusin(XEvent *e);
170 static void focusmon(const Arg *arg);
171 static void focusstack(const Arg *arg);
172 static Bool getrootptr(int *x, int *y);
173 static long getstate(Window w);
174 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
175 static void grabbuttons(Client *c, Bool focused);
176 static void grabkeys(void);
177 static void incnmaster(const Arg *arg);
178 static void keypress(XEvent *e);
179 static void killclient(const Arg *arg);
180 static void manage(Window w, XWindowAttributes *wa);
181 static void mappingnotify(XEvent *e);
182 static void maprequest(XEvent *e);
183 static void monocle(Monitor *m);
184 static void motionnotify(XEvent *e);
185 static void movemouse(const Arg *arg);
186 static Client *nexttiled(Client *c);
187 static Client *nextvisible(Client *c);
188 static void pop(Client *);
189 static void propertynotify(XEvent *e);
190 static void quit(const Arg *arg);
191 static Monitor *recttomon(int x, int y, int w, int h);
192 static void resize(Client *c, int x, int y, int w, int h, Bool interact);
193 static void resizeclient(Client *c, int x, int y, int w, int h);
194 static void resizemouse(const Arg *arg);
195 static void restack(Monitor *m);
196 static void run(void);
197 static void scan(void);
198 static Bool sendevent(Client *c, Atom proto);
199 static void sendmon(Client *c, Monitor *m);
200 static void setclientstate(Client *c, long state);
201 static void setfocus(Client *c);
202 static void setfullscreen(Client *c, Bool fullscreen);
203 static void setlayout(const Arg *arg);
204 static void setmfact(const Arg *arg);
205 static void setup(void);
206 static void showhide(Client *c);
207 static void sigchld(int unused);
208 static void spawn(const Arg *arg);
209 static void tag(const Arg *arg);
210 static void tagmon(const Arg *arg);
211 static void tile(Monitor *);
212 static void togglebar(const Arg *arg);
213 static void togglefloating(const Arg *arg);
214 static void toggletag(const Arg *arg);
215 static void toggleview(const Arg *arg);
216 static void unfocus(Client *c, Bool setfocus);
217 static void unmanage(Client *c, Bool destroyed);
218 static void unmapnotify(XEvent *e);
219 static Bool updategeom(void);
220 static void updatebarpos(Monitor *m);
221 static void updatebars(void);
222 static void updateclientlist(void);
223 static void updatenumlockmask(void);
224 static void updatesizehints(Client *c);
225 static void updatestatus(void);
226 static void updatewindowtype(Client *c);
227 static void updatetitle(Client *c);
228 static void updatewmhints(Client *c);
229 static void view(const Arg *arg);
230 static Client *wintoclient(Window w);
231 static Monitor *wintomon(Window w);
232 static int xerror(Display *dpy, XErrorEvent *ee);
233 static int xerrordummy(Display *dpy, XErrorEvent *ee);
234 static int xerrorstart(Display *dpy, XErrorEvent *ee);
235 static void zoom(const Arg *arg);
238 static const char broken[] = "broken";
239 static char stext[256];
241 static int sw, sh; /* X display screen geometry width, height */
242 static int bh, blw = 0; /* bar geometry */
243 static int (*xerrorxlib)(Display *, XErrorEvent *);
244 static unsigned int numlockmask = 0;
245 static void (*handler[LASTEvent]) (XEvent *) = {
246 [ButtonPress] = buttonpress,
247 [ClientMessage] = clientmessage,
248 [ConfigureRequest] = configurerequest,
249 [ConfigureNotify] = configurenotify,
250 [DestroyNotify] = destroynotify,
251 [EnterNotify] = enternotify,
254 [KeyPress] = keypress,
255 [MappingNotify] = mappingnotify,
256 [MapRequest] = maprequest,
257 [MotionNotify] = motionnotify,
258 [PropertyNotify] = propertynotify,
259 [UnmapNotify] = unmapnotify
261 static Atom wmatom[WMLast], netatom[NetLast];
262 static Bool running = True;
263 static Cur *cursor[CurLast];
264 static ClrScheme scheme[SchemeLast];
268 static Monitor *mons, *selmon;
271 // unfocused windows get transparent (feature)
272 static const unsigned long unfocused_opacity[] = { 0xbfffffff };
273 static void window_set_opaque(Client *c);
274 static void window_set_translucent(Client *c);
276 window_set_opaque(Client *c) {
277 XDeleteProperty(dpy, c->win, netatom[NetWMWindowOpacity]);
280 window_set_translucent(Client *c) {
281 XChangeProperty(dpy, c->win, netatom[NetWMWindowOpacity], XA_CARDINAL, 32, PropModeReplace, (unsigned char *)unfocused_opacity, 1);
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)
401 } else for(m = mons; m; m = m->next)
406 arrangemon(Monitor *m) {
407 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
408 if(m->lt[m->sellt]->arrange)
409 m->lt[m->sellt]->arrange(m);
415 c->next = c->mon->sel->next;
416 c->mon->sel->next = c;
418 c->next = c->mon->clients;
424 attachstack(Client *c) {
425 c->snext = c->mon->stack;
430 buttonpress(XEvent *e) {
431 unsigned int i, x, click;
435 XButtonPressedEvent *ev = &e->xbutton;
438 /* focus monitor if necessary */
439 if((m = wintomon(ev->window)) && m != selmon) {
440 unfocus(selmon->sel, True);
444 if(ev->window == selmon->barwin) {
448 while(ev->x >= x && ++i < LENGTH(tags));
449 if(i < LENGTH(tags)) {
453 else if(ev->x < x + blw)
455 else if(ev->x > selmon->ww - TEXTW(stext))
456 click = ClkStatusText;
460 else if((c = wintoclient(ev->window))) {
462 click = ClkClientWin;
464 for(i = 0; i < LENGTH(buttons); i++)
465 if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
466 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
467 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
472 xerrorxlib = XSetErrorHandler(xerrorstart);
473 /* this causes an error if some other window manager is running */
474 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
476 XSetErrorHandler(xerror);
483 Layout foo = { "", NULL };
487 selmon->lt[selmon->sellt] = &foo;
488 for(m = mons; m; m = m->next)
490 unmanage(m->stack, False);
491 XUngrabKey(dpy, AnyKey, AnyModifier, root);
494 drw_cur_free(drw, cursor[CurNormal]);
495 drw_cur_free(drw, cursor[CurResize]);
496 drw_cur_free(drw, cursor[CurMove]);
497 drw_font_free(dpy, fnt);
498 drw_clr_free(scheme[SchemeNorm].border);
499 drw_clr_free(scheme[SchemeNorm].bg);
500 drw_clr_free(scheme[SchemeNorm].fg);
501 drw_clr_free(scheme[SchemeSel].border);
502 drw_clr_free(scheme[SchemeSel].bg);
503 drw_clr_free(scheme[SchemeSel].fg);
506 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
507 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
511 cleanupmon(Monitor *mon) {
517 for(m = mons; m && m->next != mon; m = m->next);
520 XUnmapWindow(dpy, mon->barwin);
521 XDestroyWindow(dpy, mon->barwin);
526 clearurgent(Client *c) {
530 if(!(wmh = XGetWMHints(dpy, c->win)))
532 wmh->flags &= ~XUrgencyHint;
533 XSetWMHints(dpy, c->win, wmh);
538 clientmessage(XEvent *e) {
539 XClientMessageEvent *cme = &e->xclient;
540 Client *c = wintoclient(cme->window);
544 if(cme->message_type == netatom[NetWMState]) {
545 if(cme->data.l[1] == netatom[NetWMFullscreen] || cme->data.l[2] == netatom[NetWMFullscreen])
546 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
547 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
549 else if(cme->message_type == netatom[NetActiveWindow]) {
551 c->mon->seltags ^= 1;
552 c->mon->tagset[c->mon->seltags] = c->tags;
559 configure(Client *c) {
562 ce.type = ConfigureNotify;
570 ce.border_width = c->bw;
572 ce.override_redirect = False;
573 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
577 configurenotify(XEvent *e) {
579 XConfigureEvent *ev = &e->xconfigure;
582 // TODO: updategeom handling sucks, needs to be simplified
583 if(ev->window == root) {
584 dirty = (sw != ev->width || sh != ev->height);
587 if(updategeom() || dirty) {
588 drw_resize(drw, sw, bh);
590 for(m = mons; m; m = m->next)
591 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
599 configurerequest(XEvent *e) {
602 XConfigureRequestEvent *ev = &e->xconfigurerequest;
605 if((c = wintoclient(ev->window))) {
606 if(ev->value_mask & CWBorderWidth)
607 c->bw = ev->border_width;
608 else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
610 if(ev->value_mask & CWX) {
612 c->x = m->mx + ev->x;
614 if(ev->value_mask & CWY) {
616 c->y = m->my + ev->y;
618 if(ev->value_mask & CWWidth) {
622 if(ev->value_mask & CWHeight) {
626 if((c->x + c->w) > m->mx + m->mw && c->isfloating)
627 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
628 if((c->y + c->h) > m->my + m->mh && c->isfloating)
629 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
630 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
633 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
641 wc.width = ev->width;
642 wc.height = ev->height;
643 wc.border_width = ev->border_width;
644 wc.sibling = ev->above;
645 wc.stack_mode = ev->detail;
646 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
655 if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
656 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
657 m->tagset[0] = m->tagset[1] = 1;
659 m->nmaster = nmaster;
660 m->showbar = showbar;
662 m->lt[0] = &layouts[0];
663 m->lt[1] = &layouts[1 % LENGTH(layouts)];
664 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
669 destroynotify(XEvent *e) {
671 XDestroyWindowEvent *ev = &e->xdestroywindow;
673 if((c = wintoclient(ev->window)))
681 for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
685 // NOTE: the stack is for z-order and most-recently-focused
686 // only mon->clients determines position in visible layout
688 detachstack(Client *c) {
689 Client *prev = NULL, *next_sel = NULL, *i;
690 for(i = c->mon->stack; i && i != c; i = i->snext) {
693 if(c == c->mon->sel) {
694 // find last visible window before c
695 // WARNING if you detach() before detachstack() this will select last visible window
696 for(i = nextvisible(c->mon->clients); i && i != c; i = nextvisible(i->next))
698 // failing that, find first visible window
700 for(i = nextvisible(c->mon->clients); i && i != c; i = nextvisible(i->next))
702 c->mon->sel = next_sel;
705 prev->snext = c->snext;
707 c->mon->stack = c->snext;
716 if(!(m = selmon->next))
719 else if(selmon == mons)
720 for(m = mons; m->next; m = m->next);
722 for(m = mons; m->next != selmon; m = m->next);
727 drawbar(Monitor *m) {
729 unsigned int i, occ = 0, urg = 0;
732 for(c = m->clients; c; c = c->next) {
738 for(i = 0; i < LENGTH(tags); i++) {
740 drw_setscheme(drw, m->tagset[m->seltags] & 1 << i ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
741 drw_text(drw, x, 0, w, bh, tags[i], urg & 1 << i);
742 drw_rect(drw, x, 0, w, bh, m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
743 occ & 1 << i, urg & 1 << i);
746 w = blw = TEXTW(m->ltsymbol);
747 drw_setscheme(drw, &scheme[SchemeNorm]);
748 drw_text(drw, x, 0, w, bh, m->ltsymbol, 0);
751 if(m == selmon) { /* status is only drawn on selected monitor */
758 drw_text(drw, x, 0, w, bh, stext, 0);
762 if((w = x - xx) > bh) {
765 drw_setscheme(drw, m == selmon ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
766 drw_text(drw, x, 0, w, bh, m->sel->name, 0);
767 drw_rect(drw, x, 0, w, bh, m->sel->isfixed, m->sel->isfloating, 0);
770 drw_setscheme(drw, &scheme[SchemeNorm]);
771 drw_text(drw, x, 0, w, bh, NULL, 0);
774 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
781 for(m = mons; m; m = m->next)
786 enternotify(XEvent *e) {
789 XCrossingEvent *ev = &e->xcrossing;
791 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
793 c = wintoclient(ev->window);
794 m = c ? c->mon : wintomon(ev->window);
796 unfocus(selmon->sel, True);
799 else if(!c || c == selmon->sel)
807 XExposeEvent *ev = &e->xexpose;
809 if(ev->count == 0 && (m = wintomon(ev->window)))
815 if(!c || !ISVISIBLE(c))
816 for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
817 /* was if(selmon->sel) */
818 if(selmon->sel && selmon->sel != c)
819 unfocus(selmon->sel, False);
827 grabbuttons(c, True);
828 XSetWindowBorder(dpy, c->win, scheme[SchemeSel].border->rgb);
832 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
833 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
837 if(c && (!root || (c->win!=root)) )
838 window_set_opaque(c);
842 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
843 XFocusChangeEvent *ev = &e->xfocus;
845 if(selmon->sel && ev->window != selmon->sel->win)
846 setfocus(selmon->sel);
850 focusmon(const Arg *arg) {
855 if((m = dirtomon(arg->i)) == selmon)
857 unfocus(selmon->sel, False); /* s/True/False/ fixes input focus issues
858 in gedit and anjuta */
864 focusstack(const Arg *arg) {
865 Client *c = NULL, *i;
870 for(i = selmon->clients; i != selmon->sel; i = i->next) {
876 } else if(arg->i > 0) {
877 for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
879 for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
882 for(i = selmon->clients; i != selmon->sel; i = i->next)
886 for(; i; i = i->next)
897 getatomprop(Client *c, Atom prop) {
900 unsigned char *p = NULL;
901 Atom da, atom = None;
903 if(XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
904 &da, &di, &dl, &dl, &p) == Success && p) {
912 getrootptr(int *x, int *y) {
917 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
924 unsigned char *p = NULL;
925 unsigned long n, extra;
928 if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
929 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
938 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
943 if(!text || size == 0)
946 XGetTextProperty(dpy, w, &name, atom);
949 if(name.encoding == XA_STRING)
950 strncpy(text, (char *)name.value, size - 1);
952 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
953 strncpy(text, *list, size - 1);
954 XFreeStringList(list);
957 text[size - 1] = '\0';
963 grabbuttons(Client *c, Bool focused) {
967 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
968 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
970 for(i = 0; i < LENGTH(buttons); i++)
971 if(buttons[i].click == ClkClientWin)
972 for(j = 0; j < LENGTH(modifiers); j++)
973 XGrabButton(dpy, buttons[i].button,
974 buttons[i].mask | modifiers[j],
975 c->win, False, BUTTONMASK,
976 GrabModeAsync, GrabModeSync, None, None);
979 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
980 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
989 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
992 XUngrabKey(dpy, AnyKey, AnyModifier, root);
993 for(i = 0; i < LENGTH(keys); i++)
994 if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
995 for(j = 0; j < LENGTH(modifiers); j++)
996 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
997 True, GrabModeAsync, GrabModeAsync);
1002 incnmaster(const Arg *arg) {
1003 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
1009 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
1011 if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1012 && unique[n].width == info->width && unique[n].height == info->height)
1016 #endif /* XINERAMA */
1019 keypress(XEvent *e) {
1025 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1026 for(i = 0; i < LENGTH(keys); i++)
1027 if(keysym == keys[i].keysym
1028 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1030 keys[i].func(&(keys[i].arg));
1034 killclient(const Arg *arg) {
1037 if(!sendevent(selmon->sel, wmatom[WMDelete])) {
1039 XSetErrorHandler(xerrordummy);
1040 XSetCloseDownMode(dpy, DestroyAll);
1041 XKillClient(dpy, selmon->sel->win);
1043 XSetErrorHandler(xerror);
1049 manage(Window w, XWindowAttributes *wa) {
1050 Client *c, *t = NULL;
1051 Window trans = None;
1054 if(!(c = calloc(1, sizeof(Client))))
1055 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1058 if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1067 c->x = c->oldx = wa->x;
1068 c->y = c->oldy = wa->y;
1069 c->w = c->oldw = wa->width;
1070 c->h = c->oldh = wa->height;
1071 c->oldbw = wa->border_width;
1073 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1074 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1075 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1076 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1077 c->x = MAX(c->x, c->mon->mx);
1078 /* only fix client y-offset, if the client center might cover the bar */
1079 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1080 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1083 wc.border_width = c->bw;
1084 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1085 XSetWindowBorder(dpy, w, scheme[SchemeNorm].border->rgb);
1086 configure(c); /* propagates border_width, if size doesn't change */
1087 updatewindowtype(c);
1090 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1091 grabbuttons(c, False);
1093 c->isfloating = c->oldstate = trans != None || c->isfixed;
1095 XRaiseWindow(dpy, c->win);
1098 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1099 (unsigned char *) &(c->win), 1);
1100 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1101 setclientstate(c, NormalState);
1102 if (c->mon == selmon)
1103 unfocus(selmon->sel, False);
1106 XMapWindow(dpy, c->win);
1111 mappingnotify(XEvent *e) {
1112 XMappingEvent *ev = &e->xmapping;
1114 XRefreshKeyboardMapping(ev);
1115 if(ev->request == MappingKeyboard)
1120 maprequest(XEvent *e) {
1121 static XWindowAttributes wa;
1122 XMapRequestEvent *ev = &e->xmaprequest;
1124 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1126 if(wa.override_redirect)
1128 if(!wintoclient(ev->window))
1129 manage(ev->window, &wa);
1133 monocle(Monitor *m) {
1137 for(c = m->clients; c; c = c->next)
1140 if(n > 0) /* override layout symbol */
1141 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1142 for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
1143 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
1147 motionnotify(XEvent *e) {
1148 static Monitor *mon = NULL;
1150 XMotionEvent *ev = &e->xmotion;
1152 if(ev->window != root)
1154 if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1155 unfocus(selmon->sel, True);
1163 movemouse(const Arg *arg) {
1164 int x, y, ocx, ocy, nx, ny;
1170 if(!(c = selmon->sel))
1172 if(c->isfullscreen) /* no support moving fullscreen windows by mouse */
1177 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1178 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1180 if(!getrootptr(&x, &y))
1183 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1185 case ConfigureRequest:
1188 handler[ev.type](&ev);
1191 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1193 lasttime = ev.xmotion.time;
1195 nx = ocx + (ev.xmotion.x - x);
1196 ny = ocy + (ev.xmotion.y - y);
1197 if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1198 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1199 if(abs(selmon->wx - nx) < snap)
1201 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1202 nx = selmon->wx + selmon->ww - WIDTH(c);
1203 if(abs(selmon->wy - ny) < snap)
1205 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1206 ny = selmon->wy + selmon->wh - HEIGHT(c);
1207 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1208 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1209 togglefloating(NULL);
1211 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1212 resize(c, nx, ny, c->w, c->h, True);
1215 } while(ev.type != ButtonRelease);
1216 XUngrabPointer(dpy, CurrentTime);
1217 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1225 nexttiled(Client *c) {
1226 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1231 nextvisible(Client *c) {
1232 for(; c && !ISVISIBLE(c); c = c->next);
1245 propertynotify(XEvent *e) {
1248 XPropertyEvent *ev = &e->xproperty;
1250 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1252 else if(ev->state == PropertyDelete)
1253 return; /* ignore */
1254 else if((c = wintoclient(ev->window))) {
1257 case XA_WM_TRANSIENT_FOR:
1258 if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1259 (c->isfloating = (wintoclient(trans)) != NULL))
1262 case XA_WM_NORMAL_HINTS:
1270 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1272 if(c == c->mon->sel)
1275 if(ev->atom == netatom[NetWMWindowType])
1276 updatewindowtype(c);
1281 quit(const Arg *arg) {
1286 recttomon(int x, int y, int w, int h) {
1287 Monitor *m, *r = selmon;
1290 for(m = mons; m; m = m->next)
1291 if((a = INTERSECT(x, y, w, h, m)) > area) {
1299 resize(Client *c, int x, int y, int w, int h, Bool interact) {
1300 if(applysizehints(c, &x, &y, &w, &h, interact))
1301 resizeclient(c, x, y, w, h);
1305 resizeclient(Client *c, int x, int y, int w, int h) {
1308 c->oldx = c->x; c->x = wc.x = x;
1309 c->oldy = c->y; c->y = wc.y = y;
1310 c->oldw = c->w; c->w = wc.width = w;
1311 c->oldh = c->h; c->h = wc.height = h;
1312 wc.border_width = c->bw;
1313 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1319 resizemouse(const Arg *arg) {
1320 int ocx, ocy, nw, nh;
1326 if(!(c = selmon->sel))
1328 if(c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1333 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1334 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1336 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1338 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1340 case ConfigureRequest:
1343 handler[ev.type](&ev);
1346 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1348 lasttime = ev.xmotion.time;
1350 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1351 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1352 if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1353 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1355 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1356 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1357 togglefloating(NULL);
1359 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1360 resize(c, c->x, c->y, nw, nh, True);
1363 } while(ev.type != ButtonRelease);
1364 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1365 XUngrabPointer(dpy, CurrentTime);
1366 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1367 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1375 restack(Monitor *m) {
1383 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1384 XRaiseWindow(dpy, m->sel->win);
1385 if(m->lt[m->sellt]->arrange) {
1386 wc.stack_mode = Below;
1387 wc.sibling = m->barwin;
1388 for(c = m->stack; c; c = c->snext)
1389 if(!c->isfloating && ISVISIBLE(c)) {
1390 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1391 wc.sibling = c->win;
1395 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1401 /* main event loop */
1403 while(running && !XNextEvent(dpy, &ev))
1404 if(handler[ev.type])
1405 handler[ev.type](&ev); /* call handler */
1410 unsigned int i, num;
1411 Window d1, d2, *wins = NULL;
1412 XWindowAttributes wa;
1414 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1415 for(i = 0; i < num; i++) {
1416 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1417 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1419 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1420 manage(wins[i], &wa);
1422 for(i = 0; i < num; i++) { /* now the transients */
1423 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1425 if(XGetTransientForHint(dpy, wins[i], &d1)
1426 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1427 manage(wins[i], &wa);
1435 sendmon(Client *c, Monitor *m) {
1442 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1450 setclientstate(Client *c, long state) {
1451 long data[] = { state, None };
1453 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1454 PropModeReplace, (unsigned char *)data, 2);
1458 sendevent(Client *c, Atom proto) {
1461 Bool exists = False;
1464 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1465 while(!exists && n--)
1466 exists = protocols[n] == proto;
1470 ev.type = ClientMessage;
1471 ev.xclient.window = c->win;
1472 ev.xclient.message_type = wmatom[WMProtocols];
1473 ev.xclient.format = 32;
1474 ev.xclient.data.l[0] = proto;
1475 ev.xclient.data.l[1] = CurrentTime;
1476 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1482 setfocus(Client *c) {
1483 if(!c->neverfocus) {
1484 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1485 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1486 XA_WINDOW, 32, PropModeReplace,
1487 (unsigned char *) &(c->win), 1);
1489 sendevent(c, wmatom[WMTakeFocus]);
1493 setfullscreen(Client *c, Bool fullscreen) {
1495 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1496 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1497 c->isfullscreen = True;
1498 c->oldstate = c->isfloating;
1501 c->isfloating = True;
1502 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1503 XRaiseWindow(dpy, c->win);
1506 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1507 PropModeReplace, (unsigned char*)0, 0);
1508 c->isfullscreen = False;
1509 c->isfloating = c->oldstate;
1515 resizeclient(c, c->x, c->y, c->w, c->h);
1521 setlayout(const Arg *arg) {
1522 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1525 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1526 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1533 /* arg > 1.0 will set mfact absolutly */
1535 setmfact(const Arg *arg) {
1538 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1540 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1541 if(f < 0.1 || f > 0.9)
1549 XSetWindowAttributes wa;
1551 /* clean up any zombies immediately */
1555 screen = DefaultScreen(dpy);
1556 root = RootWindow(dpy, screen);
1557 fnt = drw_font_create(dpy, font);
1558 sw = DisplayWidth(dpy, screen);
1559 sh = DisplayHeight(dpy, screen);
1561 drw = drw_create(dpy, screen, root, sw, sh);
1562 drw_setfont(drw, fnt);
1565 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1566 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1567 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1568 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1569 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1570 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1571 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1572 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1573 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1574 netatom[NetWMWindowOpacity] = XInternAtom(dpy, "_NET_WM_WINDOW_OPACITY", False);
1575 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1576 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1577 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1579 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1580 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1581 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1582 /* init appearance */
1583 scheme[SchemeNorm].border = drw_clr_create(drw, normbordercolor);
1584 scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor);
1585 scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor);
1586 scheme[SchemeSel].border = drw_clr_create(drw, selbordercolor);
1587 scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor);
1588 scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor);
1592 /* EWMH support per view */
1593 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1594 PropModeReplace, (unsigned char *) netatom, NetLast);
1595 XDeleteProperty(dpy, root, netatom[NetClientList]);
1596 /* select for events */
1597 wa.cursor = cursor[CurNormal]->cursor;
1598 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
1599 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1600 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1601 XSelectInput(dpy, root, wa.event_mask);
1607 showhide(Client *c) {
1610 if(ISVISIBLE(c)) { /* show clients top down */
1611 XMoveWindow(dpy, c->win, c->x, c->y);
1612 if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1613 resize(c, c->x, c->y, c->w, c->h, False);
1616 else { /* hide clients bottom up */
1618 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1623 sigchld(int unused) {
1624 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1625 die("Can't install SIGCHLD handler");
1626 while(0 < waitpid(-1, NULL, WNOHANG));
1630 spawn(const Arg *arg) {
1631 if(arg->v == dmenucmd)
1632 dmenumon[0] = '0' + selmon->num;
1635 close(ConnectionNumber(dpy));
1637 execvp(((char **)arg->v)[0], (char **)arg->v);
1638 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1645 tag(const Arg *arg) {
1646 if(selmon->sel && arg->ui & TAGMASK) {
1647 selmon->sel->tags = arg->ui & TAGMASK;
1654 tagmon(const Arg *arg) {
1655 if(!selmon->sel || !mons->next)
1657 sendmon(selmon->sel, dirtomon(arg->i));
1662 unsigned int i, n, h, mw, my, ty;
1665 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1670 mw = m->nmaster ? m->ww * m->mfact : 0;
1673 for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1674 if(i < m->nmaster) {
1675 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1676 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False);
1680 h = (m->wh - ty) / (n - i);
1681 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), False);
1687 togglebar(const Arg *arg) {
1688 selmon->showbar = !selmon->showbar;
1689 updatebarpos(selmon);
1690 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1695 togglefloating(const Arg *arg) {
1698 if(selmon->sel->isfullscreen) /* no support for fullscreen windows */
1700 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1701 if(selmon->sel->isfloating)
1702 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1703 selmon->sel->w, selmon->sel->h, False);
1708 toggletag(const Arg *arg) {
1709 unsigned int newtags;
1713 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1715 selmon->sel->tags = newtags;
1722 toggleview(const Arg *arg) {
1723 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1726 selmon->tagset[selmon->seltags] = newtagset;
1733 unfocus(Client *c, Bool setfocus) {
1736 if(!root || c->win!=root)
1737 window_set_translucent(c);
1738 grabbuttons(c, False);
1739 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->rgb);
1741 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1742 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1747 unmanage(Client *c, Bool destroyed) {
1748 Monitor *m = c->mon;
1751 /* The server grab construct avoids race conditions. */
1755 wc.border_width = c->oldbw;
1757 XSetErrorHandler(xerrordummy);
1758 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1759 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1760 setclientstate(c, WithdrawnState);
1762 XSetErrorHandler(xerror);
1766 focus(selmon ? selmon->sel : NULL);
1772 unmapnotify(XEvent *e) {
1774 XUnmapEvent *ev = &e->xunmap;
1776 if((c = wintoclient(ev->window))) {
1778 setclientstate(c, WithdrawnState);
1787 XSetWindowAttributes wa = {
1788 .override_redirect = True,
1789 .background_pixmap = ParentRelative,
1790 .event_mask = ButtonPressMask|ExposureMask
1792 for(m = mons; m; m = m->next) {
1795 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1796 CopyFromParent, DefaultVisual(dpy, screen),
1797 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1798 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
1799 XMapRaised(dpy, m->barwin);
1804 updatebarpos(Monitor *m) {
1809 m->by = m->topbar ? m->wy : m->wy + m->wh;
1810 m->wy = m->topbar ? m->wy + bh : m->wy;
1817 updateclientlist() {
1821 XDeleteProperty(dpy, root, netatom[NetClientList]);
1822 for(m = mons; m; m = m->next)
1823 for(c = m->clients; c; c = c->next)
1824 XChangeProperty(dpy, root, netatom[NetClientList],
1825 XA_WINDOW, 32, PropModeAppend,
1826 (unsigned char *) &(c->win), 1);
1834 if(XineramaIsActive(dpy)) {
1838 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1839 XineramaScreenInfo *unique = NULL;
1841 for(n = 0, m = mons; m; m = m->next, n++);
1842 /* only consider unique geometries as separate screens */
1843 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
1844 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
1845 for(i = 0, j = 0; i < nn; i++)
1846 if(isuniquegeom(unique, j, &info[i]))
1847 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1851 for(i = 0; i < (nn - n); i++) { /* new monitors available */
1852 for(m = mons; m && m->next; m = m->next);
1854 m->next = createmon();
1858 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
1860 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1861 || unique[i].width != m->mw || unique[i].height != m->mh))
1865 m->mx = m->wx = unique[i].x_org;
1866 m->my = m->wy = unique[i].y_org;
1867 m->mw = m->ww = unique[i].width;
1868 m->mh = m->wh = unique[i].height;
1872 else { /* less monitors available nn < n */
1873 for(i = nn; i < n; i++) {
1874 for(m = mons; m && m->next; m = m->next);
1878 m->clients = c->next;
1892 #endif /* XINERAMA */
1893 /* default monitor setup */
1897 if(mons->mw != sw || mons->mh != sh) {
1899 mons->mw = mons->ww = sw;
1900 mons->mh = mons->wh = sh;
1906 selmon = wintomon(root);
1912 updatenumlockmask(void) {
1914 XModifierKeymap *modmap;
1917 modmap = XGetModifierMapping(dpy);
1918 for(i = 0; i < 8; i++)
1919 for(j = 0; j < modmap->max_keypermod; j++)
1920 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1921 == XKeysymToKeycode(dpy, XK_Num_Lock))
1922 numlockmask = (1 << i);
1923 XFreeModifiermap(modmap);
1927 updatesizehints(Client *c) {
1931 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
1932 /* size is uninitialized, ensure that size.flags aren't used */
1934 if(size.flags & PBaseSize) {
1935 c->basew = size.base_width;
1936 c->baseh = size.base_height;
1938 else if(size.flags & PMinSize) {
1939 c->basew = size.min_width;
1940 c->baseh = size.min_height;
1943 c->basew = c->baseh = 0;
1944 if(size.flags & PResizeInc) {
1945 c->incw = size.width_inc;
1946 c->inch = size.height_inc;
1949 c->incw = c->inch = 0;
1950 if(size.flags & PMaxSize) {
1951 c->maxw = size.max_width;
1952 c->maxh = size.max_height;
1955 c->maxw = c->maxh = 0;
1956 if(size.flags & PMinSize) {
1957 c->minw = size.min_width;
1958 c->minh = size.min_height;
1960 else if(size.flags & PBaseSize) {
1961 c->minw = size.base_width;
1962 c->minh = size.base_height;
1965 c->minw = c->minh = 0;
1966 if(size.flags & PAspect) {
1967 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
1968 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
1971 c->maxa = c->mina = 0.0;
1972 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
1973 && c->maxw == c->minw && c->maxh == c->minh);
1977 updatetitle(Client *c) {
1978 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
1979 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
1980 if(c->name[0] == '\0') /* hack to mark broken clients */
1981 strcpy(c->name, broken);
1985 updatestatus(void) {
1986 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
1987 strcpy(stext, "dwm-"VERSION);
1992 updatewindowtype(Client *c) {
1993 Atom state = getatomprop(c, netatom[NetWMState]);
1994 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
1996 if(state == netatom[NetWMFullscreen])
1997 setfullscreen(c, True);
1998 if(wtype == netatom[NetWMWindowTypeDialog])
1999 c->isfloating = True;
2003 updatewmhints(Client *c) {
2006 if((wmh = XGetWMHints(dpy, c->win))) {
2007 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
2008 wmh->flags &= ~XUrgencyHint;
2009 XSetWMHints(dpy, c->win, wmh);
2012 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
2013 if(wmh->flags & InputHint)
2014 c->neverfocus = !wmh->input;
2016 c->neverfocus = False;
2022 view(const Arg *arg) {
2023 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2025 selmon->seltags ^= 1; /* toggle sel tagset */
2026 if(arg->ui & TAGMASK)
2027 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2033 wintoclient(Window w) {
2037 for(m = mons; m; m = m->next)
2038 for(c = m->clients; c; c = c->next)
2045 wintomon(Window w) {
2050 if(w == root && getrootptr(&x, &y))
2051 return recttomon(x, y, 1, 1);
2052 for(m = mons; m; m = m->next)
2055 if((c = wintoclient(w)))
2060 /* There's no way to check accesses to destroyed windows, thus those cases are
2061 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2062 * default error handler, which may call exit. */
2064 xerror(Display *dpy, XErrorEvent *ee) {
2065 if(ee->error_code == BadWindow
2066 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2067 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2068 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2069 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2070 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2071 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2072 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2073 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2075 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2076 ee->request_code, ee->error_code);
2077 return xerrorxlib(dpy, ee); /* may call exit */
2081 xerrordummy(Display *dpy, XErrorEvent *ee) {
2085 /* Startup Error handler to check if another window manager
2086 * is already running. */
2088 xerrorstart(Display *dpy, XErrorEvent *ee) {
2089 die("dwm: another window manager is already running\n");
2094 zoom(const Arg *arg) {
2095 Client *c = selmon->sel;
2097 if(!selmon->lt[selmon->sellt]->arrange
2098 || (selmon->sel && selmon->sel->isfloating))
2100 if(c == nexttiled(selmon->clients))
2101 if(!c || !(c = nexttiled(c->next)))
2107 main(int argc, char *argv[]) {
2108 if(argc == 2 && !strcmp("-v", argv[1]))
2109 die("dwm-"VERSION", © 2006-2014 dwm engineers, see LICENSE for details\n");
2111 die("usage: dwm [-v]\n");
2112 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2113 fputs("warning: no locale support\n", stderr);
2114 if(!(dpy = XOpenDisplay(NULL)))
2115 die("dwm: cannot open display\n");
2122 return EXIT_SUCCESS;