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>
44 #include <X11/extensions/Xinerama.h>
48 #define BUTTONMASK (ButtonPressMask | ButtonReleaseMask)
49 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
50 #define LENGTH(x) (sizeof x / sizeof x[0])
52 #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
56 enum { BarTop, BarBot, BarOff }; /* bar position */
57 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
58 enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
59 enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
60 enum { WMProtocols, WMDelete, WMName, WMState, WMLast };/* default atoms */
63 typedef struct Monitor Monitor;
64 typedef struct Client Client;
68 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
69 int minax, maxax, minay, maxay;
71 unsigned int border, oldborder;
72 Bool isbanned, isfixed, isfloating, isurgent;
83 unsigned long norm[ColLast];
84 unsigned long sel[ColLast];
94 } DC; /* draw context */
99 void (*func)(const char *arg);
105 void (*arrange)(Monitor *);
121 int sx, sy, sw, sh, wax, way, wah, waw;
130 /* function declarations */
131 void applyrules(Client *c);
132 void arrange(Monitor *m);
133 void attach(Client *c);
134 void attachstack(Client *c);
136 void buttonpress(XEvent *e);
137 void checkotherwm(void);
139 void compileregs(void);
140 void configure(Client *c);
141 void configurenotify(XEvent *e);
142 void configurerequest(XEvent *e);
143 void destroynotify(XEvent *e);
144 void detach(Client *c);
145 void detachstack(Client *c);
146 void drawbar(Monitor *m);
147 void drawsquare(Monitor *m, Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
148 void drawtext(Monitor *m, const char *text, unsigned long col[ColLast], Bool invert);
149 void *emallocz(unsigned int size);
150 void enternotify(XEvent *e);
151 void eprint(const char *errstr, ...);
152 void expose(XEvent *e);
153 void floating(Monitor *m); /* default floating layout */
154 void focus(Client *c);
155 void focusin(XEvent *e);
156 void focusnext(const char *arg);
157 void focusprev(const char *arg);
158 Client *getclient(Window w);
159 unsigned long getcolor(const char *colstr);
160 Monitor *getmonitor(Window barwin);
161 long getstate(Window w);
162 Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
163 void grabbuttons(Client *c, Bool focused);
165 unsigned int idxoftag(const char *tag);
166 void initfont(const char *fontstr);
167 Bool isoccupied(Monitor *monitor, unsigned int t);
168 Bool isprotodel(Client *c);
169 Bool isurgent(Monitor *monitor, unsigned int t);
170 Bool isvisible(Client *c, Monitor *m);
171 void keypress(XEvent *e);
172 void killclient(const char *arg);
173 void manage(Window w, XWindowAttributes *wa);
174 void mappingnotify(XEvent *e);
175 void maprequest(XEvent *e);
176 Monitor *monitorat(void);
177 void movemouse(Client *c);
178 Client *nexttiled(Client *c, Monitor *monitor);
179 void propertynotify(XEvent *e);
180 void quit(const char *arg);
181 void reapply(const char *arg);
182 void resize(Client *c, int x, int y, int w, int h, Bool sizehints);
183 void resizemouse(Client *c);
184 void restack(Monitor *m);
187 void setclientstate(Client *c, long state);
188 void setlayout(const char *arg);
189 void setmwfact(const char *arg);
191 void spawn(const char *arg);
192 void tag(const char *arg);
193 unsigned int textnw(const char *text, unsigned int len);
194 unsigned int textw(const char *text);
195 void tile(Monitor *m);
196 void togglebar(const char *arg);
197 void togglefloating(const char *arg);
198 void toggletag(const char *arg);
199 void toggleview(const char *arg);
200 void unban(Client *c);
201 void unmanage(Client *c);
202 void unmapnotify(XEvent *e);
203 void updatebarpos(Monitor *m);
204 void updatesizehints(Client *c);
205 void updatetitle(Client *c);
206 void updatewmhints(Client *c);
207 void view(const char *arg);
208 void viewprevtag(const char *arg); /* views previous selected tags */
209 int xerror(Display *dpy, XErrorEvent *ee);
210 int xerrordummy(Display *dsply, XErrorEvent *ee);
211 int xerrorstart(Display *dsply, XErrorEvent *ee);
212 void zoom(const char *arg);
213 void movetomonitor(const char *arg);
214 void selectmonitor(const char *arg);
221 int (*xerrorxlib)(Display *, XErrorEvent *);
222 unsigned int bh, bpos;
223 unsigned int blw = 0;
224 unsigned int numlockmask = 0;
225 void (*handler[LASTEvent]) (XEvent *) = {
226 [ButtonPress] = buttonpress,
227 [ConfigureRequest] = configurerequest,
228 [ConfigureNotify] = configurenotify,
229 [DestroyNotify] = destroynotify,
230 [EnterNotify] = enternotify,
233 [KeyPress] = keypress,
234 [MappingNotify] = mappingnotify,
235 [MapRequest] = maprequest,
236 [PropertyNotify] = propertynotify,
237 [UnmapNotify] = unmapnotify
239 Atom wmatom[WMLast], netatom[NetLast];
240 Bool isxinerama = False;
241 Bool domwfact = True;
243 Bool otherwm, readin;
245 Client *clients = NULL;
247 Client *stack = NULL;
248 Cursor cursor[CurLast];
255 /* configuration, allows nested code to access above variables */
258 //Bool prevtags[LENGTH(tags)];
260 /* function implementations */
262 applyrules(Client *c) {
263 static char buf[512];
266 Bool matched_tag = False;
267 Bool matched_monitor = False;
268 XClassHint ch = { 0 };
271 XGetClassHint(dpy, c->win, &ch);
272 snprintf(buf, sizeof buf, "%s:%s:%s",
273 ch.res_class ? ch.res_class : "",
274 ch.res_name ? ch.res_name : "", c->name);
275 for(i = 0; i < LENGTH(rules); i++)
276 if(regs[i].propregex && !regexec(regs[i].propregex, buf, 1, &tmp, 0)) {
277 if (rules[i].monitor >= 0 && rules[i].monitor < mcount) {
278 matched_monitor = True;
279 c->monitor = &monitors[rules[i].monitor];
282 c->isfloating = rules[i].isfloating;
283 for(j = 0; regs[i].tagregex && j < LENGTH(tags); j++) {
284 if(!regexec(regs[i].tagregex, tags[j], 1, &tmp, 0)) {
295 memcpy(c->tags, monitorat()->seltags, sizeof initags);
296 if (!matched_monitor)
297 c->monitor = monitorat();
301 arrange(Monitor *m) {
305 for(c = clients; c; c = c->next)
306 if(isvisible(c, c->monitor))
312 m->layout->arrange(m);
314 for(i = 0; i < mcount; i++)
315 monitors[i].layout->arrange(&monitors[i]);
329 attachstack(Client *c) {
338 XMoveWindow(dpy, c->win, c->x + 3 * c->monitor->sw, c->y);
343 buttonpress(XEvent *e) {
346 XButtonPressedEvent *ev = &e->xbutton;
348 Monitor *m = monitorat();
350 if(ev->window == m->barwin) {
352 for(i = 0; i < LENGTH(tags); i++) {
355 if(ev->button == Button1) {
356 if(ev->state & MODKEY)
361 else if(ev->button == Button3) {
362 if(ev->state & MODKEY)
370 if((ev->x < x + blw) && ev->button == Button1)
373 else if((c = getclient(ev->window))) {
375 if(CLEANMASK(ev->state) != MODKEY)
377 if(ev->button == Button1) {
381 else if(ev->button == Button2) {
382 if((floating != m->layout->arrange) && c->isfloating)
383 togglefloating(NULL);
387 else if(ev->button == Button3 && !c->isfixed) {
397 XSetErrorHandler(xerrorstart);
399 /* this causes an error if some other window manager is running */
400 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
403 eprint("dwm: another window manager is already running\n");
405 XSetErrorHandler(NULL);
406 xerrorxlib = XSetErrorHandler(xerror);
419 XFreeFontSet(dpy, dc.font.set);
421 XFreeFont(dpy, dc.font.xfont);
423 XUngrabKey(dpy, AnyKey, AnyModifier, root);
424 XFreePixmap(dpy, dc.drawable);
426 XFreeCursor(dpy, cursor[CurNormal]);
427 XFreeCursor(dpy, cursor[CurResize]);
428 XFreeCursor(dpy, cursor[CurMove]);
429 for(i = 0; i < mcount; i++)
430 XDestroyWindow(dpy, monitors[i].barwin);
432 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
442 regs = emallocz(LENGTH(rules) * sizeof(Regs));
443 for(i = 0; i < LENGTH(rules); i++) {
445 reg = emallocz(sizeof(regex_t));
446 if(regcomp(reg, rules[i].prop, REG_EXTENDED))
449 regs[i].propregex = reg;
452 reg = emallocz(sizeof(regex_t));
453 if(regcomp(reg, rules[i].tags, REG_EXTENDED))
456 regs[i].tagregex = reg;
462 configure(Client *c) {
465 ce.type = ConfigureNotify;
473 ce.border_width = c->border;
475 ce.override_redirect = False;
476 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
480 configurenotify(XEvent *e) {
481 XConfigureEvent *ev = &e->xconfigure;
482 Monitor *m = selmonitor;
484 if(ev->window == root && (ev->width != m->sw || ev->height != m->sh)) {
485 /* TODO -- update Xinerama dimensions here */
488 XFreePixmap(dpy, dc.drawable);
489 dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(root, screen), bh, DefaultDepth(dpy, screen));
490 XResizeWindow(dpy, m->barwin, m->sw, bh);
497 configurerequest(XEvent *e) {
499 XConfigureRequestEvent *ev = &e->xconfigurerequest;
502 if((c = getclient(ev->window))) {
503 Monitor *m = c->monitor;
504 if(ev->value_mask & CWBorderWidth)
505 c->border = ev->border_width;
506 if(c->isfixed || c->isfloating || (floating == m->layout->arrange)) {
507 if(ev->value_mask & CWX)
509 if(ev->value_mask & CWY)
511 if(ev->value_mask & CWWidth)
513 if(ev->value_mask & CWHeight)
515 if((c->x - m->sx + c->w) > m->sw && c->isfloating)
516 c->x = m->sx + (m->sw / 2 - c->w / 2); /* center in x direction */
517 if((c->y - m->sy + c->h) > m->sh && c->isfloating)
518 c->y = m->sy + (m->sh / 2 - c->h / 2); /* center in y direction */
519 if((ev->value_mask & (CWX | CWY))
520 && !(ev->value_mask & (CWWidth | CWHeight)))
522 if(isvisible(c, monitorat()))
523 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
531 wc.width = ev->width;
532 wc.height = ev->height;
533 wc.border_width = ev->border_width;
534 wc.sibling = ev->above;
535 wc.stack_mode = ev->detail;
536 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
542 destroynotify(XEvent *e) {
544 XDestroyWindowEvent *ev = &e->xdestroywindow;
546 if((c = getclient(ev->window)))
553 c->prev->next = c->next;
555 c->next->prev = c->prev;
558 c->next = c->prev = NULL;
562 detachstack(Client *c) {
565 for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
570 drawbar(Monitor *m) {
575 for(c = stack; c && !isvisible(c, m); c = c->snext);
576 for(j = 0; j < LENGTH(tags); j++) {
577 dc.w = textw(tags[j]);
579 drawtext(m, tags[j], dc.sel, isurgent(m, j));
580 drawsquare(m, c && c->tags[j] && c->monitor == m,
581 isoccupied(m, j), isurgent(m, j), dc.sel);
584 drawtext(m, tags[j], dc.norm, isurgent(m, j));
585 drawsquare(m, c && c->tags[j] && c->monitor == m,
586 isoccupied(m, j), isurgent(m, j), dc.norm);
591 drawtext(m, m->layout->symbol, dc.norm, False);
593 if(m == selmonitor) {
600 drawtext(m, stext, dc.norm, False);
604 if((dc.w = dc.x - x) > bh) {
607 drawtext(m, c->name, dc.sel, False);
608 drawsquare(m, False, c->isfloating, False, dc.sel);
611 drawtext(m, NULL, dc.norm, False);
613 XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->sw, bh, 0, 0);
618 drawsquare(Monitor *m, Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
621 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
623 gcv.foreground = col[invert ? ColBG : ColFG];
624 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
625 x = (dc.font.ascent + dc.font.descent + 2) / 4;
629 r.width = r.height = x + 1;
630 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
633 r.width = r.height = x;
634 XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
639 drawtext(Monitor *m, const char *text, unsigned long col[ColLast], Bool invert) {
641 static char buf[256];
642 unsigned int len, olen;
643 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
645 XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
646 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
650 olen = len = strlen(text);
651 if(len >= sizeof buf)
652 len = sizeof buf - 1;
653 memcpy(buf, text, len);
655 h = dc.font.ascent + dc.font.descent;
656 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
658 /* shorten text if necessary */
659 while(len && (w = textnw(buf, len)) > dc.w - h)
670 return; /* too long */
671 XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
673 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
675 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
679 emallocz(unsigned int size) {
680 void *res = calloc(1, size);
683 eprint("fatal: could not malloc() %u bytes\n", size);
688 enternotify(XEvent *e) {
690 XCrossingEvent *ev = &e->xcrossing;
692 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior) {
693 if(!isxinerama || ev->window != root)
696 if((c = getclient(ev->window)))
699 selmonitor = monitorat();
700 fprintf(stderr, "updating selmonitor %d\n", selmonitor - monitors);
706 eprint(const char *errstr, ...) {
709 va_start(ap, errstr);
710 vfprintf(stderr, errstr, ap);
718 XExposeEvent *ev = &e->xexpose;
720 if(ev->count == 0 && (m = getmonitor(ev->window)))
725 floating(Monitor *m) { /* default floating layout */
728 domwfact = dozoom = False;
729 for(c = clients; c; c = c->next)
731 resize(c, c->x, c->y, c->w, c->h, True);
737 selmonitor = c->monitor;
738 if(!c || (c && !isvisible(c, selmonitor)))
739 for(c = stack; c && !isvisible(c, c->monitor); c = c->snext);
740 if(sel && sel != c) {
741 grabbuttons(sel, False);
742 XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
747 grabbuttons(c, True);
751 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
752 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
753 selmonitor = c->monitor;
756 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
761 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
762 XFocusChangeEvent *ev = &e->xfocus;
764 if(sel && ev->window != sel->win)
765 XSetInputFocus(dpy, sel->win, RevertToPointerRoot, CurrentTime);
769 focusnext(const char *arg) {
774 for(c = sel->next; c && !isvisible(c, selmonitor); c = c->next);
776 for(c = clients; c && !isvisible(c, selmonitor); c = c->next);
784 focusprev(const char *arg) {
789 for(c = sel->prev; c && !isvisible(c, selmonitor); c = c->prev);
791 for(c = clients; c && c->next; c = c->next);
792 for(; c && !isvisible(c, selmonitor); c = c->prev);
801 getclient(Window w) {
804 for(c = clients; c && c->win != w; c = c->next);
809 getcolor(const char *colstr) {
810 Colormap cmap = DefaultColormap(dpy, screen);
813 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
814 eprint("error, cannot allocate color '%s'\n", colstr);
819 getmonitor(Window barwin) {
822 for(i = 0; i < mcount; i++)
823 if(monitors[i].barwin == barwin)
832 unsigned char *p = NULL;
833 unsigned long n, extra;
836 status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
837 &real, &format, &n, &extra, (unsigned char **)&p);
838 if(status != Success)
847 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
852 if(!text || size == 0)
855 XGetTextProperty(dpy, w, &name, atom);
858 if(name.encoding == XA_STRING)
859 strncpy(text, (char *)name.value, size - 1);
861 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
863 strncpy(text, *list, size - 1);
864 XFreeStringList(list);
867 text[size - 1] = '\0';
873 grabbuttons(Client *c, Bool focused) {
874 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
877 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
878 GrabModeAsync, GrabModeSync, None, None);
879 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
880 GrabModeAsync, GrabModeSync, None, None);
881 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
882 GrabModeAsync, GrabModeSync, None, None);
883 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
884 GrabModeAsync, GrabModeSync, None, None);
886 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
887 GrabModeAsync, GrabModeSync, None, None);
888 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
889 GrabModeAsync, GrabModeSync, None, None);
890 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
891 GrabModeAsync, GrabModeSync, None, None);
892 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
893 GrabModeAsync, GrabModeSync, None, None);
895 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
896 GrabModeAsync, GrabModeSync, None, None);
897 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
898 GrabModeAsync, GrabModeSync, None, None);
899 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
900 GrabModeAsync, GrabModeSync, None, None);
901 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
902 GrabModeAsync, GrabModeSync, None, None);
905 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
906 GrabModeAsync, GrabModeSync, None, None);
913 XModifierKeymap *modmap;
915 /* init modifier map */
916 modmap = XGetModifierMapping(dpy);
917 for(i = 0; i < 8; i++)
918 for(j = 0; j < modmap->max_keypermod; j++) {
919 if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
920 numlockmask = (1 << i);
922 XFreeModifiermap(modmap);
924 XUngrabKey(dpy, AnyKey, AnyModifier, root);
925 for(i = 0; i < LENGTH(keys); i++) {
926 code = XKeysymToKeycode(dpy, keys[i].keysym);
927 XGrabKey(dpy, code, keys[i].mod, root, True,
928 GrabModeAsync, GrabModeAsync);
929 XGrabKey(dpy, code, keys[i].mod | LockMask, root, True,
930 GrabModeAsync, GrabModeAsync);
931 XGrabKey(dpy, code, keys[i].mod | numlockmask, root, True,
932 GrabModeAsync, GrabModeAsync);
933 XGrabKey(dpy, code, keys[i].mod | numlockmask | LockMask, root, True,
934 GrabModeAsync, GrabModeAsync);
939 idxoftag(const char *tag) {
942 for(i = 0; (i < LENGTH(tags)) && (tags[i] != tag); i++);
943 return (i < LENGTH(tags)) ? i : 0;
947 initfont(const char *fontstr) {
948 char *def, **missing;
953 XFreeFontSet(dpy, dc.font.set);
954 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
957 fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
958 XFreeStringList(missing);
961 XFontSetExtents *font_extents;
962 XFontStruct **xfonts;
964 dc.font.ascent = dc.font.descent = 0;
965 font_extents = XExtentsOfFontSet(dc.font.set);
966 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
967 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
968 if(dc.font.ascent < (*xfonts)->ascent)
969 dc.font.ascent = (*xfonts)->ascent;
970 if(dc.font.descent < (*xfonts)->descent)
971 dc.font.descent = (*xfonts)->descent;
977 XFreeFont(dpy, dc.font.xfont);
978 dc.font.xfont = NULL;
979 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
980 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
981 eprint("error, cannot load font: '%s'\n", fontstr);
982 dc.font.ascent = dc.font.xfont->ascent;
983 dc.font.descent = dc.font.xfont->descent;
985 dc.font.height = dc.font.ascent + dc.font.descent;
989 isoccupied(Monitor *monitor, unsigned int t) {
992 for(c = clients; c; c = c->next)
993 if(c->tags[t] && c->monitor == monitor)
999 isprotodel(Client *c) {
1004 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1005 for(i = 0; !ret && i < n; i++)
1006 if(protocols[i] == wmatom[WMDelete])
1014 isurgent(Monitor *monitor, unsigned int t) {
1017 for(c = clients; c; c = c->next)
1018 if(c->monitor == monitor && c->isurgent && c->tags[t])
1024 isvisible(Client *c, Monitor *m) {
1029 for(i = 0; i < LENGTH(tags); i++)
1030 if(c->tags[i] && c->monitor->seltags[i])
1036 keypress(XEvent *e) {
1042 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1043 for(i = 0; i < LENGTH(keys); i++)
1044 if(keysym == keys[i].keysym
1045 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state))
1048 keys[i].func(keys[i].arg);
1053 killclient(const char *arg) {
1058 if(isprotodel(sel)) {
1059 ev.type = ClientMessage;
1060 ev.xclient.window = sel->win;
1061 ev.xclient.message_type = wmatom[WMProtocols];
1062 ev.xclient.format = 32;
1063 ev.xclient.data.l[0] = wmatom[WMDelete];
1064 ev.xclient.data.l[1] = CurrentTime;
1065 XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
1068 XKillClient(dpy, sel->win);
1072 manage(Window w, XWindowAttributes *wa) {
1073 Client *c, *t = NULL;
1079 c = emallocz(sizeof(Client));
1080 c->tags = emallocz(sizeof initags);
1087 c->x = wa->x + m->sx;
1088 c->y = wa->y + m->sy;
1091 c->oldborder = wa->border_width;
1093 if(c->w == m->sw && c->h == m->sh) {
1096 c->border = wa->border_width;
1099 if(c->x + c->w + 2 * c->border > m->wax + m->waw)
1100 c->x = m->wax + m->waw - c->w - 2 * c->border;
1101 if(c->y + c->h + 2 * c->border > m->way + m->wah)
1102 c->y = m->way + m->wah - c->h - 2 * c->border;
1107 c->border = BORDERPX;
1109 wc.border_width = c->border;
1110 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1111 XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
1112 configure(c); /* propagates border_width, if size doesn't change */
1114 XSelectInput(dpy, w, EnterWindowMask | FocusChangeMask | PropertyChangeMask | StructureNotifyMask);
1115 grabbuttons(c, False);
1117 if((rettrans = XGetTransientForHint(dpy, w, &trans) == Success))
1118 for(t = clients; t && t->win != trans; t = t->next);
1120 memcpy(c->tags, t->tags, sizeof initags);
1122 c->isfloating = (rettrans == Success) || c->isfixed;
1125 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); /* some windows require this */
1127 XMapWindow(dpy, c->win);
1128 setclientstate(c, NormalState);
1133 mappingnotify(XEvent *e) {
1134 XMappingEvent *ev = &e->xmapping;
1136 XRefreshKeyboardMapping(ev);
1137 if(ev->request == MappingKeyboard)
1142 maprequest(XEvent *e) {
1143 static XWindowAttributes wa;
1144 XMapRequestEvent *ev = &e->xmaprequest;
1146 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1148 if(wa.override_redirect)
1150 if(!getclient(ev->window))
1151 manage(ev->window, &wa);
1160 XQueryPointer(dpy, root, &win, &win, &x, &y, &i, &i, &mask);
1161 for(i = 0; i < mcount; i++) {
1162 if((x >= monitors[i].sx && x < monitors[i].sx + monitors[i].sw)
1163 && (y >= monitors[i].sy && y < monitors[i].sy + monitors[i].sh)) {
1164 return &monitors[i];
1171 movemouse(Client *c) {
1172 int x1, y1, ocx, ocy, di, nx, ny;
1181 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1182 None, cursor[CurMove], CurrentTime) != GrabSuccess)
1184 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
1186 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
1189 XUngrabPointer(dpy, CurrentTime);
1191 case ConfigureRequest:
1194 handler[ev.type](&ev);
1198 nx = ocx + (ev.xmotion.x - x1);
1199 ny = ocy + (ev.xmotion.y - y1);
1200 if(abs(m->wax - nx) < SNAP)
1202 else if(abs((m->wax + m->waw) - (nx + c->w + 2 * c->border)) < SNAP)
1203 nx = m->wax + m->waw - c->w - 2 * c->border;
1204 if(abs(m->way - ny) < SNAP)
1206 else if(abs((m->way + m->wah) - (ny + c->h + 2 * c->border)) < SNAP)
1207 ny = m->way + m->wah - c->h - 2 * c->border;
1208 if(!c->isfloating && (m->layout->arrange != floating) && (abs(nx - c->x) > SNAP || abs(ny - c->y) > SNAP))
1209 togglefloating(NULL);
1210 if((m->layout->arrange == floating) || c->isfloating)
1211 resize(c, nx, ny, c->w, c->h, False);
1218 nexttiled(Client *c, Monitor *monitor) {
1219 for(; c && (c->isfloating || !isvisible(c, monitor)); c = c->next);
1224 propertynotify(XEvent *e) {
1227 XPropertyEvent *ev = &e->xproperty;
1229 if(ev->state == PropertyDelete)
1230 return; /* ignore */
1231 if((c = getclient(ev->window))) {
1234 case XA_WM_TRANSIENT_FOR:
1235 XGetTransientForHint(dpy, c->win, &trans);
1236 if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
1237 arrange(c->monitor);
1239 case XA_WM_NORMAL_HINTS:
1244 drawbar(c->monitor);
1247 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1250 drawbar(c->monitor);
1256 quit(const char *arg) {
1257 readin = running = False;
1261 reapply(const char *arg) {
1262 static Bool zerotags[LENGTH(tags)] = { 0 };
1265 for(c = clients; c; c = c->next) {
1266 memcpy(c->tags, zerotags, sizeof zerotags);
1273 resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
1279 /* set minimum possible */
1285 /* temporarily remove base dimensions */
1289 /* adjust for aspect limits */
1290 if (c->minay > 0 && c->maxay > 0 && c->minax > 0 && c->maxax > 0) {
1291 if (w * c->maxay > h * c->maxax)
1292 w = h * c->maxax / c->maxay;
1293 else if (w * c->minay < h * c->minax)
1294 h = w * c->minay / c->minax;
1297 /* adjust for increment value */
1303 /* restore base dimensions */
1307 if(c->minw > 0 && w < c->minw)
1309 if(c->minh > 0 && h < c->minh)
1311 if(c->maxw > 0 && w > c->maxw)
1313 if(c->maxh > 0 && h > c->maxh)
1316 if(w <= 0 || h <= 0)
1319 x = m->sw - w - 2 * c->border;
1321 y = m->sh - h - 2 * c->border;
1322 if(x + w + 2 * c->border < m->sx)
1324 if(y + h + 2 * c->border < m->sy)
1326 fprintf(stderr, "resize %d %d %d %d (%d %d %d %d)\n", x, y , w, h, m->sx, m->sy, m->sw, m->sh);
1327 if(c->x != x || c->y != y || c->w != w || c->h != h) {
1330 c->w = wc.width = w;
1331 c->h = wc.height = h;
1332 wc.border_width = c->border;
1333 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
1340 resizemouse(Client *c) {
1349 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1350 None, cursor[CurResize], CurrentTime) != GrabSuccess)
1352 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
1354 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
1357 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
1358 c->w + c->border - 1, c->h + c->border - 1);
1359 XUngrabPointer(dpy, CurrentTime);
1360 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1362 case ConfigureRequest:
1365 handler[ev.type](&ev);
1369 if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
1371 if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
1373 if(!c->isfloating && (m->layout->arrange != floating) && (abs(nw - c->w) > SNAP || abs(nh - c->h) > SNAP))
1374 togglefloating(NULL);
1375 if((m->layout->arrange == floating) || c->isfloating)
1376 resize(c, c->x, c->y, nw, nh, True);
1383 restack(Monitor *m) {
1391 if(sel->isfloating || (m->layout->arrange == floating))
1392 XRaiseWindow(dpy, sel->win);
1393 if(m->layout->arrange != floating) {
1394 wc.stack_mode = Below;
1395 wc.sibling = m->barwin;
1396 if(!sel->isfloating) {
1397 XConfigureWindow(dpy, sel->win, CWSibling | CWStackMode, &wc);
1398 wc.sibling = sel->win;
1400 for(c = nexttiled(clients, m); c; c = nexttiled(c->next, m)) {
1403 XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc);
1404 wc.sibling = c->win;
1408 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1414 char buf[sizeof stext];
1417 unsigned int len, offset;
1420 /* main event loop, also reads status text from stdin */
1422 xfd = ConnectionNumber(dpy);
1425 len = sizeof stext - 1;
1426 buf[len] = stext[len] = '\0'; /* 0-terminator is never touched */
1430 FD_SET(STDIN_FILENO, &rd);
1432 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
1435 eprint("select failed\n");
1437 if(FD_ISSET(STDIN_FILENO, &rd)) {
1438 switch((r = read(STDIN_FILENO, buf + offset, len - offset))) {
1440 strncpy(stext, strerror(errno), len);
1444 strncpy(stext, "EOF", 4);
1448 for(p = buf + offset; r > 0; p++, r--, offset++)
1449 if(*p == '\n' || *p == '\0') {
1451 strncpy(stext, buf, len);
1452 p += r - 1; /* p is buf + offset + r - 1 */
1453 for(r = 0; *(p - r) && *(p - r) != '\n'; r++);
1456 memmove(buf, p - r + 1, r);
1461 drawbar(selmonitor);
1463 while(XPending(dpy)) {
1464 XNextEvent(dpy, &ev);
1465 if(handler[ev.type])
1466 (handler[ev.type])(&ev); /* call handler */
1473 unsigned int i, num;
1474 Window *wins, d1, d2;
1475 XWindowAttributes wa;
1478 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1479 for(i = 0; i < num; i++) {
1480 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1481 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1483 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1484 manage(wins[i], &wa);
1486 for(i = 0; i < num; i++) { /* now the transients */
1487 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1489 if(XGetTransientForHint(dpy, wins[i], &d1)
1490 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1491 manage(wins[i], &wa);
1499 setclientstate(Client *c, long state) {
1500 long data[] = {state, None};
1502 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1503 PropModeReplace, (unsigned char *)data, 2);
1507 setlayout(const char *arg) {
1509 Monitor *m = monitorat();
1513 if(m->layout == &layouts[LENGTH(layouts)])
1514 m->layout = &layouts[0];
1517 for(i = 0; i < LENGTH(layouts); i++)
1518 if(!strcmp(arg, layouts[i].symbol))
1520 if(i == LENGTH(layouts))
1522 m->layout = &layouts[i];
1531 setmwfact(const char *arg) {
1534 Monitor *m = monitorat();
1538 /* arg handling, manipulate mwfact */
1541 else if(sscanf(arg, "%lf", &delta) == 1) {
1542 if(arg[0] == '+' || arg[0] == '-')
1548 else if(m->mwfact > 0.9)
1558 XSetWindowAttributes wa;
1559 XineramaScreenInfo *info = NULL;
1562 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1563 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1564 wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
1565 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1566 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1567 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1570 wa.cursor = cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1571 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1572 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1574 // init screens/monitors first
1575 if((isxinerama = XineramaIsActive(dpy)))
1576 info = XineramaQueryScreens(dpy, &mcount);
1577 selmonitor = monitors = emallocz(mcount * sizeof(Monitor));
1579 screen = DefaultScreen(dpy);
1580 root = RootWindow(dpy, screen);
1582 /* init appearance */
1583 dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
1584 dc.norm[ColBG] = getcolor(NORMBGCOLOR);
1585 dc.norm[ColFG] = getcolor(NORMFGCOLOR);
1586 dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
1587 dc.sel[ColBG] = getcolor(SELBGCOLOR);
1588 dc.sel[ColFG] = getcolor(SELFGCOLOR);
1590 dc.h = bh = dc.font.height + 2;
1591 dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
1592 dc.gc = XCreateGC(dpy, root, 0, 0);
1593 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1595 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
1597 for(blw = i = 0; i < LENGTH(layouts); i++) {
1598 i = textw(layouts[i].symbol);
1602 for(i = 0; i < mcount; i++) {
1606 if(mcount != 1 && isxinerama) {
1607 m->sx = info[i].x_org;
1608 m->sy = info[i].y_org;
1609 m->sw = info[i].width;
1610 m->sh = info[i].height;
1611 fprintf(stderr, "monitor[%d]: %d,%d,%d,%d\n", i, m->sx, m->sy, m->sw, m->sh);
1616 m->sw = DisplayWidth(dpy, screen);
1617 m->sh = DisplayHeight(dpy, screen);
1620 m->seltags = emallocz(sizeof initags);
1621 m->prevtags = emallocz(sizeof initags);
1623 memcpy(m->seltags, initags, sizeof initags);
1624 memcpy(m->prevtags, initags, sizeof initags);
1628 m->layout = &layouts[0];
1630 // TODO: bpos per screen?
1632 wa.override_redirect = 1;
1633 wa.background_pixmap = ParentRelative;
1634 wa.event_mask = ButtonPressMask | ExposureMask;
1637 m->barwin = XCreateWindow(dpy, root, m->sx, m->sy, m->sw, bh, 0,
1638 DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
1639 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
1640 XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
1642 XMapRaised(dpy, m->barwin);
1643 strcpy(stext, "dwm-"VERSION);
1645 /* EWMH support per monitor */
1646 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1647 PropModeReplace, (unsigned char *) netatom, NetLast);
1649 /* select for events */
1650 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
1651 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
1652 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
1653 XSelectInput(dpy, root, wa.event_mask);
1666 selmonitor = monitorat();
1667 fprintf(stderr, "selmonitor == %d\n", selmonitor - monitors);
1671 spawn(const char *arg) {
1672 static char *shell = NULL;
1674 if(!shell && !(shell = getenv("SHELL")))
1678 /* The double-fork construct avoids zombie processes and keeps the code
1679 * clean from stupid signal handlers. */
1683 close(ConnectionNumber(dpy));
1685 execl(shell, shell, "-c", arg, (char *)NULL);
1686 fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
1695 tag(const char *arg) {
1700 for(i = 0; i < LENGTH(tags); i++)
1701 sel->tags[i] = (NULL == arg);
1702 sel->tags[idxoftag(arg)] = True;
1703 arrange(sel->monitor);
1707 textnw(const char *text, unsigned int len) {
1711 XmbTextExtents(dc.font.set, text, len, NULL, &r);
1714 return XTextWidth(dc.font.xfont, text, len);
1718 textw(const char *text) {
1719 return textnw(text, strlen(text)) + dc.font.height;
1724 unsigned int i, n, nx, ny, nw, nh, mw, th;
1727 domwfact = dozoom = True;
1731 for(n = 0, c = nexttiled(clients, m); c; c = nexttiled(c->next, m))
1735 mw = (n == 1) ? m->waw : m->mwfact * m->waw;
1736 th = (n > 1) ? m->wah / (n - 1) : 0;
1737 if(n > 1 && th < bh)
1740 for(i = 0, c = mc = nexttiled(clients, m); c; c = nexttiled(c->next, m)) {
1741 if(i == 0) { /* master */
1744 nw = mw - 2 * c->border;
1745 nh = m->wah - 2 * c->border;
1747 else { /* tile window */
1750 nx += mc->w + 2 * mc->border;
1751 nw = m->waw - mw - 2 * c->border;
1753 if(i + 1 == n) /* remainder */
1754 nh = (m->way + m->wah) - ny - 2 * c->border;
1756 nh = th - 2 * c->border;
1758 fprintf(stderr, "tile %d %d %d %d\n", nx, ny, nw, nh);
1759 resize(c, nx, ny, nw, nh, RESIZEHINTS);
1760 if((RESIZEHINTS) && ((c->h < bh) || (c->h > nh) || (c->w < bh) || (c->w > nw)))
1761 /* client doesn't accept size constraints */
1762 resize(c, nx, ny, nw, nh, False);
1763 if(n > 1 && th != m->wah)
1764 ny = c->y + c->h + 2 * c->border;
1770 togglebar(const char *arg) {
1772 bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
1775 updatebarpos(monitorat());
1776 arrange(monitorat());
1780 togglefloating(const char *arg) {
1783 sel->isfloating = !sel->isfloating;
1785 resize(sel, sel->x, sel->y, sel->w, sel->h, True);
1786 arrange(sel->monitor);
1790 toggletag(const char *arg) {
1796 sel->tags[i] = !sel->tags[i];
1797 for(j = 0; j < LENGTH(tags) && !sel->tags[j]; j++);
1798 if(j == LENGTH(tags))
1799 sel->tags[i] = True; /* at least one tag must be enabled */
1800 arrange(sel->monitor);
1804 toggleview(const char *arg) {
1806 Monitor *m = monitorat();
1809 m->seltags[i] = !m->seltags[i];
1810 for(j = 0; j < LENGTH(tags) && !m->seltags[j]; j++);
1811 if(j == LENGTH(tags))
1812 m->seltags[i] = True; /* at least one tag must be viewed */
1820 XMoveWindow(dpy, c->win, c->x, c->y);
1821 c->isbanned = False;
1825 unmanage(Client *c) {
1826 Monitor *m = c->monitor;
1829 wc.border_width = c->oldborder;
1830 /* The server grab construct avoids race conditions. */
1832 XSetErrorHandler(xerrordummy);
1833 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1838 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1839 setclientstate(c, WithdrawnState);
1843 XSetErrorHandler(xerror);
1849 unmapnotify(XEvent *e) {
1851 XUnmapEvent *ev = &e->xunmap;
1853 if((c = getclient(ev->window)))
1858 updatebarpos(Monitor *m) {
1869 XMoveWindow(dpy, m->barwin, m->sx, m->sy);
1873 XMoveWindow(dpy, m->barwin, m->sx, m->sy + m->wah);
1876 XMoveWindow(dpy, m->barwin, m->sx, m->sy - bh);
1880 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1884 updatesizehints(Client *c) {
1888 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
1890 c->flags = size.flags;
1891 if(c->flags & PBaseSize) {
1892 c->basew = size.base_width;
1893 c->baseh = size.base_height;
1895 else if(c->flags & PMinSize) {
1896 c->basew = size.min_width;
1897 c->baseh = size.min_height;
1900 c->basew = c->baseh = 0;
1901 if(c->flags & PResizeInc) {
1902 c->incw = size.width_inc;
1903 c->inch = size.height_inc;
1906 c->incw = c->inch = 0;
1907 if(c->flags & PMaxSize) {
1908 c->maxw = size.max_width;
1909 c->maxh = size.max_height;
1912 c->maxw = c->maxh = 0;
1913 if(c->flags & PMinSize) {
1914 c->minw = size.min_width;
1915 c->minh = size.min_height;
1917 else if(c->flags & PBaseSize) {
1918 c->minw = size.base_width;
1919 c->minh = size.base_height;
1922 c->minw = c->minh = 0;
1923 if(c->flags & PAspect) {
1924 c->minax = size.min_aspect.x;
1925 c->maxax = size.max_aspect.x;
1926 c->minay = size.min_aspect.y;
1927 c->maxay = size.max_aspect.y;
1930 c->minax = c->maxax = c->minay = c->maxay = 0;
1931 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
1932 && c->maxw == c->minw && c->maxh == c->minh);
1936 updatetitle(Client *c) {
1937 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
1938 gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
1942 updatewmhints(Client *c) {
1945 if((wmh = XGetWMHints(dpy, c->win))) {
1946 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
1951 /* There's no way to check accesses to destroyed windows, thus those cases are
1952 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1953 * default error handler, which may call exit. */
1955 xerror(Display *dpy, XErrorEvent *ee) {
1956 if(ee->error_code == BadWindow
1957 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
1958 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
1959 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
1960 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
1961 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
1962 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
1963 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
1965 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
1966 ee->request_code, ee->error_code);
1967 return xerrorxlib(dpy, ee); /* may call exit */
1971 xerrordummy(Display *dsply, XErrorEvent *ee) {
1975 /* Startup Error handler to check if another window manager
1976 * is already running. */
1978 xerrorstart(Display *dsply, XErrorEvent *ee) {
1984 view(const char *arg) {
1986 Bool tmp[LENGTH(tags)];
1987 Monitor *m = monitorat();
1989 for(i = 0; i < LENGTH(tags); i++)
1990 tmp[i] = (NULL == arg);
1991 tmp[idxoftag(arg)] = True;
1992 if(memcmp(m->seltags, tmp, sizeof initags) != 0) {
1993 memcpy(m->prevtags, m->seltags, sizeof initags);
1994 memcpy(m->seltags, tmp, sizeof initags);
2000 viewprevtag(const char *arg) {
2001 static Bool tmp[LENGTH(tags)];
2003 Monitor *m = monitorat();
2005 memcpy(tmp, m->seltags, sizeof initags);
2006 memcpy(m->seltags, m->prevtags, sizeof initags);
2007 memcpy(m->prevtags, tmp, sizeof initags);
2012 zoom(const char *arg) {
2015 if(!sel || !dozoom || sel->isfloating)
2017 if(c == nexttiled(clients, c->monitor))
2018 if(!(c = nexttiled(c->next, c->monitor)))
2023 arrange(c->monitor);
2027 movetomonitor(const char *arg) {
2035 for(i = 0; &monitors[i] != sel->monitor && i < mcount; i++);
2038 sel->monitor = &monitors[i % mcount];
2040 memcpy(sel->tags, sel->monitor->seltags, sizeof initags);
2041 resize(sel, sel->monitor->wax, sel->monitor->way, sel->w, sel->h, True);
2042 arrange(sel->monitor);
2046 selectmonitor(const char *arg) {
2053 for(i = 0; &monitors[i] != sel->monitor && i < mcount; i++);
2056 m = &monitors[i % mcount];
2057 XWarpPointer(dpy, None, root, 0, 0, 0, 0, m->wax+m->waw/2, m->way+m->wah/2);
2063 main(int argc, char *argv[]) {
2064 if(argc == 2 && !strcmp("-v", argv[1]))
2065 eprint("dwm-"VERSION", © 2006-2008 dwm engineers, see LICENSE for details\n");
2067 eprint("usage: dwm [-v]\n");
2069 setlocale(LC_CTYPE, "");
2070 if(!(dpy = XOpenDisplay(0)))
2071 eprint("dwm: cannot open display\n");