]>
Dogcows Code - chaz/openbox/blob - tools/themetoxml/themetoxml.c
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
= g_strstrip(retvalue
.addr
);
100 static gchar
hextodec(gchar h
)
102 if (h
>= '0' && h
<= '9') return h
- '0';
103 else if (h
>= 'a' && h
<= 'f') return h
- 'a' + 10;
104 else if (h
>= 'A' && h
<= 'F') return h
- 'A' + 10;
108 static gboolean
parse_color(const gchar
*c
, gint
*r
, gint
*g
, gint
*b
)
110 int dig1
, dig2
, i
, color
[3];
113 if (len
> 4 && c
[0] == 'r' && c
[1] == 'g' && c
[2] == 'b' && c
[3] == ':') {
115 for (i
= 0; i
< 3; ++i
) {
116 dig1
= hextodec(c
[0]);
117 if (c
[1] == '/' || (c
[1] == '\0' && i
== 2)) { dig2
= dig1
; c
+=2; }
118 else { dig2
= hextodec(c
[1]); c
+=3; }
120 if (dig1
< 0 || dig2
< 0) return FALSE
;
122 color
[i
] = dig1
*16 + dig2
;
124 *r
= color
[0]; *g
= color
[1]; *b
= color
[2];
126 } else if ((len
== 4 || len
== 7) && c
[0] == '#') {
128 for (i
= 0; i
< 3; ++i
) {
129 dig1
= hextodec(c
[0]);
130 if (len
== 4) { dig2
= dig1
; c
++; }
131 else { dig2
= hextodec(c
[1]); c
+=2; }
133 if (dig1
< 0 || dig2
< 0) return FALSE
;
135 color
[i
] = dig1
*16 + dig2
;
137 *r
= color
[0]; *g
= color
[1]; *b
= color
[2];
142 for (i
= 0; colornames
[i
].name
!= NULL
; ++i
) {
143 if (!strcmp(colornames
[i
].name
, c
)) {
144 *r
= colornames
[i
].r
;
145 *g
= colornames
[i
].g
;
146 *b
= colornames
[i
].b
;
154 static gboolean
read_color(XrmDatabase db
, const gchar
*rname
,
155 gint
*r
, gint
*g
, gint
*b
)
157 gboolean ret
= FALSE
;
158 gchar
*rclass
= create_class_name(rname
);
162 if (XrmGetResource(db
, rname
, rclass
, &rettype
, &retvalue
) &&
163 retvalue
.addr
!= NULL
) {
164 ret
= parse_color(retvalue
.addr
, r
, g
, b
);
171 xmlNodePtr
go(xmlNodePtr node
, const char *s
)
175 for (p
= node
->children
; p
; p
= p
->next
) {
176 if (!xmlStrcasecmp(p
->name
, (const xmlChar
*) s
))
179 return xmlAddChild(node
, xmlNewNode(NULL
, (const xmlChar
*) s
));
182 static gchar number
[20];
183 static inline gchar
* NUM(int i
) {
184 g_snprintf(number
, 20, "%d", i
); return number
;
186 static xmlDocPtr doc
;
187 static xmlNodePtr root
;
189 #define GO1(a) (go(root, a))
190 #define GO2(a,b) (go(GO1(a), b))
191 #define GO3(a,b,c) (go(GO2(a, b), c))
192 #define GO4(a,b,c,d) (go(GO3(a, b, c), d))
193 #define GO5(a,b,c,d,e) (go(GO4(a, b, c, d), e))
194 #define GO6(a,b,c,d,e,f) (go(GO5(a, b, c, d, e), f))
195 #define GO7(a,b,c,d,e,f,g) (go(GO6(a, b, c, d, e, f), g))
197 #define CONT1(a,cont) (xmlNodeSetContent(GO1(a), (const xmlChar*)cont))
198 #define CONT2(a,b,cont) (xmlNodeSetContent(GO2(a,b), (const xmlChar*)cont))
199 #define CONT3(a,b,c,cont) (xmlNodeSetContent(GO3(a,b,c), (const xmlChar*)cont))
200 #define CONT4(a,b,c,d,cont) (xmlNodeSetContent(GO4(a,b,c,d), (const xmlChar*)cont))
201 #define CONT5(a,b,c,d,e,cont) (xmlNodeSetContent(GO5(a,b,c,d,e), (const xmlChar*)cont))
202 #define CONT6(a,b,c,d,e,f,cont) (xmlNodeSetContent(GO6(a,b,c,d,e,f), (const xmlChar*)cont))
204 #define ATTR1(a,name,cont) (xmlSetProp(GO1(a), (const xmlChar*)name, (const xmlChar*)cont))
205 #define ATTR2(a,b,name,cont) (xmlSetProp(GO2(a,b), (const xmlChar*)name, (const xmlChar*)cont))
206 #define ATTR3(a,b,c,name,cont) (xmlSetProp(GO3(a,b,c), (const xmlChar*)name, (const xmlChar*)cont))
207 #define ATTR4(a,b,c,d,name,cont) (xmlSetProp(GO4(a,b,c,d), (const xmlChar*)name, (const xmlChar*)cont))
208 #define ATTR5(a,b,c,d,e,name,cont) (xmlSetProp(GO5(a,b,c,d,e), (const xmlChar*)name, (const xmlChar*)cont))
209 #define ATTR6(a,b,c,d,e,f,name,cont) (xmlSetProp(GO6(a,b,c,d,e,f), (const xmlChar*)name, (const xmlChar*)cont))
210 #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))
212 #define COLOR1(a,R,G,B,A) (ATTR1(a,"r",NUM(R)), \
213 ATTR1(a,"g",NUM(G)), \
214 ATTR1(a,"b",NUM(B)), \
216 #define COLOR2(a,b,R,G,B,A) (ATTR2(a,b,"r",NUM(R)), \
217 ATTR2(a,b,"g",NUM(G)), \
218 ATTR2(a,b,"b",NUM(B)), \
219 ATTR2(a,b,"a",NUM(A)))
220 #define COLOR3(a,b,c,R,G,B,A) (ATTR3(a,b,c,"r",NUM(R)), \
221 ATTR3(a,b,c,"g",NUM(G)), \
222 ATTR3(a,b,c,"b",NUM(B)), \
223 ATTR3(a,b,c,"a",NUM(A)))
224 #define COLOR4(a,b,c,d,R,G,B,A) (ATTR4(a,b,c,d,"r",NUM(R)), \
225 ATTR4(a,b,c,d,"g",NUM(G)), \
226 ATTR4(a,b,c,d,"b",NUM(B)), \
227 ATTR4(a,b,c,d,"a",NUM(A)))
228 #define COLOR5(a,b,c,d,e,R,G,B,A) (ATTR5(a,b,c,d,e,"r",NUM(R)), \
229 ATTR5(a,b,c,d,e,"g",NUM(G)), \
230 ATTR5(a,b,c,d,e,"b",NUM(B)), \
231 ATTR5(a,b,c,d,e,"a",NUM(A)))
232 #define COLOR6(a,b,c,d,e,f,R,G,B,A) (ATTR6(a,b,c,d,e,f,"r",NUM(R)), \
233 ATTR6(a,b,c,d,e,f,"g",NUM(G)), \
234 ATTR6(a,b,c,d,e,f,"b",NUM(B)), \
235 ATTR6(a,b,c,d,e,f,"a",NUM(A)))
237 #define APPEARANCE2(res,a,b) \
239 if (read_string(db, res, &s)) \
240 CONT3(a, b, "style", s); \
241 if (read_color(db, res".color", &i, &j, &k)) \
242 COLOR3(a, b, "primary", i, j, k, 255); \
243 if (read_color(db, res".colorTo", &i, &j, &k)) \
244 COLOR3(a, b, "secondary", i, j, k, 255); \
245 if (read_color(db, res".border.color", &i, &j, &k)) \
246 COLOR3(a, b, "border", i, j, k, 255); \
247 if (read_color(db, res".interlace.color", &i, &j, &k)) \
248 COLOR3(a, b, "interlace", i, j, k, 255); \
251 #define APPEARANCE3(res,a,b,c) \
253 if (read_string(db, res, &s)) \
254 CONT4(a, b, c, "style", s); \
255 if (read_color(db, res".color", &i, &j, &k)) \
256 COLOR4(a, b, c, "primary", i, j, k, 255); \
257 if (read_color(db, res".colorTo", &i, &j, &k)) \
258 COLOR4(a, b, c, "secondary", i, j, k, 255); \
259 if (read_color(db, res".border.color", &i, &j, &k)) \
260 COLOR4(a, b, c, "border", i, j, k, 255); \
261 if (read_color(db, res".interlace.color", &i, &j, &k)) \
262 COLOR4(a, b, c, "interlace", i, j, k, 255); \
265 #define APPEARANCE4(res,a,b,c,d) \
267 if (read_string(db, res, &s)) \
268 CONT5(a, b, c, d, "style", s); \
269 if (read_color(db, res".color", &i, &j, &k)) \
270 COLOR5(a, b, c, d, "primary", i, j, k, 255); \
271 if (read_color(db, res".colorTo", &i, &j, &k)) \
272 COLOR5(a, b, c, d, "secondary", i, j, k, 255); \
273 if (read_color(db, res".border.color", &i, &j, &k)) \
274 COLOR5(a, b, c, d, "border", i, j, k, 255); \
275 if (read_color(db, res".interlace.color", &i, &j, &k)) \
276 COLOR5(a, b, c, d, "interlace", i, j, k, 255); \
279 int main(int argc
, char **argv
)
287 fprintf(stderr
, "themetoxml (C) 2007 Dana Jansens\n"
288 "This tool takes an older Openbox3 themerc file and prints "
290 "theme in the newer XML (themerc.xml) style.\n");
294 if (argc
== 1 || !strcmp(argv
[1], "-")) {
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");
314 else if (argc
== 2) {
315 if ((db
= XrmGetFileDatabase(argv
[1])) == NULL
) {
316 fprintf(stderr
, "Unable to read the database from %s\n", argv
[1]);
322 doc
= xmlNewDoc((const xmlChar
*) "1.0");
324 (doc
,(root
= xmlNewNode(NULL
, (const xmlChar
*)"openbox_theme")));
325 xmlSetProp(root
, (const xmlChar
*)"engine", (const xmlChar
*)"box");
326 xmlSetProp(root
, (const xmlChar
*)"version", (const xmlChar
*)"1");
327 xmlSetProp(root
, (const xmlChar
*)"xmlns",
328 (const xmlChar
*)"http://openbox.org/4.0/themerc");
329 CONT2("about", "author", "");
330 CONT2("about", "email", "");
331 CONT2("about", "webpage", "");
332 CONT2("about", "comment", "");
334 if (read_int(db
, "window.handle.width", &i
))
335 CONT2("dimensions", "handle", NUM(i
));
337 if (read_int(db
, "padding.width", &i
)) {
338 ATTR2("dimensions", "padding", "x", NUM(i
));
339 ATTR2("dimensions", "padding", "y", NUM(i
));
342 if (read_int(db
, "borderWidth", &i
)) {
343 CONT3("dimensions", "window", "border", NUM(i
));
344 CONT3("dimensions", "menu", "border", NUM(i
));
345 } else if (read_int(db
, "border.width", &i
)) {
346 CONT3("dimensions", "window", "border", NUM(i
));
347 CONT3("dimensions", "menu", "border", NUM(i
));
349 if (read_int(db
, "menu.border.width", &i
))
350 CONT3("dimensions", "menu", "border", NUM(i
));
352 if (read_color(db
, "border.color", &i
, &j
, &k
)) {
353 COLOR3("window", "active", "border", i
, j
, k
, 255);
354 COLOR3("window", "inactive", "border", i
, j
, k
, 255);
355 COLOR2("menu", "border", i
, j
, k
, 255);
357 if (read_color(db
, "window.active.border.color", &i
, &j
, &k
))
358 COLOR3("window", "active", "border", i
, j
, k
, 255);
359 if (read_color(db
, "window.inactive.border.color", &i
, &j
, &k
))
360 COLOR3("window", "inactive", "border", i
, j
, k
, 255);
361 if (read_color(db
, "menu.border.color", &i
, &j
, &k
))
362 COLOR2("menu", "border", i
, j
, k
, 255);
364 if (read_int(db
, "window.client.padding.width", &i
)) {
365 ATTR3("dimensions", "window", "clientpadding", "x", NUM(i
));
366 ATTR3("dimensions", "window", "clientpadding", "y", NUM(i
));
369 if (read_string(db
, "window.label.text.justify", &s
)) {
370 if (!g_ascii_strcasecmp(s
, "right")) s
= "right";
371 else if (!g_ascii_strcasecmp(s
, "center")) s
= "center";
373 CONT2("window", "justify", s
);
376 if (read_string(db
, "menu.title.text.justify", &s
)) {
377 if (!g_ascii_strcasecmp(s
, "right")) s
= "right";
378 else if (!g_ascii_strcasecmp(s
, "center")) s
= "center";
380 CONT2("menu", "justify", s
);
383 if (read_int(db
, "menu.overlap", &i
))
384 CONT2("menu", "overlap", NUM(i
));
386 if (read_color(db
, "window.active.client.color", &i
, &j
, &k
))
387 COLOR3("window","active","clientpadding",i
,j
,k
,255);
389 if (read_color(db
, "window.inactive.client.color", &i
, &j
, &k
))
390 COLOR3("window","inactive","clientpadding",i
,j
,k
,255);
392 if (read_color(db
, "window.active.label.text.color", &i
, &j
, &k
))
393 COLOR5("window","active","label","text","primary",i
,j
,k
,255);
395 if (read_color(db
, "window.inactive.label.text.color", &i
, &j
, &k
))
396 COLOR5("window","inactive","label","text","primary",i
,j
,k
,255);
398 if (read_color(db
, "window.active.button.unpressed.image.color",
400 COLOR5("window","active","buttons","unpressed","image",i
,j
,k
,255);
402 if (read_color(db
, "window.inactive.button.unpressed.image.color",
404 COLOR5("window","inactive","buttons","unpressed","image",i
,j
,k
,255);
406 if (read_color(db
, "window.active.button.pressed.image.color",
408 COLOR5("window","active","buttons","pressed","image",i
,j
,k
,255);
410 if (read_color(db
, "window.inactive.button.pressed.image.color",
412 COLOR5("window","inactive","buttons","pressed","image",i
,j
,k
,255);
414 if (read_color(db
, "window.active.button.disabled.image.color",
416 COLOR5("window","active","buttons","disabled","image",i
,j
,k
,255);
418 if (read_color(db
, "window.inactive.button.disabled.image.color",
420 COLOR5("window","inactive","buttons","disabled","image",i
,j
,k
,255);
422 if (read_color(db
, "window.active.button.hover.image.color",
424 COLOR5("window","active","buttons","hover","image",i
,j
,k
,255);
426 if (read_color(db
, "window.inactive.button.hover.image.color",
428 COLOR5("window","inactive","buttons","hover","image",i
,j
,k
,255);
430 if (read_color(db
, "window.active.button.toggled.image.color",
432 COLOR5("window","active","buttons","toggled-unpressed","image",i
,j
,k
,255);
434 if (read_color(db
, "window.inactive.button.toggled.image.color",
436 COLOR5("window","inactive","buttons","toggled-unpressed","image",i
,j
,k
,255);
438 if (read_color(db
, "menu.title.text.color",
440 COLOR4("menu","title","text","primary",i
,j
,k
,255);
442 if (read_color(db
, "menu.items.text.color",
444 COLOR3("menu","inactive","primary",i
,j
,k
,255);
446 if (read_color(db
, "menu.items.disabled.text.color",
448 COLOR3("menu","disabled","primary",i
,j
,k
,255);
449 read_color(db
, "menu.items.activedisabled.text.color",
450 &i
, &j
, &k
); /* read this if we can */
451 COLOR4("menu","activedisabled","text","primary",i
,j
,k
,255);
454 if (read_color(db
, "menu.items.active.text.color",
456 COLOR4("menu","active","text","primary",i
,j
,k
,255);
458 APPEARANCE3("window.active.title.bg", "window", "active", "titlebar");
459 APPEARANCE3("window.inactive.title.bg", "window", "inactive", "titlebar");
460 APPEARANCE3("window.active.label.bg", "window", "active", "label");
461 APPEARANCE3("window.inactive.label.bg", "window", "inactive", "label");
462 APPEARANCE3("window.active.handle.bg", "window", "active", "handle");
463 APPEARANCE3("window.inactive.handle.bg", "window", "inactive", "handle");
464 APPEARANCE3("window.active.grip.bg", "window", "active", "grip");
465 APPEARANCE3("window.inactive.grip.bg", "window", "inactive", "grip");
466 APPEARANCE2("menu.items.bg", "menu", "entries");
467 APPEARANCE2("menu.items.active.bg", "menu", "active");
468 APPEARANCE2("menu.items.active.bg", "menu", "activedisabled");
469 APPEARANCE2("menu.title.bg", "menu", "title");
471 APPEARANCE4("window.active.button.disabled.bg",
472 "window", "active", "buttons", "disabled");
473 APPEARANCE4("window.inactive.button.disabled.bg",
474 "window", "inactive", "buttons", "disabled");
475 APPEARANCE4("window.active.button.pressed.bg",
476 "window", "active", "buttons", "pressed");
477 APPEARANCE4("window.inactive.button.pressed.bg",
478 "window", "inactive", "buttons", "pressed");
479 APPEARANCE4("window.active.button.toggled.bg",
480 "window", "active", "buttons", "toggled-unpressed");
481 APPEARANCE4("window.inactive.button.toggled.bg",
482 "window", "inactive", "buttons", "toggled-unpressed");
483 APPEARANCE4("window.active.button.unpressed.bg",
484 "window", "active", "buttons", "unpressed");
485 APPEARANCE4("window.inactive.button.unpressed.bg",
486 "window", "inactive", "buttons", "unpressed");
487 APPEARANCE4("window.active.button.hover.bg",
488 "window", "active", "buttons", "hover");
489 APPEARANCE4("window.inactive.button.hover.bg",
490 "window", "inactive", "buttons", "hover");
492 if (read_string(db
, "window.active.label.text.font", &s
)) {
494 if (strstr(s
, "shadow=y")) {
495 if ((p
= strstr(s
, "shadowoffset=")))
496 i
= parse_inline_number(p
+ strlen("shadowoffset="));
499 ATTR6("window","active","label","text","shadow","offset",
501 ATTR6("window","active","label","text","shadow","offset",
504 if ((p
= strstr(s
, "shadowtint=")))
506 i
= parse_inline_number(p
+ strlen("shadowtint="));
507 j
= (i
> 0 ? 0 : 255);
509 COLOR6("window","active","label","text","shadow","primary",
514 if (read_string(db
, "window.inactive.label.text.font", &s
)) {
516 if (strstr(s
, "shadow=y")) {
517 if ((p
= strstr(s
, "shadowoffset=")))
518 i
= parse_inline_number(p
+ strlen("shadowoffset="));
521 ATTR6("window","inactive","label","text","shadow","offset",
523 ATTR6("window","inactive","label","text","shadow","offset",
526 if ((p
= strstr(s
, "shadowtint=")))
528 i
= parse_inline_number(p
+ strlen("shadowtint="));
529 j
= (i
> 0 ? 0 : 255);
531 COLOR6("window","inactive","label","text","shadow","primary",
536 if (read_string(db
, "menu.title.text.font", &s
)) {
538 if (strstr(s
, "shadow=y")) {
539 if ((p
= strstr(s
, "shadowoffset=")))
540 i
= parse_inline_number(p
+ strlen("shadowoffset="));
543 ATTR5("menu","title","text","shadow","offset","x",NUM(i
));
544 ATTR5("menu","title","text","shadow","offset","y",NUM(i
));
546 if ((p
= strstr(s
, "shadowtint=")))
548 i
= parse_inline_number(p
+ strlen("shadowtint="));
549 j
= (i
> 0 ? 0 : 255);
551 COLOR5("menu","title","text","shadow","primary",j
,j
,j
,i
);
555 if (read_string(db
, "menu.items.font", &s
)) {
557 if (strstr(s
, "shadow=y")) {
558 if ((p
= strstr(s
, "shadowoffset=")))
559 i
= parse_inline_number(p
+ strlen("shadowoffset="));
562 ATTR4("menu","inactive","shadow","offset","x",NUM(i
));
563 ATTR4("menu","inactive","shadow","offset","y",NUM(i
));
564 ATTR5("menu","active","text","shadow","offset","x",NUM(i
));
565 ATTR5("menu","active","text","shadow","offset","y",NUM(i
));
566 ATTR4("menu","disabled","shadow","offset","x",NUM(i
));
567 ATTR4("menu","disabled","shadow","offset","y",NUM(i
));
568 ATTR5("menu","activedisabled","text","shadow","offset","x",NUM(i
));
569 ATTR5("menu","activedisabled","text","shadow","offset","y",NUM(i
));
571 if ((p
= strstr(s
, "shadowtint=")))
573 i
= parse_inline_number(p
+ strlen("shadowtint="));
574 j
= (i
> 0 ? 0 : 255);
576 COLOR4("menu","inactive","shadow","primary",j
,j
,j
,i
);
577 COLOR5("menu","active","text","shadow","primary",j
,j
,j
,i
);
578 COLOR4("menu","disabled","shadow","primary",j
,j
,j
,i
);
579 COLOR5("menu","activedisabled","text","shadow","primary",j
,j
,j
,i
);
583 if (xmlSaveFormatFile("-", doc
, 1) < 0) {
584 fprintf(stderr
, "Error writing the xml tree\n");
This page took 0.076987 seconds and 4 git commands to generate.