]> icculus.org git repositories - dana/openbox.git/blob - openbox/config.c
sometimes you have to declare variables too
[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
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     xmlNodePtr n;
390     for (node = node->children; node; node = node->next) {
391         if (!xmlStrcasecmp(node->name, (const xmlChar*) "file")) {
392             gchar *c;
393
394             c = parse_string(doc, node);
395             config_menu_files = g_slist_append(config_menu_files,
396                                                parse_expand_tilde(c));
397             g_free(c);
398         }
399         if ((n = parse_find_node("warpPointer", node)))
400             config_menu_warppointer = parse_bool(doc, n);
401         if ((n = parse_find_node("xorStyle", node)))
402             config_menu_xorstyle = parse_bool(doc, n);
403         if ((n = parse_find_node("hilightFirst", node)))
404             config_menu_hilightfirst = parse_bool(doc, n);
405         if ((n = parse_find_node("hideDelay", node)))
406             config_menu_hide_delay = parse_int(doc, n);
407     }
408 }
409    
410 static void parse_resistance(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node, 
411                              gpointer d)
412 {
413     xmlNodePtr n;
414
415     node = node->children;
416     if ((n = parse_find_node("strength", node)))
417         config_resist_win = parse_int(doc, n);
418     if ((n = parse_find_node("screen_edge_strength", node)))
419         config_resist_edge = parse_int(doc, n);
420 }
421
422 typedef struct
423 {
424     const gchar *key;
425     const gchar *actname;
426 } ObDefKeyBind;
427
428 static void bind_default_keyboard()
429 {
430     ObDefKeyBind *it;
431     ObDefKeyBind binds[] = {
432         { "A-Tab", "NextWindow" },
433         { "S-A-Tab", "PreviousWindow" },
434         { "A-F4", "Close" },
435         { NULL, NULL }
436     };
437
438     for (it = binds; it->key; ++it) {
439         GList *l = g_list_append(NULL, g_strdup(it->key));
440         keyboard_bind(l, action_from_string(it->actname,
441                                             OB_USER_ACTION_KEYBOARD_KEY));
442     }
443 }
444
445 typedef struct
446 {
447     const gchar *button;
448     const gchar *context;
449     const ObMouseAction mact;
450     const gchar *actname;
451 } ObDefMouseBind;
452
453 static void bind_default_mouse()
454 {
455     ObDefMouseBind *it;
456     ObDefMouseBind binds[] = {
457         { "Left", "Client", OB_MOUSE_ACTION_PRESS, "Focus" },
458         { "Middle", "Client", OB_MOUSE_ACTION_PRESS, "Focus" },
459         { "Right", "Client", OB_MOUSE_ACTION_PRESS, "Focus" },
460         { "Left", "Desktop", OB_MOUSE_ACTION_PRESS, "Focus" },
461         { "Middle", "Desktop", OB_MOUSE_ACTION_PRESS, "Focus" },
462         { "Right", "Desktop", OB_MOUSE_ACTION_PRESS, "Focus" },
463         { "Left", "Titlebar", OB_MOUSE_ACTION_PRESS, "Focus" },
464         { "Left", "Handle", OB_MOUSE_ACTION_PRESS, "Focus" },
465         { "Left", "BLCorner", OB_MOUSE_ACTION_PRESS, "Focus" },
466         { "Left", "BRCorner", OB_MOUSE_ACTION_PRESS, "Focus" },
467         { "Left", "TLCorner", OB_MOUSE_ACTION_PRESS, "Focus" },
468         { "Left", "TRCorner", OB_MOUSE_ACTION_PRESS, "Focus" },
469         { "Left", "Close", OB_MOUSE_ACTION_PRESS, "Focus" },
470         { "Left", "Maximize", OB_MOUSE_ACTION_PRESS, "Focus" },
471         { "Left", "Iconify", OB_MOUSE_ACTION_PRESS, "Focus" },
472         { "Left", "Icon", OB_MOUSE_ACTION_PRESS, "Focus" },
473         { "Left", "AllDesktops", OB_MOUSE_ACTION_PRESS, "Focus" },
474         { "Left", "Shade", OB_MOUSE_ACTION_PRESS, "Focus" },
475         { "Left", "Client", OB_MOUSE_ACTION_CLICK, "Raise" },
476         { "Left", "Titlebar", OB_MOUSE_ACTION_CLICK, "Raise" },
477         { "Middle", "Titlebar", OB_MOUSE_ACTION_CLICK, "Lower" },
478         { "Left", "Handle", OB_MOUSE_ACTION_CLICK, "Raise" },
479         { "Left", "BLCorner", OB_MOUSE_ACTION_CLICK, "Raise" },
480         { "Left", "BRCorner", OB_MOUSE_ACTION_CLICK, "Raise" },
481         { "Left", "TLCorner", OB_MOUSE_ACTION_CLICK, "Raise" },
482         { "Left", "TRCorner", OB_MOUSE_ACTION_CLICK, "Raise" },
483         { "Left", "Close", OB_MOUSE_ACTION_CLICK, "Raise" },
484         { "Left", "Maximize", OB_MOUSE_ACTION_CLICK, "Raise" },
485         { "Left", "Iconify", OB_MOUSE_ACTION_CLICK, "Raise" },
486         { "Left", "Icon", OB_MOUSE_ACTION_CLICK, "Raise" },
487         { "Left", "AllDesktops", OB_MOUSE_ACTION_CLICK, "Raise" },
488         { "Left", "Shade", OB_MOUSE_ACTION_CLICK, "Raise" },
489         { "Left", "Close", OB_MOUSE_ACTION_CLICK, "Close" },
490         { "Left", "Maximize", OB_MOUSE_ACTION_CLICK, "ToggleMaximizeFull" },
491         { "Left", "Iconify", OB_MOUSE_ACTION_CLICK, "Iconify" },
492         { "Left", "AllDesktops", OB_MOUSE_ACTION_CLICK, "ToggleOmnipresent" },
493         { "Left", "Shade", OB_MOUSE_ACTION_CLICK, "ToggleShade" },
494         { "Left", "TLCorner", OB_MOUSE_ACTION_MOTION, "Resize" },
495         { "Left", "TRCorner", OB_MOUSE_ACTION_MOTION, "Resize" },
496         { "Left", "BLCorner", OB_MOUSE_ACTION_MOTION, "Resize" },
497         { "Left", "BRCorner", OB_MOUSE_ACTION_MOTION, "Resize" },
498         { "Left", "Titlebar", OB_MOUSE_ACTION_MOTION, "Move" },
499         { "A-Left", "Frame", OB_MOUSE_ACTION_MOTION, "Move" },
500         { "A-Middle", "Frame", OB_MOUSE_ACTION_MOTION, "Resize" },
501         { NULL, NULL, 0, NULL }
502     };
503
504     for (it = binds; it->button; ++it) {
505         ObUserAction uact;
506         switch (it->mact) {
507         case OB_MOUSE_ACTION_PRESS:
508             uact = OB_USER_ACTION_MOUSE_PRESS; break;
509         case OB_MOUSE_ACTION_RELEASE:
510             uact = OB_USER_ACTION_MOUSE_RELEASE; break;
511         case OB_MOUSE_ACTION_CLICK:
512             uact = OB_USER_ACTION_MOUSE_CLICK; break;
513         case OB_MOUSE_ACTION_DOUBLE_CLICK:
514             uact = OB_USER_ACTION_MOUSE_DOUBLE_CLICK; break;
515         case OB_MOUSE_ACTION_MOTION:
516             uact = OB_USER_ACTION_MOUSE_MOTION; break;
517         case OB_NUM_MOUSE_ACTIONS:
518             g_assert_not_reached();
519         }
520         mouse_bind(it->button, it->context, it->mact,
521                    action_from_string(it->actname, uact));
522     }
523 }
524
525 void config_startup(ObParseInst *i)
526 {
527     config_focus_new = TRUE;
528     config_focus_follow = FALSE;
529     config_focus_delay = 0;
530     config_focus_raise = FALSE;
531     config_focus_last = FALSE;
532
533     parse_register(i, "focus", parse_focus, NULL);
534
535     config_place_policy = OB_PLACE_POLICY_SMART;
536
537     parse_register(i, "placement", parse_placement, NULL);
538
539     config_theme = NULL;
540
541     config_title_layout = g_strdup("NLIMC");
542
543     parse_register(i, "theme", parse_theme, NULL);
544
545     config_desktops_num = 4;
546     config_screen_firstdesk = 1;
547     config_desktops_names = NULL;
548
549     parse_register(i, "desktops", parse_desktops, NULL);
550
551     config_resize_redraw = TRUE;
552     config_resize_popup_show = 1; /* nonpixel increments */
553     config_resize_popup_pos = 0;  /* center of client */
554
555     parse_register(i, "resize", parse_resize, NULL);
556
557     config_dock_layer = OB_STACKING_LAYER_DOCK_ABOVE;
558     config_dock_pos = OB_DIRECTION_NORTHEAST;
559     config_dock_floating = FALSE;
560     config_dock_x = 0;
561     config_dock_y = 0;
562     config_dock_orient = OB_ORIENTATION_VERT;
563     config_dock_hide = FALSE;
564     config_dock_hide_delay = 300;
565     config_dock_app_move_button = 2; /* middle */
566     config_dock_app_move_modifiers = 0;
567
568     parse_register(i, "dock", parse_dock, NULL);
569
570     translate_key("C-g", &config_keyboard_reset_state,
571                   &config_keyboard_reset_keycode);
572
573     bind_default_keyboard();
574
575     parse_register(i, "keyboard", parse_keyboard, NULL);
576
577     config_mouse_threshold = 3;
578     config_mouse_dclicktime = 200;
579
580     bind_default_mouse();
581
582     parse_register(i, "mouse", parse_mouse, NULL);
583
584     config_resist_win = 10;
585     config_resist_edge = 20;
586
587     parse_register(i, "resistance", parse_resistance, NULL);
588
589     config_menu_warppointer = TRUE;
590     config_menu_xorstyle = FALSE;
591     config_menu_hilightfirst = TRUE;
592     config_menu_hide_delay = 250;
593     config_menu_files = NULL;
594
595     parse_register(i, "menu", parse_menu, NULL);
596 }
597
598 void config_shutdown()
599 {
600     GSList *it;
601
602     g_free(config_theme);
603
604     g_free(config_title_layout);
605
606     for (it = config_desktops_names; it; it = g_slist_next(it))
607         g_free(it->data);
608     g_slist_free(config_desktops_names);
609
610     for (it = config_menu_files; it; it = g_slist_next(it))
611         g_free(it->data);
612     g_slist_free(config_menu_files);
613 }