all repos — dwm @ 3465bed290abc62cb2e69a8096084ba6b8eb4956

fork of suckless dynamic window manager

dwm.c (view raw)

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