]> icculus.org git repositories - dana/openbox.git/blob - openbox/config.c
on second thought, don't change the default behaviour in stable series
[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) 2004        Mikael Magnusson
5    Copyright (c) 2003        Ben Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "config.h"
21 #include "keyboard.h"
22 #include "mouse.h"
23 #include "prop.h"
24 #include "translate.h"
25 #include "parser/parse.h"
26 #include "openbox.h"
27
28 gboolean config_focus_new;
29 gboolean config_focus_follow;
30 guint    config_focus_delay;
31 gboolean config_focus_raise;
32 gboolean config_focus_last;
33
34 ObPlacePolicy config_place_policy;
35
36 gchar   *config_theme;
37 gboolean config_theme_keepborder;
38
39 gchar *config_title_layout;
40
41 gint    config_desktops_num;
42 GSList *config_desktops_names;
43 gint    config_screen_firstdesk;
44
45 gboolean config_resize_redraw;
46 gint     config_resize_popup_show;
47 gint     config_resize_popup_pos;
48
49 ObStackingLayer config_dock_layer;
50 gboolean        config_dock_floating;
51 ObDirection     config_dock_pos;
52 gint            config_dock_x;
53 gint            config_dock_y;
54 ObOrientation   config_dock_orient;
55 gboolean        config_dock_hide;
56 guint           config_dock_hide_delay;
57 guint           config_dock_app_move_button;
58 guint           config_dock_app_move_modifiers;
59
60 guint config_keyboard_reset_keycode;
61 guint config_keyboard_reset_state;
62
63 gint config_mouse_threshold;
64 gint config_mouse_dclicktime;
65
66 gboolean config_menu_warppointer;
67 gboolean config_menu_xorstyle;
68 gboolean config_menu_hilightfirst;
69 guint    config_menu_hide_delay;
70
71 GSList *config_menu_files;
72
73 gint config_resist_win;
74 gint config_resist_edge;
75
76 /*
77
78 <keybind key="C-x">
79   <action name="ChangeDesktop">
80     <desktop>3</desktop>
81   </action>
82 </keybind>
83
84 */
85
86 static void parse_key(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
87                       GList *keylist)
88 {
89     gchar *key;
90     ObAction *action;
91     xmlNodePtr n, nact;
92     GList *it;
93
94     if ((n = parse_find_node("chainQuitKey", node))) {
95         key = parse_string(doc, n);
96         translate_key(key, &config_keyboard_reset_state,
97                       &config_keyboard_reset_keycode);
98         g_free(key);
99     }
100
101     n = parse_find_node("keybind", node);
102     while (n) {
103         if (parse_attr_string("key", n, &key)) {
104             keylist = g_list_append(keylist, key);
105
106             parse_key(i, doc, n->children, keylist);
107
108             it = g_list_last(keylist);
109             g_free(it->data);
110             keylist = g_list_delete_link(keylist, it);
111         }
112         n = parse_find_node("keybind", n->next);
113     }
114     if (keylist) {
115         nact = parse_find_node("action", node);
116         while (nact) {
117             if ((action = action_parse(i, doc, nact,
118                                        OB_USER_ACTION_KEYBOARD_KEY)))
119                 keyboard_bind(keylist, action);
120             nact = parse_find_node("action", nact->next);
121         }
122     }
123 }
124
125 static void parse_keyboard(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
126                            gpointer d)
127 {
128     keyboard_unbind_all();
129
130     parse_key(i, doc, node->children, NULL);
131 }
132
133 /*
134
135 <context name="Titlebar"> 
136   <mousebind button="Left" action="Press">
137     <action name="Raise"></action>
138   </mousebind>
139 </context>
140
141 */
142
143 static void parse_mouse(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
144                         gpointer d)
145 {
146     xmlNodePtr n, nbut, nact;
147     gchar *buttonstr;
148     gchar *contextstr;
149     ObUserAction uact;
150     ObMouseAction mact;
151     ObAction *action;
152
153     mouse_unbind_all();
154
155     node = node->children;
156     
157     if ((n = parse_find_node("dragThreshold", node)))
158         config_mouse_threshold = parse_int(doc, n);
159     if ((n = parse_find_node("doubleClickTime", node)))
160         config_mouse_dclicktime = parse_int(doc, n);
161
162     n = parse_find_node("context", node);
163     while (n) {
164         if (!parse_attr_string("name", n, &contextstr))
165             goto next_n;
166         nbut = parse_find_node("mousebind", n->children);
167         while (nbut) {
168             if (!parse_attr_string("button", nbut, &buttonstr))
169                 goto next_nbut;
170             if (parse_attr_contains("press", nbut, "action")) {
171                 uact = OB_USER_ACTION_MOUSE_PRESS;
172                 mact = OB_MOUSE_ACTION_PRESS;
173             } else if (parse_attr_contains("release", nbut, "action")) {
174                 uact = OB_USER_ACTION_MOUSE_RELEASE;
175                 mact = OB_MOUSE_ACTION_RELEASE;
176             } else if (parse_attr_contains("click", nbut, "action")) {
177                 uact = OB_USER_ACTION_MOUSE_CLICK;
178                 mact = OB_MOUSE_ACTION_CLICK;
179             } else if (parse_attr_contains("doubleclick", nbut,"action")) {
180                 uact = OB_USER_ACTION_MOUSE_DOUBLE_CLICK;
181                 mact = OB_MOUSE_ACTION_DOUBLE_CLICK;
182             } else if (parse_attr_contains("drag", nbut, "action")) {
183                 uact = OB_USER_ACTION_MOUSE_MOTION;
184                 mact = OB_MOUSE_ACTION_MOTION;
185             } else
186                 goto next_nbut;
187             nact = parse_find_node("action", nbut->children);
188             while (nact) {
189                 if ((action = action_parse(i, doc, nact, uact)))
190                     mouse_bind(buttonstr, contextstr, mact, action);
191                 nact = parse_find_node("action", nact->next);
192             }
193             g_free(buttonstr);
194         next_nbut:
195             nbut = parse_find_node("mousebind", nbut->next);
196         }
197         g_free(contextstr);
198     next_n:
199         n = parse_find_node("context", n->next);
200     }
201 }
202
203 static void parse_focus(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
204                         gpointer d)
205 {
206     xmlNodePtr n;
207
208     node = node->children;
209     
210     if ((n = parse_find_node("focusNew", node)))
211         config_focus_new = parse_bool(doc, n);
212     if ((n = parse_find_node("followMouse", node)))
213         config_focus_follow = parse_bool(doc, n);
214     if ((n = parse_find_node("focusDelay", node)))
215         config_focus_delay = parse_int(doc, n) * 1000;
216     if ((n = parse_find_node("raiseOnFocus", node)))
217         config_focus_raise = parse_bool(doc, n);
218     if ((n = parse_find_node("focusLast", node)))
219         config_focus_last = parse_bool(doc, n);
220 }
221
222 static void parse_placement(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
223                             gpointer d)
224 {
225     xmlNodePtr n;
226
227     node = node->children;
228     
229     if ((n = parse_find_node("policy", node)))
230         if (parse_contains("UnderMouse", doc, n))
231             config_place_policy = OB_PLACE_POLICY_MOUSE;
232 }
233
234 static void parse_theme(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
235                         gpointer d)
236 {
237     xmlNodePtr n;
238
239     node = node->children;
240
241     if ((n = parse_find_node("name", node))) {
242         gchar *c;
243
244         g_free(config_theme);
245         c = parse_string(doc, n);
246         config_theme = parse_expand_tilde(c);
247         g_free(c);
248     }
249     if ((n = parse_find_node("titleLayout", node))) {
250         g_free(config_title_layout);
251         config_title_layout = parse_string(doc, n);
252     }
253     if ((n = parse_find_node("keepBorder", node)))
254         config_theme_keepborder = parse_bool(doc, n);
255 }
256
257 static void parse_desktops(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
258                            gpointer d)
259 {
260     xmlNodePtr n;
261
262     node = node->children;
263     
264     if ((n = parse_find_node("number", node))) {
265         gint d = parse_int(doc, n);
266         if (d > 0)
267             config_desktops_num = d;
268     }
269     if ((n = parse_find_node("firstdesk", node))) {
270         gint d = parse_int(doc, n);
271         if (d > 0)
272             config_screen_firstdesk = d;
273     }
274     if ((n = parse_find_node("names", node))) {
275         GSList *it;
276         xmlNodePtr nname;
277
278         for (it = config_desktops_names; it; it = it->next)
279             g_free(it->data);
280         g_slist_free(config_desktops_names);
281         config_desktops_names = NULL;
282
283         nname = parse_find_node("name", n->children);
284         while (nname) {
285             config_desktops_names = g_slist_append(config_desktops_names,
286                                                    parse_string(doc, nname));
287             nname = parse_find_node("name", nname->next);
288         }
289     }
290 }
291
292 static void parse_resize(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
293                          gpointer d)
294 {
295     xmlNodePtr n;
296
297     node = node->children;
298     
299     if ((n = parse_find_node("drawContents", node)))
300         config_resize_redraw = parse_bool(doc, n);
301     if ((n = parse_find_node("popupShow", node))) {
302         config_resize_popup_show = parse_int(doc, n);
303         if (parse_contains("Always", doc, n))
304             config_resize_popup_show = 2;
305         else if (parse_contains("Never", doc, n))
306             config_resize_popup_show = 0;
307         else if (parse_contains("Nonpixel", doc, n))
308             config_resize_popup_show = 1;
309     }
310     if ((n = parse_find_node("popupPosition", node))) {
311         config_resize_popup_pos = parse_int(doc, n);
312         if (parse_contains("Top", doc, n))
313             config_resize_popup_pos = 1;
314         else if (parse_contains("Center", doc, n))
315             config_resize_popup_pos = 0;
316     }
317 }
318
319 static void parse_dock(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
320                        gpointer d)
321 {
322     xmlNodePtr n;
323
324     node = node->children;
325
326     if ((n = parse_find_node("position", node))) {
327         if (parse_contains("TopLeft", doc, n))
328             config_dock_floating = FALSE,
329             config_dock_pos = OB_DIRECTION_NORTHWEST;
330         else if (parse_contains("Top", doc, n))
331             config_dock_floating = FALSE,
332             config_dock_pos = OB_DIRECTION_NORTH;
333         else if (parse_contains("TopRight", doc, n))
334             config_dock_floating = FALSE,
335             config_dock_pos = OB_DIRECTION_NORTHEAST;
336         else if (parse_contains("Right", doc, n))
337             config_dock_floating = FALSE,
338             config_dock_pos = OB_DIRECTION_EAST;
339         else if (parse_contains("BottomRight", doc, n))
340             config_dock_floating = FALSE,
341             config_dock_pos = OB_DIRECTION_SOUTHEAST;
342         else if (parse_contains("Bottom", doc, n))
343             config_dock_floating = FALSE,
344             config_dock_pos = OB_DIRECTION_SOUTH;
345         else if (parse_contains("BottomLeft", doc, n))
346             config_dock_floating = FALSE,
347             config_dock_pos = OB_DIRECTION_SOUTHWEST;
348         else if (parse_contains("Left", doc, n))
349             config_dock_floating = FALSE,
350             config_dock_pos = OB_DIRECTION_WEST;
351         else if (parse_contains("Floating", doc, n))
352             config_dock_floating = TRUE;
353     }
354     if (config_dock_floating) {
355         if ((n = parse_find_node("floatingX", node)))
356             config_dock_x = parse_int(doc, n);
357         if ((n = parse_find_node("floatingY", node)))
358             config_dock_y = parse_int(doc, n);
359     }
360     if ((n = parse_find_node("stacking", node))) {
361         if (parse_contains("top", doc, n))
362             config_dock_layer = OB_STACKING_LAYER_DOCK_ABOVE;
363         else if (parse_contains("normal", doc, n))
364             config_dock_layer = OB_STACKING_LAYER_DOCK_NORMAL;
365         else if (parse_contains("bottom", doc, n))
366             config_dock_layer = OB_STACKING_LAYER_DOCK_BELOW;
367     }
368     if ((n = parse_find_node("direction", node))) {
369         if (parse_contains("horizontal", doc, n))
370             config_dock_orient = OB_ORIENTATION_HORZ;
371         else if (parse_contains("vertical", doc, n))
372             config_dock_orient = OB_ORIENTATION_VERT;
373     }
374     if ((n = parse_find_node("autoHide", node)))
375         config_dock_hide = parse_bool(doc, n);
376     if ((n = parse_find_node("hideDelay", node)))
377         config_dock_hide_delay = parse_int(doc, n) * 1000;
378     if ((n = parse_find_node("moveButton", node))) {
379         gchar *str = parse_string(doc, n);
380         guint b, s;
381         if (translate_button(str, &s, &b)) {
382             config_dock_app_move_button = b;
383             config_dock_app_move_modifiers = s;
384         } else {
385             g_warning("invalid button '%s'", str);
386         }
387         g_free(str);
388     }
389 }
390
391 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
392                        gpointer d)
393 {
394     xmlNodePtr n;
395     for (node = node->children; node; node = node->next) {
396         if (!xmlStrcasecmp(node->name, (const xmlChar*) "file")) {
397             gchar *c;
398
399             c = parse_string(doc, node);
400             config_menu_files = g_slist_append(config_menu_files,
401                                                parse_expand_tilde(c));
402             g_free(c);
403         }
404         if ((n = parse_find_node("warpPointer", node)))
405             config_menu_warppointer = parse_bool(doc, n);
406         if ((n = parse_find_node("xorStyle", node)))
407             config_menu_xorstyle = parse_bool(doc, n);
408         if ((n = parse_find_node("hilightFirst", node)))
409             config_menu_hilightfirst = parse_bool(doc, n);
410         if ((n = parse_find_node("hideDelay", node)))
411             config_menu_hide_delay = parse_int(doc, n);
412     }
413 }
414    
415 static void parse_resistance(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node, 
416                              gpointer d)
417 {
418     xmlNodePtr n;
419
420     node = node->children;
421     if ((n = parse_find_node("strength", node)))
422         config_resist_win = parse_int(doc, n);
423     if ((n = parse_find_node("screen_edge_strength", node)))
424         config_resist_edge = parse_int(doc, n);
425 }
426
427 typedef struct
428 {
429     const gchar *key;
430     const gchar *actname;
431 } ObDefKeyBind;
432
433 static void bind_default_keyboard()
434 {
435     ObDefKeyBind *it;
436     ObDefKeyBind binds[] = {
437         { "A-Tab", "NextWindow" },
438         { "S-A-Tab", "PreviousWindow" },
439         { "A-F4", "Close" },
440         { NULL, NULL }
441     };
442
443     for (it = binds; it->key; ++it) {
444         GList *l = g_list_append(NULL, g_strdup(it->key));
445         keyboard_bind(l, action_from_string(it->actname,
446                                             OB_USER_ACTION_KEYBOARD_KEY));
447     }
448 }
449
450 typedef struct
451 {
452     const gchar *button;
453     const gchar *context;
454     const ObMouseAction mact;
455     const gchar *actname;
456 } ObDefMouseBind;
457
458 static void bind_default_mouse()
459 {
460     ObDefMouseBind *it;
461     ObDefMouseBind binds[] = {
462         { "Left", "Client", OB_MOUSE_ACTION_PRESS, "Focus" },
463         { "Middle", "Client", OB_MOUSE_ACTION_PRESS, "Focus" },
464         { "Right", "Client", OB_MOUSE_ACTION_PRESS, "Focus" },
465         { "Left", "Desktop", OB_MOUSE_ACTION_PRESS, "Focus" },
466         { "Middle", "Desktop", OB_MOUSE_ACTION_PRESS, "Focus" },
467         { "Right", "Desktop", OB_MOUSE_ACTION_PRESS, "Focus" },
468         { "Left", "Titlebar", OB_MOUSE_ACTION_PRESS, "Focus" },
469         { "Left", "Handle", OB_MOUSE_ACTION_PRESS, "Focus" },
470         { "Left", "BLCorner", OB_MOUSE_ACTION_PRESS, "Focus" },
471         { "Left", "BRCorner", OB_MOUSE_ACTION_PRESS, "Focus" },
472         { "Left", "TLCorner", OB_MOUSE_ACTION_PRESS, "Focus" },
473         { "Left", "TRCorner", OB_MOUSE_ACTION_PRESS, "Focus" },
474         { "Left", "Close", OB_MOUSE_ACTION_PRESS, "Focus" },
475         { "Left", "Maximize", OB_MOUSE_ACTION_PRESS, "Focus" },
476         { "Left", "Iconify", OB_MOUSE_ACTION_PRESS, "Focus" },
477         { "Left", "Icon", OB_MOUSE_ACTION_PRESS, "Focus" },
478         { "Left", "AllDesktops", OB_MOUSE_ACTION_PRESS, "Focus" },
479         { "Left", "Shade", OB_MOUSE_ACTION_PRESS, "Focus" },
480         { "Left", "Client", OB_MOUSE_ACTION_CLICK, "Raise" },
481         { "Left", "Titlebar", OB_MOUSE_ACTION_CLICK, "Raise" },
482         { "Middle", "Titlebar", OB_MOUSE_ACTION_CLICK, "Lower" },
483         { "Left", "Handle", OB_MOUSE_ACTION_CLICK, "Raise" },
484         { "Left", "BLCorner", OB_MOUSE_ACTION_CLICK, "Raise" },
485         { "Left", "BRCorner", OB_MOUSE_ACTION_CLICK, "Raise" },
486         { "Left", "TLCorner", OB_MOUSE_ACTION_CLICK, "Raise" },
487         { "Left", "TRCorner", OB_MOUSE_ACTION_CLICK, "Raise" },
488         { "Left", "Close", OB_MOUSE_ACTION_CLICK, "Raise" },
489         { "Left", "Maximize", OB_MOUSE_ACTION_CLICK, "Raise" },
490         { "Left", "Iconify", OB_MOUSE_ACTION_CLICK, "Raise" },
491         { "Left", "Icon", OB_MOUSE_ACTION_CLICK, "Raise" },
492         { "Left", "AllDesktops", OB_MOUSE_ACTION_CLICK, "Raise" },
493         { "Left", "Shade", OB_MOUSE_ACTION_CLICK, "Raise" },
494         { "Left", "Close", OB_MOUSE_ACTION_CLICK, "Close" },
495         { "Left", "Maximize", OB_MOUSE_ACTION_CLICK, "ToggleMaximizeFull" },
496         { "Left", "Iconify", OB_MOUSE_ACTION_CLICK, "Iconify" },
497         { "Left", "AllDesktops", OB_MOUSE_ACTION_CLICK, "ToggleOmnipresent" },
498         { "Left", "Shade", OB_MOUSE_ACTION_CLICK, "ToggleShade" },
499         { "Left", "TLCorner", OB_MOUSE_ACTION_MOTION, "Resize" },
500         { "Left", "TRCorner", OB_MOUSE_ACTION_MOTION, "Resize" },
501         { "Left", "BLCorner", OB_MOUSE_ACTION_MOTION, "Resize" },
502         { "Left", "BRCorner", OB_MOUSE_ACTION_MOTION, "Resize" },
503         { "Left", "Titlebar", OB_MOUSE_ACTION_MOTION, "Move" },
504         { "A-Left", "Frame", OB_MOUSE_ACTION_MOTION, "Move" },
505         { "A-Middle", "Frame", OB_MOUSE_ACTION_MOTION, "Resize" },
506         { NULL, NULL, 0, NULL }
507     };
508
509     for (it = binds; it->button; ++it) {
510         ObUserAction uact;
511         switch (it->mact) {
512         case OB_MOUSE_ACTION_PRESS:
513             uact = OB_USER_ACTION_MOUSE_PRESS; break;
514         case OB_MOUSE_ACTION_RELEASE:
515             uact = OB_USER_ACTION_MOUSE_RELEASE; break;
516         case OB_MOUSE_ACTION_CLICK:
517             uact = OB_USER_ACTION_MOUSE_CLICK; break;
518         case OB_MOUSE_ACTION_DOUBLE_CLICK:
519             uact = OB_USER_ACTION_MOUSE_DOUBLE_CLICK; break;
520         case OB_MOUSE_ACTION_MOTION:
521             uact = OB_USER_ACTION_MOUSE_MOTION; break;
522         case OB_NUM_MOUSE_ACTIONS:
523             g_assert_not_reached();
524         }
525         mouse_bind(it->button, it->context, it->mact,
526                    action_from_string(it->actname, uact));
527     }
528 }
529
530 void config_startup(ObParseInst *i)
531 {
532     config_focus_new = TRUE;
533     config_focus_follow = FALSE;
534     config_focus_delay = 0;
535     config_focus_raise = FALSE;
536     config_focus_last = FALSE;
537
538     parse_register(i, "focus", parse_focus, NULL);
539
540     config_place_policy = OB_PLACE_POLICY_SMART;
541
542     parse_register(i, "placement", parse_placement, NULL);
543
544     config_theme = NULL;
545
546     config_title_layout = g_strdup("NLIMC");
547     config_theme_keepborder = TRUE;
548
549     parse_register(i, "theme", parse_theme, NULL);
550
551     config_desktops_num = 4;
552     config_screen_firstdesk = 1;
553     config_desktops_names = NULL;
554
555     parse_register(i, "desktops", parse_desktops, NULL);
556
557     config_resize_redraw = TRUE;
558     config_resize_popup_show = 1; /* nonpixel increments */
559     config_resize_popup_pos = 0;  /* center of client */
560
561     parse_register(i, "resize", parse_resize, NULL);
562
563     config_dock_layer = OB_STACKING_LAYER_DOCK_ABOVE;
564     config_dock_pos = OB_DIRECTION_NORTHEAST;
565     config_dock_floating = FALSE;
566     config_dock_x = 0;
567     config_dock_y = 0;
568     config_dock_orient = OB_ORIENTATION_VERT;
569     config_dock_hide = FALSE;
570     config_dock_hide_delay = 300;
571     config_dock_app_move_button = 2; /* middle */
572     config_dock_app_move_modifiers = 0;
573
574     parse_register(i, "dock", parse_dock, NULL);
575
576     translate_key("C-g", &config_keyboard_reset_state,
577                   &config_keyboard_reset_keycode);
578
579     bind_default_keyboard();
580
581     parse_register(i, "keyboard", parse_keyboard, NULL);
582
583     config_mouse_threshold = 3;
584     config_mouse_dclicktime = 200;
585
586     bind_default_mouse();
587
588     parse_register(i, "mouse", parse_mouse, NULL);
589
590     config_resist_win = 10;
591     config_resist_edge = 20;
592
593     parse_register(i, "resistance", parse_resistance, NULL);
594
595     config_menu_warppointer = TRUE;
596     config_menu_xorstyle = TRUE;
597     config_menu_hilightfirst = TRUE;
598     config_menu_hide_delay = 250;
599     config_menu_files = NULL;
600
601     parse_register(i, "menu", parse_menu, NULL);
602 }
603
604 void config_shutdown()
605 {
606     GSList *it;
607
608     g_free(config_theme);
609
610     g_free(config_title_layout);
611
612     for (it = config_desktops_names; it; it = g_slist_next(it))
613         g_free(it->data);
614     g_slist_free(config_desktops_names);
615
616     for (it = config_menu_files; it; it = g_slist_next(it))
617         g_free(it->data);
618     g_slist_free(config_menu_files);
619 }