Mercurial > pidgin.yaz
annotate finch/libgnt/gntkeys.c @ 19105:fd27baf1711b
disapproval of revision 'c80d121e4fd54ef1ba9c1ce9e4afa059f6b777e9'
author | Eric Polino <aluink@pidgin.im> |
---|---|
date | Tue, 19 Jun 2007 19:33:16 +0000 |
parents | ac904659104f |
children | c6282dc3fb9f |
rev | line source |
---|---|
17718
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
17538
diff
changeset
|
1 /** |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
17538
diff
changeset
|
2 * GNT - The GLib Ncurses Toolkit |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
17538
diff
changeset
|
3 * |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
17538
diff
changeset
|
4 * GNT is the legal property of its developers, whose names are too numerous |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
17538
diff
changeset
|
5 * to list here. Please refer to the COPYRIGHT file distributed with this |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
17538
diff
changeset
|
6 * source distribution. |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
17538
diff
changeset
|
7 * |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
17538
diff
changeset
|
8 * This library is free software; you can redistribute it and/or modify |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
17538
diff
changeset
|
9 * it under the terms of the GNU General Public License as published by |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
17538
diff
changeset
|
10 * the Free Software Foundation; either version 2 of the License, or |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
17538
diff
changeset
|
11 * (at your option) any later version. |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
17538
diff
changeset
|
12 * |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
17538
diff
changeset
|
13 * This program is distributed in the hope that it will be useful, |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
17538
diff
changeset
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
17538
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
17538
diff
changeset
|
16 * GNU General Public License for more details. |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
17538
diff
changeset
|
17 * |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
17538
diff
changeset
|
18 * You should have received a copy of the GNU General Public License |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
17538
diff
changeset
|
19 * along with this program; if not, write to the Free Software |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
17538
diff
changeset
|
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
17538
diff
changeset
|
21 */ |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
17538
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); | |
75 | |
76 INSERT_KEY("left", GNT_KEY_LEFT); | |
77 INSERT_KEY("right", GNT_KEY_RIGHT); | |
78 INSERT_KEY("up", GNT_KEY_UP); | |
79 INSERT_KEY("down", GNT_KEY_DOWN); | |
80 | |
81 INSERT_KEY("tab", "\t"); | |
82 INSERT_KEY("menu", GNT_KEY_POPUP); | |
83 | |
84 INSERT_KEY("f1", GNT_KEY_F1); | |
85 INSERT_KEY("f2", GNT_KEY_F2); | |
86 INSERT_KEY("f3", GNT_KEY_F3); | |
87 INSERT_KEY("f4", GNT_KEY_F4); | |
88 INSERT_KEY("f5", GNT_KEY_F5); | |
89 INSERT_KEY("f6", GNT_KEY_F6); | |
90 INSERT_KEY("f7", GNT_KEY_F7); | |
91 INSERT_KEY("f8", GNT_KEY_F8); | |
92 INSERT_KEY("f9", GNT_KEY_F9); | |
93 INSERT_KEY("f10", GNT_KEY_F10); | |
94 INSERT_KEY("f11", GNT_KEY_F11); | |
95 INSERT_KEY("f12", GNT_KEY_F12); | |
96 | |
97 #define REM_LENGTH (sizeof(key) - (cur - key)) | |
98 #define INSERT_COMB(k, code) do { \ | |
99 snprintf(key, sizeof(key), "%s%s%s", controls[c], alts[a], k); \ | |
100 INSERT_KEY(key, code); \ | |
101 } while (0); | |
102 | |
103 /* Lower-case alphabets */ | |
104 for (a = 0, c = 0; controls[c]; c++, a = 0) { | |
105 if (c) { | |
106 INSERT_COMB("up", gnt_key_cup); | |
107 INSERT_COMB("down", gnt_key_cdown); | |
108 INSERT_COMB("left", gnt_key_cleft); | |
109 INSERT_COMB("right", gnt_key_cright); | |
110 } | |
111 | |
112 for (a = 0; alts[a]; a++) { | |
113 for (ch = 0; ch < 26; ch++) { | |
114 char str[2] = {'a' + ch, 0}, code[4] = "\0\0\0\0"; | |
115 int ind = 0; | |
116 if (a) | |
117 code[ind++] = '\033'; | |
118 code[ind] = (c ? 1 : 'a') + ch; | |
119 INSERT_COMB(str, code); | |
120 } | |
121 } | |
122 } | |
123 c = 0; | |
124 for (a = 0; alts[a]; a++) { | |
125 /* Upper-case alphabets */ | |
126 for (ch = 0; ch < 26; ch++) { | |
127 char str[2] = {'A' + ch, 0}, code[] = {'\033', 'A' + ch, 0}; | |
128 INSERT_COMB(str, code); | |
129 } | |
130 /* Digits */ | |
131 for (ch = 0; ch < 10; ch++) { | |
132 char str[2] = {'0' + ch, 0}, code[] = {'\033', '0' + ch, 0}; | |
133 INSERT_COMB(str, code); | |
134 } | |
135 } | |
136 } | |
137 | |
138 void gnt_keys_refine(char *text) | |
139 { | |
140 if (*text == 27 && *(text + 1) == '[' && | |
141 (*(text + 2) >= 'A' && *(text + 2) <= 'D')) { | |
142 /* Apparently this is necessary for urxvt and screen and xterm */ | |
143 if (strcmp(term, "screen") == 0 || strcmp(term, "rxvt-unicode") == 0 || | |
144 strcmp(term, "xterm") == 0) | |
145 *(text + 1) = 'O'; | |
146 } else if (*(unsigned char*)text == 195) { | |
147 if (*(text + 2) == 0 && strcmp(term, "xterm") == 0) { | |
148 *(text) = 27; | |
149 *(text + 1) -= 64; /* Say wha? */ | |
150 } | |
151 } | |
152 } | |
153 | |
154 const char *gnt_key_translate(const char *name) | |
155 { | |
156 return g_hash_table_lookup(specials, name); | |
157 } | |
158 | |
15979
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
159 typedef struct { |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
160 const char *name; |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
161 const char *key; |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
162 } gntkey; |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
163 |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
164 static void |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
165 get_key_name(gpointer key, gpointer value, gpointer data) |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
166 { |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
167 gntkey *k = data; |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
168 if (k->name) |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
169 return; |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
170 if (g_utf8_collate(value, k->key) == 0) |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
171 k->name = key; |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
172 } |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
173 |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
174 const char *gnt_key_lookup(const char *key) |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
175 { |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
176 gntkey k = {NULL, key}; |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
177 g_hash_table_foreach(specials, get_key_name, &k); |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
178 return k.name; |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
179 } |
2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
180 |
15818 | 181 /** |
182 * The key-bindings will be saved in a tree. When a keystroke happens, GNT will | |
183 * find the sequence that matches a binding and return the length. | |
184 * A sequence should not be a prefix of another sequence. If it is, then only | |
185 * the shortest one will be processed. If we want to change that, we will need | |
186 * to allow getting the k-th prefix that matches the input, and pay attention | |
187 * to the return value of gnt_wm_process_input in gntmain.c. | |
188 */ | |
189 #define SIZE 256 | |
190 | |
191 #define IS_END 1 << 0 | |
192 struct _node | |
193 { | |
194 struct _node *next[SIZE]; | |
195 int ref; | |
196 int flags; | |
197 }; | |
198 | |
199 static struct _node root = {.ref = 1, .flags = 0}; | |
200 | |
201 static void add_path(struct _node *node, const char *path) | |
202 { | |
203 struct _node *n = NULL; | |
204 if (!path || !*path) { | |
205 node->flags |= IS_END; | |
206 return; | |
207 } | |
208 while (*path && node->next[*path]) { | |
209 node = node->next[*path]; | |
210 node->ref++; | |
211 path++; | |
212 } | |
213 if (!*path) | |
214 return; | |
215 n = g_new0(struct _node, 1); | |
216 n->ref = 1; | |
217 node->next[*path++] = n; | |
218 add_path(n, path); | |
219 } | |
220 | |
221 void gnt_keys_add_combination(const char *path) | |
222 { | |
223 add_path(&root, path); | |
224 } | |
225 | |
226 static void del_path(struct _node *node, const char *path) | |
227 { | |
228 struct _node *next = NULL; | |
229 | |
230 if (!*path) | |
231 return; | |
232 next = node->next[*path]; | |
233 if (!next) | |
234 return; | |
235 del_path(next, path + 1); | |
236 next->ref--; | |
237 if (next->ref == 0) { | |
238 node->next[*path] = NULL; | |
239 g_free(next); | |
240 } | |
241 } | |
242 | |
243 void gnt_keys_del_combination(const char *path) | |
244 { | |
245 del_path(&root, path); | |
246 } | |
247 | |
248 int gnt_keys_find_combination(const char *path) | |
249 { | |
250 int depth = 0; | |
251 struct _node *n = &root; | |
252 | |
253 root.flags &= ~IS_END; | |
254 while (*path && n->next[*path] && !(n->flags & IS_END)) { | |
16044
ca463cecd432
Only ascii keys can be bound, fixes a crash when inputting non-ascii chars
Richard Nelson <wabz@pidgin.im>
parents:
15979
diff
changeset
|
255 if (!g_ascii_isspace(*path) && |
ca463cecd432
Only ascii keys can be bound, fixes a crash when inputting non-ascii chars
Richard Nelson <wabz@pidgin.im>
parents:
15979
diff
changeset
|
256 !g_ascii_iscntrl(*path) && |
ca463cecd432
Only ascii keys can be bound, fixes a crash when inputting non-ascii chars
Richard Nelson <wabz@pidgin.im>
parents:
15979
diff
changeset
|
257 !g_ascii_isgraph(*path)) |
15818 | 258 return 0; |
259 n = n->next[*path++]; | |
260 depth++; | |
261 } | |
262 | |
263 if (!(n->flags & IS_END)) | |
264 depth = 0; | |
265 return depth; | |
266 } | |
267 | |
268 static void | |
269 print_path(struct _node *node, int depth) | |
270 { | |
271 int i; | |
272 for (i = 0; i < SIZE; i++) { | |
273 if (node->next[i]) { | |
274 g_printerr("%*c (%d:%d)\n", depth * 4, i, node->next[i]->ref, | |
275 node->next[i]->flags); | |
276 print_path(node->next[i], depth + 1); | |
277 } | |
278 } | |
279 } | |
280 | |
281 /* this is purely for debugging purposes. */ | |
282 void gnt_keys_print_combinations(void); | |
283 void gnt_keys_print_combinations() | |
284 { | |
285 g_printerr("--------\n"); | |
286 print_path(&root, 1); | |
287 g_printerr("--------\n"); | |
288 } | |
289 |