misparsing of rgb:4/5/6 colors
[mikachu/openbox.git] / tools / themetoxml / themetoxml.c
index c03bd29..9b688c5 100644 (file)
 #include <ctype.h>
 #include <string.h>
 
-static gboolean read_int(XrmDatabase db, gchar *rname, gint *value);
-static gboolean read_string(XrmDatabase db, gchar *rname, gchar **value);
-static gboolean read_color(XrmDatabase db, gchar *rname,
+static gboolean read_int(XrmDatabase db, const gchar *rname, gint *value);
+static gboolean read_string(XrmDatabase db, const gchar *rname,
+                            const gchar **value);
+static gboolean read_color(XrmDatabase db, const gchar *rname,
                            gint *r, gint *g, gint *b);
 
-static int parse_inline_number(char *p)
+static int parse_inline_number(const char *p)
 {
     int neg = 1;
     int res = 0;
@@ -45,7 +46,7 @@ static int parse_inline_number(char *p)
     return res;
 }
 
-static gchar *create_class_name(gchar *rname)
+static gchar *create_class_name(const gchar *rname)
 {
     gchar *rclass = g_strdup(rname);
     gchar *p = rclass;
@@ -60,7 +61,7 @@ static gchar *create_class_name(gchar *rname)
     return rclass;
 }
 
-static gboolean read_int(XrmDatabase db, gchar *rname, gint *value)
+static gboolean read_int(XrmDatabase db, const gchar *rname, gint *value)
 {
     gboolean ret = FALSE;
     gchar *rclass = create_class_name(rname);
@@ -78,7 +79,8 @@ static gboolean read_int(XrmDatabase db, gchar *rname, gint *value)
     return ret;
 }
 
-static gboolean read_string(XrmDatabase db, gchar *rname, gchar **value)
+static gboolean read_string(XrmDatabase db, const gchar *rname,
+                            const gchar **value)
 {
     gboolean ret = FALSE;
     gchar *rclass = create_class_name(rname);
@@ -87,8 +89,7 @@ static gboolean read_string(XrmDatabase db, gchar *rname, gchar **value)
   
     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
         retvalue.addr != NULL) {
-        *value = retvalue.addr;
-        g_strstrip(*value);
+        *value = g_strstrip(retvalue.addr);
         ret = TRUE;
     }
 
@@ -104,7 +105,7 @@ static gchar hextodec(gchar h)
     return -1;
 }
 
-static gboolean parse_color(gchar *c, gint *r, gint *g, gint *b)
+static gboolean parse_color(const gchar *c, gint *r, gint *g, gint *b)
 {
     int dig1, dig2, i, color[3];
     int len = strlen(c);
@@ -113,7 +114,7 @@ static gboolean parse_color(gchar *c, gint *r, gint *g, gint *b)
         c += 4;
         for (i = 0; i < 3; ++i) {
             dig1 = hextodec(c[0]);
-            if (c[1] == '/') { dig2 = dig1; c+=2; }
+            if (c[1] == '/' || (c[1] == '\0' && i == 2)) { dig2 = dig1; c+=2; }
             else { dig2 = hextodec(c[1]); c+=3; }
 
             if (dig1 < 0 || dig2 < 0) return FALSE;
@@ -150,7 +151,7 @@ static gboolean parse_color(gchar *c, gint *r, gint *g, gint *b)
     return FALSE;
 }
 
-static gboolean read_color(XrmDatabase db, gchar *rname,
+static gboolean read_color(XrmDatabase db, const gchar *rname,
                            gint *r, gint *g, gint *b)
 {
     gboolean ret = FALSE;
@@ -279,17 +280,18 @@ int main(int argc, char **argv)
 {
     XrmDatabase db;
     int i,j,k;
-    gchar *s;
+    const gchar *s;
     int ret = 0;
 
-    if (argc > 1) {
+    if (argc > 2) {
         fprintf(stderr, "themetoxml (C) 2007 Dana Jansens\n"
-                "This tool takes an older Openbox3 themerc file on stdin,"
-                " and gives back the\n"
-                "theme in the newer themerc.xml XML style.\n");
+                "This tool takes an older Openbox3 themerc file and prints "
+                "out the theme in\n"
+                "theme in the newer XML (themerc.xml) style.\n");
         return 0;
     }
-    {
+
+    if (argc == 1 || !strcmp(argv[1], "-")) {
         gchar *buf = g_new(gchar, 1000);
         gint sz = 1000;
         gint r = 0, rthis;
@@ -309,10 +311,18 @@ int main(int argc, char **argv)
         }
         g_free(buf);
     }
+    else if (argc == 2) {
+        if ((db = XrmGetFileDatabase(argv[1])) == NULL) {
+            fprintf(stderr, "Unable to read the database from %s\n", argv[1]);
+            return 1;
+        }
+    }
+
 
     doc = xmlNewDoc((const xmlChar*) "1.0");
     xmlDocSetRootElement
         (doc,(root = xmlNewNode(NULL, (const xmlChar*)"openbox_theme")));
+    xmlSetProp(root, (const xmlChar*)"engine", (const xmlChar*)"box");
     xmlSetProp(root, (const xmlChar*)"version", (const xmlChar*)"1");
     xmlSetProp(root, (const xmlChar*)"xmlns",
                (const xmlChar*)"http://openbox.org/themerc");
@@ -406,11 +416,11 @@ int main(int argc, char **argv)
 
     if (read_color(db, "window.active.button.toggled.image.color",
                    &i, &j, &k))
-        COLOR5("window","active","buttons","toggled","image",i,j,k,255);
+        COLOR5("window","active","buttons","toggled-unpressed","image",i,j,k,255);
 
     if (read_color(db, "window.inactive.button.toggled.image.color",
                    &i, &j, &k))
-        COLOR5("window","inactive","buttons","toggled","image",i,j,k,255);
+        COLOR5("window","inactive","buttons","toggled-unpressed","image",i,j,k,255);
 
     if (read_color(db, "menu.title.text.color",
                    &i, &j, &k))
@@ -421,8 +431,12 @@ int main(int argc, char **argv)
         COLOR3("menu","inactive","primary",i,j,k,255);
 
     if (read_color(db, "menu.items.disabled.text.color",
-                   &i, &j, &k))
+                   &i, &j, &k)) {
         COLOR3("menu","disabled","primary",i,j,k,255);
+        read_color(db, "menu.items.activedisabled.text.color",
+                   &i, &j, &k); /* read this if we can */
+        COLOR4("menu","activedisabled","text","primary",i,j,k,255);
+    }
 
     if (read_color(db, "menu.items.active.text.color",
                    &i, &j, &k))
@@ -438,6 +452,7 @@ int main(int argc, char **argv)
     APPEARANCE3("window.inactive.grip.bg", "window", "inactive", "grip");
     APPEARANCE2("menu.items.bg", "menu", "entries");
     APPEARANCE2("menu.items.active.bg", "menu", "active");
+    APPEARANCE2("menu.items.active.bg", "menu", "activedisabled");
     APPEARANCE2("menu.title.bg", "menu", "title");
 
     APPEARANCE4("window.active.button.disabled.bg",
@@ -449,9 +464,9 @@ int main(int argc, char **argv)
     APPEARANCE4("window.inactive.button.pressed.bg",
                 "window", "inactive", "buttons", "pressed");
     APPEARANCE4("window.active.button.toggled.bg",
-                "window", "active", "buttons", "toggled");
+                "window", "active", "buttons", "toggled-unpressed");
     APPEARANCE4("window.inactive.button.toggled.bg",
-                "window", "inactive", "buttons", "toggled");
+                "window", "inactive", "buttons", "toggled-unpressed");
     APPEARANCE4("window.active.button.unpressed.bg",
                 "window", "active", "buttons", "unpressed");
     APPEARANCE4("window.inactive.button.unpressed.bg",
@@ -477,7 +492,7 @@ int main(int argc, char **argv)
         {
             i = parse_inline_number(p + strlen("shadowtint="));
             j = (i > 0 ? 0 : 255);
-            i = ABS(i);
+            i = ABS(i*255/100);
             COLOR6("window","active","label","text","shadow","primary",
                    j,j,j,i);
         }
@@ -499,7 +514,7 @@ int main(int argc, char **argv)
         {
             i = parse_inline_number(p + strlen("shadowtint="));
             j = (i > 0 ? 0 : 255);
-            i = ABS(i);
+            i = ABS(i*255/100);
             COLOR6("window","inactive","label","text","shadow","primary",
                    j,j,j,i);
         }
@@ -519,12 +534,12 @@ int main(int argc, char **argv)
         {
             i = parse_inline_number(p + strlen("shadowtint="));
             j = (i > 0 ? 0 : 255);
-            i = ABS(i);
+            i = ABS(i*255/100);
             COLOR5("menu","title","text","shadow","primary",j,j,j,i);
         }
     }
 
-    if (read_string(db, "menu.items.text.font", &s)) {
+    if (read_string(db, "menu.items.font", &s)) {
         char *p;
         if (strstr(s, "shadow=y")) {
             if ((p = strstr(s, "shadowoffset=")))
@@ -537,15 +552,18 @@ int main(int argc, char **argv)
             ATTR5("menu","active","text","shadow","offset","y",NUM(i));
             ATTR4("menu","disabled","shadow","offset","x",NUM(i));
             ATTR4("menu","disabled","shadow","offset","y",NUM(i));
+            ATTR5("menu","activedisabled","text","shadow","offset","x",NUM(i));
+            ATTR5("menu","activedisabled","text","shadow","offset","y",NUM(i));
         }
         if ((p = strstr(s, "shadowtint=")))
         {
             i = parse_inline_number(p + strlen("shadowtint="));
             j = (i > 0 ? 0 : 255);
-            i = ABS(i);
+            i = ABS(i*255/100);
             COLOR4("menu","inactive","shadow","primary",j,j,j,i);
             COLOR5("menu","active","text","shadow","primary",j,j,j,i);
             COLOR4("menu","disabled","shadow","primary",j,j,j,i);
+            COLOR5("menu","activedisabled","text","shadow","primary",j,j,j,i);
         }
     }