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