all repos — dwm @ c45d46ad9a44b9897c8c12eacb60b5cbdd31be7c

fork of suckless dynamic window manager

dwm.c (view raw)

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