… | |
… | |
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 |
70 | |
70 | |
71 | keysym_t keyboard_manager::stock_keymap[] = { |
71 | keysym_t keyboard_manager::stock_keymap[] = { |
72 | /* examples */ |
72 | /* examples */ |
73 | /* keysym, state, range, handler, str */ |
73 | /* keysym, state, range, type, str */ |
74 | //{XK_ISO_Left_Tab, 0, 1, keysym_t::STRING, "\033[Z"}, |
74 | //{XK_ISO_Left_Tab, 0, 1, keysym_t::STRING, "\033[Z"}, |
75 | //{ 'a', 0, 26, keysym_t::RANGE_META8, "a" "%c"}, |
75 | //{ 'a', 0, 26, keysym_t::RANGE_META8, "a" "%c"}, |
76 | //{ 'a', ControlMask, 26, keysym_t::RANGE_META8, "" "%c"}, |
76 | //{ 'a', ControlMask, 26, keysym_t::RANGE_META8, "" "%c"}, |
77 | //{ XK_Left, 0, 4, keysym_t::LIST, ".\033[.DACB."}, |
77 | //{ XK_Left, 0, 4, keysym_t::LIST, ".\033[.DACB."}, |
78 | //{ XK_Left, ShiftMask, 4, keysym_t::LIST, ".\033[.dacb."}, |
78 | //{ XK_Left, ShiftMask, 4, keysym_t::LIST, ".\033[.dacb."}, |
… | |
… | |
138 | } |
138 | } |
139 | |
139 | |
140 | return len; |
140 | return len; |
141 | } |
141 | } |
142 | |
142 | |
143 | //////////////////////////////////////////////////////////////////////////////// |
|
|
144 | // return: #bits of '1' |
|
|
145 | #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3) |
|
|
146 | # define bitcount(n) (__extension__ ({ uint32_t n__ = (n); __builtin_popcount (n__); })) |
|
|
147 | #else |
|
|
148 | static int |
|
|
149 | bitcount (uint16_t n) |
|
|
150 | { |
|
|
151 | int i; |
|
|
152 | |
|
|
153 | for (i = 0; n; ++i, n &= n - 1) |
|
|
154 | ; |
|
|
155 | |
|
|
156 | return i; |
|
|
157 | } |
|
|
158 | #endif |
|
|
159 | |
|
|
160 | // return: priority_of_a - priority_of_b |
143 | // return: priority_of_a - priority_of_b |
161 | static int |
144 | static int |
162 | compare_priority (keysym_t *a, keysym_t *b) |
145 | compare_priority (keysym_t *a, keysym_t *b) |
163 | { |
146 | { |
164 | // (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 |
165 | int ca = bitcount (a->state /* & OtherModMask */); |
148 | int ca = rxvt_popcount (a->state /* & OtherModMask */); |
166 | int cb = bitcount (b->state /* & OtherModMask */); |
149 | int cb = rxvt_popcount (b->state /* & OtherModMask */); |
167 | |
150 | |
168 | if (ca != cb) |
151 | if (ca != cb) |
169 | return ca - cb; |
152 | return ca - cb; |
170 | //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 |
171 | // return b->state - a->state; |
154 | // return b->state - a->state; |
172 | else |
155 | else |
173 | return b->range - a->range; |
156 | return b->range - a->range; |
174 | } |
157 | } |
175 | |
158 | |
… | |
… | |
188 | void |
171 | void |
189 | keyboard_manager::clear () |
172 | keyboard_manager::clear () |
190 | { |
173 | { |
191 | keymap.clear (); |
174 | keymap.clear (); |
192 | hash [0] = 2; |
175 | hash [0] = 2; |
193 | |
|
|
194 | for (unsigned int i = 0; i < user_translations.size (); ++i) |
|
|
195 | { |
|
|
196 | free ((void *)user_translations [i]); |
|
|
197 | user_translations [i] = 0; |
|
|
198 | } |
|
|
199 | |
|
|
200 | for (unsigned int i = 0; i < user_keymap.size (); ++i) |
|
|
201 | { |
|
|
202 | delete user_keymap [i]; |
|
|
203 | user_keymap [i] = 0; |
|
|
204 | } |
|
|
205 | |
|
|
206 | user_keymap.clear (); |
|
|
207 | user_translations.clear (); |
|
|
208 | } |
176 | } |
209 | |
177 | |
210 | // a wrapper for register_keymap, |
178 | // a wrapper for register_keymap, |
211 | // so that outside codes don't have to know so much details. |
179 | // so that outside codes don't have to know so much details. |
212 | // |
180 | // |
… | |
… | |
230 | |
198 | |
231 | if (strncmp (translation, "list", 4) == 0 && translation [4]) |
199 | if (strncmp (translation, "list", 4) == 0 && translation [4]) |
232 | { |
200 | { |
233 | char *middle = strchr (translation + 5, translation [4]); |
201 | char *middle = strchr (translation + 5, translation [4]); |
234 | char *suffix = strrchr (translation + 5, translation [4]); |
202 | char *suffix = strrchr (translation + 5, translation [4]); |
235 | |
203 | |
236 | if (suffix && middle && suffix > middle + 1) |
204 | if (suffix && middle && suffix > middle + 1) |
237 | { |
205 | { |
238 | key->type = keysym_t::LIST; |
206 | key->type = keysym_t::LIST; |
239 | key->range = suffix - middle - 1; |
207 | key->range = suffix - middle - 1; |
240 | |
208 | |
241 | strcpy (translation, translation + 4); |
209 | memmove (translation, translation + 4, strlen (translation + 4) + 1); |
242 | } |
210 | } |
243 | else |
211 | else |
244 | rxvt_warn ("cannot parse list-type keysym '%s', treating as normal keysym.\n", translation); |
212 | rxvt_warn ("cannot parse list-type keysym '%s', treating as normal keysym.\n", translation); |
245 | } |
213 | } |
246 | else if (strncmp (translation, "builtin:", 8) == 0) |
214 | else if (strncmp (translation, "builtin:", 8) == 0) |
247 | key->type = keysym_t::BUILTIN; |
215 | key->type = keysym_t::BUILTIN; |
248 | |
216 | |
249 | user_keymap.push_back (key); |
|
|
250 | user_translations.push_back (translation); |
|
|
251 | register_keymap (key); |
217 | register_keymap (key); |
252 | } |
218 | } |
253 | else |
219 | else |
254 | { |
220 | { |
255 | delete key; |
221 | delete key; |
… | |
… | |
395 | void |
361 | void |
396 | keyboard_manager::setup_hash () |
362 | keyboard_manager::setup_hash () |
397 | { |
363 | { |
398 | unsigned int i, index, hashkey; |
364 | unsigned int i, index, hashkey; |
399 | vector <keysym_t *> sorted_keymap; |
365 | vector <keysym_t *> sorted_keymap; |
400 | uint16_t hash_budget_size[KEYSYM_HASH_BUDGETS]; // size of each budget |
366 | uint16_t hash_bucket_size[KEYSYM_HASH_BUCKETS]; // size of each bucket |
401 | uint16_t hash_budget_counter[KEYSYM_HASH_BUDGETS]; // #elements in each budget |
367 | uint16_t hash_bucket_counter[KEYSYM_HASH_BUCKETS]; // #elements in each bucket |
402 | |
368 | |
403 | memset (hash_budget_size, 0, sizeof (hash_budget_size)); |
369 | memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); |
404 | memset (hash_budget_counter, 0, sizeof (hash_budget_counter)); |
370 | memset (hash_bucket_counter, 0, sizeof (hash_bucket_counter)); |
405 | |
371 | |
406 | // determine hash bucket size |
372 | // determine hash bucket size |
407 | for (i = 0; i < keymap.size (); ++i) |
373 | for (i = 0; i < keymap.size (); ++i) |
408 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j) |
374 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUCKETS) - 1; j >= 0; --j) |
409 | { |
375 | { |
410 | hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; |
376 | hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; |
411 | ++hash_budget_size [hashkey]; |
377 | ++hash_bucket_size [hashkey]; |
412 | } |
378 | } |
413 | |
379 | |
414 | // now we know the size of each budget |
380 | // now we know the size of each bucket |
415 | // compute the index of each budget |
381 | // compute the index of each bucket |
416 | hash [0] = 0; |
382 | hash [0] = 0; |
417 | for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i) |
383 | for (index = 0, i = 1; i < KEYSYM_HASH_BUCKETS; ++i) |
418 | { |
384 | { |
419 | index += hash_budget_size [i - 1]; |
385 | index += hash_bucket_size [i - 1]; |
420 | hash [i] = index; |
386 | hash [i] = index; |
421 | } |
387 | } |
422 | |
388 | |
423 | // and allocate just enough space |
389 | // and allocate just enough space |
424 | sorted_keymap.insert (sorted_keymap.begin (), index + hash_budget_size [i - 1], 0); |
390 | sorted_keymap.insert (sorted_keymap.begin (), index + hash_bucket_size [i - 1], 0); |
425 | |
391 | |
426 | // fill in sorted_keymap |
392 | // fill in sorted_keymap |
427 | // it is sorted in each budget |
393 | // it is sorted in each bucket |
428 | for (i = 0; i < keymap.size (); ++i) |
394 | for (i = 0; i < keymap.size (); ++i) |
429 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j) |
395 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUCKETS) - 1; j >= 0; --j) |
430 | { |
396 | { |
431 | hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; |
397 | hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; |
432 | |
398 | |
433 | index = hash [hashkey] + hash_budget_counter [hashkey]; |
399 | index = hash [hashkey] + hash_bucket_counter [hashkey]; |
434 | |
400 | |
435 | while (index > hash [hashkey] |
401 | while (index > hash [hashkey] |
436 | && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) |
402 | && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) |
437 | { |
403 | { |
438 | sorted_keymap [index] = sorted_keymap [index - 1]; |
404 | sorted_keymap [index] = sorted_keymap [index - 1]; |
439 | --index; |
405 | --index; |
440 | } |
406 | } |
441 | |
407 | |
442 | sorted_keymap [index] = keymap [i]; |
408 | sorted_keymap [index] = keymap [i]; |
443 | ++hash_budget_counter [hashkey]; |
409 | ++hash_bucket_counter [hashkey]; |
444 | } |
410 | } |
445 | |
411 | |
446 | keymap.swap (sorted_keymap); |
412 | keymap.swap (sorted_keymap); |
447 | |
413 | |
448 | #ifdef DEBUG_STRICT |
414 | #ifndef NDEBUG |
449 | // check for invariants |
415 | // check for invariants |
450 | for (i = 0; i < KEYSYM_HASH_BUDGETS; ++i) |
416 | for (i = 0; i < KEYSYM_HASH_BUCKETS; ++i) |
451 | { |
417 | { |
452 | index = hash[i]; |
418 | index = hash[i]; |
453 | for (int j = 0; j < hash_budget_size [i]; ++j) |
419 | for (int j = 0; j < hash_bucket_size [i]; ++j) |
454 | { |
420 | { |
455 | if (keymap [index + j]->range == 1) |
421 | if (keymap [index + j]->range == 1) |
456 | assert (i == (keymap [index + j]->keysym & KEYSYM_HASH_MASK)); |
422 | assert (i == (keymap [index + j]->keysym & KEYSYM_HASH_MASK)); |
457 | |
423 | |
458 | if (j) |
424 | if (j) |
… | |
… | |
469 | { |
435 | { |
470 | int index = find_keysym (a->keysym + j, a->state); |
436 | int index = find_keysym (a->keysym + j, a->state); |
471 | |
437 | |
472 | assert (index >= 0); |
438 | assert (index >= 0); |
473 | keysym_t *b = keymap [index]; |
439 | keysym_t *b = keymap [index]; |
474 | assert (i == (signed) index || // the normally expected result |
440 | assert (i == index // the normally expected result |
475 | (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 |
441 | || IN_RANGE_INC (a->keysym + j, b->keysym, b->keysym + b->range) |
|
|
442 | && compare_priority (a, b) <= 0); // is effectively the same or a closer match |
476 | } |
443 | } |
477 | } |
444 | } |
478 | #endif |
445 | #endif |
479 | } |
446 | } |
480 | |
447 | |
481 | int |
448 | int |
482 | keyboard_manager::find_keysym (KeySym keysym, unsigned int state) |
449 | keyboard_manager::find_keysym (KeySym keysym, unsigned int state) |
483 | { |
450 | { |
484 | int hashkey = keysym & KEYSYM_HASH_MASK; |
451 | int hashkey = keysym & KEYSYM_HASH_MASK; |
485 | unsigned int index = hash [hashkey]; |
452 | unsigned int index = hash [hashkey]; |
486 | unsigned int end = hashkey < KEYSYM_HASH_BUDGETS - 1 |
453 | unsigned int end = hashkey < KEYSYM_HASH_BUCKETS - 1 |
487 | ? hash [hashkey + 1] |
454 | ? hash [hashkey + 1] |
488 | : keymap.size (); |
455 | : keymap.size (); |
489 | |
456 | |
490 | for (; index < end; ++index) |
457 | for (; index < end; ++index) |
491 | { |
458 | { |
492 | keysym_t *key = keymap [index]; |
459 | keysym_t *key = keymap [index]; |