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>
34 #include <X11/cursorfont.h>
35 #include <X11/keysym.h>
36 #include <X11/Xatom.h>
38 #include <X11/Xproto.h>
39 #include <X11/Xutil.h>
40 #include <X11/Xresource.h>
42 #include <X11/extensions/Xinerama.h>
44 #include <X11/Xft/Xft.h>
50 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
51 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
52 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
53 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
54 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
55 #define LENGTH(X) (sizeof X / sizeof X[0])
56 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
57 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
58 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
59 #define TAGMASK ((1 << LENGTH(tags)) - 1)
60 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
63 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
64 enum { SchemeNorm, SchemeSel }; /* color schemes */
65 enum { NetSupported, NetWMName, NetWMState, NetWMCheck,
66 NetWMFullscreen, NetActiveWindow, NetWMWindowType,
67 NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */
68 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
69 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
70 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
83 void (*func)(const Arg *arg);
87 typedef struct Monitor Monitor;
88 typedef struct Client Client;
93 int oldx, oldy, oldw, oldh;
94 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
97 int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
107 void (*func)(const Arg *);
113 void (*arrange)(Monitor *);
116 typedef struct Pertag Pertag;
122 int by; /* bar geometry */
123 int mx, my, mw, mh; /* screen size */
124 int wx, wy, ww, wh; /* window area */
125 int gappx; /* gaps between windows */
126 unsigned int seltags;
128 unsigned int tagset[2];
142 const char *instance;
149 /* Xresources preferences */
158 enum resource_type type;
162 /* function declarations */
163 static void applyrules(Client *c);
164 static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact);
165 static void arrange(Monitor *m);
166 static void arrangemon(Monitor *m);
167 static void attach(Client *c);
168 static void attachstack(Client *c);
169 static void buttonpress(XEvent *e);
170 static void checkotherwm(void);
171 static void cleanup(void);
172 static void cleanupmon(Monitor *mon);
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 Monitor *dirtomon(int dir);
182 static void drawbar(Monitor *m);
183 static void drawbars(void);
184 static void enternotify(XEvent *e);
185 static void expose(XEvent *e);
186 static void focus(Client *c);
187 static void focusin(XEvent *e);
188 static void focusmon(const Arg *arg);
189 static void focusstack(const Arg *arg);
190 static Atom getatomprop(Client *c, Atom prop);
191 static int getrootptr(int *x, int *y);
192 static long getstate(Window w);
193 static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
194 static void grabbuttons(Client *c, int focused);
195 static void grabkeys(void);
196 static void incnmaster(const Arg *arg);
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 motionnotify(XEvent *e);
204 static void movemouse(const Arg *arg);
205 static Client *nexttiled(Client *c);
206 static void pop(Client *);
207 static void propertynotify(XEvent *e);
208 static void quit(const Arg *arg);
209 static Monitor *recttomon(int x, int y, int w, int h);
210 static void resize(Client *c, int x, int y, int w, int h, int interact);
211 static void resizeclient(Client *c, int x, int y, int w, int h);
212 static void resizemouse(const Arg *arg);
213 static void restack(Monitor *m);
214 static void run(void);
215 static void runautostart(void);
216 static void scan(void);
217 static int sendevent(Client *c, Atom proto);
218 static void sendmon(Client *c, Monitor *m);
219 static void setclientstate(Client *c, long state);
220 static void setfocus(Client *c);
221 static void setfullscreen(Client *c, int fullscreen);
222 static void setgaps(const Arg *arg);
223 static void setlayout(const Arg *arg);
224 static void setmfact(const Arg *arg);
225 static void setup(void);
226 static void seturgent(Client *c, int urg);
227 static void showhide(Client *c);
228 static void sigchld(int unused);
229 static void spawn(const Arg *arg);
230 static void tag(const Arg *arg);
231 static void tagmon(const Arg *arg);
232 static void tile(Monitor *);
233 static void togglebar(const Arg *arg);
234 static void togglefloating(const Arg *arg);
235 static void toggletag(const Arg *arg);
236 static void toggleview(const Arg *arg);
237 static void unfocus(Client *c, int setfocus);
238 static void unmanage(Client *c, int destroyed);
239 static void unmapnotify(XEvent *e);
240 static void updatebarpos(Monitor *m);
241 static void updatebars(void);
242 static void updateclientlist(void);
243 static int updategeom(void);
244 static void updatenumlockmask(void);
245 static void updatesizehints(Client *c);
246 static void updatestatus(void);
247 static void updatetitle(Client *c);
248 static void updatewindowtype(Client *c);
249 static void updatewmhints(Client *c);
250 static void view(const Arg *arg);
251 static Client *wintoclient(Window w);
252 static Monitor *wintomon(Window w);
253 static int xerror(Display *dpy, XErrorEvent *ee);
254 static int xerrordummy(Display *dpy, XErrorEvent *ee);
255 static int xerrorstart(Display *dpy, XErrorEvent *ee);
256 static void zoom(const Arg *arg);
257 static void load_xresources(void);
258 static void resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst);
261 static const char autostartblocksh[] = "autostart_blocking.sh";
262 static const char autostartsh[] = "autostart.sh";
263 static const char broken[] = "broken";
264 static const char dwmdir[] = "dwm";
265 static const char localshare[] = ".local/share";
266 static char stext[256];
268 static int sw, sh; /* X display screen geometry width, height */
269 static int bh, blw = 0; /* bar geometry */
270 static int lrpad; /* sum of left and right padding for text */
271 static int vp; /* vertical padding for bar */
272 static int sp; /* side padding for bar */
273 static int (*xerrorxlib)(Display *, XErrorEvent *);
274 static unsigned int numlockmask = 0;
275 static void (*handler[LASTEvent]) (XEvent *) = {
276 [ButtonPress] = buttonpress,
277 [ClientMessage] = clientmessage,
278 [ConfigureRequest] = configurerequest,
279 [ConfigureNotify] = configurenotify,
280 [DestroyNotify] = destroynotify,
281 [EnterNotify] = enternotify,
284 [KeyPress] = keypress,
285 [MappingNotify] = mappingnotify,
286 [MapRequest] = maprequest,
287 [MotionNotify] = motionnotify,
288 [PropertyNotify] = propertynotify,
289 [UnmapNotify] = unmapnotify
291 static Atom wmatom[WMLast], netatom[NetLast];
292 static int running = 1;
293 static Cur *cursor[CurLast];
297 static Monitor *mons, *selmon;
298 static Window root, wmcheckwin;
300 /* configuration, allows nested code to access above variables */
304 unsigned int curtag, prevtag; /* current and previous tag */
305 int nmasters[LENGTH(tags) + 1]; /* number of windows in master area */
306 float mfacts[LENGTH(tags) + 1]; /* mfacts per tag */
307 unsigned int sellts[LENGTH(tags) + 1]; /* selected layouts */
308 const Layout *ltidxs[LENGTH(tags) + 1][2]; /* matrix of tags and layouts indexes */
309 int showbars[LENGTH(tags) + 1]; /* display bar for the current tag */
312 /* compile-time check if all tags fit into an unsigned int bit array. */
313 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
315 /* function implementations */
317 applyrules(Client *c)
319 const char *class, *instance;
323 XClassHint ch = { NULL, NULL };
328 XGetClassHint(dpy, c->win, &ch);
329 class = ch.res_class ? ch.res_class : broken;
330 instance = ch.res_name ? ch.res_name : broken;
332 for (i = 0; i < LENGTH(rules); i++) {
334 if ((!r->title || strstr(c->name, r->title))
335 && (!r->class || strstr(class, r->class))
336 && (!r->instance || strstr(instance, r->instance)))
338 c->isfloating = r->isfloating;
340 for (m = mons; m && m->num != r->monitor; m = m->next);
349 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
353 applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact)
358 /* set minimum possible */
366 if (*x + *w + 2 * c->bw < 0)
368 if (*y + *h + 2 * c->bw < 0)
371 if (*x >= m->wx + m->ww)
372 *x = m->wx + m->ww - WIDTH(c);
373 if (*y >= m->wy + m->wh)
374 *y = m->wy + m->wh - HEIGHT(c);
375 if (*x + *w + 2 * c->bw <= m->wx)
377 if (*y + *h + 2 * c->bw <= m->wy)
384 if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
385 /* see last two sentences in ICCCM 4.1.2.3 */
386 baseismin = c->basew == c->minw && c->baseh == c->minh;
387 if (!baseismin) { /* temporarily remove base dimensions */
391 /* adjust for aspect limits */
392 if (c->mina > 0 && c->maxa > 0) {
393 if (c->maxa < (float)*w / *h)
394 *w = *h * c->maxa + 0.5;
395 else if (c->mina < (float)*h / *w)
396 *h = *w * c->mina + 0.5;
398 if (baseismin) { /* increment calculation requires this */
402 /* adjust for increment value */
407 /* restore base dimensions */
408 *w = MAX(*w + c->basew, c->minw);
409 *h = MAX(*h + c->baseh, c->minh);
411 *w = MIN(*w, c->maxw);
413 *h = MIN(*h, c->maxh);
415 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
423 else for (m = mons; m; m = m->next)
428 } else for (m = mons; m; m = m->next)
433 arrangemon(Monitor *m)
435 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
436 if (m->lt[m->sellt]->arrange)
437 m->lt[m->sellt]->arrange(m);
443 c->next = c->mon->clients;
448 attachstack(Client *c)
450 c->snext = c->mon->stack;
455 buttonpress(XEvent *e)
457 unsigned int i, x, click;
461 XButtonPressedEvent *ev = &e->xbutton;
464 /* focus monitor if necessary */
465 if ((m = wintomon(ev->window)) && m != selmon) {
466 unfocus(selmon->sel, 1);
470 if (ev->window == selmon->barwin) {
474 while (ev->x >= x && ++i < LENGTH(tags));
475 if (i < LENGTH(tags)) {
478 } else if (ev->x < x + blw)
480 else if (ev->x > selmon->ww - TEXTW(stext))
481 click = ClkStatusText;
484 } else if ((c = wintoclient(ev->window))) {
487 XAllowEvents(dpy, ReplayPointer, CurrentTime);
488 click = ClkClientWin;
490 for (i = 0; i < LENGTH(buttons); i++)
491 if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
492 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
493 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
499 xerrorxlib = XSetErrorHandler(xerrorstart);
500 /* this causes an error if some other window manager is running */
501 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
503 XSetErrorHandler(xerror);
511 Layout foo = { "", NULL };
516 selmon->lt[selmon->sellt] = &foo;
517 for (m = mons; m; m = m->next)
519 unmanage(m->stack, 0);
520 XUngrabKey(dpy, AnyKey, AnyModifier, root);
523 for (i = 0; i < CurLast; i++)
524 drw_cur_free(drw, cursor[i]);
525 for (i = 0; i < LENGTH(colors); i++)
527 XDestroyWindow(dpy, wmcheckwin);
530 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
531 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
535 cleanupmon(Monitor *mon)
542 for (m = mons; m && m->next != mon; m = m->next);
545 XUnmapWindow(dpy, mon->barwin);
546 XDestroyWindow(dpy, mon->barwin);
551 clientmessage(XEvent *e)
553 XClientMessageEvent *cme = &e->xclient;
554 Client *c = wintoclient(cme->window);
558 if (cme->message_type == netatom[NetWMState]) {
559 if (cme->data.l[1] == netatom[NetWMFullscreen]
560 || cme->data.l[2] == netatom[NetWMFullscreen])
561 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
562 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
563 } else if (cme->message_type == netatom[NetActiveWindow]) {
564 if (c != selmon->sel && !c->isurgent)
574 ce.type = ConfigureNotify;
582 ce.border_width = c->bw;
584 ce.override_redirect = False;
585 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
589 configurenotify(XEvent *e)
593 XConfigureEvent *ev = &e->xconfigure;
596 /* TODO: updategeom handling sucks, needs to be simplified */
597 if (ev->window == root) {
598 dirty = (sw != ev->width || sh != ev->height);
601 if (updategeom() || dirty) {
602 drw_resize(drw, sw, bh);
604 for (m = mons; m; m = m->next) {
605 for (c = m->clients; c; c = c->next)
607 resizeclient(c, m->mx, m->my, m->mw, m->mh);
608 XMoveResizeWindow(dpy, m->barwin, m->wx + sp, m->by + vp, m->ww - 2 * sp, bh);
617 configurerequest(XEvent *e)
621 XConfigureRequestEvent *ev = &e->xconfigurerequest;
624 if ((c = wintoclient(ev->window))) {
625 if (ev->value_mask & CWBorderWidth)
626 c->bw = ev->border_width;
627 else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
629 if (ev->value_mask & CWX) {
631 c->x = m->mx + ev->x;
633 if (ev->value_mask & CWY) {
635 c->y = m->my + ev->y;
637 if (ev->value_mask & CWWidth) {
641 if (ev->value_mask & CWHeight) {
645 if ((c->x + c->w) > m->mx + m->mw && c->isfloating)
646 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
647 if ((c->y + c->h) > m->my + m->mh && c->isfloating)
648 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
649 if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
652 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
658 wc.width = ev->width;
659 wc.height = ev->height;
660 wc.border_width = ev->border_width;
661 wc.sibling = ev->above;
662 wc.stack_mode = ev->detail;
663 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
674 m = ecalloc(1, sizeof(Monitor));
675 m->tagset[0] = m->tagset[1] = 1;
677 m->nmaster = nmaster;
678 m->showbar = showbar;
681 m->lt[0] = &layouts[0];
682 m->lt[1] = &layouts[1 % LENGTH(layouts)];
683 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
684 m->pertag = ecalloc(1, sizeof(Pertag));
685 m->pertag->curtag = m->pertag->prevtag = 1;
687 for (i = 0; i <= LENGTH(tags); i++) {
688 m->pertag->nmasters[i] = m->nmaster;
689 m->pertag->mfacts[i] = m->mfact;
691 m->pertag->ltidxs[i][0] = m->lt[0];
692 m->pertag->ltidxs[i][1] = m->lt[1];
693 m->pertag->sellts[i] = m->sellt;
695 m->pertag->showbars[i] = m->showbar;
702 destroynotify(XEvent *e)
705 XDestroyWindowEvent *ev = &e->xdestroywindow;
707 if ((c = wintoclient(ev->window)))
716 for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
721 detachstack(Client *c)
725 for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
728 if (c == c->mon->sel) {
729 for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
740 if (!(m = selmon->next))
742 } else if (selmon == mons)
743 for (m = mons; m->next; m = m->next);
745 for (m = mons; m->next != selmon; m = m->next);
753 int boxs = drw->fonts->h / 9;
754 int boxw = drw->fonts->h / 6 + 2;
755 unsigned int i, occ = 0, urg = 0;
758 /* draw status first so it can be overdrawn by tags later */
759 if (m == selmon) { /* status is only drawn on selected monitor */
760 drw_setscheme(drw, scheme[SchemeNorm]);
761 tw = TEXTW(stext) - lrpad + 2; /* 2px right padding */
762 drw_text(drw, m->ww - tw - 2 * sp, 0, tw, bh, 0, stext, 0);
765 for (c = m->clients; c; c = c->next) {
771 for (i = 0; i < LENGTH(tags); i++) {
773 drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]);
774 drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i);
776 drw_rect(drw, x + boxs, boxs, boxw, boxw,
777 m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
781 w = blw = TEXTW(m->ltsymbol);
782 drw_setscheme(drw, scheme[SchemeNorm]);
783 x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0);
785 if ((w = m->ww - tw - x) > bh) {
787 drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]);
788 drw_text(drw, x, 0, w - 2 * sp, bh, lrpad / 2, m->sel->name, 0);
789 if (m->sel->isfloating)
790 drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
792 drw_setscheme(drw, scheme[SchemeNorm]);
793 drw_rect(drw, x, 0, w - 2 * sp, bh, 1, 1);
796 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
804 for (m = mons; m; m = m->next)
809 enternotify(XEvent *e)
813 XCrossingEvent *ev = &e->xcrossing;
815 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
817 c = wintoclient(ev->window);
818 m = c ? c->mon : wintomon(ev->window);
820 unfocus(selmon->sel, 1);
822 } else if (!c || c == selmon->sel)
831 XExposeEvent *ev = &e->xexpose;
833 if (ev->count == 0 && (m = wintomon(ev->window)))
840 if (!c || !ISVISIBLE(c))
841 for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
842 if (selmon->sel && selmon->sel != c)
843 unfocus(selmon->sel, 0);
845 if (c->mon != selmon)
852 XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel);
855 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
856 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
862 /* there are some broken focus acquiring clients needing extra handling */
866 XFocusChangeEvent *ev = &e->xfocus;
868 if (selmon->sel && ev->window != selmon->sel->win)
869 setfocus(selmon->sel);
873 focusmon(const Arg *arg)
879 if ((m = dirtomon(arg->i)) == selmon)
881 unfocus(selmon->sel, 0);
887 focusstack(const Arg *arg)
889 Client *c = NULL, *i;
894 for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
896 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)
917 unsigned char *p = NULL;
918 Atom da, atom = None;
920 if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
921 &da, &di, &dl, &dl, &p) == Success && p) {
929 getrootptr(int *x, int *y)
935 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
943 unsigned char *p = NULL;
944 unsigned long n, extra;
947 if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
948 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
957 gettextprop(Window w, Atom atom, char *text, unsigned int size)
963 if (!text || size == 0)
966 if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems)
968 if (name.encoding == XA_STRING)
969 strncpy(text, (char *)name.value, size - 1);
971 if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
972 strncpy(text, *list, size - 1);
973 XFreeStringList(list);
976 text[size - 1] = '\0';
982 grabbuttons(Client *c, int focused)
987 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
988 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
990 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
991 BUTTONMASK, GrabModeSync, GrabModeSync, None, None);
992 for (i = 0; i < LENGTH(buttons); i++)
993 if (buttons[i].click == ClkClientWin)
994 for (j = 0; j < LENGTH(modifiers); j++)
995 XGrabButton(dpy, buttons[i].button,
996 buttons[i].mask | modifiers[j],
997 c->win, False, BUTTONMASK,
998 GrabModeAsync, GrabModeSync, None, None);
1005 updatenumlockmask();
1008 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1011 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1012 for (i = 0; i < LENGTH(keys); i++)
1013 if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
1014 for (j = 0; j < LENGTH(modifiers); j++)
1015 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
1016 True, GrabModeAsync, GrabModeAsync);
1021 incnmaster(const Arg *arg)
1024 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
1025 for(i=0; i<LENGTH(tags); ++i)
1026 if(selmon->tagset[selmon->seltags] & 1<<i)
1027 selmon->pertag->nmasters[i+1] = selmon->nmaster;
1029 if(selmon->pertag->curtag == 0)
1031 selmon->pertag->nmasters[0] = selmon->nmaster;
1038 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
1041 if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1042 && unique[n].width == info->width && unique[n].height == info->height)
1046 #endif /* XINERAMA */
1056 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1057 for (i = 0; i < LENGTH(keys); i++)
1058 if (keysym == keys[i].keysym
1059 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1061 keys[i].func(&(keys[i].arg));
1065 killclient(const Arg *arg)
1069 if (!sendevent(selmon->sel, wmatom[WMDelete])) {
1071 XSetErrorHandler(xerrordummy);
1072 XSetCloseDownMode(dpy, DestroyAll);
1073 XKillClient(dpy, selmon->sel->win);
1075 XSetErrorHandler(xerror);
1081 manage(Window w, XWindowAttributes *wa)
1083 Client *c, *t = NULL;
1084 Window trans = None;
1087 c = ecalloc(1, sizeof(Client));
1090 c->x = c->oldx = wa->x;
1091 c->y = c->oldy = wa->y;
1092 c->w = c->oldw = wa->width;
1093 c->h = c->oldh = wa->height;
1094 c->oldbw = wa->border_width;
1097 if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1105 if (c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1106 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1107 if (c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1108 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1109 c->x = MAX(c->x, c->mon->mx);
1110 /* only fix client y-offset, if the client center might cover the bar */
1111 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1112 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1115 wc.border_width = c->bw;
1116 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1117 XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel);
1118 configure(c); /* propagates border_width, if size doesn't change */
1119 updatewindowtype(c);
1122 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1125 c->isfloating = c->oldstate = trans != None || c->isfixed;
1127 XRaiseWindow(dpy, c->win);
1130 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1131 (unsigned char *) &(c->win), 1);
1132 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1133 setclientstate(c, NormalState);
1134 if (c->mon == selmon)
1135 unfocus(selmon->sel, 0);
1138 XMapWindow(dpy, c->win);
1143 mappingnotify(XEvent *e)
1145 XMappingEvent *ev = &e->xmapping;
1147 XRefreshKeyboardMapping(ev);
1148 if (ev->request == MappingKeyboard)
1153 maprequest(XEvent *e)
1155 static XWindowAttributes wa;
1156 XMapRequestEvent *ev = &e->xmaprequest;
1158 if (!XGetWindowAttributes(dpy, ev->window, &wa))
1160 if (wa.override_redirect)
1162 if (!wintoclient(ev->window))
1163 manage(ev->window, &wa);
1172 for (c = m->clients; c; c = c->next)
1175 if (n > 0) /* override layout symbol */
1176 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1177 for (c = nexttiled(m->clients); c; c = nexttiled(c->next))
1178 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
1182 motionnotify(XEvent *e)
1184 static Monitor *mon = NULL;
1186 XMotionEvent *ev = &e->xmotion;
1188 if (ev->window != root)
1190 if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1191 unfocus(selmon->sel, 1);
1199 movemouse(const Arg *arg)
1201 int x, y, ocx, ocy, nx, ny;
1207 if (!(c = selmon->sel))
1209 if (c->isfullscreen) /* no support moving fullscreen windows by mouse */
1214 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1215 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1217 if (!getrootptr(&x, &y))
1220 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1222 case ConfigureRequest:
1225 handler[ev.type](&ev);
1228 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1230 lasttime = ev.xmotion.time;
1232 nx = ocx + (ev.xmotion.x - x);
1233 ny = ocy + (ev.xmotion.y - y);
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);
1245 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1246 resize(c, nx, ny, c->w, c->h, 1);
1249 } while (ev.type != ButtonRelease);
1250 XUngrabPointer(dpy, CurrentTime);
1251 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1259 nexttiled(Client *c)
1261 for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1275 propertynotify(XEvent *e)
1279 XPropertyEvent *ev = &e->xproperty;
1281 if ((ev->window == root) && (ev->atom == XA_WM_NAME))
1283 else if (ev->state == PropertyDelete)
1284 return; /* ignore */
1285 else if ((c = wintoclient(ev->window))) {
1288 case XA_WM_TRANSIENT_FOR:
1289 if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1290 (c->isfloating = (wintoclient(trans)) != NULL))
1293 case XA_WM_NORMAL_HINTS:
1301 if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1303 if (c == c->mon->sel)
1306 if (ev->atom == netatom[NetWMWindowType])
1307 updatewindowtype(c);
1312 quit(const Arg *arg)
1318 recttomon(int x, int y, int w, int h)
1320 Monitor *m, *r = selmon;
1323 for (m = mons; m; m = m->next)
1324 if ((a = INTERSECT(x, y, w, h, m)) > area) {
1332 resize(Client *c, int x, int y, int w, int h, int interact)
1334 if (applysizehints(c, &x, &y, &w, &h, interact))
1335 resizeclient(c, x, y, w, h);
1339 resizeclient(Client *c, int x, int y, int w, int h)
1343 c->oldx = c->x; c->x = wc.x = x;
1344 c->oldy = c->y; c->y = wc.y = y;
1345 c->oldw = c->w; c->w = wc.width = w;
1346 c->oldh = c->h; c->h = wc.height = h;
1347 wc.border_width = c->bw;
1348 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1354 resizemouse(const Arg *arg)
1356 int ocx, ocy, nw, nh;
1362 if (!(c = selmon->sel))
1364 if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1369 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1370 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1372 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1374 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1376 case ConfigureRequest:
1379 handler[ev.type](&ev);
1382 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1384 lasttime = ev.xmotion.time;
1386 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1387 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1388 if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1389 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1391 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1392 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1393 togglefloating(NULL);
1395 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1396 resize(c, c->x, c->y, nw, nh, 1);
1399 } while (ev.type != ButtonRelease);
1400 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1401 XUngrabPointer(dpy, CurrentTime);
1402 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1403 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1420 if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
1421 XRaiseWindow(dpy, m->sel->win);
1422 if (m->lt[m->sellt]->arrange) {
1423 wc.stack_mode = Below;
1424 wc.sibling = m->barwin;
1425 for (c = m->stack; c; c = c->snext)
1426 if (!c->isfloating && ISVISIBLE(c)) {
1427 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1428 wc.sibling = c->win;
1432 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1439 /* main event loop */
1441 while (running && !XNextEvent(dpy, &ev))
1442 if (handler[ev.type])
1443 handler[ev.type](&ev); /* call handler */
1455 if ((home = getenv("HOME")) == NULL)
1456 /* this is almost impossible */
1459 /* if $XDG_DATA_HOME is set and not empty, use $XDG_DATA_HOME/dwm,
1460 * otherwise use ~/.local/share/dwm as autostart script directory
1462 xdgdatahome = getenv("XDG_DATA_HOME");
1463 if (xdgdatahome != NULL && *xdgdatahome != '\0') {
1464 /* space for path segments, separators and nul */
1465 pathpfx = ecalloc(1, strlen(xdgdatahome) + strlen(dwmdir) + 2);
1467 if (sprintf(pathpfx, "%s/%s", xdgdatahome, dwmdir) <= 0) {
1472 /* space for path segments, separators and nul */
1473 pathpfx = ecalloc(1, strlen(home) + strlen(localshare)
1474 + strlen(dwmdir) + 3);
1476 if (sprintf(pathpfx, "%s/%s/%s", home, localshare, dwmdir) < 0) {
1482 /* check if the autostart script directory exists */
1483 if (! (stat(pathpfx, &sb) == 0 && S_ISDIR(sb.st_mode))) {
1484 /* the XDG conformant path does not exist or is no directory
1485 * so we try ~/.dwm instead
1487 char *pathpfx_new = realloc(pathpfx, strlen(home) + strlen(dwmdir) + 3);
1488 if(pathpfx_new == NULL) {
1492 pathpfx = pathpfx_new;
1494 if (sprintf(pathpfx, "%s/.%s", home, dwmdir) <= 0) {
1500 /* try the blocking script first */
1501 path = ecalloc(1, strlen(pathpfx) + strlen(autostartblocksh) + 2);
1502 if (sprintf(path, "%s/%s", pathpfx, autostartblocksh) <= 0) {
1507 if (access(path, X_OK) == 0)
1510 /* now the non-blocking script */
1511 if (sprintf(path, "%s/%s", pathpfx, autostartsh) <= 0) {
1516 if (access(path, X_OK) == 0)
1517 system(strcat(path, " &"));
1526 unsigned int i, num;
1527 Window d1, d2, *wins = NULL;
1528 XWindowAttributes wa;
1530 if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1531 for (i = 0; i < num; i++) {
1532 if (!XGetWindowAttributes(dpy, wins[i], &wa)
1533 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1535 if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1536 manage(wins[i], &wa);
1538 for (i = 0; i < num; i++) { /* now the transients */
1539 if (!XGetWindowAttributes(dpy, wins[i], &wa))
1541 if (XGetTransientForHint(dpy, wins[i], &d1)
1542 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1543 manage(wins[i], &wa);
1551 sendmon(Client *c, Monitor *m)
1559 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1567 setclientstate(Client *c, long state)
1569 long data[] = { state, None };
1571 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1572 PropModeReplace, (unsigned char *)data, 2);
1576 sendevent(Client *c, Atom proto)
1583 if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1584 while (!exists && n--)
1585 exists = protocols[n] == proto;
1589 ev.type = ClientMessage;
1590 ev.xclient.window = c->win;
1591 ev.xclient.message_type = wmatom[WMProtocols];
1592 ev.xclient.format = 32;
1593 ev.xclient.data.l[0] = proto;
1594 ev.xclient.data.l[1] = CurrentTime;
1595 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1603 if (!c->neverfocus) {
1604 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1605 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1606 XA_WINDOW, 32, PropModeReplace,
1607 (unsigned char *) &(c->win), 1);
1609 sendevent(c, wmatom[WMTakeFocus]);
1613 setfullscreen(Client *c, int fullscreen)
1615 if (fullscreen && !c->isfullscreen) {
1616 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1617 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1618 c->isfullscreen = 1;
1619 c->oldstate = c->isfloating;
1623 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1624 XRaiseWindow(dpy, c->win);
1625 } else if (!fullscreen && c->isfullscreen){
1626 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1627 PropModeReplace, (unsigned char*)0, 0);
1628 c->isfullscreen = 0;
1629 c->isfloating = c->oldstate;
1635 resizeclient(c, c->x, c->y, c->w, c->h);
1641 setgaps(const Arg *arg)
1643 if ((arg->i == 0) || (selmon->gappx + arg->i < 0))
1646 selmon->gappx += arg->i;
1651 setlayout(const Arg *arg)
1654 if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1657 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1658 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1660 for(i=0; i<LENGTH(tags); ++i)
1661 if(selmon->tagset[selmon->seltags] & 1<<i)
1663 selmon->pertag->ltidxs[i+1][selmon->sellt] = selmon->lt[selmon->sellt];
1664 selmon->pertag->sellts[i+1] = selmon->sellt;
1667 if(selmon->pertag->curtag == 0)
1669 selmon->pertag->ltidxs[0][selmon->sellt] = selmon->lt[selmon->sellt];
1670 selmon->pertag->sellts[0] = selmon->sellt;
1679 /* arg > 1.0 will set mfact absolutely */
1681 setmfact(const Arg *arg)
1686 if (!arg || !selmon->lt[selmon->sellt]->arrange)
1688 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1691 if (f < 0.05 || f > 0.95)
1694 for(i=0; i<LENGTH(tags); ++i)
1695 if(selmon->tagset[selmon->seltags] & 1<<i)
1696 selmon->pertag->mfacts[i+1] = f;
1698 if(selmon->pertag->curtag == 0)
1700 selmon->pertag->mfacts[0] = f;
1709 XSetWindowAttributes wa;
1712 /* clean up any zombies immediately */
1716 screen = DefaultScreen(dpy);
1717 sw = DisplayWidth(dpy, screen);
1718 sh = DisplayHeight(dpy, screen);
1719 root = RootWindow(dpy, screen);
1720 drw = drw_create(dpy, screen, root, sw, sh);
1721 if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
1722 die("no fonts could be loaded.");
1723 lrpad = drw->fonts->h;
1724 bh = drw->fonts->h + 2;
1727 vp = (topbar == 1) ? vertpad : - vertpad;
1730 utf8string = XInternAtom(dpy, "UTF8_STRING", False);
1731 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1732 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1733 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1734 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1735 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1736 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1737 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1738 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1739 netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
1740 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1741 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1742 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1743 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1745 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1746 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1747 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1748 /* init appearance */
1749 scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
1750 for (i = 0; i < LENGTH(colors); i++)
1751 scheme[i] = drw_scm_create(drw, colors[i], 3);
1755 updatebarpos(selmon);
1756 /* supporting window for NetWMCheck */
1757 wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
1758 XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
1759 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1760 XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8,
1761 PropModeReplace, (unsigned char *) "dwm", 3);
1762 XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32,
1763 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1764 /* EWMH support per view */
1765 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1766 PropModeReplace, (unsigned char *) netatom, NetLast);
1767 XDeleteProperty(dpy, root, netatom[NetClientList]);
1769 wa.cursor = cursor[CurNormal]->cursor;
1770 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
1771 |ButtonPressMask|PointerMotionMask|EnterWindowMask
1772 |LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1773 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1774 XSelectInput(dpy, root, wa.event_mask);
1781 seturgent(Client *c, int urg)
1786 if (!(wmh = XGetWMHints(dpy, c->win)))
1788 wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
1789 XSetWMHints(dpy, c->win, wmh);
1799 /* show clients top down */
1800 XMoveWindow(dpy, c->win, c->x, c->y);
1801 if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1802 resize(c, c->x, c->y, c->w, c->h, 0);
1805 /* hide clients bottom up */
1807 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1814 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1815 die("can't install SIGCHLD handler:");
1816 while (0 < waitpid(-1, NULL, WNOHANG));
1820 spawn(const Arg *arg)
1822 if (arg->v == dmenucmd)
1823 dmenumon[0] = '0' + selmon->num;
1826 close(ConnectionNumber(dpy));
1828 execvp(((char **)arg->v)[0], (char **)arg->v);
1829 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1838 if (selmon->sel && arg->ui & TAGMASK) {
1839 selmon->sel->tags = arg->ui & TAGMASK;
1846 tagmon(const Arg *arg)
1848 if (!selmon->sel || !mons->next)
1850 sendmon(selmon->sel, dirtomon(arg->i));
1856 unsigned int i, n, h, mw, my, ty;
1859 for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1864 mw = m->nmaster ? m->ww * m->mfact : 0;
1866 mw = m->ww - m->gappx;
1867 for (i = 0, my = ty = m->gappx, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1868 if (i < m->nmaster) {
1869 h = (m->wh - my) / (MIN(n, m->nmaster) - i) - m->gappx;
1870 resize(c, m->wx + m->gappx, m->wy + my, mw - (2*c->bw) - m->gappx, h - (2*c->bw), 0);
1871 if (my + HEIGHT(c) + m->gappx < m->wh)
1872 my += HEIGHT(c) + m->gappx;
1874 h = (m->wh - ty) / (n - i) - m->gappx;
1875 resize(c, m->wx + mw + m->gappx, m->wy + ty, m->ww - mw - (2*c->bw) - 2*m->gappx, h - (2*c->bw), 0);
1876 if (ty + HEIGHT(c) + m->gappx < m->wh)
1877 ty += HEIGHT(c) + m->gappx;
1882 togglebar(const Arg *arg)
1885 selmon->showbar = !selmon->showbar;
1886 for(i=0; i<LENGTH(tags); ++i)
1887 if(selmon->tagset[selmon->seltags] & 1<<i)
1888 selmon->pertag->showbars[i+1] = selmon->showbar;
1890 if(selmon->pertag->curtag == 0)
1892 selmon->pertag->showbars[0] = selmon->showbar;
1894 updatebarpos(selmon);
1895 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx + sp, selmon->by + vp, selmon->ww - 2 * sp, bh);
1900 togglefloating(const Arg *arg)
1904 if (selmon->sel->isfullscreen) /* no support for fullscreen windows */
1906 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1907 if (selmon->sel->isfloating)
1908 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1909 selmon->sel->w, selmon->sel->h, 0);
1914 toggletag(const Arg *arg)
1916 unsigned int newtags;
1920 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1922 selmon->sel->tags = newtags;
1929 toggleview(const Arg *arg)
1931 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1935 selmon->tagset[selmon->seltags] = newtagset;
1937 if (newtagset == ~0) {
1938 selmon->pertag->prevtag = selmon->pertag->curtag;
1939 selmon->pertag->curtag = 0;
1942 /* test if the user did not select the same tag */
1943 if (!(newtagset & 1 << (selmon->pertag->curtag - 1))) {
1944 selmon->pertag->prevtag = selmon->pertag->curtag;
1945 for (i = 0; !(newtagset & 1 << i); i++) ;
1946 selmon->pertag->curtag = i + 1;
1949 /* apply settings for this view */
1950 selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag];
1951 selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag];
1952 selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
1953 selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
1954 selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1];
1956 if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag])
1965 unfocus(Client *c, int setfocus)
1970 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
1972 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1973 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1978 unmanage(Client *c, int destroyed)
1980 Monitor *m = c->mon;
1986 wc.border_width = c->oldbw;
1987 XGrabServer(dpy); /* avoid race conditions */
1988 XSetErrorHandler(xerrordummy);
1989 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1990 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1991 setclientstate(c, WithdrawnState);
1993 XSetErrorHandler(xerror);
2003 unmapnotify(XEvent *e)
2006 XUnmapEvent *ev = &e->xunmap;
2008 if ((c = wintoclient(ev->window))) {
2010 setclientstate(c, WithdrawnState);
2020 XSetWindowAttributes wa = {
2021 .override_redirect = True,
2022 .background_pixmap = ParentRelative,
2023 .event_mask = ButtonPressMask|ExposureMask
2025 XClassHint ch = {"dwm", "dwm"};
2026 for (m = mons; m; m = m->next) {
2029 m->barwin = XCreateWindow(dpy, root, m->wx + sp, m->by + vp, m->ww - 2 * sp, bh, 0, DefaultDepth(dpy, screen),
2030 CopyFromParent, DefaultVisual(dpy, screen),
2031 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
2032 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
2033 XMapRaised(dpy, m->barwin);
2034 XSetClassHint(dpy, m->barwin, &ch);
2039 updatebarpos(Monitor *m)
2044 m->wh = m->wh - vertpad - bh;
2045 m->by = m->topbar ? m->wy : m->wy + m->wh + vertpad;
2046 m->wy = m->topbar ? m->wy + bh + vp : m->wy;
2057 XDeleteProperty(dpy, root, netatom[NetClientList]);
2058 for (m = mons; m; m = m->next)
2059 for (c = m->clients; c; c = c->next)
2060 XChangeProperty(dpy, root, netatom[NetClientList],
2061 XA_WINDOW, 32, PropModeAppend,
2062 (unsigned char *) &(c->win), 1);
2071 if (XineramaIsActive(dpy)) {
2075 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
2076 XineramaScreenInfo *unique = NULL;
2078 for (n = 0, m = mons; m; m = m->next, n++);
2079 /* only consider unique geometries as separate screens */
2080 unique = ecalloc(nn, sizeof(XineramaScreenInfo));
2081 for (i = 0, j = 0; i < nn; i++)
2082 if (isuniquegeom(unique, j, &info[i]))
2083 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
2086 if (n <= nn) { /* new monitors available */
2087 for (i = 0; i < (nn - n); i++) {
2088 for (m = mons; m && m->next; m = m->next);
2090 m->next = createmon();
2094 for (i = 0, m = mons; i < nn && m; m = m->next, i++)
2096 || unique[i].x_org != m->mx || unique[i].y_org != m->my
2097 || unique[i].width != m->mw || unique[i].height != m->mh)
2101 m->mx = m->wx = unique[i].x_org;
2102 m->my = m->wy = unique[i].y_org;
2103 m->mw = m->ww = unique[i].width;
2104 m->mh = m->wh = unique[i].height;
2107 } else { /* less monitors available nn < n */
2108 for (i = nn; i < n; i++) {
2109 for (m = mons; m && m->next; m = m->next);
2110 while ((c = m->clients)) {
2112 m->clients = c->next;
2125 #endif /* XINERAMA */
2126 { /* default monitor setup */
2129 if (mons->mw != sw || mons->mh != sh) {
2131 mons->mw = mons->ww = sw;
2132 mons->mh = mons->wh = sh;
2138 selmon = wintomon(root);
2144 updatenumlockmask(void)
2147 XModifierKeymap *modmap;
2150 modmap = XGetModifierMapping(dpy);
2151 for (i = 0; i < 8; i++)
2152 for (j = 0; j < modmap->max_keypermod; j++)
2153 if (modmap->modifiermap[i * modmap->max_keypermod + j]
2154 == XKeysymToKeycode(dpy, XK_Num_Lock))
2155 numlockmask = (1 << i);
2156 XFreeModifiermap(modmap);
2160 updatesizehints(Client *c)
2165 if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
2166 /* size is uninitialized, ensure that size.flags aren't used */
2168 if (size.flags & PBaseSize) {
2169 c->basew = size.base_width;
2170 c->baseh = size.base_height;
2171 } else if (size.flags & PMinSize) {
2172 c->basew = size.min_width;
2173 c->baseh = size.min_height;
2175 c->basew = c->baseh = 0;
2176 if (size.flags & PResizeInc) {
2177 c->incw = size.width_inc;
2178 c->inch = size.height_inc;
2180 c->incw = c->inch = 0;
2181 if (size.flags & PMaxSize) {
2182 c->maxw = size.max_width;
2183 c->maxh = size.max_height;
2185 c->maxw = c->maxh = 0;
2186 if (size.flags & PMinSize) {
2187 c->minw = size.min_width;
2188 c->minh = size.min_height;
2189 } else if (size.flags & PBaseSize) {
2190 c->minw = size.base_width;
2191 c->minh = size.base_height;
2193 c->minw = c->minh = 0;
2194 if (size.flags & PAspect) {
2195 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2196 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2198 c->maxa = c->mina = 0.0;
2199 c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh);
2205 if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2206 strcpy(stext, "dwm-"VERSION);
2211 updatetitle(Client *c)
2213 if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2214 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2215 if (c->name[0] == '\0') /* hack to mark broken clients */
2216 strcpy(c->name, broken);
2220 updatewindowtype(Client *c)
2222 Atom state = getatomprop(c, netatom[NetWMState]);
2223 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2225 if (state == netatom[NetWMFullscreen])
2226 setfullscreen(c, 1);
2227 if (wtype == netatom[NetWMWindowTypeDialog])
2232 updatewmhints(Client *c)
2236 if ((wmh = XGetWMHints(dpy, c->win))) {
2237 if (c == selmon->sel && wmh->flags & XUrgencyHint) {
2238 wmh->flags &= ~XUrgencyHint;
2239 XSetWMHints(dpy, c->win, wmh);
2241 c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
2242 if (wmh->flags & InputHint)
2243 c->neverfocus = !wmh->input;
2251 view(const Arg *arg)
2254 unsigned int tmptag;
2256 if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2258 selmon->seltags ^= 1; /* toggle sel tagset */
2259 if (arg->ui & TAGMASK) {
2260 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2261 selmon->pertag->prevtag = selmon->pertag->curtag;
2264 selmon->pertag->curtag = 0;
2266 for (i = 0; !(arg->ui & 1 << i); i++) ;
2267 selmon->pertag->curtag = i + 1;
2270 tmptag = selmon->pertag->prevtag;
2271 selmon->pertag->prevtag = selmon->pertag->curtag;
2272 selmon->pertag->curtag = tmptag;
2275 selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag];
2276 selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag];
2277 selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
2278 selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
2279 selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1];
2281 if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag])
2289 wintoclient(Window w)
2294 for (m = mons; m; m = m->next)
2295 for (c = m->clients; c; c = c->next)
2308 if (w == root && getrootptr(&x, &y))
2309 return recttomon(x, y, 1, 1);
2310 for (m = mons; m; m = m->next)
2313 if ((c = wintoclient(w)))
2318 /* There's no way to check accesses to destroyed windows, thus those cases are
2319 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2320 * default error handler, which may call exit. */
2322 xerror(Display *dpy, XErrorEvent *ee)
2324 if (ee->error_code == BadWindow
2325 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2326 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2327 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2328 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2329 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2330 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2331 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2332 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2334 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2335 ee->request_code, ee->error_code);
2336 return xerrorxlib(dpy, ee); /* may call exit */
2340 xerrordummy(Display *dpy, XErrorEvent *ee)
2345 /* Startup Error handler to check if another window manager
2346 * is already running. */
2348 xerrorstart(Display *dpy, XErrorEvent *ee)
2350 die("dwm: another window manager is already running");
2355 zoom(const Arg *arg)
2357 Client *c = selmon->sel;
2359 if (!selmon->lt[selmon->sellt]->arrange
2360 || (selmon->sel && selmon->sel->isfloating))
2362 if (c == nexttiled(selmon->clients))
2363 if (!c || !(c = nexttiled(c->next)))
2369 resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst)
2383 snprintf(fullname, sizeof(fullname), "%s.%s", "dwm", name);
2384 fullname[sizeof(fullname) - 1] = '\0';
2386 XrmGetResource(db, fullname, "*", &type, &ret);
2387 if (!(ret.addr == NULL || strncmp("String", type, 64)))
2391 strcpy(sdst, ret.addr);
2394 *idst = strtoul(ret.addr, NULL, 10);
2397 *fdst = strtof(ret.addr, NULL);
2404 load_xresources(void)
2411 display = XOpenDisplay(NULL);
2412 resm = XResourceManagerString(display);
2416 db = XrmGetStringDatabase(resm);
2417 for (p = resources; p < resources + LENGTH(resources); p++)
2418 resource_load(db, p->name, p->type, p->dst);
2419 XCloseDisplay(display);
2423 main(int argc, char *argv[])
2425 if (argc == 2 && !strcmp("-v", argv[1]))
2428 die("usage: dwm [-v]");
2429 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2430 fputs("warning: no locale support\n", stderr);
2431 if (!(dpy = XOpenDisplay(NULL)))
2432 die("dwm: cannot open display");
2438 if (pledge("stdio rpath proc exec", NULL) == -1)
2440 #endif /* __OpenBSD__ */
2446 return EXIT_SUCCESS;