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