]> icculus.org git repositories - dana/openbox.git/blob - openbox/keytree.c
wip: Add config_parser.c which will provide a nice means to specify config variables...
[dana/openbox.git] / openbox / keytree.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    keytree.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "keyboard.h"
21 #include "translate.h"
22 #include "action.h"
23 #include "action_list.h"
24 #include <glib.h>
25
26 void tree_destroy(KeyBindingTree *tree)
27 {
28     KeyBindingTree *c;
29
30     while (tree) {
31         tree_destroy(tree->next_sibling);
32         c = tree->first_child;
33         if (c == NULL) {
34             GList *it;
35             for (it = tree->keylist; it != NULL; it = it->next)
36                 g_free(it->data);
37             g_list_free(tree->keylist);
38             action_list_unref(tree->actions);
39         }
40         g_slice_free(KeyBindingTree, tree);
41         tree = c;
42     }
43 }
44
45 KeyBindingTree *tree_build(GList *keylist)
46 {
47     GList *it;
48     KeyBindingTree *ret = NULL, *p;
49
50     if (g_list_length(keylist) <= 0)
51         return NULL; /* nothing in the list.. */
52
53     for (it = g_list_last(keylist); it; it = g_list_previous(it)) {
54         GList *kit;
55
56         p = ret;
57         ret = g_slice_new0(KeyBindingTree);
58
59         for (kit = it; kit != NULL; kit = g_list_previous(kit))
60             ret->keylist = g_list_prepend(ret->keylist,
61                                           g_strdup(kit->data)); /* deep copy */
62         ret->first_child = p;
63         if (p != NULL) p->parent = ret;
64         translate_key(it->data, &ret->state, &ret->key);
65     }
66     return ret;
67 }
68
69 void tree_assimilate(KeyBindingTree *node)
70 {
71     KeyBindingTree *a, *b, *tmp, *last;
72
73     if (keyboard_firstnode == NULL) {
74         /* there are no nodes at this level yet */
75         keyboard_firstnode = node;
76     } else {
77         a = keyboard_firstnode;
78         last = a;
79         b = node;
80         while (a) {
81             last = a;
82             /* check b->key != 0 for key bindings that didn't get translated */
83             if (!(a->state == b->state && a->key == b->key && b->key != 0)) {
84                 a = a->next_sibling;
85             } else {
86                 tmp = b;
87                 b = b->first_child;
88                 g_slice_free(KeyBindingTree, tmp);
89                 a = a->first_child;
90             }
91         }
92         /* check b->key != 0, and save key bindings that didn't get translated
93            as siblings here */
94         if (!(last->state == b->state && last->key == b->key && b->key != 0)) {
95             last->next_sibling = b;
96             b->parent = last->parent;
97         } else {
98             last->first_child = b->first_child;
99             last->first_child->parent = last;
100             g_slice_free(KeyBindingTree, b);
101         }
102     }
103 }
104
105 KeyBindingTree *tree_find(KeyBindingTree *search, gboolean *conflict)
106 {
107     KeyBindingTree *a, *b;
108
109     *conflict = FALSE;
110
111     a = keyboard_firstnode;
112     b = search;
113     while (a && b) {
114         /* check b->key != 0 for key bindings that didn't get translated, and
115            don't make them conflict with anything else so that they can all
116            live together in peace and harmony */
117         if (!(a->state == b->state && a->key == b->key && b->key != 0)) {
118             a = a->next_sibling;
119         } else {
120             if ((a->first_child == NULL) == (b->first_child == NULL)) {
121                 if (a->first_child == NULL) {
122                     /* found it! (return the actual node, not the search's) */
123                     return a;
124                 }
125             } else {
126                 *conflict = TRUE;
127                 return NULL; /* the chain status' don't match (conflict!) */
128             }
129             b = b->first_child;
130             a = a->first_child;
131         }
132     }
133     return NULL; /* it just isn't in here */
134 }
135
136 gboolean tree_chroot(KeyBindingTree *tree, GList *keylist)
137 {
138     guint key, state;
139     translate_key(keylist->data, &state, &key);
140     while (tree != NULL && !(tree->state == state && tree->key == key))
141         tree = tree->next_sibling;
142     if (tree != NULL) {
143         if (keylist->next == NULL) {
144             tree->chroot = TRUE;
145             return TRUE;
146         } else
147             return tree_chroot(tree->first_child, keylist->next);
148     }
149     return FALSE;
150 }