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 (*xerrorxlib)(Display *, XErrorEvent *);
270 static unsigned int numlockmask = 0;
271 static void (*handler[LASTEvent]) (XEvent *) = {
272 [ButtonPress] = buttonpress,
273 [ClientMessage] = clientmessage,
274 [ConfigureRequest] = configurerequest,
275 [ConfigureNotify] = configurenotify,
276 [DestroyNotify] = destroynotify,
277 [EnterNotify] = enternotify,
280 [KeyPress] = keypress,
281 [MappingNotify] = mappingnotify,
282 [MapRequest] = maprequest,
283 [MotionNotify] = motionnotify,
284 [PropertyNotify] = propertynotify,
285 [UnmapNotify] = unmapnotify
287 static Atom wmatom[WMLast], netatom[NetLast];
288 static int running = 1;
289 static Cur *cursor[CurLast];
293 static Monitor *mons, *selmon;
294 static Window root, wmcheckwin;
296 /* configuration, allows nested code to access above variables */
299 /* compile-time check if all tags fit into an unsigned int bit array. */
300 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
302 /* function implementations */
304 applyrules(Client *c)
306 const char *class, *instance;
310 XClassHint ch = { NULL, NULL };
315 XGetClassHint(dpy, c->win, &ch);
316 class = ch.res_class ? ch.res_class : broken;
317 instance = ch.res_name ? ch.res_name : broken;
319 for (i = 0; i < LENGTH(rules); i++) {
321 if ((!r->title || strstr(c->name, r->title))
322 && (!r->class || strstr(class, r->class))
323 && (!r->instance || strstr(instance, r->instance)))
325 c->isfloating = r->isfloating;
327 for (m = mons; m && m->num != r->monitor; m = m->next);
336 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
340 applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact)
345 /* set minimum possible */
353 if (*x + *w + 2 * c->bw < 0)
355 if (*y + *h + 2 * c->bw < 0)
358 if (*x >= m->wx + m->ww)
359 *x = m->wx + m->ww - WIDTH(c);
360 if (*y >= m->wy + m->wh)
361 *y = m->wy + m->wh - HEIGHT(c);
362 if (*x + *w + 2 * c->bw <= m->wx)
364 if (*y + *h + 2 * c->bw <= m->wy)
371 if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
372 /* see last two sentences in ICCCM 4.1.2.3 */
373 baseismin = c->basew == c->minw && c->baseh == c->minh;
374 if (!baseismin) { /* temporarily remove base dimensions */
378 /* adjust for aspect limits */
379 if (c->mina > 0 && c->maxa > 0) {
380 if (c->maxa < (float)*w / *h)
381 *w = *h * c->maxa + 0.5;
382 else if (c->mina < (float)*h / *w)
383 *h = *w * c->mina + 0.5;
385 if (baseismin) { /* increment calculation requires this */
389 /* adjust for increment value */
394 /* restore base dimensions */
395 *w = MAX(*w + c->basew, c->minw);
396 *h = MAX(*h + c->baseh, c->minh);
398 *w = MIN(*w, c->maxw);
400 *h = MIN(*h, c->maxh);
402 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
410 else for (m = mons; m; m = m->next)
415 } else for (m = mons; m; m = m->next)
420 arrangemon(Monitor *m)
422 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
423 if (m->lt[m->sellt]->arrange)
424 m->lt[m->sellt]->arrange(m);
430 c->next = c->mon->clients;
435 attachstack(Client *c)
437 c->snext = c->mon->stack;
442 buttonpress(XEvent *e)
444 unsigned int i, x, click;
448 XButtonPressedEvent *ev = &e->xbutton;
451 /* focus monitor if necessary */
452 if ((m = wintomon(ev->window)) && m != selmon) {
453 unfocus(selmon->sel, 1);
457 if (ev->window == selmon->barwin) {
461 while (ev->x >= x && ++i < LENGTH(tags));
462 if (i < LENGTH(tags)) {
465 } else if (ev->x < x + blw)
467 else if (ev->x > selmon->ww - TEXTW(stext))
468 click = ClkStatusText;
471 } else if ((c = wintoclient(ev->window))) {
474 XAllowEvents(dpy, ReplayPointer, CurrentTime);
475 click = ClkClientWin;
477 for (i = 0; i < LENGTH(buttons); i++)
478 if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
479 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
480 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
486 xerrorxlib = XSetErrorHandler(xerrorstart);
487 /* this causes an error if some other window manager is running */
488 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
490 XSetErrorHandler(xerror);
498 Layout foo = { "", NULL };
503 selmon->lt[selmon->sellt] = &foo;
504 for (m = mons; m; m = m->next)
506 unmanage(m->stack, 0);
507 XUngrabKey(dpy, AnyKey, AnyModifier, root);
510 for (i = 0; i < CurLast; i++)
511 drw_cur_free(drw, cursor[i]);
512 for (i = 0; i < LENGTH(colors); i++)
514 XDestroyWindow(dpy, wmcheckwin);
517 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
518 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
522 cleanupmon(Monitor *mon)
529 for (m = mons; m && m->next != mon; m = m->next);
532 XUnmapWindow(dpy, mon->barwin);
533 XDestroyWindow(dpy, mon->barwin);
538 clientmessage(XEvent *e)
540 XClientMessageEvent *cme = &e->xclient;
541 Client *c = wintoclient(cme->window);
545 if (cme->message_type == netatom[NetWMState]) {
546 if (cme->data.l[1] == netatom[NetWMFullscreen]
547 || cme->data.l[2] == netatom[NetWMFullscreen])
548 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
549 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
550 } else if (cme->message_type == netatom[NetActiveWindow]) {
551 if (c != selmon->sel && !c->isurgent)
561 ce.type = ConfigureNotify;
569 ce.border_width = c->bw;
571 ce.override_redirect = False;
572 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
576 configurenotify(XEvent *e)
580 XConfigureEvent *ev = &e->xconfigure;
583 /* TODO: updategeom handling sucks, needs to be simplified */
584 if (ev->window == root) {
585 dirty = (sw != ev->width || sh != ev->height);
588 if (updategeom() || dirty) {
589 drw_resize(drw, sw, bh);
591 for (m = mons; m; m = m->next) {
592 for (c = m->clients; c; c = c->next)
594 resizeclient(c, m->mx, m->my, m->mw, m->mh);
595 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
604 configurerequest(XEvent *e)
608 XConfigureRequestEvent *ev = &e->xconfigurerequest;
611 if ((c = wintoclient(ev->window))) {
612 if (ev->value_mask & CWBorderWidth)
613 c->bw = ev->border_width;
614 else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
616 if (ev->value_mask & CWX) {
618 c->x = m->mx + ev->x;
620 if (ev->value_mask & CWY) {
622 c->y = m->my + ev->y;
624 if (ev->value_mask & CWWidth) {
628 if (ev->value_mask & CWHeight) {
632 if ((c->x + c->w) > m->mx + m->mw && c->isfloating)
633 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
634 if ((c->y + c->h) > m->my + m->mh && c->isfloating)
635 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
636 if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
639 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
645 wc.width = ev->width;
646 wc.height = ev->height;
647 wc.border_width = ev->border_width;
648 wc.sibling = ev->above;
649 wc.stack_mode = ev->detail;
650 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
660 m = ecalloc(1, sizeof(Monitor));
661 m->tagset[0] = m->tagset[1] = 1;
663 m->nmaster = nmaster;
664 m->showbar = showbar;
667 m->lt[0] = &layouts[0];
668 m->lt[1] = &layouts[1 % LENGTH(layouts)];
669 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
674 destroynotify(XEvent *e)
677 XDestroyWindowEvent *ev = &e->xdestroywindow;
679 if ((c = wintoclient(ev->window)))
688 for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
693 detachstack(Client *c)
697 for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
700 if (c == c->mon->sel) {
701 for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
712 if (!(m = selmon->next))
714 } else if (selmon == mons)
715 for (m = mons; m->next; m = m->next);
717 for (m = mons; m->next != selmon; m = m->next);
725 int boxs = drw->fonts->h / 9;
726 int boxw = drw->fonts->h / 6 + 2;
727 unsigned int i, occ = 0, urg = 0;
730 /* draw status first so it can be overdrawn by tags later */
731 if (m == selmon) { /* status is only drawn on selected monitor */
732 drw_setscheme(drw, scheme[SchemeNorm]);
733 tw = TEXTW(stext) - lrpad + 2; /* 2px right padding */
734 drw_text(drw, m->ww - tw, 0, tw, bh, 0, stext, 0);
737 for (c = m->clients; c; c = c->next) {
743 for (i = 0; i < LENGTH(tags); i++) {
745 drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]);
746 drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i);
748 drw_rect(drw, x + boxs, boxs, boxw, boxw,
749 m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
753 w = blw = TEXTW(m->ltsymbol);
754 drw_setscheme(drw, scheme[SchemeNorm]);
755 x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0);
757 if ((w = m->ww - tw - x) > bh) {
759 drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]);
760 drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->name, 0);
761 if (m->sel->isfloating)
762 drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
764 drw_setscheme(drw, scheme[SchemeNorm]);
765 drw_rect(drw, x, 0, w, bh, 1, 1);
768 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
776 for (m = mons; m; m = m->next)
781 enternotify(XEvent *e)
785 XCrossingEvent *ev = &e->xcrossing;
787 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
789 c = wintoclient(ev->window);
790 m = c ? c->mon : wintomon(ev->window);
792 unfocus(selmon->sel, 1);
794 } else if (!c || c == selmon->sel)
803 XExposeEvent *ev = &e->xexpose;
805 if (ev->count == 0 && (m = wintomon(ev->window)))
812 if (!c || !ISVISIBLE(c))
813 for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
814 if (selmon->sel && selmon->sel != c)
815 unfocus(selmon->sel, 0);
817 if (c->mon != selmon)
824 XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel);
827 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
828 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
834 /* there are some broken focus acquiring clients needing extra handling */
838 XFocusChangeEvent *ev = &e->xfocus;
840 if (selmon->sel && ev->window != selmon->sel->win)
841 setfocus(selmon->sel);
845 focusmon(const Arg *arg)
851 if ((m = dirtomon(arg->i)) == selmon)
853 unfocus(selmon->sel, 0);
859 focusstack(const Arg *arg)
861 Client *c = NULL, *i;
866 for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
868 for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
870 for (i = selmon->clients; i != selmon->sel; i = i->next)
874 for (; i; i = i->next)
885 getatomprop(Client *c, Atom prop)
889 unsigned char *p = NULL;
890 Atom da, atom = None;
892 if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
893 &da, &di, &dl, &dl, &p) == Success && p) {
901 getrootptr(int *x, int *y)
907 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
915 unsigned char *p = NULL;
916 unsigned long n, extra;
919 if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
920 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
929 gettextprop(Window w, Atom atom, char *text, unsigned int size)
935 if (!text || size == 0)
938 if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems)
940 if (name.encoding == XA_STRING)
941 strncpy(text, (char *)name.value, size - 1);
943 if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
944 strncpy(text, *list, size - 1);
945 XFreeStringList(list);
948 text[size - 1] = '\0';
954 grabbuttons(Client *c, int focused)
959 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
960 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
962 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
963 BUTTONMASK, GrabModeSync, GrabModeSync, None, None);
964 for (i = 0; i < LENGTH(buttons); i++)
965 if (buttons[i].click == ClkClientWin)
966 for (j = 0; j < LENGTH(modifiers); j++)
967 XGrabButton(dpy, buttons[i].button,
968 buttons[i].mask | modifiers[j],
969 c->win, False, BUTTONMASK,
970 GrabModeAsync, GrabModeSync, None, None);
980 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
983 XUngrabKey(dpy, AnyKey, AnyModifier, root);
984 for (i = 0; i < LENGTH(keys); i++)
985 if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
986 for (j = 0; j < LENGTH(modifiers); j++)
987 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
988 True, GrabModeAsync, GrabModeAsync);
993 incnmaster(const Arg *arg)
995 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
1001 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
1004 if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1005 && unique[n].width == info->width && unique[n].height == info->height)
1009 #endif /* XINERAMA */
1019 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1020 for (i = 0; i < LENGTH(keys); i++)
1021 if (keysym == keys[i].keysym
1022 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1024 keys[i].func(&(keys[i].arg));
1028 killclient(const Arg *arg)
1032 if (!sendevent(selmon->sel, wmatom[WMDelete])) {
1034 XSetErrorHandler(xerrordummy);
1035 XSetCloseDownMode(dpy, DestroyAll);
1036 XKillClient(dpy, selmon->sel->win);
1038 XSetErrorHandler(xerror);
1044 manage(Window w, XWindowAttributes *wa)
1046 Client *c, *t = NULL;
1047 Window trans = None;
1050 c = ecalloc(1, sizeof(Client));
1053 c->x = c->oldx = wa->x;
1054 c->y = c->oldy = wa->y;
1055 c->w = c->oldw = wa->width;
1056 c->h = c->oldh = wa->height;
1057 c->oldbw = wa->border_width;
1060 if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1068 if (c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1069 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1070 if (c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1071 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1072 c->x = MAX(c->x, c->mon->mx);
1073 /* only fix client y-offset, if the client center might cover the bar */
1074 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1075 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1078 wc.border_width = c->bw;
1079 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1080 XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel);
1081 configure(c); /* propagates border_width, if size doesn't change */
1082 updatewindowtype(c);
1085 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1088 c->isfloating = c->oldstate = trans != None || c->isfixed;
1090 XRaiseWindow(dpy, c->win);
1093 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1094 (unsigned char *) &(c->win), 1);
1095 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1096 setclientstate(c, NormalState);
1097 if (c->mon == selmon)
1098 unfocus(selmon->sel, 0);
1101 XMapWindow(dpy, c->win);
1106 mappingnotify(XEvent *e)
1108 XMappingEvent *ev = &e->xmapping;
1110 XRefreshKeyboardMapping(ev);
1111 if (ev->request == MappingKeyboard)
1116 maprequest(XEvent *e)
1118 static XWindowAttributes wa;
1119 XMapRequestEvent *ev = &e->xmaprequest;
1121 if (!XGetWindowAttributes(dpy, ev->window, &wa))
1123 if (wa.override_redirect)
1125 if (!wintoclient(ev->window))
1126 manage(ev->window, &wa);
1135 for (c = m->clients; c; c = c->next)
1138 if (n > 0) /* override layout symbol */
1139 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1140 for (c = nexttiled(m->clients); c; c = nexttiled(c->next))
1141 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
1145 motionnotify(XEvent *e)
1147 static Monitor *mon = NULL;
1149 XMotionEvent *ev = &e->xmotion;
1151 if (ev->window != root)
1153 if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1154 unfocus(selmon->sel, 1);
1162 movemouse(const Arg *arg)
1164 int x, y, ocx, ocy, nx, ny;
1170 if (!(c = selmon->sel))
1172 if (c->isfullscreen) /* no support moving fullscreen windows by mouse */
1177 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1178 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1180 if (!getrootptr(&x, &y))
1183 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1185 case ConfigureRequest:
1188 handler[ev.type](&ev);
1191 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1193 lasttime = ev.xmotion.time;
1195 nx = ocx + (ev.xmotion.x - x);
1196 ny = ocy + (ev.xmotion.y - y);
1197 if (abs(selmon->wx - nx) < snap)
1199 else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1200 nx = selmon->wx + selmon->ww - WIDTH(c);
1201 if (abs(selmon->wy - ny) < snap)
1203 else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1204 ny = selmon->wy + selmon->wh - HEIGHT(c);
1205 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1206 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1207 togglefloating(NULL);
1208 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1209 resize(c, nx, ny, c->w, c->h, 1);
1212 } while (ev.type != ButtonRelease);
1213 XUngrabPointer(dpy, CurrentTime);
1214 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1222 nexttiled(Client *c)
1224 for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1238 propertynotify(XEvent *e)
1242 XPropertyEvent *ev = &e->xproperty;
1244 if ((ev->window == root) && (ev->atom == XA_WM_NAME))
1246 else if (ev->state == PropertyDelete)
1247 return; /* ignore */
1248 else if ((c = wintoclient(ev->window))) {
1251 case XA_WM_TRANSIENT_FOR:
1252 if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1253 (c->isfloating = (wintoclient(trans)) != NULL))
1256 case XA_WM_NORMAL_HINTS:
1264 if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1266 if (c == c->mon->sel)
1269 if (ev->atom == netatom[NetWMWindowType])
1270 updatewindowtype(c);
1275 quit(const Arg *arg)
1281 recttomon(int x, int y, int w, int h)
1283 Monitor *m, *r = selmon;
1286 for (m = mons; m; m = m->next)
1287 if ((a = INTERSECT(x, y, w, h, m)) > area) {
1295 resize(Client *c, int x, int y, int w, int h, int interact)
1297 if (applysizehints(c, &x, &y, &w, &h, interact))
1298 resizeclient(c, x, y, w, h);
1302 resizeclient(Client *c, int x, int y, int w, int h)
1306 c->oldx = c->x; c->x = wc.x = x;
1307 c->oldy = c->y; c->y = wc.y = y;
1308 c->oldw = c->w; c->w = wc.width = w;
1309 c->oldh = c->h; c->h = wc.height = h;
1310 wc.border_width = c->bw;
1311 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1317 resizemouse(const Arg *arg)
1319 int ocx, ocy, nw, nh;
1325 if (!(c = selmon->sel))
1327 if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1332 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1333 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1335 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1337 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1339 case ConfigureRequest:
1342 handler[ev.type](&ev);
1345 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1347 lasttime = ev.xmotion.time;
1349 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1350 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1351 if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1352 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1354 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1355 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1356 togglefloating(NULL);
1358 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1359 resize(c, c->x, c->y, nw, nh, 1);
1362 } while (ev.type != ButtonRelease);
1363 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1364 XUngrabPointer(dpy, CurrentTime);
1365 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1366 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1383 if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
1384 XRaiseWindow(dpy, m->sel->win);
1385 if (m->lt[m->sellt]->arrange) {
1386 wc.stack_mode = Below;
1387 wc.sibling = m->barwin;
1388 for (c = m->stack; c; c = c->snext)
1389 if (!c->isfloating && ISVISIBLE(c)) {
1390 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1391 wc.sibling = c->win;
1395 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1402 /* main event loop */
1404 while (running && !XNextEvent(dpy, &ev))
1405 if (handler[ev.type])
1406 handler[ev.type](&ev); /* call handler */
1418 if ((home = getenv("HOME")) == NULL)
1419 /* this is almost impossible */
1422 /* if $XDG_DATA_HOME is set and not empty, use $XDG_DATA_HOME/dwm,
1423 * otherwise use ~/.local/share/dwm as autostart script directory
1425 xdgdatahome = getenv("XDG_DATA_HOME");
1426 if (xdgdatahome != NULL && *xdgdatahome != '\0') {
1427 /* space for path segments, separators and nul */
1428 pathpfx = ecalloc(1, strlen(xdgdatahome) + strlen(dwmdir) + 2);
1430 if (sprintf(pathpfx, "%s/%s", xdgdatahome, dwmdir) <= 0) {
1435 /* space for path segments, separators and nul */
1436 pathpfx = ecalloc(1, strlen(home) + strlen(localshare)
1437 + strlen(dwmdir) + 3);
1439 if (sprintf(pathpfx, "%s/%s/%s", home, localshare, dwmdir) < 0) {
1445 /* check if the autostart script directory exists */
1446 if (! (stat(pathpfx, &sb) == 0 && S_ISDIR(sb.st_mode))) {
1447 /* the XDG conformant path does not exist or is no directory
1448 * so we try ~/.dwm instead
1450 char *pathpfx_new = realloc(pathpfx, strlen(home) + strlen(dwmdir) + 3);
1451 if(pathpfx_new == NULL) {
1455 pathpfx = pathpfx_new;
1457 if (sprintf(pathpfx, "%s/.%s", home, dwmdir) <= 0) {
1463 /* try the blocking script first */
1464 path = ecalloc(1, strlen(pathpfx) + strlen(autostartblocksh) + 2);
1465 if (sprintf(path, "%s/%s", pathpfx, autostartblocksh) <= 0) {
1470 if (access(path, X_OK) == 0)
1473 /* now the non-blocking script */
1474 if (sprintf(path, "%s/%s", pathpfx, autostartsh) <= 0) {
1479 if (access(path, X_OK) == 0)
1480 system(strcat(path, " &"));
1489 unsigned int i, num;
1490 Window d1, d2, *wins = NULL;
1491 XWindowAttributes wa;
1493 if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1494 for (i = 0; i < num; i++) {
1495 if (!XGetWindowAttributes(dpy, wins[i], &wa)
1496 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1498 if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1499 manage(wins[i], &wa);
1501 for (i = 0; i < num; i++) { /* now the transients */
1502 if (!XGetWindowAttributes(dpy, wins[i], &wa))
1504 if (XGetTransientForHint(dpy, wins[i], &d1)
1505 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1506 manage(wins[i], &wa);
1514 sendmon(Client *c, Monitor *m)
1522 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1530 setclientstate(Client *c, long state)
1532 long data[] = { state, None };
1534 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1535 PropModeReplace, (unsigned char *)data, 2);
1539 sendevent(Client *c, Atom proto)
1546 if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1547 while (!exists && n--)
1548 exists = protocols[n] == proto;
1552 ev.type = ClientMessage;
1553 ev.xclient.window = c->win;
1554 ev.xclient.message_type = wmatom[WMProtocols];
1555 ev.xclient.format = 32;
1556 ev.xclient.data.l[0] = proto;
1557 ev.xclient.data.l[1] = CurrentTime;
1558 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1566 if (!c->neverfocus) {
1567 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1568 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1569 XA_WINDOW, 32, PropModeReplace,
1570 (unsigned char *) &(c->win), 1);
1572 sendevent(c, wmatom[WMTakeFocus]);
1576 setfullscreen(Client *c, int fullscreen)
1578 if (fullscreen && !c->isfullscreen) {
1579 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1580 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1581 c->isfullscreen = 1;
1582 c->oldstate = c->isfloating;
1586 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1587 XRaiseWindow(dpy, c->win);
1588 } else if (!fullscreen && c->isfullscreen){
1589 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1590 PropModeReplace, (unsigned char*)0, 0);
1591 c->isfullscreen = 0;
1592 c->isfloating = c->oldstate;
1598 resizeclient(c, c->x, c->y, c->w, c->h);
1604 setgaps(const Arg *arg)
1606 if ((arg->i == 0) || (selmon->gappx + arg->i < 0))
1609 selmon->gappx += arg->i;
1614 setlayout(const Arg *arg)
1616 if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1619 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1620 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1627 /* arg > 1.0 will set mfact absolutely */
1629 setmfact(const Arg *arg)
1633 if (!arg || !selmon->lt[selmon->sellt]->arrange)
1635 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1636 if (f < 0.05 || f > 0.95)
1646 XSetWindowAttributes wa;
1649 /* clean up any zombies immediately */
1653 screen = DefaultScreen(dpy);
1654 sw = DisplayWidth(dpy, screen);
1655 sh = DisplayHeight(dpy, screen);
1656 root = RootWindow(dpy, screen);
1657 drw = drw_create(dpy, screen, root, sw, sh);
1658 if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
1659 die("no fonts could be loaded.");
1660 lrpad = drw->fonts->h;
1661 bh = drw->fonts->h + 2;
1664 utf8string = XInternAtom(dpy, "UTF8_STRING", False);
1665 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1666 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1667 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1668 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1669 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1670 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1671 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1672 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1673 netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
1674 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1675 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1676 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1677 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1679 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1680 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1681 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1682 /* init appearance */
1683 scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
1684 for (i = 0; i < LENGTH(colors); i++)
1685 scheme[i] = drw_scm_create(drw, colors[i], 3);
1689 /* supporting window for NetWMCheck */
1690 wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
1691 XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
1692 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1693 XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8,
1694 PropModeReplace, (unsigned char *) "dwm", 3);
1695 XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32,
1696 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1697 /* EWMH support per view */
1698 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1699 PropModeReplace, (unsigned char *) netatom, NetLast);
1700 XDeleteProperty(dpy, root, netatom[NetClientList]);
1702 wa.cursor = cursor[CurNormal]->cursor;
1703 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
1704 |ButtonPressMask|PointerMotionMask|EnterWindowMask
1705 |LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1706 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1707 XSelectInput(dpy, root, wa.event_mask);
1714 seturgent(Client *c, int urg)
1719 if (!(wmh = XGetWMHints(dpy, c->win)))
1721 wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
1722 XSetWMHints(dpy, c->win, wmh);
1732 /* show clients top down */
1733 XMoveWindow(dpy, c->win, c->x, c->y);
1734 if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1735 resize(c, c->x, c->y, c->w, c->h, 0);
1738 /* hide clients bottom up */
1740 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1747 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1748 die("can't install SIGCHLD handler:");
1749 while (0 < waitpid(-1, NULL, WNOHANG));
1753 spawn(const Arg *arg)
1755 if (arg->v == dmenucmd)
1756 dmenumon[0] = '0' + selmon->num;
1759 close(ConnectionNumber(dpy));
1761 execvp(((char **)arg->v)[0], (char **)arg->v);
1762 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1771 if (selmon->sel && arg->ui & TAGMASK) {
1772 selmon->sel->tags = arg->ui & TAGMASK;
1779 tagmon(const Arg *arg)
1781 if (!selmon->sel || !mons->next)
1783 sendmon(selmon->sel, dirtomon(arg->i));
1789 unsigned int i, n, h, mw, my, ty;
1792 for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1797 mw = m->nmaster ? m->ww * m->mfact : 0;
1799 mw = m->ww - m->gappx;
1800 for (i = 0, my = ty = m->gappx, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1801 if (i < m->nmaster) {
1802 h = (m->wh - my) / (MIN(n, m->nmaster) - i) - m->gappx;
1803 resize(c, m->wx + m->gappx, m->wy + my, mw - (2*c->bw) - m->gappx, h - (2*c->bw), 0);
1804 if (my + HEIGHT(c) + m->gappx < m->wh)
1805 my += HEIGHT(c) + m->gappx;
1807 h = (m->wh - ty) / (n - i) - m->gappx;
1808 resize(c, m->wx + mw + m->gappx, m->wy + ty, m->ww - mw - (2*c->bw) - 2*m->gappx, h - (2*c->bw), 0);
1809 if (ty + HEIGHT(c) + m->gappx < m->wh)
1810 ty += HEIGHT(c) + m->gappx;
1815 togglebar(const Arg *arg)
1817 selmon->showbar = !selmon->showbar;
1818 updatebarpos(selmon);
1819 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1824 togglefloating(const Arg *arg)
1828 if (selmon->sel->isfullscreen) /* no support for fullscreen windows */
1830 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1831 if (selmon->sel->isfloating)
1832 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1833 selmon->sel->w, selmon->sel->h, 0);
1838 toggletag(const Arg *arg)
1840 unsigned int newtags;
1844 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1846 selmon->sel->tags = newtags;
1853 toggleview(const Arg *arg)
1855 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1858 selmon->tagset[selmon->seltags] = newtagset;
1865 unfocus(Client *c, int setfocus)
1870 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
1872 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1873 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1878 unmanage(Client *c, int destroyed)
1880 Monitor *m = c->mon;
1886 wc.border_width = c->oldbw;
1887 XGrabServer(dpy); /* avoid race conditions */
1888 XSetErrorHandler(xerrordummy);
1889 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1890 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1891 setclientstate(c, WithdrawnState);
1893 XSetErrorHandler(xerror);
1903 unmapnotify(XEvent *e)
1906 XUnmapEvent *ev = &e->xunmap;
1908 if ((c = wintoclient(ev->window))) {
1910 setclientstate(c, WithdrawnState);
1920 XSetWindowAttributes wa = {
1921 .override_redirect = True,
1922 .background_pixmap = ParentRelative,
1923 .event_mask = ButtonPressMask|ExposureMask
1925 XClassHint ch = {"dwm", "dwm"};
1926 for (m = mons; m; m = m->next) {
1929 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1930 CopyFromParent, DefaultVisual(dpy, screen),
1931 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1932 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
1933 XMapRaised(dpy, m->barwin);
1934 XSetClassHint(dpy, m->barwin, &ch);
1939 updatebarpos(Monitor *m)
1945 m->by = m->topbar ? m->wy : m->wy + m->wh;
1946 m->wy = m->topbar ? m->wy + bh : m->wy;
1957 XDeleteProperty(dpy, root, netatom[NetClientList]);
1958 for (m = mons; m; m = m->next)
1959 for (c = m->clients; c; c = c->next)
1960 XChangeProperty(dpy, root, netatom[NetClientList],
1961 XA_WINDOW, 32, PropModeAppend,
1962 (unsigned char *) &(c->win), 1);
1971 if (XineramaIsActive(dpy)) {
1975 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1976 XineramaScreenInfo *unique = NULL;
1978 for (n = 0, m = mons; m; m = m->next, n++);
1979 /* only consider unique geometries as separate screens */
1980 unique = ecalloc(nn, sizeof(XineramaScreenInfo));
1981 for (i = 0, j = 0; i < nn; i++)
1982 if (isuniquegeom(unique, j, &info[i]))
1983 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1986 if (n <= nn) { /* new monitors available */
1987 for (i = 0; i < (nn - n); i++) {
1988 for (m = mons; m && m->next; m = m->next);
1990 m->next = createmon();
1994 for (i = 0, m = mons; i < nn && m; m = m->next, i++)
1996 || unique[i].x_org != m->mx || unique[i].y_org != m->my
1997 || unique[i].width != m->mw || unique[i].height != m->mh)
2001 m->mx = m->wx = unique[i].x_org;
2002 m->my = m->wy = unique[i].y_org;
2003 m->mw = m->ww = unique[i].width;
2004 m->mh = m->wh = unique[i].height;
2007 } else { /* less monitors available nn < n */
2008 for (i = nn; i < n; i++) {
2009 for (m = mons; m && m->next; m = m->next);
2010 while ((c = m->clients)) {
2012 m->clients = c->next;
2025 #endif /* XINERAMA */
2026 { /* default monitor setup */
2029 if (mons->mw != sw || mons->mh != sh) {
2031 mons->mw = mons->ww = sw;
2032 mons->mh = mons->wh = sh;
2038 selmon = wintomon(root);
2044 updatenumlockmask(void)
2047 XModifierKeymap *modmap;
2050 modmap = XGetModifierMapping(dpy);
2051 for (i = 0; i < 8; i++)
2052 for (j = 0; j < modmap->max_keypermod; j++)
2053 if (modmap->modifiermap[i * modmap->max_keypermod + j]
2054 == XKeysymToKeycode(dpy, XK_Num_Lock))
2055 numlockmask = (1 << i);
2056 XFreeModifiermap(modmap);
2060 updatesizehints(Client *c)
2065 if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
2066 /* size is uninitialized, ensure that size.flags aren't used */
2068 if (size.flags & PBaseSize) {
2069 c->basew = size.base_width;
2070 c->baseh = size.base_height;
2071 } else if (size.flags & PMinSize) {
2072 c->basew = size.min_width;
2073 c->baseh = size.min_height;
2075 c->basew = c->baseh = 0;
2076 if (size.flags & PResizeInc) {
2077 c->incw = size.width_inc;
2078 c->inch = size.height_inc;
2080 c->incw = c->inch = 0;
2081 if (size.flags & PMaxSize) {
2082 c->maxw = size.max_width;
2083 c->maxh = size.max_height;
2085 c->maxw = c->maxh = 0;
2086 if (size.flags & PMinSize) {
2087 c->minw = size.min_width;
2088 c->minh = size.min_height;
2089 } else if (size.flags & PBaseSize) {
2090 c->minw = size.base_width;
2091 c->minh = size.base_height;
2093 c->minw = c->minh = 0;
2094 if (size.flags & PAspect) {
2095 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2096 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2098 c->maxa = c->mina = 0.0;
2099 c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh);
2105 if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2106 strcpy(stext, "dwm-"VERSION);
2111 updatetitle(Client *c)
2113 if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2114 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2115 if (c->name[0] == '\0') /* hack to mark broken clients */
2116 strcpy(c->name, broken);
2120 updatewindowtype(Client *c)
2122 Atom state = getatomprop(c, netatom[NetWMState]);
2123 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2125 if (state == netatom[NetWMFullscreen])
2126 setfullscreen(c, 1);
2127 if (wtype == netatom[NetWMWindowTypeDialog])
2132 updatewmhints(Client *c)
2136 if ((wmh = XGetWMHints(dpy, c->win))) {
2137 if (c == selmon->sel && wmh->flags & XUrgencyHint) {
2138 wmh->flags &= ~XUrgencyHint;
2139 XSetWMHints(dpy, c->win, wmh);
2141 c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
2142 if (wmh->flags & InputHint)
2143 c->neverfocus = !wmh->input;
2151 view(const Arg *arg)
2153 if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2155 selmon->seltags ^= 1; /* toggle sel tagset */
2156 if (arg->ui & TAGMASK)
2157 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2163 wintoclient(Window w)
2168 for (m = mons; m; m = m->next)
2169 for (c = m->clients; c; c = c->next)
2182 if (w == root && getrootptr(&x, &y))
2183 return recttomon(x, y, 1, 1);
2184 for (m = mons; m; m = m->next)
2187 if ((c = wintoclient(w)))
2192 /* There's no way to check accesses to destroyed windows, thus those cases are
2193 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2194 * default error handler, which may call exit. */
2196 xerror(Display *dpy, XErrorEvent *ee)
2198 if (ee->error_code == BadWindow
2199 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2200 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2201 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2202 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2203 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2204 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2205 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2206 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2208 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2209 ee->request_code, ee->error_code);
2210 return xerrorxlib(dpy, ee); /* may call exit */
2214 xerrordummy(Display *dpy, XErrorEvent *ee)
2219 /* Startup Error handler to check if another window manager
2220 * is already running. */
2222 xerrorstart(Display *dpy, XErrorEvent *ee)
2224 die("dwm: another window manager is already running");
2229 zoom(const Arg *arg)
2231 Client *c = selmon->sel;
2233 if (!selmon->lt[selmon->sellt]->arrange
2234 || (selmon->sel && selmon->sel->isfloating))
2236 if (c == nexttiled(selmon->clients))
2237 if (!c || !(c = nexttiled(c->next)))
2243 resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst)
2257 snprintf(fullname, sizeof(fullname), "%s.%s", "dwm", name);
2258 fullname[sizeof(fullname) - 1] = '\0';
2260 XrmGetResource(db, fullname, "*", &type, &ret);
2261 if (!(ret.addr == NULL || strncmp("String", type, 64)))
2265 strcpy(sdst, ret.addr);
2268 *idst = strtoul(ret.addr, NULL, 10);
2271 *fdst = strtof(ret.addr, NULL);
2278 load_xresources(void)
2285 display = XOpenDisplay(NULL);
2286 resm = XResourceManagerString(display);
2290 db = XrmGetStringDatabase(resm);
2291 for (p = resources; p < resources + LENGTH(resources); p++)
2292 resource_load(db, p->name, p->type, p->dst);
2293 XCloseDisplay(display);
2297 main(int argc, char *argv[])
2299 if (argc == 2 && !strcmp("-v", argv[1]))
2302 die("usage: dwm [-v]");
2303 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2304 fputs("warning: no locale support\n", stderr);
2305 if (!(dpy = XOpenDisplay(NULL)))
2306 die("dwm: cannot open display");
2312 if (pledge("stdio rpath proc exec", NULL) == -1)
2314 #endif /* __OpenBSD__ */
2320 return EXIT_SUCCESS;