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>
45 #include <X11/Xlib-xcb.h>
48 #include <sys/sysctl.h>
50 #endif /* __OpenBSD */
56 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
57 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
58 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
59 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
60 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
61 #define LENGTH(X) (sizeof X / sizeof X[0])
62 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
63 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
64 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
65 #define TAGMASK ((1 << LENGTH(tags)) - 1)
66 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
69 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
70 enum { SchemeNorm, SchemeSel }; /* color schemes */
71 enum { NetSupported, NetWMName, NetWMState, NetWMCheck,
72 NetWMFullscreen, NetActiveWindow, NetWMWindowType,
73 NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */
74 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
75 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
76 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
89 void (*func)(const Arg *arg);
93 typedef struct Monitor Monitor;
94 typedef struct Client Client;
99 int oldx, oldy, oldw, oldh;
100 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
103 int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen, isterminal, noswallow;
115 void (*func)(const Arg *);
121 void (*arrange)(Monitor *);
124 typedef struct Pertag Pertag;
130 int by; /* bar geometry */
131 int mx, my, mw, mh; /* screen size */
132 int wx, wy, ww, wh; /* window area */
133 int gappx; /* gaps between windows */
134 unsigned int seltags;
136 unsigned int tagset[2];
150 const char *instance;
159 /* Xresources preferences */
168 enum resource_type type;
172 /* function declarations */
173 static void applyrules(Client *c);
174 static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact);
175 static void arrange(Monitor *m);
176 static void arrangemon(Monitor *m);
177 static void attach(Client *c);
178 static void attachstack(Client *c);
179 static void buttonpress(XEvent *e);
180 static void checkotherwm(void);
181 static void cleanup(void);
182 static void cleanupmon(Monitor *mon);
183 static void clientmessage(XEvent *e);
184 static void configure(Client *c);
185 static void configurenotify(XEvent *e);
186 static void configurerequest(XEvent *e);
187 static Monitor *createmon(void);
188 static void destroynotify(XEvent *e);
189 static void detach(Client *c);
190 static void detachstack(Client *c);
191 static Monitor *dirtomon(int dir);
192 static void drawbar(Monitor *m);
193 static void drawbars(void);
194 static void enternotify(XEvent *e);
195 static void expose(XEvent *e);
196 static void focus(Client *c);
197 static void focusin(XEvent *e);
198 static void focusmon(const Arg *arg);
199 static void focusstack(const Arg *arg);
200 static Atom getatomprop(Client *c, Atom prop);
201 static int getrootptr(int *x, int *y);
202 static long getstate(Window w);
203 static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
204 static void grabbuttons(Client *c, int focused);
205 static void grabkeys(void);
206 static void incnmaster(const Arg *arg);
207 static void keypress(XEvent *e);
208 static void killclient(const Arg *arg);
209 static void manage(Window w, XWindowAttributes *wa);
210 static void mappingnotify(XEvent *e);
211 static void maprequest(XEvent *e);
212 static void monocle(Monitor *m);
213 static void motionnotify(XEvent *e);
214 static void movemouse(const Arg *arg);
215 static Client *nexttiled(Client *c);
216 static void pop(Client *);
217 static void propertynotify(XEvent *e);
218 static void quit(const Arg *arg);
219 static Monitor *recttomon(int x, int y, int w, int h);
220 static void resize(Client *c, int x, int y, int w, int h, int interact);
221 static void resizeclient(Client *c, int x, int y, int w, int h);
222 static void resizemouse(const Arg *arg);
223 static void restack(Monitor *m);
224 static void run(void);
225 static void runautostart(void);
226 static void scan(void);
227 static int sendevent(Client *c, Atom proto);
228 static void sendmon(Client *c, Monitor *m);
229 static void setclientstate(Client *c, long state);
230 static void setfocus(Client *c);
231 static void setfullscreen(Client *c, int fullscreen);
232 static void setgaps(const Arg *arg);
233 static void setlayout(const Arg *arg);
234 static void setmfact(const Arg *arg);
235 static void setup(void);
236 static void seturgent(Client *c, int urg);
237 static void showhide(Client *c);
238 static void sigchld(int unused);
239 static void spawn(const Arg *arg);
240 static void tag(const Arg *arg);
241 static void tagmon(const Arg *arg);
242 static void tile(Monitor *);
243 static void togglebar(const Arg *arg);
244 static void togglefloating(const Arg *arg);
245 static void toggletag(const Arg *arg);
246 static void toggleview(const Arg *arg);
247 static void unfocus(Client *c, int setfocus);
248 static void unmanage(Client *c, int destroyed);
249 static void unmapnotify(XEvent *e);
250 static void updatebarpos(Monitor *m);
251 static void updatebars(void);
252 static void updateclientlist(void);
253 static int updategeom(void);
254 static void updatenumlockmask(void);
255 static void updatesizehints(Client *c);
256 static void updatestatus(void);
257 static void updatetitle(Client *c);
258 static void updatewindowtype(Client *c);
259 static void updatewmhints(Client *c);
260 static void view(const Arg *arg);
261 static Client *wintoclient(Window w);
262 static Monitor *wintomon(Window w);
263 static int xerror(Display *dpy, XErrorEvent *ee);
264 static int xerrordummy(Display *dpy, XErrorEvent *ee);
265 static int xerrorstart(Display *dpy, XErrorEvent *ee);
266 static void zoom(const Arg *arg);
267 static pid_t getparentprocess(pid_t p);
268 static int isdescprocess(pid_t p, pid_t c);
269 static Client *swallowingclient(Window w);
270 static Client *termforwin(const Client *c);
271 static pid_t winpid(Window w);
272 static void load_xresources(void);
273 static void resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst);
276 static const char autostartblocksh[] = "autostart_blocking.sh";
277 static const char autostartsh[] = "autostart.sh";
278 static const char broken[] = "broken";
279 static const char dwmdir[] = "dwm";
280 static const char localshare[] = ".local/share";
281 static char stext[256];
283 static int sw, sh; /* X display screen geometry width, height */
284 static int bh, blw = 0; /* bar geometry */
285 static int lrpad; /* sum of left and right padding for text */
286 static int vp; /* vertical padding for bar */
287 static int sp; /* side padding for bar */
288 static int (*xerrorxlib)(Display *, XErrorEvent *);
289 static unsigned int numlockmask = 0;
290 static void (*handler[LASTEvent]) (XEvent *) = {
291 [ButtonPress] = buttonpress,
292 [ClientMessage] = clientmessage,
293 [ConfigureRequest] = configurerequest,
294 [ConfigureNotify] = configurenotify,
295 [DestroyNotify] = destroynotify,
296 [EnterNotify] = enternotify,
299 [KeyPress] = keypress,
300 [MappingNotify] = mappingnotify,
301 [MapRequest] = maprequest,
302 [MotionNotify] = motionnotify,
303 [PropertyNotify] = propertynotify,
304 [UnmapNotify] = unmapnotify
306 static Atom wmatom[WMLast], netatom[NetLast];
307 static int running = 1;
308 static Cur *cursor[CurLast];
312 static Monitor *mons, *selmon;
313 static Window root, wmcheckwin;
315 static xcb_connection_t *xcon;
317 /* configuration, allows nested code to access above variables */
321 unsigned int curtag, prevtag; /* current and previous tag */
322 int nmasters[LENGTH(tags) + 1]; /* number of windows in master area */
323 float mfacts[LENGTH(tags) + 1]; /* mfacts per tag */
324 unsigned int sellts[LENGTH(tags) + 1]; /* selected layouts */
325 const Layout *ltidxs[LENGTH(tags) + 1][2]; /* matrix of tags and layouts indexes */
326 int showbars[LENGTH(tags) + 1]; /* display bar for the current tag */
329 /* compile-time check if all tags fit into an unsigned int bit array. */
330 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
332 /* function implementations */
334 applyrules(Client *c)
336 const char *class, *instance;
340 XClassHint ch = { NULL, NULL };
345 XGetClassHint(dpy, c->win, &ch);
346 class = ch.res_class ? ch.res_class : broken;
347 instance = ch.res_name ? ch.res_name : broken;
349 for (i = 0; i < LENGTH(rules); i++) {
351 if ((!r->title || strstr(c->name, r->title))
352 && (!r->class || strstr(class, r->class))
353 && (!r->instance || strstr(instance, r->instance)))
355 c->isterminal = r->isterminal;
356 c->noswallow = r->noswallow;
357 c->isfloating = r->isfloating;
359 for (m = mons; m && m->num != r->monitor; m = m->next);
368 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
372 applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact)
377 /* set minimum possible */
385 if (*x + *w + 2 * c->bw < 0)
387 if (*y + *h + 2 * c->bw < 0)
390 if (*x >= m->wx + m->ww)
391 *x = m->wx + m->ww - WIDTH(c);
392 if (*y >= m->wy + m->wh)
393 *y = m->wy + m->wh - HEIGHT(c);
394 if (*x + *w + 2 * c->bw <= m->wx)
396 if (*y + *h + 2 * c->bw <= m->wy)
403 if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
404 /* see last two sentences in ICCCM 4.1.2.3 */
405 baseismin = c->basew == c->minw && c->baseh == c->minh;
406 if (!baseismin) { /* temporarily remove base dimensions */
410 /* adjust for aspect limits */
411 if (c->mina > 0 && c->maxa > 0) {
412 if (c->maxa < (float)*w / *h)
413 *w = *h * c->maxa + 0.5;
414 else if (c->mina < (float)*h / *w)
415 *h = *w * c->mina + 0.5;
417 if (baseismin) { /* increment calculation requires this */
421 /* adjust for increment value */
426 /* restore base dimensions */
427 *w = MAX(*w + c->basew, c->minw);
428 *h = MAX(*h + c->baseh, c->minh);
430 *w = MIN(*w, c->maxw);
432 *h = MIN(*h, c->maxh);
434 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
442 else for (m = mons; m; m = m->next)
447 } else for (m = mons; m; m = m->next)
452 arrangemon(Monitor *m)
454 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
455 if (m->lt[m->sellt]->arrange)
456 m->lt[m->sellt]->arrange(m);
462 c->next = c->mon->clients;
467 attachstack(Client *c)
469 c->snext = c->mon->stack;
474 swallow(Client *p, Client *c)
477 if (c->noswallow || c->isterminal)
479 if (c->noswallow && !swallowfloating && c->isfloating)
485 setclientstate(c, WithdrawnState);
486 XUnmapWindow(dpy, p->win);
495 XMoveResizeWindow(dpy, p->win, p->x, p->y, p->w, p->h);
504 c->win = c->swallowing->win;
507 c->swallowing = NULL;
509 /* unfullscreen the client */
513 XMapWindow(dpy, c->win);
514 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
515 setclientstate(c, NormalState);
521 buttonpress(XEvent *e)
523 unsigned int i, x, click;
527 XButtonPressedEvent *ev = &e->xbutton;
530 /* focus monitor if necessary */
531 if ((m = wintomon(ev->window)) && m != selmon) {
532 unfocus(selmon->sel, 1);
536 if (ev->window == selmon->barwin) {
540 while (ev->x >= x && ++i < LENGTH(tags));
541 if (i < LENGTH(tags)) {
544 } else if (ev->x < x + blw)
546 else if (ev->x > selmon->ww - TEXTW(stext))
547 click = ClkStatusText;
550 } else if ((c = wintoclient(ev->window))) {
553 XAllowEvents(dpy, ReplayPointer, CurrentTime);
554 click = ClkClientWin;
556 for (i = 0; i < LENGTH(buttons); i++)
557 if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
558 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
559 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
565 xerrorxlib = XSetErrorHandler(xerrorstart);
566 /* this causes an error if some other window manager is running */
567 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
569 XSetErrorHandler(xerror);
577 Layout foo = { "", NULL };
582 selmon->lt[selmon->sellt] = &foo;
583 for (m = mons; m; m = m->next)
585 unmanage(m->stack, 0);
586 XUngrabKey(dpy, AnyKey, AnyModifier, root);
589 for (i = 0; i < CurLast; i++)
590 drw_cur_free(drw, cursor[i]);
591 for (i = 0; i < LENGTH(colors); i++)
593 XDestroyWindow(dpy, wmcheckwin);
596 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
597 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
601 cleanupmon(Monitor *mon)
608 for (m = mons; m && m->next != mon; m = m->next);
611 XUnmapWindow(dpy, mon->barwin);
612 XDestroyWindow(dpy, mon->barwin);
617 clientmessage(XEvent *e)
619 XClientMessageEvent *cme = &e->xclient;
620 Client *c = wintoclient(cme->window);
624 if (cme->message_type == netatom[NetWMState]) {
625 if (cme->data.l[1] == netatom[NetWMFullscreen]
626 || cme->data.l[2] == netatom[NetWMFullscreen])
627 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
628 || cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */));
629 } else if (cme->message_type == netatom[NetActiveWindow]) {
630 if (c != selmon->sel && !c->isurgent)
640 ce.type = ConfigureNotify;
648 ce.border_width = c->bw;
650 ce.override_redirect = False;
651 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
655 configurenotify(XEvent *e)
658 XConfigureEvent *ev = &e->xconfigure;
661 /* TODO: updategeom handling sucks, needs to be simplified */
662 if (ev->window == root) {
663 dirty = (sw != ev->width || sh != ev->height);
666 if (updategeom() || dirty) {
667 drw_resize(drw, sw, bh);
669 for (m = mons; m; m = m->next) {
670 XMoveResizeWindow(dpy, m->barwin, m->wx + sp, m->by + vp, m->ww - 2 * sp, bh);
679 configurerequest(XEvent *e)
683 XConfigureRequestEvent *ev = &e->xconfigurerequest;
686 if ((c = wintoclient(ev->window))) {
687 if (ev->value_mask & CWBorderWidth)
688 c->bw = ev->border_width;
689 else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
691 if (ev->value_mask & CWX) {
693 c->x = m->mx + ev->x;
695 if (ev->value_mask & CWY) {
697 c->y = m->my + ev->y;
699 if (ev->value_mask & CWWidth) {
703 if (ev->value_mask & CWHeight) {
707 if ((c->x + c->w) > m->mx + m->mw && c->isfloating)
708 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
709 if ((c->y + c->h) > m->my + m->mh && c->isfloating)
710 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
711 if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
714 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
720 wc.width = ev->width;
721 wc.height = ev->height;
722 wc.border_width = ev->border_width;
723 wc.sibling = ev->above;
724 wc.stack_mode = ev->detail;
725 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
736 m = ecalloc(1, sizeof(Monitor));
737 m->tagset[0] = m->tagset[1] = 1;
739 m->nmaster = nmaster;
740 m->showbar = showbar;
743 m->lt[0] = &layouts[0];
744 m->lt[1] = &layouts[1 % LENGTH(layouts)];
745 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
746 m->pertag = ecalloc(1, sizeof(Pertag));
747 m->pertag->curtag = m->pertag->prevtag = 1;
749 for (i = 0; i <= LENGTH(tags); i++) {
750 m->pertag->nmasters[i] = m->nmaster;
751 m->pertag->mfacts[i] = m->mfact;
753 m->pertag->ltidxs[i][0] = m->lt[0];
754 m->pertag->ltidxs[i][1] = m->lt[1];
755 m->pertag->sellts[i] = m->sellt;
757 m->pertag->showbars[i] = m->showbar;
764 destroynotify(XEvent *e)
767 XDestroyWindowEvent *ev = &e->xdestroywindow;
769 if ((c = wintoclient(ev->window)))
772 else if ((c = swallowingclient(ev->window)))
773 unmanage(c->swallowing, 1);
781 for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
786 detachstack(Client *c)
790 for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
793 if (c == c->mon->sel) {
794 for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
805 if (!(m = selmon->next))
807 } else if (selmon == mons)
808 for (m = mons; m->next; m = m->next);
810 for (m = mons; m->next != selmon; m = m->next);
818 int boxs = drw->fonts->h / 9;
819 int boxw = drw->fonts->h / 6 + 2;
820 unsigned int i, occ = 0, urg = 0;
823 /* draw status first so it can be overdrawn by tags later */
824 if (m == selmon) { /* status is only drawn on selected monitor */
825 drw_setscheme(drw, scheme[SchemeNorm]);
826 tw = TEXTW(stext) - lrpad + 2; /* 2px right padding */
827 drw_text(drw, m->ww - tw - 2 * sp, 0, tw, bh, 0, stext, 0);
830 for (c = m->clients; c; c = c->next) {
836 for (i = 0; i < LENGTH(tags); i++) {
838 drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]);
839 drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i);
841 drw_rect(drw, x + boxs, boxs, boxw, boxw,
842 m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
846 w = blw = TEXTW(m->ltsymbol);
847 drw_setscheme(drw, scheme[SchemeNorm]);
848 x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0);
850 if ((w = m->ww - tw - x) > bh) {
852 drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]);
853 drw_text(drw, x, 0, w - 2 * sp, bh, lrpad / 2, m->sel->name, 0);
854 if (m->sel->isfloating)
855 drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
857 drw_setscheme(drw, scheme[SchemeNorm]);
858 drw_rect(drw, x, 0, w - 2 * sp, bh, 1, 1);
861 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
869 for (m = mons; m; m = m->next)
874 enternotify(XEvent *e)
878 XCrossingEvent *ev = &e->xcrossing;
880 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
882 c = wintoclient(ev->window);
883 m = c ? c->mon : wintomon(ev->window);
885 unfocus(selmon->sel, 1);
887 } else if (!c || c == selmon->sel)
896 XExposeEvent *ev = &e->xexpose;
898 if (ev->count == 0 && (m = wintomon(ev->window)))
905 if (!c || !ISVISIBLE(c))
906 for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
907 if (selmon->sel && selmon->sel != c)
908 unfocus(selmon->sel, 0);
910 if (c->mon != selmon)
917 XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel);
920 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
921 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
927 /* there are some broken focus acquiring clients needing extra handling */
931 XFocusChangeEvent *ev = &e->xfocus;
933 if (selmon->sel && ev->window != selmon->sel->win)
934 setfocus(selmon->sel);
938 focusmon(const Arg *arg)
944 if ((m = dirtomon(arg->i)) == selmon)
946 unfocus(selmon->sel, 0);
952 focusstack(const Arg *arg)
954 Client *c = NULL, *i;
959 for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
961 for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
963 for (i = selmon->clients; i != selmon->sel; i = i->next)
967 for (; i; i = i->next)
978 getatomprop(Client *c, Atom prop)
982 unsigned char *p = NULL;
983 Atom da, atom = None;
985 if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
986 &da, &di, &dl, &dl, &p) == Success && p) {
994 getrootptr(int *x, int *y)
1000 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
1008 unsigned char *p = NULL;
1009 unsigned long n, extra;
1012 if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
1013 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
1022 gettextprop(Window w, Atom atom, char *text, unsigned int size)
1028 if (!text || size == 0)
1031 if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems)
1033 if (name.encoding == XA_STRING)
1034 strncpy(text, (char *)name.value, size - 1);
1036 if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
1037 strncpy(text, *list, size - 1);
1038 XFreeStringList(list);
1041 text[size - 1] = '\0';
1047 grabbuttons(Client *c, int focused)
1049 updatenumlockmask();
1052 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1053 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1055 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
1056 BUTTONMASK, GrabModeSync, GrabModeSync, None, None);
1057 for (i = 0; i < LENGTH(buttons); i++)
1058 if (buttons[i].click == ClkClientWin)
1059 for (j = 0; j < LENGTH(modifiers); j++)
1060 XGrabButton(dpy, buttons[i].button,
1061 buttons[i].mask | modifiers[j],
1062 c->win, False, BUTTONMASK,
1063 GrabModeAsync, GrabModeSync, None, None);
1070 updatenumlockmask();
1073 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1076 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1077 for (i = 0; i < LENGTH(keys); i++)
1078 if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
1079 for (j = 0; j < LENGTH(modifiers); j++)
1080 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
1081 True, GrabModeAsync, GrabModeAsync);
1086 incnmaster(const Arg *arg)
1089 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
1090 for(i=0; i<LENGTH(tags); ++i)
1091 if(selmon->tagset[selmon->seltags] & 1<<i)
1092 selmon->pertag->nmasters[i+1] = selmon->nmaster;
1094 if(selmon->pertag->curtag == 0)
1096 selmon->pertag->nmasters[0] = selmon->nmaster;
1103 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
1106 if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1107 && unique[n].width == info->width && unique[n].height == info->height)
1111 #endif /* XINERAMA */
1121 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1122 for (i = 0; i < LENGTH(keys); i++)
1123 if (keysym == keys[i].keysym
1124 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1126 keys[i].func(&(keys[i].arg));
1130 killclient(const Arg *arg)
1134 if (!sendevent(selmon->sel, wmatom[WMDelete])) {
1136 XSetErrorHandler(xerrordummy);
1137 XSetCloseDownMode(dpy, DestroyAll);
1138 XKillClient(dpy, selmon->sel->win);
1140 XSetErrorHandler(xerror);
1146 manage(Window w, XWindowAttributes *wa)
1148 Client *c, *t = NULL, *term = NULL;
1149 Window trans = None;
1152 c = ecalloc(1, sizeof(Client));
1156 c->x = c->oldx = wa->x;
1157 c->y = c->oldy = wa->y;
1158 c->w = c->oldw = wa->width;
1159 c->h = c->oldh = wa->height;
1160 c->oldbw = wa->border_width;
1163 if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1169 term = termforwin(c);
1172 if (c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1173 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1174 if (c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1175 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1176 c->x = MAX(c->x, c->mon->mx);
1177 /* only fix client y-offset, if the client center might cover the bar */
1178 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1179 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1182 wc.border_width = c->bw;
1183 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1184 XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel);
1185 configure(c); /* propagates border_width, if size doesn't change */
1186 updatewindowtype(c);
1189 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1192 c->isfloating = c->oldstate = trans != None || c->isfixed;
1194 XRaiseWindow(dpy, c->win);
1197 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1198 (unsigned char *) &(c->win), 1);
1199 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1200 setclientstate(c, NormalState);
1201 if (c->mon == selmon)
1202 unfocus(selmon->sel, 0);
1205 XMapWindow(dpy, c->win);
1212 mappingnotify(XEvent *e)
1214 XMappingEvent *ev = &e->xmapping;
1216 XRefreshKeyboardMapping(ev);
1217 if (ev->request == MappingKeyboard)
1222 maprequest(XEvent *e)
1224 static XWindowAttributes wa;
1225 XMapRequestEvent *ev = &e->xmaprequest;
1227 if (!XGetWindowAttributes(dpy, ev->window, &wa))
1229 if (wa.override_redirect)
1231 if (!wintoclient(ev->window))
1232 manage(ev->window, &wa);
1241 for (c = m->clients; c; c = c->next)
1244 if (n > 0) /* override layout symbol */
1245 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1246 for (c = nexttiled(m->clients); c; c = nexttiled(c->next))
1247 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
1251 motionnotify(XEvent *e)
1253 static Monitor *mon = NULL;
1255 XMotionEvent *ev = &e->xmotion;
1257 if (ev->window != root)
1259 if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1260 unfocus(selmon->sel, 1);
1268 movemouse(const Arg *arg)
1270 int x, y, ocx, ocy, nx, ny;
1276 if (!(c = selmon->sel))
1281 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1282 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1284 if (!getrootptr(&x, &y))
1287 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1289 case ConfigureRequest:
1292 handler[ev.type](&ev);
1295 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1297 lasttime = ev.xmotion.time;
1299 nx = ocx + (ev.xmotion.x - x);
1300 ny = ocy + (ev.xmotion.y - y);
1301 if (abs(selmon->wx - nx) < snap)
1303 else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1304 nx = selmon->wx + selmon->ww - WIDTH(c);
1305 if (abs(selmon->wy - ny) < snap)
1307 else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1308 ny = selmon->wy + selmon->wh - HEIGHT(c);
1309 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1310 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1311 togglefloating(NULL);
1312 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1313 resize(c, nx, ny, c->w, c->h, 1);
1316 } while (ev.type != ButtonRelease);
1317 XUngrabPointer(dpy, CurrentTime);
1318 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1326 nexttiled(Client *c)
1328 for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1342 propertynotify(XEvent *e)
1346 XPropertyEvent *ev = &e->xproperty;
1348 if ((ev->window == root) && (ev->atom == XA_WM_NAME))
1350 else if (ev->state == PropertyDelete)
1351 return; /* ignore */
1352 else if ((c = wintoclient(ev->window))) {
1355 case XA_WM_TRANSIENT_FOR:
1356 if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1357 (c->isfloating = (wintoclient(trans)) != NULL))
1360 case XA_WM_NORMAL_HINTS:
1368 if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1370 if (c == c->mon->sel)
1373 if (ev->atom == netatom[NetWMWindowType])
1374 updatewindowtype(c);
1379 quit(const Arg *arg)
1385 recttomon(int x, int y, int w, int h)
1387 Monitor *m, *r = selmon;
1390 for (m = mons; m; m = m->next)
1391 if ((a = INTERSECT(x, y, w, h, m)) > area) {
1399 resize(Client *c, int x, int y, int w, int h, int interact)
1401 if (applysizehints(c, &x, &y, &w, &h, interact))
1402 resizeclient(c, x, y, w, h);
1406 resizeclient(Client *c, int x, int y, int w, int h)
1410 c->oldx = c->x; c->x = wc.x = x;
1411 c->oldy = c->y; c->y = wc.y = y;
1412 c->oldw = c->w; c->w = wc.width = w;
1413 c->oldh = c->h; c->h = wc.height = h;
1414 wc.border_width = c->bw;
1415 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1421 resizemouse(const Arg *arg)
1423 int ocx, ocy, nw, nh;
1429 if (!(c = selmon->sel))
1434 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1435 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1437 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1439 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1441 case ConfigureRequest:
1444 handler[ev.type](&ev);
1447 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1449 lasttime = ev.xmotion.time;
1451 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1452 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1453 if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1454 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1456 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1457 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1458 togglefloating(NULL);
1460 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1461 resize(c, c->x, c->y, nw, nh, 1);
1464 } while (ev.type != ButtonRelease);
1465 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1466 XUngrabPointer(dpy, CurrentTime);
1467 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1468 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1485 if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
1486 XRaiseWindow(dpy, m->sel->win);
1487 if (m->lt[m->sellt]->arrange) {
1488 wc.stack_mode = Below;
1489 wc.sibling = m->barwin;
1490 for (c = m->stack; c; c = c->snext)
1491 if (!c->isfloating && ISVISIBLE(c)) {
1492 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1493 wc.sibling = c->win;
1497 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1504 /* main event loop */
1506 while (running && !XNextEvent(dpy, &ev))
1507 if (handler[ev.type])
1508 handler[ev.type](&ev); /* call handler */
1520 if ((home = getenv("HOME")) == NULL)
1521 /* this is almost impossible */
1524 /* if $XDG_DATA_HOME is set and not empty, use $XDG_DATA_HOME/dwm,
1525 * otherwise use ~/.local/share/dwm as autostart script directory
1527 xdgdatahome = getenv("XDG_DATA_HOME");
1528 if (xdgdatahome != NULL && *xdgdatahome != '\0') {
1529 /* space for path segments, separators and nul */
1530 pathpfx = ecalloc(1, strlen(xdgdatahome) + strlen(dwmdir) + 2);
1532 if (sprintf(pathpfx, "%s/%s", xdgdatahome, dwmdir) <= 0) {
1537 /* space for path segments, separators and nul */
1538 pathpfx = ecalloc(1, strlen(home) + strlen(localshare)
1539 + strlen(dwmdir) + 3);
1541 if (sprintf(pathpfx, "%s/%s/%s", home, localshare, dwmdir) < 0) {
1547 /* check if the autostart script directory exists */
1548 if (! (stat(pathpfx, &sb) == 0 && S_ISDIR(sb.st_mode))) {
1549 /* the XDG conformant path does not exist or is no directory
1550 * so we try ~/.dwm instead
1552 char *pathpfx_new = realloc(pathpfx, strlen(home) + strlen(dwmdir) + 3);
1553 if(pathpfx_new == NULL) {
1557 pathpfx = pathpfx_new;
1559 if (sprintf(pathpfx, "%s/.%s", home, dwmdir) <= 0) {
1565 /* try the blocking script first */
1566 path = ecalloc(1, strlen(pathpfx) + strlen(autostartblocksh) + 2);
1567 if (sprintf(path, "%s/%s", pathpfx, autostartblocksh) <= 0) {
1572 if (access(path, X_OK) == 0)
1575 /* now the non-blocking script */
1576 if (sprintf(path, "%s/%s", pathpfx, autostartsh) <= 0) {
1581 if (access(path, X_OK) == 0)
1582 system(strcat(path, " &"));
1591 unsigned int i, num;
1592 Window d1, d2, *wins = NULL;
1593 XWindowAttributes wa;
1595 if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1596 for (i = 0; i < num; i++) {
1597 if (!XGetWindowAttributes(dpy, wins[i], &wa)
1598 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1600 if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1601 manage(wins[i], &wa);
1603 for (i = 0; i < num; i++) { /* now the transients */
1604 if (!XGetWindowAttributes(dpy, wins[i], &wa))
1606 if (XGetTransientForHint(dpy, wins[i], &d1)
1607 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1608 manage(wins[i], &wa);
1616 sendmon(Client *c, Monitor *m)
1624 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1632 setclientstate(Client *c, long state)
1634 long data[] = { state, None };
1636 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1637 PropModeReplace, (unsigned char *)data, 2);
1641 sendevent(Client *c, Atom proto)
1648 if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1649 while (!exists && n--)
1650 exists = protocols[n] == proto;
1654 ev.type = ClientMessage;
1655 ev.xclient.window = c->win;
1656 ev.xclient.message_type = wmatom[WMProtocols];
1657 ev.xclient.format = 32;
1658 ev.xclient.data.l[0] = proto;
1659 ev.xclient.data.l[1] = CurrentTime;
1660 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1668 if (!c->neverfocus) {
1669 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1670 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1671 XA_WINDOW, 32, PropModeReplace,
1672 (unsigned char *) &(c->win), 1);
1674 sendevent(c, wmatom[WMTakeFocus]);
1678 setfullscreen(Client *c, int fullscreen)
1680 if (fullscreen && !c->isfullscreen) {
1681 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1682 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1683 c->isfullscreen = 1;
1684 } else if (!fullscreen && c->isfullscreen){
1685 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1686 PropModeReplace, (unsigned char*)0, 0);
1687 c->isfullscreen = 0;
1692 setgaps(const Arg *arg)
1694 if ((arg->i == 0) || (selmon->gappx + arg->i < 0))
1697 selmon->gappx += arg->i;
1702 setlayout(const Arg *arg)
1705 if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1708 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1709 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1711 for(i=0; i<LENGTH(tags); ++i)
1712 if(selmon->tagset[selmon->seltags] & 1<<i)
1714 selmon->pertag->ltidxs[i+1][selmon->sellt] = selmon->lt[selmon->sellt];
1715 selmon->pertag->sellts[i+1] = selmon->sellt;
1718 if(selmon->pertag->curtag == 0)
1720 selmon->pertag->ltidxs[0][selmon->sellt] = selmon->lt[selmon->sellt];
1721 selmon->pertag->sellts[0] = selmon->sellt;
1730 /* arg > 1.0 will set mfact absolutely */
1732 setmfact(const Arg *arg)
1737 if (!arg || !selmon->lt[selmon->sellt]->arrange)
1739 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1742 if (f < 0.05 || f > 0.95)
1745 for(i=0; i<LENGTH(tags); ++i)
1746 if(selmon->tagset[selmon->seltags] & 1<<i)
1747 selmon->pertag->mfacts[i+1] = f;
1749 if(selmon->pertag->curtag == 0)
1751 selmon->pertag->mfacts[0] = f;
1760 XSetWindowAttributes wa;
1763 /* clean up any zombies immediately */
1767 screen = DefaultScreen(dpy);
1768 sw = DisplayWidth(dpy, screen);
1769 sh = DisplayHeight(dpy, screen);
1770 root = RootWindow(dpy, screen);
1771 drw = drw_create(dpy, screen, root, sw, sh);
1772 if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
1773 die("no fonts could be loaded.");
1774 lrpad = drw->fonts->h;
1775 bh = drw->fonts->h + 2;
1778 vp = (topbar == 1) ? vertpad : - vertpad;
1781 utf8string = XInternAtom(dpy, "UTF8_STRING", False);
1782 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1783 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1784 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1785 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1786 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1787 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1788 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1789 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1790 netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
1791 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1792 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1793 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1794 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1796 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1797 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1798 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1799 /* init appearance */
1800 scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
1801 for (i = 0; i < LENGTH(colors); i++)
1802 scheme[i] = drw_scm_create(drw, colors[i], 3);
1806 updatebarpos(selmon);
1807 /* supporting window for NetWMCheck */
1808 wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
1809 XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
1810 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1811 XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8,
1812 PropModeReplace, (unsigned char *) "dwm", 3);
1813 XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32,
1814 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1815 /* EWMH support per view */
1816 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1817 PropModeReplace, (unsigned char *) netatom, NetLast);
1818 XDeleteProperty(dpy, root, netatom[NetClientList]);
1820 wa.cursor = cursor[CurNormal]->cursor;
1821 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
1822 |ButtonPressMask|PointerMotionMask|EnterWindowMask
1823 |LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1824 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1825 XSelectInput(dpy, root, wa.event_mask);
1832 seturgent(Client *c, int urg)
1837 if (!(wmh = XGetWMHints(dpy, c->win)))
1839 wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
1840 XSetWMHints(dpy, c->win, wmh);
1850 /* show clients top down */
1851 XMoveWindow(dpy, c->win, c->x, c->y);
1852 if (!c->mon->lt[c->mon->sellt]->arrange || c->isfloating)
1853 resize(c, c->x, c->y, c->w, c->h, 0);
1856 /* hide clients bottom up */
1858 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1865 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1866 die("can't install SIGCHLD handler:");
1867 while (0 < waitpid(-1, NULL, WNOHANG));
1871 spawn(const Arg *arg)
1873 if (arg->v == dmenucmd)
1874 dmenumon[0] = '0' + selmon->num;
1877 close(ConnectionNumber(dpy));
1879 execvp(((char **)arg->v)[0], (char **)arg->v);
1880 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1889 if (selmon->sel && arg->ui & TAGMASK) {
1890 selmon->sel->tags = arg->ui & TAGMASK;
1897 tagmon(const Arg *arg)
1899 if (!selmon->sel || !mons->next)
1901 sendmon(selmon->sel, dirtomon(arg->i));
1907 unsigned int i, n, h, mw, my, ty;
1910 for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1915 mw = m->nmaster ? m->ww * m->mfact : 0;
1917 mw = m->ww - m->gappx;
1918 for (i = 0, my = ty = m->gappx, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1919 if (i < m->nmaster) {
1920 h = (m->wh - my) / (MIN(n, m->nmaster) - i) - m->gappx;
1921 resize(c, m->wx + m->gappx, m->wy + my, mw - (2*c->bw) - m->gappx, h - (2*c->bw), 0);
1922 if (my + HEIGHT(c) + m->gappx < m->wh)
1923 my += HEIGHT(c) + m->gappx;
1925 h = (m->wh - ty) / (n - i) - m->gappx;
1926 resize(c, m->wx + mw + m->gappx, m->wy + ty, m->ww - mw - (2*c->bw) - 2*m->gappx, h - (2*c->bw), 0);
1927 if (ty + HEIGHT(c) + m->gappx < m->wh)
1928 ty += HEIGHT(c) + m->gappx;
1933 togglebar(const Arg *arg)
1936 selmon->showbar = !selmon->showbar;
1937 for(i=0; i<LENGTH(tags); ++i)
1938 if(selmon->tagset[selmon->seltags] & 1<<i)
1939 selmon->pertag->showbars[i+1] = selmon->showbar;
1941 if(selmon->pertag->curtag == 0)
1943 selmon->pertag->showbars[0] = selmon->showbar;
1945 updatebarpos(selmon);
1946 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx + sp, selmon->by + vp, selmon->ww - 2 * sp, bh);
1951 togglefloating(const Arg *arg)
1955 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1956 if (selmon->sel->isfloating)
1957 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1958 selmon->sel->w, selmon->sel->h, 0);
1963 toggletag(const Arg *arg)
1965 unsigned int newtags;
1969 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1971 selmon->sel->tags = newtags;
1978 toggleview(const Arg *arg)
1980 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1984 selmon->tagset[selmon->seltags] = newtagset;
1986 if (newtagset == ~0) {
1987 selmon->pertag->prevtag = selmon->pertag->curtag;
1988 selmon->pertag->curtag = 0;
1991 /* test if the user did not select the same tag */
1992 if (!(newtagset & 1 << (selmon->pertag->curtag - 1))) {
1993 selmon->pertag->prevtag = selmon->pertag->curtag;
1994 for (i = 0; !(newtagset & 1 << i); i++) ;
1995 selmon->pertag->curtag = i + 1;
1998 /* apply settings for this view */
1999 selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag];
2000 selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag];
2001 selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
2002 selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
2003 selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1];
2005 if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag])
2014 unfocus(Client *c, int setfocus)
2019 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
2021 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
2022 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
2027 unmanage(Client *c, int destroyed)
2029 Monitor *m = c->mon;
2032 if (c->swallowing) {
2037 Client *s = swallowingclient(c->win);
2039 free(s->swallowing);
2040 s->swallowing = NULL;
2049 wc.border_width = c->oldbw;
2050 XGrabServer(dpy); /* avoid race conditions */
2051 XSetErrorHandler(xerrordummy);
2052 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
2053 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
2054 setclientstate(c, WithdrawnState);
2056 XSetErrorHandler(xerror);
2069 unmapnotify(XEvent *e)
2072 XUnmapEvent *ev = &e->xunmap;
2074 if ((c = wintoclient(ev->window))) {
2076 setclientstate(c, WithdrawnState);
2086 XSetWindowAttributes wa = {
2087 .override_redirect = True,
2088 .background_pixmap = ParentRelative,
2089 .event_mask = ButtonPressMask|ExposureMask
2091 XClassHint ch = {"dwm", "dwm"};
2092 for (m = mons; m; m = m->next) {
2095 m->barwin = XCreateWindow(dpy, root, m->wx + sp, m->by + vp, m->ww - 2 * sp, bh, 0, DefaultDepth(dpy, screen),
2096 CopyFromParent, DefaultVisual(dpy, screen),
2097 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
2098 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
2099 XMapRaised(dpy, m->barwin);
2100 XSetClassHint(dpy, m->barwin, &ch);
2105 updatebarpos(Monitor *m)
2110 m->wh = m->wh - vertpad - bh;
2111 m->by = m->topbar ? m->wy : m->wy + m->wh + vertpad;
2112 m->wy = m->topbar ? m->wy + bh + vp : m->wy;
2123 XDeleteProperty(dpy, root, netatom[NetClientList]);
2124 for (m = mons; m; m = m->next)
2125 for (c = m->clients; c; c = c->next)
2126 XChangeProperty(dpy, root, netatom[NetClientList],
2127 XA_WINDOW, 32, PropModeAppend,
2128 (unsigned char *) &(c->win), 1);
2137 if (XineramaIsActive(dpy)) {
2141 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
2142 XineramaScreenInfo *unique = NULL;
2144 for (n = 0, m = mons; m; m = m->next, n++);
2145 /* only consider unique geometries as separate screens */
2146 unique = ecalloc(nn, sizeof(XineramaScreenInfo));
2147 for (i = 0, j = 0; i < nn; i++)
2148 if (isuniquegeom(unique, j, &info[i]))
2149 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
2152 if (n <= nn) { /* new monitors available */
2153 for (i = 0; i < (nn - n); i++) {
2154 for (m = mons; m && m->next; m = m->next);
2156 m->next = createmon();
2160 for (i = 0, m = mons; i < nn && m; m = m->next, i++)
2162 || unique[i].x_org != m->mx || unique[i].y_org != m->my
2163 || unique[i].width != m->mw || unique[i].height != m->mh)
2167 m->mx = m->wx = unique[i].x_org;
2168 m->my = m->wy = unique[i].y_org;
2169 m->mw = m->ww = unique[i].width;
2170 m->mh = m->wh = unique[i].height;
2173 } else { /* less monitors available nn < n */
2174 for (i = nn; i < n; i++) {
2175 for (m = mons; m && m->next; m = m->next);
2176 while ((c = m->clients)) {
2178 m->clients = c->next;
2191 #endif /* XINERAMA */
2192 { /* default monitor setup */
2195 if (mons->mw != sw || mons->mh != sh) {
2197 mons->mw = mons->ww = sw;
2198 mons->mh = mons->wh = sh;
2204 selmon = wintomon(root);
2210 updatenumlockmask(void)
2213 XModifierKeymap *modmap;
2216 modmap = XGetModifierMapping(dpy);
2217 for (i = 0; i < 8; i++)
2218 for (j = 0; j < modmap->max_keypermod; j++)
2219 if (modmap->modifiermap[i * modmap->max_keypermod + j]
2220 == XKeysymToKeycode(dpy, XK_Num_Lock))
2221 numlockmask = (1 << i);
2222 XFreeModifiermap(modmap);
2226 updatesizehints(Client *c)
2231 if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
2232 /* size is uninitialized, ensure that size.flags aren't used */
2234 if (size.flags & PBaseSize) {
2235 c->basew = size.base_width;
2236 c->baseh = size.base_height;
2237 } else if (size.flags & PMinSize) {
2238 c->basew = size.min_width;
2239 c->baseh = size.min_height;
2241 c->basew = c->baseh = 0;
2242 if (size.flags & PResizeInc) {
2243 c->incw = size.width_inc;
2244 c->inch = size.height_inc;
2246 c->incw = c->inch = 0;
2247 if (size.flags & PMaxSize) {
2248 c->maxw = size.max_width;
2249 c->maxh = size.max_height;
2251 c->maxw = c->maxh = 0;
2252 if (size.flags & PMinSize) {
2253 c->minw = size.min_width;
2254 c->minh = size.min_height;
2255 } else if (size.flags & PBaseSize) {
2256 c->minw = size.base_width;
2257 c->minh = size.base_height;
2259 c->minw = c->minh = 0;
2260 if (size.flags & PAspect) {
2261 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2262 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2264 c->maxa = c->mina = 0.0;
2265 c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh);
2271 if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2272 strcpy(stext, "dwm-"VERSION);
2277 updatetitle(Client *c)
2279 if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2280 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2281 if (c->name[0] == '\0') /* hack to mark broken clients */
2282 strcpy(c->name, broken);
2286 updatewindowtype(Client *c)
2288 Atom state = getatomprop(c, netatom[NetWMState]);
2289 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2291 if (state == netatom[NetWMFullscreen])
2292 setfullscreen(c, 1);
2293 if (wtype == netatom[NetWMWindowTypeDialog])
2298 updatewmhints(Client *c)
2302 if ((wmh = XGetWMHints(dpy, c->win))) {
2303 if (c == selmon->sel && wmh->flags & XUrgencyHint) {
2304 wmh->flags &= ~XUrgencyHint;
2305 XSetWMHints(dpy, c->win, wmh);
2307 c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
2308 if (wmh->flags & InputHint)
2309 c->neverfocus = !wmh->input;
2317 view(const Arg *arg)
2320 unsigned int tmptag;
2322 if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2324 selmon->seltags ^= 1; /* toggle sel tagset */
2325 if (arg->ui & TAGMASK) {
2326 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2327 selmon->pertag->prevtag = selmon->pertag->curtag;
2330 selmon->pertag->curtag = 0;
2332 for (i = 0; !(arg->ui & 1 << i); i++) ;
2333 selmon->pertag->curtag = i + 1;
2336 tmptag = selmon->pertag->prevtag;
2337 selmon->pertag->prevtag = selmon->pertag->curtag;
2338 selmon->pertag->curtag = tmptag;
2341 selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag];
2342 selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag];
2343 selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
2344 selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
2345 selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1];
2347 if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag])
2361 xcb_res_client_id_spec_t spec = {0};
2363 spec.mask = XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID;
2365 xcb_generic_error_t *e = NULL;
2366 xcb_res_query_client_ids_cookie_t c = xcb_res_query_client_ids(xcon, 1, &spec);
2367 xcb_res_query_client_ids_reply_t *r = xcb_res_query_client_ids_reply(xcon, c, &e);
2372 xcb_res_client_id_value_iterator_t i = xcb_res_query_client_ids_ids_iterator(r);
2373 for (; i.rem; xcb_res_client_id_value_next(&i)) {
2374 spec = i.data->spec;
2375 if (spec.mask & XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID) {
2376 uint32_t *t = xcb_res_client_id_value_value(i.data);
2384 if (result == (pid_t)-1)
2387 #endif /* __linux__ */
2392 unsigned long len, bytes;
2393 unsigned char *prop;
2396 if (XGetWindowProperty(dpy, w, XInternAtom(dpy, "_NET_WM_PID", 1), 0, 1, False, AnyPropertyType, &type, &format, &len, &bytes, &prop) != Success || !prop)
2399 ret = *(pid_t*)prop;
2403 #endif /* __OpenBSD__ */
2408 getparentprocess(pid_t p)
2415 snprintf(buf, sizeof(buf) - 1, "/proc/%u/stat", (unsigned)p);
2417 if (!(f = fopen(buf, "r")))
2420 fscanf(f, "%*u %*s %*c %u", &v);
2422 #endif /* __linux__*/
2427 struct kinfo_proc *kp;
2429 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, NULL);
2433 kp = kvm_getprocs(kd, KERN_PROC_PID, p, sizeof(*kp), &n);
2435 #endif /* __OpenBSD__ */
2441 isdescprocess(pid_t p, pid_t c)
2443 while (p != c && c != 0)
2444 c = getparentprocess(c);
2450 termforwin(const Client *w)
2455 if (!w->pid || w->isterminal)
2458 for (m = mons; m; m = m->next) {
2459 for (c = m->clients; c; c = c->next) {
2460 if (c->isterminal && !c->swallowing && c->pid && isdescprocess(c->pid, w->pid))
2469 swallowingclient(Window w)
2474 for (m = mons; m; m = m->next) {
2475 for (c = m->clients; c; c = c->next) {
2476 if (c->swallowing && c->swallowing->win == w)
2485 wintoclient(Window w)
2490 for (m = mons; m; m = m->next)
2491 for (c = m->clients; c; c = c->next)
2504 if (w == root && getrootptr(&x, &y))
2505 return recttomon(x, y, 1, 1);
2506 for (m = mons; m; m = m->next)
2509 if ((c = wintoclient(w)))
2514 /* There's no way to check accesses to destroyed windows, thus those cases are
2515 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2516 * default error handler, which may call exit. */
2518 xerror(Display *dpy, XErrorEvent *ee)
2520 if (ee->error_code == BadWindow
2521 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2522 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2523 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2524 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2525 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2526 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2527 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2528 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2530 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2531 ee->request_code, ee->error_code);
2532 return xerrorxlib(dpy, ee); /* may call exit */
2536 xerrordummy(Display *dpy, XErrorEvent *ee)
2541 /* Startup Error handler to check if another window manager
2542 * is already running. */
2544 xerrorstart(Display *dpy, XErrorEvent *ee)
2546 die("dwm: another window manager is already running");
2551 zoom(const Arg *arg)
2553 Client *c = selmon->sel;
2555 if (!selmon->lt[selmon->sellt]->arrange
2556 || (selmon->sel && selmon->sel->isfloating))
2558 if (c == nexttiled(selmon->clients))
2559 if (!c || !(c = nexttiled(c->next)))
2565 resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst)
2579 snprintf(fullname, sizeof(fullname), "%s.%s", "dwm", name);
2580 fullname[sizeof(fullname) - 1] = '\0';
2582 XrmGetResource(db, fullname, "*", &type, &ret);
2583 if (!(ret.addr == NULL || strncmp("String", type, 64)))
2587 strcpy(sdst, ret.addr);
2590 *idst = strtoul(ret.addr, NULL, 10);
2593 *fdst = strtof(ret.addr, NULL);
2600 load_xresources(void)
2607 display = XOpenDisplay(NULL);
2608 resm = XResourceManagerString(display);
2612 db = XrmGetStringDatabase(resm);
2613 for (p = resources; p < resources + LENGTH(resources); p++)
2614 resource_load(db, p->name, p->type, p->dst);
2615 XCloseDisplay(display);
2619 main(int argc, char *argv[])
2621 if (argc == 2 && !strcmp("-v", argv[1]))
2624 die("usage: dwm [-v]");
2625 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2626 fputs("warning: no locale support\n", stderr);
2627 if (!(dpy = XOpenDisplay(NULL)))
2628 die("dwm: cannot open display");
2629 if (!(xcon = XGetXCBConnection(dpy)))
2630 die("dwm: cannot get xcb connection\n");
2636 if (pledge("stdio rpath proc exec ps", NULL) == -1)
2638 #endif /* __OpenBSD__ */
2644 return EXIT_SUCCESS;