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>
48 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
49 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
50 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
51 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
52 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
53 #define LENGTH(X) (sizeof X / sizeof X[0])
54 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
55 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
56 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
57 #define TAGMASK ((1 << LENGTH(tags)) - 1)
58 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
61 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
62 enum { SchemeNorm, SchemeSel }; /* color schemes */
63 enum { NetSupported, NetWMName, NetWMState, NetWMCheck,
64 NetWMFullscreen, NetActiveWindow, NetWMWindowType,
65 NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */
66 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
67 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
68 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
81 void (*func)(const Arg *arg);
85 typedef struct Monitor Monitor;
86 typedef struct Client Client;
91 int oldx, oldy, oldw, oldh;
92 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
95 int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
105 void (*func)(const Arg *);
111 void (*arrange)(Monitor *);
119 int by; /* bar geometry */
120 int mx, my, mw, mh; /* screen size */
121 int wx, wy, ww, wh; /* window area */
122 int gappx; /* gaps between windows */
123 unsigned int seltags;
125 unsigned int tagset[2];
138 const char *instance;
145 /* function declarations */
146 static void applyrules(Client *c);
147 static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact);
148 static void arrange(Monitor *m);
149 static void arrangemon(Monitor *m);
150 static void attach(Client *c);
151 static void attachstack(Client *c);
152 static void buttonpress(XEvent *e);
153 static void checkotherwm(void);
154 static void cleanup(void);
155 static void cleanupmon(Monitor *mon);
156 static void clientmessage(XEvent *e);
157 static void configure(Client *c);
158 static void configurenotify(XEvent *e);
159 static void configurerequest(XEvent *e);
160 static Monitor *createmon(void);
161 static void destroynotify(XEvent *e);
162 static void detach(Client *c);
163 static void detachstack(Client *c);
164 static Monitor *dirtomon(int dir);
165 static void drawbar(Monitor *m);
166 static void drawbars(void);
167 static void enternotify(XEvent *e);
168 static void expose(XEvent *e);
169 static void focus(Client *c);
170 static void focusin(XEvent *e);
171 static void focusmon(const Arg *arg);
172 static void focusstack(const Arg *arg);
173 static Atom getatomprop(Client *c, Atom prop);
174 static int getrootptr(int *x, int *y);
175 static long getstate(Window w);
176 static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
177 static void grabbuttons(Client *c, int focused);
178 static void grabkeys(void);
179 static void incnmaster(const Arg *arg);
180 static void keypress(XEvent *e);
181 static void killclient(const Arg *arg);
182 static void manage(Window w, XWindowAttributes *wa);
183 static void mappingnotify(XEvent *e);
184 static void maprequest(XEvent *e);
185 static void monocle(Monitor *m);
186 static void motionnotify(XEvent *e);
187 static void movemouse(const Arg *arg);
188 static Client *nexttiled(Client *c);
189 static void pop(Client *);
190 static void propertynotify(XEvent *e);
191 static void quit(const Arg *arg);
192 static Monitor *recttomon(int x, int y, int w, int h);
193 static void resize(Client *c, int x, int y, int w, int h, int interact);
194 static void resizeclient(Client *c, int x, int y, int w, int h);
195 static void resizemouse(const Arg *arg);
196 static void restack(Monitor *m);
197 static void run(void);
198 static void scan(void);
199 static int sendevent(Client *c, Atom proto);
200 static void sendmon(Client *c, Monitor *m);
201 static void setclientstate(Client *c, long state);
202 static void setfocus(Client *c);
203 static void setfullscreen(Client *c, int fullscreen);
204 static void setgaps(const Arg *arg);
205 static void setlayout(const Arg *arg);
206 static void setmfact(const Arg *arg);
207 static void setup(void);
208 static void seturgent(Client *c, int urg);
209 static void showhide(Client *c);
210 static void sigchld(int unused);
211 static void spawn(const Arg *arg);
212 static void tag(const Arg *arg);
213 static void tagmon(const Arg *arg);
214 static void tile(Monitor *);
215 static void togglebar(const Arg *arg);
216 static void togglefloating(const Arg *arg);
217 static void toggletag(const Arg *arg);
218 static void toggleview(const Arg *arg);
219 static void unfocus(Client *c, int setfocus);
220 static void unmanage(Client *c, int destroyed);
221 static void unmapnotify(XEvent *e);
222 static void updatebarpos(Monitor *m);
223 static void updatebars(void);
224 static void updateclientlist(void);
225 static int updategeom(void);
226 static void updatenumlockmask(void);
227 static void updatesizehints(Client *c);
228 static void updatestatus(void);
229 static void updatetitle(Client *c);
230 static void updatewindowtype(Client *c);
231 static void updatewmhints(Client *c);
232 static void view(const Arg *arg);
233 static Client *wintoclient(Window w);
234 static Monitor *wintomon(Window w);
235 static int xerror(Display *dpy, XErrorEvent *ee);
236 static int xerrordummy(Display *dpy, XErrorEvent *ee);
237 static int xerrorstart(Display *dpy, XErrorEvent *ee);
238 static void zoom(const Arg *arg);
241 static const char broken[] = "broken";
242 static char stext[256];
244 static int sw, sh; /* X display screen geometry width, height */
245 static int bh, blw = 0; /* bar geometry */
246 static int lrpad; /* sum of left and right padding for text */
247 static int (*xerrorxlib)(Display *, XErrorEvent *);
248 static unsigned int numlockmask = 0;
249 static void (*handler[LASTEvent]) (XEvent *) = {
250 [ButtonPress] = buttonpress,
251 [ClientMessage] = clientmessage,
252 [ConfigureRequest] = configurerequest,
253 [ConfigureNotify] = configurenotify,
254 [DestroyNotify] = destroynotify,
255 [EnterNotify] = enternotify,
258 [KeyPress] = keypress,
259 [MappingNotify] = mappingnotify,
260 [MapRequest] = maprequest,
261 [MotionNotify] = motionnotify,
262 [PropertyNotify] = propertynotify,
263 [UnmapNotify] = unmapnotify
265 static Atom wmatom[WMLast], netatom[NetLast];
266 static int running = 1;
267 static Cur *cursor[CurLast];
271 static Monitor *mons, *selmon;
272 static Window root, wmcheckwin;
274 /* configuration, allows nested code to access above variables */
277 /* compile-time check if all tags fit into an unsigned int bit array. */
278 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
280 /* function implementations */
282 applyrules(Client *c)
284 const char *class, *instance;
288 XClassHint ch = { NULL, NULL };
293 XGetClassHint(dpy, c->win, &ch);
294 class = ch.res_class ? ch.res_class : broken;
295 instance = ch.res_name ? ch.res_name : broken;
297 for (i = 0; i < LENGTH(rules); i++) {
299 if ((!r->title || strstr(c->name, r->title))
300 && (!r->class || strstr(class, r->class))
301 && (!r->instance || strstr(instance, r->instance)))
303 c->isfloating = r->isfloating;
305 for (m = mons; m && m->num != r->monitor; m = m->next);
314 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
318 applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact)
323 /* set minimum possible */
331 if (*x + *w + 2 * c->bw < 0)
333 if (*y + *h + 2 * c->bw < 0)
336 if (*x >= m->wx + m->ww)
337 *x = m->wx + m->ww - WIDTH(c);
338 if (*y >= m->wy + m->wh)
339 *y = m->wy + m->wh - HEIGHT(c);
340 if (*x + *w + 2 * c->bw <= m->wx)
342 if (*y + *h + 2 * c->bw <= m->wy)
349 if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
350 /* see last two sentences in ICCCM 4.1.2.3 */
351 baseismin = c->basew == c->minw && c->baseh == c->minh;
352 if (!baseismin) { /* temporarily remove base dimensions */
356 /* adjust for aspect limits */
357 if (c->mina > 0 && c->maxa > 0) {
358 if (c->maxa < (float)*w / *h)
359 *w = *h * c->maxa + 0.5;
360 else if (c->mina < (float)*h / *w)
361 *h = *w * c->mina + 0.5;
363 if (baseismin) { /* increment calculation requires this */
367 /* adjust for increment value */
372 /* restore base dimensions */
373 *w = MAX(*w + c->basew, c->minw);
374 *h = MAX(*h + c->baseh, c->minh);
376 *w = MIN(*w, c->maxw);
378 *h = MIN(*h, c->maxh);
380 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
388 else for (m = mons; m; m = m->next)
393 } else for (m = mons; m; m = m->next)
398 arrangemon(Monitor *m)
400 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
401 if (m->lt[m->sellt]->arrange)
402 m->lt[m->sellt]->arrange(m);
408 c->next = c->mon->clients;
413 attachstack(Client *c)
415 c->snext = c->mon->stack;
420 buttonpress(XEvent *e)
422 unsigned int i, x, click;
426 XButtonPressedEvent *ev = &e->xbutton;
429 /* focus monitor if necessary */
430 if ((m = wintomon(ev->window)) && m != selmon) {
431 unfocus(selmon->sel, 1);
435 if (ev->window == selmon->barwin) {
439 while (ev->x >= x && ++i < LENGTH(tags));
440 if (i < LENGTH(tags)) {
443 } else if (ev->x < x + blw)
445 else if (ev->x > selmon->ww - TEXTW(stext))
446 click = ClkStatusText;
449 } else if ((c = wintoclient(ev->window))) {
452 XAllowEvents(dpy, ReplayPointer, CurrentTime);
453 click = ClkClientWin;
455 for (i = 0; i < LENGTH(buttons); i++)
456 if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
457 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
458 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
464 xerrorxlib = XSetErrorHandler(xerrorstart);
465 /* this causes an error if some other window manager is running */
466 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
468 XSetErrorHandler(xerror);
476 Layout foo = { "", NULL };
481 selmon->lt[selmon->sellt] = &foo;
482 for (m = mons; m; m = m->next)
484 unmanage(m->stack, 0);
485 XUngrabKey(dpy, AnyKey, AnyModifier, root);
488 for (i = 0; i < CurLast; i++)
489 drw_cur_free(drw, cursor[i]);
490 for (i = 0; i < LENGTH(colors); i++)
492 XDestroyWindow(dpy, wmcheckwin);
495 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
496 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
500 cleanupmon(Monitor *mon)
507 for (m = mons; m && m->next != mon; m = m->next);
510 XUnmapWindow(dpy, mon->barwin);
511 XDestroyWindow(dpy, mon->barwin);
516 clientmessage(XEvent *e)
518 XClientMessageEvent *cme = &e->xclient;
519 Client *c = wintoclient(cme->window);
523 if (cme->message_type == netatom[NetWMState]) {
524 if (cme->data.l[1] == netatom[NetWMFullscreen]
525 || cme->data.l[2] == netatom[NetWMFullscreen])
526 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
527 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
528 } else if (cme->message_type == netatom[NetActiveWindow]) {
529 if (c != selmon->sel && !c->isurgent)
539 ce.type = ConfigureNotify;
547 ce.border_width = c->bw;
549 ce.override_redirect = False;
550 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
554 configurenotify(XEvent *e)
558 XConfigureEvent *ev = &e->xconfigure;
561 /* TODO: updategeom handling sucks, needs to be simplified */
562 if (ev->window == root) {
563 dirty = (sw != ev->width || sh != ev->height);
566 if (updategeom() || dirty) {
567 drw_resize(drw, sw, bh);
569 for (m = mons; m; m = m->next) {
570 for (c = m->clients; c; c = c->next)
572 resizeclient(c, m->mx, m->my, m->mw, m->mh);
573 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
582 configurerequest(XEvent *e)
586 XConfigureRequestEvent *ev = &e->xconfigurerequest;
589 if ((c = wintoclient(ev->window))) {
590 if (ev->value_mask & CWBorderWidth)
591 c->bw = ev->border_width;
592 else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
594 if (ev->value_mask & CWX) {
596 c->x = m->mx + ev->x;
598 if (ev->value_mask & CWY) {
600 c->y = m->my + ev->y;
602 if (ev->value_mask & CWWidth) {
606 if (ev->value_mask & CWHeight) {
610 if ((c->x + c->w) > m->mx + m->mw && c->isfloating)
611 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
612 if ((c->y + c->h) > m->my + m->mh && c->isfloating)
613 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
614 if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
617 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
623 wc.width = ev->width;
624 wc.height = ev->height;
625 wc.border_width = ev->border_width;
626 wc.sibling = ev->above;
627 wc.stack_mode = ev->detail;
628 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
638 m = ecalloc(1, sizeof(Monitor));
639 m->tagset[0] = m->tagset[1] = 1;
641 m->nmaster = nmaster;
642 m->showbar = showbar;
645 m->lt[0] = &layouts[0];
646 m->lt[1] = &layouts[1 % LENGTH(layouts)];
647 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
652 destroynotify(XEvent *e)
655 XDestroyWindowEvent *ev = &e->xdestroywindow;
657 if ((c = wintoclient(ev->window)))
666 for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
671 detachstack(Client *c)
675 for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
678 if (c == c->mon->sel) {
679 for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
690 if (!(m = selmon->next))
692 } else if (selmon == mons)
693 for (m = mons; m->next; m = m->next);
695 for (m = mons; m->next != selmon; m = m->next);
703 int boxs = drw->fonts->h / 9;
704 int boxw = drw->fonts->h / 6 + 2;
705 unsigned int i, occ = 0, urg = 0;
708 /* draw status first so it can be overdrawn by tags later */
709 if (m == selmon) { /* status is only drawn on selected monitor */
710 drw_setscheme(drw, scheme[SchemeNorm]);
711 tw = TEXTW(stext) - lrpad + 2; /* 2px right padding */
712 drw_text(drw, m->ww - tw, 0, tw, bh, 0, stext, 0);
715 for (c = m->clients; c; c = c->next) {
721 for (i = 0; i < LENGTH(tags); i++) {
723 drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]);
724 drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i);
726 drw_rect(drw, x + boxs, boxs, boxw, boxw,
727 m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
731 w = blw = TEXTW(m->ltsymbol);
732 drw_setscheme(drw, scheme[SchemeNorm]);
733 x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0);
735 if ((w = m->ww - tw - x) > bh) {
737 drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]);
738 drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->name, 0);
739 if (m->sel->isfloating)
740 drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
742 drw_setscheme(drw, scheme[SchemeNorm]);
743 drw_rect(drw, x, 0, w, bh, 1, 1);
746 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
754 for (m = mons; m; m = m->next)
759 enternotify(XEvent *e)
763 XCrossingEvent *ev = &e->xcrossing;
765 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
767 c = wintoclient(ev->window);
768 m = c ? c->mon : wintomon(ev->window);
770 unfocus(selmon->sel, 1);
772 } else if (!c || c == selmon->sel)
781 XExposeEvent *ev = &e->xexpose;
783 if (ev->count == 0 && (m = wintomon(ev->window)))
790 if (!c || !ISVISIBLE(c))
791 for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
792 if (selmon->sel && selmon->sel != c)
793 unfocus(selmon->sel, 0);
795 if (c->mon != selmon)
802 XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel);
805 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
806 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
812 /* there are some broken focus acquiring clients needing extra handling */
816 XFocusChangeEvent *ev = &e->xfocus;
818 if (selmon->sel && ev->window != selmon->sel->win)
819 setfocus(selmon->sel);
823 focusmon(const Arg *arg)
829 if ((m = dirtomon(arg->i)) == selmon)
831 unfocus(selmon->sel, 0);
837 focusstack(const Arg *arg)
839 Client *c = NULL, *i;
844 for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
846 for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
848 for (i = selmon->clients; i != selmon->sel; i = i->next)
852 for (; i; i = i->next)
863 getatomprop(Client *c, Atom prop)
867 unsigned char *p = NULL;
868 Atom da, atom = None;
870 if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
871 &da, &di, &dl, &dl, &p) == Success && p) {
879 getrootptr(int *x, int *y)
885 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
893 unsigned char *p = NULL;
894 unsigned long n, extra;
897 if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
898 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
907 gettextprop(Window w, Atom atom, char *text, unsigned int size)
913 if (!text || size == 0)
916 if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems)
918 if (name.encoding == XA_STRING)
919 strncpy(text, (char *)name.value, size - 1);
921 if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
922 strncpy(text, *list, size - 1);
923 XFreeStringList(list);
926 text[size - 1] = '\0';
932 grabbuttons(Client *c, int focused)
937 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
938 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
940 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
941 BUTTONMASK, GrabModeSync, GrabModeSync, None, None);
942 for (i = 0; i < LENGTH(buttons); i++)
943 if (buttons[i].click == ClkClientWin)
944 for (j = 0; j < LENGTH(modifiers); j++)
945 XGrabButton(dpy, buttons[i].button,
946 buttons[i].mask | modifiers[j],
947 c->win, False, BUTTONMASK,
948 GrabModeAsync, GrabModeSync, None, None);
958 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
961 XUngrabKey(dpy, AnyKey, AnyModifier, root);
962 for (i = 0; i < LENGTH(keys); i++)
963 if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
964 for (j = 0; j < LENGTH(modifiers); j++)
965 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
966 True, GrabModeAsync, GrabModeAsync);
971 incnmaster(const Arg *arg)
973 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
979 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
982 if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
983 && unique[n].width == info->width && unique[n].height == info->height)
987 #endif /* XINERAMA */
997 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
998 for (i = 0; i < LENGTH(keys); i++)
999 if (keysym == keys[i].keysym
1000 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1002 keys[i].func(&(keys[i].arg));
1006 killclient(const Arg *arg)
1010 if (!sendevent(selmon->sel, wmatom[WMDelete])) {
1012 XSetErrorHandler(xerrordummy);
1013 XSetCloseDownMode(dpy, DestroyAll);
1014 XKillClient(dpy, selmon->sel->win);
1016 XSetErrorHandler(xerror);
1022 manage(Window w, XWindowAttributes *wa)
1024 Client *c, *t = NULL;
1025 Window trans = None;
1028 c = ecalloc(1, sizeof(Client));
1031 c->x = c->oldx = wa->x;
1032 c->y = c->oldy = wa->y;
1033 c->w = c->oldw = wa->width;
1034 c->h = c->oldh = wa->height;
1035 c->oldbw = wa->border_width;
1038 if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1046 if (c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1047 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1048 if (c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1049 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1050 c->x = MAX(c->x, c->mon->mx);
1051 /* only fix client y-offset, if the client center might cover the bar */
1052 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1053 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1056 wc.border_width = c->bw;
1057 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1058 XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel);
1059 configure(c); /* propagates border_width, if size doesn't change */
1060 updatewindowtype(c);
1063 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1066 c->isfloating = c->oldstate = trans != None || c->isfixed;
1068 XRaiseWindow(dpy, c->win);
1071 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1072 (unsigned char *) &(c->win), 1);
1073 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1074 setclientstate(c, NormalState);
1075 if (c->mon == selmon)
1076 unfocus(selmon->sel, 0);
1079 XMapWindow(dpy, c->win);
1084 mappingnotify(XEvent *e)
1086 XMappingEvent *ev = &e->xmapping;
1088 XRefreshKeyboardMapping(ev);
1089 if (ev->request == MappingKeyboard)
1094 maprequest(XEvent *e)
1096 static XWindowAttributes wa;
1097 XMapRequestEvent *ev = &e->xmaprequest;
1099 if (!XGetWindowAttributes(dpy, ev->window, &wa))
1101 if (wa.override_redirect)
1103 if (!wintoclient(ev->window))
1104 manage(ev->window, &wa);
1113 for (c = m->clients; c; c = c->next)
1116 if (n > 0) /* override layout symbol */
1117 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1118 for (c = nexttiled(m->clients); c; c = nexttiled(c->next))
1119 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
1123 motionnotify(XEvent *e)
1125 static Monitor *mon = NULL;
1127 XMotionEvent *ev = &e->xmotion;
1129 if (ev->window != root)
1131 if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1132 unfocus(selmon->sel, 1);
1140 movemouse(const Arg *arg)
1142 int x, y, ocx, ocy, nx, ny;
1148 if (!(c = selmon->sel))
1150 if (c->isfullscreen) /* no support moving fullscreen windows by mouse */
1155 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1156 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1158 if (!getrootptr(&x, &y))
1161 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1163 case ConfigureRequest:
1166 handler[ev.type](&ev);
1169 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1171 lasttime = ev.xmotion.time;
1173 nx = ocx + (ev.xmotion.x - x);
1174 ny = ocy + (ev.xmotion.y - y);
1175 if (abs(selmon->wx - nx) < snap)
1177 else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1178 nx = selmon->wx + selmon->ww - WIDTH(c);
1179 if (abs(selmon->wy - ny) < snap)
1181 else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1182 ny = selmon->wy + selmon->wh - HEIGHT(c);
1183 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1184 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1185 togglefloating(NULL);
1186 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1187 resize(c, nx, ny, c->w, c->h, 1);
1190 } while (ev.type != ButtonRelease);
1191 XUngrabPointer(dpy, CurrentTime);
1192 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1200 nexttiled(Client *c)
1202 for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1216 propertynotify(XEvent *e)
1220 XPropertyEvent *ev = &e->xproperty;
1222 if ((ev->window == root) && (ev->atom == XA_WM_NAME))
1224 else if (ev->state == PropertyDelete)
1225 return; /* ignore */
1226 else if ((c = wintoclient(ev->window))) {
1229 case XA_WM_TRANSIENT_FOR:
1230 if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1231 (c->isfloating = (wintoclient(trans)) != NULL))
1234 case XA_WM_NORMAL_HINTS:
1242 if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1244 if (c == c->mon->sel)
1247 if (ev->atom == netatom[NetWMWindowType])
1248 updatewindowtype(c);
1253 quit(const Arg *arg)
1259 recttomon(int x, int y, int w, int h)
1261 Monitor *m, *r = selmon;
1264 for (m = mons; m; m = m->next)
1265 if ((a = INTERSECT(x, y, w, h, m)) > area) {
1273 resize(Client *c, int x, int y, int w, int h, int interact)
1275 if (applysizehints(c, &x, &y, &w, &h, interact))
1276 resizeclient(c, x, y, w, h);
1280 resizeclient(Client *c, int x, int y, int w, int h)
1284 c->oldx = c->x; c->x = wc.x = x;
1285 c->oldy = c->y; c->y = wc.y = y;
1286 c->oldw = c->w; c->w = wc.width = w;
1287 c->oldh = c->h; c->h = wc.height = h;
1288 wc.border_width = c->bw;
1289 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1295 resizemouse(const Arg *arg)
1297 int ocx, ocy, nw, nh;
1303 if (!(c = selmon->sel))
1305 if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1310 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1311 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1313 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1315 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1317 case ConfigureRequest:
1320 handler[ev.type](&ev);
1323 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1325 lasttime = ev.xmotion.time;
1327 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1328 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1329 if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1330 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1332 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1333 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1334 togglefloating(NULL);
1336 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1337 resize(c, c->x, c->y, nw, nh, 1);
1340 } while (ev.type != ButtonRelease);
1341 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1342 XUngrabPointer(dpy, CurrentTime);
1343 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1344 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1361 if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
1362 XRaiseWindow(dpy, m->sel->win);
1363 if (m->lt[m->sellt]->arrange) {
1364 wc.stack_mode = Below;
1365 wc.sibling = m->barwin;
1366 for (c = m->stack; c; c = c->snext)
1367 if (!c->isfloating && ISVISIBLE(c)) {
1368 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1369 wc.sibling = c->win;
1373 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1380 /* main event loop */
1382 while (running && !XNextEvent(dpy, &ev))
1383 if (handler[ev.type])
1384 handler[ev.type](&ev); /* call handler */
1390 unsigned int i, num;
1391 Window d1, d2, *wins = NULL;
1392 XWindowAttributes wa;
1394 if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1395 for (i = 0; i < num; i++) {
1396 if (!XGetWindowAttributes(dpy, wins[i], &wa)
1397 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1399 if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1400 manage(wins[i], &wa);
1402 for (i = 0; i < num; i++) { /* now the transients */
1403 if (!XGetWindowAttributes(dpy, wins[i], &wa))
1405 if (XGetTransientForHint(dpy, wins[i], &d1)
1406 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1407 manage(wins[i], &wa);
1415 sendmon(Client *c, Monitor *m)
1423 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1431 setclientstate(Client *c, long state)
1433 long data[] = { state, None };
1435 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1436 PropModeReplace, (unsigned char *)data, 2);
1440 sendevent(Client *c, Atom proto)
1447 if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1448 while (!exists && n--)
1449 exists = protocols[n] == proto;
1453 ev.type = ClientMessage;
1454 ev.xclient.window = c->win;
1455 ev.xclient.message_type = wmatom[WMProtocols];
1456 ev.xclient.format = 32;
1457 ev.xclient.data.l[0] = proto;
1458 ev.xclient.data.l[1] = CurrentTime;
1459 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1467 if (!c->neverfocus) {
1468 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1469 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1470 XA_WINDOW, 32, PropModeReplace,
1471 (unsigned char *) &(c->win), 1);
1473 sendevent(c, wmatom[WMTakeFocus]);
1477 setfullscreen(Client *c, int fullscreen)
1479 if (fullscreen && !c->isfullscreen) {
1480 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1481 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1482 c->isfullscreen = 1;
1483 c->oldstate = c->isfloating;
1487 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1488 XRaiseWindow(dpy, c->win);
1489 } else if (!fullscreen && c->isfullscreen){
1490 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1491 PropModeReplace, (unsigned char*)0, 0);
1492 c->isfullscreen = 0;
1493 c->isfloating = c->oldstate;
1499 resizeclient(c, c->x, c->y, c->w, c->h);
1505 setgaps(const Arg *arg)
1507 if ((arg->i == 0) || (selmon->gappx + arg->i < 0))
1510 selmon->gappx += arg->i;
1515 setlayout(const Arg *arg)
1517 if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1520 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1521 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1528 /* arg > 1.0 will set mfact absolutely */
1530 setmfact(const Arg *arg)
1534 if (!arg || !selmon->lt[selmon->sellt]->arrange)
1536 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1537 if (f < 0.05 || f > 0.95)
1547 XSetWindowAttributes wa;
1550 /* clean up any zombies immediately */
1554 screen = DefaultScreen(dpy);
1555 sw = DisplayWidth(dpy, screen);
1556 sh = DisplayHeight(dpy, screen);
1557 root = RootWindow(dpy, screen);
1558 drw = drw_create(dpy, screen, root, sw, sh);
1559 if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
1560 die("no fonts could be loaded.");
1561 lrpad = drw->fonts->h;
1562 bh = drw->fonts->h + 2;
1565 utf8string = XInternAtom(dpy, "UTF8_STRING", False);
1566 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1567 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1568 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1569 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1570 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1571 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1572 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1573 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1574 netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
1575 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1576 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1577 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1578 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1580 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1581 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1582 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1583 /* init appearance */
1584 scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
1585 for (i = 0; i < LENGTH(colors); i++)
1586 scheme[i] = drw_scm_create(drw, colors[i], 3);
1590 /* supporting window for NetWMCheck */
1591 wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
1592 XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
1593 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1594 XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8,
1595 PropModeReplace, (unsigned char *) "dwm", 3);
1596 XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32,
1597 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1598 /* EWMH support per view */
1599 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1600 PropModeReplace, (unsigned char *) netatom, NetLast);
1601 XDeleteProperty(dpy, root, netatom[NetClientList]);
1603 wa.cursor = cursor[CurNormal]->cursor;
1604 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
1605 |ButtonPressMask|PointerMotionMask|EnterWindowMask
1606 |LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1607 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1608 XSelectInput(dpy, root, wa.event_mask);
1615 seturgent(Client *c, int urg)
1620 if (!(wmh = XGetWMHints(dpy, c->win)))
1622 wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
1623 XSetWMHints(dpy, c->win, wmh);
1633 /* show clients top down */
1634 XMoveWindow(dpy, c->win, c->x, c->y);
1635 if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1636 resize(c, c->x, c->y, c->w, c->h, 0);
1639 /* hide clients bottom up */
1641 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1648 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1649 die("can't install SIGCHLD handler:");
1650 while (0 < waitpid(-1, NULL, WNOHANG));
1654 spawn(const Arg *arg)
1656 if (arg->v == dmenucmd)
1657 dmenumon[0] = '0' + selmon->num;
1660 close(ConnectionNumber(dpy));
1662 execvp(((char **)arg->v)[0], (char **)arg->v);
1663 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1672 if (selmon->sel && arg->ui & TAGMASK) {
1673 selmon->sel->tags = arg->ui & TAGMASK;
1680 tagmon(const Arg *arg)
1682 if (!selmon->sel || !mons->next)
1684 sendmon(selmon->sel, dirtomon(arg->i));
1690 unsigned int i, n, h, mw, my, ty;
1693 for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1698 mw = m->nmaster ? m->ww * m->mfact : 0;
1700 mw = m->ww - m->gappx;
1701 for (i = 0, my = ty = m->gappx, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1702 if (i < m->nmaster) {
1703 h = (m->wh - my) / (MIN(n, m->nmaster) - i) - m->gappx;
1704 resize(c, m->wx + m->gappx, m->wy + my, mw - (2*c->bw) - m->gappx, h - (2*c->bw), 0);
1705 if (my + HEIGHT(c) + m->gappx < m->wh)
1706 my += HEIGHT(c) + m->gappx;
1708 h = (m->wh - ty) / (n - i) - m->gappx;
1709 resize(c, m->wx + mw + m->gappx, m->wy + ty, m->ww - mw - (2*c->bw) - 2*m->gappx, h - (2*c->bw), 0);
1710 if (ty + HEIGHT(c) + m->gappx < m->wh)
1711 ty += HEIGHT(c) + m->gappx;
1716 togglebar(const Arg *arg)
1718 selmon->showbar = !selmon->showbar;
1719 updatebarpos(selmon);
1720 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1725 togglefloating(const Arg *arg)
1729 if (selmon->sel->isfullscreen) /* no support for fullscreen windows */
1731 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1732 if (selmon->sel->isfloating)
1733 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1734 selmon->sel->w, selmon->sel->h, 0);
1739 toggletag(const Arg *arg)
1741 unsigned int newtags;
1745 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1747 selmon->sel->tags = newtags;
1754 toggleview(const Arg *arg)
1756 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1759 selmon->tagset[selmon->seltags] = newtagset;
1766 unfocus(Client *c, int setfocus)
1771 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
1773 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1774 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1779 unmanage(Client *c, int destroyed)
1781 Monitor *m = c->mon;
1787 wc.border_width = c->oldbw;
1788 XGrabServer(dpy); /* avoid race conditions */
1789 XSetErrorHandler(xerrordummy);
1790 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1791 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1792 setclientstate(c, WithdrawnState);
1794 XSetErrorHandler(xerror);
1804 unmapnotify(XEvent *e)
1807 XUnmapEvent *ev = &e->xunmap;
1809 if ((c = wintoclient(ev->window))) {
1811 setclientstate(c, WithdrawnState);
1821 XSetWindowAttributes wa = {
1822 .override_redirect = True,
1823 .background_pixmap = ParentRelative,
1824 .event_mask = ButtonPressMask|ExposureMask
1826 XClassHint ch = {"dwm", "dwm"};
1827 for (m = mons; m; m = m->next) {
1830 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1831 CopyFromParent, DefaultVisual(dpy, screen),
1832 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1833 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
1834 XMapRaised(dpy, m->barwin);
1835 XSetClassHint(dpy, m->barwin, &ch);
1840 updatebarpos(Monitor *m)
1846 m->by = m->topbar ? m->wy : m->wy + m->wh;
1847 m->wy = m->topbar ? m->wy + bh : m->wy;
1858 XDeleteProperty(dpy, root, netatom[NetClientList]);
1859 for (m = mons; m; m = m->next)
1860 for (c = m->clients; c; c = c->next)
1861 XChangeProperty(dpy, root, netatom[NetClientList],
1862 XA_WINDOW, 32, PropModeAppend,
1863 (unsigned char *) &(c->win), 1);
1872 if (XineramaIsActive(dpy)) {
1876 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1877 XineramaScreenInfo *unique = NULL;
1879 for (n = 0, m = mons; m; m = m->next, n++);
1880 /* only consider unique geometries as separate screens */
1881 unique = ecalloc(nn, sizeof(XineramaScreenInfo));
1882 for (i = 0, j = 0; i < nn; i++)
1883 if (isuniquegeom(unique, j, &info[i]))
1884 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1887 if (n <= nn) { /* new monitors available */
1888 for (i = 0; i < (nn - n); i++) {
1889 for (m = mons; m && m->next; m = m->next);
1891 m->next = createmon();
1895 for (i = 0, m = mons; i < nn && m; m = m->next, i++)
1897 || unique[i].x_org != m->mx || unique[i].y_org != m->my
1898 || unique[i].width != m->mw || unique[i].height != m->mh)
1902 m->mx = m->wx = unique[i].x_org;
1903 m->my = m->wy = unique[i].y_org;
1904 m->mw = m->ww = unique[i].width;
1905 m->mh = m->wh = unique[i].height;
1908 } else { /* less monitors available nn < n */
1909 for (i = nn; i < n; i++) {
1910 for (m = mons; m && m->next; m = m->next);
1911 while ((c = m->clients)) {
1913 m->clients = c->next;
1926 #endif /* XINERAMA */
1927 { /* default monitor setup */
1930 if (mons->mw != sw || mons->mh != sh) {
1932 mons->mw = mons->ww = sw;
1933 mons->mh = mons->wh = sh;
1939 selmon = wintomon(root);
1945 updatenumlockmask(void)
1948 XModifierKeymap *modmap;
1951 modmap = XGetModifierMapping(dpy);
1952 for (i = 0; i < 8; i++)
1953 for (j = 0; j < modmap->max_keypermod; j++)
1954 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1955 == XKeysymToKeycode(dpy, XK_Num_Lock))
1956 numlockmask = (1 << i);
1957 XFreeModifiermap(modmap);
1961 updatesizehints(Client *c)
1966 if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
1967 /* size is uninitialized, ensure that size.flags aren't used */
1969 if (size.flags & PBaseSize) {
1970 c->basew = size.base_width;
1971 c->baseh = size.base_height;
1972 } else if (size.flags & PMinSize) {
1973 c->basew = size.min_width;
1974 c->baseh = size.min_height;
1976 c->basew = c->baseh = 0;
1977 if (size.flags & PResizeInc) {
1978 c->incw = size.width_inc;
1979 c->inch = size.height_inc;
1981 c->incw = c->inch = 0;
1982 if (size.flags & PMaxSize) {
1983 c->maxw = size.max_width;
1984 c->maxh = size.max_height;
1986 c->maxw = c->maxh = 0;
1987 if (size.flags & PMinSize) {
1988 c->minw = size.min_width;
1989 c->minh = size.min_height;
1990 } else if (size.flags & PBaseSize) {
1991 c->minw = size.base_width;
1992 c->minh = size.base_height;
1994 c->minw = c->minh = 0;
1995 if (size.flags & PAspect) {
1996 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
1997 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
1999 c->maxa = c->mina = 0.0;
2000 c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh);
2006 if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2007 strcpy(stext, "dwm-"VERSION);
2012 updatetitle(Client *c)
2014 if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2015 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2016 if (c->name[0] == '\0') /* hack to mark broken clients */
2017 strcpy(c->name, broken);
2021 updatewindowtype(Client *c)
2023 Atom state = getatomprop(c, netatom[NetWMState]);
2024 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2026 if (state == netatom[NetWMFullscreen])
2027 setfullscreen(c, 1);
2028 if (wtype == netatom[NetWMWindowTypeDialog])
2033 updatewmhints(Client *c)
2037 if ((wmh = XGetWMHints(dpy, c->win))) {
2038 if (c == selmon->sel && wmh->flags & XUrgencyHint) {
2039 wmh->flags &= ~XUrgencyHint;
2040 XSetWMHints(dpy, c->win, wmh);
2042 c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
2043 if (wmh->flags & InputHint)
2044 c->neverfocus = !wmh->input;
2052 view(const Arg *arg)
2054 if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2056 selmon->seltags ^= 1; /* toggle sel tagset */
2057 if (arg->ui & TAGMASK)
2058 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2064 wintoclient(Window w)
2069 for (m = mons; m; m = m->next)
2070 for (c = m->clients; c; c = c->next)
2083 if (w == root && getrootptr(&x, &y))
2084 return recttomon(x, y, 1, 1);
2085 for (m = mons; m; m = m->next)
2088 if ((c = wintoclient(w)))
2093 /* There's no way to check accesses to destroyed windows, thus those cases are
2094 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2095 * default error handler, which may call exit. */
2097 xerror(Display *dpy, XErrorEvent *ee)
2099 if (ee->error_code == BadWindow
2100 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2101 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2102 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2103 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2104 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2105 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2106 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2107 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2109 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2110 ee->request_code, ee->error_code);
2111 return xerrorxlib(dpy, ee); /* may call exit */
2115 xerrordummy(Display *dpy, XErrorEvent *ee)
2120 /* Startup Error handler to check if another window manager
2121 * is already running. */
2123 xerrorstart(Display *dpy, XErrorEvent *ee)
2125 die("dwm: another window manager is already running");
2130 zoom(const Arg *arg)
2132 Client *c = selmon->sel;
2134 if (!selmon->lt[selmon->sellt]->arrange
2135 || (selmon->sel && selmon->sel->isfloating))
2137 if (c == nexttiled(selmon->clients))
2138 if (!c || !(c = nexttiled(c->next)))
2144 main(int argc, char *argv[])
2146 if (argc == 2 && !strcmp("-v", argv[1]))
2149 die("usage: dwm [-v]");
2150 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2151 fputs("warning: no locale support\n", stderr);
2152 if (!(dpy = XOpenDisplay(NULL)))
2153 die("dwm: cannot open display");
2157 if (pledge("stdio rpath proc exec", NULL) == -1)
2159 #endif /* __OpenBSD__ */
2164 return EXIT_SUCCESS;