… | |
… | |
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 */ |
… | |
… | |
32 | //{ '.', ControlMask, 1, keysym_t::NORMAL, "\033<C-.>"}, |
65 | //{ '.', ControlMask, 1, keysym_t::NORMAL, "\033<C-.>"}, |
33 | //{ '0', ControlMask, 10, keysym_t::RANGE, "0" "\033<C-%c>"}, |
66 | //{ '0', ControlMask, 10, keysym_t::RANGE, "0" "\033<C-%c>"}, |
34 | //{ '0', MetaMask|ControlMask, 10, keysym_t::RANGE, "0" "\033<M-C-%c>"}, |
67 | //{ '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>"}, |
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, "command:", 8) == 0) |
75 | if (strncmp (str, "command:", 8) == 0) |
… | |
… | |
81 | } |
115 | } |
82 | |
116 | |
83 | //////////////////////////////////////////////////////////////////////////////// |
117 | //////////////////////////////////////////////////////////////////////////////// |
84 | // return: #bits of '1' |
118 | // return: #bits of '1' |
85 | #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3) |
119 | #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3) |
86 | # define bitcount(n) (__extension__ ({ uint32_t n__ = (n); __builtin_popcount (n); })) |
120 | # define bitcount(n) (__extension__ ({ uint32_t n__ = (n); __builtin_popcount (n__); })) |
87 | #else |
121 | #else |
88 | static int |
122 | static int |
89 | bitcount (uint16_t n) |
123 | bitcount (uint16_t n) |
90 | { |
124 | { |
91 | int i; |
125 | int i; |
… | |
… | |
181 | strcpy (translation, translation + 4); |
215 | strcpy (translation, translation + 4); |
182 | } |
216 | } |
183 | else |
217 | else |
184 | rxvt_warn ("cannot parse list-type keysym '%s', treating as normal keysym.\n", translation); |
218 | rxvt_warn ("cannot parse list-type keysym '%s', treating as normal keysym.\n", translation); |
185 | } |
219 | } |
|
|
220 | else if (strncmp (translation, "builtin:", 8) == 0) |
|
|
221 | key->type = keysym_t::BUILTIN; |
186 | |
222 | |
187 | user_keymap.push_back (key); |
223 | user_keymap.push_back (key); |
188 | user_translations.push_back (translation); |
224 | user_translations.push_back (translation); |
189 | register_keymap (key); |
225 | register_keymap (key); |
190 | } |
226 | } |
… | |
… | |
207 | } |
243 | } |
208 | |
244 | |
209 | void |
245 | void |
210 | keyboard_manager::register_done () |
246 | keyboard_manager::register_done () |
211 | { |
247 | { |
|
|
248 | #if STOCK_KEYMAP |
212 | unsigned int i, n = sizeof (stock_keymap) / sizeof (keysym_t); |
249 | int n = sizeof (stock_keymap) / sizeof (keysym_t); |
213 | |
250 | |
|
|
251 | //TODO: shield against repeated calls and empty keymap |
214 | if (keymap.back () != &stock_keymap[n - 1]) |
252 | //if (keymap.back () != &stock_keymap[n - 1]) |
215 | for (i = 0; i < n; ++i) |
253 | for (int i = 0; i < n; ++i) |
216 | register_keymap (&stock_keymap[i]); |
254 | register_keymap (&stock_keymap[i]); |
|
|
255 | #endif |
217 | |
256 | |
218 | purge_duplicate_keymap (); |
257 | purge_duplicate_keymap (); |
219 | |
258 | |
220 | setup_hash (); |
259 | setup_hash (); |
221 | } |
260 | } |
222 | |
261 | |
223 | bool |
262 | bool |
224 | keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state) |
263 | keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state) |
225 | { |
264 | { |
226 | assert (hash[0] == 0 && "register_done() need to be called"); |
265 | assert (hash[0] == 0 && "register_done() need to be called"); |
|
|
266 | |
|
|
267 | state &= OtherModMask; // mask out uninteresting modifiers |
227 | |
268 | |
228 | if (state & term->ModMetaMask) state |= MetaMask; |
269 | if (state & term->ModMetaMask) state |= MetaMask; |
229 | if (state & term->ModNumLockMask) state |= NumLockMask; |
270 | if (state & term->ModNumLockMask) state |= NumLockMask; |
230 | if (state & term->ModLevel3Mask) state |= Level3Mask; |
271 | if (state & term->ModLevel3Mask) state |= Level3Mask; |
231 | |
272 | |
… | |
… | |
236 | |
277 | |
237 | if (index >= 0) |
278 | if (index >= 0) |
238 | { |
279 | { |
239 | const keysym_t &key = *keymap [index]; |
280 | const keysym_t &key = *keymap [index]; |
240 | |
281 | |
|
|
282 | if (key.type != keysym_t::BUILTIN) |
|
|
283 | { |
241 | int keysym_offset = keysym - key.keysym; |
284 | int keysym_offset = keysym - key.keysym; |
242 | |
285 | |
243 | wchar_t *wc = rxvt_utf8towcs (key.str); |
286 | wchar_t *wc = rxvt_utf8towcs (key.str); |
244 | |
|
|
245 | char *str = rxvt_wcstombs (wc); |
287 | char *str = rxvt_wcstombs (wc); |
246 | // TODO: do translations, unescaping etc, here (allow \u escape etc.) |
288 | // TODO: do (some) translations, unescaping etc, here (allow \u escape etc.) |
247 | free (wc); |
289 | free (wc); |
248 | |
290 | |
249 | switch (key.type) |
291 | switch (key.type) |
250 | { |
|
|
251 | case keysym_t::NORMAL: |
|
|
252 | output_string (term, str); |
|
|
253 | break; |
|
|
254 | |
|
|
255 | case keysym_t::RANGE: |
|
|
256 | { |
292 | { |
|
|
293 | case keysym_t::NORMAL: |
|
|
294 | output_string (term, str); |
|
|
295 | break; |
|
|
296 | |
|
|
297 | case keysym_t::RANGE: |
|
|
298 | { |
257 | char buf[STRING_MAX]; |
299 | char buf[STRING_MAX]; |
258 | |
300 | |
259 | if (format_keyrange_string (str, keysym_offset, buf, sizeof (buf)) > 0) |
301 | if (format_keyrange_string (str, keysym_offset, buf, sizeof (buf)) > 0) |
|
|
302 | output_string (term, buf); |
|
|
303 | } |
|
|
304 | break; |
|
|
305 | |
|
|
306 | case keysym_t::RANGE_META8: |
|
|
307 | { |
|
|
308 | int len; |
|
|
309 | char buf[STRING_MAX]; |
|
|
310 | |
|
|
311 | len = format_keyrange_string (str, keysym_offset, buf, sizeof (buf)); |
|
|
312 | if (len > 0) |
|
|
313 | output_string_meta8 (term, state, buf, len); |
|
|
314 | } |
|
|
315 | break; |
|
|
316 | |
|
|
317 | case keysym_t::LIST: |
|
|
318 | { |
|
|
319 | char buf[STRING_MAX]; |
|
|
320 | |
|
|
321 | char *prefix, *middle, *suffix; |
|
|
322 | |
|
|
323 | prefix = str; |
|
|
324 | middle = strchr (prefix + 1, *prefix); |
|
|
325 | suffix = strrchr (middle + 1, *prefix); |
|
|
326 | |
|
|
327 | memcpy (buf, prefix + 1, middle - prefix - 1); |
|
|
328 | buf [middle - prefix - 1] = middle [keysym_offset + 1]; |
|
|
329 | strcpy (buf + (middle - prefix), suffix + 1); |
|
|
330 | |
260 | output_string (term, buf); |
331 | output_string (term, buf); |
|
|
332 | } |
|
|
333 | break; |
261 | } |
334 | } |
262 | break; |
|
|
263 | |
335 | |
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); |
336 | free (str); |
295 | |
337 | |
296 | return true; |
338 | return true; |
297 | } |
339 | } |
298 | else |
|
|
299 | { |
340 | } |
300 | // fprintf(stderr,"[%x:%x]",state,keysym); |
341 | |
301 | return false; |
342 | return false; |
302 | } |
|
|
303 | } |
343 | } |
304 | |
344 | |
305 | // purge duplicate keymap entries |
345 | // purge duplicate keymap entries |
306 | void keyboard_manager::purge_duplicate_keymap () |
346 | void keyboard_manager::purge_duplicate_keymap () |
307 | { |
347 | { |
… | |
… | |
317 | if (i < keymap.size ()) |
357 | if (i < keymap.size ()) |
318 | { |
358 | { |
319 | keymap[i] = keymap.back (); |
359 | keymap[i] = keymap.back (); |
320 | keymap.pop_back (); |
360 | keymap.pop_back (); |
321 | } |
361 | } |
|
|
362 | |
322 | break; |
363 | break; |
323 | } |
364 | } |
324 | } |
365 | } |
325 | } |
366 | } |
326 | } |
367 | } |
… | |
… | |
334 | uint16_t hash_budget_counter[KEYSYM_HASH_BUDGETS]; // #elements in each budget |
375 | uint16_t hash_budget_counter[KEYSYM_HASH_BUDGETS]; // #elements in each budget |
335 | |
376 | |
336 | memset (hash_budget_size, 0, sizeof (hash_budget_size)); |
377 | memset (hash_budget_size, 0, sizeof (hash_budget_size)); |
337 | memset (hash_budget_counter, 0, sizeof (hash_budget_counter)); |
378 | memset (hash_budget_counter, 0, sizeof (hash_budget_counter)); |
338 | |
379 | |
339 | // count keysyms for corresponding hash budgets |
380 | // determine hash bucket size |
340 | for (i = 0; i < keymap.size (); ++i) |
381 | for (i = 0; i < keymap.size (); ++i) |
|
|
382 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j) |
341 | { |
383 | { |
342 | hashkey = keymap [i]->keysym & KEYSYM_HASH_MASK; |
384 | hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; |
343 | ++hash_budget_size [hashkey]; |
385 | ++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 | } |
386 | } |
359 | } |
|
|
360 | |
387 | |
361 | // now we know the size of each budget |
388 | // now we know the size of each budget |
362 | // compute the index of each budget |
389 | // compute the index of each budget |
363 | hash [0] = 0; |
390 | hash [0] = 0; |
364 | for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i) |
391 | for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i) |
365 | { |
392 | { |
366 | index += hash_budget_size [i - 1]; |
393 | index += hash_budget_size [i - 1]; |
367 | hash[i] = (hash_budget_size [i] ? index : hash [i - 1]); |
394 | hash [i] = index; |
368 | } |
395 | } |
369 | |
396 | |
370 | // and allocate just enough space |
397 | // and allocate just enough space |
371 | sorted_keymap.insert (sorted_keymap.begin (), index + hash_budget_size [i - 1], 0); |
398 | sorted_keymap.insert (sorted_keymap.begin (), index + hash_budget_size [i - 1], 0); |
372 | |
399 | |
373 | // fill in sorted_keymap |
400 | // fill in sorted_keymap |
374 | // it is sorted in each budget |
401 | // it is sorted in each budget |
375 | for (i = 0; i < keymap.size (); ++i) |
402 | for (i = 0; i < keymap.size (); ++i) |
376 | { |
|
|
377 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j) |
403 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j) |
378 | { |
404 | { |
379 | hashkey = ((keymap [i]->keysym + j) & KEYSYM_HASH_MASK); |
405 | hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; |
380 | |
406 | |
381 | if (hash_budget_size [hashkey]) |
|
|
382 | { |
|
|
383 | index = hash [hashkey] + hash_budget_counter [hashkey]; |
407 | index = hash [hashkey] + hash_budget_counter [hashkey]; |
384 | |
408 | |
385 | while (index > hash [hashkey] |
409 | while (index > hash [hashkey] |
386 | && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) |
410 | && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) |
387 | { |
411 | { |
388 | sorted_keymap [index] = sorted_keymap [index - 1]; |
412 | sorted_keymap [index] = sorted_keymap [index - 1]; |
389 | --index; |
413 | --index; |
390 | } |
414 | } |
391 | |
415 | |
392 | sorted_keymap [index] = keymap [i]; |
416 | sorted_keymap [index] = keymap [i]; |
393 | ++hash_budget_counter [hashkey]; |
417 | ++hash_budget_counter [hashkey]; |
394 | } |
|
|
395 | } |
418 | } |
396 | } |
|
|
397 | |
419 | |
398 | keymap.swap (sorted_keymap); |
420 | keymap.swap (sorted_keymap); |
399 | |
421 | |
400 | #if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD) |
422 | #if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD) |
401 | // check for invariants |
423 | // check for invariants |
… | |
… | |
433 | int |
455 | int |
434 | keyboard_manager::find_keysym (KeySym keysym, unsigned int state) |
456 | keyboard_manager::find_keysym (KeySym keysym, unsigned int state) |
435 | { |
457 | { |
436 | int hashkey = keysym & KEYSYM_HASH_MASK; |
458 | int hashkey = keysym & KEYSYM_HASH_MASK; |
437 | unsigned int index = hash [hashkey]; |
459 | unsigned int index = hash [hashkey]; |
|
|
460 | unsigned int end = hashkey < KEYSYM_HASH_BUDGETS - 1 |
|
|
461 | ? hash [hashkey + 1] |
|
|
462 | : keymap.size (); |
438 | |
463 | |
439 | for (; index < keymap.size (); ++index) |
464 | for (; index < end; ++index) |
440 | { |
465 | { |
441 | keysym_t *key = keymap [index]; |
466 | keysym_t *key = keymap [index]; |
442 | |
467 | |
443 | if (key->keysym <= keysym && key->keysym + key->range > keysym |
468 | if (key->keysym <= keysym && keysym < key->keysym + key->range |
444 | // match only the specified bits in state and ignore others |
469 | // match only the specified bits in state and ignore others |
445 | && (key->state & state) == key->state) |
470 | && (key->state & state) == key->state) |
446 | return index; |
471 | return index; |
447 | else if ((key->keysym & KEYSYM_HASH_MASK) > hashkey && key->range == 1) |
|
|
448 | return -1; |
|
|
449 | } |
472 | } |
450 | |
473 | |
451 | return -1; |
474 | return -1; |
452 | } |
475 | } |
453 | |
476 | |