… | |
… | |
49 | * index: 0 I1 I2 I3 In |
49 | * index: 0 I1 I2 I3 In |
50 | * value: 0...0, A1...A1, A2...A2, A3...A3, ..., An...An |
50 | * value: 0...0, A1...A1, A2...A2, A3...A3, ..., An...An |
51 | * where |
51 | * where |
52 | * A1 = 0; |
52 | * A1 = 0; |
53 | * Ai+1 = N1 + N2 + ... + Ni. |
53 | * Ai+1 = N1 + N2 + ... + Ni. |
54 | * it is computed from hash_budget_size[]: |
54 | * it is computed from hash_bucket_size[]: |
55 | * index: 0 I1 I2 I3 In |
55 | * index: 0 I1 I2 I3 In |
56 | * value: 0...0, N1, 0...0, N2, 0...0, N3, ..., Nn, 0...0 |
56 | * value: 0...0, N1, 0...0, N2, 0...0, N3, ..., Nn, 0...0 |
57 | * 0...0, 0.......0, N1.....N1, N1+N2...N1+N2, ... (the compution of hash[]) |
57 | * 0...0, 0.......0, N1.....N1, N1+N2...N1+N2, ... (the computation of hash[]) |
58 | * or we can say |
58 | * or we can say |
59 | * hash_budget_size[Ii] = Ni; hash_budget_size[elsewhere] = 0, |
59 | * hash_bucket_size[Ii] = Ni; hash_bucket_size[elsewhere] = 0, |
60 | * where |
60 | * where |
61 | * set {I1, I2, ..., In} = { hashkey of keymap[0]->keysym, ..., keymap[keymap.size-1]->keysym } |
61 | * set {I1, I2, ..., In} = { hashkey of keymap[0]->keysym, ..., keymap[keymap.size-1]->keysym } |
62 | * where hashkey of keymap[i]->keysym = keymap[i]->keysym & KEYSYM_HASH_MASK |
62 | * where hashkey of keymap[i]->keysym = keymap[i]->keysym & KEYSYM_HASH_MASK |
63 | * n(the number of groups) = the number of non-zero member of hash_budget_size[]; |
63 | * n(the number of groups) = the number of non-zero member of hash_bucket_size[]; |
64 | * Ni(the size of group i) = hash_budget_size[Ii]. |
64 | * Ni(the size of group i) = hash_bucket_size[Ii]. |
65 | */ |
65 | */ |
66 | |
66 | |
67 | #if STOCK_KEYMAP |
67 | #if STOCK_KEYMAP |
68 | //////////////////////////////////////////////////////////////////////////////// |
68 | //////////////////////////////////////////////////////////////////////////////// |
69 | // default keycode translation map and keyevent handlers |
69 | // default keycode translation map and keyevent handlers |
… | |
… | |
143 | // return: priority_of_a - priority_of_b |
143 | // return: priority_of_a - priority_of_b |
144 | static int |
144 | static int |
145 | compare_priority (keysym_t *a, keysym_t *b) |
145 | compare_priority (keysym_t *a, keysym_t *b) |
146 | { |
146 | { |
147 | // (the more '1's in state; the less range): the greater priority |
147 | // (the more '1's in state; the less range): the greater priority |
148 | int ca = popcount (a->state /* & OtherModMask */); |
148 | int ca = rxvt_popcount (a->state /* & OtherModMask */); |
149 | int cb = popcount (b->state /* & OtherModMask */); |
149 | int cb = rxvt_popcount (b->state /* & OtherModMask */); |
150 | |
150 | |
151 | if (ca != cb) |
151 | if (ca != cb) |
152 | return ca - cb; |
152 | return ca - cb; |
153 | //else if (a->state != b->state) // this behavior is to be disscussed |
153 | //else if (a->state != b->state) // this behavior is to be discussed |
154 | // return b->state - a->state; |
154 | // return b->state - a->state; |
155 | else |
155 | else |
156 | return b->range - a->range; |
156 | return b->range - a->range; |
157 | } |
157 | } |
158 | |
158 | |
… | |
… | |
378 | void |
378 | void |
379 | keyboard_manager::setup_hash () |
379 | keyboard_manager::setup_hash () |
380 | { |
380 | { |
381 | unsigned int i, index, hashkey; |
381 | unsigned int i, index, hashkey; |
382 | vector <keysym_t *> sorted_keymap; |
382 | vector <keysym_t *> sorted_keymap; |
383 | uint16_t hash_budget_size[KEYSYM_HASH_BUDGETS]; // size of each budget |
383 | uint16_t hash_bucket_size[KEYSYM_HASH_BUCKETS]; // size of each bucket |
384 | uint16_t hash_budget_counter[KEYSYM_HASH_BUDGETS]; // #elements in each budget |
384 | uint16_t hash_bucket_counter[KEYSYM_HASH_BUCKETS]; // #elements in each bucket |
385 | |
385 | |
386 | memset (hash_budget_size, 0, sizeof (hash_budget_size)); |
386 | memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); |
387 | memset (hash_budget_counter, 0, sizeof (hash_budget_counter)); |
387 | memset (hash_bucket_counter, 0, sizeof (hash_bucket_counter)); |
388 | |
388 | |
389 | // determine hash bucket size |
389 | // determine hash bucket size |
390 | for (i = 0; i < keymap.size (); ++i) |
390 | for (i = 0; i < keymap.size (); ++i) |
391 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j) |
391 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUCKETS) - 1; j >= 0; --j) |
392 | { |
392 | { |
393 | hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; |
393 | hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; |
394 | ++hash_budget_size [hashkey]; |
394 | ++hash_bucket_size [hashkey]; |
395 | } |
395 | } |
396 | |
396 | |
397 | // now we know the size of each budget |
397 | // now we know the size of each bucket |
398 | // compute the index of each budget |
398 | // compute the index of each bucket |
399 | hash [0] = 0; |
399 | hash [0] = 0; |
400 | for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i) |
400 | for (index = 0, i = 1; i < KEYSYM_HASH_BUCKETS; ++i) |
401 | { |
401 | { |
402 | index += hash_budget_size [i - 1]; |
402 | index += hash_bucket_size [i - 1]; |
403 | hash [i] = index; |
403 | hash [i] = index; |
404 | } |
404 | } |
405 | |
405 | |
406 | // and allocate just enough space |
406 | // and allocate just enough space |
407 | sorted_keymap.insert (sorted_keymap.begin (), index + hash_budget_size [i - 1], 0); |
407 | sorted_keymap.insert (sorted_keymap.begin (), index + hash_bucket_size [i - 1], 0); |
408 | |
408 | |
409 | // fill in sorted_keymap |
409 | // fill in sorted_keymap |
410 | // it is sorted in each budget |
410 | // it is sorted in each bucket |
411 | for (i = 0; i < keymap.size (); ++i) |
411 | for (i = 0; i < keymap.size (); ++i) |
412 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j) |
412 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUCKETS) - 1; j >= 0; --j) |
413 | { |
413 | { |
414 | hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; |
414 | hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; |
415 | |
415 | |
416 | index = hash [hashkey] + hash_budget_counter [hashkey]; |
416 | index = hash [hashkey] + hash_bucket_counter [hashkey]; |
417 | |
417 | |
418 | while (index > hash [hashkey] |
418 | while (index > hash [hashkey] |
419 | && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) |
419 | && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) |
420 | { |
420 | { |
421 | sorted_keymap [index] = sorted_keymap [index - 1]; |
421 | sorted_keymap [index] = sorted_keymap [index - 1]; |
422 | --index; |
422 | --index; |
423 | } |
423 | } |
424 | |
424 | |
425 | sorted_keymap [index] = keymap [i]; |
425 | sorted_keymap [index] = keymap [i]; |
426 | ++hash_budget_counter [hashkey]; |
426 | ++hash_bucket_counter [hashkey]; |
427 | } |
427 | } |
428 | |
428 | |
429 | keymap.swap (sorted_keymap); |
429 | keymap.swap (sorted_keymap); |
430 | |
430 | |
431 | #ifdef DEBUG_STRICT |
431 | #ifndef NDEBUG |
432 | // check for invariants |
432 | // check for invariants |
433 | for (i = 0; i < KEYSYM_HASH_BUDGETS; ++i) |
433 | for (i = 0; i < KEYSYM_HASH_BUCKETS; ++i) |
434 | { |
434 | { |
435 | index = hash[i]; |
435 | index = hash[i]; |
436 | for (int j = 0; j < hash_budget_size [i]; ++j) |
436 | for (int j = 0; j < hash_bucket_size [i]; ++j) |
437 | { |
437 | { |
438 | if (keymap [index + j]->range == 1) |
438 | if (keymap [index + j]->range == 1) |
439 | assert (i == (keymap [index + j]->keysym & KEYSYM_HASH_MASK)); |
439 | assert (i == (keymap [index + j]->keysym & KEYSYM_HASH_MASK)); |
440 | |
440 | |
441 | if (j) |
441 | if (j) |
… | |
… | |
452 | { |
452 | { |
453 | int index = find_keysym (a->keysym + j, a->state); |
453 | int index = find_keysym (a->keysym + j, a->state); |
454 | |
454 | |
455 | assert (index >= 0); |
455 | assert (index >= 0); |
456 | keysym_t *b = keymap [index]; |
456 | keysym_t *b = keymap [index]; |
457 | assert (i == (signed) index || // the normally expected result |
457 | assert (i == index // the normally expected result |
458 | (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 |
458 | || IN_RANGE_INC (a->keysym + j, b->keysym, b->keysym + b->range) |
|
|
459 | && compare_priority (a, b) <= 0); // is effectively the same or a closer match |
459 | } |
460 | } |
460 | } |
461 | } |
461 | #endif |
462 | #endif |
462 | } |
463 | } |
463 | |
464 | |
464 | int |
465 | int |
465 | keyboard_manager::find_keysym (KeySym keysym, unsigned int state) |
466 | keyboard_manager::find_keysym (KeySym keysym, unsigned int state) |
466 | { |
467 | { |
467 | int hashkey = keysym & KEYSYM_HASH_MASK; |
468 | int hashkey = keysym & KEYSYM_HASH_MASK; |
468 | unsigned int index = hash [hashkey]; |
469 | unsigned int index = hash [hashkey]; |
469 | unsigned int end = hashkey < KEYSYM_HASH_BUDGETS - 1 |
470 | unsigned int end = hashkey < KEYSYM_HASH_BUCKETS - 1 |
470 | ? hash [hashkey + 1] |
471 | ? hash [hashkey + 1] |
471 | : keymap.size (); |
472 | : keymap.size (); |
472 | |
473 | |
473 | for (; index < end; ++index) |
474 | for (; index < end; ++index) |
474 | { |
475 | { |