1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 themetoxml.c for the Openbox window manager
4 Copyright (c) 2007 Dana Jansens
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.
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.
16 See the COPYING file for a copy of the GNU General Public License.
22 #include <X11/Xresource.h>
24 #include <libxml/parser.h>
29 static gboolean read_int(XrmDatabase db, const gchar *rname, gint *value);
30 static gboolean read_string(XrmDatabase db, const gchar *rname,
32 static gboolean read_color(XrmDatabase db, const gchar *rname,
33 gint *r, gint *g, gint *b);
35 static int parse_inline_number(const char *p)
43 for (; isdigit(*p); ++p)
44 res = res * 10 + *p - '0';
49 static gchar *create_class_name(const gchar *rname)
51 gchar *rclass = g_strdup(rname);
59 if (*p == '\0') break;
64 static gboolean read_int(XrmDatabase db, const gchar *rname, gint *value)
67 gchar *rclass = create_class_name(rname);
71 if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
72 retvalue.addr != NULL) {
73 *value = (gint)strtol(retvalue.addr, &end, 10);
74 if (end != retvalue.addr)
82 static gboolean read_string(XrmDatabase db, const gchar *rname,
86 gchar *rclass = create_class_name(rname);
90 if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
91 retvalue.addr != NULL) {
92 *value = retvalue.addr;
101 static gchar hextodec(gchar h)
103 if (h >= '0' && h <= '9') return h - '0';
104 else if (h >= 'a' && h <= 'f') return h - 'a' + 10;
105 else if (h >= 'A' && h <= 'F') return h - 'A' + 10;
109 static gboolean parse_color(const gchar *c, gint *r, gint *g, gint *b)
111 int dig1, dig2, i, color[3];
114 if (len > 4 && c[0] == 'r' && c[1] == 'g' && c[2] == 'b' && c[3] == ':') {
116 for (i = 0; i < 3; ++i) {
117 dig1 = hextodec(c[0]);
118 if (c[1] == '/') { dig2 = dig1; c+=2; }
119 else { dig2 = hextodec(c[1]); c+=3; }
121 if (dig1 < 0 || dig2 < 0) return FALSE;
123 color[i] = dig1*16 + dig2;
125 *r = color[0]; *g = color[1]; *b = color[2];
127 } else if ((len == 4 || len == 7) && c[0] == '#') {
129 for (i = 0; i < 3; ++i) {
130 dig1 = hextodec(c[0]);
131 if (len == 4) { dig2 = dig1; c++; }
132 else { dig2 = hextodec(c[1]); c+=2; }
134 if (dig1 < 0 || dig2 < 0) return FALSE;
136 color[i] = dig1*16 + dig2;
138 *r = color[0]; *g = color[1]; *b = color[2];
143 for (i = 0; colornames[i].name != NULL; ++i) {
144 if (!strcmp(colornames[i].name, c)) {
145 *r = colornames[i].r;
146 *g = colornames[i].g;
147 *b = colornames[i].b;
155 static gboolean read_color(XrmDatabase db, const gchar *rname,
156 gint *r, gint *g, gint *b)
158 gboolean ret = FALSE;
159 gchar *rclass = create_class_name(rname);
163 if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
164 retvalue.addr != NULL) {
165 ret = parse_color(retvalue.addr, r, g, b);
172 xmlNodePtr go(xmlNodePtr node, const char *s)
176 for (p = node->children; p; p = p->next) {
177 if (!xmlStrcasecmp(p->name, (const xmlChar*) s))
180 return xmlAddChild(node, xmlNewNode(NULL, (const xmlChar*) s));
183 static gchar number[20];
184 static inline gchar* NUM(int i) {
185 g_snprintf(number, 20, "%d", i); return number;
187 static xmlDocPtr doc;
188 static xmlNodePtr root;
190 #define GO1(a) (go(root, a))
191 #define GO2(a,b) (go(GO1(a), b))
192 #define GO3(a,b,c) (go(GO2(a, b), c))
193 #define GO4(a,b,c,d) (go(GO3(a, b, c), d))
194 #define GO5(a,b,c,d,e) (go(GO4(a, b, c, d), e))
195 #define GO6(a,b,c,d,e,f) (go(GO5(a, b, c, d, e), f))
196 #define GO7(a,b,c,d,e,f,g) (go(GO6(a, b, c, d, e, f), g))
198 #define CONT1(a,cont) (xmlNodeSetContent(GO1(a), (const xmlChar*)cont))
199 #define CONT2(a,b,cont) (xmlNodeSetContent(GO2(a,b), (const xmlChar*)cont))
200 #define CONT3(a,b,c,cont) (xmlNodeSetContent(GO3(a,b,c), (const xmlChar*)cont))
201 #define CONT4(a,b,c,d,cont) (xmlNodeSetContent(GO4(a,b,c,d), (const xmlChar*)cont))
202 #define CONT5(a,b,c,d,e,cont) (xmlNodeSetContent(GO5(a,b,c,d,e), (const xmlChar*)cont))
203 #define CONT6(a,b,c,d,e,f,cont) (xmlNodeSetContent(GO6(a,b,c,d,e,f), (const xmlChar*)cont))
205 #define ATTR1(a,name,cont) (xmlSetProp(GO1(a), (const xmlChar*)name, (const xmlChar*)cont))
206 #define ATTR2(a,b,name,cont) (xmlSetProp(GO2(a,b), (const xmlChar*)name, (const xmlChar*)cont))
207 #define ATTR3(a,b,c,name,cont) (xmlSetProp(GO3(a,b,c), (const xmlChar*)name, (const xmlChar*)cont))
208 #define ATTR4(a,b,c,d,name,cont) (xmlSetProp(GO4(a,b,c,d), (const xmlChar*)name, (const xmlChar*)cont))
209 #define ATTR5(a,b,c,d,e,name,cont) (xmlSetProp(GO5(a,b,c,d,e), (const xmlChar*)name, (const xmlChar*)cont))
210 #define ATTR6(a,b,c,d,e,f,name,cont) (xmlSetProp(GO6(a,b,c,d,e,f), (const xmlChar*)name, (const xmlChar*)cont))
211 #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))
213 #define COLOR1(a,R,G,B,A) (ATTR1(a,"r",NUM(R)), \
214 ATTR1(a,"g",NUM(G)), \
215 ATTR1(a,"b",NUM(B)), \
217 #define COLOR2(a,b,R,G,B,A) (ATTR2(a,b,"r",NUM(R)), \
218 ATTR2(a,b,"g",NUM(G)), \
219 ATTR2(a,b,"b",NUM(B)), \
220 ATTR2(a,b,"a",NUM(A)))
221 #define COLOR3(a,b,c,R,G,B,A) (ATTR3(a,b,c,"r",NUM(R)), \
222 ATTR3(a,b,c,"g",NUM(G)), \
223 ATTR3(a,b,c,"b",NUM(B)), \
224 ATTR3(a,b,c,"a",NUM(A)))
225 #define COLOR4(a,b,c,d,R,G,B,A) (ATTR4(a,b,c,d,"r",NUM(R)), \
226 ATTR4(a,b,c,d,"g",NUM(G)), \
227 ATTR4(a,b,c,d,"b",NUM(B)), \
228 ATTR4(a,b,c,d,"a",NUM(A)))
229 #define COLOR5(a,b,c,d,e,R,G,B,A) (ATTR5(a,b,c,d,e,"r",NUM(R)), \
230 ATTR5(a,b,c,d,e,"g",NUM(G)), \
231 ATTR5(a,b,c,d,e,"b",NUM(B)), \
232 ATTR5(a,b,c,d,e,"a",NUM(A)))
233 #define COLOR6(a,b,c,d,e,f,R,G,B,A) (ATTR6(a,b,c,d,e,f,"r",NUM(R)), \
234 ATTR6(a,b,c,d,e,f,"g",NUM(G)), \
235 ATTR6(a,b,c,d,e,f,"b",NUM(B)), \
236 ATTR6(a,b,c,d,e,f,"a",NUM(A)))
238 #define APPEARANCE2(res,a,b) \
240 if (read_string(db, res, &s)) \
241 CONT3(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); \
252 #define APPEARANCE3(res,a,b,c) \
254 if (read_string(db, res, &s)) \
255 CONT4(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); \
266 #define APPEARANCE4(res,a,b,c,d) \
268 if (read_string(db, res, &s)) \
269 CONT5(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); \
280 int main(int argc, char **argv)
288 fprintf(stderr, "themetoxml (C) 2007 Dana Jansens\n"
289 "This tool takes an older Openbox3 themerc file on stdin,"
290 " and gives back the\n"
291 "theme in the newer themerc.xml XML style.\n");
295 gchar *buf = g_new(gchar, 1000);
299 while ((rthis = read(0, buf + r, sz - r)) > 0) {
303 buf = g_renew(gchar,buf,sz);
308 if ((db = XrmGetStringDatabase(buf)) == NULL) {
309 fprintf(stderr, "Unable to read the database from stdin\n");
315 doc = xmlNewDoc((const xmlChar*) "1.0");
317 (doc,(root = xmlNewNode(NULL, (const xmlChar*)"openbox_theme")));
318 xmlSetProp(root, (const xmlChar*)"version", (const xmlChar*)"1");
319 xmlSetProp(root, (const xmlChar*)"xmlns",
320 (const xmlChar*)"http://openbox.org/themerc");
322 if (read_int(db, "window.handle.width", &i))
323 CONT2("dimensions", "handle", NUM(i));
325 if (read_int(db, "padding.width", &i)) {
326 ATTR2("dimensions", "padding", "x", NUM(i));
327 ATTR2("dimensions", "padding", "y", NUM(i));
330 if (read_int(db, "borderWidth", &i)) {
331 CONT3("window", "border", "width", NUM(i));
332 CONT3("menu", "border", "width", NUM(i));
333 } else if (read_int(db, "border.width", &i)) {
334 CONT3("window", "border", "width", NUM(i));
335 CONT3("menu", "border", "width", NUM(i));
338 if (read_color(db, "border.color", &i, &j, &k)) {
339 COLOR3("window", "border", "primary", i, j, k, 255);
340 COLOR3("menu", "border", "primary", i, j, k, 255);
343 if (read_int(db, "window.client.padding.width", &i)) {
344 ATTR2("window", "clientpadding", "x", NUM(i));
345 ATTR2("window", "clientpadding", "y", NUM(i));
348 if (read_string(db, "window.label.text.justify", &s)) {
349 if (!g_ascii_strcasecmp(s, "right")) s = "right";
350 else if (!g_ascii_strcasecmp(s, "center")) s = "center";
352 CONT2("window", "justify", s);
355 if (read_string(db, "menu.title.text.justify", &s)) {
356 if (!g_ascii_strcasecmp(s, "right")) s = "right";
357 else if (!g_ascii_strcasecmp(s, "center")) s = "center";
359 CONT2("menu", "justify", s);
362 if (read_int(db, "menu.overlap", &i))
363 CONT2("menu", "overlap", NUM(i));
365 if (read_color(db, "window.active.client.color", &i, &j, &k))
366 COLOR3("window","active","clientpadding",i,j,k,255);
368 if (read_color(db, "window.inactive.client.color", &i, &j, &k))
369 COLOR3("window","inactive","clientpadding",i,j,k,255);
371 if (read_color(db, "window.active.label.text.color", &i, &j, &k))
372 COLOR5("window","active","label","text","primary",i,j,k,255);
374 if (read_color(db, "window.inactive.label.text.color", &i, &j, &k))
375 COLOR5("window","inactive","label","text","primary",i,j,k,255);
377 if (read_color(db, "window.active.button.unpressed.image.color",
379 COLOR5("window","active","buttons","unpressed","image",i,j,k,255);
381 if (read_color(db, "window.inactive.button.unpressed.image.color",
383 COLOR5("window","inactive","buttons","unpressed","image",i,j,k,255);
385 if (read_color(db, "window.active.button.pressed.image.color",
387 COLOR5("window","active","buttons","pressed","image",i,j,k,255);
389 if (read_color(db, "window.inactive.button.pressed.image.color",
391 COLOR5("window","inactive","buttons","pressed","image",i,j,k,255);
393 if (read_color(db, "window.active.button.disabled.image.color",
395 COLOR5("window","active","buttons","disabled","image",i,j,k,255);
397 if (read_color(db, "window.inactive.button.disabled.image.color",
399 COLOR5("window","inactive","buttons","disabled","image",i,j,k,255);
401 if (read_color(db, "window.active.button.hover.image.color",
403 COLOR5("window","active","buttons","hover","image",i,j,k,255);
405 if (read_color(db, "window.inactive.button.hover.image.color",
407 COLOR5("window","inactive","buttons","hover","image",i,j,k,255);
409 if (read_color(db, "window.active.button.toggled.image.color",
411 COLOR5("window","active","buttons","toggled-unpressed","image",i,j,k,255);
413 if (read_color(db, "window.inactive.button.toggled.image.color",
415 COLOR5("window","inactive","buttons","toggled-unpressed","image",i,j,k,255);
417 if (read_color(db, "menu.title.text.color",
419 COLOR4("menu","title","text","primary",i,j,k,255);
421 if (read_color(db, "menu.items.text.color",
423 COLOR3("menu","inactive","primary",i,j,k,255);
425 if (read_color(db, "menu.items.disabled.text.color",
427 COLOR3("menu","disabled","primary",i,j,k,255);
429 if (read_color(db, "menu.items.active.text.color",
431 COLOR4("menu","active","text","primary",i,j,k,255);
433 APPEARANCE3("window.active.title.bg", "window", "active", "titlebar");
434 APPEARANCE3("window.inactive.title.bg", "window", "inactive", "titlebar");
435 APPEARANCE3("window.active.label.bg", "window", "active", "label");
436 APPEARANCE3("window.inactive.label.bg", "window", "inactive", "label");
437 APPEARANCE3("window.active.handle.bg", "window", "active", "handle");
438 APPEARANCE3("window.inactive.handle.bg", "window", "inactive", "handle");
439 APPEARANCE3("window.active.grip.bg", "window", "active", "grip");
440 APPEARANCE3("window.inactive.grip.bg", "window", "inactive", "grip");
441 APPEARANCE2("menu.items.bg", "menu", "entries");
442 APPEARANCE2("menu.items.active.bg", "menu", "active");
443 APPEARANCE2("menu.title.bg", "menu", "title");
445 APPEARANCE4("window.active.button.disabled.bg",
446 "window", "active", "buttons", "disabled");
447 APPEARANCE4("window.inactive.button.disabled.bg",
448 "window", "inactive", "buttons", "disabled");
449 APPEARANCE4("window.active.button.pressed.bg",
450 "window", "active", "buttons", "pressed");
451 APPEARANCE4("window.inactive.button.pressed.bg",
452 "window", "inactive", "buttons", "pressed");
453 APPEARANCE4("window.active.button.toggled.bg",
454 "window", "active", "buttons", "toggled-unpressed");
455 APPEARANCE4("window.inactive.button.toggled.bg",
456 "window", "inactive", "buttons", "toggled-unpressed");
457 APPEARANCE4("window.active.button.unpressed.bg",
458 "window", "active", "buttons", "unpressed");
459 APPEARANCE4("window.inactive.button.unpressed.bg",
460 "window", "inactive", "buttons", "unpressed");
461 APPEARANCE4("window.active.button.hover.bg",
462 "window", "active", "buttons", "hover");
463 APPEARANCE4("window.inactive.button.hover.bg",
464 "window", "inactive", "buttons", "hover");
466 if (read_string(db, "window.active.label.text.font", &s)) {
468 if (strstr(s, "shadow=y")) {
469 if ((p = strstr(s, "shadowoffset=")))
470 i = parse_inline_number(p + strlen("shadowoffset="));
473 ATTR6("window","active","label","text","shadow","offset",
475 ATTR6("window","active","label","text","shadow","offset",
478 if ((p = strstr(s, "shadowtint=")))
480 i = parse_inline_number(p + strlen("shadowtint="));
481 j = (i > 0 ? 0 : 255);
483 COLOR6("window","active","label","text","shadow","primary",
488 if (read_string(db, "window.inactive.label.text.font", &s)) {
490 if (strstr(s, "shadow=y")) {
491 if ((p = strstr(s, "shadowoffset=")))
492 i = parse_inline_number(p + strlen("shadowoffset="));
495 ATTR6("window","inactive","label","text","shadow","offset",
497 ATTR6("window","inactive","label","text","shadow","offset",
500 if ((p = strstr(s, "shadowtint=")))
502 i = parse_inline_number(p + strlen("shadowtint="));
503 j = (i > 0 ? 0 : 255);
505 COLOR6("window","inactive","label","text","shadow","primary",
510 if (read_string(db, "menu.title.text.font", &s)) {
512 if (strstr(s, "shadow=y")) {
513 if ((p = strstr(s, "shadowoffset=")))
514 i = parse_inline_number(p + strlen("shadowoffset="));
517 ATTR5("menu","title","text","shadow","offset","x",NUM(i));
518 ATTR5("menu","title","text","shadow","offset","y",NUM(i));
520 if ((p = strstr(s, "shadowtint=")))
522 i = parse_inline_number(p + strlen("shadowtint="));
523 j = (i > 0 ? 0 : 255);
525 COLOR5("menu","title","text","shadow","primary",j,j,j,i);
529 if (read_string(db, "menu.items.font", &s)) {
531 if (strstr(s, "shadow=y")) {
532 if ((p = strstr(s, "shadowoffset=")))
533 i = parse_inline_number(p + strlen("shadowoffset="));
536 ATTR4("menu","inactive","shadow","offset","x",NUM(i));
537 ATTR4("menu","inactive","shadow","offset","y",NUM(i));
538 ATTR5("menu","active","text","shadow","offset","x",NUM(i));
539 ATTR5("menu","active","text","shadow","offset","y",NUM(i));
540 ATTR4("menu","disabled","shadow","offset","x",NUM(i));
541 ATTR4("menu","disabled","shadow","offset","y",NUM(i));
543 if ((p = strstr(s, "shadowtint=")))
545 i = parse_inline_number(p + strlen("shadowtint="));
546 j = (i > 0 ? 0 : 255);
548 COLOR4("menu","inactive","shadow","primary",j,j,j,i);
549 COLOR5("menu","active","text","shadow","primary",j,j,j,i);
550 COLOR4("menu","disabled","shadow","primary",j,j,j,i);
554 if (xmlSaveFormatFile("-", doc, 1) < 0) {
555 fprintf(stderr, "Error writing the xml tree\n");