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