Mercurial > pidgin
annotate finch/libgnt/gntkeys.c @ 18045:08e93462f189
Update the irssi WM to allow tiling the conversation windows.
author | Sadrul Habib Chowdhury <imadil@gmail.com> |
---|---|
date | Wed, 06 Jun 2007 14:34:58 +0000 |
parents | 05d7fe2320a9 |
children | 1cedd520cd18 |
rev | line source |
---|---|
15817 | 1 #include "gntkeys.h" |
2 | |
3 #include <glib.h> | |
4 #include <stdlib.h> | |
5 #include <string.h> | |
6 | |
7 char *gnt_key_cup; | |
8 char *gnt_key_cdown; | |
9 char *gnt_key_cleft; | |
10 char *gnt_key_cright; | |
11 | |
12 static const char *term; | |
13 static GHashTable *specials; | |
14 | |
15 void gnt_init_keys() | |
16 { | |
17 const char *controls[] = {"", "c-", "ctrl-", "ctr-", "ctl-", NULL}; | |
18 const char *alts[] = {"", "alt-", "a-", "m-", "meta-", NULL}; | |
19 int c, a, ch; | |
20 char key[32]; | |
21 | |
22 if (term == NULL) { | |
23 term = getenv("TERM"); | |
24 if (!term) | |
25 term = ""; /* Just in case */ | |
26 } | |
27 | |
28 if (strcmp(term, "xterm") == 0 || strcmp(term, "rxvt") == 0) { | |
29 gnt_key_cup = "\033" "[1;5A"; | |
30 gnt_key_cdown = "\033" "[1;5B"; | |
31 gnt_key_cright = "\033" "[1;5C"; | |
32 gnt_key_cleft = "\033" "[1;5D"; | |
33 } else if (strcmp(term, "screen") == 0 || strcmp(term, "rxvt-unicode") == 0) { | |
34 gnt_key_cup = "\033" "Oa"; | |
35 gnt_key_cdown = "\033" "Ob"; | |
36 gnt_key_cright = "\033" "Oc"; | |
37 gnt_key_cleft = "\033" "Od"; | |
38 } | |
39 | |
40 specials = g_hash_table_new(g_str_hash, g_str_equal); | |
41 | |
42 #define INSERT_KEY(k, code) do { \ | |
43 g_hash_table_insert(specials, g_strdup(k), g_strdup(code)); \ | |
44 gnt_keys_add_combination(code); \ | |
45 } while (0) | |
46 | |
47 INSERT_KEY("home", GNT_KEY_HOME); | |
48 INSERT_KEY("end", GNT_KEY_END); | |
49 INSERT_KEY("pageup", GNT_KEY_PGUP); | |
50 INSERT_KEY("pagedown", GNT_KEY_PGDOWN); | |
51 INSERT_KEY("insert", GNT_KEY_INS); | |
52 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
|
53 INSERT_KEY("back_tab", GNT_KEY_BACK_TAB); |
15817 | 54 |
55 INSERT_KEY("left", GNT_KEY_LEFT); | |
56 INSERT_KEY("right", GNT_KEY_RIGHT); | |
57 INSERT_KEY("up", GNT_KEY_UP); | |
58 INSERT_KEY("down", GNT_KEY_DOWN); | |
59 | |
60 INSERT_KEY("tab", "\t"); | |
61 INSERT_KEY("menu", GNT_KEY_POPUP); | |
62 | |
63 INSERT_KEY("f1", GNT_KEY_F1); | |
64 INSERT_KEY("f2", GNT_KEY_F2); | |
65 INSERT_KEY("f3", GNT_KEY_F3); | |
66 INSERT_KEY("f4", GNT_KEY_F4); | |
67 INSERT_KEY("f5", GNT_KEY_F5); | |
68 INSERT_KEY("f6", GNT_KEY_F6); | |
69 INSERT_KEY("f7", GNT_KEY_F7); | |
70 INSERT_KEY("f8", GNT_KEY_F8); | |
71 INSERT_KEY("f9", GNT_KEY_F9); | |
72 INSERT_KEY("f10", GNT_KEY_F10); | |
73 INSERT_KEY("f11", GNT_KEY_F11); | |
74 INSERT_KEY("f12", GNT_KEY_F12); | |
75 | |
76 #define REM_LENGTH (sizeof(key) - (cur - key)) | |
77 #define INSERT_COMB(k, code) do { \ | |
78 snprintf(key, sizeof(key), "%s%s%s", controls[c], alts[a], k); \ | |
79 INSERT_KEY(key, code); \ | |
80 } while (0); | |
81 | |
82 /* Lower-case alphabets */ | |
83 for (a = 0, c = 0; controls[c]; c++, a = 0) { | |
84 if (c) { | |
85 INSERT_COMB("up", gnt_key_cup); | |
86 INSERT_COMB("down", gnt_key_cdown); | |
87 INSERT_COMB("left", gnt_key_cleft); | |
88 INSERT_COMB("right", gnt_key_cright); | |
89 } | |
90 | |
91 for (a = 0; alts[a]; a++) { | |
92 for (ch = 0; ch < 26; ch++) { | |
93 char str[2] = {'a' + ch, 0}, code[4] = "\0\0\0\0"; | |
94 int ind = 0; | |
95 if (a) | |
96 code[ind++] = '\033'; | |
97 code[ind] = (c ? 1 : 'a') + ch; | |
98 INSERT_COMB(str, code); | |
99 } | |
100 } | |
101 } | |
102 c = 0; | |
103 for (a = 0; alts[a]; a++) { | |
104 /* Upper-case alphabets */ | |
105 for (ch = 0; ch < 26; ch++) { | |
106 char str[2] = {'A' + ch, 0}, code[] = {'\033', 'A' + ch, 0}; | |
107 INSERT_COMB(str, code); | |
108 } | |
109 /* Digits */ | |
110 for (ch = 0; ch < 10; ch++) { | |
111 char str[2] = {'0' + ch, 0}, code[] = {'\033', '0' + ch, 0}; | |
112 INSERT_COMB(str, code); | |
113 } | |
114 } | |
115 } | |
116 | |
117 void gnt_keys_refine(char *text) | |
118 { | |
119 if (*text == 27 && *(text + 1) == '[' && | |
120 (*(text + 2) >= 'A' && *(text + 2) <= 'D')) { | |
121 /* Apparently this is necessary for urxvt and screen and xterm */ | |
122 if (strcmp(term, "screen") == 0 || strcmp(term, "rxvt-unicode") == 0 || | |
123 strcmp(term, "xterm") == 0) | |
124 *(text + 1) = 'O'; | |
125 } else if (*(unsigned char*)text == 195) { | |
126 if (*(text + 2) == 0 && strcmp(term, "xterm") == 0) { | |
127 *(text) = 27; | |
128 *(text + 1) -= 64; /* Say wha? */ | |
129 } | |
130 } | |
131 } | |
132 | |
133 const char *gnt_key_translate(const char *name) | |
134 { | |
135 return g_hash_table_lookup(specials, name); | |
136 } | |
137 | |
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:
15817
diff
changeset
|
138 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:
15817
diff
changeset
|
139 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:
15817
diff
changeset
|
140 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:
15817
diff
changeset
|
141 } 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:
15817
diff
changeset
|
142 |
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:
15817
diff
changeset
|
143 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:
15817
diff
changeset
|
144 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:
15817
diff
changeset
|
145 { |
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:
15817
diff
changeset
|
146 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:
15817
diff
changeset
|
147 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:
15817
diff
changeset
|
148 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:
15817
diff
changeset
|
149 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:
15817
diff
changeset
|
150 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:
15817
diff
changeset
|
151 } |
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:
15817
diff
changeset
|
152 |
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:
15817
diff
changeset
|
153 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:
15817
diff
changeset
|
154 { |
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:
15817
diff
changeset
|
155 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:
15817
diff
changeset
|
156 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:
15817
diff
changeset
|
157 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:
15817
diff
changeset
|
158 } |
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:
15817
diff
changeset
|
159 |
15817 | 160 /** |
161 * The key-bindings will be saved in a tree. When a keystroke happens, GNT will | |
162 * find the sequence that matches a binding and return the length. | |
163 * A sequence should not be a prefix of another sequence. If it is, then only | |
164 * the shortest one will be processed. If we want to change that, we will need | |
165 * to allow getting the k-th prefix that matches the input, and pay attention | |
166 * to the return value of gnt_wm_process_input in gntmain.c. | |
167 */ | |
168 #define SIZE 256 | |
169 | |
170 #define IS_END 1 << 0 | |
171 struct _node | |
172 { | |
173 struct _node *next[SIZE]; | |
174 int ref; | |
175 int flags; | |
176 }; | |
177 | |
178 static struct _node root = {.ref = 1, .flags = 0}; | |
179 | |
180 static void add_path(struct _node *node, const char *path) | |
181 { | |
182 struct _node *n = NULL; | |
183 if (!path || !*path) { | |
184 node->flags |= IS_END; | |
185 return; | |
186 } | |
187 while (*path && node->next[*path]) { | |
188 node = node->next[*path]; | |
189 node->ref++; | |
190 path++; | |
191 } | |
192 if (!*path) | |
193 return; | |
194 n = g_new0(struct _node, 1); | |
195 n->ref = 1; | |
196 node->next[*path++] = n; | |
197 add_path(n, path); | |
198 } | |
199 | |
200 void gnt_keys_add_combination(const char *path) | |
201 { | |
202 add_path(&root, path); | |
203 } | |
204 | |
205 static void del_path(struct _node *node, const char *path) | |
206 { | |
207 struct _node *next = NULL; | |
208 | |
209 if (!*path) | |
210 return; | |
211 next = node->next[*path]; | |
212 if (!next) | |
213 return; | |
214 del_path(next, path + 1); | |
215 next->ref--; | |
216 if (next->ref == 0) { | |
217 node->next[*path] = NULL; | |
218 g_free(next); | |
219 } | |
220 } | |
221 | |
222 void gnt_keys_del_combination(const char *path) | |
223 { | |
224 del_path(&root, path); | |
225 } | |
226 | |
227 int gnt_keys_find_combination(const char *path) | |
228 { | |
229 int depth = 0; | |
230 struct _node *n = &root; | |
231 | |
232 root.flags &= ~IS_END; | |
233 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
|
234 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
|
235 !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
|
236 !g_ascii_isgraph(*path)) |
15817 | 237 return 0; |
238 n = n->next[*path++]; | |
239 depth++; | |
240 } | |
241 | |
242 if (!(n->flags & IS_END)) | |
243 depth = 0; | |
244 return depth; | |
245 } | |
246 | |
247 static void | |
248 print_path(struct _node *node, int depth) | |
249 { | |
250 int i; | |
251 for (i = 0; i < SIZE; i++) { | |
252 if (node->next[i]) { | |
253 g_printerr("%*c (%d:%d)\n", depth * 4, i, node->next[i]->ref, | |
254 node->next[i]->flags); | |
255 print_path(node->next[i], depth + 1); | |
256 } | |
257 } | |
258 } | |
259 | |
260 /* this is purely for debugging purposes. */ | |
261 void gnt_keys_print_combinations(void); | |
262 void gnt_keys_print_combinations() | |
263 { | |
264 g_printerr("--------\n"); | |
265 print_path(&root, 1); | |
266 g_printerr("--------\n"); | |
267 } | |
268 |