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 */));
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)
592 XConfigureEvent *ev = &e->xconfigure;
595 /* TODO: updategeom handling sucks, needs to be simplified */
596 if (ev->window == root) {
597 dirty = (sw != ev->width || sh != ev->height);
600 if (updategeom() || dirty) {
601 drw_resize(drw, sw, bh);
603 for (m = mons; m; m = m->next) {
604 XMoveResizeWindow(dpy, m->barwin, m->wx + sp, m->by + vp, m->ww - 2 * sp, bh);
613 configurerequest(XEvent *e)
617 XConfigureRequestEvent *ev = &e->xconfigurerequest;
620 if ((c = wintoclient(ev->window))) {
621 if (ev->value_mask & CWBorderWidth)
622 c->bw = ev->border_width;
623 else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
625 if (ev->value_mask & CWX) {
627 c->x = m->mx + ev->x;
629 if (ev->value_mask & CWY) {
631 c->y = m->my + ev->y;
633 if (ev->value_mask & CWWidth) {
637 if (ev->value_mask & CWHeight) {
641 if ((c->x + c->w) > m->mx + m->mw && c->isfloating)
642 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
643 if ((c->y + c->h) > m->my + m->mh && c->isfloating)
644 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
645 if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
648 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
654 wc.width = ev->width;
655 wc.height = ev->height;
656 wc.border_width = ev->border_width;
657 wc.sibling = ev->above;
658 wc.stack_mode = ev->detail;
659 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
670 m = ecalloc(1, sizeof(Monitor));
671 m->tagset[0] = m->tagset[1] = 1;
673 m->nmaster = nmaster;
674 m->showbar = showbar;
677 m->lt[0] = &layouts[0];
678 m->lt[1] = &layouts[1 % LENGTH(layouts)];
679 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
680 m->pertag = ecalloc(1, sizeof(Pertag));
681 m->pertag->curtag = m->pertag->prevtag = 1;
683 for (i = 0; i <= LENGTH(tags); i++) {
684 m->pertag->nmasters[i] = m->nmaster;
685 m->pertag->mfacts[i] = m->mfact;
687 m->pertag->ltidxs[i][0] = m->lt[0];
688 m->pertag->ltidxs[i][1] = m->lt[1];
689 m->pertag->sellts[i] = m->sellt;
691 m->pertag->showbars[i] = m->showbar;
698 destroynotify(XEvent *e)
701 XDestroyWindowEvent *ev = &e->xdestroywindow;
703 if ((c = wintoclient(ev->window)))
712 for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
717 detachstack(Client *c)
721 for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
724 if (c == c->mon->sel) {
725 for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
736 if (!(m = selmon->next))
738 } else if (selmon == mons)
739 for (m = mons; m->next; m = m->next);
741 for (m = mons; m->next != selmon; m = m->next);
749 int boxs = drw->fonts->h / 9;
750 int boxw = drw->fonts->h / 6 + 2;
751 unsigned int i, occ = 0, urg = 0;
754 /* draw status first so it can be overdrawn by tags later */
755 if (m == selmon) { /* status is only drawn on selected monitor */
756 drw_setscheme(drw, scheme[SchemeNorm]);
757 tw = TEXTW(stext) - lrpad + 2; /* 2px right padding */
758 drw_text(drw, m->ww - tw - 2 * sp, 0, tw, bh, 0, stext, 0);
761 for (c = m->clients; c; c = c->next) {
767 for (i = 0; i < LENGTH(tags); i++) {
769 drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]);
770 drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i);
772 drw_rect(drw, x + boxs, boxs, boxw, boxw,
773 m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
777 w = blw = TEXTW(m->ltsymbol);
778 drw_setscheme(drw, scheme[SchemeNorm]);
779 x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0);
781 if ((w = m->ww - tw - x) > bh) {
783 drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]);
784 drw_text(drw, x, 0, w - 2 * sp, bh, lrpad / 2, m->sel->name, 0);
785 if (m->sel->isfloating)
786 drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
788 drw_setscheme(drw, scheme[SchemeNorm]);
789 drw_rect(drw, x, 0, w - 2 * sp, bh, 1, 1);
792 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
800 for (m = mons; m; m = m->next)
805 enternotify(XEvent *e)
809 XCrossingEvent *ev = &e->xcrossing;
811 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
813 c = wintoclient(ev->window);
814 m = c ? c->mon : wintomon(ev->window);
816 unfocus(selmon->sel, 1);
818 } else if (!c || c == selmon->sel)
827 XExposeEvent *ev = &e->xexpose;
829 if (ev->count == 0 && (m = wintomon(ev->window)))
836 if (!c || !ISVISIBLE(c))
837 for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
838 if (selmon->sel && selmon->sel != c)
839 unfocus(selmon->sel, 0);
841 if (c->mon != selmon)
848 XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel);
851 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
852 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
858 /* there are some broken focus acquiring clients needing extra handling */
862 XFocusChangeEvent *ev = &e->xfocus;
864 if (selmon->sel && ev->window != selmon->sel->win)
865 setfocus(selmon->sel);
869 focusmon(const Arg *arg)
875 if ((m = dirtomon(arg->i)) == selmon)
877 unfocus(selmon->sel, 0);
883 focusstack(const Arg *arg)
885 Client *c = NULL, *i;
890 for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
892 for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
894 for (i = selmon->clients; i != selmon->sel; i = i->next)
898 for (; i; i = i->next)
909 getatomprop(Client *c, Atom prop)
913 unsigned char *p = NULL;
914 Atom da, atom = None;
916 if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
917 &da, &di, &dl, &dl, &p) == Success && p) {
925 getrootptr(int *x, int *y)
931 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
939 unsigned char *p = NULL;
940 unsigned long n, extra;
943 if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
944 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
953 gettextprop(Window w, Atom atom, char *text, unsigned int size)
959 if (!text || size == 0)
962 if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems)
964 if (name.encoding == XA_STRING)
965 strncpy(text, (char *)name.value, size - 1);
967 if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
968 strncpy(text, *list, size - 1);
969 XFreeStringList(list);
972 text[size - 1] = '\0';
978 grabbuttons(Client *c, int focused)
983 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
984 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
986 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
987 BUTTONMASK, GrabModeSync, GrabModeSync, None, None);
988 for (i = 0; i < LENGTH(buttons); i++)
989 if (buttons[i].click == ClkClientWin)
990 for (j = 0; j < LENGTH(modifiers); j++)
991 XGrabButton(dpy, buttons[i].button,
992 buttons[i].mask | modifiers[j],
993 c->win, False, BUTTONMASK,
994 GrabModeAsync, GrabModeSync, None, None);
1001 updatenumlockmask();
1004 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1007 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1008 for (i = 0; i < LENGTH(keys); i++)
1009 if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
1010 for (j = 0; j < LENGTH(modifiers); j++)
1011 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
1012 True, GrabModeAsync, GrabModeAsync);
1017 incnmaster(const Arg *arg)
1020 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
1021 for(i=0; i<LENGTH(tags); ++i)
1022 if(selmon->tagset[selmon->seltags] & 1<<i)
1023 selmon->pertag->nmasters[i+1] = selmon->nmaster;
1025 if(selmon->pertag->curtag == 0)
1027 selmon->pertag->nmasters[0] = selmon->nmaster;
1034 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
1037 if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1038 && unique[n].width == info->width && unique[n].height == info->height)
1042 #endif /* XINERAMA */
1052 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1053 for (i = 0; i < LENGTH(keys); i++)
1054 if (keysym == keys[i].keysym
1055 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1057 keys[i].func(&(keys[i].arg));
1061 killclient(const Arg *arg)
1065 if (!sendevent(selmon->sel, wmatom[WMDelete])) {
1067 XSetErrorHandler(xerrordummy);
1068 XSetCloseDownMode(dpy, DestroyAll);
1069 XKillClient(dpy, selmon->sel->win);
1071 XSetErrorHandler(xerror);
1077 manage(Window w, XWindowAttributes *wa)
1079 Client *c, *t = NULL;
1080 Window trans = None;
1083 c = ecalloc(1, sizeof(Client));
1086 c->x = c->oldx = wa->x;
1087 c->y = c->oldy = wa->y;
1088 c->w = c->oldw = wa->width;
1089 c->h = c->oldh = wa->height;
1090 c->oldbw = wa->border_width;
1093 if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1101 if (c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1102 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1103 if (c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1104 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1105 c->x = MAX(c->x, c->mon->mx);
1106 /* only fix client y-offset, if the client center might cover the bar */
1107 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1108 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1111 wc.border_width = c->bw;
1112 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1113 XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel);
1114 configure(c); /* propagates border_width, if size doesn't change */
1115 updatewindowtype(c);
1118 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1121 c->isfloating = c->oldstate = trans != None || c->isfixed;
1123 XRaiseWindow(dpy, c->win);
1126 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1127 (unsigned char *) &(c->win), 1);
1128 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1129 setclientstate(c, NormalState);
1130 if (c->mon == selmon)
1131 unfocus(selmon->sel, 0);
1134 XMapWindow(dpy, c->win);
1139 mappingnotify(XEvent *e)
1141 XMappingEvent *ev = &e->xmapping;
1143 XRefreshKeyboardMapping(ev);
1144 if (ev->request == MappingKeyboard)
1149 maprequest(XEvent *e)
1151 static XWindowAttributes wa;
1152 XMapRequestEvent *ev = &e->xmaprequest;
1154 if (!XGetWindowAttributes(dpy, ev->window, &wa))
1156 if (wa.override_redirect)
1158 if (!wintoclient(ev->window))
1159 manage(ev->window, &wa);
1168 for (c = m->clients; c; c = c->next)
1171 if (n > 0) /* override layout symbol */
1172 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1173 for (c = nexttiled(m->clients); c; c = nexttiled(c->next))
1174 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
1178 motionnotify(XEvent *e)
1180 static Monitor *mon = NULL;
1182 XMotionEvent *ev = &e->xmotion;
1184 if (ev->window != root)
1186 if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1187 unfocus(selmon->sel, 1);
1195 movemouse(const Arg *arg)
1197 int x, y, ocx, ocy, nx, ny;
1203 if (!(c = selmon->sel))
1208 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1209 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1211 if (!getrootptr(&x, &y))
1214 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1216 case ConfigureRequest:
1219 handler[ev.type](&ev);
1222 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1224 lasttime = ev.xmotion.time;
1226 nx = ocx + (ev.xmotion.x - x);
1227 ny = ocy + (ev.xmotion.y - y);
1228 if (abs(selmon->wx - nx) < snap)
1230 else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1231 nx = selmon->wx + selmon->ww - WIDTH(c);
1232 if (abs(selmon->wy - ny) < snap)
1234 else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1235 ny = selmon->wy + selmon->wh - HEIGHT(c);
1236 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1237 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1238 togglefloating(NULL);
1239 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1240 resize(c, nx, ny, c->w, c->h, 1);
1243 } while (ev.type != ButtonRelease);
1244 XUngrabPointer(dpy, CurrentTime);
1245 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1253 nexttiled(Client *c)
1255 for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1269 propertynotify(XEvent *e)
1273 XPropertyEvent *ev = &e->xproperty;
1275 if ((ev->window == root) && (ev->atom == XA_WM_NAME))
1277 else if (ev->state == PropertyDelete)
1278 return; /* ignore */
1279 else if ((c = wintoclient(ev->window))) {
1282 case XA_WM_TRANSIENT_FOR:
1283 if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1284 (c->isfloating = (wintoclient(trans)) != NULL))
1287 case XA_WM_NORMAL_HINTS:
1295 if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1297 if (c == c->mon->sel)
1300 if (ev->atom == netatom[NetWMWindowType])
1301 updatewindowtype(c);
1306 quit(const Arg *arg)
1312 recttomon(int x, int y, int w, int h)
1314 Monitor *m, *r = selmon;
1317 for (m = mons; m; m = m->next)
1318 if ((a = INTERSECT(x, y, w, h, m)) > area) {
1326 resize(Client *c, int x, int y, int w, int h, int interact)
1328 if (applysizehints(c, &x, &y, &w, &h, interact))
1329 resizeclient(c, x, y, w, h);
1333 resizeclient(Client *c, int x, int y, int w, int h)
1337 c->oldx = c->x; c->x = wc.x = x;
1338 c->oldy = c->y; c->y = wc.y = y;
1339 c->oldw = c->w; c->w = wc.width = w;
1340 c->oldh = c->h; c->h = wc.height = h;
1341 wc.border_width = c->bw;
1342 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1348 resizemouse(const Arg *arg)
1350 int ocx, ocy, nw, nh;
1356 if (!(c = selmon->sel))
1361 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1362 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1364 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1366 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1368 case ConfigureRequest:
1371 handler[ev.type](&ev);
1374 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1376 lasttime = ev.xmotion.time;
1378 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1379 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1380 if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1381 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1383 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1384 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1385 togglefloating(NULL);
1387 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1388 resize(c, c->x, c->y, nw, nh, 1);
1391 } while (ev.type != ButtonRelease);
1392 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1393 XUngrabPointer(dpy, CurrentTime);
1394 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1395 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1412 if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
1413 XRaiseWindow(dpy, m->sel->win);
1414 if (m->lt[m->sellt]->arrange) {
1415 wc.stack_mode = Below;
1416 wc.sibling = m->barwin;
1417 for (c = m->stack; c; c = c->snext)
1418 if (!c->isfloating && ISVISIBLE(c)) {
1419 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1420 wc.sibling = c->win;
1424 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1431 /* main event loop */
1433 while (running && !XNextEvent(dpy, &ev))
1434 if (handler[ev.type])
1435 handler[ev.type](&ev); /* call handler */
1447 if ((home = getenv("HOME")) == NULL)
1448 /* this is almost impossible */
1451 /* if $XDG_DATA_HOME is set and not empty, use $XDG_DATA_HOME/dwm,
1452 * otherwise use ~/.local/share/dwm as autostart script directory
1454 xdgdatahome = getenv("XDG_DATA_HOME");
1455 if (xdgdatahome != NULL && *xdgdatahome != '\0') {
1456 /* space for path segments, separators and nul */
1457 pathpfx = ecalloc(1, strlen(xdgdatahome) + strlen(dwmdir) + 2);
1459 if (sprintf(pathpfx, "%s/%s", xdgdatahome, dwmdir) <= 0) {
1464 /* space for path segments, separators and nul */
1465 pathpfx = ecalloc(1, strlen(home) + strlen(localshare)
1466 + strlen(dwmdir) + 3);
1468 if (sprintf(pathpfx, "%s/%s/%s", home, localshare, dwmdir) < 0) {
1474 /* check if the autostart script directory exists */
1475 if (! (stat(pathpfx, &sb) == 0 && S_ISDIR(sb.st_mode))) {
1476 /* the XDG conformant path does not exist or is no directory
1477 * so we try ~/.dwm instead
1479 char *pathpfx_new = realloc(pathpfx, strlen(home) + strlen(dwmdir) + 3);
1480 if(pathpfx_new == NULL) {
1484 pathpfx = pathpfx_new;
1486 if (sprintf(pathpfx, "%s/.%s", home, dwmdir) <= 0) {
1492 /* try the blocking script first */
1493 path = ecalloc(1, strlen(pathpfx) + strlen(autostartblocksh) + 2);
1494 if (sprintf(path, "%s/%s", pathpfx, autostartblocksh) <= 0) {
1499 if (access(path, X_OK) == 0)
1502 /* now the non-blocking script */
1503 if (sprintf(path, "%s/%s", pathpfx, autostartsh) <= 0) {
1508 if (access(path, X_OK) == 0)
1509 system(strcat(path, " &"));
1518 unsigned int i, num;
1519 Window d1, d2, *wins = NULL;
1520 XWindowAttributes wa;
1522 if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1523 for (i = 0; i < num; i++) {
1524 if (!XGetWindowAttributes(dpy, wins[i], &wa)
1525 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1527 if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1528 manage(wins[i], &wa);
1530 for (i = 0; i < num; i++) { /* now the transients */
1531 if (!XGetWindowAttributes(dpy, wins[i], &wa))
1533 if (XGetTransientForHint(dpy, wins[i], &d1)
1534 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1535 manage(wins[i], &wa);
1543 sendmon(Client *c, Monitor *m)
1551 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1559 setclientstate(Client *c, long state)
1561 long data[] = { state, None };
1563 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1564 PropModeReplace, (unsigned char *)data, 2);
1568 sendevent(Client *c, Atom proto)
1575 if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1576 while (!exists && n--)
1577 exists = protocols[n] == proto;
1581 ev.type = ClientMessage;
1582 ev.xclient.window = c->win;
1583 ev.xclient.message_type = wmatom[WMProtocols];
1584 ev.xclient.format = 32;
1585 ev.xclient.data.l[0] = proto;
1586 ev.xclient.data.l[1] = CurrentTime;
1587 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1595 if (!c->neverfocus) {
1596 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1597 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1598 XA_WINDOW, 32, PropModeReplace,
1599 (unsigned char *) &(c->win), 1);
1601 sendevent(c, wmatom[WMTakeFocus]);
1605 setfullscreen(Client *c, int fullscreen)
1607 if (fullscreen && !c->isfullscreen) {
1608 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1609 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1610 c->isfullscreen = 1;
1611 } else if (!fullscreen && c->isfullscreen){
1612 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1613 PropModeReplace, (unsigned char*)0, 0);
1614 c->isfullscreen = 0;
1619 setgaps(const Arg *arg)
1621 if ((arg->i == 0) || (selmon->gappx + arg->i < 0))
1624 selmon->gappx += arg->i;
1629 setlayout(const Arg *arg)
1632 if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1635 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1636 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1638 for(i=0; i<LENGTH(tags); ++i)
1639 if(selmon->tagset[selmon->seltags] & 1<<i)
1641 selmon->pertag->ltidxs[i+1][selmon->sellt] = selmon->lt[selmon->sellt];
1642 selmon->pertag->sellts[i+1] = selmon->sellt;
1645 if(selmon->pertag->curtag == 0)
1647 selmon->pertag->ltidxs[0][selmon->sellt] = selmon->lt[selmon->sellt];
1648 selmon->pertag->sellts[0] = selmon->sellt;
1657 /* arg > 1.0 will set mfact absolutely */
1659 setmfact(const Arg *arg)
1664 if (!arg || !selmon->lt[selmon->sellt]->arrange)
1666 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1669 if (f < 0.05 || f > 0.95)
1672 for(i=0; i<LENGTH(tags); ++i)
1673 if(selmon->tagset[selmon->seltags] & 1<<i)
1674 selmon->pertag->mfacts[i+1] = f;
1676 if(selmon->pertag->curtag == 0)
1678 selmon->pertag->mfacts[0] = f;
1687 XSetWindowAttributes wa;
1690 /* clean up any zombies immediately */
1694 screen = DefaultScreen(dpy);
1695 sw = DisplayWidth(dpy, screen);
1696 sh = DisplayHeight(dpy, screen);
1697 root = RootWindow(dpy, screen);
1698 drw = drw_create(dpy, screen, root, sw, sh);
1699 if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
1700 die("no fonts could be loaded.");
1701 lrpad = drw->fonts->h;
1702 bh = drw->fonts->h + 2;
1705 vp = (topbar == 1) ? vertpad : - vertpad;
1708 utf8string = XInternAtom(dpy, "UTF8_STRING", False);
1709 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1710 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1711 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1712 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1713 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1714 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1715 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1716 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1717 netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
1718 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1719 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1720 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1721 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1723 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1724 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1725 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1726 /* init appearance */
1727 scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
1728 for (i = 0; i < LENGTH(colors); i++)
1729 scheme[i] = drw_scm_create(drw, colors[i], 3);
1733 updatebarpos(selmon);
1734 /* supporting window for NetWMCheck */
1735 wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
1736 XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
1737 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1738 XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8,
1739 PropModeReplace, (unsigned char *) "dwm", 3);
1740 XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32,
1741 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1742 /* EWMH support per view */
1743 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1744 PropModeReplace, (unsigned char *) netatom, NetLast);
1745 XDeleteProperty(dpy, root, netatom[NetClientList]);
1747 wa.cursor = cursor[CurNormal]->cursor;
1748 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
1749 |ButtonPressMask|PointerMotionMask|EnterWindowMask
1750 |LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1751 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1752 XSelectInput(dpy, root, wa.event_mask);
1759 seturgent(Client *c, int urg)
1764 if (!(wmh = XGetWMHints(dpy, c->win)))
1766 wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
1767 XSetWMHints(dpy, c->win, wmh);
1777 /* show clients top down */
1778 XMoveWindow(dpy, c->win, c->x, c->y);
1779 if (!c->mon->lt[c->mon->sellt]->arrange || c->isfloating)
1780 resize(c, c->x, c->y, c->w, c->h, 0);
1783 /* hide clients bottom up */
1785 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1792 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1793 die("can't install SIGCHLD handler:");
1794 while (0 < waitpid(-1, NULL, WNOHANG));
1798 spawn(const Arg *arg)
1800 if (arg->v == dmenucmd)
1801 dmenumon[0] = '0' + selmon->num;
1804 close(ConnectionNumber(dpy));
1806 execvp(((char **)arg->v)[0], (char **)arg->v);
1807 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1816 if (selmon->sel && arg->ui & TAGMASK) {
1817 selmon->sel->tags = arg->ui & TAGMASK;
1824 tagmon(const Arg *arg)
1826 if (!selmon->sel || !mons->next)
1828 sendmon(selmon->sel, dirtomon(arg->i));
1834 unsigned int i, n, h, mw, my, ty;
1837 for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1842 mw = m->nmaster ? m->ww * m->mfact : 0;
1844 mw = m->ww - m->gappx;
1845 for (i = 0, my = ty = m->gappx, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1846 if (i < m->nmaster) {
1847 h = (m->wh - my) / (MIN(n, m->nmaster) - i) - m->gappx;
1848 resize(c, m->wx + m->gappx, m->wy + my, mw - (2*c->bw) - m->gappx, h - (2*c->bw), 0);
1849 if (my + HEIGHT(c) + m->gappx < m->wh)
1850 my += HEIGHT(c) + m->gappx;
1852 h = (m->wh - ty) / (n - i) - m->gappx;
1853 resize(c, m->wx + mw + m->gappx, m->wy + ty, m->ww - mw - (2*c->bw) - 2*m->gappx, h - (2*c->bw), 0);
1854 if (ty + HEIGHT(c) + m->gappx < m->wh)
1855 ty += HEIGHT(c) + m->gappx;
1860 togglebar(const Arg *arg)
1863 selmon->showbar = !selmon->showbar;
1864 for(i=0; i<LENGTH(tags); ++i)
1865 if(selmon->tagset[selmon->seltags] & 1<<i)
1866 selmon->pertag->showbars[i+1] = selmon->showbar;
1868 if(selmon->pertag->curtag == 0)
1870 selmon->pertag->showbars[0] = selmon->showbar;
1872 updatebarpos(selmon);
1873 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx + sp, selmon->by + vp, selmon->ww - 2 * sp, bh);
1878 togglefloating(const Arg *arg)
1882 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1883 if (selmon->sel->isfloating)
1884 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1885 selmon->sel->w, selmon->sel->h, 0);
1890 toggletag(const Arg *arg)
1892 unsigned int newtags;
1896 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1898 selmon->sel->tags = newtags;
1905 toggleview(const Arg *arg)
1907 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1911 selmon->tagset[selmon->seltags] = newtagset;
1913 if (newtagset == ~0) {
1914 selmon->pertag->prevtag = selmon->pertag->curtag;
1915 selmon->pertag->curtag = 0;
1918 /* test if the user did not select the same tag */
1919 if (!(newtagset & 1 << (selmon->pertag->curtag - 1))) {
1920 selmon->pertag->prevtag = selmon->pertag->curtag;
1921 for (i = 0; !(newtagset & 1 << i); i++) ;
1922 selmon->pertag->curtag = i + 1;
1925 /* apply settings for this view */
1926 selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag];
1927 selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag];
1928 selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
1929 selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
1930 selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1];
1932 if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag])
1941 unfocus(Client *c, int setfocus)
1946 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
1948 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1949 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1954 unmanage(Client *c, int destroyed)
1956 Monitor *m = c->mon;
1962 wc.border_width = c->oldbw;
1963 XGrabServer(dpy); /* avoid race conditions */
1964 XSetErrorHandler(xerrordummy);
1965 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1966 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1967 setclientstate(c, WithdrawnState);
1969 XSetErrorHandler(xerror);
1979 unmapnotify(XEvent *e)
1982 XUnmapEvent *ev = &e->xunmap;
1984 if ((c = wintoclient(ev->window))) {
1986 setclientstate(c, WithdrawnState);
1996 XSetWindowAttributes wa = {
1997 .override_redirect = True,
1998 .background_pixmap = ParentRelative,
1999 .event_mask = ButtonPressMask|ExposureMask
2001 XClassHint ch = {"dwm", "dwm"};
2002 for (m = mons; m; m = m->next) {
2005 m->barwin = XCreateWindow(dpy, root, m->wx + sp, m->by + vp, m->ww - 2 * sp, bh, 0, DefaultDepth(dpy, screen),
2006 CopyFromParent, DefaultVisual(dpy, screen),
2007 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
2008 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
2009 XMapRaised(dpy, m->barwin);
2010 XSetClassHint(dpy, m->barwin, &ch);
2015 updatebarpos(Monitor *m)
2020 m->wh = m->wh - vertpad - bh;
2021 m->by = m->topbar ? m->wy : m->wy + m->wh + vertpad;
2022 m->wy = m->topbar ? m->wy + bh + vp : m->wy;
2033 XDeleteProperty(dpy, root, netatom[NetClientList]);
2034 for (m = mons; m; m = m->next)
2035 for (c = m->clients; c; c = c->next)
2036 XChangeProperty(dpy, root, netatom[NetClientList],
2037 XA_WINDOW, 32, PropModeAppend,
2038 (unsigned char *) &(c->win), 1);
2047 if (XineramaIsActive(dpy)) {
2051 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
2052 XineramaScreenInfo *unique = NULL;
2054 for (n = 0, m = mons; m; m = m->next, n++);
2055 /* only consider unique geometries as separate screens */
2056 unique = ecalloc(nn, sizeof(XineramaScreenInfo));
2057 for (i = 0, j = 0; i < nn; i++)
2058 if (isuniquegeom(unique, j, &info[i]))
2059 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
2062 if (n <= nn) { /* new monitors available */
2063 for (i = 0; i < (nn - n); i++) {
2064 for (m = mons; m && m->next; m = m->next);
2066 m->next = createmon();
2070 for (i = 0, m = mons; i < nn && m; m = m->next, i++)
2072 || unique[i].x_org != m->mx || unique[i].y_org != m->my
2073 || unique[i].width != m->mw || unique[i].height != m->mh)
2077 m->mx = m->wx = unique[i].x_org;
2078 m->my = m->wy = unique[i].y_org;
2079 m->mw = m->ww = unique[i].width;
2080 m->mh = m->wh = unique[i].height;
2083 } else { /* less monitors available nn < n */
2084 for (i = nn; i < n; i++) {
2085 for (m = mons; m && m->next; m = m->next);
2086 while ((c = m->clients)) {
2088 m->clients = c->next;
2101 #endif /* XINERAMA */
2102 { /* default monitor setup */
2105 if (mons->mw != sw || mons->mh != sh) {
2107 mons->mw = mons->ww = sw;
2108 mons->mh = mons->wh = sh;
2114 selmon = wintomon(root);
2120 updatenumlockmask(void)
2123 XModifierKeymap *modmap;
2126 modmap = XGetModifierMapping(dpy);
2127 for (i = 0; i < 8; i++)
2128 for (j = 0; j < modmap->max_keypermod; j++)
2129 if (modmap->modifiermap[i * modmap->max_keypermod + j]
2130 == XKeysymToKeycode(dpy, XK_Num_Lock))
2131 numlockmask = (1 << i);
2132 XFreeModifiermap(modmap);
2136 updatesizehints(Client *c)
2141 if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
2142 /* size is uninitialized, ensure that size.flags aren't used */
2144 if (size.flags & PBaseSize) {
2145 c->basew = size.base_width;
2146 c->baseh = size.base_height;
2147 } else if (size.flags & PMinSize) {
2148 c->basew = size.min_width;
2149 c->baseh = size.min_height;
2151 c->basew = c->baseh = 0;
2152 if (size.flags & PResizeInc) {
2153 c->incw = size.width_inc;
2154 c->inch = size.height_inc;
2156 c->incw = c->inch = 0;
2157 if (size.flags & PMaxSize) {
2158 c->maxw = size.max_width;
2159 c->maxh = size.max_height;
2161 c->maxw = c->maxh = 0;
2162 if (size.flags & PMinSize) {
2163 c->minw = size.min_width;
2164 c->minh = size.min_height;
2165 } else if (size.flags & PBaseSize) {
2166 c->minw = size.base_width;
2167 c->minh = size.base_height;
2169 c->minw = c->minh = 0;
2170 if (size.flags & PAspect) {
2171 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2172 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2174 c->maxa = c->mina = 0.0;
2175 c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh);
2181 if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2182 strcpy(stext, "dwm-"VERSION);
2187 updatetitle(Client *c)
2189 if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2190 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2191 if (c->name[0] == '\0') /* hack to mark broken clients */
2192 strcpy(c->name, broken);
2196 updatewindowtype(Client *c)
2198 Atom state = getatomprop(c, netatom[NetWMState]);
2199 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2201 if (state == netatom[NetWMFullscreen])
2202 setfullscreen(c, 1);
2203 if (wtype == netatom[NetWMWindowTypeDialog])
2208 updatewmhints(Client *c)
2212 if ((wmh = XGetWMHints(dpy, c->win))) {
2213 if (c == selmon->sel && wmh->flags & XUrgencyHint) {
2214 wmh->flags &= ~XUrgencyHint;
2215 XSetWMHints(dpy, c->win, wmh);
2217 c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
2218 if (wmh->flags & InputHint)
2219 c->neverfocus = !wmh->input;
2227 view(const Arg *arg)
2230 unsigned int tmptag;
2232 if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2234 selmon->seltags ^= 1; /* toggle sel tagset */
2235 if (arg->ui & TAGMASK) {
2236 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2237 selmon->pertag->prevtag = selmon->pertag->curtag;
2240 selmon->pertag->curtag = 0;
2242 for (i = 0; !(arg->ui & 1 << i); i++) ;
2243 selmon->pertag->curtag = i + 1;
2246 tmptag = selmon->pertag->prevtag;
2247 selmon->pertag->prevtag = selmon->pertag->curtag;
2248 selmon->pertag->curtag = tmptag;
2251 selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag];
2252 selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag];
2253 selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
2254 selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
2255 selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1];
2257 if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag])
2265 wintoclient(Window w)
2270 for (m = mons; m; m = m->next)
2271 for (c = m->clients; c; c = c->next)
2284 if (w == root && getrootptr(&x, &y))
2285 return recttomon(x, y, 1, 1);
2286 for (m = mons; m; m = m->next)
2289 if ((c = wintoclient(w)))
2294 /* There's no way to check accesses to destroyed windows, thus those cases are
2295 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2296 * default error handler, which may call exit. */
2298 xerror(Display *dpy, XErrorEvent *ee)
2300 if (ee->error_code == BadWindow
2301 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2302 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2303 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2304 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2305 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2306 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2307 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2308 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2310 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2311 ee->request_code, ee->error_code);
2312 return xerrorxlib(dpy, ee); /* may call exit */
2316 xerrordummy(Display *dpy, XErrorEvent *ee)
2321 /* Startup Error handler to check if another window manager
2322 * is already running. */
2324 xerrorstart(Display *dpy, XErrorEvent *ee)
2326 die("dwm: another window manager is already running");
2331 zoom(const Arg *arg)
2333 Client *c = selmon->sel;
2335 if (!selmon->lt[selmon->sellt]->arrange
2336 || (selmon->sel && selmon->sel->isfloating))
2338 if (c == nexttiled(selmon->clients))
2339 if (!c || !(c = nexttiled(c->next)))
2345 resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst)
2359 snprintf(fullname, sizeof(fullname), "%s.%s", "dwm", name);
2360 fullname[sizeof(fullname) - 1] = '\0';
2362 XrmGetResource(db, fullname, "*", &type, &ret);
2363 if (!(ret.addr == NULL || strncmp("String", type, 64)))
2367 strcpy(sdst, ret.addr);
2370 *idst = strtoul(ret.addr, NULL, 10);
2373 *fdst = strtof(ret.addr, NULL);
2380 load_xresources(void)
2387 display = XOpenDisplay(NULL);
2388 resm = XResourceManagerString(display);
2392 db = XrmGetStringDatabase(resm);
2393 for (p = resources; p < resources + LENGTH(resources); p++)
2394 resource_load(db, p->name, p->type, p->dst);
2395 XCloseDisplay(display);
2399 main(int argc, char *argv[])
2401 if (argc == 2 && !strcmp("-v", argv[1]))
2404 die("usage: dwm [-v]");
2405 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2406 fputs("warning: no locale support\n", stderr);
2407 if (!(dpy = XOpenDisplay(NULL)))
2408 die("dwm: cannot open display");
2414 if (pledge("stdio rpath proc exec", NULL) == -1)
2416 #endif /* __OpenBSD__ */
2422 return EXIT_SUCCESS;