comparison console/libgnt/gntkeys.c @ 15778:20e934a1a47e

Some changes to the last change. People should really test this thing out.
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Wed, 07 Mar 2007 13:29:54 +0000
parents c2c2a854f5b3
children 567097a973c6
comparison
equal deleted inserted replaced
15777:c2c2a854f5b3 15778:20e934a1a47e
48 } 48 }
49 } 49 }
50 50
51 /** 51 /**
52 * The key-bindings will be saved in a tree. When a keystroke happens, GNT will 52 * The key-bindings will be saved in a tree. When a keystroke happens, GNT will
53 * find the longest sequence that matches a binding and return the length. 53 * find the sequence that matches a binding and return the length.
54 * A sequence should not be a prefix of another sequence. If it is, then only
55 * the shortest one will be processed. If we want to change that, we will need
56 * to allow getting the k-th prefix that matches the input, and pay attention
57 * to the return value of gnt_wm_process_input in gntmain.c.
54 */ 58 */
55 #define SIZE 256 59 #define SIZE 256
56 60
57 #define HAS_CHILD 1 << 0 61 #define IS_END 1 << 0
58 struct _node 62 struct _node
59 { 63 {
60 struct _node *next[SIZE]; 64 struct _node *next[SIZE];
61 int ref; 65 int ref;
62 int flags; 66 int flags;
63 }; 67 };
64 68
65 static struct _node root = {.ref = 1, .flags = HAS_CHILD}; 69 static struct _node root = {.ref = 1, .flags = 0};
66 70
67 static void add_path(struct _node *node, const char *path) 71 static void add_path(struct _node *node, const char *path)
68 { 72 {
69 struct _node *n = NULL; 73 struct _node *n = NULL;
70 if (!path || !*path) 74 if (!path || !*path) {
75 node->flags |= IS_END;
71 return; 76 return;
77 }
72 while (*path && node->next[*path]) { 78 while (*path && node->next[*path]) {
73 node = node->next[*path]; 79 node = node->next[*path];
74 node->ref++; 80 node->ref++;
75 path++; 81 path++;
76 } 82 }
77 if (!*path) 83 if (!*path)
78 return; 84 return;
79 n = g_new0(struct _node, 1); 85 n = g_new0(struct _node, 1);
80 n->ref = 1; 86 n->ref = 1;
81 node->next[*path++] = n; 87 node->next[*path++] = n;
82 node->flags |= HAS_CHILD;
83 add_path(n, path); 88 add_path(n, path);
84 } 89 }
85 90
86 void gnt_keys_add_combination(const char *path) 91 void gnt_keys_add_combination(const char *path)
87 { 92 {
113 int gnt_keys_find_combination(const char *path) 118 int gnt_keys_find_combination(const char *path)
114 { 119 {
115 int depth = 0; 120 int depth = 0;
116 struct _node *n = &root; 121 struct _node *n = &root;
117 122
118 while (*path && n->next[*path] && (n->flags & HAS_CHILD)) { 123 while (*path && n->next[*path] && !(n->flags & IS_END)) {
119 n = n->next[*path++]; 124 n = n->next[*path++];
120 depth++; 125 depth++;
121 } 126 }
122 127
123 if (n->flags & HAS_CHILD) 128 if (!(n->flags & IS_END))
124 depth = 0; 129 depth = 0;
125 return depth; 130 return depth;
126 } 131 }
127 132
128 static void 133 static void