all repos — dwm @ ca3e847e459e1ba43f45513877d39d50cce7a0c5

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