JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
some more polishing/consistency changes
[dwm.git] / dwm.c
1 /* See LICENSE file for copyright and license details.
2  *
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.
8  *
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.
15  *
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.  
21  *
22  * Keys and tagging rules are organized as arrays and defined in config.h.
23  *
24  * To understand everything else, start reading main().
25  */
26 #include <errno.h>
27 #include <locale.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/select.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <regex.h>
37 #include <X11/cursorfont.h>
38 #include <X11/keysym.h>
39 #include <X11/Xatom.h>
40 #include <X11/Xlib.h>
41 #include <X11/Xproto.h>
42 #include <X11/Xutil.h>
43 //#ifdef XINERAMA
44 #include <X11/extensions/Xinerama.h>
45 //#endif
46
47 /* macros */
48 #define BUTTONMASK              (ButtonPressMask | ButtonReleaseMask)
49 #define CLEANMASK(mask)         (mask & ~(numlockmask | LockMask))
50 #define LENGTH(x)               (sizeof x / sizeof x[0])
51 #define MAXTAGLEN               16
52 #define MOUSEMASK               (BUTTONMASK | PointerMotionMask)
53
54
55 /* enums */
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 */
61
62 /* typedefs */
63 typedef struct Client Client;
64 struct Client {
65         char name[256];
66         int x, y, w, h;
67         int basew, baseh, incw, inch, maxw, maxh, minw, minh;
68         int minax, maxax, minay, maxay;
69         long flags;
70         unsigned int border, oldborder;
71         Bool isbanned, isfixed, isfloating;
72         Bool *tags;
73         Client *next;
74         Client *prev;
75         Client *snext;
76         Window win;
77         int monitor;
78 };
79
80 typedef struct {
81         int x, y, w, h;
82         unsigned long norm[ColLast];
83         unsigned long sel[ColLast];
84         Drawable drawable;
85         GC gc;
86         struct {
87                 int ascent;
88                 int descent;
89                 int height;
90                 XFontSet set;
91                 XFontStruct *xfont;
92         } font;
93 } DC; /* draw context */
94
95 typedef struct {
96         unsigned long mod;
97         KeySym keysym;
98         void (*func)(const char *arg);
99         const char *arg;
100 } Key;
101
102 typedef struct {
103         const char *symbol;
104         void (*arrange)(void);
105 } Layout;
106
107 typedef struct {
108         const char *prop;
109         const char *tags;
110         Bool isfloating;
111         int monitor;
112 } Rule;
113
114 typedef struct {
115         regex_t *propregex;
116         regex_t *tagregex;
117 } Regs;
118
119 typedef struct {
120         int screen;
121         Window root;
122         Window barwin;
123         int sx, sy, sw, sh, wax, way, wah, waw;
124         DC dc;
125         Bool *seltags;
126         Bool *prevtags;
127         Layout *layout;
128         double mwfact;
129 } Monitor;
130
131 /* function declarations */
132 void applyrules(Client *c);
133 void arrange(void);
134 void attach(Client *c);
135 void attachstack(Client *c);
136 void ban(Client *c);
137 void buttonpress(XEvent *e);
138 void checkotherwm(void);
139 void cleanup(void);
140 void compileregs(void);
141 void configure(Client *c);
142 void configurenotify(XEvent *e);
143 void configurerequest(XEvent *e);
144 void destroynotify(XEvent *e);
145 void detach(Client *c);
146 void detachstack(Client *c);
147 void drawbar(void);
148 void drawsquare(Monitor *, Bool filled, Bool empty, unsigned long col[ColLast]);
149 void drawtext(Monitor *, const char *text, unsigned long col[ColLast]);
150 void *emallocz(unsigned int size);
151 void enternotify(XEvent *e);
152 void eprint(const char *errstr, ...);
153 void expose(XEvent *e);
154 void floating(void); /* default floating layout */
155 void focus(Client *c);
156 void focusin(XEvent *e);
157 void focusnext(const char *arg);
158 void focusprev(const char *arg);
159 Client *getclient(Window w);
160 unsigned long getcolor(const char *colstr, int screen);
161 long getstate(Window w);
162 Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
163 void grabbuttons(Client *c, Bool focused);
164 void grabkeys(void);
165 unsigned int idxoftag(const char *tag);
166 void initfont(Monitor*, const char *fontstr);
167 Bool isoccupied(Monitor *m, unsigned int t);
168 Bool isprotodel(Client *c);
169 Bool isvisible(Client *c, Monitor *m);
170 void keypress(XEvent *e);
171 void killclient(const char *arg);
172 void manage(Window w, XWindowAttributes *wa);
173 void mappingnotify(XEvent *e);
174 void maprequest(XEvent *e);
175 void movemouse(Client *c);
176 Client *nexttiled(Client *c, Monitor *m);
177 void propertynotify(XEvent *e);
178 void quit(const char *arg);
179 void reapply(const char *arg);
180 void resize(Client *c, int x, int y, int w, int h, Bool sizehints);
181 void resizemouse(Client *c);
182 void restack(void);
183 void run(void);
184 void scan(void);
185 void setclientstate(Client *c, long state);
186 void setlayout(const char *arg);
187 void setmwfact(const char *arg);
188 void setup(void);
189 void spawn(const char *arg);
190 void tag(const char *arg);
191 unsigned int textnw(Monitor*, const char *text, unsigned int len);
192 unsigned int textw(Monitor*, const char *text);
193 void tile(void);
194 void togglebar(const char *arg);
195 void togglefloating(const char *arg);
196 void toggletag(const char *arg);
197 void toggleview(const char *arg);
198 void unban(Client *c);
199 void unmanage(Client *c);
200 void unmapnotify(XEvent *e);
201 void updatebarpos(Monitor *m);
202 void updatesizehints(Client *c);
203 void updatetitle(Client *c);
204 void view(const char *arg);
205 void viewprevtag(const char *arg);      /* views previous selected tags */
206 int xerror(Display *dpy, XErrorEvent *ee);
207 int xerrordummy(Display *dsply, XErrorEvent *ee);
208 int xerrorstart(Display *dsply, XErrorEvent *ee);
209 void zoom(const char *arg);
210 int monitorat(int, int);
211 void movetomonitor(const char *arg);
212 void selectmonitor(const char *arg);
213
214 /* variables */
215 char stext[256];
216 int mcount = 1;
217 //double mwfact;
218 int (*xerrorxlib)(Display *, XErrorEvent *);
219 unsigned int bh, bpos;
220 unsigned int blw = 0;
221 unsigned int numlockmask = 0;
222 void (*handler[LASTEvent]) (XEvent *) = {
223         [ButtonPress] = buttonpress,
224         [ConfigureRequest] = configurerequest,
225         [ConfigureNotify] = configurenotify,
226         [DestroyNotify] = destroynotify,
227         [EnterNotify] = enternotify,
228         [Expose] = expose,
229         [FocusIn] = focusin,
230         [KeyPress] = keypress,
231         [MappingNotify] = mappingnotify,
232         [MapRequest] = maprequest,
233         [PropertyNotify] = propertynotify,
234         [UnmapNotify] = unmapnotify
235 };
236 Atom wmatom[WMLast], netatom[NetLast];
237 Bool domwfact = True;
238 Bool dozoom = True;
239 Bool otherwm, readin;
240 Bool running = True;
241 Client *clients = NULL;
242 Client *sel = NULL;
243 Client *stack = NULL;
244 Cursor cursor[CurLast];
245 Display *dpy;
246 DC dc = {0};
247 Regs *regs = NULL;
248 Monitor *monitors;
249 int selmonitor = 0;
250
251 /* configuration, allows nested code to access above variables */
252 #include "config.h"
253
254 //Bool prevtags[LENGTH(tags)];
255
256 /* function implementations */
257 void
258 applyrules(Client *c) {
259         static char buf[512];
260         unsigned int i, j;
261         regmatch_t tmp;
262         Bool matched_tag = False;
263         Bool matched_monitor = False;
264         XClassHint ch = { 0 };
265
266         /* rule matching */
267         XGetClassHint(dpy, c->win, &ch);
268         snprintf(buf, sizeof buf, "%s:%s:%s",
269                         ch.res_class ? ch.res_class : "",
270                         ch.res_name ? ch.res_name : "", c->name);
271         for(i = 0; i < LENGTH(rules); i++)
272                 if(regs[i].propregex && !regexec(regs[i].propregex, buf, 1, &tmp, 0)) {
273                         if (rules[i].monitor >= 0 && rules[i].monitor < mcount) {
274                                 matched_monitor = True;
275                                 c->monitor = rules[i].monitor;
276                         }
277
278                         c->isfloating = rules[i].isfloating;
279                         for(j = 0; regs[i].tagregex && j < LENGTH(tags); j++) {
280                                 if(!regexec(regs[i].tagregex, tags[j], 1, &tmp, 0)) {
281                                         matched_tag = True;
282                                         c->tags[j] = True;
283                                 }
284                         }
285                 }
286         if(ch.res_class)
287                 XFree(ch.res_class);
288         if(ch.res_name)
289                 XFree(ch.res_name);
290         if(!matched_tag)
291                 memcpy(c->tags, monitors[monitorat(-1, -1)].seltags, sizeof initags);
292         if (!matched_monitor)
293                 c->monitor = monitorat(-1, -1);
294 }
295
296 void
297 arrange(void) {
298         Client *c;
299
300         for(c = clients; c; c = c->next)
301                 if(isvisible(c, &monitors[c->monitor]))
302                         unban(c);
303                 else
304                         ban(c);
305
306         monitors[selmonitor].layout->arrange();
307         focus(NULL);
308         restack();
309 }
310
311 void
312 attach(Client *c) {
313         if(clients)
314                 clients->prev = c;
315         c->next = clients;
316         clients = c;
317 }
318
319 void
320 attachstack(Client *c) {
321         c->snext = stack;
322         stack = c;
323 }
324
325 void
326 ban(Client *c) {
327         if(c->isbanned)
328                 return;
329         XMoveWindow(dpy, c->win, c->x + 3 * monitors[c->monitor].sw, c->y);
330         c->isbanned = True;
331 }
332
333 void
334 buttonpress(XEvent *e) {
335         unsigned int i, x;
336         Client *c;
337         XButtonPressedEvent *ev = &e->xbutton;
338
339         Monitor *m = &monitors[monitorat(-1, -1)];
340
341         if(ev->window == m->barwin) {
342                 x = 0;
343                 for(i = 0; i < LENGTH(tags); i++) {
344                         x += textw(m, tags[i]);
345                         if(ev->x < x) {
346                                 if(ev->button == Button1) {
347                                         if(ev->state & MODKEY)
348                                                 tag(tags[i]);
349                                         else
350                                                 view(tags[i]);
351                                 }
352                                 else if(ev->button == Button3) {
353                                         if(ev->state & MODKEY)
354                                                 toggletag(tags[i]);
355                                         else
356                                                 toggleview(tags[i]);
357                                 }
358                                 return;
359                         }
360                 }
361                 if((ev->x < x + blw) && ev->button == Button1)
362                         setlayout(NULL);
363         }
364         else if((c = getclient(ev->window))) {
365                 focus(c);
366                 if(CLEANMASK(ev->state) != MODKEY)
367                         return;
368                 if(ev->button == Button1) {
369                         if((m->layout->arrange == floating) || c->isfloating)
370                                 restack();
371                         else
372                                 togglefloating(NULL);
373                         movemouse(c);
374                 }
375                 else if(ev->button == Button2) {
376                         if((floating != m->layout->arrange) && c->isfloating)
377                                 togglefloating(NULL);
378                         else
379                                 zoom(NULL);
380                 }
381                 else if(ev->button == Button3 && !c->isfixed) {
382                         if((floating == m->layout->arrange) || c->isfloating)
383                                 restack();
384                         else
385                                 togglefloating(NULL);
386                         resizemouse(c);
387                 }
388         }
389 }
390
391 void
392 checkotherwm(void) {
393         otherwm = False;
394         XSetErrorHandler(xerrorstart);
395
396         /* this causes an error if some other window manager is running */
397         XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
398         XSync(dpy, False);
399         if(otherwm)
400                 eprint("dwm: another window manager is already running\n");
401         XSync(dpy, False);
402         XSetErrorHandler(NULL);
403         xerrorxlib = XSetErrorHandler(xerror);
404         XSync(dpy, False);
405 }
406
407 void
408 cleanup(void) {
409         unsigned int i;
410         close(STDIN_FILENO);
411         while(stack) {
412                 unban(stack);
413                 unmanage(stack);
414         }
415         for(i = 0; i < mcount; i++) {
416                 Monitor *m = &monitors[i];
417                 if(m->dc.font.set)
418                         XFreeFontSet(dpy, m->dc.font.set);
419                 else
420                         XFreeFont(dpy, m->dc.font.xfont);
421                 XUngrabKey(dpy, AnyKey, AnyModifier, m->root);
422                 XFreePixmap(dpy, m->dc.drawable);
423                 XFreeGC(dpy, m->dc.gc);
424                 XDestroyWindow(dpy, m->barwin);
425                 XFreeCursor(dpy, cursor[CurNormal]);
426                 XFreeCursor(dpy, cursor[CurResize]);
427                 XFreeCursor(dpy, cursor[CurMove]);
428                 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
429                 XSync(dpy, False);
430         }
431 }
432
433 void
434 compileregs(void) {
435         unsigned int i;
436         regex_t *reg;
437
438         if(regs)
439                 return;
440         regs = emallocz(LENGTH(rules) * sizeof(Regs));
441         for(i = 0; i < LENGTH(rules); i++) {
442                 if(rules[i].prop) {
443                         reg = emallocz(sizeof(regex_t));
444                         if(regcomp(reg, rules[i].prop, REG_EXTENDED))
445                                 free(reg);
446                         else
447                                 regs[i].propregex = reg;
448                 }
449                 if(rules[i].tags) {
450                         reg = emallocz(sizeof(regex_t));
451                         if(regcomp(reg, rules[i].tags, REG_EXTENDED))
452                                 free(reg);
453                         else
454                                 regs[i].tagregex = reg;
455                 }
456         }
457 }
458
459 void
460 configure(Client *c) {
461         XConfigureEvent ce;
462
463         ce.type = ConfigureNotify;
464         ce.display = dpy;
465         ce.event = c->win;
466         ce.window = c->win;
467         ce.x = c->x;
468         ce.y = c->y;
469         ce.width = c->w;
470         ce.height = c->h;
471         ce.border_width = c->border;
472         ce.above = None;
473         ce.override_redirect = False;
474         XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
475 }
476
477 void
478 configurenotify(XEvent *e) {
479         XConfigureEvent *ev = &e->xconfigure;
480         Monitor *m = &monitors[selmonitor];
481
482         if(ev->window == m->root && (ev->width != m->sw || ev->height != m->sh)) {
483                 m->sw = ev->width;
484                 m->sh = ev->height;
485                 XFreePixmap(dpy, dc.drawable);
486                 dc.drawable = XCreatePixmap(dpy, m->root, m->sw, bh, DefaultDepth(dpy, m->screen));
487                 XResizeWindow(dpy, m->barwin, m->sw, bh);
488                 updatebarpos(m);
489                 arrange();
490         }
491 }
492
493 void
494 configurerequest(XEvent *e) {
495         Client *c;
496         XConfigureRequestEvent *ev = &e->xconfigurerequest;
497         XWindowChanges wc;
498
499         if((c = getclient(ev->window))) {
500                 Monitor *m = &monitors[c->monitor];
501                 if(ev->value_mask & CWBorderWidth)
502                         c->border = ev->border_width;
503                 if(c->isfixed || c->isfloating || (floating == m->layout->arrange)) {
504                         if(ev->value_mask & CWX)
505                                 c->x = m->sx+ev->x;
506                         if(ev->value_mask & CWY)
507                                 c->y = m->sy+ev->y;
508                         if(ev->value_mask & CWWidth)
509                                 c->w = ev->width;
510                         if(ev->value_mask & CWHeight)
511                                 c->h = ev->height;
512                         if((c->x - m->sx + c->w) > m->sw && c->isfloating)
513                                 c->x = m->sx + (m->sw / 2 - c->w / 2); /* center in x direction */
514                         if((c->y - m->sy + c->h) > m->sh && c->isfloating)
515                                 c->y = m->sy + (m->sh / 2 - c->h / 2); /* center in y direction */
516                         if((ev->value_mask & (CWX | CWY))
517                         && !(ev->value_mask & (CWWidth | CWHeight)))
518                                 configure(c);
519                         if(isvisible(c, &monitors[monitorat(-1,-1)]))
520                                 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
521                 }
522                 else
523                         configure(c);
524         }
525         else {
526                 wc.x = ev->x;
527                 wc.y = ev->y;
528                 wc.width = ev->width;
529                 wc.height = ev->height;
530                 wc.border_width = ev->border_width;
531                 wc.sibling = ev->above;
532                 wc.stack_mode = ev->detail;
533                 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
534         }
535         XSync(dpy, False);
536 }
537
538 void
539 destroynotify(XEvent *e) {
540         Client *c;
541         XDestroyWindowEvent *ev = &e->xdestroywindow;
542
543         if((c = getclient(ev->window)))
544                 unmanage(c);
545 }
546
547 void
548 detach(Client *c) {
549         if(c->prev)
550                 c->prev->next = c->next;
551         if(c->next)
552                 c->next->prev = c->prev;
553         if(c == clients)
554                 clients = c->next;
555         c->next = c->prev = NULL;
556 }
557
558 void
559 detachstack(Client *c) {
560         Client **tc;
561
562         for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
563         *tc = c->snext;
564 }
565
566 void
567 drawbar(void) {
568         int i, j, x;
569
570         for(i = 0; i < mcount; i++) {
571                 Monitor *m = &monitors[i];
572                 m->dc.x = 0;
573                 for(j = 0; j < LENGTH(tags); j++) {
574                         m->dc.w = textw(m, tags[j]);
575                         if(m->seltags[j]) {
576                                 drawtext(m, tags[j], m->dc.sel);
577                                 drawsquare(m, sel && sel->tags[j] && sel->monitor == selmonitor, isoccupied(m, j), m->dc.sel);
578                         }
579                         else {
580                                 drawtext(m, tags[j], m->dc.norm);
581                                 drawsquare(m, sel && sel->tags[j] && sel->monitor == selmonitor, isoccupied(m, j), m->dc.norm);
582                         }
583                         m->dc.x += m->dc.w;
584                 }
585                 m->dc.w = blw;
586                 drawtext(m, m->layout->symbol, m->dc.norm);
587                 x = m->dc.x + m->dc.w;
588                 m->dc.w = textw(m, stext);
589                 m->dc.x = m->sw - m->dc.w;
590                 if(m->dc.x < x) {
591                         m->dc.x = x;
592                         m->dc.w = m->sw - x;
593                 }
594                 drawtext(m, stext, m->dc.norm);
595                 if((m->dc.w = m->dc.x - x) > bh) {
596                         m->dc.x = x;
597                         if(sel && sel->monitor == selmonitor) {
598                                 drawtext(m, sel->name, m->dc.sel);
599                                 drawsquare(m, False, sel->isfloating, m->dc.sel);
600                         }
601                         else
602                                 drawtext(m, NULL, m->dc.norm);
603                 }
604                 XCopyArea(dpy, m->dc.drawable, m->barwin, m->dc.gc, 0, 0, m->sw, bh, 0, 0);
605                 XSync(dpy, False);
606         }
607 }
608
609 void
610 drawsquare(Monitor *m, Bool filled, Bool empty, unsigned long col[ColLast]) {
611         int x;
612         XGCValues gcv;
613         XRectangle r = { m->dc.x, m->dc.y, m->dc.w, m->dc.h };
614
615         gcv.foreground = col[ColFG];
616         XChangeGC(dpy, m->dc.gc, GCForeground, &gcv);
617         x = (m->dc.font.ascent + m->dc.font.descent + 2) / 4;
618         r.x = m->dc.x + 1;
619         r.y = m->dc.y + 1;
620         if(filled) {
621                 r.width = r.height = x + 1;
622                 XFillRectangles(dpy, m->dc.drawable, m->dc.gc, &r, 1);
623         }
624         else if(empty) {
625                 r.width = r.height = x;
626                 XDrawRectangles(dpy, m->dc.drawable, m->dc.gc, &r, 1);
627         }
628 }
629
630 void
631 drawtext(Monitor *m, const char *text, unsigned long col[ColLast]) {
632         int x, y, w, h;
633         static char buf[256];
634         unsigned int len, olen;
635         XRectangle r = { m->dc.x, m->dc.y, m->dc.w, m->dc.h };
636
637         XSetForeground(dpy, m->dc.gc, col[ColBG]);
638         XFillRectangles(dpy, m->dc.drawable, m->dc.gc, &r, 1);
639         if(!text)
640                 return;
641         w = 0;
642         olen = len = strlen(text);
643         if(len >= sizeof buf)
644                 len = sizeof buf - 1;
645         memcpy(buf, text, len);
646         buf[len] = 0;
647         h = m->dc.font.ascent + m->dc.font.descent;
648         y = m->dc.y + (m->dc.h / 2) - (h / 2) + m->dc.font.ascent;
649         x = m->dc.x + (h / 2);
650         /* shorten text if necessary */
651         while(len && (w = textnw(m, buf, len)) > m->dc.w - h)
652                 buf[--len] = 0;
653         if(len < olen) {
654                 if(len > 1)
655                         buf[len - 1] = '.';
656                 if(len > 2)
657                         buf[len - 2] = '.';
658                 if(len > 3)
659                         buf[len - 3] = '.';
660         }
661         if(w > m->dc.w)
662                 return; /* too long */
663         XSetForeground(dpy, m->dc.gc, col[ColFG]);
664         if(m->dc.font.set)
665                 XmbDrawString(dpy, m->dc.drawable, m->dc.font.set, m->dc.gc, x, y, buf, len);
666         else
667                 XDrawString(dpy, m->dc.drawable, m->dc.gc, x, y, buf, len);
668 }
669
670 void *
671 emallocz(unsigned int size) {
672         void *res = calloc(1, size);
673
674         if(!res)
675                 eprint("fatal: could not malloc() %u bytes\n", size);
676         return res;
677 }
678
679 void
680 enternotify(XEvent *e) {
681         unsigned int i;
682         Client *c;
683         XCrossingEvent *ev = &e->xcrossing;
684
685         if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
686                 return;
687         if((c = getclient(ev->window)))
688                 focus(c);
689         else {
690                 for(i = 0; i < mcount; i++)
691                         if(ev->window == monitors[i].root) {
692                                 selmonitor = i;
693                                 focus(NULL);
694                                 break;
695                         }
696         }
697 }
698
699 void
700 eprint(const char *errstr, ...) {
701         va_list ap;
702
703         va_start(ap, errstr);
704         vfprintf(stderr, errstr, ap);
705         va_end(ap);
706         exit(EXIT_FAILURE);
707 }
708
709 void
710 expose(XEvent *e) {
711         XExposeEvent *ev = &e->xexpose;
712
713         if(ev->count == 0) {
714                 if(ev->window == monitors[selmonitor].barwin)
715                         drawbar();
716         }
717 }
718
719 void
720 floating(void) { /* default floating layout */
721         Client *c;
722
723         domwfact = dozoom = False;
724         for(c = clients; c; c = c->next)
725                 if(isvisible(c, &monitors[selmonitor]))
726                         resize(c, c->x, c->y, c->w, c->h, True);
727 }
728
729 void
730 focus(Client *c) {
731         Monitor *m = &monitors[c ? c->monitor : selmonitor];
732         if(!c || (c && !isvisible(c, m)))
733                 for(c = stack; c && !isvisible(c, m); c = c->snext);
734         if(sel && sel != c) {
735                 grabbuttons(sel, False);
736                 XSetWindowBorder(dpy, sel->win, monitors[sel->monitor].dc.norm[ColBorder]);
737         }
738         if(c) {
739                 detachstack(c);
740                 attachstack(c);
741                 grabbuttons(c, True);
742         }
743         sel = c;
744         drawbar();
745         if(c) {
746                 XSetWindowBorder(dpy, c->win, m->dc.sel[ColBorder]);
747                 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
748                 selmonitor = c->monitor;
749         }
750         else {
751                 XSetInputFocus(dpy, m->root, RevertToPointerRoot, CurrentTime);
752         }
753 }
754
755 void
756 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
757         XFocusChangeEvent *ev = &e->xfocus;
758
759         if(sel && ev->window != sel->win)
760                 XSetInputFocus(dpy, sel->win, RevertToPointerRoot, CurrentTime);
761 }
762
763 void
764 focusnext(const char *arg) {
765         Client *c;
766         Monitor *m = &monitors[selmonitor];
767
768         if(!sel)
769                 return;
770         for(c = sel->next; c && !isvisible(c, m); c = c->next);
771         if(!c)
772                 for(c = clients; c && !isvisible(c, m); c = c->next);
773         if(c) {
774                 focus(c);
775                 restack();
776         }
777 }
778
779 void
780 focusprev(const char *arg) {
781         Client *c;
782         Monitor *m = &monitors[selmonitor];
783
784         if(!sel)
785                 return;
786         for(c = sel->prev; c && !isvisible(c, m); c = c->prev);
787         if(!c) {
788                 for(c = clients; c && c->next; c = c->next);
789                 for(; c && !isvisible(c, m); c = c->prev);
790         }
791         if(c) {
792                 focus(c);
793                 restack();
794         }
795 }
796
797 Client *
798 getclient(Window w) {
799         Client *c;
800
801         for(c = clients; c && c->win != w; c = c->next);
802         return c;
803 }
804
805 unsigned long
806 getcolor(const char *colstr, int screen) {
807         Colormap cmap = DefaultColormap(dpy, screen);
808         XColor color;
809
810         if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
811                 eprint("error, cannot allocate color '%s'\n", colstr);
812         return color.pixel;
813 }
814
815 long
816 getstate(Window w) {
817         int format, status;
818         long result = -1;
819         unsigned char *p = NULL;
820         unsigned long n, extra;
821         Atom real;
822
823         status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
824                         &real, &format, &n, &extra, (unsigned char **)&p);
825         if(status != Success)
826                 return -1;
827         if(n != 0)
828                 result = *p;
829         XFree(p);
830         return result;
831 }
832
833 Bool
834 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
835         char **list = NULL;
836         int n;
837         XTextProperty name;
838
839         if(!text || size == 0)
840                 return False;
841         text[0] = '\0';
842         XGetTextProperty(dpy, w, &name, atom);
843         if(!name.nitems)
844                 return False;
845         if(name.encoding == XA_STRING)
846                 strncpy(text, (char *)name.value, size - 1);
847         else {
848                 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
849                 && n > 0 && *list) {
850                         strncpy(text, *list, size - 1);
851                         XFreeStringList(list);
852                 }
853         }
854         text[size - 1] = '\0';
855         XFree(name.value);
856         return True;
857 }
858
859 void
860 grabbuttons(Client *c, Bool focused) {
861         XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
862
863         if(focused) {
864                 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
865                                 GrabModeAsync, GrabModeSync, None, None);
866                 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
867                                 GrabModeAsync, GrabModeSync, None, None);
868                 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
869                                 GrabModeAsync, GrabModeSync, None, None);
870                 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
871                                 GrabModeAsync, GrabModeSync, None, None);
872
873                 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
874                                 GrabModeAsync, GrabModeSync, None, None);
875                 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
876                                 GrabModeAsync, GrabModeSync, None, None);
877                 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
878                                 GrabModeAsync, GrabModeSync, None, None);
879                 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
880                                 GrabModeAsync, GrabModeSync, None, None);
881
882                 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
883                                 GrabModeAsync, GrabModeSync, None, None);
884                 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
885                                 GrabModeAsync, GrabModeSync, None, None);
886                 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
887                                 GrabModeAsync, GrabModeSync, None, None);
888                 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
889                                 GrabModeAsync, GrabModeSync, None, None);
890         }
891         else
892                 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
893                                 GrabModeAsync, GrabModeSync, None, None);
894 }
895
896 void
897 grabkeys(void)  {
898         unsigned int i, j;
899         KeyCode code;
900         XModifierKeymap *modmap;
901
902         /* init modifier map */
903         modmap = XGetModifierMapping(dpy);
904         for(i = 0; i < 8; i++)
905                 for(j = 0; j < modmap->max_keypermod; j++) {
906                         if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
907                                 numlockmask = (1 << i);
908                 }
909         XFreeModifiermap(modmap);
910
911         for(i = 0; i < mcount; i++) {
912                 Monitor *m = &monitors[i];
913                 XUngrabKey(dpy, AnyKey, AnyModifier, m->root);
914                 for(j = 0; j < LENGTH(keys); j++) {
915                         code = XKeysymToKeycode(dpy, keys[j].keysym);
916                         XGrabKey(dpy, code, keys[j].mod, m->root, True,
917                                         GrabModeAsync, GrabModeAsync);
918                         XGrabKey(dpy, code, keys[j].mod | LockMask, m->root, True,
919                                         GrabModeAsync, GrabModeAsync);
920                         XGrabKey(dpy, code, keys[j].mod | numlockmask, m->root, True,
921                                         GrabModeAsync, GrabModeAsync);
922                         XGrabKey(dpy, code, keys[j].mod | numlockmask | LockMask, m->root, True,
923                                         GrabModeAsync, GrabModeAsync);
924                 }
925         }
926 }
927
928 unsigned int
929 idxoftag(const char *tag) {
930         unsigned int i;
931
932         for(i = 0; (i < LENGTH(tags)) && (tags[i] != tag); i++);
933         return (i < LENGTH(tags)) ? i : 0;
934 }
935
936 void
937 initfont(Monitor *m, const char *fontstr) {
938         char *def, **missing;
939         int i, n;
940
941         missing = NULL;
942         if(m->dc.font.set)
943                 XFreeFontSet(dpy, m->dc.font.set);
944         m->dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
945         if(missing) {
946                 while(n--)
947                         fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
948                 XFreeStringList(missing);
949         }
950         if(m->dc.font.set) {
951                 XFontSetExtents *font_extents;
952                 XFontStruct **xfonts;
953                 char **font_names;
954                 m->dc.font.ascent = m->dc.font.descent = 0;
955                 font_extents = XExtentsOfFontSet(m->dc.font.set);
956                 n = XFontsOfFontSet(m->dc.font.set, &xfonts, &font_names);
957                 for(i = 0, m->dc.font.ascent = 0, m->dc.font.descent = 0; i < n; i++) {
958                         if(m->dc.font.ascent < (*xfonts)->ascent)
959                                 m->dc.font.ascent = (*xfonts)->ascent;
960                         if(m->dc.font.descent < (*xfonts)->descent)
961                                 m->dc.font.descent = (*xfonts)->descent;
962                         xfonts++;
963                 }
964         }
965         else {
966                 if(m->dc.font.xfont)
967                         XFreeFont(dpy, m->dc.font.xfont);
968                 m->dc.font.xfont = NULL;
969                 if(!(m->dc.font.xfont = XLoadQueryFont(dpy, fontstr))
970                 && !(m->dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
971                         eprint("error, cannot load font: '%s'\n", fontstr);
972                 m->dc.font.ascent = m->dc.font.xfont->ascent;
973                 m->dc.font.descent = m->dc.font.xfont->descent;
974         }
975         m->dc.font.height = m->dc.font.ascent + m->dc.font.descent;
976 }
977
978 Bool
979 isoccupied(Monitor *m, unsigned int t) {
980         Client *c;
981
982         for(c = clients; c; c = c->next)
983                 if(c->tags[t] && c->monitor == selmonitor)
984                         return True;
985         return False;
986 }
987
988 Bool
989 isprotodel(Client *c) {
990         int i, n;
991         Atom *protocols;
992         Bool ret = False;
993
994         if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
995                 for(i = 0; !ret && i < n; i++)
996                         if(protocols[i] == wmatom[WMDelete])
997                                 ret = True;
998                 XFree(protocols);
999         }
1000         return ret;
1001 }
1002
1003 Bool
1004 isvisible(Client *c, Monitor *m) {
1005         unsigned int i;
1006
1007         for(i = 0; i < LENGTH(tags); i++)
1008                 if(c->tags[i] && monitors[c->monitor].seltags[i] && c->monitor == selmonitor)
1009                         return True;
1010         return False;
1011 }
1012
1013 void
1014 keypress(XEvent *e) {
1015         unsigned int i;
1016         KeySym keysym;
1017         XKeyEvent *ev;
1018
1019         ev = &e->xkey;
1020         keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1021         for(i = 0; i < LENGTH(keys); i++)
1022                 if(keysym == keys[i].keysym
1023                 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state))
1024                 {
1025                         if(keys[i].func)
1026                                 keys[i].func(keys[i].arg);
1027                 }
1028 }
1029
1030 void
1031 killclient(const char *arg) {
1032         XEvent ev;
1033
1034         if(!sel)
1035                 return;
1036         if(isprotodel(sel)) {
1037                 ev.type = ClientMessage;
1038                 ev.xclient.window = sel->win;
1039                 ev.xclient.message_type = wmatom[WMProtocols];
1040                 ev.xclient.format = 32;
1041                 ev.xclient.data.l[0] = wmatom[WMDelete];
1042                 ev.xclient.data.l[1] = CurrentTime;
1043                 XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
1044         }
1045         else
1046                 XKillClient(dpy, sel->win);
1047 }
1048
1049 void
1050 manage(Window w, XWindowAttributes *wa) {
1051         Client *c, *t = NULL;
1052         Monitor *m = &monitors[selmonitor];
1053         Status rettrans;
1054         Window trans;
1055         XWindowChanges wc;
1056
1057         c = emallocz(sizeof(Client));
1058         c->tags = emallocz(sizeof initags);
1059         c->win = w;
1060
1061         applyrules(c);
1062
1063         c->x = wa->x + m->sx;
1064         c->y = wa->y + m->sy;
1065         c->w = wa->width;
1066         c->h = wa->height;
1067         c->oldborder = wa->border_width;
1068
1069         if (monitorat(c->x, c->y) != c->monitor) {
1070                 c->x = m->sx;
1071                 c->y = m->sy;
1072         }
1073
1074         if(c->w == m->sw && c->h == m->sh) {
1075                 c->x = m->sx;
1076                 c->y = m->sy;
1077                 c->border = wa->border_width;
1078         }
1079         else {
1080                 if(c->x + c->w + 2 * c->border > m->wax + m->waw)
1081                         c->x = m->wax + m->waw - c->w - 2 * c->border;
1082                 if(c->y + c->h + 2 * c->border > m->way + m->wah)
1083                         c->y = m->way + m->wah - c->h - 2 * c->border;
1084                 if(c->x < m->wax)
1085                         c->x = m->wax;
1086                 if(c->y < m->way)
1087                         c->y = m->way;
1088                 c->border = BORDERPX;
1089         }
1090         wc.border_width = c->border;
1091         XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1092         XSetWindowBorder(dpy, w, m->dc.norm[ColBorder]);
1093         configure(c); /* propagates border_width, if size doesn't change */
1094         updatesizehints(c);
1095         XSelectInput(dpy, w, EnterWindowMask | FocusChangeMask | PropertyChangeMask | StructureNotifyMask);
1096         grabbuttons(c, False);
1097         updatetitle(c);
1098         if((rettrans = XGetTransientForHint(dpy, w, &trans) == Success))
1099                 for(t = clients; t && t->win != trans; t = t->next);
1100         if(t)
1101                 memcpy(c->tags, t->tags, sizeof initags);
1102         if(!c->isfloating)
1103                 c->isfloating = (rettrans == Success) || c->isfixed;
1104         attach(c);
1105         attachstack(c);
1106         XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); /* some windows require this */
1107         ban(c);
1108         XMapWindow(dpy, c->win);
1109         setclientstate(c, NormalState);
1110         arrange();
1111 }
1112
1113 void
1114 mappingnotify(XEvent *e) {
1115         XMappingEvent *ev = &e->xmapping;
1116
1117         XRefreshKeyboardMapping(ev);
1118         if(ev->request == MappingKeyboard)
1119                 grabkeys();
1120 }
1121
1122 void
1123 maprequest(XEvent *e) {
1124         static XWindowAttributes wa;
1125         XMapRequestEvent *ev = &e->xmaprequest;
1126
1127         if(!XGetWindowAttributes(dpy, ev->window, &wa))
1128                 return;
1129         if(wa.override_redirect)
1130                 return;
1131         if(!getclient(ev->window))
1132                 manage(ev->window, &wa);
1133 }
1134
1135 void
1136 movemouse(Client *c) {
1137         int x1, y1, ocx, ocy, di, nx, ny;
1138         unsigned int dui;
1139         Window dummy;
1140         XEvent ev;
1141
1142         ocx = nx = c->x;
1143         ocy = ny = c->y;
1144         if(XGrabPointer(dpy, monitors[selmonitor].root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1145                         None, cursor[CurMove], CurrentTime) != GrabSuccess)
1146                 return;
1147         XQueryPointer(dpy, monitors[selmonitor].root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
1148         for(;;) {
1149                 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
1150                 switch (ev.type) {
1151                 case ButtonRelease:
1152                         XUngrabPointer(dpy, CurrentTime);
1153                         return;
1154                 case ConfigureRequest:
1155                 case Expose:
1156                 case MapRequest:
1157                         handler[ev.type](&ev);
1158                         break;
1159                 case MotionNotify:
1160                         XSync(dpy, False);
1161                         nx = ocx + (ev.xmotion.x - x1);
1162                         ny = ocy + (ev.xmotion.y - y1);
1163                         Monitor *m = &monitors[monitorat(nx, ny)];
1164                         if(abs(m->wax - nx) < SNAP)
1165                                 nx = m->wax;
1166                         else if(abs((m->wax + m->waw) - (nx + c->w + 2 * c->border)) < SNAP)
1167                                 nx = m->wax + m->waw - c->w - 2 * c->border;
1168                         if(abs(m->way - ny) < SNAP)
1169                                 ny = m->way;
1170                         else if(abs((m->way + m->wah) - (ny + c->h + 2 * c->border)) < SNAP)
1171                                 ny = m->way + m->wah - c->h - 2 * c->border;
1172                         resize(c, nx, ny, c->w, c->h, False);
1173                         memcpy(c->tags, monitors[monitorat(nx, ny)].seltags, sizeof initags);
1174                         break;
1175                 }
1176         }
1177 }
1178
1179 Client *
1180 nexttiled(Client *c, Monitor *m) {
1181         for(; c && (c->isfloating || !isvisible(c, m)); c = c->next);
1182         return c;
1183 }
1184
1185 void
1186 propertynotify(XEvent *e) {
1187         Client *c;
1188         Window trans;
1189         XPropertyEvent *ev = &e->xproperty;
1190
1191         if(ev->state == PropertyDelete)
1192                 return; /* ignore */
1193         if((c = getclient(ev->window))) {
1194                 switch (ev->atom) {
1195                         default: break;
1196                         case XA_WM_TRANSIENT_FOR:
1197                                 XGetTransientForHint(dpy, c->win, &trans);
1198                                 if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
1199                                         arrange();
1200                                 break;
1201                         case XA_WM_NORMAL_HINTS:
1202                                 updatesizehints(c);
1203                                 break;
1204                 }
1205                 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1206                         updatetitle(c);
1207                         if(c == sel)
1208                                 drawbar();
1209                 }
1210         }
1211 }
1212
1213 void
1214 quit(const char *arg) {
1215         readin = running = False;
1216 }
1217
1218 void
1219 reapply(const char *arg) {
1220         static Bool zerotags[LENGTH(tags)] = { 0 };
1221         Client *c;
1222
1223         for(c = clients; c; c = c->next) {
1224                 memcpy(c->tags, zerotags, sizeof zerotags);
1225                 applyrules(c);
1226         }
1227         arrange();
1228 }
1229
1230 void
1231 resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
1232         XWindowChanges wc;
1233         Monitor scr = monitors[monitorat(x, y)];
1234         c->monitor = monitorat(x, y);
1235
1236         if(sizehints) {
1237                 /* set minimum possible */
1238                 if (w < 1)
1239                         w = 1;
1240                 if (h < 1)
1241                         h = 1;
1242
1243                 /* temporarily remove base dimensions */
1244                 w -= c->basew;
1245                 h -= c->baseh;
1246
1247                 /* adjust for aspect limits */
1248                 if (c->minay > 0 && c->maxay > 0 && c->minax > 0 && c->maxax > 0) {
1249                         if (w * c->maxay > h * c->maxax)
1250                                 w = h * c->maxax / c->maxay;
1251                         else if (w * c->minay < h * c->minax)
1252                                 h = w * c->minay / c->minax;
1253                 }
1254
1255                 /* adjust for increment value */
1256                 if(c->incw)
1257                         w -= w % c->incw;
1258                 if(c->inch)
1259                         h -= h % c->inch;
1260
1261                 /* restore base dimensions */
1262                 w += c->basew;
1263                 h += c->baseh;
1264
1265                 if(c->minw > 0 && w < c->minw)
1266                         w = c->minw;
1267                 if(c->minh > 0 && h < c->minh)
1268                         h = c->minh;
1269                 if(c->maxw > 0 && w > c->maxw)
1270                         w = c->maxw;
1271                 if(c->maxh > 0 && h > c->maxh)
1272                         h = c->maxh;
1273         }
1274         if(w <= 0 || h <= 0)
1275                 return;
1276         /* TODO: offscreen appearance fixes */
1277         /*
1278         if(x > scr.sw)
1279                 x = scr.sw - w - 2 * c->border;
1280         if(y > scr.sh)
1281                 y = scr.sh - h - 2 * c->border;
1282         if(x + w + 2 * c->border < scr.sx)
1283                 x = scr.sx;
1284         if(y + h + 2 * c->border < scr.sy)
1285                 y = scr.sy;
1286         */
1287         if(c->x != x || c->y != y || c->w != w || c->h != h) {
1288                 c->x = wc.x = x;
1289                 c->y = wc.y = y;
1290                 c->w = wc.width = w;
1291                 c->h = wc.height = h;
1292                 wc.border_width = c->border;
1293                 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
1294                 configure(c);
1295                 XSync(dpy, False);
1296         }
1297 }
1298
1299 void
1300 resizemouse(Client *c) {
1301         int ocx, ocy;
1302         int nw, nh;
1303         XEvent ev;
1304
1305         ocx = c->x;
1306         ocy = c->y;
1307         if(XGrabPointer(dpy, monitors[selmonitor].root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1308                         None, cursor[CurResize], CurrentTime) != GrabSuccess)
1309                 return;
1310         XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
1311         for(;;) {
1312                 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
1313                 switch(ev.type) {
1314                 case ButtonRelease:
1315                         XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
1316                                         c->w + c->border - 1, c->h + c->border - 1);
1317                         XUngrabPointer(dpy, CurrentTime);
1318                         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1319                         return;
1320                 case ConfigureRequest:
1321                 case Expose:
1322                 case MapRequest:
1323                         handler[ev.type](&ev);
1324                         break;
1325                 case MotionNotify:
1326                         XSync(dpy, False);
1327                         if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
1328                                 nw = 1;
1329                         if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
1330                                 nh = 1;
1331                         resize(c, c->x, c->y, nw, nh, True);
1332                         break;
1333                 }
1334         }
1335 }
1336
1337 void
1338 restack(void) {
1339         unsigned int i;
1340         Client *c;
1341         XEvent ev;
1342         XWindowChanges wc;
1343
1344         drawbar();
1345         if(!sel)
1346                 return;
1347         if(sel->isfloating || (monitors[selmonitor].layout->arrange == floating))
1348                 XRaiseWindow(dpy, sel->win);
1349         if(monitors[selmonitor].layout->arrange != floating) {
1350                 wc.stack_mode = Below;
1351                 wc.sibling = monitors[selmonitor].barwin;
1352                 if(!sel->isfloating) {
1353                         XConfigureWindow(dpy, sel->win, CWSibling | CWStackMode, &wc);
1354                         wc.sibling = sel->win;
1355                 }
1356                 for(i = 0; i < mcount; i++) {
1357                         for(c = nexttiled(clients, &monitors[i]); c; c = nexttiled(c->next, &monitors[i])) {
1358                                 if(c == sel)
1359                                         continue;
1360                                 XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc);
1361                                 wc.sibling = c->win;
1362                         }
1363                 }
1364         }
1365         XSync(dpy, False);
1366         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1367 }
1368
1369 void
1370 run(void) {
1371         char *p;
1372         char buf[sizeof stext];
1373         fd_set rd;
1374         int r, xfd;
1375         unsigned int len, offset;
1376         XEvent ev;
1377
1378         /* main event loop, also reads status text from stdin */
1379         XSync(dpy, False);
1380         xfd = ConnectionNumber(dpy);
1381         readin = True;
1382         offset = 0;
1383         len = sizeof stext - 1;
1384         buf[len] = stext[len] = '\0'; /* 0-terminator is never touched */
1385         while(running) {
1386                 FD_ZERO(&rd);
1387                 if(readin)
1388                         FD_SET(STDIN_FILENO, &rd);
1389                 FD_SET(xfd, &rd);
1390                 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
1391                         if(errno == EINTR)
1392                                 continue;
1393                         eprint("select failed\n");
1394                 }
1395                 if(FD_ISSET(STDIN_FILENO, &rd)) {
1396                         switch((r = read(STDIN_FILENO, buf + offset, len - offset))) {
1397                         case -1:
1398                                 strncpy(stext, strerror(errno), len);
1399                                 readin = False;
1400                                 break;
1401                         case 0:
1402                                 strncpy(stext, "EOF", 4);
1403                                 readin = False;
1404                                 break;
1405                         default:
1406                                 for(p = buf + offset; r > 0; p++, r--, offset++)
1407                                         if(*p == '\n' || *p == '\0') {
1408                                                 *p = '\0';
1409                                                 strncpy(stext, buf, len);
1410                                                 p += r - 1; /* p is buf + offset + r - 1 */
1411                                                 for(r = 0; *(p - r) && *(p - r) != '\n'; r++);
1412                                                 offset = r;
1413                                                 if(r)
1414                                                         memmove(buf, p - r + 1, r);
1415                                                 break;
1416                                         }
1417                                 break;
1418                         }
1419                         drawbar();
1420                 }
1421                 while(XPending(dpy)) {
1422                         XNextEvent(dpy, &ev);
1423                         if(handler[ev.type])
1424                                 (handler[ev.type])(&ev); /* call handler */
1425                 }
1426         }
1427 }
1428
1429 void
1430 scan(void) {
1431         unsigned int i, j, num;
1432         Window *wins, d1, d2;
1433         XWindowAttributes wa;
1434
1435         for(i = 0; i < mcount; i++) {
1436                 Monitor *m = &monitors[i];
1437                 wins = NULL;
1438                 if(XQueryTree(dpy, m->root, &d1, &d2, &wins, &num)) {
1439                         for(j = 0; j < num; j++) {
1440                                 if(!XGetWindowAttributes(dpy, wins[j], &wa)
1441                                 || wa.override_redirect || XGetTransientForHint(dpy, wins[j], &d1))
1442                                         continue;
1443                                 if(wa.map_state == IsViewable || getstate(wins[j]) == IconicState)
1444                                         manage(wins[j], &wa);
1445                         }
1446                         for(j = 0; j < num; j++) { /* now the transients */
1447                                 if(!XGetWindowAttributes(dpy, wins[j], &wa))
1448                                         continue;
1449                                 if(XGetTransientForHint(dpy, wins[j], &d1)
1450                                 && (wa.map_state == IsViewable || getstate(wins[j]) == IconicState))
1451                                         manage(wins[j], &wa);
1452                         }
1453                 }
1454                 if(wins)
1455                         XFree(wins);
1456         }
1457 }
1458
1459 void
1460 setclientstate(Client *c, long state) {
1461         long data[] = {state, None};
1462
1463         XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1464                         PropModeReplace, (unsigned char *)data, 2);
1465 }
1466
1467 void
1468 setlayout(const char *arg) {
1469         unsigned int i;
1470         Monitor *m = &monitors[monitorat(-1, -1)];
1471
1472         if(!arg) {
1473                 m->layout++;
1474                 if(m->layout == &layouts[LENGTH(layouts)])
1475                         m->layout = &layouts[0];
1476         }
1477         else {
1478                 for(i = 0; i < LENGTH(layouts); i++)
1479                         if(!strcmp(arg, layouts[i].symbol))
1480                                 break;
1481                 if(i == LENGTH(layouts))
1482                         return;
1483                 m->layout = &layouts[i];
1484         }
1485         if(sel)
1486                 arrange();
1487         else
1488                 drawbar();
1489 }
1490
1491 void
1492 setmwfact(const char *arg) {
1493         double delta;
1494
1495         Monitor *m = &monitors[monitorat(-1, -1)];
1496
1497         if(!domwfact)
1498                 return;
1499         /* arg handling, manipulate mwfact */
1500         if(arg == NULL)
1501                 m->mwfact = MWFACT;
1502         else if(sscanf(arg, "%lf", &delta) == 1) {
1503                 if(arg[0] == '+' || arg[0] == '-')
1504                         m->mwfact += delta;
1505                 else
1506                         m->mwfact = delta;
1507                 if(m->mwfact < 0.1)
1508                         m->mwfact = 0.1;
1509                 else if(m->mwfact > 0.9)
1510                         m->mwfact = 0.9;
1511         }
1512         arrange();
1513 }
1514
1515 void
1516 setup(void) {
1517         unsigned int i, j, k;
1518         Monitor *m;
1519         XSetWindowAttributes wa;
1520         XineramaScreenInfo *info = NULL;
1521
1522         /* init atoms */
1523         wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1524         wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1525         wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
1526         wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1527         netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1528         netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1529
1530         /* init cursors */
1531         wa.cursor = cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1532         cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1533         cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1534
1535         // init screens/monitors first
1536         if (XineramaIsActive(dpy)) {
1537                 info = XineramaQueryScreens(dpy, &mcount);
1538         }
1539         mcount = 1;
1540         monitors = emallocz(mcount * sizeof(Monitor));
1541
1542         for(i = 0; i < mcount; i++) {
1543                 /* init geometry */
1544                 m = &monitors[i];
1545
1546                 m->screen = i;
1547                 m->root = RootWindow(dpy, i);
1548
1549                 if (mcount != 1) { // TODO: Xinerama
1550                         m->sx = info[i].x_org;
1551                         m->sy = info[i].y_org;
1552                         m->sw = info[i].width;
1553                         m->sh = info[i].height;
1554                 }
1555                 else {
1556                         m->sx = 0;
1557                         m->sy = 0;
1558                         m->sw = DisplayWidth(dpy, m->screen);
1559                         m->sh = DisplayHeight(dpy, m->screen);
1560                 }
1561
1562                 m->seltags = emallocz(sizeof initags);
1563                 m->prevtags = emallocz(sizeof initags);
1564
1565                 memcpy(m->seltags, initags, sizeof initags);
1566                 memcpy(m->prevtags, initags, sizeof initags);
1567
1568                 /* init appearance */
1569                 m->dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR, i);
1570                 m->dc.norm[ColBG] = getcolor(NORMBGCOLOR, i);
1571                 m->dc.norm[ColFG] = getcolor(NORMFGCOLOR, i);
1572                 m->dc.sel[ColBorder] = getcolor(SELBORDERCOLOR, i);
1573                 m->dc.sel[ColBG] = getcolor(SELBGCOLOR, i);
1574                 m->dc.sel[ColFG] = getcolor(SELFGCOLOR, i);
1575                 initfont(m, FONT);
1576                 m->dc.h = bh = m->dc.font.height + 2;
1577
1578                 /* init layouts */
1579                 m->mwfact = MWFACT;
1580                 m->layout = &layouts[0];
1581                 for(blw = k = 0; k < LENGTH(layouts); k++) {
1582                         j = textw(m, layouts[k].symbol);
1583                         if(j > blw)
1584                                 blw = j;
1585                 }
1586
1587                 // TODO: bpos per screen?
1588                 bpos = BARPOS;
1589                 wa.override_redirect = 1;
1590                 wa.background_pixmap = ParentRelative;
1591                 wa.event_mask = ButtonPressMask | ExposureMask;
1592
1593                 /* init bars */
1594                 m->barwin = XCreateWindow(dpy, m->root, m->sx, m->sy, m->sw, bh, 0,
1595                                 DefaultDepth(dpy, m->screen), CopyFromParent, DefaultVisual(dpy, m->screen),
1596                                 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
1597                 XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
1598                 updatebarpos(m);
1599                 XMapRaised(dpy, m->barwin);
1600                 strcpy(stext, "dwm-"VERSION);
1601                 m->dc.drawable = XCreatePixmap(dpy, m->root, m->sw, bh, DefaultDepth(dpy, m->screen));
1602                 m->dc.gc = XCreateGC(dpy, m->root, 0, 0);
1603                 XSetLineAttributes(dpy, m->dc.gc, 1, LineSolid, CapButt, JoinMiter);
1604                 if(!m->dc.font.set)
1605                         XSetFont(dpy, m->dc.gc, m->dc.font.xfont->fid);
1606
1607                 /* EWMH support per monitor */
1608                 XChangeProperty(dpy, m->root, netatom[NetSupported], XA_ATOM, 32,
1609                                 PropModeReplace, (unsigned char *) netatom, NetLast);
1610
1611                 /* select for events */
1612                 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
1613                                 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
1614                 XChangeWindowAttributes(dpy, m->root, CWEventMask | CWCursor, &wa);
1615                 XSelectInput(dpy, m->root, wa.event_mask);
1616         }
1617
1618         /* grab keys */
1619         grabkeys();
1620
1621         /* init tags */
1622         compileregs();
1623 }
1624
1625 void
1626 spawn(const char *arg) {
1627         static char *shell = NULL;
1628
1629         if(!shell && !(shell = getenv("SHELL")))
1630                 shell = "/bin/sh";
1631         if(!arg)
1632                 return;
1633         /* The double-fork construct avoids zombie processes and keeps the code
1634          * clean from stupid signal handlers. */
1635         if(fork() == 0) {
1636                 if(fork() == 0) {
1637                         if(dpy)
1638                                 close(ConnectionNumber(dpy));
1639                         setsid();
1640                         execl(shell, shell, "-c", arg, (char *)NULL);
1641                         fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
1642                         perror(" failed");
1643                 }
1644                 exit(0);
1645         }
1646         wait(0);
1647 }
1648
1649 void
1650 tag(const char *arg) {
1651         unsigned int i;
1652
1653         if(!sel)
1654                 return;
1655         for(i = 0; i < LENGTH(tags); i++)
1656                 sel->tags[i] = (NULL == arg);
1657         sel->tags[idxoftag(arg)] = True;
1658         arrange();
1659 }
1660
1661 unsigned int
1662 textnw(Monitor *m, const char *text, unsigned int len) {
1663         XRectangle r;
1664
1665         if(m->dc.font.set) {
1666                 XmbTextExtents(m->dc.font.set, text, len, NULL, &r);
1667                 return r.width;
1668         }
1669         return XTextWidth(m->dc.font.xfont, text, len);
1670 }
1671
1672 unsigned int
1673 textw(Monitor *m, const char *text) {
1674         return textnw(m, text, strlen(text)) + m->dc.font.height;
1675 }
1676
1677 void
1678 tile(void) {
1679         unsigned int i, j, n, nx, ny, nw, nh, mw, th;
1680         Client *c, *mc;
1681
1682         domwfact = dozoom = True;
1683
1684         nw = 0; /* gcc stupidity requires this */
1685
1686         for (i = 0; i < mcount; i++) {
1687                 Monitor *m = &monitors[i];
1688
1689                 for(n = 0, c = nexttiled(clients, m); c; c = nexttiled(c->next, m))
1690                         n++;
1691
1692                 for(j = 0, c = mc = nexttiled(clients, m); c; c = nexttiled(c->next, m)) {
1693                         /* window geoms */
1694                         mw = (n == 1) ? m->waw : m->mwfact * m->waw;
1695                         th = (n > 1) ? m->wah / (n - 1) : 0;
1696                         if(n > 1 && th < bh)
1697                                 th = m->wah;
1698                         if(j == 0) { /* master */
1699                                 nx = m->wax;
1700                                 ny = m->way;
1701                                 nw = mw - 2 * c->border;
1702                                 nh = m->wah - 2 * c->border;
1703                         }
1704                         else {  /* tile window */
1705                                 if(j == 1) {
1706                                         ny = m->way;
1707                                         nx += mc->w + 2 * mc->border;
1708                                         nw = m->waw - mw - 2 * c->border;
1709                                 }
1710                                 if(j + 1 == n) /* remainder */
1711                                         nh = (m->way + m->wah) - ny - 2 * c->border;
1712                                 else
1713                                         nh = th - 2 * c->border;
1714                         }
1715                         resize(c, nx, ny, nw, nh, RESIZEHINTS);
1716                         if((RESIZEHINTS) && ((c->h < bh) || (c->h > nh) || (c->w < bh) || (c->w > nw)))
1717                                 /* client doesn't accept size constraints */
1718                                 resize(c, nx, ny, nw, nh, False);
1719                         if(n > 1 && th != m->wah)
1720                                 ny = c->y + c->h + 2 * c->border;
1721
1722                         j++;
1723                 }
1724         }
1725 }
1726 void
1727 togglebar(const char *arg) {
1728         if(bpos == BarOff)
1729                 bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
1730         else
1731                 bpos = BarOff;
1732         updatebarpos(&monitors[monitorat(-1,-1)]);
1733         arrange();
1734 }
1735
1736 void
1737 togglefloating(const char *arg) {
1738         if(!sel)
1739                 return;
1740         sel->isfloating = !sel->isfloating;
1741         if(sel->isfloating)
1742                 resize(sel, sel->x, sel->y, sel->w, sel->h, True);
1743         arrange();
1744 }
1745
1746 void
1747 toggletag(const char *arg) {
1748         unsigned int i, j;
1749
1750         if(!sel)
1751                 return;
1752         i = idxoftag(arg);
1753         sel->tags[i] = !sel->tags[i];
1754         for(j = 0; j < LENGTH(tags) && !sel->tags[j]; j++);
1755         if(j == LENGTH(tags))
1756                 sel->tags[i] = True; /* at least one tag must be enabled */
1757         arrange();
1758 }
1759
1760 void
1761 toggleview(const char *arg) {
1762         unsigned int i, j;
1763
1764         Monitor *m = &monitors[monitorat(-1, -1)];
1765
1766         i = idxoftag(arg);
1767         m->seltags[i] = !m->seltags[i];
1768         for(j = 0; j < LENGTH(tags) && !m->seltags[j]; j++);
1769         if(j == LENGTH(tags))
1770                 m->seltags[i] = True; /* at least one tag must be viewed */
1771         arrange();
1772 }
1773
1774 void
1775 unban(Client *c) {
1776         if(!c->isbanned)
1777                 return;
1778         XMoveWindow(dpy, c->win, c->x, c->y);
1779         c->isbanned = False;
1780 }
1781
1782 void
1783 unmanage(Client *c) {
1784         XWindowChanges wc;
1785
1786         wc.border_width = c->oldborder;
1787         /* The server grab construct avoids race conditions. */
1788         XGrabServer(dpy);
1789         XSetErrorHandler(xerrordummy);
1790         XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1791         detach(c);
1792         detachstack(c);
1793         if(sel == c)
1794                 focus(NULL);
1795         XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1796         setclientstate(c, WithdrawnState);
1797         free(c->tags);
1798         free(c);
1799         XSync(dpy, False);
1800         XSetErrorHandler(xerror);
1801         XUngrabServer(dpy);
1802         arrange();
1803 }
1804
1805 void
1806 unmapnotify(XEvent *e) {
1807         Client *c;
1808         XUnmapEvent *ev = &e->xunmap;
1809
1810         if((c = getclient(ev->window)))
1811                 unmanage(c);
1812 }
1813
1814 void
1815 updatebarpos(Monitor *m) {
1816         XEvent ev;
1817
1818         m->wax = m->sx;
1819         m->way = m->sy;
1820         m->wah = m->sh;
1821         m->waw = m->sw;
1822         switch(bpos) {
1823         default:
1824                 m->wah -= bh;
1825                 m->way += bh;
1826                 XMoveWindow(dpy, m->barwin, m->sx, m->sy);
1827                 break;
1828         case BarBot:
1829                 m->wah -= bh;
1830                 XMoveWindow(dpy, m->barwin, m->sx, m->sy + m->wah);
1831                 break;
1832         case BarOff:
1833                 XMoveWindow(dpy, m->barwin, m->sx, m->sy - bh);
1834                 break;
1835         }
1836         XSync(dpy, False);
1837         while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1838 }
1839
1840 void
1841 updatesizehints(Client *c) {
1842         long msize;
1843         XSizeHints size;
1844
1845         if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
1846                 size.flags = PSize;
1847         c->flags = size.flags;
1848         if(c->flags & PBaseSize) {
1849                 c->basew = size.base_width;
1850                 c->baseh = size.base_height;
1851         }
1852         else if(c->flags & PMinSize) {
1853                 c->basew = size.min_width;
1854                 c->baseh = size.min_height;
1855         }
1856         else
1857                 c->basew = c->baseh = 0;
1858         if(c->flags & PResizeInc) {
1859                 c->incw = size.width_inc;
1860                 c->inch = size.height_inc;
1861         }
1862         else
1863                 c->incw = c->inch = 0;
1864         if(c->flags & PMaxSize) {
1865                 c->maxw = size.max_width;
1866                 c->maxh = size.max_height;
1867         }
1868         else
1869                 c->maxw = c->maxh = 0;
1870         if(c->flags & PMinSize) {
1871                 c->minw = size.min_width;
1872                 c->minh = size.min_height;
1873         }
1874         else if(c->flags & PBaseSize) {
1875                 c->minw = size.base_width;
1876                 c->minh = size.base_height;
1877         }
1878         else
1879                 c->minw = c->minh = 0;
1880         if(c->flags & PAspect) {
1881                 c->minax = size.min_aspect.x;
1882                 c->maxax = size.max_aspect.x;
1883                 c->minay = size.min_aspect.y;
1884                 c->maxay = size.max_aspect.y;
1885         }
1886         else
1887                 c->minax = c->maxax = c->minay = c->maxay = 0;
1888         c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
1889                         && c->maxw == c->minw && c->maxh == c->minh);
1890 }
1891
1892 void
1893 updatetitle(Client *c) {
1894         if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
1895                 gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
1896 }
1897
1898 /* There's no way to check accesses to destroyed windows, thus those cases are
1899  * ignored (especially on UnmapNotify's).  Other types of errors call Xlibs
1900  * default error handler, which may call exit.  */
1901 int
1902 xerror(Display *dpy, XErrorEvent *ee) {
1903         if(ee->error_code == BadWindow
1904         || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
1905         || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
1906         || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
1907         || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
1908         || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
1909         || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
1910         || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
1911                 return 0;
1912         fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
1913                 ee->request_code, ee->error_code);
1914         return xerrorxlib(dpy, ee); /* may call exit */
1915 }
1916
1917 int
1918 xerrordummy(Display *dsply, XErrorEvent *ee) {
1919         return 0;
1920 }
1921
1922 /* Startup Error handler to check if another window manager
1923  * is already running. */
1924 int
1925 xerrorstart(Display *dsply, XErrorEvent *ee) {
1926         otherwm = True;
1927         return -1;
1928 }
1929
1930 void
1931 view(const char *arg) {
1932         unsigned int i;
1933
1934         Monitor *m = &monitors[monitorat(-1, -1)];
1935
1936         memcpy(m->prevtags, m->seltags, sizeof initags);
1937         for(i = 0; i < LENGTH(tags); i++)
1938                 m->seltags[i] = (NULL == arg);
1939         m->seltags[idxoftag(arg)] = True;
1940         arrange();
1941 }
1942
1943 void
1944 viewprevtag(const char *arg) {
1945         static Bool tmp[LENGTH(tags)];
1946
1947         Monitor *m = &monitors[monitorat(-1, -1)];
1948
1949         memcpy(tmp, m->seltags, sizeof initags);
1950         memcpy(m->seltags, m->prevtags, sizeof initags);
1951         memcpy(m->prevtags, tmp, sizeof initags);
1952         arrange();
1953 }
1954
1955 void
1956 zoom(const char *arg) {
1957         Client *c;
1958
1959         if(!sel || !dozoom || sel->isfloating)
1960                 return;
1961         if((c = sel) == nexttiled(clients, &monitors[c->monitor]))
1962                 if(!(c = nexttiled(c->next, &monitors[c->monitor])))
1963                         return;
1964         detach(c);
1965         attach(c);
1966         focus(c);
1967         arrange();
1968 }
1969
1970 int
1971 monitorat(int x, int y) {
1972         int i;
1973
1974         return 0;
1975         if(!XineramaIsActive(dpy))
1976                 return 0;
1977
1978         if (x < 0 || y < 0) {
1979                 Window win;
1980                 unsigned int mask;
1981                 XQueryPointer(dpy, DefaultRootWindow(dpy), &win, &win, &x, &y, &i, &i, &mask);
1982         }
1983
1984         for(i = 0; i < mcount; i++) {
1985                 Monitor *m = &monitors[i];
1986                 if((x < 0 || (x >= m->sx && x < m->sx + m->sw))
1987                                 && (y < 0 || (y >= m->sy && y < m->sy + m->sh)))
1988                 {
1989                         return i;
1990                 }
1991         }
1992         return 0;
1993 }
1994
1995 void
1996 movetomonitor(const char *arg) {
1997         if (sel) {
1998                 sel->monitor = arg ? atoi(arg) : (sel->monitor+1) % mcount;
1999
2000                 memcpy(sel->tags, monitors[sel->monitor].seltags, sizeof initags);
2001                 resize(sel, monitors[sel->monitor].wax, monitors[sel->monitor].way, sel->w, sel->h, True);
2002                 arrange();
2003         }
2004 }
2005
2006 void
2007 selectmonitor(const char *arg) {
2008         Monitor *m = &monitors[arg ? atoi(arg) : (monitorat(-1, -1)+1) % mcount];
2009
2010         XWarpPointer(dpy, None, m->root, 0, 0, 0, 0, m->wax+m->waw/2, m->way+m->wah/2);
2011         focus(NULL);
2012 }
2013
2014
2015 int
2016 main(int argc, char *argv[]) {
2017         if(argc == 2 && !strcmp("-v", argv[1]))
2018                 eprint("dwm-"VERSION", © 2006-2007 Anselm R. Garbe, Sander van Dijk, "
2019                        "Jukka Salmi, Premysl Hruby, Szabolcs Nagy, Christof Musik\n");
2020         else if(argc != 1)
2021                 eprint("usage: dwm [-v]\n");
2022
2023         setlocale(LC_CTYPE, "");
2024         if(!(dpy = XOpenDisplay(0)))
2025                 eprint("dwm: cannot open display\n");
2026
2027         checkotherwm();
2028         setup();
2029         drawbar();
2030         scan();
2031         run();
2032         cleanup();
2033
2034         XCloseDisplay(dpy);
2035         return 0;
2036 }