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>
33 #include <X11/cursorfont.h>
34 #include <X11/keysym.h>
35 #include <X11/Xatom.h>
37 #include <X11/Xproto.h>
38 #include <X11/Xutil.h>
40 #include <X11/extensions/Xinerama.h>
42 #include <X11/Xft/Xft.h>
43 #include <X11/XF86keysym.h>
49 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
50 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
51 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
52 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
53 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
54 #define LENGTH(X) (sizeof X / sizeof X[0])
55 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
56 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
57 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
58 #define TAGMASK ((1 << LENGTH(tags)) - 1)
59 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
62 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
63 enum { SchemeNorm, SchemeSel }; /* color schemes */
64 enum { NetSupported, NetWMName, NetWMState, NetWMCheck,
65 NetWMFullscreen, NetActiveWindow, NetWMWindowType,
66 NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */
67 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
68 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
69 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
82 void (*func)(const Arg *arg);
86 typedef struct Monitor Monitor;
87 typedef struct Client Client;
92 int oldx, oldy, oldw, oldh;
93 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
96 int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
106 void (*func)(const Arg *);
112 void (*arrange)(Monitor *);
115 typedef struct Pertag Pertag;
121 int by; /* bar geometry */
122 int mx, my, mw, mh; /* screen size */
123 int wx, wy, ww, wh; /* window area */
124 unsigned int seltags;
126 unsigned int tagset[2];
140 const char *instance;
147 /* function declarations */
148 static void applyrules(Client *c);
149 static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact);
150 static void arrange(Monitor *m);
151 static void arrangemon(Monitor *m);
152 static void attach(Client *c);
153 static void attachbottom(Client *c);
154 static void attachstack(Client *c);
155 static void buttonpress(XEvent *e);
156 static void checkotherwm(void);
157 static void cleanup(void);
158 static void cleanupmon(Monitor *mon);
159 static void clientmessage(XEvent *e);
160 static void configure(Client *c);
161 static void configurenotify(XEvent *e);
162 static void configurerequest(XEvent *e);
163 static Monitor *createmon(void);
164 static void destroynotify(XEvent *e);
165 static void detach(Client *c);
166 static void detachstack(Client *c);
167 static Monitor *dirtomon(int dir);
168 static void drawbar(Monitor *m);
169 static void drawbars(void);
170 static void enternotify(XEvent *e);
171 static void expose(XEvent *e);
172 static Client *findbefore(Client *c);
173 static void focus(Client *c);
174 static void focusin(XEvent *e);
175 static void focusmon(const Arg *arg);
176 static void focusstack(const Arg *arg);
177 static Atom getatomprop(Client *c, Atom prop);
178 static int getrootptr(int *x, int *y);
179 static long getstate(Window w);
180 static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
181 static void grabbuttons(Client *c, int focused);
182 static void grabkeys(void);
183 static void incnmaster(const Arg *arg);
184 static void keypress(XEvent *e);
185 static void killclient(const Arg *arg);
186 static void manage(Window w, XWindowAttributes *wa);
187 static void mappingnotify(XEvent *e);
188 static void maprequest(XEvent *e);
189 static void monocle(Monitor *m);
190 static void motionnotify(XEvent *e);
191 static void movemouse(const Arg *arg);
192 static Client *nexttiled(Client *c);
193 static Client *prevtiled(Client *c);
194 static void propertynotify(XEvent *e);
195 static void pushdown(const Arg *arg);
196 static void pushup(const Arg *arg);
197 static void quit(const Arg *arg);
198 static Monitor *recttomon(int x, int y, int w, int h);
199 static void resize(Client *c, int x, int y, int w, int h, int interact);
200 static void resizeclient(Client *c, int x, int y, int w, int h);
201 static void resizemouse(const Arg *arg);
202 static void restack(Monitor *m);
203 static void run(void);
204 static void scan(void);
205 static int sendevent(Client *c, Atom proto);
206 static void sendmon(Client *c, Monitor *m);
207 static void setclientstate(Client *c, long state);
208 static void setfocus(Client *c);
209 static void setfullscreen(Client *c, int fullscreen);
210 static void setlayout(const Arg *arg);
211 static void setmfact(const Arg *arg);
212 static void setup(void);
213 static void seturgent(Client *c, int urg);
214 static void showhide(Client *c);
215 static void sigchld(int unused);
216 static void spawn(const Arg *arg);
217 static void tag(const Arg *arg);
218 static void tagmon(const Arg *arg);
219 static void tile(Monitor *);
220 static void togglebar(const Arg *arg);
221 static void togglefloating(const Arg *arg);
222 static void toggletag(const Arg *arg);
223 static void toggleview(const Arg *arg);
224 static void unfocus(Client *c, int setfocus);
225 static void unmanage(Client *c, int destroyed);
226 static void unmapnotify(XEvent *e);
227 static void updatebarpos(Monitor *m);
228 static void updatebars(void);
229 static void updateclientlist(void);
230 static int updategeom(void);
231 static void updatenumlockmask(void);
232 static void updatesizehints(Client *c);
233 static void updatestatus(void);
234 static void updatetitle(Client *c);
235 static void updatewindowtype(Client *c);
236 static void updatewmhints(Client *c);
237 static void view(const Arg *arg);
238 static Client *wintoclient(Window w);
239 static Monitor *wintomon(Window w);
240 static int xerror(Display *dpy, XErrorEvent *ee);
241 static int xerrordummy(Display *dpy, XErrorEvent *ee);
242 static int xerrorstart(Display *dpy, XErrorEvent *ee);
243 static void zoom(const Arg *arg);
246 static Client *prevzoom = NULL;
247 static const char broken[] = "broken";
248 static char stext[256];
250 static int sw, sh; /* X display screen geometry width, height */
251 static int bh, blw = 0; /* bar geometry */
252 static int lrpad; /* sum of left and right padding for text */
253 static int (*xerrorxlib)(Display *, XErrorEvent *);
254 static unsigned int numlockmask = 0;
255 static void (*handler[LASTEvent]) (XEvent *) = {
256 [ButtonPress] = buttonpress,
257 [ClientMessage] = clientmessage,
258 [ConfigureRequest] = configurerequest,
259 [ConfigureNotify] = configurenotify,
260 [DestroyNotify] = destroynotify,
261 [EnterNotify] = enternotify,
264 [KeyPress] = keypress,
265 [MappingNotify] = mappingnotify,
266 [MapRequest] = maprequest,
267 [MotionNotify] = motionnotify,
268 [PropertyNotify] = propertynotify,
269 [UnmapNotify] = unmapnotify
271 static Atom wmatom[WMLast], netatom[NetLast];
272 static int running = 1;
273 static Cur *cursor[CurLast];
277 static Monitor *mons, *selmon;
278 static Window root, wmcheckwin;
280 /* configuration, allows nested code to access above variables */
284 unsigned int curtag, prevtag; /* current and previous tag */
285 int nmasters[LENGTH(tags) + 1]; /* number of windows in master area */
286 float mfacts[LENGTH(tags) + 1]; /* mfacts per tag */
287 unsigned int sellts[LENGTH(tags) + 1]; /* selected layouts */
288 const Layout *ltidxs[LENGTH(tags) + 1][2]; /* matrix of tags and layouts indexes */
289 int showbars[LENGTH(tags) + 1]; /* display bar for the current tag */
292 /* compile-time check if all tags fit into an unsigned int bit array. */
293 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
295 /* dwm will keep pid's of processes from autostart array and kill them at quit */
296 static pid_t *autostart_pids;
297 static size_t autostart_len;
299 /* execute command from autostart array */
302 const char *const *p;
306 for (p = autostart; *p; autostart_len++, p++)
309 autostart_pids = malloc(autostart_len * sizeof(pid_t));
310 for (p = autostart; *p; i++, p++) {
311 if ((autostart_pids[i] = fork()) == 0) {
313 execvp(*p, (char *const *)p);
314 fprintf(stderr, "dwm: execvp %s\n", *p);
323 /* function implementations */
325 applyrules(Client *c)
327 const char *class, *instance;
331 XClassHint ch = { NULL, NULL };
336 XGetClassHint(dpy, c->win, &ch);
337 class = ch.res_class ? ch.res_class : broken;
338 instance = ch.res_name ? ch.res_name : broken;
340 for (i = 0; i < LENGTH(rules); i++) {
342 if ((!r->title || strstr(c->name, r->title))
343 && (!r->class || strstr(class, r->class))
344 && (!r->instance || strstr(instance, r->instance)))
346 c->isfloating = r->isfloating;
348 for (m = mons; m && m->num != r->monitor; m = m->next);
357 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
361 applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact)
366 /* set minimum possible */
374 if (*x + *w + 2 * c->bw < 0)
376 if (*y + *h + 2 * c->bw < 0)
379 if (*x >= m->wx + m->ww)
380 *x = m->wx + m->ww - WIDTH(c);
381 if (*y >= m->wy + m->wh)
382 *y = m->wy + m->wh - HEIGHT(c);
383 if (*x + *w + 2 * c->bw <= m->wx)
385 if (*y + *h + 2 * c->bw <= m->wy)
392 if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
393 /* see last two sentences in ICCCM 4.1.2.3 */
394 baseismin = c->basew == c->minw && c->baseh == c->minh;
395 if (!baseismin) { /* temporarily remove base dimensions */
399 /* adjust for aspect limits */
400 if (c->mina > 0 && c->maxa > 0) {
401 if (c->maxa < (float)*w / *h)
402 *w = *h * c->maxa + 0.5;
403 else if (c->mina < (float)*h / *w)
404 *h = *w * c->mina + 0.5;
406 if (baseismin) { /* increment calculation requires this */
410 /* adjust for increment value */
415 /* restore base dimensions */
416 *w = MAX(*w + c->basew, c->minw);
417 *h = MAX(*h + c->baseh, c->minh);
419 *w = MIN(*w, c->maxw);
421 *h = MIN(*h, c->maxh);
423 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
431 else for (m = mons; m; m = m->next)
436 } else for (m = mons; m; m = m->next)
441 arrangemon(Monitor *m)
443 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
444 if (m->lt[m->sellt]->arrange)
445 m->lt[m->sellt]->arrange(m);
451 c->next = c->mon->clients;
456 attachbottom(Client *c)
460 for (tc = &c->mon->clients; *tc; tc = &(*tc)->next);
465 attachstack(Client *c)
467 c->snext = c->mon->stack;
472 buttonpress(XEvent *e)
474 unsigned int i, x, click;
478 XButtonPressedEvent *ev = &e->xbutton;
481 /* focus monitor if necessary */
482 if ((m = wintomon(ev->window)) && m != selmon) {
483 unfocus(selmon->sel, 1);
487 if (ev->window == selmon->barwin) {
491 while (ev->x >= x && ++i < LENGTH(tags));
492 if (i < LENGTH(tags)) {
495 } else if (ev->x < x + blw)
497 else if (ev->x > selmon->ww - (int)TEXTW(stext))
498 click = ClkStatusText;
501 } else if ((c = wintoclient(ev->window))) {
504 XAllowEvents(dpy, ReplayPointer, CurrentTime);
505 click = ClkClientWin;
507 for (i = 0; i < LENGTH(buttons); i++)
508 if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
509 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
510 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
516 xerrorxlib = XSetErrorHandler(xerrorstart);
517 /* this causes an error if some other window manager is running */
518 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
520 XSetErrorHandler(xerror);
528 Layout foo = { "", NULL };
533 selmon->lt[selmon->sellt] = &foo;
534 for (m = mons; m; m = m->next)
536 unmanage(m->stack, 0);
537 XUngrabKey(dpy, AnyKey, AnyModifier, root);
540 for (i = 0; i < CurLast; i++)
541 drw_cur_free(drw, cursor[i]);
542 for (i = 0; i < LENGTH(colors); i++)
544 XDestroyWindow(dpy, wmcheckwin);
547 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
548 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
552 cleanupmon(Monitor *mon)
559 for (m = mons; m && m->next != mon; m = m->next);
562 XUnmapWindow(dpy, mon->barwin);
563 XDestroyWindow(dpy, mon->barwin);
568 clientmessage(XEvent *e)
570 XClientMessageEvent *cme = &e->xclient;
571 Client *c = wintoclient(cme->window);
575 if (cme->message_type == netatom[NetWMState]) {
576 if (cme->data.l[1] == netatom[NetWMFullscreen]
577 || cme->data.l[2] == netatom[NetWMFullscreen])
578 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
579 || cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */));
580 } else if (cme->message_type == netatom[NetActiveWindow]) {
581 if (c != selmon->sel && !c->isurgent)
591 ce.type = ConfigureNotify;
599 ce.border_width = c->bw;
601 ce.override_redirect = False;
602 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
606 configurenotify(XEvent *e)
609 XConfigureEvent *ev = &e->xconfigure;
612 /* TODO: updategeom handling sucks, needs to be simplified */
613 if (ev->window == root) {
614 dirty = (sw != ev->width || sh != ev->height);
617 if (updategeom() || dirty) {
618 drw_resize(drw, sw, bh);
620 for (m = mons; m; m = m->next) {
621 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
630 configurerequest(XEvent *e)
634 XConfigureRequestEvent *ev = &e->xconfigurerequest;
637 if ((c = wintoclient(ev->window))) {
638 if (ev->value_mask & CWBorderWidth)
639 c->bw = ev->border_width;
640 else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
642 if (ev->value_mask & CWX) {
644 c->x = m->mx + ev->x;
646 if (ev->value_mask & CWY) {
648 c->y = m->my + ev->y;
650 if (ev->value_mask & CWWidth) {
654 if (ev->value_mask & CWHeight) {
658 if ((c->x + c->w) > m->mx + m->mw && c->isfloating)
659 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
660 if ((c->y + c->h) > m->my + m->mh && c->isfloating)
661 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
662 if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
665 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
671 wc.width = ev->width;
672 wc.height = ev->height;
673 wc.border_width = ev->border_width;
674 wc.sibling = ev->above;
675 wc.stack_mode = ev->detail;
676 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
687 m = ecalloc(1, sizeof(Monitor));
688 m->tagset[0] = m->tagset[1] = 1;
690 m->nmaster = nmaster;
691 m->showbar = showbar;
693 m->lt[0] = &layouts[0];
694 m->lt[1] = &layouts[1 % LENGTH(layouts)];
695 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
696 m->pertag = ecalloc(1, sizeof(Pertag));
697 m->pertag->curtag = m->pertag->prevtag = 1;
699 for (i = 0; i <= LENGTH(tags); i++) {
700 m->pertag->nmasters[i] = m->nmaster;
701 m->pertag->mfacts[i] = m->mfact;
703 m->pertag->ltidxs[i][0] = m->lt[0];
704 m->pertag->ltidxs[i][1] = m->lt[1];
705 m->pertag->sellts[i] = m->sellt;
707 m->pertag->showbars[i] = m->showbar;
714 destroynotify(XEvent *e)
717 XDestroyWindowEvent *ev = &e->xdestroywindow;
719 if ((c = wintoclient(ev->window)))
728 for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
733 detachstack(Client *c)
737 for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
740 if (c == c->mon->sel) {
741 for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
752 if (!(m = selmon->next))
754 } else if (selmon == mons)
755 for (m = mons; m->next; m = m->next);
757 for (m = mons; m->next != selmon; m = m->next);
765 int boxs = drw->fonts->h / 9;
766 int boxw = drw->fonts->h / 6 + 2;
767 unsigned int i, occ = 0, urg = 0;
770 /* draw status first so it can be overdrawn by tags later */
771 if (m == selmon) { /* status is only drawn on selected monitor */
772 drw_setscheme(drw, scheme[SchemeNorm]);
773 tw = TEXTW(stext) - lrpad + 2; /* 2px right padding */
774 drw_text(drw, m->ww - tw, 0, tw, bh, 0, stext, 0);
777 for (c = m->clients; c; c = c->next) {
783 for (i = 0; i < LENGTH(tags); i++) {
785 drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]);
786 drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i);
788 drw_rect(drw, x + boxs, boxs, boxw, boxw,
789 m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
793 w = blw = TEXTW(m->ltsymbol);
794 drw_setscheme(drw, scheme[SchemeNorm]);
795 x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0);
797 if ((w = m->ww - tw - x) > bh) {
799 drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]);
800 drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->name, 0);
801 if (m->sel->isfloating)
802 drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
804 drw_setscheme(drw, scheme[SchemeNorm]);
805 drw_rect(drw, x, 0, w, bh, 1, 1);
808 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
816 for (m = mons; m; m = m->next)
821 enternotify(XEvent *e)
825 XCrossingEvent *ev = &e->xcrossing;
827 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
829 c = wintoclient(ev->window);
830 m = c ? c->mon : wintomon(ev->window);
832 unfocus(selmon->sel, 1);
834 } else if (!c || c == selmon->sel)
843 XExposeEvent *ev = &e->xexpose;
845 if (ev->count == 0 && (m = wintomon(ev->window)))
850 findbefore(Client *c)
853 if (c == selmon->clients)
855 for (tmp = selmon->clients; tmp && tmp->next != c; tmp = tmp->next);
862 if (!c || !ISVISIBLE(c))
863 for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
864 if (selmon->sel && selmon->sel != c)
865 unfocus(selmon->sel, 0);
867 if (c->mon != selmon)
874 XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel);
877 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
878 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
884 /* there are some broken focus acquiring clients needing extra handling */
888 XFocusChangeEvent *ev = &e->xfocus;
890 if (selmon->sel && ev->window != selmon->sel->win)
891 setfocus(selmon->sel);
895 focusmon(const Arg *arg)
901 if ((m = dirtomon(arg->i)) == selmon)
903 unfocus(selmon->sel, 0);
907 XWarpPointer(dpy, None, selmon->sel->win, 0, 0, 0, 0, selmon->sel->w/2, selmon->sel->h/2);
911 focusstack(const Arg *arg)
913 Client *c = NULL, *i;
918 for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
920 for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
922 for (i = selmon->clients; i != selmon->sel; i = i->next)
926 for (; i; i = i->next)
933 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w/2, c->h/2);
938 getatomprop(Client *c, Atom prop)
942 unsigned char *p = NULL;
943 Atom da, atom = None;
945 if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
946 &da, &di, &dl, &dl, &p) == Success && p) {
954 getrootptr(int *x, int *y)
960 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
968 unsigned char *p = NULL;
969 unsigned long n, extra;
972 if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
973 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
982 gettextprop(Window w, Atom atom, char *text, unsigned int size)
988 if (!text || size == 0)
991 if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems)
993 if (name.encoding == XA_STRING)
994 strncpy(text, (char *)name.value, size - 1);
996 if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
997 strncpy(text, *list, size - 1);
998 XFreeStringList(list);
1001 text[size - 1] = '\0';
1007 grabbuttons(Client *c, int focused)
1009 updatenumlockmask();
1012 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1013 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1015 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
1016 BUTTONMASK, GrabModeSync, GrabModeSync, None, None);
1017 for (i = 0; i < LENGTH(buttons); i++)
1018 if (buttons[i].click == ClkClientWin)
1019 for (j = 0; j < LENGTH(modifiers); j++)
1020 XGrabButton(dpy, buttons[i].button,
1021 buttons[i].mask | modifiers[j],
1022 c->win, False, BUTTONMASK,
1023 GrabModeAsync, GrabModeSync, None, None);
1030 updatenumlockmask();
1033 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1036 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1037 for (i = 0; i < LENGTH(keys); i++)
1038 if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
1039 for (j = 0; j < LENGTH(modifiers); j++)
1040 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
1041 True, GrabModeAsync, GrabModeAsync);
1046 incnmaster(const Arg *arg)
1048 selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag] = MAX(selmon->nmaster + arg->i, 0);
1054 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
1057 if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1058 && unique[n].width == info->width && unique[n].height == info->height)
1062 #endif /* XINERAMA */
1072 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1073 for (i = 0; i < LENGTH(keys); i++)
1074 if (keysym == keys[i].keysym
1075 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1077 keys[i].func(&(keys[i].arg));
1081 killclient(const Arg *arg)
1085 if (!sendevent(selmon->sel, wmatom[WMDelete])) {
1087 XSetErrorHandler(xerrordummy);
1088 XSetCloseDownMode(dpy, DestroyAll);
1089 XKillClient(dpy, selmon->sel->win);
1091 XSetErrorHandler(xerror);
1097 manage(Window w, XWindowAttributes *wa)
1099 Client *c, *t = NULL;
1100 Window trans = None;
1103 c = ecalloc(1, sizeof(Client));
1106 c->x = c->oldx = wa->x;
1107 c->y = c->oldy = wa->y;
1108 c->w = c->oldw = wa->width;
1109 c->h = c->oldh = wa->height;
1110 c->oldbw = wa->border_width;
1113 if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1121 if (c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1122 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1123 if (c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1124 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1125 c->x = MAX(c->x, c->mon->mx);
1126 /* only fix client y-offset, if the client center might cover the bar */
1127 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1128 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1131 wc.border_width = c->bw;
1132 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1133 XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel);
1134 configure(c); /* propagates border_width, if size doesn't change */
1135 updatewindowtype(c);
1138 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1141 c->isfloating = c->oldstate = trans != None || c->isfixed;
1143 XRaiseWindow(dpy, c->win);
1146 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1147 (unsigned char *) &(c->win), 1);
1148 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1149 setclientstate(c, NormalState);
1150 if (c->mon == selmon)
1151 unfocus(selmon->sel, 0);
1154 XMapWindow(dpy, c->win);
1159 mappingnotify(XEvent *e)
1161 XMappingEvent *ev = &e->xmapping;
1163 XRefreshKeyboardMapping(ev);
1164 if (ev->request == MappingKeyboard)
1169 maprequest(XEvent *e)
1171 static XWindowAttributes wa;
1172 XMapRequestEvent *ev = &e->xmaprequest;
1174 if (!XGetWindowAttributes(dpy, ev->window, &wa))
1176 if (wa.override_redirect)
1178 if (!wintoclient(ev->window))
1179 manage(ev->window, &wa);
1188 for (c = m->clients; c; c = c->next)
1191 if (n > 0) /* override layout symbol */
1192 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1193 for (c = nexttiled(m->clients); c; c = nexttiled(c->next))
1194 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
1198 motionnotify(XEvent *e)
1200 static Monitor *mon = NULL;
1202 XMotionEvent *ev = &e->xmotion;
1204 if (ev->window != root)
1206 if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1207 unfocus(selmon->sel, 1);
1215 movemouse(const Arg *arg)
1217 int x, y, ocx, ocy, nx, ny;
1223 if (!(c = selmon->sel))
1228 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1229 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1231 if (!getrootptr(&x, &y))
1234 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1236 case ConfigureRequest:
1239 handler[ev.type](&ev);
1242 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1244 lasttime = ev.xmotion.time;
1246 nx = ocx + (ev.xmotion.x - x);
1247 ny = ocy + (ev.xmotion.y - y);
1248 if (abs(selmon->wx - nx) < snap)
1250 else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1251 nx = selmon->wx + selmon->ww - WIDTH(c);
1252 if (abs(selmon->wy - ny) < snap)
1254 else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1255 ny = selmon->wy + selmon->wh - HEIGHT(c);
1256 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1257 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1258 togglefloating(NULL);
1259 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1260 resize(c, nx, ny, c->w, c->h, 1);
1263 } while (ev.type != ButtonRelease);
1264 XUngrabPointer(dpy, CurrentTime);
1265 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1273 nexttiled(Client *c)
1275 for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1280 prevtiled(Client *c) {
1283 for(p = selmon->clients, r = NULL; p && p != c; p = p->next)
1284 if(!p->isfloating && ISVISIBLE(p))
1290 propertynotify(XEvent *e)
1294 XPropertyEvent *ev = &e->xproperty;
1296 if ((ev->window == root) && (ev->atom == XA_WM_NAME))
1298 else if (ev->state == PropertyDelete)
1299 return; /* ignore */
1300 else if ((c = wintoclient(ev->window))) {
1303 case XA_WM_TRANSIENT_FOR:
1304 if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1305 (c->isfloating = (wintoclient(trans)) != NULL))
1308 case XA_WM_NORMAL_HINTS:
1316 if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1318 if (c == c->mon->sel)
1321 if (ev->atom == netatom[NetWMWindowType])
1322 updatewindowtype(c);
1327 pushdown(const Arg *arg) {
1328 Client *sel = selmon->sel, *c;
1330 if(!sel || sel->isfloating)
1332 if((c = nexttiled(sel->next))) {
1334 sel->next = c->next;
1345 pushup(const Arg *arg) {
1346 Client *sel = selmon->sel, *c;
1348 if(!sel || sel->isfloating)
1350 if((c = prevtiled(sel))) {
1353 if(selmon->clients == c)
1354 selmon->clients = sel;
1356 for(c = selmon->clients; c->next != sel->next; c = c->next);
1360 for(c = sel; c->next; c = c->next);
1370 quit(const Arg *arg)
1374 /* kill child processes */
1375 for (i = 0; i < autostart_len; i++) {
1376 if (0 < autostart_pids[i]) {
1377 kill(autostart_pids[i], SIGTERM);
1378 waitpid(autostart_pids[i], NULL, 0);
1386 recttomon(int x, int y, int w, int h)
1388 Monitor *m, *r = selmon;
1391 for (m = mons; m; m = m->next)
1392 if ((a = INTERSECT(x, y, w, h, m)) > area) {
1400 resize(Client *c, int x, int y, int w, int h, int interact)
1402 if (applysizehints(c, &x, &y, &w, &h, interact))
1403 resizeclient(c, x, y, w, h);
1407 resizeclient(Client *c, int x, int y, int w, int h)
1411 c->oldx = c->x; c->x = wc.x = x;
1412 c->oldy = c->y; c->y = wc.y = y;
1413 c->oldw = c->w; c->w = wc.width = w;
1414 c->oldh = c->h; c->h = wc.height = h;
1415 wc.border_width = c->bw;
1416 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1422 resizemouse(const Arg *arg)
1424 int ocx, ocy, nw, nh;
1430 if (!(c = selmon->sel))
1435 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1436 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1438 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1440 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1442 case ConfigureRequest:
1445 handler[ev.type](&ev);
1448 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1450 lasttime = ev.xmotion.time;
1452 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1453 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1454 if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1455 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1457 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1458 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1459 togglefloating(NULL);
1461 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1462 resize(c, c->x, c->y, nw, nh, 1);
1465 } while (ev.type != ButtonRelease);
1466 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1467 XUngrabPointer(dpy, CurrentTime);
1468 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1469 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1486 if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
1487 XRaiseWindow(dpy, m->sel->win);
1488 if (m->lt[m->sellt]->arrange) {
1489 wc.stack_mode = Below;
1490 wc.sibling = m->barwin;
1491 for (c = m->stack; c; c = c->snext)
1492 if (!c->isfloating && ISVISIBLE(c)) {
1493 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1494 wc.sibling = c->win;
1498 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1505 /* main event loop */
1507 while (running && !XNextEvent(dpy, &ev))
1508 if (handler[ev.type])
1509 handler[ev.type](&ev); /* call handler */
1515 unsigned int i, num;
1516 Window d1, d2, *wins = NULL;
1517 XWindowAttributes wa;
1519 if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1520 for (i = 0; i < num; i++) {
1521 if (!XGetWindowAttributes(dpy, wins[i], &wa)
1522 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1524 if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1525 manage(wins[i], &wa);
1527 for (i = 0; i < num; i++) { /* now the transients */
1528 if (!XGetWindowAttributes(dpy, wins[i], &wa))
1530 if (XGetTransientForHint(dpy, wins[i], &d1)
1531 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1532 manage(wins[i], &wa);
1540 sendmon(Client *c, Monitor *m)
1548 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1556 setclientstate(Client *c, long state)
1558 long data[] = { state, None };
1560 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1561 PropModeReplace, (unsigned char *)data, 2);
1565 sendevent(Client *c, Atom proto)
1572 if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1573 while (!exists && n--)
1574 exists = protocols[n] == proto;
1578 ev.type = ClientMessage;
1579 ev.xclient.window = c->win;
1580 ev.xclient.message_type = wmatom[WMProtocols];
1581 ev.xclient.format = 32;
1582 ev.xclient.data.l[0] = proto;
1583 ev.xclient.data.l[1] = CurrentTime;
1584 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1592 if (!c->neverfocus) {
1593 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1594 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1595 XA_WINDOW, 32, PropModeReplace,
1596 (unsigned char *) &(c->win), 1);
1598 sendevent(c, wmatom[WMTakeFocus]);
1602 setfullscreen(Client *c, int fullscreen)
1604 if (fullscreen && !c->isfullscreen) {
1605 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1606 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1607 c->isfullscreen = 1;
1608 } else if (!fullscreen && c->isfullscreen){
1609 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1610 PropModeReplace, (unsigned char*)0, 0);
1611 c->isfullscreen = 0;
1616 setlayout(const Arg *arg)
1618 if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1619 selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag] ^= 1;
1621 selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][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)
1640 selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag] = f;
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 utf8string = XInternAtom(dpy, "UTF8_STRING", False);
1667 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1668 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1669 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1670 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1671 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1672 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1673 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1674 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1675 netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
1676 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1677 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1678 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1679 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1681 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1682 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1683 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1684 /* init appearance */
1685 scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
1686 for (i = 0; i < LENGTH(colors); i++)
1687 scheme[i] = drw_scm_create(drw, colors[i], 3);
1691 /* supporting window for NetWMCheck */
1692 wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
1693 XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
1694 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1695 XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8,
1696 PropModeReplace, (unsigned char *) "dwm", 3);
1697 XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32,
1698 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1699 /* EWMH support per view */
1700 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1701 PropModeReplace, (unsigned char *) netatom, NetLast);
1702 XDeleteProperty(dpy, root, netatom[NetClientList]);
1704 wa.cursor = cursor[CurNormal]->cursor;
1705 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
1706 |ButtonPressMask|PointerMotionMask|EnterWindowMask
1707 |LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1708 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1709 XSelectInput(dpy, root, wa.event_mask);
1716 seturgent(Client *c, int urg)
1721 if (!(wmh = XGetWMHints(dpy, c->win)))
1723 wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
1724 XSetWMHints(dpy, c->win, wmh);
1734 /* show clients top down */
1735 XMoveWindow(dpy, c->win, c->x, c->y);
1736 if (!c->mon->lt[c->mon->sellt]->arrange || c->isfloating)
1737 resize(c, c->x, c->y, c->w, c->h, 0);
1740 /* hide clients bottom up */
1742 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1751 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1752 die("can't install SIGCHLD handler:");
1753 while (0 < (pid = waitpid(-1, NULL, WNOHANG))) {
1756 if (!(p = autostart_pids))
1758 lim = &p[autostart_len];
1760 for (; p < lim; p++) {
1771 spawn(const Arg *arg)
1773 if (arg->v == dmenucmd)
1774 dmenumon[0] = '0' + selmon->num;
1777 close(ConnectionNumber(dpy));
1779 execvp(((char **)arg->v)[0], (char **)arg->v);
1780 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1789 if (selmon->sel && arg->ui & TAGMASK) {
1790 selmon->sel->tags = arg->ui & TAGMASK;
1797 tagmon(const Arg *arg)
1799 if (!selmon->sel || !mons->next)
1801 sendmon(selmon->sel, dirtomon(arg->i));
1807 unsigned int i, n, h, mw, my, ty;
1810 for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1815 mw = m->nmaster ? m->ww * m->mfact : 0;
1818 for (i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1819 if (i < m->nmaster) {
1820 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1821 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), 0);
1822 if (my + HEIGHT(c) < m->wh)
1825 h = (m->wh - ty) / (n - i);
1826 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), 0);
1827 if (ty + HEIGHT(c) < m->wh)
1833 togglebar(const Arg *arg)
1835 selmon->showbar = selmon->pertag->showbars[selmon->pertag->curtag] = !selmon->showbar;
1836 updatebarpos(selmon);
1837 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1842 togglefloating(const Arg *arg)
1846 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1847 if (selmon->sel->isfloating)
1848 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1849 selmon->sel->w, selmon->sel->h, 0);
1854 toggletag(const Arg *arg)
1856 unsigned int newtags;
1860 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1862 selmon->sel->tags = newtags;
1869 toggleview(const Arg *arg)
1871 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1875 selmon->tagset[selmon->seltags] = newtagset;
1877 if (newtagset == ~0) {
1878 selmon->pertag->prevtag = selmon->pertag->curtag;
1879 selmon->pertag->curtag = 0;
1882 /* test if the user did not select the same tag */
1883 if (!(newtagset & 1 << (selmon->pertag->curtag - 1))) {
1884 selmon->pertag->prevtag = selmon->pertag->curtag;
1885 for (i = 0; !(newtagset & 1 << i); i++) ;
1886 selmon->pertag->curtag = i + 1;
1889 /* apply settings for this view */
1890 selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag];
1891 selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag];
1892 selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
1893 selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
1894 selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1];
1896 if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag])
1905 unfocus(Client *c, int setfocus)
1910 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
1912 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1913 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1918 unmanage(Client *c, int destroyed)
1920 Monitor *m = c->mon;
1926 wc.border_width = c->oldbw;
1927 XGrabServer(dpy); /* avoid race conditions */
1928 XSetErrorHandler(xerrordummy);
1929 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1930 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1931 setclientstate(c, WithdrawnState);
1933 XSetErrorHandler(xerror);
1944 unmapnotify(XEvent *e)
1947 XUnmapEvent *ev = &e->xunmap;
1949 if ((c = wintoclient(ev->window))) {
1951 setclientstate(c, WithdrawnState);
1961 XSetWindowAttributes wa = {
1962 .override_redirect = True,
1963 .background_pixmap = ParentRelative,
1964 .event_mask = ButtonPressMask|ExposureMask
1966 XClassHint ch = {"dwm", "dwm"};
1967 for (m = mons; m; m = m->next) {
1970 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1971 CopyFromParent, DefaultVisual(dpy, screen),
1972 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1973 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
1974 XMapRaised(dpy, m->barwin);
1975 XSetClassHint(dpy, m->barwin, &ch);
1980 updatebarpos(Monitor *m)
1986 m->by = m->topbar ? m->wy : m->wy + m->wh;
1987 m->wy = m->topbar ? m->wy + bh : m->wy;
1998 XDeleteProperty(dpy, root, netatom[NetClientList]);
1999 for (m = mons; m; m = m->next)
2000 for (c = m->clients; c; c = c->next)
2001 XChangeProperty(dpy, root, netatom[NetClientList],
2002 XA_WINDOW, 32, PropModeAppend,
2003 (unsigned char *) &(c->win), 1);
2012 if (XineramaIsActive(dpy)) {
2016 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
2017 XineramaScreenInfo *unique = NULL;
2019 for (n = 0, m = mons; m; m = m->next, n++);
2020 /* only consider unique geometries as separate screens */
2021 unique = ecalloc(nn, sizeof(XineramaScreenInfo));
2022 for (i = 0, j = 0; i < nn; i++)
2023 if (isuniquegeom(unique, j, &info[i]))
2024 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
2027 if (n <= nn) { /* new monitors available */
2028 for (i = 0; i < (nn - n); i++) {
2029 for (m = mons; m && m->next; m = m->next);
2031 m->next = createmon();
2035 for (i = 0, m = mons; i < nn && m; m = m->next, i++)
2037 || unique[i].x_org != m->mx || unique[i].y_org != m->my
2038 || unique[i].width != m->mw || unique[i].height != m->mh)
2042 m->mx = m->wx = unique[i].x_org;
2043 m->my = m->wy = unique[i].y_org;
2044 m->mw = m->ww = unique[i].width;
2045 m->mh = m->wh = unique[i].height;
2048 } else { /* less monitors available nn < n */
2049 for (i = nn; i < n; i++) {
2050 for (m = mons; m && m->next; m = m->next);
2051 while ((c = m->clients)) {
2053 m->clients = c->next;
2066 #endif /* XINERAMA */
2067 { /* default monitor setup */
2070 if (mons->mw != sw || mons->mh != sh) {
2072 mons->mw = mons->ww = sw;
2073 mons->mh = mons->wh = sh;
2079 selmon = wintomon(root);
2085 updatenumlockmask(void)
2088 XModifierKeymap *modmap;
2091 modmap = XGetModifierMapping(dpy);
2092 for (i = 0; i < 8; i++)
2093 for (j = 0; j < modmap->max_keypermod; j++)
2094 if (modmap->modifiermap[i * modmap->max_keypermod + j]
2095 == XKeysymToKeycode(dpy, XK_Num_Lock))
2096 numlockmask = (1 << i);
2097 XFreeModifiermap(modmap);
2101 updatesizehints(Client *c)
2106 if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
2107 /* size is uninitialized, ensure that size.flags aren't used */
2109 if (size.flags & PBaseSize) {
2110 c->basew = size.base_width;
2111 c->baseh = size.base_height;
2112 } else if (size.flags & PMinSize) {
2113 c->basew = size.min_width;
2114 c->baseh = size.min_height;
2116 c->basew = c->baseh = 0;
2117 if (size.flags & PResizeInc) {
2118 c->incw = size.width_inc;
2119 c->inch = size.height_inc;
2121 c->incw = c->inch = 0;
2122 if (size.flags & PMaxSize) {
2123 c->maxw = size.max_width;
2124 c->maxh = size.max_height;
2126 c->maxw = c->maxh = 0;
2127 if (size.flags & PMinSize) {
2128 c->minw = size.min_width;
2129 c->minh = size.min_height;
2130 } else if (size.flags & PBaseSize) {
2131 c->minw = size.base_width;
2132 c->minh = size.base_height;
2134 c->minw = c->minh = 0;
2135 if (size.flags & PAspect) {
2136 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2137 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2139 c->maxa = c->mina = 0.0;
2140 c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh);
2146 if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2147 strcpy(stext, "dwm-"VERSION);
2152 updatetitle(Client *c)
2154 if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2155 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2156 if (c->name[0] == '\0') /* hack to mark broken clients */
2157 strcpy(c->name, broken);
2161 updatewindowtype(Client *c)
2163 Atom state = getatomprop(c, netatom[NetWMState]);
2164 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2166 if (state == netatom[NetWMFullscreen])
2167 setfullscreen(c, 1);
2168 if (wtype == netatom[NetWMWindowTypeDialog])
2173 updatewmhints(Client *c)
2177 if ((wmh = XGetWMHints(dpy, c->win))) {
2178 if (c == selmon->sel && wmh->flags & XUrgencyHint) {
2179 wmh->flags &= ~XUrgencyHint;
2180 XSetWMHints(dpy, c->win, wmh);
2182 c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
2183 if (wmh->flags & InputHint)
2184 c->neverfocus = !wmh->input;
2192 view(const Arg *arg)
2195 unsigned int tmptag;
2197 if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2199 selmon->seltags ^= 1; /* toggle sel tagset */
2200 if (arg->ui & TAGMASK) {
2201 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2202 selmon->pertag->prevtag = selmon->pertag->curtag;
2205 selmon->pertag->curtag = 0;
2207 for (i = 0; !(arg->ui & 1 << i); i++) ;
2208 selmon->pertag->curtag = i + 1;
2211 tmptag = selmon->pertag->prevtag;
2212 selmon->pertag->prevtag = selmon->pertag->curtag;
2213 selmon->pertag->curtag = tmptag;
2216 selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag];
2217 selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag];
2218 selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
2219 selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
2220 selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1];
2222 if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag])
2230 wintoclient(Window w)
2235 for (m = mons; m; m = m->next)
2236 for (c = m->clients; c; c = c->next)
2249 if (w == root && getrootptr(&x, &y))
2250 return recttomon(x, y, 1, 1);
2251 for (m = mons; m; m = m->next)
2254 if ((c = wintoclient(w)))
2259 /* There's no way to check accesses to destroyed windows, thus those cases are
2260 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2261 * default error handler, which may call exit. */
2263 xerror(Display *dpy, XErrorEvent *ee)
2265 if (ee->error_code == BadWindow
2266 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2267 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2268 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2269 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2270 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2271 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2272 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2273 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2275 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2276 ee->request_code, ee->error_code);
2277 return xerrorxlib(dpy, ee); /* may call exit */
2281 xerrordummy(Display *dpy, XErrorEvent *ee)
2286 /* Startup Error handler to check if another window manager
2287 * is already running. */
2289 xerrorstart(Display *dpy, XErrorEvent *ee)
2291 die("dwm: another window manager is already running");
2296 zoom(const Arg *arg)
2298 Client *c = selmon->sel;
2299 Client *at = NULL, *cold, *cprevious = NULL;
2301 if (!selmon->lt[selmon->sellt]->arrange
2302 || (selmon->sel && selmon->sel->isfloating))
2304 if (c == nexttiled(selmon->clients)) {
2305 at = findbefore(prevzoom);
2307 cprevious = nexttiled(at->next);
2308 if (!cprevious || cprevious != prevzoom) {
2310 if (!c || !(c = nexttiled(c->next)))
2315 cold = nexttiled(selmon->clients);
2316 if (c != cold && !at)
2320 /* swap windows instead of pushing the previous one down */
2321 if (c != cold && at) {
2323 if (cold && at != cold) {
2325 cold->next = at->next;
2334 main(int argc, char *argv[])
2336 if (argc == 2 && !strcmp("-v", argv[1]))
2339 die("usage: dwm [-v]");
2340 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2341 fputs("warning: no locale support\n", stderr);
2342 if (!(dpy = XOpenDisplay(NULL)))
2343 die("dwm: cannot open display");
2348 if (pledge("stdio rpath proc exec", NULL) == -1)
2350 #endif /* __OpenBSD__ */
2355 return EXIT_SUCCESS;