JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
removed the string-based setgeom approach, introduced a new Geom type instead and...
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index 5d93d34..f83e800 100644 (file)
--- a/dwm.c
+++ b/dwm.c
 #define LENGTH(x)              (sizeof x / sizeof x[0])
 #define MAXTAGLEN              16
 #define MOUSEMASK              (BUTTONMASK|PointerMotionMask)
+#define DEFGEOM(GEONAME,BX,BY,BW,WX,WY,WW,WH,MX,MY,MW,MH,TX,TY,TW,TH,MOX,MOY,MOW,MOH) \
+void GEONAME(void) { \
+       bx = (BX); by = (BY); bw = (BW); \
+       wx = (WX); wy = (WY); ww = (WW); wh = (WH); \
+       mx = (MX); my = (MY); mw = (MW); mh = (MH); \
+       tx = (TX); ty = (TY); tw = (TW); th = (TH); \
+       mox = (MOX); moy = (MOY); mow = (MOW); moh = (MOH); \
+}
 
 /* enums */
 enum { CurNormal, CurResize, CurMove, CurLast };       /* cursor */
@@ -86,6 +94,11 @@ typedef struct {
 } DC; /* draw context */
 
 typedef struct {
+       const char *symbol;
+       void (*apply)(void);
+} Geom;
+
+typedef struct {
        unsigned long mod;
        KeySym keysym;
        void (*func)(const char *arg);
@@ -136,7 +149,6 @@ void focusnext(const char *arg);
 void focusprev(const char *arg);
 Client *getclient(Window w);
 unsigned long getcolor(const char *colstr);
-double getdouble(const char *s);
 long getstate(Window w);
 Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
 void grabbuttons(Client *c, Bool focused);
@@ -225,6 +237,7 @@ Client *stack = NULL;
 Cursor cursor[CurLast];
 Display *dpy;
 DC dc = {0};
+Geom *geom = NULL;
 Layout *lt = NULL;
 Window root, barwin;
 
@@ -410,11 +423,8 @@ void
 configurenotify(XEvent *e) {
        XConfigureEvent *ev = &e->xconfigure;
 
-       if(ev->window == root && (ev->width != sw || ev->height != sh)) {
+       if(ev->window == root && (ev->width != sw || ev->height != sh))
                setgeom(NULL);
-               updatebarpos();
-               arrange();
-       }
 }
 
 void
@@ -1391,93 +1401,17 @@ setclientstate(Client *c, long state) {
                        PropModeReplace, (unsigned char *)data, 2);
 }
 
-/**
- * Idea:
- *
- * having a geom syntax as follows, which is interpreted as integer.
- *
- * [-,+][<0..n>|<W,H,B>]
- *
- *
- * B = bar height, W = DisplayWidth(), H = DisplayHeight()
- *
- * -/+/* /: is relative to current
- *
- * Then we would come down with <bx>,<by>,<bw>,<bh>,...
- *
- * "0 0 W B 0 0 W W N E B,W,B,
- *
- *
- */
-
-double
-getdouble(const char *s) {
-       char *endp;
-       double result = 0;
-
-       fprintf(stderr, "getdouble '%s'\n", s);
-       switch(*s) {
-       default: 
-               result = strtod(s, &endp);
-               if(s == endp || *endp != 0)
-                       result = strtol(s, &endp, 0);
-               break;
-       case 'B': result = dc.font.height + 2; break;
-       case 'W': result = sw; break;
-       case 'H': result = sh; break;
-       }
-       fprintf(stderr, "getdouble returns '%f'\n", result);
-       return result;
-}
-
 void
 setgeom(const char *arg) {
-       static const char *lastArg = NULL;
-       char op, *s, *e, *p;
-       double val;
-       int i, *map[] = { &bx,  &by,  &bw,  &bh,
-                         &wx,  &wy,  &ww,  &wh,
-                         &mx,  &my,  &mw,  &mh,
-                         &tx,  &ty,  &tw,  &th,
-                         &mox, &moy, &mow, &moh };
+       unsigned int i;
 
-       if(!arg)
-               arg = lastArg;
-       else
-               lastArg = arg;
-       if(!lastArg)
+       for(i = 0; arg && i < LENGTH(geoms); i++)
+               if(!strcmp(geoms[i].symbol, arg))
+                       break;
+       if(i == LENGTH(geoms))
                return;
-       strncpy(buf, arg, sizeof buf);
-       for(i = 0, e = s = buf; e && *e; e++)
-               if(*e == ' ') {
-                       *e = 0;
-                       fprintf(stderr, "next geom arg='%s'\n", s);
-                       op = 0;
-                       /* check if there is an operator */
-                       for(p = s; *p && *p != '-' && *p != '+' && *p != '*' && *p != ':'; p++);
-                       if(*p) {
-                               op = *p;
-                               *p = 0;
-                       }
-                       val = getdouble(s);
-                       fprintf(stderr, "val1: %d\n", val);
-                       if(p > s) { /* intermediate operand, e.g. H-B */
-                               *(map[i]) = val;
-                               s = ++p;
-                               val = getdouble(s);
-                               fprintf(stderr, "val2: %d\n", val);
-                       }
-                       switch(op) {
-                       default: *(map[i])   = val; break;
-                       case '-': *(map[i]) -= val; break;
-                       case '+': *(map[i]) += val; break;
-                       case '*': *(map[i]) *= val; break;
-                       case ':': if(val != 0) *(map[i]) /= val; break;
-                       }
-                       fprintf(stderr, "map[i]='%d'\n", val);
-                       s = ++e;
-                       i++;
-               }
+       geom = &geoms[i];
+       geom->apply();
        updatebarpos();
        arrange();
 }
@@ -1516,12 +1450,14 @@ setup(void) {
        root = RootWindow(dpy, screen);
        initfont(FONT);
 
-       /* apply default dimensions */
+       /* apply default geometry */
        sx = 0;
        sy = 0;
        sw = DisplayWidth(dpy, screen);
        sh = DisplayHeight(dpy, screen);
-       setgeom(GEOMETRY);
+       bh = dc.font.height + 2;
+       geom = &geoms[0];
+       geom->apply();
 
        /* init atoms */
        wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);