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