1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 keytree.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
21 #include "translate.h"
25 void tree_destroy(KeyBindingTree *tree)
30 tree_destroy(tree->next_sibling);
31 c = tree->first_child;
35 for (it = tree->keylist; it != NULL; it = it->next)
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);
42 g_slice_free(KeyBindingTree, tree);
47 KeyBindingTree *tree_build(GList *keylist, gboolean grab, gboolean no_repeat)
50 KeyBindingTree *ret = NULL, *p;
52 if (g_list_length(keylist) <= 0)
53 return NULL; /* nothing in the list.. */
55 for (it = g_list_last(keylist); it; it = g_list_previous(it)) {
59 ret = g_slice_new0(KeyBindingTree);
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 */
66 ret->no_repeat = no_repeat;
67 if (p != NULL) p->parent = ret;
68 translate_key(it->data, &ret->state, &ret->key);
73 void tree_assimilate(KeyBindingTree *node)
75 KeyBindingTree *a, *b, *tmp, *last;
77 if (keyboard_firstnode == NULL) {
78 /* there are no nodes at this level yet */
79 keyboard_firstnode = node;
81 a = keyboard_firstnode;
86 /* check b->key != 0 for key bindings that didn't get translated */
87 if (!(a->state == b->state && a->key == b->key && b->key != 0)) {
92 g_slice_free(KeyBindingTree, tmp);
96 /* check b->key != 0, and save key bindings that didn't get translated
98 if (!(last->state == b->state && last->key == b->key && b->key != 0)) {
99 last->next_sibling = b;
100 b->parent = last->parent;
102 last->first_child = b->first_child;
103 last->first_child->parent = last;
104 g_slice_free(KeyBindingTree, b);
109 KeyBindingTree *tree_find(KeyBindingTree *search, gboolean *conflict)
111 KeyBindingTree *a, *b;
115 a = keyboard_firstnode;
118 /* check b->key != 0 for key bindings that didn't get translated, and
119 don't make them conflict with anything else so that they can all
120 live together in peace and harmony */
121 if (!(a->state == b->state && a->key == b->key && b->key != 0)) {
124 if ((a->first_child == NULL) == (b->first_child == NULL)) {
125 if (a->first_child == NULL) {
126 /* found it! (return the actual node, not the search's) */
131 return NULL; /* the chain status' don't match (conflict!) */
137 return NULL; /* it just isn't in here */
140 gboolean tree_chroot(KeyBindingTree *tree, GList *keylist)
143 translate_key(keylist->data, &state, &key);
144 while (tree != NULL && !(tree->state == state && tree->key == key))
145 tree = tree->next_sibling;
147 if (keylist->next == NULL) {
151 return tree_chroot(tree->first_child, keylist->next);