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