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>
47 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
48 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
49 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
50 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
51 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
52 #define LENGTH(X) (sizeof X / sizeof X[0])
53 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
54 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
55 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
56 #define TAGMASK ((1 << LENGTH(tags)) - 1)
57 #define TEXTW(X) (drw_font_getexts_width(drw->font, X, strlen(X)) + drw->font->h)
60 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
61 enum { SchemeNorm, SchemeSel, SchemeLast }; /* color schemes */
62 enum { NetSupported, NetWMName, NetWMState,
63 NetWMFullscreen, NetWMWindowOpacity, NetActiveWindow, NetWMWindowType,
64 NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */
65 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
66 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
67 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
80 void (*func)(const Arg *arg);
84 typedef struct Monitor Monitor;
85 typedef struct Client Client;
90 int oldx, oldy, oldw, oldh;
91 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
94 Bool isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
104 void (*func)(const Arg *);
110 void (*arrange)(Monitor *);
118 int by; /* bar geometry */
119 int mx, my, mw, mh; /* screen size */
120 int wx, wy, ww, wh; /* window area */
121 unsigned int seltags;
123 unsigned int tagset[2];
136 const char *instance;
143 /* function declarations */
144 static void applyrules(Client *c);
145 static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
146 static void arrange(Monitor *m);
147 static void arrangemon(Monitor *m);
148 static void attach(Client *c);
149 static void attachstack(Client *c);
150 static void buttonpress(XEvent *e);
151 static void checkotherwm(void);
152 static void cleanup(void);
153 static void cleanupmon(Monitor *mon);
154 static void clearurgent(Client *c);
155 static void clientmessage(XEvent *e);
156 static void configure(Client *c);
157 static void configurenotify(XEvent *e);
158 static void configurerequest(XEvent *e);
159 static Monitor *createmon(void);
160 static void destroynotify(XEvent *e);
161 static void detach(Client *c);
162 static void detachstack(Client *c);
163 static Monitor *dirtomon(int dir);
164 static void drawbar(Monitor *m);
165 static void drawbars(void);
166 static void enternotify(XEvent *e);
167 static void expose(XEvent *e);
168 static void focus(Client *c);
169 static void focusin(XEvent *e);
170 static void focusmon(const Arg *arg);
171 static void focusstack(const Arg *arg);
172 static Bool getrootptr(int *x, int *y);
173 static long getstate(Window w);
174 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
175 static void grabbuttons(Client *c, Bool focused);
176 static void grabkeys(void);
177 static void incnmaster(const Arg *arg);
178 static void keypress(XEvent *e);
179 static void killclient(const Arg *arg);
180 static void manage(Window w, XWindowAttributes *wa);
181 static void mappingnotify(XEvent *e);
182 static void maprequest(XEvent *e);
183 static void monocle(Monitor *m);
184 static void motionnotify(XEvent *e);
185 static void movemouse(const Arg *arg);
186 static Client *nexttiled(Client *c);
187 static void pop(Client *);
188 static void propertynotify(XEvent *e);
189 static void quit(const Arg *arg);
190 static Monitor *recttomon(int x, int y, int w, int h);
191 static void resize(Client *c, int x, int y, int w, int h, Bool interact);
192 static void resizeclient(Client *c, int x, int y, int w, int h);
193 static void resizemouse(const Arg *arg);
194 static void restack(Monitor *m);
195 static void run(void);
196 static void scan(void);
197 static Bool sendevent(Client *c, Atom proto);
198 static void sendmon(Client *c, Monitor *m);
199 static void setclientstate(Client *c, long state);
200 static void setfocus(Client *c);
201 static void setfullscreen(Client *c, Bool fullscreen);
202 static void setlayout(const Arg *arg);
203 static void setmfact(const Arg *arg);
204 static void setup(void);
205 static void showhide(Client *c);
206 static void sigchld(int unused);
207 static void spawn(const Arg *arg);
208 static void tag(const Arg *arg);
209 static void tagmon(const Arg *arg);
210 static void tile(Monitor *);
211 static void togglebar(const Arg *arg);
212 static void togglefloating(const Arg *arg);
213 static void toggletag(const Arg *arg);
214 static void toggleview(const Arg *arg);
215 static void unfocus(Client *c, Bool setfocus);
216 static void unmanage(Client *c, Bool destroyed);
217 static void unmapnotify(XEvent *e);
218 static Bool updategeom(void);
219 static void updatebarpos(Monitor *m);
220 static void updatebars(void);
221 static void updateclientlist(void);
222 static void updatenumlockmask(void);
223 static void updatesizehints(Client *c);
224 static void updatestatus(void);
225 static void updatewindowtype(Client *c);
226 static void updatetitle(Client *c);
227 static void updatewmhints(Client *c);
228 static void view(const Arg *arg);
229 static Client *wintoclient(Window w);
230 static Monitor *wintomon(Window w);
231 static int xerror(Display *dpy, XErrorEvent *ee);
232 static int xerrordummy(Display *dpy, XErrorEvent *ee);
233 static int xerrorstart(Display *dpy, XErrorEvent *ee);
234 static void zoom(const Arg *arg);
237 static const char broken[] = "broken";
238 static char stext[256];
240 static int sw, sh; /* X display screen geometry width, height */
241 static int bh, blw = 0; /* bar geometry */
242 static int (*xerrorxlib)(Display *, XErrorEvent *);
243 static unsigned int numlockmask = 0;
244 static void (*handler[LASTEvent]) (XEvent *) = {
245 [ButtonPress] = buttonpress,
246 [ClientMessage] = clientmessage,
247 [ConfigureRequest] = configurerequest,
248 [ConfigureNotify] = configurenotify,
249 [DestroyNotify] = destroynotify,
250 [EnterNotify] = enternotify,
253 [KeyPress] = keypress,
254 [MappingNotify] = mappingnotify,
255 [MapRequest] = maprequest,
256 [MotionNotify] = motionnotify,
257 [PropertyNotify] = propertynotify,
258 [UnmapNotify] = unmapnotify
260 static Atom wmatom[WMLast], netatom[NetLast];
261 static Bool running = True;
262 static Cur *cursor[CurLast];
263 static ClrScheme scheme[SchemeLast];
267 static Monitor *mons, *selmon;
270 // unfocused windows get transparent (feature)
271 static const unsigned long unfocused_opacity[] = { 0xbfffffff };
272 static void window_set_opaque(Client *c);
273 static void window_set_translucent(Client *c);
275 window_set_opaque(Client *c) {
276 XChangeProperty(dpy, c->win, netatom[NetWMWindowOpacity], XA_CARDINAL, 32, PropModeReplace, (unsigned char *)unfocused_opacity, 1);
279 window_set_translucent(Client *c) {
280 XDeleteProperty(dpy, c->win, netatom[NetWMWindowOpacity]);
284 /* configuration, allows nested code to access above variables */
287 /* compile-time check if all tags fit into an unsigned int bit array. */
288 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
290 /* function implementations */
292 applyrules(Client *c) {
293 const char *class, *instance;
297 XClassHint ch = { NULL, NULL };
300 c->isfloating = c->tags = 0;
301 XGetClassHint(dpy, c->win, &ch);
302 class = ch.res_class ? ch.res_class : broken;
303 instance = ch.res_name ? ch.res_name : broken;
305 for(i = 0; i < LENGTH(rules); i++) {
307 if((!r->title || strstr(c->name, r->title))
308 && (!r->class || strstr(class, r->class))
309 && (!r->instance || strstr(instance, r->instance)))
311 c->isfloating = r->isfloating;
313 for(m = mons; m && m->num != r->monitor; m = m->next);
322 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
326 applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
330 /* set minimum possible */
338 if(*x + *w + 2 * c->bw < 0)
340 if(*y + *h + 2 * c->bw < 0)
344 if(*x >= m->wx + m->ww)
345 *x = m->wx + m->ww - WIDTH(c);
346 if(*y >= m->wy + m->wh)
347 *y = m->wy + m->wh - HEIGHT(c);
348 if(*x + *w + 2 * c->bw <= m->wx)
350 if(*y + *h + 2 * c->bw <= m->wy)
357 if(resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
358 /* see last two sentences in ICCCM 4.1.2.3 */
359 baseismin = c->basew == c->minw && c->baseh == c->minh;
360 if(!baseismin) { /* temporarily remove base dimensions */
364 /* adjust for aspect limits */
365 if(c->mina > 0 && c->maxa > 0) {
366 if(c->maxa < (float)*w / *h)
367 *w = *h * c->maxa + 0.5;
368 else if(c->mina < (float)*h / *w)
369 *h = *w * c->mina + 0.5;
371 if(baseismin) { /* increment calculation requires this */
375 /* adjust for increment value */
380 /* restore base dimensions */
381 *w = MAX(*w + c->basew, c->minw);
382 *h = MAX(*h + c->baseh, c->minh);
384 *w = MIN(*w, c->maxw);
386 *h = MIN(*h, c->maxh);
388 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
392 arrange(Monitor *m) {
395 else for(m = mons; m; m = m->next)
400 } else for(m = mons; m; m = m->next)
405 arrangemon(Monitor *m) {
406 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
407 if(m->lt[m->sellt]->arrange)
408 m->lt[m->sellt]->arrange(m);
413 c->next = c->mon->clients;
418 attachstack(Client *c) {
419 c->snext = c->mon->stack;
424 buttonpress(XEvent *e) {
425 unsigned int i, x, click;
429 XButtonPressedEvent *ev = &e->xbutton;
432 /* focus monitor if necessary */
433 if((m = wintomon(ev->window)) && m != selmon) {
434 unfocus(selmon->sel, True);
438 if(ev->window == selmon->barwin) {
442 while(ev->x >= x && ++i < LENGTH(tags));
443 if(i < LENGTH(tags)) {
447 else if(ev->x < x + blw)
449 else if(ev->x > selmon->ww - TEXTW(stext))
450 click = ClkStatusText;
454 else if((c = wintoclient(ev->window))) {
456 click = ClkClientWin;
458 for(i = 0; i < LENGTH(buttons); i++)
459 if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
460 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
461 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
466 xerrorxlib = XSetErrorHandler(xerrorstart);
467 /* this causes an error if some other window manager is running */
468 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
470 XSetErrorHandler(xerror);
477 Layout foo = { "", NULL };
481 selmon->lt[selmon->sellt] = &foo;
482 for(m = mons; m; m = m->next)
484 unmanage(m->stack, False);
485 XUngrabKey(dpy, AnyKey, AnyModifier, root);
488 drw_cur_free(drw, cursor[CurNormal]);
489 drw_cur_free(drw, cursor[CurResize]);
490 drw_cur_free(drw, cursor[CurMove]);
491 drw_font_free(dpy, fnt);
492 drw_clr_free(scheme[SchemeNorm].border);
493 drw_clr_free(scheme[SchemeNorm].bg);
494 drw_clr_free(scheme[SchemeNorm].fg);
495 drw_clr_free(scheme[SchemeSel].border);
496 drw_clr_free(scheme[SchemeSel].bg);
497 drw_clr_free(scheme[SchemeSel].fg);
500 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
501 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
505 cleanupmon(Monitor *mon) {
511 for(m = mons; m && m->next != mon; m = m->next);
514 XUnmapWindow(dpy, mon->barwin);
515 XDestroyWindow(dpy, mon->barwin);
520 clearurgent(Client *c) {
524 if(!(wmh = XGetWMHints(dpy, c->win)))
526 wmh->flags &= ~XUrgencyHint;
527 XSetWMHints(dpy, c->win, wmh);
532 clientmessage(XEvent *e) {
533 XClientMessageEvent *cme = &e->xclient;
534 Client *c = wintoclient(cme->window);
538 if(cme->message_type == netatom[NetWMState]) {
539 if(cme->data.l[1] == netatom[NetWMFullscreen] || cme->data.l[2] == netatom[NetWMFullscreen])
540 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
541 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
543 else if(cme->message_type == netatom[NetActiveWindow]) {
545 c->mon->seltags ^= 1;
546 c->mon->tagset[c->mon->seltags] = c->tags;
553 configure(Client *c) {
556 ce.type = ConfigureNotify;
564 ce.border_width = c->bw;
566 ce.override_redirect = False;
567 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
571 configurenotify(XEvent *e) {
573 XConfigureEvent *ev = &e->xconfigure;
576 // TODO: updategeom handling sucks, needs to be simplified
577 if(ev->window == root) {
578 dirty = (sw != ev->width || sh != ev->height);
581 if(updategeom() || dirty) {
582 drw_resize(drw, sw, bh);
584 for(m = mons; m; m = m->next)
585 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
593 configurerequest(XEvent *e) {
596 XConfigureRequestEvent *ev = &e->xconfigurerequest;
599 if((c = wintoclient(ev->window))) {
600 if(ev->value_mask & CWBorderWidth)
601 c->bw = ev->border_width;
602 else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
604 if(ev->value_mask & CWX) {
606 c->x = m->mx + ev->x;
608 if(ev->value_mask & CWY) {
610 c->y = m->my + ev->y;
612 if(ev->value_mask & CWWidth) {
616 if(ev->value_mask & CWHeight) {
620 if((c->x + c->w) > m->mx + m->mw && c->isfloating)
621 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
622 if((c->y + c->h) > m->my + m->mh && c->isfloating)
623 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
624 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
627 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
635 wc.width = ev->width;
636 wc.height = ev->height;
637 wc.border_width = ev->border_width;
638 wc.sibling = ev->above;
639 wc.stack_mode = ev->detail;
640 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
649 if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
650 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
651 m->tagset[0] = m->tagset[1] = 1;
653 m->nmaster = nmaster;
654 m->showbar = showbar;
656 m->lt[0] = &layouts[0];
657 m->lt[1] = &layouts[1 % LENGTH(layouts)];
658 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
663 destroynotify(XEvent *e) {
665 XDestroyWindowEvent *ev = &e->xdestroywindow;
667 if((c = wintoclient(ev->window)))
675 for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
680 detachstack(Client *c) {
683 for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
686 if(c == c->mon->sel) {
687 for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
697 if(!(m = selmon->next))
700 else if(selmon == mons)
701 for(m = mons; m->next; m = m->next);
703 for(m = mons; m->next != selmon; m = m->next);
708 drawbar(Monitor *m) {
710 unsigned int i, occ = 0, urg = 0;
713 for(c = m->clients; c; c = c->next) {
719 for(i = 0; i < LENGTH(tags); i++) {
721 drw_setscheme(drw, m->tagset[m->seltags] & 1 << i ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
722 drw_text(drw, x, 0, w, bh, tags[i], urg & 1 << i);
723 drw_rect(drw, x, 0, w, bh, m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
724 occ & 1 << i, urg & 1 << i);
727 w = blw = TEXTW(m->ltsymbol);
728 drw_setscheme(drw, &scheme[SchemeNorm]);
729 drw_text(drw, x, 0, w, bh, m->ltsymbol, 0);
732 if(m == selmon) { /* status is only drawn on selected monitor */
739 drw_text(drw, x, 0, w, bh, stext, 0);
743 if((w = x - xx) > bh) {
746 drw_setscheme(drw, m == selmon ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
747 drw_text(drw, x, 0, w, bh, m->sel->name, 0);
748 drw_rect(drw, x, 0, w, bh, m->sel->isfixed, m->sel->isfloating, 0);
751 drw_setscheme(drw, &scheme[SchemeNorm]);
752 drw_text(drw, x, 0, w, bh, NULL, 0);
755 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
762 for(m = mons; m; m = m->next)
767 enternotify(XEvent *e) {
770 XCrossingEvent *ev = &e->xcrossing;
772 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
774 c = wintoclient(ev->window);
775 m = c ? c->mon : wintomon(ev->window);
777 unfocus(selmon->sel, True);
780 else if(!c || c == selmon->sel)
788 XExposeEvent *ev = &e->xexpose;
790 if(ev->count == 0 && (m = wintomon(ev->window)))
796 if(!c || !ISVISIBLE(c))
797 for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
798 /* was if(selmon->sel) */
799 if(selmon->sel && selmon->sel != c)
800 unfocus(selmon->sel, False);
801 if(selmon->sel && c!=selmon->sel && c && (!root || (selmon->sel->win!=root && c->win!=root)) )
802 window_set_opaque(selmon->sel);
803 if(c && c!=selmon->sel && (!root || (c->win!=root)) )
804 window_set_translucent(c);
812 grabbuttons(c, True);
813 XSetWindowBorder(dpy, c->win, scheme[SchemeSel].border->rgb);
817 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
818 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
823 window_set_translucent(c);
827 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
828 XFocusChangeEvent *ev = &e->xfocus;
830 if(selmon->sel && ev->window != selmon->sel->win)
831 setfocus(selmon->sel);
835 focusmon(const Arg *arg) {
840 if((m = dirtomon(arg->i)) == selmon)
842 unfocus(selmon->sel, False); /* s/True/False/ fixes input focus issues
843 in gedit and anjuta */
849 focusstack(const Arg *arg) {
850 Client *c = NULL, *i;
855 for(i = selmon->clients; i != selmon->sel; i = i->next) {
861 } else if(arg->i > 0) {
862 for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
864 for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
867 for(i = selmon->clients; i != selmon->sel; i = i->next)
871 for(; i; i = i->next)
882 getatomprop(Client *c, Atom prop) {
885 unsigned char *p = NULL;
886 Atom da, atom = None;
888 if(XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
889 &da, &di, &dl, &dl, &p) == Success && p) {
897 getrootptr(int *x, int *y) {
902 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
909 unsigned char *p = NULL;
910 unsigned long n, extra;
913 if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
914 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
923 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
928 if(!text || size == 0)
931 XGetTextProperty(dpy, w, &name, atom);
934 if(name.encoding == XA_STRING)
935 strncpy(text, (char *)name.value, size - 1);
937 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
938 strncpy(text, *list, size - 1);
939 XFreeStringList(list);
942 text[size - 1] = '\0';
948 grabbuttons(Client *c, Bool focused) {
952 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
953 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
955 for(i = 0; i < LENGTH(buttons); i++)
956 if(buttons[i].click == ClkClientWin)
957 for(j = 0; j < LENGTH(modifiers); j++)
958 XGrabButton(dpy, buttons[i].button,
959 buttons[i].mask | modifiers[j],
960 c->win, False, BUTTONMASK,
961 GrabModeAsync, GrabModeSync, None, None);
964 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
965 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
974 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
977 XUngrabKey(dpy, AnyKey, AnyModifier, root);
978 for(i = 0; i < LENGTH(keys); i++)
979 if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
980 for(j = 0; j < LENGTH(modifiers); j++)
981 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
982 True, GrabModeAsync, GrabModeAsync);
987 incnmaster(const Arg *arg) {
988 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
994 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
996 if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
997 && unique[n].width == info->width && unique[n].height == info->height)
1001 #endif /* XINERAMA */
1004 keypress(XEvent *e) {
1010 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1011 for(i = 0; i < LENGTH(keys); i++)
1012 if(keysym == keys[i].keysym
1013 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1015 keys[i].func(&(keys[i].arg));
1019 killclient(const Arg *arg) {
1022 if(!sendevent(selmon->sel, wmatom[WMDelete])) {
1024 XSetErrorHandler(xerrordummy);
1025 XSetCloseDownMode(dpy, DestroyAll);
1026 XKillClient(dpy, selmon->sel->win);
1028 XSetErrorHandler(xerror);
1034 manage(Window w, XWindowAttributes *wa) {
1035 Client *c, *t = NULL;
1036 Window trans = None;
1039 if(!(c = calloc(1, sizeof(Client))))
1040 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1043 if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1052 c->x = c->oldx = wa->x;
1053 c->y = c->oldy = wa->y;
1054 c->w = c->oldw = wa->width;
1055 c->h = c->oldh = wa->height;
1056 c->oldbw = wa->border_width;
1058 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1059 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1060 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1061 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1062 c->x = MAX(c->x, c->mon->mx);
1063 /* only fix client y-offset, if the client center might cover the bar */
1064 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1065 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1068 wc.border_width = c->bw;
1069 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1070 XSetWindowBorder(dpy, w, scheme[SchemeNorm].border->rgb);
1071 configure(c); /* propagates border_width, if size doesn't change */
1072 updatewindowtype(c);
1075 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1076 grabbuttons(c, False);
1078 c->isfloating = c->oldstate = trans != None || c->isfixed;
1080 XRaiseWindow(dpy, c->win);
1083 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1084 (unsigned char *) &(c->win), 1);
1085 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1086 setclientstate(c, NormalState);
1087 if (c->mon == selmon)
1088 unfocus(selmon->sel, False);
1091 XMapWindow(dpy, c->win);
1096 mappingnotify(XEvent *e) {
1097 XMappingEvent *ev = &e->xmapping;
1099 XRefreshKeyboardMapping(ev);
1100 if(ev->request == MappingKeyboard)
1105 maprequest(XEvent *e) {
1106 static XWindowAttributes wa;
1107 XMapRequestEvent *ev = &e->xmaprequest;
1109 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1111 if(wa.override_redirect)
1113 if(!wintoclient(ev->window))
1114 manage(ev->window, &wa);
1118 monocle(Monitor *m) {
1122 for(c = m->clients; c; c = c->next)
1125 if(n > 0) /* override layout symbol */
1126 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1127 for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
1128 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
1132 motionnotify(XEvent *e) {
1133 static Monitor *mon = NULL;
1135 XMotionEvent *ev = &e->xmotion;
1137 if(ev->window != root)
1139 if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1140 unfocus(selmon->sel, True);
1148 movemouse(const Arg *arg) {
1149 int x, y, ocx, ocy, nx, ny;
1155 if(!(c = selmon->sel))
1157 if(c->isfullscreen) /* no support moving fullscreen windows by mouse */
1162 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1163 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1165 if(!getrootptr(&x, &y))
1168 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1170 case ConfigureRequest:
1173 handler[ev.type](&ev);
1176 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1178 lasttime = ev.xmotion.time;
1180 nx = ocx + (ev.xmotion.x - x);
1181 ny = ocy + (ev.xmotion.y - y);
1182 if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1183 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1184 if(abs(selmon->wx - nx) < snap)
1186 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1187 nx = selmon->wx + selmon->ww - WIDTH(c);
1188 if(abs(selmon->wy - ny) < snap)
1190 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1191 ny = selmon->wy + selmon->wh - HEIGHT(c);
1192 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1193 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1194 togglefloating(NULL);
1196 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1197 resize(c, nx, ny, c->w, c->h, True);
1200 } while(ev.type != ButtonRelease);
1201 XUngrabPointer(dpy, CurrentTime);
1202 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1210 nexttiled(Client *c) {
1211 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1224 propertynotify(XEvent *e) {
1227 XPropertyEvent *ev = &e->xproperty;
1229 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1231 else if(ev->state == PropertyDelete)
1232 return; /* ignore */
1233 else if((c = wintoclient(ev->window))) {
1236 case XA_WM_TRANSIENT_FOR:
1237 if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1238 (c->isfloating = (wintoclient(trans)) != NULL))
1241 case XA_WM_NORMAL_HINTS:
1249 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1251 if(c == c->mon->sel)
1254 if(ev->atom == netatom[NetWMWindowType])
1255 updatewindowtype(c);
1260 quit(const Arg *arg) {
1265 recttomon(int x, int y, int w, int h) {
1266 Monitor *m, *r = selmon;
1269 for(m = mons; m; m = m->next)
1270 if((a = INTERSECT(x, y, w, h, m)) > area) {
1278 resize(Client *c, int x, int y, int w, int h, Bool interact) {
1279 if(applysizehints(c, &x, &y, &w, &h, interact))
1280 resizeclient(c, x, y, w, h);
1284 resizeclient(Client *c, int x, int y, int w, int h) {
1287 c->oldx = c->x; c->x = wc.x = x;
1288 c->oldy = c->y; c->y = wc.y = y;
1289 c->oldw = c->w; c->w = wc.width = w;
1290 c->oldh = c->h; c->h = wc.height = h;
1291 wc.border_width = c->bw;
1292 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1298 resizemouse(const Arg *arg) {
1299 int ocx, ocy, nw, nh;
1305 if(!(c = selmon->sel))
1307 if(c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1312 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1313 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1315 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1317 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1319 case ConfigureRequest:
1322 handler[ev.type](&ev);
1325 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1327 lasttime = ev.xmotion.time;
1329 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1330 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1331 if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1332 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1334 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1335 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1336 togglefloating(NULL);
1338 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1339 resize(c, c->x, c->y, nw, nh, True);
1342 } while(ev.type != ButtonRelease);
1343 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1344 XUngrabPointer(dpy, CurrentTime);
1345 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1346 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1354 restack(Monitor *m) {
1362 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1363 XRaiseWindow(dpy, m->sel->win);
1364 if(m->lt[m->sellt]->arrange) {
1365 wc.stack_mode = Below;
1366 wc.sibling = m->barwin;
1367 for(c = m->stack; c; c = c->snext)
1368 if(!c->isfloating && ISVISIBLE(c)) {
1369 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1370 wc.sibling = c->win;
1374 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 */
1389 unsigned int i, num;
1390 Window d1, d2, *wins = NULL;
1391 XWindowAttributes wa;
1393 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1394 for(i = 0; i < num; i++) {
1395 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1396 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1398 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1399 manage(wins[i], &wa);
1401 for(i = 0; i < num; i++) { /* now the transients */
1402 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1404 if(XGetTransientForHint(dpy, wins[i], &d1)
1405 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1406 manage(wins[i], &wa);
1414 sendmon(Client *c, Monitor *m) {
1421 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1429 setclientstate(Client *c, long state) {
1430 long data[] = { state, None };
1432 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1433 PropModeReplace, (unsigned char *)data, 2);
1437 sendevent(Client *c, Atom proto) {
1440 Bool exists = False;
1443 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1444 while(!exists && n--)
1445 exists = protocols[n] == proto;
1449 ev.type = ClientMessage;
1450 ev.xclient.window = c->win;
1451 ev.xclient.message_type = wmatom[WMProtocols];
1452 ev.xclient.format = 32;
1453 ev.xclient.data.l[0] = proto;
1454 ev.xclient.data.l[1] = CurrentTime;
1455 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1461 setfocus(Client *c) {
1462 if(!c->neverfocus) {
1463 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1464 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1465 XA_WINDOW, 32, PropModeReplace,
1466 (unsigned char *) &(c->win), 1);
1468 sendevent(c, wmatom[WMTakeFocus]);
1472 setfullscreen(Client *c, Bool fullscreen) {
1474 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1475 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1476 c->isfullscreen = True;
1477 c->oldstate = c->isfloating;
1480 c->isfloating = True;
1481 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1482 XRaiseWindow(dpy, c->win);
1485 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1486 PropModeReplace, (unsigned char*)0, 0);
1487 c->isfullscreen = False;
1488 c->isfloating = c->oldstate;
1494 resizeclient(c, c->x, c->y, c->w, c->h);
1500 setlayout(const Arg *arg) {
1501 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1504 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1505 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1512 /* arg > 1.0 will set mfact absolutly */
1514 setmfact(const Arg *arg) {
1517 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1519 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1520 if(f < 0.1 || f > 0.9)
1528 XSetWindowAttributes wa;
1530 /* clean up any zombies immediately */
1534 screen = DefaultScreen(dpy);
1535 root = RootWindow(dpy, screen);
1536 fnt = drw_font_create(dpy, font);
1537 sw = DisplayWidth(dpy, screen);
1538 sh = DisplayHeight(dpy, screen);
1540 drw = drw_create(dpy, screen, root, sw, sh);
1541 drw_setfont(drw, fnt);
1544 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1545 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1546 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1547 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1548 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1549 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1550 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1551 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1552 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1553 netatom[NetWMWindowOpacity] = XInternAtom(dpy, "_NET_WM_WINDOW_OPACITY", False);
1554 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1555 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1556 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1558 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1559 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1560 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1561 /* init appearance */
1562 scheme[SchemeNorm].border = drw_clr_create(drw, normbordercolor);
1563 scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor);
1564 scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor);
1565 scheme[SchemeSel].border = drw_clr_create(drw, selbordercolor);
1566 scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor);
1567 scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor);
1571 /* EWMH support per view */
1572 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1573 PropModeReplace, (unsigned char *) netatom, NetLast);
1574 XDeleteProperty(dpy, root, netatom[NetClientList]);
1575 /* select for events */
1576 wa.cursor = cursor[CurNormal]->cursor;
1577 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
1578 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1579 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1580 XSelectInput(dpy, root, wa.event_mask);
1586 showhide(Client *c) {
1589 if(ISVISIBLE(c)) { /* show clients top down */
1590 XMoveWindow(dpy, c->win, c->x, c->y);
1591 if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1592 resize(c, c->x, c->y, c->w, c->h, False);
1595 else { /* hide clients bottom up */
1597 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1602 sigchld(int unused) {
1603 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1604 die("Can't install SIGCHLD handler");
1605 while(0 < waitpid(-1, NULL, WNOHANG));
1609 spawn(const Arg *arg) {
1610 if(arg->v == dmenucmd)
1611 dmenumon[0] = '0' + selmon->num;
1614 close(ConnectionNumber(dpy));
1616 execvp(((char **)arg->v)[0], (char **)arg->v);
1617 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1624 tag(const Arg *arg) {
1625 if(selmon->sel && arg->ui & TAGMASK) {
1626 selmon->sel->tags = arg->ui & TAGMASK;
1633 tagmon(const Arg *arg) {
1634 if(!selmon->sel || !mons->next)
1636 sendmon(selmon->sel, dirtomon(arg->i));
1641 unsigned int i, n, h, mw, my, ty;
1644 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1649 mw = m->nmaster ? m->ww * m->mfact : 0;
1652 for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1653 if(i < m->nmaster) {
1654 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1655 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False);
1659 h = (m->wh - ty) / (n - i);
1660 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), False);
1666 togglebar(const Arg *arg) {
1667 selmon->showbar = !selmon->showbar;
1668 updatebarpos(selmon);
1669 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1674 togglefloating(const Arg *arg) {
1677 if(selmon->sel->isfullscreen) /* no support for fullscreen windows */
1679 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1680 if(selmon->sel->isfloating)
1681 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1682 selmon->sel->w, selmon->sel->h, False);
1687 toggletag(const Arg *arg) {
1688 unsigned int newtags;
1692 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1694 selmon->sel->tags = newtags;
1701 toggleview(const Arg *arg) {
1702 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1705 selmon->tagset[selmon->seltags] = newtagset;
1712 unfocus(Client *c, Bool setfocus) {
1715 grabbuttons(c, False);
1716 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->rgb);
1718 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1719 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1724 unmanage(Client *c, Bool destroyed) {
1725 Monitor *m = c->mon;
1728 /* The server grab construct avoids race conditions. */
1732 wc.border_width = c->oldbw;
1734 XSetErrorHandler(xerrordummy);
1735 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1736 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1737 setclientstate(c, WithdrawnState);
1739 XSetErrorHandler(xerror);
1749 unmapnotify(XEvent *e) {
1751 XUnmapEvent *ev = &e->xunmap;
1753 if((c = wintoclient(ev->window))) {
1755 setclientstate(c, WithdrawnState);
1764 XSetWindowAttributes wa = {
1765 .override_redirect = True,
1766 .background_pixmap = ParentRelative,
1767 .event_mask = ButtonPressMask|ExposureMask
1769 for(m = mons; m; m = m->next) {
1772 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1773 CopyFromParent, DefaultVisual(dpy, screen),
1774 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1775 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
1776 XMapRaised(dpy, m->barwin);
1781 updatebarpos(Monitor *m) {
1786 m->by = m->topbar ? m->wy : m->wy + m->wh;
1787 m->wy = m->topbar ? m->wy + bh : m->wy;
1794 updateclientlist() {
1798 XDeleteProperty(dpy, root, netatom[NetClientList]);
1799 for(m = mons; m; m = m->next)
1800 for(c = m->clients; c; c = c->next)
1801 XChangeProperty(dpy, root, netatom[NetClientList],
1802 XA_WINDOW, 32, PropModeAppend,
1803 (unsigned char *) &(c->win), 1);
1811 if(XineramaIsActive(dpy)) {
1815 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1816 XineramaScreenInfo *unique = NULL;
1818 for(n = 0, m = mons; m; m = m->next, n++);
1819 /* only consider unique geometries as separate screens */
1820 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
1821 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
1822 for(i = 0, j = 0; i < nn; i++)
1823 if(isuniquegeom(unique, j, &info[i]))
1824 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1828 for(i = 0; i < (nn - n); i++) { /* new monitors available */
1829 for(m = mons; m && m->next; m = m->next);
1831 m->next = createmon();
1835 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
1837 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1838 || unique[i].width != m->mw || unique[i].height != m->mh))
1842 m->mx = m->wx = unique[i].x_org;
1843 m->my = m->wy = unique[i].y_org;
1844 m->mw = m->ww = unique[i].width;
1845 m->mh = m->wh = unique[i].height;
1849 else { /* less monitors available nn < n */
1850 for(i = nn; i < n; i++) {
1851 for(m = mons; m && m->next; m = m->next);
1855 m->clients = c->next;
1869 #endif /* XINERAMA */
1870 /* default monitor setup */
1874 if(mons->mw != sw || mons->mh != sh) {
1876 mons->mw = mons->ww = sw;
1877 mons->mh = mons->wh = sh;
1883 selmon = wintomon(root);
1889 updatenumlockmask(void) {
1891 XModifierKeymap *modmap;
1894 modmap = XGetModifierMapping(dpy);
1895 for(i = 0; i < 8; i++)
1896 for(j = 0; j < modmap->max_keypermod; j++)
1897 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1898 == XKeysymToKeycode(dpy, XK_Num_Lock))
1899 numlockmask = (1 << i);
1900 XFreeModifiermap(modmap);
1904 updatesizehints(Client *c) {
1908 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
1909 /* size is uninitialized, ensure that size.flags aren't used */
1911 if(size.flags & PBaseSize) {
1912 c->basew = size.base_width;
1913 c->baseh = size.base_height;
1915 else if(size.flags & PMinSize) {
1916 c->basew = size.min_width;
1917 c->baseh = size.min_height;
1920 c->basew = c->baseh = 0;
1921 if(size.flags & PResizeInc) {
1922 c->incw = size.width_inc;
1923 c->inch = size.height_inc;
1926 c->incw = c->inch = 0;
1927 if(size.flags & PMaxSize) {
1928 c->maxw = size.max_width;
1929 c->maxh = size.max_height;
1932 c->maxw = c->maxh = 0;
1933 if(size.flags & PMinSize) {
1934 c->minw = size.min_width;
1935 c->minh = size.min_height;
1937 else if(size.flags & PBaseSize) {
1938 c->minw = size.base_width;
1939 c->minh = size.base_height;
1942 c->minw = c->minh = 0;
1943 if(size.flags & PAspect) {
1944 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
1945 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
1948 c->maxa = c->mina = 0.0;
1949 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
1950 && c->maxw == c->minw && c->maxh == c->minh);
1954 updatetitle(Client *c) {
1955 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
1956 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
1957 if(c->name[0] == '\0') /* hack to mark broken clients */
1958 strcpy(c->name, broken);
1962 updatestatus(void) {
1963 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
1964 strcpy(stext, "dwm-"VERSION);
1969 updatewindowtype(Client *c) {
1970 Atom state = getatomprop(c, netatom[NetWMState]);
1971 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
1973 if(state == netatom[NetWMFullscreen])
1974 setfullscreen(c, True);
1975 if(wtype == netatom[NetWMWindowTypeDialog])
1976 c->isfloating = True;
1980 updatewmhints(Client *c) {
1983 if((wmh = XGetWMHints(dpy, c->win))) {
1984 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
1985 wmh->flags &= ~XUrgencyHint;
1986 XSetWMHints(dpy, c->win, wmh);
1989 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
1990 if(wmh->flags & InputHint)
1991 c->neverfocus = !wmh->input;
1993 c->neverfocus = False;
1999 view(const Arg *arg) {
2000 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2002 selmon->seltags ^= 1; /* toggle sel tagset */
2003 if(arg->ui & TAGMASK)
2004 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2010 wintoclient(Window w) {
2014 for(m = mons; m; m = m->next)
2015 for(c = m->clients; c; c = c->next)
2022 wintomon(Window w) {
2027 if(w == root && getrootptr(&x, &y))
2028 return recttomon(x, y, 1, 1);
2029 for(m = mons; m; m = m->next)
2032 if((c = wintoclient(w)))
2037 /* There's no way to check accesses to destroyed windows, thus those cases are
2038 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2039 * default error handler, which may call exit. */
2041 xerror(Display *dpy, XErrorEvent *ee) {
2042 if(ee->error_code == BadWindow
2043 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2044 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2045 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2046 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2047 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2048 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2049 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2050 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2052 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2053 ee->request_code, ee->error_code);
2054 return xerrorxlib(dpy, ee); /* may call exit */
2058 xerrordummy(Display *dpy, XErrorEvent *ee) {
2062 /* Startup Error handler to check if another window manager
2063 * is already running. */
2065 xerrorstart(Display *dpy, XErrorEvent *ee) {
2066 die("dwm: another window manager is already running\n");
2071 zoom(const Arg *arg) {
2072 Client *c = selmon->sel;
2074 if(!selmon->lt[selmon->sellt]->arrange
2075 || (selmon->sel && selmon->sel->isfloating))
2077 if(c == nexttiled(selmon->clients))
2078 if(!c || !(c = nexttiled(c->next)))
2084 main(int argc, char *argv[]) {
2085 if(argc == 2 && !strcmp("-v", argv[1]))
2086 die("dwm-"VERSION", © 2006-2014 dwm engineers, see LICENSE for details\n");
2088 die("usage: dwm [-v]\n");
2089 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2090 fputs("warning: no locale support\n", stderr);
2091 if(!(dpy = XOpenDisplay(NULL)))
2092 die("dwm: cannot open display\n");
2099 return EXIT_SUCCESS;