]> icculus.org git repositories - dana/openbox.git/blob - openbox/config.c
use gpointer for void*
[dana/openbox.git] / openbox / config.c
1 /* -*- indent-tabs-mode: nil; 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 guint    config_focus_raise;
31
32 ObPlacePolicy config_place_policy;
33
34 gchar *config_theme;
35
36 gchar *config_title_layout;
37
38 gint    config_desktops_num;
39 GSList *config_desktops_names;
40
41 gboolean config_redraw_resize;
42
43 ObStackingLayer config_dock_layer;
44 gboolean        config_dock_floating;
45 ObDirection     config_dock_pos;
46 gint            config_dock_x;
47 gint            config_dock_y;
48 ObOrientation   config_dock_orient;
49 gboolean        config_dock_hide;
50 guint           config_dock_hide_delay;
51 guint           config_dock_app_move_button;
52 guint           config_dock_app_move_modifiers;
53
54 guint config_keyboard_reset_keycode;
55 guint config_keyboard_reset_state;
56
57 gint config_mouse_threshold;
58 gint config_mouse_dclicktime;
59
60 GSList *config_menu_files;
61
62 gint config_resist_win;
63 gint config_resist_edge;
64
65 /*
66
67 <keybind key="C-x">
68   <action name="ChangeDesktop">
69     <desktop>3</desktop>
70   </action>
71 </keybind>
72
73 */
74
75 static void parse_key(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
76                       GList *keylist)
77 {
78     gchar *key;
79     ObAction *action;
80     xmlNodePtr n, nact;
81     GList *it;
82
83     if ((n = parse_find_node("chainQuitKey", node))) {
84         key = parse_string(doc, n);
85         translate_key(key, &config_keyboard_reset_state,
86                       &config_keyboard_reset_keycode);
87         g_free(key);
88     }
89
90     n = parse_find_node("keybind", node);
91     while (n) {
92         if (parse_attr_string("key", n, &key)) {
93             keylist = g_list_append(keylist, key);
94
95             parse_key(i, doc, n->children, keylist);
96
97             it = g_list_last(keylist);
98             g_free(it->data);
99             keylist = g_list_delete_link(keylist, it);
100         }
101         n = parse_find_node("keybind", n->next);
102     }
103     if (keylist) {
104         nact = parse_find_node("action", node);
105         while (nact) {
106             if ((action = action_parse(i, doc, nact,
107                                        OB_USER_ACTION_KEYBOARD_KEY)))
108                 keyboard_bind(keylist, action);
109             nact = parse_find_node("action", nact->next);
110         }
111     }
112 }
113
114 static void parse_keyboard(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
115                            gpointer d)
116 {
117     keyboard_unbind_all();
118
119     parse_key(i, doc, node->children, NULL);
120 }
121
122 /*
123
124 <context name="Titlebar"> 
125   <mousebind button="Left" action="Press">
126     <action name="Raise"></action>
127   </mousebind>
128 </context>
129
130 */
131
132 static void parse_mouse(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
133                         gpointer d)
134 {
135     xmlNodePtr n, nbut, nact;
136     gchar *buttonstr;
137     gchar *contextstr;
138     ObUserAction uact;
139     ObMouseAction mact;
140     ObAction *action;
141
142     mouse_unbind_all();
143
144     node = node->children;
145     
146     if ((n = parse_find_node("dragThreshold", node)))
147         config_mouse_threshold = parse_int(doc, n);
148     if ((n = parse_find_node("doubleClickTime", node)))
149         config_mouse_dclicktime = parse_int(doc, n);
150
151     n = parse_find_node("context", node);
152     while (n) {
153         if (!parse_attr_string("name", n, &contextstr))
154             goto next_n;
155         nbut = parse_find_node("mousebind", n->children);
156         while (nbut) {
157             if (!parse_attr_string("button", nbut, &buttonstr))
158                 goto next_nbut;
159             if (parse_attr_contains("press", nbut, "action")) {
160                 uact = OB_USER_ACTION_MOUSE_PRESS;
161                 mact = OB_MOUSE_ACTION_PRESS;
162             } else if (parse_attr_contains("release", nbut, "action")) {
163                 uact = OB_USER_ACTION_MOUSE_RELEASE;
164                 mact = OB_MOUSE_ACTION_RELEASE;
165             } else if (parse_attr_contains("click", nbut, "action")) {
166                 uact = OB_USER_ACTION_MOUSE_CLICK;
167                 mact = OB_MOUSE_ACTION_CLICK;
168             } else if (parse_attr_contains("doubleclick", nbut,"action")) {
169                 uact = OB_USER_ACTION_MOUSE_DOUBLE_CLICK;
170                 mact = OB_MOUSE_ACTION_DOUBLE_CLICK;
171             } else if (parse_attr_contains("drag", nbut, "action")) {
172                 uact = OB_USER_ACTION_MOUSE_MOTION;
173                 mact = OB_MOUSE_ACTION_MOTION;
174             } else
175                 goto next_nbut;
176             nact = parse_find_node("action", nbut->children);
177             while (nact) {
178                 if ((action = action_parse(i, doc, nact, uact)))
179                     mouse_bind(buttonstr, contextstr, mact, action);
180                 nact = parse_find_node("action", nact->next);
181             }
182             g_free(buttonstr);
183         next_nbut:
184             nbut = parse_find_node("mousebind", nbut->next);
185         }
186         g_free(contextstr);
187     next_n:
188         n = parse_find_node("context", n->next);
189     }
190 }
191
192 static void parse_focus(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
193                         gpointer d)
194 {
195     xmlNodePtr n;
196
197     node = node->children;
198     
199     if ((n = parse_find_node("focusNew", node)))
200         config_focus_new = parse_bool(doc, n);
201     if ((n = parse_find_node("followMouse", node)))
202         config_focus_follow = parse_bool(doc, n);
203     if ((n = parse_find_node("focusDelay", node)))
204         config_focus_delay = parse_int(doc, n) * 1000;
205     if ((n = parse_find_node("raiseOnFocus", node)))
206         config_focus_raise = parse_bool(doc, n);
207 }
208
209 static void parse_placement(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
210                             gpointer d)
211 {
212     xmlNodePtr n;
213
214     node = node->children;
215     
216     if ((n = parse_find_node("policy", node)))
217         if (parse_contains("UnderMouse", doc, n))
218             config_place_policy = OB_PLACE_POLICY_MOUSE;
219 }
220
221 static void parse_theme(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
222                         gpointer d)
223 {
224     xmlNodePtr n;
225
226     node = node->children;
227
228     if ((n = parse_find_node("name", node))) {
229         gchar *c;
230
231         g_free(config_theme);
232         c = parse_string(doc, n);
233         config_theme = parse_expand_tilde(c);
234         g_free(c);
235     }
236     if ((n = parse_find_node("titleLayout", node))) {
237         g_free(config_title_layout);
238         config_title_layout = parse_string(doc, n);
239     }
240 }
241
242 static void parse_desktops(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
243                            gpointer d)
244 {
245     xmlNodePtr n;
246
247     node = node->children;
248     
249     if ((n = parse_find_node("number", node))) {
250         guint d = parse_int(doc, n);
251         if (d > 0)
252             config_desktops_num = d;
253     }
254     if ((n = parse_find_node("names", node))) {
255         GSList *it;
256         xmlNodePtr nname;
257
258         for (it = config_desktops_names; it; it = it->next)
259             g_free(it->data);
260         g_slist_free(config_desktops_names);
261         config_desktops_names = NULL;
262
263         nname = parse_find_node("name", n->children);
264         while (nname) {
265             config_desktops_names = g_slist_append(config_desktops_names,
266                                                    parse_string(doc, nname));
267             nname = parse_find_node("name", nname->next);
268         }
269     }
270 }
271
272 static void parse_resize(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
273                          gpointer d)
274 {
275     xmlNodePtr n;
276
277     node = node->children;
278     
279     if ((n = parse_find_node("drawContents", node)))
280         config_redraw_resize = parse_bool(doc, n);
281 }
282
283 static void parse_dock(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
284                        gpointer d)
285 {
286     xmlNodePtr n;
287
288     node = node->children;
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_DOCK_ABOVE;
327         else if (parse_contains("normal", doc, n))
328             config_dock_layer = OB_STACKING_LAYER_DOCK_NORMAL;
329         else if (parse_contains("bottom", doc, n))
330             config_dock_layer = OB_STACKING_LAYER_DOCK_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("hideDelay", node)))
341         config_dock_hide_delay = parse_int(doc, n) * 1000;
342     if ((n = parse_find_node("moveButton", node))) {
343         gchar *str = parse_string(doc, n);
344         guint b, s;
345         if (translate_button(str, &s, &b)) {
346             config_dock_app_move_button = b;
347             config_dock_app_move_modifiers = s;
348         } else {
349             g_warning("invalid button '%s'", str);
350         }
351         g_free(str);
352     }
353 }
354
355 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
356                        gpointer d)
357 {
358     for (node = node->children; node; node = node->next) {
359         if (!xmlStrcasecmp(node->name, (const xmlChar*) "file")) {
360             gchar *c;
361
362             c = parse_string(doc, node);
363             config_menu_files = g_slist_append(config_menu_files,
364                                                parse_expand_tilde(c));
365             g_free(c);
366         }
367     }
368 }
369    
370 static void parse_resistance(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node, 
371                              gpointer d)
372 {
373     xmlNodePtr n;
374
375     node = node->children;
376     if ((n = parse_find_node("strength", node)))
377         config_resist_win = parse_int(doc, n);
378     if ((n = parse_find_node("screen_edge_strength", node)))
379         config_resist_edge = parse_int(doc, n);
380 }
381
382 typedef struct
383 {
384     const gchar *key;
385     const gchar *actname;
386 } ObDefKeyBind;
387
388 static void bind_default_keyboard()
389 {
390     ObDefKeyBind *it;
391     ObDefKeyBind binds[] = {
392         { "A-Tab", "NextWindow" },
393         { "S-A-Tab", "PreviousWindow" },
394         { "A-F4", "Close" },
395         { NULL, NULL }
396     };
397
398     for (it = binds; it->key; ++it) {
399         GList *l = g_list_append(NULL, g_strdup(it->key));
400         keyboard_bind(l, action_from_string(it->actname,
401                                             OB_USER_ACTION_KEYBOARD_KEY));
402     }
403 }
404
405 typedef struct
406 {
407     const gchar *button;
408     const gchar *context;
409     const ObMouseAction mact;
410     const gchar *actname;
411 } ObDefMouseBind;
412
413 static void bind_default_mouse()
414 {
415     ObDefMouseBind *it;
416     ObDefMouseBind binds[] = {
417         { "Left", "Client", OB_MOUSE_ACTION_PRESS, "Focus" },
418         { "Middle", "Client", OB_MOUSE_ACTION_PRESS, "Focus" },
419         { "Right", "Client", OB_MOUSE_ACTION_PRESS, "Focus" },
420         { "Left", "Desktop", OB_MOUSE_ACTION_PRESS, "Focus" },
421         { "Middle", "Desktop", OB_MOUSE_ACTION_PRESS, "Focus" },
422         { "Right", "Desktop", OB_MOUSE_ACTION_PRESS, "Focus" },
423         { "Left", "Titlebar", OB_MOUSE_ACTION_PRESS, "Focus" },
424         { "Left", "Handle", OB_MOUSE_ACTION_PRESS, "Focus" },
425         { "Left", "BLCorner", OB_MOUSE_ACTION_PRESS, "Focus" },
426         { "Left", "BRCorner", OB_MOUSE_ACTION_PRESS, "Focus" },
427         { "Left", "TLCorner", OB_MOUSE_ACTION_PRESS, "Focus" },
428         { "Left", "TRCorner", OB_MOUSE_ACTION_PRESS, "Focus" },
429         { "Left", "Close", OB_MOUSE_ACTION_PRESS, "Focus" },
430         { "Left", "Maximize", OB_MOUSE_ACTION_PRESS, "Focus" },
431         { "Left", "Iconify", OB_MOUSE_ACTION_PRESS, "Focus" },
432         { "Left", "Icon", OB_MOUSE_ACTION_PRESS, "Focus" },
433         { "Left", "AllDesktops", OB_MOUSE_ACTION_PRESS, "Focus" },
434         { "Left", "Shade", OB_MOUSE_ACTION_PRESS, "Focus" },
435         { "Left", "Client", OB_MOUSE_ACTION_CLICK, "Raise" },
436         { "Left", "Titlebar", OB_MOUSE_ACTION_CLICK, "Raise" },
437         { "Middle", "Titlebar", OB_MOUSE_ACTION_CLICK, "Lower" },
438         { "Left", "Handle", OB_MOUSE_ACTION_CLICK, "Raise" },
439         { "Left", "BLCorner", OB_MOUSE_ACTION_CLICK, "Raise" },
440         { "Left", "BRCorner", OB_MOUSE_ACTION_CLICK, "Raise" },
441         { "Left", "TLCorner", OB_MOUSE_ACTION_CLICK, "Raise" },
442         { "Left", "TRCorner", OB_MOUSE_ACTION_CLICK, "Raise" },
443         { "Left", "Close", OB_MOUSE_ACTION_CLICK, "Raise" },
444         { "Left", "Maximize", OB_MOUSE_ACTION_CLICK, "Raise" },
445         { "Left", "Iconify", OB_MOUSE_ACTION_CLICK, "Raise" },
446         { "Left", "Icon", OB_MOUSE_ACTION_CLICK, "Raise" },
447         { "Left", "AllDesktops", OB_MOUSE_ACTION_CLICK, "Raise" },
448         { "Left", "Shade", OB_MOUSE_ACTION_CLICK, "Raise" },
449         { "Left", "Close", OB_MOUSE_ACTION_CLICK, "Close" },
450         { "Left", "Maximize", OB_MOUSE_ACTION_CLICK, "ToggleMaximizeFull" },
451         { "Left", "Iconify", OB_MOUSE_ACTION_CLICK, "Iconify" },
452         { "Left", "AllDesktops", OB_MOUSE_ACTION_CLICK, "ToggleOmnipresent" },
453         { "Left", "Shade", OB_MOUSE_ACTION_CLICK, "ToggleShade" },
454         { "Left", "TLCorner", OB_MOUSE_ACTION_MOTION, "Resize" },
455         { "Left", "TRCorner", OB_MOUSE_ACTION_MOTION, "Resize" },
456         { "Left", "BLCorner", OB_MOUSE_ACTION_MOTION, "Resize" },
457         { "Left", "BRCorner", OB_MOUSE_ACTION_MOTION, "Resize" },
458         { "Left", "Titlebar", OB_MOUSE_ACTION_MOTION, "Move" },
459         { "A-Left", "Frame", OB_MOUSE_ACTION_MOTION, "Move" },
460         { "A-Middle", "Frame", OB_MOUSE_ACTION_MOTION, "Resize" },
461         { NULL, NULL, 0, NULL }
462     };
463
464     for (it = binds; it->button; ++it) {
465         ObUserAction uact;
466         switch (it->mact) {
467         case OB_MOUSE_ACTION_PRESS:
468             uact = OB_USER_ACTION_MOUSE_PRESS; break;
469         case OB_MOUSE_ACTION_RELEASE:
470             uact = OB_USER_ACTION_MOUSE_RELEASE; break;
471         case OB_MOUSE_ACTION_CLICK:
472             uact = OB_USER_ACTION_MOUSE_CLICK; break;
473         case OB_MOUSE_ACTION_DOUBLE_CLICK:
474             uact = OB_USER_ACTION_MOUSE_DOUBLE_CLICK; break;
475         case OB_MOUSE_ACTION_MOTION:
476             uact = OB_USER_ACTION_MOUSE_MOTION; break;
477         case OB_NUM_MOUSE_ACTIONS:
478             g_assert_not_reached();
479         }
480         mouse_bind(it->button, it->context, it->mact,
481                    action_from_string(it->actname, uact));
482     }
483 }
484
485 void config_startup(ObParseInst *i)
486 {
487     config_focus_new = TRUE;
488     config_focus_follow = FALSE;
489     config_focus_delay = 0;
490     config_focus_raise = FALSE;
491
492     parse_register(i, "focus", parse_focus, NULL);
493
494     config_place_policy = OB_PLACE_POLICY_SMART;
495
496     parse_register(i, "placement", parse_placement, NULL);
497
498     config_theme = NULL;
499
500     config_title_layout = g_strdup("NLIMC");
501
502     parse_register(i, "theme", parse_theme, NULL);
503
504     config_desktops_num = 4;
505     config_desktops_names = NULL;
506
507     parse_register(i, "desktops", parse_desktops, NULL);
508
509     config_redraw_resize = TRUE;
510
511     parse_register(i, "resize", parse_resize, NULL);
512
513     config_dock_layer = OB_STACKING_LAYER_DOCK_ABOVE;
514     config_dock_pos = OB_DIRECTION_NORTHEAST;
515     config_dock_floating = FALSE;
516     config_dock_x = 0;
517     config_dock_y = 0;
518     config_dock_orient = OB_ORIENTATION_VERT;
519     config_dock_hide = FALSE;
520     config_dock_hide_delay = 300;
521     config_dock_app_move_button = 2; /* middle */
522     config_dock_app_move_modifiers = 0;
523
524     parse_register(i, "dock", parse_dock, NULL);
525
526     translate_key("C-g", &config_keyboard_reset_state,
527                   &config_keyboard_reset_keycode);
528
529     bind_default_keyboard();
530
531     parse_register(i, "keyboard", parse_keyboard, NULL);
532
533     config_mouse_threshold = 3;
534     config_mouse_dclicktime = 200;
535
536     bind_default_mouse();
537
538     parse_register(i, "mouse", parse_mouse, NULL);
539
540     config_resist_win = 10;
541     config_resist_edge = 20;
542
543     parse_register(i, "resistance", parse_resistance, NULL);
544
545     config_menu_files = NULL;
546
547     parse_register(i, "menu", parse_menu, NULL);
548 }
549
550 void config_shutdown()
551 {
552     GSList *it;
553
554     g_free(config_theme);
555
556     g_free(config_title_layout);
557
558     for (it = config_desktops_names; it; it = g_slist_next(it))
559         g_free(it->data);
560     g_slist_free(config_desktops_names);
561
562     for (it = config_menu_files; it; it = g_slist_next(it))
563         g_free(it->data);
564     g_slist_free(config_menu_files);
565 }