Make floating windows spawn within the monitor's window area
[dwm.git] / dwm.c
blobf2bc4ba944984ac68464c8e4e2bf722257b0de74
1 /* See LICENSE file for copyright and license details.
3 * dynamic window manager is designed like any other X client as well. It is
4 * driven through handling X events. In contrast to other X clients, a window
5 * manager selects for SubstructureRedirectMask on the root window, to receive
6 * events about window (dis-)appearance. Only one X connection at a time is
7 * allowed to select for this event mask.
9 * The event handlers of dwm are organized in an array which is accessed
10 * whenever a new event has been fetched. This allows event dispatching
11 * in O(1) time.
13 * Each child of the root window is called a client, except windows which have
14 * set the override_redirect flag. Clients are organized in a linked client
15 * list on each monitor, the focus history is remembered through a stack list
16 * on each monitor. Each client contains a bit array to indicate the tags of a
17 * client.
19 * Keys and tagging rules are organized as arrays and defined in config.h.
21 * To understand everything else, start reading main().
23 #include <errno.h>
24 #include <locale.h>
25 #include <signal.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <X11/cursorfont.h>
34 #include <X11/keysym.h>
35 #include <X11/Xatom.h>
36 #include <X11/Xlib.h>
37 #include <X11/Xproto.h>
38 #include <X11/Xutil.h>
39 #ifdef XINERAMA
40 #include <X11/extensions/Xinerama.h>
41 #endif /* XINERAMA */
42 #include <X11/Xft/Xft.h>
44 #include "drw.h"
45 #include "util.h"
47 /* macros */
48 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
49 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
50 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
51 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
52 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
53 #define LENGTH(X) (sizeof X / sizeof X[0])
54 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
55 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
56 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
57 #define TAGMASK ((1 << LENGTH(tags)) - 1)
58 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
60 /* enums */
61 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
62 enum { SchemeNorm, SchemeSel }; /* color schemes */
63 enum { NetSupported, NetWMName, NetWMState, NetWMCheck,
64 NetWMFullscreen, NetActiveWindow, NetWMWindowType,
65 NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */
66 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
67 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
68 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
70 typedef union {
71 int i;
72 unsigned int ui;
73 float f;
74 const void *v;
75 } Arg;
77 typedef struct {
78 unsigned int click;
79 unsigned int mask;
80 unsigned int button;
81 void (*func)(const Arg *arg);
82 const Arg arg;
83 } Button;
85 typedef struct Monitor Monitor;
86 typedef struct Client Client;
87 struct Client {
88 char name[256];
89 float mina, maxa;
90 int x, y, w, h;
91 int oldx, oldy, oldw, oldh;
92 int basew, baseh, incw, inch, maxw, maxh, minw, minh, hintsvalid;
93 int bw, oldbw;
94 unsigned int tags;
95 int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
96 Client *next;
97 Client *snext;
98 Monitor *mon;
99 Window win;
102 typedef struct {
103 unsigned int mod;
104 KeySym keysym;
105 void (*func)(const Arg *);
106 const Arg arg;
107 } Key;
109 typedef struct {
110 const char *symbol;
111 void (*arrange)(Monitor *);
112 } Layout;
114 struct Monitor {
115 char ltsymbol[16];
116 float mfact;
117 int nmaster;
118 int num;
119 int by; /* bar geometry */
120 int mx, my, mw, mh; /* screen size */
121 int wx, wy, ww, wh; /* window area */
122 unsigned int seltags;
123 unsigned int sellt;
124 unsigned int tagset[2];
125 int showbar;
126 int topbar;
127 Client *clients;
128 Client *sel;
129 Client *stack;
130 Monitor *next;
131 Window barwin;
132 const Layout *lt[2];
135 typedef struct {
136 const char *class;
137 const char *instance;
138 const char *title;
139 unsigned int tags;
140 int isfloating;
141 int monitor;
142 } Rule;
144 /* function declarations */
145 static void applyrules(Client *c);
146 static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact);
147 static void arrange(Monitor *m);
148 static void arrangemon(Monitor *m);
149 static void attach(Client *c);
150 static void attachstack(Client *c);
151 static void buttonpress(XEvent *e);
152 static void checkotherwm(void);
153 static void cleanup(void);
154 static void cleanupmon(Monitor *mon);
155 static void clientmessage(XEvent *e);
156 static void configure(Client *c);
157 static void configurenotify(XEvent *e);
158 static void configurerequest(XEvent *e);
159 static Monitor *createmon(void);
160 static void destroynotify(XEvent *e);
161 static void detach(Client *c);
162 static void detachstack(Client *c);
163 static Monitor *dirtomon(int dir);
164 static void drawbar(Monitor *m);
165 static void drawbars(void);
166 static void enternotify(XEvent *e);
167 static void expose(XEvent *e);
168 static void focus(Client *c);
169 static void focusin(XEvent *e);
170 static void focusmon(const Arg *arg);
171 static void focusstack(const Arg *arg);
172 static Atom getatomprop(Client *c, Atom prop);
173 static int getrootptr(int *x, int *y);
174 static long getstate(Window w);
175 static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
176 static void grabbuttons(Client *c, int focused);
177 static void grabkeys(void);
178 static void incnmaster(const Arg *arg);
179 static void keypress(XEvent *e);
180 static void killclient(const Arg *arg);
181 static void manage(Window w, XWindowAttributes *wa);
182 static void mappingnotify(XEvent *e);
183 static void maprequest(XEvent *e);
184 static void monocle(Monitor *m);
185 static void motionnotify(XEvent *e);
186 static void movemouse(const Arg *arg);
187 static Client *nexttiled(Client *c);
188 static void pop(Client *c);
189 static void propertynotify(XEvent *e);
190 static void quit(const Arg *arg);
191 static Monitor *recttomon(int x, int y, int w, int h);
192 static void resize(Client *c, int x, int y, int w, int h, int interact);
193 static void resizeclient(Client *c, int x, int y, int w, int h);
194 static void resizemouse(const Arg *arg);
195 static void restack(Monitor *m);
196 static void run(void);
197 static void scan(void);
198 static int sendevent(Client *c, Atom proto);
199 static void sendmon(Client *c, Monitor *m);
200 static void setclientstate(Client *c, long state);
201 static void setfocus(Client *c);
202 static void setfullscreen(Client *c, int fullscreen);
203 static void setlayout(const Arg *arg);
204 static void setmfact(const Arg *arg);
205 static void setup(void);
206 static void seturgent(Client *c, int urg);
207 static void showhide(Client *c);
208 static void sigchld(int unused);
209 static void spawn(const Arg *arg);
210 static void tag(const Arg *arg);
211 static void tagmon(const Arg *arg);
212 static void tile(Monitor *m);
213 static void togglebar(const Arg *arg);
214 static void togglefloating(const Arg *arg);
215 static void toggletag(const Arg *arg);
216 static void toggleview(const Arg *arg);
217 static void unfocus(Client *c, int setfocus);
218 static void unmanage(Client *c, int destroyed);
219 static void unmapnotify(XEvent *e);
220 static void updatebarpos(Monitor *m);
221 static void updatebars(void);
222 static void updateclientlist(void);
223 static int updategeom(void);
224 static void updatenumlockmask(void);
225 static void updatesizehints(Client *c);
226 static void updatestatus(void);
227 static void updatetitle(Client *c);
228 static void updatewindowtype(Client *c);
229 static void updatewmhints(Client *c);
230 static void view(const Arg *arg);
231 static Client *wintoclient(Window w);
232 static Monitor *wintomon(Window w);
233 static int xerror(Display *dpy, XErrorEvent *ee);
234 static int xerrordummy(Display *dpy, XErrorEvent *ee);
235 static int xerrorstart(Display *dpy, XErrorEvent *ee);
236 static void zoom(const Arg *arg);
238 /* variables */
239 static const char broken[] = "broken";
240 static char stext[256];
241 static int screen;
242 static int sw, sh; /* X display screen geometry width, height */
243 static int bh, blw = 0; /* bar geometry */
244 static int lrpad; /* sum of left and right padding for text */
245 static int (*xerrorxlib)(Display *, XErrorEvent *);
246 static unsigned int numlockmask = 0;
247 static void (*handler[LASTEvent]) (XEvent *) = {
248 [ButtonPress] = buttonpress,
249 [ClientMessage] = clientmessage,
250 [ConfigureRequest] = configurerequest,
251 [ConfigureNotify] = configurenotify,
252 [DestroyNotify] = destroynotify,
253 [EnterNotify] = enternotify,
254 [Expose] = expose,
255 [FocusIn] = focusin,
256 [KeyPress] = keypress,
257 [MappingNotify] = mappingnotify,
258 [MapRequest] = maprequest,
259 [MotionNotify] = motionnotify,
260 [PropertyNotify] = propertynotify,
261 [UnmapNotify] = unmapnotify
263 static Atom wmatom[WMLast], netatom[NetLast];
264 static int running = 1;
265 static Cur *cursor[CurLast];
266 static Clr **scheme;
267 static Display *dpy;
268 static Drw *drw;
269 static Monitor *mons, *selmon;
270 static Window root, wmcheckwin;
272 /* configuration, allows nested code to access above variables */
273 #include "config.h"
275 /* compile-time check if all tags fit into an unsigned int bit array. */
276 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
278 /* function implementations */
279 void
280 applyrules(Client *c)
282 const char *class, *instance;
283 unsigned int i;
284 const Rule *r;
285 Monitor *m;
286 XClassHint ch = { NULL, NULL };
288 /* rule matching */
289 c->isfloating = 0;
290 c->tags = 0;
291 XGetClassHint(dpy, c->win, &ch);
292 class = ch.res_class ? ch.res_class : broken;
293 instance = ch.res_name ? ch.res_name : broken;
295 for (i = 0; i < LENGTH(rules); i++) {
296 r = &rules[i];
297 if ((!r->title || strstr(c->name, r->title))
298 && (!r->class || strstr(class, r->class))
299 && (!r->instance || strstr(instance, r->instance)))
301 c->isfloating = r->isfloating;
302 c->tags |= r->tags;
303 for (m = mons; m && m->num != r->monitor; m = m->next);
304 if (m)
305 c->mon = m;
308 if (ch.res_class)
309 XFree(ch.res_class);
310 if (ch.res_name)
311 XFree(ch.res_name);
312 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
316 applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact)
318 int baseismin;
319 Monitor *m = c->mon;
321 /* set minimum possible */
322 *w = MAX(1, *w);
323 *h = MAX(1, *h);
324 if (interact) {
325 if (*x > sw)
326 *x = sw - WIDTH(c);
327 if (*y > sh)
328 *y = sh - HEIGHT(c);
329 if (*x + *w + 2 * c->bw < 0)
330 *x = 0;
331 if (*y + *h + 2 * c->bw < 0)
332 *y = 0;
333 } else {
334 if (*x >= m->wx + m->ww)
335 *x = m->wx + m->ww - WIDTH(c);
336 if (*y >= m->wy + m->wh)
337 *y = m->wy + m->wh - HEIGHT(c);
338 if (*x + *w + 2 * c->bw <= m->wx)
339 *x = m->wx;
340 if (*y + *h + 2 * c->bw <= m->wy)
341 *y = m->wy;
343 if (*h < bh)
344 *h = bh;
345 if (*w < bh)
346 *w = bh;
347 if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
348 if (!c->hintsvalid)
349 updatesizehints(c);
350 /* see last two sentences in ICCCM 4.1.2.3 */
351 baseismin = c->basew == c->minw && c->baseh == c->minh;
352 if (!baseismin) { /* temporarily remove base dimensions */
353 *w -= c->basew;
354 *h -= c->baseh;
356 /* adjust for aspect limits */
357 if (c->mina > 0 && c->maxa > 0) {
358 if (c->maxa < (float)*w / *h)
359 *w = *h * c->maxa + 0.5;
360 else if (c->mina < (float)*h / *w)
361 *h = *w * c->mina + 0.5;
363 if (baseismin) { /* increment calculation requires this */
364 *w -= c->basew;
365 *h -= c->baseh;
367 /* adjust for increment value */
368 if (c->incw)
369 *w -= *w % c->incw;
370 if (c->inch)
371 *h -= *h % c->inch;
372 /* restore base dimensions */
373 *w = MAX(*w + c->basew, c->minw);
374 *h = MAX(*h + c->baseh, c->minh);
375 if (c->maxw)
376 *w = MIN(*w, c->maxw);
377 if (c->maxh)
378 *h = MIN(*h, c->maxh);
380 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
383 void
384 arrange(Monitor *m)
386 if (m)
387 showhide(m->stack);
388 else for (m = mons; m; m = m->next)
389 showhide(m->stack);
390 if (m) {
391 arrangemon(m);
392 restack(m);
393 } else for (m = mons; m; m = m->next)
394 arrangemon(m);
397 void
398 arrangemon(Monitor *m)
400 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
401 if (m->lt[m->sellt]->arrange)
402 m->lt[m->sellt]->arrange(m);
405 void
406 attach(Client *c)
408 c->next = c->mon->clients;
409 c->mon->clients = c;
412 void
413 attachstack(Client *c)
415 c->snext = c->mon->stack;
416 c->mon->stack = c;
419 void
420 buttonpress(XEvent *e)
422 unsigned int i, x, click;
423 Arg arg = {0};
424 Client *c;
425 Monitor *m;
426 XButtonPressedEvent *ev = &e->xbutton;
428 click = ClkRootWin;
429 /* focus monitor if necessary */
430 if ((m = wintomon(ev->window)) && m != selmon) {
431 unfocus(selmon->sel, 1);
432 selmon = m;
433 focus(NULL);
435 if (ev->window == selmon->barwin) {
436 i = x = 0;
438 x += TEXTW(tags[i]);
439 while (ev->x >= x && ++i < LENGTH(tags));
440 if (i < LENGTH(tags)) {
441 click = ClkTagBar;
442 arg.ui = 1 << i;
443 } else if (ev->x < x + blw)
444 click = ClkLtSymbol;
445 else if (ev->x > selmon->ww - (int)TEXTW(stext))
446 click = ClkStatusText;
447 else
448 click = ClkWinTitle;
449 } else if ((c = wintoclient(ev->window))) {
450 focus(c);
451 restack(selmon);
452 XAllowEvents(dpy, ReplayPointer, CurrentTime);
453 click = ClkClientWin;
455 for (i = 0; i < LENGTH(buttons); i++)
456 if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
457 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
458 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
461 void
462 checkotherwm(void)
464 xerrorxlib = XSetErrorHandler(xerrorstart);
465 /* this causes an error if some other window manager is running */
466 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
467 XSync(dpy, False);
468 XSetErrorHandler(xerror);
469 XSync(dpy, False);
472 void
473 cleanup(void)
475 Arg a = {.ui = ~0};
476 Layout foo = { "", NULL };
477 Monitor *m;
478 size_t i;
480 view(&a);
481 selmon->lt[selmon->sellt] = &foo;
482 for (m = mons; m; m = m->next)
483 while (m->stack)
484 unmanage(m->stack, 0);
485 XUngrabKey(dpy, AnyKey, AnyModifier, root);
486 while (mons)
487 cleanupmon(mons);
488 for (i = 0; i < CurLast; i++)
489 drw_cur_free(drw, cursor[i]);
490 for (i = 0; i < LENGTH(colors); i++)
491 free(scheme[i]);
492 free(scheme);
493 XDestroyWindow(dpy, wmcheckwin);
494 drw_free(drw);
495 XSync(dpy, False);
496 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
497 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
500 void
501 cleanupmon(Monitor *mon)
503 Monitor *m;
505 if (mon == mons)
506 mons = mons->next;
507 else {
508 for (m = mons; m && m->next != mon; m = m->next);
509 m->next = mon->next;
511 XUnmapWindow(dpy, mon->barwin);
512 XDestroyWindow(dpy, mon->barwin);
513 free(mon);
516 void
517 clientmessage(XEvent *e)
519 XClientMessageEvent *cme = &e->xclient;
520 Client *c = wintoclient(cme->window);
522 if (!c)
523 return;
524 if (cme->message_type == netatom[NetWMState]) {
525 if (cme->data.l[1] == netatom[NetWMFullscreen]
526 || cme->data.l[2] == netatom[NetWMFullscreen])
527 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
528 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
529 } else if (cme->message_type == netatom[NetActiveWindow]) {
530 if (c != selmon->sel && !c->isurgent)
531 seturgent(c, 1);
535 void
536 configure(Client *c)
538 XConfigureEvent ce;
540 ce.type = ConfigureNotify;
541 ce.display = dpy;
542 ce.event = c->win;
543 ce.window = c->win;
544 ce.x = c->x;
545 ce.y = c->y;
546 ce.width = c->w;
547 ce.height = c->h;
548 ce.border_width = c->bw;
549 ce.above = None;
550 ce.override_redirect = False;
551 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
554 void
555 configurenotify(XEvent *e)
557 Monitor *m;
558 Client *c;
559 XConfigureEvent *ev = &e->xconfigure;
560 int dirty;
562 /* TODO: updategeom handling sucks, needs to be simplified */
563 if (ev->window == root) {
564 dirty = (sw != ev->width || sh != ev->height);
565 sw = ev->width;
566 sh = ev->height;
567 if (updategeom() || dirty) {
568 drw_resize(drw, sw, bh);
569 updatebars();
570 for (m = mons; m; m = m->next) {
571 for (c = m->clients; c; c = c->next)
572 if (c->isfullscreen)
573 resizeclient(c, m->mx, m->my, m->mw, m->mh);
574 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
576 focus(NULL);
577 arrange(NULL);
582 void
583 configurerequest(XEvent *e)
585 Client *c;
586 Monitor *m;
587 XConfigureRequestEvent *ev = &e->xconfigurerequest;
588 XWindowChanges wc;
590 if ((c = wintoclient(ev->window))) {
591 if (ev->value_mask & CWBorderWidth)
592 c->bw = ev->border_width;
593 else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
594 m = c->mon;
595 if (ev->value_mask & CWX) {
596 c->oldx = c->x;
597 c->x = m->mx + ev->x;
599 if (ev->value_mask & CWY) {
600 c->oldy = c->y;
601 c->y = m->my + ev->y;
603 if (ev->value_mask & CWWidth) {
604 c->oldw = c->w;
605 c->w = ev->width;
607 if (ev->value_mask & CWHeight) {
608 c->oldh = c->h;
609 c->h = ev->height;
611 if ((c->x + c->w) > m->mx + m->mw && c->isfloating)
612 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
613 if ((c->y + c->h) > m->my + m->mh && c->isfloating)
614 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
615 if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
616 configure(c);
617 if (ISVISIBLE(c))
618 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
619 } else
620 configure(c);
621 } else {
622 wc.x = ev->x;
623 wc.y = ev->y;
624 wc.width = ev->width;
625 wc.height = ev->height;
626 wc.border_width = ev->border_width;
627 wc.sibling = ev->above;
628 wc.stack_mode = ev->detail;
629 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
631 XSync(dpy, False);
634 Monitor *
635 createmon(void)
637 Monitor *m;
639 m = ecalloc(1, sizeof(Monitor));
640 m->tagset[0] = m->tagset[1] = 1;
641 m->mfact = mfact;
642 m->nmaster = nmaster;
643 m->showbar = showbar;
644 m->topbar = topbar;
645 m->lt[0] = &layouts[0];
646 m->lt[1] = &layouts[1 % LENGTH(layouts)];
647 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
648 return m;
651 void
652 destroynotify(XEvent *e)
654 Client *c;
655 XDestroyWindowEvent *ev = &e->xdestroywindow;
657 if ((c = wintoclient(ev->window)))
658 unmanage(c, 1);
661 void
662 detach(Client *c)
664 Client **tc;
666 for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
667 *tc = c->next;
670 void
671 detachstack(Client *c)
673 Client **tc, *t;
675 for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
676 *tc = c->snext;
678 if (c == c->mon->sel) {
679 for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
680 c->mon->sel = t;
684 Monitor *
685 dirtomon(int dir)
687 Monitor *m = NULL;
689 if (dir > 0) {
690 if (!(m = selmon->next))
691 m = mons;
692 } else if (selmon == mons)
693 for (m = mons; m->next; m = m->next);
694 else
695 for (m = mons; m->next != selmon; m = m->next);
696 return m;
699 void
700 drawbar(Monitor *m)
702 int x, w, tw = 0;
703 int boxs = drw->fonts->h / 9;
704 int boxw = drw->fonts->h / 6 + 2;
705 unsigned int i, occ = 0, urg = 0;
706 Client *c;
708 if (!m->showbar)
709 return;
711 /* draw status first so it can be overdrawn by tags later */
712 if (m == selmon) { /* status is only drawn on selected monitor */
713 drw_setscheme(drw, scheme[SchemeNorm]);
714 tw = TEXTW(stext) - lrpad + 2; /* 2px right padding */
715 drw_text(drw, m->ww - tw, 0, tw, bh, 0, stext, 0);
718 for (c = m->clients; c; c = c->next) {
719 occ |= c->tags;
720 if (c->isurgent)
721 urg |= c->tags;
723 x = 0;
724 for (i = 0; i < LENGTH(tags); i++) {
725 w = TEXTW(tags[i]);
726 drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]);
727 drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i);
728 if (occ & 1 << i)
729 drw_rect(drw, x + boxs, boxs, boxw, boxw,
730 m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
731 urg & 1 << i);
732 x += w;
734 w = blw = TEXTW(m->ltsymbol);
735 drw_setscheme(drw, scheme[SchemeNorm]);
736 x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0);
738 if ((w = m->ww - tw - x) > bh) {
739 if (m->sel) {
740 drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]);
741 drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->name, 0);
742 if (m->sel->isfloating)
743 drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
744 } else {
745 drw_setscheme(drw, scheme[SchemeNorm]);
746 drw_rect(drw, x, 0, w, bh, 1, 1);
749 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
752 void
753 drawbars(void)
755 Monitor *m;
757 for (m = mons; m; m = m->next)
758 drawbar(m);
761 void
762 enternotify(XEvent *e)
764 Client *c;
765 Monitor *m;
766 XCrossingEvent *ev = &e->xcrossing;
768 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
769 return;
770 c = wintoclient(ev->window);
771 m = c ? c->mon : wintomon(ev->window);
772 if (m != selmon) {
773 unfocus(selmon->sel, 1);
774 selmon = m;
775 } else if (!c || c == selmon->sel)
776 return;
777 focus(c);
780 void
781 expose(XEvent *e)
783 Monitor *m;
784 XExposeEvent *ev = &e->xexpose;
786 if (ev->count == 0 && (m = wintomon(ev->window)))
787 drawbar(m);
790 void
791 focus(Client *c)
793 if (!c || !ISVISIBLE(c))
794 for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
795 if (selmon->sel && selmon->sel != c)
796 unfocus(selmon->sel, 0);
797 if (c) {
798 if (c->mon != selmon)
799 selmon = c->mon;
800 if (c->isurgent)
801 seturgent(c, 0);
802 detachstack(c);
803 attachstack(c);
804 grabbuttons(c, 1);
805 XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel);
806 setfocus(c);
807 } else {
808 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
809 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
811 selmon->sel = c;
812 drawbars();
815 /* there are some broken focus acquiring clients needing extra handling */
816 void
817 focusin(XEvent *e)
819 XFocusChangeEvent *ev = &e->xfocus;
821 if (selmon->sel && ev->window != selmon->sel->win)
822 setfocus(selmon->sel);
825 void
826 focusmon(const Arg *arg)
828 Monitor *m;
830 if (!mons->next)
831 return;
832 if ((m = dirtomon(arg->i)) == selmon)
833 return;
834 unfocus(selmon->sel, 0);
835 selmon = m;
836 focus(NULL);
839 void
840 focusstack(const Arg *arg)
842 Client *c = NULL, *i;
844 if (!selmon->sel || (selmon->sel->isfullscreen && lockfullscreen))
845 return;
846 if (arg->i > 0) {
847 for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
848 if (!c)
849 for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
850 } else {
851 for (i = selmon->clients; i != selmon->sel; i = i->next)
852 if (ISVISIBLE(i))
853 c = i;
854 if (!c)
855 for (; i; i = i->next)
856 if (ISVISIBLE(i))
857 c = i;
859 if (c) {
860 focus(c);
861 restack(selmon);
865 Atom
866 getatomprop(Client *c, Atom prop)
868 int di;
869 unsigned long dl;
870 unsigned char *p = NULL;
871 Atom da, atom = None;
873 if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
874 &da, &di, &dl, &dl, &p) == Success && p) {
875 atom = *(Atom *)p;
876 XFree(p);
878 return atom;
882 getrootptr(int *x, int *y)
884 int di;
885 unsigned int dui;
886 Window dummy;
888 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
891 long
892 getstate(Window w)
894 int format;
895 long result = -1;
896 unsigned char *p = NULL;
897 unsigned long n, extra;
898 Atom real;
900 if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
901 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
902 return -1;
903 if (n != 0)
904 result = *p;
905 XFree(p);
906 return result;
910 gettextprop(Window w, Atom atom, char *text, unsigned int size)
912 char **list = NULL;
913 int n;
914 XTextProperty name;
916 if (!text || size == 0)
917 return 0;
918 text[0] = '\0';
919 if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems)
920 return 0;
921 if (name.encoding == XA_STRING) {
922 strncpy(text, (char *)name.value, size - 1);
923 } else if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
924 strncpy(text, *list, size - 1);
925 XFreeStringList(list);
927 text[size - 1] = '\0';
928 XFree(name.value);
929 return 1;
932 void
933 grabbuttons(Client *c, int focused)
935 updatenumlockmask();
937 unsigned int i, j;
938 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
939 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
940 if (!focused)
941 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
942 BUTTONMASK, GrabModeSync, GrabModeSync, None, None);
943 for (i = 0; i < LENGTH(buttons); i++)
944 if (buttons[i].click == ClkClientWin)
945 for (j = 0; j < LENGTH(modifiers); j++)
946 XGrabButton(dpy, buttons[i].button,
947 buttons[i].mask | modifiers[j],
948 c->win, False, BUTTONMASK,
949 GrabModeAsync, GrabModeSync, None, None);
953 void
954 grabkeys(void)
956 updatenumlockmask();
958 unsigned int i, j;
959 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
960 KeyCode code;
962 XUngrabKey(dpy, AnyKey, AnyModifier, root);
963 for (i = 0; i < LENGTH(keys); i++)
964 if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
965 for (j = 0; j < LENGTH(modifiers); j++)
966 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
967 True, GrabModeAsync, GrabModeAsync);
971 void
972 incnmaster(const Arg *arg)
974 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
975 arrange(selmon);
978 #ifdef XINERAMA
979 static int
980 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
982 while (n--)
983 if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
984 && unique[n].width == info->width && unique[n].height == info->height)
985 return 0;
986 return 1;
988 #endif /* XINERAMA */
990 void
991 keypress(XEvent *e)
993 unsigned int i;
994 KeySym keysym;
995 XKeyEvent *ev;
997 ev = &e->xkey;
998 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
999 for (i = 0; i < LENGTH(keys); i++)
1000 if (keysym == keys[i].keysym
1001 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1002 && keys[i].func)
1003 keys[i].func(&(keys[i].arg));
1006 void
1007 killclient(const Arg *arg)
1009 if (!selmon->sel)
1010 return;
1011 if (!sendevent(selmon->sel, wmatom[WMDelete])) {
1012 XGrabServer(dpy);
1013 XSetErrorHandler(xerrordummy);
1014 XSetCloseDownMode(dpy, DestroyAll);
1015 XKillClient(dpy, selmon->sel->win);
1016 XSync(dpy, False);
1017 XSetErrorHandler(xerror);
1018 XUngrabServer(dpy);
1022 void
1023 manage(Window w, XWindowAttributes *wa)
1025 Client *c, *t = NULL;
1026 Window trans = None;
1027 XWindowChanges wc;
1029 c = ecalloc(1, sizeof(Client));
1030 c->win = w;
1031 /* geometry */
1032 c->x = c->oldx = wa->x;
1033 c->y = c->oldy = wa->y;
1034 c->w = c->oldw = wa->width;
1035 c->h = c->oldh = wa->height;
1036 c->oldbw = wa->border_width;
1038 updatetitle(c);
1039 if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1040 c->mon = t->mon;
1041 c->tags = t->tags;
1042 } else {
1043 c->mon = selmon;
1044 applyrules(c);
1047 if (c->x + WIDTH(c) > c->mon->wx + c->mon->ww)
1048 c->x = c->mon->wx + c->mon->ww - WIDTH(c);
1049 if (c->y + HEIGHT(c) > c->mon->wy + c->mon->wh)
1050 c->y = c->mon->wy + c->mon->wh - HEIGHT(c);
1051 c->x = MAX(c->x, c->mon->wx);
1052 c->y = MAX(c->y, c->mon->wy);
1053 c->bw = borderpx;
1055 wc.border_width = c->bw;
1056 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1057 XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel);
1058 configure(c); /* propagates border_width, if size doesn't change */
1059 updatewindowtype(c);
1060 updatesizehints(c);
1061 updatewmhints(c);
1062 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1063 grabbuttons(c, 0);
1064 if (!c->isfloating)
1065 c->isfloating = c->oldstate = trans != None || c->isfixed;
1066 if (c->isfloating)
1067 XRaiseWindow(dpy, c->win);
1068 attach(c);
1069 attachstack(c);
1070 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1071 (unsigned char *) &(c->win), 1);
1072 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1073 setclientstate(c, NormalState);
1074 if (c->mon == selmon)
1075 unfocus(selmon->sel, 0);
1076 c->mon->sel = c;
1077 arrange(c->mon);
1078 XMapWindow(dpy, c->win);
1079 focus(NULL);
1082 void
1083 mappingnotify(XEvent *e)
1085 XMappingEvent *ev = &e->xmapping;
1087 XRefreshKeyboardMapping(ev);
1088 if (ev->request == MappingKeyboard)
1089 grabkeys();
1092 void
1093 maprequest(XEvent *e)
1095 static XWindowAttributes wa;
1096 XMapRequestEvent *ev = &e->xmaprequest;
1098 if (!XGetWindowAttributes(dpy, ev->window, &wa) || wa.override_redirect)
1099 return;
1100 if (!wintoclient(ev->window))
1101 manage(ev->window, &wa);
1104 void
1105 monocle(Monitor *m)
1107 unsigned int n = 0;
1108 Client *c;
1110 for (c = m->clients; c; c = c->next)
1111 if (ISVISIBLE(c))
1112 n++;
1113 if (n > 0) /* override layout symbol */
1114 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1115 for (c = nexttiled(m->clients); c; c = nexttiled(c->next))
1116 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
1119 void
1120 motionnotify(XEvent *e)
1122 static Monitor *mon = NULL;
1123 Monitor *m;
1124 XMotionEvent *ev = &e->xmotion;
1126 if (ev->window != root)
1127 return;
1128 if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1129 unfocus(selmon->sel, 1);
1130 selmon = m;
1131 focus(NULL);
1133 mon = m;
1136 void
1137 movemouse(const Arg *arg)
1139 int x, y, ocx, ocy, nx, ny;
1140 Client *c;
1141 Monitor *m;
1142 XEvent ev;
1143 Time lasttime = 0;
1145 if (!(c = selmon->sel))
1146 return;
1147 if (c->isfullscreen) /* no support moving fullscreen windows by mouse */
1148 return;
1149 restack(selmon);
1150 ocx = c->x;
1151 ocy = c->y;
1152 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1153 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1154 return;
1155 if (!getrootptr(&x, &y))
1156 return;
1157 do {
1158 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1159 switch(ev.type) {
1160 case ConfigureRequest:
1161 case Expose:
1162 case MapRequest:
1163 handler[ev.type](&ev);
1164 break;
1165 case MotionNotify:
1166 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1167 continue;
1168 lasttime = ev.xmotion.time;
1170 nx = ocx + (ev.xmotion.x - x);
1171 ny = ocy + (ev.xmotion.y - y);
1172 if (abs(selmon->wx - nx) < snap)
1173 nx = selmon->wx;
1174 else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1175 nx = selmon->wx + selmon->ww - WIDTH(c);
1176 if (abs(selmon->wy - ny) < snap)
1177 ny = selmon->wy;
1178 else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1179 ny = selmon->wy + selmon->wh - HEIGHT(c);
1180 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1181 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1182 togglefloating(NULL);
1183 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1184 resize(c, nx, ny, c->w, c->h, 1);
1185 break;
1187 } while (ev.type != ButtonRelease);
1188 XUngrabPointer(dpy, CurrentTime);
1189 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1190 sendmon(c, m);
1191 selmon = m;
1192 focus(NULL);
1196 Client *
1197 nexttiled(Client *c)
1199 for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1200 return c;
1203 void
1204 pop(Client *c)
1206 detach(c);
1207 attach(c);
1208 focus(c);
1209 arrange(c->mon);
1212 void
1213 propertynotify(XEvent *e)
1215 Client *c;
1216 Window trans;
1217 XPropertyEvent *ev = &e->xproperty;
1219 if ((ev->window == root) && (ev->atom == XA_WM_NAME))
1220 updatestatus();
1221 else if (ev->state == PropertyDelete)
1222 return; /* ignore */
1223 else if ((c = wintoclient(ev->window))) {
1224 switch(ev->atom) {
1225 default: break;
1226 case XA_WM_TRANSIENT_FOR:
1227 if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1228 (c->isfloating = (wintoclient(trans)) != NULL))
1229 arrange(c->mon);
1230 break;
1231 case XA_WM_NORMAL_HINTS:
1232 c->hintsvalid = 0;
1233 break;
1234 case XA_WM_HINTS:
1235 updatewmhints(c);
1236 drawbars();
1237 break;
1239 if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1240 updatetitle(c);
1241 if (c == c->mon->sel)
1242 drawbar(c->mon);
1244 if (ev->atom == netatom[NetWMWindowType])
1245 updatewindowtype(c);
1249 void
1250 quit(const Arg *arg)
1252 running = 0;
1255 Monitor *
1256 recttomon(int x, int y, int w, int h)
1258 Monitor *m, *r = selmon;
1259 int a, area = 0;
1261 for (m = mons; m; m = m->next)
1262 if ((a = INTERSECT(x, y, w, h, m)) > area) {
1263 area = a;
1264 r = m;
1266 return r;
1269 void
1270 resize(Client *c, int x, int y, int w, int h, int interact)
1272 if (applysizehints(c, &x, &y, &w, &h, interact))
1273 resizeclient(c, x, y, w, h);
1276 void
1277 resizeclient(Client *c, int x, int y, int w, int h)
1279 XWindowChanges wc;
1281 c->oldx = c->x; c->x = wc.x = x;
1282 c->oldy = c->y; c->y = wc.y = y;
1283 c->oldw = c->w; c->w = wc.width = w;
1284 c->oldh = c->h; c->h = wc.height = h;
1285 wc.border_width = c->bw;
1286 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1287 configure(c);
1288 XSync(dpy, False);
1291 void
1292 resizemouse(const Arg *arg)
1294 int ocx, ocy, nw, nh;
1295 Client *c;
1296 Monitor *m;
1297 XEvent ev;
1298 Time lasttime = 0;
1300 if (!(c = selmon->sel))
1301 return;
1302 if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1303 return;
1304 restack(selmon);
1305 ocx = c->x;
1306 ocy = c->y;
1307 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1308 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1309 return;
1310 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1311 do {
1312 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1313 switch(ev.type) {
1314 case ConfigureRequest:
1315 case Expose:
1316 case MapRequest:
1317 handler[ev.type](&ev);
1318 break;
1319 case MotionNotify:
1320 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1321 continue;
1322 lasttime = ev.xmotion.time;
1324 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1325 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1326 if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1327 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1329 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1330 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1331 togglefloating(NULL);
1333 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1334 resize(c, c->x, c->y, nw, nh, 1);
1335 break;
1337 } while (ev.type != ButtonRelease);
1338 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1339 XUngrabPointer(dpy, CurrentTime);
1340 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1341 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1342 sendmon(c, m);
1343 selmon = m;
1344 focus(NULL);
1348 void
1349 restack(Monitor *m)
1351 Client *c;
1352 XEvent ev;
1353 XWindowChanges wc;
1355 drawbar(m);
1356 if (!m->sel)
1357 return;
1358 if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
1359 XRaiseWindow(dpy, m->sel->win);
1360 if (m->lt[m->sellt]->arrange) {
1361 wc.stack_mode = Below;
1362 wc.sibling = m->barwin;
1363 for (c = m->stack; c; c = c->snext)
1364 if (!c->isfloating && ISVISIBLE(c)) {
1365 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1366 wc.sibling = c->win;
1369 XSync(dpy, False);
1370 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1373 void
1374 run(void)
1376 XEvent ev;
1377 /* main event loop */
1378 XSync(dpy, False);
1379 while (running && !XNextEvent(dpy, &ev))
1380 if (handler[ev.type])
1381 handler[ev.type](&ev); /* call handler */
1384 void
1385 scan(void)
1387 unsigned int i, num;
1388 Window d1, d2, *wins = NULL;
1389 XWindowAttributes wa;
1391 if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1392 for (i = 0; i < num; i++) {
1393 if (!XGetWindowAttributes(dpy, wins[i], &wa)
1394 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1395 continue;
1396 if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1397 manage(wins[i], &wa);
1399 for (i = 0; i < num; i++) { /* now the transients */
1400 if (!XGetWindowAttributes(dpy, wins[i], &wa))
1401 continue;
1402 if (XGetTransientForHint(dpy, wins[i], &d1)
1403 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1404 manage(wins[i], &wa);
1406 if (wins)
1407 XFree(wins);
1411 void
1412 sendmon(Client *c, Monitor *m)
1414 if (c->mon == m)
1415 return;
1416 unfocus(c, 1);
1417 detach(c);
1418 detachstack(c);
1419 c->mon = m;
1420 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1421 attach(c);
1422 attachstack(c);
1423 focus(NULL);
1424 arrange(NULL);
1427 void
1428 setclientstate(Client *c, long state)
1430 long data[] = { state, None };
1432 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1433 PropModeReplace, (unsigned char *)data, 2);
1437 sendevent(Client *c, Atom proto)
1439 int n;
1440 Atom *protocols;
1441 int exists = 0;
1442 XEvent ev;
1444 if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1445 while (!exists && n--)
1446 exists = protocols[n] == proto;
1447 XFree(protocols);
1449 if (exists) {
1450 ev.type = ClientMessage;
1451 ev.xclient.window = c->win;
1452 ev.xclient.message_type = wmatom[WMProtocols];
1453 ev.xclient.format = 32;
1454 ev.xclient.data.l[0] = proto;
1455 ev.xclient.data.l[1] = CurrentTime;
1456 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1458 return exists;
1461 void
1462 setfocus(Client *c)
1464 if (!c->neverfocus) {
1465 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1466 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1467 XA_WINDOW, 32, PropModeReplace,
1468 (unsigned char *) &(c->win), 1);
1470 sendevent(c, wmatom[WMTakeFocus]);
1473 void
1474 setfullscreen(Client *c, int fullscreen)
1476 if (fullscreen && !c->isfullscreen) {
1477 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1478 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1479 c->isfullscreen = 1;
1480 c->oldstate = c->isfloating;
1481 c->oldbw = c->bw;
1482 c->bw = 0;
1483 c->isfloating = 1;
1484 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1485 XRaiseWindow(dpy, c->win);
1486 } else if (!fullscreen && c->isfullscreen){
1487 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1488 PropModeReplace, (unsigned char*)0, 0);
1489 c->isfullscreen = 0;
1490 c->isfloating = c->oldstate;
1491 c->bw = c->oldbw;
1492 c->x = c->oldx;
1493 c->y = c->oldy;
1494 c->w = c->oldw;
1495 c->h = c->oldh;
1496 resizeclient(c, c->x, c->y, c->w, c->h);
1497 arrange(c->mon);
1501 void
1502 setlayout(const Arg *arg)
1504 if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1505 selmon->sellt ^= 1;
1506 if (arg && arg->v)
1507 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1508 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1509 if (selmon->sel)
1510 arrange(selmon);
1511 else
1512 drawbar(selmon);
1515 /* arg > 1.0 will set mfact absolutely */
1516 void
1517 setmfact(const Arg *arg)
1519 float f;
1521 if (!arg || !selmon->lt[selmon->sellt]->arrange)
1522 return;
1523 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1524 if (f < 0.05 || f > 0.95)
1525 return;
1526 selmon->mfact = f;
1527 arrange(selmon);
1530 void
1531 setup(void)
1533 int i;
1534 XSetWindowAttributes wa;
1535 Atom utf8string;
1537 /* clean up any zombies immediately */
1538 sigchld(0);
1540 /* init screen */
1541 screen = DefaultScreen(dpy);
1542 sw = DisplayWidth(dpy, screen);
1543 sh = DisplayHeight(dpy, screen);
1544 root = RootWindow(dpy, screen);
1545 drw = drw_create(dpy, screen, root, sw, sh);
1546 if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
1547 die("no fonts could be loaded.");
1548 lrpad = drw->fonts->h;
1549 bh = drw->fonts->h + 2;
1550 updategeom();
1551 /* init atoms */
1552 utf8string = XInternAtom(dpy, "UTF8_STRING", False);
1553 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1554 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1555 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1556 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1557 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1558 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1559 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1560 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1561 netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
1562 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1563 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1564 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1565 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1566 /* init cursors */
1567 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1568 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1569 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1570 /* init appearance */
1571 scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
1572 for (i = 0; i < LENGTH(colors); i++)
1573 scheme[i] = drw_scm_create(drw, colors[i], 3);
1574 /* init bars */
1575 updatebars();
1576 updatestatus();
1577 /* supporting window for NetWMCheck */
1578 wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
1579 XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
1580 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1581 XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8,
1582 PropModeReplace, (unsigned char *) "dwm", 3);
1583 XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32,
1584 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1585 /* EWMH support per view */
1586 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1587 PropModeReplace, (unsigned char *) netatom, NetLast);
1588 XDeleteProperty(dpy, root, netatom[NetClientList]);
1589 /* select events */
1590 wa.cursor = cursor[CurNormal]->cursor;
1591 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
1592 |ButtonPressMask|PointerMotionMask|EnterWindowMask
1593 |LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1594 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1595 XSelectInput(dpy, root, wa.event_mask);
1596 grabkeys();
1597 focus(NULL);
1600 void
1601 seturgent(Client *c, int urg)
1603 XWMHints *wmh;
1605 c->isurgent = urg;
1606 if (!(wmh = XGetWMHints(dpy, c->win)))
1607 return;
1608 wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
1609 XSetWMHints(dpy, c->win, wmh);
1610 XFree(wmh);
1613 void
1614 showhide(Client *c)
1616 if (!c)
1617 return;
1618 if (ISVISIBLE(c)) {
1619 /* show clients top down */
1620 XMoveWindow(dpy, c->win, c->x, c->y);
1621 if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1622 resize(c, c->x, c->y, c->w, c->h, 0);
1623 showhide(c->snext);
1624 } else {
1625 /* hide clients bottom up */
1626 showhide(c->snext);
1627 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1631 void
1632 sigchld(int unused)
1634 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1635 die("can't install SIGCHLD handler:");
1636 while (0 < waitpid(-1, NULL, WNOHANG));
1639 void
1640 spawn(const Arg *arg)
1642 if (arg->v == dmenucmd)
1643 dmenumon[0] = '0' + selmon->num;
1644 if (fork() == 0) {
1645 if (dpy)
1646 close(ConnectionNumber(dpy));
1647 setsid();
1648 execvp(((char **)arg->v)[0], (char **)arg->v);
1649 die("dwm: execvp '%s' failed:", ((char **)arg->v)[0]);
1653 void
1654 tag(const Arg *arg)
1656 if (selmon->sel && arg->ui & TAGMASK) {
1657 selmon->sel->tags = arg->ui & TAGMASK;
1658 focus(NULL);
1659 arrange(selmon);
1663 void
1664 tagmon(const Arg *arg)
1666 if (!selmon->sel || !mons->next)
1667 return;
1668 sendmon(selmon->sel, dirtomon(arg->i));
1671 void
1672 tile(Monitor *m)
1674 unsigned int i, n, h, mw, my, ty;
1675 Client *c;
1677 for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1678 if (n == 0)
1679 return;
1681 if (n > m->nmaster)
1682 mw = m->nmaster ? m->ww * m->mfact : 0;
1683 else
1684 mw = m->ww;
1685 for (i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1686 if (i < m->nmaster) {
1687 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1688 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), 0);
1689 if (my + HEIGHT(c) < m->wh)
1690 my += HEIGHT(c);
1691 } else {
1692 h = (m->wh - ty) / (n - i);
1693 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), 0);
1694 if (ty + HEIGHT(c) < m->wh)
1695 ty += HEIGHT(c);
1699 void
1700 togglebar(const Arg *arg)
1702 selmon->showbar = !selmon->showbar;
1703 updatebarpos(selmon);
1704 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1705 arrange(selmon);
1708 void
1709 togglefloating(const Arg *arg)
1711 if (!selmon->sel)
1712 return;
1713 if (selmon->sel->isfullscreen) /* no support for fullscreen windows */
1714 return;
1715 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1716 if (selmon->sel->isfloating)
1717 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1718 selmon->sel->w, selmon->sel->h, 0);
1719 arrange(selmon);
1722 void
1723 toggletag(const Arg *arg)
1725 unsigned int newtags;
1727 if (!selmon->sel)
1728 return;
1729 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1730 if (newtags) {
1731 selmon->sel->tags = newtags;
1732 focus(NULL);
1733 arrange(selmon);
1737 void
1738 toggleview(const Arg *arg)
1740 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1742 if (newtagset) {
1743 selmon->tagset[selmon->seltags] = newtagset;
1744 focus(NULL);
1745 arrange(selmon);
1749 void
1750 unfocus(Client *c, int setfocus)
1752 if (!c)
1753 return;
1754 grabbuttons(c, 0);
1755 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
1756 if (setfocus) {
1757 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1758 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1762 void
1763 unmanage(Client *c, int destroyed)
1765 Monitor *m = c->mon;
1766 XWindowChanges wc;
1768 detach(c);
1769 detachstack(c);
1770 if (!destroyed) {
1771 wc.border_width = c->oldbw;
1772 XGrabServer(dpy); /* avoid race conditions */
1773 XSetErrorHandler(xerrordummy);
1774 XSelectInput(dpy, c->win, NoEventMask);
1775 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1776 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1777 setclientstate(c, WithdrawnState);
1778 XSync(dpy, False);
1779 XSetErrorHandler(xerror);
1780 XUngrabServer(dpy);
1782 free(c);
1783 focus(NULL);
1784 updateclientlist();
1785 arrange(m);
1788 void
1789 unmapnotify(XEvent *e)
1791 Client *c;
1792 XUnmapEvent *ev = &e->xunmap;
1794 if ((c = wintoclient(ev->window))) {
1795 if (ev->send_event)
1796 setclientstate(c, WithdrawnState);
1797 else
1798 unmanage(c, 0);
1802 void
1803 updatebars(void)
1805 Monitor *m;
1806 XSetWindowAttributes wa = {
1807 .override_redirect = True,
1808 .background_pixmap = ParentRelative,
1809 .event_mask = ButtonPressMask|ExposureMask
1811 XClassHint ch = {"dwm", "dwm"};
1812 for (m = mons; m; m = m->next) {
1813 if (m->barwin)
1814 continue;
1815 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1816 CopyFromParent, DefaultVisual(dpy, screen),
1817 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1818 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
1819 XMapRaised(dpy, m->barwin);
1820 XSetClassHint(dpy, m->barwin, &ch);
1824 void
1825 updatebarpos(Monitor *m)
1827 m->wy = m->my;
1828 m->wh = m->mh;
1829 if (m->showbar) {
1830 m->wh -= bh;
1831 m->by = m->topbar ? m->wy : m->wy + m->wh;
1832 m->wy = m->topbar ? m->wy + bh : m->wy;
1833 } else
1834 m->by = -bh;
1837 void
1838 updateclientlist()
1840 Client *c;
1841 Monitor *m;
1843 XDeleteProperty(dpy, root, netatom[NetClientList]);
1844 for (m = mons; m; m = m->next)
1845 for (c = m->clients; c; c = c->next)
1846 XChangeProperty(dpy, root, netatom[NetClientList],
1847 XA_WINDOW, 32, PropModeAppend,
1848 (unsigned char *) &(c->win), 1);
1852 updategeom(void)
1854 int dirty = 0;
1856 #ifdef XINERAMA
1857 if (XineramaIsActive(dpy)) {
1858 int i, j, n, nn;
1859 Client *c;
1860 Monitor *m;
1861 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1862 XineramaScreenInfo *unique = NULL;
1864 for (n = 0, m = mons; m; m = m->next, n++);
1865 /* only consider unique geometries as separate screens */
1866 unique = ecalloc(nn, sizeof(XineramaScreenInfo));
1867 for (i = 0, j = 0; i < nn; i++)
1868 if (isuniquegeom(unique, j, &info[i]))
1869 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1870 XFree(info);
1871 nn = j;
1873 /* new monitors if nn > n */
1874 for (i = n; i < nn; i++) {
1875 for (m = mons; m && m->next; m = m->next);
1876 if (m)
1877 m->next = createmon();
1878 else
1879 mons = createmon();
1881 for (i = 0, m = mons; i < nn && m; m = m->next, i++)
1882 if (i >= n
1883 || unique[i].x_org != m->mx || unique[i].y_org != m->my
1884 || unique[i].width != m->mw || unique[i].height != m->mh)
1886 dirty = 1;
1887 m->num = i;
1888 m->mx = m->wx = unique[i].x_org;
1889 m->my = m->wy = unique[i].y_org;
1890 m->mw = m->ww = unique[i].width;
1891 m->mh = m->wh = unique[i].height;
1892 updatebarpos(m);
1894 /* removed monitors if n > nn */
1895 for (i = nn; i < n; i++) {
1896 for (m = mons; m && m->next; m = m->next);
1897 while ((c = m->clients)) {
1898 dirty = 1;
1899 m->clients = c->next;
1900 detachstack(c);
1901 c->mon = mons;
1902 attach(c);
1903 attachstack(c);
1905 if (m == selmon)
1906 selmon = mons;
1907 cleanupmon(m);
1909 free(unique);
1910 } else
1911 #endif /* XINERAMA */
1912 { /* default monitor setup */
1913 if (!mons)
1914 mons = createmon();
1915 if (mons->mw != sw || mons->mh != sh) {
1916 dirty = 1;
1917 mons->mw = mons->ww = sw;
1918 mons->mh = mons->wh = sh;
1919 updatebarpos(mons);
1922 if (dirty) {
1923 selmon = mons;
1924 selmon = wintomon(root);
1926 return dirty;
1929 void
1930 updatenumlockmask(void)
1932 unsigned int i, j;
1933 XModifierKeymap *modmap;
1935 numlockmask = 0;
1936 modmap = XGetModifierMapping(dpy);
1937 for (i = 0; i < 8; i++)
1938 for (j = 0; j < modmap->max_keypermod; j++)
1939 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1940 == XKeysymToKeycode(dpy, XK_Num_Lock))
1941 numlockmask = (1 << i);
1942 XFreeModifiermap(modmap);
1945 void
1946 updatesizehints(Client *c)
1948 long msize;
1949 XSizeHints size;
1951 if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
1952 /* size is uninitialized, ensure that size.flags aren't used */
1953 size.flags = PSize;
1954 if (size.flags & PBaseSize) {
1955 c->basew = size.base_width;
1956 c->baseh = size.base_height;
1957 } else if (size.flags & PMinSize) {
1958 c->basew = size.min_width;
1959 c->baseh = size.min_height;
1960 } else
1961 c->basew = c->baseh = 0;
1962 if (size.flags & PResizeInc) {
1963 c->incw = size.width_inc;
1964 c->inch = size.height_inc;
1965 } else
1966 c->incw = c->inch = 0;
1967 if (size.flags & PMaxSize) {
1968 c->maxw = size.max_width;
1969 c->maxh = size.max_height;
1970 } else
1971 c->maxw = c->maxh = 0;
1972 if (size.flags & PMinSize) {
1973 c->minw = size.min_width;
1974 c->minh = size.min_height;
1975 } else if (size.flags & PBaseSize) {
1976 c->minw = size.base_width;
1977 c->minh = size.base_height;
1978 } else
1979 c->minw = c->minh = 0;
1980 if (size.flags & PAspect) {
1981 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
1982 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
1983 } else
1984 c->maxa = c->mina = 0.0;
1985 c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh);
1986 c->hintsvalid = 1;
1989 void
1990 updatestatus(void)
1992 if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
1993 strcpy(stext, "dwm-"VERSION);
1994 drawbar(selmon);
1997 void
1998 updatetitle(Client *c)
2000 if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2001 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2002 if (c->name[0] == '\0') /* hack to mark broken clients */
2003 strcpy(c->name, broken);
2006 void
2007 updatewindowtype(Client *c)
2009 Atom state = getatomprop(c, netatom[NetWMState]);
2010 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2012 if (state == netatom[NetWMFullscreen])
2013 setfullscreen(c, 1);
2014 if (wtype == netatom[NetWMWindowTypeDialog])
2015 c->isfloating = 1;
2018 void
2019 updatewmhints(Client *c)
2021 XWMHints *wmh;
2023 if ((wmh = XGetWMHints(dpy, c->win))) {
2024 if (c == selmon->sel && wmh->flags & XUrgencyHint) {
2025 wmh->flags &= ~XUrgencyHint;
2026 XSetWMHints(dpy, c->win, wmh);
2027 } else
2028 c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
2029 if (wmh->flags & InputHint)
2030 c->neverfocus = !wmh->input;
2031 else
2032 c->neverfocus = 0;
2033 XFree(wmh);
2037 void
2038 view(const Arg *arg)
2040 if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2041 return;
2042 selmon->seltags ^= 1; /* toggle sel tagset */
2043 if (arg->ui & TAGMASK)
2044 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2045 focus(NULL);
2046 arrange(selmon);
2049 Client *
2050 wintoclient(Window w)
2052 Client *c;
2053 Monitor *m;
2055 for (m = mons; m; m = m->next)
2056 for (c = m->clients; c; c = c->next)
2057 if (c->win == w)
2058 return c;
2059 return NULL;
2062 Monitor *
2063 wintomon(Window w)
2065 int x, y;
2066 Client *c;
2067 Monitor *m;
2069 if (w == root && getrootptr(&x, &y))
2070 return recttomon(x, y, 1, 1);
2071 for (m = mons; m; m = m->next)
2072 if (w == m->barwin)
2073 return m;
2074 if ((c = wintoclient(w)))
2075 return c->mon;
2076 return selmon;
2079 /* There's no way to check accesses to destroyed windows, thus those cases are
2080 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2081 * default error handler, which may call exit. */
2083 xerror(Display *dpy, XErrorEvent *ee)
2085 if (ee->error_code == BadWindow
2086 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2087 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2088 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2089 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2090 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2091 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2092 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2093 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2094 return 0;
2095 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2096 ee->request_code, ee->error_code);
2097 return xerrorxlib(dpy, ee); /* may call exit */
2101 xerrordummy(Display *dpy, XErrorEvent *ee)
2103 return 0;
2106 /* Startup Error handler to check if another window manager
2107 * is already running. */
2109 xerrorstart(Display *dpy, XErrorEvent *ee)
2111 die("dwm: another window manager is already running");
2112 return -1;
2115 void
2116 zoom(const Arg *arg)
2118 Client *c = selmon->sel;
2120 if (!selmon->lt[selmon->sellt]->arrange || !c || c->isfloating)
2121 return;
2122 if (c == nexttiled(selmon->clients) && !(c = nexttiled(c->next)))
2123 return;
2124 pop(c);
2128 main(int argc, char *argv[])
2130 if (argc == 2 && !strcmp("-v", argv[1]))
2131 die("dwm-"VERSION);
2132 else if (argc != 1)
2133 die("usage: dwm [-v]");
2134 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2135 fputs("warning: no locale support\n", stderr);
2136 if (!(dpy = XOpenDisplay(NULL)))
2137 die("dwm: cannot open display");
2138 checkotherwm();
2139 setup();
2140 #ifdef __OpenBSD__
2141 if (pledge("stdio rpath proc exec", NULL) == -1)
2142 die("pledge");
2143 #endif /* __OpenBSD__ */
2144 scan();
2145 run();
2146 cleanup();
2147 XCloseDisplay(dpy);
2148 return EXIT_SUCCESS;