all repos — dwm @ 9cb9c32ee7d76554cfc44ad8801d70cef9fe25e9

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