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