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