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