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_as_master(Client *c);
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 Client *nextvisible(Client *c);
189 static void pop(Client *);
190 static void propertynotify(XEvent *e);
191 static void quit(const Arg *arg);
192 static Monitor *recttomon(int x, int y, int w, int h);
193 static void resize(Client *c, int x, int y, int w, int h, Bool interact);
194 static void resizeclient(Client *c, int x, int y, int w, int h);
195 static void resizemouse(const Arg *arg);
196 static void restack(Monitor *m);
197 static void run(void);
198 static void scan(void);
199 static Bool sendevent(Client *c, Atom proto);
200 static void sendmon(Client *c, Monitor *m);
201 static void setclientstate(Client *c, long state);
202 static void setfocus(Client *c);
203 static void setfullscreen(Client *c, Bool fullscreen);
204 static void setlayout(const Arg *arg);
205 static void setmfact(const Arg *arg);
206 static void setup(void);
207 static void showhide(Client *c);
208 static void sigchld(int unused);
209 static void spawn(const Arg *arg);
210 static void tag(const Arg *arg);
211 static void tagmon(const Arg *arg);
212 static void tile(Monitor *);
213 static void togglebar(const Arg *arg);
214 static void togglefloating(const Arg *arg);
215 static void toggletag(const Arg *arg);
216 static void toggleview(const Arg *arg);
217 static void unfocus(Client *c, Bool setfocus);
218 static void unmanage(Client *c, Bool destroyed);
219 static void unmapnotify(XEvent *e);
220 static Bool updategeom(void);
221 static void updatebarpos(Monitor *m);
222 static void updatebars(void);
223 static void updateclientlist(void);
224 static void updatenumlockmask(void);
225 static void updatesizehints(Client *c);
226 static void updatestatus(void);
227 static void updatewindowtype(Client *c);
228 static void updatetitle(Client *c);
229 static void updatewmhints(Client *c);
230 static void view(const Arg *arg);
231 static Client *wintoclient(Window w);
232 static Monitor *wintomon(Window w);
233 static int xerror(Display *dpy, XErrorEvent *ee);
234 static int xerrordummy(Display *dpy, XErrorEvent *ee);
235 static int xerrorstart(Display *dpy, XErrorEvent *ee);
236 static void zoom(const Arg *arg);
239 static const char broken[] = "broken";
240 static char stext[256];
242 static int sw, sh; /* X display screen geometry width, height */
243 static int bh, blw = 0; /* bar geometry */
244 static int (*xerrorxlib)(Display *, XErrorEvent *);
245 static unsigned int numlockmask = 0;
246 static void (*handler[LASTEvent]) (XEvent *) = {
247 [ButtonPress] = buttonpress,
248 [ClientMessage] = clientmessage,
249 [ConfigureRequest] = configurerequest,
250 [ConfigureNotify] = configurenotify,
251 [DestroyNotify] = destroynotify,
252 [EnterNotify] = enternotify,
255 [KeyPress] = keypress,
256 [MappingNotify] = mappingnotify,
257 [MapRequest] = maprequest,
258 [MotionNotify] = motionnotify,
259 [PropertyNotify] = propertynotify,
260 [UnmapNotify] = unmapnotify
262 static Atom wmatom[WMLast], netatom[NetLast];
263 static Bool running = True;
264 static Cur *cursor[CurLast];
265 static ClrScheme scheme[SchemeLast];
269 static Monitor *mons, *selmon;
272 // unfocused windows get transparent (feature)
273 static const unsigned long unfocused_opacity[] = { 0xbfffffff };
274 static void window_set_opaque(Client *c);
275 static void window_set_translucent(Client *c);
277 window_set_opaque(Client *c) {
278 XDeleteProperty(dpy, c->win, netatom[NetWMWindowOpacity]);
281 window_set_translucent(Client *c) {
282 XChangeProperty(dpy, c->win, netatom[NetWMWindowOpacity], XA_CARDINAL, 32, PropModeReplace, (unsigned char *)unfocused_opacity, 1);
286 /* configuration, allows nested code to access above variables */
289 /* compile-time check if all tags fit into an unsigned int bit array. */
290 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
292 /* function implementations */
294 applyrules(Client *c) {
295 const char *class, *instance;
299 XClassHint ch = { NULL, NULL };
302 c->isfloating = c->tags = 0;
303 XGetClassHint(dpy, c->win, &ch);
304 class = ch.res_class ? ch.res_class : broken;
305 instance = ch.res_name ? ch.res_name : broken;
307 for(i = 0; i < LENGTH(rules); i++) {
309 if((!r->title || strstr(c->name, r->title))
310 && (!r->class || strstr(class, r->class))
311 && (!r->instance || strstr(instance, r->instance)))
313 c->isfloating = r->isfloating;
315 for(m = mons; m && m->num != r->monitor; m = m->next);
324 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
328 applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
332 /* set minimum possible */
340 if(*x + *w + 2 * c->bw < 0)
342 if(*y + *h + 2 * c->bw < 0)
346 if(*x >= m->wx + m->ww)
347 *x = m->wx + m->ww - WIDTH(c);
348 if(*y >= m->wy + m->wh)
349 *y = m->wy + m->wh - HEIGHT(c);
350 if(*x + *w + 2 * c->bw <= m->wx)
352 if(*y + *h + 2 * c->bw <= m->wy)
359 if(resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
360 /* see last two sentences in ICCCM 4.1.2.3 */
361 baseismin = c->basew == c->minw && c->baseh == c->minh;
362 if(!baseismin) { /* temporarily remove base dimensions */
366 /* adjust for aspect limits */
367 if(c->mina > 0 && c->maxa > 0) {
368 if(c->maxa < (float)*w / *h)
369 *w = *h * c->maxa + 0.5;
370 else if(c->mina < (float)*h / *w)
371 *h = *w * c->mina + 0.5;
373 if(baseismin) { /* increment calculation requires this */
377 /* adjust for increment value */
382 /* restore base dimensions */
383 *w = MAX(*w + c->basew, c->minw);
384 *h = MAX(*h + c->baseh, c->minh);
386 *w = MIN(*w, c->maxw);
388 *h = MIN(*h, c->maxh);
390 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
394 arrange(Monitor *m) {
397 else for(m = mons; m; m = m->next)
402 } else for(m = mons; m; m = m->next)
407 arrangemon(Monitor *m) {
408 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
409 if(m->lt[m->sellt]->arrange)
410 m->lt[m->sellt]->arrange(m);
414 attach_as_master(Client *c) {
415 c->next = c->mon->clients;
421 c->next = c->mon->sel->next;
422 c->mon->sel->next = c;
429 attachstack(Client *c) {
430 c->snext = c->mon->stack;
435 buttonpress(XEvent *e) {
436 unsigned int i, x, click;
440 XButtonPressedEvent *ev = &e->xbutton;
443 /* focus monitor if necessary */
444 if((m = wintomon(ev->window)) && m != selmon) {
445 unfocus(selmon->sel, True);
449 if(ev->window == selmon->barwin) {
453 while(ev->x >= x && ++i < LENGTH(tags));
454 if(i < LENGTH(tags)) {
458 else if(ev->x < x + blw)
460 else if(ev->x > selmon->ww - TEXTW(stext))
461 click = ClkStatusText;
465 else if((c = wintoclient(ev->window))) {
467 click = ClkClientWin;
469 for(i = 0; i < LENGTH(buttons); i++)
470 if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
471 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
472 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
477 xerrorxlib = XSetErrorHandler(xerrorstart);
478 /* this causes an error if some other window manager is running */
479 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
481 XSetErrorHandler(xerror);
488 Layout foo = { "", NULL };
492 selmon->lt[selmon->sellt] = &foo;
493 for(m = mons; m; m = m->next)
495 unmanage(m->stack, False);
496 XUngrabKey(dpy, AnyKey, AnyModifier, root);
499 drw_cur_free(drw, cursor[CurNormal]);
500 drw_cur_free(drw, cursor[CurResize]);
501 drw_cur_free(drw, cursor[CurMove]);
502 drw_font_free(dpy, fnt);
503 drw_clr_free(scheme[SchemeNorm].border);
504 drw_clr_free(scheme[SchemeNorm].bg);
505 drw_clr_free(scheme[SchemeNorm].fg);
506 drw_clr_free(scheme[SchemeSel].border);
507 drw_clr_free(scheme[SchemeSel].bg);
508 drw_clr_free(scheme[SchemeSel].fg);
511 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
512 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
516 cleanupmon(Monitor *mon) {
522 for(m = mons; m && m->next != mon; m = m->next);
525 XUnmapWindow(dpy, mon->barwin);
526 XDestroyWindow(dpy, mon->barwin);
531 clearurgent(Client *c) {
535 if(!(wmh = XGetWMHints(dpy, c->win)))
537 wmh->flags &= ~XUrgencyHint;
538 XSetWMHints(dpy, c->win, wmh);
543 clientmessage(XEvent *e) {
544 XClientMessageEvent *cme = &e->xclient;
545 Client *c = wintoclient(cme->window);
549 if(cme->message_type == netatom[NetWMState]) {
550 if(cme->data.l[1] == netatom[NetWMFullscreen] || cme->data.l[2] == netatom[NetWMFullscreen])
551 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
552 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
554 else if(cme->message_type == netatom[NetActiveWindow]) {
557 c->mon->seltags ^= 1;
558 c->mon->tagset[c->mon->seltags] = c->tags;
565 configure(Client *c) {
568 ce.type = ConfigureNotify;
576 ce.border_width = c->bw;
578 ce.override_redirect = False;
579 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
583 configurenotify(XEvent *e) {
585 XConfigureEvent *ev = &e->xconfigure;
588 // TODO: updategeom handling sucks, needs to be simplified
589 if(ev->window == root) {
590 dirty = (sw != ev->width || sh != ev->height);
593 if(updategeom() || dirty) {
594 drw_resize(drw, sw, bh);
596 for(m = mons; m; m = m->next)
597 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
605 configurerequest(XEvent *e) {
608 XConfigureRequestEvent *ev = &e->xconfigurerequest;
611 if((c = wintoclient(ev->window))) {
612 if(ev->value_mask & CWBorderWidth)
613 c->bw = ev->border_width;
614 else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
616 if(ev->value_mask & CWX) {
618 c->x = m->mx + ev->x;
620 if(ev->value_mask & CWY) {
622 c->y = m->my + ev->y;
624 if(ev->value_mask & CWWidth) {
628 if(ev->value_mask & CWHeight) {
632 if((c->x + c->w) > m->mx + m->mw && c->isfloating)
633 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
634 if((c->y + c->h) > m->my + m->mh && c->isfloating)
635 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
636 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
639 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
647 wc.width = ev->width;
648 wc.height = ev->height;
649 wc.border_width = ev->border_width;
650 wc.sibling = ev->above;
651 wc.stack_mode = ev->detail;
652 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
661 if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
662 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
663 m->tagset[0] = m->tagset[1] = 1;
665 m->nmaster = nmaster;
666 m->showbar = showbar;
668 m->lt[0] = &layouts[0];
669 m->lt[1] = &layouts[1 % LENGTH(layouts)];
670 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
675 destroynotify(XEvent *e) {
677 XDestroyWindowEvent *ev = &e->xdestroywindow;
679 if((c = wintoclient(ev->window)))
687 for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
691 // NOTE: the stack is for z-order and most-recently-focused
692 // only mon->clients determines position in visible layout
694 detachstack(Client *c) {
695 Client *prev = NULL, *next_sel = NULL, *i;
696 for(i = c->mon->stack; i && i != c; i = i->snext) {
699 if(c == c->mon->sel) {
700 // find last visible window before c
701 // WARNING if you detach() before detachstack() this will select last visible window
702 for(i = nextvisible(c->mon->clients); i && i != c; i = nextvisible(i->next))
704 // failing that, find first visible window (besides c)
706 for(i = nextvisible(c->mon->clients); i && i == c; i = nextvisible(i->next));
709 c->mon->sel = next_sel;
712 prev->snext = c->snext;
714 c->mon->stack = c->snext;
723 if(!(m = selmon->next))
726 else if(selmon == mons)
727 for(m = mons; m->next; m = m->next);
729 for(m = mons; m->next != selmon; m = m->next);
734 drawbar(Monitor *m) {
736 unsigned int i, occ = 0, urg = 0;
739 for(c = m->clients; c; c = c->next) {
745 for(i = 0; i < LENGTH(tags); i++) {
747 drw_setscheme(drw, m->tagset[m->seltags] & 1 << i ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
748 drw_text(drw, x, 0, w, bh, tags[i], urg & 1 << i);
749 drw_rect(drw, x, 0, w, bh, m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
750 occ & 1 << i, urg & 1 << i);
753 w = blw = TEXTW(m->ltsymbol);
754 drw_setscheme(drw, &scheme[SchemeNorm]);
755 drw_text(drw, x, 0, w, bh, m->ltsymbol, 0);
758 if(m == selmon) { /* status is only drawn on selected monitor */
765 drw_text(drw, x, 0, w, bh, stext, 0);
769 if((w = x - xx) > bh) {
772 drw_setscheme(drw, m == selmon ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
773 drw_text(drw, x, 0, w, bh, m->sel->name, 0);
774 drw_rect(drw, x, 0, w, bh, m->sel->isfixed, m->sel->isfloating, 0);
777 drw_setscheme(drw, &scheme[SchemeNorm]);
778 drw_text(drw, x, 0, w, bh, NULL, 0);
781 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
788 for(m = mons; m; m = m->next)
793 enternotify(XEvent *e) {
796 XCrossingEvent *ev = &e->xcrossing;
798 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
800 c = wintoclient(ev->window);
801 m = c ? c->mon : wintomon(ev->window);
803 unfocus(selmon->sel, True);
806 else if(!c || c == selmon->sel)
814 XExposeEvent *ev = &e->xexpose;
816 if(ev->count == 0 && (m = wintomon(ev->window)))
822 if(!c || !ISVISIBLE(c))
823 for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
824 /* was if(selmon->sel) */
825 if(selmon->sel && selmon->sel != c)
826 unfocus(selmon->sel, False);
834 grabbuttons(c, True);
835 XSetWindowBorder(dpy, c->win, scheme[SchemeSel].border->rgb);
839 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
840 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
844 if(c && (!root || (c->win!=root)) )
845 window_set_opaque(c);
849 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
850 XFocusChangeEvent *ev = &e->xfocus;
852 if(selmon->sel && ev->window != selmon->sel->win)
853 setfocus(selmon->sel);
857 focusmon(const Arg *arg) {
862 if((m = dirtomon(arg->i)) == selmon)
864 unfocus(selmon->sel, False); /* s/True/False/ fixes input focus issues
865 in gedit and anjuta */
871 focusstack(const Arg *arg) {
872 Client *c = NULL, *i;
877 for(i = selmon->clients; i != selmon->sel; i = i->next) {
883 } else if(arg->i > 0) {
884 for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
886 for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
889 for(i = selmon->clients; i != selmon->sel; i = i->next)
893 for(; i; i = i->next)
904 getatomprop(Client *c, Atom prop) {
907 unsigned char *p = NULL;
908 Atom da, atom = None;
910 if(XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
911 &da, &di, &dl, &dl, &p) == Success && p) {
919 getrootptr(int *x, int *y) {
924 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
931 unsigned char *p = NULL;
932 unsigned long n, extra;
935 if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
936 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
945 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
950 if(!text || size == 0)
953 XGetTextProperty(dpy, w, &name, atom);
956 if(name.encoding == XA_STRING)
957 strncpy(text, (char *)name.value, size - 1);
959 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
960 strncpy(text, *list, size - 1);
961 XFreeStringList(list);
964 text[size - 1] = '\0';
970 grabbuttons(Client *c, Bool focused) {
974 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
975 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
977 for(i = 0; i < LENGTH(buttons); i++)
978 if(buttons[i].click == ClkClientWin)
979 for(j = 0; j < LENGTH(modifiers); j++)
980 XGrabButton(dpy, buttons[i].button,
981 buttons[i].mask | modifiers[j],
982 c->win, False, BUTTONMASK,
983 GrabModeAsync, GrabModeSync, None, None);
986 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
987 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
996 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
999 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1000 for(i = 0; i < LENGTH(keys); i++)
1001 if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
1002 for(j = 0; j < LENGTH(modifiers); j++)
1003 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
1004 True, GrabModeAsync, GrabModeAsync);
1009 incnmaster(const Arg *arg) {
1010 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
1016 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
1018 if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1019 && unique[n].width == info->width && unique[n].height == info->height)
1023 #endif /* XINERAMA */
1026 keypress(XEvent *e) {
1032 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1033 for(i = 0; i < LENGTH(keys); i++)
1034 if(keysym == keys[i].keysym
1035 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1037 keys[i].func(&(keys[i].arg));
1041 killclient(const Arg *arg) {
1044 if(!sendevent(selmon->sel, wmatom[WMDelete])) {
1046 XSetErrorHandler(xerrordummy);
1047 XSetCloseDownMode(dpy, DestroyAll);
1048 XKillClient(dpy, selmon->sel->win);
1050 XSetErrorHandler(xerror);
1056 manage(Window w, XWindowAttributes *wa) {
1057 Client *c, *t = NULL;
1058 Window trans = None;
1061 if(!(c = calloc(1, sizeof(Client))))
1062 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1065 if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1074 c->x = c->oldx = wa->x;
1075 c->y = c->oldy = wa->y;
1076 c->w = c->oldw = wa->width;
1077 c->h = c->oldh = wa->height;
1078 c->oldbw = wa->border_width;
1080 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1081 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1082 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1083 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1084 c->x = MAX(c->x, c->mon->mx);
1085 /* only fix client y-offset, if the client center might cover the bar */
1086 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1087 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1090 wc.border_width = c->bw;
1091 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1092 XSetWindowBorder(dpy, w, scheme[SchemeNorm].border->rgb);
1093 configure(c); /* propagates border_width, if size doesn't change */
1094 updatewindowtype(c);
1097 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1098 grabbuttons(c, False);
1100 c->isfloating = c->oldstate = trans != None || c->isfixed;
1102 XRaiseWindow(dpy, c->win);
1105 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1106 (unsigned char *) &(c->win), 1);
1107 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1108 setclientstate(c, NormalState);
1109 if (c->mon == selmon)
1110 unfocus(selmon->sel, False);
1113 XMapWindow(dpy, c->win);
1118 mappingnotify(XEvent *e) {
1119 XMappingEvent *ev = &e->xmapping;
1121 XRefreshKeyboardMapping(ev);
1122 if(ev->request == MappingKeyboard)
1127 maprequest(XEvent *e) {
1128 static XWindowAttributes wa;
1129 XMapRequestEvent *ev = &e->xmaprequest;
1131 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1133 if(wa.override_redirect)
1135 if(!wintoclient(ev->window))
1136 manage(ev->window, &wa);
1140 monocle(Monitor *m) {
1144 for(c = m->clients; c; c = c->next)
1147 if(n > 0) /* override layout symbol */
1148 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1149 for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
1150 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
1154 motionnotify(XEvent *e) {
1155 static Monitor *mon = NULL;
1157 XMotionEvent *ev = &e->xmotion;
1159 if(ev->window != root)
1161 if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1162 unfocus(selmon->sel, True);
1170 movemouse(const Arg *arg) {
1171 int x, y, ocx, ocy, nx, ny;
1177 if(!(c = selmon->sel))
1179 if(c->isfullscreen) /* no support moving fullscreen windows by mouse */
1184 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1185 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1187 if(!getrootptr(&x, &y))
1190 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1192 case ConfigureRequest:
1195 handler[ev.type](&ev);
1198 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1200 lasttime = ev.xmotion.time;
1202 nx = ocx + (ev.xmotion.x - x);
1203 ny = ocy + (ev.xmotion.y - y);
1204 if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1205 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1206 if(abs(selmon->wx - nx) < snap)
1208 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1209 nx = selmon->wx + selmon->ww - WIDTH(c);
1210 if(abs(selmon->wy - ny) < snap)
1212 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1213 ny = selmon->wy + selmon->wh - HEIGHT(c);
1214 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1215 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1216 togglefloating(NULL);
1218 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1219 resize(c, nx, ny, c->w, c->h, True);
1222 } while(ev.type != ButtonRelease);
1223 XUngrabPointer(dpy, CurrentTime);
1224 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1232 nexttiled(Client *c) {
1233 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1238 nextvisible(Client *c) {
1239 for(; c && !ISVISIBLE(c); c = c->next);
1246 attach_as_master(c);
1252 propertynotify(XEvent *e) {
1255 XPropertyEvent *ev = &e->xproperty;
1257 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1259 else if(ev->state == PropertyDelete)
1260 return; /* ignore */
1261 else if((c = wintoclient(ev->window))) {
1264 case XA_WM_TRANSIENT_FOR:
1265 if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1266 (c->isfloating = (wintoclient(trans)) != NULL))
1269 case XA_WM_NORMAL_HINTS:
1277 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1279 if(c == c->mon->sel)
1282 if(ev->atom == netatom[NetWMWindowType])
1283 updatewindowtype(c);
1288 quit(const Arg *arg) {
1293 recttomon(int x, int y, int w, int h) {
1294 Monitor *m, *r = selmon;
1297 for(m = mons; m; m = m->next)
1298 if((a = INTERSECT(x, y, w, h, m)) > area) {
1306 resize(Client *c, int x, int y, int w, int h, Bool interact) {
1307 if(applysizehints(c, &x, &y, &w, &h, interact))
1308 resizeclient(c, x, y, w, h);
1312 resizeclient(Client *c, int x, int y, int w, int h) {
1315 c->oldx = c->x; c->x = wc.x = x;
1316 c->oldy = c->y; c->y = wc.y = y;
1317 c->oldw = c->w; c->w = wc.width = w;
1318 c->oldh = c->h; c->h = wc.height = h;
1319 wc.border_width = c->bw;
1320 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1326 resizemouse(const Arg *arg) {
1327 int ocx, ocy, nw, nh;
1333 if(!(c = selmon->sel))
1335 if(c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1340 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1341 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1343 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1345 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1347 case ConfigureRequest:
1350 handler[ev.type](&ev);
1353 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1355 lasttime = ev.xmotion.time;
1357 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1358 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1359 if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1360 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1362 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1363 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1364 togglefloating(NULL);
1366 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1367 resize(c, c->x, c->y, nw, nh, True);
1370 } while(ev.type != ButtonRelease);
1371 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1372 XUngrabPointer(dpy, CurrentTime);
1373 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1374 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1382 restack(Monitor *m) {
1390 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1391 XRaiseWindow(dpy, m->sel->win);
1392 if(m->lt[m->sellt]->arrange) {
1393 wc.stack_mode = Below;
1394 wc.sibling = m->barwin;
1395 for(c = m->stack; c; c = c->snext)
1396 if(!c->isfloating && ISVISIBLE(c)) {
1397 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1398 wc.sibling = c->win;
1402 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1408 /* main event loop */
1410 while(running && !XNextEvent(dpy, &ev))
1411 if(handler[ev.type])
1412 handler[ev.type](&ev); /* call handler */
1417 unsigned int i, num;
1418 Window d1, d2, *wins = NULL;
1419 XWindowAttributes wa;
1421 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1422 for(i = 0; i < num; i++) {
1423 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1424 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1426 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1427 manage(wins[i], &wa);
1429 for(i = 0; i < num; i++) { /* now the transients */
1430 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1432 if(XGetTransientForHint(dpy, wins[i], &d1)
1433 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1434 manage(wins[i], &wa);
1442 sendmon(Client *c, Monitor *m) {
1449 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1457 setclientstate(Client *c, long state) {
1458 long data[] = { state, None };
1460 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1461 PropModeReplace, (unsigned char *)data, 2);
1465 sendevent(Client *c, Atom proto) {
1468 Bool exists = False;
1471 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1472 while(!exists && n--)
1473 exists = protocols[n] == proto;
1477 ev.type = ClientMessage;
1478 ev.xclient.window = c->win;
1479 ev.xclient.message_type = wmatom[WMProtocols];
1480 ev.xclient.format = 32;
1481 ev.xclient.data.l[0] = proto;
1482 ev.xclient.data.l[1] = CurrentTime;
1483 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1489 setfocus(Client *c) {
1490 if(!c->neverfocus) {
1491 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1492 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1493 XA_WINDOW, 32, PropModeReplace,
1494 (unsigned char *) &(c->win), 1);
1496 sendevent(c, wmatom[WMTakeFocus]);
1500 setfullscreen(Client *c, Bool fullscreen) {
1502 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1503 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1504 c->isfullscreen = True;
1505 c->oldstate = c->isfloating;
1508 c->isfloating = True;
1509 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1510 XRaiseWindow(dpy, c->win);
1513 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1514 PropModeReplace, (unsigned char*)0, 0);
1515 c->isfullscreen = False;
1516 c->isfloating = c->oldstate;
1522 resizeclient(c, c->x, c->y, c->w, c->h);
1528 setlayout(const Arg *arg) {
1529 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1532 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1533 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1540 /* arg > 1.0 will set mfact absolutly */
1542 setmfact(const Arg *arg) {
1545 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1547 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1548 if(f < 0.1 || f > 0.9)
1556 XSetWindowAttributes wa;
1558 /* clean up any zombies immediately */
1562 screen = DefaultScreen(dpy);
1563 root = RootWindow(dpy, screen);
1564 fnt = drw_font_create(dpy, font);
1565 sw = DisplayWidth(dpy, screen);
1566 sh = DisplayHeight(dpy, screen);
1568 drw = drw_create(dpy, screen, root, sw, sh);
1569 drw_setfont(drw, fnt);
1572 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1573 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1574 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1575 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1576 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1577 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1578 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1579 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1580 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1581 netatom[NetWMWindowOpacity] = XInternAtom(dpy, "_NET_WM_WINDOW_OPACITY", False);
1582 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1583 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1584 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1586 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1587 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1588 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1589 /* init appearance */
1590 scheme[SchemeNorm].border = drw_clr_create(drw, normbordercolor);
1591 scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor);
1592 scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor);
1593 scheme[SchemeSel].border = drw_clr_create(drw, selbordercolor);
1594 scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor);
1595 scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor);
1599 /* EWMH support per view */
1600 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1601 PropModeReplace, (unsigned char *) netatom, NetLast);
1602 XDeleteProperty(dpy, root, netatom[NetClientList]);
1603 /* select for events */
1604 wa.cursor = cursor[CurNormal]->cursor;
1605 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
1606 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1607 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1608 XSelectInput(dpy, root, wa.event_mask);
1614 showhide(Client *c) {
1617 if(ISVISIBLE(c)) { /* show clients top down */
1618 XMoveWindow(dpy, c->win, c->x, c->y);
1619 if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1620 resize(c, c->x, c->y, c->w, c->h, False);
1623 else { /* hide clients bottom up */
1625 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1630 sigchld(int unused) {
1631 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1632 die("Can't install SIGCHLD handler");
1633 while(0 < waitpid(-1, NULL, WNOHANG));
1637 spawn(const Arg *arg) {
1639 if(arg->v == termcmd) {
1640 for(i = 0; i < 32; ++i) {
1641 if(selmon->tagset[selmon->seltags] & (1 << i)) {
1646 _SWM_WS[8] = swm_tags[tag][0];
1647 _SWM_WS[9] = swm_tags[tag][1];
1649 if(arg->v == dmenucmd)
1650 dmenumon[0] = '0' + selmon->num;
1653 close(ConnectionNumber(dpy));
1655 execvp(((char **)arg->v)[0], (char **)arg->v);
1656 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1663 tag(const Arg *arg) {
1664 if(selmon->sel && arg->ui & TAGMASK) {
1665 selmon->sel->tags = arg->ui & TAGMASK;
1672 tagmon(const Arg *arg) {
1673 if(!selmon->sel || !mons->next)
1675 sendmon(selmon->sel, dirtomon(arg->i));
1680 unsigned int i, n, h, mw, my, ty;
1683 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1688 mw = m->nmaster ? m->ww * m->mfact : 0;
1691 for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1692 if(i < m->nmaster) {
1693 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1694 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False);
1698 h = (m->wh - ty) / (n - i);
1699 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), False);
1705 togglebar(const Arg *arg) {
1706 selmon->showbar = !selmon->showbar;
1707 updatebarpos(selmon);
1708 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1713 togglefloating(const Arg *arg) {
1716 if(selmon->sel->isfullscreen) /* no support for fullscreen windows */
1718 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1719 if(selmon->sel->isfloating)
1720 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1721 selmon->sel->w, selmon->sel->h, False);
1726 toggletag(const Arg *arg) {
1727 unsigned int newtags;
1731 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1733 selmon->sel->tags = newtags;
1740 toggleview(const Arg *arg) {
1741 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1744 selmon->tagset[selmon->seltags] = newtagset;
1751 unfocus(Client *c, Bool setfocus) {
1754 if(!root || c->win!=root)
1755 window_set_translucent(c);
1756 grabbuttons(c, False);
1757 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->rgb);
1759 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1760 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1765 unmanage(Client *c, Bool destroyed) {
1766 Monitor *m = c->mon;
1769 /* The server grab construct avoids race conditions. */
1773 wc.border_width = c->oldbw;
1775 XSetErrorHandler(xerrordummy);
1776 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1777 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1778 setclientstate(c, WithdrawnState);
1780 XSetErrorHandler(xerror);
1784 focus(selmon ? selmon->sel : NULL);
1790 unmapnotify(XEvent *e) {
1792 XUnmapEvent *ev = &e->xunmap;
1794 if((c = wintoclient(ev->window))) {
1796 setclientstate(c, WithdrawnState);
1805 XSetWindowAttributes wa = {
1806 .override_redirect = True,
1807 .background_pixmap = ParentRelative,
1808 .event_mask = ButtonPressMask|ExposureMask
1810 for(m = mons; m; m = m->next) {
1813 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1814 CopyFromParent, DefaultVisual(dpy, screen),
1815 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1816 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
1817 XMapRaised(dpy, m->barwin);
1822 updatebarpos(Monitor *m) {
1827 m->by = m->topbar ? m->wy : m->wy + m->wh;
1828 m->wy = m->topbar ? m->wy + bh : m->wy;
1835 updateclientlist() {
1839 XDeleteProperty(dpy, root, netatom[NetClientList]);
1840 for(m = mons; m; m = m->next)
1841 for(c = m->clients; c; c = c->next)
1842 XChangeProperty(dpy, root, netatom[NetClientList],
1843 XA_WINDOW, 32, PropModeAppend,
1844 (unsigned char *) &(c->win), 1);
1852 if(XineramaIsActive(dpy)) {
1856 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1857 XineramaScreenInfo *unique = NULL;
1859 for(n = 0, m = mons; m; m = m->next, n++);
1860 /* only consider unique geometries as separate screens */
1861 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
1862 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
1863 for(i = 0, j = 0; i < nn; i++)
1864 if(isuniquegeom(unique, j, &info[i]))
1865 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1869 for(i = 0; i < (nn - n); i++) { /* new monitors available */
1870 for(m = mons; m && m->next; m = m->next);
1872 m->next = createmon();
1876 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
1878 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1879 || unique[i].width != m->mw || unique[i].height != m->mh))
1883 m->mx = m->wx = unique[i].x_org;
1884 m->my = m->wy = unique[i].y_org;
1885 m->mw = m->ww = unique[i].width;
1886 m->mh = m->wh = unique[i].height;
1890 else { /* less monitors available nn < n */
1891 for(i = nn; i < n; i++) {
1892 for(m = mons; m && m->next; m = m->next);
1896 m->clients = c->next;
1910 #endif /* XINERAMA */
1911 /* default monitor setup */
1915 if(mons->mw != sw || mons->mh != sh) {
1917 mons->mw = mons->ww = sw;
1918 mons->mh = mons->wh = sh;
1924 selmon = wintomon(root);
1930 updatenumlockmask(void) {
1932 XModifierKeymap *modmap;
1935 modmap = XGetModifierMapping(dpy);
1936 for(i = 0; i < 8; i++)
1937 for(j = 0; j < modmap->max_keypermod; j++)
1938 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1939 == XKeysymToKeycode(dpy, XK_Num_Lock))
1940 numlockmask = (1 << i);
1941 XFreeModifiermap(modmap);
1945 updatesizehints(Client *c) {
1949 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
1950 /* size is uninitialized, ensure that size.flags aren't used */
1952 if(size.flags & PBaseSize) {
1953 c->basew = size.base_width;
1954 c->baseh = size.base_height;
1956 else if(size.flags & PMinSize) {
1957 c->basew = size.min_width;
1958 c->baseh = size.min_height;
1961 c->basew = c->baseh = 0;
1962 if(size.flags & PResizeInc) {
1963 c->incw = size.width_inc;
1964 c->inch = size.height_inc;
1967 c->incw = c->inch = 0;
1968 if(size.flags & PMaxSize) {
1969 c->maxw = size.max_width;
1970 c->maxh = size.max_height;
1973 c->maxw = c->maxh = 0;
1974 if(size.flags & PMinSize) {
1975 c->minw = size.min_width;
1976 c->minh = size.min_height;
1978 else if(size.flags & PBaseSize) {
1979 c->minw = size.base_width;
1980 c->minh = size.base_height;
1983 c->minw = c->minh = 0;
1984 if(size.flags & PAspect) {
1985 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
1986 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
1989 c->maxa = c->mina = 0.0;
1990 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
1991 && c->maxw == c->minw && c->maxh == c->minh);
1995 updatetitle(Client *c) {
1996 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
1997 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
1998 if(c->name[0] == '\0') /* hack to mark broken clients */
1999 strcpy(c->name, broken);
2003 updatestatus(void) {
2004 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2005 strcpy(stext, "dwm-"VERSION);
2010 updatewindowtype(Client *c) {
2011 Atom state = getatomprop(c, netatom[NetWMState]);
2012 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2014 if(state == netatom[NetWMFullscreen])
2015 setfullscreen(c, True);
2016 if(wtype == netatom[NetWMWindowTypeDialog])
2017 c->isfloating = True;
2021 updatewmhints(Client *c) {
2024 if((wmh = XGetWMHints(dpy, c->win))) {
2025 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
2026 wmh->flags &= ~XUrgencyHint;
2027 XSetWMHints(dpy, c->win, wmh);
2030 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
2031 if(wmh->flags & InputHint)
2032 c->neverfocus = !wmh->input;
2034 c->neverfocus = False;
2040 view(const Arg *arg) {
2041 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2043 selmon->seltags ^= 1; /* toggle sel tagset */
2044 if(arg->ui & TAGMASK)
2045 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2051 wintoclient(Window w) {
2055 for(m = mons; m; m = m->next)
2056 for(c = m->clients; c; c = c->next)
2063 wintomon(Window w) {
2068 if(w == root && getrootptr(&x, &y))
2069 return recttomon(x, y, 1, 1);
2070 for(m = mons; m; m = m->next)
2073 if((c = wintoclient(w)))
2078 /* There's no way to check accesses to destroyed windows, thus those cases are
2079 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2080 * default error handler, which may call exit. */
2082 xerror(Display *dpy, XErrorEvent *ee) {
2083 if(ee->error_code == BadWindow
2084 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2085 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2086 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2087 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2088 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2089 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2090 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2091 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2093 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2094 ee->request_code, ee->error_code);
2095 return xerrorxlib(dpy, ee); /* may call exit */
2099 xerrordummy(Display *dpy, XErrorEvent *ee) {
2103 /* Startup Error handler to check if another window manager
2104 * is already running. */
2106 xerrorstart(Display *dpy, XErrorEvent *ee) {
2107 die("dwm: another window manager is already running\n");
2112 zoom(const Arg *arg) {
2113 Client *c = selmon->sel;
2115 if(!selmon->lt[selmon->sellt]->arrange
2116 || (selmon->sel && selmon->sel->isfloating))
2118 if(c == nexttiled(selmon->clients))
2119 if(!c || !(c = nexttiled(c->next)))
2125 main(int argc, char *argv[]) {
2126 if(argc == 2 && !strcmp("-v", argv[1]))
2127 die("dwm-"VERSION", © 2006-2014 dwm engineers, see LICENSE for details\n");
2129 die("usage: dwm [-v]\n");
2130 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2131 fputs("warning: no locale support\n", stderr);
2132 if(!(dpy = XOpenDisplay(NULL)))
2133 die("dwm: cannot open display\n");
2140 return EXIT_SUCCESS;