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, 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);
34 static int parse_inline_number(char *p)
42 for (; isdigit(*p); ++p)
43 res = res * 10 + *p - '0';
48 static gchar *create_class_name(gchar *rname)
50 gchar *rclass = g_strdup(rname);
58 if (*p == '\0') break;
63 static gboolean read_int(XrmDatabase db, gchar *rname, gint *value)
66 gchar *rclass = create_class_name(rname);
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)
81 static gboolean read_string(XrmDatabase db, gchar *rname, gchar **value)
84 gchar *rclass = create_class_name(rname);
88 if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
89 retvalue.addr != NULL) {
90 *value = retvalue.addr;
99 static gchar hextodec(gchar h)
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;
107 static gboolean parse_color(gchar *c, gint *r, gint *g, gint *b)
109 int dig1, dig2, i, color[3];
112 if (c[0] == '#' && (len != 4 && len != 7)) return FALSE;
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;
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]);
134 if (dig1 < 0 || dig2 < 0) return FALSE;
136 color[i] = dig1*16 + dig2;
138 *r = color[0]; *g = color[1]; *b = color[2];
142 static gboolean read_color(XrmDatabase db, gchar *rname,
143 gint *r, gint *g, gint *b)
145 gboolean ret = FALSE;
146 gchar *rclass = create_class_name(rname);
150 if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
151 retvalue.addr != NULL) {
152 ret = parse_color(retvalue.addr, r, g, b);
159 xmlNodePtr go(xmlNodePtr node, const char *s)
163 for (p = node->children; p; p = p->next) {
164 if (!xmlStrcasecmp(p->name, (const xmlChar*) s))
167 return xmlAddChild(node, xmlNewNode(NULL, (const xmlChar*) s));
170 static gchar number[20];
171 static inline gchar* NUM(int i) {
172 g_snprintf(number, 20, "%d", i); return number;
174 static xmlDocPtr doc;
175 static xmlNodePtr root;
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))
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))
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))
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))
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))
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)))
238 #define APPEARANCE2(res,a,b) \
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); \
252 #define APPEARANCE3(res,a,b,c) \
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); \
266 #define APPEARANCE4(res,a,b,c,d) \
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); \
280 int main(int argc, char **argv)
288 printf("Please specify an Openbox3 themerc file\n");
292 if ((db = XrmGetFileDatabase(argv[1])) == NULL) {
293 printf("Unable to open the database from stdin\n");
297 doc = xmlNewDoc((const xmlChar*) "1.0");
299 (doc,(root = xmlNewNode(NULL, (const xmlChar*)"openbox_theme")));
301 if (read_int(db, "window.handle.width", &i))
302 CONT2("dimensions", "handle", NUM(i));
304 if (read_int(db, "padding.width", &i)) {
305 ATTR2("dimensions", "padding", "horizontal", NUM(i));
306 ATTR2("dimensions", "padding", "vertical", NUM(i));
309 if (read_int(db, "border.width", &i)) {
310 APPCONT3("window", "border", "width", NUM(i));
311 APPCONT3("menu", "border", "width", NUM(i));
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);
319 if (read_int(db, "window.client.padding.width", &i))
320 APPCONT3("window", "clientborder", "width", NUM(i));
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";
326 APPCONT5("window", "inactive", "label", "text", "justify", s);
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";
333 APPCONT4("menu", "title", "text", "justify", s);
336 if (read_int(db, "menu.overlap", &i))
337 APPCONT2("menu", "overlap", NUM(i));
339 if (read_color(db, "window.active.client.color", &i, &j, &k))
340 COLOR4("window","active","clientborder","primary",i,j,k,255);
342 if (read_color(db, "window.inactive.client.color", &i, &j, &k))
343 COLOR4("window","inactive","clientborder","primary",i,j,k,255);
345 if (read_color(db, "window.active.label.text.color", &i, &j, &k))
346 COLOR5("window","active","label","text","primary",i,j,k,255);
348 if (read_color(db, "window.inactive.label.text.color", &i, &j, &k))
349 COLOR5("window","inactive","label","text","primary",i,j,k,255);
351 if (read_color(db, "window.active.button.unpressed.image.color",
353 COLOR5("window","active","buttons","unpressed","image",i,j,k,255);
355 if (read_color(db, "window.inactive.button.unpressed.image.color",
357 COLOR5("window","inactive","buttons","unpressed","image",i,j,k,255);
359 if (read_color(db, "window.active.button.pressed.image.color",
361 COLOR5("window","active","buttons","pressed","image",i,j,k,255);
363 if (read_color(db, "window.inactive.button.pressed.image.color",
365 COLOR5("window","inactive","buttons","pressed","image",i,j,k,255);
367 if (read_color(db, "window.active.button.disabled.image.color",
369 COLOR5("window","active","buttons","disabled","image",i,j,k,255);
371 if (read_color(db, "window.inactive.button.disabled.image.color",
373 COLOR5("window","inactive","buttons","disabled","image",i,j,k,255);
375 if (read_color(db, "window.active.button.hover.image.color",
377 COLOR5("window","active","buttons","hover","image",i,j,k,255);
379 if (read_color(db, "window.inactive.button.hover.image.color",
381 COLOR5("window","inactive","buttons","hover","image",i,j,k,255);
383 if (read_color(db, "window.active.button.toggled.image.color",
385 COLOR5("window","active","buttons","toggled","image",i,j,k,255);
387 if (read_color(db, "window.inactive.button.toggled.image.color",
389 COLOR5("window","inactive","buttons","toggled","image",i,j,k,255);
391 if (read_color(db, "menu.title.text.color",
393 COLOR4("menu","title","text","primary",i,j,k,255);
395 if (read_color(db, "menu.items.text.color",
397 COLOR4("menu","inactive","text","primary",i,j,k,255);
399 if (read_color(db, "menu.items.disabled.text.color",
401 COLOR3("menu","disabled","primary",i,j,k,255);
403 if (read_color(db, "menu.items.active.text.color",
405 COLOR4("menu","active","text","primary",i,j,k,255);
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");
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");
440 if (read_string(db, "window.active.label.text.font", &s)) {
442 if (strstr(s, "shadow=y")) {
443 if ((p = strstr(s, "shadowoffset=")))
444 i = parse_inline_number(p + strlen("shadowoffset="));
447 APPATTR6("window","active","label","text","shadow","offset",
449 APPATTR6("window","active","label","text","shadow","offset",
452 if ((p = strstr(s, "shadowtint=")))
454 i = parse_inline_number(p + strlen("shadowtint="));
455 j = (i > 0 ? 0 : 255);
457 COLOR6("window","active","label","text","shadow","primary",
462 if (read_string(db, "window.inactive.label.text.font", &s)) {
464 if (strstr(s, "shadow=y")) {
465 if ((p = strstr(s, "shadowoffset=")))
466 i = parse_inline_number(p + strlen("shadowoffset="));
469 APPATTR6("window","inactive","label","text","shadow","offset",
471 APPATTR6("window","inactive","label","text","shadow","offset",
474 if ((p = strstr(s, "shadowtint=")))
476 i = parse_inline_number(p + strlen("shadowtint="));
477 j = (i > 0 ? 0 : 255);
479 COLOR6("window","inactive","label","text","shadow","primary",
484 if (read_string(db, "menu.title.text.font", &s)) {
486 if (strstr(s, "shadow=y")) {
487 if ((p = strstr(s, "shadowoffset=")))
488 i = parse_inline_number(p + strlen("shadowoffset="));
491 APPATTR5("menu","title","text","shadow","offset","x",NUM(i));
492 APPATTR5("menu","title","text","shadow","offset","y",NUM(i));
494 if ((p = strstr(s, "shadowtint=")))
496 i = parse_inline_number(p + strlen("shadowtint="));
497 j = (i > 0 ? 0 : 255);
499 COLOR5("menu","title","text","shadow","primary",j,j,j,i);
503 if (read_string(db, "menu.items.text.font", &s)) {
505 if (strstr(s, "shadow=y")) {
506 if ((p = strstr(s, "shadowoffset=")))
507 i = parse_inline_number(p + strlen("shadowoffset="));
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));
517 if ((p = strstr(s, "shadowtint=")))
519 i = parse_inline_number(p + strlen("shadowtint="));
520 j = (i > 0 ? 0 : 255);
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);
528 if (xmlSaveFormatFile("-", doc, 1) < 0) {
529 printf("Error writing the xml tree\n");