]> icculus.org git repositories - mikachu/openbox.git/blob - engines/openbox/theme.c
make the openbox engine use the new config shit instead of the themerc shit.
[mikachu/openbox.git] / engines / openbox / theme.c
1 #include "openbox.h"
2 #include "../../kernel/config.h"
3 #include "../../kernel/openbox.h"
4
5 #include <glib.h>
6 #include <X11/Xlib.h>
7 #include <X11/Xresource.h>
8 #ifdef HAVE_STDLIB_H
9 #  include <stdlib.h>
10 #endif
11 #ifdef HAVE_CTYPE_H
12 #  include <ctype.h>
13 #endif
14 #ifdef HAVE_STRING_H
15 #  include <string.h>
16 #endif
17
18 static XrmDatabase loaddb(char *theme)
19 {
20     XrmDatabase db;
21
22     db = XrmGetFileDatabase(theme);
23     if (db == NULL) {
24         char *s = g_build_filename(g_get_home_dir(), ".openbox", "themes",
25                                    "openbox", theme, NULL);
26         db = XrmGetFileDatabase(s);
27         g_free(s);
28     }
29     if (db == NULL) {
30         char *s = g_build_filename(THEMEDIR, theme, NULL);
31         db = XrmGetFileDatabase(s);
32         g_free(s);
33     }
34     return db;
35 }
36
37 static char *create_class_name(char *rname)
38 {
39     char *rclass = g_strdup(rname);
40     char *p = rclass;
41
42     while (TRUE) {
43         *p = toupper(*p);
44         p = strchr(p+1, '.');
45         if (p == NULL) break;
46         ++p;
47         if (*p == '\0') break;
48     }
49     return rclass;
50 }
51
52 gboolean read_bool(XrmDatabase db, char *rname, gboolean *value)
53 {
54     gboolean ret = FALSE;
55     char *rclass = create_class_name(rname);
56     char *rettype;
57     XrmValue retvalue;
58   
59     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
60         retvalue.addr != NULL) {
61         if (!g_ascii_strcasecmp(retvalue.addr, "true"))
62             *value = TRUE;
63         else
64             *value = FALSE;
65         ret = TRUE;
66     }
67
68     g_free(rclass);
69     return ret;
70 }
71
72 gboolean read_int(XrmDatabase db, char *rname, int *value)
73 {
74     gboolean ret = FALSE;
75     char *rclass = create_class_name(rname);
76     char *rettype, *end;
77     XrmValue retvalue;
78   
79     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
80         retvalue.addr != NULL) {
81         *value = (int)strtol(retvalue.addr, &end, 10);
82         if (end != retvalue.addr)
83             ret = TRUE;
84     }
85
86     g_free(rclass);
87     return ret;
88 }
89
90 gboolean read_string(XrmDatabase db, char *rname, char **value)
91 {
92     gboolean ret = FALSE;
93     char *rclass = create_class_name(rname);
94     char *rettype;
95     XrmValue retvalue;
96   
97     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
98         retvalue.addr != NULL) {
99         *value = g_strdup(retvalue.addr);
100         ret = TRUE;
101     }
102
103     g_free(rclass);
104     return ret;
105 }
106
107 gboolean read_color(XrmDatabase db, char *rname, color_rgb **value)
108 {
109     gboolean ret = FALSE;
110     char *rclass = create_class_name(rname);
111     char *rettype;
112     XrmValue retvalue;
113   
114     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
115         retvalue.addr != NULL) {
116         color_rgb *c = color_parse(retvalue.addr);
117         if (c != NULL) {
118             *value = c;
119             ret = TRUE;
120         }
121     }
122
123     g_free(rclass);
124     return ret;
125 }
126
127 gboolean read_mask(XrmDatabase db, char *rname, pixmap_mask **value)
128 {
129     gboolean ret = FALSE;
130     char *rclass = create_class_name(rname);
131     char *rettype;
132     char *s;
133     char *button_dir;
134     XrmValue retvalue;
135     int hx, hy; /* ignored */
136     unsigned int w, h;
137     unsigned char *b;
138     ConfigValue theme;
139   
140     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
141         retvalue.addr != NULL) {
142         if (!config_get("theme", Config_String, &theme))
143             g_assert_not_reached(); /* where's the default!? its not set? */
144
145         button_dir = g_strdup_printf("%s_buttons", theme.string);
146
147         s = g_build_filename(g_get_home_dir(), ".openbox", "themes",
148                              "openbox", button_dir, retvalue.addr, NULL);
149
150         if (XReadBitmapFileData(s, &w, &h, &b, &hx, &hy) == BitmapSuccess)
151             ret = TRUE;
152         else {
153             g_free(s);
154             s = g_build_filename(THEMEDIR, button_dir, retvalue.addr, NULL);
155         
156             if (XReadBitmapFileData(s, &w, &h, &b, &hx, &hy) == BitmapSuccess) 
157                 ret = TRUE;
158             else {
159                 g_free(s);
160                 s = g_strdup_printf("%s_buttons/%s", theme.string,
161                                     theme.string);
162                 if (XReadBitmapFileData(s, &w, &h, &b, &hx, &hy) ==
163                     BitmapSuccess) 
164                     ret = TRUE;
165                 else
166                     g_message("Unable to find bitmap '%s'", s);
167             }
168         }
169
170         if (ret) {
171             *value = pixmap_mask_new(w, h, (char*)b);
172             XFree(b);
173         }
174       
175         g_free(s);
176         g_free(button_dir);
177     }
178
179     g_free(rclass);
180     return ret;
181 }
182
183 static void parse_appearance(char *tex, SurfaceColorType *grad,
184                              ReliefType *relief, BevelType *bevel,
185                              gboolean *interlaced, gboolean *border)
186 {
187     char *t;
188
189     /* convert to all lowercase */
190     for (t = tex; *t != '\0'; ++t)
191         *t = g_ascii_tolower(*t);
192
193     if (strstr(tex, "parentrelative") != NULL) {
194         *grad = Background_ParentRelative;
195     } else {
196         if (strstr(tex, "gradient") != NULL) {
197             if (strstr(tex, "crossdiagonal") != NULL)
198                 *grad = Background_CrossDiagonal;
199             else if (strstr(tex, "rectangle") != NULL)
200                 *grad = Background_Rectangle;
201             else if (strstr(tex, "pyramid") != NULL)
202                 *grad = Background_Pyramid;
203             else if (strstr(tex, "pipecross") != NULL)
204                 *grad = Background_PipeCross;
205             else if (strstr(tex, "elliptic") != NULL)
206                 *grad = Background_Elliptic;
207             else if (strstr(tex, "horizontal") != NULL)
208                 *grad = Background_Horizontal;
209             else if (strstr(tex, "vertical") != NULL)
210                 *grad = Background_Vertical;
211             else
212                 *grad = Background_Diagonal;
213         } else {
214             *grad = Background_Solid;
215         }
216
217         if (strstr(tex, "sunken") != NULL)
218             *relief = Sunken;
219         else if (strstr(tex, "flat") != NULL)
220             *relief = Flat;
221         else
222             *relief = Raised;
223         
224         *border = FALSE;
225         if (*relief == Flat) {
226             if (strstr(tex, "border") != NULL)
227                 *border = TRUE;
228         } else {
229             if (strstr(tex, "bevel2") != NULL)
230                 *bevel = Bevel2;
231             else
232                 *bevel = Bevel1;
233         }
234
235         if (strstr(tex, "interlaced") != NULL)
236             *interlaced = TRUE;
237         else
238             *interlaced = FALSE;
239     }
240 }
241
242
243 gboolean read_appearance(XrmDatabase db, char *rname, Appearance *value)
244 {
245     gboolean ret = FALSE;
246     char *rclass = create_class_name(rname), *cname, *ctoname, *bcname;
247     char *rettype;
248     XrmValue retvalue;
249
250     cname = g_strconcat(rname, ".color", NULL);
251     ctoname = g_strconcat(rname, ".colorTo", NULL);
252     bcname = g_strconcat(rname, ".borderColor", NULL);
253
254     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
255         retvalue.addr != NULL) {
256         parse_appearance(retvalue.addr,
257                          &value->surface.data.planar.grad,
258                          &value->surface.data.planar.relief,
259                          &value->surface.data.planar.bevel,
260                          &value->surface.data.planar.interlaced,
261                          &value->surface.data.planar.border);
262         if (!read_color(db, cname, &value->surface.data.planar.primary))
263             value->surface.data.planar.primary = color_new(0, 0, 0);
264         if (!read_color(db, ctoname, &value->surface.data.planar.secondary))
265             value->surface.data.planar.secondary = color_new(0, 0, 0);
266         if (value->surface.data.planar.border)
267             if (!read_color(db, bcname,
268                             &value->surface.data.planar.border_color))
269                 value->surface.data.planar.border_color = color_new(0, 0, 0);
270         ret = TRUE;
271     }
272
273     g_free(bcname);
274     g_free(ctoname);
275     g_free(cname);
276     g_free(rclass);
277     return ret;
278 }
279
280 void set_default_appearance(Appearance *a)
281 {
282     a->surface.data.planar.grad = Background_Solid;
283     a->surface.data.planar.relief = Flat;
284     a->surface.data.planar.bevel = Bevel1;
285     a->surface.data.planar.interlaced = FALSE;
286     a->surface.data.planar.border = FALSE;
287     a->surface.data.planar.primary = color_new(0, 0, 0);
288     a->surface.data.planar.secondary = color_new(0, 0, 0);
289 }
290
291 gboolean load()
292 {
293     XrmDatabase db = NULL;
294     Justify winjust;
295     char *winjuststr;
296     ConfigValue theme, shadow, offset, font;
297
298     if (config_get("theme", Config_String, &theme)) {
299         db = loaddb(theme.string);
300         if (db == NULL) {
301             g_warning("Failed to load the theme '%s'", theme.string);
302             g_message("Falling back to the default: '%s'", DEFAULT_THEME);
303         }
304     }
305     if (db == NULL) {
306         db = loaddb(DEFAULT_THEME);
307         if (db == NULL) {
308             g_warning("Failed to load the theme '%s'.", DEFAULT_THEME);
309             return FALSE;
310         }
311         /* change to reflect what was actually loaded */
312         theme.string = DEFAULT_THEME;
313         config_set("theme", Config_String, theme);
314     }
315
316     /* load the font, not from the theme file tho, its in the config */
317     s_winfont_shadow = 1; /* XXX read from themrc */
318     if (!config_get("font.shadow.offset", Config_Integer, &offset) ||
319         offset.integer < 0 || offset.integer >= 10) {
320         s_winfont_shadow_offset = 1; /* default */
321     }
322     
323     if (!config_get("font", Config_String, &font)) {
324         font.string = DEFAULT_FONT;
325         config_set("font", Config_String, font);
326     }
327     s_winfont = font_open(font.string);
328     s_winfont_height = font_height(s_winfont, s_winfont_shadow,
329                                    s_winfont_shadow_offset);
330
331     winjust = Justify_Left;
332     if (read_string(db, "window.justify", &winjuststr)) {
333         if (!g_ascii_strcasecmp(winjuststr, "right"))
334             winjust = Justify_Right;
335         else if (!g_ascii_strcasecmp(winjuststr, "center"))
336             winjust = Justify_Center;
337         g_free(winjuststr);
338     }
339
340     if (!read_int(db, "handleWidth", &s_handle_height) ||
341         s_handle_height < 0 || s_handle_height > 100) s_handle_height = 6;
342     if (!read_int(db, "bevelWidth", &s_bevel) ||
343         s_bevel <= 0 || s_bevel > 100) s_bevel = 3;
344     if (!read_int(db, "borderWidth", &s_bwidth) ||
345         s_bwidth < 0 || s_bwidth > 100) s_bwidth = 1;
346     if (!read_int(db, "frameWidth", &s_cbwidth) ||
347         s_cbwidth < 0 || s_cbwidth > 100) s_cbwidth = s_bevel;
348
349     if (!read_color(db, "borderColor", &s_b_color))
350         s_b_color = color_new(0, 0, 0);
351     if (!read_color(db, "window.frame.focusColor", &s_cb_focused_color))
352         s_cb_focused_color = color_new(0xff, 0xff, 0xff);
353     if (!read_color(db, "window.frame.unfocusColor", &s_cb_unfocused_color))
354         s_cb_unfocused_color = color_new(0xff, 0xff, 0xff);
355     if (!read_color(db, "window.label.focus.textColor",
356                     &s_title_focused_color))
357         s_title_focused_color = color_new(0xff, 0xff, 0xff);
358     if (!read_color(db, "window.label.unfocus.textColor",
359                     &s_title_unfocused_color))
360         s_title_unfocused_color = color_new(0xff, 0xff, 0xff);
361     if (!read_color(db, "window.button.focus.picColor",
362                     &s_titlebut_focused_color))
363         s_titlebut_focused_color = color_new(0, 0, 0);
364     if (!read_color(db, "window.button.unfocus.picColor",
365                     &s_titlebut_unfocused_color))
366         s_titlebut_unfocused_color = color_new(0xff, 0xff, 0xff);
367
368     if (!read_mask(db, "window.button.max.mask", &s_max_mask)) {
369         char data[] = { 0x7c, 0x44, 0x47, 0x47, 0x7f, 0x1f, 0x1f  };
370         s_max_mask = pixmap_mask_new(7, 7, data);
371     }
372     if (!read_mask(db, "window.button.icon.mask", &s_icon_mask)) {
373         char data[] = { 0x00, 0x00, 0x00, 0x00, 0x3e, 0x3e, 0x3e };
374         s_icon_mask = pixmap_mask_new(7, 7, data);
375     }
376     if (!read_mask(db, "window.button.stick.mask", &s_desk_mask)) {
377         char data[] = { 0x00, 0x36, 0x36, 0x00, 0x36, 0x36, 0x00 };
378         s_desk_mask = pixmap_mask_new(7, 7, data);
379     }
380     if (!read_mask(db, "window.button.close.mask", &s_close_mask)) {
381         char data[] = { 0x22, 0x77, 0x3e, 0x1c, 0x3e, 0x77, 0x22 };
382         s_close_mask = pixmap_mask_new(7, 7, data);
383     }        
384
385     if (!read_appearance(db, "window.title.focus", a_focused_title))
386         set_default_appearance(a_focused_title);
387     if (!read_appearance(db, "window.title.unfocus", a_unfocused_title))
388         set_default_appearance(a_unfocused_title);
389     if (!read_appearance(db, "window.label.focus", a_focused_label))
390         set_default_appearance(a_focused_label);
391     if (!read_appearance(db, "window.label.unfocus", a_unfocused_label))
392         set_default_appearance(a_unfocused_label);
393     if (!read_appearance(db, "window.handle.focus", a_focused_handle))
394         set_default_appearance(a_focused_handle);
395     if (!read_appearance(db, "window.handle.unfocus", a_unfocused_handle))
396         set_default_appearance(a_unfocused_handle);
397     if (!read_appearance(db, "window.grip.focus", a_focused_grip))
398         set_default_appearance(a_focused_grip);
399     if (!read_appearance(db, "window.grip.unfocus", a_unfocused_grip))
400         set_default_appearance(a_unfocused_grip);
401
402     if (!read_appearance(db, "window.button.pressed.focus",
403                          a_focused_pressed_max))
404         if (!read_appearance(db, "window.button.pressed",
405                              a_focused_pressed_max))
406             set_default_appearance(a_focused_pressed_max);
407     if (!read_appearance(db, "window.button.pressed.unfocus",
408                          a_unfocused_pressed_max))
409         if (!read_appearance(db, "window.button.pressed",
410                              a_unfocused_pressed_max))
411             set_default_appearance(a_unfocused_pressed_max);
412     if (!read_appearance(db, "window.button.focus",
413                          a_focused_unpressed_max))
414         set_default_appearance(a_focused_unpressed_max);
415     if (!read_appearance(db, "window.button.unfocus",
416                          a_unfocused_unpressed_max))
417         set_default_appearance(a_unfocused_unpressed_max);
418
419     a_unfocused_unpressed_close = appearance_copy(a_unfocused_unpressed_max);
420     a_unfocused_pressed_close = appearance_copy(a_unfocused_pressed_max);
421     a_focused_unpressed_close = appearance_copy(a_focused_unpressed_max);
422     a_focused_pressed_close = appearance_copy(a_focused_pressed_max);
423     a_unfocused_unpressed_desk = appearance_copy(a_unfocused_unpressed_max);
424     a_unfocused_pressed_desk = appearance_copy(a_unfocused_pressed_max);
425     a_focused_unpressed_desk = appearance_copy(a_focused_unpressed_max);
426     a_focused_pressed_desk = appearance_copy(a_focused_pressed_max);
427     a_unfocused_unpressed_iconify = appearance_copy(a_unfocused_unpressed_max);
428     a_unfocused_pressed_iconify = appearance_copy(a_unfocused_pressed_max);
429     a_focused_unpressed_iconify = appearance_copy(a_focused_unpressed_max);
430     a_focused_pressed_iconify = appearance_copy(a_focused_pressed_max);
431
432     a_icon->surface.data.planar.grad = Background_ParentRelative;
433
434     /* set up the textures */
435     a_focused_label->texture[0].type = Text;
436     a_focused_label->texture[0].data.text.justify = winjust;
437     a_focused_label->texture[0].data.text.font = s_winfont;
438     a_focused_label->texture[0].data.text.shadow = s_winfont_shadow;
439     a_focused_label->texture[0].data.text.offset = s_winfont_shadow_offset;
440     a_focused_label->texture[0].data.text.color = s_title_focused_color;
441
442     a_unfocused_label->texture[0].type = Text;
443     a_unfocused_label->texture[0].data.text.justify = winjust;
444     a_unfocused_label->texture[0].data.text.font = s_winfont;
445     a_unfocused_label->texture[0].data.text.shadow = s_winfont_shadow;
446     a_unfocused_label->texture[0].data.text.offset = s_winfont_shadow_offset;
447     a_unfocused_label->texture[0].data.text.color = s_title_unfocused_color;
448
449     a_focused_unpressed_max->texture[0].type = 
450         a_focused_pressed_max->texture[0].type = 
451         a_unfocused_unpressed_max->texture[0].type = 
452         a_unfocused_pressed_max->texture[0].type = 
453         a_focused_unpressed_close->texture[0].type = 
454         a_focused_pressed_close->texture[0].type = 
455         a_unfocused_unpressed_close->texture[0].type = 
456         a_unfocused_pressed_close->texture[0].type = 
457         a_focused_unpressed_desk->texture[0].type = 
458         a_focused_pressed_desk->texture[0].type = 
459         a_unfocused_unpressed_desk->texture[0].type = 
460         a_unfocused_pressed_desk->texture[0].type = 
461         a_focused_unpressed_iconify->texture[0].type = 
462         a_focused_pressed_iconify->texture[0].type = 
463         a_unfocused_unpressed_iconify->texture[0].type = 
464         a_unfocused_pressed_iconify->texture[0].type = Bitmask;
465     a_focused_unpressed_max->texture[0].data.mask.mask = 
466         a_focused_pressed_max->texture[0].data.mask.mask = 
467         a_unfocused_unpressed_max->texture[0].data.mask.mask = 
468         a_unfocused_pressed_max->texture[0].data.mask.mask = s_max_mask;
469     a_focused_unpressed_close->texture[0].data.mask.mask = 
470         a_focused_pressed_close->texture[0].data.mask.mask = 
471         a_unfocused_unpressed_close->texture[0].data.mask.mask = 
472         a_unfocused_pressed_close->texture[0].data.mask.mask = s_close_mask;
473     a_focused_unpressed_desk->texture[0].data.mask.mask = 
474         a_focused_pressed_desk->texture[0].data.mask.mask = 
475         a_unfocused_unpressed_desk->texture[0].data.mask.mask = 
476         a_unfocused_pressed_desk->texture[0].data.mask.mask = s_desk_mask;
477     a_focused_unpressed_iconify->texture[0].data.mask.mask = 
478         a_focused_pressed_iconify->texture[0].data.mask.mask = 
479         a_unfocused_unpressed_iconify->texture[0].data.mask.mask = 
480         a_unfocused_pressed_iconify->texture[0].data.mask.mask = s_icon_mask;
481     a_focused_unpressed_max->texture[0].data.mask.color = 
482         a_focused_pressed_max->texture[0].data.mask.color = 
483         a_focused_unpressed_close->texture[0].data.mask.color = 
484         a_focused_pressed_close->texture[0].data.mask.color = 
485         a_focused_unpressed_desk->texture[0].data.mask.color = 
486         a_focused_pressed_desk->texture[0].data.mask.color = 
487         a_focused_unpressed_iconify->texture[0].data.mask.color = 
488         a_focused_pressed_iconify->texture[0].data.mask.color =
489         s_titlebut_focused_color;
490     a_unfocused_unpressed_max->texture[0].data.mask.color = 
491         a_unfocused_pressed_max->texture[0].data.mask.color = 
492         a_unfocused_unpressed_close->texture[0].data.mask.color = 
493         a_unfocused_pressed_close->texture[0].data.mask.color = 
494         a_unfocused_unpressed_desk->texture[0].data.mask.color = 
495         a_unfocused_pressed_desk->texture[0].data.mask.color = 
496         a_unfocused_unpressed_iconify->texture[0].data.mask.color = 
497         a_unfocused_pressed_iconify->texture[0].data.mask.color =
498         s_titlebut_unfocused_color;
499
500     XrmDestroyDatabase(db);
501     return TRUE;
502 }
503
504