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