+#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 */
+
+/* typedefs */
+typedef unsigned int uint;
+typedef unsigned long ulong;
+typedef struct Client Client;
+struct Client {
+ char name[256];
+ int x, y, w, h;
+ int basew, baseh, incw, inch, maxw, maxh, minw, minh;
+ int minax, maxax, minay, maxay;
+ int bw, oldbw;
+ Bool isbanned, isfixed, isfloating, ismoved, isurgent;
+ uint tags;
+ Client *next;
+ Client *prev;
+ 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 void *arg);
+ const void *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 */
+void applyrules(Client *c);
+void arrange(void);
+void attach(Client *c);
+void attachstack(Client *c);
+void buttonpress(XEvent *e);
+void checkotherwm(void);
+void cleanup(void);
+void configure(Client *c);
+void configurenotify(XEvent *e);
+void configurerequest(XEvent *e);
+void destroynotify(XEvent *e);
+void detach(Client *c);
+void detachstack(Client *c);
+void drawbar(void);
+void drawsquare(Bool filled, Bool empty, Bool invert, ulong col[ColLast]);
+void drawtext(const char *text, ulong col[ColLast], Bool invert);
+void enternotify(XEvent *e);
+void eprint(const char *errstr, ...);
+void expose(XEvent *e);
+void focus(Client *c);
+void focusin(XEvent *e);
+void focusnext(const void *arg);
+void focusprev(const void *arg);
+Client *getclient(Window w);
+ulong getcolor(const char *colstr);
+long getstate(Window w);
+Bool gettextprop(Window w, Atom atom, char *text, uint size);
+void grabbuttons(Client *c, Bool focused);
+void grabkeys(void);
+void initfont(const char *fontstr);
+Bool isoccupied(uint t);
+Bool isprotodel(Client *c);
+Bool isurgent(uint t);
+void keypress(XEvent *e);
+void killclient(const void *arg);
+void manage(Window w, XWindowAttributes *wa);
+void mappingnotify(XEvent *e);
+void maprequest(XEvent *e);
+void movemouse(Client *c);
+Client *nexttiled(Client *c);
+void propertynotify(XEvent *e);
+void quit(const void *arg);
+void resize(Client *c, int x, int y, int w, int h, Bool sizehints);
+void resizemouse(Client *c);
+void restack(void);
+void run(void);
+void scan(void);
+void setclientstate(Client *c, long state);
+void setmfact(const void *arg);
+void setup(void);
+void spawn(const void *arg);
+void tag(const void *arg);
+uint textnw(const char *text, uint len);
+void tile(void);
+void togglebar(const void *arg);
+void togglefloating(const void *arg);
+void togglelayout(const void *arg);
+void togglemax(const void *arg);
+void toggletag(const void *arg);
+void toggleview(const void *arg);
+void unmanage(Client *c);
+void unmapnotify(XEvent *e);
+void updatebar(void);
+void updategeom(void);
+void updatesizehints(Client *c);
+void updatetitle(Client *c);
+void updatewmhints(Client *c);
+void view(const void *arg);
+int xerror(Display *dpy, XErrorEvent *ee);
+int xerrordummy(Display *dpy, XErrorEvent *ee);
+int xerrorstart(Display *dpy, XErrorEvent *ee);
+void zoom(const void *arg);
+
+/* variables */
+char stext[256];
+int screen, sx, sy, sw, sh;
+int by, bh, blw, wx, wy, ww, wh;
+uint seltags = 0;
+int (*xerrorxlib)(Display *, XErrorEvent *);
+uint numlockmask = 0;
+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
+};
+Atom wmatom[WMLast], netatom[NetLast];
+Bool ismax = False;
+Bool otherwm, readin;
+Bool running = True;
+uint tagset[] = {1, 1}; /* after start, first tag is selected */
+Client *clients = NULL;
+Client *sel = NULL;
+Client *stack = NULL;
+Cursor cursor[CurLast];
+Display *dpy;
+DC dc = {0};
+Layout layouts[];
+Layout *lt = layouts;
+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 */