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