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