… | |
… | |
3 | |
3 | |
4 | #ifdef KEYSYM_RESOURCE |
4 | #ifdef KEYSYM_RESOURCE |
5 | |
5 | |
6 | #include <cstring> |
6 | #include <cstring> |
7 | |
7 | |
|
|
8 | #include "rxvtperl.h" |
8 | #include "keyboard.h" |
9 | #include "keyboard.h" |
9 | #include "command.h" |
10 | #include "command.h" |
10 | |
11 | |
|
|
12 | /* an intro to the data structure: |
|
|
13 | * |
|
|
14 | * vector keymap[] is grouped. |
|
|
15 | * |
|
|
16 | * inside each group, elements are sorted by the criteria given by compare_priority(). |
|
|
17 | * the lookup of keysym is done in two steps: |
|
|
18 | * 1) locate the group corresponds to the keysym; |
|
|
19 | * 2) do a linear search inside the group. |
|
|
20 | * |
|
|
21 | * array hash[] effectively defines a map from a keysym to a group in keymap[]. |
|
|
22 | * |
|
|
23 | * each group has its address(the index of first group element in keymap[]), |
|
|
24 | * which is computed and stored in hash[]. |
|
|
25 | * hash[] stores the addresses in the form of: |
|
|
26 | * index: 0 I1 I2 I3 In |
|
|
27 | * value: 0...0, A1...A1, A2...A2, A3...A3, ..., An...An |
|
|
28 | * where |
|
|
29 | * A1 = 0; |
|
|
30 | * Ai+1 = N1 + N2 + ... + Ni. |
|
|
31 | * it is computed from hash_budget_size[]: |
|
|
32 | * index: 0 I1 I2 I3 In |
|
|
33 | * value: 0...0, N1, 0...0, N2, 0...0, N3, ..., Nn, 0...0 |
|
|
34 | * 0...0, 0.......0, N1.....N1, N1+N2...N1+N2, ... (the compution of hash[]) |
|
|
35 | * or we can say |
|
|
36 | * hash_budget_size[Ii] = Ni; hash_budget_size[elsewhere] = 0, |
|
|
37 | * where |
|
|
38 | * set {I1, I2, ..., In} = { hashkey of keymap[0]->keysym, ..., keymap[keymap.size-1]->keysym } |
|
|
39 | * where hashkey of keymap[i]->keysym = keymap[i]->keysym & KEYSYM_HASH_MASK |
|
|
40 | * n(the number of groups) = the number of non-zero member of hash_budget_size[]; |
|
|
41 | * Ni(the size of group i) = hash_budget_size[Ii]. |
|
|
42 | */ |
|
|
43 | |
|
|
44 | #if STOCK_KEYMAP |
11 | //////////////////////////////////////////////////////////////////////////////// |
45 | //////////////////////////////////////////////////////////////////////////////// |
12 | // default keycode translation map and keyevent handlers |
46 | // default keycode translation map and keyevent handlers |
13 | |
47 | |
14 | keysym_t keyboard_manager::stock_keymap[] = { |
48 | keysym_t keyboard_manager::stock_keymap[] = { |
15 | /* examples */ |
49 | /* examples */ |
… | |
… | |
32 | //{ '.', ControlMask, 1, keysym_t::NORMAL, "\033<C-.>"}, |
66 | //{ '.', ControlMask, 1, keysym_t::NORMAL, "\033<C-.>"}, |
33 | //{ '0', ControlMask, 10, keysym_t::RANGE, "0" "\033<C-%c>"}, |
67 | //{ '0', ControlMask, 10, keysym_t::RANGE, "0" "\033<C-%c>"}, |
34 | //{ '0', MetaMask|ControlMask, 10, keysym_t::RANGE, "0" "\033<M-C-%c>"}, |
68 | //{ '0', MetaMask|ControlMask, 10, keysym_t::RANGE, "0" "\033<M-C-%c>"}, |
35 | //{ 'a', MetaMask|ControlMask, 26, keysym_t::RANGE, "a" "\033<M-C-%c>"}, |
69 | //{ 'a', MetaMask|ControlMask, 26, keysym_t::RANGE, "a" "\033<M-C-%c>"}, |
36 | }; |
70 | }; |
|
|
71 | #endif |
37 | |
72 | |
38 | static void |
73 | static void |
39 | output_string (rxvt_term *rt, const char *str) |
74 | output_string (rxvt_term *rt, const char *str) |
40 | { |
75 | { |
41 | if (strncmp (str, "command:", 8) == 0) |
76 | if (strncmp (str, "command:", 8) == 0) |
42 | rt->cmd_write ((unsigned char *)str + 8, strlen (str) - 8); |
77 | rt->cmd_write (str + 8, strlen (str) - 8); |
|
|
78 | else if (strncmp (str, "perl:", 5) == 0) |
|
|
79 | PERL_INVOKE((rt, HOOK_KEYBOARD_COMMAND, DT_STRING, str + 5, DT_END)); |
43 | else |
80 | else |
44 | rt->tt_write ((unsigned char *)str, strlen (str)); |
81 | rt->tt_write (str, strlen (str)); |
45 | } |
82 | } |
46 | |
83 | |
47 | static void |
84 | static void |
48 | output_string_meta8 (rxvt_term *rt, unsigned int state, char *buf, int buflen) |
85 | output_string_meta8 (rxvt_term *rt, unsigned int state, char *buf, int buflen) |
49 | { |
86 | { |
… | |
… | |
56 | *ch |= 0x80; |
93 | *ch |= 0x80; |
57 | } |
94 | } |
58 | else if (rt->meta_char == C0_ESC) /* escape prefix */ |
95 | else if (rt->meta_char == C0_ESC) /* escape prefix */ |
59 | #endif |
96 | #endif |
60 | { |
97 | { |
61 | const unsigned char ch = C0_ESC; |
98 | const char ch = C0_ESC; |
62 | rt->tt_write (&ch, 1); |
99 | rt->tt_write (&ch, 1); |
63 | } |
100 | } |
64 | } |
101 | } |
65 | |
102 | |
66 | rt->tt_write ((unsigned char *) buf, buflen); |
103 | rt->tt_write (buf, buflen); |
67 | } |
104 | } |
68 | |
105 | |
69 | static int |
106 | static int |
70 | format_keyrange_string (const char *str, int keysym_offset, char *buf, int bufsize) |
107 | format_keyrange_string (const char *str, int keysym_offset, char *buf, int bufsize) |
71 | { |
108 | { |
… | |
… | |
81 | } |
118 | } |
82 | |
119 | |
83 | //////////////////////////////////////////////////////////////////////////////// |
120 | //////////////////////////////////////////////////////////////////////////////// |
84 | // return: #bits of '1' |
121 | // return: #bits of '1' |
85 | #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3) |
122 | #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3) |
86 | # define bitcount(n) (__extension__ ({ uint32_t n__ = (n); __builtin_popcount (n); })) |
123 | # define bitcount(n) (__extension__ ({ uint32_t n__ = (n); __builtin_popcount (n__); })) |
87 | #else |
124 | #else |
88 | static int |
125 | static int |
89 | bitcount (uint16_t n) |
126 | bitcount (uint16_t n) |
90 | { |
127 | { |
91 | int i; |
128 | int i; |
… | |
… | |
155 | void |
192 | void |
156 | keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const char *trans) |
193 | keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const char *trans) |
157 | { |
194 | { |
158 | keysym_t *key = new keysym_t; |
195 | keysym_t *key = new keysym_t; |
159 | wchar_t *wc = rxvt_mbstowcs (trans); |
196 | wchar_t *wc = rxvt_mbstowcs (trans); |
160 | const char *translation = rxvt_wcstoutf8 (wc); |
197 | char *translation = rxvt_wcstoutf8 (wc); |
161 | free (wc); |
198 | free (wc); |
162 | |
199 | |
163 | if (key && translation) |
200 | if (key && translation) |
164 | { |
201 | { |
165 | key->keysym = keysym; |
202 | key->keysym = keysym; |
… | |
… | |
181 | strcpy (translation, translation + 4); |
218 | strcpy (translation, translation + 4); |
182 | } |
219 | } |
183 | else |
220 | else |
184 | rxvt_warn ("cannot parse list-type keysym '%s', treating as normal keysym.\n", translation); |
221 | rxvt_warn ("cannot parse list-type keysym '%s', treating as normal keysym.\n", translation); |
185 | } |
222 | } |
|
|
223 | else if (strncmp (translation, "builtin:", 8) == 0) |
|
|
224 | key->type = keysym_t::BUILTIN; |
186 | |
225 | |
187 | user_keymap.push_back (key); |
226 | user_keymap.push_back (key); |
188 | user_translations.push_back (translation); |
227 | user_translations.push_back (translation); |
189 | register_keymap (key); |
228 | register_keymap (key); |
190 | } |
229 | } |
… | |
… | |
207 | } |
246 | } |
208 | |
247 | |
209 | void |
248 | void |
210 | keyboard_manager::register_done () |
249 | keyboard_manager::register_done () |
211 | { |
250 | { |
|
|
251 | #if STOCK_KEYMAP |
212 | unsigned int i, n = sizeof (stock_keymap) / sizeof (keysym_t); |
252 | int n = sizeof (stock_keymap) / sizeof (keysym_t); |
213 | |
253 | |
|
|
254 | //TODO: shield against repeated calls and empty keymap |
214 | if (keymap.back () != &stock_keymap[n - 1]) |
255 | //if (keymap.back () != &stock_keymap[n - 1]) |
215 | for (i = 0; i < n; ++i) |
256 | for (int i = 0; i < n; ++i) |
216 | register_keymap (&stock_keymap[i]); |
257 | register_keymap (&stock_keymap[i]); |
|
|
258 | #endif |
217 | |
259 | |
218 | purge_duplicate_keymap (); |
260 | purge_duplicate_keymap (); |
219 | |
261 | |
220 | setup_hash (); |
262 | setup_hash (); |
221 | } |
263 | } |
222 | |
264 | |
223 | bool |
265 | bool |
224 | keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state) |
266 | keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state) |
225 | { |
267 | { |
226 | assert (hash[0] == 0 && "register_done() need to be called"); |
268 | assert (hash[0] == 0 && "register_done() need to be called"); |
|
|
269 | |
|
|
270 | state &= OtherModMask; // mask out uninteresting modifiers |
227 | |
271 | |
228 | if (state & term->ModMetaMask) state |= MetaMask; |
272 | if (state & term->ModMetaMask) state |= MetaMask; |
229 | if (state & term->ModNumLockMask) state |= NumLockMask; |
273 | if (state & term->ModNumLockMask) state |= NumLockMask; |
230 | if (state & term->ModLevel3Mask) state |= Level3Mask; |
274 | if (state & term->ModLevel3Mask) state |= Level3Mask; |
231 | |
275 | |
… | |
… | |
236 | |
280 | |
237 | if (index >= 0) |
281 | if (index >= 0) |
238 | { |
282 | { |
239 | const keysym_t &key = *keymap [index]; |
283 | const keysym_t &key = *keymap [index]; |
240 | |
284 | |
|
|
285 | if (key.type != keysym_t::BUILTIN) |
|
|
286 | { |
241 | int keysym_offset = keysym - key.keysym; |
287 | int keysym_offset = keysym - key.keysym; |
242 | |
288 | |
243 | wchar_t *wc = rxvt_utf8towcs (key.str); |
289 | wchar_t *wc = rxvt_utf8towcs (key.str); |
244 | |
|
|
245 | char *str = rxvt_wcstombs (wc); |
290 | char *str = rxvt_wcstombs (wc); |
246 | // TODO: do translations, unescaping etc, here (allow \u escape etc.) |
291 | // TODO: do (some) translations, unescaping etc, here (allow \u escape etc.) |
247 | free (wc); |
292 | free (wc); |
248 | |
293 | |
249 | switch (key.type) |
294 | switch (key.type) |
250 | { |
|
|
251 | case keysym_t::NORMAL: |
|
|
252 | output_string (term, str); |
|
|
253 | break; |
|
|
254 | |
|
|
255 | case keysym_t::RANGE: |
|
|
256 | { |
295 | { |
|
|
296 | case keysym_t::NORMAL: |
|
|
297 | output_string (term, str); |
|
|
298 | break; |
|
|
299 | |
|
|
300 | case keysym_t::RANGE: |
|
|
301 | { |
257 | char buf[STRING_MAX]; |
302 | char buf[STRING_MAX]; |
258 | |
303 | |
259 | if (format_keyrange_string (str, keysym_offset, buf, sizeof (buf)) > 0) |
304 | if (format_keyrange_string (str, keysym_offset, buf, sizeof (buf)) > 0) |
|
|
305 | output_string (term, buf); |
|
|
306 | } |
|
|
307 | break; |
|
|
308 | |
|
|
309 | case keysym_t::RANGE_META8: |
|
|
310 | { |
|
|
311 | int len; |
|
|
312 | char buf[STRING_MAX]; |
|
|
313 | |
|
|
314 | len = format_keyrange_string (str, keysym_offset, buf, sizeof (buf)); |
|
|
315 | if (len > 0) |
|
|
316 | output_string_meta8 (term, state, buf, len); |
|
|
317 | } |
|
|
318 | break; |
|
|
319 | |
|
|
320 | case keysym_t::LIST: |
|
|
321 | { |
|
|
322 | char buf[STRING_MAX]; |
|
|
323 | |
|
|
324 | char *prefix, *middle, *suffix; |
|
|
325 | |
|
|
326 | prefix = str; |
|
|
327 | middle = strchr (prefix + 1, *prefix); |
|
|
328 | suffix = strrchr (middle + 1, *prefix); |
|
|
329 | |
|
|
330 | memcpy (buf, prefix + 1, middle - prefix - 1); |
|
|
331 | buf [middle - prefix - 1] = middle [keysym_offset + 1]; |
|
|
332 | strcpy (buf + (middle - prefix), suffix + 1); |
|
|
333 | |
260 | output_string (term, buf); |
334 | output_string (term, buf); |
|
|
335 | } |
|
|
336 | break; |
261 | } |
337 | } |
262 | break; |
|
|
263 | |
338 | |
264 | case keysym_t::RANGE_META8: |
|
|
265 | { |
|
|
266 | int len; |
|
|
267 | char buf[STRING_MAX]; |
|
|
268 | |
|
|
269 | len = format_keyrange_string (str, keysym_offset, buf, sizeof (buf)); |
|
|
270 | if (len > 0) |
|
|
271 | output_string_meta8 (term, state, buf, len); |
|
|
272 | } |
|
|
273 | break; |
|
|
274 | |
|
|
275 | case keysym_t::LIST: |
|
|
276 | { |
|
|
277 | char buf[STRING_MAX]; |
|
|
278 | |
|
|
279 | char *prefix, *middle, *suffix; |
|
|
280 | |
|
|
281 | prefix = str; |
|
|
282 | middle = strchr (prefix + 1, *prefix); |
|
|
283 | suffix = strrchr (middle + 1, *prefix); |
|
|
284 | |
|
|
285 | memcpy (buf, prefix + 1, middle - prefix - 1); |
|
|
286 | buf [middle - prefix - 1] = middle [keysym_offset + 1]; |
|
|
287 | strcpy (buf + (middle - prefix), suffix + 1); |
|
|
288 | |
|
|
289 | output_string (term, buf); |
|
|
290 | } |
|
|
291 | break; |
|
|
292 | } |
|
|
293 | |
|
|
294 | free (str); |
339 | free (str); |
295 | |
340 | |
296 | return true; |
341 | return true; |
297 | } |
342 | } |
298 | else |
|
|
299 | { |
343 | } |
300 | // fprintf(stderr,"[%x:%x]",state,keysym); |
344 | |
301 | return false; |
345 | return false; |
302 | } |
|
|
303 | } |
346 | } |
304 | |
347 | |
305 | // purge duplicate keymap entries |
348 | // purge duplicate keymap entries |
306 | void keyboard_manager::purge_duplicate_keymap () |
349 | void keyboard_manager::purge_duplicate_keymap () |
307 | { |
350 | { |
… | |
… | |
317 | if (i < keymap.size ()) |
360 | if (i < keymap.size ()) |
318 | { |
361 | { |
319 | keymap[i] = keymap.back (); |
362 | keymap[i] = keymap.back (); |
320 | keymap.pop_back (); |
363 | keymap.pop_back (); |
321 | } |
364 | } |
|
|
365 | |
322 | break; |
366 | break; |
323 | } |
367 | } |
324 | } |
368 | } |
325 | } |
369 | } |
326 | } |
370 | } |
… | |
… | |
334 | uint16_t hash_budget_counter[KEYSYM_HASH_BUDGETS]; // #elements in each budget |
378 | uint16_t hash_budget_counter[KEYSYM_HASH_BUDGETS]; // #elements in each budget |
335 | |
379 | |
336 | memset (hash_budget_size, 0, sizeof (hash_budget_size)); |
380 | memset (hash_budget_size, 0, sizeof (hash_budget_size)); |
337 | memset (hash_budget_counter, 0, sizeof (hash_budget_counter)); |
381 | memset (hash_budget_counter, 0, sizeof (hash_budget_counter)); |
338 | |
382 | |
339 | // count keysyms for corresponding hash budgets |
383 | // determine hash bucket size |
340 | for (i = 0; i < keymap.size (); ++i) |
384 | for (i = 0; i < keymap.size (); ++i) |
|
|
385 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j) |
341 | { |
386 | { |
342 | hashkey = keymap [i]->keysym & KEYSYM_HASH_MASK; |
387 | hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; |
343 | ++hash_budget_size [hashkey]; |
388 | ++hash_budget_size [hashkey]; |
344 | } |
|
|
345 | |
|
|
346 | // a keysym_t with range>1 is counted one more time for every keysym that |
|
|
347 | // lies in its range |
|
|
348 | for (i = 0; i < keymap.size (); ++i) |
|
|
349 | { |
|
|
350 | if (keymap[i]->range > 1) |
|
|
351 | { |
|
|
352 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j > 0; --j) |
|
|
353 | { |
|
|
354 | hashkey = ((keymap [i]->keysym + j) & KEYSYM_HASH_MASK); |
|
|
355 | if (hash_budget_size [hashkey]) |
|
|
356 | ++hash_budget_size [hashkey]; |
|
|
357 | } |
|
|
358 | } |
389 | } |
359 | } |
|
|
360 | |
390 | |
361 | // now we know the size of each budget |
391 | // now we know the size of each budget |
362 | // compute the index of each budget |
392 | // compute the index of each budget |
363 | hash [0] = 0; |
393 | hash [0] = 0; |
364 | for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i) |
394 | for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i) |
365 | { |
395 | { |
366 | index += hash_budget_size [i - 1]; |
396 | index += hash_budget_size [i - 1]; |
367 | hash[i] = (hash_budget_size [i] ? index : hash [i - 1]); |
397 | hash [i] = index; |
368 | } |
398 | } |
369 | |
399 | |
370 | // and allocate just enough space |
400 | // and allocate just enough space |
371 | sorted_keymap.insert (sorted_keymap.begin (), index + hash_budget_size [i - 1], 0); |
401 | sorted_keymap.insert (sorted_keymap.begin (), index + hash_budget_size [i - 1], 0); |
372 | |
402 | |
373 | // fill in sorted_keymap |
403 | // fill in sorted_keymap |
374 | // it is sorted in each budget |
404 | // it is sorted in each budget |
375 | for (i = 0; i < keymap.size (); ++i) |
405 | for (i = 0; i < keymap.size (); ++i) |
376 | { |
|
|
377 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j) |
406 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j) |
378 | { |
407 | { |
379 | hashkey = ((keymap [i]->keysym + j) & KEYSYM_HASH_MASK); |
408 | hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; |
380 | |
409 | |
381 | if (hash_budget_size [hashkey]) |
|
|
382 | { |
|
|
383 | index = hash [hashkey] + hash_budget_counter [hashkey]; |
410 | index = hash [hashkey] + hash_budget_counter [hashkey]; |
384 | |
411 | |
385 | while (index > hash [hashkey] |
412 | while (index > hash [hashkey] |
386 | && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) |
413 | && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) |
387 | { |
414 | { |
388 | sorted_keymap [index] = sorted_keymap [index - 1]; |
415 | sorted_keymap [index] = sorted_keymap [index - 1]; |
389 | --index; |
416 | --index; |
390 | } |
417 | } |
391 | |
418 | |
392 | sorted_keymap [index] = keymap [i]; |
419 | sorted_keymap [index] = keymap [i]; |
393 | ++hash_budget_counter [hashkey]; |
420 | ++hash_budget_counter [hashkey]; |
394 | } |
|
|
395 | } |
421 | } |
396 | } |
|
|
397 | |
422 | |
398 | keymap.swap (sorted_keymap); |
423 | keymap.swap (sorted_keymap); |
399 | |
424 | |
400 | #if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD) |
425 | #if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD) |
401 | // check for invariants |
426 | // check for invariants |
… | |
… | |
433 | int |
458 | int |
434 | keyboard_manager::find_keysym (KeySym keysym, unsigned int state) |
459 | keyboard_manager::find_keysym (KeySym keysym, unsigned int state) |
435 | { |
460 | { |
436 | int hashkey = keysym & KEYSYM_HASH_MASK; |
461 | int hashkey = keysym & KEYSYM_HASH_MASK; |
437 | unsigned int index = hash [hashkey]; |
462 | unsigned int index = hash [hashkey]; |
|
|
463 | unsigned int end = hashkey < KEYSYM_HASH_BUDGETS - 1 |
|
|
464 | ? hash [hashkey + 1] |
|
|
465 | : keymap.size (); |
438 | |
466 | |
439 | for (; index < keymap.size (); ++index) |
467 | for (; index < end; ++index) |
440 | { |
468 | { |
441 | keysym_t *key = keymap [index]; |
469 | keysym_t *key = keymap [index]; |
442 | |
470 | |
443 | if (key->keysym <= keysym && key->keysym + key->range > keysym |
471 | if (key->keysym <= keysym && keysym < key->keysym + key->range |
444 | // match only the specified bits in state and ignore others |
472 | // match only the specified bits in state and ignore others |
445 | && (key->state & state) == key->state) |
473 | && (key->state & state) == key->state) |
446 | return index; |
474 | return index; |
447 | else if ((key->keysym & KEYSYM_HASH_MASK) > hashkey && key->range == 1) |
|
|
448 | return -1; |
|
|
449 | } |
475 | } |
450 | |
476 | |
451 | return -1; |
477 | return -1; |
452 | } |
478 | } |
453 | |
479 | |