]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/config.c
add booleans
[mikachu/openbox.git] / openbox / config.c
1 #include "config.h"
2
3 #ifdef HAVE_STDIO_H
4 #  include <stdio.h>
5 #endif
6
7 static void config_free_entry(ConfigEntry *entry);
8 static void config_set_entry(char *name, ConfigValueType type,
9                              ConfigValue value);
10 static void config_def_free(ConfigDefEntry *entry);
11 static void print_config(GQuark q, gpointer data, gpointer fonk){
12     ConfigDefEntry *e = (ConfigDefEntry *)data;
13     g_message("config: %s %d", e->name, e->hasList);
14 }
15
16 static GData *config = NULL;
17 static GData *config_def = NULL;
18
19 /* provided by cparse.l */
20 void cparse_go(char *filename, FILE *);
21
22
23 void config_startup()
24 {
25     /* set up options exported by the kernel */
26     config_def_set(config_def_new("engine", Config_String,
27                                   "Engine",
28                                   "The name of the theming engine to be used "
29                                   "to decorate windows."));
30     config_def_set(config_def_new("theme", Config_String,
31                                   "Theme",
32                                   "The name of the theme to load with the "
33                                   "chosen engine."));
34     config_def_set(config_def_new("font", Config_String,
35                                   "Titlebar Font",
36                                   "The fontstring specifying the font to "
37                                   "be used in window titlebars."));
38     config_def_set(config_def_new("font.shadow", Config_Bool,
39                                   "Titlebar Font Shadow",
40                                   "Whether or not the text in the window "
41                                   "titlebars gets a drop shadow."));
42     config_def_set(config_def_new("font.shadow.offset", Config_Integer,
43                                   "Titlebar Font Shadow Offset",
44                                   "The offset of the drop shadow for text "
45                                   "in the window titlebars."));
46     config_def_set(config_def_new("titlebar.layout", Config_String,
47                                   "Titlebar Layout",
48                                   "The ordering of the elements in the "
49                                   "window titlebars."));
50
51     /*g_datalist_foreach(&config_def, print_config, NULL);*/
52 }
53
54 void config_shutdown()
55 {
56     g_datalist_clear(&config);
57     g_datalist_clear(&config_def);
58 }
59
60 void config_parse()
61 {
62     FILE *file;
63     char *path;
64     gboolean load = FALSE;
65
66     /* load the user rc */
67     path = g_build_filename(g_get_home_dir(), ".openbox", "rc3", NULL);
68     if ((file = fopen(path, "r")) != NULL) {
69         cparse_go(path, file);
70         fclose(file);
71         load = TRUE;
72     }
73     g_free(path);
74
75     if (!load) {
76         /* load the system wide rc */
77         path = g_build_filename(RCDIR, "rc3", NULL);
78         if ((file = fopen(path, "r")) != NULL) {
79             /*cparse_go(path, file);*/
80             fclose(file);
81         }
82         g_free(path);
83     }
84 }
85
86 gboolean config_set(char *name, ConfigValueType type, ConfigValue value)
87 {
88     ConfigDefEntry *def;
89     gboolean ret = FALSE;
90
91     name = g_ascii_strdown(name, -1);
92     g_message("Setting %s", name);
93
94     /*g_datalist_foreach(&config_def, print_config, NULL);*/
95     def = g_datalist_get_data(&config_def, name);
96
97     if (def == NULL) {
98         g_message("Invalid config option '%s'", name);
99     } else {
100         if (def->hasList) {
101             gboolean found = FALSE;
102             GSList *it;
103
104             it = def->values;
105             g_assert(it != NULL);
106             do {
107                 if (g_ascii_strcasecmp(it->data, value.string) == 0) {
108                     found = TRUE;
109                     break;
110                 }
111             } while ((it = it->next));
112
113             if (!found)
114                 g_message("Invalid value '%s' for config option '%s'",
115                           value.string, name);
116             else
117                 ret = TRUE;
118         } else
119             ret = TRUE;
120
121     }
122
123     if (ret)
124         config_set_entry(name, type, value);
125     else
126         g_free(name);
127
128     return ret;
129 }
130
131 gboolean config_get(char *name, ConfigValueType type, ConfigValue *value)
132 {
133     ConfigEntry *entry;
134     gboolean ret = FALSE;
135
136     name = g_ascii_strdown(name, -1);
137     entry = g_datalist_get_data(&config, name);
138     if (entry != NULL && entry->type == type) {
139         *value = entry->value;
140         ret = TRUE;
141     }
142     g_free(name);
143     return ret;
144 }
145
146 static void config_set_entry(char *name, ConfigValueType type,
147                              ConfigValue value)
148 {
149     ConfigEntry *entry = NULL;
150
151     entry = g_new(ConfigEntry, 1);
152     entry->name = name;
153     entry->type = type;
154     if (type == Config_String)
155         entry->value.string = g_strdup(value.string);
156     else
157         entry->value = value;
158
159     g_datalist_set_data_full(&config, name, entry,
160                              (GDestroyNotify)config_free_entry);
161 }
162
163 static void config_free_entry(ConfigEntry *entry)
164 {
165     g_free(entry->name);
166     entry->name = NULL;
167     if(entry->type == Config_String) {
168         g_free(entry->value.string);
169         entry->value.string = NULL;
170     }
171     g_free(entry);
172 }
173
174 ConfigDefEntry *config_def_new(char *name, ConfigValueType type,
175                                 char *descriptive_name, char *long_description)
176 {
177     ConfigDefEntry *entry;
178
179     entry = g_new(ConfigDefEntry, 1);
180     entry->name = g_ascii_strdown(name, -1);
181     entry->descriptive_name = g_strdup(descriptive_name);
182     entry->long_description = g_strdup(long_description);
183     entry->hasList = FALSE;
184     entry->type = type;
185     entry->values = NULL;
186     return entry;
187 }
188
189 static void config_def_free(ConfigDefEntry *entry)
190 {
191     GSList *it;
192
193     g_free(entry->name);
194     g_free(entry->descriptive_name);
195     g_free(entry->long_description);
196     if (entry->hasList) {
197         for (it = entry->values; it != NULL; it = it->next)
198             g_free(it->data);
199         g_slist_free(entry->values);
200     }
201     g_free(entry);
202 }
203
204 gboolean config_def_add_value(ConfigDefEntry *entry, char *value)
205 {
206     if (entry->type != Config_String) {
207         g_warning("Tried adding value to non-string config definition");
208         return FALSE;
209     }
210
211     entry->hasList = TRUE;
212     entry->values = g_slist_append(entry->values, g_ascii_strdown(value, -1));
213     return TRUE;
214 }
215
216 gboolean config_def_set(ConfigDefEntry *entry)
217 {
218     gboolean ret = FALSE;
219     ConfigDefEntry *def;
220
221     if ((def = g_datalist_get_data(&config_def, entry->name))) {
222         g_assert(def != entry); /* adding it twice!? */
223         g_warning("Definition already set for config option '%s'. ",
224                   entry->name);
225         config_def_free(entry);
226     } else {
227         g_datalist_set_data_full(&config_def, entry->name, entry,
228                                  (GDestroyNotify)config_def_free);
229         ret = TRUE;
230     }
231
232     return ret;
233 }