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 * Calls to fetch an X event from the event queue are blocking. Due reading
10 * status text from standard input, a select()-driven main loop has been
11 * implemented which selects for reads on the X connection and STDIN_FILENO to
12 * handle all data smoothly. The event handlers of dwm are organized in an
13 * array which is accessed whenever a new event has been fetched. This allows
14 * event dispatching in O(1) time.
16 * Each child of the root window is called a client, except windows which have
17 * set the override_redirect flag. Clients are organized in a global
18 * doubly-linked client list, the focus history is remembered through a global
19 * stack list. Each client contains an array of Bools of the same size as the
20 * global tags array to indicate the tags of a client.
22 * Keys and tagging rules are organized as arrays and defined in config.h.
24 * To understand everything else, start reading main().
33 #include <sys/select.h>
34 #include <sys/types.h>
37 #include <X11/cursorfont.h>
38 #include <X11/keysym.h>
39 #include <X11/Xatom.h>
41 #include <X11/Xproto.h>
42 #include <X11/Xutil.h>
45 #define BUTTONMASK (ButtonPressMask | ButtonReleaseMask)
46 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
47 #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
50 enum { BarTop, BarBot, BarOff }; /* bar position */
51 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
52 enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
53 enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
54 enum { WMProtocols, WMDelete, WMName, WMState, WMLast };/* default atoms */
57 typedef struct Client Client;
61 int rx, ry, rw, rh; /* revert geometry */
62 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
63 int minax, maxax, minay, maxay;
65 unsigned int border, oldborder;
66 Bool isbanned, isfixed, ismax, isfloating, wasfloating;
76 unsigned long norm[ColLast];
77 unsigned long sel[ColLast];
87 } DC; /* draw context */
92 void (*func)(const char *arg);
98 void (*arrange)(void);
112 /* function declarations */
113 void applyrules(Client *c);
115 void attach(Client *c);
116 void attachstack(Client *c);
118 void buttonpress(XEvent *e);
119 void checkotherwm(void);
121 void compileregs(void);
122 void configure(Client *c);
123 void configurenotify(XEvent *e);
124 void configurerequest(XEvent *e);
125 void destroynotify(XEvent *e);
126 void detach(Client *c);
127 void detachstack(Client *c);
129 void drawsquare(Bool filled, Bool empty, unsigned long col[ColLast]);
130 void drawtext(const char *text, unsigned long col[ColLast]);
131 void *emallocz(unsigned int size);
132 void enternotify(XEvent *e);
133 void eprint(const char *errstr, ...);
134 void expose(XEvent *e);
135 void floating(void); /* default floating layout */
136 void focus(Client *c);
137 void focusnext(const char *arg);
138 void focusprev(const char *arg);
139 Client *getclient(Window w);
140 unsigned long getcolor(const char *colstr);
141 long getstate(Window w);
142 Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
143 void grabbuttons(Client *c, Bool focused);
144 unsigned int idxoftag(const char *tag);
145 void initfont(const char *fontstr);
146 Bool isoccupied(unsigned int t);
147 Bool isprotodel(Client *c);
148 Bool isvisible(Client *c);
149 void keypress(XEvent *e);
150 void killclient(const char *arg);
151 void leavenotify(XEvent *e);
152 void manage(Window w, XWindowAttributes *wa);
153 void mappingnotify(XEvent *e);
154 void maprequest(XEvent *e);
155 void movemouse(Client *c);
156 Client *nexttiled(Client *c);
157 void propertynotify(XEvent *e);
158 void quit(const char *arg);
159 void resize(Client *c, int x, int y, int w, int h, Bool sizehints);
160 void resizemouse(Client *c);
164 void setclientstate(Client *c, long state);
165 void setlayout(const char *arg);
166 void setmwfact(const char *arg);
168 void spawn(const char *arg);
169 void tag(const char *arg);
170 unsigned int textnw(const char *text, unsigned int len);
171 unsigned int textw(const char *text);
173 void togglebar(const char *arg);
174 void togglefloating(const char *arg);
175 void togglemax(const char *arg);
176 void toggletag(const char *arg);
177 void toggleview(const char *arg);
178 void unban(Client *c);
179 void unmanage(Client *c);
180 void unmapnotify(XEvent *e);
181 void updatebarpos(void);
182 void updatesizehints(Client *c);
183 void updatetitle(Client *c);
184 void view(const char *arg);
185 void viewprevtag(const char *arg); /* views previous selected tags */
186 int xerror(Display *dpy, XErrorEvent *ee);
187 int xerrordummy(Display *dsply, XErrorEvent *ee);
188 int xerrorstart(Display *dsply, XErrorEvent *ee);
189 void zoom(const char *arg);
194 int screen, sx, sy, sw, sh, wax, way, waw, wah;
195 int (*xerrorxlib)(Display *, XErrorEvent *);
196 unsigned int bh, bpos;
197 unsigned int blw = 0;
198 unsigned int numlockmask = 0;
199 void (*handler[LASTEvent]) (XEvent *) = {
200 [ButtonPress] = buttonpress,
201 [ConfigureRequest] = configurerequest,
202 [ConfigureNotify] = configurenotify,
203 [DestroyNotify] = destroynotify,
204 [EnterNotify] = enternotify,
205 [LeaveNotify] = leavenotify,
207 [KeyPress] = keypress,
208 [MappingNotify] = mappingnotify,
209 [MapRequest] = maprequest,
210 [PropertyNotify] = propertynotify,
211 [UnmapNotify] = unmapnotify
213 Atom wmatom[WMLast], netatom[NetLast];
214 Bool domwfact = True;
216 Bool otherwm, readin;
218 Bool selscreen = True;
219 Client *clients = NULL;
221 Client *stack = NULL;
222 Cursor cursor[CurLast];
225 Layout *layout = NULL;
229 /* configuration, allows nested code to access above variables */
232 /* function implementations */
234 applyrules(Client *c) {
235 static char buf[512];
238 Bool matched = False;
239 XClassHint ch = { 0 };
242 XGetClassHint(dpy, c->win, &ch);
243 snprintf(buf, sizeof buf, "%s:%s:%s",
244 ch.res_class ? ch.res_class : "",
245 ch.res_name ? ch.res_name : "", c->name);
246 for(i = 0; i < LENGTH(rules); i++)
247 if(regs[i].propregex && !regexec(regs[i].propregex, buf, 1, &tmp, 0)) {
248 c->isfloating = rules[i].isfloating;
249 for(j = 0; regs[i].tagregex && j < LENGTH(tags); j++) {
250 if(!regexec(regs[i].tagregex, tags[j], 1, &tmp, 0)) {
261 memcpy(c->tags, seltags, sizeof seltags);
268 for(c = clients; c; c = c->next)
287 attachstack(Client *c) {
296 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
301 buttonpress(XEvent *e) {
304 XButtonPressedEvent *ev = &e->xbutton;
306 if(barwin == ev->window) {
308 for(i = 0; i < LENGTH(tags); i++) {
311 if(ev->button == Button1) {
312 if(ev->state & MODKEY)
317 else if(ev->button == Button3) {
318 if(ev->state & MODKEY)
326 if((ev->x < x + blw) && ev->button == Button1)
329 else if((c = getclient(ev->window))) {
331 if(CLEANMASK(ev->state) != MODKEY)
333 if(ev->button == Button1) {
334 if((floating == layout->arrange) || c->isfloating)
337 togglefloating(NULL);
340 else if(ev->button == Button2) {
341 if((floating != layout->arrange) && c->isfloating)
342 togglefloating(NULL);
346 else if(ev->button == Button3 && !c->isfixed) {
347 if((floating == layout->arrange) || c->isfloating)
350 togglefloating(NULL);
359 XSetErrorHandler(xerrorstart);
361 /* this causes an error if some other window manager is running */
362 XSelectInput(dpy, root, SubstructureRedirectMask);
365 eprint("dwm: another window manager is already running\n");
367 XSetErrorHandler(NULL);
368 xerrorxlib = XSetErrorHandler(xerror);
380 XFreeFontSet(dpy, dc.font.set);
382 XFreeFont(dpy, dc.font.xfont);
383 XUngrabKey(dpy, AnyKey, AnyModifier, root);
384 XFreePixmap(dpy, dc.drawable);
386 XDestroyWindow(dpy, barwin);
387 XFreeCursor(dpy, cursor[CurNormal]);
388 XFreeCursor(dpy, cursor[CurResize]);
389 XFreeCursor(dpy, cursor[CurMove]);
390 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
401 regs = emallocz(LENGTH(rules) * sizeof(Regs));
402 for(i = 0; i < LENGTH(rules); i++) {
404 reg = emallocz(sizeof(regex_t));
405 if(regcomp(reg, rules[i].prop, REG_EXTENDED))
408 regs[i].propregex = reg;
411 reg = emallocz(sizeof(regex_t));
412 if(regcomp(reg, rules[i].tags, REG_EXTENDED))
415 regs[i].tagregex = reg;
421 configure(Client *c) {
424 ce.type = ConfigureNotify;
432 ce.border_width = c->border;
434 ce.override_redirect = False;
435 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
439 configurenotify(XEvent *e) {
440 XConfigureEvent *ev = &e->xconfigure;
442 if(ev->window == root && (ev->width != sw || ev->height != sh)) {
445 XFreePixmap(dpy, dc.drawable);
446 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
447 XResizeWindow(dpy, barwin, sw, bh);
454 configurerequest(XEvent *e) {
456 XConfigureRequestEvent *ev = &e->xconfigurerequest;
459 if((c = getclient(ev->window))) {
461 if(ev->value_mask & CWBorderWidth)
462 c->border = ev->border_width;
463 if(c->isfixed || c->isfloating || (floating == layout->arrange)) {
464 if(ev->value_mask & CWX)
466 if(ev->value_mask & CWY)
468 if(ev->value_mask & CWWidth)
470 if(ev->value_mask & CWHeight)
472 if((c->x + c->w) > sw && c->isfloating)
473 c->x = sw / 2 - c->w / 2; /* center in x direction */
474 if((c->y + c->h) > sh && c->isfloating)
475 c->y = sh / 2 - c->h / 2; /* center in y direction */
476 if((ev->value_mask & (CWX | CWY))
477 && !(ev->value_mask & (CWWidth | CWHeight)))
480 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
488 wc.width = ev->width;
489 wc.height = ev->height;
490 wc.border_width = ev->border_width;
491 wc.sibling = ev->above;
492 wc.stack_mode = ev->detail;
493 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
499 destroynotify(XEvent *e) {
501 XDestroyWindowEvent *ev = &e->xdestroywindow;
503 if((c = getclient(ev->window)))
510 c->prev->next = c->next;
512 c->next->prev = c->prev;
515 c->next = c->prev = NULL;
519 detachstack(Client *c) {
522 for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
531 for(i = 0; i < LENGTH(tags); i++) {
532 dc.w = textw(tags[i]);
534 drawtext(tags[i], dc.sel);
535 drawsquare(sel && sel->tags[i], isoccupied(i), dc.sel);
538 drawtext(tags[i], dc.norm);
539 drawsquare(sel && sel->tags[i], isoccupied(i), dc.norm);
544 drawtext(layout->symbol, dc.norm);
552 drawtext(stext, dc.norm);
553 if((dc.w = dc.x - x) > bh) {
556 drawtext(sel->name, dc.sel);
557 drawsquare(sel->ismax, sel->isfloating, dc.sel);
560 drawtext(NULL, dc.norm);
562 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0);
567 drawsquare(Bool filled, Bool empty, unsigned long col[ColLast]) {
570 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
572 gcv.foreground = col[ColFG];
573 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
574 x = (dc.font.ascent + dc.font.descent + 2) / 4;
578 r.width = r.height = x + 1;
579 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
582 r.width = r.height = x;
583 XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
588 drawtext(const char *text, unsigned long col[ColLast]) {
590 static char buf[256];
591 unsigned int len, olen;
592 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
594 XSetForeground(dpy, dc.gc, col[ColBG]);
595 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
599 olen = len = strlen(text);
600 if(len >= sizeof buf)
601 len = sizeof buf - 1;
602 memcpy(buf, text, len);
604 h = dc.font.ascent + dc.font.descent;
605 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
607 /* shorten text if necessary */
608 while(len && (w = textnw(buf, len)) > dc.w - h)
619 return; /* too long */
620 XSetForeground(dpy, dc.gc, col[ColFG]);
622 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
624 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
628 emallocz(unsigned int size) {
629 void *res = calloc(1, size);
632 eprint("fatal: could not malloc() %u bytes\n", size);
637 enternotify(XEvent *e) {
639 XCrossingEvent *ev = &e->xcrossing;
641 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
643 if((c = getclient(ev->window)))
645 else if(ev->window == root) {
652 eprint(const char *errstr, ...) {
655 va_start(ap, errstr);
656 vfprintf(stderr, errstr, ap);
663 XExposeEvent *ev = &e->xexpose;
666 if(barwin == ev->window)
672 floating(void) { /* default floating layout */
675 domwfact = dozoom = False;
676 for(c = clients; c; c = c->next)
678 resize(c, c->x, c->y, c->w, c->h, True);
683 if((!c && selscreen) || (c && !isvisible(c)))
684 for(c = stack; c && !isvisible(c); c = c->snext);
685 if(sel && sel != c) {
686 grabbuttons(sel, False);
687 XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
692 grabbuttons(c, True);
699 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
700 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
703 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
707 focusnext(const char *arg) {
712 for(c = sel->next; c && !isvisible(c); c = c->next);
714 for(c = clients; c && !isvisible(c); c = c->next);
722 focusprev(const char *arg) {
727 for(c = sel->prev; c && !isvisible(c); c = c->prev);
729 for(c = clients; c && c->next; c = c->next);
730 for(; c && !isvisible(c); c = c->prev);
739 getclient(Window w) {
742 for(c = clients; c && c->win != w; c = c->next);
747 getcolor(const char *colstr) {
748 Colormap cmap = DefaultColormap(dpy, screen);
751 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
752 eprint("error, cannot allocate color '%s'\n", colstr);
760 unsigned char *p = NULL;
761 unsigned long n, extra;
764 status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
765 &real, &format, &n, &extra, (unsigned char **)&p);
766 if(status != Success)
775 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
780 if(!text || 0 == size)
783 XGetTextProperty(dpy, w, &name, atom);
786 if(name.encoding == XA_STRING)
787 strncpy(text, (char *)name.value, size - 1);
789 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
792 strncpy(text, *list, size - 1);
793 XFreeStringList(list);
796 text[size - 1] = '\0';
802 grabbuttons(Client *c, Bool focused) {
803 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
806 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
807 GrabModeAsync, GrabModeSync, None, None);
808 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
809 GrabModeAsync, GrabModeSync, None, None);
810 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
811 GrabModeAsync, GrabModeSync, None, None);
812 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
813 GrabModeAsync, GrabModeSync, None, None);
815 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
816 GrabModeAsync, GrabModeSync, None, None);
817 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
818 GrabModeAsync, GrabModeSync, None, None);
819 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
820 GrabModeAsync, GrabModeSync, None, None);
821 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
822 GrabModeAsync, GrabModeSync, None, None);
824 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
825 GrabModeAsync, GrabModeSync, None, None);
826 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
827 GrabModeAsync, GrabModeSync, None, None);
828 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
829 GrabModeAsync, GrabModeSync, None, None);
830 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
831 GrabModeAsync, GrabModeSync, None, None);
834 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
835 GrabModeAsync, GrabModeSync, None, None);
839 idxoftag(const char *tag) {
842 for(i = 0; (i < LENGTH(tags)) && (tags[i] != tag); i++);
843 return (i < LENGTH(tags)) ? i : 0;
847 initfont(const char *fontstr) {
848 char *def, **missing;
853 XFreeFontSet(dpy, dc.font.set);
854 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
857 fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
858 XFreeStringList(missing);
861 XFontSetExtents *font_extents;
862 XFontStruct **xfonts;
864 dc.font.ascent = dc.font.descent = 0;
865 font_extents = XExtentsOfFontSet(dc.font.set);
866 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
867 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
868 if(dc.font.ascent < (*xfonts)->ascent)
869 dc.font.ascent = (*xfonts)->ascent;
870 if(dc.font.descent < (*xfonts)->descent)
871 dc.font.descent = (*xfonts)->descent;
877 XFreeFont(dpy, dc.font.xfont);
878 dc.font.xfont = NULL;
879 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
880 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
881 eprint("error, cannot load font: '%s'\n", fontstr);
882 dc.font.ascent = dc.font.xfont->ascent;
883 dc.font.descent = dc.font.xfont->descent;
885 dc.font.height = dc.font.ascent + dc.font.descent;
889 isoccupied(unsigned int t) {
892 for(c = clients; c; c = c->next)
899 isprotodel(Client *c) {
904 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
905 for(i = 0; !ret && i < n; i++)
906 if(protocols[i] == wmatom[WMDelete])
914 isvisible(Client *c) {
917 for(i = 0; i < LENGTH(tags); i++)
918 if(c->tags[i] && seltags[i])
924 keypress(XEvent *e) {
931 if(!e) { /* grabkeys */
932 XUngrabKey(dpy, AnyKey, AnyModifier, root);
933 for(i = 0; i < LENGTH(keys); i++) {
934 code = XKeysymToKeycode(dpy, keys[i].keysym);
935 XGrabKey(dpy, code, keys[i].mod, root, True,
936 GrabModeAsync, GrabModeAsync);
937 XGrabKey(dpy, code, keys[i].mod | LockMask, root, True,
938 GrabModeAsync, GrabModeAsync);
939 XGrabKey(dpy, code, keys[i].mod | numlockmask, root, True,
940 GrabModeAsync, GrabModeAsync);
941 XGrabKey(dpy, code, keys[i].mod | numlockmask | LockMask, root, True,
942 GrabModeAsync, GrabModeAsync);
947 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
948 for(i = 0; i < LENGTH(keys); i++)
949 if(keysym == keys[i].keysym
950 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state))
953 keys[i].func(keys[i].arg);
958 killclient(const char *arg) {
963 if(isprotodel(sel)) {
964 ev.type = ClientMessage;
965 ev.xclient.window = sel->win;
966 ev.xclient.message_type = wmatom[WMProtocols];
967 ev.xclient.format = 32;
968 ev.xclient.data.l[0] = wmatom[WMDelete];
969 ev.xclient.data.l[1] = CurrentTime;
970 XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
973 XKillClient(dpy, sel->win);
977 leavenotify(XEvent *e) {
978 XCrossingEvent *ev = &e->xcrossing;
980 if((ev->window == root) && !ev->same_screen) {
987 manage(Window w, XWindowAttributes *wa) {
988 Client *c, *t = NULL;
993 c = emallocz(sizeof(Client));
994 c->tags = emallocz(sizeof seltags);
1000 c->oldborder = wa->border_width;
1001 if(c->w == sw && c->h == sh) {
1004 c->border = wa->border_width;
1007 if(c->x + c->w + 2 * c->border > wax + waw)
1008 c->x = wax + waw - c->w - 2 * c->border;
1009 if(c->y + c->h + 2 * c->border > way + wah)
1010 c->y = way + wah - c->h - 2 * c->border;
1015 c->border = BORDERPX;
1017 wc.border_width = c->border;
1018 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1019 XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
1020 configure(c); /* propagates border_width, if size doesn't change */
1022 XSelectInput(dpy, w,
1023 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
1024 grabbuttons(c, False);
1026 if((rettrans = XGetTransientForHint(dpy, w, &trans) == Success))
1027 for(t = clients; t && t->win != trans; t = t->next);
1029 memcpy(c->tags, t->tags, sizeof seltags);
1032 c->isfloating = (rettrans == Success) || c->isfixed;
1035 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); /* some windows require this */
1037 XMapWindow(dpy, c->win);
1038 setclientstate(c, NormalState);
1043 mappingnotify(XEvent *e) {
1044 XMappingEvent *ev = &e->xmapping;
1046 XRefreshKeyboardMapping(ev);
1047 if(ev->request == MappingKeyboard)
1052 maprequest(XEvent *e) {
1053 static XWindowAttributes wa;
1054 XMapRequestEvent *ev = &e->xmaprequest;
1056 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1058 if(wa.override_redirect)
1060 if(!getclient(ev->window))
1061 manage(ev->window, &wa);
1065 movemouse(Client *c) {
1066 int x1, y1, ocx, ocy, di, nx, ny;
1073 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1074 None, cursor[CurMove], CurrentTime) != GrabSuccess)
1077 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
1079 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
1082 XUngrabPointer(dpy, CurrentTime);
1084 case ConfigureRequest:
1087 handler[ev.type](&ev);
1091 nx = ocx + (ev.xmotion.x - x1);
1092 ny = ocy + (ev.xmotion.y - y1);
1093 if(abs(wax + nx) < SNAP)
1095 else if(abs((wax + waw) - (nx + c->w + 2 * c->border)) < SNAP)
1096 nx = wax + waw - c->w - 2 * c->border;
1097 if(abs(way - ny) < SNAP)
1099 else if(abs((way + wah) - (ny + c->h + 2 * c->border)) < SNAP)
1100 ny = way + wah - c->h - 2 * c->border;
1101 resize(c, nx, ny, c->w, c->h, False);
1108 nexttiled(Client *c) {
1109 for(; c && (c->isfloating || !isvisible(c)); c = c->next);
1114 propertynotify(XEvent *e) {
1117 XPropertyEvent *ev = &e->xproperty;
1119 if(ev->state == PropertyDelete)
1120 return; /* ignore */
1121 if((c = getclient(ev->window))) {
1124 case XA_WM_TRANSIENT_FOR:
1125 XGetTransientForHint(dpy, c->win, &trans);
1126 if(!c->isfloating && (c->isfloating = (NULL != getclient(trans))))
1129 case XA_WM_NORMAL_HINTS:
1133 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1142 quit(const char *arg) {
1143 readin = running = False;
1148 resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
1152 /* set minimum possible */
1158 /* temporarily remove base dimensions */
1162 /* adjust for aspect limits */
1163 if (c->minay > 0 && c->maxay > 0 && c->minax > 0 && c->maxax > 0) {
1164 if (w * c->maxay > h * c->maxax)
1165 w = h * c->maxax / c->maxay;
1166 else if (w * c->minay < h * c->minax)
1167 h = w * c->minay / c->minax;
1170 /* adjust for increment value */
1176 /* restore base dimensions */
1180 if(c->minw > 0 && w < c->minw)
1182 if(c->minh > 0 && h < c->minh)
1184 if(c->maxw > 0 && w > c->maxw)
1186 if(c->maxh > 0 && h > c->maxh)
1189 if(w <= 0 || h <= 0)
1191 /* offscreen appearance fixes */
1193 x = sw - w - 2 * c->border;
1195 y = sh - h - 2 * c->border;
1196 if(x + w + 2 * c->border < sx)
1198 if(y + h + 2 * c->border < sy)
1200 if(c->x != x || c->y != y || c->w != w || c->h != h) {
1203 c->w = wc.width = w;
1204 c->h = wc.height = h;
1205 wc.border_width = c->border;
1206 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
1213 resizemouse(Client *c) {
1220 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1221 None, cursor[CurResize], CurrentTime) != GrabSuccess)
1224 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
1226 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
1229 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
1230 c->w + c->border - 1, c->h + c->border - 1);
1231 XUngrabPointer(dpy, CurrentTime);
1232 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1234 case ConfigureRequest:
1237 handler[ev.type](&ev);
1241 if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
1243 if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
1245 resize(c, c->x, c->y, nw, nh, True);
1260 if(sel->isfloating || (floating == layout->arrange))
1261 XRaiseWindow(dpy, sel->win);
1262 if(floating != layout->arrange) {
1263 wc.stack_mode = Below;
1264 wc.sibling = barwin;
1265 if(!sel->isfloating) {
1266 XConfigureWindow(dpy, sel->win, CWSibling | CWStackMode, &wc);
1267 wc.sibling = sel->win;
1269 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
1272 XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc);
1273 wc.sibling = c->win;
1277 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1285 unsigned int len, offset;
1288 /* main event loop, also reads status text from stdin */
1290 xfd = ConnectionNumber(dpy);
1293 len = sizeof stext - 1;
1294 stext[len] = '\0'; /* 0-terminator is never touched */
1298 FD_SET(STDIN_FILENO, &rd);
1300 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
1303 eprint("select failed\n");
1305 if(FD_ISSET(STDIN_FILENO, &rd)) {
1306 switch((r = read(STDIN_FILENO, stext + offset, len - offset))) {
1308 strncpy(stext, strerror(errno), len);
1312 strncpy(stext, "EOF", 4);
1316 stext[offset + r] = '\0';
1317 for(p = stext; *p && *p != '\n'; p++);
1323 offset = (offset + r < len - 1) ? offset + r : 0;
1327 while(XPending(dpy)) {
1328 XNextEvent(dpy, &ev);
1329 if(handler[ev.type])
1330 (handler[ev.type])(&ev); /* call handler */
1337 unsigned int i, num;
1338 Window *wins, d1, d2;
1339 XWindowAttributes wa;
1342 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1343 for(i = 0; i < num; i++) {
1344 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1345 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1347 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1348 manage(wins[i], &wa);
1350 for(i = 0; i < num; i++) { /* now the transients */
1351 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1353 if(XGetTransientForHint(dpy, wins[i], &d1)
1354 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1355 manage(wins[i], &wa);
1363 setclientstate(Client *c, long state) {
1364 long data[] = {state, None};
1366 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1367 PropModeReplace, (unsigned char *)data, 2);
1371 setlayout(const char *arg) {
1375 if(++layout == &layouts[LENGTH(layouts)])
1376 layout = &layouts[0];
1379 for(i = 0; i < LENGTH(layouts); i++)
1380 if(!strcmp(arg, layouts[i].symbol))
1382 if(i == LENGTH(layouts))
1384 layout = &layouts[i];
1393 setmwfact(const char *arg) {
1398 /* arg handling, manipulate mwfact */
1401 else if(1 == sscanf(arg, "%lf", &delta)) {
1402 if(arg[0] == '+' || arg[0] == '-')
1408 else if(mwfact > 0.9)
1417 unsigned int i, j, mask;
1419 XModifierKeymap *modmap;
1420 XSetWindowAttributes wa;
1423 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1424 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1425 wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
1426 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1427 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1428 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1429 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1430 PropModeReplace, (unsigned char *) netatom, NetLast);
1433 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1434 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1435 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1439 sw = DisplayWidth(dpy, screen);
1440 sh = DisplayHeight(dpy, screen);
1442 /* init modifier map */
1443 modmap = XGetModifierMapping(dpy);
1444 for(i = 0; i < 8; i++)
1445 for(j = 0; j < modmap->max_keypermod; j++) {
1446 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1447 == XKeysymToKeycode(dpy, XK_Num_Lock))
1448 numlockmask = (1 << i);
1450 XFreeModifiermap(modmap);
1452 /* select for events */
1453 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
1454 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
1455 wa.cursor = cursor[CurNormal];
1456 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
1457 XSelectInput(dpy, root, wa.event_mask);
1465 /* init appearance */
1466 dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
1467 dc.norm[ColBG] = getcolor(NORMBGCOLOR);
1468 dc.norm[ColFG] = getcolor(NORMFGCOLOR);
1469 dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
1470 dc.sel[ColBG] = getcolor(SELBGCOLOR);
1471 dc.sel[ColFG] = getcolor(SELFGCOLOR);
1473 dc.h = bh = dc.font.height + 2;
1477 layout = &layouts[0];
1478 for(blw = i = 0; i < LENGTH(layouts); i++) {
1479 j = textw(layouts[i].symbol);
1486 wa.override_redirect = 1;
1487 wa.background_pixmap = ParentRelative;
1488 wa.event_mask = ButtonPressMask | ExposureMask;
1489 barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0,
1490 DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
1491 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
1492 XDefineCursor(dpy, barwin, cursor[CurNormal]);
1494 XMapRaised(dpy, barwin);
1495 strcpy(stext, "dwm-"VERSION);
1496 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
1497 dc.gc = XCreateGC(dpy, root, 0, 0);
1498 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1500 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
1502 /* multihead support */
1503 selscreen = XQueryPointer(dpy, root, &w, &w, &d, &d, &d, &d, &mask);
1507 spawn(const char *arg) {
1508 static char *shell = NULL;
1510 if(!shell && !(shell = getenv("SHELL")))
1514 /* The double-fork construct avoids zombie processes and keeps the code
1515 * clean from stupid signal handlers. */
1519 close(ConnectionNumber(dpy));
1521 execl(shell, shell, "-c", arg, (char *)NULL);
1522 fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
1531 tag(const char *arg) {
1536 for(i = 0; i < LENGTH(tags); i++)
1537 sel->tags[i] = (NULL == arg);
1538 sel->tags[idxoftag(arg)] = True;
1543 textnw(const char *text, unsigned int len) {
1547 XmbTextExtents(dc.font.set, text, len, NULL, &r);
1550 return XTextWidth(dc.font.xfont, text, len);
1554 textw(const char *text) {
1555 return textnw(text, strlen(text)) + dc.font.height;
1560 unsigned int i, n, nx, ny, nw, nh, mw, th;
1563 domwfact = dozoom = True;
1564 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
1568 mw = (n == 1) ? waw : mwfact * waw;
1569 th = (n > 1) ? wah / (n - 1) : 0;
1570 if(n > 1 && th < bh)
1575 nw = 0; /* gcc stupidity requires this */
1576 for(i = 0, c = mc = nexttiled(clients); c; c = nexttiled(c->next), i++) {
1578 if(0 == i) { /* master */
1579 nw = mw - 2 * c->border;
1580 nh = wah - 2 * c->border;
1582 else { /* tile window */
1585 nx += mc->w + 2 * mc->border;
1586 nw = waw - nx - 2 * c->border;
1588 if(i + 1 == n) /* remainder */
1589 nh = (way + wah) - ny - 2 * c->border;
1591 nh = th - 2 * c->border;
1593 resize(c, nx, ny, nw, nh, RESIZEHINTS);
1594 if((RESIZEHINTS) && ((c->h < bh) || (c->h > nh) || (c->w < bh) || (c->w > nw)))
1595 /* client doesn't accept size constraints */
1596 resize(c, nx, ny, nw, nh, False);
1597 if(n > 1 && th != wah)
1598 ny = c->y + c->h + 2 * c->border;
1603 togglebar(const char *arg) {
1605 bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
1613 togglefloating(const char *arg) {
1616 sel->isfloating = !sel->isfloating;
1618 resize(sel, sel->x, sel->y, sel->w, sel->h, True);
1623 togglemax(const char *arg) {
1626 if(!sel || sel->isfixed)
1628 if((sel->ismax = !sel->ismax)) {
1629 if((floating == layout->arrange) || sel->isfloating)
1630 sel->wasfloating = True;
1632 togglefloating(NULL);
1633 sel->wasfloating = False;
1639 resize(sel, wax, way, waw - 2 * sel->border, wah - 2 * sel->border, True);
1642 resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True);
1643 if(!sel->wasfloating)
1644 togglefloating(NULL);
1647 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1651 toggletag(const char *arg) {
1657 sel->tags[i] = !sel->tags[i];
1658 for(j = 0; j < LENGTH(tags) && !sel->tags[j]; j++);
1659 if(j == LENGTH(tags))
1660 sel->tags[i] = True; /* at least one tag must be enabled */
1665 toggleview(const char *arg) {
1669 seltags[i] = !seltags[i];
1670 for(j = 0; j < LENGTH(tags) && !seltags[j]; j++);
1671 if(j == LENGTH(tags))
1672 seltags[i] = True; /* at least one tag must be viewed */
1680 XMoveWindow(dpy, c->win, c->x, c->y);
1681 c->isbanned = False;
1685 unmanage(Client *c) {
1688 wc.border_width = c->oldborder;
1689 /* The server grab construct avoids race conditions. */
1691 XSetErrorHandler(xerrordummy);
1692 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1697 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1698 setclientstate(c, WithdrawnState);
1702 XSetErrorHandler(xerror);
1708 unmapnotify(XEvent *e) {
1710 XUnmapEvent *ev = &e->xunmap;
1712 if((c = getclient(ev->window)))
1717 updatebarpos(void) {
1728 XMoveWindow(dpy, barwin, sx, sy);
1732 XMoveWindow(dpy, barwin, sx, sy + wah);
1735 XMoveWindow(dpy, barwin, sx, sy - bh);
1739 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1743 updatesizehints(Client *c) {
1747 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
1749 c->flags = size.flags;
1750 if(c->flags & PBaseSize) {
1751 c->basew = size.base_width;
1752 c->baseh = size.base_height;
1754 else if(c->flags & PMinSize) {
1755 c->basew = size.min_width;
1756 c->baseh = size.min_height;
1759 c->basew = c->baseh = 0;
1760 if(c->flags & PResizeInc) {
1761 c->incw = size.width_inc;
1762 c->inch = size.height_inc;
1765 c->incw = c->inch = 0;
1766 if(c->flags & PMaxSize) {
1767 c->maxw = size.max_width;
1768 c->maxh = size.max_height;
1771 c->maxw = c->maxh = 0;
1772 if(c->flags & PMinSize) {
1773 c->minw = size.min_width;
1774 c->minh = size.min_height;
1776 else if(c->flags & PBaseSize) {
1777 c->minw = size.base_width;
1778 c->minh = size.base_height;
1781 c->minw = c->minh = 0;
1782 if(c->flags & PAspect) {
1783 c->minax = size.min_aspect.x;
1784 c->maxax = size.max_aspect.x;
1785 c->minay = size.min_aspect.y;
1786 c->maxay = size.max_aspect.y;
1789 c->minax = c->maxax = c->minay = c->maxay = 0;
1790 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
1791 && c->maxw == c->minw && c->maxh == c->minh);
1795 updatetitle(Client *c) {
1796 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
1797 gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
1800 /* There's no way to check accesses to destroyed windows, thus those cases are
1801 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1802 * default error handler, which may call exit. */
1804 xerror(Display *dpy, XErrorEvent *ee) {
1805 if(ee->error_code == BadWindow
1806 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
1807 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
1808 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
1809 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
1810 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
1811 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
1812 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
1814 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
1815 ee->request_code, ee->error_code);
1816 return xerrorxlib(dpy, ee); /* may call exit */
1820 xerrordummy(Display *dsply, XErrorEvent *ee) {
1824 /* Startup Error handler to check if another window manager
1825 * is already running. */
1827 xerrorstart(Display *dsply, XErrorEvent *ee) {
1833 view(const char *arg) {
1836 memcpy(prevtags, seltags, sizeof seltags);
1837 for(i = 0; i < LENGTH(tags); i++)
1838 seltags[i] = (NULL == arg);
1839 seltags[idxoftag(arg)] = True;
1844 viewprevtag(const char *arg) {
1845 static Bool tmptags[sizeof tags / sizeof tags[0]];
1847 memcpy(tmptags, seltags, sizeof seltags);
1848 memcpy(seltags, prevtags, sizeof seltags);
1849 memcpy(prevtags, tmptags, sizeof seltags);
1854 zoom(const char *arg) {
1857 if(!sel || !dozoom || sel->isfloating)
1859 if((c = sel) == nexttiled(clients))
1860 if(!(c = nexttiled(c->next)))
1869 main(int argc, char *argv[]) {
1870 if(argc == 2 && !strcmp("-v", argv[1]))
1871 eprint("dwm-"VERSION", © 2006-2007 Anselm R. Garbe, Sander van Dijk, "
1872 "Jukka Salmi, Premysl Hruby, Szabolcs Nagy\n");
1874 eprint("usage: dwm [-v]\n");
1876 setlocale(LC_CTYPE, "");
1877 if(!(dpy = XOpenDisplay(0)))
1878 eprint("dwm: cannot open display\n");
1879 screen = DefaultScreen(dpy);
1880 root = RootWindow(dpy, screen);