all repos — dwm @ 8e0f8ffcc63f27bd2e2c666b4e889a3056b3b70b

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