all repos — dwm @ c2c54cc0faad89483edf3ffddee3e3ff20cf8263

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