dont use obsolete xmlChildrenNode
[dana/openbox.git] / openbox / config.c
1 #include "config.h"
2 #include "keyboard.h"
3 #include "mouse.h"
4 #include "prop.h"
5 #include "translate.h"
6 #include "parser/parse.h"
7 #include "openbox.h"
8
9 gboolean config_focus_new;
10 gboolean config_focus_follow;
11 gboolean config_focus_last;
12 gboolean config_focus_last_on_desktop;
13 guint    config_focus_delay;
14
15 char *config_theme;
16
17 gchar *config_title_layout;
18
19 int     config_desktops_num;
20 GSList *config_desktops_names;
21
22 gboolean config_redraw_resize;
23
24 ObStackingLayer config_dock_layer;
25 gboolean        config_dock_floating;
26 ObDirection     config_dock_pos;
27 gint            config_dock_x;
28 gint            config_dock_y;
29 ObOrientation   config_dock_orient;
30 gboolean        config_dock_hide;
31 guint           config_dock_hide_timeout;
32
33 guint config_keyboard_reset_keycode;
34 guint config_keyboard_reset_state;
35
36 gint config_mouse_threshold;
37 gint config_mouse_dclicktime;
38
39 GSList *config_menu_files;
40
41 gint config_resist_win;
42 gint config_resist_edge;
43
44 /*
45
46 <keybind key="C-x">
47   <action name="ChangeDesktop">
48     <desktop>3</desktop>
49   </action>
50 </keybind>
51
52 */
53
54 static void parse_key(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
55                       GList *keylist)
56 {
57     char *key;
58     ObAction *action;
59     xmlNodePtr n, nact;
60     GList *it;
61
62     if ((n = parse_find_node("chainQuitKey", node))) {
63         key = parse_string(doc, n);
64         translate_key(key, &config_keyboard_reset_state,
65                       &config_keyboard_reset_keycode);
66         g_free(key);
67     }
68
69     n = parse_find_node("keybind", node);
70     while (n) {
71         if (parse_attr_string("key", n, &key)) {
72             keylist = g_list_append(keylist, key);
73
74             parse_key(i, doc, n->children, keylist);
75
76             it = g_list_last(keylist);
77             g_free(it->data);
78             keylist = g_list_delete_link(keylist, it);
79         }
80         n = parse_find_node("keybind", n->next);
81     }
82     if (keylist) {
83         nact = parse_find_node("action", node);
84         while (nact) {
85             if ((action = action_parse(i, doc, nact,
86                                        OB_USER_ACTION_KEYBOARD_KEY)))
87                 keyboard_bind(keylist, action);
88             nact = parse_find_node("action", nact->next);
89         }
90     }
91 }
92
93 static void parse_keyboard(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
94                            void *d)
95 {
96     parse_key(i, doc, node->children, NULL);
97 }
98
99 /*
100
101 <context name="Titlebar"> 
102   <mousebind button="Left" action="Press">
103     <action name="Raise"></action>
104   </mousebind>
105 </context>
106
107 */
108
109 static void parse_mouse(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
110                         void *d)
111 {
112     xmlNodePtr n, nbut, nact;
113     char *buttonstr;
114     char *contextstr;
115     ObUserAction uact;
116     ObMouseAction mact;
117     ObAction *action;
118
119     node = node->children;
120     
121     if ((n = parse_find_node("dragThreshold", node)))
122         config_mouse_threshold = parse_int(doc, n);
123     if ((n = parse_find_node("doubleClickTime", node)))
124         config_mouse_dclicktime = parse_int(doc, n);
125
126     n = parse_find_node("context", node);
127     while (n) {
128         if (!parse_attr_string("name", n, &contextstr))
129             goto next_n;
130         nbut = parse_find_node("mousebind", n->children);
131         while (nbut) {
132             if (!parse_attr_string("button", nbut, &buttonstr))
133                 goto next_nbut;
134             if (parse_attr_contains("press", nbut, "action")) {
135                 uact = OB_USER_ACTION_MOUSE_PRESS;
136                 mact = OB_MOUSE_ACTION_PRESS;
137             } else if (parse_attr_contains("release", nbut, "action")) {
138                 uact = OB_USER_ACTION_MOUSE_RELEASE;
139                 mact = OB_MOUSE_ACTION_RELEASE;
140             } else if (parse_attr_contains("click", nbut, "action")) {
141                 uact = OB_USER_ACTION_MOUSE_CLICK;
142                 mact = OB_MOUSE_ACTION_CLICK;
143             } else if (parse_attr_contains("doubleclick", nbut,"action")) {
144                 uact = OB_USER_ACTION_MOUSE_DOUBLE_CLICK;
145                 mact = OB_MOUSE_ACTION_DOUBLE_CLICK;
146             } else if (parse_attr_contains("drag", nbut, "action")) {
147                 uact = OB_USER_ACTION_MOUSE_MOTION;
148                 mact = OB_MOUSE_ACTION_MOTION;
149             } else
150                 goto next_nbut;
151             nact = parse_find_node("action", nbut->children);
152             while (nact) {
153                 if ((action = action_parse(i, doc, nact, uact)))
154                     mouse_bind(buttonstr, contextstr, mact, action);
155                 nact = parse_find_node("action", nact->next);
156             }
157             g_free(buttonstr);
158         next_nbut:
159             nbut = parse_find_node("mousebind", nbut->next);
160         }
161         g_free(contextstr);
162     next_n:
163         n = parse_find_node("context", n->next);
164     }
165 }
166
167 static void parse_focus(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
168                         void *d)
169 {
170     xmlNodePtr n;
171
172     node = node->children;
173     
174     if ((n = parse_find_node("focusNew", node)))
175         config_focus_new = parse_bool(doc, n);
176     if ((n = parse_find_node("followMouse", node)))
177         config_focus_follow = parse_bool(doc, n);
178     if ((n = parse_find_node("focusLast", node)))
179         config_focus_last = parse_bool(doc, n);
180     if ((n = parse_find_node("focusLastOnDesktop", node)))
181         config_focus_last_on_desktop = parse_bool(doc, n);
182     if ((n = parse_find_node("focusDelay", node)))
183         config_focus_delay = parse_int(doc, n);
184 }
185
186 static void parse_theme(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
187                         void *d)
188 {
189     xmlNodePtr n;
190
191     node = node->children;
192
193     if ((n = parse_find_node("name", node))) {
194         gchar *c;
195
196         g_free(config_theme);
197         c = parse_string(doc, n);
198         config_theme = ob_expand_tilde(c);
199         g_free(c);
200     }
201     if ((n = parse_find_node("titleLayout", node))) {
202         g_free(config_title_layout);
203         config_title_layout = parse_string(doc, n);
204     }
205 }
206
207 static void parse_desktops(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
208                            void *d)
209 {
210     xmlNodePtr n;
211
212     node = node->children;
213     
214     if ((n = parse_find_node("number", node)))
215         config_desktops_num = parse_int(doc, n);
216     if ((n = parse_find_node("names", node))) {
217         GSList *it;
218         xmlNodePtr nname;
219
220         for (it = config_desktops_names; it; it = it->next)
221             g_free(it->data);
222         g_slist_free(config_desktops_names);
223         config_desktops_names = NULL;
224
225         nname = parse_find_node("name", n->children);
226         while (nname) {
227             config_desktops_names = g_slist_append(config_desktops_names,
228                                                    parse_string(doc, nname));
229             nname = parse_find_node("name", nname->next);
230         }
231     }
232 }
233
234 static void parse_resize(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
235                          void *d)
236 {
237     xmlNodePtr n;
238
239     node = node->children;
240     
241     if ((n = parse_find_node("drawContents", node)))
242         config_redraw_resize = parse_bool(doc, n);
243 }
244
245 static void parse_dock(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node, void *d)
246 {
247     xmlNodePtr n;
248
249     node = node->children;
250
251     if ((n = parse_find_node("position", node))) {
252         if (parse_contains("TopLeft", doc, n))
253             config_dock_floating = FALSE,
254             config_dock_pos = OB_DIRECTION_NORTHWEST;
255         else if (parse_contains("Top", doc, n))
256             config_dock_floating = FALSE,
257             config_dock_pos = OB_DIRECTION_NORTH;
258         else if (parse_contains("TopRight", doc, n))
259             config_dock_floating = FALSE,
260             config_dock_pos = OB_DIRECTION_NORTHEAST;
261         else if (parse_contains("Right", doc, n))
262             config_dock_floating = FALSE,
263             config_dock_pos = OB_DIRECTION_EAST;
264         else if (parse_contains("BottomRight", doc, n))
265             config_dock_floating = FALSE,
266             config_dock_pos = OB_DIRECTION_SOUTHEAST;
267         else if (parse_contains("Bottom", doc, n))
268             config_dock_floating = FALSE,
269             config_dock_pos = OB_DIRECTION_SOUTH;
270         else if (parse_contains("BottomLeft", doc, n))
271             config_dock_floating = FALSE,
272             config_dock_pos = OB_DIRECTION_SOUTHWEST;
273         else if (parse_contains("Left", doc, n))
274             config_dock_floating = FALSE,
275             config_dock_pos = OB_DIRECTION_WEST;
276         else if (parse_contains("Floating", doc, n))
277             config_dock_floating = TRUE;
278     }
279     if (config_dock_floating) {
280         if ((n = parse_find_node("floatingX", node)))
281             config_dock_x = parse_int(doc, n);
282         if ((n = parse_find_node("floatingY", node)))
283             config_dock_y = parse_int(doc, n);
284     }
285     if ((n = parse_find_node("stacking", node))) {
286         if (parse_contains("top", doc, n))
287             config_dock_layer = OB_STACKING_LAYER_TOP;
288         else if (parse_contains("normal", doc, n))
289             config_dock_layer = OB_STACKING_LAYER_NORMAL;
290         else if (parse_contains("bottom", doc, n))
291             config_dock_layer = OB_STACKING_LAYER_BELOW;
292     }
293     if ((n = parse_find_node("direction", node))) {
294         if (parse_contains("horizontal", doc, n))
295             config_dock_orient = OB_ORIENTATION_HORZ;
296         else if (parse_contains("vertical", doc, n))
297             config_dock_orient = OB_ORIENTATION_VERT;
298     }
299     if ((n = parse_find_node("autoHide", node)))
300         config_dock_hide = parse_bool(doc, n);
301     if ((n = parse_find_node("hideTimeout", node)))
302         config_dock_hide_timeout = parse_int(doc, n);
303 }
304
305 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node, void *d)
306 {
307     for (node = node->children; node; node = node->next) {
308         if (!xmlStrcasecmp(node->name, (const xmlChar*) "file")) {
309             gchar *c;
310
311             c = parse_string(doc, node);
312             config_menu_files = g_slist_append(config_menu_files,
313                                                ob_expand_tilde(c));
314             g_free(c);
315         }
316     }
317 }
318    
319 static void parse_resistance(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node, 
320                              void *d)
321 {
322     xmlNodePtr n;
323
324     node = node->children;
325     if ((n = parse_find_node("strength", node)))
326         config_resist_win = parse_int(doc, n);
327     if ((n = parse_find_node("screen_edge_strength", node)))
328         config_resist_edge = parse_int(doc, n);
329 }
330
331 void config_startup(ObParseInst *i)
332 {
333     config_focus_new = TRUE;
334     config_focus_follow = FALSE;
335     config_focus_last = TRUE;
336     config_focus_last_on_desktop = TRUE;
337     config_focus_delay = 0;
338
339     parse_register(i, "focus", parse_focus, NULL);
340
341     config_theme = NULL;
342
343     config_title_layout = g_strdup("NLIMC");
344
345     parse_register(i, "theme", parse_theme, NULL);
346
347     config_desktops_num = 4;
348     config_desktops_names = NULL;
349
350     parse_register(i, "desktops", parse_desktops, NULL);
351
352     config_redraw_resize = TRUE;
353
354     parse_register(i, "resize", parse_resize, NULL);
355
356     config_dock_layer = OB_STACKING_LAYER_TOP;
357     config_dock_pos = OB_DIRECTION_NORTHEAST;
358     config_dock_floating = FALSE;
359     config_dock_x = 0;
360     config_dock_y = 0;
361     config_dock_orient = OB_ORIENTATION_VERT;
362     config_dock_hide = FALSE;
363     config_dock_hide_timeout = 3000;
364
365     parse_register(i, "dock", parse_dock, NULL);
366
367     translate_key("C-g", &config_keyboard_reset_state,
368                   &config_keyboard_reset_keycode);
369
370     parse_register(i, "keyboard", parse_keyboard, NULL);
371
372     config_mouse_threshold = 3;
373     config_mouse_dclicktime = 200;
374
375     parse_register(i, "mouse", parse_mouse, NULL);
376
377     config_resist_win = 10;
378     config_resist_edge = 20;
379
380     parse_register(i, "resistance", parse_resistance, NULL);
381
382     config_menu_files = NULL;
383
384     parse_register(i, "menu", parse_menu, NULL);
385 }
386
387 void config_shutdown()
388 {
389     GSList *it;
390
391     g_free(config_theme);
392
393     for (it = config_desktops_names; it; it = g_slist_next(it))
394         g_free(it->data);
395     g_slist_free(config_desktops_names);
396
397     for (it = config_menu_files; it; it = g_slist_next(it))
398         g_free(it->data);
399     g_slist_free(config_menu_files);
400 }