all repos — dwm @ 6ab163c6955cf7830dfbf39b6c7ba16114054d94

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