JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
made the string-based setgeom working
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index 0894c65..4a9ca82 100644 (file)
--- a/dwm.c
+++ b/dwm.c
@@ -96,16 +96,18 @@ typedef struct {
        const char *symbol;
        void (*arrange)(void);
        Bool isfloating;
-} Layout; /* TODO: layout should keep an auxilliary pointer to its Geometry,
-            instead of having all those layout specific vars globally */
+} Layout; 
 
 typedef struct {
-       const char *prop;
+       const char *class;
+       const char *instance;
+       const char *title;
        const char *tag;
        Bool isfloating;
 } Rule;
 
 /* function declarations */
+void applygeom(const char *arg);
 void applyrules(Client *c);
 void arrange(void);
 void attach(Client *c);
@@ -135,6 +137,7 @@ 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);
@@ -162,7 +165,7 @@ void restack(void);
 void run(void);
 void scan(void);
 void setclientstate(Client *c, long state);
-void setdefaultgeoms(void);
+void setgeom(const char *arg);
 void setlayout(const char *arg);
 void setup(void);
 void spawn(const char *arg);
@@ -225,7 +228,6 @@ Display *dpy;
 DC dc = {0};
 Layout *lt = NULL;
 Window root, barwin;
-void (*setgeoms)(void) = setdefaultgeoms;
 
 /* configuration, allows nested code to access above variables */
 #include "config.h"
@@ -235,6 +237,55 @@ static Bool tmp[LENGTH(tags)];
 /* function implementations */
 
 void
+applygeometry(const char *arg) {
+       static const char *lastArg = NULL;
+       char delim, 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 };
+
+       if(!arg)
+               arg = lastArg;
+       else
+               lastArg = arg;
+       if(!lastArg)
+               return;
+       strncpy(buf, arg, sizeof buf);
+       for(i = 0, e = s = buf; i < LENGTH(map) && e; e++)
+               if(*e == ' ' || *e == 0) {
+                       delim = *e;
+                       *e = 0;
+                       op = 0;
+                       /* check if there is an operator */
+                       for(p = s; p < e && *p != '-' && *p != '+' && *p != '*'; p++);
+                       if(*p) {
+                               op = *p;
+                               *p = 0;
+                       }
+                       val = getdouble(s);
+                       if(op && p > s) { /* intermediate operand, e.g. H-B */
+                               *(map[i]) = (int)val;
+                               s = ++p;
+                               val = getdouble(s);
+                       }
+                       switch(op) {
+                       default:  *(map[i])  = (int)val; break;
+                       case '-': *(map[i]) -= (int)val; break;
+                       case '+': *(map[i]) += (int)val; break;
+                       case '*': *(map[i])  = (int)(((double)*(map[i])) * val); break;
+                       }
+                       if(delim == 0)
+                               e = NULL;
+                       else
+                               s = ++e;
+                       i++;
+               }
+}
+
+void
 applyrules(Client *c) {
        unsigned int i;
        Bool matched = False;
@@ -245,9 +296,9 @@ applyrules(Client *c) {
        XGetClassHint(dpy, c->win, &ch);
        for(i = 0; i < LENGTH(rules); i++) {
                r = &rules[i];
-               if(strstr(c->name, r->prop)
-               || (ch.res_class && strstr(ch.res_class, r->prop))
-               || (ch.res_name && strstr(ch.res_name, r->prop)))
+               if(strstr(c->name, r->title)
+               || (ch.res_class && r->class && strstr(ch.res_class, r->class))
+               || (ch.res_name && r->instance && strstr(ch.res_name, r->instance)))
                {
                        c->isfloating = r->isfloating;
                        if(r->tag) {
@@ -409,11 +460,8 @@ void
 configurenotify(XEvent *e) {
        XConfigureEvent *ev = &e->xconfigure;
 
-       if(ev->window == root && (ev->width != sw || ev->height != sh)) {
-               setgeoms();
-               updatebarpos();
-               arrange();
-       }
+       if(ev->window == root && (ev->width != sw || ev->height != sh))
+               setgeom(NULL);
 }
 
 void
@@ -1052,7 +1100,7 @@ maprequest(XEvent *e) {
 }
 
 void
-monocle(void) { 
+monocle(void) {
        Client *c;
 
        for(c = clients; c; c = c->next)
@@ -1390,44 +1438,29 @@ setclientstate(Client *c, long state) {
                        PropModeReplace, (unsigned char *)data, 2);
 }
 
-void
-setdefaultgeoms(void) {
-
-       /* screen dimensions */
-       sx = 0;
-       sy = 0;
-       sw = DisplayWidth(dpy, screen);
-       sh = DisplayHeight(dpy, screen);
-
-       /* bar position */
-       bx = sx;
-       by = sy;
-       bw = sw;
-       bh = dc.font.height + 2;
-
-       /* window area */
-       wx = sx;
-       wy = sy + bh;
-       ww = sw;
-       wh = sh - bh;
-
-       /* master area */
-       mx = wx;
-       my = wy;
-       mw = ((float)sw) * 0.55;
-       mh = wh;
-
-       /* tile area */
-       tx = mx + mw;
-       ty = wy;
-       tw = ww - mw;
-       th = wh;
+double
+getdouble(const char *s) {
+       char *endp;
+       double result = 0;
+
+       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;
+       }
+       return result;
+}
 
-       /* monocle area */
-       mox = wx;
-       moy = wy;
-       mow = ww;
-       moh = wh;
+void
+setgeom(const char *arg) {
+       applygeometry(arg);
+       updatebarpos();
+       arrange();
 }
 
 void
@@ -1464,8 +1497,12 @@ setup(void) {
        root = RootWindow(dpy, screen);
        initfont(FONT);
 
-       /* apply default geometries */
-       setgeoms();
+       /* apply default dimensions */
+       sx = 0;
+       sy = 0;
+       sw = DisplayWidth(dpy, screen);
+       sh = DisplayHeight(dpy, screen);
+       applygeometry(GEOMETRY);
 
        /* init atoms */
        wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);