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