all repos — dwm @ 5c6545adf5e2fc7011bd53735910f66998cade64

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