/* See LICENSE file for copyright and license details. * * dynamic window manager is designed like any other X client as well. It is * driven through handling X events. In contrast to other X clients, a window * manager selects for SubstructureRedirectMask on the root window, to receive * events about window (dis-)appearance. Only one X connection at a time is * allowed to select for this event mask. * * Calls to fetch an X event from the event queue are blocking. Due reading * status text from standard input, a select()-driven main loop has been * implemented which selects for reads on the X connection and STDIN_FILENO to * handle all data smoothly. The event handlers of dwm are organized in an * array which is accessed whenever a new event has been fetched. This allows * event dispatching in O(1) time. * * Each child of the root window is called a client, except windows which have * set the override_redirect flag. Clients are organized in a global * doubly-linked client list, the focus history is remembered through a global * stack list. Each client contains a bit array to indicate the tags of a * client. * * Keys and tagging rules are organized as arrays and defined in config.h. * * To understand everything else, start reading main(). */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef XINERAMA #include #endif /* macros */ #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask) #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask)) #define LENGTH(x) (sizeof x / sizeof x[0]) #define MAXTAGLEN 16 #define MOUSEMASK (BUTTONMASK|PointerMotionMask) #define TAGMASK ((int)((1LL << LENGTH(tags)) - 1)) #define TEXTW(x) (textnw(x, strlen(x)) + dc.font.height) /* enums */ enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */ enum { ColBorder, ColFG, ColBG, ColLast }; /* color */ enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */ enum { WMProtocols, WMDelete, WMName, WMState, WMLast };/* default atoms */ enum { ClkLtSymbol = 64, ClkStatusText, ClkWinTitle, ClkClientWin, ClkRootWin, ClkLast }; /* clicks */ /* typedefs */ typedef unsigned int uint; typedef unsigned long ulong; typedef union { int i; uint ui; float f; void *v; } Arg; typedef struct { uint click; uint mask; uint button; void (*func)(const Arg *arg); const Arg arg; } Button; typedef struct Client Client; struct Client { char name[256]; float mina, maxa; int x, y, w, h; int basew, baseh, incw, inch, maxw, maxh, minw, minh; int bw, oldbw; uint tags; Bool isbanned, isfixed, isfloating, isurgent; Client *next; Client *snext; Window win; }; typedef struct { int x, y, w, h; ulong norm[ColLast]; ulong sel[ColLast]; Drawable drawable; GC gc; struct { int ascent; int descent; int height; XFontSet set; XFontStruct *xfont; } font; } DC; /* draw context */ typedef struct { uint mod; KeySym keysym; void (*func)(const Arg *); const Arg arg; } Key; typedef struct { const char *symbol; void (*arrange)(void); } Layout; typedef struct { const char *class; const char *instance; const char *title; uint tags; Bool isfloating; } Rule; /* function declarations */ static void applyrules(Client *c); static void arrange(void); static void attach(Client *c); static void attachstack(Client *c); static void buttonpress(XEvent *e); static void checkotherwm(void); static void cleanup(void); static void configure(Client *c); static void configurenotify(XEvent *e); static void configurerequest(XEvent *e); static void destroynotify(XEvent *e); static void detach(Client *c); static void detachstack(Client *c); static void drawbar(void); static void drawsquare(Bool filled, Bool empty, Bool invert, ulong col[ColLast]); static void drawtext(const char *text, ulong col[ColLast], Bool invert); static void enternotify(XEvent *e); static void eprint(const char *errstr, ...); static void expose(XEvent *e); static void focus(Client *c); static void focusin(XEvent *e); static void focusstack(const Arg *arg); static Client *getclient(Window w); static ulong getcolor(const char *colstr); static long getstate(Window w); static Bool gettextprop(Window w, Atom atom, char *text, uint size); static void grabbuttons(Client *c, Bool focused); static void grabkeys(void); static void initfont(const char *fontstr); static Bool isoccupied(uint t); static Bool isprotodel(Client *c); static Bool isurgent(uint t); static void keypress(XEvent *e); static void killclient(const Arg *arg); static void manage(Window w, XWindowAttributes *wa); static void mappingnotify(XEvent *e); static void maprequest(XEvent *e); static void monocle(void); static void movemouse(const Arg *arg); static Client *nexttiled(Client *c); static void propertynotify(XEvent *e); static void quit(const Arg *arg); static void resize(Client *c, int x, int y, int w, int h, Bool sizehints); static void resizemouse(const Arg *arg); static void restack(void); static void run(void); static void scan(void); static void setclientstate(Client *c, long state); static void setlayout(const Arg *arg); static void setmfact(const Arg *arg); static void setup(void); static void spawn(const Arg *arg); static void tag(const Arg *arg); static int textnw(const char *text, uint len); static void tile(void); static void togglebar(const Arg *arg); static void togglefloating(const Arg *arg); static void toggletag(const Arg *arg); static void toggleview(const Arg *arg); static void unmanage(Client *c); static void unmapnotify(XEvent *e); static void updatebar(void); static void updategeom(void); static void updatesizehints(Client *c); static void updatetitle(Client *c); static void updatewmhints(Client *c); static void view(const Arg *arg); static int xerror(Display *dpy, XErrorEvent *ee); static int xerrordummy(Display *dpy, XErrorEvent *ee); static int xerrorstart(Display *dpy, XErrorEvent *ee); static void zoom(const Arg *arg); /* variables */ static char stext[256]; static int screen, sx, sy, sw, sh; static int by, bh, blw, wx, wy, ww, wh; static uint seltags = 0; static int (*xerrorxlib)(Display *, XErrorEvent *); static uint numlockmask = 0; static void (*handler[LASTEvent]) (XEvent *) = { [ButtonPress] = buttonpress, [ConfigureRequest] = configurerequest, [ConfigureNotify] = configurenotify, [DestroyNotify] = destroynotify, [EnterNotify] = enternotify, [Expose] = expose, [FocusIn] = focusin, [KeyPress] = keypress, [MappingNotify] = mappingnotify, [MapRequest] = maprequest, [PropertyNotify] = propertynotify, [UnmapNotify] = unmapnotify }; static Atom wmatom[WMLast], netatom[NetLast]; static Bool otherwm, readin; static Bool running = True; static uint tagset[] = {1, 1}; /* after start, first tag is selected */ static Client *clients = NULL; static Client *sel = NULL; static Client *stack = NULL; static Cursor cursor[CurLast]; static Display *dpy; static DC dc = {0}; static Layout *lt = NULL; static Window root, barwin; /* configuration, allows nested code to access above variables */ #include "config.h" /* compile-time check if all tags fit into an uint bit array. */ struct NumTags { char limitexceeded[sizeof(uint) * 8 < LENGTH(tags) ? -1 : 1]; }; /* function implementations */ void applyrules(Client *c) { uint i; Rule *r; XClassHint ch = { 0 }; /* rule matching */ XGetClassHint(dpy, c->win, &ch); for(i = 0; i < LENGTH(rules); i++) { r = &rules[i]; if((!r->title || strstr(c->name, r->title)) && (!r->class || (ch.res_class && strstr(ch.res_class, r->class))) && (!r->instance || (ch.res_name && strstr(ch.res_name, r->instance)))) { c->isfloating = r->isfloating; c->tags |= r->tags & TAGMASK; } } if(ch.res_class) XFree(ch.res_class); if(ch.res_name) XFree(ch.res_name); if(!c->tags) c->tags = tagset[seltags]; } void arrange(void) { Client *c; for(c = clients; c; c = c->next) if(c->tags & tagset[seltags]) { /* is visible */ if(!lt->arrange || c->isfloating) resize(c, c->x, c->y, c->w, c->h, True); c->isbanned = False; } else if(!c->isbanned) { XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y); c->isbanned = True; } focus(NULL); if(lt->arrange) lt->arrange(); restack(); } void attach(Client *c) { c->next = clients; clients = c; } void attachstack(Client *c) { c->snext = stack; stack = c; } void buttonpress(XEvent *e) { uint i, x, click; Client *c; XButtonPressedEvent *ev = &e->xbutton; click = ClkRootWin; if(ev->window == barwin) { i = x = 0; do x += TEXTW(tags[i]); while(ev->x >= x && ++i < LENGTH(tags)); if(i < LENGTH(tags)) click = i; else if(ev->x < x + blw) click = ClkLtSymbol; else if(ev->x > wx + ww - TEXTW(stext)) click = ClkStatusText; else click = ClkWinTitle; } else if((c = getclient(ev->window))) { focus(c); click = ClkClientWin; } for(i = 0; i < LENGTH(buttons); i++) if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state)) buttons[i].func(&buttons[i].arg); } void checkotherwm(void) { otherwm = False; XSetErrorHandler(xerrorstart); /* this causes an error if some other window manager is running */ XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask); XSync(dpy, False); if(otherwm) eprint("dwm: another window manager is already running\n"); XSetErrorHandler(NULL); xerrorxlib = XSetErrorHandler(xerror); XSync(dpy, False); } void cleanup(void) { Arg a = {.i = ~0}; Layout foo = { "", NULL }; close(STDIN_FILENO); view(&a); lt = &foo; while(stack) unmanage(stack); if(dc.font.set) XFreeFontSet(dpy, dc.font.set); else XFreeFont(dpy, dc.font.xfont); XUngrabKey(dpy, AnyKey, AnyModifier, root); XFreePixmap(dpy, dc.drawable); XFreeGC(dpy, dc.gc); XFreeCursor(dpy, cursor[CurNormal]); XFreeCursor(dpy, cursor[CurResize]); XFreeCursor(dpy, cursor[CurMove]); XDestroyWindow(dpy, barwin); XSync(dpy, False); XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); } void configure(Client *c) { XConfigureEvent ce; ce.type = ConfigureNotify; ce.display = dpy; ce.event = c->win; ce.window = c->win; ce.x = c->x; ce.y = c->y; ce.width = c->w; ce.height = c->h; ce.border_width = c->bw; ce.above = None; ce.override_redirect = False; XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce); } void configurenotify(XEvent *e) { XConfigureEvent *ev = &e->xconfigure; if(ev->window == root && (ev->width != sw || ev->height != sh)) { sw = ev->width; sh = ev->height; updategeom(); updatebar(); arrange(); } } void configurerequest(XEvent *e) { Client *c; XConfigureRequestEvent *ev = &e->xconfigurerequest; XWindowChanges wc; if((c = getclient(ev->window))) { if(ev->value_mask & CWBorderWidth) c->bw = ev->border_width; else if(c->isfloating || !lt->arrange) { if(ev->value_mask & CWX) c->x = sx + ev->x; if(ev->value_mask & CWY) c->y = sy + ev->y; if(ev->value_mask & CWWidth) c->w = ev->width; if(ev->value_mask & CWHeight) c->h = ev->height; if((c->x - sx + c->w) > sw && c->isfloating) c->x = sx + (sw / 2 - c->w / 2); /* center in x direction */ if((c->y - sy + c->h) > sh && c->isfloating) c->y = sy + (sh / 2
/* See LICENSE file for copyright and license details. */
#include "dwm.h"
#include <stdlib.h>

unsigned int blw = 0;
Layout *lt = NULL;

/* static */

static unsigned int nlayouts = 0;
static unsigned int masterw = MASTERWIDTH;
static unsigned int nmaster = NMASTER;

static void
ban(Client *c) {
	if (c->isbanned)
		return;
	XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
	c->isbanned = True;
}

static void
unban(Client *c) {
	if (!c->isbanned)
		return;
	XMoveWindow(dpy, c->win, c->x, c->y);
	c->isbanned = False;
}

static void
tile(void) {
	unsigned int i, n, nx, ny, nw, nh, mw, mh, tw, th;
	Client *c;

	for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
		n++;
	/* window geoms */
	mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1);
	mw = (n > nmaster) ? (waw * masterw) / 1000 : waw;
	th = (n > nmaster) ? wah / (n - nmaster) : 0;
	tw = waw - mw;

	for(i = 0, c = clients; c; c = c->next)
		if(isvisible(c)) {
			unban(c);
			if(c->isfloating)
				continue;
			c->ismax = False;
			nx = wax;
			ny = way;
			if(i < nmaster) {
				ny += i * mh;
				nw = mw - 2 * c->border;
				nh = mh;
				if(i + 1 == (n < nmaster ? n : nmaster)) /* remainder */
					nh = wah - mh * i;
				nh -= 2 * c->border;
			}
			else {  /* tile window */
				nx += mw;
				nw = tw - 2 * c->border;
				if(th > 2 * c->border) {
					ny += (i - nmaster) * th;
					nh = th;
					if(i + 1 == n) /* remainder */
						nh = wah - th * (i - nmaster);
					nh -= 2 * c->border;
				}
				else /* fallback if th <= 2 * c->border */
					nh = wah - 2 * c->border;
			}
			resize(c, nx, ny, nw, nh, False);
			i++;
		}
		else
			ban(c);
	focus(NULL);
	restack();
}

LAYOUTS

/* extern */

void
floating(void) {
	Client *c;

	for(c = clients; c; c = c->next)
		if(isvisible(c)) {
			unban(c);
			resize(c, c->x, c->y, c->w, c->h, True);
		}
		else
			ban(c);
	focus(NULL);
	restack();
}

void
focusclient(const char *arg) {
	Client *c;
   
	if(!sel || !arg)
		return;
	if(atoi(arg) < 0) {
		for(c = sel->prev; c && !isvisible(c); c = c->prev);
		if(!c) {
			for(c = clients; c && c->next; c = c->next);