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