1 /* See LICENSE file for copyright and license details.
3 * dynamic window manager is designed like any other X client as well. It is
4 * driven through handling X events. In contrast to other X clients, a window
5 * manager selects for SubstructureRedirectMask on the root window, to receive
6 * events about window (dis-)appearance. Only one X connection at a time is
7 * allowed to select for this event mask.
9 * The event handlers of dwm are organized in an array which is accessed
10 * whenever a new event has been fetched. This allows event dispatching
13 * Each child of the root window is called a client, except windows which have
14 * set the override_redirect flag. Clients are organized in a linked client
15 * list on each monitor, the focus history is remembered through a stack list
16 * on each monitor. Each client contains a bit array to indicate the tags of a
19 * Keys and tagging rules are organized as arrays and defined in config.h.
21 * To understand everything else, start reading main().
31 #include <sys/types.h>
34 #include <X11/cursorfont.h>
35 #include <X11/keysym.h>
36 #include <X11/Xatom.h>
38 #include <X11/Xproto.h>
39 #include <X11/Xutil.h>
40 #include <X11/Xresource.h>
42 #include <X11/extensions/Xinerama.h>
44 #include <X11/Xft/Xft.h>
50 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
51 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
52 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
53 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
54 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
55 #define LENGTH(X) (sizeof X / sizeof X[0])
56 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
57 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
58 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
59 #define TAGMASK ((1 << LENGTH(tags)) - 1)
60 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
63 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
64 enum { SchemeNorm, SchemeSel }; /* color schemes */
65 enum { NetSupported, NetWMName, NetWMState, NetWMCheck,
66 NetWMFullscreen, NetActiveWindow, NetWMWindowType,
67 NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */
68 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
69 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
70 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
83 void (*func)(const Arg *arg);
87 typedef struct Monitor Monitor;
88 typedef struct Client Client;
93 int oldx, oldy, oldw, oldh;
94 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
97 int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
107 void (*func)(const Arg *);
113 void (*arrange)(Monitor *);
121 int by; /* bar geometry */
122 int mx, my, mw, mh; /* screen size */
123 int wx, wy, ww, wh; /* window area */
124 int gappx; /* gaps between windows */
125 unsigned int seltags;
127 unsigned int tagset[2];
140 const char *instance;
147 /* Xresources preferences */
156 enum resource_type type;
160 /* function declarations */
161 static void applyrules(Client *c);
162 static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact);
163 static void arrange(Monitor *m);
164 static void arrangemon(Monitor *m);
165 static void attach(Client *c);
166 static void attachstack(Client *c);
167 static void buttonpress(XEvent *e);
168 static void checkotherwm(void);
169 static void cleanup(void);
170 static void cleanupmon(Monitor *mon);
171 static void clientmessage(XEvent *e);
172 static void configure(Client *c);
173 static void configurenotify(XEvent *e);
174 static void configurerequest(XEvent *e);
175 static Monitor *createmon(void);
176 static void destroynotify(XEvent *e);
177 static void detach(Client *c);
178 static void detachstack(Client *c);
179 static Monitor *dirtomon(int dir);
180 static void drawbar(Monitor *m);
181 static void drawbars(void);
182 static void enternotify(XEvent *e);
183 static void expose(XEvent *e);
184 static void focus(Client *c);
185 static void focusin(XEvent *e);
186 static void focusmon(const Arg *arg);
187 static void focusstack(const Arg *arg);
188 static Atom getatomprop(Client *c, Atom prop);
189 static int getrootptr(int *x, int *y);
190 static long getstate(Window w);
191 static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
192 static void grabbuttons(Client *c, int focused);
193 static void grabkeys(void);
194 static void incnmaster(const Arg *arg);
195 static void keypress(XEvent *e);
196 static void killclient(const Arg *arg);
197 static void manage(Window w, XWindowAttributes *wa);
198 static void mappingnotify(XEvent *e);
199 static void maprequest(XEvent *e);
200 static void monocle(Monitor *m);
201 static void motionnotify(XEvent *e);
202 static void movemouse(const Arg *arg);
203 static Client *nexttiled(Client *c);
204 static void pop(Client *);
205 static void propertynotify(XEvent *e);
206 static void quit(const Arg *arg);
207 static Monitor *recttomon(int x, int y, int w, int h);
208 static void resize(Client *c, int x, int y, int w, int h, int interact);
209 static void resizeclient(Client *c, int x, int y, int w, int h);
210 static void resizemouse(const Arg *arg);
211 static void restack(Monitor *m);
212 static void run(void);
213 static void runautostart(void);
214 static void scan(void);
215 static int sendevent(Client *c, Atom proto);
216 static void sendmon(Client *c, Monitor *m);
217 static void setclientstate(Client *c, long state);
218 static void setfocus(Client *c);
219 static void setfullscreen(Client *c, int fullscreen);
220 static void setgaps(const Arg *arg);
221 static void setlayout(const Arg *arg);
222 static void setmfact(const Arg *arg);
223 static void setup(void);
224 static void seturgent(Client *c, int urg);
225 static void showhide(Client *c);
226 static void sigchld(int unused);
227 static void spawn(const Arg *arg);
228 static void tag(const Arg *arg);
229 static void tagmon(const Arg *arg);
230 static void tile(Monitor *);
231 static void togglebar(const Arg *arg);
232 static void togglefloating(const Arg *arg);
233 static void toggletag(const Arg *arg);
234 static void toggleview(const Arg *arg);
235 static void unfocus(Client *c, int setfocus);
236 static void unmanage(Client *c, int destroyed);
237 static void unmapnotify(XEvent *e);
238 static void updatebarpos(Monitor *m);
239 static void updatebars(void);
240 static void updateclientlist(void);
241 static int updategeom(void);
242 static void updatenumlockmask(void);
243 static void updatesizehints(Client *c);
244 static void updatestatus(void);
245 static void updatetitle(Client *c);
246 static void updatewindowtype(Client *c);
247 static void updatewmhints(Client *c);
248 static void view(const Arg *arg);
249 static Client *wintoclient(Window w);
250 static Monitor *wintomon(Window w);
251 static int xerror(Display *dpy, XErrorEvent *ee);
252 static int xerrordummy(Display *dpy, XErrorEvent *ee);
253 static int xerrorstart(Display *dpy, XErrorEvent *ee);
254 static void zoom(const Arg *arg);
255 static void load_xresources(void);
256 static void resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst);
259 static const char autostartblocksh[] = "autostart_blocking.sh";
260 static const char autostartsh[] = "autostart.sh";
261 static const char broken[] = "broken";
262 static const char dwmdir[] = "dwm";
263 static const char localshare[] = ".local/share";
264 static char stext[256];
266 static int sw, sh; /* X display screen geometry width, height */
267 static int bh, blw = 0; /* bar geometry */
268 static int lrpad; /* sum of left and right padding for text */
269 static int vp; /* vertical padding for bar */
270 static int sp; /* side padding for bar */
271 static int (*xerrorxlib)(Display *, XErrorEvent *);
272 static unsigned int numlockmask = 0;
273 static void (*handler[LASTEvent]) (XEvent *) = {
274 [ButtonPress] = buttonpress,
275 [ClientMessage] = clientmessage,
276 [ConfigureRequest] = configurerequest,
277 [ConfigureNotify] = configurenotify,
278 [DestroyNotify] = destroynotify,
279 [EnterNotify] = enternotify,
282 [KeyPress] = keypress,
283 [MappingNotify] = mappingnotify,
284 [MapRequest] = maprequest,
285 [MotionNotify] = motionnotify,
286 [PropertyNotify] = propertynotify,
287 [UnmapNotify] = unmapnotify
289 static Atom wmatom[WMLast], netatom[NetLast];
290 static int running = 1;
291 static Cur *cursor[CurLast];
295 static Monitor *mons, *selmon;
296 static Window root, wmcheckwin;
298 /* configuration, allows nested code to access above variables */
301 /* compile-time check if all tags fit into an unsigned int bit array. */
302 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
304 /* function implementations */
306 applyrules(Client *c)
308 const char *class, *instance;
312 XClassHint ch = { NULL, NULL };
317 XGetClassHint(dpy, c->win, &ch);
318 class = ch.res_class ? ch.res_class : broken;
319 instance = ch.res_name ? ch.res_name : broken;
321 for (i = 0; i < LENGTH(rules); i++) {
323 if ((!r->title || strstr(c->name, r->title))
324 && (!r->class || strstr(class, r->class))
325 && (!r->instance || strstr(instance, r->instance)))
327 c->isfloating = r->isfloating;
329 for (m = mons; m && m->num != r->monitor; m = m->next);
338 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
342 applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact)
347 /* set minimum possible */
355 if (*x + *w + 2 * c->bw < 0)
357 if (*y + *h + 2 * c->bw < 0)
360 if (*x >= m->wx + m->ww)
361 *x = m->wx + m->ww - WIDTH(c);
362 if (*y >= m->wy + m->wh)
363 *y = m->wy + m->wh - HEIGHT(c);
364 if (*x + *w + 2 * c->bw <= m->wx)
366 if (*y + *h + 2 * c->bw <= m->wy)
373 if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
374 /* see last two sentences in ICCCM 4.1.2.3 */
375 baseismin = c->basew == c->minw && c->baseh == c->minh;
376 if (!baseismin) { /* temporarily remove base dimensions */
380 /* adjust for aspect limits */
381 if (c->mina > 0 && c->maxa > 0) {
382 if (c->maxa < (float)*w / *h)
383 *w = *h * c->maxa + 0.5;
384 else if (c->mina < (float)*h / *w)
385 *h = *w * c->mina + 0.5;
387 if (baseismin) { /* increment calculation requires this */
391 /* adjust for increment value */
396 /* restore base dimensions */
397 *w = MAX(*w + c->basew, c->minw);
398 *h = MAX(*h + c->baseh, c->minh);
400 *w = MIN(*w, c->maxw);
402 *h = MIN(*h, c->maxh);
404 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
412 else for (m = mons; m; m = m->next)
417 } else for (m = mons; m; m = m->next)
422 arrangemon(Monitor *m)
424 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
425 if (m->lt[m->sellt]->arrange)
426 m->lt[m->sellt]->arrange(m);
432 c->next = c->mon->clients;
437 attachstack(Client *c)
439 c->snext = c->mon->stack;
444 buttonpress(XEvent *e)
446 unsigned int i, x, click;
450 XButtonPressedEvent *ev = &e->xbutton;
453 /* focus monitor if necessary */
454 if ((m = wintomon(ev->window)) && m != selmon) {
455 unfocus(selmon->sel, 1);
459 if (ev->window == selmon->barwin) {
463 while (ev->x >= x && ++i < LENGTH(tags));
464 if (i < LENGTH(tags)) {
467 } else if (ev->x < x + blw)
469 else if (ev->x > selmon->ww - TEXTW(stext))
470 click = ClkStatusText;
473 } else if ((c = wintoclient(ev->window))) {
476 XAllowEvents(dpy, ReplayPointer, CurrentTime);
477 click = ClkClientWin;
479 for (i = 0; i < LENGTH(buttons); i++)
480 if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
481 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
482 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
488 xerrorxlib = XSetErrorHandler(xerrorstart);
489 /* this causes an error if some other window manager is running */
490 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
492 XSetErrorHandler(xerror);
500 Layout foo = { "", NULL };
505 selmon->lt[selmon->sellt] = &foo;
506 for (m = mons; m; m = m->next)
508 unmanage(m->stack, 0);
509 XUngrabKey(dpy, AnyKey, AnyModifier, root);
512 for (i = 0; i < CurLast; i++)
513 drw_cur_free(drw, cursor[i]);
514 for (i = 0; i < LENGTH(colors); i++)
516 XDestroyWindow(dpy, wmcheckwin);
519 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
520 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
524 cleanupmon(Monitor *mon)
531 for (m = mons; m && m->next != mon; m = m->next);
534 XUnmapWindow(dpy, mon->barwin);
535 XDestroyWindow(dpy, mon->barwin);
540 clientmessage(XEvent *e)
542 XClientMessageEvent *cme = &e->xclient;
543 Client *c = wintoclient(cme->window);
547 if (cme->message_type == netatom[NetWMState]) {
548 if (cme->data.l[1] == netatom[NetWMFullscreen]
549 || cme->data.l[2] == netatom[NetWMFullscreen])
550 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
551 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
552 } else if (cme->message_type == netatom[NetActiveWindow]) {
553 if (c != selmon->sel && !c->isurgent)
563 ce.type = ConfigureNotify;
571 ce.border_width = c->bw;
573 ce.override_redirect = False;
574 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
578 configurenotify(XEvent *e)
582 XConfigureEvent *ev = &e->xconfigure;
585 /* TODO: updategeom handling sucks, needs to be simplified */
586 if (ev->window == root) {
587 dirty = (sw != ev->width || sh != ev->height);
590 if (updategeom() || dirty) {
591 drw_resize(drw, sw, bh);
593 for (m = mons; m; m = m->next) {
594 for (c = m->clients; c; c = c->next)
596 resizeclient(c, m->mx, m->my, m->mw, m->mh);
597 XMoveResizeWindow(dpy, m->barwin, m->wx + sp, m->by + vp, m->ww - 2 * sp, bh);
606 configurerequest(XEvent *e)
610 XConfigureRequestEvent *ev = &e->xconfigurerequest;
613 if ((c = wintoclient(ev->window))) {
614 if (ev->value_mask & CWBorderWidth)
615 c->bw = ev->border_width;
616 else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
618 if (ev->value_mask & CWX) {
620 c->x = m->mx + ev->x;
622 if (ev->value_mask & CWY) {
624 c->y = m->my + ev->y;
626 if (ev->value_mask & CWWidth) {
630 if (ev->value_mask & CWHeight) {
634 if ((c->x + c->w) > m->mx + m->mw && c->isfloating)
635 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
636 if ((c->y + c->h) > m->my + m->mh && c->isfloating)
637 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
638 if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
641 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
647 wc.width = ev->width;
648 wc.height = ev->height;
649 wc.border_width = ev->border_width;
650 wc.sibling = ev->above;
651 wc.stack_mode = ev->detail;
652 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
662 m = ecalloc(1, sizeof(Monitor));
663 m->tagset[0] = m->tagset[1] = 1;
665 m->nmaster = nmaster;
666 m->showbar = showbar;
669 m->lt[0] = &layouts[0];
670 m->lt[1] = &layouts[1 % LENGTH(layouts)];
671 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
676 destroynotify(XEvent *e)
679 XDestroyWindowEvent *ev = &e->xdestroywindow;
681 if ((c = wintoclient(ev->window)))
690 for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
695 detachstack(Client *c)
699 for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
702 if (c == c->mon->sel) {
703 for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
714 if (!(m = selmon->next))
716 } else if (selmon == mons)
717 for (m = mons; m->next; m = m->next);
719 for (m = mons; m->next != selmon; m = m->next);
727 int boxs = drw->fonts->h / 9;
728 int boxw = drw->fonts->h / 6 + 2;
729 unsigned int i, occ = 0, urg = 0;
732 /* draw status first so it can be overdrawn by tags later */
733 if (m == selmon) { /* status is only drawn on selected monitor */
734 drw_setscheme(drw, scheme[SchemeNorm]);
735 tw = TEXTW(stext) - lrpad + 2; /* 2px right padding */
736 drw_text(drw, m->ww - tw - 2 * sp, 0, tw, bh, 0, stext, 0);
739 for (c = m->clients; c; c = c->next) {
745 for (i = 0; i < LENGTH(tags); i++) {
747 drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]);
748 drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i);
750 drw_rect(drw, x + boxs, boxs, boxw, boxw,
751 m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
755 w = blw = TEXTW(m->ltsymbol);
756 drw_setscheme(drw, scheme[SchemeNorm]);
757 x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0);
759 if ((w = m->ww - tw - x) > bh) {
761 drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]);
762 drw_text(drw, x, 0, w - 2 * sp, bh, lrpad / 2, m->sel->name, 0);
763 if (m->sel->isfloating)
764 drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
766 drw_setscheme(drw, scheme[SchemeNorm]);
767 drw_rect(drw, x, 0, w - 2 * sp, bh, 1, 1);
770 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
778 for (m = mons; m; m = m->next)
783 enternotify(XEvent *e)
787 XCrossingEvent *ev = &e->xcrossing;
789 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
791 c = wintoclient(ev->window);
792 m = c ? c->mon : wintomon(ev->window);
794 unfocus(selmon->sel, 1);
796 } else if (!c || c == selmon->sel)
805 XExposeEvent *ev = &e->xexpose;
807 if (ev->count == 0 && (m = wintomon(ev->window)))
814 if (!c || !ISVISIBLE(c))
815 for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
816 if (selmon->sel && selmon->sel != c)
817 unfocus(selmon->sel, 0);
819 if (c->mon != selmon)
826 XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel);
829 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
830 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
836 /* there are some broken focus acquiring clients needing extra handling */
840 XFocusChangeEvent *ev = &e->xfocus;
842 if (selmon->sel && ev->window != selmon->sel->win)
843 setfocus(selmon->sel);
847 focusmon(const Arg *arg)
853 if ((m = dirtomon(arg->i)) == selmon)
855 unfocus(selmon->sel, 0);
861 focusstack(const Arg *arg)
863 Client *c = NULL, *i;
868 for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
870 for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
872 for (i = selmon->clients; i != selmon->sel; i = i->next)
876 for (; i; i = i->next)
887 getatomprop(Client *c, Atom prop)
891 unsigned char *p = NULL;
892 Atom da, atom = None;
894 if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
895 &da, &di, &dl, &dl, &p) == Success && p) {
903 getrootptr(int *x, int *y)
909 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
917 unsigned char *p = NULL;
918 unsigned long n, extra;
921 if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
922 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
931 gettextprop(Window w, Atom atom, char *text, unsigned int size)
937 if (!text || size == 0)
940 if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems)
942 if (name.encoding == XA_STRING)
943 strncpy(text, (char *)name.value, size - 1);
945 if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
946 strncpy(text, *list, size - 1);
947 XFreeStringList(list);
950 text[size - 1] = '\0';
956 grabbuttons(Client *c, int focused)
961 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
962 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
964 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
965 BUTTONMASK, GrabModeSync, GrabModeSync, None, None);
966 for (i = 0; i < LENGTH(buttons); i++)
967 if (buttons[i].click == ClkClientWin)
968 for (j = 0; j < LENGTH(modifiers); j++)
969 XGrabButton(dpy, buttons[i].button,
970 buttons[i].mask | modifiers[j],
971 c->win, False, BUTTONMASK,
972 GrabModeAsync, GrabModeSync, None, None);
982 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
985 XUngrabKey(dpy, AnyKey, AnyModifier, root);
986 for (i = 0; i < LENGTH(keys); i++)
987 if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
988 for (j = 0; j < LENGTH(modifiers); j++)
989 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
990 True, GrabModeAsync, GrabModeAsync);
995 incnmaster(const Arg *arg)
997 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
1003 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
1006 if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1007 && unique[n].width == info->width && unique[n].height == info->height)
1011 #endif /* XINERAMA */
1021 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1022 for (i = 0; i < LENGTH(keys); i++)
1023 if (keysym == keys[i].keysym
1024 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1026 keys[i].func(&(keys[i].arg));
1030 killclient(const Arg *arg)
1034 if (!sendevent(selmon->sel, wmatom[WMDelete])) {
1036 XSetErrorHandler(xerrordummy);
1037 XSetCloseDownMode(dpy, DestroyAll);
1038 XKillClient(dpy, selmon->sel->win);
1040 XSetErrorHandler(xerror);
1046 manage(Window w, XWindowAttributes *wa)
1048 Client *c, *t = NULL;
1049 Window trans = None;
1052 c = ecalloc(1, sizeof(Client));
1055 c->x = c->oldx = wa->x;
1056 c->y = c->oldy = wa->y;
1057 c->w = c->oldw = wa->width;
1058 c->h = c->oldh = wa->height;
1059 c->oldbw = wa->border_width;
1062 if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1070 if (c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1071 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1072 if (c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1073 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1074 c->x = MAX(c->x, c->mon->mx);
1075 /* only fix client y-offset, if the client center might cover the bar */
1076 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1077 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1080 wc.border_width = c->bw;
1081 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1082 XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel);
1083 configure(c); /* propagates border_width, if size doesn't change */
1084 updatewindowtype(c);
1087 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1090 c->isfloating = c->oldstate = trans != None || c->isfixed;
1092 XRaiseWindow(dpy, c->win);
1095 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1096 (unsigned char *) &(c->win), 1);
1097 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1098 setclientstate(c, NormalState);
1099 if (c->mon == selmon)
1100 unfocus(selmon->sel, 0);
1103 XMapWindow(dpy, c->win);
1108 mappingnotify(XEvent *e)
1110 XMappingEvent *ev = &e->xmapping;
1112 XRefreshKeyboardMapping(ev);
1113 if (ev->request == MappingKeyboard)
1118 maprequest(XEvent *e)
1120 static XWindowAttributes wa;
1121 XMapRequestEvent *ev = &e->xmaprequest;
1123 if (!XGetWindowAttributes(dpy, ev->window, &wa))
1125 if (wa.override_redirect)
1127 if (!wintoclient(ev->window))
1128 manage(ev->window, &wa);
1137 for (c = m->clients; c; c = c->next)
1140 if (n > 0) /* override layout symbol */
1141 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1142 for (c = nexttiled(m->clients); c; c = nexttiled(c->next))
1143 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
1147 motionnotify(XEvent *e)
1149 static Monitor *mon = NULL;
1151 XMotionEvent *ev = &e->xmotion;
1153 if (ev->window != root)
1155 if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1156 unfocus(selmon->sel, 1);
1164 movemouse(const Arg *arg)
1166 int x, y, ocx, ocy, nx, ny;
1172 if (!(c = selmon->sel))
1174 if (c->isfullscreen) /* no support moving fullscreen windows by mouse */
1179 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1180 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1182 if (!getrootptr(&x, &y))
1185 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1187 case ConfigureRequest:
1190 handler[ev.type](&ev);
1193 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1195 lasttime = ev.xmotion.time;
1197 nx = ocx + (ev.xmotion.x - x);
1198 ny = ocy + (ev.xmotion.y - y);
1199 if (abs(selmon->wx - nx) < snap)
1201 else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1202 nx = selmon->wx + selmon->ww - WIDTH(c);
1203 if (abs(selmon->wy - ny) < snap)
1205 else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1206 ny = selmon->wy + selmon->wh - HEIGHT(c);
1207 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1208 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1209 togglefloating(NULL);
1210 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1211 resize(c, nx, ny, c->w, c->h, 1);
1214 } while (ev.type != ButtonRelease);
1215 XUngrabPointer(dpy, CurrentTime);
1216 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1224 nexttiled(Client *c)
1226 for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1240 propertynotify(XEvent *e)
1244 XPropertyEvent *ev = &e->xproperty;
1246 if ((ev->window == root) && (ev->atom == XA_WM_NAME))
1248 else if (ev->state == PropertyDelete)
1249 return; /* ignore */
1250 else if ((c = wintoclient(ev->window))) {
1253 case XA_WM_TRANSIENT_FOR:
1254 if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1255 (c->isfloating = (wintoclient(trans)) != NULL))
1258 case XA_WM_NORMAL_HINTS:
1266 if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1268 if (c == c->mon->sel)
1271 if (ev->atom == netatom[NetWMWindowType])
1272 updatewindowtype(c);
1277 quit(const Arg *arg)
1283 recttomon(int x, int y, int w, int h)
1285 Monitor *m, *r = selmon;
1288 for (m = mons; m; m = m->next)
1289 if ((a = INTERSECT(x, y, w, h, m)) > area) {
1297 resize(Client *c, int x, int y, int w, int h, int interact)
1299 if (applysizehints(c, &x, &y, &w, &h, interact))
1300 resizeclient(c, x, y, w, h);
1304 resizeclient(Client *c, int x, int y, int w, int h)
1308 c->oldx = c->x; c->x = wc.x = x;
1309 c->oldy = c->y; c->y = wc.y = y;
1310 c->oldw = c->w; c->w = wc.width = w;
1311 c->oldh = c->h; c->h = wc.height = h;
1312 wc.border_width = c->bw;
1313 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1319 resizemouse(const Arg *arg)
1321 int ocx, ocy, nw, nh;
1327 if (!(c = selmon->sel))
1329 if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1334 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1335 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1337 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1339 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1341 case ConfigureRequest:
1344 handler[ev.type](&ev);
1347 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1349 lasttime = ev.xmotion.time;
1351 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1352 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1353 if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1354 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1356 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1357 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1358 togglefloating(NULL);
1360 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1361 resize(c, c->x, c->y, nw, nh, 1);
1364 } while (ev.type != ButtonRelease);
1365 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1366 XUngrabPointer(dpy, CurrentTime);
1367 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1368 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1385 if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
1386 XRaiseWindow(dpy, m->sel->win);
1387 if (m->lt[m->sellt]->arrange) {
1388 wc.stack_mode = Below;
1389 wc.sibling = m->barwin;
1390 for (c = m->stack; c; c = c->snext)
1391 if (!c->isfloating && ISVISIBLE(c)) {
1392 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1393 wc.sibling = c->win;
1397 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1404 /* main event loop */
1406 while (running && !XNextEvent(dpy, &ev))
1407 if (handler[ev.type])
1408 handler[ev.type](&ev); /* call handler */
1420 if ((home = getenv("HOME")) == NULL)
1421 /* this is almost impossible */
1424 /* if $XDG_DATA_HOME is set and not empty, use $XDG_DATA_HOME/dwm,
1425 * otherwise use ~/.local/share/dwm as autostart script directory
1427 xdgdatahome = getenv("XDG_DATA_HOME");
1428 if (xdgdatahome != NULL && *xdgdatahome != '\0') {
1429 /* space for path segments, separators and nul */
1430 pathpfx = ecalloc(1, strlen(xdgdatahome) + strlen(dwmdir) + 2);
1432 if (sprintf(pathpfx, "%s/%s", xdgdatahome, dwmdir) <= 0) {
1437 /* space for path segments, separators and nul */
1438 pathpfx = ecalloc(1, strlen(home) + strlen(localshare)
1439 + strlen(dwmdir) + 3);
1441 if (sprintf(pathpfx, "%s/%s/%s", home, localshare, dwmdir) < 0) {
1447 /* check if the autostart script directory exists */
1448 if (! (stat(pathpfx, &sb) == 0 && S_ISDIR(sb.st_mode))) {
1449 /* the XDG conformant path does not exist or is no directory
1450 * so we try ~/.dwm instead
1452 char *pathpfx_new = realloc(pathpfx, strlen(home) + strlen(dwmdir) + 3);
1453 if(pathpfx_new == NULL) {
1457 pathpfx = pathpfx_new;
1459 if (sprintf(pathpfx, "%s/.%s", home, dwmdir) <= 0) {
1465 /* try the blocking script first */
1466 path = ecalloc(1, strlen(pathpfx) + strlen(autostartblocksh) + 2);
1467 if (sprintf(path, "%s/%s", pathpfx, autostartblocksh) <= 0) {
1472 if (access(path, X_OK) == 0)
1475 /* now the non-blocking script */
1476 if (sprintf(path, "%s/%s", pathpfx, autostartsh) <= 0) {
1481 if (access(path, X_OK) == 0)
1482 system(strcat(path, " &"));
1491 unsigned int i, num;
1492 Window d1, d2, *wins = NULL;
1493 XWindowAttributes wa;
1495 if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1496 for (i = 0; i < num; i++) {
1497 if (!XGetWindowAttributes(dpy, wins[i], &wa)
1498 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1500 if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1501 manage(wins[i], &wa);
1503 for (i = 0; i < num; i++) { /* now the transients */
1504 if (!XGetWindowAttributes(dpy, wins[i], &wa))
1506 if (XGetTransientForHint(dpy, wins[i], &d1)
1507 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1508 manage(wins[i], &wa);
1516 sendmon(Client *c, Monitor *m)
1524 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1532 setclientstate(Client *c, long state)
1534 long data[] = { state, None };
1536 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1537 PropModeReplace, (unsigned char *)data, 2);
1541 sendevent(Client *c, Atom proto)
1548 if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1549 while (!exists && n--)
1550 exists = protocols[n] == proto;
1554 ev.type = ClientMessage;
1555 ev.xclient.window = c->win;
1556 ev.xclient.message_type = wmatom[WMProtocols];
1557 ev.xclient.format = 32;
1558 ev.xclient.data.l[0] = proto;
1559 ev.xclient.data.l[1] = CurrentTime;
1560 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1568 if (!c->neverfocus) {
1569 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1570 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1571 XA_WINDOW, 32, PropModeReplace,
1572 (unsigned char *) &(c->win), 1);
1574 sendevent(c, wmatom[WMTakeFocus]);
1578 setfullscreen(Client *c, int fullscreen)
1580 if (fullscreen && !c->isfullscreen) {
1581 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1582 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1583 c->isfullscreen = 1;
1584 c->oldstate = c->isfloating;
1588 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1589 XRaiseWindow(dpy, c->win);
1590 } else if (!fullscreen && c->isfullscreen){
1591 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1592 PropModeReplace, (unsigned char*)0, 0);
1593 c->isfullscreen = 0;
1594 c->isfloating = c->oldstate;
1600 resizeclient(c, c->x, c->y, c->w, c->h);
1606 setgaps(const Arg *arg)
1608 if ((arg->i == 0) || (selmon->gappx + arg->i < 0))
1611 selmon->gappx += arg->i;
1616 setlayout(const Arg *arg)
1618 if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1621 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1622 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1629 /* arg > 1.0 will set mfact absolutely */
1631 setmfact(const Arg *arg)
1635 if (!arg || !selmon->lt[selmon->sellt]->arrange)
1637 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1638 if (f < 0.05 || f > 0.95)
1648 XSetWindowAttributes wa;
1651 /* clean up any zombies immediately */
1655 screen = DefaultScreen(dpy);
1656 sw = DisplayWidth(dpy, screen);
1657 sh = DisplayHeight(dpy, screen);
1658 root = RootWindow(dpy, screen);
1659 drw = drw_create(dpy, screen, root, sw, sh);
1660 if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
1661 die("no fonts could be loaded.");
1662 lrpad = drw->fonts->h;
1663 bh = drw->fonts->h + 2;
1666 vp = (topbar == 1) ? vertpad : - vertpad;
1669 utf8string = XInternAtom(dpy, "UTF8_STRING", False);
1670 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1671 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1672 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1673 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1674 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1675 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1676 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1677 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1678 netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
1679 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1680 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1681 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1682 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1684 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1685 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1686 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1687 /* init appearance */
1688 scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
1689 for (i = 0; i < LENGTH(colors); i++)
1690 scheme[i] = drw_scm_create(drw, colors[i], 3);
1694 updatebarpos(selmon);
1695 /* supporting window for NetWMCheck */
1696 wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
1697 XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
1698 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1699 XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8,
1700 PropModeReplace, (unsigned char *) "dwm", 3);
1701 XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32,
1702 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1703 /* EWMH support per view */
1704 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1705 PropModeReplace, (unsigned char *) netatom, NetLast);
1706 XDeleteProperty(dpy, root, netatom[NetClientList]);
1708 wa.cursor = cursor[CurNormal]->cursor;
1709 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
1710 |ButtonPressMask|PointerMotionMask|EnterWindowMask
1711 |LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1712 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1713 XSelectInput(dpy, root, wa.event_mask);
1720 seturgent(Client *c, int urg)
1725 if (!(wmh = XGetWMHints(dpy, c->win)))
1727 wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
1728 XSetWMHints(dpy, c->win, wmh);
1738 /* show clients top down */
1739 XMoveWindow(dpy, c->win, c->x, c->y);
1740 if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1741 resize(c, c->x, c->y, c->w, c->h, 0);
1744 /* hide clients bottom up */
1746 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1753 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1754 die("can't install SIGCHLD handler:");
1755 while (0 < waitpid(-1, NULL, WNOHANG));
1759 spawn(const Arg *arg)
1761 if (arg->v == dmenucmd)
1762 dmenumon[0] = '0' + selmon->num;
1765 close(ConnectionNumber(dpy));
1767 execvp(((char **)arg->v)[0], (char **)arg->v);
1768 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1777 if (selmon->sel && arg->ui & TAGMASK) {
1778 selmon->sel->tags = arg->ui & TAGMASK;
1785 tagmon(const Arg *arg)
1787 if (!selmon->sel || !mons->next)
1789 sendmon(selmon->sel, dirtomon(arg->i));
1795 unsigned int i, n, h, mw, my, ty;
1798 for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1803 mw = m->nmaster ? m->ww * m->mfact : 0;
1805 mw = m->ww - m->gappx;
1806 for (i = 0, my = ty = m->gappx, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1807 if (i < m->nmaster) {
1808 h = (m->wh - my) / (MIN(n, m->nmaster) - i) - m->gappx;
1809 resize(c, m->wx + m->gappx, m->wy + my, mw - (2*c->bw) - m->gappx, h - (2*c->bw), 0);
1810 if (my + HEIGHT(c) + m->gappx < m->wh)
1811 my += HEIGHT(c) + m->gappx;
1813 h = (m->wh - ty) / (n - i) - m->gappx;
1814 resize(c, m->wx + mw + m->gappx, m->wy + ty, m->ww - mw - (2*c->bw) - 2*m->gappx, h - (2*c->bw), 0);
1815 if (ty + HEIGHT(c) + m->gappx < m->wh)
1816 ty += HEIGHT(c) + m->gappx;
1821 togglebar(const Arg *arg)
1823 selmon->showbar = !selmon->showbar;
1824 updatebarpos(selmon);
1825 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx + sp, selmon->by + vp, selmon->ww - 2 * sp, bh);
1830 togglefloating(const Arg *arg)
1834 if (selmon->sel->isfullscreen) /* no support for fullscreen windows */
1836 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1837 if (selmon->sel->isfloating)
1838 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1839 selmon->sel->w, selmon->sel->h, 0);
1844 toggletag(const Arg *arg)
1846 unsigned int newtags;
1850 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1852 selmon->sel->tags = newtags;
1859 toggleview(const Arg *arg)
1861 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1864 selmon->tagset[selmon->seltags] = newtagset;
1871 unfocus(Client *c, int setfocus)
1876 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
1878 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1879 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1884 unmanage(Client *c, int destroyed)
1886 Monitor *m = c->mon;
1892 wc.border_width = c->oldbw;
1893 XGrabServer(dpy); /* avoid race conditions */
1894 XSetErrorHandler(xerrordummy);
1895 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1896 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1897 setclientstate(c, WithdrawnState);
1899 XSetErrorHandler(xerror);
1909 unmapnotify(XEvent *e)
1912 XUnmapEvent *ev = &e->xunmap;
1914 if ((c = wintoclient(ev->window))) {
1916 setclientstate(c, WithdrawnState);
1926 XSetWindowAttributes wa = {
1927 .override_redirect = True,
1928 .background_pixmap = ParentRelative,
1929 .event_mask = ButtonPressMask|ExposureMask
1931 XClassHint ch = {"dwm", "dwm"};
1932 for (m = mons; m; m = m->next) {
1935 m->barwin = XCreateWindow(dpy, root, m->wx + sp, m->by + vp, m->ww - 2 * sp, bh, 0, DefaultDepth(dpy, screen),
1936 CopyFromParent, DefaultVisual(dpy, screen),
1937 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1938 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
1939 XMapRaised(dpy, m->barwin);
1940 XSetClassHint(dpy, m->barwin, &ch);
1945 updatebarpos(Monitor *m)
1950 m->wh = m->wh - vertpad - bh;
1951 m->by = m->topbar ? m->wy : m->wy + m->wh + vertpad;
1952 m->wy = m->topbar ? m->wy + bh + vp : m->wy;
1963 XDeleteProperty(dpy, root, netatom[NetClientList]);
1964 for (m = mons; m; m = m->next)
1965 for (c = m->clients; c; c = c->next)
1966 XChangeProperty(dpy, root, netatom[NetClientList],
1967 XA_WINDOW, 32, PropModeAppend,
1968 (unsigned char *) &(c->win), 1);
1977 if (XineramaIsActive(dpy)) {
1981 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1982 XineramaScreenInfo *unique = NULL;
1984 for (n = 0, m = mons; m; m = m->next, n++);
1985 /* only consider unique geometries as separate screens */
1986 unique = ecalloc(nn, sizeof(XineramaScreenInfo));
1987 for (i = 0, j = 0; i < nn; i++)
1988 if (isuniquegeom(unique, j, &info[i]))
1989 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1992 if (n <= nn) { /* new monitors available */
1993 for (i = 0; i < (nn - n); i++) {
1994 for (m = mons; m && m->next; m = m->next);
1996 m->next = createmon();
2000 for (i = 0, m = mons; i < nn && m; m = m->next, i++)
2002 || unique[i].x_org != m->mx || unique[i].y_org != m->my
2003 || unique[i].width != m->mw || unique[i].height != m->mh)
2007 m->mx = m->wx = unique[i].x_org;
2008 m->my = m->wy = unique[i].y_org;
2009 m->mw = m->ww = unique[i].width;
2010 m->mh = m->wh = unique[i].height;
2013 } else { /* less monitors available nn < n */
2014 for (i = nn; i < n; i++) {
2015 for (m = mons; m && m->next; m = m->next);
2016 while ((c = m->clients)) {
2018 m->clients = c->next;
2031 #endif /* XINERAMA */
2032 { /* default monitor setup */
2035 if (mons->mw != sw || mons->mh != sh) {
2037 mons->mw = mons->ww = sw;
2038 mons->mh = mons->wh = sh;
2044 selmon = wintomon(root);
2050 updatenumlockmask(void)
2053 XModifierKeymap *modmap;
2056 modmap = XGetModifierMapping(dpy);
2057 for (i = 0; i < 8; i++)
2058 for (j = 0; j < modmap->max_keypermod; j++)
2059 if (modmap->modifiermap[i * modmap->max_keypermod + j]
2060 == XKeysymToKeycode(dpy, XK_Num_Lock))
2061 numlockmask = (1 << i);
2062 XFreeModifiermap(modmap);
2066 updatesizehints(Client *c)
2071 if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
2072 /* size is uninitialized, ensure that size.flags aren't used */
2074 if (size.flags & PBaseSize) {
2075 c->basew = size.base_width;
2076 c->baseh = size.base_height;
2077 } else if (size.flags & PMinSize) {
2078 c->basew = size.min_width;
2079 c->baseh = size.min_height;
2081 c->basew = c->baseh = 0;
2082 if (size.flags & PResizeInc) {
2083 c->incw = size.width_inc;
2084 c->inch = size.height_inc;
2086 c->incw = c->inch = 0;
2087 if (size.flags & PMaxSize) {
2088 c->maxw = size.max_width;
2089 c->maxh = size.max_height;
2091 c->maxw = c->maxh = 0;
2092 if (size.flags & PMinSize) {
2093 c->minw = size.min_width;
2094 c->minh = size.min_height;
2095 } else if (size.flags & PBaseSize) {
2096 c->minw = size.base_width;
2097 c->minh = size.base_height;
2099 c->minw = c->minh = 0;
2100 if (size.flags & PAspect) {
2101 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2102 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2104 c->maxa = c->mina = 0.0;
2105 c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh);
2111 if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2112 strcpy(stext, "dwm-"VERSION);
2117 updatetitle(Client *c)
2119 if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2120 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2121 if (c->name[0] == '\0') /* hack to mark broken clients */
2122 strcpy(c->name, broken);
2126 updatewindowtype(Client *c)
2128 Atom state = getatomprop(c, netatom[NetWMState]);
2129 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2131 if (state == netatom[NetWMFullscreen])
2132 setfullscreen(c, 1);
2133 if (wtype == netatom[NetWMWindowTypeDialog])
2138 updatewmhints(Client *c)
2142 if ((wmh = XGetWMHints(dpy, c->win))) {
2143 if (c == selmon->sel && wmh->flags & XUrgencyHint) {
2144 wmh->flags &= ~XUrgencyHint;
2145 XSetWMHints(dpy, c->win, wmh);
2147 c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
2148 if (wmh->flags & InputHint)
2149 c->neverfocus = !wmh->input;
2157 view(const Arg *arg)
2159 if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2161 selmon->seltags ^= 1; /* toggle sel tagset */
2162 if (arg->ui & TAGMASK)
2163 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2169 wintoclient(Window w)
2174 for (m = mons; m; m = m->next)
2175 for (c = m->clients; c; c = c->next)
2188 if (w == root && getrootptr(&x, &y))
2189 return recttomon(x, y, 1, 1);
2190 for (m = mons; m; m = m->next)
2193 if ((c = wintoclient(w)))
2198 /* There's no way to check accesses to destroyed windows, thus those cases are
2199 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2200 * default error handler, which may call exit. */
2202 xerror(Display *dpy, XErrorEvent *ee)
2204 if (ee->error_code == BadWindow
2205 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2206 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2207 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2208 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2209 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2210 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2211 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2212 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2214 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2215 ee->request_code, ee->error_code);
2216 return xerrorxlib(dpy, ee); /* may call exit */
2220 xerrordummy(Display *dpy, XErrorEvent *ee)
2225 /* Startup Error handler to check if another window manager
2226 * is already running. */
2228 xerrorstart(Display *dpy, XErrorEvent *ee)
2230 die("dwm: another window manager is already running");
2235 zoom(const Arg *arg)
2237 Client *c = selmon->sel;
2239 if (!selmon->lt[selmon->sellt]->arrange
2240 || (selmon->sel && selmon->sel->isfloating))
2242 if (c == nexttiled(selmon->clients))
2243 if (!c || !(c = nexttiled(c->next)))
2249 resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst)
2263 snprintf(fullname, sizeof(fullname), "%s.%s", "dwm", name);
2264 fullname[sizeof(fullname) - 1] = '\0';
2266 XrmGetResource(db, fullname, "*", &type, &ret);
2267 if (!(ret.addr == NULL || strncmp("String", type, 64)))
2271 strcpy(sdst, ret.addr);
2274 *idst = strtoul(ret.addr, NULL, 10);
2277 *fdst = strtof(ret.addr, NULL);
2284 load_xresources(void)
2291 display = XOpenDisplay(NULL);
2292 resm = XResourceManagerString(display);
2296 db = XrmGetStringDatabase(resm);
2297 for (p = resources; p < resources + LENGTH(resources); p++)
2298 resource_load(db, p->name, p->type, p->dst);
2299 XCloseDisplay(display);
2303 main(int argc, char *argv[])
2305 if (argc == 2 && !strcmp("-v", argv[1]))
2308 die("usage: dwm [-v]");
2309 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2310 fputs("warning: no locale support\n", stderr);
2311 if (!(dpy = XOpenDisplay(NULL)))
2312 die("dwm: cannot open display");
2318 if (pledge("stdio rpath proc exec", NULL) == -1)
2320 #endif /* __OpenBSD__ */
2326 return EXIT_SUCCESS;