1 | #include "../config.h" |
1 | #include "../config.h" |
2 | #include "rxvt.h" |
2 | #include "rxvt.h" |
|
|
3 | |
|
|
4 | #ifdef KEYSYM_RESOURCE |
|
|
5 | |
|
|
6 | #include <cstring> |
|
|
7 | |
3 | #include "keyboard.h" |
8 | #include "keyboard.h" |
4 | #include "command.h" |
9 | #include "command.h" |
5 | #include <string.h> |
|
|
6 | #include <X11/X.h> |
|
|
7 | |
10 | |
8 | #ifdef KEYSYM_RESOURCE |
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 | */ |
9 | |
42 | |
|
|
43 | #if STOCK_KEYMAP |
10 | //////////////////////////////////////////////////////////////////////////////// |
44 | //////////////////////////////////////////////////////////////////////////////// |
11 | // default keycode translation map and keyevent handlers |
45 | // default keycode translation map and keyevent handlers |
12 | |
46 | |
13 | keysym_t keyboard_manager::stock_keymap[] = { |
47 | keysym_t keyboard_manager::stock_keymap[] = { |
14 | /* examples */ |
48 | /* examples */ |
15 | /* keysym, state, range, handler, str */ |
49 | /* keysym, state, range, handler, str */ |
16 | //{XK_ISO_Left_Tab, 0, 1, NORMAL, "\033[Z"}, |
50 | //{XK_ISO_Left_Tab, 0, 1, keysym_t::NORMAL, "\033[Z"}, |
17 | //{ 'a', 0, 26, RANGE_META8, "a" "%c"}, |
51 | //{ 'a', 0, 26, keysym_t::RANGE_META8, "a" "%c"}, |
18 | //{ 'a', ControlMask, 26, RANGE_META8, "" "%c"}, |
52 | //{ 'a', ControlMask, 26, keysym_t::RANGE_META8, "" "%c"}, |
19 | //{ XK_Left, 0, 4, LIST, "DACBZ" "\033[Z"}, |
53 | //{ XK_Left, 0, 4, keysym_t::LIST, ".\033[.DACB."}, |
20 | //{ XK_Left, ShiftMask, 4, LIST, "dacbZ" "\033[Z"}, |
54 | //{ XK_Left, ShiftMask, 4, keysym_t::LIST, ".\033[.dacb."}, |
21 | //{ XK_Left, ControlMask, 4, LIST, "dacbZ" "\033OZ"}, |
55 | //{ XK_Left, ControlMask, 4, keysym_t::LIST, ".\033O.dacb."}, |
22 | //{ XK_Tab, ControlMask, 1, NORMAL, "\033<C-Tab>"}, |
56 | //{ XK_Tab, ControlMask, 1, keysym_t::NORMAL, "\033<C-Tab>"}, |
23 | //{ XK_apostrophe, ControlMask, 1, NORMAL, "\033<C-'>"}, |
57 | //{ XK_apostrophe, ControlMask, 1, keysym_t::NORMAL, "\033<C-'>"}, |
24 | //{ XK_slash, ControlMask, 1, NORMAL, "\033<C-/>"}, |
58 | //{ XK_slash, ControlMask, 1, keysym_t::NORMAL, "\033<C-/>"}, |
25 | //{ XK_semicolon, ControlMask, 1, NORMAL, "\033<C-;>"}, |
59 | //{ XK_semicolon, ControlMask, 1, keysym_t::NORMAL, "\033<C-;>"}, |
26 | //{ XK_grave, ControlMask, 1, NORMAL, "\033<C-`>"}, |
60 | //{ XK_grave, ControlMask, 1, keysym_t::NORMAL, "\033<C-`>"}, |
27 | //{ XK_comma, ControlMask, 1, NORMAL, "\033<C-\054>"}, |
61 | //{ XK_comma, ControlMask, 1, keysym_t::NORMAL, "\033<C-\054>"}, |
28 | //{ XK_Return, ControlMask, 1, NORMAL, "\033<C-Return>"}, |
62 | //{ XK_Return, ControlMask, 1, keysym_t::NORMAL, "\033<C-Return>"}, |
29 | //{ XK_Return, ShiftMask, 1, NORMAL, "\033<S-Return>"}, |
63 | //{ XK_Return, ShiftMask, 1, keysym_t::NORMAL, "\033<S-Return>"}, |
30 | //{ ' ', ShiftMask, 1, NORMAL, "\033<S-Space>"}, |
64 | //{ ' ', ShiftMask, 1, keysym_t::NORMAL, "\033<S-Space>"}, |
31 | //{ '.', ControlMask, 1, NORMAL, "\033<C-.>"}, |
65 | //{ '.', ControlMask, 1, keysym_t::NORMAL, "\033<C-.>"}, |
32 | //{ '0', ControlMask, 10, RANGE, "0" "\033<C-%c>"}, |
66 | //{ '0', ControlMask, 10, keysym_t::RANGE, "0" "\033<C-%c>"}, |
33 | //{ '0', MetaMask|ControlMask, 10, RANGE, "0" "\033<M-C-%c>"}, |
67 | //{ '0', MetaMask|ControlMask, 10, keysym_t::RANGE, "0" "\033<M-C-%c>"}, |
34 | //{ 'a', MetaMask|ControlMask, 26, RANGE, "a" "\033<M-C-%c>"}, |
68 | //{ 'a', MetaMask|ControlMask, 26, keysym_t::RANGE, "a" "\033<M-C-%c>"}, |
35 | }; |
69 | }; |
|
|
70 | #endif |
36 | |
71 | |
37 | static void |
72 | static void |
38 | output_string (rxvt_term *rt, const char *str) |
73 | output_string (rxvt_term *rt, const char *str) |
39 | { |
74 | { |
40 | assert (rt && str); |
|
|
41 | |
|
|
42 | if (strncmp (str, "proto:", 6) == 0) |
75 | if (strncmp (str, "command:", 8) == 0) |
43 | rt->cmd_write ((unsigned char *)str + 6, strlen (str) - 6); |
76 | rt->cmd_write ((unsigned char *)str + 8, strlen (str) - 8); |
44 | else |
77 | else |
45 | rt->tt_write ((unsigned char *)str, strlen (str)); |
78 | rt->tt_write ((unsigned char *)str, strlen (str)); |
46 | } |
79 | } |
47 | |
80 | |
48 | static void |
81 | static void |
… | |
… | |
68 | } |
101 | } |
69 | |
102 | |
70 | static int |
103 | static int |
71 | format_keyrange_string (const char *str, int keysym_offset, char *buf, int bufsize) |
104 | format_keyrange_string (const char *str, int keysym_offset, char *buf, int bufsize) |
72 | { |
105 | { |
73 | int len = snprintf (buf, bufsize, str + 1, keysym_offset + str [0]); |
106 | size_t len = snprintf (buf, bufsize, str + 1, keysym_offset + str [0]); |
74 | |
107 | |
75 | if (len >= bufsize) |
108 | if (len >= (size_t)bufsize) |
76 | { |
|
|
77 | rxvt_warn ("buffer overflowed!\n"); |
|
|
78 | *buf = 0; |
|
|
79 | } |
109 | { |
80 | else if (len < 0) |
110 | rxvt_warn ("format_keyrange_string: formatting failed, ignoring key.\n"); |
81 | { |
|
|
82 | rxvt_warn ("keyrange_translator(), snprintf error"); |
|
|
83 | *buf = 0; |
111 | *buf = 0; |
84 | } |
112 | } |
85 | |
113 | |
86 | return len; |
114 | return len; |
87 | } |
115 | } |
88 | |
116 | |
89 | //////////////////////////////////////////////////////////////////////////////// |
117 | //////////////////////////////////////////////////////////////////////////////// |
90 | // return: #bits of '1' |
118 | // return: #bits of '1' |
|
|
119 | #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3) |
|
|
120 | # define bitcount(n) (__extension__ ({ uint32_t n__ = (n); __builtin_popcount (n); })) |
|
|
121 | #else |
91 | static int |
122 | static int |
92 | bitcount (unsigned int n) |
123 | bitcount (uint16_t n) |
93 | { |
124 | { |
94 | int i; |
125 | int i; |
95 | |
126 | |
96 | for (i = 0; n; ++i, n &= (n - 1)) |
127 | for (i = 0; n; ++i, n &= n - 1) |
97 | ; |
128 | ; |
98 | |
129 | |
99 | return i; |
130 | return i; |
100 | } |
131 | } |
|
|
132 | #endif |
101 | |
133 | |
102 | // return: priority_of_a - priority_of_b |
134 | // return: priority_of_a - priority_of_b |
103 | static int |
135 | static int |
104 | compare_priority (keysym_t *a, keysym_t *b) |
136 | compare_priority (keysym_t *a, keysym_t *b) |
105 | { |
137 | { |
106 | assert (a && b); |
|
|
107 | |
|
|
108 | // (the more '1's in state; the less range): the greater priority |
138 | // (the more '1's in state; the less range): the greater priority |
109 | int ca = bitcount (a->state /* & OtherModMask */); |
139 | int ca = bitcount (a->state /* & OtherModMask */); |
110 | int cb = bitcount (b->state /* & OtherModMask */); |
140 | int cb = bitcount (b->state /* & OtherModMask */); |
111 | |
141 | |
112 | if (ca != cb) |
142 | if (ca != cb) |
… | |
… | |
157 | // the string 'trans' is copied to an internal managed buffer, |
187 | // the string 'trans' is copied to an internal managed buffer, |
158 | // so the caller can free memory of 'trans' at any time. |
188 | // so the caller can free memory of 'trans' at any time. |
159 | void |
189 | void |
160 | keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const char *trans) |
190 | keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const char *trans) |
161 | { |
191 | { |
162 | assert (trans); |
|
|
163 | |
|
|
164 | keysym_t *key = new keysym_t; |
192 | keysym_t *key = new keysym_t; |
165 | wchar_t *wc = rxvt_mbstowcs (trans); |
193 | wchar_t *wc = rxvt_mbstowcs (trans); |
166 | const char *translation = rxvt_wcstoutf8 (wc); |
194 | const char *translation = rxvt_wcstoutf8 (wc); |
167 | free (wc); |
195 | free (wc); |
168 | |
196 | |
… | |
… | |
185 | key->range = suffix - middle - 1; |
213 | key->range = suffix - middle - 1; |
186 | |
214 | |
187 | strcpy (translation, translation + 4); |
215 | strcpy (translation, translation + 4); |
188 | } |
216 | } |
189 | else |
217 | else |
190 | { |
|
|
191 | key->range = 1; |
|
|
192 | 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); |
193 | } |
219 | } |
194 | } |
220 | else if (strncmp (translation, "builtin:", 8) == 0) |
195 | else |
221 | key->type = keysym_t::BUILTIN; |
196 | |
222 | |
197 | user_keymap.push_back (key); |
223 | user_keymap.push_back (key); |
198 | user_translations.push_back (translation); |
224 | user_translations.push_back (translation); |
199 | register_keymap (key); |
225 | register_keymap (key); |
200 | } |
226 | } |
… | |
… | |
207 | } |
233 | } |
208 | |
234 | |
209 | void |
235 | void |
210 | keyboard_manager::register_keymap (keysym_t *key) |
236 | keyboard_manager::register_keymap (keysym_t *key) |
211 | { |
237 | { |
212 | assert (key); |
|
|
213 | assert (key->range >= 1); |
|
|
214 | |
|
|
215 | if (keymap.size () == keymap.capacity ()) |
238 | if (keymap.size () == keymap.capacity ()) |
216 | keymap.reserve (keymap.size () * 2); |
239 | keymap.reserve (keymap.size () * 2); |
217 | |
240 | |
218 | keymap.push_back (key); |
241 | keymap.push_back (key); |
219 | hash[0] = 3; |
242 | hash[0] = 3; |
220 | } |
243 | } |
221 | |
244 | |
222 | void |
245 | void |
223 | keyboard_manager::register_done () |
246 | keyboard_manager::register_done () |
224 | { |
247 | { |
|
|
248 | #if STOCK_KEYMAP |
225 | unsigned int i, n = sizeof (stock_keymap) / sizeof (keysym_t); |
249 | int n = sizeof (stock_keymap) / sizeof (keysym_t); |
226 | |
250 | |
|
|
251 | //TODO: shield against repeated calls and empty keymap |
227 | if (keymap.back () != &stock_keymap[n - 1]) |
252 | //if (keymap.back () != &stock_keymap[n - 1]) |
228 | for (i = 0; i < n; ++i) |
253 | for (int i = 0; i < n; ++i) |
229 | register_keymap (&stock_keymap[i]); |
254 | register_keymap (&stock_keymap[i]); |
|
|
255 | #endif |
230 | |
256 | |
231 | purge_duplicate_keymap (); |
257 | purge_duplicate_keymap (); |
232 | |
258 | |
233 | setup_hash (); |
259 | setup_hash (); |
234 | } |
260 | } |
235 | |
261 | |
236 | bool |
262 | bool |
237 | keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state) |
263 | keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state) |
238 | { |
264 | { |
239 | 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 |
240 | |
268 | |
241 | if (state & term->ModMetaMask) state |= MetaMask; |
269 | if (state & term->ModMetaMask) state |= MetaMask; |
242 | if (state & term->ModNumLockMask) state |= NumLockMask; |
270 | if (state & term->ModNumLockMask) state |= NumLockMask; |
243 | if (state & term->ModLevel3Mask) state |= Level3Mask; |
271 | if (state & term->ModLevel3Mask) state |= Level3Mask; |
244 | |
272 | |
… | |
… | |
247 | |
275 | |
248 | int index = find_keysym (keysym, state); |
276 | int index = find_keysym (keysym, state); |
249 | |
277 | |
250 | if (index >= 0) |
278 | if (index >= 0) |
251 | { |
279 | { |
252 | assert (term && keymap [index]); |
|
|
253 | const keysym_t &key = *keymap [index]; |
280 | const keysym_t &key = *keymap [index]; |
254 | |
281 | |
|
|
282 | if (key.type != keysym_t::BUILTIN) |
|
|
283 | { |
255 | int keysym_offset = keysym - key.keysym; |
284 | int keysym_offset = keysym - key.keysym; |
256 | |
285 | |
257 | wchar_t *wc = rxvt_utf8towcs (key.str); |
286 | wchar_t *wc = rxvt_utf8towcs (key.str); |
258 | char *str = rxvt_wcstombs (wc); |
287 | char *str = rxvt_wcstombs (wc); |
259 | // TODO: do translations, unescaping etc, here (allow \u escape etc.) |
288 | // TODO: do (some) translations, unescaping etc, here (allow \u escape etc.) |
260 | free (wc); |
289 | free (wc); |
261 | |
290 | |
262 | switch (key.type) |
291 | switch (key.type) |
263 | { |
|
|
264 | case keysym_t::NORMAL: |
|
|
265 | output_string (term, str); |
|
|
266 | break; |
|
|
267 | |
|
|
268 | case keysym_t::RANGE: |
|
|
269 | { |
292 | { |
|
|
293 | case keysym_t::NORMAL: |
|
|
294 | output_string (term, str); |
|
|
295 | break; |
|
|
296 | |
|
|
297 | case keysym_t::RANGE: |
|
|
298 | { |
270 | char buf[STRING_MAX]; |
299 | char buf[STRING_MAX]; |
271 | |
300 | |
272 | 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 | |
273 | output_string (term, buf); |
331 | output_string (term, buf); |
|
|
332 | } |
|
|
333 | break; |
274 | } |
334 | } |
275 | break; |
|
|
276 | |
335 | |
277 | case keysym_t::RANGE_META8: |
|
|
278 | { |
|
|
279 | int len; |
|
|
280 | char buf[STRING_MAX]; |
|
|
281 | |
|
|
282 | len = format_keyrange_string (str, keysym_offset, buf, sizeof (buf)); |
|
|
283 | if (len > 0) |
|
|
284 | output_string_meta8 (term, state, buf, len); |
|
|
285 | } |
|
|
286 | break; |
|
|
287 | |
|
|
288 | case keysym_t::LIST: |
|
|
289 | { |
|
|
290 | char buf[STRING_MAX]; |
|
|
291 | |
|
|
292 | char *prefix, *middle, *suffix; |
|
|
293 | |
|
|
294 | prefix = str; |
|
|
295 | middle = strchr (prefix + 1, *prefix); |
|
|
296 | suffix = strrchr (middle + 1, *prefix); |
|
|
297 | |
|
|
298 | memcpy (buf, prefix + 1, middle - prefix - 1); |
|
|
299 | buf [middle - prefix - 1] = middle [keysym_offset + 1]; |
|
|
300 | strcpy (buf + (middle - prefix), suffix + 1); |
|
|
301 | |
|
|
302 | output_string (term, buf); |
|
|
303 | } |
|
|
304 | break; |
|
|
305 | } |
|
|
306 | |
|
|
307 | free (str); |
336 | free (str); |
308 | |
337 | |
309 | return true; |
338 | return true; |
310 | } |
339 | } |
311 | else |
|
|
312 | { |
340 | } |
313 | // fprintf(stderr,"[%x:%x]",state,keysym); |
341 | |
314 | return false; |
342 | return false; |
315 | } |
|
|
316 | } |
343 | } |
317 | |
344 | |
318 | // purge duplicate keymap entries |
345 | // purge duplicate keymap entries |
319 | void keyboard_manager::purge_duplicate_keymap () |
346 | void keyboard_manager::purge_duplicate_keymap () |
320 | { |
347 | { |
… | |
… | |
330 | if (i < keymap.size ()) |
357 | if (i < keymap.size ()) |
331 | { |
358 | { |
332 | keymap[i] = keymap.back (); |
359 | keymap[i] = keymap.back (); |
333 | keymap.pop_back (); |
360 | keymap.pop_back (); |
334 | } |
361 | } |
|
|
362 | |
335 | break; |
363 | break; |
336 | } |
364 | } |
337 | } |
365 | } |
338 | } |
366 | } |
339 | } |
367 | } |
… | |
… | |
347 | 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 |
348 | |
376 | |
349 | memset (hash_budget_size, 0, sizeof (hash_budget_size)); |
377 | memset (hash_budget_size, 0, sizeof (hash_budget_size)); |
350 | memset (hash_budget_counter, 0, sizeof (hash_budget_counter)); |
378 | memset (hash_budget_counter, 0, sizeof (hash_budget_counter)); |
351 | |
379 | |
352 | // count keysyms for corresponding hash budgets |
380 | // determine hash bucket size |
353 | 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) |
354 | { |
383 | { |
355 | assert (keymap [i]); |
|
|
356 | hashkey = (keymap [i]->keysym & KEYSYM_HASH_MASK); |
384 | hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; |
357 | ++hash_budget_size [hashkey]; |
385 | ++hash_budget_size [hashkey]; |
358 | } |
|
|
359 | |
|
|
360 | // keysym A with range>1 is counted one more time for |
|
|
361 | // every keysym B lies in its range |
|
|
362 | for (i = 0; i < keymap.size (); ++i) |
|
|
363 | { |
|
|
364 | if (keymap[i]->range > 1) |
|
|
365 | { |
|
|
366 | for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j > 0; --j) |
|
|
367 | { |
|
|
368 | hashkey = ((keymap [i]->keysym + j) & KEYSYM_HASH_MASK); |
|
|
369 | if (hash_budget_size [hashkey]) |
|
|
370 | ++hash_budget_size [hashkey]; |
|
|
371 | } |
|
|
372 | } |
386 | } |
373 | } |
|
|
374 | |
387 | |
375 | // now we know the size of each budget |
388 | // now we know the size of each budget |
376 | // compute the index of each budget |
389 | // compute the index of each budget |
377 | hash [0] = 0; |
390 | hash [0] = 0; |
378 | for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i) |
391 | for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i) |
379 | { |
392 | { |
380 | index += hash_budget_size [i - 1]; |
393 | index += hash_budget_size [i - 1]; |
381 | hash[i] = (hash_budget_size [i] ? index : hash [i - 1]); |
394 | hash [i] = index; |
382 | } |
395 | } |
383 | |
396 | |
384 | // and allocate just enough space |
397 | // and allocate just enough space |
385 | //sorted_keymap.reserve (hash[i - 1] + hash_budget_size[i - 1]); |
|
|
386 | 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); |
387 | |
399 | |
388 | // fill in sorted_keymap |
400 | // fill in sorted_keymap |
389 | // it is sorted in each budget |
401 | // it is sorted in each budget |
390 | for (i = 0; i < keymap.size (); ++i) |
402 | for (i = 0; i < keymap.size (); ++i) |
391 | { |
|
|
392 | 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) |
393 | { |
404 | { |
394 | hashkey = ((keymap [i]->keysym + j) & KEYSYM_HASH_MASK); |
405 | hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; |
395 | |
406 | |
396 | if (hash_budget_size [hashkey]) |
|
|
397 | { |
|
|
398 | index = hash [hashkey] + hash_budget_counter [hashkey]; |
407 | index = hash [hashkey] + hash_budget_counter [hashkey]; |
399 | |
408 | |
400 | while (index > hash [hashkey] |
409 | while (index > hash [hashkey] |
401 | && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) |
410 | && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) |
402 | { |
411 | { |
403 | sorted_keymap [index] = sorted_keymap [index - 1]; |
412 | sorted_keymap [index] = sorted_keymap [index - 1]; |
404 | --index; |
413 | --index; |
405 | } |
414 | } |
406 | |
415 | |
407 | sorted_keymap [index] = keymap [i]; |
416 | sorted_keymap [index] = keymap [i]; |
408 | ++hash_budget_counter [hashkey]; |
417 | ++hash_budget_counter [hashkey]; |
409 | } |
|
|
410 | } |
418 | } |
411 | } |
|
|
412 | |
419 | |
413 | keymap.swap (sorted_keymap); |
420 | keymap.swap (sorted_keymap); |
414 | |
421 | |
415 | #if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD) |
422 | #if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD) |
416 | // check for invariants |
423 | // check for invariants |
… | |
… | |
432 | for (i = 0; i < sorted_keymap.size (); ++i) |
439 | for (i = 0; i < sorted_keymap.size (); ++i) |
433 | { |
440 | { |
434 | keysym_t *a = sorted_keymap[i]; |
441 | keysym_t *a = sorted_keymap[i]; |
435 | for (int j = 0; j < a->range; ++j) |
442 | for (int j = 0; j < a->range; ++j) |
436 | { |
443 | { |
437 | int index = find_keysym (a->keysym + j, a->state & OtherModMask); |
444 | int index = find_keysym (a->keysym + j, a->state); |
438 | |
445 | |
439 | assert (index >= 0); |
446 | assert (index >= 0); |
440 | keysym_t *b = keymap [index]; |
447 | keysym_t *b = keymap [index]; |
441 | assert (i == (signed) index || // the normally expected result |
448 | assert (i == (signed) index || // the normally expected result |
442 | (a->keysym + j) >= b->keysym && (a->keysym + j) <= (b->keysym + b->range) && compare_priority (a, b) <= 0); // is effectively the same |
449 | (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 |
443 | } |
450 | } |
444 | } |
451 | } |
445 | #endif |
452 | #endif |
446 | } |
453 | } |
447 | |
454 | |
448 | int |
455 | int |
449 | keyboard_manager::find_keysym (KeySym keysym, unsigned int state) |
456 | keyboard_manager::find_keysym (KeySym keysym, unsigned int state) |
450 | { |
457 | { |
451 | int hashkey = keysym & KEYSYM_HASH_MASK; |
458 | int hashkey = keysym & KEYSYM_HASH_MASK; |
452 | 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 (); |
453 | |
463 | |
454 | for (; index < keymap.size (); ++index) |
464 | for (; index < end; ++index) |
455 | { |
465 | { |
456 | keysym_t *key = keymap [index]; |
466 | keysym_t *key = keymap [index]; |
457 | assert (key); |
|
|
458 | |
467 | |
459 | if (key->keysym <= keysym && key->keysym + key->range > keysym |
468 | if (key->keysym <= keysym && keysym < key->keysym + key->range |
460 | // match only the specified bits in state and ignore others |
469 | // match only the specified bits in state and ignore others |
461 | && (key->state & OtherModMask) == (key->state & state)) |
470 | && (key->state & state) == key->state) |
462 | return index; |
471 | return index; |
463 | else if (key->keysym > keysym && key->range == 1) |
|
|
464 | return -1; |
|
|
465 | } |
472 | } |
466 | |
473 | |
467 | return -1; |
474 | return -1; |
468 | } |
475 | } |
469 | |
476 | |