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 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 unsigned int seltags;
124 unsigned int tagset[2];
137 const char *instance;
144 /* function declarations */
145 static void applyrules(Client *c);
146 static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int 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 int getrootptr(int *x, int *y);
174 static long getstate(Window w);
175 static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
176 static void grabbuttons(Client *c, int 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, int 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 int 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, int 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, int setfocus);
217 static void unmanage(Client *c, int destroyed);
218 static void unmapnotify(XEvent *e);
219 static int 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 int running = 1;
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 };
288 XGetClassHint(dpy, c->win, &ch);
289 class = ch.res_class ? ch.res_class : broken;
290 instance = ch.res_name ? ch.res_name : broken;
292 for(i = 0; i < LENGTH(rules); i++) {
294 if((!r->title || strstr(c->name, r->title))
295 && (!r->class || strstr(class, r->class))
296 && (!r->instance || strstr(instance, r->instance)))
298 c->isfloating = r->isfloating;
300 for(m = mons; m && m->num != r->monitor; m = m->next);
309 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
313 applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact) {
317 /* set minimum possible */
325 if(*x + *w + 2 * c->bw < 0)
327 if(*y + *h + 2 * c->bw < 0)
331 if(*x >= m->wx + m->ww)
332 *x = m->wx + m->ww - WIDTH(c);
333 if(*y >= m->wy + m->wh)
334 *y = m->wy + m->wh - HEIGHT(c);
335 if(*x + *w + 2 * c->bw <= m->wx)
337 if(*y + *h + 2 * c->bw <= m->wy)
344 if(resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
345 /* see last two sentences in ICCCM 4.1.2.3 */
346 baseismin = c->basew == c->minw && c->baseh == c->minh;
347 if(!baseismin) { /* temporarily remove base dimensions */
351 /* adjust for aspect limits */
352 if(c->mina > 0 && c->maxa > 0) {
353 if(c->maxa < (float)*w / *h)
354 *w = *h * c->maxa + 0.5;
355 else if(c->mina < (float)*h / *w)
356 *h = *w * c->mina + 0.5;
358 if(baseismin) { /* increment calculation requires this */
362 /* adjust for increment value */
367 /* restore base dimensions */
368 *w = MAX(*w + c->basew, c->minw);
369 *h = MAX(*h + c->baseh, c->minh);
371 *w = MIN(*w, c->maxw);
373 *h = MIN(*h, c->maxh);
375 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
379 arrange(Monitor *m) {
382 else for(m = mons; m; m = m->next)
387 } else for(m = mons; m; m = m->next)
392 arrangemon(Monitor *m) {
393 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
394 if(m->lt[m->sellt]->arrange)
395 m->lt[m->sellt]->arrange(m);
400 c->next = c->mon->clients;
405 attachstack(Client *c) {
406 c->snext = c->mon->stack;
411 buttonpress(XEvent *e) {
412 unsigned int i, x, click;
416 XButtonPressedEvent *ev = &e->xbutton;
419 /* focus monitor if necessary */
420 if((m = wintomon(ev->window)) && m != selmon) {
421 unfocus(selmon->sel, 1);
425 if(ev->window == selmon->barwin) {
429 while(ev->x >= x && ++i < LENGTH(tags));
430 if(i < LENGTH(tags)) {
434 else if(ev->x < x + blw)
436 else if(ev->x > selmon->ww - TEXTW(stext))
437 click = ClkStatusText;
441 else if((c = wintoclient(ev->window))) {
443 click = ClkClientWin;
445 for(i = 0; i < LENGTH(buttons); i++)
446 if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
447 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
448 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
453 xerrorxlib = XSetErrorHandler(xerrorstart);
454 /* this causes an error if some other window manager is running */
455 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
457 XSetErrorHandler(xerror);
464 Layout foo = { "", NULL };
469 selmon->lt[selmon->sellt] = &foo;
470 for(m = mons; m; m = m->next)
472 unmanage(m->stack, 0);
473 XUngrabKey(dpy, AnyKey, AnyModifier, root);
476 for(i = 0; i < CurLast; i++)
477 drw_cur_free(drw, cursor[i]);
478 for(i = 0; i < SchemeLast; i++) {
479 drw_clr_free(scheme[i].border);
480 drw_clr_free(scheme[i].bg);
481 drw_clr_free(scheme[i].fg);
485 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
486 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
490 cleanupmon(Monitor *mon) {
496 for(m = mons; m && m->next != mon; m = m->next);
499 XUnmapWindow(dpy, mon->barwin);
500 XDestroyWindow(dpy, mon->barwin);
505 clearurgent(Client *c) {
509 if(!(wmh = XGetWMHints(dpy, c->win)))
511 wmh->flags &= ~XUrgencyHint;
512 XSetWMHints(dpy, c->win, wmh);
517 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] || cme->data.l[2] == netatom[NetWMFullscreen])
525 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
526 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
528 else if(cme->message_type == netatom[NetActiveWindow]) {
530 c->mon->seltags ^= 1;
531 c->mon->tagset[c->mon->seltags] = c->tags;
538 configure(Client *c) {
541 ce.type = ConfigureNotify;
549 ce.border_width = c->bw;
551 ce.override_redirect = False;
552 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
556 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 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
578 configurerequest(XEvent *e) {
581 XConfigureRequestEvent *ev = &e->xconfigurerequest;
584 if((c = wintoclient(ev->window))) {
585 if(ev->value_mask & CWBorderWidth)
586 c->bw = ev->border_width;
587 else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
589 if(ev->value_mask & CWX) {
591 c->x = m->mx + ev->x;
593 if(ev->value_mask & CWY) {
595 c->y = m->my + ev->y;
597 if(ev->value_mask & CWWidth) {
601 if(ev->value_mask & CWHeight) {
605 if((c->x + c->w) > m->mx + m->mw && c->isfloating)
606 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
607 if((c->y + c->h) > m->my + m->mh && c->isfloating)
608 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
609 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
612 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
620 wc.width = ev->width;
621 wc.height = ev->height;
622 wc.border_width = ev->border_width;
623 wc.sibling = ev->above;
624 wc.stack_mode = ev->detail;
625 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
634 m = ecalloc(1, sizeof(Monitor));
635 m->tagset[0] = m->tagset[1] = 1;
637 m->nmaster = nmaster;
638 m->showbar = showbar;
640 m->lt[0] = &layouts[0];
641 m->lt[1] = &layouts[1 % LENGTH(layouts)];
642 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
647 destroynotify(XEvent *e) {
649 XDestroyWindowEvent *ev = &e->xdestroywindow;
651 if((c = wintoclient(ev->window)))
659 for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
664 detachstack(Client *c) {
667 for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
670 if(c == c->mon->sel) {
671 for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
681 if(!(m = selmon->next))
684 else if(selmon == mons)
685 for(m = mons; m->next; m = m->next);
687 for(m = mons; m->next != selmon; m = m->next);
692 drawbar(Monitor *m) {
694 unsigned int i, occ = 0, urg = 0;
697 dx = (drw->fonts[0]->ascent + drw->fonts[0]->descent + 2) / 4;
699 for(c = m->clients; c; c = c->next) {
705 for(i = 0; i < LENGTH(tags); i++) {
707 drw_setscheme(drw, m->tagset[m->seltags] & 1 << i ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
708 drw_text(drw, x, 0, w, bh, tags[i], urg & 1 << i);
709 drw_rect(drw, x + 1, 1, dx, dx, m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
710 occ & 1 << i, urg & 1 << i);
713 w = blw = TEXTW(m->ltsymbol);
714 drw_setscheme(drw, &scheme[SchemeNorm]);
715 drw_text(drw, x, 0, w, bh, m->ltsymbol, 0);
718 if(m == selmon) { /* status is only drawn on selected monitor */
725 drw_text(drw, x, 0, w, bh, stext, 0);
729 if((w = x - xx) > bh) {
732 drw_setscheme(drw, m == selmon ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
733 drw_text(drw, x, 0, w, bh, m->sel->name, 0);
734 drw_rect(drw, x + 1, 1, dx, dx, m->sel->isfixed, m->sel->isfloating, 0);
737 drw_setscheme(drw, &scheme[SchemeNorm]);
738 drw_rect(drw, x, 0, w, bh, 1, 0, 1);
741 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
748 for(m = mons; m; m = m->next)
753 enternotify(XEvent *e) {
756 XCrossingEvent *ev = &e->xcrossing;
758 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
760 c = wintoclient(ev->window);
761 m = c ? c->mon : wintomon(ev->window);
763 unfocus(selmon->sel, 1);
766 else if(!c || c == selmon->sel)
774 XExposeEvent *ev = &e->xexpose;
776 if(ev->count == 0 && (m = wintomon(ev->window)))
782 if(!c || !ISVISIBLE(c))
783 for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
784 /* was if(selmon->sel) */
785 if(selmon->sel && selmon->sel != c)
786 unfocus(selmon->sel, 0);
795 XSetWindowBorder(dpy, c->win, scheme[SchemeSel].border->pix);
799 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
800 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
807 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
808 XFocusChangeEvent *ev = &e->xfocus;
810 if(selmon->sel && ev->window != selmon->sel->win)
811 setfocus(selmon->sel);
815 focusmon(const Arg *arg) {
820 if((m = dirtomon(arg->i)) == selmon)
822 unfocus(selmon->sel, 0); /* s/1/0/ fixes input focus issues
823 in gedit and anjuta */
829 focusstack(const Arg *arg) {
830 Client *c = NULL, *i;
835 for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
837 for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
840 for(i = selmon->clients; i != selmon->sel; i = i->next)
844 for(; i; i = i->next)
855 getatomprop(Client *c, Atom prop) {
858 unsigned char *p = NULL;
859 Atom da, atom = None;
861 if(XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
862 &da, &di, &dl, &dl, &p) == Success && p) {
870 getrootptr(int *x, int *y) {
875 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
882 unsigned char *p = NULL;
883 unsigned long n, extra;
886 if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
887 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
896 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
901 if(!text || size == 0)
904 XGetTextProperty(dpy, w, &name, atom);
907 if(name.encoding == XA_STRING)
908 strncpy(text, (char *)name.value, size - 1);
910 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
911 strncpy(text, *list, size - 1);
912 XFreeStringList(list);
915 text[size - 1] = '\0';
921 grabbuttons(Client *c, int focused) {
925 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
926 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
928 for(i = 0; i < LENGTH(buttons); i++)
929 if(buttons[i].click == ClkClientWin)
930 for(j = 0; j < LENGTH(modifiers); j++)
931 XGrabButton(dpy, buttons[i].button,
932 buttons[i].mask | modifiers[j],
933 c->win, False, BUTTONMASK,
934 GrabModeAsync, GrabModeSync, None, None);
937 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
938 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
947 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
950 XUngrabKey(dpy, AnyKey, AnyModifier, root);
951 for(i = 0; i < LENGTH(keys); i++)
952 if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
953 for(j = 0; j < LENGTH(modifiers); j++)
954 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
955 True, GrabModeAsync, GrabModeAsync);
960 incnmaster(const Arg *arg) {
961 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
967 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
969 if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
970 && unique[n].width == info->width && unique[n].height == info->height)
974 #endif /* XINERAMA */
977 keypress(XEvent *e) {
983 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
984 for(i = 0; i < LENGTH(keys); i++)
985 if(keysym == keys[i].keysym
986 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
988 keys[i].func(&(keys[i].arg));
992 killclient(const Arg *arg) {
995 if(!sendevent(selmon->sel, wmatom[WMDelete])) {
997 XSetErrorHandler(xerrordummy);
998 XSetCloseDownMode(dpy, DestroyAll);
999 XKillClient(dpy, selmon->sel->win);
1001 XSetErrorHandler(xerror);
1007 manage(Window w, XWindowAttributes *wa) {
1008 Client *c, *t = NULL;
1009 Window trans = None;
1012 c = ecalloc(1, sizeof(Client));
1015 if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1024 c->x = c->oldx = wa->x;
1025 c->y = c->oldy = wa->y;
1026 c->w = c->oldw = wa->width;
1027 c->h = c->oldh = wa->height;
1028 c->oldbw = wa->border_width;
1030 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1031 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1032 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1033 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1034 c->x = MAX(c->x, c->mon->mx);
1035 /* only fix client y-offset, if the client center might cover the bar */
1036 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1037 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1040 wc.border_width = c->bw;
1041 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1042 XSetWindowBorder(dpy, w, scheme[SchemeNorm].border->pix);
1043 configure(c); /* propagates border_width, if size doesn't change */
1044 updatewindowtype(c);
1047 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1050 c->isfloating = c->oldstate = trans != None || c->isfixed;
1052 XRaiseWindow(dpy, c->win);
1055 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1056 (unsigned char *) &(c->win), 1);
1057 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1058 setclientstate(c, NormalState);
1059 if (c->mon == selmon)
1060 unfocus(selmon->sel, 0);
1063 XMapWindow(dpy, c->win);
1068 mappingnotify(XEvent *e) {
1069 XMappingEvent *ev = &e->xmapping;
1071 XRefreshKeyboardMapping(ev);
1072 if(ev->request == MappingKeyboard)
1077 maprequest(XEvent *e) {
1078 static XWindowAttributes wa;
1079 XMapRequestEvent *ev = &e->xmaprequest;
1081 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1083 if(wa.override_redirect)
1085 if(!wintoclient(ev->window))
1086 manage(ev->window, &wa);
1090 monocle(Monitor *m) {
1094 for(c = m->clients; c; c = c->next)
1097 if(n > 0) /* override layout symbol */
1098 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1099 for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
1100 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
1104 motionnotify(XEvent *e) {
1105 static Monitor *mon = NULL;
1107 XMotionEvent *ev = &e->xmotion;
1109 if(ev->window != root)
1111 if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1112 unfocus(selmon->sel, 1);
1120 movemouse(const Arg *arg) {
1121 int x, y, ocx, ocy, nx, ny;
1127 if(!(c = selmon->sel))
1129 if(c->isfullscreen) /* no support moving fullscreen windows by mouse */
1134 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1135 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1137 if(!getrootptr(&x, &y))
1140 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1142 case ConfigureRequest:
1145 handler[ev.type](&ev);
1148 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1150 lasttime = ev.xmotion.time;
1152 nx = ocx + (ev.xmotion.x - x);
1153 ny = ocy + (ev.xmotion.y - y);
1154 if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1155 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1156 if(abs(selmon->wx - nx) < snap)
1158 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1159 nx = selmon->wx + selmon->ww - WIDTH(c);
1160 if(abs(selmon->wy - ny) < snap)
1162 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1163 ny = selmon->wy + selmon->wh - HEIGHT(c);
1164 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1165 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1166 togglefloating(NULL);
1168 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1169 resize(c, nx, ny, c->w, c->h, 1);
1172 } while(ev.type != ButtonRelease);
1173 XUngrabPointer(dpy, CurrentTime);
1174 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1182 nexttiled(Client *c) {
1183 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1196 propertynotify(XEvent *e) {
1199 XPropertyEvent *ev = &e->xproperty;
1201 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1203 else if(ev->state == PropertyDelete)
1204 return; /* ignore */
1205 else if((c = wintoclient(ev->window))) {
1208 case XA_WM_TRANSIENT_FOR:
1209 if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1210 (c->isfloating = (wintoclient(trans)) != NULL))
1213 case XA_WM_NORMAL_HINTS:
1221 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1223 if(c == c->mon->sel)
1226 if(ev->atom == netatom[NetWMWindowType])
1227 updatewindowtype(c);
1232 quit(const Arg *arg) {
1237 recttomon(int x, int y, int w, int h) {
1238 Monitor *m, *r = selmon;
1241 for(m = mons; m; m = m->next)
1242 if((a = INTERSECT(x, y, w, h, m)) > area) {
1250 resize(Client *c, int x, int y, int w, int h, int interact) {
1251 if(applysizehints(c, &x, &y, &w, &h, interact))
1252 resizeclient(c, x, y, w, h);
1256 resizeclient(Client *c, int x, int y, int w, int h) {
1259 c->oldx = c->x; c->x = wc.x = x;
1260 c->oldy = c->y; c->y = wc.y = y;
1261 c->oldw = c->w; c->w = wc.width = w;
1262 c->oldh = c->h; c->h = wc.height = h;
1263 wc.border_width = c->bw;
1264 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1270 resizemouse(const Arg *arg) {
1271 int ocx, ocy, nw, nh;
1277 if(!(c = selmon->sel))
1279 if(c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1284 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1285 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1287 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1289 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1291 case ConfigureRequest:
1294 handler[ev.type](&ev);
1297 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1299 lasttime = ev.xmotion.time;
1301 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1302 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1303 if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1304 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1306 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1307 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1308 togglefloating(NULL);
1310 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1311 resize(c, c->x, c->y, nw, nh, 1);
1314 } while(ev.type != ButtonRelease);
1315 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1316 XUngrabPointer(dpy, CurrentTime);
1317 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1318 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1326 restack(Monitor *m) {
1334 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1335 XRaiseWindow(dpy, m->sel->win);
1336 if(m->lt[m->sellt]->arrange) {
1337 wc.stack_mode = Below;
1338 wc.sibling = m->barwin;
1339 for(c = m->stack; c; c = c->snext)
1340 if(!c->isfloating && ISVISIBLE(c)) {
1341 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1342 wc.sibling = c->win;
1346 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1352 /* main event loop */
1354 while(running && !XNextEvent(dpy, &ev))
1355 if(handler[ev.type])
1356 handler[ev.type](&ev); /* call handler */
1361 unsigned int i, num;
1362 Window d1, d2, *wins = NULL;
1363 XWindowAttributes wa;
1365 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1366 for(i = 0; i < num; i++) {
1367 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1368 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1370 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1371 manage(wins[i], &wa);
1373 for(i = 0; i < num; i++) { /* now the transients */
1374 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1376 if(XGetTransientForHint(dpy, wins[i], &d1)
1377 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1378 manage(wins[i], &wa);
1386 sendmon(Client *c, Monitor *m) {
1393 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1401 setclientstate(Client *c, long state) {
1402 long data[] = { state, None };
1404 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1405 PropModeReplace, (unsigned char *)data, 2);
1409 sendevent(Client *c, Atom proto) {
1415 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1416 while(!exists && n--)
1417 exists = protocols[n] == proto;
1421 ev.type = ClientMessage;
1422 ev.xclient.window = c->win;
1423 ev.xclient.message_type = wmatom[WMProtocols];
1424 ev.xclient.format = 32;
1425 ev.xclient.data.l[0] = proto;
1426 ev.xclient.data.l[1] = CurrentTime;
1427 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1433 setfocus(Client *c) {
1434 if(!c->neverfocus) {
1435 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1436 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1437 XA_WINDOW, 32, PropModeReplace,
1438 (unsigned char *) &(c->win), 1);
1440 sendevent(c, wmatom[WMTakeFocus]);
1444 setfullscreen(Client *c, int fullscreen) {
1445 if(fullscreen && !c->isfullscreen) {
1446 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1447 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1448 c->isfullscreen = 1;
1449 c->oldstate = c->isfloating;
1453 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1454 XRaiseWindow(dpy, c->win);
1456 else if(!fullscreen && c->isfullscreen){
1457 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1458 PropModeReplace, (unsigned char*)0, 0);
1459 c->isfullscreen = 0;
1460 c->isfloating = c->oldstate;
1466 resizeclient(c, c->x, c->y, c->w, c->h);
1472 setlayout(const Arg *arg) {
1473 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1476 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1477 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1484 /* arg > 1.0 will set mfact absolutly */
1486 setmfact(const Arg *arg) {
1489 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1491 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1492 if(f < 0.1 || f > 0.9)
1500 XSetWindowAttributes wa;
1502 /* clean up any zombies immediately */
1506 screen = DefaultScreen(dpy);
1507 sw = DisplayWidth(dpy, screen);
1508 sh = DisplayHeight(dpy, screen);
1509 root = RootWindow(dpy, screen);
1510 drw = drw_create(dpy, screen, root, sw, sh);
1511 drw_load_fonts(drw, fonts, LENGTH(fonts));
1512 if (!drw->fontcount)
1513 die("no fonts could be loaded.\n");
1514 bh = drw->fonts[0]->h + 2;
1517 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1518 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1519 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1520 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1521 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1522 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1523 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1524 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1525 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1526 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1527 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1528 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1530 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1531 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1532 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1533 /* init appearance */
1534 scheme[SchemeNorm].border = drw_clr_create(drw, normbordercolor);
1535 scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor);
1536 scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor);
1537 scheme[SchemeSel].border = drw_clr_create(drw, selbordercolor);
1538 scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor);
1539 scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor);
1543 /* EWMH support per view */
1544 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1545 PropModeReplace, (unsigned char *) netatom, NetLast);
1546 XDeleteProperty(dpy, root, netatom[NetClientList]);
1547 /* select for events */
1548 wa.cursor = cursor[CurNormal]->cursor;
1549 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
1550 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1551 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1552 XSelectInput(dpy, root, wa.event_mask);
1558 showhide(Client *c) {
1561 if(ISVISIBLE(c)) { /* show clients top down */
1562 XMoveWindow(dpy, c->win, c->x, c->y);
1563 if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1564 resize(c, c->x, c->y, c->w, c->h, 0);
1567 else { /* hide clients bottom up */
1569 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1574 sigchld(int unused) {
1575 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1576 die("can't install SIGCHLD handler:");
1577 while(0 < waitpid(-1, NULL, WNOHANG));
1581 spawn(const Arg *arg) {
1582 if(arg->v == dmenucmd)
1583 dmenumon[0] = '0' + selmon->num;
1586 close(ConnectionNumber(dpy));
1588 execvp(((char **)arg->v)[0], (char **)arg->v);
1589 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1596 tag(const Arg *arg) {
1597 if(selmon->sel && arg->ui & TAGMASK) {
1598 selmon->sel->tags = arg->ui & TAGMASK;
1605 tagmon(const Arg *arg) {
1606 if(!selmon->sel || !mons->next)
1608 sendmon(selmon->sel, dirtomon(arg->i));
1613 unsigned int i, n, h, mw, my, ty;
1616 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1621 mw = m->nmaster ? m->ww * m->mfact : 0;
1624 for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1625 if(i < m->nmaster) {
1626 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1627 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), 0);
1631 h = (m->wh - ty) / (n - i);
1632 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), 0);
1638 togglebar(const Arg *arg) {
1639 selmon->showbar = !selmon->showbar;
1640 updatebarpos(selmon);
1641 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1646 togglefloating(const Arg *arg) {
1649 if(selmon->sel->isfullscreen) /* no support for fullscreen windows */
1651 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1652 if(selmon->sel->isfloating)
1653 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1654 selmon->sel->w, selmon->sel->h, 0);
1659 toggletag(const Arg *arg) {
1660 unsigned int newtags;
1664 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1666 selmon->sel->tags = newtags;
1673 toggleview(const Arg *arg) {
1674 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1677 selmon->tagset[selmon->seltags] = newtagset;
1684 unfocus(Client *c, int setfocus) {
1688 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->pix);
1690 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1691 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1696 unmanage(Client *c, int destroyed) {
1697 Monitor *m = c->mon;
1700 /* The server grab construct avoids race conditions. */
1704 wc.border_width = c->oldbw;
1706 XSetErrorHandler(xerrordummy);
1707 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1708 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1709 setclientstate(c, WithdrawnState);
1711 XSetErrorHandler(xerror);
1721 unmapnotify(XEvent *e) {
1723 XUnmapEvent *ev = &e->xunmap;
1725 if((c = wintoclient(ev->window))) {
1727 setclientstate(c, WithdrawnState);
1736 XSetWindowAttributes wa = {
1737 .override_redirect = True,
1738 .background_pixmap = ParentRelative,
1739 .event_mask = ButtonPressMask|ExposureMask
1741 for(m = mons; m; m = m->next) {
1744 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1745 CopyFromParent, DefaultVisual(dpy, screen),
1746 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1747 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
1748 XMapRaised(dpy, m->barwin);
1753 updatebarpos(Monitor *m) {
1758 m->by = m->topbar ? m->wy : m->wy + m->wh;
1759 m->wy = m->topbar ? m->wy + bh : m->wy;
1766 updateclientlist() {
1770 XDeleteProperty(dpy, root, netatom[NetClientList]);
1771 for(m = mons; m; m = m->next)
1772 for(c = m->clients; c; c = c->next)
1773 XChangeProperty(dpy, root, netatom[NetClientList],
1774 XA_WINDOW, 32, PropModeAppend,
1775 (unsigned char *) &(c->win), 1);
1783 if(XineramaIsActive(dpy)) {
1787 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1788 XineramaScreenInfo *unique = NULL;
1790 for(n = 0, m = mons; m; m = m->next, n++);
1791 /* only consider unique geometries as separate screens */
1792 unique = ecalloc(nn, sizeof(XineramaScreenInfo));
1793 for(i = 0, j = 0; i < nn; i++)
1794 if(isuniquegeom(unique, j, &info[i]))
1795 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1799 for(i = 0; i < (nn - n); i++) { /* new monitors available */
1800 for(m = mons; m && m->next; m = m->next);
1802 m->next = createmon();
1806 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
1808 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1809 || unique[i].width != m->mw || unique[i].height != m->mh))
1813 m->mx = m->wx = unique[i].x_org;
1814 m->my = m->wy = unique[i].y_org;
1815 m->mw = m->ww = unique[i].width;
1816 m->mh = m->wh = unique[i].height;
1820 else { /* less monitors available nn < n */
1821 for(i = nn; i < n; i++) {
1822 for(m = mons; m && m->next; m = m->next);
1826 m->clients = c->next;
1840 #endif /* XINERAMA */
1841 /* default monitor setup */
1845 if(mons->mw != sw || mons->mh != sh) {
1847 mons->mw = mons->ww = sw;
1848 mons->mh = mons->wh = sh;
1854 selmon = wintomon(root);
1860 updatenumlockmask(void) {
1862 XModifierKeymap *modmap;
1865 modmap = XGetModifierMapping(dpy);
1866 for(i = 0; i < 8; i++)
1867 for(j = 0; j < modmap->max_keypermod; j++)
1868 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1869 == XKeysymToKeycode(dpy, XK_Num_Lock))
1870 numlockmask = (1 << i);
1871 XFreeModifiermap(modmap);
1875 updatesizehints(Client *c) {
1879 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
1880 /* size is uninitialized, ensure that size.flags aren't used */
1882 if(size.flags & PBaseSize) {
1883 c->basew = size.base_width;
1884 c->baseh = size.base_height;
1886 else if(size.flags & PMinSize) {
1887 c->basew = size.min_width;
1888 c->baseh = size.min_height;
1891 c->basew = c->baseh = 0;
1892 if(size.flags & PResizeInc) {
1893 c->incw = size.width_inc;
1894 c->inch = size.height_inc;
1897 c->incw = c->inch = 0;
1898 if(size.flags & PMaxSize) {
1899 c->maxw = size.max_width;
1900 c->maxh = size.max_height;
1903 c->maxw = c->maxh = 0;
1904 if(size.flags & PMinSize) {
1905 c->minw = size.min_width;
1906 c->minh = size.min_height;
1908 else if(size.flags & PBaseSize) {
1909 c->minw = size.base_width;
1910 c->minh = size.base_height;
1913 c->minw = c->minh = 0;
1914 if(size.flags & PAspect) {
1915 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
1916 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
1919 c->maxa = c->mina = 0.0;
1920 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
1921 && c->maxw == c->minw && c->maxh == c->minh);
1925 updatetitle(Client *c) {
1926 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
1927 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
1928 if(c->name[0] == '\0') /* hack to mark broken clients */
1929 strcpy(c->name, broken);
1933 updatestatus(void) {
1934 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
1935 strcpy(stext, "dwm-"VERSION);
1940 updatewindowtype(Client *c) {
1941 Atom state = getatomprop(c, netatom[NetWMState]);
1942 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
1944 if(state == netatom[NetWMFullscreen])
1945 setfullscreen(c, 1);
1946 if(wtype == netatom[NetWMWindowTypeDialog])
1951 updatewmhints(Client *c) {
1954 if((wmh = XGetWMHints(dpy, c->win))) {
1955 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
1956 wmh->flags &= ~XUrgencyHint;
1957 XSetWMHints(dpy, c->win, wmh);
1960 c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
1961 if(wmh->flags & InputHint)
1962 c->neverfocus = !wmh->input;
1970 view(const Arg *arg) {
1971 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
1973 selmon->seltags ^= 1; /* toggle sel tagset */
1974 if(arg->ui & TAGMASK)
1975 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
1981 wintoclient(Window w) {
1985 for(m = mons; m; m = m->next)
1986 for(c = m->clients; c; c = c->next)
1993 wintomon(Window w) {
1998 if(w == root && getrootptr(&x, &y))
1999 return recttomon(x, y, 1, 1);
2000 for(m = mons; m; m = m->next)
2003 if((c = wintoclient(w)))
2008 /* There's no way to check accesses to destroyed windows, thus those cases are
2009 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2010 * default error handler, which may call exit. */
2012 xerror(Display *dpy, XErrorEvent *ee) {
2013 if(ee->error_code == BadWindow
2014 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2015 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2016 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2017 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2018 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2019 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2020 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2021 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2023 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2024 ee->request_code, ee->error_code);
2025 return xerrorxlib(dpy, ee); /* may call exit */
2029 xerrordummy(Display *dpy, XErrorEvent *ee) {
2033 /* Startup Error handler to check if another window manager
2034 * is already running. */
2036 xerrorstart(Display *dpy, XErrorEvent *ee) {
2037 die("dwm: another window manager is already running\n");
2042 zoom(const Arg *arg) {
2043 Client *c = selmon->sel;
2045 if(!selmon->lt[selmon->sellt]->arrange
2046 || (selmon->sel && selmon->sel->isfloating))
2048 if(c == nexttiled(selmon->clients))
2049 if(!c || !(c = nexttiled(c->next)))
2055 main(int argc, char *argv[]) {
2056 if(argc == 2 && !strcmp("-v", argv[1]))
2057 die("dwm-"VERSION", © 2006-2015 dwm engineers, see LICENSE for details\n");
2059 die("usage: dwm [-v]\n");
2060 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2061 fputs("warning: no locale support\n", stderr);
2062 if(!(dpy = XOpenDisplay(NULL)))
2063 die("dwm: cannot open display\n");
2070 return EXIT_SUCCESS;