… | |
… | |
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 | #if STOCK_KEYMAP |
11 | //////////////////////////////////////////////////////////////////////////////// |
12 | //////////////////////////////////////////////////////////////////////////////// |
12 | // default keycode translation map and keyevent handlers |
13 | // default keycode translation map and keyevent handlers |
13 | |
14 | |
14 | keysym_t keyboard_manager::stock_keymap[] = { |
15 | keysym_t keyboard_manager::stock_keymap[] = { |
15 | /* examples */ |
16 | /* examples */ |
16 | /* keysym, state, range, handler, str */ |
17 | /* keysym, state, range, handler, str */ |
17 | //{XK_ISO_Left_Tab, 0, 1, NORMAL, "\033[Z"}, |
18 | //{XK_ISO_Left_Tab, 0, 1, keysym_t::NORMAL, "\033[Z"}, |
18 | //{ 'a', 0, 26, RANGE_META8, "a" "%c"}, |
19 | //{ 'a', 0, 26, keysym_t::RANGE_META8, "a" "%c"}, |
19 | //{ 'a', ControlMask, 26, RANGE_META8, "" "%c"}, |
20 | //{ 'a', ControlMask, 26, keysym_t::RANGE_META8, "" "%c"}, |
20 | //{ XK_Left, 0, 4, LIST, "DACBZ" "\033[Z"}, |
21 | //{ XK_Left, 0, 4, keysym_t::LIST, ".\033[.DACB."}, |
21 | //{ XK_Left, ShiftMask, 4, LIST, "dacbZ" "\033[Z"}, |
22 | //{ XK_Left, ShiftMask, 4, keysym_t::LIST, ".\033[.dacb."}, |
22 | //{ XK_Left, ControlMask, 4, LIST, "dacbZ" "\033OZ"}, |
23 | //{ XK_Left, ControlMask, 4, keysym_t::LIST, ".\033O.dacb."}, |
23 | //{ XK_Tab, ControlMask, 1, NORMAL, "\033<C-Tab>"}, |
24 | //{ XK_Tab, ControlMask, 1, keysym_t::NORMAL, "\033<C-Tab>"}, |
24 | //{ XK_apostrophe, ControlMask, 1, NORMAL, "\033<C-'>"}, |
25 | //{ XK_apostrophe, ControlMask, 1, keysym_t::NORMAL, "\033<C-'>"}, |
25 | //{ XK_slash, ControlMask, 1, NORMAL, "\033<C-/>"}, |
26 | //{ XK_slash, ControlMask, 1, keysym_t::NORMAL, "\033<C-/>"}, |
26 | //{ XK_semicolon, ControlMask, 1, NORMAL, "\033<C-;>"}, |
27 | //{ XK_semicolon, ControlMask, 1, keysym_t::NORMAL, "\033<C-;>"}, |
27 | //{ XK_grave, ControlMask, 1, NORMAL, "\033<C-`>"}, |
28 | //{ XK_grave, ControlMask, 1, keysym_t::NORMAL, "\033<C-`>"}, |
28 | //{ XK_comma, ControlMask, 1, NORMAL, "\033<C-\054>"}, |
29 | //{ XK_comma, ControlMask, 1, keysym_t::NORMAL, "\033<C-\054>"}, |
29 | //{ XK_Return, ControlMask, 1, NORMAL, "\033<C-Return>"}, |
30 | //{ XK_Return, ControlMask, 1, keysym_t::NORMAL, "\033<C-Return>"}, |
30 | //{ XK_Return, ShiftMask, 1, NORMAL, "\033<S-Return>"}, |
31 | //{ XK_Return, ShiftMask, 1, keysym_t::NORMAL, "\033<S-Return>"}, |
31 | //{ ' ', ShiftMask, 1, NORMAL, "\033<S-Space>"}, |
32 | //{ ' ', ShiftMask, 1, keysym_t::NORMAL, "\033<S-Space>"}, |
32 | //{ '.', ControlMask, 1, NORMAL, "\033<C-.>"}, |
33 | //{ '.', ControlMask, 1, keysym_t::NORMAL, "\033<C-.>"}, |
33 | //{ '0', ControlMask, 10, RANGE, "0" "\033<C-%c>"}, |
34 | //{ '0', ControlMask, 10, keysym_t::RANGE, "0" "\033<C-%c>"}, |
34 | //{ '0', MetaMask|ControlMask, 10, RANGE, "0" "\033<M-C-%c>"}, |
35 | //{ '0', MetaMask|ControlMask, 10, keysym_t::RANGE, "0" "\033<M-C-%c>"}, |
35 | //{ 'a', MetaMask|ControlMask, 26, RANGE, "a" "\033<M-C-%c>"}, |
36 | //{ 'a', MetaMask|ControlMask, 26, keysym_t::RANGE, "a" "\033<M-C-%c>"}, |
36 | }; |
37 | }; |
|
|
38 | #endif |
37 | |
39 | |
38 | static void |
40 | static void |
39 | output_string (rxvt_term *rt, const char *str) |
41 | output_string (rxvt_term *rt, const char *str) |
40 | { |
42 | { |
41 | assert (rt && str); |
|
|
42 | |
|
|
43 | if (strncmp (str, "proto:", 6) == 0) |
43 | if (strncmp (str, "command:", 8) == 0) |
44 | rt->cmd_write ((unsigned char *)str + 6, strlen (str) - 6); |
44 | rt->cmd_write ((unsigned char *)str + 8, strlen (str) - 8); |
45 | else |
45 | else |
46 | rt->tt_write ((unsigned char *)str, strlen (str)); |
46 | rt->tt_write ((unsigned char *)str, strlen (str)); |
47 | } |
47 | } |
48 | |
48 | |
49 | static void |
49 | static void |
… | |
… | |
101 | |
101 | |
102 | // return: priority_of_a - priority_of_b |
102 | // return: priority_of_a - priority_of_b |
103 | static int |
103 | static int |
104 | compare_priority (keysym_t *a, keysym_t *b) |
104 | compare_priority (keysym_t *a, keysym_t *b) |
105 | { |
105 | { |
106 | assert (a && b); |
|
|
107 | |
|
|
108 | // (the more '1's in state; the less range): the greater priority |
106 | // (the more '1's in state; the less range): the greater priority |
109 | int ca = bitcount (a->state /* & OtherModMask */); |
107 | int ca = bitcount (a->state /* & OtherModMask */); |
110 | int cb = bitcount (b->state /* & OtherModMask */); |
108 | int cb = bitcount (b->state /* & OtherModMask */); |
111 | |
109 | |
112 | if (ca != cb) |
110 | if (ca != cb) |
… | |
… | |
157 | // the string 'trans' is copied to an internal managed buffer, |
155 | // the string 'trans' is copied to an internal managed buffer, |
158 | // so the caller can free memory of 'trans' at any time. |
156 | // so the caller can free memory of 'trans' at any time. |
159 | void |
157 | void |
160 | keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const char *trans) |
158 | keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const char *trans) |
161 | { |
159 | { |
162 | assert (trans); |
|
|
163 | |
|
|
164 | keysym_t *key = new keysym_t; |
160 | keysym_t *key = new keysym_t; |
165 | wchar_t *wc = rxvt_mbstowcs (trans); |
161 | 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); |
162 | const char *translation = rxvt_wcstoutf8 (wc); |
168 | free (wc); |
163 | free (wc); |
169 | |
164 | |
170 | if (key && translation) |
165 | if (key && translation) |
171 | { |
166 | { |
… | |
… | |
204 | } |
199 | } |
205 | |
200 | |
206 | void |
201 | void |
207 | keyboard_manager::register_keymap (keysym_t *key) |
202 | keyboard_manager::register_keymap (keysym_t *key) |
208 | { |
203 | { |
209 | assert (key); |
|
|
210 | assert (key->range >= 1); |
|
|
211 | |
|
|
212 | if (keymap.size () == keymap.capacity ()) |
204 | if (keymap.size () == keymap.capacity ()) |
213 | keymap.reserve (keymap.size () * 2); |
205 | keymap.reserve (keymap.size () * 2); |
214 | |
206 | |
215 | keymap.push_back (key); |
207 | keymap.push_back (key); |
216 | hash[0] = 3; |
208 | hash[0] = 3; |
217 | } |
209 | } |
218 | |
210 | |
219 | void |
211 | void |
220 | keyboard_manager::register_done () |
212 | keyboard_manager::register_done () |
221 | { |
213 | { |
|
|
214 | #if STOCK_KEYMAP |
222 | unsigned int i, n = sizeof (stock_keymap) / sizeof (keysym_t); |
215 | int n = sizeof (stock_keymap) / sizeof (keysym_t); |
223 | |
216 | |
|
|
217 | //TODO: shield against repeated calls and empty keymap |
224 | if (keymap.back () != &stock_keymap[n - 1]) |
218 | //if (keymap.back () != &stock_keymap[n - 1]) |
225 | for (i = 0; i < n; ++i) |
219 | for (int i = 0; i < n; ++i) |
226 | register_keymap (&stock_keymap[i]); |
220 | register_keymap (&stock_keymap[i]); |
|
|
221 | #endif |
227 | |
222 | |
228 | purge_duplicate_keymap (); |
223 | purge_duplicate_keymap (); |
229 | |
224 | |
230 | setup_hash (); |
225 | setup_hash (); |
231 | } |
226 | } |
… | |
… | |
249 | const keysym_t &key = *keymap [index]; |
244 | const keysym_t &key = *keymap [index]; |
250 | |
245 | |
251 | int keysym_offset = keysym - key.keysym; |
246 | int keysym_offset = keysym - key.keysym; |
252 | |
247 | |
253 | wchar_t *wc = rxvt_utf8towcs (key.str); |
248 | wchar_t *wc = rxvt_utf8towcs (key.str); |
254 | |
|
|
255 | char *str = rxvt_wcstombs (wc); |
249 | char *str = rxvt_wcstombs (wc); |
256 | // TODO: do translations, unescaping etc, here (allow \u escape etc.) |
250 | // TODO: do (some) translations, unescaping etc, here (allow \u escape etc.) |
257 | free (wc); |
251 | free (wc); |
258 | |
252 | |
259 | switch (key.type) |
253 | switch (key.type) |
260 | { |
254 | { |
261 | case keysym_t::NORMAL: |
255 | case keysym_t::NORMAL: |
… | |
… | |
304 | free (str); |
298 | free (str); |
305 | |
299 | |
306 | return true; |
300 | return true; |
307 | } |
301 | } |
308 | else |
302 | else |
309 | { |
|
|
310 | // fprintf(stderr,"[%x:%x]",state,keysym); |
|
|
311 | return false; |
303 | return false; |
312 | } |
|
|
313 | } |
304 | } |
314 | |
305 | |
315 | // purge duplicate keymap entries |
306 | // purge duplicate keymap entries |
316 | void keyboard_manager::purge_duplicate_keymap () |
307 | void keyboard_manager::purge_duplicate_keymap () |
317 | { |
308 | { |
… | |
… | |
327 | if (i < keymap.size ()) |
318 | if (i < keymap.size ()) |
328 | { |
319 | { |
329 | keymap[i] = keymap.back (); |
320 | keymap[i] = keymap.back (); |
330 | keymap.pop_back (); |
321 | keymap.pop_back (); |
331 | } |
322 | } |
|
|
323 | |
332 | break; |
324 | break; |
333 | } |
325 | } |
334 | } |
326 | } |
335 | } |
327 | } |
336 | } |
328 | } |
… | |
… | |
344 | uint16_t hash_budget_counter[KEYSYM_HASH_BUDGETS]; // #elements in each budget |
336 | uint16_t hash_budget_counter[KEYSYM_HASH_BUDGETS]; // #elements in each budget |
345 | |
337 | |
346 | memset (hash_budget_size, 0, sizeof (hash_budget_size)); |
338 | memset (hash_budget_size, 0, sizeof (hash_budget_size)); |
347 | memset (hash_budget_counter, 0, sizeof (hash_budget_counter)); |
339 | memset (hash_budget_counter, 0, sizeof (hash_budget_counter)); |
348 | |
340 | |
349 | // count keysyms for corresponding hash budgets |
341 | // determine hash bucket size |
350 | for (i = 0; i < keymap.size (); ++i) |
342 | for (i = 0; i < keymap.size (); ++i) |
|
|
343 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j) |
351 | { |
344 | { |
352 | assert (keymap [i]); |
|
|
353 | hashkey = (keymap [i]->keysym & KEYSYM_HASH_MASK); |
345 | hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; |
354 | ++hash_budget_size [hashkey]; |
346 | ++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 | } |
347 | } |
370 | } |
|
|
371 | |
348 | |
372 | // now we know the size of each budget |
349 | // now we know the size of each budget |
373 | // compute the index of each budget |
350 | // compute the index of each budget |
374 | hash [0] = 0; |
351 | hash [0] = 0; |
375 | for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i) |
352 | for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i) |
376 | { |
353 | { |
377 | index += hash_budget_size [i - 1]; |
354 | index += hash_budget_size [i - 1]; |
378 | hash[i] = (hash_budget_size [i] ? index : hash [i - 1]); |
355 | hash [i] = index; |
379 | } |
356 | } |
380 | |
357 | |
381 | // and allocate just enough space |
358 | // 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); |
359 | sorted_keymap.insert (sorted_keymap.begin (), index + hash_budget_size [i - 1], 0); |
384 | |
360 | |
385 | // fill in sorted_keymap |
361 | // fill in sorted_keymap |
386 | // it is sorted in each budget |
362 | // it is sorted in each budget |
387 | for (i = 0; i < keymap.size (); ++i) |
363 | for (i = 0; i < keymap.size (); ++i) |
388 | { |
|
|
389 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j) |
364 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j) |
390 | { |
365 | { |
391 | hashkey = ((keymap [i]->keysym + j) & KEYSYM_HASH_MASK); |
366 | hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; |
392 | |
367 | |
393 | if (hash_budget_size [hashkey]) |
|
|
394 | { |
|
|
395 | index = hash [hashkey] + hash_budget_counter [hashkey]; |
368 | index = hash [hashkey] + hash_budget_counter [hashkey]; |
396 | |
369 | |
397 | while (index > hash [hashkey] |
370 | while (index > hash [hashkey] |
398 | && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) |
371 | && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) |
399 | { |
372 | { |
400 | sorted_keymap [index] = sorted_keymap [index - 1]; |
373 | sorted_keymap [index] = sorted_keymap [index - 1]; |
401 | --index; |
374 | --index; |
402 | } |
375 | } |
403 | |
376 | |
404 | sorted_keymap [index] = keymap [i]; |
377 | sorted_keymap [index] = keymap [i]; |
405 | ++hash_budget_counter [hashkey]; |
378 | ++hash_budget_counter [hashkey]; |
406 | } |
|
|
407 | } |
379 | } |
408 | } |
|
|
409 | |
380 | |
410 | keymap.swap (sorted_keymap); |
381 | keymap.swap (sorted_keymap); |
411 | |
382 | |
412 | #if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD) |
383 | #if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD) |
413 | // check for invariants |
384 | // check for invariants |
… | |
… | |
434 | int index = find_keysym (a->keysym + j, a->state); |
405 | int index = find_keysym (a->keysym + j, a->state); |
435 | |
406 | |
436 | assert (index >= 0); |
407 | assert (index >= 0); |
437 | keysym_t *b = keymap [index]; |
408 | keysym_t *b = keymap [index]; |
438 | assert (i == (signed) index || // the normally expected result |
409 | 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 |
410 | (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 | } |
411 | } |
441 | } |
412 | } |
442 | #endif |
413 | #endif |
443 | } |
414 | } |
444 | |
415 | |
445 | int |
416 | int |
446 | keyboard_manager::find_keysym (KeySym keysym, unsigned int state) |
417 | keyboard_manager::find_keysym (KeySym keysym, unsigned int state) |
447 | { |
418 | { |
448 | int hashkey = keysym & KEYSYM_HASH_MASK; |
419 | int hashkey = keysym & KEYSYM_HASH_MASK; |
449 | unsigned int index = hash [hashkey]; |
420 | unsigned int index = hash [hashkey]; |
|
|
421 | unsigned int end = hashkey < KEYSYM_HASH_BUDGETS - 1 |
|
|
422 | ? hash [hashkey + 1] |
|
|
423 | : keymap.size (); |
450 | |
424 | |
451 | for (; index < keymap.size (); ++index) |
425 | for (; index < end; ++index) |
452 | { |
426 | { |
453 | keysym_t *key = keymap [index]; |
427 | keysym_t *key = keymap [index]; |
454 | assert (key); |
|
|
455 | |
428 | |
456 | if (key->keysym <= keysym && key->keysym + key->range > keysym |
429 | if (key->keysym <= keysym && keysym < key->keysym + key->range |
457 | // match only the specified bits in state and ignore others |
430 | // match only the specified bits in state and ignore others |
458 | && (key->state & state) == key->state) |
431 | && (key->state & state) == key->state) |
459 | return index; |
432 | return index; |
460 | else if (key->keysym > keysym && key->range == 1) |
|
|
461 | return -1; |
|
|
462 | } |
433 | } |
463 | |
434 | |
464 | return -1; |
435 | return -1; |
465 | } |
436 | } |
466 | |
437 | |