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