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_text(drw, 0, 0, 0, 0, (X), 0) + drw->fonts[0]->h)
61 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
62 enum { SchemeNorm, SchemeSel, SchemeLast }; /* color schemes */
63 enum { NetSupported, NetWMName, NetWMState,
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 Bool 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 unsigned int seltags;
124 unsigned int tagset[2];
137 const char *instance;
144 /* function declarations */
145 static void applyrules(Client *c);
146 static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
147 static void arrange(Monitor *m);
148 static void arrangemon(Monitor *m);
149 static void attach(Client *c);
150 static void attachstack(Client *c);
151 static void buttonpress(XEvent *e);
152 static void checkotherwm(void);
153 static void cleanup(void);
154 static void cleanupmon(Monitor *mon);
155 static void clearurgent(Client *c);
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 Bool getrootptr(int *x, int *y);
174 static long getstate(Window w);
175 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
176 static void grabbuttons(Client *c, Bool focused);
177 static void grabkeys(void);
178 static void incnmaster(const Arg *arg);
179 static void keypress(XEvent *e);
180 static void killclient(const Arg *arg);
181 static void manage(Window w, XWindowAttributes *wa);
182 static void mappingnotify(XEvent *e);
183 static void maprequest(XEvent *e);
184 static void monocle(Monitor *m);
185 static void motionnotify(XEvent *e);
186 static void movemouse(const Arg *arg);
187 static Client *nexttiled(Client *c);
188 static void pop(Client *);
189 static void propertynotify(XEvent *e);
190 static void quit(const Arg *arg);
191 static Monitor *recttomon(int x, int y, int w, int h);
192 static void resize(Client *c, int x, int y, int w, int h, Bool interact);
193 static void resizeclient(Client *c, int x, int y, int w, int h);
194 static void resizemouse(const Arg *arg);
195 static void restack(Monitor *m);
196 static void run(void);
197 static void scan(void);
198 static Bool sendevent(Client *c, Atom proto);
199 static void sendmon(Client *c, Monitor *m);
200 static void setclientstate(Client *c, long state);
201 static void setfocus(Client *c);
202 static void setfullscreen(Client *c, Bool fullscreen);
203 static void setlayout(const Arg *arg);
204 static void setmfact(const Arg *arg);
205 static void setup(void);
206 static void showhide(Client *c);
207 static void sigchld(int unused);
208 static void spawn(const Arg *arg);
209 static void tag(const Arg *arg);
210 static void tagmon(const Arg *arg);
211 static void tile(Monitor *);
212 static void togglebar(const Arg *arg);
213 static void togglefloating(const Arg *arg);
214 static void toggletag(const Arg *arg);
215 static void toggleview(const Arg *arg);
216 static void unfocus(Client *c, Bool setfocus);
217 static void unmanage(Client *c, Bool destroyed);
218 static void unmapnotify(XEvent *e);
219 static Bool updategeom(void);
220 static void updatebarpos(Monitor *m);
221 static void updatebars(void);
222 static void updateclientlist(void);
223 static void updatenumlockmask(void);
224 static void updatesizehints(Client *c);
225 static void updatestatus(void);
226 static void updatewindowtype(Client *c);
227 static void updatetitle(Client *c);
228 static void updatewmhints(Client *c);
229 static void view(const Arg *arg);
230 static Client *wintoclient(Window w);
231 static Monitor *wintomon(Window w);
232 static int xerror(Display *dpy, XErrorEvent *ee);
233 static int xerrordummy(Display *dpy, XErrorEvent *ee);
234 static int xerrorstart(Display *dpy, XErrorEvent *ee);
235 static void zoom(const Arg *arg);
238 static const char broken[] = "broken";
239 static char stext[256];
241 static int sw, sh; /* X display screen geometry width, height */
242 static int bh, blw = 0; /* bar geometry */
243 static int (*xerrorxlib)(Display *, XErrorEvent *);
244 static unsigned int numlockmask = 0;
245 static void (*handler[LASTEvent]) (XEvent *) = {
246 [ButtonPress] = buttonpress,
247 [ClientMessage] = clientmessage,
248 [ConfigureRequest] = configurerequest,
249 [ConfigureNotify] = configurenotify,
250 [DestroyNotify] = destroynotify,
251 [EnterNotify] = enternotify,
254 [KeyPress] = keypress,
255 [MappingNotify] = mappingnotify,
256 [MapRequest] = maprequest,
257 [MotionNotify] = motionnotify,
258 [PropertyNotify] = propertynotify,
259 [UnmapNotify] = unmapnotify
261 static Atom wmatom[WMLast], netatom[NetLast];
262 static Bool running = True;
263 static Cur *cursor[CurLast];
264 static ClrScheme scheme[SchemeLast];
267 static Monitor *mons, *selmon;
270 /* configuration, allows nested code to access above variables */
273 /* compile-time check if all tags fit into an unsigned int bit array. */
274 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
276 /* function implementations */
278 applyrules(Client *c) {
279 const char *class, *instance;
283 XClassHint ch = { NULL, NULL };
286 c->isfloating = c->tags = 0;
287 XGetClassHint(dpy, c->win, &ch);
288 class = ch.res_class ? ch.res_class : broken;
289 instance = ch.res_name ? ch.res_name : broken;
291 for(i = 0; i < LENGTH(rules); i++) {
293 if((!r->title || strstr(c->name, r->title))
294 && (!r->class || strstr(class, r->class))
295 && (!r->instance || strstr(instance, r->instance)))
297 c->isfloating = r->isfloating;
299 for(m = mons; m && m->num != r->monitor; m = m->next);
308 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
312 applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
316 /* set minimum possible */
324 if(*x + *w + 2 * c->bw < 0)
326 if(*y + *h + 2 * c->bw < 0)
330 if(*x >= m->wx + m->ww)
331 *x = m->wx + m->ww - WIDTH(c);
332 if(*y >= m->wy + m->wh)
333 *y = m->wy + m->wh - HEIGHT(c);
334 if(*x + *w + 2 * c->bw <= m->wx)
336 if(*y + *h + 2 * c->bw <= m->wy)
343 if(resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
344 /* see last two sentences in ICCCM 4.1.2.3 */
345 baseismin = c->basew == c->minw && c->baseh == c->minh;
346 if(!baseismin) { /* temporarily remove base dimensions */
350 /* adjust for aspect limits */
351 if(c->mina > 0 && c->maxa > 0) {
352 if(c->maxa < (float)*w / *h)
353 *w = *h * c->maxa + 0.5;
354 else if(c->mina < (float)*h / *w)
355 *h = *w * c->mina + 0.5;
357 if(baseismin) { /* increment calculation requires this */
361 /* adjust for increment value */
366 /* restore base dimensions */
367 *w = MAX(*w + c->basew, c->minw);
368 *h = MAX(*h + c->baseh, c->minh);
370 *w = MIN(*w, c->maxw);
372 *h = MIN(*h, c->maxh);
374 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
378 arrange(Monitor *m) {
381 else for(m = mons; m; m = m->next)
386 } else for(m = mons; m; m = m->next)
391 arrangemon(Monitor *m) {
392 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
393 if(m->lt[m->sellt]->arrange)
394 m->lt[m->sellt]->arrange(m);
399 c->next = c->mon->clients;
404 attachstack(Client *c) {
405 c->snext = c->mon->stack;
410 buttonpress(XEvent *e) {
411 unsigned int i, x, click;
415 XButtonPressedEvent *ev = &e->xbutton;
418 /* focus monitor if necessary */
419 if((m = wintomon(ev->window)) && m != selmon) {
420 unfocus(selmon->sel, True);
424 if(ev->window == selmon->barwin) {
428 while(ev->x >= x && ++i < LENGTH(tags));
429 if(i < LENGTH(tags)) {
433 else if(ev->x < x + blw)
435 else if(ev->x > selmon->ww - TEXTW(stext))
436 click = ClkStatusText;
440 else if((c = wintoclient(ev->window))) {
442 click = ClkClientWin;
444 for(i = 0; i < LENGTH(buttons); i++)
445 if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
446 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
447 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
452 xerrorxlib = XSetErrorHandler(xerrorstart);
453 /* this causes an error if some other window manager is running */
454 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
456 XSetErrorHandler(xerror);
463 Layout foo = { "", NULL };
468 selmon->lt[selmon->sellt] = &foo;
469 for(m = mons; m; m = m->next)
471 unmanage(m->stack, False);
472 XUngrabKey(dpy, AnyKey, AnyModifier, root);
475 for(i = 0; i < CurLast; i++)
476 drw_cur_free(drw, cursor[i]);
477 for(i = 0; i < SchemeLast; i++) {
478 drw_clr_free(scheme[i].border);
479 drw_clr_free(scheme[i].bg);
480 drw_clr_free(scheme[i].fg);
484 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
485 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
489 cleanupmon(Monitor *mon) {
495 for(m = mons; m && m->next != mon; m = m->next);
498 XUnmapWindow(dpy, mon->barwin);
499 XDestroyWindow(dpy, mon->barwin);
504 clearurgent(Client *c) {
508 if(!(wmh = XGetWMHints(dpy, c->win)))
510 wmh->flags &= ~XUrgencyHint;
511 XSetWMHints(dpy, c->win, wmh);
516 clientmessage(XEvent *e) {
517 XClientMessageEvent *cme = &e->xclient;
518 Client *c = wintoclient(cme->window);
522 if(cme->message_type == netatom[NetWMState]) {
523 if(cme->data.l[1] == netatom[NetWMFullscreen] || cme->data.l[2] == netatom[NetWMFullscreen])
524 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
525 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
527 else if(cme->message_type == netatom[NetActiveWindow]) {
529 c->mon->seltags ^= 1;
530 c->mon->tagset[c->mon->seltags] = c->tags;
537 configure(Client *c) {
540 ce.type = ConfigureNotify;
548 ce.border_width = c->bw;
550 ce.override_redirect = False;
551 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
555 configurenotify(XEvent *e) {
557 XConfigureEvent *ev = &e->xconfigure;
560 /* TODO: updategeom handling sucks, needs to be simplified */
561 if(ev->window == root) {
562 dirty = (sw != ev->width || sh != ev->height);
565 if(updategeom() || dirty) {
566 drw_resize(drw, sw, bh);
568 for(m = mons; m; m = m->next)
569 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
577 configurerequest(XEvent *e) {
580 XConfigureRequestEvent *ev = &e->xconfigurerequest;
583 if((c = wintoclient(ev->window))) {
584 if(ev->value_mask & CWBorderWidth)
585 c->bw = ev->border_width;
586 else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
588 if(ev->value_mask & CWX) {
590 c->x = m->mx + ev->x;
592 if(ev->value_mask & CWY) {
594 c->y = m->my + ev->y;
596 if(ev->value_mask & CWWidth) {
600 if(ev->value_mask & CWHeight) {
604 if((c->x + c->w) > m->mx + m->mw && c->isfloating)
605 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
606 if((c->y + c->h) > m->my + m->mh && c->isfloating)
607 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
608 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
611 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
619 wc.width = ev->width;
620 wc.height = ev->height;
621 wc.border_width = ev->border_width;
622 wc.sibling = ev->above;
623 wc.stack_mode = ev->detail;
624 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
633 m = ecalloc(1, sizeof(Monitor));
634 m->tagset[0] = m->tagset[1] = 1;
636 m->nmaster = nmaster;
637 m->showbar = showbar;
639 m->lt[0] = &layouts[0];
640 m->lt[1] = &layouts[1 % LENGTH(layouts)];
641 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
646 destroynotify(XEvent *e) {
648 XDestroyWindowEvent *ev = &e->xdestroywindow;
650 if((c = wintoclient(ev->window)))
658 for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
663 detachstack(Client *c) {
666 for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
669 if(c == c->mon->sel) {
670 for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
680 if(!(m = selmon->next))
683 else if(selmon == mons)
684 for(m = mons; m->next; m = m->next);
686 for(m = mons; m->next != selmon; m = m->next);
691 drawbar(Monitor *m) {
693 unsigned int i, occ = 0, urg = 0;
696 dx = (drw->fonts[0]->ascent + drw->fonts[0]->descent + 2) / 4;
698 for(c = m->clients; c; c = c->next) {
704 for(i = 0; i < LENGTH(tags); i++) {
706 drw_setscheme(drw, m->tagset[m->seltags] & 1 << i ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
707 drw_text(drw, x, 0, w, bh, tags[i], urg & 1 << i);
708 drw_rect(drw, x + 1, 1, dx, dx, m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
709 occ & 1 << i, urg & 1 << i);
712 w = blw = TEXTW(m->ltsymbol);
713 drw_setscheme(drw, &scheme[SchemeNorm]);
714 drw_text(drw, x, 0, w, bh, m->ltsymbol, 0);
717 if(m == selmon) { /* status is only drawn on selected monitor */
724 drw_text(drw, x, 0, w, bh, stext, 0);
728 if((w = x - xx) > bh) {
731 drw_setscheme(drw, m == selmon ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
732 drw_text(drw, x, 0, w, bh, m->sel->name, 0);
733 drw_rect(drw, x + 1, 1, dx, dx, m->sel->isfixed, m->sel->isfloating, 0);
736 drw_setscheme(drw, &scheme[SchemeNorm]);
737 drw_rect(drw, x, 0, w, bh, 1, 0, 1);
740 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
747 for(m = mons; m; m = m->next)
752 enternotify(XEvent *e) {
755 XCrossingEvent *ev = &e->xcrossing;
757 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
759 c = wintoclient(ev->window);
760 m = c ? c->mon : wintomon(ev->window);
762 unfocus(selmon->sel, True);
765 else if(!c || c == selmon->sel)
773 XExposeEvent *ev = &e->xexpose;
775 if(ev->count == 0 && (m = wintomon(ev->window)))
781 if(!c || !ISVISIBLE(c))
782 for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
783 /* was if(selmon->sel) */
784 if(selmon->sel && selmon->sel != c)
785 unfocus(selmon->sel, False);
793 grabbuttons(c, True);
794 XSetWindowBorder(dpy, c->win, scheme[SchemeSel].border->pix);
798 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
799 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
806 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
807 XFocusChangeEvent *ev = &e->xfocus;
809 if(selmon->sel && ev->window != selmon->sel->win)
810 setfocus(selmon->sel);
814 focusmon(const Arg *arg) {
819 if((m = dirtomon(arg->i)) == selmon)
821 unfocus(selmon->sel, False); /* s/True/False/ fixes input focus issues
822 in gedit and anjuta */
828 focusstack(const Arg *arg) {
829 Client *c = NULL, *i;
834 for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
836 for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
839 for(i = selmon->clients; i != selmon->sel; i = i->next)
843 for(; i; i = i->next)
854 getatomprop(Client *c, Atom prop) {
857 unsigned char *p = NULL;
858 Atom da, atom = None;
860 if(XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
861 &da, &di, &dl, &dl, &p) == Success && p) {
869 getrootptr(int *x, int *y) {
874 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
881 unsigned char *p = NULL;
882 unsigned long n, extra;
885 if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
886 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
895 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
900 if(!text || size == 0)
903 XGetTextProperty(dpy, w, &name, atom);
906 if(name.encoding == XA_STRING)
907 strncpy(text, (char *)name.value, size - 1);
909 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
910 strncpy(text, *list, size - 1);
911 XFreeStringList(list);
914 text[size - 1] = '\0';
920 grabbuttons(Client *c, Bool focused) {
924 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
925 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
927 for(i = 0; i < LENGTH(buttons); i++)
928 if(buttons[i].click == ClkClientWin)
929 for(j = 0; j < LENGTH(modifiers); j++)
930 XGrabButton(dpy, buttons[i].button,
931 buttons[i].mask | modifiers[j],
932 c->win, False, BUTTONMASK,
933 GrabModeAsync, GrabModeSync, None, None);
936 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
937 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
946 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
949 XUngrabKey(dpy, AnyKey, AnyModifier, root);
950 for(i = 0; i < LENGTH(keys); i++)
951 if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
952 for(j = 0; j < LENGTH(modifiers); j++)
953 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
954 True, GrabModeAsync, GrabModeAsync);
959 incnmaster(const Arg *arg) {
960 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
966 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
968 if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
969 && unique[n].width == info->width && unique[n].height == info->height)
973 #endif /* XINERAMA */
976 keypress(XEvent *e) {
982 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
983 for(i = 0; i < LENGTH(keys); i++)
984 if(keysym == keys[i].keysym
985 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
987 keys[i].func(&(keys[i].arg));
991 killclient(const Arg *arg) {
994 if(!sendevent(selmon->sel, wmatom[WMDelete])) {
996 XSetErrorHandler(xerrordummy);
997 XSetCloseDownMode(dpy, DestroyAll);
998 XKillClient(dpy, selmon->sel->win);
1000 XSetErrorHandler(xerror);
1006 manage(Window w, XWindowAttributes *wa) {
1007 Client *c, *t = NULL;
1008 Window trans = None;
1011 c = ecalloc(1, sizeof(Client));
1014 if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1023 c->x = c->oldx = wa->x;
1024 c->y = c->oldy = wa->y;
1025 c->w = c->oldw = wa->width;
1026 c->h = c->oldh = wa->height;
1027 c->oldbw = wa->border_width;
1029 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1030 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1031 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1032 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1033 c->x = MAX(c->x, c->mon->mx);
1034 /* only fix client y-offset, if the client center might cover the bar */
1035 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1036 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1039 wc.border_width = c->bw;
1040 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1041 XSetWindowBorder(dpy, w, scheme[SchemeNorm].border->pix);
1042 configure(c); /* propagates border_width, if size doesn't change */
1043 updatewindowtype(c);
1046 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1047 grabbuttons(c, False);
1049 c->isfloating = c->oldstate = trans != None || c->isfixed;
1051 XRaiseWindow(dpy, c->win);
1054 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1055 (unsigned char *) &(c->win), 1);
1056 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1057 setclientstate(c, NormalState);
1058 if (c->mon == selmon)
1059 unfocus(selmon->sel, False);
1062 XMapWindow(dpy, c->win);
1067 mappingnotify(XEvent *e) {
1068 XMappingEvent *ev = &e->xmapping;
1070 XRefreshKeyboardMapping(ev);
1071 if(ev->request == MappingKeyboard)
1076 maprequest(XEvent *e) {
1077 static XWindowAttributes wa;
1078 XMapRequestEvent *ev = &e->xmaprequest;
1080 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1082 if(wa.override_redirect)
1084 if(!wintoclient(ev->window))
1085 manage(ev->window, &wa);
1089 monocle(Monitor *m) {
1093 for(c = m->clients; c; c = c->next)
1096 if(n > 0) /* override layout symbol */
1097 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1098 for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
1099 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
1103 motionnotify(XEvent *e) {
1104 static Monitor *mon = NULL;
1106 XMotionEvent *ev = &e->xmotion;
1108 if(ev->window != root)
1110 if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1111 unfocus(selmon->sel, True);
1119 movemouse(const Arg *arg) {
1120 int x, y, ocx, ocy, nx, ny;
1126 if(!(c = selmon->sel))
1128 if(c->isfullscreen) /* no support moving fullscreen windows by mouse */
1133 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1134 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1136 if(!getrootptr(&x, &y))
1139 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1141 case ConfigureRequest:
1144 handler[ev.type](&ev);
1147 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1149 lasttime = ev.xmotion.time;
1151 nx = ocx + (ev.xmotion.x - x);
1152 ny = ocy + (ev.xmotion.y - y);
1153 if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1154 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1155 if(abs(selmon->wx - nx) < snap)
1157 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1158 nx = selmon->wx + selmon->ww - WIDTH(c);
1159 if(abs(selmon->wy - ny) < snap)
1161 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1162 ny = selmon->wy + selmon->wh - HEIGHT(c);
1163 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1164 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1165 togglefloating(NULL);
1167 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1168 resize(c, nx, ny, c->w, c->h, True);
1171 } while(ev.type != ButtonRelease);
1172 XUngrabPointer(dpy, CurrentTime);
1173 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1181 nexttiled(Client *c) {
1182 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1195 propertynotify(XEvent *e) {
1198 XPropertyEvent *ev = &e->xproperty;
1200 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1202 else if(ev->state == PropertyDelete)
1203 return; /* ignore */
1204 else if((c = wintoclient(ev->window))) {
1207 case XA_WM_TRANSIENT_FOR:
1208 if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1209 (c->isfloating = (wintoclient(trans)) != NULL))
1212 case XA_WM_NORMAL_HINTS:
1220 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1222 if(c == c->mon->sel)
1225 if(ev->atom == netatom[NetWMWindowType])
1226 updatewindowtype(c);
1231 quit(const Arg *arg) {
1236 recttomon(int x, int y, int w, int h) {
1237 Monitor *m, *r = selmon;
1240 for(m = mons; m; m = m->next)
1241 if((a = INTERSECT(x, y, w, h, m)) > area) {
1249 resize(Client *c, int x, int y, int w, int h, Bool interact) {
1250 if(applysizehints(c, &x, &y, &w, &h, interact))
1251 resizeclient(c, x, y, w, h);
1255 resizeclient(Client *c, int x, int y, int w, int h) {
1258 c->oldx = c->x; c->x = wc.x = x;
1259 c->oldy = c->y; c->y = wc.y = y;
1260 c->oldw = c->w; c->w = wc.width = w;
1261 c->oldh = c->h; c->h = wc.height = h;
1262 wc.border_width = c->bw;
1263 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1269 resizemouse(const Arg *arg) {
1270 int ocx, ocy, nw, nh;
1276 if(!(c = selmon->sel))
1278 if(c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1283 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1284 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1286 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1288 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1290 case ConfigureRequest:
1293 handler[ev.type](&ev);
1296 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1298 lasttime = ev.xmotion.time;
1300 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1301 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1302 if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1303 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1305 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1306 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1307 togglefloating(NULL);
1309 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1310 resize(c, c->x, c->y, nw, nh, True);
1313 } while(ev.type != ButtonRelease);
1314 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1315 XUngrabPointer(dpy, CurrentTime);
1316 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1317 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1325 restack(Monitor *m) {
1333 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1334 XRaiseWindow(dpy, m->sel->win);
1335 if(m->lt[m->sellt]->arrange) {
1336 wc.stack_mode = Below;
1337 wc.sibling = m->barwin;
1338 for(c = m->stack; c; c = c->snext)
1339 if(!c->isfloating && ISVISIBLE(c)) {
1340 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1341 wc.sibling = c->win;
1345 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1351 /* main event loop */
1353 while(running && !XNextEvent(dpy, &ev))
1354 if(handler[ev.type])
1355 handler[ev.type](&ev); /* call handler */
1360 unsigned int i, num;
1361 Window d1, d2, *wins = NULL;
1362 XWindowAttributes wa;
1364 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1365 for(i = 0; i < num; i++) {
1366 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1367 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1369 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1370 manage(wins[i], &wa);
1372 for(i = 0; i < num; i++) { /* now the transients */
1373 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1375 if(XGetTransientForHint(dpy, wins[i], &d1)
1376 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1377 manage(wins[i], &wa);
1385 sendmon(Client *c, Monitor *m) {
1392 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1400 setclientstate(Client *c, long state) {
1401 long data[] = { state, None };
1403 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1404 PropModeReplace, (unsigned char *)data, 2);
1408 sendevent(Client *c, Atom proto) {
1411 Bool exists = False;
1414 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1415 while(!exists && n--)
1416 exists = protocols[n] == proto;
1420 ev.type = ClientMessage;
1421 ev.xclient.window = c->win;
1422 ev.xclient.message_type = wmatom[WMProtocols];
1423 ev.xclient.format = 32;
1424 ev.xclient.data.l[0] = proto;
1425 ev.xclient.data.l[1] = CurrentTime;
1426 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1432 setfocus(Client *c) {
1433 if(!c->neverfocus) {
1434 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1435 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1436 XA_WINDOW, 32, PropModeReplace,
1437 (unsigned char *) &(c->win), 1);
1439 sendevent(c, wmatom[WMTakeFocus]);
1443 setfullscreen(Client *c, Bool fullscreen) {
1445 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1446 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1447 c->isfullscreen = True;
1448 c->oldstate = c->isfloating;
1451 c->isfloating = True;
1452 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1453 XRaiseWindow(dpy, c->win);
1456 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1457 PropModeReplace, (unsigned char*)0, 0);
1458 c->isfullscreen = False;
1459 c->isfloating = c->oldstate;
1465 resizeclient(c, c->x, c->y, c->w, c->h);
1471 setlayout(const Arg *arg) {
1472 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1475 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1476 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1483 /* arg > 1.0 will set mfact absolutly */
1485 setmfact(const Arg *arg) {
1488 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1490 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1491 if(f < 0.1 || f > 0.9)
1499 XSetWindowAttributes wa;
1501 /* clean up any zombies immediately */
1505 screen = DefaultScreen(dpy);
1506 sw = DisplayWidth(dpy, screen);
1507 sh = DisplayHeight(dpy, screen);
1508 root = RootWindow(dpy, screen);
1509 drw = drw_create(dpy, screen, root, sw, sh);
1510 drw_load_fonts(drw, fonts, LENGTH(fonts));
1511 if (!drw->fontcount)
1512 die("no fonts could be loaded.\n");
1513 bh = drw->fonts[0]->h + 2;
1516 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1517 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1518 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1519 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1520 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1521 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1522 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1523 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1524 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1525 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1526 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1527 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1529 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1530 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1531 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1532 /* init appearance */
1533 scheme[SchemeNorm].border = drw_clr_create(drw, normbordercolor);
1534 scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor);
1535 scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor);
1536 scheme[SchemeSel].border = drw_clr_create(drw, selbordercolor);
1537 scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor);
1538 scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor);
1542 /* EWMH support per view */
1543 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1544 PropModeReplace, (unsigned char *) netatom, NetLast);
1545 XDeleteProperty(dpy, root, netatom[NetClientList]);
1546 /* select for events */
1547 wa.cursor = cursor[CurNormal]->cursor;
1548 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
1549 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1550 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1551 XSelectInput(dpy, root, wa.event_mask);
1557 showhide(Client *c) {
1560 if(ISVISIBLE(c)) { /* show clients top down */
1561 XMoveWindow(dpy, c->win, c->x, c->y);
1562 if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1563 resize(c, c->x, c->y, c->w, c->h, False);
1566 else { /* hide clients bottom up */
1568 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1573 sigchld(int unused) {
1574 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1575 die("can't install SIGCHLD handler:");
1576 while(0 < waitpid(-1, NULL, WNOHANG));
1580 spawn(const Arg *arg) {
1581 if(arg->v == dmenucmd)
1582 dmenumon[0] = '0' + selmon->num;
1585 close(ConnectionNumber(dpy));
1587 execvp(((char **)arg->v)[0], (char **)arg->v);
1588 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1595 tag(const Arg *arg) {
1596 if(selmon->sel && arg->ui & TAGMASK) {
1597 selmon->sel->tags = arg->ui & TAGMASK;
1604 tagmon(const Arg *arg) {
1605 if(!selmon->sel || !mons->next)
1607 sendmon(selmon->sel, dirtomon(arg->i));
1612 unsigned int i, n, h, mw, my, ty;
1615 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1620 mw = m->nmaster ? m->ww * m->mfact : 0;
1623 for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1624 if(i < m->nmaster) {
1625 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1626 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False);
1630 h = (m->wh - ty) / (n - i);
1631 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), False);
1637 togglebar(const Arg *arg) {
1638 selmon->showbar = !selmon->showbar;
1639 updatebarpos(selmon);
1640 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1645 togglefloating(const Arg *arg) {
1648 if(selmon->sel->isfullscreen) /* no support for fullscreen windows */
1650 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1651 if(selmon->sel->isfloating)
1652 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1653 selmon->sel->w, selmon->sel->h, False);
1658 toggletag(const Arg *arg) {
1659 unsigned int newtags;
1663 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1665 selmon->sel->tags = newtags;
1672 toggleview(const Arg *arg) {
1673 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1676 selmon->tagset[selmon->seltags] = newtagset;
1683 unfocus(Client *c, Bool setfocus) {
1686 grabbuttons(c, False);
1687 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->pix);
1689 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1690 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1695 unmanage(Client *c, Bool destroyed) {
1696 Monitor *m = c->mon;
1699 /* The server grab construct avoids race conditions. */
1703 wc.border_width = c->oldbw;
1705 XSetErrorHandler(xerrordummy);
1706 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1707 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1708 setclientstate(c, WithdrawnState);
1710 XSetErrorHandler(xerror);
1720 unmapnotify(XEvent *e) {
1722 XUnmapEvent *ev = &e->xunmap;
1724 if((c = wintoclient(ev->window))) {
1726 setclientstate(c, WithdrawnState);
1735 XSetWindowAttributes wa = {
1736 .override_redirect = True,
1737 .background_pixmap = ParentRelative,
1738 .event_mask = ButtonPressMask|ExposureMask
1740 for(m = mons; m; m = m->next) {
1743 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1744 CopyFromParent, DefaultVisual(dpy, screen),
1745 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1746 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
1747 XMapRaised(dpy, m->barwin);
1752 updatebarpos(Monitor *m) {
1757 m->by = m->topbar ? m->wy : m->wy + m->wh;
1758 m->wy = m->topbar ? m->wy + bh : m->wy;
1765 updateclientlist() {
1769 XDeleteProperty(dpy, root, netatom[NetClientList]);
1770 for(m = mons; m; m = m->next)
1771 for(c = m->clients; c; c = c->next)
1772 XChangeProperty(dpy, root, netatom[NetClientList],
1773 XA_WINDOW, 32, PropModeAppend,
1774 (unsigned char *) &(c->win), 1);
1782 if(XineramaIsActive(dpy)) {
1786 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1787 XineramaScreenInfo *unique = NULL;
1789 for(n = 0, m = mons; m; m = m->next, n++);
1790 /* only consider unique geometries as separate screens */
1791 unique = ecalloc(nn, sizeof(XineramaScreenInfo));
1792 for(i = 0, j = 0; i < nn; i++)
1793 if(isuniquegeom(unique, j, &info[i]))
1794 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1798 for(i = 0; i < (nn - n); i++) { /* new monitors available */
1799 for(m = mons; m && m->next; m = m->next);
1801 m->next = createmon();
1805 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
1807 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1808 || unique[i].width != m->mw || unique[i].height != m->mh))
1812 m->mx = m->wx = unique[i].x_org;
1813 m->my = m->wy = unique[i].y_org;
1814 m->mw = m->ww = unique[i].width;
1815 m->mh = m->wh = unique[i].height;
1819 else { /* less monitors available nn < n */
1820 for(i = nn; i < n; i++) {
1821 for(m = mons; m && m->next; m = m->next);
1825 m->clients = c->next;
1839 #endif /* XINERAMA */
1840 /* default monitor setup */
1844 if(mons->mw != sw || mons->mh != sh) {
1846 mons->mw = mons->ww = sw;
1847 mons->mh = mons->wh = sh;
1853 selmon = wintomon(root);
1859 updatenumlockmask(void) {
1861 XModifierKeymap *modmap;
1864 modmap = XGetModifierMapping(dpy);
1865 for(i = 0; i < 8; i++)
1866 for(j = 0; j < modmap->max_keypermod; j++)
1867 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1868 == XKeysymToKeycode(dpy, XK_Num_Lock))
1869 numlockmask = (1 << i);
1870 XFreeModifiermap(modmap);
1874 updatesizehints(Client *c) {
1878 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
1879 /* size is uninitialized, ensure that size.flags aren't used */
1881 if(size.flags & PBaseSize) {
1882 c->basew = size.base_width;
1883 c->baseh = size.base_height;
1885 else if(size.flags & PMinSize) {
1886 c->basew = size.min_width;
1887 c->baseh = size.min_height;
1890 c->basew = c->baseh = 0;
1891 if(size.flags & PResizeInc) {
1892 c->incw = size.width_inc;
1893 c->inch = size.height_inc;
1896 c->incw = c->inch = 0;
1897 if(size.flags & PMaxSize) {
1898 c->maxw = size.max_width;
1899 c->maxh = size.max_height;
1902 c->maxw = c->maxh = 0;
1903 if(size.flags & PMinSize) {
1904 c->minw = size.min_width;
1905 c->minh = size.min_height;
1907 else if(size.flags & PBaseSize) {
1908 c->minw = size.base_width;
1909 c->minh = size.base_height;
1912 c->minw = c->minh = 0;
1913 if(size.flags & PAspect) {
1914 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
1915 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
1918 c->maxa = c->mina = 0.0;
1919 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
1920 && c->maxw == c->minw && c->maxh == c->minh);
1924 updatetitle(Client *c) {
1925 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
1926 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
1927 if(c->name[0] == '\0') /* hack to mark broken clients */
1928 strcpy(c->name, broken);
1932 updatestatus(void) {
1933 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
1934 strcpy(stext, "dwm-"VERSION);
1939 updatewindowtype(Client *c) {
1940 Atom state = getatomprop(c, netatom[NetWMState]);
1941 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
1943 if(state == netatom[NetWMFullscreen])
1944 setfullscreen(c, True);
1945 if(wtype == netatom[NetWMWindowTypeDialog])
1946 c->isfloating = True;
1950 updatewmhints(Client *c) {
1953 if((wmh = XGetWMHints(dpy, c->win))) {
1954 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
1955 wmh->flags &= ~XUrgencyHint;
1956 XSetWMHints(dpy, c->win, wmh);
1959 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
1960 if(wmh->flags & InputHint)
1961 c->neverfocus = !wmh->input;
1963 c->neverfocus = False;
1969 view(const Arg *arg) {
1970 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
1972 selmon->seltags ^= 1; /* toggle sel tagset */
1973 if(arg->ui & TAGMASK)
1974 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
1980 wintoclient(Window w) {
1984 for(m = mons; m; m = m->next)
1985 for(c = m->clients; c; c = c->next)
1992 wintomon(Window w) {
1997 if(w == root && getrootptr(&x, &y))
1998 return recttomon(x, y, 1, 1);
1999 for(m = mons; m; m = m->next)
2002 if((c = wintoclient(w)))
2007 /* There's no way to check accesses to destroyed windows, thus those cases are
2008 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2009 * default error handler, which may call exit. */
2011 xerror(Display *dpy, XErrorEvent *ee) {
2012 if(ee->error_code == BadWindow
2013 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2014 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2015 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2016 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2017 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2018 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2019 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2020 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2022 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2023 ee->request_code, ee->error_code);
2024 return xerrorxlib(dpy, ee); /* may call exit */
2028 xerrordummy(Display *dpy, XErrorEvent *ee) {
2032 /* Startup Error handler to check if another window manager
2033 * is already running. */
2035 xerrorstart(Display *dpy, XErrorEvent *ee) {
2036 die("dwm: another window manager is already running\n");
2041 zoom(const Arg *arg) {
2042 Client *c = selmon->sel;
2044 if(!selmon->lt[selmon->sellt]->arrange
2045 || (selmon->sel && selmon->sel->isfloating))
2047 if(c == nexttiled(selmon->clients))
2048 if(!c || !(c = nexttiled(c->next)))
2054 main(int argc, char *argv[]) {
2055 if(argc == 2 && !strcmp("-v", argv[1]))
2056 die("dwm-"VERSION", © 2006-2015 dwm engineers, see LICENSE for details\n");
2058 die("usage: dwm [-v]\n");
2059 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2060 fputs("warning: no locale support\n", stderr);
2061 if(!(dpy = XOpenDisplay(NULL)))
2062 die("dwm: cannot open display\n");
2069 return EXIT_SUCCESS;