]> icculus.org git repositories - dana/openbox.git/blob - openbox/config.c
take numbers as args for the popup options 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         config_resize_popup_show = parse_int(doc, n);
300         if (parse_contains("Always", doc, n))
301             config_resize_popup_show = 2;
302         else if (parse_contains("Never", doc, n))
303             config_resize_popup_show = 0;
304         else if (parse_contains("Nonpixel", doc, n))
305             config_resize_popup_show = 1;
306     }
307     if ((n = parse_find_node("popupPosition", node))) {
308         config_resize_popup_pos = parse_int(doc, n);
309         if (parse_contains("Top", doc, n))
310             config_resize_popup_pos = 1;
311         else if (parse_contains("Center", doc, n))
312             config_resize_popup_pos = 0;
313     }
314 }
315
316 static void parse_dock(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
317                        gpointer d)
318 {
319     xmlNodePtr n;
320
321     node = node->children;
322
323     if ((n = parse_find_node("position", node))) {
324         if (parse_contains("TopLeft", doc, n))
325             config_dock_floating = FALSE,
326             config_dock_pos = OB_DIRECTION_NORTHWEST;
327         else if (parse_contains("Top", doc, n))
328             config_dock_floating = FALSE,
329             config_dock_pos = OB_DIRECTION_NORTH;
330         else if (parse_contains("TopRight", doc, n))
331             config_dock_floating = FALSE,
332             config_dock_pos = OB_DIRECTION_NORTHEAST;
333         else if (parse_contains("Right", doc, n))
334             config_dock_floating = FALSE,
335             config_dock_pos = OB_DIRECTION_EAST;
336         else if (parse_contains("BottomRight", doc, n))
337             config_dock_floating = FALSE,
338             config_dock_pos = OB_DIRECTION_SOUTHEAST;
339         else if (parse_contains("Bottom", doc, n))
340             config_dock_floating = FALSE,
341             config_dock_pos = OB_DIRECTION_SOUTH;
342         else if (parse_contains("BottomLeft", doc, n))
343             config_dock_floating = FALSE,
344             config_dock_pos = OB_DIRECTION_SOUTHWEST;
345         else if (parse_contains("Left", doc, n))
346             config_dock_floating = FALSE,
347             config_dock_pos = OB_DIRECTION_WEST;
348         else if (parse_contains("Floating", doc, n))
349             config_dock_floating = TRUE;
350     }
351     if (config_dock_floating) {
352         if ((n = parse_find_node("floatingX", node)))
353             config_dock_x = parse_int(doc, n);
354         if ((n = parse_find_node("floatingY", node)))
355             config_dock_y = parse_int(doc, n);
356     }
357     if ((n = parse_find_node("stacking", node))) {
358         if (parse_contains("top", doc, n))
359             config_dock_layer = OB_STACKING_LAYER_DOCK_ABOVE;
360         else if (parse_contains("normal", doc, n))
361             config_dock_layer = OB_STACKING_LAYER_DOCK_NORMAL;
362         else if (parse_contains("bottom", doc, n))
363             config_dock_layer = OB_STACKING_LAYER_DOCK_BELOW;
364     }
365     if ((n = parse_find_node("direction", node))) {
366         if (parse_contains("horizontal", doc, n))
367             config_dock_orient = OB_ORIENTATION_HORZ;
368         else if (parse_contains("vertical", doc, n))
369             config_dock_orient = OB_ORIENTATION_VERT;
370     }
371     if ((n = parse_find_node("autoHide", node)))
372         config_dock_hide = parse_bool(doc, n);
373     if ((n = parse_find_node("hideDelay", node)))
374         config_dock_hide_delay = parse_int(doc, n) * 1000;
375     if ((n = parse_find_node("moveButton", node))) {
376         gchar *str = parse_string(doc, n);
377         guint b, s;
378         if (translate_button(str, &s, &b)) {
379             config_dock_app_move_button = b;
380             config_dock_app_move_modifiers = s;
381         } else {
382             g_warning("invalid button '%s'", str);
383         }
384         g_free(str);
385     }
386 }
387
388 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
389                        gpointer d)
390 {
391     xmlNodePtr n;
392     for (node = node->children; node; node = node->next) {
393         if (!xmlStrcasecmp(node->name, (const xmlChar*) "file")) {
394             gchar *c;
395
396             c = parse_string(doc, node);
397             config_menu_files = g_slist_append(config_menu_files,
398                                                parse_expand_tilde(c));
399             g_free(c);
400         }
401         if ((n = parse_find_node("warpPointer", node)))
402             config_menu_warppointer = parse_bool(doc, n);
403         if ((n = parse_find_node("xorStyle", node)))
404             config_menu_xorstyle = parse_bool(doc, n);
405         if ((n = parse_find_node("hilightFirst", node)))
406             config_menu_hilightfirst = parse_bool(doc, n);
407         if ((n = parse_find_node("hideDelay", node)))
408             config_menu_hide_delay = parse_int(doc, n);
409     }
410 }
411    
412 static void parse_resistance(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node, 
413                              gpointer d)
414 {
415     xmlNodePtr n;
416
417     node = node->children;
418     if ((n = parse_find_node("strength", node)))
419         config_resist_win = parse_int(doc, n);
420     if ((n = parse_find_node("screen_edge_strength", node)))
421         config_resist_edge = parse_int(doc, n);
422 }
423
424 typedef struct
425 {
426     const gchar *key;
427     const gchar *actname;
428 } ObDefKeyBind;
429
430 static void bind_default_keyboard()
431 {
432     ObDefKeyBind *it;
433     ObDefKeyBind binds[] = {
434         { "A-Tab", "NextWindow" },
435         { "S-A-Tab", "PreviousWindow" },
436         { "A-F4", "Close" },
437         { NULL, NULL }
438     };
439
440     for (it = binds; it->key; ++it) {
441         GList *l = g_list_append(NULL, g_strdup(it->key));
442         keyboard_bind(l, action_from_string(it->actname,
443                                             OB_USER_ACTION_KEYBOARD_KEY));
444     }
445 }
446
447 typedef struct
448 {
449     const gchar *button;
450     const gchar *context;
451     const ObMouseAction mact;
452     const gchar *actname;
453 } ObDefMouseBind;
454
455 static void bind_default_mouse()
456 {
457     ObDefMouseBind *it;
458     ObDefMouseBind binds[] = {
459         { "Left", "Client", OB_MOUSE_ACTION_PRESS, "Focus" },
460         { "Middle", "Client", OB_MOUSE_ACTION_PRESS, "Focus" },
461         { "Right", "Client", OB_MOUSE_ACTION_PRESS, "Focus" },
462         { "Left", "Desktop", OB_MOUSE_ACTION_PRESS, "Focus" },
463         { "Middle", "Desktop", OB_MOUSE_ACTION_PRESS, "Focus" },
464         { "Right", "Desktop", OB_MOUSE_ACTION_PRESS, "Focus" },
465         { "Left", "Titlebar", OB_MOUSE_ACTION_PRESS, "Focus" },
466         { "Left", "Handle", OB_MOUSE_ACTION_PRESS, "Focus" },
467         { "Left", "BLCorner", OB_MOUSE_ACTION_PRESS, "Focus" },
468         { "Left", "BRCorner", OB_MOUSE_ACTION_PRESS, "Focus" },
469         { "Left", "TLCorner", OB_MOUSE_ACTION_PRESS, "Focus" },
470         { "Left", "TRCorner", OB_MOUSE_ACTION_PRESS, "Focus" },
471         { "Left", "Close", OB_MOUSE_ACTION_PRESS, "Focus" },
472         { "Left", "Maximize", OB_MOUSE_ACTION_PRESS, "Focus" },
473         { "Left", "Iconify", OB_MOUSE_ACTION_PRESS, "Focus" },
474         { "Left", "Icon", OB_MOUSE_ACTION_PRESS, "Focus" },
475         { "Left", "AllDesktops", OB_MOUSE_ACTION_PRESS, "Focus" },
476         { "Left", "Shade", OB_MOUSE_ACTION_PRESS, "Focus" },
477         { "Left", "Client", OB_MOUSE_ACTION_CLICK, "Raise" },
478         { "Left", "Titlebar", OB_MOUSE_ACTION_CLICK, "Raise" },
479         { "Middle", "Titlebar", OB_MOUSE_ACTION_CLICK, "Lower" },
480         { "Left", "Handle", OB_MOUSE_ACTION_CLICK, "Raise" },
481         { "Left", "BLCorner", OB_MOUSE_ACTION_CLICK, "Raise" },
482         { "Left", "BRCorner", OB_MOUSE_ACTION_CLICK, "Raise" },
483         { "Left", "TLCorner", OB_MOUSE_ACTION_CLICK, "Raise" },
484         { "Left", "TRCorner", OB_MOUSE_ACTION_CLICK, "Raise" },
485         { "Left", "Close", OB_MOUSE_ACTION_CLICK, "Raise" },
486         { "Left", "Maximize", OB_MOUSE_ACTION_CLICK, "Raise" },
487         { "Left", "Iconify", OB_MOUSE_ACTION_CLICK, "Raise" },
488         { "Left", "Icon", OB_MOUSE_ACTION_CLICK, "Raise" },
489         { "Left", "AllDesktops", OB_MOUSE_ACTION_CLICK, "Raise" },
490         { "Left", "Shade", OB_MOUSE_ACTION_CLICK, "Raise" },
491         { "Left", "Close", OB_MOUSE_ACTION_CLICK, "Close" },
492         { "Left", "Maximize", OB_MOUSE_ACTION_CLICK, "ToggleMaximizeFull" },
493         { "Left", "Iconify", OB_MOUSE_ACTION_CLICK, "Iconify" },
494         { "Left", "AllDesktops", OB_MOUSE_ACTION_CLICK, "ToggleOmnipresent" },
495         { "Left", "Shade", OB_MOUSE_ACTION_CLICK, "ToggleShade" },
496         { "Left", "TLCorner", OB_MOUSE_ACTION_MOTION, "Resize" },
497         { "Left", "TRCorner", OB_MOUSE_ACTION_MOTION, "Resize" },
498         { "Left", "BLCorner", OB_MOUSE_ACTION_MOTION, "Resize" },
499         { "Left", "BRCorner", OB_MOUSE_ACTION_MOTION, "Resize" },
500         { "Left", "Titlebar", OB_MOUSE_ACTION_MOTION, "Move" },
501         { "A-Left", "Frame", OB_MOUSE_ACTION_MOTION, "Move" },
502         { "A-Middle", "Frame", OB_MOUSE_ACTION_MOTION, "Resize" },
503         { NULL, NULL, 0, NULL }
504     };
505
506     for (it = binds; it->button; ++it) {
507         ObUserAction uact;
508         switch (it->mact) {
509         case OB_MOUSE_ACTION_PRESS:
510             uact = OB_USER_ACTION_MOUSE_PRESS; break;
511         case OB_MOUSE_ACTION_RELEASE:
512             uact = OB_USER_ACTION_MOUSE_RELEASE; break;
513         case OB_MOUSE_ACTION_CLICK:
514             uact = OB_USER_ACTION_MOUSE_CLICK; break;
515         case OB_MOUSE_ACTION_DOUBLE_CLICK:
516             uact = OB_USER_ACTION_MOUSE_DOUBLE_CLICK; break;
517         case OB_MOUSE_ACTION_MOTION:
518             uact = OB_USER_ACTION_MOUSE_MOTION; break;
519         case OB_NUM_MOUSE_ACTIONS:
520             g_assert_not_reached();
521         }
522         mouse_bind(it->button, it->context, it->mact,
523                    action_from_string(it->actname, uact));
524     }
525 }
526
527 void config_startup(ObParseInst *i)
528 {
529     config_focus_new = TRUE;
530     config_focus_follow = FALSE;
531     config_focus_delay = 0;
532     config_focus_raise = FALSE;
533     config_focus_last = FALSE;
534
535     parse_register(i, "focus", parse_focus, NULL);
536
537     config_place_policy = OB_PLACE_POLICY_SMART;
538
539     parse_register(i, "placement", parse_placement, NULL);
540
541     config_theme = NULL;
542
543     config_title_layout = g_strdup("NLIMC");
544
545     parse_register(i, "theme", parse_theme, NULL);
546
547     config_desktops_num = 4;
548     config_screen_firstdesk = 1;
549     config_desktops_names = NULL;
550
551     parse_register(i, "desktops", parse_desktops, NULL);
552
553     config_resize_redraw = TRUE;
554     config_resize_popup_show = 1; /* nonpixel increments */
555     config_resize_popup_pos = 0;  /* center of client */
556
557     parse_register(i, "resize", parse_resize, NULL);
558
559     config_dock_layer = OB_STACKING_LAYER_DOCK_ABOVE;
560     config_dock_pos = OB_DIRECTION_NORTHEAST;
561     config_dock_floating = FALSE;
562     config_dock_x = 0;
563     config_dock_y = 0;
564     config_dock_orient = OB_ORIENTATION_VERT;
565     config_dock_hide = FALSE;
566     config_dock_hide_delay = 300;
567     config_dock_app_move_button = 2; /* middle */
568     config_dock_app_move_modifiers = 0;
569
570     parse_register(i, "dock", parse_dock, NULL);
571
572     translate_key("C-g", &config_keyboard_reset_state,
573                   &config_keyboard_reset_keycode);
574
575     bind_default_keyboard();
576
577     parse_register(i, "keyboard", parse_keyboard, NULL);
578
579     config_mouse_threshold = 3;
580     config_mouse_dclicktime = 200;
581
582     bind_default_mouse();
583
584     parse_register(i, "mouse", parse_mouse, NULL);
585
586     config_resist_win = 10;
587     config_resist_edge = 20;
588
589     parse_register(i, "resistance", parse_resistance, NULL);
590
591     config_menu_warppointer = TRUE;
592     config_menu_xorstyle = FALSE;
593     config_menu_hilightfirst = TRUE;
594     config_menu_hide_delay = 250;
595     config_menu_files = NULL;
596
597     parse_register(i, "menu", parse_menu, NULL);
598 }
599
600 void config_shutdown()
601 {
602     GSList *it;
603
604     g_free(config_theme);
605
606     g_free(config_title_layout);
607
608     for (it = config_desktops_names; it; it = g_slist_next(it))
609         g_free(it->data);
610     g_slist_free(config_desktops_names);
611
612     for (it = config_menu_files; it; it = g_slist_next(it))
613         g_free(it->data);
614     g_slist_free(config_menu_files);
615 }