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