JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
removed emallocz
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index 5138752..483641c 100644 (file)
--- a/dwm.c
+++ b/dwm.c
@@ -61,6 +61,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 +69,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 +80,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 +94,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 +130,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 +140,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);
@@ -156,7 +156,7 @@ void manage(Window w, XWindowAttributes *wa);
 void mappingnotify(XEvent *e);
 void maprequest(XEvent *e);
 void movemouse(Client *c);
-Client *nextunfloating(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);
@@ -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;
@@ -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;
@@ -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],
@@ -931,7 +921,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 */
@@ -1051,7 +1042,7 @@ movemouse(Client *c) {
 }
 
 Client *
-nextunfloating(Client *c) {
+nexttiled(Client *c) {
        for(; c && (c->isfloating || !isvisible(c)); c = c->next);
        return c;
 }
@@ -1464,12 +1455,12 @@ tile(void) {
        uint i, n;
        Client *c;
 
-       for(n = 0, c = nextunfloating(clients); c; c = nextunfloating(c->next), n++);
+       for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next), n++);
        if(n == 0)
                return;
 
        /* master */
-       c = nextunfloating(clients);
+       c = nexttiled(clients);
 
        if(n == 1)
                tileresize(c, wx, wy, ww - 2 * c->bw, wh - 2 * c->bw);
@@ -1487,7 +1478,7 @@ tile(void) {
        if(h < bh)
                h = th;
 
-       for(i = 0, c = nextunfloating(c->next); c; c = nextunfloating(c->next), i++) {
+       for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
                if(i + 1 == n) /* remainder */
                        tileresize(c, x, y, w - 2 * c->bw, (ty + th) - y - 2 * c->bw);
                else
@@ -1547,14 +1538,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();
@@ -1789,14 +1772,14 @@ void
 zoom(const void *arg) {
        Client *c = sel;
 
-       if(c == nextunfloating(clients))
-               if(!c || !(c = nextunfloating(c->next)))
+       if(!lt->arrange || sel->isfloating)
+               return;
+       if(c == nexttiled(clients))
+               if(!c || !(c = nexttiled(c->next)))
                        return;
-       if(lt->arrange == tile && !sel->isfloating) {
-               detach(c);
-               attach(c);
-               focus(c);
-       }
+       detach(c);
+       attach(c);
+       focus(c);
        arrange();
 }