]> Dogcows Code - chaz/openbox/blob - openbox/keytree.c
do it better, prevent creating new races
[chaz/openbox] / 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 <glib.h>
23
24 void tree_destroy(KeyBindingTree *tree)
25 {
26 KeyBindingTree *c;
27
28 while (tree) {
29 tree_destroy(tree->next_sibling);
30 c = tree->first_child;
31 if (c == NULL) {
32 GList *it;
33 GSList *sit;
34 for (it = tree->keylist; it != NULL; it = it->next)
35 g_free(it->data);
36 g_list_free(tree->keylist);
37 for (sit = tree->actions; sit != NULL; sit = sit->next)
38 action_unref(sit->data);
39 g_slist_free(tree->actions);
40 }
41 g_free(tree);
42 tree = c;
43 }
44 }
45
46 KeyBindingTree *tree_build(GList *keylist)
47 {
48 GList *it;
49 KeyBindingTree *ret = NULL, *p;
50
51 if (g_list_length(keylist) <= 0)
52 return NULL; /* nothing in the list.. */
53
54 for (it = g_list_last(keylist); it; it = g_list_previous(it)) {
55 GList *kit;
56
57 p = ret;
58 ret = g_new0(KeyBindingTree, 1);
59
60 for (kit = it; kit != NULL; kit = g_list_previous(kit))
61 ret->keylist = g_list_prepend(ret->keylist,
62 g_strdup(kit->data)); /* deep copy */
63 ret->first_child = p;
64 if (p != NULL) p->parent = ret;
65 if (!translate_key(it->data, &ret->state, &ret->key)) {
66 tree_destroy(ret);
67 return NULL;
68 }
69 }
70 return ret;
71 }
72
73 void tree_assimilate(KeyBindingTree *node)
74 {
75 KeyBindingTree *a, *b, *tmp, *last;
76
77 if (keyboard_firstnode == NULL) {
78 /* there are no nodes at this level yet */
79 keyboard_firstnode = node;
80 } else {
81 a = keyboard_firstnode;
82 last = a;
83 b = node;
84 while (a) {
85 last = a;
86 if (!(a->state == b->state && a->key == b->key)) {
87 a = a->next_sibling;
88 } else {
89 tmp = b;
90 b = b->first_child;
91 g_free(tmp);
92 a = a->first_child;
93 }
94 }
95 if (!(last->state == b->state && last->key == b->key)) {
96 last->next_sibling = b;
97 b->parent = last->parent;
98 } else {
99 last->first_child = b->first_child;
100 last->first_child->parent = last;
101 g_free(b);
102 }
103 }
104 }
105
106 KeyBindingTree *tree_find(KeyBindingTree *search, gboolean *conflict)
107 {
108 KeyBindingTree *a, *b;
109
110 *conflict = FALSE;
111
112 a = keyboard_firstnode;
113 b = search;
114 while (a && b) {
115 if (!(a->state == b->state && a->key == b->key)) {
116 a = a->next_sibling;
117 } else {
118 if ((a->first_child == NULL) == (b->first_child == NULL)) {
119 if (a->first_child == NULL) {
120 /* found it! (return the actual node, not the search's) */
121 return a;
122 }
123 } else {
124 *conflict = TRUE;
125 return NULL; /* the chain status' don't match (conflict!) */
126 }
127 b = b->first_child;
128 a = a->first_child;
129 }
130 }
131 return NULL; /* it just isn't in here */
132 }
133
134 gboolean tree_chroot(KeyBindingTree *tree, GList *keylist)
135 {
136 guint key, state;
137 if (translate_key(keylist->data, &state, &key)) {
138 while (tree != NULL && !(tree->state == state && tree->key == key))
139 tree = tree->next_sibling;
140 if (tree != NULL) {
141 if (keylist->next == NULL) {
142 tree->chroot = TRUE;
143 return TRUE;
144 } else
145 return tree_chroot(tree->first_child, keylist->next);
146 }
147 }
148 return FALSE;
149 }
This page took 0.038972 seconds and 4 git commands to generate.