Mercurial > pidgin.yaz
annotate finch/libgnt/gntkeys.c @ 23693:d93578eb7244
merge of '3a9f71b3ea427344c6b6e95c63c3559bef222013'
and 'd370f99001126b27ce4cf920e66083b8ff7cd629'
author | Justin Rodriguez <ffdragon@soc.pidgin.im> |
---|---|
date | Wed, 06 Aug 2008 02:54:39 +0000 |
parents | ded0a9cf1785 |
children | dc8fac020fad |
rev | line source |
---|---|
18049
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
1 /** |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
2 * GNT - The GLib Ncurses Toolkit |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
3 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
4 * GNT is the legal property of its developers, whose names are too numerous |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
5 * to list here. Please refer to the COPYRIGHT file distributed with this |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
6 * source distribution. |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
7 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
8 * This library is free software; you can redistribute it and/or modify |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
9 * it under the terms of the GNU General Public License as published by |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
10 * the Free Software Foundation; either version 2 of the License, or |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
11 * (at your option) any later version. |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
12 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
13 * This program is distributed in the hope that it will be useful, |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
16 * GNU General Public License for more details. |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
17 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
18 * You should have received a copy of the GNU General Public License |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
19 * along with this program; if not, write to the Free Software |
19680
44b4e8bd759b
The FSF changed its address a while ago; our files were out of date.
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
18423
diff
changeset
|
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
18049
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
21 */ |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
22 |
15818 | 23 #include "gntkeys.h" |
24 | |
25 #include <glib.h> | |
26 #include <stdlib.h> | |
27 #include <string.h> | |
28 | |
29 char *gnt_key_cup; | |
30 char *gnt_key_cdown; | |
31 char *gnt_key_cleft; | |
32 char *gnt_key_cright; | |
33 | |
34 static const char *term; | |
35 static GHashTable *specials; | |
36 | |
37 void gnt_init_keys() | |
38 { | |
39 const char *controls[] = {"", "c-", "ctrl-", "ctr-", "ctl-", NULL}; | |
40 const char *alts[] = {"", "alt-", "a-", "m-", "meta-", NULL}; | |
41 int c, a, ch; | |
42 char key[32]; | |
43 | |
44 if (term == NULL) { | |
45 term = getenv("TERM"); | |
46 if (!term) | |
47 term = ""; /* Just in case */ | |
48 } | |
49 | |
50 if (strcmp(term, "xterm") == 0 || strcmp(term, "rxvt") == 0) { | |
51 gnt_key_cup = "\033" "[1;5A"; | |
52 gnt_key_cdown = "\033" "[1;5B"; | |
53 gnt_key_cright = "\033" "[1;5C"; | |
54 gnt_key_cleft = "\033" "[1;5D"; | |
55 } else if (strcmp(term, "screen") == 0 || strcmp(term, "rxvt-unicode") == 0) { | |
56 gnt_key_cup = "\033" "Oa"; | |
57 gnt_key_cdown = "\033" "Ob"; | |
58 gnt_key_cright = "\033" "Oc"; | |
59 gnt_key_cleft = "\033" "Od"; | |
60 } | |
61 | |
62 specials = g_hash_table_new(g_str_hash, g_str_equal); | |
63 | |
64 #define INSERT_KEY(k, code) do { \ | |
65 g_hash_table_insert(specials, g_strdup(k), g_strdup(code)); \ | |
66 gnt_keys_add_combination(code); \ | |
67 } while (0) | |
68 | |
69 INSERT_KEY("home", GNT_KEY_HOME); | |
70 INSERT_KEY("end", GNT_KEY_END); | |
71 INSERT_KEY("pageup", GNT_KEY_PGUP); | |
72 INSERT_KEY("pagedown", GNT_KEY_PGDOWN); | |
73 INSERT_KEY("insert", GNT_KEY_INS); | |
74 INSERT_KEY("delete", GNT_KEY_DEL); | |
18000
05d7fe2320a9
(16:39:44) rekkanoryo: i have a feature request
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16044
diff
changeset
|
75 INSERT_KEY("back_tab", GNT_KEY_BACK_TAB); |
15818 | 76 |
77 INSERT_KEY("left", GNT_KEY_LEFT); | |
78 INSERT_KEY("right", GNT_KEY_RIGHT); | |
79 INSERT_KEY("up", GNT_KEY_UP); | |
80 INSERT_KEY("down", GNT_KEY_DOWN); | |
81 | |
82 INSERT_KEY("tab", "\t"); | |
23396
e145f37cb2e2
Allow binding space, escape and return to some action.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19680
diff
changeset
|
83 INSERT_KEY("escape", "\033"); |
e145f37cb2e2
Allow binding space, escape and return to some action.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19680
diff
changeset
|
84 INSERT_KEY("space", " "); |
e145f37cb2e2
Allow binding space, escape and return to some action.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19680
diff
changeset
|
85 INSERT_KEY("return", GNT_KEY_ENTER); |
15818 | 86 INSERT_KEY("menu", GNT_KEY_POPUP); |
87 | |
88 INSERT_KEY("f1", GNT_KEY_F1); | |
89 INSERT_KEY("f2", GNT_KEY_F2); | |
90 INSERT_KEY("f3", GNT_KEY_F3); | |
91 INSERT_KEY("f4", GNT_KEY_F4); | |
92 INSERT_KEY("f5", GNT_KEY_F5); | |
93 INSERT_KEY("f6", GNT_KEY_F6); | |
94 INSERT_KEY("f7", GNT_KEY_F7); | |
95 INSERT_KEY("f8", GNT_KEY_F8); | |
96 INSERT_KEY("f9", GNT_KEY_F9); | |
97 INSERT_KEY("f10", GNT_KEY_F10); | |
98 INSERT_KEY("f11", GNT_KEY_F11); | |
99 INSERT_KEY("f12", GNT_KEY_F12); | |
100 | |
101 #define REM_LENGTH (sizeof(key) - (cur - key)) | |
102 #define INSERT_COMB(k, code) do { \ | |
103 snprintf(key, sizeof(key), "%s%s%s", controls[c], alts[a], k); \ | |
104 INSERT_KEY(key, code); \ | |
105 } while (0); | |
106 | |
107 /* Lower-case alphabets */ | |
108 for (a = 0, c = 0; controls[c]; c++, a = 0) { | |
109 if (c) { | |
110 INSERT_COMB("up", gnt_key_cup); | |
111 INSERT_COMB("down", gnt_key_cdown); | |
112 INSERT_COMB("left", gnt_key_cleft); | |
113 INSERT_COMB("right", gnt_key_cright); | |
114 } | |
115 | |
116 for (a = 0; alts[a]; a++) { | |
117 for (ch = 0; ch < 26; ch++) { | |
118 char str[2] = {'a' + ch, 0}, code[4] = "\0\0\0\0"; | |
119 int ind = 0; | |
120 if (a) | |
121 code[ind++] = '\033'; | |
122 code[ind] = (c ? 1 : 'a') + ch; | |
123 INSERT_COMB(str, code); | |
124 } | |
23396
e145f37cb2e2
Allow binding space, escape and return to some action.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19680
diff
changeset
|
125 if (c == 0) { |
e145f37cb2e2
Allow binding space, escape and return to some action.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19680
diff
changeset
|
126 INSERT_COMB("tab", "\033\t"); |
e145f37cb2e2
Allow binding space, escape and return to some action.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
19680
diff
changeset
|
127 } |
15818 | 128 } |
129 } | |
130 c = 0; | |
131 for (a = 0; alts[a]; a++) { | |
132 /* Upper-case alphabets */ | |
133 for (ch = 0; ch < 26; ch++) { | |
134 char str[2] = {'A' + ch, 0}, code[] = {'\033', 'A' + ch, 0}; | |
135 INSERT_COMB(str, code); | |
136 } | |
137 /* Digits */ | |
138 for (ch = 0; ch < 10; ch++) { | |
139 char str[2] = {'0' + ch, 0}, code[] = {'\033', '0' + ch, 0}; | |
140 INSERT_COMB(str, code); | |
141 } | |
142 } | |
143 } | |
144 | |
23219
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
145 void gnt_keys_refine(char *text) |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
146 { |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
147 if (*text == 27 && *(text + 1) == '[' && |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
148 (*(text + 2) >= 'A' && *(text + 2) <= 'D')) { |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
149 /* Apparently this is necessary for urxvt and screen and xterm */ |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
150 if (strcmp(term, "screen") == 0 || strcmp(term, "rxvt-unicode") == 0 || |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
151 strcmp(term, "xterm") == 0) |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
152 *(text + 1) = 'O'; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
153 } else if (*(unsigned char*)text == 195) { |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
154 if (*(text + 2) == 0 && strcmp(term, "xterm") == 0) { |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
155 *(text) = 27; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
156 *(text + 1) -= 64; /* Say wha? */ |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
157 } |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
158 } |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
159 } |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
160 |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
161 const char *gnt_key_translate(const char *name) |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
162 { |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
163 return name ? g_hash_table_lookup(specials, name) : NULL; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
164 } |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
165 |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
166 typedef struct { |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
167 const char *name; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
168 const char *key; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
169 } gntkey; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
170 |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
171 static void |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
172 get_key_name(gpointer key, gpointer value, gpointer data) |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
173 { |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
174 gntkey *k = data; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
175 if (k->name) |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
176 return; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
177 if (g_utf8_collate(value, k->key) == 0) |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
178 k->name = key; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
179 } |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
180 |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
181 const char *gnt_key_lookup(const char *key) |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
182 { |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
183 gntkey k = {NULL, key}; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
184 g_hash_table_foreach(specials, get_key_name, &k); |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
185 return k.name; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
186 } |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
187 |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
188 /** |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
189 * The key-bindings will be saved in a tree. When a keystroke happens, GNT will |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
190 * find the sequence that matches a binding and return the length. |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
191 * A sequence should not be a prefix of another sequence. If it is, then only |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
192 * the shortest one will be processed. If we want to change that, we will need |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
193 * to allow getting the k-th prefix that matches the input, and pay attention |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
194 * to the return value of gnt_wm_process_input in gntmain.c. |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
195 */ |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
196 #define SIZE 256 |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
197 |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
198 #define IS_END 1 << 0 |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
199 struct _node |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
200 { |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
201 struct _node *next[SIZE]; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
202 int ref; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
203 int flags; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
204 }; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
205 |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
206 static struct _node root = {.ref = 1, .flags = 0}; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
207 |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
208 static void add_path(struct _node *node, const char *path) |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
209 { |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
210 struct _node *n = NULL; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
211 if (!path || !*path) { |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
212 node->flags |= IS_END; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
213 return; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
214 } |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
215 while (*path && node->next[(unsigned char)*path]) { |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
216 node = node->next[(unsigned char)*path]; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
217 node->ref++; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
218 path++; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
219 } |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
220 if (!*path) |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
221 return; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
222 n = g_new0(struct _node, 1); |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
223 n->ref = 1; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
224 node->next[(unsigned char)*path++] = n; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
225 add_path(n, path); |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
226 } |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
227 |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
228 void gnt_keys_add_combination(const char *path) |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
229 { |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
230 add_path(&root, path); |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
231 } |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
232 |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
233 static void del_path(struct _node *node, const char *path) |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
234 { |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
235 struct _node *next = NULL; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
236 |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
237 if (!*path) |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
238 return; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
239 next = node->next[(unsigned char)*path]; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
240 if (!next) |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
241 return; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
242 del_path(next, path + 1); |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
243 next->ref--; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
244 if (next->ref == 0) { |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
245 node->next[(unsigned char)*path] = NULL; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
246 g_free(next); |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
247 } |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
248 } |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
249 |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
250 void gnt_keys_del_combination(const char *path) |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
251 { |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
252 del_path(&root, path); |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
253 } |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
254 |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
255 int gnt_keys_find_combination(const char *path) |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
256 { |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
257 int depth = 0; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
258 struct _node *n = &root; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
259 |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
260 root.flags &= ~IS_END; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
261 while (*path && n->next[(unsigned char)*path] && !(n->flags & IS_END)) { |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
262 if (!g_ascii_isspace(*path) && |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
263 !g_ascii_iscntrl(*path) && |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
264 !g_ascii_isgraph(*path)) |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
265 return 0; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
266 n = n->next[(unsigned char)*path++]; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
267 depth++; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
268 } |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
269 |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
270 if (!(n->flags & IS_END)) |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
271 depth = 0; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
272 return depth; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
273 } |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
274 |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
275 static void |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
276 print_path(struct _node *node, int depth) |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
277 { |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
278 int i; |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
279 for (i = 0; i < SIZE; i++) { |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
280 if (node->next[i]) { |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
281 g_printerr("%*c (%d:%d)\n", depth * 4, i, node->next[i]->ref, |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
282 node->next[i]->flags); |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
283 print_path(node->next[i], depth + 1); |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
284 } |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
285 } |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
286 } |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
287 |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
288 /* this is purely for debugging purposes. */ |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
289 void gnt_keys_print_combinations(void); |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
290 void gnt_keys_print_combinations() |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
291 { |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
292 g_printerr("--------\n"); |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
293 print_path(&root, 1); |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
294 g_printerr("--------\n"); |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
295 } |
7b7af53e136a
disapproval of revision '79699160e06b69f70db91eef65287f15685ec2bf'
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
23218
diff
changeset
|
296 |