]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/keytree.c
no tabs
[mikachu/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) 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 "keyboard.h"
20 #include "translate.h"
21 #include <glib.h>
22
23 void tree_destroy(KeyBindingTree *tree)
24 {
25     KeyBindingTree *c;
26
27     while (tree) {
28         tree_destroy(tree->next_sibling);
29         c = tree->first_child;
30         if (c == NULL) {
31             GList *it;
32             GSList *sit;
33             for (it = tree->keylist; it != NULL; it = it->next)
34                 g_free(it->data);
35             g_list_free(tree->keylist);
36             for (sit = tree->actions; sit != NULL; sit = sit->next)
37                 action_free(sit->data);
38             g_slist_free(tree->actions);
39         }
40         g_free(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 != NULL; it = it->prev) {
54         p = ret;
55         ret = g_new0(KeyBindingTree, 1);
56         if (p == NULL) {
57             GList *it;
58
59             /* this is the first built node, the bottom node of the tree */
60             ret->keylist = g_list_copy(keylist); /* shallow copy */
61             for (it = ret->keylist; it != NULL; it = it->next) /* deep copy */
62                 it->data = g_strdup(it->data);
63         }
64         ret->first_child = p;
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         else {
98             last->first_child = b->first_child;
99             g_free(b);
100         }
101     }
102 }
103
104 KeyBindingTree *tree_find(KeyBindingTree *search, gboolean *conflict)
105 {
106     KeyBindingTree *a, *b;
107
108     *conflict = FALSE;
109
110     a = keyboard_firstnode;
111     b = search;
112     while (a && b) {
113         if (!(a->state == b->state && a->key == b->key)) {
114             a = a->next_sibling;
115         } else {
116             if ((a->first_child == NULL) == (b->first_child == NULL)) {
117                 if (a->first_child == NULL) {
118                     /* found it! (return the actual node, not the search's) */
119                     return a;
120                 }
121             } else {
122                 *conflict = TRUE;
123                 return NULL; /* the chain status' don't match (conflict!) */
124             }
125             b = b->first_child;
126             a = a->first_child;
127         }
128     }
129     return NULL; /* it just isn't in here */
130 }