[download]

local/src/dwm/dwm.c

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