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