JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
replaced isvisible with a macro
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index 8dc88fe..908c464 100644 (file)
--- a/dwm.c
+++ b/dwm.c
@@ -52,6 +52,7 @@
 #define MAXTAGLEN       16
 #define MOUSEMASK       (BUTTONMASK|PointerMotionMask)
 #define TAGMASK         ((int)((1LL << LENGTH(tags)) - 1))
+#define VISIBLE(x)      ((x)->tags & tagset[seltags])
 
 /* enums */
 enum { CurNormal, CurResize, CurMove, CurLast };        /* cursor */
@@ -61,6 +62,7 @@ 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];
@@ -68,7 +70,7 @@ struct Client {
        int basew, baseh, incw, inch, maxw, maxh, minw, minh;
        int minax, maxax, minay, maxay;
        long flags;
-       uint bw, oldbw;
+       int bw, oldbw;
        Bool isbanned, isfixed, isfloating, isurgent;
        uint tags;
        Client *next;
@@ -79,8 +81,8 @@ struct Client {
 
 typedef struct {
        int x, y, w, h;
-       unsigned long norm[ColLast];
-       unsigned long sel[ColLast];
+       ulong norm[ColLast];
+       ulong sel[ColLast];
        Drawable drawable;
        GC gc;
        struct {
@@ -93,7 +95,7 @@ typedef struct {
 } DC; /* draw context */
 
 typedef struct {
-       unsigned long mod;
+       uint mod;
        KeySym keysym;
        void (*func)(const void *arg);
        const void *arg;
@@ -129,9 +131,8 @@ void destroynotify(XEvent *e);
 void detach(Client *c);
 void detachstack(Client *c);
 void drawbar(void);
-void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
-void drawtext(const char *text, unsigned long col[ColLast], Bool invert);
-void *emallocz(uint size);
+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);
@@ -140,7 +141,7 @@ void focusin(XEvent *e);
 void focusnext(const void *arg);
 void focusprev(const void *arg);
 Client *getclient(Window w);
-unsigned long getcolor(const char *colstr);
+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);
@@ -149,7 +150,6 @@ void initfont(const char *fontstr);
 Bool isoccupied(uint t);
 Bool isprotodel(Client *c);
 Bool isurgent(uint t);
-Bool isvisible(Client *c);
 void keypress(XEvent *e);
 void killclient(const void *arg);
 void manage(Window w, XWindowAttributes *wa);
@@ -233,11 +233,10 @@ Window root, barwin;
 /* configuration, allows nested code to access above variables */
 #include "config.h"
 
-/* check if all tags will fit into a uint bitarray. */
-static char tags_is_a_sign_that_your_IQ[sizeof(int) * 8 < LENGTH(tags) ? -1 : 1];
+/* 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;
@@ -268,7 +267,7 @@ arrange(void) {
        Client *c;
 
        for(c = clients; c; c = c->next)
-               if(isvisible(c)) {
+               if(VISIBLE(c)) {
                        unban(c);
                        if(!lt->arrange || c->isfloating)
                                resize(c, c->x, c->y, c->w, c->h, True);
@@ -445,7 +444,7 @@ configurerequest(XEvent *e) {
                        if((ev->value_mask & (CWX|CWY))
                        && !(ev->value_mask & (CWWidth|CWHeight)))
                                configure(c);
-                       if(isvisible(c))
+                       if(VISIBLE(c))
                                XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
                }
                else
@@ -498,7 +497,7 @@ drawbar(void) {
        Client *c;
 
        dc.x = 0;
-       for(c = stack; c && !isvisible(c); c = c->snext);
+       for(c = stack; c && !VISIBLE(c); c = c->snext);
        for(i = 0; i < LENGTH(tags); i++) {
                dc.w = textw(tags[i]);
                if(tagset[seltags] & 1 << i) {
@@ -539,7 +538,7 @@ drawbar(void) {
 }
 
 void
-drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
+drawsquare(Bool filled, Bool empty, Bool invert, ulong col[ColLast]) {
        int x;
        XGCValues gcv;
        XRectangle r = { dc.x, dc.y, dc.w, dc.h };
@@ -560,7 +559,7 @@ drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
 }
 
 void
-drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
+drawtext(const char *text, ulong col[ColLast], Bool invert) {
        int x, y, w, h;
        uint len, olen;
        XRectangle r = { dc.x, dc.y, dc.w, dc.h };
@@ -596,15 +595,6 @@ drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
                XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
 }
 
-void *
-emallocz(uint size) {
-       void *res = calloc(1, size);
-
-       if(!res)
-               eprint("fatal: could not malloc() %u bytes\n", size);
-       return res;
-}
-
 void
 enternotify(XEvent *e) {
        Client *c;
@@ -638,8 +628,8 @@ expose(XEvent *e) {
 
 void
 focus(Client *c) {
-       if(!c || (c && !isvisible(c)))
-               for(c = stack; c && !isvisible(c); c = c->snext);
+       if(!c || (c && !VISIBLE(c)))
+               for(c = stack; c && !VISIBLE(c); c = c->snext);
        if(sel && sel != c) {
                grabbuttons(sel, False);
                XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
@@ -673,9 +663,9 @@ focusnext(const void *arg) {
 
        if(!sel)
                return;
-       for(c = sel->next; c && !isvisible(c); c = c->next);
+       for(c = sel->next; c && !VISIBLE(c); c = c->next);
        if(!c)
-               for(c = clients; c && !isvisible(c); c = c->next);
+               for(c = clients; c && !VISIBLE(c); c = c->next);
        if(c) {
                focus(c);
                restack();
@@ -688,10 +678,10 @@ focusprev(const void *arg) {
 
        if(!sel)
                return;
-       for(c = sel->prev; c && !isvisible(c); c = c->prev);
+       for(c = sel->prev; c && !VISIBLE(c); c = c->prev);
        if(!c) {
                for(c = clients; c && c->next; c = c->next);
-               for(; c && !isvisible(c); c = c->prev);
+               for(; c && !VISIBLE(c); c = c->prev);
        }
        if(c) {
                focus(c);
@@ -707,7 +697,7 @@ getclient(Window w) {
        return c;
 }
 
-unsigned long
+ulong
 getcolor(const char *colstr) {
        Colormap cmap = DefaultColormap(dpy, screen);
        XColor color;
@@ -722,7 +712,7 @@ getstate(Window w) {
        int format, status;
        long result = -1;
        unsigned char *p = NULL;
-       unsigned long n, extra;
+       ulong n, extra;
        Atom real;
 
        status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
@@ -883,11 +873,6 @@ isurgent(uint t) {
        return False;
 }
 
-Bool
-isvisible(Client *c) {
-       return c->tags & tagset[seltags];
-}
-
 void
 keypress(XEvent *e) {
        uint i;
@@ -931,7 +916,8 @@ manage(Window w, XWindowAttributes *wa) {
        Window trans;
        XWindowChanges wc;
 
-       c = emallocz(sizeof(Client));
+       if(!(c = calloc(1, sizeof(Client))))
+               eprint("fatal: could not calloc() %u bytes\n", sizeof(Client));
        c->win = w;
 
        /* geometry */
@@ -1052,7 +1038,7 @@ movemouse(Client *c) {
 
 Client *
 nexttiled(Client *c) {
-       for(; c && (c->isfloating || !isvisible(c)); c = c->next);
+       for(; c && (c->isfloating || !VISIBLE(c)); c = c->next);
        return c;
 }
 
@@ -1216,7 +1202,7 @@ restack(void) {
                wc.stack_mode = Below;
                wc.sibling = barwin;
                for(c = stack; c; c = c->snext)
-                       if(!c->isfloating && isvisible(c)) {
+                       if(!c->isfloating && VISIBLE(c)) {
                                XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
                                wc.sibling = c->win;
                        }
@@ -1547,14 +1533,6 @@ togglelayout(const void *arg) {
 
 void
 toggletag(const void *arg) {
-       int i, m = *(int *)arg;
-       for(i = 0; i < sizeof(int) * 8; i++)
-               fputc(m & 1 << i ? '1' : '0', stdout);
-       puts("");
-       for(i = 0; i < sizeof(int) * 8; i++)
-               fputc(TAGMASK & 1 << i ? '1' : '0', stdout);
-       puts("aaa");
-
        if(sel && (sel->tags ^ ((*(int *)arg) & TAGMASK))) {
                sel->tags ^= (*(int *)arg) & TAGMASK;
                arrange();