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