dwm

Suckless' dwm with my personal patches
git clone https://git.instinctive.eu/dwm.git
Log | Files | Refs | README | LICENSE

dwm.c (74250B)


      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  * The event handlers of dwm are organized in an array which is accessed
     10  * whenever a new event has been fetched. This allows event dispatching
     11  * in O(1) time.
     12  *
     13  * Each child of the root window is called a client, except windows which have
     14  * set the override_redirect flag. Clients are organized in a linked client
     15  * list on each monitor, the focus history is remembered through a stack list
     16  * on each monitor. Each client contains a bit array to indicate the tags of a
     17  * client.
     18  *
     19  * Keys and tagging rules are organized as arrays and defined in config.h.
     20  *
     21  * To understand everything else, start reading main().
     22  */
     23 #include <errno.h>
     24 #include <locale.h>
     25 #include <signal.h>
     26 #include <stdarg.h>
     27 #include <stdio.h>
     28 #include <stdlib.h>
     29 #include <string.h>
     30 #include <unistd.h>
     31 #include <sys/types.h>
     32 #include <sys/stat.h>
     33 #include <sys/wait.h>
     34 #include <X11/cursorfont.h>
     35 #include <X11/keysym.h>
     36 #include <X11/Xatom.h>
     37 #include <X11/Xlib.h>
     38 #include <X11/Xproto.h>
     39 #include <X11/Xutil.h>
     40 #ifdef XINERAMA
     41 #include <X11/extensions/Xinerama.h>
     42 #endif /* XINERAMA */
     43 #include <X11/Xft/Xft.h>
     44 
     45 #include "drw.h"
     46 #include "util.h"
     47 
     48 /* macros */
     49 #define BUTTONMASK              (ButtonPressMask|ButtonReleaseMask)
     50 #define CLEANMASK(mask)         (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
     51 #define INTERSECT(x,y,w,h,m)    (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
     52                                * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
     53 #define ISVISIBLE(C)            ((C->tags & C->mon->tagset[C->mon->seltags]))
     54 #define MOUSEMASK               (BUTTONMASK|PointerMotionMask)
     55 #define WIDTH(X)                ((X)->w + 2 * (X)->bw)
     56 #define HEIGHT(X)               ((X)->h + 2 * (X)->bw)
     57 #define TAGMASK                 ((1 << LENGTH(tags)) - 1)
     58 #define TEXTW(X)                (drw_fontset_getwidth(drw, (X)) + lrpad)
     59 
     60 /* enums */
     61 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
     62 enum { SchemeNorm, SchemeSel }; /* color schemes */
     63 enum { NetSupported, NetWMName, NetWMState, NetWMCheck,
     64        NetWMFullscreen, NetActiveWindow, NetWMWindowType,
     65        NetWMWindowTypeDialog, NetClientList, NetClientListStacking, NetLast }; /* EWMH atoms */
     66 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
     67 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
     68        ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
     69 
     70 typedef union {
     71 	int i;
     72 	unsigned int ui;
     73 	float f;
     74 	const void *v;
     75 } Arg;
     76 
     77 typedef struct {
     78 	unsigned int click;
     79 	unsigned int mask;
     80 	unsigned int button;
     81 	void (*func)(const Arg *arg);
     82 	const Arg arg;
     83 } Button;
     84 
     85 typedef struct Monitor Monitor;
     86 typedef struct Client Client;
     87 struct Client {
     88 	char name[256];
     89 	float mina, maxa;
     90 	int x, y, w, h;
     91 	int oldx, oldy, oldw, oldh;
     92 	int basew, baseh, incw, inch, maxw, maxh, minw, minh, hintsvalid;
     93 	int bw, oldbw;
     94 	int barx, barw;
     95 	unsigned int tags;
     96 	int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
     97 	Client *next;
     98 	Client *snext;
     99 	Monitor *mon;
    100 	Window win;
    101 };
    102 
    103 typedef struct {
    104 	unsigned int mod;
    105 	KeySym keysym;
    106 	void (*func)(const Arg *);
    107 	const Arg arg;
    108 } Key;
    109 
    110 typedef struct {
    111 	const char *symbol;
    112 	void (*arrange)(Monitor *);
    113 } Layout;
    114 
    115 typedef struct Pertag Pertag;
    116 struct Monitor {
    117 	char ltsymbol[16];
    118 	float mfact;
    119 	int nmaster;
    120 	int num;
    121 	int by;               /* bar geometry */
    122 	int mx, my, mw, mh;   /* screen size */
    123 	int wx, wy, ww, wh;   /* window area  */
    124 	unsigned int seltags;
    125 	unsigned int sellt;
    126 	unsigned int tagset[2];
    127 	int showbar;
    128 	int topbar;
    129 	Client *clients;
    130 	Client *sel;
    131 	Client *stack;
    132 	Monitor *next;
    133 	Window barwin;
    134 	const Layout *lt[2];
    135 	Pertag *pertag;
    136 };
    137 
    138 typedef struct {
    139 	const char *class;
    140 	const char *instance;
    141 	const char *title;
    142 	unsigned int tags;
    143 	int isfloating;
    144 	int monitor;
    145 } Rule;
    146 
    147 /* function declarations */
    148 static void applyrules(Client *c);
    149 static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact);
    150 static void arrange(Monitor *m);
    151 static void arrangemon(Monitor *m);
    152 static void attach(Client *c);
    153 static void attachbottom(Client *c);
    154 static void attachstack(Client *c);
    155 static void buttonpress(XEvent *e);
    156 static void checkotherwm(void);
    157 static void cleanup(void);
    158 static void cleanupmon(Monitor *mon);
    159 static void clientmessage(XEvent *e);
    160 static void configure(Client *c);
    161 static void configurenotify(XEvent *e);
    162 static void configurerequest(XEvent *e);
    163 static Monitor *createmon(void);
    164 static void deck(Monitor *m);
    165 static void destroynotify(XEvent *e);
    166 static void detach(Client *c);
    167 static void detachstack(Client *c);
    168 static Monitor *dirtomon(int dir);
    169 static void drawbar(Monitor *m);
    170 static void drawbars(void);
    171 static void enternotify(XEvent *e);
    172 static void expose(XEvent *e);
    173 static void focus(Client *c);
    174 static void focusin(XEvent *e);
    175 static void focusmon(const Arg *arg);
    176 static void focusstack(const Arg *arg);
    177 static void focustitle(const Arg *arg);
    178 static Atom getatomprop(Client *c, Atom prop);
    179 static Client *getclientundermouse(void);
    180 static int getrootptr(int *x, int *y);
    181 static long getstate(Window w);
    182 static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
    183 static void grabbuttons(Client *c, int focused);
    184 static void grabkeys(void);
    185 static void incnmaster(const Arg *arg);
    186 static void keypress(XEvent *e);
    187 static void killclient(const Arg *arg);
    188 static void manage(Window w, XWindowAttributes *wa);
    189 static void mappingnotify(XEvent *e);
    190 static void maprequest(XEvent *e);
    191 static void monocle(Monitor *m);
    192 static void motionnotify(XEvent *e);
    193 static void movemouse(const Arg *arg);
    194 static Client *nexttiled(Client *c);
    195 static void pixeldeckcol(Monitor *m);
    196 static void pixelrow(Monitor *m);
    197 static void pop(Client *c);
    198 static void propertynotify(XEvent *e);
    199 static void quit(const Arg *arg);
    200 static Monitor *recttomon(int x, int y, int w, int h);
    201 static void resize(Client *c, int x, int y, int w, int h, int interact);
    202 static void resizeclient(Client *c, int x, int y, int w, int h);
    203 static void resizemouse(const Arg *arg);
    204 static void restack(Monitor *m);
    205 static void run(void);
    206 static void runautostart(void);
    207 static void scan(void);
    208 static int sendevent(Client *c, Atom proto);
    209 static void sendmon(Client *c, Monitor *m);
    210 static void setclientstate(Client *c, long state);
    211 static void setfocus(Client *c);
    212 static void setfullscreen(Client *c, int fullscreen);
    213 static void setlayout(const Arg *arg);
    214 static void setmark(Client *c);    
    215 static void setmfact(const Arg *arg);
    216 static void setup(void);
    217 static void seturgent(Client *c, int urg);
    218 static void showhide(Client *c);
    219 static void sighup(int unused);
    220 static void sigterm(int unused);
    221 static void spawn(const Arg *arg);
    222 static void swapclient(const Arg *arg);
    223 static void swapfocus(const Arg *arg);
    224 static void swapmon(const Arg *arg);
    225 static void swapmonvisible(const Arg *arg);
    226 static void tag(const Arg *arg);
    227 static void tagmon(const Arg *arg);
    228 static void tile(Monitor *m);
    229 static void togglebar(const Arg *arg);
    230 static void togglefloating(const Arg *arg);
    231 static void togglemark(const Arg *arg);
    232 static void togglescratch(const Arg *arg);
    233 static void toggletag(const Arg *arg);
    234 static void toggleview(const Arg *arg);
    235 static void unfocus(Client *c, int setfocus);
    236 static void unmanage(Client *c, int destroyed);
    237 static void unmapnotify(XEvent *e);
    238 static void updatebarpos(Monitor *m);
    239 static void updatebars(void);
    240 static void updateclientlist(void);
    241 static int updategeom(void);
    242 static void updatenumlockmask(void);
    243 static void updatesizehints(Client *c);
    244 static void updatestatus(void);
    245 static void updatetitle(Client *c);
    246 static void updatewindowtype(Client *c);
    247 static void updatewmhints(Client *c);
    248 static void view(const Arg *arg);
    249 static void warp(const Client *c);
    250 static Client *wintoclient(Window w);
    251 static Monitor *wintomon(Window w);
    252 static int xerror(Display *dpy, XErrorEvent *ee);
    253 static int xerrordummy(Display *dpy, XErrorEvent *ee);
    254 static int xerrorstart(Display *dpy, XErrorEvent *ee);
    255 static void zoom(const Arg *arg);
    256 
    257 static void focusmaster(const Arg *arg);
    258 
    259 /* variables */
    260 static const char autostartblocksh[] = "autostart_blocking.sh";
    261 static const char autostartsh[] = "autostart.sh";
    262 static const char broken[] = "broken";
    263 static const char dwmdir[] = "dwm";
    264 static const char localshare[] = ".local/share";
    265 static char stext[256];
    266 static int screen;
    267 static int sw, sh;           /* X display screen geometry width, height */
    268 static int bh;               /* bar height */
    269 static int lrpad;            /* sum of left and right padding for text */
    270 static int (*xerrorxlib)(Display *, XErrorEvent *);
    271 static unsigned int numlockmask = 0;
    272 static void (*handler[LASTEvent]) (XEvent *) = {
    273 	[ButtonPress] = buttonpress,
    274 	[ClientMessage] = clientmessage,
    275 	[ConfigureRequest] = configurerequest,
    276 	[ConfigureNotify] = configurenotify,
    277 	[DestroyNotify] = destroynotify,
    278 	[EnterNotify] = enternotify,
    279 	[Expose] = expose,
    280 	[FocusIn] = focusin,
    281 	[KeyPress] = keypress,
    282 	[MappingNotify] = mappingnotify,
    283 	[MapRequest] = maprequest,
    284 	[MotionNotify] = motionnotify,
    285 	[PropertyNotify] = propertynotify,
    286 	[UnmapNotify] = unmapnotify
    287 };
    288 static Atom wmatom[WMLast], netatom[NetLast];
    289 static int restart = 0;
    290 static int running = 1;
    291 static Cur *cursor[CurLast];
    292 static Clr **scheme;
    293 static Display *dpy;
    294 static Drw *drw;
    295 static Monitor *mons, *selmon;
    296 static Window root, wmcheckwin;
    297 static Client *mark;
    298 
    299 /* configuration, allows nested code to access above variables */
    300 #include "config.h"
    301 
    302 struct Pertag {
    303 	unsigned int curtag, prevtag; /* current and previous tag */
    304 	int nmasters[LENGTH(tags) + 1]; /* number of windows in master area */
    305 	float mfacts[LENGTH(tags) + 1]; /* mfacts per tag */
    306 	unsigned int sellts[LENGTH(tags) + 1]; /* selected layouts */
    307 	const Layout *ltidxs[LENGTH(tags) + 1][2]; /* matrix of tags and layouts indexes  */
    308 	int showbars[LENGTH(tags) + 1]; /* display bar for the current tag */
    309 };
    310 
    311 static unsigned int scratchtag = 1 << LENGTH(tags);
    312 
    313 /* compile-time check if all tags fit into an unsigned int bit array. */
    314 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
    315 
    316 /* function implementations */
    317 void
    318 applyrules(Client *c)
    319 {
    320 	const char *class, *instance;
    321 	unsigned int i;
    322 	const Rule *r;
    323 	Monitor *m;
    324 	XClassHint ch = { NULL, NULL };
    325 
    326 	/* rule matching */
    327 	c->isfloating = 0;
    328 	c->tags = 0;
    329 	XGetClassHint(dpy, c->win, &ch);
    330 	class    = ch.res_class ? ch.res_class : broken;
    331 	instance = ch.res_name  ? ch.res_name  : broken;
    332 
    333 	for (i = 0; i < LENGTH(rules); i++) {
    334 		r = &rules[i];
    335 		if ((!r->title || strstr(c->name, r->title))
    336 		&& (!r->class || strstr(class, r->class))
    337 		&& (!r->instance || strstr(instance, r->instance)))
    338 		{
    339 			c->isfloating = r->isfloating;
    340 			c->tags |= r->tags;
    341 			for (m = mons; m && m->num != r->monitor; m = m->next);
    342 			if (m)
    343 				c->mon = m;
    344 		}
    345 	}
    346 	if (ch.res_class)
    347 		XFree(ch.res_class);
    348 	if (ch.res_name)
    349 		XFree(ch.res_name);
    350 	c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
    351 }
    352 
    353 int
    354 applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact)
    355 {
    356 	int baseismin;
    357 	Monitor *m = c->mon;
    358 
    359 	/* set minimum possible */
    360 	*w = MAX(1, *w);
    361 	*h = MAX(1, *h);
    362 	if (interact) {
    363 		if (*x > sw)
    364 			*x = sw - WIDTH(c);
    365 		if (*y > sh)
    366 			*y = sh - HEIGHT(c);
    367 		if (*x + *w + 2 * c->bw < 0)
    368 			*x = 0;
    369 		if (*y + *h + 2 * c->bw < 0)
    370 			*y = 0;
    371 	} else {
    372 		if (*x >= m->wx + m->ww)
    373 			*x = m->wx + m->ww - WIDTH(c);
    374 		if (*y >= m->wy + m->wh)
    375 			*y = m->wy + m->wh - HEIGHT(c);
    376 		if (*x + *w + 2 * c->bw <= m->wx)
    377 			*x = m->wx;
    378 		if (*y + *h + 2 * c->bw <= m->wy)
    379 			*y = m->wy;
    380 	}
    381 	if (*h < bh)
    382 		*h = bh;
    383 	if (*w < bh)
    384 		*w = bh;
    385 	if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
    386 		if (!c->hintsvalid)
    387 			updatesizehints(c);
    388 		/* see last two sentences in ICCCM 4.1.2.3 */
    389 		baseismin = c->basew == c->minw && c->baseh == c->minh;
    390 		if (!baseismin) { /* temporarily remove base dimensions */
    391 			*w -= c->basew;
    392 			*h -= c->baseh;
    393 		}
    394 		/* adjust for aspect limits */
    395 		if (c->mina > 0 && c->maxa > 0) {
    396 			if (c->maxa < (float)*w / *h)
    397 				*w = *h * c->maxa + 0.5;
    398 			else if (c->mina < (float)*h / *w)
    399 				*h = *w * c->mina + 0.5;
    400 		}
    401 		if (baseismin) { /* increment calculation requires this */
    402 			*w -= c->basew;
    403 			*h -= c->baseh;
    404 		}
    405 		/* adjust for increment value */
    406 		if (c->incw)
    407 			*w -= *w % c->incw;
    408 		if (c->inch)
    409 			*h -= *h % c->inch;
    410 		/* restore base dimensions */
    411 		*w = MAX(*w + c->basew, c->minw);
    412 		*h = MAX(*h + c->baseh, c->minh);
    413 		if (c->maxw)
    414 			*w = MIN(*w, c->maxw);
    415 		if (c->maxh)
    416 			*h = MIN(*h, c->maxh);
    417 	}
    418 	return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
    419 }
    420 
    421 void
    422 arrange(Monitor *m)
    423 {
    424 	if (m)
    425 		showhide(m->stack);
    426 	else for (m = mons; m; m = m->next)
    427 		showhide(m->stack);
    428 	if (m) {
    429 		arrangemon(m);
    430 		restack(m);
    431 	} else for (m = mons; m; m = m->next)
    432 		arrangemon(m);
    433 }
    434 
    435 void
    436 arrangemon(Monitor *m)
    437 {
    438 	strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
    439 	if (m->lt[m->sellt]->arrange)
    440 		m->lt[m->sellt]->arrange(m);
    441 }
    442 
    443 void
    444 attach(Client *c)
    445 {
    446 	c->next = c->mon->clients;
    447 	c->mon->clients = c;
    448 }
    449 
    450 void
    451 attachbottom(Client *c)
    452 {
    453 	Client **tc;
    454 	c->next = NULL;
    455 	for (tc = &c->mon->clients; *tc; tc = &(*tc)->next);
    456 	*tc = c;
    457 }
    458 
    459 void
    460 attachstack(Client *c)
    461 {
    462 	c->snext = c->mon->stack;
    463 	c->mon->stack = c;
    464 }
    465 
    466 void
    467 buttonpress(XEvent *e)
    468 {
    469 	unsigned int i, x, click, occ = 0;
    470 	Arg arg = {0};
    471 	Client *c;
    472 	Monitor *m;
    473 	XButtonPressedEvent *ev = &e->xbutton;
    474 
    475 	click = ClkRootWin;
    476 	/* focus monitor if necessary */
    477 	if ((m = wintomon(ev->window)) && m != selmon) {
    478 		unfocus(selmon->sel, 1);
    479 		selmon = m;
    480 		focus(getclientundermouse());
    481 	}
    482 	if (ev->window == selmon->barwin) {
    483 		i = x = 0;
    484 		for (c = m->clients; c; c = c->next)
    485 			occ |= c->tags == 255 ? 0 : c->tags;
    486 		do {
    487 			/* do not reserve space for vacant tags */
    488 			if (!(occ & 1 << i || m->tagset[m->seltags] & 1 << i))
    489 				continue;
    490 			x += TEXTW(tags[i]);
    491 		} while (ev->x >= x && ++i < LENGTH(tags));
    492 		if (i < LENGTH(tags)) {
    493 			click = ClkTagBar;
    494 			arg.ui = 1 << i;
    495 		} else if (ev->x < x + TEXTW(selmon->ltsymbol))
    496 			click = ClkLtSymbol;
    497 		else if (ev->x > selmon->ww - (int)TEXTW(stext))
    498 			click = ClkStatusText;
    499 		else {
    500 			click = ClkWinTitle;
    501 			for (c = m->clients; c; c = c->next)
    502 				if (c->barx >= 0 && ev->x >= c->barx && ev->x < c->barx + c->barw)
    503 					arg.v = c;
    504 		}
    505 	} else if ((c = wintoclient(ev->window))) {
    506 		focus(c);
    507 		restack(selmon);
    508 		XAllowEvents(dpy, ReplayPointer, CurrentTime);
    509 		click = ClkClientWin;
    510 	}
    511 	for (i = 0; i < LENGTH(buttons); i++)
    512 		if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
    513 		&& CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
    514 			buttons[i].func((click == ClkTagBar || click == ClkWinTitle) && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
    515 }
    516 
    517 void
    518 checkotherwm(void)
    519 {
    520 	xerrorxlib = XSetErrorHandler(xerrorstart);
    521 	/* this causes an error if some other window manager is running */
    522 	XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
    523 	XSync(dpy, False);
    524 	XSetErrorHandler(xerror);
    525 	XSync(dpy, False);
    526 }
    527 
    528 void
    529 cleanup(void)
    530 {
    531 	Arg a = {.ui = ~0};
    532 	Layout foo = { "", NULL };
    533 	Monitor *m;
    534 	size_t i;
    535 
    536 	view(&a);
    537 	selmon->lt[selmon->sellt] = &foo;
    538 	for (m = mons; m; m = m->next)
    539 		while (m->stack)
    540 			unmanage(m->stack, 0);
    541 	XUngrabKey(dpy, AnyKey, AnyModifier, root);
    542 	while (mons)
    543 		cleanupmon(mons);
    544 	for (i = 0; i < CurLast; i++)
    545 		drw_cur_free(drw, cursor[i]);
    546 	for (i = 0; i < LENGTH(colors); i++)
    547 		free(scheme[i]);
    548 	free(scheme);
    549 	XDestroyWindow(dpy, wmcheckwin);
    550 	drw_free(drw);
    551 	XSync(dpy, False);
    552 	XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
    553 	XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
    554 }
    555 
    556 void
    557 cleanupmon(Monitor *mon)
    558 {
    559 	Monitor *m;
    560 
    561 	if (mon == mons)
    562 		mons = mons->next;
    563 	else {
    564 		for (m = mons; m && m->next != mon; m = m->next);
    565 		m->next = mon->next;
    566 	}
    567 	XUnmapWindow(dpy, mon->barwin);
    568 	XDestroyWindow(dpy, mon->barwin);
    569 	free(mon);
    570 }
    571 
    572 void
    573 clientmessage(XEvent *e)
    574 {
    575 	XClientMessageEvent *cme = &e->xclient;
    576 	Client *c = wintoclient(cme->window);
    577 
    578 	if (!c)
    579 		return;
    580 	if (cme->message_type == netatom[NetWMState]) {
    581 		if (cme->data.l[1] == netatom[NetWMFullscreen]
    582 		|| cme->data.l[2] == netatom[NetWMFullscreen])
    583 			setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD    */
    584 				|| (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
    585 	} else if (cme->message_type == netatom[NetActiveWindow]) {
    586 		if (c != selmon->sel && !c->isurgent)
    587 			seturgent(c, 1);
    588 	}
    589 }
    590 
    591 void
    592 configure(Client *c)
    593 {
    594 	XConfigureEvent ce;
    595 
    596 	ce.type = ConfigureNotify;
    597 	ce.display = dpy;
    598 	ce.event = c->win;
    599 	ce.window = c->win;
    600 	ce.x = c->x;
    601 	ce.y = c->y;
    602 	ce.width = c->w;
    603 	ce.height = c->h;
    604 	ce.border_width = c->bw;
    605 	ce.above = None;
    606 	ce.override_redirect = False;
    607 	XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
    608 }
    609 
    610 void
    611 configurenotify(XEvent *e)
    612 {
    613 	Monitor *m;
    614 	Client *c;
    615 	XConfigureEvent *ev = &e->xconfigure;
    616 	int dirty;
    617 
    618 	/* TODO: updategeom handling sucks, needs to be simplified */
    619 	if (ev->window == root) {
    620 		dirty = (sw != ev->width || sh != ev->height);
    621 		sw = ev->width;
    622 		sh = ev->height;
    623 		if (updategeom() || dirty) {
    624 			drw_resize(drw, sw, bh);
    625 			updatebars();
    626 			for (m = mons; m; m = m->next) {
    627 				for (c = m->clients; c; c = c->next)
    628 					if (c->isfullscreen)
    629 						resizeclient(c, m->mx, m->my, m->mw, m->mh);
    630 				XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
    631 			}
    632 			arrange(NULL);
    633 			focus(getclientundermouse());
    634 		}
    635 	}
    636 }
    637 
    638 void
    639 configurerequest(XEvent *e)
    640 {
    641 	Client *c;
    642 	Monitor *m;
    643 	XConfigureRequestEvent *ev = &e->xconfigurerequest;
    644 	XWindowChanges wc;
    645 
    646 	if ((c = wintoclient(ev->window))) {
    647 		if (ev->value_mask & CWBorderWidth)
    648 			c->bw = ev->border_width;
    649 		else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
    650 			m = c->mon;
    651 			if (ev->value_mask & CWX) {
    652 				c->oldx = c->x;
    653 				c->x = m->mx + ev->x;
    654 			}
    655 			if (ev->value_mask & CWY) {
    656 				c->oldy = c->y;
    657 				c->y = m->my + ev->y;
    658 			}
    659 			if (ev->value_mask & CWWidth) {
    660 				c->oldw = c->w;
    661 				c->w = ev->width;
    662 			}
    663 			if (ev->value_mask & CWHeight) {
    664 				c->oldh = c->h;
    665 				c->h = ev->height;
    666 			}
    667 			if ((c->x + c->w) > m->mx + m->mw && c->isfloating)
    668 				c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
    669 			if ((c->y + c->h) > m->my + m->mh && c->isfloating)
    670 				c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
    671 			if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
    672 				configure(c);
    673 			if (ISVISIBLE(c))
    674 				XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
    675 		} else
    676 			configure(c);
    677 	} else {
    678 		wc.x = ev->x;
    679 		wc.y = ev->y;
    680 		wc.width = ev->width;
    681 		wc.height = ev->height;
    682 		wc.border_width = ev->border_width;
    683 		wc.sibling = ev->above;
    684 		wc.stack_mode = ev->detail;
    685 		XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
    686 	}
    687 	XSync(dpy, False);
    688 }
    689 
    690 Monitor *
    691 createmon(void)
    692 {
    693 	Monitor *m;
    694 	unsigned int i;
    695 
    696 	m = ecalloc(1, sizeof(Monitor));
    697 	m->tagset[0] = m->tagset[1] = 1;
    698 	m->mfact = mfact;
    699 	m->nmaster = nmaster;
    700 	m->showbar = showbar;
    701 	m->topbar = topbar;
    702 	m->lt[0] = &layouts[0];
    703 	m->lt[1] = &layouts[1 % LENGTH(layouts)];
    704 	strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
    705 	m->pertag = ecalloc(1, sizeof(Pertag));
    706 	m->pertag->curtag = m->pertag->prevtag = 1;
    707 
    708 	for (i = 0; i <= LENGTH(tags); i++) {
    709 		m->pertag->nmasters[i] = m->nmaster;
    710 		m->pertag->mfacts[i] = m->mfact;
    711 
    712 		m->pertag->ltidxs[i][0] = m->lt[0];
    713 		m->pertag->ltidxs[i][1] = m->lt[1];
    714 		m->pertag->sellts[i] = m->sellt;
    715 
    716 		m->pertag->showbars[i] = m->showbar;
    717 	}
    718 
    719 	return m;
    720 }
    721 
    722 void
    723 destroynotify(XEvent *e)
    724 {
    725 	Client *c;
    726 	XDestroyWindowEvent *ev = &e->xdestroywindow;
    727 
    728 	if ((c = wintoclient(ev->window)))
    729 		unmanage(c, 1);
    730 }
    731 
    732 void
    733 deck(Monitor *m) {
    734 	unsigned int i, n, h, mw, my;
    735 	Client *c;
    736 
    737 	for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
    738 	if(n == 0)
    739 		return;
    740 
    741 	if(n > m->nmaster) {
    742 		mw = m->nmaster ? m->ww * m->mfact : 0;
    743 		snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n - m->nmaster);
    744 	}
    745 	else
    746 		mw = m->ww;
    747 	for(i = my = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
    748 		if(i < m->nmaster) {
    749 			h = (m->wh - my) / (MIN(n, m->nmaster) - i);
    750 			resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False);
    751 			my += HEIGHT(c);
    752 		}
    753 		else
    754 			resize(c, m->wx + mw, m->wy, m->ww - mw - (2*c->bw), m->wh - (2*c->bw), False);
    755 }
    756 
    757 void
    758 detach(Client *c)
    759 {
    760 	Client **tc;
    761 
    762 	for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
    763 	*tc = c->next;
    764 }
    765 
    766 void
    767 detachstack(Client *c)
    768 {
    769 	Client **tc, *t;
    770 
    771 	for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
    772 	*tc = c->snext;
    773 
    774 	if (c == c->mon->sel) {
    775 		for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
    776 		c->mon->sel = t;
    777 	}
    778 }
    779 
    780 Monitor *
    781 dirtomon(int dir)
    782 {
    783 	Monitor *m = NULL;
    784 
    785 	if (dir > 0) {
    786 		if (!(m = selmon->next))
    787 			m = mons;
    788 	} else if (selmon == mons)
    789 		for (m = mons; m->next; m = m->next);
    790 	else
    791 		for (m = mons; m->next != selmon; m = m->next);
    792 	return m;
    793 }
    794 
    795 void
    796 drawbar(Monitor *m)
    797 {
    798 	int indn;
    799 	int x, w, tw = 0, mw, ew = 0;
    800 	int boxs = drw->fonts->h / 9;
    801 	int boxw = drw->fonts->h / 6 + 2;
    802 	unsigned int i, occ = 0, urg = 0, n = 0;
    803 	Client *c;
    804 
    805 	if (!m->showbar)
    806 		return;
    807 
    808 	/* draw status first so it can be overdrawn by tags later */
    809 	if (m == selmon) { /* status is only drawn on selected monitor */
    810 		drw_setscheme(drw, scheme[SchemeNorm]);
    811 		tw = TEXTW(stext) - lrpad + 2; /* 2px right padding */
    812 		drw_text(drw, m->ww - tw, 0, tw, bh, 0, stext, 0);
    813 	}
    814 
    815 	for (c = m->clients; c; c = c->next) {
    816 		if (ISVISIBLE(c))
    817 			n++;
    818 		occ |= c->tags == 255 ? 0 : c->tags;
    819 		if (c->isurgent)
    820 			urg |= c->tags;
    821 	}
    822 	x = 0;
    823 	for (i = 0; i < LENGTH(tags); i++) {
    824 		/* do not draw vacant tags */
    825 		if (!(occ & 1 << i || m->tagset[m->seltags] & 1 << i))
    826 		continue;
    827 
    828 		indn = 0;
    829 		w = TEXTW(tags[i]);
    830 		drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]);
    831 		drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i);
    832 
    833 		for (c = m->clients; c; c = c->next) {
    834 			if (c->tags & (1 << i)) {
    835 				drw_rect(drw, x, 1 + (indn * 2), selmon->sel == c ? 6 : 1, 1, 1, urg & 1 << i);
    836 				indn++;
    837 			}
    838 		}
    839 
    840 		x += w;
    841 	}
    842 	w = TEXTW(m->ltsymbol);
    843 	drw_setscheme(drw, scheme[SchemeNorm]);
    844 	x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0);
    845 
    846 	if ((w = m->ww - tw - x) > bh) {
    847 		if (n > 0) {
    848 			tw = TEXTW(m->sel->name) + lrpad;
    849 			mw = (tw >= w || n == 1) ? 0 : (w - tw) / (n - 1);
    850 
    851 			i = 0;
    852 			for (c = m->clients; c; c = c->next) {
    853 				if (!ISVISIBLE(c) || c == m->sel)
    854 					continue;
    855 				tw = TEXTW(c->name);
    856 				if(tw < mw)
    857 					ew += (mw - tw);
    858 				else
    859 					i++;
    860 			}
    861 			if (i > 0)
    862 				mw += ew / i;
    863 
    864 			for (c = m->clients; c; c = c->next) {
    865 				if (!ISVISIBLE(c)) {
    866 					c->barx = c->barw = -1;
    867 					continue;
    868 				}
    869 
    870 				tw = MIN(m->sel == c ? w : mw, TEXTW(c->name));
    871 				c->barx = x;
    872 				c->barw = tw;
    873 
    874 				drw_setscheme(drw, scheme[m == selmon && m->sel == c ? SchemeSel : SchemeNorm]);
    875 				if (tw > lrpad / 2)
    876 					drw_text(drw, x, 0, tw, bh, lrpad / 2, c->name, 0);
    877 				if (c->isfloating)
    878 					drw_rect(drw, x + boxs, boxs, boxw, boxw, c->isfixed, 0);
    879 				x += tw;
    880 				w -= tw;
    881 			}
    882 		}
    883 		drw_setscheme(drw, scheme[SchemeNorm]);
    884 		drw_rect(drw, x, 0, w, bh, 1, 1);
    885 	}
    886 	drw_map(drw, m->barwin, 0, 0, m->ww, bh);
    887 }
    888 
    889 void
    890 drawbars(void)
    891 {
    892 	Monitor *m;
    893 
    894 	for (m = mons; m; m = m->next)
    895 		drawbar(m);
    896 }
    897 
    898 void
    899 enternotify(XEvent *e)
    900 {
    901 	Client *c;
    902 	Monitor *m;
    903 	XCrossingEvent *ev = &e->xcrossing;
    904 
    905 	if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
    906 		return;
    907 	c = wintoclient(ev->window);
    908 	m = c ? c->mon : wintomon(ev->window);
    909 	if (m != selmon) {
    910 		unfocus(selmon->sel, 1);
    911 		selmon = m;
    912 	} else if (!c || c == selmon->sel)
    913 		return;
    914 	focus(c);
    915 }
    916 
    917 void
    918 expose(XEvent *e)
    919 {
    920 	Monitor *m;
    921 	XExposeEvent *ev = &e->xexpose;
    922 
    923 	if (ev->count == 0 && (m = wintomon(ev->window)))
    924 		drawbar(m);
    925 }
    926 
    927 void
    928 focus(Client *c)
    929 {
    930 	if (!c || !ISVISIBLE(c))
    931 		for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
    932 	if (selmon->sel && selmon->sel != c)
    933 		unfocus(selmon->sel, 0);
    934 	if (c) {
    935 		if (c->mon != selmon)
    936 			selmon = c->mon;
    937 		if (c->isurgent)
    938 			seturgent(c, 0);
    939 		detachstack(c);
    940 		attachstack(c);
    941 		grabbuttons(c, 1);
    942 		if (c == mark)
    943 			XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColMark].pixel);
    944 		else
    945 			XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel);
    946 		setfocus(c);
    947 	} else {
    948 		XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
    949 		XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
    950 	}
    951 	selmon->sel = c;
    952 	drawbars();
    953 }
    954 
    955 /* there are some broken focus acquiring clients needing extra handling */
    956 void
    957 focusin(XEvent *e)
    958 {
    959 	XFocusChangeEvent *ev = &e->xfocus;
    960 
    961 	if (selmon->sel && ev->window != selmon->sel->win)
    962 		setfocus(selmon->sel);
    963 }
    964 
    965 void
    966 focusmon(const Arg *arg)
    967 {
    968 	Monitor *m;
    969 
    970 	if (!mons->next)
    971 		return;
    972 	if ((m = dirtomon(arg->i)) == selmon)
    973 		return;
    974 	unfocus(selmon->sel, 0);
    975 	selmon = m;
    976 	focus(NULL);
    977 	warp(selmon->sel);
    978 }
    979 
    980 void
    981 focusstack(const Arg *arg)
    982 {
    983 	Client *c = NULL, *i;
    984 
    985 	if (!selmon->sel || (selmon->sel->isfullscreen && lockfullscreen))
    986 		return;
    987 	if (arg->i > 0) {
    988 		for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
    989 		if (!c)
    990 			for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
    991 	} else {
    992 		for (i = selmon->clients; i != selmon->sel; i = i->next)
    993 			if (ISVISIBLE(i))
    994 				c = i;
    995 		if (!c)
    996 			for (; i; i = i->next)
    997 				if (ISVISIBLE(i))
    998 					c = i;
    999 	}
   1000 	if (c) {
   1001 		focus(c);
   1002 		restack(selmon);
   1003 	}
   1004 }
   1005 
   1006 void
   1007 focustitle(const Arg *arg)
   1008 {
   1009 	focus((Client *)arg->v);
   1010 	restack(selmon);
   1011 }
   1012 
   1013 Atom
   1014 getatomprop(Client *c, Atom prop)
   1015 {
   1016 	int di;
   1017 	unsigned long dl;
   1018 	unsigned char *p = NULL;
   1019 	Atom da, atom = None;
   1020 
   1021 	if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
   1022 		&da, &di, &dl, &dl, &p) == Success && p) {
   1023 		atom = *(Atom *)p;
   1024 		XFree(p);
   1025 	}
   1026 	return atom;
   1027 }
   1028 
   1029 Client *
   1030 getclientundermouse(void)
   1031 {
   1032 	int ret, di;
   1033 	unsigned int dui;
   1034 	Window child, dummy;
   1035 
   1036 	ret = XQueryPointer(dpy, root, &dummy, &child, &di, &di, &di, &di, &dui);
   1037 	if (!ret)
   1038 		return NULL;
   1039 
   1040 	return wintoclient(child);
   1041 }
   1042 
   1043 int
   1044 getrootptr(int *x, int *y)
   1045 {
   1046 	int di;
   1047 	unsigned int dui;
   1048 	Window dummy;
   1049 
   1050 	return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
   1051 }
   1052 
   1053 long
   1054 getstate(Window w)
   1055 {
   1056 	int format;
   1057 	long result = -1;
   1058 	unsigned char *p = NULL;
   1059 	unsigned long n, extra;
   1060 	Atom real;
   1061 
   1062 	if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
   1063 		&real, &format, &n, &extra, (unsigned char **)&p) != Success)
   1064 		return -1;
   1065 	if (n != 0)
   1066 		result = *p;
   1067 	XFree(p);
   1068 	return result;
   1069 }
   1070 
   1071 int
   1072 gettextprop(Window w, Atom atom, char *text, unsigned int size)
   1073 {
   1074 	char **list = NULL;
   1075 	int n;
   1076 	XTextProperty name;
   1077 
   1078 	if (!text || size == 0)
   1079 		return 0;
   1080 	text[0] = '\0';
   1081 	if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems)
   1082 		return 0;
   1083 	if (name.encoding == XA_STRING) {
   1084 		strncpy(text, (char *)name.value, size - 1);
   1085 	} else if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
   1086 		strncpy(text, *list, size - 1);
   1087 		XFreeStringList(list);
   1088 	}
   1089 	text[size - 1] = '\0';
   1090 	XFree(name.value);
   1091 	return 1;
   1092 }
   1093 
   1094 void
   1095 grabbuttons(Client *c, int focused)
   1096 {
   1097 	updatenumlockmask();
   1098 	{
   1099 		unsigned int i, j;
   1100 		unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
   1101 		XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
   1102 		if (!focused)
   1103 			XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
   1104 				BUTTONMASK, GrabModeSync, GrabModeSync, None, None);
   1105 		for (i = 0; i < LENGTH(buttons); i++)
   1106 			if (buttons[i].click == ClkClientWin)
   1107 				for (j = 0; j < LENGTH(modifiers); j++)
   1108 					XGrabButton(dpy, buttons[i].button,
   1109 						buttons[i].mask | modifiers[j],
   1110 						c->win, False, BUTTONMASK,
   1111 						GrabModeAsync, GrabModeSync, None, None);
   1112 	}
   1113 }
   1114 
   1115 void
   1116 grabkeys(void)
   1117 {
   1118 	updatenumlockmask();
   1119 	{
   1120 		unsigned int i, j, k;
   1121 		unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
   1122 		int start, end, skip;
   1123 		KeySym *syms;
   1124 
   1125 		XUngrabKey(dpy, AnyKey, AnyModifier, root);
   1126 		XDisplayKeycodes(dpy, &start, &end);
   1127 		syms = XGetKeyboardMapping(dpy, start, end - start + 1, &skip);
   1128 		if (!syms)
   1129 			return;
   1130 		for (k = start; k <= end; k++)
   1131 			for (i = 0; i < LENGTH(keys); i++)
   1132 				/* skip modifier codes, we do that ourselves */
   1133 				if (keys[i].keysym == syms[(k - start) * skip])
   1134 					for (j = 0; j < LENGTH(modifiers); j++)
   1135 						XGrabKey(dpy, k,
   1136 							 keys[i].mod | modifiers[j],
   1137 							 root, True,
   1138 							 GrabModeAsync, GrabModeAsync);
   1139 		XFree(syms);
   1140 	}
   1141 }
   1142 
   1143 void
   1144 incnmaster(const Arg *arg)
   1145 {
   1146 	selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag] = MAX(selmon->nmaster + arg->i, 0);
   1147 	arrange(selmon);
   1148 }
   1149 
   1150 #ifdef XINERAMA
   1151 static int
   1152 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
   1153 {
   1154 	while (n--)
   1155 		if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
   1156 		&& unique[n].width == info->width && unique[n].height == info->height)
   1157 			return 0;
   1158 	return 1;
   1159 }
   1160 #endif /* XINERAMA */
   1161 
   1162 void
   1163 keypress(XEvent *e)
   1164 {
   1165 	unsigned int i;
   1166 	KeySym keysym;
   1167 	XKeyEvent *ev;
   1168 
   1169 	ev = &e->xkey;
   1170 	keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
   1171 	for (i = 0; i < LENGTH(keys); i++)
   1172 		if (keysym == keys[i].keysym
   1173 		&& CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
   1174 		&& keys[i].func)
   1175 			keys[i].func(&(keys[i].arg));
   1176 }
   1177 
   1178 void
   1179 killclient(const Arg *arg)
   1180 {
   1181 	if (!selmon->sel)
   1182 		return;
   1183 	if (!sendevent(selmon->sel, wmatom[WMDelete])) {
   1184 		XGrabServer(dpy);
   1185 		XSetErrorHandler(xerrordummy);
   1186 		XSetCloseDownMode(dpy, DestroyAll);
   1187 		XKillClient(dpy, selmon->sel->win);
   1188 		XSync(dpy, False);
   1189 		XSetErrorHandler(xerror);
   1190 		XUngrabServer(dpy);
   1191 	}
   1192 }
   1193 
   1194 void
   1195 manage(Window w, XWindowAttributes *wa)
   1196 {
   1197 	Client *c, *t = NULL;
   1198 	Window trans = None;
   1199 	XWindowChanges wc;
   1200 
   1201 	c = ecalloc(1, sizeof(Client));
   1202 	c->win = w;
   1203 	/* geometry */
   1204 	c->x = c->oldx = wa->x;
   1205 	c->y = c->oldy = wa->y;
   1206 	c->w = c->oldw = wa->width;
   1207 	c->h = c->oldh = wa->height;
   1208 	c->oldbw = wa->border_width;
   1209 	c->barx = c->barw = -1;
   1210 
   1211 	updatetitle(c);
   1212 	if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
   1213 		c->mon = t->mon;
   1214 		c->tags = t->tags;
   1215 	} else {
   1216 		c->mon = selmon;
   1217 		applyrules(c);
   1218 	}
   1219 
   1220 	if (c->x + WIDTH(c) > c->mon->wx + c->mon->ww)
   1221 		c->x = c->mon->wx + c->mon->ww - WIDTH(c);
   1222 	if (c->y + HEIGHT(c) > c->mon->wy + c->mon->wh)
   1223 		c->y = c->mon->wy + c->mon->wh - HEIGHT(c);
   1224 	c->x = MAX(c->x, c->mon->wx);
   1225 	c->y = MAX(c->y, c->mon->wy);
   1226 	c->bw = borderpx;
   1227 
   1228 	selmon->tagset[selmon->seltags] &= ~scratchtag;
   1229 	if (!strcmp(c->name, scratchpadname)) {
   1230 		c->mon->tagset[c->mon->seltags] |= c->tags = scratchtag;
   1231 		c->isfloating = True;
   1232 		c->x = c->mon->wx + (c->mon->ww / 2 - WIDTH(c) / 2);
   1233 		c->y = c->mon->wy + (c->mon->wh / 2 - HEIGHT(c) / 2);
   1234 	}
   1235 
   1236 	wc.border_width = c->bw;
   1237 	XConfigureWindow(dpy, w, CWBorderWidth, &wc);
   1238 	if (c == mark)
   1239 		XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColMark].pixel);
   1240 	else
   1241 		XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel);
   1242 	configure(c); /* propagates border_width, if size doesn't change */
   1243 	updatewindowtype(c);
   1244 	updatesizehints(c);
   1245 	updatewmhints(c);
   1246 	c->x = c->mon->mx + (c->mon->mw - WIDTH(c)) / 2;
   1247 	c->y = c->mon->my + (c->mon->mh - HEIGHT(c)) / 2;
   1248 	XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
   1249 	grabbuttons(c, 0);
   1250 	if (!c->isfloating)
   1251 		c->isfloating = c->oldstate = trans != None || c->isfixed;
   1252 	if (c->isfloating)
   1253 		XRaiseWindow(dpy, c->win);
   1254 	attachbottom(c);
   1255 	attachstack(c);
   1256 	XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
   1257 		(unsigned char *) &(c->win), 1);
   1258 	XChangeProperty(dpy, root, netatom[NetClientListStacking], XA_WINDOW, 32, PropModePrepend,
   1259 		(unsigned char *) &(c->win), 1);
   1260 	XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
   1261 	setclientstate(c, NormalState);
   1262 	if (c->mon == selmon)
   1263 		unfocus(selmon->sel, 0);
   1264 	c->mon->sel = c;
   1265 	arrange(c->mon);
   1266 	XMapWindow(dpy, c->win);
   1267 	focus(NULL);
   1268 }
   1269 
   1270 void
   1271 mappingnotify(XEvent *e)
   1272 {
   1273 	XMappingEvent *ev = &e->xmapping;
   1274 
   1275 	XRefreshKeyboardMapping(ev);
   1276 	if (ev->request == MappingKeyboard)
   1277 		grabkeys();
   1278 }
   1279 
   1280 void
   1281 maprequest(XEvent *e)
   1282 {
   1283 	static XWindowAttributes wa;
   1284 	XMapRequestEvent *ev = &e->xmaprequest;
   1285 
   1286 	if (!XGetWindowAttributes(dpy, ev->window, &wa) || wa.override_redirect)
   1287 		return;
   1288 	if (!wintoclient(ev->window))
   1289 		manage(ev->window, &wa);
   1290 }
   1291 
   1292 void
   1293 monocle(Monitor *m)
   1294 {
   1295 	unsigned int n = 0;
   1296 	Client *c;
   1297 
   1298 	for (c = m->clients; c; c = c->next)
   1299 		if (ISVISIBLE(c))
   1300 			n++;
   1301 	if (n > 0) /* override layout symbol */
   1302 		snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
   1303 	for (c = nexttiled(m->clients); c; c = nexttiled(c->next))
   1304 		resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
   1305 }
   1306 
   1307 void
   1308 motionnotify(XEvent *e)
   1309 {
   1310 	static Monitor *mon = NULL;
   1311 	Monitor *m;
   1312 	XMotionEvent *ev = &e->xmotion;
   1313 
   1314 	if (ev->window != root)
   1315 		return;
   1316 	if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
   1317 		unfocus(selmon->sel, 1);
   1318 		selmon = m;
   1319 		focus(getclientundermouse());
   1320 	}
   1321 	mon = m;
   1322 }
   1323 
   1324 void
   1325 movemouse(const Arg *arg)
   1326 {
   1327 	int x, y, ocx, ocy, nx, ny;
   1328 	Client *c;
   1329 	Monitor *m;
   1330 	XEvent ev;
   1331 	Time lasttime = 0;
   1332 
   1333 	if (!(c = selmon->sel))
   1334 		return;
   1335 	if (c->isfullscreen) /* no support moving fullscreen windows by mouse */
   1336 		return;
   1337 	restack(selmon);
   1338 	ocx = c->x;
   1339 	ocy = c->y;
   1340 	if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
   1341 		None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
   1342 		return;
   1343 	if (!getrootptr(&x, &y))
   1344 		return;
   1345 	do {
   1346 		XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
   1347 		switch(ev.type) {
   1348 		case ConfigureRequest:
   1349 		case Expose:
   1350 		case MapRequest:
   1351 			handler[ev.type](&ev);
   1352 			break;
   1353 		case MotionNotify:
   1354 			if ((ev.xmotion.time - lasttime) <= (1000 / 60))
   1355 				continue;
   1356 			lasttime = ev.xmotion.time;
   1357 
   1358 			nx = ocx + (ev.xmotion.x - x);
   1359 			ny = ocy + (ev.xmotion.y - y);
   1360 			if (abs(selmon->wx - nx) < snap)
   1361 				nx = selmon->wx;
   1362 			else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
   1363 				nx = selmon->wx + selmon->ww - WIDTH(c);
   1364 			if (abs(selmon->wy - ny) < snap)
   1365 				ny = selmon->wy;
   1366 			else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
   1367 				ny = selmon->wy + selmon->wh - HEIGHT(c);
   1368 			if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
   1369 			&& (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
   1370 				togglefloating(NULL);
   1371 			if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
   1372 				resize(c, nx, ny, c->w, c->h, 1);
   1373 			break;
   1374 		}
   1375 	} while (ev.type != ButtonRelease);
   1376 	XUngrabPointer(dpy, CurrentTime);
   1377 	if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
   1378 		sendmon(c, m);
   1379 		selmon = m;
   1380 		focus(getclientundermouse());
   1381 	}
   1382 }
   1383 
   1384 Client *
   1385 nexttiled(Client *c)
   1386 {
   1387 	for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
   1388 	return c;
   1389 }
   1390 
   1391 void
   1392 pixeldeckcol(Monitor *m)
   1393 {
   1394 	unsigned int i, n, h, tx, ty;
   1395 	unsigned int nrows = m->wh / pixelheight;
   1396 	unsigned int has_bottom = nrows * pixelheight + pixelheightmin < m->wh;
   1397 	unsigned int ncols = (m->ww - pixelwidthmin) / pixelwidth;
   1398 	unsigned int nmaster = MIN(m->nmaster, nrows * ncols);
   1399 	Client *c;
   1400 
   1401 	if (nmaster == 0) {
   1402 		monocle(m);
   1403 		return;
   1404 	}
   1405 
   1406 	for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
   1407 	if (n == 0)
   1408 		return;
   1409 
   1410 	snprintf(m->ltsymbol, sizeof m->ltsymbol, "%s%d", selmon->lt[selmon->sellt]->symbol, n - MIN(m->nmaster, n));
   1411 
   1412 	for (i = tx = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) {
   1413 
   1414 		if (i < nmaster) {
   1415 			h = (has_bottom ? pixelheight : (m->wh - ty) / (nrows - i % nrows));
   1416 			resize(c, m->wx + tx, m->wy + ty, pixelwidth - (2*c->bw), h - (2 * c->bw), 0);
   1417 			ty += h;
   1418 
   1419 			if ((i + 1) % nrows == 0) {
   1420 				tx += pixelwidth;
   1421 				ty = 0;
   1422 			}
   1423 		} else if (has_bottom && i < nmaster + nmaster / nrows) {
   1424 			resize(c, m->wx + (i - nmaster) * pixelwidth, m->wy + nrows * pixelheight, pixelwidth - (2*c->bw), m->wh - nrows * pixelheight - (2*c->bw), 0);
   1425 		} else if (ty > 0) {
   1426 			resize(c, m->wx + tx, m->wy + ty, pixelwidth - (2*c->bw), m->wh - ty - (2*c->bw), 0);
   1427 			tx += pixelwidth;
   1428 			ty = 0;
   1429 		} else
   1430 			resize(c, m->wx + tx, m->wy + ty, m->ww - tx - (2*c->bw), m->wh - ty - (2*c->bw), 0);
   1431 	}
   1432 }
   1433 
   1434 void
   1435 pixelrow(Monitor *m)
   1436 {
   1437 	unsigned int i, n, w, h, tx, ty, tt;
   1438 	unsigned int nrows = m->wh / pixelheight;
   1439 	unsigned int has_bottom = nrows * pixelheight + pixelheightmin < m->wh;
   1440 	unsigned int ncols = m->ww / pixelwidth;
   1441 	unsigned int has_right = ncols * pixelwidth + pixelwidthmin < m->ww;
   1442 	unsigned int nmaster = MIN(m->nmaster, nrows * ncols - (has_bottom ? 0 : 1));
   1443 	Client *c;
   1444 
   1445 	if (nrows == 0 || ncols == 0) {
   1446 		tile(m);
   1447 		return;
   1448 	}
   1449 
   1450 	for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
   1451 	if (n == 0)
   1452 		return;
   1453 
   1454 	for (i = tx = ty = tt = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) {
   1455 		if (i < nmaster) {
   1456 			w = (has_right ? pixelwidth : (m->ww - tx) / (ncols - i % ncols));
   1457 			h = (has_bottom ? pixelheight : (m->wh - ty) / (nrows - i / ncols));
   1458 			resize(c, m->wx + tx, m->wy + ty, w - (2*c->bw), h - (2 * c->bw), 0);
   1459 			tx += w;
   1460 
   1461 			if ((i + 1) % ncols == 0) {
   1462 				tx = 0;
   1463 				ty += h;
   1464 			}
   1465 			if (i + 1 == nmaster && !has_bottom) {
   1466 				tt = tx;
   1467 				tx = 0;
   1468 			}
   1469 		} else if (has_right && i < nmaster + nmaster / ncols) {
   1470 			h = (ty - tt) / (nmaster + nmaster / ncols - i);
   1471 			resize(c, m->wx + ncols * pixelwidth, m->wy + tt, m->ww - ncols * pixelwidth - (2*c->bw), h, 0);
   1472 			if (tt + h < ty) tt += h;
   1473 			if (i + 1 == nmaster + nmaster / ncols)
   1474 				tt = 0;
   1475 		} else if (tx > 0) {
   1476 			resize(c, m->wx + tx, m->wy + ty, m->ww - tx - (2*c->bw), pixelheight - (2*c->bw), 0);
   1477 			tx = tt = 0;
   1478 			ty += pixelheight;
   1479 		} else {
   1480 			w = (m->ww - tt) / (n - i);
   1481 			resize(c, m->wx + tt, m->wy + ty, w - (2*c->bw), m->wh - ty - (2*c->bw), 0);
   1482 			if (tt + w < m->ww) tt += w;
   1483 		}
   1484 	}
   1485 }
   1486 
   1487 void
   1488 pop(Client *c)
   1489 {
   1490 	detach(c);
   1491 	attach(c);
   1492 	focus(c);
   1493 	arrange(c->mon);
   1494 }
   1495 
   1496 void
   1497 propertynotify(XEvent *e)
   1498 {
   1499 	Client *c;
   1500 	Window trans;
   1501 	XPropertyEvent *ev = &e->xproperty;
   1502 
   1503 	if ((ev->window == root) && (ev->atom == XA_WM_NAME))
   1504 		updatestatus();
   1505 	else if (ev->state == PropertyDelete)
   1506 		return; /* ignore */
   1507 	else if ((c = wintoclient(ev->window))) {
   1508 		switch(ev->atom) {
   1509 		default: break;
   1510 		case XA_WM_TRANSIENT_FOR:
   1511 			if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
   1512 				(c->isfloating = (wintoclient(trans)) != NULL))
   1513 				arrange(c->mon);
   1514 			break;
   1515 		case XA_WM_NORMAL_HINTS:
   1516 			c->hintsvalid = 0;
   1517 			break;
   1518 		case XA_WM_HINTS:
   1519 			updatewmhints(c);
   1520 			drawbars();
   1521 			break;
   1522 		}
   1523 		if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
   1524 			updatetitle(c);
   1525 			if (c == c->mon->sel)
   1526 				drawbar(c->mon);
   1527 		}
   1528 		if (ev->atom == netatom[NetWMWindowType])
   1529 			updatewindowtype(c);
   1530 	}
   1531 }
   1532 
   1533 void
   1534 quit(const Arg *arg)
   1535 {
   1536 	if(arg->i) restart = 1;
   1537 	running = 0;
   1538 }
   1539 
   1540 Monitor *
   1541 recttomon(int x, int y, int w, int h)
   1542 {
   1543 	Monitor *m, *r = selmon;
   1544 	int a, area = 0;
   1545 
   1546 	for (m = mons; m; m = m->next)
   1547 		if ((a = INTERSECT(x, y, w, h, m)) > area) {
   1548 			area = a;
   1549 			r = m;
   1550 		}
   1551 	return r;
   1552 }
   1553 
   1554 void
   1555 resize(Client *c, int x, int y, int w, int h, int interact)
   1556 {
   1557 	if (applysizehints(c, &x, &y, &w, &h, interact))
   1558 		resizeclient(c, x, y, w, h);
   1559 }
   1560 
   1561 void
   1562 resizeclient(Client *c, int x, int y, int w, int h)
   1563 {
   1564 	XWindowChanges wc;
   1565 
   1566 	c->oldx = c->x; c->x = wc.x = x;
   1567 	c->oldy = c->y; c->y = wc.y = y;
   1568 	c->oldw = c->w; c->w = wc.width = w;
   1569 	c->oldh = c->h; c->h = wc.height = h;
   1570 	wc.border_width = c->bw;
   1571 	XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
   1572 	configure(c);
   1573 	XSync(dpy, False);
   1574 }
   1575 
   1576 void
   1577 resizemouse(const Arg *arg)
   1578 {
   1579 	int ocx, ocy, nw, nh;
   1580 	Client *c;
   1581 	Monitor *m;
   1582 	XEvent ev;
   1583 	Time lasttime = 0;
   1584 
   1585 	if (!(c = selmon->sel))
   1586 		return;
   1587 	if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
   1588 		return;
   1589 	restack(selmon);
   1590 	ocx = c->x;
   1591 	ocy = c->y;
   1592 	if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
   1593 		None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
   1594 		return;
   1595 	XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
   1596 	do {
   1597 		XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
   1598 		switch(ev.type) {
   1599 		case ConfigureRequest:
   1600 		case Expose:
   1601 		case MapRequest:
   1602 			handler[ev.type](&ev);
   1603 			break;
   1604 		case MotionNotify:
   1605 			if ((ev.xmotion.time - lasttime) <= (1000 / 60))
   1606 				continue;
   1607 			lasttime = ev.xmotion.time;
   1608 
   1609 			nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
   1610 			nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
   1611 			if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
   1612 			&& c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
   1613 			{
   1614 				if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
   1615 				&& (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
   1616 					togglefloating(NULL);
   1617 			}
   1618 			if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
   1619 				resize(c, c->x, c->y, nw, nh, 1);
   1620 			break;
   1621 		}
   1622 	} while (ev.type != ButtonRelease);
   1623 	XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
   1624 	XUngrabPointer(dpy, CurrentTime);
   1625 	while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
   1626 	if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
   1627 		sendmon(c, m);
   1628 		selmon = m;
   1629 		focus(getclientundermouse());
   1630 	}
   1631 }
   1632 
   1633 void
   1634 restack(Monitor *m)
   1635 {
   1636 	Client *c;
   1637 	XEvent ev;
   1638 	XWindowChanges wc;
   1639 
   1640 	drawbar(m);
   1641 	if (!m->sel)
   1642 		return;
   1643 	if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
   1644 		XRaiseWindow(dpy, m->sel->win);
   1645 	if (m->lt[m->sellt]->arrange) {
   1646 		wc.stack_mode = Below;
   1647 		wc.sibling = m->barwin;
   1648 		for (c = m->stack; c; c = c->snext)
   1649 			if (!c->isfloating && ISVISIBLE(c)) {
   1650 				XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
   1651 				wc.sibling = c->win;
   1652 			}
   1653 	}
   1654 	XSync(dpy, False);
   1655 	while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
   1656 	if (m == selmon && (m->tagset[m->seltags] & m->sel->tags) && selmon->lt[selmon->sellt] != &layouts[2])
   1657 		warp(m->sel);
   1658 }
   1659 
   1660 void
   1661 run(void)
   1662 {
   1663 	XEvent ev;
   1664 	/* main event loop */
   1665 	XSync(dpy, False);
   1666 	while (running && !XNextEvent(dpy, &ev))
   1667 		if (handler[ev.type])
   1668 			handler[ev.type](&ev); /* call handler */
   1669 }
   1670 
   1671 void
   1672 runautostart(void)
   1673 {
   1674 	char *pathpfx;
   1675 	char *path;
   1676 	char *xdgdatahome;
   1677 	char *home;
   1678 	struct stat sb;
   1679 	int result;
   1680 
   1681 	if ((home = getenv("HOME")) == NULL)
   1682 		/* this is almost impossible */
   1683 		return;
   1684 
   1685 	/* if $XDG_DATA_HOME is set and not empty, use $XDG_DATA_HOME/dwm,
   1686 	 * otherwise use ~/.local/share/dwm as autostart script directory
   1687 	 */
   1688 	xdgdatahome = getenv("XDG_DATA_HOME");
   1689 	if (xdgdatahome != NULL && *xdgdatahome != '\0') {
   1690 		/* space for path segments, separators and nul */
   1691 		pathpfx = ecalloc(1, strlen(xdgdatahome) + strlen(dwmdir) + 2);
   1692 
   1693 		if (sprintf(pathpfx, "%s/%s", xdgdatahome, dwmdir) <= 0) {
   1694 			free(pathpfx);
   1695 			return;
   1696 		}
   1697 	} else {
   1698 		/* space for path segments, separators and nul */
   1699 		pathpfx = ecalloc(1, strlen(home) + strlen(localshare)
   1700 		                     + strlen(dwmdir) + 3);
   1701 
   1702 		if (sprintf(pathpfx, "%s/%s/%s", home, localshare, dwmdir) < 0) {
   1703 			free(pathpfx);
   1704 			return;
   1705 		}
   1706 	}
   1707 
   1708 	/* check if the autostart script directory exists */
   1709 	if (! (stat(pathpfx, &sb) == 0 && S_ISDIR(sb.st_mode))) {
   1710 		/* the XDG conformant path does not exist or is no directory
   1711 		 * so we try ~/.dwm instead
   1712 		 */
   1713 		char *pathpfx_new = realloc(pathpfx, strlen(home) + strlen(dwmdir) + 3);
   1714 		if(pathpfx_new == NULL) {
   1715 			free(pathpfx);
   1716 			return;
   1717 		}
   1718 		pathpfx = pathpfx_new;
   1719 
   1720 		if (sprintf(pathpfx, "%s/.%s", home, dwmdir) <= 0) {
   1721 			free(pathpfx);
   1722 			return;
   1723 		}
   1724 	}
   1725 
   1726 	/* try the blocking script first */
   1727 	path = ecalloc(1, strlen(pathpfx) + strlen(autostartblocksh) + 2);
   1728 	if (sprintf(path, "%s/%s", pathpfx, autostartblocksh) <= 0) {
   1729 		free(path);
   1730 		free(pathpfx);
   1731 	}
   1732 
   1733 	if (access(path, X_OK) == 0)
   1734 		result = system(path);
   1735 
   1736 	/* now the non-blocking script */
   1737 	if (sprintf(path, "%s/%s", pathpfx, autostartsh) <= 0) {
   1738 		free(path);
   1739 		free(pathpfx);
   1740 	}
   1741 
   1742 	if (access(path, X_OK) == 0)
   1743 		result = system(strcat(path, " &"));
   1744 
   1745 	(void)result;
   1746 	free(pathpfx);
   1747 	free(path);
   1748 }
   1749 
   1750 void
   1751 scan(void)
   1752 {
   1753 	unsigned int i, num;
   1754 	Window d1, d2, *wins = NULL;
   1755 	XWindowAttributes wa;
   1756 
   1757 	if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
   1758 		for (i = 0; i < num; i++) {
   1759 			if (!XGetWindowAttributes(dpy, wins[i], &wa)
   1760 			|| wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
   1761 				continue;
   1762 			if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
   1763 				manage(wins[i], &wa);
   1764 		}
   1765 		for (i = 0; i < num; i++) { /* now the transients */
   1766 			if (!XGetWindowAttributes(dpy, wins[i], &wa))
   1767 				continue;
   1768 			if (XGetTransientForHint(dpy, wins[i], &d1)
   1769 			&& (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
   1770 				manage(wins[i], &wa);
   1771 		}
   1772 		if (wins)
   1773 			XFree(wins);
   1774 	}
   1775 }
   1776 
   1777 void
   1778 sendmon(Client *c, Monitor *m)
   1779 {
   1780 	if (c->mon == m)
   1781 		return;
   1782 	unfocus(c, 1);
   1783 	detach(c);
   1784 	detachstack(c);
   1785 	c->mon = m;
   1786 	c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
   1787 	c->x = c->mon->mx + (c->mon->mw - WIDTH(c)) / 2;
   1788 	c->y = c->mon->my + (c->mon->mh - HEIGHT(c)) / 2;
   1789 	attachbottom(c);
   1790 	attachstack(c);
   1791 	arrange(NULL);
   1792 	focus(getclientundermouse());
   1793 }
   1794 
   1795 void
   1796 setclientstate(Client *c, long state)
   1797 {
   1798 	long data[] = { state, None };
   1799 
   1800 	XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
   1801 		PropModeReplace, (unsigned char *)data, 2);
   1802 }
   1803 
   1804 int
   1805 sendevent(Client *c, Atom proto)
   1806 {
   1807 	int n;
   1808 	Atom *protocols;
   1809 	int exists = 0;
   1810 	XEvent ev;
   1811 
   1812 	if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
   1813 		while (!exists && n--)
   1814 			exists = protocols[n] == proto;
   1815 		XFree(protocols);
   1816 	}
   1817 	if (exists) {
   1818 		ev.type = ClientMessage;
   1819 		ev.xclient.window = c->win;
   1820 		ev.xclient.message_type = wmatom[WMProtocols];
   1821 		ev.xclient.format = 32;
   1822 		ev.xclient.data.l[0] = proto;
   1823 		ev.xclient.data.l[1] = CurrentTime;
   1824 		XSendEvent(dpy, c->win, False, NoEventMask, &ev);
   1825 	}
   1826 	return exists;
   1827 }
   1828 
   1829 void
   1830 setfocus(Client *c)
   1831 {
   1832 	if (!c->neverfocus) {
   1833 		XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
   1834 		XChangeProperty(dpy, root, netatom[NetActiveWindow],
   1835 			XA_WINDOW, 32, PropModeReplace,
   1836 			(unsigned char *) &(c->win), 1);
   1837 	}
   1838 	sendevent(c, wmatom[WMTakeFocus]);
   1839 }
   1840 
   1841 void
   1842 setfullscreen(Client *c, int fullscreen)
   1843 {
   1844 	if (fullscreen && !c->isfullscreen) {
   1845 		XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
   1846 			PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
   1847 		c->isfullscreen = 1;
   1848 		c->oldstate = c->isfloating;
   1849 		c->oldbw = c->bw;
   1850 		c->bw = 0;
   1851 		c->isfloating = 1;
   1852 		resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
   1853 		XRaiseWindow(dpy, c->win);
   1854 	} else if (!fullscreen && c->isfullscreen){
   1855 		XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
   1856 			PropModeReplace, (unsigned char*)0, 0);
   1857 		c->isfullscreen = 0;
   1858 		c->isfloating = c->oldstate;
   1859 		c->bw = c->oldbw;
   1860 		c->x = c->oldx;
   1861 		c->y = c->oldy;
   1862 		c->w = c->oldw;
   1863 		c->h = c->oldh;
   1864 		resizeclient(c, c->x, c->y, c->w, c->h);
   1865 		arrange(c->mon);
   1866 	}
   1867 }
   1868 
   1869 void
   1870 setlayout(const Arg *arg)
   1871 {
   1872 	if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
   1873 		selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag] ^= 1;
   1874 	if (arg && arg->v)
   1875 		selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt] = (Layout *)arg->v;
   1876 	strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
   1877 	if (selmon->sel)
   1878 		arrange(selmon);
   1879 	else
   1880 		drawbar(selmon);
   1881 }
   1882 
   1883 void
   1884 setmark(Client *c)
   1885 {
   1886 	if (c == mark)
   1887 		return;
   1888 	if (mark) {
   1889 		XSetWindowBorder(dpy, mark->win, scheme[mark == selmon->sel
   1890 				? SchemeSel : SchemeNorm][ColBorder].pixel);
   1891 		mark = 0;
   1892 	}
   1893 	if (c) {
   1894 		XSetWindowBorder(dpy, c->win, scheme[c == selmon->sel
   1895 				? SchemeSel : SchemeNorm][ColMark].pixel);
   1896 		mark = c;
   1897 	}
   1898 }
   1899 
   1900 /* arg > 1.0 will set mfact absolutely */
   1901 void
   1902 setmfact(const Arg *arg)
   1903 {
   1904 	float f;
   1905 
   1906 	if (!arg || !selmon->lt[selmon->sellt]->arrange)
   1907 		return;
   1908 	f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
   1909 	if (f < 0.05 || f > 0.95)
   1910 		return;
   1911 	selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag] = f;
   1912 	arrange(selmon);
   1913 }
   1914 
   1915 void
   1916 setup(void)
   1917 {
   1918 	int i;
   1919 	XSetWindowAttributes wa;
   1920 	Atom utf8string;
   1921 	struct sigaction sa;
   1922 
   1923 	/* do not transform children into zombies when they terminate */
   1924 	sigemptyset(&sa.sa_mask);
   1925 	sa.sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT | SA_RESTART;
   1926 	sa.sa_handler = SIG_IGN;
   1927 	sigaction(SIGCHLD, &sa, NULL);
   1928 
   1929 	/* clean up any zombies (inherited from .xinitrc etc) immediately */
   1930 	while (waitpid(-1, NULL, WNOHANG) > 0);
   1931 
   1932 	signal(SIGHUP, sighup);
   1933 	signal(SIGTERM, sigterm);
   1934 
   1935 	/* init screen */
   1936 	screen = DefaultScreen(dpy);
   1937 	sw = DisplayWidth(dpy, screen);
   1938 	sh = DisplayHeight(dpy, screen);
   1939 	root = RootWindow(dpy, screen);
   1940 	drw = drw_create(dpy, screen, root, sw, sh);
   1941 	if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
   1942 		die("no fonts could be loaded.");
   1943 	lrpad = drw->fonts->h;
   1944 	bh = drw->fonts->h + 2;
   1945 	updategeom();
   1946 	/* init atoms */
   1947 	utf8string = XInternAtom(dpy, "UTF8_STRING", False);
   1948 	wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
   1949 	wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
   1950 	wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
   1951 	wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
   1952 	netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
   1953 	netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
   1954 	netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
   1955 	netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
   1956 	netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
   1957 	netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
   1958 	netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
   1959 	netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
   1960 	netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
   1961 	netatom[NetClientListStacking] = XInternAtom(dpy, "_NET_CLIENT_LIST_STACKING", False);
   1962 	/* init cursors */
   1963 	cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
   1964 	cursor[CurResize] = drw_cur_create(drw, XC_sizing);
   1965 	cursor[CurMove] = drw_cur_create(drw, XC_fleur);
   1966 	/* init appearance */
   1967 	scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
   1968 	for (i = 0; i < LENGTH(colors); i++)
   1969 		scheme[i] = drw_scm_create(drw, colors[i], 4);
   1970 	/* init bars */
   1971 	updatebars();
   1972 	updatestatus();
   1973 	/* supporting window for NetWMCheck */
   1974 	wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
   1975 	XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
   1976 		PropModeReplace, (unsigned char *) &wmcheckwin, 1);
   1977 	XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8,
   1978 		PropModeReplace, (unsigned char *) "dwm", 3);
   1979 	XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32,
   1980 		PropModeReplace, (unsigned char *) &wmcheckwin, 1);
   1981 	/* EWMH support per view */
   1982 	XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
   1983 		PropModeReplace, (unsigned char *) netatom, NetLast);
   1984 	XDeleteProperty(dpy, root, netatom[NetClientList]);
   1985 	XDeleteProperty(dpy, root, netatom[NetClientListStacking]);
   1986 	/* select events */
   1987 	wa.cursor = cursor[CurNormal]->cursor;
   1988 	wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
   1989 		|ButtonPressMask|PointerMotionMask|EnterWindowMask
   1990 		|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
   1991 	XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
   1992 	XSelectInput(dpy, root, wa.event_mask);
   1993 	grabkeys();
   1994 	focus(NULL);
   1995 }
   1996 
   1997 void
   1998 seturgent(Client *c, int urg)
   1999 {
   2000 	XWMHints *wmh;
   2001 
   2002 	c->isurgent = urg;
   2003 	if (!(wmh = XGetWMHints(dpy, c->win)))
   2004 		return;
   2005 	wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
   2006 	XSetWMHints(dpy, c->win, wmh);
   2007 	XFree(wmh);
   2008 }
   2009 
   2010 void
   2011 showhide(Client *c)
   2012 {
   2013 	if (!c)
   2014 		return;
   2015 	if (ISVISIBLE(c)) {
   2016 		/* show clients top down */
   2017 		XMoveWindow(dpy, c->win, c->x, c->y);
   2018 		if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
   2019 			resize(c, c->x, c->y, c->w, c->h, 0);
   2020 		showhide(c->snext);
   2021 	} else {
   2022 		/* hide clients bottom up */
   2023 		showhide(c->snext);
   2024 		XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
   2025 	}
   2026 }
   2027 
   2028 void
   2029 sighup(int unused)
   2030 {
   2031 	Arg a = {.i = 1};
   2032 	quit(&a);
   2033 }
   2034 
   2035 void
   2036 sigterm(int unused)
   2037 {
   2038 	Arg a = {.i = 0};
   2039 	quit(&a);
   2040 }
   2041 
   2042 void
   2043 spawn(const Arg *arg)
   2044 {
   2045 	struct sigaction sa;
   2046 
   2047 	if (arg->v == dmenucmd)
   2048 		dmenumon[0] = '0' + selmon->num;
   2049 	selmon->tagset[selmon->seltags] &= ~scratchtag;
   2050 	if (fork() == 0) {
   2051 		if (dpy)
   2052 			close(ConnectionNumber(dpy));
   2053 		setsid();
   2054 
   2055 		sigemptyset(&sa.sa_mask);
   2056 		sa.sa_flags = 0;
   2057 		sa.sa_handler = SIG_DFL;
   2058 		sigaction(SIGCHLD, &sa, NULL);
   2059 
   2060 		execvp(((char **)arg->v)[0], (char **)arg->v);
   2061 		die("dwm: execvp '%s' failed:", ((char **)arg->v)[0]);
   2062 	}
   2063 }
   2064 
   2065 void
   2066 swapclient(const Arg *arg)
   2067 {
   2068 	Client *s, *m, t;
   2069 
   2070 	if (!mark || !selmon->sel || mark == selmon->sel
   2071 	    || !selmon->lt[selmon->sellt]->arrange)
   2072 		return;
   2073 	s = selmon->sel;
   2074 	m = mark;
   2075 	t = *s;
   2076 	strcpy(s->name, m->name);
   2077 	s->win = m->win;
   2078 	s->x = m->x;
   2079 	s->y = m->y;
   2080 	s->w = m->w;
   2081 	s->h = m->h;
   2082 
   2083 	m->win = t.win;
   2084 	strcpy(m->name, t.name);
   2085 	m->x = t.x;
   2086 	m->y = t.y;
   2087 	m->w = t.w;
   2088 	m->h = t.h;
   2089 
   2090 	selmon->sel = m;
   2091 	mark = s;
   2092 	focus(s);
   2093 	setmark(m);
   2094 
   2095 	arrange(s->mon);
   2096 	if (s->mon != m->mon) {
   2097 		arrange(m->mon);
   2098 	}
   2099 }
   2100 
   2101 void
   2102 swapfocus(const Arg *arg)
   2103 {
   2104 	Client *t;
   2105 
   2106 	if (!selmon->sel || !mark || selmon->sel == mark)
   2107 		return;
   2108 	t = selmon->sel;
   2109 	if (mark->mon != selmon) {
   2110 		unfocus(selmon->sel, 0);
   2111 		selmon = mark->mon;
   2112 	}
   2113 	if (ISVISIBLE(mark)) {
   2114 		focus(mark);
   2115 		restack(selmon);
   2116 	} else {
   2117 		selmon->seltags ^= 1;
   2118 		selmon->tagset[selmon->seltags] = mark->tags;
   2119 		focus(mark);
   2120 		arrange(selmon);
   2121 	}
   2122 	setmark(t);
   2123 }
   2124 
   2125 void
   2126 swapmon(const Arg *arg)
   2127 {
   2128 	Client *c;
   2129 	Monitor *targetmon;
   2130 	Monitor tmp;
   2131 
   2132 	if (!mons->next)
   2133 		return;
   2134 
   2135 	unfocus(selmon->sel, 1);
   2136 	targetmon = dirtomon(arg->i);
   2137 	tmp = *selmon;
   2138 
   2139 	selmon->mfact = targetmon->mfact;
   2140 	selmon->nmaster = targetmon->nmaster;
   2141 	selmon->seltags = targetmon->seltags;
   2142 	selmon->sellt = targetmon->sellt;
   2143 	selmon->tagset[0] = targetmon->tagset[0];
   2144 	selmon->tagset[1] = targetmon->tagset[1];
   2145 	selmon->showbar = targetmon->showbar;
   2146 	selmon->topbar = targetmon->topbar;
   2147 	selmon->clients = targetmon->clients;
   2148 	selmon->sel = targetmon->sel;
   2149 	selmon->stack = targetmon->stack;
   2150 	selmon->lt[0] = targetmon->lt[0];
   2151 	selmon->lt[1] = targetmon->lt[1];
   2152 	selmon->pertag = targetmon->pertag;
   2153 
   2154 	targetmon->mfact = tmp.mfact;
   2155 	targetmon->nmaster = tmp.nmaster;
   2156 	targetmon->seltags = tmp.seltags;
   2157 	targetmon->sellt = tmp.sellt;
   2158 	targetmon->tagset[0] = tmp.tagset[0];
   2159 	targetmon->tagset[1] = tmp.tagset[1];
   2160 	targetmon->showbar = tmp.showbar;
   2161 	targetmon->topbar = tmp.topbar;
   2162 	targetmon->clients = tmp.clients;
   2163 	targetmon->sel = tmp.sel;
   2164 	targetmon->stack = tmp.stack;
   2165 	targetmon->lt[0] = tmp.lt[0];
   2166 	targetmon->lt[1] = tmp.lt[1];
   2167 	targetmon->pertag = tmp.pertag;
   2168 
   2169 	for (c = selmon->clients; c; c = c->next)
   2170 		c->mon = selmon;
   2171 
   2172 	for (c = targetmon->clients; c; c = c->next)
   2173 		c->mon = targetmon;
   2174 
   2175 	arrange(selmon);
   2176 	arrange(targetmon);
   2177 	focus(getclientundermouse());
   2178 }
   2179 
   2180 void
   2181 swapmonvisible(const Arg *arg)
   2182 {
   2183 	Client **tc, *c;
   2184 	Client *firstnew = NULL;
   2185 	Client **dest;
   2186 	Monitor *targetmon;
   2187 	unsigned int tagset = selmon->tagset[selmon->seltags];
   2188 
   2189 	if (!mons->next)
   2190 		return;
   2191 
   2192 	targetmon = dirtomon(arg->i);
   2193 
   2194 	/* Move visible clients on selected screen to the target */
   2195 
   2196 	for (dest = &targetmon->clients; *dest; dest = &(*dest)->next);
   2197 
   2198 	tc = &selmon->clients;
   2199 	while (*tc) {
   2200 		c = *tc;
   2201 
   2202 		if (c->tags & tagset) {
   2203 			if (c == selmon->sel) {
   2204 				unfocus(c, 1);
   2205 			}
   2206 
   2207 			if (!firstnew) firstnew = c;
   2208 			c->mon = targetmon;
   2209 			*tc = c->next;
   2210 			c->next = *dest;
   2211 			*dest = c;
   2212 			dest = &c->next;
   2213 		} else {
   2214 			tc = &(*tc)->next;
   2215 		}
   2216 	}
   2217 
   2218 	/* Move matching clients on target back */
   2219 
   2220 	for (dest = &selmon->clients; *dest; dest = &(*dest)->next);
   2221 
   2222 	tc = &targetmon->clients;
   2223 	while (*tc != firstnew) {
   2224 		c = *tc;
   2225 
   2226 		if (c->tags & tagset) {
   2227 			if (c == targetmon->sel) {
   2228 				unfocus(c, 0);
   2229 			}
   2230 
   2231 			c->mon = selmon;
   2232 			*tc = c->next;
   2233 			c->next = *dest;
   2234 			*dest = c;
   2235 			dest = &c->next;
   2236 		} else {
   2237 			tc = &(*tc)->next;
   2238 		}
   2239 	}
   2240 
   2241 	/* Move client stack from selected to target */
   2242 
   2243 	dest = &targetmon->stack;
   2244 	tc = &selmon->stack;
   2245 
   2246 	while (*tc) {
   2247 		c = *tc;
   2248 
   2249 		if (c->tags & tagset) {
   2250 			*tc = c->snext;
   2251 			c->snext = *dest;
   2252 			*dest = c;
   2253 			dest = &c->snext;
   2254 		} else {
   2255 			tc = &(*tc)->snext;
   2256 		}
   2257 	}
   2258 
   2259 	/* Move client stack from target to selected */
   2260 
   2261 	tc = dest;
   2262 	dest = &selmon->stack;
   2263 
   2264 	while (*tc) {
   2265 		c = *tc;
   2266 
   2267 		if (c->tags & tagset) {
   2268 			*tc = c->snext;
   2269 			c->snext = *dest;
   2270 			*dest = c;
   2271 			dest = &c->snext;
   2272 		} else {
   2273 			tc = &(*tc)->snext;
   2274 		}
   2275 	}
   2276 
   2277 	/* Swap pertag data if only a single tag is visible */
   2278 
   2279 	if (selmon->pertag->curtag > 0 &&
   2280 	    1 << (selmon->pertag->curtag - 1) == tagset) {
   2281 		unsigned int curtag = selmon->pertag->curtag;
   2282 		selmon->pertag->nmasters[curtag] = targetmon->pertag->nmasters[curtag];
   2283 		selmon->pertag->mfacts[curtag] = targetmon->pertag->mfacts[curtag];
   2284 		selmon->pertag->sellts[curtag] = targetmon->pertag->sellts[curtag];
   2285 		selmon->pertag->ltidxs[curtag][0] = targetmon->pertag->ltidxs[curtag][0];
   2286 		selmon->pertag->ltidxs[curtag][1] = targetmon->pertag->ltidxs[curtag][1];
   2287 		selmon->pertag->showbars[curtag] = targetmon->pertag->showbars[curtag];
   2288 
   2289 		targetmon->pertag->nmasters[curtag] = selmon->nmaster;
   2290 		targetmon->pertag->mfacts[curtag] = selmon->mfact;
   2291 		targetmon->pertag->sellts[curtag] = selmon->sellt;
   2292 		targetmon->pertag->ltidxs[curtag][0] = selmon->lt[0];
   2293 		targetmon->pertag->ltidxs[curtag][1] = selmon->lt[1];
   2294 		targetmon->pertag->showbars[curtag] = selmon->showbar;
   2295 
   2296 		selmon->nmaster = selmon->pertag->nmasters[curtag];
   2297 		selmon->mfact = selmon->pertag->mfacts[curtag];
   2298 		selmon->sellt = selmon->pertag->sellts[curtag];
   2299 		selmon->lt[0] = selmon->pertag->ltidxs[curtag][0];
   2300 		selmon->lt[1] = selmon->pertag->ltidxs[curtag][1];
   2301 
   2302 		if (selmon->showbar != selmon->pertag->showbars[curtag]) {
   2303 			selmon->showbar = selmon->pertag->showbars[curtag];
   2304 			updatebarpos(selmon);
   2305 			XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
   2306 		}
   2307 
   2308 		if (targetmon->pertag->curtag == curtag) {
   2309 			targetmon->nmaster = targetmon->pertag->nmasters[curtag];
   2310 			targetmon->mfact = targetmon->pertag->mfacts[curtag];
   2311 			targetmon->sellt = targetmon->pertag->sellts[curtag];
   2312 			targetmon->lt[0] = targetmon->pertag->ltidxs[curtag][0];
   2313 			targetmon->lt[1] = targetmon->pertag->ltidxs[curtag][1];
   2314 
   2315 			if (targetmon->showbar != targetmon->pertag->showbars[curtag]) {
   2316 				targetmon->showbar = targetmon->pertag->showbars[curtag];
   2317 				updatebarpos(targetmon);
   2318 				XMoveResizeWindow(dpy, targetmon->barwin, targetmon->wx, targetmon->by, targetmon->ww, bh);
   2319 			}
   2320 		}
   2321 	}
   2322 
   2323 	/* Refocus and rearrange */
   2324 
   2325 	arrange(NULL);
   2326 	focus(getclientundermouse());
   2327 	// drawbars(); needed?
   2328 }
   2329 
   2330 void
   2331 togglemark(const Arg *arg)
   2332 {
   2333 	if (!selmon->sel)
   2334 		return;
   2335 	setmark(selmon->sel == mark ? 0 : selmon->sel);
   2336 }
   2337 
   2338 
   2339 void
   2340 tag(const Arg *arg)
   2341 {
   2342 	if (selmon->sel && arg->ui & TAGMASK) {
   2343 		selmon->sel->tags = arg->ui & TAGMASK;
   2344 		arrange(selmon);
   2345 		focus(getclientundermouse());
   2346 	}
   2347 }
   2348 
   2349 void
   2350 tagmon(const Arg *arg)
   2351 {
   2352 	if (!selmon->sel || !mons->next)
   2353 		return;
   2354 	sendmon(selmon->sel, dirtomon(arg->i));
   2355 }
   2356 
   2357 void
   2358 tile(Monitor *m)
   2359 {
   2360 	unsigned int i, n, h, mw, my, ty;
   2361 	Client *c;
   2362 
   2363 	for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
   2364 	if (n == 0)
   2365 		return;
   2366 
   2367 	if (n > m->nmaster)
   2368 		mw = m->nmaster ? m->ww * m->mfact : 0;
   2369 	else
   2370 		mw = m->ww;
   2371 	for (i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
   2372 		if (i < m->nmaster) {
   2373 			h = (m->wh - my) / (MIN(n, m->nmaster) - i);
   2374 			resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), 0);
   2375 			if (my + HEIGHT(c) < m->wh)
   2376 				my += HEIGHT(c);
   2377 		} else {
   2378 			h = (m->wh - ty) / (n - i);
   2379 			resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), 0);
   2380 			if (ty + HEIGHT(c) < m->wh)
   2381 				ty += HEIGHT(c);
   2382 		}
   2383 }
   2384 
   2385 void
   2386 togglebar(const Arg *arg)
   2387 {
   2388 	selmon->showbar = selmon->pertag->showbars[selmon->pertag->curtag] = !selmon->showbar;
   2389 	updatebarpos(selmon);
   2390 	XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
   2391 	arrange(selmon);
   2392 }
   2393 
   2394 void
   2395 togglefloating(const Arg *arg)
   2396 {
   2397 	if (!selmon->sel)
   2398 		return;
   2399 	if (selmon->sel->isfullscreen) /* no support for fullscreen windows */
   2400 		return;
   2401 	selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
   2402 	if (selmon->sel->isfloating)
   2403 		resize(selmon->sel, selmon->sel->x, selmon->sel->y,
   2404 			selmon->sel->w, selmon->sel->h, 0);
   2405 
   2406     selmon->sel->x = selmon->sel->mon->mx + (selmon->sel->mon->mw - WIDTH(selmon->sel)) / 2;
   2407     selmon->sel->y = selmon->sel->mon->my + (selmon->sel->mon->mh - HEIGHT(selmon->sel)) / 2;
   2408 
   2409 	arrange(selmon);
   2410 }
   2411 
   2412 void
   2413 togglescratch(const Arg *arg)
   2414 {
   2415 	Client *c;
   2416 	unsigned int found = 0;
   2417 
   2418 	for (c = selmon->clients; c && !(found = c->tags & scratchtag); c = c->next);
   2419 	if (found) {
   2420 		unsigned int newtagset = selmon->tagset[selmon->seltags] ^ scratchtag;
   2421 		if (newtagset) {
   2422 			selmon->tagset[selmon->seltags] = newtagset;
   2423 			arrange(selmon);
   2424 		}
   2425 		if (ISVISIBLE(c)) {
   2426 			focus(c);
   2427 			restack(selmon);
   2428 		} else
   2429 			focus(getclientundermouse());
   2430 	} else
   2431 		spawn(arg);
   2432 }
   2433 
   2434 void
   2435 toggletag(const Arg *arg)
   2436 {
   2437 	unsigned int newtags;
   2438 
   2439 	if (!selmon->sel)
   2440 		return;
   2441 	newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
   2442 	if (newtags) {
   2443 		selmon->sel->tags = newtags;
   2444 		arrange(selmon);
   2445 		focus(getclientundermouse());
   2446 	}
   2447 }
   2448 
   2449 void
   2450 toggleview(const Arg *arg)
   2451 {
   2452 	unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
   2453 	int i;
   2454 
   2455 	if (newtagset) {
   2456 		selmon->tagset[selmon->seltags] = newtagset;
   2457 
   2458 		if (newtagset == ~0) {
   2459 			selmon->pertag->prevtag = selmon->pertag->curtag;
   2460 			selmon->pertag->curtag = 0;
   2461 		}
   2462 
   2463 		/* test if the user did not select the same tag */
   2464 		if (!(newtagset & 1 << (selmon->pertag->curtag - 1))) {
   2465 			selmon->pertag->prevtag = selmon->pertag->curtag;
   2466 			for (i = 0; !(newtagset & 1 << i); i++) ;
   2467 			selmon->pertag->curtag = i + 1;
   2468 		}
   2469 
   2470 		/* apply settings for this view */
   2471 		selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag];
   2472 		selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag];
   2473 		selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
   2474 		selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
   2475 		selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1];
   2476 
   2477 		if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag])
   2478 			togglebar(NULL);
   2479 
   2480 		arrange(selmon);
   2481 		focus(getclientundermouse());
   2482 	}
   2483 }
   2484 
   2485 void
   2486 unfocus(Client *c, int setfocus)
   2487 {
   2488 	if (!c)
   2489 		return;
   2490 	grabbuttons(c, 0);
   2491 	if (c == mark)
   2492 		XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColMark].pixel);
   2493 	else
   2494 		XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
   2495 	if (setfocus) {
   2496 		XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
   2497 		XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
   2498 	}
   2499 }
   2500 
   2501 void
   2502 unmanage(Client *c, int destroyed)
   2503 {
   2504 	Monitor *m = c->mon;
   2505 	XWindowChanges wc;
   2506 
   2507 	if (c == mark)
   2508 		setmark(0);
   2509 
   2510 	detach(c);
   2511 	detachstack(c);
   2512 	if (!destroyed) {
   2513 		wc.border_width = c->oldbw;
   2514 		XGrabServer(dpy); /* avoid race conditions */
   2515 		XSetErrorHandler(xerrordummy);
   2516 		XSelectInput(dpy, c->win, NoEventMask);
   2517 		XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
   2518 		XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
   2519 		setclientstate(c, WithdrawnState);
   2520 		XSync(dpy, False);
   2521 		XSetErrorHandler(xerror);
   2522 		XUngrabServer(dpy);
   2523 	}
   2524 	free(c);
   2525 	focus(getclientundermouse());
   2526 	updateclientlist();
   2527 	arrange(m);
   2528 }
   2529 
   2530 void
   2531 unmapnotify(XEvent *e)
   2532 {
   2533 	Client *c;
   2534 	XUnmapEvent *ev = &e->xunmap;
   2535 
   2536 	if ((c = wintoclient(ev->window))) {
   2537 		if (ev->send_event)
   2538 			setclientstate(c, WithdrawnState);
   2539 		else
   2540 			unmanage(c, 0);
   2541 	}
   2542 }
   2543 
   2544 void
   2545 updatebars(void)
   2546 {
   2547 	Monitor *m;
   2548 	XSetWindowAttributes wa = {
   2549 		.override_redirect = True,
   2550 		.background_pixmap = ParentRelative,
   2551 		.event_mask = ButtonPressMask|ExposureMask
   2552 	};
   2553 	XClassHint ch = {"dwm", "dwm"};
   2554 	for (m = mons; m; m = m->next) {
   2555 		if (m->barwin)
   2556 			continue;
   2557 		m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
   2558 				CopyFromParent, DefaultVisual(dpy, screen),
   2559 				CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
   2560 		XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
   2561 		XMapRaised(dpy, m->barwin);
   2562 		XSetClassHint(dpy, m->barwin, &ch);
   2563 	}
   2564 }
   2565 
   2566 void
   2567 updatebarpos(Monitor *m)
   2568 {
   2569 	m->wy = m->my;
   2570 	m->wh = m->mh;
   2571 	if (m->showbar) {
   2572 		m->wh -= bh;
   2573 		m->by = m->topbar ? m->wy : m->wy + m->wh;
   2574 		m->wy = m->topbar ? m->wy + bh : m->wy;
   2575 	} else
   2576 		m->by = -bh;
   2577 }
   2578 
   2579 void
   2580 updateclientlist(void)
   2581 {
   2582 	Client *c;
   2583 	Monitor *m;
   2584 
   2585 	XDeleteProperty(dpy, root, netatom[NetClientList]);
   2586 	for (m = mons; m; m = m->next)
   2587 		for (c = m->clients; c; c = c->next)
   2588 			XChangeProperty(dpy, root, netatom[NetClientList],
   2589 				XA_WINDOW, 32, PropModeAppend,
   2590 				(unsigned char *) &(c->win), 1);
   2591 
   2592 	XDeleteProperty(dpy, root, netatom[NetClientListStacking]);
   2593 	for (m = mons; m; m = m->next)
   2594 		for (c = m->stack; c; c = c->snext)
   2595 			XChangeProperty(dpy, root, netatom[NetClientListStacking],
   2596 				XA_WINDOW, 32, PropModeAppend,
   2597 				(unsigned char *) &(c->win), 1);
   2598 }
   2599 
   2600 int
   2601 updategeom(void)
   2602 {
   2603 	int dirty = 0;
   2604 
   2605 #ifdef XINERAMA
   2606 	if (XineramaIsActive(dpy)) {
   2607 		int i, j, n, nn;
   2608 		Client *c;
   2609 		Client **tc;
   2610 		Monitor *m;
   2611 		XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
   2612 		XineramaScreenInfo *unique = NULL;
   2613 
   2614 		for (n = 0, m = mons; m; m = m->next, n++);
   2615 		/* only consider unique geometries as separate screens */
   2616 		unique = ecalloc(nn, sizeof(XineramaScreenInfo));
   2617 		for (i = 0, j = 0; i < nn; i++)
   2618 			if (isuniquegeom(unique, j, &info[i]))
   2619 				memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
   2620 		XFree(info);
   2621 		nn = j;
   2622 
   2623 		/* new monitors if nn > n */
   2624 		for (i = n; i < nn; i++) {
   2625 			for (m = mons; m && m->next; m = m->next);
   2626 			if (m)
   2627 				m->next = createmon();
   2628 			else
   2629 				mons = createmon();
   2630 		}
   2631 		for (i = 0, m = mons; i < nn && m; m = m->next, i++)
   2632 			if (i >= n
   2633 			|| unique[i].x_org != m->mx || unique[i].y_org != m->my
   2634 			|| unique[i].width != m->mw || unique[i].height != m->mh)
   2635 			{
   2636 				dirty = 1;
   2637 				m->num = i;
   2638 				m->mx = m->wx = unique[i].x_org;
   2639 				m->my = m->wy = unique[i].y_org;
   2640 				m->mw = m->ww = unique[i].width;
   2641 				m->mh = m->wh = unique[i].height;
   2642 				updatebarpos(m);
   2643 			}
   2644 		/* removed monitors if n > nn */
   2645 		for (i = nn; i < n; i++) {
   2646 			for (m = mons; m && m->next; m = m->next);
   2647 			if (m->clients) {
   2648 				dirty = 1;
   2649 				for (c = m->clients; c; c = c->next)
   2650 					c->mon = mons;
   2651 				for (tc = &mons->clients; *tc; tc = &(*tc)->next);
   2652 				*tc = m->clients;
   2653 				m->clients = NULL;
   2654 				for (tc = &mons->stack; *tc; tc = &(*tc)->snext);
   2655 				*tc = m->stack;
   2656 				m->stack = NULL;
   2657 			}
   2658 			if (m == selmon)
   2659 				selmon = mons;
   2660 			cleanupmon(m);
   2661 		}
   2662 		free(unique);
   2663 	} else
   2664 #endif /* XINERAMA */
   2665 	{ /* default monitor setup */
   2666 		if (!mons)
   2667 			mons = createmon();
   2668 		if (mons->mw != sw || mons->mh != sh) {
   2669 			dirty = 1;
   2670 			mons->mw = mons->ww = sw;
   2671 			mons->mh = mons->wh = sh;
   2672 			updatebarpos(mons);
   2673 		}
   2674 	}
   2675 	if (dirty) {
   2676 		selmon = mons;
   2677 		selmon = wintomon(root);
   2678 	}
   2679 	return dirty;
   2680 }
   2681 
   2682 void
   2683 updatenumlockmask(void)
   2684 {
   2685 	unsigned int i, j;
   2686 	XModifierKeymap *modmap;
   2687 
   2688 	numlockmask = 0;
   2689 	modmap = XGetModifierMapping(dpy);
   2690 	for (i = 0; i < 8; i++)
   2691 		for (j = 0; j < modmap->max_keypermod; j++)
   2692 			if (modmap->modifiermap[i * modmap->max_keypermod + j]
   2693 				== XKeysymToKeycode(dpy, XK_Num_Lock))
   2694 				numlockmask = (1 << i);
   2695 	XFreeModifiermap(modmap);
   2696 }
   2697 
   2698 void
   2699 updatesizehints(Client *c)
   2700 {
   2701 	long msize;
   2702 	XSizeHints size;
   2703 
   2704 	if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
   2705 		/* size is uninitialized, ensure that size.flags aren't used */
   2706 		size.flags = PSize;
   2707 	if (size.flags & PBaseSize) {
   2708 		c->basew = size.base_width;
   2709 		c->baseh = size.base_height;
   2710 	} else if (size.flags & PMinSize) {
   2711 		c->basew = size.min_width;
   2712 		c->baseh = size.min_height;
   2713 	} else
   2714 		c->basew = c->baseh = 0;
   2715 	if (size.flags & PResizeInc) {
   2716 		c->incw = size.width_inc;
   2717 		c->inch = size.height_inc;
   2718 	} else
   2719 		c->incw = c->inch = 0;
   2720 	if (size.flags & PMaxSize) {
   2721 		c->maxw = size.max_width;
   2722 		c->maxh = size.max_height;
   2723 	} else
   2724 		c->maxw = c->maxh = 0;
   2725 	if (size.flags & PMinSize) {
   2726 		c->minw = size.min_width;
   2727 		c->minh = size.min_height;
   2728 	} else if (size.flags & PBaseSize) {
   2729 		c->minw = size.base_width;
   2730 		c->minh = size.base_height;
   2731 	} else
   2732 		c->minw = c->minh = 0;
   2733 	if (size.flags & PAspect) {
   2734 		c->mina = (float)size.min_aspect.y / size.min_aspect.x;
   2735 		c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
   2736 	} else
   2737 		c->maxa = c->mina = 0.0;
   2738 	c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh);
   2739 	c->hintsvalid = 1;
   2740 }
   2741 
   2742 void
   2743 updatestatus(void)
   2744 {
   2745 	if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
   2746 		strcpy(stext, "dwm-"VERSION);
   2747 	drawbar(selmon);
   2748 }
   2749 
   2750 void
   2751 updatetitle(Client *c)
   2752 {
   2753 	if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
   2754 		gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
   2755 	if (c->name[0] == '\0') /* hack to mark broken clients */
   2756 		strcpy(c->name, broken);
   2757 }
   2758 
   2759 void
   2760 updatewindowtype(Client *c)
   2761 {
   2762 	Atom state = getatomprop(c, netatom[NetWMState]);
   2763 	Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
   2764 
   2765 	if (state == netatom[NetWMFullscreen])
   2766 		setfullscreen(c, 1);
   2767 	if (wtype == netatom[NetWMWindowTypeDialog])
   2768 		c->isfloating = 1;
   2769 }
   2770 
   2771 void
   2772 updatewmhints(Client *c)
   2773 {
   2774 	XWMHints *wmh;
   2775 
   2776 	if ((wmh = XGetWMHints(dpy, c->win))) {
   2777 		if (c == selmon->sel && wmh->flags & XUrgencyHint) {
   2778 			wmh->flags &= ~XUrgencyHint;
   2779 			XSetWMHints(dpy, c->win, wmh);
   2780 		} else
   2781 			c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
   2782 		if (wmh->flags & InputHint)
   2783 			c->neverfocus = !wmh->input;
   2784 		else
   2785 			c->neverfocus = 0;
   2786 		XFree(wmh);
   2787 	}
   2788 }
   2789 
   2790 void
   2791 view(const Arg *arg)
   2792 {
   2793 	int i;
   2794 	unsigned int tmptag;
   2795 
   2796 	if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
   2797 		return;
   2798 	selmon->seltags ^= 1; /* toggle sel tagset */
   2799 	if (arg->ui & TAGMASK) {
   2800 		selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
   2801 		selmon->pertag->prevtag = selmon->pertag->curtag;
   2802 
   2803 		if (arg->ui == ~0)
   2804 			selmon->pertag->curtag = 0;
   2805 		else {
   2806 			for (i = 0; !(arg->ui & 1 << i); i++) ;
   2807 			selmon->pertag->curtag = i + 1;
   2808 		}
   2809 	} else {
   2810 		tmptag = selmon->pertag->prevtag;
   2811 		selmon->pertag->prevtag = selmon->pertag->curtag;
   2812 		selmon->pertag->curtag = tmptag;
   2813 	}
   2814 
   2815 	selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag];
   2816 	selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag];
   2817 	selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
   2818 	selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
   2819 	selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1];
   2820 
   2821 	if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag])
   2822 		togglebar(NULL);
   2823 
   2824 	arrange(selmon);
   2825 	focus(getclientundermouse());
   2826 }
   2827 
   2828 void
   2829 warp(const Client *c)
   2830 {
   2831 	int x, y;
   2832 
   2833 	if (!c) {
   2834 		XWarpPointer(dpy, None, root, 0, 0, 0, 0, selmon->wx + selmon->ww/2, selmon->wy + selmon->wh/2);
   2835 		return;
   2836 	}
   2837 
   2838 	if (!getrootptr(&x, &y) ||
   2839 	    (x > c->x - c->bw &&
   2840 	     y > c->y - c->bw &&
   2841 	     x < c->x + c->w + c->bw*2 &&
   2842 	     y < c->y + c->h + c->bw*2) ||
   2843 	    (y > c->mon->by && y < c->mon->by + bh) ||
   2844 	    (c->mon->topbar && !y))
   2845 		return;
   2846 
   2847 	XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w / 2, c->h / 2);
   2848 }
   2849 
   2850 Client *
   2851 wintoclient(Window w)
   2852 {
   2853 	Client *c;
   2854 	Monitor *m;
   2855 
   2856 	for (m = mons; m; m = m->next)
   2857 		for (c = m->clients; c; c = c->next)
   2858 			if (c->win == w)
   2859 				return c;
   2860 	return NULL;
   2861 }
   2862 
   2863 Monitor *
   2864 wintomon(Window w)
   2865 {
   2866 	int x, y;
   2867 	Client *c;
   2868 	Monitor *m;
   2869 
   2870 	if (w == root && getrootptr(&x, &y))
   2871 		return recttomon(x, y, 1, 1);
   2872 	for (m = mons; m; m = m->next)
   2873 		if (w == m->barwin)
   2874 			return m;
   2875 	if ((c = wintoclient(w)))
   2876 		return c->mon;
   2877 	return selmon;
   2878 }
   2879 
   2880 /* There's no way to check accesses to destroyed windows, thus those cases are
   2881  * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
   2882  * default error handler, which may call exit. */
   2883 int
   2884 xerror(Display *dpy, XErrorEvent *ee)
   2885 {
   2886 	if (ee->error_code == BadWindow
   2887 	|| (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
   2888 	|| (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
   2889 	|| (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
   2890 	|| (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
   2891 	|| (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
   2892 	|| (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
   2893 	|| (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
   2894 	|| (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
   2895 		return 0;
   2896 	fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
   2897 		ee->request_code, ee->error_code);
   2898 	return xerrorxlib(dpy, ee); /* may call exit */
   2899 }
   2900 
   2901 int
   2902 xerrordummy(Display *dpy, XErrorEvent *ee)
   2903 {
   2904 	return 0;
   2905 }
   2906 
   2907 /* Startup Error handler to check if another window manager
   2908  * is already running. */
   2909 int
   2910 xerrorstart(Display *dpy, XErrorEvent *ee)
   2911 {
   2912 	die("dwm: another window manager is already running");
   2913 	return -1;
   2914 }
   2915 
   2916 void
   2917 zoom(const Arg *arg)
   2918 {
   2919 	Client *c = selmon->sel;
   2920 
   2921 	if (!selmon->lt[selmon->sellt]->arrange || !c || c->isfloating)
   2922 		return;
   2923 	if (c == nexttiled(selmon->clients) && !(c = nexttiled(c->next)))
   2924 		return;
   2925 	pop(c);
   2926 }
   2927 
   2928 int
   2929 main(int argc, char *argv[])
   2930 {
   2931 	if (argc == 2 && !strcmp("-v", argv[1]))
   2932 		die("dwm-"VERSION);
   2933 	else if (argc != 1)
   2934 		die("usage: dwm [-v]");
   2935 	if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
   2936 		fputs("warning: no locale support\n", stderr);
   2937 	if (!(dpy = XOpenDisplay(NULL)))
   2938 		die("dwm: cannot open display");
   2939 	checkotherwm();
   2940 	setup();
   2941 #ifdef __OpenBSD__
   2942 	if (pledge("stdio rpath proc exec", NULL) == -1)
   2943 		die("pledge");
   2944 #endif /* __OpenBSD__ */
   2945 	scan();
   2946 	runautostart();
   2947 	run();
   2948 	if(restart) execvp(argv[0], argv);
   2949 	cleanup();
   2950 	XCloseDisplay(dpy);
   2951 	return EXIT_SUCCESS;
   2952 }
   2953 
   2954 void
   2955 focusmaster(const Arg *arg)
   2956 {
   2957 	Client *c;
   2958 
   2959 	if (selmon->nmaster < 1)
   2960 		return;
   2961 	if (!selmon->sel || (selmon->sel->isfullscreen && lockfullscreen))
   2962 		return;
   2963 
   2964 	c = nexttiled(selmon->clients);
   2965 
   2966 	if (c) {
   2967 		focus(c);
   2968 		restack(selmon);
   2969 	}
   2970 }