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 *);
129 int by; /* bar geometry */
130 int mx, my, mw, mh; /* screen size */
131 int wx, wy, ww, wh; /* window area */
132 int gappx; /* gaps between windows */
133 unsigned int seltags;
135 unsigned int tagset[2];
148 const char *instance;
157 /* Xresources preferences */
166 enum resource_type type;
170 /* function declarations */
171 static void applyrules(Client *c);
172 static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact);
173 static void arrange(Monitor *m);
174 static void arrangemon(Monitor *m);
175 static void attach(Client *c);
176 static void attachstack(Client *c);
177 static void buttonpress(XEvent *e);
178 static void checkotherwm(void);
179 static void cleanup(void);
180 static void cleanupmon(Monitor *mon);
181 static void clientmessage(XEvent *e);
182 static void configure(Client *c);
183 static void configurenotify(XEvent *e);
184 static void configurerequest(XEvent *e);
185 static Monitor *createmon(void);
186 static void destroynotify(XEvent *e);
187 static void detach(Client *c);
188 static void detachstack(Client *c);
189 static Monitor *dirtomon(int dir);
190 static void drawbar(Monitor *m);
191 static void drawbars(void);
192 static void enternotify(XEvent *e);
193 static void expose(XEvent *e);
194 static void focus(Client *c);
195 static void focusin(XEvent *e);
196 static void focusmon(const Arg *arg);
197 static void focusstack(const Arg *arg);
198 static Atom getatomprop(Client *c, Atom prop);
199 static int getrootptr(int *x, int *y);
200 static long getstate(Window w);
201 static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
202 static void grabbuttons(Client *c, int focused);
203 static void grabkeys(void);
204 static void incnmaster(const Arg *arg);
205 static void keypress(XEvent *e);
206 static void killclient(const Arg *arg);
207 static void manage(Window w, XWindowAttributes *wa);
208 static void mappingnotify(XEvent *e);
209 static void maprequest(XEvent *e);
210 static void monocle(Monitor *m);
211 static void motionnotify(XEvent *e);
212 static void movemouse(const Arg *arg);
213 static Client *nexttiled(Client *c);
214 static void pop(Client *);
215 static void propertynotify(XEvent *e);
216 static void quit(const Arg *arg);
217 static Monitor *recttomon(int x, int y, int w, int h);
218 static void resize(Client *c, int x, int y, int w, int h, int interact);
219 static void resizeclient(Client *c, int x, int y, int w, int h);
220 static void resizemouse(const Arg *arg);
221 static void restack(Monitor *m);
222 static void run(void);
223 static void runautostart(void);
224 static void scan(void);
225 static int sendevent(Client *c, Atom proto);
226 static void sendmon(Client *c, Monitor *m);
227 static void setclientstate(Client *c, long state);
228 static void setfocus(Client *c);
229 static void setfullscreen(Client *c, int fullscreen);
230 static void setgaps(const Arg *arg);
231 static void setlayout(const Arg *arg);
232 static void setmfact(const Arg *arg);
233 static void setup(void);
234 static void seturgent(Client *c, int urg);
235 static void showhide(Client *c);
236 static void sigchld(int unused);
237 static void spawn(const Arg *arg);
238 static void tag(const Arg *arg);
239 static void tagmon(const Arg *arg);
240 static void tile(Monitor *);
241 static void togglebar(const Arg *arg);
242 static void togglefloating(const Arg *arg);
243 static void toggletag(const Arg *arg);
244 static void toggleview(const Arg *arg);
245 static void unfocus(Client *c, int setfocus);
246 static void unmanage(Client *c, int destroyed);
247 static void unmapnotify(XEvent *e);
248 static void updatebarpos(Monitor *m);
249 static void updatebars(void);
250 static void updateclientlist(void);
251 static int updategeom(void);
252 static void updatenumlockmask(void);
253 static void updatesizehints(Client *c);
254 static void updatestatus(void);
255 static void updatetitle(Client *c);
256 static void updatewindowtype(Client *c);
257 static void updatewmhints(Client *c);
258 static void view(const Arg *arg);
259 static Client *wintoclient(Window w);
260 static Monitor *wintomon(Window w);
261 static int xerror(Display *dpy, XErrorEvent *ee);
262 static int xerrordummy(Display *dpy, XErrorEvent *ee);
263 static int xerrorstart(Display *dpy, XErrorEvent *ee);
264 static void zoom(const Arg *arg);
265 static pid_t getparentprocess(pid_t p);
266 static int isdescprocess(pid_t p, pid_t c);
267 static Client *swallowingclient(Window w);
268 static Client *termforwin(const Client *c);
269 static pid_t winpid(Window w);
270 static void load_xresources(void);
271 static void resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst);
274 static const char autostartblocksh[] = "autostart_blocking.sh";
275 static const char autostartsh[] = "autostart.sh";
276 static const char broken[] = "broken";
277 static const char dwmdir[] = "dwm";
278 static const char localshare[] = ".local/share";
279 static char stext[256];
281 static int sw, sh; /* X display screen geometry width, height */
282 static int bh, blw = 0; /* bar geometry */
283 static int lrpad; /* sum of left and right padding for text */
284 static int vp; /* vertical padding for bar */
285 static int sp; /* side padding for bar */
286 static int (*xerrorxlib)(Display *, XErrorEvent *);
287 static unsigned int numlockmask = 0;
288 static void (*handler[LASTEvent]) (XEvent *) = {
289 [ButtonPress] = buttonpress,
290 [ClientMessage] = clientmessage,
291 [ConfigureRequest] = configurerequest,
292 [ConfigureNotify] = configurenotify,
293 [DestroyNotify] = destroynotify,
294 [EnterNotify] = enternotify,
297 [KeyPress] = keypress,
298 [MappingNotify] = mappingnotify,
299 [MapRequest] = maprequest,
300 [MotionNotify] = motionnotify,
301 [PropertyNotify] = propertynotify,
302 [UnmapNotify] = unmapnotify
304 static Atom wmatom[WMLast], netatom[NetLast];
305 static int running = 1;
306 static Cur *cursor[CurLast];
310 static Monitor *mons, *selmon;
311 static Window root, wmcheckwin;
313 static xcb_connection_t *xcon;
315 /* configuration, allows nested code to access above variables */
318 /* compile-time check if all tags fit into an unsigned int bit array. */
319 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
321 /* function implementations */
323 applyrules(Client *c)
325 const char *class, *instance;
329 XClassHint ch = { NULL, NULL };
334 XGetClassHint(dpy, c->win, &ch);
335 class = ch.res_class ? ch.res_class : broken;
336 instance = ch.res_name ? ch.res_name : broken;
338 for (i = 0; i < LENGTH(rules); i++) {
340 if ((!r->title || strstr(c->name, r->title))
341 && (!r->class || strstr(class, r->class))
342 && (!r->instance || strstr(instance, r->instance)))
344 c->isterminal = r->isterminal;
345 c->noswallow = r->noswallow;
346 c->isfloating = r->isfloating;
348 for (m = mons; m && m->num != r->monitor; m = m->next);
357 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
361 applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact)
366 /* set minimum possible */
374 if (*x + *w + 2 * c->bw < 0)
376 if (*y + *h + 2 * c->bw < 0)
379 if (*x >= m->wx + m->ww)
380 *x = m->wx + m->ww - WIDTH(c);
381 if (*y >= m->wy + m->wh)
382 *y = m->wy + m->wh - HEIGHT(c);
383 if (*x + *w + 2 * c->bw <= m->wx)
385 if (*y + *h + 2 * c->bw <= m->wy)
392 if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
393 /* see last two sentences in ICCCM 4.1.2.3 */
394 baseismin = c->basew == c->minw && c->baseh == c->minh;
395 if (!baseismin) { /* temporarily remove base dimensions */
399 /* adjust for aspect limits */
400 if (c->mina > 0 && c->maxa > 0) {
401 if (c->maxa < (float)*w / *h)
402 *w = *h * c->maxa + 0.5;
403 else if (c->mina < (float)*h / *w)
404 *h = *w * c->mina + 0.5;
406 if (baseismin) { /* increment calculation requires this */
410 /* adjust for increment value */
415 /* restore base dimensions */
416 *w = MAX(*w + c->basew, c->minw);
417 *h = MAX(*h + c->baseh, c->minh);
419 *w = MIN(*w, c->maxw);
421 *h = MIN(*h, c->maxh);
423 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
431 else for (m = mons; m; m = m->next)
436 } else for (m = mons; m; m = m->next)
441 arrangemon(Monitor *m)
443 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
444 if (m->lt[m->sellt]->arrange)
445 m->lt[m->sellt]->arrange(m);
451 c->next = c->mon->clients;
456 attachstack(Client *c)
458 c->snext = c->mon->stack;
463 swallow(Client *p, Client *c)
466 if (c->noswallow || c->isterminal)
468 if (c->noswallow && !swallowfloating && c->isfloating)
474 setclientstate(c, WithdrawnState);
475 XUnmapWindow(dpy, p->win);
484 XMoveResizeWindow(dpy, p->win, p->x, p->y, p->w, p->h);
493 c->win = c->swallowing->win;
496 c->swallowing = NULL;
498 /* unfullscreen the client */
502 XMapWindow(dpy, c->win);
503 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
504 setclientstate(c, NormalState);
510 buttonpress(XEvent *e)
512 unsigned int i, x, click;
516 XButtonPressedEvent *ev = &e->xbutton;
519 /* focus monitor if necessary */
520 if ((m = wintomon(ev->window)) && m != selmon) {
521 unfocus(selmon->sel, 1);
525 if (ev->window == selmon->barwin) {
529 while (ev->x >= x && ++i < LENGTH(tags));
530 if (i < LENGTH(tags)) {
533 } else if (ev->x < x + blw)
535 else if (ev->x > selmon->ww - TEXTW(stext))
536 click = ClkStatusText;
539 } else if ((c = wintoclient(ev->window))) {
542 XAllowEvents(dpy, ReplayPointer, CurrentTime);
543 click = ClkClientWin;
545 for (i = 0; i < LENGTH(buttons); i++)
546 if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
547 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
548 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
554 xerrorxlib = XSetErrorHandler(xerrorstart);
555 /* this causes an error if some other window manager is running */
556 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
558 XSetErrorHandler(xerror);
566 Layout foo = { "", NULL };
571 selmon->lt[selmon->sellt] = &foo;
572 for (m = mons; m; m = m->next)
574 unmanage(m->stack, 0);
575 XUngrabKey(dpy, AnyKey, AnyModifier, root);
578 for (i = 0; i < CurLast; i++)
579 drw_cur_free(drw, cursor[i]);
580 for (i = 0; i < LENGTH(colors); i++)
582 XDestroyWindow(dpy, wmcheckwin);
585 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
586 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
590 cleanupmon(Monitor *mon)
597 for (m = mons; m && m->next != mon; m = m->next);
600 XUnmapWindow(dpy, mon->barwin);
601 XDestroyWindow(dpy, mon->barwin);
606 clientmessage(XEvent *e)
608 XClientMessageEvent *cme = &e->xclient;
609 Client *c = wintoclient(cme->window);
613 if (cme->message_type == netatom[NetWMState]) {
614 if (cme->data.l[1] == netatom[NetWMFullscreen]
615 || cme->data.l[2] == netatom[NetWMFullscreen])
616 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
617 || cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */));
618 } else if (cme->message_type == netatom[NetActiveWindow]) {
619 if (c != selmon->sel && !c->isurgent)
629 ce.type = ConfigureNotify;
637 ce.border_width = c->bw;
639 ce.override_redirect = False;
640 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
644 configurenotify(XEvent *e)
647 XConfigureEvent *ev = &e->xconfigure;
650 /* TODO: updategeom handling sucks, needs to be simplified */
651 if (ev->window == root) {
652 dirty = (sw != ev->width || sh != ev->height);
655 if (updategeom() || dirty) {
656 drw_resize(drw, sw, bh);
658 for (m = mons; m; m = m->next) {
659 XMoveResizeWindow(dpy, m->barwin, m->wx + sp, m->by + vp, m->ww - 2 * sp, bh);
668 configurerequest(XEvent *e)
672 XConfigureRequestEvent *ev = &e->xconfigurerequest;
675 if ((c = wintoclient(ev->window))) {
676 if (ev->value_mask & CWBorderWidth)
677 c->bw = ev->border_width;
678 else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
680 if (ev->value_mask & CWX) {
682 c->x = m->mx + ev->x;
684 if (ev->value_mask & CWY) {
686 c->y = m->my + ev->y;
688 if (ev->value_mask & CWWidth) {
692 if (ev->value_mask & CWHeight) {
696 if ((c->x + c->w) > m->mx + m->mw && c->isfloating)
697 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
698 if ((c->y + c->h) > m->my + m->mh && c->isfloating)
699 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
700 if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
703 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
709 wc.width = ev->width;
710 wc.height = ev->height;
711 wc.border_width = ev->border_width;
712 wc.sibling = ev->above;
713 wc.stack_mode = ev->detail;
714 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
724 m = ecalloc(1, sizeof(Monitor));
725 m->tagset[0] = m->tagset[1] = 1;
727 m->nmaster = nmaster;
728 m->showbar = showbar;
731 m->lt[0] = &layouts[0];
732 m->lt[1] = &layouts[1 % LENGTH(layouts)];
733 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
738 destroynotify(XEvent *e)
741 XDestroyWindowEvent *ev = &e->xdestroywindow;
743 if ((c = wintoclient(ev->window)))
746 else if ((c = swallowingclient(ev->window)))
747 unmanage(c->swallowing, 1);
755 for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
760 detachstack(Client *c)
764 for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
767 if (c == c->mon->sel) {
768 for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
779 if (!(m = selmon->next))
781 } else if (selmon == mons)
782 for (m = mons; m->next; m = m->next);
784 for (m = mons; m->next != selmon; m = m->next);
792 int boxs = drw->fonts->h / 9;
793 int boxw = drw->fonts->h / 6 + 2;
794 unsigned int i, occ = 0, urg = 0;
797 /* draw status first so it can be overdrawn by tags later */
798 if (m == selmon) { /* status is only drawn on selected monitor */
799 drw_setscheme(drw, scheme[SchemeNorm]);
800 tw = TEXTW(stext) - lrpad + 2; /* 2px right padding */
801 drw_text(drw, m->ww - tw - 2 * sp, 0, tw, bh, 0, stext, 0);
804 for (c = m->clients; c; c = c->next) {
810 for (i = 0; i < LENGTH(tags); i++) {
812 drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]);
813 drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i);
815 drw_rect(drw, x + boxs, boxs, boxw, boxw,
816 m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
820 w = blw = TEXTW(m->ltsymbol);
821 drw_setscheme(drw, scheme[SchemeNorm]);
822 x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0);
824 if ((w = m->ww - tw - x) > bh) {
826 drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]);
827 drw_text(drw, x, 0, w - 2 * sp, bh, lrpad / 2, m->sel->name, 0);
828 if (m->sel->isfloating)
829 drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
831 drw_setscheme(drw, scheme[SchemeNorm]);
832 drw_rect(drw, x, 0, w - 2 * sp, bh, 1, 1);
835 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
843 for (m = mons; m; m = m->next)
848 enternotify(XEvent *e)
852 XCrossingEvent *ev = &e->xcrossing;
854 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
856 c = wintoclient(ev->window);
857 m = c ? c->mon : wintomon(ev->window);
859 unfocus(selmon->sel, 1);
861 } else if (!c || c == selmon->sel)
870 XExposeEvent *ev = &e->xexpose;
872 if (ev->count == 0 && (m = wintomon(ev->window)))
879 if (!c || !ISVISIBLE(c))
880 for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
881 if (selmon->sel && selmon->sel != c)
882 unfocus(selmon->sel, 0);
884 if (c->mon != selmon)
891 XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel);
894 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
895 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
901 /* there are some broken focus acquiring clients needing extra handling */
905 XFocusChangeEvent *ev = &e->xfocus;
907 if (selmon->sel && ev->window != selmon->sel->win)
908 setfocus(selmon->sel);
912 focusmon(const Arg *arg)
918 if ((m = dirtomon(arg->i)) == selmon)
920 unfocus(selmon->sel, 0);
926 focusstack(const Arg *arg)
928 Client *c = NULL, *i;
933 for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
935 for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
937 for (i = selmon->clients; i != selmon->sel; i = i->next)
941 for (; i; i = i->next)
952 getatomprop(Client *c, Atom prop)
956 unsigned char *p = NULL;
957 Atom da, atom = None;
959 if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
960 &da, &di, &dl, &dl, &p) == Success && p) {
968 getrootptr(int *x, int *y)
974 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
982 unsigned char *p = NULL;
983 unsigned long n, extra;
986 if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
987 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
996 gettextprop(Window w, Atom atom, char *text, unsigned int size)
1002 if (!text || size == 0)
1005 if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems)
1007 if (name.encoding == XA_STRING)
1008 strncpy(text, (char *)name.value, size - 1);
1010 if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
1011 strncpy(text, *list, size - 1);
1012 XFreeStringList(list);
1015 text[size - 1] = '\0';
1021 grabbuttons(Client *c, int focused)
1023 updatenumlockmask();
1026 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1027 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1029 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
1030 BUTTONMASK, GrabModeSync, GrabModeSync, None, None);
1031 for (i = 0; i < LENGTH(buttons); i++)
1032 if (buttons[i].click == ClkClientWin)
1033 for (j = 0; j < LENGTH(modifiers); j++)
1034 XGrabButton(dpy, buttons[i].button,
1035 buttons[i].mask | modifiers[j],
1036 c->win, False, BUTTONMASK,
1037 GrabModeAsync, GrabModeSync, None, None);
1044 updatenumlockmask();
1047 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1050 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1051 for (i = 0; i < LENGTH(keys); i++)
1052 if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
1053 for (j = 0; j < LENGTH(modifiers); j++)
1054 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
1055 True, GrabModeAsync, GrabModeAsync);
1060 incnmaster(const Arg *arg)
1062 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
1068 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
1071 if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1072 && unique[n].width == info->width && unique[n].height == info->height)
1076 #endif /* XINERAMA */
1086 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1087 for (i = 0; i < LENGTH(keys); i++)
1088 if (keysym == keys[i].keysym
1089 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1091 keys[i].func(&(keys[i].arg));
1095 killclient(const Arg *arg)
1099 if (!sendevent(selmon->sel, wmatom[WMDelete])) {
1101 XSetErrorHandler(xerrordummy);
1102 XSetCloseDownMode(dpy, DestroyAll);
1103 XKillClient(dpy, selmon->sel->win);
1105 XSetErrorHandler(xerror);
1111 manage(Window w, XWindowAttributes *wa)
1113 Client *c, *t = NULL, *term = NULL;
1114 Window trans = None;
1117 c = ecalloc(1, sizeof(Client));
1121 c->x = c->oldx = wa->x;
1122 c->y = c->oldy = wa->y;
1123 c->w = c->oldw = wa->width;
1124 c->h = c->oldh = wa->height;
1125 c->oldbw = wa->border_width;
1128 if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1134 term = termforwin(c);
1137 if (c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1138 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1139 if (c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1140 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1141 c->x = MAX(c->x, c->mon->mx);
1142 /* only fix client y-offset, if the client center might cover the bar */
1143 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1144 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1147 wc.border_width = c->bw;
1148 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1149 XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel);
1150 configure(c); /* propagates border_width, if size doesn't change */
1151 updatewindowtype(c);
1154 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1157 c->isfloating = c->oldstate = trans != None || c->isfixed;
1159 XRaiseWindow(dpy, c->win);
1162 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1163 (unsigned char *) &(c->win), 1);
1164 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1165 setclientstate(c, NormalState);
1166 if (c->mon == selmon)
1167 unfocus(selmon->sel, 0);
1170 XMapWindow(dpy, c->win);
1177 mappingnotify(XEvent *e)
1179 XMappingEvent *ev = &e->xmapping;
1181 XRefreshKeyboardMapping(ev);
1182 if (ev->request == MappingKeyboard)
1187 maprequest(XEvent *e)
1189 static XWindowAttributes wa;
1190 XMapRequestEvent *ev = &e->xmaprequest;
1192 if (!XGetWindowAttributes(dpy, ev->window, &wa))
1194 if (wa.override_redirect)
1196 if (!wintoclient(ev->window))
1197 manage(ev->window, &wa);
1206 for (c = m->clients; c; c = c->next)
1209 if (n > 0) /* override layout symbol */
1210 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1211 for (c = nexttiled(m->clients); c; c = nexttiled(c->next))
1212 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
1216 motionnotify(XEvent *e)
1218 static Monitor *mon = NULL;
1220 XMotionEvent *ev = &e->xmotion;
1222 if (ev->window != root)
1224 if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1225 unfocus(selmon->sel, 1);
1233 movemouse(const Arg *arg)
1235 int x, y, ocx, ocy, nx, ny;
1241 if (!(c = selmon->sel))
1246 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1247 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1249 if (!getrootptr(&x, &y))
1252 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1254 case ConfigureRequest:
1257 handler[ev.type](&ev);
1260 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1262 lasttime = ev.xmotion.time;
1264 nx = ocx + (ev.xmotion.x - x);
1265 ny = ocy + (ev.xmotion.y - y);
1266 if (abs(selmon->wx - nx) < snap)
1268 else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1269 nx = selmon->wx + selmon->ww - WIDTH(c);
1270 if (abs(selmon->wy - ny) < snap)
1272 else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1273 ny = selmon->wy + selmon->wh - HEIGHT(c);
1274 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1275 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1276 togglefloating(NULL);
1277 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1278 resize(c, nx, ny, c->w, c->h, 1);
1281 } while (ev.type != ButtonRelease);
1282 XUngrabPointer(dpy, CurrentTime);
1283 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1291 nexttiled(Client *c)
1293 for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1307 propertynotify(XEvent *e)
1311 XPropertyEvent *ev = &e->xproperty;
1313 if ((ev->window == root) && (ev->atom == XA_WM_NAME))
1315 else if (ev->state == PropertyDelete)
1316 return; /* ignore */
1317 else if ((c = wintoclient(ev->window))) {
1320 case XA_WM_TRANSIENT_FOR:
1321 if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1322 (c->isfloating = (wintoclient(trans)) != NULL))
1325 case XA_WM_NORMAL_HINTS:
1333 if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1335 if (c == c->mon->sel)
1338 if (ev->atom == netatom[NetWMWindowType])
1339 updatewindowtype(c);
1344 quit(const Arg *arg)
1350 recttomon(int x, int y, int w, int h)
1352 Monitor *m, *r = selmon;
1355 for (m = mons; m; m = m->next)
1356 if ((a = INTERSECT(x, y, w, h, m)) > area) {
1364 resize(Client *c, int x, int y, int w, int h, int interact)
1366 if (applysizehints(c, &x, &y, &w, &h, interact))
1367 resizeclient(c, x, y, w, h);
1371 resizeclient(Client *c, int x, int y, int w, int h)
1375 c->oldx = c->x; c->x = wc.x = x;
1376 c->oldy = c->y; c->y = wc.y = y;
1377 c->oldw = c->w; c->w = wc.width = w;
1378 c->oldh = c->h; c->h = wc.height = h;
1379 wc.border_width = c->bw;
1380 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1386 resizemouse(const Arg *arg)
1388 int ocx, ocy, nw, nh;
1394 if (!(c = selmon->sel))
1399 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1400 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1402 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1404 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1406 case ConfigureRequest:
1409 handler[ev.type](&ev);
1412 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1414 lasttime = ev.xmotion.time;
1416 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1417 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1418 if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1419 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1421 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1422 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1423 togglefloating(NULL);
1425 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1426 resize(c, c->x, c->y, nw, nh, 1);
1429 } while (ev.type != ButtonRelease);
1430 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1431 XUngrabPointer(dpy, CurrentTime);
1432 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1433 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1450 if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
1451 XRaiseWindow(dpy, m->sel->win);
1452 if (m->lt[m->sellt]->arrange) {
1453 wc.stack_mode = Below;
1454 wc.sibling = m->barwin;
1455 for (c = m->stack; c; c = c->snext)
1456 if (!c->isfloating && ISVISIBLE(c)) {
1457 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1458 wc.sibling = c->win;
1462 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1469 /* main event loop */
1471 while (running && !XNextEvent(dpy, &ev))
1472 if (handler[ev.type])
1473 handler[ev.type](&ev); /* call handler */
1485 if ((home = getenv("HOME")) == NULL)
1486 /* this is almost impossible */
1489 /* if $XDG_DATA_HOME is set and not empty, use $XDG_DATA_HOME/dwm,
1490 * otherwise use ~/.local/share/dwm as autostart script directory
1492 xdgdatahome = getenv("XDG_DATA_HOME");
1493 if (xdgdatahome != NULL && *xdgdatahome != '\0') {
1494 /* space for path segments, separators and nul */
1495 pathpfx = ecalloc(1, strlen(xdgdatahome) + strlen(dwmdir) + 2);
1497 if (sprintf(pathpfx, "%s/%s", xdgdatahome, dwmdir) <= 0) {
1502 /* space for path segments, separators and nul */
1503 pathpfx = ecalloc(1, strlen(home) + strlen(localshare)
1504 + strlen(dwmdir) + 3);
1506 if (sprintf(pathpfx, "%s/%s/%s", home, localshare, dwmdir) < 0) {
1512 /* check if the autostart script directory exists */
1513 if (! (stat(pathpfx, &sb) == 0 && S_ISDIR(sb.st_mode))) {
1514 /* the XDG conformant path does not exist or is no directory
1515 * so we try ~/.dwm instead
1517 char *pathpfx_new = realloc(pathpfx, strlen(home) + strlen(dwmdir) + 3);
1518 if(pathpfx_new == NULL) {
1522 pathpfx = pathpfx_new;
1524 if (sprintf(pathpfx, "%s/.%s", home, dwmdir) <= 0) {
1530 /* try the blocking script first */
1531 path = ecalloc(1, strlen(pathpfx) + strlen(autostartblocksh) + 2);
1532 if (sprintf(path, "%s/%s", pathpfx, autostartblocksh) <= 0) {
1537 if (access(path, X_OK) == 0)
1540 /* now the non-blocking script */
1541 if (sprintf(path, "%s/%s", pathpfx, autostartsh) <= 0) {
1546 if (access(path, X_OK) == 0)
1547 system(strcat(path, " &"));
1556 unsigned int i, num;
1557 Window d1, d2, *wins = NULL;
1558 XWindowAttributes wa;
1560 if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1561 for (i = 0; i < num; i++) {
1562 if (!XGetWindowAttributes(dpy, wins[i], &wa)
1563 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1565 if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1566 manage(wins[i], &wa);
1568 for (i = 0; i < num; i++) { /* now the transients */
1569 if (!XGetWindowAttributes(dpy, wins[i], &wa))
1571 if (XGetTransientForHint(dpy, wins[i], &d1)
1572 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1573 manage(wins[i], &wa);
1581 sendmon(Client *c, Monitor *m)
1589 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1597 setclientstate(Client *c, long state)
1599 long data[] = { state, None };
1601 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1602 PropModeReplace, (unsigned char *)data, 2);
1606 sendevent(Client *c, Atom proto)
1613 if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1614 while (!exists && n--)
1615 exists = protocols[n] == proto;
1619 ev.type = ClientMessage;
1620 ev.xclient.window = c->win;
1621 ev.xclient.message_type = wmatom[WMProtocols];
1622 ev.xclient.format = 32;
1623 ev.xclient.data.l[0] = proto;
1624 ev.xclient.data.l[1] = CurrentTime;
1625 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1633 if (!c->neverfocus) {
1634 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1635 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1636 XA_WINDOW, 32, PropModeReplace,
1637 (unsigned char *) &(c->win), 1);
1639 sendevent(c, wmatom[WMTakeFocus]);
1643 setfullscreen(Client *c, int fullscreen)
1645 if (fullscreen && !c->isfullscreen) {
1646 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1647 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1648 c->isfullscreen = 1;
1649 } else if (!fullscreen && c->isfullscreen){
1650 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1651 PropModeReplace, (unsigned char*)0, 0);
1652 c->isfullscreen = 0;
1657 setgaps(const Arg *arg)
1659 if ((arg->i == 0) || (selmon->gappx + arg->i < 0))
1662 selmon->gappx += arg->i;
1667 setlayout(const Arg *arg)
1669 if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1672 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1673 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1680 /* arg > 1.0 will set mfact absolutely */
1682 setmfact(const Arg *arg)
1686 if (!arg || !selmon->lt[selmon->sellt]->arrange)
1688 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1689 if (f < 0.05 || f > 0.95)
1699 XSetWindowAttributes wa;
1702 /* clean up any zombies immediately */
1706 screen = DefaultScreen(dpy);
1707 sw = DisplayWidth(dpy, screen);
1708 sh = DisplayHeight(dpy, screen);
1709 root = RootWindow(dpy, screen);
1710 drw = drw_create(dpy, screen, root, sw, sh);
1711 if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
1712 die("no fonts could be loaded.");
1713 lrpad = drw->fonts->h;
1714 bh = drw->fonts->h + 2;
1717 vp = (topbar == 1) ? vertpad : - vertpad;
1720 utf8string = XInternAtom(dpy, "UTF8_STRING", False);
1721 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1722 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1723 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1724 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1725 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1726 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1727 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1728 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1729 netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
1730 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1731 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1732 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1733 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1735 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1736 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1737 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1738 /* init appearance */
1739 scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
1740 for (i = 0; i < LENGTH(colors); i++)
1741 scheme[i] = drw_scm_create(drw, colors[i], 3);
1745 updatebarpos(selmon);
1746 /* supporting window for NetWMCheck */
1747 wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
1748 XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
1749 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1750 XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8,
1751 PropModeReplace, (unsigned char *) "dwm", 3);
1752 XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32,
1753 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1754 /* EWMH support per view */
1755 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1756 PropModeReplace, (unsigned char *) netatom, NetLast);
1757 XDeleteProperty(dpy, root, netatom[NetClientList]);
1759 wa.cursor = cursor[CurNormal]->cursor;
1760 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
1761 |ButtonPressMask|PointerMotionMask|EnterWindowMask
1762 |LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1763 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1764 XSelectInput(dpy, root, wa.event_mask);
1771 seturgent(Client *c, int urg)
1776 if (!(wmh = XGetWMHints(dpy, c->win)))
1778 wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
1779 XSetWMHints(dpy, c->win, wmh);
1789 /* show clients top down */
1790 XMoveWindow(dpy, c->win, c->x, c->y);
1791 if (!c->mon->lt[c->mon->sellt]->arrange || c->isfloating)
1792 resize(c, c->x, c->y, c->w, c->h, 0);
1795 /* hide clients bottom up */
1797 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1804 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1805 die("can't install SIGCHLD handler:");
1806 while (0 < waitpid(-1, NULL, WNOHANG));
1810 spawn(const Arg *arg)
1812 if (arg->v == dmenucmd)
1813 dmenumon[0] = '0' + selmon->num;
1816 close(ConnectionNumber(dpy));
1818 execvp(((char **)arg->v)[0], (char **)arg->v);
1819 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1828 if (selmon->sel && arg->ui & TAGMASK) {
1829 selmon->sel->tags = arg->ui & TAGMASK;
1836 tagmon(const Arg *arg)
1838 if (!selmon->sel || !mons->next)
1840 sendmon(selmon->sel, dirtomon(arg->i));
1846 unsigned int i, n, h, mw, my, ty;
1849 for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1854 mw = m->nmaster ? m->ww * m->mfact : 0;
1856 mw = m->ww - m->gappx;
1857 for (i = 0, my = ty = m->gappx, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1858 if (i < m->nmaster) {
1859 h = (m->wh - my) / (MIN(n, m->nmaster) - i) - m->gappx;
1860 resize(c, m->wx + m->gappx, m->wy + my, mw - (2*c->bw) - m->gappx, h - (2*c->bw), 0);
1861 if (my + HEIGHT(c) + m->gappx < m->wh)
1862 my += HEIGHT(c) + m->gappx;
1864 h = (m->wh - ty) / (n - i) - m->gappx;
1865 resize(c, m->wx + mw + m->gappx, m->wy + ty, m->ww - mw - (2*c->bw) - 2*m->gappx, h - (2*c->bw), 0);
1866 if (ty + HEIGHT(c) + m->gappx < m->wh)
1867 ty += HEIGHT(c) + m->gappx;
1872 togglebar(const Arg *arg)
1874 selmon->showbar = !selmon->showbar;
1875 updatebarpos(selmon);
1876 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx + sp, selmon->by + vp, selmon->ww - 2 * sp, bh);
1881 togglefloating(const Arg *arg)
1885 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1886 if (selmon->sel->isfloating)
1887 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1888 selmon->sel->w, selmon->sel->h, 0);
1893 toggletag(const Arg *arg)
1895 unsigned int newtags;
1899 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1901 selmon->sel->tags = newtags;
1908 toggleview(const Arg *arg)
1910 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1913 selmon->tagset[selmon->seltags] = newtagset;
1920 unfocus(Client *c, int setfocus)
1925 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
1927 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1928 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1933 unmanage(Client *c, int destroyed)
1935 Monitor *m = c->mon;
1938 if (c->swallowing) {
1943 Client *s = swallowingclient(c->win);
1945 free(s->swallowing);
1946 s->swallowing = NULL;
1955 wc.border_width = c->oldbw;
1956 XGrabServer(dpy); /* avoid race conditions */
1957 XSetErrorHandler(xerrordummy);
1958 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1959 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1960 setclientstate(c, WithdrawnState);
1962 XSetErrorHandler(xerror);
1975 unmapnotify(XEvent *e)
1978 XUnmapEvent *ev = &e->xunmap;
1980 if ((c = wintoclient(ev->window))) {
1982 setclientstate(c, WithdrawnState);
1992 XSetWindowAttributes wa = {
1993 .override_redirect = True,
1994 .background_pixmap = ParentRelative,
1995 .event_mask = ButtonPressMask|ExposureMask
1997 XClassHint ch = {"dwm", "dwm"};
1998 for (m = mons; m; m = m->next) {
2001 m->barwin = XCreateWindow(dpy, root, m->wx + sp, m->by + vp, m->ww - 2 * sp, bh, 0, DefaultDepth(dpy, screen),
2002 CopyFromParent, DefaultVisual(dpy, screen),
2003 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
2004 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
2005 XMapRaised(dpy, m->barwin);
2006 XSetClassHint(dpy, m->barwin, &ch);
2011 updatebarpos(Monitor *m)
2016 m->wh = m->wh - vertpad - bh;
2017 m->by = m->topbar ? m->wy : m->wy + m->wh + vertpad;
2018 m->wy = m->topbar ? m->wy + bh + vp : m->wy;
2029 XDeleteProperty(dpy, root, netatom[NetClientList]);
2030 for (m = mons; m; m = m->next)
2031 for (c = m->clients; c; c = c->next)
2032 XChangeProperty(dpy, root, netatom[NetClientList],
2033 XA_WINDOW, 32, PropModeAppend,
2034 (unsigned char *) &(c->win), 1);
2043 if (XineramaIsActive(dpy)) {
2047 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
2048 XineramaScreenInfo *unique = NULL;
2050 for (n = 0, m = mons; m; m = m->next, n++);
2051 /* only consider unique geometries as separate screens */
2052 unique = ecalloc(nn, sizeof(XineramaScreenInfo));
2053 for (i = 0, j = 0; i < nn; i++)
2054 if (isuniquegeom(unique, j, &info[i]))
2055 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
2058 if (n <= nn) { /* new monitors available */
2059 for (i = 0; i < (nn - n); i++) {
2060 for (m = mons; m && m->next; m = m->next);
2062 m->next = createmon();
2066 for (i = 0, m = mons; i < nn && m; m = m->next, i++)
2068 || unique[i].x_org != m->mx || unique[i].y_org != m->my
2069 || unique[i].width != m->mw || unique[i].height != m->mh)
2073 m->mx = m->wx = unique[i].x_org;
2074 m->my = m->wy = unique[i].y_org;
2075 m->mw = m->ww = unique[i].width;
2076 m->mh = m->wh = unique[i].height;
2079 } else { /* less monitors available nn < n */
2080 for (i = nn; i < n; i++) {
2081 for (m = mons; m && m->next; m = m->next);
2082 while ((c = m->clients)) {
2084 m->clients = c->next;
2097 #endif /* XINERAMA */
2098 { /* default monitor setup */
2101 if (mons->mw != sw || mons->mh != sh) {
2103 mons->mw = mons->ww = sw;
2104 mons->mh = mons->wh = sh;
2110 selmon = wintomon(root);
2116 updatenumlockmask(void)
2119 XModifierKeymap *modmap;
2122 modmap = XGetModifierMapping(dpy);
2123 for (i = 0; i < 8; i++)
2124 for (j = 0; j < modmap->max_keypermod; j++)
2125 if (modmap->modifiermap[i * modmap->max_keypermod + j]
2126 == XKeysymToKeycode(dpy, XK_Num_Lock))
2127 numlockmask = (1 << i);
2128 XFreeModifiermap(modmap);
2132 updatesizehints(Client *c)
2137 if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
2138 /* size is uninitialized, ensure that size.flags aren't used */
2140 if (size.flags & PBaseSize) {
2141 c->basew = size.base_width;
2142 c->baseh = size.base_height;
2143 } else if (size.flags & PMinSize) {
2144 c->basew = size.min_width;
2145 c->baseh = size.min_height;
2147 c->basew = c->baseh = 0;
2148 if (size.flags & PResizeInc) {
2149 c->incw = size.width_inc;
2150 c->inch = size.height_inc;
2152 c->incw = c->inch = 0;
2153 if (size.flags & PMaxSize) {
2154 c->maxw = size.max_width;
2155 c->maxh = size.max_height;
2157 c->maxw = c->maxh = 0;
2158 if (size.flags & PMinSize) {
2159 c->minw = size.min_width;
2160 c->minh = size.min_height;
2161 } else if (size.flags & PBaseSize) {
2162 c->minw = size.base_width;
2163 c->minh = size.base_height;
2165 c->minw = c->minh = 0;
2166 if (size.flags & PAspect) {
2167 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2168 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2170 c->maxa = c->mina = 0.0;
2171 c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh);
2177 if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2178 strcpy(stext, "dwm-"VERSION);
2183 updatetitle(Client *c)
2185 if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2186 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2187 if (c->name[0] == '\0') /* hack to mark broken clients */
2188 strcpy(c->name, broken);
2192 updatewindowtype(Client *c)
2194 Atom state = getatomprop(c, netatom[NetWMState]);
2195 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2197 if (state == netatom[NetWMFullscreen])
2198 setfullscreen(c, 1);
2199 if (wtype == netatom[NetWMWindowTypeDialog])
2204 updatewmhints(Client *c)
2208 if ((wmh = XGetWMHints(dpy, c->win))) {
2209 if (c == selmon->sel && wmh->flags & XUrgencyHint) {
2210 wmh->flags &= ~XUrgencyHint;
2211 XSetWMHints(dpy, c->win, wmh);
2213 c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
2214 if (wmh->flags & InputHint)
2215 c->neverfocus = !wmh->input;
2223 view(const Arg *arg)
2225 if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2227 selmon->seltags ^= 1; /* toggle sel tagset */
2228 if (arg->ui & TAGMASK)
2229 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2241 xcb_res_client_id_spec_t spec = {0};
2243 spec.mask = XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID;
2245 xcb_generic_error_t *e = NULL;
2246 xcb_res_query_client_ids_cookie_t c = xcb_res_query_client_ids(xcon, 1, &spec);
2247 xcb_res_query_client_ids_reply_t *r = xcb_res_query_client_ids_reply(xcon, c, &e);
2252 xcb_res_client_id_value_iterator_t i = xcb_res_query_client_ids_ids_iterator(r);
2253 for (; i.rem; xcb_res_client_id_value_next(&i)) {
2254 spec = i.data->spec;
2255 if (spec.mask & XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID) {
2256 uint32_t *t = xcb_res_client_id_value_value(i.data);
2264 if (result == (pid_t)-1)
2267 #endif /* __linux__ */
2272 unsigned long len, bytes;
2273 unsigned char *prop;
2276 if (XGetWindowProperty(dpy, w, XInternAtom(dpy, "_NET_WM_PID", 1), 0, 1, False, AnyPropertyType, &type, &format, &len, &bytes, &prop) != Success || !prop)
2279 ret = *(pid_t*)prop;
2283 #endif /* __OpenBSD__ */
2288 getparentprocess(pid_t p)
2295 snprintf(buf, sizeof(buf) - 1, "/proc/%u/stat", (unsigned)p);
2297 if (!(f = fopen(buf, "r")))
2300 fscanf(f, "%*u %*s %*c %u", &v);
2302 #endif /* __linux__*/
2307 struct kinfo_proc *kp;
2309 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, NULL);
2313 kp = kvm_getprocs(kd, KERN_PROC_PID, p, sizeof(*kp), &n);
2315 #endif /* __OpenBSD__ */
2321 isdescprocess(pid_t p, pid_t c)
2323 while (p != c && c != 0)
2324 c = getparentprocess(c);
2330 termforwin(const Client *w)
2335 if (!w->pid || w->isterminal)
2338 for (m = mons; m; m = m->next) {
2339 for (c = m->clients; c; c = c->next) {
2340 if (c->isterminal && !c->swallowing && c->pid && isdescprocess(c->pid, w->pid))
2349 swallowingclient(Window w)
2354 for (m = mons; m; m = m->next) {
2355 for (c = m->clients; c; c = c->next) {
2356 if (c->swallowing && c->swallowing->win == w)
2365 wintoclient(Window w)
2370 for (m = mons; m; m = m->next)
2371 for (c = m->clients; c; c = c->next)
2384 if (w == root && getrootptr(&x, &y))
2385 return recttomon(x, y, 1, 1);
2386 for (m = mons; m; m = m->next)
2389 if ((c = wintoclient(w)))
2394 /* There's no way to check accesses to destroyed windows, thus those cases are
2395 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2396 * default error handler, which may call exit. */
2398 xerror(Display *dpy, XErrorEvent *ee)
2400 if (ee->error_code == BadWindow
2401 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2402 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2403 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2404 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2405 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2406 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2407 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2408 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2410 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2411 ee->request_code, ee->error_code);
2412 return xerrorxlib(dpy, ee); /* may call exit */
2416 xerrordummy(Display *dpy, XErrorEvent *ee)
2421 /* Startup Error handler to check if another window manager
2422 * is already running. */
2424 xerrorstart(Display *dpy, XErrorEvent *ee)
2426 die("dwm: another window manager is already running");
2431 zoom(const Arg *arg)
2433 Client *c = selmon->sel;
2435 if (!selmon->lt[selmon->sellt]->arrange
2436 || (selmon->sel && selmon->sel->isfloating))
2438 if (c == nexttiled(selmon->clients))
2439 if (!c || !(c = nexttiled(c->next)))
2445 resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst)
2459 snprintf(fullname, sizeof(fullname), "%s.%s", "dwm", name);
2460 fullname[sizeof(fullname) - 1] = '\0';
2462 XrmGetResource(db, fullname, "*", &type, &ret);
2463 if (!(ret.addr == NULL || strncmp("String", type, 64)))
2467 strcpy(sdst, ret.addr);
2470 *idst = strtoul(ret.addr, NULL, 10);
2473 *fdst = strtof(ret.addr, NULL);
2480 load_xresources(void)
2487 display = XOpenDisplay(NULL);
2488 resm = XResourceManagerString(display);
2492 db = XrmGetStringDatabase(resm);
2493 for (p = resources; p < resources + LENGTH(resources); p++)
2494 resource_load(db, p->name, p->type, p->dst);
2495 XCloseDisplay(display);
2499 main(int argc, char *argv[])
2501 if (argc == 2 && !strcmp("-v", argv[1]))
2504 die("usage: dwm [-v]");
2505 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2506 fputs("warning: no locale support\n", stderr);
2507 if (!(dpy = XOpenDisplay(NULL)))
2508 die("dwm: cannot open display");
2509 if (!(xcon = XGetXCBConnection(dpy)))
2510 die("dwm: cannot get xcb connection\n");
2516 if (pledge("stdio rpath proc exec ps", NULL) == -1)
2518 #endif /* __OpenBSD__ */
2524 return EXIT_SUCCESS;