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