add the themetoxml tool ! yay
[dana/openbox.git] / tools / themetoxml / themetoxml.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    themetoxml.c for the Openbox window manager
4    Copyright (c) 2007        Dana Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "rgb.h"
20
21 #include <X11/Xlib.h>
22 #include <X11/Xresource.h>
23 #include <glib.h>
24 #include <libxml/parser.h>
25 #include <unistd.h>
26 #include <ctype.h>
27 #include <string.h>
28
29 static gboolean read_int(XrmDatabase db, gchar *rname, gint *value);
30 static gboolean read_string(XrmDatabase db, gchar *rname, gchar **value);
31 static gboolean read_color(XrmDatabase db, gchar *rname,
32                            gint *r, gint *g, gint *b);
33
34 static int parse_inline_number(char *p)
35 {
36     int neg = 1;
37     int res = 0;
38     if (*p == '-') {
39         neg = -1;
40         ++p;
41     }
42     for (; isdigit(*p); ++p)
43         res = res * 10 + *p - '0';
44     res *= neg;
45     return res;
46 }
47
48 static gchar *create_class_name(gchar *rname)
49 {
50     gchar *rclass = g_strdup(rname);
51     gchar *p = rclass;
52
53     while (TRUE) {
54         *p = toupper(*p);
55         p = strchr(p+1, '.');
56         if (p == NULL) break;
57         ++p;
58         if (*p == '\0') break;
59     }
60     return rclass;
61 }
62
63 static gboolean read_int(XrmDatabase db, gchar *rname, gint *value)
64 {
65     gboolean ret = FALSE;
66     gchar *rclass = create_class_name(rname);
67     gchar *rettype, *end;
68     XrmValue retvalue;
69   
70     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
71         retvalue.addr != NULL) {
72         *value = (gint)strtol(retvalue.addr, &end, 10);
73         if (end != retvalue.addr)
74             ret = TRUE;
75     }
76
77     g_free(rclass);
78     return ret;
79 }
80
81 static gboolean read_string(XrmDatabase db, gchar *rname, gchar **value)
82 {
83     gboolean ret = FALSE;
84     gchar *rclass = create_class_name(rname);
85     gchar *rettype;
86     XrmValue retvalue;
87   
88     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
89         retvalue.addr != NULL) {
90         *value = retvalue.addr;
91         g_strstrip(*value);
92         ret = TRUE;
93     }
94
95     g_free(rclass);
96     return ret;
97 }
98
99 static gchar hextodec(gchar h)
100 {
101     if (h >= '0' && h <= '9') return h - '0';
102     else if (h >= 'a' && h <= 'f') return h - 'a' + 9;
103     else if (h >= 'A' && h <= 'F') return h - 'A' + 9;
104     return -1;
105 }
106
107 static gboolean parse_color(gchar *c, gint *r, gint *g, gint *b)
108 {
109     int dig1, dig2, i, color[3];
110     int len = strlen(c);
111
112     if (c[0] == '#' && (len != 4 && len != 7)) return FALSE;
113
114     if (c[0] != '#') {
115         int i;
116
117         for (i = 0; colornames[i].name != NULL; ++i) {
118             if (!strcmp(colornames[i].name, c)) {
119                 *r = colornames[i].r;
120                 *g = colornames[i].g;
121                 *b = colornames[i].b;
122                 return TRUE;
123             }
124         }
125         return FALSE;
126     }
127
128     c++;
129     for (i = 0; i < 3; ++i, c += (len == 4 ? 1 : 2)) {
130         dig2 = hextodec(c[1]);
131         if (len == 4) dig1 = dig2;
132         else dig1 = hextodec(c[0]);
133
134         if (dig1 < 0 || dig2 < 0) return FALSE;
135
136         color[i] = dig1*16 + dig2;
137     }
138     *r = color[0]; *g = color[1]; *b = color[2];
139     return TRUE;
140 }
141
142 static gboolean read_color(XrmDatabase db, gchar *rname,
143                            gint *r, gint *g, gint *b)
144 {
145     gboolean ret = FALSE;
146     gchar *rclass = create_class_name(rname);
147     gchar *rettype;
148     XrmValue retvalue;
149   
150     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
151         retvalue.addr != NULL) {
152         ret = parse_color(retvalue.addr, r, g, b);
153     }
154
155     g_free(rclass);
156     return ret;
157 }
158
159 xmlNodePtr go(xmlNodePtr node, const char *s)
160 {
161     xmlNodePtr p;
162
163     for (p = node->children; p; p = p->next) {
164         if (!xmlStrcasecmp(p->name, (const xmlChar*) s))
165             return p;
166     }
167     return xmlAddChild(node, xmlNewNode(NULL, (const xmlChar*) s));
168 }
169
170 static gchar number[20];
171 static inline gchar* NUM(int i) {
172     g_snprintf(number, 20, "%d", i); return number;
173 }
174 static xmlDocPtr doc;
175 static xmlNodePtr root;
176
177 #define GO1(a) (go(root, a))
178 #define GO2(a,b) (go(GO1(a), b))
179 #define GO3(a,b,c) (go(GO2(a, b), c))
180 #define GO4(a,b,c,d) (go(GO3(a, b, c), d))
181 #define GO5(a,b,c,d,e) (go(GO4(a, b, c, d), e))
182 #define GO6(a,b,c,d,e,f) (go(GO5(a, b, c, d, e), f))
183 #define GO7(a,b,c,d,e,f,g) (go(GO6(a, b, c, d, e, f), g))
184
185 #define CONT1(a,cont) (xmlNodeSetContent(GO1(a), (const xmlChar*)cont))
186 #define CONT2(a,b,cont) (xmlNodeSetContent(GO2(a,b), (const xmlChar*)cont))
187 #define CONT3(a,b,c,cont) (xmlNodeSetContent(GO3(a,b,c), (const xmlChar*)cont))
188 #define CONT4(a,b,c,d,cont) (xmlNodeSetContent(GO4(a,b,c,d), (const xmlChar*)cont))
189 #define CONT5(a,b,c,d,e,cont) (xmlNodeSetContent(GO5(a,b,c,d,e), (const xmlChar*)cont))
190 #define CONT6(a,b,c,d,e,f,cont) (xmlNodeSetContent(GO6(a,b,c,d,e,f), (const xmlChar*)cont))
191
192 #define ATTR1(a,name,cont) (xmlSetProp(GO1(a), (const xmlChar*)name, (const xmlChar*)cont))
193 #define ATTR2(a,b,name,cont) (xmlSetProp(GO2(a,b), (const xmlChar*)name, (const xmlChar*)cont))
194 #define ATTR3(a,b,c,name,cont) (xmlSetProp(GO3(a,b,c), (const xmlChar*)name, (const xmlChar*)cont))
195 #define ATTR4(a,b,c,d,name,cont) (xmlSetProp(GO4(a,b,c,d), (const xmlChar*)name, (const xmlChar*)cont))
196 #define ATTR5(a,b,c,d,e,name,cont) (xmlSetProp(GO5(a,b,c,d,e), (const xmlChar*)name, (const xmlChar*)cont))
197 #define ATTR6(a,b,c,d,e,f,name,cont) (xmlSetProp(GO6(a,b,c,d,e,f), (const xmlChar*)name, (const xmlChar*)cont))
198 #define ATTR7(a,b,c,d,e,f,g,name,cont) (xmlSetProp(GO7(a,b,c,d,e,f,g), (const xmlChar*)name, (const xmlChar*)cont))
199
200 #define APPCONT1(a,cont) (CONT2("appearance",a,cont))
201 #define APPCONT2(a,b,cont) (CONT3("appearance",a,b,cont))
202 #define APPCONT3(a,b,c,cont) (CONT4("appearance",a,b,c,cont))
203 #define APPCONT4(a,b,c,d,cont) (CONT5("appearance",a,b,c,d,cont))
204 #define APPCONT5(a,b,c,d,e,cont) (CONT6("appearance",a,b,c,d,e,cont))
205
206 #define APPATTR1(a,name,cont) (ATTR2("appearance",a,name,cont))
207 #define APPATTR2(a,b,name,cont) (ATTR3("appearance",a,b,name,cont))
208 #define APPATTR3(a,b,c,name,cont) (ATTR4("appearance",a,b,c,name,cont))
209 #define APPATTR4(a,b,c,d,name,cont) (ATTR5("appearance",a,b,c,d,name,cont))
210 #define APPATTR5(a,b,c,d,e,name,cont) (ATTR6("appearance",a,b,c,d,e,name,cont))
211 #define APPATTR6(a,b,c,d,e,f,name,cont) (ATTR7("appearance",a,b,c,d,e,f,name,cont))
212
213 #define COLOR1(a,R,G,B,A) (APPATTR1(a,"r",NUM(R)), \
214                            APPATTR1(a,"g",NUM(G)), \
215                            APPATTR1(a,"b",NUM(B)), \
216                            APPATTR1(a,"a",NUM(A)))
217 #define COLOR2(a,b,R,G,B,A) (APPATTR2(a,b,"r",NUM(R)), \
218                              APPATTR2(a,b,"g",NUM(G)), \
219                              APPATTR2(a,b,"b",NUM(B)), \
220                              APPATTR2(a,b,"a",NUM(A)))
221 #define COLOR3(a,b,c,R,G,B,A) (APPATTR3(a,b,c,"r",NUM(R)), \
222                                APPATTR3(a,b,c,"g",NUM(G)), \
223                                APPATTR3(a,b,c,"b",NUM(B)), \
224                                APPATTR3(a,b,c,"a",NUM(A)))
225 #define COLOR4(a,b,c,d,R,G,B,A) (APPATTR4(a,b,c,d,"r",NUM(R)), \
226                                  APPATTR4(a,b,c,d,"g",NUM(G)), \
227                                  APPATTR4(a,b,c,d,"b",NUM(B)), \
228                                  APPATTR4(a,b,c,d,"a",NUM(A)))
229 #define COLOR5(a,b,c,d,e,R,G,B,A) (APPATTR5(a,b,c,d,e,"r",NUM(R)), \
230                                    APPATTR5(a,b,c,d,e,"g",NUM(G)), \
231                                    APPATTR5(a,b,c,d,e,"b",NUM(B)), \
232                                    APPATTR5(a,b,c,d,e,"a",NUM(A)))
233 #define COLOR6(a,b,c,d,e,f,R,G,B,A) (APPATTR6(a,b,c,d,e,f,"r",NUM(R)), \
234                                      APPATTR6(a,b,c,d,e,f,"g",NUM(G)), \
235                                      APPATTR6(a,b,c,d,e,f,"b",NUM(B)), \
236                                      APPATTR6(a,b,c,d,e,f,"a",NUM(A)))
237
238 #define APPEARANCE2(res,a,b) \
239 { \
240     if (read_string(db, res, &s)) \
241         APPCONT3(a, b, "style", s); \
242     if (read_color(db, res".color", &i, &j, &k)) \
243         COLOR3(a, b, "primary", i, j, k, 255); \
244     if (read_color(db, res".colorTo", &i, &j, &k)) \
245         COLOR3(a, b, "secondary", i, j, k, 255); \
246     if (read_color(db, res".border.color", &i, &j, &k)) \
247         COLOR3(a, b, "border", i, j, k, 255); \
248     if (read_color(db, res".interlace.color", &i, &j, &k)) \
249         COLOR3(a, b, "interlace", i, j, k, 255); \
250 }
251
252 #define APPEARANCE3(res,a,b,c) \
253 { \
254     if (read_string(db, res, &s)) \
255         APPCONT4(a, b, c, "style", s); \
256     if (read_color(db, res".color", &i, &j, &k)) \
257         COLOR4(a, b, c, "primary", i, j, k, 255); \
258     if (read_color(db, res".colorTo", &i, &j, &k)) \
259         COLOR4(a, b, c, "secondary", i, j, k, 255); \
260     if (read_color(db, res".border.color", &i, &j, &k)) \
261         COLOR4(a, b, c, "border", i, j, k, 255); \
262     if (read_color(db, res".interlace.color", &i, &j, &k)) \
263         COLOR4(a, b, c, "interlace", i, j, k, 255); \
264 }
265
266 #define APPEARANCE4(res,a,b,c,d) \
267 { \
268     if (read_string(db, res, &s)) \
269         APPCONT5(a, b, c, d, "style", s); \
270     if (read_color(db, res".color", &i, &j, &k)) \
271         COLOR5(a, b, c, d, "primary", i, j, k, 255); \
272     if (read_color(db, res".colorTo", &i, &j, &k)) \
273         COLOR5(a, b, c, d, "secondary", i, j, k, 255); \
274     if (read_color(db, res".border.color", &i, &j, &k)) \
275         COLOR5(a, b, c, d, "border", i, j, k, 255); \
276     if (read_color(db, res".interlace.color", &i, &j, &k)) \
277         COLOR5(a, b, c, d, "interlace", i, j, k, 255); \
278 }
279
280 int main(int argc, char **argv)
281 {
282     XrmDatabase db;
283     int i,j,k;
284     gchar *s;
285     int ret = 0;
286
287     if (argc < 2) {
288         printf("Please specify an Openbox3 themerc file\n");
289         return 1;
290     }
291
292     if ((db = XrmGetFileDatabase(argv[1])) == NULL) {
293         printf("Unable to open the database from stdin\n");
294         return 1;
295     }
296
297     doc = xmlNewDoc((const xmlChar*) "1.0");
298     xmlDocSetRootElement
299         (doc,(root = xmlNewNode(NULL, (const xmlChar*)"openbox_theme")));
300
301     if (read_int(db, "window.handle.width", &i))
302         CONT2("dimensions", "handle", NUM(i));
303
304     if (read_int(db, "padding.width", &i)) {
305         ATTR2("dimensions", "padding", "horizontal", NUM(i));
306         ATTR2("dimensions", "padding", "vertical", NUM(i));
307     }
308     
309     if (read_int(db, "border.width", &i)) {
310         APPCONT3("window", "border", "width", NUM(i));
311         APPCONT3("menu", "border", "width", NUM(i));
312     }
313
314     if (read_color(db, "border.color", &i, &j, &k)) {
315         COLOR3("window", "border", "primary", i, j, k, 255);
316         COLOR3("menu", "border", "primary", i, j, k, 255);
317     }
318
319     if (read_int(db, "window.client.padding.width", &i))
320         APPCONT3("window", "clientborder", "width", NUM(i));
321
322     if (read_string(db, "window.label.text.justify", &s)) {
323         if (!g_ascii_strcasecmp(s, "right")) s = "right";
324         else if (!g_ascii_strcasecmp(s, "center")) s = "center";
325         else s = "left";
326         APPCONT5("window", "inactive", "label", "text", "justify", s);
327     }
328
329     if (read_string(db, "menu.title.text.justify", &s)) {
330         if (!g_ascii_strcasecmp(s, "right")) s = "right";
331         else if (!g_ascii_strcasecmp(s, "center")) s = "center";
332         else s = "left";
333         APPCONT4("menu", "title", "text", "justify", s);
334     }
335
336     if (read_int(db, "menu.overlap", &i))
337         APPCONT2("menu", "overlap", NUM(i));
338
339     if (read_color(db, "window.active.client.color", &i, &j, &k))
340         COLOR4("window","active","clientborder","primary",i,j,k,255);
341     
342     if (read_color(db, "window.inactive.client.color", &i, &j, &k))
343         COLOR4("window","inactive","clientborder","primary",i,j,k,255);
344
345     if (read_color(db, "window.active.label.text.color", &i, &j, &k))
346         COLOR5("window","active","label","text","primary",i,j,k,255);
347
348     if (read_color(db, "window.inactive.label.text.color", &i, &j, &k))
349         COLOR5("window","inactive","label","text","primary",i,j,k,255);
350
351     if (read_color(db, "window.active.button.unpressed.image.color",
352                    &i, &j, &k))
353         COLOR5("window","active","buttons","unpressed","image",i,j,k,255);
354
355     if (read_color(db, "window.inactive.button.unpressed.image.color",
356                    &i, &j, &k))
357         COLOR5("window","inactive","buttons","unpressed","image",i,j,k,255);
358
359     if (read_color(db, "window.active.button.pressed.image.color",
360                    &i, &j, &k))
361         COLOR5("window","active","buttons","pressed","image",i,j,k,255);
362
363     if (read_color(db, "window.inactive.button.pressed.image.color",
364                    &i, &j, &k))
365         COLOR5("window","inactive","buttons","pressed","image",i,j,k,255);
366
367     if (read_color(db, "window.active.button.disabled.image.color",
368                    &i, &j, &k))
369         COLOR5("window","active","buttons","disabled","image",i,j,k,255);
370
371     if (read_color(db, "window.inactive.button.disabled.image.color",
372                    &i, &j, &k))
373         COLOR5("window","inactive","buttons","disabled","image",i,j,k,255);
374
375     if (read_color(db, "window.active.button.hover.image.color",
376                    &i, &j, &k))
377         COLOR5("window","active","buttons","hover","image",i,j,k,255);
378
379     if (read_color(db, "window.inactive.button.hover.image.color",
380                    &i, &j, &k))
381         COLOR5("window","inactive","buttons","hover","image",i,j,k,255);
382
383     if (read_color(db, "window.active.button.toggled.image.color",
384                    &i, &j, &k))
385         COLOR5("window","active","buttons","toggled","image",i,j,k,255);
386
387     if (read_color(db, "window.inactive.button.toggled.image.color",
388                    &i, &j, &k))
389         COLOR5("window","inactive","buttons","toggled","image",i,j,k,255);
390
391     if (read_color(db, "menu.title.text.color",
392                    &i, &j, &k))
393         COLOR4("menu","title","text","primary",i,j,k,255);
394
395     if (read_color(db, "menu.items.text.color",
396                    &i, &j, &k))
397         COLOR4("menu","inactive","text","primary",i,j,k,255);
398
399     if (read_color(db, "menu.items.disabled.text.color",
400                    &i, &j, &k))
401         COLOR3("menu","disabled","primary",i,j,k,255);
402
403     if (read_color(db, "menu.items.active.text.color",
404                    &i, &j, &k))
405         COLOR4("menu","active","text","primary",i,j,k,255);
406
407     APPEARANCE3("window.active.title.bg", "window", "active", "titlebar");
408     APPEARANCE3("window.inactive.title.bg", "window", "inactive", "titlebar");
409     APPEARANCE3("window.active.label.bg", "window", "active", "label");
410     APPEARANCE3("window.inactive.label.bg", "window", "inactive", "label");
411     APPEARANCE3("window.active.handle.bg", "window", "active", "handle");
412     APPEARANCE3("window.inactive.handle.bg", "window", "inactive", "handle");
413     APPEARANCE3("window.active.grip.bg", "window", "active", "grip");
414     APPEARANCE3("window.inactive.grip.bg", "window", "inactive", "grip");
415     APPEARANCE2("menu.items.bg", "menu", "inactive");
416     APPEARANCE2("menu.items.active.bg", "menu", "active");
417     APPEARANCE2("menu.title.bg", "menu", "title");
418
419     APPEARANCE4("window.active.button.disabled.bg",
420                 "window", "active", "buttons", "disabled");
421     APPEARANCE4("window.inactive.button.disabled.bg",
422                 "window", "inactive", "buttons", "disabled");
423     APPEARANCE4("window.active.button.pressed.bg",
424                 "window", "active", "buttons", "pressed");
425     APPEARANCE4("window.inactive.button.pressed.bg",
426                 "window", "inactive", "buttons", "pressed");
427     APPEARANCE4("window.active.button.toggled.bg",
428                 "window", "active", "buttons", "toggled");
429     APPEARANCE4("window.inactive.button.toggled.bg",
430                 "window", "inactive", "buttons", "toggled");
431     APPEARANCE4("window.active.button.unpressed.bg",
432                 "window", "active", "buttons", "unpressed");
433     APPEARANCE4("window.inactive.button.unpressed.bg",
434                 "window", "inactive", "buttons", "unpressed");
435     APPEARANCE4("window.active.button.hover.bg",
436                 "window", "active", "buttons", "hover");
437     APPEARANCE4("window.inactive.button.hover.bg",
438                 "window", "inactive", "buttons", "hover");
439
440     if (read_string(db, "window.active.label.text.font", &s)) {
441         char *p;
442         if (strstr(s, "shadow=y")) {
443             if ((p = strstr(s, "shadowoffset=")))
444                 i = parse_inline_number(p + strlen("shadowoffset="));
445             else
446                 i = 1;
447             APPATTR6("window","active","label","text","shadow","offset",
448                      "x",NUM(i));
449             APPATTR6("window","active","label","text","shadow","offset",
450                      "y",NUM(i));
451         }
452         if ((p = strstr(s, "shadowtint=")))
453         {
454             i = parse_inline_number(p + strlen("shadowtint="));
455             j = (i > 0 ? 0 : 255);
456             i = ABS(i);
457             COLOR6("window","active","label","text","shadow","primary",
458                    j,j,j,i);
459         }
460     }
461
462     if (read_string(db, "window.inactive.label.text.font", &s)) {
463         char *p;
464         if (strstr(s, "shadow=y")) {
465             if ((p = strstr(s, "shadowoffset=")))
466                 i = parse_inline_number(p + strlen("shadowoffset="));
467             else
468                 i = 1;
469             APPATTR6("window","inactive","label","text","shadow","offset",
470                      "x",NUM(i));
471             APPATTR6("window","inactive","label","text","shadow","offset",
472                      "y",NUM(i));
473         }
474         if ((p = strstr(s, "shadowtint=")))
475         {
476             i = parse_inline_number(p + strlen("shadowtint="));
477             j = (i > 0 ? 0 : 255);
478             i = ABS(i);
479             COLOR6("window","inactive","label","text","shadow","primary",
480                    j,j,j,i);
481         }
482     }
483
484     if (read_string(db, "menu.title.text.font", &s)) {
485         char *p;
486         if (strstr(s, "shadow=y")) {
487             if ((p = strstr(s, "shadowoffset=")))
488                 i = parse_inline_number(p + strlen("shadowoffset="));
489             else
490                 i = 1;
491             APPATTR5("menu","title","text","shadow","offset","x",NUM(i));
492             APPATTR5("menu","title","text","shadow","offset","y",NUM(i));
493         }
494         if ((p = strstr(s, "shadowtint=")))
495         {
496             i = parse_inline_number(p + strlen("shadowtint="));
497             j = (i > 0 ? 0 : 255);
498             i = ABS(i);
499             COLOR5("menu","title","text","shadow","primary",j,j,j,i);
500         }
501     }
502
503     if (read_string(db, "menu.items.text.font", &s)) {
504         char *p;
505         if (strstr(s, "shadow=y")) {
506             if ((p = strstr(s, "shadowoffset=")))
507                 i = parse_inline_number(p + strlen("shadowoffset="));
508             else
509                 i = 1;
510             APPATTR5("menu","inactive","text","shadow","offset","x",NUM(i));
511             APPATTR5("menu","inactive","text","shadow","offset","y",NUM(i));
512             APPATTR5("menu","active","text","shadow","offset","x",NUM(i));
513             APPATTR5("menu","active","text","shadow","offset","y",NUM(i));
514             APPATTR5("menu","disabled","text","shadow","offset","x",NUM(i));
515             APPATTR5("menu","disabled","text","shadow","offset","y",NUM(i));
516         }
517         if ((p = strstr(s, "shadowtint=")))
518         {
519             i = parse_inline_number(p + strlen("shadowtint="));
520             j = (i > 0 ? 0 : 255);
521             i = ABS(i);
522             COLOR5("menu","inactive","text","shadow","primary",j,j,j,i);
523             COLOR5("menu","active","text","shadow","primary",j,j,j,i);
524             COLOR5("menu","disabled","text","shadow","primary",j,j,j,i);
525         }
526     }
527
528     if (xmlSaveFormatFile("-", doc, 1) < 0) {
529         printf("Error writing the xml tree\n");
530         ret = 1;
531     }
532
533     xmlFreeDoc(doc);
534     return ret;
535 }