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