… | |
… | |
6 | #include <cstring> |
6 | #include <cstring> |
7 | |
7 | |
8 | #include "keyboard.h" |
8 | #include "keyboard.h" |
9 | #include "command.h" |
9 | #include "command.h" |
10 | |
10 | |
|
|
11 | /* an intro to the data structure: |
|
|
12 | * |
|
|
13 | * vector keymap[] is grouped. |
|
|
14 | * |
|
|
15 | * inside each group, elements are sorted by the criteria given by compare_priority(). |
|
|
16 | * the lookup of keysym is done in two steps: |
|
|
17 | * 1) locate the group corresponds to the keysym; |
|
|
18 | * 2) do a linear search inside the group. |
|
|
19 | * |
|
|
20 | * array hash[] effectively defines a map from a keysym to a group in keymap[]. |
|
|
21 | * |
|
|
22 | * each group has its address(the index of first group element in keymap[]), |
|
|
23 | * which is computed and stored in hash[]. |
|
|
24 | * hash[] stores the addresses in the form of: |
|
|
25 | * index: 0 I1 I2 I3 In |
|
|
26 | * value: 0...0, A1...A1, A2...A2, A3...A3, ..., An...An |
|
|
27 | * where |
|
|
28 | * A1 = 0; |
|
|
29 | * Ai+1 = N1 + N2 + ... + Ni. |
|
|
30 | * it is computed from hash_budget_size[]: |
|
|
31 | * index: 0 I1 I2 I3 In |
|
|
32 | * value: 0...0, N1, 0...0, N2, 0...0, N3, ..., Nn, 0...0 |
|
|
33 | * 0...0, 0.......0, N1.....N1, N1+N2...N1+N2, ... (the compution of hash[]) |
|
|
34 | * or we can say |
|
|
35 | * hash_budget_size[Ii] = Ni; hash_budget_size[elsewhere] = 0, |
|
|
36 | * where |
|
|
37 | * set {I1, I2, ..., In} = { hashkey of keymap[0]->keysym, ..., keymap[keymap.size-1]->keysym } |
|
|
38 | * where hashkey of keymap[i]->keysym = keymap[i]->keysym & KEYSYM_HASH_MASK |
|
|
39 | * n(the number of groups) = the number of non-zero member of hash_budget_size[]; |
|
|
40 | * Ni(the size of group i) = hash_budget_size[Ii]. |
|
|
41 | */ |
|
|
42 | |
|
|
43 | #if STOCK_KEYMAP |
11 | //////////////////////////////////////////////////////////////////////////////// |
44 | //////////////////////////////////////////////////////////////////////////////// |
12 | // default keycode translation map and keyevent handlers |
45 | // default keycode translation map and keyevent handlers |
13 | |
46 | |
14 | keysym_t keyboard_manager::stock_keymap[] = { |
47 | keysym_t keyboard_manager::stock_keymap[] = { |
15 | /* examples */ |
48 | /* examples */ |
16 | /* keysym, state, range, handler, str */ |
49 | /* keysym, state, range, handler, str */ |
17 | //{XK_ISO_Left_Tab, 0, 1, NORMAL, "\033[Z"}, |
50 | //{XK_ISO_Left_Tab, 0, 1, keysym_t::NORMAL, "\033[Z"}, |
18 | //{ 'a', 0, 26, RANGE_META8, "a" "%c"}, |
51 | //{ 'a', 0, 26, keysym_t::RANGE_META8, "a" "%c"}, |
19 | //{ 'a', ControlMask, 26, RANGE_META8, "" "%c"}, |
52 | //{ 'a', ControlMask, 26, keysym_t::RANGE_META8, "" "%c"}, |
20 | //{ XK_Left, 0, 4, LIST, "DACBZ" "\033[Z"}, |
53 | //{ XK_Left, 0, 4, keysym_t::LIST, ".\033[.DACB."}, |
21 | //{ XK_Left, ShiftMask, 4, LIST, "dacbZ" "\033[Z"}, |
54 | //{ XK_Left, ShiftMask, 4, keysym_t::LIST, ".\033[.dacb."}, |
22 | //{ XK_Left, ControlMask, 4, LIST, "dacbZ" "\033OZ"}, |
55 | //{ XK_Left, ControlMask, 4, keysym_t::LIST, ".\033O.dacb."}, |
23 | //{ XK_Tab, ControlMask, 1, NORMAL, "\033<C-Tab>"}, |
56 | //{ XK_Tab, ControlMask, 1, keysym_t::NORMAL, "\033<C-Tab>"}, |
24 | //{ XK_apostrophe, ControlMask, 1, NORMAL, "\033<C-'>"}, |
57 | //{ XK_apostrophe, ControlMask, 1, keysym_t::NORMAL, "\033<C-'>"}, |
25 | //{ XK_slash, ControlMask, 1, NORMAL, "\033<C-/>"}, |
58 | //{ XK_slash, ControlMask, 1, keysym_t::NORMAL, "\033<C-/>"}, |
26 | //{ XK_semicolon, ControlMask, 1, NORMAL, "\033<C-;>"}, |
59 | //{ XK_semicolon, ControlMask, 1, keysym_t::NORMAL, "\033<C-;>"}, |
27 | //{ XK_grave, ControlMask, 1, NORMAL, "\033<C-`>"}, |
60 | //{ XK_grave, ControlMask, 1, keysym_t::NORMAL, "\033<C-`>"}, |
28 | //{ XK_comma, ControlMask, 1, NORMAL, "\033<C-\054>"}, |
61 | //{ XK_comma, ControlMask, 1, keysym_t::NORMAL, "\033<C-\054>"}, |
29 | //{ XK_Return, ControlMask, 1, NORMAL, "\033<C-Return>"}, |
62 | //{ XK_Return, ControlMask, 1, keysym_t::NORMAL, "\033<C-Return>"}, |
30 | //{ XK_Return, ShiftMask, 1, NORMAL, "\033<S-Return>"}, |
63 | //{ XK_Return, ShiftMask, 1, keysym_t::NORMAL, "\033<S-Return>"}, |
31 | //{ ' ', ShiftMask, 1, NORMAL, "\033<S-Space>"}, |
64 | //{ ' ', ShiftMask, 1, keysym_t::NORMAL, "\033<S-Space>"}, |
32 | //{ '.', ControlMask, 1, NORMAL, "\033<C-.>"}, |
65 | //{ '.', ControlMask, 1, keysym_t::NORMAL, "\033<C-.>"}, |
33 | //{ '0', ControlMask, 10, RANGE, "0" "\033<C-%c>"}, |
66 | //{ '0', ControlMask, 10, keysym_t::RANGE, "0" "\033<C-%c>"}, |
34 | //{ '0', MetaMask|ControlMask, 10, RANGE, "0" "\033<M-C-%c>"}, |
67 | //{ '0', MetaMask|ControlMask, 10, keysym_t::RANGE, "0" "\033<M-C-%c>"}, |
35 | //{ 'a', MetaMask|ControlMask, 26, RANGE, "a" "\033<M-C-%c>"}, |
68 | //{ 'a', MetaMask|ControlMask, 26, keysym_t::RANGE, "a" "\033<M-C-%c>"}, |
36 | }; |
69 | }; |
|
|
70 | #endif |
37 | |
71 | |
38 | static void |
72 | static void |
39 | output_string (rxvt_term *rt, const char *str) |
73 | output_string (rxvt_term *rt, const char *str) |
40 | { |
74 | { |
41 | assert (rt && str); |
|
|
42 | |
|
|
43 | if (strncmp (str, "proto:", 6) == 0) |
75 | if (strncmp (str, "command:", 8) == 0) |
44 | rt->cmd_write ((unsigned char *)str + 6, strlen (str) - 6); |
76 | rt->cmd_write ((unsigned char *)str + 8, strlen (str) - 8); |
45 | else |
77 | else |
46 | rt->tt_write ((unsigned char *)str, strlen (str)); |
78 | rt->tt_write ((unsigned char *)str, strlen (str)); |
47 | } |
79 | } |
48 | |
80 | |
49 | static void |
81 | static void |
… | |
… | |
101 | |
133 | |
102 | // return: priority_of_a - priority_of_b |
134 | // return: priority_of_a - priority_of_b |
103 | static int |
135 | static int |
104 | compare_priority (keysym_t *a, keysym_t *b) |
136 | compare_priority (keysym_t *a, keysym_t *b) |
105 | { |
137 | { |
106 | assert (a && b); |
|
|
107 | |
|
|
108 | // (the more '1's in state; the less range): the greater priority |
138 | // (the more '1's in state; the less range): the greater priority |
109 | int ca = bitcount (a->state /* & OtherModMask */); |
139 | int ca = bitcount (a->state /* & OtherModMask */); |
110 | int cb = bitcount (b->state /* & OtherModMask */); |
140 | int cb = bitcount (b->state /* & OtherModMask */); |
111 | |
141 | |
112 | if (ca != cb) |
142 | if (ca != cb) |
… | |
… | |
157 | // the string 'trans' is copied to an internal managed buffer, |
187 | // the string 'trans' is copied to an internal managed buffer, |
158 | // so the caller can free memory of 'trans' at any time. |
188 | // so the caller can free memory of 'trans' at any time. |
159 | void |
189 | void |
160 | keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const char *trans) |
190 | keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const char *trans) |
161 | { |
191 | { |
162 | assert (trans); |
|
|
163 | |
|
|
164 | keysym_t *key = new keysym_t; |
192 | keysym_t *key = new keysym_t; |
165 | wchar_t *wc = rxvt_mbstowcs (trans); |
193 | wchar_t *wc = rxvt_mbstowcs (trans); |
166 | printf ("CONV <%s> %x %x %x %x\n", trans, (int)wc[0], (int)wc[1], (int)wc[2], (int)wc[3]); |
|
|
167 | const char *translation = rxvt_wcstoutf8 (wc); |
194 | const char *translation = rxvt_wcstoutf8 (wc); |
168 | free (wc); |
195 | free (wc); |
169 | |
196 | |
170 | if (key && translation) |
197 | if (key && translation) |
171 | { |
198 | { |
… | |
… | |
204 | } |
231 | } |
205 | |
232 | |
206 | void |
233 | void |
207 | keyboard_manager::register_keymap (keysym_t *key) |
234 | keyboard_manager::register_keymap (keysym_t *key) |
208 | { |
235 | { |
209 | assert (key); |
|
|
210 | assert (key->range >= 1); |
|
|
211 | |
|
|
212 | if (keymap.size () == keymap.capacity ()) |
236 | if (keymap.size () == keymap.capacity ()) |
213 | keymap.reserve (keymap.size () * 2); |
237 | keymap.reserve (keymap.size () * 2); |
214 | |
238 | |
215 | keymap.push_back (key); |
239 | keymap.push_back (key); |
216 | hash[0] = 3; |
240 | hash[0] = 3; |
217 | } |
241 | } |
218 | |
242 | |
219 | void |
243 | void |
220 | keyboard_manager::register_done () |
244 | keyboard_manager::register_done () |
221 | { |
245 | { |
|
|
246 | #if STOCK_KEYMAP |
222 | unsigned int i, n = sizeof (stock_keymap) / sizeof (keysym_t); |
247 | int n = sizeof (stock_keymap) / sizeof (keysym_t); |
223 | |
248 | |
|
|
249 | //TODO: shield against repeated calls and empty keymap |
224 | if (keymap.back () != &stock_keymap[n - 1]) |
250 | //if (keymap.back () != &stock_keymap[n - 1]) |
225 | for (i = 0; i < n; ++i) |
251 | for (int i = 0; i < n; ++i) |
226 | register_keymap (&stock_keymap[i]); |
252 | register_keymap (&stock_keymap[i]); |
|
|
253 | #endif |
227 | |
254 | |
228 | purge_duplicate_keymap (); |
255 | purge_duplicate_keymap (); |
229 | |
256 | |
230 | setup_hash (); |
257 | setup_hash (); |
231 | } |
258 | } |
232 | |
259 | |
233 | bool |
260 | bool |
234 | keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state) |
261 | keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state) |
235 | { |
262 | { |
236 | assert (hash[0] == 0 && "register_done() need to be called"); |
263 | assert (hash[0] == 0 && "register_done() need to be called"); |
|
|
264 | |
|
|
265 | state &= OtherModMask; // mask out uninteresting modifiers |
237 | |
266 | |
238 | if (state & term->ModMetaMask) state |= MetaMask; |
267 | if (state & term->ModMetaMask) state |= MetaMask; |
239 | if (state & term->ModNumLockMask) state |= NumLockMask; |
268 | if (state & term->ModNumLockMask) state |= NumLockMask; |
240 | if (state & term->ModLevel3Mask) state |= Level3Mask; |
269 | if (state & term->ModLevel3Mask) state |= Level3Mask; |
241 | |
270 | |
… | |
… | |
249 | const keysym_t &key = *keymap [index]; |
278 | const keysym_t &key = *keymap [index]; |
250 | |
279 | |
251 | int keysym_offset = keysym - key.keysym; |
280 | int keysym_offset = keysym - key.keysym; |
252 | |
281 | |
253 | wchar_t *wc = rxvt_utf8towcs (key.str); |
282 | wchar_t *wc = rxvt_utf8towcs (key.str); |
254 | |
|
|
255 | char *str = rxvt_wcstombs (wc); |
283 | char *str = rxvt_wcstombs (wc); |
256 | // TODO: do translations, unescaping etc, here (allow \u escape etc.) |
284 | // TODO: do (some) translations, unescaping etc, here (allow \u escape etc.) |
257 | free (wc); |
285 | free (wc); |
258 | |
286 | |
259 | switch (key.type) |
287 | switch (key.type) |
260 | { |
288 | { |
261 | case keysym_t::NORMAL: |
289 | case keysym_t::NORMAL: |
… | |
… | |
304 | free (str); |
332 | free (str); |
305 | |
333 | |
306 | return true; |
334 | return true; |
307 | } |
335 | } |
308 | else |
336 | else |
309 | { |
|
|
310 | // fprintf(stderr,"[%x:%x]",state,keysym); |
|
|
311 | return false; |
337 | return false; |
312 | } |
|
|
313 | } |
338 | } |
314 | |
339 | |
315 | // purge duplicate keymap entries |
340 | // purge duplicate keymap entries |
316 | void keyboard_manager::purge_duplicate_keymap () |
341 | void keyboard_manager::purge_duplicate_keymap () |
317 | { |
342 | { |
… | |
… | |
327 | if (i < keymap.size ()) |
352 | if (i < keymap.size ()) |
328 | { |
353 | { |
329 | keymap[i] = keymap.back (); |
354 | keymap[i] = keymap.back (); |
330 | keymap.pop_back (); |
355 | keymap.pop_back (); |
331 | } |
356 | } |
|
|
357 | |
332 | break; |
358 | break; |
333 | } |
359 | } |
334 | } |
360 | } |
335 | } |
361 | } |
336 | } |
362 | } |
… | |
… | |
344 | uint16_t hash_budget_counter[KEYSYM_HASH_BUDGETS]; // #elements in each budget |
370 | uint16_t hash_budget_counter[KEYSYM_HASH_BUDGETS]; // #elements in each budget |
345 | |
371 | |
346 | memset (hash_budget_size, 0, sizeof (hash_budget_size)); |
372 | memset (hash_budget_size, 0, sizeof (hash_budget_size)); |
347 | memset (hash_budget_counter, 0, sizeof (hash_budget_counter)); |
373 | memset (hash_budget_counter, 0, sizeof (hash_budget_counter)); |
348 | |
374 | |
349 | // count keysyms for corresponding hash budgets |
375 | // determine hash bucket size |
350 | for (i = 0; i < keymap.size (); ++i) |
376 | for (i = 0; i < keymap.size (); ++i) |
|
|
377 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j) |
351 | { |
378 | { |
352 | assert (keymap [i]); |
|
|
353 | hashkey = (keymap [i]->keysym & KEYSYM_HASH_MASK); |
379 | hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; |
354 | ++hash_budget_size [hashkey]; |
380 | ++hash_budget_size [hashkey]; |
355 | } |
|
|
356 | |
|
|
357 | // keysym A with range>1 is counted one more time for |
|
|
358 | // every keysym B lies in its range |
|
|
359 | for (i = 0; i < keymap.size (); ++i) |
|
|
360 | { |
|
|
361 | if (keymap[i]->range > 1) |
|
|
362 | { |
|
|
363 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j > 0; --j) |
|
|
364 | { |
|
|
365 | hashkey = ((keymap [i]->keysym + j) & KEYSYM_HASH_MASK); |
|
|
366 | if (hash_budget_size [hashkey]) |
|
|
367 | ++hash_budget_size [hashkey]; |
|
|
368 | } |
|
|
369 | } |
381 | } |
370 | } |
|
|
371 | |
382 | |
372 | // now we know the size of each budget |
383 | // now we know the size of each budget |
373 | // compute the index of each budget |
384 | // compute the index of each budget |
374 | hash [0] = 0; |
385 | hash [0] = 0; |
375 | for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i) |
386 | for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i) |
376 | { |
387 | { |
377 | index += hash_budget_size [i - 1]; |
388 | index += hash_budget_size [i - 1]; |
378 | hash[i] = (hash_budget_size [i] ? index : hash [i - 1]); |
389 | hash [i] = index; |
379 | } |
390 | } |
380 | |
391 | |
381 | // and allocate just enough space |
392 | // and allocate just enough space |
382 | //sorted_keymap.reserve (hash[i - 1] + hash_budget_size[i - 1]); |
|
|
383 | sorted_keymap.insert (sorted_keymap.begin (), index + hash_budget_size [i - 1], 0); |
393 | sorted_keymap.insert (sorted_keymap.begin (), index + hash_budget_size [i - 1], 0); |
384 | |
394 | |
385 | // fill in sorted_keymap |
395 | // fill in sorted_keymap |
386 | // it is sorted in each budget |
396 | // it is sorted in each budget |
387 | for (i = 0; i < keymap.size (); ++i) |
397 | for (i = 0; i < keymap.size (); ++i) |
388 | { |
|
|
389 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j) |
398 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j) |
390 | { |
399 | { |
391 | hashkey = ((keymap [i]->keysym + j) & KEYSYM_HASH_MASK); |
400 | hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; |
392 | |
401 | |
393 | if (hash_budget_size [hashkey]) |
|
|
394 | { |
|
|
395 | index = hash [hashkey] + hash_budget_counter [hashkey]; |
402 | index = hash [hashkey] + hash_budget_counter [hashkey]; |
396 | |
403 | |
397 | while (index > hash [hashkey] |
404 | while (index > hash [hashkey] |
398 | && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) |
405 | && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) |
399 | { |
406 | { |
400 | sorted_keymap [index] = sorted_keymap [index - 1]; |
407 | sorted_keymap [index] = sorted_keymap [index - 1]; |
401 | --index; |
408 | --index; |
402 | } |
409 | } |
403 | |
410 | |
404 | sorted_keymap [index] = keymap [i]; |
411 | sorted_keymap [index] = keymap [i]; |
405 | ++hash_budget_counter [hashkey]; |
412 | ++hash_budget_counter [hashkey]; |
406 | } |
|
|
407 | } |
413 | } |
408 | } |
|
|
409 | |
414 | |
410 | keymap.swap (sorted_keymap); |
415 | keymap.swap (sorted_keymap); |
411 | |
416 | |
412 | #if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD) |
417 | #if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD) |
413 | // check for invariants |
418 | // check for invariants |
… | |
… | |
434 | int index = find_keysym (a->keysym + j, a->state); |
439 | int index = find_keysym (a->keysym + j, a->state); |
435 | |
440 | |
436 | assert (index >= 0); |
441 | assert (index >= 0); |
437 | keysym_t *b = keymap [index]; |
442 | keysym_t *b = keymap [index]; |
438 | assert (i == (signed) index || // the normally expected result |
443 | assert (i == (signed) index || // the normally expected result |
439 | (a->keysym + j) >= b->keysym && (a->keysym + j) <= (b->keysym + b->range) && compare_priority (a, b) <= 0); // is effectively the same |
444 | (a->keysym + j) >= b->keysym && (a->keysym + j) <= (b->keysym + b->range) && compare_priority (a, b) <= 0); // is effectively the same or a closer match |
440 | } |
445 | } |
441 | } |
446 | } |
442 | #endif |
447 | #endif |
443 | } |
448 | } |
444 | |
449 | |
445 | int |
450 | int |
446 | keyboard_manager::find_keysym (KeySym keysym, unsigned int state) |
451 | keyboard_manager::find_keysym (KeySym keysym, unsigned int state) |
447 | { |
452 | { |
448 | int hashkey = keysym & KEYSYM_HASH_MASK; |
453 | int hashkey = keysym & KEYSYM_HASH_MASK; |
449 | unsigned int index = hash [hashkey]; |
454 | unsigned int index = hash [hashkey]; |
|
|
455 | unsigned int end = hashkey < KEYSYM_HASH_BUDGETS - 1 |
|
|
456 | ? hash [hashkey + 1] |
|
|
457 | : keymap.size (); |
450 | |
458 | |
451 | for (; index < keymap.size (); ++index) |
459 | for (; index < end; ++index) |
452 | { |
460 | { |
453 | keysym_t *key = keymap [index]; |
461 | keysym_t *key = keymap [index]; |
454 | assert (key); |
|
|
455 | |
462 | |
456 | if (key->keysym <= keysym && key->keysym + key->range > keysym |
463 | if (key->keysym <= keysym && keysym < key->keysym + key->range |
|
|
464 | #if 0 // disabled because the custom ekymap does not know the builtin keymap |
457 | // match only the specified bits in state and ignore others |
465 | // match only the specified bits in state and ignore others |
458 | && (key->state & state) == key->state) |
466 | && (key->state & state) == key->state |
|
|
467 | #else // re-enable this part once the builtin keymap is handled here, too |
|
|
468 | && key->state == state |
|
|
469 | #endif |
|
|
470 | ) |
459 | return index; |
471 | return index; |
460 | else if (key->keysym > keysym && key->range == 1) |
|
|
461 | return -1; |
|
|
462 | } |
472 | } |
463 | |
473 | |
464 | return -1; |
474 | return -1; |
465 | } |
475 | } |
466 | |
476 | |