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