dwm-old

my heavily-patched and customized dwm build
git clone git://git.kocotian.pl/dwm.git
Log | Files | Refs | README | LICENSE

dwm.c (87269B)


      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/wait.h>
     33 #include <X11/cursorfont.h>
     34 #include <X11/keysym.h>
     35 #include <X11/Xatom.h>
     36 #include <X11/Xlib.h>
     37 #include <X11/Xproto.h>
     38 #include <X11/Xutil.h>
     39 #ifdef XINERAMA
     40 #include <X11/extensions/Xinerama.h>
     41 #endif /* XINERAMA */
     42 #include <X11/Xft/Xft.h>
     43 #include <X11/Xlib-xcb.h>
     44 #include <xcb/res.h>
     45 #ifdef __OpenBSD__
     46 #include <sys/sysctl.h>
     47 #include <kvm.h>
     48 #endif /* __OpenBSD */
     49 
     50 #include "drw.h"
     51 #include "util.h"
     52 
     53 /* macros */
     54 #define BUTTONMASK              (ButtonPressMask|ButtonReleaseMask)
     55 #define CLEANMASK(mask)         (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
     56 #define INTERSECT(x,y,w,h,m)    (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
     57                                * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
     58 #define ISVISIBLEONTAG(C, T)    ((C->tags & T))
     59 #define ISVISIBLE(C)            ISVISIBLEONTAG(C, C->mon->tagset[C->mon->seltags])
     60 #define LENGTH(X)               (sizeof X / sizeof X[0])
     61 #define MOUSEMASK               (BUTTONMASK|PointerMotionMask)
     62 #define WIDTH(X)                ((X)->w + 2 * (X)->bw)
     63 #define HEIGHT(X)               ((X)->h + 2 * (X)->bw)
     64 #define TAGMASK                 ((1 << LENGTH(tags)) - 1)
     65 #define TEXTW(X)                (drw_fontset_getwidth(drw, (X)) + lrpad)
     66 
     67 /* enums */
     68 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
     69 enum { SchemeNorm, SchemeSel, SchemeStatus,
     70        SchemeTagsSel, SchemeTagsNorm,
     71        SchemeTagLnSel, SchemeTagLnOcc, SchemeTagLnNorm,
     72        SchemeInfoSel, SchemeInfoNorm, SchemeTermSel, SchemeTermNorm,
     73        StatusLn, StatusBlack, StatusRed, StatusGreen, StatusYellow,
     74        StatusBlue, StatusMagenta, StatusCyan, StatusWhite }; /* color schemes */
     75 enum { NetSupported, NetWMName, NetWMState, NetWMCheck,
     76        NetWMFullscreen, NetActiveWindow, NetWMWindowType,
     77        NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */
     78 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
     79 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
     80        ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
     81 
     82 typedef union {
     83 	int i;
     84 	unsigned int ui;
     85 	float f;
     86 	const void *v;
     87 } Arg;
     88 
     89 typedef struct {
     90 	unsigned int click;
     91 	unsigned int mask;
     92 	unsigned int button;
     93 	void (*func)(const Arg *arg);
     94 	const Arg arg;
     95 } Button;
     96 
     97 typedef struct Monitor Monitor;
     98 typedef struct Client Client;
     99 struct Client {
    100 	char name[256];
    101 	float mina, maxa;
    102 	float cfact;
    103 	int x, y, w, h;
    104 	int oldx, oldy, oldw, oldh;
    105 	int basew, baseh, incw, inch, maxw, maxh, minw, minh;
    106 	int bw, oldbw;
    107 	unsigned int tags;
    108 	int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen, isterminal, noswallow;
    109 	pid_t pid;
    110 	Client *next;
    111 	Client *snext;
    112 	Client *swallowing;
    113 	Monitor *mon;
    114 	Window win;
    115 	Client *crop;
    116 };
    117 
    118 typedef struct {
    119 	unsigned int mod;
    120 	KeySym keysym;
    121 	void (*func)(const Arg *);
    122 	const Arg arg;
    123 } Key;
    124 
    125 typedef struct {
    126 	const char *symbol;
    127 	void (*arrange)(Monitor *);
    128 } Layout;
    129 
    130 typedef struct {
    131 	const char *symbol;
    132 	void (*direction)(Client *);
    133 } Direction;
    134 
    135 typedef struct Pertag Pertag;
    136 struct Monitor {
    137 	char ltsymbol[16];
    138 	char attsymbol[16];
    139 	unsigned int attachdirection;
    140 	float mfact;
    141 	int nmaster;
    142 	int num;
    143 	int by;               /* bar geometry */
    144 	int eby;	      /* extra bar geometry */
    145 	int mx, my, mw, mh;   /* screen size */
    146 	int wx, wy, ww, wh;   /* window area  */
    147 	unsigned int smartgaps, smartbord, ogappx, igappx;
    148 	unsigned int seltags;
    149 	unsigned int sellt;
    150 	unsigned int tagset[2];
    151 	int showbar;
    152 	int topbar;
    153 	Client *clients;
    154 	Client *sel;
    155 	Client *stack;
    156 	Monitor *next;
    157 	Window barwin;
    158 	Window extrabarwin;
    159 	const Layout *lt[2];
    160 	Pertag *pertag;
    161 };
    162 
    163 typedef struct {
    164 	const char *class;
    165 	const char *instance;
    166 	const char *title;
    167 	unsigned int tags;
    168 	int isfloating;
    169 	int isterminal;
    170 	int noswallow;
    171 	int monitor;
    172 } Rule;
    173 
    174 /* function declarations */
    175 static void applyrules(Client *c);
    176 static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact);
    177 static void arrange(Monitor *m);
    178 static void arrangemon(Monitor *m);
    179 static void attach(Client *c);
    180 static void attachabove(Client *c);
    181 static void attachaside(Client *c);
    182 static void attachbelow(Client *c);
    183 static void attachbottom(Client *c);
    184 static void attachtop(Client *c);
    185 static void attachstack(Client *c);
    186 static void bstack(Monitor *m);
    187 static void bstackhoriz(Monitor *m);
    188 static void buttonpress(XEvent *e);
    189 static void centeredmaster(Monitor *m);
    190 static void centeredfloatingmaster(Monitor *m);
    191 static void checkotherwm(void);
    192 static void cleanup(void);
    193 static void cleanupmon(Monitor *mon);
    194 static void clientmessage(XEvent *e);
    195 static void configure(Client *c);
    196 static void configurenotify(XEvent *e);
    197 static void configurerequest(XEvent *e);
    198 static void copyvalidchars(char *text, char *rawtext);
    199 static Monitor *createmon(void);
    200 static void destroynotify(XEvent *e);
    201 static void detach(Client *c);
    202 static void detachstack(Client *c);
    203 static Monitor *dirtomon(int dir);
    204 static void drawbar(Monitor *m);
    205 static void drawbars(void);
    206 static void enternotify(XEvent *e);
    207 static void expose(XEvent *e);
    208 static void focus(Client *c);
    209 static void focusin(XEvent *e);
    210 static void focusmon(const Arg *arg);
    211 static void focusstack(const Arg *arg);
    212 static int getdwmblockspid();
    213 static Atom getatomprop(Client *c, Atom prop);
    214 static int getrootptr(int *x, int *y);
    215 static long getstate(Window w);
    216 static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
    217 static void grabbuttons(Client *c, int focused);
    218 static void grabkeys(void);
    219 static void incnmaster(const Arg *arg);
    220 static void keypress(XEvent *e);
    221 static void killclient(const Arg *arg);
    222 static void manage(Window w, XWindowAttributes *wa);
    223 static void mappingnotify(XEvent *e);
    224 static void maprequest(XEvent *e);
    225 static void monocle(Monitor *m);
    226 static void motionnotify(XEvent *e);
    227 static void moveresize(const Arg *arg);
    228 static void moveresizeedge(const Arg *arg);
    229 static void movemouse(const Arg *arg);
    230 static Client *nexttagged(Client *c);
    231 static Client *nexttiled(Client *c);
    232 static void pop(Client *);
    233 static void propertynotify(XEvent *e);
    234 static void quit(const Arg *arg);
    235 static Monitor *recttomon(int x, int y, int w, int h);
    236 static void reorganizetags(const Arg *arg);
    237 static void resetlayout(const Arg *arg);
    238 static void resize(Client *c, int x, int y, int w, int h, int interact);
    239 static void resizeclient(Client *c, int x, int y, int w, int h);
    240 static void resizemouse(const Arg *arg);
    241 static void restack(Monitor *m);
    242 static void run(void);
    243 static void scan(void);
    244 static int sendevent(Client *c, Atom proto);
    245 static void sendmon(Client *c, Monitor *m);
    246 static void setclientstate(Client *c, long state);
    247 static void setfocus(Client *c);
    248 static void setfullscreen(Client *c, int fullscreen);
    249 static void setattach(const Arg *arg);
    250 static void setlayout(const Arg *arg);
    251 static void setcfact(const Arg *arg);
    252 static void setmfact(const Arg *arg);
    253 static void setigappx(const Arg *arg);
    254 static void setogappx(const Arg *arg);
    255 static void gaptog(const Arg *arg);
    256 static void sbtog(const Arg *arg);
    257 static void setup(void);
    258 static void seturgent(Client *c, int urg);
    259 static void showhide(Client *c);
    260 static void sigchld(int unused);
    261 static void sigdwmblocks(const Arg *arg);
    262 static void sighup(int unused);
    263 static void sigterm(int unused);
    264 static void spawn(const Arg *arg);
    265 static void swaptags(const Arg *arg);
    266 static void tag(const Arg *arg);
    267 static void tagmon(const Arg *arg);
    268 static void dynamiclayout(Monitor *m);
    269 static void tile(Monitor *);
    270 static void togglebar(const Arg *arg);
    271 static void togglefloating(const Arg *arg);
    272 static void togglefullscr(const Arg *arg);
    273 static void toggletag(const Arg *arg);
    274 static void toggleview(const Arg *arg);
    275 static void unfocus(Client *c, int setfocus);
    276 static void unmanage(Client *c, int destroyed);
    277 static void unmapnotify(XEvent *e);
    278 static void updatebarpos(Monitor *m);
    279 static void updatebars(void);
    280 static void updateclientlist(void);
    281 static int updategeom(void);
    282 static void updatenumlockmask(void);
    283 static void updatesizehints(Client *c);
    284 static void updatestatus(void);
    285 static void updatetitle(Client *c);
    286 static void updatewindowtype(Client *c);
    287 static void updatewmhints(Client *c);
    288 static void view(const Arg *arg);
    289 static Client *wintoclient(Window w);
    290 static Monitor *wintomon(Window w);
    291 static int xerror(Display *dpy, XErrorEvent *ee);
    292 static int xerrordummy(Display *dpy, XErrorEvent *ee);
    293 static int xerrorstart(Display *dpy, XErrorEvent *ee);
    294 static void zoom(const Arg *arg);
    295 
    296 static pid_t getparentprocess(pid_t p);
    297 static int isdescprocess(pid_t p, pid_t c);
    298 static Client *swallowingclient(Window w);
    299 static Client *termforwin(const Client *c);
    300 static pid_t winpid(Window w);
    301 
    302 /* variables */
    303 static const char broken[] = "broken";
    304 static char stext[2048];
    305 static char estext[2048];
    306 static char rawstext[2048];
    307 static int dwmblockssig;
    308 pid_t dwmblockspid = 0;
    309 static int screen;
    310 static int sw, sh;           /* X display screen geometry width, height */
    311 static int bh, blw = 0;      /* bar geometry */
    312 static int lrpad;            /* sum of left and right padding for text */
    313 static int (*xerrorxlib)(Display *, XErrorEvent *);
    314 static unsigned int numlockmask = 0;
    315 static void (*handler[LASTEvent]) (XEvent *) = {
    316 	[ButtonPress] = buttonpress,
    317 	[ClientMessage] = clientmessage,
    318 	[ConfigureRequest] = configurerequest,
    319 	[ConfigureNotify] = configurenotify,
    320 	[DestroyNotify] = destroynotify,
    321 	[EnterNotify] = enternotify,
    322 	[Expose] = expose,
    323 	[FocusIn] = focusin,
    324 	[KeyPress] = keypress,
    325 	[MappingNotify] = mappingnotify,
    326 	[MapRequest] = maprequest,
    327 	[MotionNotify] = motionnotify,
    328 	[PropertyNotify] = propertynotify,
    329 	[UnmapNotify] = unmapnotify
    330 };
    331 static Atom wmatom[WMLast], netatom[NetLast];
    332 static int restart = 0;
    333 static int running = 1;
    334 static Cur *cursor[CurLast];
    335 static Clr **scheme;
    336 static Display *dpy;
    337 static Drw *drw;
    338 static Monitor *mons, *selmon;
    339 static Window root, wmcheckwin;
    340 
    341 static xcb_connection_t *xcon;
    342 
    343 /* configuration, allows nested code to access above variables */
    344 #include "config.h"
    345 
    346 struct Pertag {
    347 	unsigned int curtag, prevtag; /* current and previous tag */
    348 	int nmasters[LENGTH(tags) + 1]; /* number of windows in master area */
    349 	float mfacts[LENGTH(tags) + 1]; /* mfacts per tag */
    350 	unsigned int sellts[LENGTH(tags) + 1]; /* selected layouts */
    351 	const Layout *ltidxs[LENGTH(tags) + 1][2]; /* matrix of tags and layouts indexes  */
    352 	int showbars[LENGTH(tags) + 1]; /* display bar for the current tag */
    353 };
    354 
    355 /* compile-time check if all tags fit into an unsigned int bit array. */
    356 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
    357 
    358 /* function implementations */
    359 Client *
    360 cropwintoclient(Window w)
    361 {
    362 	Client *c;
    363 	Monitor *m;
    364 
    365 	for (m = mons; m; m = m->next)
    366 		for (c = m->clients; c; c = c->next)
    367 			if (c->crop && c->crop->win == w)
    368 				return c;
    369 	return NULL;
    370 }
    371 
    372 void
    373 cropwindow(Client *c)
    374 {
    375 	int x, y;
    376 	XEvent ev;
    377 	XSetWindowAttributes wa = { .event_mask = SubstructureRedirectMask };
    378 
    379 	if (!getrootptr(&x, &y))
    380 		return;
    381 	if (!c->crop) {
    382 		c->crop = ecalloc(1, sizeof(Client));
    383 		memcpy(c->crop, c, sizeof(Client));
    384 		c->crop->crop = NULL;
    385 		c->crop->x = c->crop->y = c->crop->bw = 0;
    386 		c->basew = c->baseh = c->mina = c->maxa = 0;
    387 		c->maxw = c->maxh = c->incw = c->inch = 0;
    388 		c->minw = c->minh = 1;
    389 		if (!c->isfloating)
    390 			togglefloating(NULL);
    391 		c->win = XCreateWindow(dpy, root, x, y, 1, 1, c->bw,
    392 			0, 0, 0, CWEventMask, &wa);
    393 		XReparentWindow(dpy, c->crop->win, c->win, 0, 0);
    394 		XMapWindow(dpy, c->win);
    395 		focus(c);
    396 		XCheckTypedWindowEvent(dpy, c->crop->win, UnmapNotify, &ev);
    397 		if (XCheckTypedWindowEvent(dpy, root, UnmapNotify, &ev)
    398 		&& ev.xunmap.window != c->crop->win)
    399 			XPutBackEvent(dpy, &ev);
    400 	}
    401 	resizeclient(c->crop, c->crop->x + c->x - x, c->crop->y + c->y - y,
    402 		     c->crop->w, c->crop->h);
    403 	resizeclient(c, x, y, 1, 1);
    404 }
    405 
    406 void
    407 cropdelete(Client *c)
    408 {
    409 	Client *crop;
    410 	XEvent ev;
    411 
    412 	c->crop->x += c->x;
    413 	c->crop->y += c->y;
    414 	c->crop->bw = c->bw;
    415 	c->crop->next = c->next;
    416 	c->crop->snext = c->snext;
    417 	c->crop->tags = c->tags;
    418 	c->crop->mon = c->mon;
    419 	XReparentWindow(dpy, c->crop->win, root, c->crop->x, c->crop->y);
    420 	XDestroyWindow(dpy, c->win);
    421 	crop = c->crop;
    422 	memcpy(c, c->crop, sizeof(Client));
    423 	free(crop);
    424 	resize(c, c->x, c->y, c->w, c->h, 0);
    425 	focus(c);
    426 	XCheckTypedWindowEvent(dpy, c->win, UnmapNotify, &ev);
    427 }
    428 
    429 void
    430 cropresize(Client* c)
    431 {
    432 	resizeclient(c->crop,
    433 		     BETWEEN(c->crop->x, -(c->crop->w), 0) ? c->crop->x : 0,
    434 		     BETWEEN(c->crop->y, -(c->crop->h), 0) ? c->crop->y : 0,
    435 		     c->crop->w, c->crop->h);
    436 	resize(c, c->x, c->y,
    437 	       MIN(c->w, c->crop->x + c->crop->w),
    438 	       MIN(c->h, c->crop->y + c->crop->h), 0);
    439 }
    440 
    441 void
    442 applyrules(Client *c)
    443 {
    444 	const char *class, *instance;
    445 	unsigned int i;
    446 	const Rule *r;
    447 	Monitor *m;
    448 	XClassHint ch = { NULL, NULL };
    449 
    450 	/* rule matching */
    451 	c->isfloating = 0;
    452 	c->tags = 0;
    453 	XGetClassHint(dpy, c->win, &ch);
    454 	class    = ch.res_class ? ch.res_class : broken;
    455 	instance = ch.res_name  ? ch.res_name  : broken;
    456 
    457 	for (i = 0; i < LENGTH(rules); i++) {
    458 		r = &rules[i];
    459 		if ((!r->title || strstr(c->name, r->title))
    460 		&& (!r->class || strstr(class, r->class))
    461 		&& (!r->instance || strstr(instance, r->instance)))
    462 		{
    463 			c->isterminal = r->isterminal;
    464 			c->noswallow  = r->noswallow;
    465 			c->isfloating = r->isfloating;
    466 			c->tags |= r->tags;
    467 			for (m = mons; m && m->num != r->monitor; m = m->next);
    468 			if (m)
    469 				c->mon = m;
    470 		}
    471 	}
    472 	if (ch.res_class)
    473 		XFree(ch.res_class);
    474 	if (ch.res_name)
    475 		XFree(ch.res_name);
    476 	c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
    477 }
    478 
    479 int
    480 applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact)
    481 {
    482 	int baseismin;
    483 	Monitor *m = c->mon;
    484 
    485 	/* set minimum possible */
    486 	*w = MAX(1, *w);
    487 	*h = MAX(1, *h);
    488 	if (interact) {
    489 		if (*x > sw)
    490 			*x = sw - WIDTH(c);
    491 		if (*y > sh)
    492 			*y = sh - HEIGHT(c);
    493 		if (*x + *w + 2 * c->bw < 0)
    494 			*x = 0;
    495 		if (*y + *h + 2 * c->bw < 0)
    496 			*y = 0;
    497 	} else {
    498 		if (*x >= m->wx + m->ww)
    499 			*x = m->wx + m->ww - WIDTH(c);
    500 		if (*y >= m->wy + m->wh)
    501 			*y = m->wy + m->wh - HEIGHT(c);
    502 		if (*x + *w + 2 * c->bw <= m->wx)
    503 			*x = m->wx;
    504 		if (*y + *h + 2 * c->bw <= m->wy)
    505 			*y = m->wy;
    506 	}
    507 	if (*h < bh)
    508 		*h = bh;
    509 	if (*w < bh)
    510 		*w = bh;
    511 	if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
    512 		/* see last two sentences in ICCCM 4.1.2.3 */
    513 		baseismin = c->basew == c->minw && c->baseh == c->minh;
    514 		if (!baseismin) { /* temporarily remove base dimensions */
    515 			*w -= c->basew;
    516 			*h -= c->baseh;
    517 		}
    518 		/* adjust for aspect limits */
    519 		if (c->mina > 0 && c->maxa > 0) {
    520 			if (c->maxa < (float)*w / *h)
    521 				*w = *h * c->maxa + 0.5;
    522 			else if (c->mina < (float)*h / *w)
    523 				*h = *w * c->mina + 0.5;
    524 		}
    525 		if (baseismin) { /* increment calculation requires this */
    526 			*w -= c->basew;
    527 			*h -= c->baseh;
    528 		}
    529 		/* adjust for increment value */
    530 		if (c->incw)
    531 			*w -= *w % c->incw;
    532 		if (c->inch)
    533 			*h -= *h % c->inch;
    534 		/* restore base dimensions */
    535 		*w = MAX(*w + c->basew, c->minw);
    536 		*h = MAX(*h + c->baseh, c->minh);
    537 		if (c->maxw)
    538 			*w = MIN(*w, c->maxw);
    539 		if (c->maxh)
    540 			*h = MIN(*h, c->maxh);
    541 	}
    542 	return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
    543 }
    544 
    545 void
    546 arrange(Monitor *m)
    547 {
    548 	if (m)
    549 		showhide(m->stack);
    550 	else for (m = mons; m; m = m->next)
    551 		showhide(m->stack);
    552 	if (m) {
    553 		arrangemon(m);
    554 		restack(m);
    555 	} else for (m = mons; m; m = m->next)
    556 		arrangemon(m);
    557 }
    558 
    559 void
    560 arrangemon(Monitor *m)
    561 {
    562 	strncpy(m->attsymbol, directions[m->attachdirection].symbol, strlen(directions[m->attachdirection].symbol) + 1);
    563 	strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
    564 	if (m->lt[m->sellt]->arrange)
    565 		m->lt[m->sellt]->arrange(m);
    566 }
    567 
    568 void
    569 attach(Client *c)
    570 {
    571 	c->next = c->mon->clients;
    572 	c->mon->clients = c;
    573 }
    574 
    575 void
    576 attachabove(Client *c)
    577 {
    578 	if (c->mon->sel == NULL || c->mon->sel == c->mon->clients || c->mon->sel->isfloating) {
    579 		attach(c);
    580 		return;
    581 	}
    582 
    583 	Client *at;
    584 	for (at = c->mon->clients; at->next != c->mon->sel; at = at->next);
    585 	c->next = at->next;
    586 	at->next = c;
    587 }
    588 
    589 void
    590 attachaside(Client *c) {
    591 	Client *at = nexttagged(c);
    592 	if(!at) {
    593 		attach(c);
    594 		return;
    595 		}
    596 	c->next = at->next;
    597 	at->next = c;
    598 }
    599 
    600 void
    601 attachbelow(Client *c)
    602 {
    603 	if(c->mon->sel == NULL || c->mon->sel == c || c->mon->sel->isfloating) {
    604 		attach(c);
    605 		return;
    606 	}
    607 	c->next = c->mon->sel->next;
    608 	c->mon->sel->next = c;
    609 }
    610 
    611 void
    612 attachbottom(Client *c)
    613 {
    614 	Client *below = c->mon->clients;
    615 	for (; below && below->next; below = below->next);
    616 	c->next = NULL;
    617 	if (below)
    618 		below->next = c;
    619 	else
    620 		c->mon->clients = c;
    621 }
    622 
    623 void
    624 attachtop(Client *c)
    625 {
    626 	int n;
    627 	Monitor *m = selmon;
    628 	Client *below;
    629 
    630 	for (n = 1, below = c->mon->clients;
    631 		below && below->next && (below->isfloating || !ISVISIBLEONTAG(below, c->tags) || n != m->nmaster);
    632 		n = below->isfloating || !ISVISIBLEONTAG(below, c->tags) ? n + 0 : n + 1, below = below->next);
    633 	c->next = NULL;
    634 	if (below) {
    635 		c->next = below->next;
    636 		below->next = c;
    637 	}
    638 	else
    639 		c->mon->clients = c;
    640 }
    641 
    642 void
    643 attachstack(Client *c)
    644 {
    645 	c->snext = c->mon->stack;
    646 	c->mon->stack = c;
    647 }
    648 
    649 void
    650 swallow(Client *p, Client *c)
    651 {
    652 
    653 	if (c->noswallow || c->isterminal)
    654 		return;
    655 	if (c->noswallow && !swallowfloating && c->isfloating)
    656 		return;
    657 
    658 	detach(c);
    659 	detachstack(c);
    660 
    661 	setclientstate(c, WithdrawnState);
    662 	XUnmapWindow(dpy, p->win);
    663 
    664 	p->swallowing = c;
    665 	c->mon = p->mon;
    666 
    667 	Window w = p->win;
    668 	p->win = c->win;
    669 	c->win = w;
    670 	updatetitle(p);
    671 	XMoveResizeWindow(dpy, p->win, p->x, p->y, p->w, p->h);
    672 	arrange(p->mon);
    673 	configure(p);
    674 	updateclientlist();
    675 }
    676 
    677 void
    678 unswallow(Client *c)
    679 {
    680 	c->win = c->swallowing->win;
    681 
    682 	free(c->swallowing);
    683 	c->swallowing = NULL;
    684 
    685 	/* unfullscreen the client */
    686 	setfullscreen(c, 0);
    687 	updatetitle(c);
    688 	arrange(c->mon);
    689 	XMapWindow(dpy, c->win);
    690 	XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
    691 	setclientstate(c, NormalState);
    692 	focus(NULL);
    693 	arrange(c->mon);
    694 }
    695 
    696 void
    697 buttonpress(XEvent *e)
    698 {
    699 	unsigned int i, x, click;
    700 	Arg arg = {0};
    701 	Client *c;
    702 	Monitor *m;
    703 	XButtonPressedEvent *ev = &e->xbutton;
    704 
    705 	click = ClkRootWin;
    706 	/* focus monitor if necessary */
    707 	if ((m = wintomon(ev->window)) && m != selmon) {
    708 		unfocus(selmon->sel, 1);
    709 		selmon = m;
    710 		focus(NULL);
    711 	}
    712 	if (ev->window == selmon->barwin) {
    713 		i = x = 0;
    714 		do
    715 			x += TEXTW(tags[i]);
    716 		while (ev->x >= x && ++i < LENGTH(tags));
    717 		if (i < LENGTH(tags)) {
    718 			click = ClkTagBar;
    719 			arg.ui = 1 << i;
    720 		} else if (ev->x < x + blw)
    721 			click = ClkLtSymbol;
    722 		else if (ev->x > (x = selmon->ww - TEXTW(stext) + lrpad)) {
    723 			click = ClkStatusText;
    724 			char *text = rawstext;
    725 			int i = -1;
    726 			char ch;
    727 			dwmblockssig = 0;
    728 			while (text[++i]) {
    729 				if ((unsigned char)text[i] < ' ') {
    730 					ch = text[i];
    731 					text[i] = '\0';
    732 					x += TEXTW(text) - lrpad;
    733 					text[i] = ch;
    734 					text += i+1;
    735 					i = -1;
    736 					if (x >= ev->x) break;
    737 					dwmblockssig = ch;
    738 				}
    739 			}
    740 		} else
    741 			click = ClkWinTitle;
    742 	} else if ((c = wintoclient(ev->window))) {
    743 		focus(c);
    744 		restack(selmon);
    745 		XAllowEvents(dpy, ReplayPointer, CurrentTime);
    746 		click = ClkClientWin;
    747 	}
    748 	for (i = 0; i < LENGTH(buttons); i++)
    749 		if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
    750 		&& CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
    751 			buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
    752 }
    753 
    754 void
    755 checkotherwm(void)
    756 {
    757 	xerrorxlib = XSetErrorHandler(xerrorstart);
    758 	/* this causes an error if some other window manager is running */
    759 	XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
    760 	XSync(dpy, False);
    761 	XSetErrorHandler(xerror);
    762 	XSync(dpy, False);
    763 }
    764 
    765 void
    766 cleanup(void)
    767 {
    768 	Arg a = {.ui = ~0};
    769 	Layout foo = { "", NULL };
    770 	Monitor *m;
    771 	size_t i;
    772 
    773 	view(&a);
    774 	selmon->lt[selmon->sellt] = &foo;
    775 	for (m = mons; m; m = m->next)
    776 		while (m->stack)
    777 			unmanage(m->stack, 0);
    778 	XUngrabKey(dpy, AnyKey, AnyModifier, root);
    779 	while (mons)
    780 		cleanupmon(mons);
    781 	for (i = 0; i < CurLast; i++)
    782 		drw_cur_free(drw, cursor[i]);
    783 	for (i = 0; i < LENGTH(colors); i++)
    784 		free(scheme[i]);
    785 	XDestroyWindow(dpy, wmcheckwin);
    786 	drw_free(drw);
    787 	XSync(dpy, False);
    788 	XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
    789 	XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
    790 }
    791 
    792 void
    793 cleanupmon(Monitor *mon)
    794 {
    795 	Monitor *m;
    796 
    797 	if (mon == mons)
    798 		mons = mons->next;
    799 	else {
    800 		for (m = mons; m && m->next != mon; m = m->next);
    801 		m->next = mon->next;
    802 	}
    803 	XUnmapWindow(dpy, mon->barwin);
    804 	XUnmapWindow(dpy, mon->extrabarwin);
    805 	XDestroyWindow(dpy, mon->barwin);
    806 	XDestroyWindow(dpy, mon->extrabarwin);
    807 	free(mon);
    808 }
    809 
    810 void
    811 clientmessage(XEvent *e)
    812 {
    813 	XClientMessageEvent *cme = &e->xclient;
    814 	Client *c = wintoclient(cme->window);
    815 
    816 	if (!c && !(c = cropwintoclient(cme->window)))
    817 		return;
    818 	if (cme->message_type == netatom[NetWMState]) {
    819 		if (cme->data.l[1] == netatom[NetWMFullscreen]
    820 		|| cme->data.l[2] == netatom[NetWMFullscreen])
    821 			setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD    */
    822 				|| (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
    823 	} else if (cme->message_type == netatom[NetActiveWindow]) {
    824 		if (c != selmon->sel && !c->isurgent)
    825 			seturgent(c, 1);
    826 	}
    827 }
    828 
    829 void
    830 configure(Client *c)
    831 {
    832 	XConfigureEvent ce;
    833 
    834 	ce.type = ConfigureNotify;
    835 	ce.display = dpy;
    836 	ce.event = c->win;
    837 	ce.window = c->win;
    838 	ce.x = c->x;
    839 	ce.y = c->y;
    840 	ce.width = c->w;
    841 	ce.height = c->h;
    842 	ce.border_width = c->bw;
    843 	ce.above = None;
    844 	ce.override_redirect = False;
    845 	XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
    846 }
    847 
    848 void
    849 configurenotify(XEvent *e)
    850 {
    851 	Monitor *m;
    852 	Client *c;
    853 	XConfigureEvent *ev = &e->xconfigure;
    854 	int dirty;
    855 
    856 	/* TODO: updategeom handling sucks, needs to be simplified */
    857 	if (ev->window == root) {
    858 		dirty = (sw != ev->width || sh != ev->height);
    859 		sw = ev->width;
    860 		sh = ev->height;
    861 		if (updategeom() || dirty) {
    862 			drw_resize(drw, sw, bh);
    863 			updatebars();
    864 			for (m = mons; m; m = m->next) {
    865 				for (c = m->clients; c; c = c->next)
    866 					if (c->isfullscreen)
    867 						resizeclient(c, m->mx, m->my, m->mw, m->mh);
    868 				XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
    869 				XMoveResizeWindow(dpy, m->extrabarwin, m->wx, m->eby, m->ww, bh);
    870 			}
    871 			focus(NULL);
    872 			arrange(NULL);
    873 		}
    874 	}
    875 }
    876 
    877 void
    878 configurerequest(XEvent *e)
    879 {
    880 	Client *c, *cc = NULL;
    881 	Monitor *m;
    882 	XConfigureRequestEvent *ev = &e->xconfigurerequest;
    883 	XWindowChanges wc;
    884 
    885 	if ((c = wintoclient(ev->window))
    886 	|| (c = cc = cropwintoclient(ev->window))) {
    887 		if (ev->value_mask & CWBorderWidth)
    888 			c->bw = ev->border_width;
    889 		else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
    890 			m = c->mon;
    891 			if (c->crop)
    892 				c = c->crop;
    893 			if (ev->value_mask & CWX) {
    894 				c->oldx = c->x;
    895 				c->x = m->mx + ev->x;
    896 			}
    897 			if (ev->value_mask & CWY) {
    898 				c->oldy = c->y;
    899 				c->y = m->my + ev->y;
    900 			}
    901 			if (ev->value_mask & CWWidth) {
    902 				c->oldw = c->w;
    903 				c->w = ev->width;
    904 			}
    905 			if (ev->value_mask & CWHeight) {
    906 				c->oldh = c->h;
    907 				c->h = ev->height;
    908 			}
    909 			if ((c->x + c->w) > m->mx + m->mw && c->isfloating)
    910 				c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
    911 			if ((c->y + c->h) > m->my + m->mh && c->isfloating)
    912 				c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
    913 			if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
    914 				configure(c);
    915 			if (ISVISIBLE(c))
    916 				XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
    917 			if (cc)
    918 				cropresize(cc);
    919 		} else
    920 			configure(c);
    921 	} else {
    922 		wc.x = ev->x;
    923 		wc.y = ev->y;
    924 		wc.width = ev->width;
    925 		wc.height = ev->height;
    926 		wc.border_width = ev->border_width;
    927 		wc.sibling = ev->above;
    928 		wc.stack_mode = ev->detail;
    929 		XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
    930 	}
    931 	XSync(dpy, False);
    932 }
    933 
    934 void
    935 copyvalidchars(char *text, char *rawtext)
    936 {
    937 	int i = -1, j = 0;
    938 
    939 	while(rawtext[++i]) {
    940 		if ((unsigned char)rawtext[i] >= ' ')
    941 			text[j++] = rawtext[i];
    942 	}
    943 	text[j] = '\0';
    944 }
    945 
    946 Monitor *
    947 createmon(void)
    948 {
    949 	Monitor *m;
    950 	unsigned int i;
    951 
    952 	m = ecalloc(1, sizeof(Monitor));
    953 	m->tagset[0] = m->tagset[1] = 1;
    954 	m->attachdirection = attachdirection;
    955 	m->mfact = mfact;
    956 	m->nmaster = nmaster;
    957 	m->showbar = showbar;
    958 	m->topbar = topbar;
    959 	m->lt[0] = &layouts[0];
    960 	m->lt[1] = &layouts[1 % LENGTH(layouts)];
    961 	strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
    962 	strncpy(m->attsymbol, directions[attachdirection].symbol,
    963 			strlen(directions[attachdirection].symbol) + 1);
    964 	m->smartgaps = smartgaps;
    965 	m->smartbord = smartbord;
    966 	m->ogappx    = ogappx;
    967 	m->igappx    = igappx;
    968 
    969 	m->pertag = ecalloc(1, sizeof(Pertag));
    970 	m->pertag->curtag = m->pertag->prevtag = 1;
    971 
    972 	for (i = 0; i <= LENGTH(tags); i++) {
    973 		m->pertag->nmasters[i] = m->nmaster;
    974 		m->pertag->mfacts[i] = m->mfact;
    975 
    976 		m->pertag->ltidxs[i][0] = m->lt[0];
    977 		m->pertag->ltidxs[i][1] = m->lt[1];
    978 		m->pertag->sellts[i] = m->sellt;
    979 
    980 		m->pertag->showbars[i] = m->showbar;
    981 	}
    982 
    983 	return m;
    984 }
    985 
    986 void
    987 destroynotify(XEvent *e)
    988 {
    989 	Client *c;
    990 	XDestroyWindowEvent *ev = &e->xdestroywindow;
    991 
    992 	if ((c = wintoclient(ev->window)) || (c = cropwintoclient(ev->window)))
    993 		unmanage(c, 1);
    994 
    995 	else if ((c = swallowingclient(ev->window)))
    996 		unmanage(c->swallowing, 1);
    997 }
    998 
    999 void
   1000 detach(Client *c)
   1001 {
   1002 	Client **tc;
   1003 
   1004 	for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
   1005 	*tc = c->next;
   1006 }
   1007 
   1008 void
   1009 detachstack(Client *c)
   1010 {
   1011 	Client **tc, *t;
   1012 
   1013 	for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
   1014 	*tc = c->snext;
   1015 
   1016 	if (c == c->mon->sel) {
   1017 		for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
   1018 		c->mon->sel = t;
   1019 	}
   1020 }
   1021 
   1022 Monitor *
   1023 dirtomon(int dir)
   1024 {
   1025 	Monitor *m = NULL;
   1026 
   1027 	if (dir > 0) {
   1028 		if (!(m = selmon->next))
   1029 			m = mons;
   1030 	} else if (selmon == mons)
   1031 		for (m = mons; m->next; m = m->next);
   1032 	else
   1033 		for (m = mons; m->next != selmon; m = m->next);
   1034 	return m;
   1035 }
   1036 
   1037 void
   1038 drawbar(Monitor *m)
   1039 {
   1040 	int x, w, tw = 0;
   1041 	unsigned int i, occ = 0, urg = 0;
   1042 	char qtext[2049], *qptr, *ntext = 0, tmpch, drawn = 0;
   1043 	int xoffset = 0, qscheme = SchemeStatus, dbh = (bh - 2); /* dbh - drawable bar height */
   1044 	Client *c;
   1045 
   1046 	/* draw status first so it can be overdrawn by tags later */
   1047 	if (m == selmon) { /* status is only drawn on selected monitor */
   1048 		drw_setscheme(drw, scheme[SchemeStatus]);
   1049 		tw = TEXTW(stext) - lrpad;
   1050 		strcpy(qtext, " ");
   1051 		strcat(qtext, stext);
   1052 		qptr = qtext;
   1053 		ntext = qptr;
   1054 		--qptr;
   1055 		while (*(++qptr)) {
   1056 			if (*qptr == 127) {
   1057 				++qptr;
   1058 				if (*qptr == 127) {
   1059 					drw_setscheme(drw, scheme[qscheme]);
   1060 					tmpch = *qptr;
   1061 					*qptr = '\0';
   1062 					drw_text(drw, (m->ww - tw) + xoffset, 2, drw_fontset_getwidth(drw, (ntext)), dbh, 0, ntext, 0);
   1063 					if (qscheme == SchemeStatus)
   1064 						drw_setscheme(drw, scheme[StatusLn]);
   1065 					drw_rect(drw, (m->ww - tw) + xoffset + 1, 0, drw_fontset_getwidth(drw, (ntext)) - 2, 2, 1, 0);
   1066 					qscheme = SchemeStatus;
   1067 					drw_setscheme(drw, scheme[qscheme]);
   1068 					xoffset += drw_fontset_getwidth(drw, (ntext));
   1069 					*qptr = tmpch;
   1070 					if (tmpch)
   1071 						++qptr;
   1072 					ntext = qptr;
   1073 					drawn = 1;
   1074 				} else if (*qptr >= '0' && *qptr <= '7') {
   1075 					qscheme = StatusBlack + (*qptr - '0');
   1076 					*qptr = 127;
   1077 				}
   1078 			}
   1079 		}
   1080 		if (!drawn) {
   1081 			drw_text(drw, (m->ww - tw), 2, tw, dbh, 0, stext, 0);
   1082 			drw_rect(drw, (m->ww - tw) + 1, 0, TEXTW(ntext) - 2, 2, 1, 1);
   1083 		}
   1084 		xoffset = drawn = 0;
   1085 	}
   1086 
   1087 	for (c = m->clients; c; c = c->next) {
   1088 		occ |= c->tags;
   1089 		if (c->isurgent)
   1090 			urg |= c->tags;
   1091 	}
   1092 
   1093 	x = 0;
   1094 
   1095 	for (i = 0; i < LENGTH(tags); i++) {
   1096 		w = TEXTW(tags[i]);
   1097 		drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeTagsSel : SchemeTagsNorm]);
   1098 		drw_text(drw, x, 2, w, dbh, lrpad / 2, tags[i], urg & 1 << i);
   1099 		drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeTagLnSel : (occ & 1 << i) ? SchemeTagLnOcc : SchemeTagLnNorm]);
   1100 		drw_rect(drw, x + 1, 0, w - 2, 2,
   1101 			m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
   1102 				urg & 1 << i);
   1103 		x += w;
   1104 	}
   1105 
   1106 	/* Here you can add "modules" to
   1107 	   barwin, after ltsymbol */
   1108 
   1109 	w = blw = TEXTW(m->ltsymbol);
   1110 	drw_setscheme(drw, scheme[SchemeTagLnNorm]);
   1111 	drw_rect(drw, x + 1, 0, w - 2, 2, 1, 1);
   1112 	drw_setscheme(drw, scheme[SchemeTagsNorm]);
   1113 	x = drw_text(drw, x, 2, w, dbh, lrpad / 2, m->ltsymbol, 0);
   1114 
   1115 	w = blw = TEXTW(m->attsymbol);
   1116 	drw_setscheme(drw, scheme[SchemeTagLnNorm]);
   1117 	drw_rect(drw, x + 1, 0, w - 2, 2, 1, 1);
   1118 	drw_setscheme(drw, scheme[SchemeTagsNorm]);
   1119 	x = drw_text(drw, x, 2, w, dbh, lrpad / 2, m->attsymbol, 0);
   1120 
   1121 	if ((w = m->ww - tw - x) > bh) {
   1122 		if (m->sel) {
   1123             /* fix overflow when window name is bigger than window width */
   1124 			int mid;
   1125 			if (centerwindowname) {
   1126 				mid = (m->ww - (int)TEXTW(m->sel->name)) / 2 - x;
   1127 				/* make sure name will not overlap on tags even when it is very long */
   1128 				mid = mid >= lrpad / 2 ? mid : lrpad / 2;
   1129 			}
   1130 			drw_setscheme(drw, scheme[m == selmon ? SchemeInfoSel : SchemeInfoNorm]);
   1131 			drw_text(drw, x, 2, w, dbh, centerwindowname ? mid : (lrpad / 2), m->sel->name, 0);
   1132 			drw_setscheme(drw, scheme[m->sel->isfloating ? SchemeTagLnSel : SchemeTagLnNorm]);
   1133 			drw_rect(drw, x + 1, 0, w - 2, 2, 1, 1);
   1134 		} else {
   1135 			drw_setscheme(drw, scheme[SchemeInfoNorm]);
   1136 			drw_rect(drw, x, 0, w, bh, 1, 1);
   1137 		}
   1138 	}
   1139 
   1140 	drw_map(drw, m->barwin, 0, 0, m->ww, bh);
   1141 
   1142 	drw_setscheme(drw, scheme[SchemeNorm]);
   1143 	drw_rect(drw, 0, 0, m->ww, bh, 1, 1);
   1144 	drw_text(drw, 0, 0, mons->ww, bh, 0, "", 0);
   1145 	if (m == selmon) { /* extra status is only drawn on selected monitor */
   1146 		drw_setscheme(drw, scheme[SchemeNorm]);
   1147 		strcpy(qtext, estext);
   1148 		qptr = qtext;
   1149 		ntext = qptr;
   1150 		--qptr;
   1151 		while (*(++qptr) != 127)
   1152 			if (!(*qptr)) {
   1153 				--qptr;
   1154 				break;
   1155 			}
   1156 		while (*(++qptr)) {
   1157 			if (*qptr == 127) {
   1158 				++qptr;
   1159 				if (*qptr == 127) {
   1160 					drw_setscheme(drw, scheme[qscheme]);
   1161 					tmpch = *qptr;
   1162 					*qptr = '\0';
   1163 					drw_text(drw, xoffset, 0, mons->ww - xoffset, bh, 0, ntext, 0);
   1164 					if (qscheme == SchemeStatus)
   1165 						drw_setscheme(drw, scheme[StatusLn]);
   1166 					drw_rect(drw, xoffset + 1, dbh, drw_fontset_getwidth(drw, (ntext)) - 2, 2, 1, 0);
   1167 					qscheme = SchemeStatus;
   1168 					drw_setscheme(drw, scheme[qscheme]);
   1169 					xoffset += drw_fontset_getwidth(drw, (ntext));
   1170 					*qptr = tmpch;
   1171 					if (tmpch)
   1172 						++qptr;
   1173 					ntext = qptr;
   1174 					drawn = 1;
   1175 				} else if (*qptr >= '0' && *qptr <= '7') {
   1176 					qscheme = StatusBlack + (*qptr - '0');
   1177 					*qptr = 127;
   1178 				}
   1179 			}
   1180 		}
   1181 		if (!drawn) {
   1182 			drw_text(drw, 0, 0, tw, bh, 0, estext, 0);
   1183 			drw_rect(drw, 1, 0, TEXTW(ntext) - 2, 2, 1, 1);
   1184 		}
   1185 		xoffset = drawn = 0;
   1186 
   1187 	} else {
   1188 		drw_setscheme(drw, scheme[SchemeNorm]);
   1189 		drw_text(drw, 0, 2, mons->ww, dbh, 0, "", 0);
   1190 	}
   1191 	drw_map(drw, m->extrabarwin, 0, 0, m->ww, bh);
   1192 	drw_rect(drw, 0, 0, mons->ww, bh, 1, 1);
   1193 }
   1194 
   1195 void
   1196 drawbars(void)
   1197 {
   1198 	Monitor *m;
   1199 
   1200 	for (m = mons; m; m = m->next)
   1201 		drawbar(m);
   1202 }
   1203 
   1204 void
   1205 enternotify(XEvent *e)
   1206 {
   1207 	Client *c;
   1208 	Monitor *m;
   1209 	XCrossingEvent *ev = &e->xcrossing;
   1210 
   1211 	if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
   1212 		return;
   1213 	c = wintoclient(ev->window);
   1214 	if (!c)
   1215 		c = cropwintoclient(ev->window);
   1216 	m = c ? c->mon : wintomon(ev->window);
   1217 	if (m != selmon) {
   1218 		unfocus(selmon->sel, 1);
   1219 		selmon = m;
   1220 	} else if (!c || c == selmon->sel)
   1221 		return;
   1222 	focus(c);
   1223 }
   1224 
   1225 void
   1226 expose(XEvent *e)
   1227 {
   1228 	Monitor *m;
   1229 	XExposeEvent *ev = &e->xexpose;
   1230 
   1231 	if (ev->count == 0 && (m = wintomon(ev->window)))
   1232 		drawbar(m);
   1233 }
   1234 
   1235 void
   1236 focus(Client *c)
   1237 {
   1238 	if (!c || !ISVISIBLE(c))
   1239 		for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
   1240 	if (selmon->sel && selmon->sel != c)
   1241 		unfocus(selmon->sel, 0);
   1242 	if (c) {
   1243 		if (c->mon != selmon)
   1244 			selmon = c->mon;
   1245 		if (c->isurgent)
   1246 			seturgent(c, 0);
   1247 		detachstack(c);
   1248 		attachstack(c);
   1249 		grabbuttons(c, 1);
   1250 		XSetWindowBorder(dpy, c->win, scheme[c->isterminal && selmon->smartbord ? SchemeTermSel : SchemeSel][ColBorder].pixel);
   1251 		setfocus(c);
   1252 	} else {
   1253 		XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
   1254 		XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
   1255 	}
   1256 	selmon->sel = c;
   1257 	drawbars();
   1258 }
   1259 
   1260 /* there are some broken focus acquiring clients needing extra handling */
   1261 void
   1262 focusin(XEvent *e)
   1263 {
   1264 	XFocusChangeEvent *ev = &e->xfocus;
   1265 
   1266 	if (selmon->sel && ev->window != selmon->sel->win)
   1267 		setfocus(selmon->sel);
   1268 }
   1269 
   1270 void
   1271 focusmon(const Arg *arg)
   1272 {
   1273 	Monitor *m;
   1274 
   1275 	if (!mons->next)
   1276 		return;
   1277 	if ((m = dirtomon(arg->i)) == selmon)
   1278 		return;
   1279 	unfocus(selmon->sel, 0);
   1280 	selmon = m;
   1281 	focus(NULL);
   1282 }
   1283 
   1284 void
   1285 focusstack(const Arg *arg)
   1286 {
   1287 	Client *c = NULL, *i;
   1288 
   1289 	if (!selmon->sel)
   1290 		return;
   1291 	if (arg->i > 0) {
   1292 		for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
   1293 		if (!c)
   1294 			for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
   1295 	} else {
   1296 		for (i = selmon->clients; i != selmon->sel; i = i->next)
   1297 			if (ISVISIBLE(i))
   1298 				c = i;
   1299 		if (!c)
   1300 			for (; i; i = i->next)
   1301 				if (ISVISIBLE(i))
   1302 					c = i;
   1303 	}
   1304 	if (c) {
   1305 		focus(c);
   1306 		restack(selmon);
   1307 	}
   1308 }
   1309 
   1310 Atom
   1311 getatomprop(Client *c, Atom prop)
   1312 {
   1313 	int di;
   1314 	unsigned long dl;
   1315 	unsigned char *p = NULL;
   1316 	Atom da, atom = None;
   1317 
   1318 	if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
   1319 		&da, &di, &dl, &dl, &p) == Success && p) {
   1320 		atom = *(Atom *)p;
   1321 		XFree(p);
   1322 	}
   1323 	return atom;
   1324 }
   1325 
   1326 int
   1327 getdwmblockspid()
   1328 {
   1329 	char buf[16];
   1330 	FILE *fp = popen("pidof -s dwmblocks", "r");
   1331 	fgets(buf, sizeof(buf), fp);
   1332 	pid_t pid = strtoul(buf, NULL, 10);
   1333 	pclose(fp);
   1334 	dwmblockspid = pid;
   1335 	return pid != 0 ? 0 : -1;
   1336 }
   1337 
   1338 int
   1339 getrootptr(int *x, int *y)
   1340 {
   1341 	int di;
   1342 	unsigned int dui;
   1343 	Window dummy;
   1344 
   1345 	return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
   1346 }
   1347 
   1348 long
   1349 getstate(Window w)
   1350 {
   1351 	int format;
   1352 	long result = -1;
   1353 	unsigned char *p = NULL;
   1354 	unsigned long n, extra;
   1355 	Atom real;
   1356 
   1357 	if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
   1358 		&real, &format, &n, &extra, (unsigned char **)&p) != Success)
   1359 		return -1;
   1360 	if (n != 0)
   1361 		result = *p;
   1362 	XFree(p);
   1363 	return result;
   1364 }
   1365 
   1366 int
   1367 gettextprop(Window w, Atom atom, char *text, unsigned int size)
   1368 {
   1369 	char **list = NULL;
   1370 	int n;
   1371 	XTextProperty name;
   1372 
   1373 	if (!text || size == 0)
   1374 		return 0;
   1375 	text[0] = '\0';
   1376 	if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems)
   1377 		return 0;
   1378 	if (name.encoding == XA_STRING)
   1379 		strncpy(text, (char *)name.value, size - 1);
   1380 	else {
   1381 		if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
   1382 			strncpy(text, *list, size - 1);
   1383 			XFreeStringList(list);
   1384 		}
   1385 	}
   1386 	text[size - 1] = '\0';
   1387 	XFree(name.value);
   1388 	return 1;
   1389 }
   1390 
   1391 void
   1392 grabbuttons(Client *c, int focused)
   1393 {
   1394 	updatenumlockmask();
   1395 	{
   1396 		unsigned int i, j;
   1397 		unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
   1398 		XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
   1399 		if (!focused)
   1400 			XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
   1401 				BUTTONMASK, GrabModeSync, GrabModeSync, None, None);
   1402 		for (i = 0; i < LENGTH(buttons); i++)
   1403 			if (buttons[i].click == ClkClientWin)
   1404 				for (j = 0; j < LENGTH(modifiers); j++)
   1405 					XGrabButton(dpy, buttons[i].button,
   1406 						buttons[i].mask | modifiers[j],
   1407 						c->win, False, BUTTONMASK,
   1408 						GrabModeAsync, GrabModeSync, None, None);
   1409 	}
   1410 }
   1411 
   1412 void
   1413 grabkeys(void)
   1414 {
   1415 	updatenumlockmask();
   1416 	{
   1417 		unsigned int i, j;
   1418 		unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
   1419 		KeyCode code;
   1420 
   1421 		XUngrabKey(dpy, AnyKey, AnyModifier, root);
   1422 		for (i = 0; i < LENGTH(keys); i++)
   1423 			if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
   1424 				for (j = 0; j < LENGTH(modifiers); j++)
   1425 					XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
   1426 						True, GrabModeAsync, GrabModeAsync);
   1427 	}
   1428 }
   1429 
   1430 void
   1431 incnmaster(const Arg *arg)
   1432 {
   1433 	selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag] = MAX(selmon->nmaster + arg->i, 0);
   1434 	arrange(selmon);
   1435 }
   1436 
   1437 #ifdef XINERAMA
   1438 static int
   1439 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
   1440 {
   1441 	while (n--)
   1442 		if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
   1443 		&& unique[n].width == info->width && unique[n].height == info->height)
   1444 			return 0;
   1445 	return 1;
   1446 }
   1447 #endif /* XINERAMA */
   1448 
   1449 void
   1450 keypress(XEvent *e)
   1451 {
   1452 	unsigned int i;
   1453 	KeySym keysym;
   1454 	XKeyEvent *ev;
   1455 
   1456 	ev = &e->xkey;
   1457 	keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
   1458 	for (i = 0; i < LENGTH(keys); i++)
   1459 		if (keysym == keys[i].keysym
   1460 		&& CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
   1461 		&& keys[i].func)
   1462 			keys[i].func(&(keys[i].arg));
   1463 }
   1464 
   1465 void
   1466 killclient(const Arg *arg)
   1467 {
   1468 	if (!selmon->sel)
   1469 		return;
   1470 	if (selmon->sel->crop)
   1471 		cropdelete(selmon->sel);
   1472 	if (!sendevent(selmon->sel, wmatom[WMDelete])) {
   1473 		XGrabServer(dpy);
   1474 		XSetErrorHandler(xerrordummy);
   1475 		XSetCloseDownMode(dpy, DestroyAll);
   1476 		XKillClient(dpy, selmon->sel->win);
   1477 		XSync(dpy, False);
   1478 		XSetErrorHandler(xerror);
   1479 		XUngrabServer(dpy);
   1480 	}
   1481 }
   1482 
   1483 void
   1484 manage(Window w, XWindowAttributes *wa)
   1485 {
   1486 	Client *c, *t = NULL, *term = NULL;
   1487 	Window trans = None;
   1488 	XWindowChanges wc;
   1489 
   1490 	c = ecalloc(1, sizeof(Client));
   1491 	c->win = w;
   1492 	c->pid = winpid(w);
   1493 	/* geometry */
   1494 	c->x = c->oldx = wa->x;
   1495 	c->y = c->oldy = wa->y;
   1496 	c->w = c->oldw = wa->width;
   1497 	c->h = c->oldh = wa->height;
   1498 	c->oldbw = wa->border_width;
   1499 	c->cfact = 1.0;
   1500 
   1501 	updatetitle(c);
   1502 	if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
   1503 		c->mon = t->mon;
   1504 		c->tags = t->tags;
   1505 	} else {
   1506 		c->mon = selmon;
   1507 		applyrules(c);
   1508 		term = termforwin(c);
   1509 	}
   1510 
   1511 	if (c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
   1512 		c->x = c->mon->mx + c->mon->mw - WIDTH(c);
   1513 	if (c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
   1514 		c->y = c->mon->my + c->mon->mh - HEIGHT(c);
   1515 	c->x = MAX(c->x, c->mon->mx);
   1516 	/* only fix client y-offset, if the client center might cover the bar */
   1517 	c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
   1518 		&& (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
   1519 	c->bw = borderpx;
   1520 
   1521 	wc.border_width = c->bw;
   1522 	XConfigureWindow(dpy, w, CWBorderWidth, &wc);
   1523 	XSetWindowBorder(dpy, w, scheme[c->isterminal && selmon->smartbord ? SchemeTermNorm : SchemeNorm][ColBorder].pixel);
   1524 	configure(c); /* propagates border_width, if size doesn't change */
   1525 	updatewindowtype(c);
   1526 	updatesizehints(c);
   1527 	updatewmhints(c);
   1528 	c->x = c->mon->mx + (c->mon->mw - WIDTH(c)) / 2;
   1529 	c->y = c->mon->my + (c->mon->mh - HEIGHT(c)) / 2;
   1530 	XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
   1531 	grabbuttons(c, 0);
   1532 	if (!c->isfloating)
   1533 		c->isfloating = c->oldstate = trans != None || c->isfixed;
   1534 	if (c->isfloating)
   1535 		XRaiseWindow(dpy, c->win);
   1536 	directions[selmon->attachdirection].direction(c);
   1537 	/*
   1538 	switch (selmon->attachdirection){
   1539 		case 1:
   1540 			attachabove(c);
   1541 			break;
   1542 		case 2:
   1543 			attachaside(c);
   1544 			break;
   1545 		case 3:
   1546 			attachbelow(c);
   1547 			break;
   1548 		case 4:
   1549 			attachbottom(c);
   1550 			break;
   1551 		case 5:
   1552 			attachtop(c);
   1553 			break;
   1554 		default:
   1555 			attach(c);
   1556 	}
   1557 	*/
   1558 	attachstack(c);
   1559 	XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
   1560 		(unsigned char *) &(c->win), 1);
   1561 	XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
   1562 	setclientstate(c, NormalState);
   1563 	if (c->mon == selmon)
   1564 		unfocus(selmon->sel, 0);
   1565 	c->mon->sel = c;
   1566 	arrange(c->mon);
   1567 	XMapWindow(dpy, c->win);
   1568 	if (term)
   1569 		swallow(term, c);
   1570 	focus(NULL);
   1571 }
   1572 
   1573 void
   1574 mappingnotify(XEvent *e)
   1575 {
   1576 	XMappingEvent *ev = &e->xmapping;
   1577 
   1578 	XRefreshKeyboardMapping(ev);
   1579 	if (ev->request == MappingKeyboard)
   1580 		grabkeys();
   1581 }
   1582 
   1583 void
   1584 maprequest(XEvent *e)
   1585 {
   1586 	static XWindowAttributes wa;
   1587 	XMapRequestEvent *ev = &e->xmaprequest;
   1588 
   1589 	if (!XGetWindowAttributes(dpy, ev->window, &wa))
   1590 		return;
   1591 	if (wa.override_redirect)
   1592 		return;
   1593 	if (!wintoclient(ev->window))
   1594 		manage(ev->window, &wa);
   1595 }
   1596 
   1597 void
   1598 monocle(Monitor *m)
   1599 {
   1600 	unsigned int n = 0;
   1601 	Client *c;
   1602 
   1603 	for (c = m->clients; c; c = c->next)
   1604 		if (ISVISIBLE(c))
   1605 			n++;
   1606 	if (n > 0) /* override layout symbol */
   1607 		snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
   1608 	for (c = nexttiled(m->clients); c; c = nexttiled(c->next))
   1609 		resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
   1610 }
   1611 
   1612 void
   1613 motionnotify(XEvent *e)
   1614 {
   1615 	static Monitor *mon = NULL;
   1616 	Monitor *m;
   1617 	XMotionEvent *ev = &e->xmotion;
   1618 
   1619 	if (ev->window != root)
   1620 		return;
   1621 	if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
   1622 		unfocus(selmon->sel, 1);
   1623 		selmon = m;
   1624 		focus(NULL);
   1625 	}
   1626 	mon = m;
   1627 }
   1628 
   1629 void
   1630 movemouse(const Arg *arg)
   1631 {
   1632 	int x, y, ocx, ocy, nx, ny;
   1633 	Client *c;
   1634 	Monitor *m;
   1635 	XEvent ev;
   1636 	Time lasttime = 0;
   1637 
   1638 	if (!(c = selmon->sel))
   1639 		return;
   1640 	if (c->isfullscreen) /* no support moving fullscreen windows by mouse */
   1641 		return;
   1642 	restack(selmon);
   1643 	ocx = c->x;
   1644 	ocy = c->y;
   1645 	if (arg->i == 1 && c->crop) {
   1646 		ocx = c->crop->x;
   1647 		ocy = c->crop->y;
   1648 	}
   1649 	if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
   1650 		None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
   1651 		return;
   1652 	if (!getrootptr(&x, &y))
   1653 		return;
   1654 	do {
   1655 		XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
   1656 		switch(ev.type) {
   1657 		case ConfigureRequest:
   1658 		case Expose:
   1659 		case MapRequest:
   1660 			handler[ev.type](&ev);
   1661 			break;
   1662 		case MotionNotify:
   1663 			if ((ev.xmotion.time - lasttime) <= (1000 / 60))
   1664 				continue;
   1665 			lasttime = ev.xmotion.time;
   1666 
   1667 			nx = ocx + (ev.xmotion.x - x);
   1668 			ny = ocy + (ev.xmotion.y - y);
   1669 			if (arg->i == 1 && c->crop) {
   1670 				c->crop->x = nx;
   1671 				c->crop->y = ny;
   1672 				cropresize(c);
   1673 				continue;
   1674 			}
   1675 			if (abs(selmon->wx - nx) < snap)
   1676 				nx = selmon->wx;
   1677 			else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
   1678 				nx = selmon->wx + selmon->ww - WIDTH(c);
   1679 			if (abs(selmon->wy - ny) < snap)
   1680 				ny = selmon->wy;
   1681 			else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
   1682 				ny = selmon->wy + selmon->wh - HEIGHT(c);
   1683 			if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
   1684 			&& (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
   1685 				togglefloating(NULL);
   1686 			if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
   1687 				resize(c, nx, ny, c->w, c->h, 1);
   1688 			break;
   1689 		}
   1690 	} while (ev.type != ButtonRelease);
   1691 	XUngrabPointer(dpy, CurrentTime);
   1692 	if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
   1693 		sendmon(c, m);
   1694 		selmon = m;
   1695 		focus(NULL);
   1696 	}
   1697 }
   1698 
   1699 Client *
   1700 nexttagged(Client *c) {
   1701 	Client *walked = c->mon->clients;
   1702 	for(;
   1703 		walked && (walked->isfloating || !ISVISIBLEONTAG(walked, c->tags));
   1704 		walked = walked->next
   1705 	);
   1706 	return walked;
   1707 }
   1708 
   1709 void
   1710 moveresize(const Arg *arg) {
   1711 	/* only floating windows can be moved */
   1712 	Client *c;
   1713 	c = selmon->sel;
   1714 	int x, y, w, h, nx, ny, nw, nh, ox, oy, ow, oh;
   1715 	char xAbs, yAbs, wAbs, hAbs;
   1716 	int msx, msy, dx, dy, nmx, nmy;
   1717 	unsigned int dui;
   1718 	Window dummy;
   1719 
   1720 	if (!c || !arg)
   1721 		return;
   1722 	if (selmon->lt[selmon->sellt]->arrange && !c->isfloating)
   1723 		return;
   1724 	if (sscanf((char *)arg->v, "%d%c %d%c %d%c %d%c", &x, &xAbs, &y, &yAbs, &w, &wAbs, &h, &hAbs) != 8)
   1725 		return;
   1726 
   1727 	/* compute new window position; prevent window from be positioned outside the current monitor */
   1728 	nw = c->w + w;
   1729 	if (wAbs == 'W')
   1730 		nw = w < selmon->mw - 2 * c->bw ? w : selmon->mw - 2 * c->bw;
   1731 
   1732 	nh = c->h + h;
   1733 	if (hAbs == 'H')
   1734 		nh = h < selmon->mh - 2 * c->bw ? h : selmon->mh - 2 * c->bw;
   1735 
   1736 	nx = c->x + x;
   1737 	if (xAbs == 'X') {
   1738 		if (x < selmon->mx)
   1739 			nx = selmon->mx;
   1740 		else if (x > selmon->mx + selmon->mw)
   1741 			nx = selmon->mx + selmon->mw - nw - 2 * c->bw;
   1742 		else
   1743 			nx = x;
   1744 	}
   1745 
   1746 	ny = c->y + y;
   1747 	if (yAbs == 'Y') {
   1748 		if (y < selmon->my)
   1749 			ny = selmon->my;
   1750 		else if (y > selmon->my + selmon->mh)
   1751 			ny = selmon->my + selmon->mh - nh - 2 * c->bw;
   1752 		else
   1753 			ny = y;
   1754 	}
   1755 
   1756 	ox = c->x;
   1757 	oy = c->y;
   1758 	ow = c->w;
   1759 	oh = c->h;
   1760 
   1761 	XRaiseWindow(dpy, c->win);
   1762 	Bool xqp = XQueryPointer(dpy, root, &dummy, &dummy, &msx, &msy, &dx, &dy, &dui);
   1763 	resize(c, nx, ny, nw, nh, True);
   1764 
   1765 	/* move cursor along with the window to avoid problems caused by the sloppy focus */
   1766 	if (xqp && ox <= msx && (ox + ow) >= msx && oy <= msy && (oy + oh) >= msy)
   1767 	{
   1768 		nmx = c->x - ox + c->w - ow;
   1769 		nmy = c->y - oy + c->h - oh;
   1770 		XWarpPointer(dpy, None, None, 0, 0, 0, 0, nmx, nmy);
   1771 	}
   1772 }
   1773 
   1774 void
   1775 moveresizeedge(const Arg *arg) {
   1776 	/* move or resize floating window to edge of screen */
   1777 	Client *c;
   1778 	c = selmon->sel;
   1779 	char e;
   1780 	int nx, ny, nw, nh, ox, oy, ow, oh, bp;
   1781 	int msx, msy, dx, dy, nmx, nmy;
   1782 	int starty;
   1783 	unsigned int dui;
   1784 	Window dummy;
   1785 
   1786 	nx = c->x;
   1787 	ny = c->y;
   1788 	nw = c->w;
   1789 	nh = c->h;
   1790 
   1791 	starty = selmon->showbar && topbar ? bh : 0;
   1792 	bp = selmon->showbar && !topbar ? bh : 0;
   1793 
   1794 	if (!c || !arg)
   1795 		return;
   1796 	if (selmon->lt[selmon->sellt]->arrange && !c->isfloating)
   1797 		return;
   1798 	if(sscanf((char *)arg->v, "%c", &e) != 1)
   1799 		return;
   1800 
   1801 	if(e == 't')
   1802 		ny = starty;
   1803 
   1804 	if(e == 'b')
   1805 		ny = c->h > selmon->mh - 2 * c->bw ? c->h - bp : selmon->mh - c->h - 2 * c->bw - bp;
   1806 
   1807 	if(e == 'l')
   1808 		nx = selmon->mx;
   1809 
   1810 	if(e == 'r')
   1811 		nx = c->w > selmon->mw - 2 * c->bw ? selmon->mx + c->w : selmon->mx + selmon->mw - c->w - 2 * c->bw;
   1812 
   1813 	if(e == 'T') {
   1814 		/* if you click to resize again, it will return to old size/position */
   1815 		if(c->h + starty == c->oldh + c->oldy) {
   1816 			nh = c->oldh;
   1817 			ny = c->oldy;
   1818 		} else {
   1819 			nh = c->h + c->y - starty;
   1820 			ny = starty;
   1821 		}
   1822 	}
   1823 
   1824 	if(e == 'B')
   1825 		nh = c->h + c->y + 2 * c->bw + bp == selmon->mh ? c->oldh : selmon->mh - c->y - 2 * c->bw - bp;
   1826 
   1827 	if(e == 'L') {
   1828 		if(selmon->mx + c->w == c->oldw + c->oldx) {
   1829 			nw = c->oldw;
   1830 			nx = c->oldx;
   1831 		} else {
   1832 			nw = c->w + c->x - selmon->mx;
   1833 			nx = selmon->mx;
   1834 		}
   1835 	}
   1836 
   1837 	if(e == 'R')
   1838 		nw = c->w + c->x + 2 * c->bw == selmon->mx + selmon->mw ? c->oldw : selmon->mx + selmon->mw - c->x - 2 * c->bw;
   1839 
   1840 	ox = c->x;
   1841 	oy = c->y;
   1842 	ow = c->w;
   1843 	oh = c->h;
   1844 
   1845 	XRaiseWindow(dpy, c->win);
   1846 	Bool xqp = XQueryPointer(dpy, root, &dummy, &dummy, &msx, &msy, &dx, &dy, &dui);
   1847 	resize(c, nx, ny, nw, nh, True);
   1848 
   1849 	/* move cursor along with the window to avoid problems caused by the sloppy focus */
   1850 	if (xqp && ox <= msx && (ox + ow) >= msx && oy <= msy && (oy + oh) >= msy) {
   1851 		nmx = c->x - ox + c->w - ow;
   1852 		nmy = c->y - oy + c->h - oh;
   1853 		XWarpPointer(dpy, None, None, 0, 0, 0, 0, nmx, nmy);
   1854 	}
   1855 }
   1856 
   1857 Client *
   1858 nexttiled(Client *c)
   1859 {
   1860 	for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
   1861 	return c;
   1862 }
   1863 
   1864 void
   1865 pop(Client *c)
   1866 {
   1867 	detach(c);
   1868 	attach(c);
   1869 	focus(c);
   1870 	arrange(c->mon);
   1871 }
   1872 
   1873 void
   1874 propertynotify(XEvent *e)
   1875 {
   1876 	Client *c;
   1877 	Window trans;
   1878 	XPropertyEvent *ev = &e->xproperty;
   1879 
   1880 	if ((ev->window == root) && (ev->atom == XA_WM_NAME))
   1881 		updatestatus();
   1882 	else if (ev->state == PropertyDelete)
   1883 		return; /* ignore */
   1884 	else if ((c = wintoclient(ev->window))
   1885 	|| (c = cropwintoclient(ev->window))) {
   1886 		if (c->crop)
   1887 			c = c->crop;
   1888 		switch(ev->atom) {
   1889 		default: break;
   1890 		case XA_WM_TRANSIENT_FOR:
   1891 			if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
   1892 				(c->isfloating = (wintoclient(trans)) != NULL))
   1893 				arrange(c->mon);
   1894 			break;
   1895 		case XA_WM_NORMAL_HINTS:
   1896 			updatesizehints(c);
   1897 			break;
   1898 		case XA_WM_HINTS:
   1899 			updatewmhints(c);
   1900 			drawbars();
   1901 			break;
   1902 		}
   1903 		if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
   1904 			updatetitle(c);
   1905 			if (c == c->mon->sel)
   1906 				drawbar(c->mon);
   1907 		}
   1908 		if (ev->atom == netatom[NetWMWindowType])
   1909 			updatewindowtype(c);
   1910 	}
   1911 }
   1912 
   1913 void
   1914 quit(const Arg *arg)
   1915 {
   1916 	if(arg->i) restart = 1;
   1917 	running = 0;
   1918 }
   1919 
   1920 Monitor *
   1921 recttomon(int x, int y, int w, int h)
   1922 {
   1923 	Monitor *m, *r = selmon;
   1924 	int a, area = 0;
   1925 
   1926 	for (m = mons; m; m = m->next)
   1927 		if ((a = INTERSECT(x, y, w, h, m)) > area) {
   1928 			area = a;
   1929 			r = m;
   1930 		}
   1931 	return r;
   1932 }
   1933 
   1934 void
   1935 resetlayout(const Arg *arg)
   1936 {
   1937 	Arg default_layout = {.v = &layouts[0]};
   1938 	Arg default_mfact = {.f = mfact + 1};
   1939 	/* Arg default_attachment = {.i = -1}; */
   1940 
   1941 	setlayout(&default_layout);
   1942 	setmfact(&default_mfact);
   1943 	/* setattach(&default_attachment); */
   1944 }
   1945 
   1946 void
   1947 reorganizetags(const Arg *arg)
   1948 {
   1949 	Client *c;
   1950 	unsigned int occ, unocc, i;
   1951 	unsigned int tagdest[LENGTH(tags)];
   1952 
   1953 	occ = 0;
   1954 	for (c = selmon->clients; c; c = c->next)
   1955 		occ |= (1 << (ffs(c->tags)-1));
   1956 	unocc = 0;
   1957 	for (i = 0; i < LENGTH(tags); ++i) {
   1958 		while (unocc < i && (occ & (1 << unocc)))
   1959 			unocc++;
   1960 		if (occ & (1 << i)) {
   1961 			tagdest[i] = unocc;
   1962 			occ &= ~(1 << i);
   1963 			occ |= 1 << unocc;
   1964 		}
   1965 	}
   1966 
   1967 	for (c = selmon->clients; c; c = c->next)
   1968 		c->tags = 1 << tagdest[ffs(c->tags)-1];
   1969 	if (selmon->sel)
   1970 		selmon->tagset[selmon->seltags] = selmon->sel->tags;
   1971 	arrange(selmon);
   1972 }
   1973 
   1974 void
   1975 resize(Client *c, int x, int y, int w, int h, int interact)
   1976 {
   1977 	if (applysizehints(c, &x, &y, &w, &h, interact))
   1978 		resizeclient(c, x, y, w, h);
   1979 }
   1980 
   1981 void
   1982 resizeclient(Client *c, int x, int y, int w, int h)
   1983 {
   1984 	XWindowChanges wc;
   1985 
   1986 	c->oldx = c->x; c->x = wc.x = x;
   1987 	c->oldy = c->y; c->y = wc.y = y;
   1988 	c->oldw = c->w; c->w = wc.width = w;
   1989 	c->oldh = c->h; c->h = wc.height = h;
   1990 	wc.border_width = c->bw;
   1991 
   1992 	if ((nexttiled(c->mon->clients) == c) && !(nexttiled(c->next)))
   1993 		resetlayout(NULL);
   1994 
   1995 	/* if (((nexttiled(c->mon->clients) == c && !nexttiled(c->next)) */
   1996 	/*     || &monocle == c->mon->lt[c->mon->sellt]->arrange) */
   1997 	/*     && !c->isfullscreen && !c->isfloating */
   1998 	/*     && NULL != c->mon->lt[c->mon->sellt]->arrange) { */
   1999 	/* 	c->w = wc.width += c->bw * 2; */
   2000 	/* 	c->h = wc.height += c->bw * 2; */
   2001 	/* 	wc.border_width = 0; */
   2002 	/* } */
   2003 
   2004 	if ((((nexttiled(c->mon->clients) == c && !nexttiled(c->next))
   2005 	    || &monocle == c->mon->lt[c->mon->sellt]->arrange)
   2006 	    && !c->isfullscreen && !c->isfloating
   2007 	    && NULL != c->mon->lt[c->mon->sellt]->arrange) || c->isfloating) {
   2008 		if (c->isfloating) {
   2009 			c->w = wc.width;
   2010 			c->h = wc.height;
   2011 		} else {
   2012 			c->w = wc.width += c->bw * 2;
   2013 			c->h = wc.height += c->bw * 2;
   2014 		}
   2015 		wc.border_width = 0;
   2016 	}
   2017 
   2018 	XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
   2019 	configure(c);
   2020 	XSync(dpy, False);
   2021 }
   2022 
   2023 void
   2024 resizemouse(const Arg *arg)
   2025 {
   2026 	int ocx, ocy, nw, nh;
   2027 	Client *c;
   2028 	Monitor *m;
   2029 	XEvent ev;
   2030 	Time lasttime = 0;
   2031 
   2032 	if (!(c = selmon->sel))
   2033 		return;
   2034 	if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
   2035 		return;
   2036 	restack(selmon);
   2037 	if (arg->i == 1)
   2038 		cropwindow(c);
   2039 	ocx = c->x;
   2040 	ocy = c->y;
   2041 	if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
   2042 		None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
   2043 		return;
   2044 	if (arg->i != 1)
   2045 		XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
   2046 			c->w + c->bw - 1, c->h + c->bw - 1);
   2047 	do {
   2048 		XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
   2049 		switch(ev.type) {
   2050 		case ConfigureRequest:
   2051 		case Expose:
   2052 		case MapRequest:
   2053 			handler[ev.type](&ev);
   2054 			break;
   2055 		case MotionNotify:
   2056 			if ((ev.xmotion.time - lasttime) <= (1000 / 60))
   2057 				continue;
   2058 			lasttime = ev.xmotion.time;
   2059 
   2060 			nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
   2061 			nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
   2062 			if (c->crop) {
   2063 				nw = MIN(nw, c->crop->w + c->crop->x);
   2064 				nh = MIN(nh, c->crop->h + c->crop->y);
   2065 			}
   2066 			if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
   2067 			&& c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
   2068 			{
   2069 				if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
   2070 				&& (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
   2071 					togglefloating(NULL);
   2072 			}
   2073 			if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
   2074 				resize(c, c->x, c->y, nw, nh, 1);
   2075 			break;
   2076 		}
   2077 	} while (ev.type != ButtonRelease);
   2078 	XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
   2079 	XUngrabPointer(dpy, CurrentTime);
   2080 	while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
   2081 	if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
   2082 		sendmon(c, m);
   2083 		selmon = m;
   2084 		focus(NULL);
   2085 	}
   2086 }
   2087 
   2088 void
   2089 restack(Monitor *m)
   2090 {
   2091 	Client *c;
   2092 	XEvent ev;
   2093 	XWindowChanges wc;
   2094 
   2095 	drawbar(m);
   2096 	if (!m->sel)
   2097 		return;
   2098 	if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
   2099 		XRaiseWindow(dpy, m->sel->win);
   2100 	if (m->lt[m->sellt]->arrange) {
   2101 		wc.stack_mode = Below;
   2102 		wc.sibling = m->barwin;
   2103 		for (c = m->stack; c; c = c->snext)
   2104 			if (!c->isfloating && ISVISIBLE(c)) {
   2105 				XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
   2106 				wc.sibling = c->win;
   2107 			}
   2108 	}
   2109 	XSync(dpy, False);
   2110 	while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
   2111 }
   2112 
   2113 void
   2114 run(void)
   2115 {
   2116 	XEvent ev;
   2117 	/* main event loop */
   2118 	XSync(dpy, False);
   2119 	while (running && !XNextEvent(dpy, &ev))
   2120 		if (handler[ev.type])
   2121 			handler[ev.type](&ev); /* call handler */
   2122 }
   2123 
   2124 void
   2125 scan(void)
   2126 {
   2127 	unsigned int i, num;
   2128 	Window d1, d2, *wins = NULL;
   2129 	XWindowAttributes wa;
   2130 
   2131 	if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
   2132 		for (i = 0; i < num; i++) {
   2133 			if (!XGetWindowAttributes(dpy, wins[i], &wa)
   2134 			|| wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
   2135 				continue;
   2136 			if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
   2137 				manage(wins[i], &wa);
   2138 		}
   2139 		for (i = 0; i < num; i++) { /* now the transients */
   2140 			if (!XGetWindowAttributes(dpy, wins[i], &wa))
   2141 				continue;
   2142 			if (XGetTransientForHint(dpy, wins[i], &d1)
   2143 			&& (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
   2144 				manage(wins[i], &wa);
   2145 		}
   2146 		if (wins)
   2147 			XFree(wins);
   2148 	}
   2149 }
   2150 
   2151 void
   2152 sendmon(Client *c, Monitor *m)
   2153 {
   2154 	if (c->mon == m)
   2155 		return;
   2156 	unfocus(c, 1);
   2157 	detach(c);
   2158 	detachstack(c);
   2159 	c->mon = m;
   2160 	c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
   2161 	switch(attachdirection){
   2162 		case 1:
   2163 			attachabove(c);
   2164 			break;
   2165 		case 2:
   2166 			attachaside(c);
   2167 			break;
   2168 		case 3:
   2169 			attachbelow(c);
   2170 			break;
   2171 		case 4:
   2172 			attachbottom(c);
   2173 			break;
   2174 		case 5:
   2175 			attachtop(c);
   2176 			break;
   2177 		default:
   2178 			attach(c);
   2179 	}
   2180 	attachstack(c);
   2181 	focus(NULL);
   2182 	arrange(NULL);
   2183 }
   2184 
   2185 void
   2186 setclientstate(Client *c, long state)
   2187 {
   2188 	long data[] = { state, None };
   2189 
   2190 	if (c->crop)
   2191 		c = c->crop;
   2192 	XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
   2193 		PropModeReplace, (unsigned char *)data, 2);
   2194 }
   2195 
   2196 int
   2197 sendevent(Client *c, Atom proto)
   2198 {
   2199 	int n;
   2200 	Atom *protocols;
   2201 	int exists = 0;
   2202 	XEvent ev;
   2203 
   2204 	if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
   2205 		while (!exists && n--)
   2206 			exists = protocols[n] == proto;
   2207 		XFree(protocols);
   2208 	}
   2209 	if (exists) {
   2210 		ev.type = ClientMessage;
   2211 		ev.xclient.window = c->win;
   2212 		ev.xclient.message_type = wmatom[WMProtocols];
   2213 		ev.xclient.format = 32;
   2214 		ev.xclient.data.l[0] = proto;
   2215 		ev.xclient.data.l[1] = CurrentTime;
   2216 		XSendEvent(dpy, c->win, False, NoEventMask, &ev);
   2217 	}
   2218 	return exists;
   2219 }
   2220 
   2221 void
   2222 setfocus(Client *c)
   2223 {
   2224 	if (c->crop)
   2225 		c = c->crop;
   2226 	if (!c->neverfocus) {
   2227 		XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
   2228 		XChangeProperty(dpy, root, netatom[NetActiveWindow],
   2229 			XA_WINDOW, 32, PropModeReplace,
   2230 			(unsigned char *) &(c->win), 1);
   2231 	}
   2232 	sendevent(c, wmatom[WMTakeFocus]);
   2233 }
   2234 
   2235 void
   2236 setfullscreen(Client *c, int fullscreen)
   2237 {
   2238 	if (c->crop)
   2239 		c = c->crop;
   2240 	if (fullscreen && !c->isfullscreen) {
   2241 		XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
   2242 			PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
   2243 		c->isfullscreen = 1;
   2244 		c->oldstate = c->isfloating;
   2245 		c->oldbw = c->bw;
   2246 		c->bw = 0;
   2247 		c->isfloating = 1;
   2248 		resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
   2249 		XRaiseWindow(dpy, c->win);
   2250 	} else if (!fullscreen && c->isfullscreen){
   2251 		XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
   2252 			PropModeReplace, (unsigned char*)0, 0);
   2253 		c->isfullscreen = 0;
   2254 		c->isfloating = c->oldstate;
   2255 		c->bw = c->oldbw;
   2256 		c->x = c->oldx;
   2257 		c->y = c->oldy;
   2258 		c->w = c->oldw;
   2259 		c->h = c->oldh;
   2260 		resizeclient(c, c->x, c->y, c->w, c->h);
   2261 		arrange(c->mon);
   2262 	}
   2263 }
   2264 
   2265 void
   2266 setattach(const Arg *arg)
   2267 {
   2268 	if (arg->i == -1)
   2269 		selmon->attachdirection = 0;
   2270 	else if (selmon->attachdirection < 5)
   2271 		selmon->attachdirection++;
   2272 	else selmon->attachdirection = 0;
   2273 	if (selmon->sel)
   2274 		arrange(selmon);
   2275 	else
   2276 		drawbar(selmon);
   2277 }
   2278 
   2279 void
   2280 setlayout(const Arg *arg)
   2281 {
   2282 	if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
   2283 		selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag] ^= 1;
   2284 	if (arg && arg->v)
   2285 		selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt] = (Layout *)arg->v;
   2286 	strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
   2287 	if (selmon->sel)
   2288 		arrange(selmon);
   2289 	else
   2290 		drawbar(selmon);
   2291 }
   2292 
   2293 void setcfact(const Arg *arg) {
   2294 	float f;
   2295 	Client *c;
   2296 
   2297 	c = selmon->sel;
   2298 
   2299 	if(!arg || !c || !selmon->lt[selmon->sellt]->arrange)
   2300 		return;
   2301 	f = arg->f + c->cfact;
   2302 	if(arg->f == 0.0)
   2303 		f = 1.0;
   2304 	else if(f < 0.25 || f > 4.0)
   2305 		return;
   2306 	c->cfact = f;
   2307 	arrange(selmon);
   2308 }
   2309 
   2310 /* arg > 1.0 will set mfact absolutely */
   2311 void
   2312 setmfact(const Arg *arg)
   2313 {
   2314 	float f;
   2315 
   2316 	if (!arg || !selmon->lt[selmon->sellt]->arrange)
   2317 		return;
   2318 	f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
   2319 	if (f < 0.05 || f > 0.95)
   2320 		return;
   2321 	selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag] = f;
   2322 	arrange(selmon);
   2323 }
   2324 
   2325 void
   2326 setigappx(const Arg *arg)
   2327 {
   2328 	selmon->igappx += arg->i;
   2329 	if (selmon->igappx < 0) selmon->igappx = 0;
   2330 	arrange(selmon);
   2331 }
   2332 
   2333 void
   2334 setogappx(const Arg *arg)
   2335 {
   2336 	selmon->ogappx += arg->i;
   2337 	if (selmon->ogappx < 0) selmon->ogappx = 0;
   2338 	arrange(selmon);
   2339 }
   2340 
   2341 void
   2342 gaptog(const Arg *arg)
   2343 {
   2344 	if (arg->i)
   2345 		selmon->smartgaps = !selmon->smartgaps;
   2346 	else {
   2347 		if (selmon->ogappx == ogappx && selmon->igappx == igappx) {
   2348 			selmon->ogappx = 0; selmon->igappx = 0;
   2349 		} else {
   2350 			selmon->ogappx = ogappx; selmon->igappx = igappx;
   2351 		}
   2352 	}
   2353 	arrange(selmon);
   2354 }
   2355 
   2356 void
   2357 sbtog(const Arg *arg)
   2358 {
   2359 	selmon->smartbord = !selmon->smartbord;
   2360 }
   2361 
   2362 void
   2363 setup(void)
   2364 {
   2365 	int i;
   2366 	XSetWindowAttributes wa;
   2367 	Atom utf8string;
   2368 
   2369 	/* clean up any zombies immediately */
   2370 	sigchld(0);
   2371 
   2372 	signal(SIGHUP, sighup);
   2373 	signal(SIGTERM, sigterm);
   2374 
   2375 	/* init screen */
   2376 	screen = DefaultScreen(dpy);
   2377 	sw = DisplayWidth(dpy, screen);
   2378 	sh = DisplayHeight(dpy, screen);
   2379 	root = RootWindow(dpy, screen);
   2380 	drw = drw_create(dpy, screen, root, sw, sh);
   2381 	if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
   2382 		die("no fonts could be loaded.");
   2383 	lrpad = drw->fonts->h;
   2384 	bh = barheight ? barheight : drw->fonts->h + 2;
   2385 	updategeom();
   2386 	/* init atoms */
   2387 	utf8string = XInternAtom(dpy, "UTF8_STRING", False);
   2388 	wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
   2389 	wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
   2390 	wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
   2391 	wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
   2392 	netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
   2393 	netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
   2394 	netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
   2395 	netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
   2396 	netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
   2397 	netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
   2398 	netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
   2399 	netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
   2400 	netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
   2401 	/* init cursors */
   2402 	cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
   2403 	cursor[CurResize] = drw_cur_create(drw, XC_sizing);
   2404 	cursor[CurMove] = drw_cur_create(drw, XC_fleur);
   2405 	/* init appearance */
   2406 	scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
   2407 	for (i = 0; i < LENGTH(colors); i++)
   2408 		scheme[i] = drw_scm_create(drw, colors[i], 3);
   2409 	/* init bars */
   2410 	updatebars();
   2411 	updatestatus();
   2412 	/* supporting window for NetWMCheck */
   2413 	wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
   2414 	XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
   2415 		PropModeReplace, (unsigned char *) &wmcheckwin, 1);
   2416 	XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8,
   2417 		PropModeReplace, (unsigned char *) "dwm", 3);
   2418 	XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32,
   2419 		PropModeReplace, (unsigned char *) &wmcheckwin, 1);
   2420 	/* EWMH support per view */
   2421 	XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
   2422 		PropModeReplace, (unsigned char *) netatom, NetLast);
   2423 	XDeleteProperty(dpy, root, netatom[NetClientList]);
   2424 	/* select events */
   2425 	wa.cursor = cursor[CurNormal]->cursor;
   2426 	wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
   2427 		|ButtonPressMask|PointerMotionMask|EnterWindowMask
   2428 		|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
   2429 	XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
   2430 	XSelectInput(dpy, root, wa.event_mask);
   2431 	grabkeys();
   2432 	focus(NULL);
   2433 }
   2434 
   2435 
   2436 void
   2437 seturgent(Client *c, int urg)
   2438 {
   2439 	XWMHints *wmh;
   2440 
   2441 	c->isurgent = urg;
   2442 	if (!(wmh = XGetWMHints(dpy, c->win)))
   2443 		return;
   2444 	wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
   2445 	XSetWMHints(dpy, c->win, wmh);
   2446 	XFree(wmh);
   2447 }
   2448 
   2449 void
   2450 showhide(Client *c)
   2451 {
   2452 	if (!c)
   2453 		return;
   2454 	if (ISVISIBLE(c)) {
   2455 		/* show clients top down */
   2456 		XMoveWindow(dpy, c->win, c->x, c->y);
   2457 		if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
   2458 			resize(c, c->x, c->y, c->w, c->h, 0);
   2459 		showhide(c->snext);
   2460 	} else {
   2461 		/* hide clients bottom up */
   2462 		showhide(c->snext);
   2463 		XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
   2464 	}
   2465 }
   2466 
   2467 void
   2468 sigchld(int unused)
   2469 {
   2470 	if (signal(SIGCHLD, sigchld) == SIG_ERR)
   2471 		die("can't install SIGCHLD handler:");
   2472 	while (0 < waitpid(-1, NULL, WNOHANG));
   2473 }
   2474 
   2475 void
   2476 sighup(int unused)
   2477 {
   2478 	Arg a = {.i = 1};
   2479 	quit(&a);
   2480 }
   2481 
   2482 void
   2483 sigterm(int unused)
   2484 {
   2485 	Arg a = {.i = 0};
   2486 	quit(&a);
   2487 }
   2488 
   2489 void
   2490 sigdwmblocks(const Arg *arg)
   2491 {
   2492 	union sigval sv;
   2493 	sv.sival_int = (dwmblockssig << 8) | arg->i;
   2494 	if (!dwmblockspid)
   2495 		if (getdwmblockspid() == -1)
   2496 			return;
   2497 
   2498 	if (sigqueue(dwmblockspid, SIGUSR1, sv) == -1) {
   2499 		if (errno == ESRCH) {
   2500 			if (!getdwmblockspid())
   2501 				sigqueue(dwmblockspid, SIGUSR1, sv);
   2502 		}
   2503 	}
   2504 }
   2505 
   2506 void
   2507 spawn(const Arg *arg)
   2508 {
   2509 	if (arg->v == dmenucmd)
   2510 		dmenumon[0] = '0' + selmon->num;
   2511 	if (fork() == 0) {
   2512 		if (dpy)
   2513 			close(ConnectionNumber(dpy));
   2514 		setsid();
   2515 		execvp(((char **)arg->v)[0], (char **)arg->v);
   2516 		fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
   2517 		perror(" failed");
   2518 		exit(EXIT_SUCCESS);
   2519 	}
   2520 }
   2521 
   2522 void
   2523 swaptags(const Arg *arg)
   2524 {
   2525 	unsigned int newtag = arg->ui & TAGMASK;
   2526 	unsigned int curtag = selmon->tagset[selmon->seltags];
   2527 
   2528 	if (newtag == curtag || !curtag || (curtag & (curtag-1)))
   2529 		return;
   2530 
   2531 	for (Client *c = selmon->clients; c != NULL; c = c->next) {
   2532 		if((c->tags & newtag) || (c->tags & curtag))
   2533 			c->tags ^= curtag ^ newtag;
   2534 
   2535 		if(!c->tags) c->tags = newtag;
   2536 	}
   2537 
   2538 	selmon->tagset[selmon->seltags] = newtag;
   2539 
   2540 	focus(NULL);
   2541 	arrange(selmon);
   2542 }
   2543 
   2544 void
   2545 tag(const Arg *arg)
   2546 {
   2547 	if (selmon->sel && arg->ui & TAGMASK) {
   2548 		selmon->sel->tags = arg->ui & TAGMASK;
   2549 		focus(NULL);
   2550 		arrange(selmon);
   2551 	}
   2552 }
   2553 
   2554 void
   2555 tagmon(const Arg *arg)
   2556 {
   2557 	if (!selmon->sel || !mons->next)
   2558 		return;
   2559 	sendmon(selmon->sel, dirtomon(arg->i));
   2560 }
   2561 
   2562 void
   2563 dynamiclayout(Monitor *m)
   2564 {
   2565 	unsigned int n;
   2566 	Client *c;
   2567 
   2568 	for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
   2569 
   2570 	if (n - m->nmaster > 2)
   2571 		bstack(m); /* if there are (> 2) slave window, bottomstack em */
   2572 	else
   2573 		tile(m); /* otherwise make default tile layout */
   2574 }
   2575 
   2576 void
   2577 tile(Monitor *m)
   2578 {
   2579 	unsigned int i, n, h, mw, my, ty;
   2580 	float mfacts = 0, sfacts = 0;
   2581 	Client *c;
   2582 
   2583 	for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++) {
   2584 		if (n < m->nmaster)
   2585 			mfacts += c->cfact;
   2586 		else
   2587 			sfacts += c->cfact;
   2588 	}
   2589 	if (n == 0)
   2590 		return;
   2591 
   2592 	if (n > m->nmaster)
   2593 		mw = m->nmaster ? m->ww * m->mfact : 0;
   2594 	else
   2595 		mw = m->ww;
   2596 	for (i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
   2597 		if (i < m->nmaster) {
   2598 			h = (m->wh - my) * (c->cfact / mfacts);
   2599 			if (n <= m->nmaster)
   2600 				resize(c, m->wx + (!m->smartgaps*m->ogappx),
   2601 						m->wy + my + (!m->smartgaps*m->ogappx),
   2602 						mw - (2*c->bw) - (!m->smartgaps*(2*m->ogappx)),
   2603 						h - (2*c->bw) - (!m->smartgaps*(2*m->ogappx)), 0);
   2604 			else
   2605 				resize(c, m->wx + m->ogappx,
   2606 						m->wy + my + m->ogappx,
   2607 						mw - (2*c->bw) - m->ogappx - m->igappx,
   2608 						h - (2*c->bw) - (2*m->ogappx), 0);
   2609 			if (my + HEIGHT(c) < m->wh)
   2610 				my += HEIGHT(c) + (2*m->igappx);
   2611 			mfacts -= c->cfact;
   2612 		} else {
   2613 			h = (m->wh - ty) * (c->cfact / sfacts);
   2614 			resize(c, m->wx + mw + m->igappx,
   2615 					m->wy + ty + m->ogappx,
   2616 					m->ww - mw - (2*c->bw) - m->ogappx - m->igappx,
   2617 					h - (2*c->bw) - (2*m->ogappx), 0);
   2618 			if (ty + HEIGHT(c) < m->wh)
   2619 				ty += HEIGHT(c) + (2*m->igappx);
   2620 			sfacts -= c->cfact;
   2621 		}
   2622 }
   2623 
   2624 void
   2625 togglebar(const Arg *arg)
   2626 {
   2627 	selmon->showbar = selmon->pertag->showbars[selmon->pertag->curtag] = !selmon->showbar;
   2628 	updatebarpos(selmon);
   2629 	XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
   2630 	XMoveResizeWindow(dpy, selmon->extrabarwin, selmon->wx, selmon->eby, selmon->ww, bh);
   2631 	arrange(selmon);
   2632 }
   2633 
   2634 void
   2635 togglefloating(const Arg *arg)
   2636 {
   2637 	if (!selmon->sel)
   2638 		return;
   2639 	if (selmon->sel->isfullscreen) /* no support for fullscreen windows */
   2640 		return;
   2641 	selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
   2642 	if (selmon->sel->isfloating) {
   2643 		if (arg != NULL) {
   2644 			if (arg->i)
   2645 				resize(selmon->sel, selmon->wx + 250, selmon->wy + 150,
   2646 					selmon->ww - 500, selmon->wh - 300, 0);
   2647 			else
   2648 				resize(selmon->sel, selmon->sel->x, selmon->sel->y,
   2649 					selmon->sel->w, selmon->sel->h, 0);
   2650 		}
   2651 	}
   2652 	if (!selmon->sel->isfloating && selmon->sel->crop)
   2653 		cropdelete(selmon->sel);
   2654 	arrange(selmon);
   2655 }
   2656 
   2657 void
   2658 togglefullscr(const Arg *arg)
   2659 {
   2660   if(selmon->sel)
   2661     setfullscreen(selmon->sel, !selmon->sel->isfullscreen);
   2662 }
   2663 
   2664 void
   2665 toggletag(const Arg *arg)
   2666 {
   2667 	unsigned int newtags;
   2668 
   2669 	if (!selmon->sel)
   2670 		return;
   2671 	newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
   2672 	if (newtags) {
   2673 		selmon->sel->tags = newtags;
   2674 		focus(NULL);
   2675 		arrange(selmon);
   2676 	}
   2677 }
   2678 
   2679 void
   2680 toggleview(const Arg *arg)
   2681 {
   2682 	unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
   2683 	int i;
   2684 
   2685 	if (newtagset) {
   2686 		selmon->tagset[selmon->seltags] = newtagset;
   2687 
   2688 		if (newtagset == ~0) {
   2689 			selmon->pertag->prevtag = selmon->pertag->curtag;
   2690 			selmon->pertag->curtag = 0;
   2691 		}
   2692 
   2693 		/* test if the user did not select the same tag */
   2694 		if (!(newtagset & 1 << (selmon->pertag->curtag - 1))) {
   2695 			selmon->pertag->prevtag = selmon->pertag->curtag;
   2696 			for (i = 0; !(newtagset & 1 << i); i++) ;
   2697 			selmon->pertag->curtag = i + 1;
   2698 		}
   2699 
   2700 		/* apply settings for this view */
   2701 		selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag];
   2702 		selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag];
   2703 		selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
   2704 		selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
   2705 		selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1];
   2706 
   2707 		if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag])
   2708 			togglebar(NULL);
   2709 
   2710 		focus(NULL);
   2711 		arrange(selmon);
   2712 	}
   2713 }
   2714 
   2715 void
   2716 unfocus(Client *c, int setfocus)
   2717 {
   2718 	if (!c)
   2719 		return;
   2720 	grabbuttons(c, 0);
   2721 	XSetWindowBorder(dpy, c->win, scheme[c->isterminal && selmon->smartbord ? SchemeTermNorm : SchemeNorm][ColBorder].pixel);
   2722 	if (setfocus) {
   2723 		XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
   2724 		XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
   2725 	}
   2726 }
   2727 
   2728 void
   2729 unmanage(Client *c, int destroyed)
   2730 {
   2731 	Monitor *m = c->mon;
   2732 	XWindowChanges wc;
   2733 
   2734 	if (c->swallowing) {
   2735 		unswallow(c);
   2736 		return;
   2737 	}
   2738 
   2739 	Client *s = swallowingclient(c->win);
   2740 	if (s) {
   2741 		free(s->swallowing);
   2742 		s->swallowing = NULL;
   2743 		arrange(m);
   2744 		focus(NULL);
   2745 		return;
   2746 	}
   2747 
   2748 	if (c->crop)
   2749 		cropdelete(c);
   2750 	detach(c);
   2751 	detachstack(c);
   2752 	if (!destroyed) {
   2753 		wc.border_width = c->oldbw;
   2754 		XGrabServer(dpy); /* avoid race conditions */
   2755 		XSetErrorHandler(xerrordummy);
   2756 		XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
   2757 		XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
   2758 		setclientstate(c, WithdrawnState);
   2759 		XSync(dpy, False);
   2760 		XSetErrorHandler(xerror);
   2761 		XUngrabServer(dpy);
   2762 	}
   2763 	free(c);
   2764 
   2765 	if (!s) {
   2766 		arrange(m);
   2767 		focus(NULL);
   2768 		updateclientlist();
   2769 	}
   2770 }
   2771 
   2772 void
   2773 unmapnotify(XEvent *e)
   2774 {
   2775 	Client *c;
   2776 	XUnmapEvent *ev = &e->xunmap;
   2777 
   2778 	if ((c = wintoclient(ev->window))
   2779 	|| (c = cropwintoclient(ev->window))) {
   2780 		if (ev->send_event)
   2781 			setclientstate(c, WithdrawnState);
   2782 		else
   2783 			unmanage(c, 0);
   2784 	}
   2785 }
   2786 
   2787 void
   2788 updatebars(void)
   2789 {
   2790 	Monitor *m;
   2791 	XSetWindowAttributes wa = {
   2792 		.override_redirect = True,
   2793 		.background_pixmap = ParentRelative,
   2794 		.event_mask = ButtonPressMask|ExposureMask
   2795 	};
   2796 	XClassHint ch = {"dwm", "dwm"};
   2797 	for (m = mons; m; m = m->next) {
   2798 		if (!m->barwin) {
   2799 			m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
   2800 					CopyFromParent, DefaultVisual(dpy, screen),
   2801 					CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
   2802 			XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
   2803 			XMapRaised(dpy, m->barwin);
   2804 			XSetClassHint(dpy, m->barwin, &ch);
   2805 		}
   2806 		if (!m->extrabarwin) {
   2807 			m->extrabarwin = XCreateWindow(dpy, root, m->wx, m->eby, m->ww, bh, 0, DefaultDepth(dpy, screen),
   2808 					CopyFromParent, DefaultVisual(dpy, screen),
   2809 					CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
   2810 			XDefineCursor(dpy, m->extrabarwin, cursor[CurNormal]->cursor);
   2811 			XMapRaised(dpy, m->extrabarwin);
   2812 			XSetClassHint(dpy, m->extrabarwin, &ch);
   2813 		}
   2814 	}
   2815 }
   2816 
   2817 void
   2818 updatebarpos(Monitor *m)
   2819 {
   2820 	m->wy = m->my;
   2821 	m->wh = m->mh;
   2822 	m->wh -= bh * m->showbar * 2;
   2823 	m->wy = m->showbar ? m->wy + bh : m->wy;
   2824 	if (m->showbar) {
   2825 		m->by = m->topbar ? m->wy - bh : m->wy + m->wh;
   2826 		m->eby = m->topbar ? m->wy + m->wh : m->wy - bh;
   2827 	} else {
   2828 		m->by = -bh;
   2829 		m->eby = -bh;
   2830 	}
   2831 }
   2832 
   2833 void
   2834 updateclientlist()
   2835 {
   2836 	Client *c;
   2837 	Monitor *m;
   2838 
   2839 	XDeleteProperty(dpy, root, netatom[NetClientList]);
   2840 	for (m = mons; m; m = m->next)
   2841 		for (c = m->clients; c; c = c->next)
   2842 			XChangeProperty(dpy, root, netatom[NetClientList],
   2843 				XA_WINDOW, 32, PropModeAppend,
   2844 				(unsigned char *) &(c->win), 1);
   2845 }
   2846 
   2847 int
   2848 updategeom(void)
   2849 {
   2850 	int dirty = 0;
   2851 
   2852 #ifdef XINERAMA
   2853 	if (XineramaIsActive(dpy)) {
   2854 		int i, j, n, nn;
   2855 		Client *c;
   2856 		Monitor *m;
   2857 		XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
   2858 		XineramaScreenInfo *unique = NULL;
   2859 
   2860 		for (n = 0, m = mons; m; m = m->next, n++);
   2861 		/* only consider unique geometries as separate screens */
   2862 		unique = ecalloc(nn, sizeof(XineramaScreenInfo));
   2863 		for (i = 0, j = 0; i < nn; i++)
   2864 			if (isuniquegeom(unique, j, &info[i]))
   2865 				memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
   2866 		XFree(info);
   2867 		nn = j;
   2868 		if (n <= nn) { /* new monitors available */
   2869 			for (i = 0; i < (nn - n); i++) {
   2870 				for (m = mons; m && m->next; m = m->next);
   2871 				if (m)
   2872 					m->next = createmon();
   2873 				else
   2874 					mons = createmon();
   2875 			}
   2876 			for (i = 0, m = mons; i < nn && m; m = m->next, i++)
   2877 				if (i >= n
   2878 				|| unique[i].x_org != m->mx || unique[i].y_org != m->my
   2879 				|| unique[i].width != m->mw || unique[i].height != m->mh)
   2880 				{
   2881 					dirty = 1;
   2882 					m->num = i;
   2883 					m->mx = m->wx = unique[i].x_org;
   2884 					m->my = m->wy = unique[i].y_org;
   2885 					m->mw = m->ww = unique[i].width;
   2886 					m->mh = m->wh = unique[i].height;
   2887 					updatebarpos(m);
   2888 				}
   2889 		} else { /* less monitors available nn < n */
   2890 			for (i = nn; i < n; i++) {
   2891 				for (m = mons; m && m->next; m = m->next);
   2892 				while ((c = m->clients)) {
   2893 					dirty = 1;
   2894 					m->clients = c->next;
   2895 					detachstack(c);
   2896 					c->mon = mons;
   2897 					switch(attachdirection){
   2898 					case 1:
   2899 						attachabove(c);
   2900 						break;
   2901 					case 2:
   2902 						attachaside(c);
   2903 						break;
   2904 					case 3:
   2905 						attachbelow(c);
   2906 						break;
   2907 					case 4:
   2908 						attachbottom(c);
   2909 						break;
   2910 					case 5:
   2911 						attachtop(c);
   2912 						break;
   2913 					default:
   2914 						attach(c);
   2915 					}
   2916 					attachstack(c);
   2917 				}
   2918 				if (m == selmon)
   2919 					selmon = mons;
   2920 				cleanupmon(m);
   2921 			}
   2922 		}
   2923 		free(unique);
   2924 	} else
   2925 #endif /* XINERAMA */
   2926 	{ /* default monitor setup */
   2927 		if (!mons)
   2928 			mons = createmon();
   2929 		if (mons->mw != sw || mons->mh != sh) {
   2930 			dirty = 1;
   2931 			mons->mw = mons->ww = sw;
   2932 			mons->mh = mons->wh = sh;
   2933 			updatebarpos(mons);
   2934 		}
   2935 	}
   2936 	if (dirty) {
   2937 		selmon = mons;
   2938 		selmon = wintomon(root);
   2939 	}
   2940 	return dirty;
   2941 }
   2942 
   2943 void
   2944 updatenumlockmask(void)
   2945 {
   2946 	unsigned int i, j;
   2947 	XModifierKeymap *modmap;
   2948 
   2949 	numlockmask = 0;
   2950 	modmap = XGetModifierMapping(dpy);
   2951 	for (i = 0; i < 8; i++)
   2952 		for (j = 0; j < modmap->max_keypermod; j++)
   2953 			if (modmap->modifiermap[i * modmap->max_keypermod + j]
   2954 				== XKeysymToKeycode(dpy, XK_Num_Lock))
   2955 				numlockmask = (1 << i);
   2956 	XFreeModifiermap(modmap);
   2957 }
   2958 
   2959 void
   2960 updatesizehints(Client *c)
   2961 {
   2962 	long msize;
   2963 	XSizeHints size;
   2964 
   2965 	if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
   2966 		/* size is uninitialized, ensure that size.flags aren't used */
   2967 		size.flags = PSize;
   2968 	if (size.flags & PBaseSize) {
   2969 		c->basew = size.base_width;
   2970 		c->baseh = size.base_height;
   2971 	} else if (size.flags & PMinSize) {
   2972 		c->basew = size.min_width;
   2973 		c->baseh = size.min_height;
   2974 	} else
   2975 		c->basew = c->baseh = 0;
   2976 	if (size.flags & PResizeInc) {
   2977 		c->incw = size.width_inc;
   2978 		c->inch = size.height_inc;
   2979 	} else
   2980 		c->incw = c->inch = 0;
   2981 	if (size.flags & PMaxSize) {
   2982 		c->maxw = size.max_width;
   2983 		c->maxh = size.max_height;
   2984 	} else
   2985 		c->maxw = c->maxh = 0;
   2986 	if (size.flags & PMinSize) {
   2987 		c->minw = size.min_width;
   2988 		c->minh = size.min_height;
   2989 	} else if (size.flags & PBaseSize) {
   2990 		c->minw = size.base_width;
   2991 		c->minh = size.base_height;
   2992 	} else
   2993 		c->minw = c->minh = 0;
   2994 	if (size.flags & PAspect) {
   2995 		c->mina = (float)size.min_aspect.y / size.min_aspect.x;
   2996 		c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
   2997 	} else
   2998 		c->maxa = c->mina = 0.0;
   2999 	c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh);
   3000 }
   3001 
   3002 void
   3003 updatestatus(void)
   3004 {
   3005 	char text[2048];
   3006 	if (!gettextprop(root, XA_WM_NAME, rawstext, sizeof(rawstext))) {
   3007 		strcpy(stext, "dwm-"VERSION);
   3008 		estext[0] = '\0';
   3009 	} else {
   3010 		copyvalidchars(text, rawstext);
   3011 		char *e = strchr(text, statussep);
   3012 		if (e) {
   3013 			*e = '\0'; e++;
   3014 			strncpy(estext, e, sizeof(estext) - 1);
   3015 		} else {
   3016 			estext[0] = '\0';
   3017 		}
   3018 		strncpy(stext, text, sizeof(stext) - 1);
   3019 	}
   3020 	drawbar(selmon);
   3021 }
   3022 
   3023 void
   3024 updatetitle(Client *c)
   3025 {
   3026 	if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
   3027 		gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
   3028 	if (c->name[0] == '\0') /* hack to mark broken clients */
   3029 		strcpy(c->name, broken);
   3030 }
   3031 
   3032 void
   3033 updatewindowtype(Client *c)
   3034 {
   3035 	Atom state = getatomprop(c, netatom[NetWMState]);
   3036 	Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
   3037 
   3038 	if (state == netatom[NetWMFullscreen])
   3039 		setfullscreen(c, 1);
   3040 	if (wtype == netatom[NetWMWindowTypeDialog])
   3041 		c->isfloating = 1;
   3042 }
   3043 
   3044 void
   3045 updatewmhints(Client *c)
   3046 {
   3047 	XWMHints *wmh;
   3048 
   3049 	if ((wmh = XGetWMHints(dpy, c->win))) {
   3050 		if (c == selmon->sel && wmh->flags & XUrgencyHint) {
   3051 			wmh->flags &= ~XUrgencyHint;
   3052 			XSetWMHints(dpy, c->win, wmh);
   3053 		} else
   3054 			c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
   3055 		if (wmh->flags & InputHint)
   3056 			c->neverfocus = !wmh->input;
   3057 		else
   3058 			c->neverfocus = 0;
   3059 		XFree(wmh);
   3060 	}
   3061 }
   3062 
   3063 void
   3064 view(const Arg *arg)
   3065 {
   3066 	int i;
   3067 	unsigned int tmptag;
   3068 
   3069 	if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
   3070 		return;
   3071 	selmon->seltags ^= 1; /* toggle sel tagset */
   3072 	if (arg->ui & TAGMASK) {
   3073 		selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
   3074 		selmon->pertag->prevtag = selmon->pertag->curtag;
   3075 
   3076 		if (arg->ui == ~0)
   3077 			selmon->pertag->curtag = 0;
   3078 		else {
   3079 			for (i = 0; !(arg->ui & 1 << i); i++) ;
   3080 			selmon->pertag->curtag = i + 1;
   3081 		}
   3082 	} else {
   3083 		tmptag = selmon->pertag->prevtag;
   3084 		selmon->pertag->prevtag = selmon->pertag->curtag;
   3085 		selmon->pertag->curtag = tmptag;
   3086 	}
   3087 
   3088 	selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag];
   3089 	selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag];
   3090 	selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
   3091 	selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
   3092 	selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1];
   3093 
   3094 	if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag])
   3095 		togglebar(NULL);
   3096 
   3097 	focus(NULL);
   3098 	arrange(selmon);
   3099 }
   3100 
   3101 pid_t
   3102 winpid(Window w)
   3103 {
   3104 
   3105 	pid_t result = 0;
   3106 
   3107 #ifdef __linux__
   3108 	xcb_res_client_id_spec_t spec = {0};
   3109 	spec.client = w;
   3110 	spec.mask = XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID;
   3111 
   3112 	xcb_generic_error_t *e = NULL;
   3113 	xcb_res_query_client_ids_cookie_t c = xcb_res_query_client_ids(xcon, 1, &spec);
   3114 	xcb_res_query_client_ids_reply_t *r = xcb_res_query_client_ids_reply(xcon, c, &e);
   3115 
   3116 	if (!r)
   3117 		return (pid_t)0;
   3118 
   3119 	xcb_res_client_id_value_iterator_t i = xcb_res_query_client_ids_ids_iterator(r);
   3120 	for (; i.rem; xcb_res_client_id_value_next(&i)) {
   3121 		spec = i.data->spec;
   3122 		if (spec.mask & XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID) {
   3123 			uint32_t *t = xcb_res_client_id_value_value(i.data);
   3124 			result = *t;
   3125 			break;
   3126 		}
   3127 	}
   3128 
   3129 	free(r);
   3130 
   3131 	if (result == (pid_t)-1)
   3132 		result = 0;
   3133 
   3134 #endif /* __linux__ */
   3135 
   3136 #ifdef __OpenBSD__
   3137         Atom type;
   3138         int format;
   3139         unsigned long len, bytes;
   3140         unsigned char *prop;
   3141         pid_t ret;
   3142 
   3143         if (XGetWindowProperty(dpy, w, XInternAtom(dpy, "_NET_WM_PID", 0), 0, 1, False, AnyPropertyType, &type, &format, &len, &bytes, &prop) != Success || !prop)
   3144                return 0;
   3145 
   3146         ret = *(pid_t*)prop;
   3147         XFree(prop);
   3148         result = ret;
   3149 
   3150 #endif /* __OpenBSD__ */
   3151 	return result;
   3152 }
   3153 
   3154 pid_t
   3155 getparentprocess(pid_t p)
   3156 {
   3157 	unsigned int v = 0;
   3158 
   3159 #ifdef __linux__
   3160 	FILE *f;
   3161 	char buf[2048];
   3162 	snprintf(buf, sizeof(buf) - 1, "/proc/%u/stat", (unsigned)p);
   3163 
   3164 	if (!(f = fopen(buf, "r")))
   3165 		return 0;
   3166 
   3167 	fscanf(f, "%*u %*s %*c %u", &v);
   3168 	fclose(f);
   3169 #endif /* __linux__*/
   3170 
   3171 #ifdef __OpenBSD__
   3172 	int n;
   3173 	kvm_t *kd;
   3174 	struct kinfo_proc *kp;
   3175 
   3176 	kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, NULL);
   3177 	if (!kd)
   3178 		return 0;
   3179 
   3180 	kp = kvm_getprocs(kd, KERN_PROC_PID, p, sizeof(*kp), &n);
   3181 	v = kp->p_ppid;
   3182 #endif /* __OpenBSD__ */
   3183 
   3184 	return (pid_t)v;
   3185 }
   3186 
   3187 int
   3188 isdescprocess(pid_t p, pid_t c)
   3189 {
   3190 	while (p != c && c != 0)
   3191 		c = getparentprocess(c);
   3192 
   3193 	return (int)c;
   3194 }
   3195 
   3196 Client *
   3197 termforwin(const Client *w)
   3198 {
   3199 	Client *c;
   3200 	Monitor *m;
   3201 
   3202 	if (!w->pid || w->isterminal)
   3203 		return NULL;
   3204 
   3205 	for (m = mons; m; m = m->next) {
   3206 		for (c = m->clients; c; c = c->next) {
   3207 			if (c->isterminal && !c->swallowing && c->pid && isdescprocess(c->pid, w->pid))
   3208 				return c;
   3209 		}
   3210 	}
   3211 
   3212 	return NULL;
   3213 }
   3214 
   3215 Client *
   3216 swallowingclient(Window w)
   3217 {
   3218 	Client *c;
   3219 	Monitor *m;
   3220 
   3221 	for (m = mons; m; m = m->next) {
   3222 		for (c = m->clients; c; c = c->next) {
   3223 			if (c->swallowing && c->swallowing->win == w)
   3224 				return c;
   3225 		}
   3226 	}
   3227 
   3228 	return NULL;
   3229 }
   3230 
   3231 Client *
   3232 wintoclient(Window w)
   3233 {
   3234 	Client *c;
   3235 	Monitor *m;
   3236 
   3237 	for (m = mons; m; m = m->next)
   3238 		for (c = m->clients; c; c = c->next)
   3239 			if (c->win == w)
   3240 				return c;
   3241 	return NULL;
   3242 }
   3243 
   3244 Monitor *
   3245 wintomon(Window w)
   3246 {
   3247 	int x, y;
   3248 	Client *c;
   3249 	Monitor *m;
   3250 
   3251 	if (w == root && getrootptr(&x, &y))
   3252 		return recttomon(x, y, 1, 1);
   3253 	for (m = mons; m; m = m->next)
   3254 		if (w == m->barwin || w == m->extrabarwin)
   3255 			return m;
   3256 	if ((c = wintoclient(w)) || (c = cropwintoclient(w)))
   3257 		return c->mon;
   3258 	return selmon;
   3259 }
   3260 
   3261 /* There's no way to check accesses to destroyed windows, thus those cases are
   3262  * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
   3263  * default error handler, which may call exit. */
   3264 int
   3265 xerror(Display *dpy, XErrorEvent *ee)
   3266 {
   3267 	if (ee->error_code == BadWindow
   3268 	|| (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
   3269 	|| (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
   3270 	|| (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
   3271 	|| (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
   3272 	|| (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
   3273 	|| (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
   3274 	|| (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
   3275 	|| (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
   3276 		return 0;
   3277 	fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
   3278 		ee->request_code, ee->error_code);
   3279 	return xerrorxlib(dpy, ee); /* may call exit */
   3280 }
   3281 
   3282 int
   3283 xerrordummy(Display *dpy, XErrorEvent *ee)
   3284 {
   3285 	return 0;
   3286 }
   3287 
   3288 /* Startup Error handler to check if another window manager
   3289  * is already running. */
   3290 int
   3291 xerrorstart(Display *dpy, XErrorEvent *ee)
   3292 {
   3293 	die("dwm: another window manager is already running");
   3294 	return -1;
   3295 }
   3296 
   3297 void
   3298 zoom(const Arg *arg)
   3299 {
   3300 	Client *c = selmon->sel;
   3301 
   3302 	if (!selmon->lt[selmon->sellt]->arrange
   3303 	|| (selmon->sel && selmon->sel->isfloating))
   3304 		return;
   3305 	if (c == nexttiled(selmon->clients))
   3306 		if (!c || !(c = nexttiled(c->next)))
   3307 			return;
   3308 	pop(c);
   3309 }
   3310 
   3311 int
   3312 main(int argc, char *argv[])
   3313 {
   3314 	if (argc == 2 && !strcmp("-v", argv[1]))
   3315 		die("dwm-"VERSION);
   3316 	else if (argc != 1)
   3317 		die("usage: dwm [-v]");
   3318 	if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
   3319 		fputs("warning: no locale support\n", stderr);
   3320 	if (!(dpy = XOpenDisplay(NULL)))
   3321 		die("dwm: cannot open display");
   3322 	if (!(xcon = XGetXCBConnection(dpy)))
   3323 		die("dwm: cannot get xcb connection\n");
   3324 	checkotherwm();
   3325 	setup();
   3326 #ifdef __OpenBSD__
   3327 	if (pledge("stdio rpath proc exec ps", NULL) == -1)
   3328 		die("pledge");
   3329 #endif /* __OpenBSD__ */
   3330 	scan();
   3331 	run();
   3332 	if(restart) execvp(argv[0], argv);
   3333 	cleanup();
   3334 	XCloseDisplay(dpy);
   3335 	return EXIT_SUCCESS;
   3336 }
   3337 
   3338 void
   3339 bstack(Monitor *m)
   3340 {
   3341 	unsigned int i, n, w, mh, mx, tx;
   3342 	float mfacts = 0, sfacts = 0;
   3343 	Client *c;
   3344 
   3345 	for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++) {
   3346 		if (n < m->nmaster)
   3347 			mfacts += c->cfact;
   3348 		else
   3349 			sfacts += c->cfact;
   3350 	}
   3351 	if (n == 0)
   3352 		return;
   3353 
   3354 	if (n == 1) {
   3355 		c = nexttiled(m->clients);
   3356 		resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
   3357 		return;
   3358 	}
   3359 
   3360 	if (n > m->nmaster)
   3361 		mh = m->nmaster ? m->wh * m->mfact : 0;
   3362 	else
   3363 		mh = m->wh;
   3364 	for (i = 0, mx = tx = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
   3365 		if (i < m->nmaster) {
   3366 			w = (m->ww - mx) * (c->cfact / mfacts);
   3367 			if (n <= m->nmaster)
   3368 				resize(c, m->wx + mx + m->ogappx,
   3369 						m->wy + m->ogappx,
   3370 						w - (2*c->bw) - (2*m->ogappx),
   3371 						mh - 2*c->bw - (2*m->ogappx), 0);
   3372 			else
   3373 				resize(c, m->wx + mx + m->ogappx,
   3374 						m->wy + m->ogappx,
   3375 						w - (2*c->bw) - (2*m->ogappx),
   3376 						mh - 2*c->bw - m->ogappx, 0);
   3377 			if(mx + WIDTH(c) < m->mw)
   3378 				mx += WIDTH(c) + (2*m->igappx);
   3379 			mfacts -= c->cfact;
   3380 		} else {
   3381 			w = (m->ww - tx) * (c->cfact / sfacts);
   3382 			resize(c, m->wx + tx + m->ogappx,
   3383 					m->wy + mh + (2*m->igappx),
   3384 					w - (2*c->bw) - (2*m->ogappx),
   3385 					m->wh - mh - (2*c->bw) - (2*m->igappx) - m->ogappx,
   3386 					0);
   3387 			if(tx + WIDTH(c) < m->mw)
   3388 				tx += WIDTH(c) + (2*m->igappx);
   3389 			sfacts -= c->cfact;
   3390 		}
   3391 }
   3392 
   3393 void
   3394 bstackhoriz(Monitor *m)
   3395 {
   3396 	unsigned int i, n, h, mw, mh, my, ty;
   3397 	float mfacts = 0, sfacts = 0;
   3398 	Client *c;
   3399 
   3400 	for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++) {
   3401 		if (n < m->nmaster)
   3402 			mfacts += c->cfact;
   3403 		else
   3404 			sfacts += c->cfact;
   3405 	}
   3406 	if (n == 0)
   3407 		return;
   3408 	if(n == 1){
   3409 		c = nexttiled(m->clients);
   3410 		resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
   3411 		return;
   3412 	}
   3413 
   3414 	if (n > m->nmaster)
   3415 		mh = m->nmaster ? m->wh * m->mfact : 0;
   3416 	else
   3417 		mh = m->wh;
   3418 	mw = m->ww;
   3419 
   3420 	for (i = ty = 0, my = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
   3421 		if (i < m->nmaster) {
   3422 			h = (mh - my) * (c->cfact / mfacts);
   3423 			resize(c, m->wx, m->wy + my, mw - 2*c->bw, h - 2*c->bw, 0);
   3424 			if(my + HEIGHT(c) < m->mh)
   3425 				my += HEIGHT(c);
   3426 			mfacts -= c->cfact;
   3427 		} else {
   3428 			h = (m->wh - mh - ty) * (c->cfact / sfacts);
   3429 			resize(c, m->wx, m->wy + mh + ty, mw - 2*c->bw, h - (2*c->bw), 0);
   3430 			if(ty + HEIGHT(c) < m->mh)
   3431 				ty += HEIGHT(c);
   3432 			sfacts -= c->cfact;
   3433 		}
   3434 }
   3435 
   3436 void
   3437 centeredmaster(Monitor *m)
   3438 {
   3439 	unsigned int i, n, h, mw, mx, my, oty, ety, tw;
   3440 	float mfacts = 0, lfacts = 0, rfacts = 0;
   3441 	Client *c;
   3442 
   3443 	/* count number of clients in the selected monitor */
   3444 	for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++) {
   3445 		if (n < m->nmaster)
   3446 			mfacts += c->cfact;
   3447 		else if ((n - m->nmaster) % 2)
   3448 			lfacts += c->cfact;
   3449 		else
   3450 			rfacts += c->cfact;
   3451 	}
   3452 	if (n == 0)
   3453 		return;
   3454 	if(n == 1){
   3455 		c = nexttiled(m->clients);
   3456 		resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
   3457 		return;
   3458 	}
   3459 
   3460 	/* initialize areas */
   3461 	mw = m->ww;
   3462 	mx = 0;
   3463 	my = 0;
   3464 	tw = mw;
   3465 
   3466 	if (n > m->nmaster) {
   3467 		/* go mfact box in the center if more than nmaster clients */
   3468 		mw = m->nmaster ? m->ww * m->mfact : 0;
   3469 		tw = m->ww - mw;
   3470 
   3471 		if (n - m->nmaster > 1) {
   3472 			/* only one client */
   3473 			mx = (m->ww - mw) / 2;
   3474 			tw = (m->ww - mw) / 2;
   3475 		}
   3476 	}
   3477 
   3478 	oty = 0;
   3479 	ety = 0;
   3480 	for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
   3481 	if (i < m->nmaster) {
   3482 		/* nmaster clients are stacked vertically, in the center
   3483 		 * of the screen */
   3484 		h = (m->wh - my) * (c->cfact / mfacts);
   3485 		resize(c, m->wx + mx, m->wy + my, mw - 2*c->bw,
   3486 		      h - 2*c->bw, 0);
   3487 		if(my + HEIGHT(c) < m->mh)
   3488 			my += HEIGHT(c);
   3489 		mfacts -= c->cfact;
   3490 	} else {
   3491 		/* stack clients are stacked vertically */
   3492 		if ((i - m->nmaster) % 2) {
   3493 			h = (m->wh - ety) * (c->cfact / lfacts);
   3494 			if(m->nmaster == 0)
   3495 				resize(c, m->wx, m->wy + ety, tw - 2*c->bw,
   3496 			       h - 2*c->bw, 0);
   3497 			else
   3498 				resize(c, m->wx, m->wy + ety, tw - 2*c->bw,
   3499 			       h - 2*c->bw, 0);
   3500 			if(ety + HEIGHT(c) < m->mh)
   3501 				ety += HEIGHT(c);
   3502 			lfacts -= c->cfact;
   3503 		} else {
   3504 			h = (m->wh - oty) * (c->cfact / rfacts);
   3505 			resize(c, m->wx + mx + mw, m->wy + oty,
   3506 			       tw - 2*c->bw, h - 2*c->bw, 0);
   3507 			if(oty + HEIGHT(c) < m->mh)
   3508 				oty += HEIGHT(c);
   3509 			rfacts -= c->cfact;
   3510 		}
   3511 	}
   3512 }
   3513 
   3514 void
   3515 centeredfloatingmaster(Monitor *m)
   3516 {
   3517 	unsigned int i, n, w, mh, mw, mx, mxo, my, myo, tx;
   3518 	float mfacts = 0, sfacts = 0;
   3519 	Client *c;
   3520 
   3521 	/* count number of clients in the selected monitor */
   3522 	for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++) {
   3523 		if (n < m->nmaster)
   3524 			mfacts += c->cfact;
   3525 		else
   3526 			sfacts += c->cfact;
   3527 	}
   3528 	if (n == 0)
   3529 		return;
   3530 	if(n == 1){
   3531 		c = nexttiled(m->clients);
   3532 		resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
   3533 		return;
   3534 	}
   3535 
   3536 	/* initialize nmaster area */
   3537 	if (n > m->nmaster) {
   3538 		/* go mfact box in the center if more than nmaster clients */
   3539 		if (m->ww > m->wh) {
   3540 			mw = m->nmaster ? m->ww * m->mfact : 0;
   3541 			mh = m->nmaster ? m->wh * 0.9 : 0;
   3542 		} else {
   3543 			mh = m->nmaster ? m->wh * m->mfact : 0;
   3544 			mw = m->nmaster ? m->ww * 0.9 : 0;
   3545 		}
   3546 		mx = mxo = (m->ww - mw) / 2;
   3547 		my = myo = (m->wh - mh) / 2;
   3548 	} else {
   3549 		/* go fullscreen if all clients are in the master area */
   3550 		mh = m->wh;
   3551 		mw = m->ww;
   3552 		mx = mxo = 0;
   3553 		my = myo = 0;
   3554 	}
   3555 
   3556 	for(i = tx = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
   3557 	if (i < m->nmaster) {
   3558 		/* nmaster clients are stacked horizontally, in the center
   3559 		 * of the screen */
   3560 		w = (mw + mxo - mx) * (c->cfact / mfacts);
   3561 		resize(c, m->wx + mx, m->wy + my, w - 2*c->bw,
   3562 		       mh - 2*c->bw, 0);
   3563 		if(mx + WIDTH(c) < m->mw)
   3564 			mx += WIDTH(c);
   3565 		mfacts -= c->cfact;
   3566 	} else {
   3567 		/* stack clients are stacked horizontally */
   3568 		w = (m->ww - tx) * (c->cfact / sfacts);
   3569 		resize(c, m->wx + tx, m->wy, w - 2*c->bw,
   3570 		       m->wh - 2*c->bw, 0);
   3571 		if(tx + WIDTH(c) < m->mw)
   3572 			tx += WIDTH(c);
   3573 		sfacts -= c->cfact;
   3574 	}
   3575 }