aewl

diff dwm.c @ 754:4c12dccc288d

this is the (second) begin of aewl - my personal stipped down dwm
author meillo@marmaro.de
date Thu, 29 May 2008 23:12:30 +0200
parents main.c@628c5bac7f3b
children cdd895c163bd
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dwm.c	Thu May 29 23:12:30 2008 +0200
     1.3 @@ -0,0 +1,1846 @@
     1.4 +/* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
     1.5 + * See LICENSE file for license details.
     1.6 + *
     1.7 + * dynamic window manager is designed like any other X client as well. It is
     1.8 + * driven through handling X events. In contrast to other X clients, a window
     1.9 + * manager selects for SubstructureRedirectMask on the root window, to receive
    1.10 + * events about window (dis-)appearance.  Only one X connection at a time is
    1.11 + * allowed to select for this event mask.
    1.12 + *
    1.13 + * Calls to fetch an X event from the event queue are blocking.  Due reading
    1.14 + * status text from standard input, a select()-driven main loop has been
    1.15 + * implemented which selects for reads on the X connection and STDIN_FILENO to
    1.16 + * handle all data smoothly. The event handlers of dwm are organized in an
    1.17 + * array which is accessed whenever a new event has been fetched. This allows
    1.18 + * event dispatching in O(1) time.
    1.19 + *
    1.20 + * Each child of the root window is called a client, except windows which have
    1.21 + * set the override_redirect flag.  Clients are organized in a global
    1.22 + * doubly-linked client list, the focus history is remembered through a global
    1.23 + * stack list. Each client contains an array of Bools of the same size as the
    1.24 + * global tags array to indicate the tags of a client.  For each client dwm
    1.25 + * creates a small title window, which is resized whenever the (_NET_)WM_NAME
    1.26 + * properties are updated or the client is moved/resized.
    1.27 + *
    1.28 + * Keys and tagging rules are organized as arrays and defined in the config.h
    1.29 + * file. These arrays are kept static in event.o and tag.o respectively,
    1.30 + * because no other part of dwm needs access to them.  The current mode is
    1.31 + * represented by the arrange() function pointer, which wether points to
    1.32 + * dofloat() or dotile().
    1.33 + *
    1.34 + * To understand everything else, start reading main.c:main().
    1.35 + */
    1.36 +
    1.37 +#include "config.h"
    1.38 +#include <errno.h>
    1.39 +#include <locale.h>
    1.40 +#include <regex.h>
    1.41 +#include <stdio.h>
    1.42 +#include <stdarg.h>
    1.43 +#include <stdlib.h>
    1.44 +#include <string.h>
    1.45 +#include <unistd.h>
    1.46 +#include <sys/select.h>
    1.47 +#include <sys/types.h>
    1.48 +#include <sys/wait.h>
    1.49 +#include <X11/cursorfont.h>
    1.50 +#include <X11/keysym.h>
    1.51 +#include <X11/Xatom.h>
    1.52 +#include <X11/Xlib.h>
    1.53 +#include <X11/Xproto.h>
    1.54 +#include <X11/Xutil.h>
    1.55 +
    1.56 +/* mask shorthands, used in event.c and client.c */
    1.57 +#define BUTTONMASK		(ButtonPressMask | ButtonReleaseMask)
    1.58 +
    1.59 +enum { NetSupported, NetWMName, NetLast };		/* EWMH atoms */
    1.60 +enum { WMProtocols, WMDelete, WMState, WMLast };	/* default atoms */
    1.61 +enum { CurNormal, CurResize, CurMove, CurLast };	/* cursor */
    1.62 +enum { ColBorder, ColFG, ColBG, ColLast };		/* color */
    1.63 +
    1.64 +typedef union {
    1.65 +	const char *cmd;
    1.66 +	int i;
    1.67 +} Arg; /* argument type */
    1.68 +
    1.69 +typedef struct {
    1.70 +	int ascent;
    1.71 +	int descent;
    1.72 +	int height;
    1.73 +	XFontSet set;
    1.74 +	XFontStruct *xfont;
    1.75 +} Fnt;
    1.76 +
    1.77 +typedef struct {
    1.78 +	int x, y, w, h;
    1.79 +	unsigned long norm[ColLast];
    1.80 +	unsigned long sel[ColLast];
    1.81 +	Drawable drawable;
    1.82 +	Fnt font;
    1.83 +	GC gc;
    1.84 +} DC; /* draw context */
    1.85 +
    1.86 +typedef struct Client Client;
    1.87 +struct Client {
    1.88 +	char name[256];
    1.89 +	int x, y, w, h;
    1.90 +	int rx, ry, rw, rh; /* revert geometry */
    1.91 +	int basew, baseh, incw, inch, maxw, maxh, minw, minh;
    1.92 +	int minax, minay, maxax, maxay;
    1.93 +	long flags;
    1.94 +	unsigned int border;
    1.95 +	Bool isfixed, isfloat, ismax;
    1.96 +	Bool *tags;
    1.97 +	Client *next;
    1.98 +	Client *prev;
    1.99 +	Client *snext;
   1.100 +	Window win;
   1.101 +};
   1.102 +
   1.103 +typedef struct {
   1.104 +	const char *clpattern;
   1.105 +	const char *tpattern;
   1.106 +	Bool isfloat;
   1.107 +} Rule;
   1.108 +
   1.109 +typedef struct {
   1.110 +	regex_t *clregex;
   1.111 +	regex_t *tregex;
   1.112 +} RReg;
   1.113 +
   1.114 +
   1.115 +typedef struct {
   1.116 +	unsigned long mod;
   1.117 +	KeySym keysym;
   1.118 +	void (*func)(Arg *arg);
   1.119 +	Arg arg;
   1.120 +} Key;
   1.121 +
   1.122 +
   1.123 +#define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
   1.124 +#define MOUSEMASK		(BUTTONMASK | PointerMotionMask)
   1.125 +
   1.126 +
   1.127 +
   1.128 +const char *tags[];			/* all tags */
   1.129 +char stext[256];				/* status text */
   1.130 +int bh, bmw;				/* bar height, bar mode label width */
   1.131 +int screen, sx, sy, sw, sh;		/* screen geometry */
   1.132 +int wax, way, wah, waw;			/* windowarea geometry */
   1.133 +unsigned int nmaster;		/* number of master clients */
   1.134 +unsigned int ntags, numlockmask;		/* number of tags, dynamic lock mask */
   1.135 +void (*handler[LASTEvent])(XEvent *);	/* event handler */
   1.136 +void (*arrange)(void);			/* arrange function, indicates mode  */
   1.137 +Atom wmatom[WMLast], netatom[NetLast];
   1.138 +Bool running, selscreen, *seltag;	/* seltag is array of Bool */
   1.139 +Client *clients, *sel, *stack;		/* global client list and stack */
   1.140 +Cursor cursor[CurLast];
   1.141 +DC dc;					/* global draw context */
   1.142 +Display *dpy;
   1.143 +Window root, barwin;
   1.144 +
   1.145 +Bool running = True;
   1.146 +Bool selscreen = True;
   1.147 +Client *clients = NULL;
   1.148 +Client *sel = NULL;
   1.149 +Client *stack = NULL;
   1.150 +DC dc = {0};
   1.151 +
   1.152 +static int (*xerrorxlib)(Display *, XErrorEvent *);
   1.153 +static Bool otherwm, readin;
   1.154 +static RReg *rreg = NULL;
   1.155 +static unsigned int len = 0;
   1.156 +
   1.157 +
   1.158 +TAGS
   1.159 +RULES
   1.160 +
   1.161 +
   1.162 +/* client.c */
   1.163 +void configure(Client *c);		/* send synthetic configure event */
   1.164 +void focus(Client *c);			/* focus c, c may be NULL */
   1.165 +Client *getclient(Window w);		/* return client of w */
   1.166 +Bool isprotodel(Client *c);		/* returns True if c->win supports wmatom[WMDelete] */
   1.167 +void killclient(Arg *arg);		/* kill c nicely */
   1.168 +void manage(Window w, XWindowAttributes *wa);	/* manage new client */
   1.169 +void resize(Client *c, Bool sizehints);	/* resize c*/
   1.170 +void updatesizehints(Client *c);		/* update the size hint variables of c */
   1.171 +void updatetitle(Client *c);		/* update the name of c */
   1.172 +void unmanage(Client *c);		/* destroy c */
   1.173 +
   1.174 +/* draw.c */
   1.175 +void drawstatus(void);			/* draw the bar */
   1.176 +unsigned long getcolor(const char *colstr);	/* return color of colstr */
   1.177 +void setfont(const char *fontstr);	/* set the font for DC */
   1.178 +unsigned int textw(const char *text);	/* return the width of text in px*/
   1.179 +
   1.180 +/* event.c */
   1.181 +void grabkeys(void);			/* grab all keys defined in config.h */
   1.182 +void procevent(void);			/* process pending X events */
   1.183 +
   1.184 +/* main.c */
   1.185 +void quit(Arg *arg);			/* quit dwm nicely */
   1.186 +void sendevent(Window w, Atom a, long value);	/* send synthetic event to w */
   1.187 +int xerror(Display *dsply, XErrorEvent *ee);	/* dwm's X error handler */
   1.188 +
   1.189 +/* tag.c */
   1.190 +void initrregs(void);			/* initialize regexps of rules defined in config.h */
   1.191 +Client *getnext(Client *c);		/* returns next visible client */
   1.192 +Client *getprev(Client *c);		/* returns previous visible client */
   1.193 +void settags(Client *c, Client *trans);	/* sets tags of c */
   1.194 +void tag(Arg *arg);			/* tags c with arg's index */
   1.195 +void toggletag(Arg *arg);		/* toggles c tags with arg's index */
   1.196 +void viewnext(Arg *arg);    /* view next tag(s) */
   1.197 +
   1.198 +/* util.c */
   1.199 +void *emallocz(unsigned int size);	/* allocates zero-initialized memory, exits on error */
   1.200 +void eprint(const char *errstr, ...);	/* prints errstr and exits with 1 */
   1.201 +void spawn(Arg *arg);			/* forks a new subprocess with to arg's cmd */
   1.202 +
   1.203 +/* view.c */
   1.204 +void detach(Client *c);			/* detaches c from global client list */
   1.205 +void dofloat(void);			/* arranges all windows floating */
   1.206 +void dotile(void);			/* arranges all windows tiled */
   1.207 +void domax(void);            /* arranges all windows fullscreen */
   1.208 +void focusnext(Arg *arg);		/* focuses next visible client, arg is ignored  */
   1.209 +void incnmaster(Arg *arg);		/* increments nmaster with arg's index value */
   1.210 +Bool isvisible(Client *c);		/* returns True if client is visible */
   1.211 +void restack(void);			/* restores z layers of all clients */
   1.212 +void togglefloat(Arg *arg);		/* toggles focusesd client between floating/non-floating state */
   1.213 +void togglemode(Arg *arg);		/* toggles global arrange function (dotile/dofloat) */
   1.214 +void view(Arg *arg);			/* views the tag with arg's index */
   1.215 +void zoom(Arg *arg);			/* zooms the focused client to master area, arg is ignored */
   1.216 +
   1.217 +
   1.218 +
   1.219 +
   1.220 +
   1.221 +
   1.222 +
   1.223 +
   1.224 +
   1.225 +
   1.226 +/* from view.c */
   1.227 +/* static */
   1.228 +
   1.229 +static Client *
   1.230 +nexttiled(Client *c) {
   1.231 +	for(c = getnext(c); c && c->isfloat; c = getnext(c->next));
   1.232 +	return c;
   1.233 +}
   1.234 +
   1.235 +static void
   1.236 +togglemax(Client *c) {
   1.237 +	XEvent ev;
   1.238 +		
   1.239 +	if(c->isfixed)
   1.240 +		return;
   1.241 +
   1.242 +	if((c->ismax = !c->ismax)) {
   1.243 +		c->rx = c->x; c->x = wax;
   1.244 +		c->ry = c->y; c->y = way;
   1.245 +		c->rw = c->w; c->w = waw - 2 * BORDERPX;
   1.246 +		c->rh = c->h; c->h = wah - 2 * BORDERPX;
   1.247 +	}
   1.248 +	else {
   1.249 +		c->x = c->rx;
   1.250 +		c->y = c->ry;
   1.251 +		c->w = c->rw;
   1.252 +		c->h = c->rh;
   1.253 +	}
   1.254 +	resize(c, True);
   1.255 +	while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
   1.256 +}
   1.257 +
   1.258 +
   1.259 +
   1.260 +void (*arrange)(void) = DEFMODE;
   1.261 +
   1.262 +void
   1.263 +detach(Client *c) {
   1.264 +	if(c->prev)
   1.265 +		c->prev->next = c->next;
   1.266 +	if(c->next)
   1.267 +		c->next->prev = c->prev;
   1.268 +	if(c == clients)
   1.269 +		clients = c->next;
   1.270 +	c->next = c->prev = NULL;
   1.271 +}
   1.272 +
   1.273 +void
   1.274 +dofloat(void) {
   1.275 +	Client *c;
   1.276 +
   1.277 +	for(c = clients; c; c = c->next) {
   1.278 +		if(isvisible(c)) {
   1.279 +			resize(c, True);
   1.280 +		}
   1.281 +		else
   1.282 +			XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
   1.283 +	}
   1.284 +	if(!sel || !isvisible(sel)) {
   1.285 +		for(c = stack; c && !isvisible(c); c = c->snext);
   1.286 +		focus(c);
   1.287 +	}
   1.288 +	restack();
   1.289 +}
   1.290 +
   1.291 +void
   1.292 +dotile(void) {
   1.293 +	unsigned int i, n, mw, mh, tw, th;
   1.294 +	Client *c;
   1.295 +
   1.296 +	for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
   1.297 +		n++;
   1.298 +	/* window geoms */
   1.299 +	mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1);
   1.300 +	mw = (n > nmaster) ? waw / 2 : waw;
   1.301 +	th = (n > nmaster) ? wah / (n - nmaster) : 0;
   1.302 +	tw = waw - mw;
   1.303 +
   1.304 +	for(i = 0, c = clients; c; c = c->next)
   1.305 +		if(isvisible(c)) {
   1.306 +			if(c->isfloat) {
   1.307 +				resize(c, True);
   1.308 +				continue;
   1.309 +			}
   1.310 +			c->ismax = False;
   1.311 +			c->x = wax;
   1.312 +			c->y = way;
   1.313 +			if(i < nmaster) {
   1.314 +				c->y += i * mh;
   1.315 +				c->w = mw - 2 * BORDERPX;
   1.316 +				c->h = mh - 2 * BORDERPX;
   1.317 +			}
   1.318 +			else {  /* tile window */
   1.319 +				c->x += mw;
   1.320 +				c->w = tw - 2 * BORDERPX;
   1.321 +				if(th > 2 * BORDERPX) {
   1.322 +					c->y += (i - nmaster) * th;
   1.323 +					c->h = th - 2 * BORDERPX;
   1.324 +				}
   1.325 +				else /* fallback if th <= 2 * BORDERPX */
   1.326 +					c->h = wah - 2 * BORDERPX;
   1.327 +			}
   1.328 +			resize(c, False);
   1.329 +			i++;
   1.330 +		}
   1.331 +		else
   1.332 +			XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
   1.333 +	if(!sel || !isvisible(sel)) {
   1.334 +		for(c = stack; c && !isvisible(c); c = c->snext);
   1.335 +		focus(c);
   1.336 +	}
   1.337 +	restack();
   1.338 +}
   1.339 +
   1.340 +/* begin code by mitch */
   1.341 +void
   1.342 +arrangemax(Client *c) {
   1.343 +  if(c == sel) {
   1.344 +    c->ismax = True;
   1.345 +    c->x = sx;
   1.346 +    c->y = bh;
   1.347 +    c->w = sw - 2 * BORDERPX;
   1.348 +    c->h = sh - bh - 2 * BORDERPX;
   1.349 +    XRaiseWindow(dpy, c->win);
   1.350 +  } else {
   1.351 +    c->ismax = False;
   1.352 +    XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
   1.353 +    XLowerWindow(dpy, c->win);
   1.354 +  }
   1.355 +}
   1.356 +
   1.357 +void
   1.358 +domax(void) {
   1.359 +  Client *c;
   1.360 +
   1.361 +  for(c = clients; c; c = c->next) {
   1.362 +    if(isvisible(c)) {
   1.363 +      if(c->isfloat) {
   1.364 +        resize(c, True);
   1.365 +        continue;
   1.366 +      }
   1.367 +      arrangemax(c);
   1.368 +      resize(c, False);
   1.369 +    } else {
   1.370 +      XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
   1.371 +    }
   1.372 +
   1.373 +  }
   1.374 +  if(!sel || !isvisible(sel)) {
   1.375 +    for(c = stack; c && !isvisible(c); c = c->snext);
   1.376 +    focus(c);
   1.377 +  }
   1.378 +  restack();
   1.379 +}
   1.380 +/* end code by mitch */
   1.381 +
   1.382 +void
   1.383 +focusnext(Arg *arg) {
   1.384 +	Client *c;
   1.385 +
   1.386 +	if(!sel)
   1.387 +		return;
   1.388 +	if(!(c = getnext(sel->next)))
   1.389 +		c = getnext(clients);
   1.390 +	if(c) {
   1.391 +		focus(c);
   1.392 +		restack();
   1.393 +	}
   1.394 +}
   1.395 +
   1.396 +void
   1.397 +incnmaster(Arg *arg) {
   1.398 +	if((arrange == dofloat) || (nmaster + arg->i < 1)
   1.399 +		|| (wah / (nmaster + arg->i) <= 2 * BORDERPX))
   1.400 +		return;
   1.401 +	nmaster += arg->i;
   1.402 +	if(sel)
   1.403 +		arrange();
   1.404 +	else
   1.405 +		drawstatus();
   1.406 +}
   1.407 +
   1.408 +Bool
   1.409 +isvisible(Client *c) {
   1.410 +	unsigned int i;
   1.411 +
   1.412 +	for(i = 0; i < ntags; i++)
   1.413 +		if(c->tags[i] && seltag[i])
   1.414 +			return True;
   1.415 +	return False;
   1.416 +}
   1.417 +
   1.418 +void
   1.419 +restack(void) {
   1.420 +	Client *c;
   1.421 +	XEvent ev;
   1.422 +
   1.423 +	drawstatus();
   1.424 +	if(!sel)
   1.425 +		return;
   1.426 +	if(sel->isfloat || arrange == dofloat)
   1.427 +		XRaiseWindow(dpy, sel->win);
   1.428 +
   1.429 +  /* begin code by mitch */
   1.430 +  if(arrange == domax) {
   1.431 +    for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
   1.432 +      arrangemax(c);
   1.433 +      resize(c, False);
   1.434 +    }
   1.435 +
   1.436 +  } else if (arrange == dotile) {
   1.437 +  /* end code by mitch */
   1.438 +
   1.439 +		if(!sel->isfloat)
   1.440 +			XLowerWindow(dpy, sel->win);
   1.441 +		for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
   1.442 +			if(c == sel)
   1.443 +				continue;
   1.444 +			XLowerWindow(dpy, c->win);
   1.445 +		}
   1.446 +	}
   1.447 +	XSync(dpy, False);
   1.448 +	while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
   1.449 +}
   1.450 +
   1.451 +void
   1.452 +togglefloat(Arg *arg) {
   1.453 +	if (!sel || arrange == dofloat)
   1.454 +		return;
   1.455 +	sel->isfloat = !sel->isfloat;
   1.456 +	arrange();
   1.457 +}
   1.458 +
   1.459 +void
   1.460 +togglemode(Arg *arg) {
   1.461 +  /* only toggle between tile and max - float is just available through togglefloat */
   1.462 +  arrange = (arrange == dotile) ? domax : dotile;
   1.463 +	if(sel)
   1.464 +		arrange();
   1.465 +	else
   1.466 +		drawstatus();
   1.467 +}
   1.468 +
   1.469 +void
   1.470 +view(Arg *arg) {
   1.471 +	unsigned int i;
   1.472 +
   1.473 +	for(i = 0; i < ntags; i++)
   1.474 +		seltag[i] = (arg->i == -1) ? True : False;
   1.475 +	if(arg->i >= 0 && arg->i < ntags)
   1.476 +		seltag[arg->i] = True;
   1.477 +	arrange();
   1.478 +}
   1.479 +
   1.480 +void
   1.481 +zoom(Arg *arg) {
   1.482 +	unsigned int n;
   1.483 +	Client *c;
   1.484 +
   1.485 +	if(!sel)
   1.486 +		return;
   1.487 +	if(sel->isfloat || (arrange == dofloat)) {
   1.488 +		togglemax(sel);
   1.489 +		return;
   1.490 +	}
   1.491 +	for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
   1.492 +		n++;
   1.493 +
   1.494 +	if((c = sel) == nexttiled(clients))
   1.495 +		if(!(c = nexttiled(c->next)))
   1.496 +			return;
   1.497 +	detach(c);
   1.498 +	if(clients)
   1.499 +		clients->prev = c;
   1.500 +	c->next = clients;
   1.501 +	clients = c;
   1.502 +	focus(c);
   1.503 +	arrange();
   1.504 +}
   1.505 +
   1.506 +
   1.507 +
   1.508 +
   1.509 +
   1.510 +
   1.511 +
   1.512 +
   1.513 +
   1.514 +
   1.515 +
   1.516 +
   1.517 +
   1.518 +
   1.519 +
   1.520 +
   1.521 +/* from util.c */
   1.522 +
   1.523 +
   1.524 +void *
   1.525 +emallocz(unsigned int size) {
   1.526 +	void *res = calloc(1, size);
   1.527 +
   1.528 +	if(!res)
   1.529 +		eprint("fatal: could not malloc() %u bytes\n", size);
   1.530 +	return res;
   1.531 +}
   1.532 +
   1.533 +void
   1.534 +eprint(const char *errstr, ...) {
   1.535 +	va_list ap;
   1.536 +
   1.537 +	va_start(ap, errstr);
   1.538 +	vfprintf(stderr, errstr, ap);
   1.539 +	va_end(ap);
   1.540 +	exit(EXIT_FAILURE);
   1.541 +}
   1.542 +
   1.543 +void
   1.544 +spawn(Arg *arg) {
   1.545 +	static char *shell = NULL;
   1.546 +
   1.547 +	if(!shell && !(shell = getenv("SHELL")))
   1.548 +		shell = "/bin/sh";
   1.549 +	if(!arg->cmd)
   1.550 +		return;
   1.551 +	/* The double-fork construct avoids zombie processes and keeps the code
   1.552 +	 * clean from stupid signal handlers. */
   1.553 +	if(fork() == 0) {
   1.554 +		if(fork() == 0) {
   1.555 +			if(dpy)
   1.556 +				close(ConnectionNumber(dpy));
   1.557 +			setsid();
   1.558 +			execl(shell, shell, "-c", arg->cmd, (char *)NULL);
   1.559 +			fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg->cmd);
   1.560 +			perror(" failed");
   1.561 +		}
   1.562 +		exit(0);
   1.563 +	}
   1.564 +	wait(0);
   1.565 +}
   1.566 +
   1.567 +
   1.568 +
   1.569 +
   1.570 +
   1.571 +
   1.572 +
   1.573 +
   1.574 +
   1.575 +
   1.576 +
   1.577 +
   1.578 +
   1.579 +/* from tag.c */
   1.580 +
   1.581 +/* static */
   1.582 +
   1.583 +Client *
   1.584 +getnext(Client *c) {
   1.585 +	for(; c && !isvisible(c); c = c->next);
   1.586 +	return c;
   1.587 +}
   1.588 +
   1.589 +Client *
   1.590 +getprev(Client *c) {
   1.591 +	for(; c && !isvisible(c); c = c->prev);
   1.592 +	return c;
   1.593 +}
   1.594 +
   1.595 +void
   1.596 +initrregs(void) {
   1.597 +	unsigned int i;
   1.598 +	regex_t *reg;
   1.599 +
   1.600 +	if(rreg)
   1.601 +		return;
   1.602 +	len = sizeof rule / sizeof rule[0];
   1.603 +	rreg = emallocz(len * sizeof(RReg));
   1.604 +	for(i = 0; i < len; i++) {
   1.605 +		if(rule[i].clpattern) {
   1.606 +			reg = emallocz(sizeof(regex_t));
   1.607 +			if(regcomp(reg, rule[i].clpattern, REG_EXTENDED))
   1.608 +				free(reg);
   1.609 +			else
   1.610 +				rreg[i].clregex = reg;
   1.611 +		}
   1.612 +		if(rule[i].tpattern) {
   1.613 +			reg = emallocz(sizeof(regex_t));
   1.614 +			if(regcomp(reg, rule[i].tpattern, REG_EXTENDED))
   1.615 +				free(reg);
   1.616 +			else
   1.617 +				rreg[i].tregex = reg;
   1.618 +		}
   1.619 +	}
   1.620 +}
   1.621 +
   1.622 +void
   1.623 +settags(Client *c, Client *trans) {
   1.624 +	char prop[512];
   1.625 +	unsigned int i, j;
   1.626 +	regmatch_t tmp;
   1.627 +	Bool matched = trans != NULL;
   1.628 +	XClassHint ch = { 0 };
   1.629 +
   1.630 +	if(matched) {
   1.631 +		for(i = 0; i < ntags; i++)
   1.632 +			c->tags[i] = trans->tags[i];
   1.633 +	}
   1.634 +	else {
   1.635 +		XGetClassHint(dpy, c->win, &ch);
   1.636 +		snprintf(prop, sizeof prop, "%s:%s:%s",
   1.637 +				ch.res_class ? ch.res_class : "",
   1.638 +				ch.res_name ? ch.res_name : "", c->name);
   1.639 +		for(i = 0; i < len; i++)
   1.640 +			if(rreg[i].clregex && !regexec(rreg[i].clregex, prop, 1, &tmp, 0)) {
   1.641 +				c->isfloat = rule[i].isfloat;
   1.642 +				for(j = 0; rreg[i].tregex && j < ntags; j++) {
   1.643 +					if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) {
   1.644 +						matched = True;
   1.645 +						c->tags[j] = True;
   1.646 +					}
   1.647 +				}
   1.648 +        break;  /* perform only the first rule matching */
   1.649 +			}
   1.650 +		if(ch.res_class)
   1.651 +			XFree(ch.res_class);
   1.652 +		if(ch.res_name)
   1.653 +			XFree(ch.res_name);
   1.654 +	}
   1.655 +	if(!matched)
   1.656 +		for(i = 0; i < ntags; i++)
   1.657 +			c->tags[i] = seltag[i];
   1.658 +}
   1.659 +
   1.660 +void
   1.661 +tag(Arg *arg) {
   1.662 +	unsigned int i;
   1.663 +
   1.664 +	if(!sel)
   1.665 +		return;
   1.666 +	for(i = 0; i < ntags; i++)
   1.667 +		sel->tags[i] = (arg->i == -1) ? True : False;
   1.668 +	if(arg->i >= 0 && arg->i < ntags)
   1.669 +		sel->tags[arg->i] = True;
   1.670 +	arrange();
   1.671 +}
   1.672 +
   1.673 +void
   1.674 +toggletag(Arg *arg) {
   1.675 +	unsigned int i;
   1.676 +
   1.677 +	if(!sel)
   1.678 +		return;
   1.679 +	sel->tags[arg->i] = !sel->tags[arg->i];
   1.680 +	for(i = 0; i < ntags && !sel->tags[i]; i++);
   1.681 +	if(i == ntags)
   1.682 +		sel->tags[arg->i] = True;
   1.683 +	arrange();
   1.684 +}
   1.685 +
   1.686 +/* begin code by jukka */
   1.687 +void
   1.688 +viewnext(Arg *arg) {
   1.689 +  unsigned int i;
   1.690 +  Bool last = seltag[ntags-1];
   1.691 +
   1.692 +  for (i=ntags-1; i>0; --i)
   1.693 +    seltag[i] = seltag[i-1];
   1.694 +  seltag[0] = last;
   1.695 +  arrange();
   1.696 +}
   1.697 +/* end code by jukka */
   1.698 +
   1.699 +
   1.700 +
   1.701 +
   1.702 +
   1.703 +
   1.704 +
   1.705 +
   1.706 +
   1.707 +
   1.708 +
   1.709 +
   1.710 +
   1.711 +
   1.712 +
   1.713 +
   1.714 +/* from event.c */
   1.715 +/* static */
   1.716 +
   1.717 +KEYS
   1.718 +
   1.719 +
   1.720 +
   1.721 +static void
   1.722 +movemouse(Client *c) {
   1.723 +	int x1, y1, ocx, ocy, di;
   1.724 +	unsigned int dui;
   1.725 +	Window dummy;
   1.726 +	XEvent ev;
   1.727 +
   1.728 +	ocx = c->x;
   1.729 +	ocy = c->y;
   1.730 +	if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
   1.731 +			None, cursor[CurMove], CurrentTime) != GrabSuccess)
   1.732 +		return;
   1.733 +	c->ismax = False;
   1.734 +	XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
   1.735 +	for(;;) {
   1.736 +		XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
   1.737 +		switch (ev.type) {
   1.738 +		case ButtonRelease:
   1.739 +			resize(c, True);
   1.740 +			XUngrabPointer(dpy, CurrentTime);
   1.741 +			return;
   1.742 +		case ConfigureRequest:
   1.743 +		case Expose:
   1.744 +		case MapRequest:
   1.745 +			handler[ev.type](&ev);
   1.746 +			break;
   1.747 +		case MotionNotify:
   1.748 +			XSync(dpy, False);
   1.749 +			c->x = ocx + (ev.xmotion.x - x1);
   1.750 +			c->y = ocy + (ev.xmotion.y - y1);
   1.751 +			if(abs(wax + c->x) < SNAP)
   1.752 +				c->x = wax;
   1.753 +			else if(abs((wax + waw) - (c->x + c->w + 2 * c->border)) < SNAP)
   1.754 +				c->x = wax + waw - c->w - 2 * c->border;
   1.755 +			if(abs(way - c->y) < SNAP)
   1.756 +				c->y = way;
   1.757 +			else if(abs((way + wah) - (c->y + c->h + 2 * c->border)) < SNAP)
   1.758 +				c->y = way + wah - c->h - 2 * c->border;
   1.759 +			resize(c, False);
   1.760 +			break;
   1.761 +		}
   1.762 +	}
   1.763 +}
   1.764 +
   1.765 +static void
   1.766 +resizemouse(Client *c) {
   1.767 +	int ocx, ocy;
   1.768 +	int nw, nh;
   1.769 +	XEvent ev;
   1.770 +
   1.771 +	ocx = c->x;
   1.772 +	ocy = c->y;
   1.773 +	if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
   1.774 +			None, cursor[CurResize], CurrentTime) != GrabSuccess)
   1.775 +		return;
   1.776 +	c->ismax = False;
   1.777 +	XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
   1.778 +	for(;;) {
   1.779 +		XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
   1.780 +		switch(ev.type) {
   1.781 +		case ButtonRelease:
   1.782 +			resize(c, True);
   1.783 +			XUngrabPointer(dpy, CurrentTime);
   1.784 +			return;
   1.785 +		case ConfigureRequest:
   1.786 +		case Expose:
   1.787 +		case MapRequest:
   1.788 +			handler[ev.type](&ev);
   1.789 +			break;
   1.790 +		case MotionNotify:
   1.791 +			XSync(dpy, False);
   1.792 +			nw = ev.xmotion.x - ocx - 2 * c->border + 1;
   1.793 +			c->w = nw > 0 ? nw : 1;
   1.794 +			nh = ev.xmotion.y - ocy - 2 * c->border + 1;
   1.795 +			c->h = nh > 0 ? nh : 1;
   1.796 +			resize(c, True);
   1.797 +			break;
   1.798 +		}
   1.799 +	}
   1.800 +}
   1.801 +
   1.802 +static void
   1.803 +buttonpress(XEvent *e) {
   1.804 +	int x;
   1.805 +	Arg a;
   1.806 +	Client *c;
   1.807 +	XButtonPressedEvent *ev = &e->xbutton;
   1.808 +
   1.809 +	if(barwin == ev->window) {
   1.810 +		x = 0;
   1.811 +		for(a.i = 0; a.i < ntags; a.i++) {
   1.812 +			x += textw(tags[a.i]);
   1.813 +			if(ev->x < x) {
   1.814 +				if(ev->button == Button1) {
   1.815 +					view(&a);
   1.816 +				}
   1.817 +				return;
   1.818 +			}
   1.819 +		}
   1.820 +		if(ev->x < x + bmw)
   1.821 +			if (ev->button == Button1) {
   1.822 +				togglemode(NULL);
   1.823 +			}
   1.824 +	}
   1.825 +	else if((c = getclient(ev->window))) {
   1.826 +		focus(c);
   1.827 +		if(CLEANMASK(ev->state) != MODKEY)
   1.828 +			return;
   1.829 +		if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
   1.830 +			restack();
   1.831 +			movemouse(c);
   1.832 +		}
   1.833 +		else if(ev->button == Button2)
   1.834 +			zoom(NULL);
   1.835 +		else if(ev->button == Button3 && (arrange == dofloat || c->isfloat) &&
   1.836 +				!c->isfixed) {
   1.837 +			restack();
   1.838 +			resizemouse(c);
   1.839 +		}
   1.840 +	}
   1.841 +}
   1.842 +
   1.843 +static void
   1.844 +configurerequest(XEvent *e) {
   1.845 +	unsigned long newmask;
   1.846 +	Client *c;
   1.847 +	XConfigureRequestEvent *ev = &e->xconfigurerequest;
   1.848 +	XWindowChanges wc;
   1.849 +
   1.850 +	if((c = getclient(ev->window))) {
   1.851 +		c->ismax = False;
   1.852 +		if(ev->value_mask & CWX)
   1.853 +			c->x = ev->x;
   1.854 +		if(ev->value_mask & CWY)
   1.855 +			c->y = ev->y;
   1.856 +		if(ev->value_mask & CWWidth)
   1.857 +			c->w = ev->width;
   1.858 +		if(ev->value_mask & CWHeight)
   1.859 +			c->h = ev->height;
   1.860 +		if(ev->value_mask & CWBorderWidth)
   1.861 +			c->border = ev->border_width;
   1.862 +		wc.x = c->x;
   1.863 +		wc.y = c->y;
   1.864 +		wc.width = c->w;
   1.865 +		wc.height = c->h;
   1.866 +		newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
   1.867 +		if(newmask)
   1.868 +			XConfigureWindow(dpy, c->win, newmask, &wc);
   1.869 +		else
   1.870 +			configure(c);
   1.871 +		XSync(dpy, False);
   1.872 +		if(c->isfloat) {
   1.873 +			resize(c, False);
   1.874 +			if(!isvisible(c))
   1.875 +				XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
   1.876 +		}
   1.877 +		else
   1.878 +			arrange();
   1.879 +	}
   1.880 +	else {
   1.881 +		wc.x = ev->x;
   1.882 +		wc.y = ev->y;
   1.883 +		wc.width = ev->width;
   1.884 +		wc.height = ev->height;
   1.885 +		wc.border_width = ev->border_width;
   1.886 +		wc.sibling = ev->above;
   1.887 +		wc.stack_mode = ev->detail;
   1.888 +		XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
   1.889 +		XSync(dpy, False);
   1.890 +	}
   1.891 +}
   1.892 +
   1.893 +static void
   1.894 +destroynotify(XEvent *e) {
   1.895 +	Client *c;
   1.896 +	XDestroyWindowEvent *ev = &e->xdestroywindow;
   1.897 +
   1.898 +	if((c = getclient(ev->window)))
   1.899 +		unmanage(c);
   1.900 +}
   1.901 +
   1.902 +static void
   1.903 +enternotify(XEvent *e) {
   1.904 +	Client *c;
   1.905 +	XCrossingEvent *ev = &e->xcrossing;
   1.906 +
   1.907 +	if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
   1.908 +		return;
   1.909 +	if((c = getclient(ev->window)) && isvisible(c))
   1.910 +		focus(c);
   1.911 +	else if(ev->window == root) {
   1.912 +		selscreen = True;
   1.913 +		for(c = stack; c && !isvisible(c); c = c->snext);
   1.914 +		focus(c);
   1.915 +	}
   1.916 +}
   1.917 +
   1.918 +static void
   1.919 +expose(XEvent *e) {
   1.920 +	XExposeEvent *ev = &e->xexpose;
   1.921 +
   1.922 +	if(ev->count == 0) {
   1.923 +		if(barwin == ev->window)
   1.924 +			drawstatus();
   1.925 +	}
   1.926 +}
   1.927 +
   1.928 +static void
   1.929 +keypress(XEvent *e) {
   1.930 +	static unsigned int len = sizeof key / sizeof key[0];
   1.931 +	unsigned int i;
   1.932 +	KeySym keysym;
   1.933 +	XKeyEvent *ev = &e->xkey;
   1.934 +
   1.935 +	keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
   1.936 +	for(i = 0; i < len; i++) {
   1.937 +		if(keysym == key[i].keysym
   1.938 +			&& CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
   1.939 +		{
   1.940 +			if(key[i].func)
   1.941 +				key[i].func(&key[i].arg);
   1.942 +		}
   1.943 +	}
   1.944 +}
   1.945 +
   1.946 +static void
   1.947 +leavenotify(XEvent *e) {
   1.948 +	XCrossingEvent *ev = &e->xcrossing;
   1.949 +
   1.950 +	if((ev->window == root) && !ev->same_screen) {
   1.951 +		selscreen = False;
   1.952 +		focus(NULL);
   1.953 +	}
   1.954 +}
   1.955 +
   1.956 +static void
   1.957 +mappingnotify(XEvent *e) {
   1.958 +	XMappingEvent *ev = &e->xmapping;
   1.959 +
   1.960 +	XRefreshKeyboardMapping(ev);
   1.961 +	if(ev->request == MappingKeyboard)
   1.962 +		grabkeys();
   1.963 +}
   1.964 +
   1.965 +static void
   1.966 +maprequest(XEvent *e) {
   1.967 +	static XWindowAttributes wa;
   1.968 +	XMapRequestEvent *ev = &e->xmaprequest;
   1.969 +
   1.970 +	if(!XGetWindowAttributes(dpy, ev->window, &wa))
   1.971 +		return;
   1.972 +	if(wa.override_redirect) {
   1.973 +		XSelectInput(dpy, ev->window,
   1.974 +				(StructureNotifyMask | PropertyChangeMask));
   1.975 +		return;
   1.976 +	}
   1.977 +	if(!getclient(ev->window))
   1.978 +		manage(ev->window, &wa);
   1.979 +}
   1.980 +
   1.981 +static void
   1.982 +propertynotify(XEvent *e) {
   1.983 +	Client *c;
   1.984 +	Window trans;
   1.985 +	XPropertyEvent *ev = &e->xproperty;
   1.986 +
   1.987 +	if(ev->state == PropertyDelete)
   1.988 +		return; /* ignore */
   1.989 +	if((c = getclient(ev->window))) {
   1.990 +		switch (ev->atom) {
   1.991 +			default: break;
   1.992 +			case XA_WM_TRANSIENT_FOR:
   1.993 +				XGetTransientForHint(dpy, c->win, &trans);
   1.994 +				if(!c->isfloat && (c->isfloat = (trans != 0)))
   1.995 +					arrange();
   1.996 +				break;
   1.997 +			case XA_WM_NORMAL_HINTS:
   1.998 +				updatesizehints(c);
   1.999 +				break;
  1.1000 +		}
  1.1001 +		if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1.1002 +			updatetitle(c);
  1.1003 +			if(c == sel)
  1.1004 +				drawstatus();
  1.1005 +		}
  1.1006 +	}
  1.1007 +}
  1.1008 +
  1.1009 +static void
  1.1010 +unmapnotify(XEvent *e) {
  1.1011 +	Client *c;
  1.1012 +	XUnmapEvent *ev = &e->xunmap;
  1.1013 +
  1.1014 +	if((c = getclient(ev->window)))
  1.1015 +		unmanage(c);
  1.1016 +}
  1.1017 +
  1.1018 +
  1.1019 +
  1.1020 +void (*handler[LASTEvent]) (XEvent *) = {
  1.1021 +	[ButtonPress] = buttonpress,
  1.1022 +	[ConfigureRequest] = configurerequest,
  1.1023 +	[DestroyNotify] = destroynotify,
  1.1024 +	[EnterNotify] = enternotify,
  1.1025 +	[LeaveNotify] = leavenotify,
  1.1026 +	[Expose] = expose,
  1.1027 +	[KeyPress] = keypress,
  1.1028 +	[MappingNotify] = mappingnotify,
  1.1029 +	[MapRequest] = maprequest,
  1.1030 +	[PropertyNotify] = propertynotify,
  1.1031 +	[UnmapNotify] = unmapnotify
  1.1032 +};
  1.1033 +
  1.1034 +void
  1.1035 +grabkeys(void) {
  1.1036 +	static unsigned int len = sizeof key / sizeof key[0];
  1.1037 +	unsigned int i;
  1.1038 +	KeyCode code;
  1.1039 +
  1.1040 +	XUngrabKey(dpy, AnyKey, AnyModifier, root);
  1.1041 +	for(i = 0; i < len; i++) {
  1.1042 +		code = XKeysymToKeycode(dpy, key[i].keysym);
  1.1043 +		XGrabKey(dpy, code, key[i].mod, root, True,
  1.1044 +				GrabModeAsync, GrabModeAsync);
  1.1045 +		XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
  1.1046 +				GrabModeAsync, GrabModeAsync);
  1.1047 +		XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
  1.1048 +				GrabModeAsync, GrabModeAsync);
  1.1049 +		XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
  1.1050 +				GrabModeAsync, GrabModeAsync);
  1.1051 +	}
  1.1052 +}
  1.1053 +
  1.1054 +void
  1.1055 +procevent(void) {
  1.1056 +	XEvent ev;
  1.1057 +
  1.1058 +	while(XPending(dpy)) {
  1.1059 +		XNextEvent(dpy, &ev);
  1.1060 +		if(handler[ev.type])
  1.1061 +			(handler[ev.type])(&ev); /* call handler */
  1.1062 +	}
  1.1063 +}
  1.1064 +
  1.1065 +
  1.1066 +
  1.1067 +
  1.1068 +
  1.1069 +
  1.1070 +
  1.1071 +
  1.1072 +
  1.1073 +
  1.1074 +
  1.1075 +
  1.1076 +
  1.1077 +
  1.1078 +
  1.1079 +/* from draw.c */
  1.1080 +/* static */
  1.1081 +
  1.1082 +static unsigned int
  1.1083 +textnw(const char *text, unsigned int len) {
  1.1084 +	XRectangle r;
  1.1085 +
  1.1086 +	if(dc.font.set) {
  1.1087 +		XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1.1088 +		return r.width;
  1.1089 +	}
  1.1090 +	return XTextWidth(dc.font.xfont, text, len);
  1.1091 +}
  1.1092 +
  1.1093 +static void
  1.1094 +drawtext(const char *text, unsigned long col[ColLast]) {
  1.1095 +	int x, y, w, h;
  1.1096 +	static char buf[256];
  1.1097 +	unsigned int len, olen;
  1.1098 +	XGCValues gcv;
  1.1099 +	XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  1.1100 +
  1.1101 +	XSetForeground(dpy, dc.gc, col[ColBG]);
  1.1102 +	XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  1.1103 +	if(!text)
  1.1104 +		return;
  1.1105 +	w = 0;
  1.1106 +	olen = len = strlen(text);
  1.1107 +	if(len >= sizeof buf)
  1.1108 +		len = sizeof buf - 1;
  1.1109 +	memcpy(buf, text, len);
  1.1110 +	buf[len] = 0;
  1.1111 +	h = dc.font.ascent + dc.font.descent;
  1.1112 +	y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  1.1113 +	x = dc.x + (h / 2);
  1.1114 +	/* shorten text if necessary */
  1.1115 +	while(len && (w = textnw(buf, len)) > dc.w - h)
  1.1116 +		buf[--len] = 0;
  1.1117 +	if(len < olen) {
  1.1118 +		if(len > 1)
  1.1119 +			buf[len - 1] = '.';
  1.1120 +		if(len > 2)
  1.1121 +			buf[len - 2] = '.';
  1.1122 +		if(len > 3)
  1.1123 +			buf[len - 3] = '.';
  1.1124 +	}
  1.1125 +	if(w > dc.w)
  1.1126 +		return; /* too long */
  1.1127 +	gcv.foreground = col[ColFG];
  1.1128 +	if(dc.font.set) {
  1.1129 +		XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  1.1130 +		XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  1.1131 +	} else {
  1.1132 +		gcv.font = dc.font.xfont->fid;
  1.1133 +		XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
  1.1134 +		XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  1.1135 +	}
  1.1136 +}
  1.1137 +
  1.1138 +
  1.1139 +
  1.1140 +void
  1.1141 +drawstatus(void) {
  1.1142 +	int i, x;
  1.1143 +
  1.1144 +	dc.x = dc.y = 0;
  1.1145 +	for(i = 0; i < ntags; i++) {
  1.1146 +		dc.w = textw(tags[i]);
  1.1147 +		drawtext(tags[i], (seltag[i] ? dc.sel : dc.norm));
  1.1148 +		dc.x += dc.w + 1;
  1.1149 +	}
  1.1150 +	dc.w = bmw;
  1.1151 +	drawtext("", dc.norm);
  1.1152 +	x = dc.x + dc.w;
  1.1153 +	dc.w = textw(stext);
  1.1154 +	dc.x = sw - dc.w;
  1.1155 +	if(dc.x < x) {
  1.1156 +		dc.x = x;
  1.1157 +		dc.w = sw - x;
  1.1158 +	}
  1.1159 +	drawtext(stext, dc.norm);
  1.1160 +	if((dc.w = dc.x - x) > bh) {
  1.1161 +		dc.x = x;
  1.1162 +		drawtext(sel ? sel->name : NULL, dc.norm);
  1.1163 +	}
  1.1164 +	XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0);
  1.1165 +	XSync(dpy, False);
  1.1166 +}
  1.1167 +
  1.1168 +unsigned long
  1.1169 +getcolor(const char *colstr) {
  1.1170 +	Colormap cmap = DefaultColormap(dpy, screen);
  1.1171 +	XColor color;
  1.1172 +
  1.1173 +	if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  1.1174 +		eprint("error, cannot allocate color '%s'\n", colstr);
  1.1175 +	return color.pixel;
  1.1176 +}
  1.1177 +
  1.1178 +void
  1.1179 +setfont(const char *fontstr) {
  1.1180 +	char *def, **missing;
  1.1181 +	int i, n;
  1.1182 +
  1.1183 +	missing = NULL;
  1.1184 +	if(dc.font.set)
  1.1185 +		XFreeFontSet(dpy, dc.font.set);
  1.1186 +	dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  1.1187 +	if(missing) {
  1.1188 +		while(n--)
  1.1189 +			fprintf(stderr, "missing fontset: %s\n", missing[n]);
  1.1190 +		XFreeStringList(missing);
  1.1191 +	}
  1.1192 +	if(dc.font.set) {
  1.1193 +		XFontSetExtents *font_extents;
  1.1194 +		XFontStruct **xfonts;
  1.1195 +		char **font_names;
  1.1196 +		dc.font.ascent = dc.font.descent = 0;
  1.1197 +		font_extents = XExtentsOfFontSet(dc.font.set);
  1.1198 +		n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  1.1199 +		for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  1.1200 +			if(dc.font.ascent < (*xfonts)->ascent)
  1.1201 +				dc.font.ascent = (*xfonts)->ascent;
  1.1202 +			if(dc.font.descent < (*xfonts)->descent)
  1.1203 +				dc.font.descent = (*xfonts)->descent;
  1.1204 +			xfonts++;
  1.1205 +		}
  1.1206 +	} else {
  1.1207 +		if(dc.font.xfont)
  1.1208 +			XFreeFont(dpy, dc.font.xfont);
  1.1209 +		dc.font.xfont = NULL;
  1.1210 +		if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
  1.1211 +			eprint("error, cannot load font: '%s'\n", fontstr);
  1.1212 +		dc.font.ascent = dc.font.xfont->ascent;
  1.1213 +		dc.font.descent = dc.font.xfont->descent;
  1.1214 +	}
  1.1215 +	dc.font.height = dc.font.ascent + dc.font.descent;
  1.1216 +}
  1.1217 +
  1.1218 +unsigned int
  1.1219 +textw(const char *text) {
  1.1220 +	return textnw(text, strlen(text)) + dc.font.height;
  1.1221 +}
  1.1222 +
  1.1223 +
  1.1224 +
  1.1225 +
  1.1226 +
  1.1227 +
  1.1228 +
  1.1229 +
  1.1230 +
  1.1231 +
  1.1232 +
  1.1233 +/* from client.c */
  1.1234 +/* static */
  1.1235 +
  1.1236 +static void
  1.1237 +detachstack(Client *c) {
  1.1238 +	Client **tc;
  1.1239 +	for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
  1.1240 +	*tc = c->snext;
  1.1241 +}
  1.1242 +
  1.1243 +static void
  1.1244 +grabbuttons(Client *c, Bool focused) {
  1.1245 +	XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1.1246 +
  1.1247 +	if(focused) {
  1.1248 +		XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
  1.1249 +				GrabModeAsync, GrabModeSync, None, None);
  1.1250 +		XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
  1.1251 +				GrabModeAsync, GrabModeSync, None, None);
  1.1252 +		XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  1.1253 +				GrabModeAsync, GrabModeSync, None, None);
  1.1254 +		XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  1.1255 +				GrabModeAsync, GrabModeSync, None, None);
  1.1256 +
  1.1257 +		XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
  1.1258 +				GrabModeAsync, GrabModeSync, None, None);
  1.1259 +		XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
  1.1260 +				GrabModeAsync, GrabModeSync, None, None);
  1.1261 +		XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  1.1262 +				GrabModeAsync, GrabModeSync, None, None);
  1.1263 +		XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  1.1264 +				GrabModeAsync, GrabModeSync, None, None);
  1.1265 +
  1.1266 +		XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
  1.1267 +				GrabModeAsync, GrabModeSync, None, None);
  1.1268 +		XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
  1.1269 +				GrabModeAsync, GrabModeSync, None, None);
  1.1270 +		XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  1.1271 +				GrabModeAsync, GrabModeSync, None, None);
  1.1272 +		XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  1.1273 +				GrabModeAsync, GrabModeSync, None, None);
  1.1274 +	} else {
  1.1275 +		XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
  1.1276 +				GrabModeAsync, GrabModeSync, None, None);
  1.1277 +	}
  1.1278 +}
  1.1279 +
  1.1280 +static void
  1.1281 +setclientstate(Client *c, long state) {
  1.1282 +	long data[] = {state, None};
  1.1283 +	XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1.1284 +			PropModeReplace, (unsigned char *)data, 2);
  1.1285 +}
  1.1286 +
  1.1287 +static int
  1.1288 +xerrordummy(Display *dsply, XErrorEvent *ee) {
  1.1289 +	return 0;
  1.1290 +}
  1.1291 +
  1.1292 +
  1.1293 +
  1.1294 +void
  1.1295 +configure(Client *c) {
  1.1296 +	XEvent synev;
  1.1297 +
  1.1298 +	synev.type = ConfigureNotify;
  1.1299 +	synev.xconfigure.display = dpy;
  1.1300 +	synev.xconfigure.event = c->win;
  1.1301 +	synev.xconfigure.window = c->win;
  1.1302 +	synev.xconfigure.x = c->x;
  1.1303 +	synev.xconfigure.y = c->y;
  1.1304 +	synev.xconfigure.width = c->w;
  1.1305 +	synev.xconfigure.height = c->h;
  1.1306 +	synev.xconfigure.border_width = c->border;
  1.1307 +	synev.xconfigure.above = None;
  1.1308 +	XSendEvent(dpy, c->win, True, NoEventMask, &synev);
  1.1309 +}
  1.1310 +
  1.1311 +void
  1.1312 +focus(Client *c) {
  1.1313 +	if(c && !isvisible(c))
  1.1314 +		return;
  1.1315 +	if(sel && sel != c) {
  1.1316 +		grabbuttons(sel, False);
  1.1317 +		XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
  1.1318 +	}
  1.1319 +	if(c) {
  1.1320 +		detachstack(c);
  1.1321 +		c->snext = stack;
  1.1322 +		stack = c;
  1.1323 +		grabbuttons(c, True);
  1.1324 +	}
  1.1325 +	sel = c;
  1.1326 +	drawstatus();
  1.1327 +	if(!selscreen)
  1.1328 +		return;
  1.1329 +	if(c) {
  1.1330 +		XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  1.1331 +		XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  1.1332 +	} else {
  1.1333 +		XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  1.1334 +	}
  1.1335 +}
  1.1336 +
  1.1337 +Client *
  1.1338 +getclient(Window w) {
  1.1339 +	Client *c;
  1.1340 +
  1.1341 +	for(c = clients; c; c = c->next) {
  1.1342 +		if(c->win == w) {
  1.1343 +			return c;
  1.1344 +		}
  1.1345 +	}
  1.1346 +	return NULL;
  1.1347 +}
  1.1348 +
  1.1349 +Bool
  1.1350 +isprotodel(Client *c) {
  1.1351 +	int i, n;
  1.1352 +	Atom *protocols;
  1.1353 +	Bool ret = False;
  1.1354 +
  1.1355 +	if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  1.1356 +		for(i = 0; !ret && i < n; i++)
  1.1357 +			if(protocols[i] == wmatom[WMDelete])
  1.1358 +				ret = True;
  1.1359 +		XFree(protocols);
  1.1360 +	}
  1.1361 +	return ret;
  1.1362 +}
  1.1363 +
  1.1364 +void
  1.1365 +killclient(Arg *arg) {
  1.1366 +	if(!sel)
  1.1367 +		return;
  1.1368 +	if(isprotodel(sel))
  1.1369 +		sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
  1.1370 +	else
  1.1371 +		XKillClient(dpy, sel->win);
  1.1372 +}
  1.1373 +
  1.1374 +void
  1.1375 +manage(Window w, XWindowAttributes *wa) {
  1.1376 +	Client *c;
  1.1377 +	Window trans;
  1.1378 +
  1.1379 +	c = emallocz(sizeof(Client));
  1.1380 +	c->tags = emallocz(ntags * sizeof(Bool));
  1.1381 +	c->win = w;
  1.1382 +	c->x = wa->x;
  1.1383 +	c->y = wa->y;
  1.1384 +	c->w = wa->width;
  1.1385 +	c->h = wa->height;
  1.1386 +	if(c->w == sw && c->h == sh) {
  1.1387 +		c->border = 0;
  1.1388 +		c->x = sx;
  1.1389 +		c->y = sy;
  1.1390 +	} else {
  1.1391 +		c->border = BORDERPX;
  1.1392 +		if(c->x + c->w + 2 * c->border > wax + waw)
  1.1393 +			c->x = wax + waw - c->w - 2 * c->border;
  1.1394 +		if(c->y + c->h + 2 * c->border > way + wah)
  1.1395 +			c->y = way + wah - c->h - 2 * c->border;
  1.1396 +		if(c->x < wax)
  1.1397 +			c->x = wax;
  1.1398 +		if(c->y < way)
  1.1399 +			c->y = way;
  1.1400 +	}
  1.1401 +	updatesizehints(c);
  1.1402 +	XSelectInput(dpy, c->win,
  1.1403 +		StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  1.1404 +	XGetTransientForHint(dpy, c->win, &trans);
  1.1405 +	grabbuttons(c, False);
  1.1406 +	XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
  1.1407 +	updatetitle(c);
  1.1408 +	settags(c, getclient(trans));
  1.1409 +	if(!c->isfloat)
  1.1410 +		c->isfloat = trans || c->isfixed;
  1.1411 +	if(clients)
  1.1412 +		clients->prev = c;
  1.1413 +	c->next = clients;
  1.1414 +	c->snext = stack;
  1.1415 +	stack = clients = c;
  1.1416 +	XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  1.1417 +	XMapWindow(dpy, c->win);
  1.1418 +	setclientstate(c, NormalState);
  1.1419 +	if(isvisible(c))
  1.1420 +		focus(c);
  1.1421 +	arrange();
  1.1422 +}
  1.1423 +
  1.1424 +void
  1.1425 +resize(Client *c, Bool sizehints) {
  1.1426 +	float actual, dx, dy, max, min;
  1.1427 +	XWindowChanges wc;
  1.1428 +
  1.1429 +	if(c->w <= 0 || c->h <= 0)
  1.1430 +		return;
  1.1431 +	if(sizehints) {
  1.1432 +		if(c->minw && c->w < c->minw)
  1.1433 +			c->w = c->minw;
  1.1434 +		if(c->minh && c->h < c->minh)
  1.1435 +			c->h = c->minh;
  1.1436 +		if(c->maxw && c->w > c->maxw)
  1.1437 +			c->w = c->maxw;
  1.1438 +		if(c->maxh && c->h > c->maxh)
  1.1439 +			c->h = c->maxh;
  1.1440 +		/* inspired by algorithm from fluxbox */
  1.1441 +		if(c->minay > 0 && c->maxay && (c->h - c->baseh) > 0) {
  1.1442 +			dx = (float)(c->w - c->basew);
  1.1443 +			dy = (float)(c->h - c->baseh);
  1.1444 +			min = (float)(c->minax) / (float)(c->minay);
  1.1445 +			max = (float)(c->maxax) / (float)(c->maxay);
  1.1446 +			actual = dx / dy;
  1.1447 +			if(max > 0 && min > 0 && actual > 0) {
  1.1448 +				if(actual < min) {
  1.1449 +					dy = (dx * min + dy) / (min * min + 1);
  1.1450 +					dx = dy * min;
  1.1451 +					c->w = (int)dx + c->basew;
  1.1452 +					c->h = (int)dy + c->baseh;
  1.1453 +				}
  1.1454 +				else if(actual > max) {
  1.1455 +					dy = (dx * min + dy) / (max * max + 1);
  1.1456 +					dx = dy * min;
  1.1457 +					c->w = (int)dx + c->basew;
  1.1458 +					c->h = (int)dy + c->baseh;
  1.1459 +				}
  1.1460 +			}
  1.1461 +		}
  1.1462 +		if(c->incw)
  1.1463 +			c->w -= (c->w - c->basew) % c->incw;
  1.1464 +		if(c->inch)
  1.1465 +			c->h -= (c->h - c->baseh) % c->inch;
  1.1466 +	}
  1.1467 +	if(c->w == sw && c->h == sh)
  1.1468 +		c->border = 0;
  1.1469 +	else
  1.1470 +		c->border = BORDERPX;
  1.1471 +	/* offscreen appearance fixes */
  1.1472 +	if(c->x > sw)
  1.1473 +		c->x = sw - c->w - 2 * c->border;
  1.1474 +	if(c->y > sh)
  1.1475 +		c->y = sh - c->h - 2 * c->border;
  1.1476 +	if(c->x + c->w + 2 * c->border < sx)
  1.1477 +		c->x = sx;
  1.1478 +	if(c->y + c->h + 2 * c->border < sy)
  1.1479 +		c->y = sy;
  1.1480 +	wc.x = c->x;
  1.1481 +	wc.y = c->y;
  1.1482 +	wc.width = c->w;
  1.1483 +	wc.height = c->h;
  1.1484 +	wc.border_width = c->border;
  1.1485 +	XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  1.1486 +	configure(c);
  1.1487 +	XSync(dpy, False);
  1.1488 +}
  1.1489 +
  1.1490 +void
  1.1491 +updatesizehints(Client *c) {
  1.1492 +	long msize;
  1.1493 +	XSizeHints size;
  1.1494 +
  1.1495 +	if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  1.1496 +		size.flags = PSize;
  1.1497 +	c->flags = size.flags;
  1.1498 +	if(c->flags & PBaseSize) {
  1.1499 +		c->basew = size.base_width;
  1.1500 +		c->baseh = size.base_height;
  1.1501 +	} else {
  1.1502 +		c->basew = c->baseh = 0;
  1.1503 +	}
  1.1504 +	if(c->flags & PResizeInc) {
  1.1505 +		c->incw = size.width_inc;
  1.1506 +		c->inch = size.height_inc;
  1.1507 +	} else {
  1.1508 +		c->incw = c->inch = 0;
  1.1509 +	}
  1.1510 +	if(c->flags & PMaxSize) {
  1.1511 +		c->maxw = size.max_width;
  1.1512 +		c->maxh = size.max_height;
  1.1513 +	} else {
  1.1514 +		c->maxw = c->maxh = 0;
  1.1515 +	}
  1.1516 +	if(c->flags & PMinSize) {
  1.1517 +		c->minw = size.min_width;
  1.1518 +		c->minh = size.min_height;
  1.1519 +	} else {
  1.1520 +		c->minw = c->minh = 0;
  1.1521 +	}
  1.1522 +	if(c->flags & PAspect) {
  1.1523 +		c->minax = size.min_aspect.x;
  1.1524 +		c->minay = size.min_aspect.y;
  1.1525 +		c->maxax = size.max_aspect.x;
  1.1526 +		c->maxay = size.max_aspect.y;
  1.1527 +	} else {
  1.1528 +		c->minax = c->minay = c->maxax = c->maxay = 0;
  1.1529 +	}
  1.1530 +	c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
  1.1531 +				c->maxw == c->minw && c->maxh == c->minh);
  1.1532 +}
  1.1533 +
  1.1534 +void
  1.1535 +updatetitle(Client *c) {
  1.1536 +	char **list = NULL;
  1.1537 +	int n;
  1.1538 +	XTextProperty name;
  1.1539 +
  1.1540 +	name.nitems = 0;
  1.1541 +	c->name[0] = 0;
  1.1542 +	XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  1.1543 +	if(!name.nitems)
  1.1544 +		XGetWMName(dpy, c->win, &name);
  1.1545 +	if(!name.nitems)
  1.1546 +		return;
  1.1547 +	if(name.encoding == XA_STRING)
  1.1548 +		strncpy(c->name, (char *)name.value, sizeof c->name);
  1.1549 +	else {
  1.1550 +		if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
  1.1551 +			strncpy(c->name, *list, sizeof c->name);
  1.1552 +			XFreeStringList(list);
  1.1553 +		}
  1.1554 +	}
  1.1555 +	XFree(name.value);
  1.1556 +}
  1.1557 +
  1.1558 +void
  1.1559 +unmanage(Client *c) {
  1.1560 +	Client *nc;
  1.1561 +
  1.1562 +	/* The server grab construct avoids race conditions. */
  1.1563 +	XGrabServer(dpy);
  1.1564 +	XSetErrorHandler(xerrordummy);
  1.1565 +	detach(c);
  1.1566 +	detachstack(c);
  1.1567 +	if(sel == c) {
  1.1568 +		for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
  1.1569 +		focus(nc);
  1.1570 +	}
  1.1571 +	XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1.1572 +	setclientstate(c, WithdrawnState);
  1.1573 +	free(c->tags);
  1.1574 +	free(c);
  1.1575 +	XSync(dpy, False);
  1.1576 +	XSetErrorHandler(xerror);
  1.1577 +	XUngrabServer(dpy);
  1.1578 +	arrange();
  1.1579 +}
  1.1580 +
  1.1581 +
  1.1582 +
  1.1583 +
  1.1584 +
  1.1585 +
  1.1586 +
  1.1587 +
  1.1588 +
  1.1589 +
  1.1590 +
  1.1591 +
  1.1592 +
  1.1593 +
  1.1594 +
  1.1595 +
  1.1596 +
  1.1597 +
  1.1598 +
  1.1599 +/* static */
  1.1600 +
  1.1601 +
  1.1602 +static void
  1.1603 +cleanup(void) {
  1.1604 +	close(STDIN_FILENO);
  1.1605 +	while(stack) {
  1.1606 +		resize(stack, True);
  1.1607 +		unmanage(stack);
  1.1608 +	}
  1.1609 +	if(dc.font.set)
  1.1610 +		XFreeFontSet(dpy, dc.font.set);
  1.1611 +	else
  1.1612 +		XFreeFont(dpy, dc.font.xfont);
  1.1613 +	XUngrabKey(dpy, AnyKey, AnyModifier, root);
  1.1614 +	XFreePixmap(dpy, dc.drawable);
  1.1615 +	XFreeGC(dpy, dc.gc);
  1.1616 +	XDestroyWindow(dpy, barwin);
  1.1617 +	XFreeCursor(dpy, cursor[CurNormal]);
  1.1618 +	XFreeCursor(dpy, cursor[CurResize]);
  1.1619 +	XFreeCursor(dpy, cursor[CurMove]);
  1.1620 +	XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  1.1621 +	XSync(dpy, False);
  1.1622 +	free(seltag);
  1.1623 +}
  1.1624 +
  1.1625 +static void
  1.1626 +scan(void) {
  1.1627 +	unsigned int i, num;
  1.1628 +	Window *wins, d1, d2;
  1.1629 +	XWindowAttributes wa;
  1.1630 +
  1.1631 +	wins = NULL;
  1.1632 +	if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1.1633 +		for(i = 0; i < num; i++) {
  1.1634 +			if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1.1635 +				continue;
  1.1636 +			if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1.1637 +				continue;
  1.1638 +			if(wa.map_state == IsViewable)
  1.1639 +				manage(wins[i], &wa);
  1.1640 +		}
  1.1641 +	}
  1.1642 +	if(wins)
  1.1643 +		XFree(wins);
  1.1644 +}
  1.1645 +
  1.1646 +static void
  1.1647 +setup(void) {
  1.1648 +	int i, j;
  1.1649 +	unsigned int mask;
  1.1650 +	Window w;
  1.1651 +	XModifierKeymap *modmap;
  1.1652 +	XSetWindowAttributes wa;
  1.1653 +
  1.1654 +	/* init atoms */
  1.1655 +	wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1.1656 +	wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1.1657 +	wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1.1658 +	netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1.1659 +	netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1.1660 +	XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1.1661 +			PropModeReplace, (unsigned char *) netatom, NetLast);
  1.1662 +	/* init cursors */
  1.1663 +	cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1.1664 +	cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1.1665 +	cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1.1666 +	/* init modifier map */
  1.1667 +	numlockmask = 0;
  1.1668 +	modmap = XGetModifierMapping(dpy);
  1.1669 +	for (i = 0; i < 8; i++) {
  1.1670 +		for (j = 0; j < modmap->max_keypermod; j++) {
  1.1671 +			if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
  1.1672 +				numlockmask = (1 << i);
  1.1673 +		}
  1.1674 +	}
  1.1675 +	XFreeModifiermap(modmap);
  1.1676 +	/* select for events */
  1.1677 +	wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
  1.1678 +		| EnterWindowMask | LeaveWindowMask;
  1.1679 +	wa.cursor = cursor[CurNormal];
  1.1680 +	XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
  1.1681 +	grabkeys();
  1.1682 +	initrregs();
  1.1683 +	for(ntags = 0; tags[ntags]; ntags++);
  1.1684 +	seltag = emallocz(sizeof(Bool) * ntags);
  1.1685 +	seltag[0] = True;
  1.1686 +	/* style */
  1.1687 +	dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
  1.1688 +	dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  1.1689 +	dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  1.1690 +	dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
  1.1691 +	dc.sel[ColBG] = getcolor(SELBGCOLOR);
  1.1692 +	dc.sel[ColFG] = getcolor(SELFGCOLOR);
  1.1693 +	setfont(FONT);
  1.1694 +	/* geometry */
  1.1695 +	sx = sy = 0;
  1.1696 +	sw = DisplayWidth(dpy, screen);
  1.1697 +	sh = DisplayHeight(dpy, screen);
  1.1698 +	nmaster = NMASTER;
  1.1699 +	bmw = 1;
  1.1700 +	/* bar */
  1.1701 +	dc.h = bh = dc.font.height + 2;
  1.1702 +	wa.override_redirect = 1;
  1.1703 +	wa.background_pixmap = ParentRelative;
  1.1704 +	wa.event_mask = ButtonPressMask | ExposureMask;
  1.1705 +	barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0,
  1.1706 +			DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
  1.1707 +			CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  1.1708 +	XDefineCursor(dpy, barwin, cursor[CurNormal]);
  1.1709 +	XMapRaised(dpy, barwin);
  1.1710 +	strcpy(stext, "dwm-"VERSION);
  1.1711 +	/* windowarea */
  1.1712 +	wax = sx;
  1.1713 +	way = sy + bh;
  1.1714 +	wah = sh - bh;
  1.1715 +	waw = sw;
  1.1716 +	/* pixmap for everything */
  1.1717 +	dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  1.1718 +	dc.gc = XCreateGC(dpy, root, 0, 0);
  1.1719 +	XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1.1720 +	/* multihead support */
  1.1721 +	selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
  1.1722 +}
  1.1723 +
  1.1724 +/*
  1.1725 + * Startup Error handler to check if another window manager
  1.1726 + * is already running.
  1.1727 + */
  1.1728 +static int
  1.1729 +xerrorstart(Display *dsply, XErrorEvent *ee) {
  1.1730 +	otherwm = True;
  1.1731 +	return -1;
  1.1732 +}
  1.1733 +
  1.1734 +
  1.1735 +
  1.1736 +void
  1.1737 +sendevent(Window w, Atom a, long value) {
  1.1738 +	XEvent e;
  1.1739 +
  1.1740 +	e.type = ClientMessage;
  1.1741 +	e.xclient.window = w;
  1.1742 +	e.xclient.message_type = a;
  1.1743 +	e.xclient.format = 32;
  1.1744 +	e.xclient.data.l[0] = value;
  1.1745 +	e.xclient.data.l[1] = CurrentTime;
  1.1746 +	XSendEvent(dpy, w, False, NoEventMask, &e);
  1.1747 +	XSync(dpy, False);
  1.1748 +}
  1.1749 +
  1.1750 +void
  1.1751 +quit(Arg *arg) {
  1.1752 +	readin = running = False;
  1.1753 +}
  1.1754 +
  1.1755 +/* There's no way to check accesses to destroyed windows, thus those cases are
  1.1756 + * ignored (especially on UnmapNotify's).  Other types of errors call Xlibs
  1.1757 + * default error handler, which may call exit.
  1.1758 + */
  1.1759 +int
  1.1760 +xerror(Display *dpy, XErrorEvent *ee) {
  1.1761 +	if(ee->error_code == BadWindow
  1.1762 +	|| (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1.1763 +	|| (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1.1764 +	|| (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1.1765 +	|| (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1.1766 +	|| (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1.1767 +	|| (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1.1768 +	|| (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1.1769 +		return 0;
  1.1770 +	fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1.1771 +		ee->request_code, ee->error_code);
  1.1772 +	return xerrorxlib(dpy, ee); /* may call exit */
  1.1773 +}
  1.1774 +
  1.1775 +int
  1.1776 +main(int argc, char *argv[]) {
  1.1777 +	char *p;
  1.1778 +	int r, xfd;
  1.1779 +	fd_set rd;
  1.1780 +
  1.1781 +	if(argc == 2 && !strncmp("-v", argv[1], 3)) {
  1.1782 +		fputs("dwm-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n", stdout);
  1.1783 +		exit(EXIT_SUCCESS);
  1.1784 +	}
  1.1785 +	else if(argc != 1)
  1.1786 +		eprint("usage: dwm [-v]\n");
  1.1787 +	setlocale(LC_CTYPE, "");
  1.1788 +	dpy = XOpenDisplay(0);
  1.1789 +	if(!dpy)
  1.1790 +		eprint("dwm: cannot open display\n");
  1.1791 +	xfd = ConnectionNumber(dpy);
  1.1792 +	screen = DefaultScreen(dpy);
  1.1793 +	root = RootWindow(dpy, screen);
  1.1794 +	otherwm = False;
  1.1795 +	XSetErrorHandler(xerrorstart);
  1.1796 +	/* this causes an error if some other window manager is running */
  1.1797 +	XSelectInput(dpy, root, SubstructureRedirectMask);
  1.1798 +	XSync(dpy, False);
  1.1799 +	if(otherwm)
  1.1800 +		eprint("dwm: another window manager is already running\n");
  1.1801 +
  1.1802 +	XSync(dpy, False);
  1.1803 +	XSetErrorHandler(NULL);
  1.1804 +	xerrorxlib = XSetErrorHandler(xerror);
  1.1805 +	XSync(dpy, False);
  1.1806 +	setup();
  1.1807 +	drawstatus();
  1.1808 +	scan();
  1.1809 +
  1.1810 +	/* main event loop, also reads status text from stdin */
  1.1811 +	XSync(dpy, False);
  1.1812 +	procevent();
  1.1813 +	readin = True;
  1.1814 +	while(running) {
  1.1815 +		FD_ZERO(&rd);
  1.1816 +		if(readin)
  1.1817 +			FD_SET(STDIN_FILENO, &rd);
  1.1818 +		FD_SET(xfd, &rd);
  1.1819 +		if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  1.1820 +			if(errno == EINTR)
  1.1821 +				continue;
  1.1822 +			eprint("select failed\n");
  1.1823 +		}
  1.1824 +		if(FD_ISSET(STDIN_FILENO, &rd)) {
  1.1825 +			switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
  1.1826 +			case -1:
  1.1827 +				strncpy(stext, strerror(errno), sizeof stext - 1);
  1.1828 +				stext[sizeof stext - 1] = '\0';
  1.1829 +				readin = False;
  1.1830 +				break;
  1.1831 +			case 0:
  1.1832 +				strncpy(stext, "EOF", 4);
  1.1833 +				readin = False;
  1.1834 +				break;
  1.1835 +			default:
  1.1836 +				for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
  1.1837 +				for(; p >= stext && *p != '\n'; --p);
  1.1838 +				if(p > stext)
  1.1839 +					strncpy(stext, p + 1, sizeof stext);
  1.1840 +			}
  1.1841 +			drawstatus();
  1.1842 +		}
  1.1843 +		if(FD_ISSET(xfd, &rd))
  1.1844 +			procevent();
  1.1845 +	}
  1.1846 +	cleanup();
  1.1847 +	XCloseDisplay(dpy);
  1.1848 +	return 0;
  1.1849 +}