… | |
… | |
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 | if (strncmp (str, "proto:", 6) == 0) |
75 | if (strncmp (str, "command:", 8) == 0) |
42 | rt->cmd_write ((unsigned char *)str + 6, strlen (str) - 6); |
76 | rt->cmd_write ((unsigned char *)str + 8, strlen (str) - 8); |
43 | else |
77 | else |
44 | rt->tt_write ((unsigned char *)str, strlen (str)); |
78 | rt->tt_write ((unsigned char *)str, strlen (str)); |
45 | } |
79 | } |
46 | |
80 | |
47 | static void |
81 | static void |
… | |
… | |
207 | } |
241 | } |
208 | |
242 | |
209 | void |
243 | void |
210 | keyboard_manager::register_done () |
244 | keyboard_manager::register_done () |
211 | { |
245 | { |
|
|
246 | #if STOCK_KEYMAP |
212 | unsigned int i, n = sizeof (stock_keymap) / sizeof (keysym_t); |
247 | int n = sizeof (stock_keymap) / sizeof (keysym_t); |
213 | |
248 | |
|
|
249 | //TODO: shield against repeated calls and empty keymap |
214 | if (keymap.back () != &stock_keymap[n - 1]) |
250 | //if (keymap.back () != &stock_keymap[n - 1]) |
215 | for (i = 0; i < n; ++i) |
251 | for (int i = 0; i < n; ++i) |
216 | register_keymap (&stock_keymap[i]); |
252 | register_keymap (&stock_keymap[i]); |
|
|
253 | #endif |
217 | |
254 | |
218 | purge_duplicate_keymap (); |
255 | purge_duplicate_keymap (); |
219 | |
256 | |
220 | setup_hash (); |
257 | setup_hash (); |
221 | } |
258 | } |
… | |
… | |
239 | const keysym_t &key = *keymap [index]; |
276 | const keysym_t &key = *keymap [index]; |
240 | |
277 | |
241 | int keysym_offset = keysym - key.keysym; |
278 | int keysym_offset = keysym - key.keysym; |
242 | |
279 | |
243 | wchar_t *wc = rxvt_utf8towcs (key.str); |
280 | wchar_t *wc = rxvt_utf8towcs (key.str); |
244 | |
|
|
245 | char *str = rxvt_wcstombs (wc); |
281 | char *str = rxvt_wcstombs (wc); |
246 | // TODO: do translations, unescaping etc, here (allow \u escape etc.) |
282 | // TODO: do (some) translations, unescaping etc, here (allow \u escape etc.) |
247 | free (wc); |
283 | free (wc); |
248 | |
284 | |
249 | switch (key.type) |
285 | switch (key.type) |
250 | { |
286 | { |
251 | case keysym_t::NORMAL: |
287 | case keysym_t::NORMAL: |
… | |
… | |
294 | free (str); |
330 | free (str); |
295 | |
331 | |
296 | return true; |
332 | return true; |
297 | } |
333 | } |
298 | else |
334 | else |
299 | { |
|
|
300 | // fprintf(stderr,"[%x:%x]",state,keysym); |
|
|
301 | return false; |
335 | return false; |
302 | } |
|
|
303 | } |
336 | } |
304 | |
337 | |
305 | // purge duplicate keymap entries |
338 | // purge duplicate keymap entries |
306 | void keyboard_manager::purge_duplicate_keymap () |
339 | void keyboard_manager::purge_duplicate_keymap () |
307 | { |
340 | { |
… | |
… | |
317 | if (i < keymap.size ()) |
350 | if (i < keymap.size ()) |
318 | { |
351 | { |
319 | keymap[i] = keymap.back (); |
352 | keymap[i] = keymap.back (); |
320 | keymap.pop_back (); |
353 | keymap.pop_back (); |
321 | } |
354 | } |
|
|
355 | |
322 | break; |
356 | break; |
323 | } |
357 | } |
324 | } |
358 | } |
325 | } |
359 | } |
326 | } |
360 | } |
… | |
… | |
334 | uint16_t hash_budget_counter[KEYSYM_HASH_BUDGETS]; // #elements in each budget |
368 | uint16_t hash_budget_counter[KEYSYM_HASH_BUDGETS]; // #elements in each budget |
335 | |
369 | |
336 | memset (hash_budget_size, 0, sizeof (hash_budget_size)); |
370 | memset (hash_budget_size, 0, sizeof (hash_budget_size)); |
337 | memset (hash_budget_counter, 0, sizeof (hash_budget_counter)); |
371 | memset (hash_budget_counter, 0, sizeof (hash_budget_counter)); |
338 | |
372 | |
339 | // count keysyms for corresponding hash budgets |
373 | // determine hash bucket size |
340 | for (i = 0; i < keymap.size (); ++i) |
374 | for (i = 0; i < keymap.size (); ++i) |
|
|
375 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j) |
341 | { |
376 | { |
342 | hashkey = keymap [i]->keysym & KEYSYM_HASH_MASK; |
377 | hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; |
343 | ++hash_budget_size [hashkey]; |
378 | ++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 | } |
379 | } |
359 | } |
|
|
360 | |
380 | |
361 | // now we know the size of each budget |
381 | // now we know the size of each budget |
362 | // compute the index of each budget |
382 | // compute the index of each budget |
363 | hash [0] = 0; |
383 | hash [0] = 0; |
364 | for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i) |
384 | for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i) |
365 | { |
385 | { |
366 | index += hash_budget_size [i - 1]; |
386 | index += hash_budget_size [i - 1]; |
367 | hash[i] = (hash_budget_size [i] ? index : hash [i - 1]); |
387 | hash [i] = index; |
368 | } |
388 | } |
369 | |
389 | |
370 | // and allocate just enough space |
390 | // and allocate just enough space |
371 | //sorted_keymap.reserve (hash[i - 1] + hash_budget_size[i - 1]); |
|
|
372 | sorted_keymap.insert (sorted_keymap.begin (), index + hash_budget_size [i - 1], 0); |
391 | sorted_keymap.insert (sorted_keymap.begin (), index + hash_budget_size [i - 1], 0); |
373 | |
392 | |
374 | // fill in sorted_keymap |
393 | // fill in sorted_keymap |
375 | // it is sorted in each budget |
394 | // it is sorted in each budget |
376 | for (i = 0; i < keymap.size (); ++i) |
395 | for (i = 0; i < keymap.size (); ++i) |
377 | { |
|
|
378 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j) |
396 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j) |
379 | { |
397 | { |
380 | hashkey = ((keymap [i]->keysym + j) & KEYSYM_HASH_MASK); |
398 | hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; |
381 | |
399 | |
382 | if (hash_budget_size [hashkey]) |
|
|
383 | { |
|
|
384 | index = hash [hashkey] + hash_budget_counter [hashkey]; |
400 | index = hash [hashkey] + hash_budget_counter [hashkey]; |
385 | |
401 | |
386 | while (index > hash [hashkey] |
402 | while (index > hash [hashkey] |
387 | && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) |
403 | && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) |
388 | { |
404 | { |
389 | sorted_keymap [index] = sorted_keymap [index - 1]; |
405 | sorted_keymap [index] = sorted_keymap [index - 1]; |
390 | --index; |
406 | --index; |
391 | } |
407 | } |
392 | |
408 | |
393 | sorted_keymap [index] = keymap [i]; |
409 | sorted_keymap [index] = keymap [i]; |
394 | ++hash_budget_counter [hashkey]; |
410 | ++hash_budget_counter [hashkey]; |
395 | } |
|
|
396 | } |
411 | } |
397 | } |
|
|
398 | |
412 | |
399 | keymap.swap (sorted_keymap); |
413 | keymap.swap (sorted_keymap); |
400 | |
414 | |
401 | #if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD) |
415 | #if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD) |
402 | // check for invariants |
416 | // check for invariants |
… | |
… | |
423 | int index = find_keysym (a->keysym + j, a->state); |
437 | int index = find_keysym (a->keysym + j, a->state); |
424 | |
438 | |
425 | assert (index >= 0); |
439 | assert (index >= 0); |
426 | keysym_t *b = keymap [index]; |
440 | keysym_t *b = keymap [index]; |
427 | assert (i == (signed) index || // the normally expected result |
441 | assert (i == (signed) index || // the normally expected result |
428 | (a->keysym + j) >= b->keysym && (a->keysym + j) <= (b->keysym + b->range) && compare_priority (a, b) <= 0); // is effectively the same |
442 | (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 |
429 | } |
443 | } |
430 | } |
444 | } |
431 | #endif |
445 | #endif |
432 | } |
446 | } |
433 | |
447 | |
434 | int |
448 | int |
435 | keyboard_manager::find_keysym (KeySym keysym, unsigned int state) |
449 | keyboard_manager::find_keysym (KeySym keysym, unsigned int state) |
436 | { |
450 | { |
437 | int hashkey = keysym & KEYSYM_HASH_MASK; |
451 | int hashkey = keysym & KEYSYM_HASH_MASK; |
438 | unsigned int index = hash [hashkey]; |
452 | unsigned int index = hash [hashkey]; |
|
|
453 | unsigned int end = hashkey < KEYSYM_HASH_BUDGETS - 1 |
|
|
454 | ? hash [hashkey + 1] |
|
|
455 | : keymap.size (); |
439 | |
456 | |
440 | for (; index < keymap.size (); ++index) |
457 | for (; index < end; ++index) |
441 | { |
458 | { |
442 | keysym_t *key = keymap [index]; |
459 | keysym_t *key = keymap [index]; |
443 | |
460 | |
444 | if (key->keysym <= keysym && key->keysym + key->range > keysym |
461 | if (key->keysym <= keysym && keysym < key->keysym + key->range |
445 | // match only the specified bits in state and ignore others |
462 | // match only the specified bits in state and ignore others |
446 | && (key->state & state) == key->state) |
463 | && (key->state & state) == key->state) |
447 | return index; |
464 | return index; |
448 | else if (key->keysym > keysym && key->range == 1) |
|
|
449 | return -1; |
|
|
450 | } |
465 | } |
451 | |
466 | |
452 | return -1; |
467 | return -1; |
453 | } |
468 | } |
454 | |
469 | |