|
|
1 | /*----------------------------------------------------------------------* |
|
|
2 | * File: keyboard.C |
|
|
3 | *----------------------------------------------------------------------* |
|
|
4 | * |
|
|
5 | * All portions of code are copyright by their respective author/s. |
|
|
6 | * Copyright (c) 2005 WU Fengguang |
|
|
7 | * Copyright (c) 2005-2006 Marc Lehmann <schmorp@schmorp.de> |
|
|
8 | * |
|
|
9 | * This program is free software; you can redistribute it and/or modify |
|
|
10 | * it under the terms of the GNU General Public License as published by |
|
|
11 | * the Free Software Foundation; either version 2 of the License, or |
|
|
12 | * (at your option) any later version. |
|
|
13 | * |
|
|
14 | * This program is distributed in the hope that it will be useful, |
|
|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
17 | * GNU General Public License for more details. |
|
|
18 | * |
|
|
19 | * You should have received a copy of the GNU General Public License |
|
|
20 | * along with this program; if not, write to the Free Software |
|
|
21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|
|
22 | *----------------------------------------------------------------------*/ |
|
|
23 | |
1 | #include "../config.h" |
24 | #include "../config.h" |
2 | #include "rxvt.h" |
25 | #include "rxvt.h" |
|
|
26 | |
|
|
27 | #ifdef KEYSYM_RESOURCE |
|
|
28 | |
|
|
29 | #include <cstring> |
|
|
30 | |
|
|
31 | #include "rxvtperl.h" |
3 | #include "keyboard.h" |
32 | #include "keyboard.h" |
4 | #include "command.h" |
33 | #include "command.h" |
5 | #include <string.h> |
|
|
6 | #include <X11/X.h> |
|
|
7 | |
34 | |
8 | #ifdef KEYSYM_RESOURCE |
35 | /* an intro to the data structure: |
|
|
36 | * |
|
|
37 | * vector keymap[] is grouped. |
|
|
38 | * |
|
|
39 | * inside each group, elements are sorted by the criteria given by compare_priority(). |
|
|
40 | * the lookup of keysym is done in two steps: |
|
|
41 | * 1) locate the group corresponds to the keysym; |
|
|
42 | * 2) do a linear search inside the group. |
|
|
43 | * |
|
|
44 | * array hash[] effectively defines a map from a keysym to a group in keymap[]. |
|
|
45 | * |
|
|
46 | * each group has its address(the index of first group element in keymap[]), |
|
|
47 | * which is computed and stored in hash[]. |
|
|
48 | * hash[] stores the addresses in the form of: |
|
|
49 | * index: 0 I1 I2 I3 In |
|
|
50 | * value: 0...0, A1...A1, A2...A2, A3...A3, ..., An...An |
|
|
51 | * where |
|
|
52 | * A1 = 0; |
|
|
53 | * Ai+1 = N1 + N2 + ... + Ni. |
|
|
54 | * it is computed from hash_bucket_size[]: |
|
|
55 | * index: 0 I1 I2 I3 In |
|
|
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 computation of hash[]) |
|
|
58 | * or we can say |
|
|
59 | * hash_bucket_size[Ii] = Ni; hash_bucket_size[elsewhere] = 0, |
|
|
60 | * where |
|
|
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 |
|
|
63 | * n(the number of groups) = the number of non-zero member of hash_bucket_size[]; |
|
|
64 | * Ni(the size of group i) = hash_bucket_size[Ii]. |
|
|
65 | */ |
9 | |
66 | |
10 | //////////////////////////////////////////////////////////////////////////////// |
67 | static void |
11 | // default keycode translation map and keyevent handlers |
68 | output_string (rxvt_term *term, const char *str) |
12 | keyevent_handler keysym_translator; |
|
|
13 | keyevent_handler keyrange_translator; |
|
|
14 | keyevent_handler keyrange_translator_meta8; |
|
|
15 | keyevent_handler keylist_translator; |
|
|
16 | |
|
|
17 | keysym_t keyboard_manager::stock_keymap_[] = |
|
|
18 | { |
69 | { |
19 | /* examples */ |
70 | if (strncmp (str, "command:", 8) == 0) |
20 | /* keysym, state, range, handler, str*/ |
71 | term->cmdbuf_append (str + 8, strlen (str) - 8); |
21 | //{XK_ISO_Left_Tab, 0, 1, keysym_translator, "\033[Z"}, |
|
|
22 | //{ 'a', 0, 26, keyrange_translator_meta8, "a" "%c"}, |
|
|
23 | //{ 'a', ControlMask, 26, keyrange_translator_meta8, "" "%c"}, |
|
|
24 | //{ XK_Left, 0, 4, keylist_translator, "DACBZ" "\033[Z"}, |
|
|
25 | //{ XK_Left, ShiftMask, 4, keylist_translator, "dacbZ" "\033[Z"}, |
|
|
26 | //{ XK_Left, ControlMask, 4, keylist_translator, "dacbZ" "\033OZ"}, |
|
|
27 | //{ XK_Tab, ControlMask, 1, keysym_translator, "\033<C-Tab>"}, |
|
|
28 | //{ XK_apostrophe, ControlMask, 1, keysym_translator, "\033<C-'>"}, |
|
|
29 | //{ XK_slash, ControlMask, 1, keysym_translator, "\033<C-/>"}, |
|
|
30 | //{ XK_semicolon, ControlMask, 1, keysym_translator, "\033<C-;>"}, |
|
|
31 | //{ XK_grave, ControlMask, 1, keysym_translator, "\033<C-`>"}, |
|
|
32 | //{ XK_comma, ControlMask, 1, keysym_translator, "\033<C-\054>"}, |
|
|
33 | //{ XK_Return, ControlMask, 1, keysym_translator, "\033<C-Return>"}, |
|
|
34 | //{ XK_Return, ShiftMask, 1, keysym_translator, "\033<S-Return>"}, |
|
|
35 | //{ ' ', ShiftMask, 1, keysym_translator, "\033<S-Space>"}, |
|
|
36 | //{ '.', ControlMask, 1, keysym_translator, "\033<C-.>"}, |
|
|
37 | //{ '0', ControlMask, 10, keyrange_translator, "0" "\033<C-%c>"}, |
|
|
38 | //{ '0', MetaMask|ControlMask, 10, keyrange_translator, "0" "\033<M-C-%c>"}, |
|
|
39 | //{ 'a', MetaMask|ControlMask, 26, keyrange_translator, "a" "\033<M-C-%c>"}, |
|
|
40 | }; |
|
|
41 | |
|
|
42 | void output_string (rxvt_term *rt, |
|
|
43 | const char *str) |
|
|
44 | { |
|
|
45 | assert (rt && str); |
|
|
46 | if (strncmp (str, "proto:", 6) == 0) |
72 | else if (strncmp (str, "perl:", 5) == 0) |
47 | rt->cmd_write ((unsigned char*)str + 6, strlen (str) - 6); |
73 | HOOK_INVOKE((term, HOOK_USER_COMMAND, DT_STR, str + 5, DT_END)); |
48 | else |
74 | else |
49 | rt->tt_write ((unsigned char*)str, strlen (str)); |
75 | term->tt_write (str, strlen (str)); |
50 | } |
|
|
51 | |
|
|
52 | void output_string_meta8 (rxvt_term *rt, |
|
|
53 | unsigned int state, |
|
|
54 | char buf[], |
|
|
55 | int buflen) |
|
|
56 | { |
|
|
57 | if (state & rt->ModMetaMask) |
|
|
58 | { |
|
|
59 | #ifdef META8_OPTION |
|
|
60 | if(rt->meta_char == 0x80) /* set 8-bit on */ |
|
|
61 | { |
|
|
62 | for (char *ch = buf; ch < buf + buflen; ch++) |
|
|
63 | *ch |= 0x80; |
|
|
64 | } |
|
|
65 | else if(rt->meta_char == C0_ESC) /* escape prefix */ |
|
|
66 | #endif |
|
|
67 | { |
|
|
68 | const unsigned char ch = C0_ESC; |
|
|
69 | rt->tt_write (&ch, 1); |
|
|
70 | } |
|
|
71 | } |
|
|
72 | |
|
|
73 | rt->tt_write ((unsigned char*)buf, buflen); |
|
|
74 | } |
|
|
75 | |
|
|
76 | int format_keyrange_string (keysym_t *key, |
|
|
77 | KeySym keysym, |
|
|
78 | char buf[], |
|
|
79 | int bufsize) |
|
|
80 | { |
|
|
81 | int len; |
|
|
82 | |
|
|
83 | assert (key->str); |
|
|
84 | len = snprintf (buf, bufsize, key->str + 1, |
|
|
85 | keysym - key->keysym + (int)(key->str[0])); |
|
|
86 | if (len >= bufsize) |
|
|
87 | { |
|
|
88 | fprintf (stderr, "buffer overflowed!\n"); |
|
|
89 | buf[bufsize-1] = '\0'; |
|
|
90 | } |
|
|
91 | else if (len < 0) |
|
|
92 | { |
|
|
93 | perror("keyrange_translator()"); |
|
|
94 | } |
|
|
95 | |
|
|
96 | return len; |
|
|
97 | } |
|
|
98 | |
|
|
99 | bool format_keylist_string (keysym_t *key, |
|
|
100 | KeySym keysym, |
|
|
101 | char buf[], |
|
|
102 | int bufsize) |
|
|
103 | { |
|
|
104 | char *p; |
|
|
105 | |
|
|
106 | assert (key->str); |
|
|
107 | strncpy (buf, key->str + key->range + 1, bufsize); |
|
|
108 | buf[bufsize-1] = '\0'; |
|
|
109 | |
|
|
110 | p = strchr (buf, key->str[key->range]); |
|
|
111 | if (p) |
|
|
112 | { |
|
|
113 | *p = key->str[keysym - key->keysym]; |
|
|
114 | return true; |
|
|
115 | } |
|
|
116 | else |
|
|
117 | { |
|
|
118 | fprintf (stderr, "invalid str for keylist_translator()!\n"); |
|
|
119 | return false; |
|
|
120 | } |
|
|
121 | } |
|
|
122 | |
|
|
123 | ///////////////////////////////////////////////////////////////// |
|
|
124 | void keysym_translator (rxvt_term *rt, |
|
|
125 | keysym_t *key, |
|
|
126 | KeySym keysym, |
|
|
127 | unsigned int state) |
|
|
128 | { |
|
|
129 | output_string (rt, key->str); |
|
|
130 | } |
|
|
131 | |
|
|
132 | void keyrange_translator (rxvt_term *rt, |
|
|
133 | keysym_t *key, |
|
|
134 | KeySym keysym, |
|
|
135 | unsigned int state) |
|
|
136 | { |
|
|
137 | char buf[STRING_MAX]; |
|
|
138 | |
|
|
139 | if (format_keyrange_string (key, keysym, buf, sizeof(buf)) > 0) |
|
|
140 | output_string (rt, buf); |
|
|
141 | } |
|
|
142 | |
|
|
143 | void keyrange_translator_meta8 (rxvt_term *rt, |
|
|
144 | keysym_t *key, |
|
|
145 | KeySym keysym, |
|
|
146 | unsigned int state) |
|
|
147 | { |
|
|
148 | int len; |
|
|
149 | char buf[STRING_MAX]; |
|
|
150 | |
|
|
151 | len = format_keyrange_string (key, keysym, buf, sizeof(buf)); |
|
|
152 | if (len > 0) |
|
|
153 | output_string_meta8 (rt, state, buf, len); |
|
|
154 | } |
|
|
155 | |
|
|
156 | void keylist_translator (rxvt_term *rt, |
|
|
157 | keysym_t *key, |
|
|
158 | KeySym keysym, |
|
|
159 | unsigned int state) |
|
|
160 | { |
|
|
161 | char buf[STRING_MAX]; |
|
|
162 | |
|
|
163 | format_keylist_string (key, keysym, buf, sizeof(buf)); |
|
|
164 | output_string (rt, buf); |
|
|
165 | } |
|
|
166 | |
|
|
167 | //////////////////////////////////////////////////////////////////////////////// |
|
|
168 | // return: #bits of '1' |
|
|
169 | int |
|
|
170 | bitcount (unsigned int n) |
|
|
171 | { |
|
|
172 | int i; |
|
|
173 | for (i = 0; n; ++i, n &= (n - 1)); |
|
|
174 | return i; |
|
|
175 | } |
76 | } |
176 | |
77 | |
177 | // return: priority_of_a - priority_of_b |
78 | // return: priority_of_a - priority_of_b |
178 | int |
79 | static int |
179 | compare_priority (keysym_t *a, keysym_t *b) |
80 | compare_priority (keysym_t *a, keysym_t *b) |
180 | { |
81 | { |
181 | assert (a && b); |
|
|
182 | |
|
|
183 | // (the more '1's in state; the less range): the greater priority |
82 | // (the more '1's in state; the less range): the greater priority |
184 | int ca = bitcount (a->state/* & AllModMask*/); |
83 | int ca = ecb_popcount32 (a->state /* & OtherModMask */); |
185 | int cb = bitcount (b->state/* & AllModMask*/); |
84 | int cb = ecb_popcount32 (b->state /* & OtherModMask */); |
|
|
85 | |
186 | if (ca != cb) |
86 | if (ca != cb) |
187 | return ca - cb; |
87 | return ca - cb; |
188 | //else if (a->state != b->state) // this behavior is to be disscussed |
88 | //else if (a->state != b->state) // this behavior is to be discussed |
189 | // return b->state - a->state; |
89 | // return b->state - a->state; |
190 | else |
90 | else |
191 | return b->range - a->range; |
91 | return 0; |
192 | } |
92 | } |
193 | |
93 | |
194 | //////////////////////////////////////////////////////////////////////////////// |
94 | //////////////////////////////////////////////////////////////////////////////// |
195 | keyboard_manager::keyboard_manager (rxvt_term *rt) |
95 | keyboard_manager::keyboard_manager () |
196 | :term_(rt) |
|
|
197 | { |
96 | { |
198 | keymap_.reserve (256); |
97 | keymap.reserve (256); |
199 | hash_[0] = 1; // hash_[0] != 0 indicates uninitialized data |
98 | hash [0] = 1; // hash[0] != 0 indicates uninitialized data |
200 | } |
99 | } |
201 | |
100 | |
202 | keyboard_manager::~keyboard_manager () |
101 | keyboard_manager::~keyboard_manager () |
203 | { |
102 | { |
204 | clear (); |
103 | for (unsigned int i = 0; i < keymap.size (); ++i) |
|
|
104 | { |
|
|
105 | free (keymap [i]->str); |
|
|
106 | delete keymap [i]; |
|
|
107 | } |
205 | } |
108 | } |
206 | |
109 | |
|
|
110 | // a wrapper for register_translation that converts the input string |
|
|
111 | // to utf-8 and expands 'list' syntax. |
207 | void |
112 | void |
208 | keyboard_manager::clear () |
113 | keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const wchar_t *ws) |
209 | { |
114 | { |
210 | keymap_.clear (); |
115 | char *translation = rxvt_wcstoutf8 (ws); |
211 | hash_[0] = 2; |
|
|
212 | |
116 | |
213 | for(unsigned int i = 0;i < user_translations_.size();++i) |
117 | if (strncmp (translation, "list", 4) == 0 && translation [4] |
214 | { |
118 | && strlen (translation) < STRING_MAX) |
215 | delete[] user_translations_[i]; |
|
|
216 | user_translations_[i] = 0; |
|
|
217 | } |
119 | { |
|
|
120 | char *prefix = translation + 4; |
|
|
121 | char *middle = strchr (prefix + 1, translation [4]); |
|
|
122 | char *suffix = strrchr (prefix + 1, translation [4]); |
218 | |
123 | |
219 | for(unsigned int i = 0;i < user_keymap_.size();++i) |
124 | if (suffix && middle && suffix > middle + 1) |
|
|
125 | { |
|
|
126 | int range = suffix - middle - 1; |
|
|
127 | int prefix_len = middle - prefix - 1; |
|
|
128 | char buf[STRING_MAX]; |
|
|
129 | |
|
|
130 | memcpy (buf, prefix + 1, prefix_len); |
|
|
131 | strcpy (buf + prefix_len + 1, suffix + 1); |
|
|
132 | |
|
|
133 | for (int i = 0; i < range; i++) |
|
|
134 | { |
|
|
135 | buf [prefix_len] = middle [i + 1]; |
|
|
136 | register_translation (keysym + i, state, strdup (buf)); |
|
|
137 | } |
|
|
138 | |
|
|
139 | free (translation); |
|
|
140 | return; |
|
|
141 | } |
|
|
142 | else |
|
|
143 | rxvt_warn ("unable to parse list-type keysym '%s', processing as normal keysym.\n", translation); |
220 | { |
144 | } |
221 | delete user_keymap_[i]; |
|
|
222 | user_keymap_[i] = 0; |
|
|
223 | } |
|
|
224 | |
145 | |
225 | user_keymap_.clear(); |
146 | register_translation (keysym, state, translation); |
226 | user_translations_.clear(); |
|
|
227 | } |
147 | } |
228 | |
148 | |
229 | // a wrapper for register_keymap, |
|
|
230 | // so that outside codes don't have to know so much details. |
|
|
231 | // |
|
|
232 | // the string 'trans' is copied to an internal managed buffer, |
|
|
233 | // so the caller can free memory of 'trans' at any time. |
|
|
234 | void |
149 | void |
235 | keyboard_manager::register_user_translation (KeySym keysym, |
150 | keyboard_manager::register_translation (KeySym keysym, unsigned int state, char *translation) |
236 | unsigned int state, |
|
|
237 | const char *trans) |
|
|
238 | { |
151 | { |
239 | assert(trans); |
|
|
240 | |
|
|
241 | keysym_t *key = new keysym_t; |
152 | keysym_t *key = new keysym_t; |
242 | const char *translation = new char[1+strlen(trans)]; |
|
|
243 | |
153 | |
244 | if(key && translation) |
154 | if (key && translation) |
245 | { |
155 | { |
246 | key->keysym = keysym; |
156 | key->keysym = keysym; |
247 | key->state = state; |
157 | key->state = state; |
248 | key->range = 1; |
|
|
249 | key->str = translation; |
158 | key->str = translation; |
|
|
159 | key->type = keysym_t::STRING; |
250 | |
160 | |
251 | if (strncmp (trans, "list", 4) == 0) |
161 | if (strncmp (translation, "builtin:", 8) == 0) |
|
|
162 | key->type = keysym_t::BUILTIN; |
|
|
163 | |
|
|
164 | if (keymap.size () == keymap.capacity ()) |
|
|
165 | keymap.reserve (keymap.size () * 2); |
|
|
166 | |
|
|
167 | keymap.push_back (key); |
|
|
168 | hash[0] = 3; |
|
|
169 | } |
|
|
170 | else |
|
|
171 | { |
|
|
172 | delete key; |
|
|
173 | free (translation); |
|
|
174 | rxvt_fatal ("memory allocation failure. aborting.\n"); |
|
|
175 | } |
|
|
176 | } |
|
|
177 | |
|
|
178 | bool |
|
|
179 | keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state) |
|
|
180 | { |
|
|
181 | assert (("register_done() need to be called", hash[0] == 0)); |
|
|
182 | |
|
|
183 | state &= OtherModMask; // mask out uninteresting modifiers |
|
|
184 | |
|
|
185 | if (state & term->ModMetaMask) state |= MetaMask; |
|
|
186 | if (state & term->ModNumLockMask) state |= NumLockMask; |
|
|
187 | if (state & term->ModLevel3Mask) state |= Level3Mask; |
|
|
188 | |
|
|
189 | if (!!(term->priv_modes & PrivMode_aplKP) != !!(state & ShiftMask)) |
|
|
190 | state |= AppKeypadMask; |
|
|
191 | |
|
|
192 | int index = find_keysym (keysym, state); |
|
|
193 | |
|
|
194 | if (index >= 0) |
|
|
195 | { |
|
|
196 | keysym_t *key = keymap [index]; |
|
|
197 | |
|
|
198 | if (key->type != keysym_t::BUILTIN) |
252 | { |
199 | { |
253 | const char *p = &trans[4]; |
200 | wchar_t *ws = rxvt_utf8towcs (key->str); |
254 | if (*p && (p = strchr (p+1, *p))) |
201 | char *str = rxvt_wcstombs (ws); |
255 | if ((p - trans - 5 > 1) && strchr (p+1, *p)) |
202 | // TODO: do (some) translations, unescaping etc, here (allow \u escape etc.) |
256 | { |
203 | free (ws); |
257 | strcpy (translation, trans+5); |
204 | |
258 | key->range = p - trans - 5; |
205 | output_string (term, str); |
259 | key->handler = keylist_translator; |
206 | |
260 | } |
207 | free (str); |
|
|
208 | |
|
|
209 | return true; |
261 | } |
210 | } |
262 | if (key->range == 1) |
|
|
263 | { |
|
|
264 | strcpy (translation, trans); |
|
|
265 | key->handler = keysym_translator; |
|
|
266 | } |
|
|
267 | |
|
|
268 | user_keymap_.push_back (key); |
|
|
269 | user_translations_.push_back (translation); |
|
|
270 | register_keymap (key); |
|
|
271 | } |
|
|
272 | else |
|
|
273 | { |
211 | } |
274 | delete key; |
|
|
275 | delete[] translation; |
|
|
276 | rxvt_fatal ("out of memory, aborting.\n"); |
|
|
277 | } |
|
|
278 | } |
|
|
279 | |
212 | |
280 | void |
213 | return false; |
281 | keyboard_manager::register_keymap (keysym_t *key) |
|
|
282 | { |
|
|
283 | assert (key); |
|
|
284 | assert (key->handler); |
|
|
285 | assert (key->range >= 1); |
|
|
286 | |
|
|
287 | if (keymap_.size () == keymap_.capacity ()) |
|
|
288 | keymap_.reserve (keymap_.size () * 2); |
|
|
289 | |
|
|
290 | keymap_.push_back (key); |
|
|
291 | hash_[0] = 3; |
|
|
292 | } |
214 | } |
293 | |
215 | |
294 | void |
216 | void |
295 | keyboard_manager::register_done () |
217 | keyboard_manager::register_done () |
296 | { |
218 | { |
297 | unsigned int i, n = sizeof(stock_keymap_)/sizeof(keysym_t); |
219 | unsigned int i, index, hashkey; |
|
|
220 | vector <keysym_t *> sorted_keymap; |
|
|
221 | uint16_t hash_bucket_size[KEYSYM_HASH_BUCKETS]; // size of each bucket |
298 | |
222 | |
299 | if(keymap_.back() != &stock_keymap_[n-1]) |
223 | memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); |
300 | for(i = 0;i < n;++i) |
|
|
301 | register_keymap(&stock_keymap_[i]); |
|
|
302 | |
224 | |
303 | purge_duplicate_keymap (); |
225 | // determine hash bucket size |
304 | |
|
|
305 | for (i = 0; i < keymap_.size(); ++i) |
226 | for (i = 0; i < keymap.size (); ++i) |
|
|
227 | { |
|
|
228 | hashkey = keymap [i]->keysym & KEYSYM_HASH_MASK; |
|
|
229 | ++hash_bucket_size [hashkey]; |
306 | { |
230 | } |
307 | keysym_t *key = keymap_[i]; |
|
|
308 | |
231 | |
309 | assert (bitcount (term_->ModMetaMask) == 1 |
232 | // now we know the size of each bucket |
310 | && "call me after ModMetaMask was set!"); |
233 | // compute the index of each bucket |
311 | if (key->state & MetaMask) |
234 | hash [0] = 0; |
|
|
235 | for (index = 0, i = 1; i < KEYSYM_HASH_BUCKETS; ++i) |
|
|
236 | { |
|
|
237 | index += hash_bucket_size [i - 1]; |
|
|
238 | hash [i] = index; |
|
|
239 | } |
|
|
240 | |
|
|
241 | // and allocate just enough space |
|
|
242 | sorted_keymap.insert (sorted_keymap.begin (), index + hash_bucket_size [i - 1], 0); |
|
|
243 | |
|
|
244 | memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); |
|
|
245 | |
|
|
246 | // fill in sorted_keymap |
|
|
247 | // it is sorted in each bucket |
|
|
248 | for (i = 0; i < keymap.size (); ++i) |
|
|
249 | { |
|
|
250 | hashkey = keymap [i]->keysym & KEYSYM_HASH_MASK; |
|
|
251 | |
|
|
252 | index = hash [hashkey] + hash_bucket_size [hashkey]; |
|
|
253 | |
|
|
254 | while (index > hash [hashkey] |
|
|
255 | && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) |
312 | { |
256 | { |
313 | //key->state &= ~MetaMask; |
257 | sorted_keymap [index] = sorted_keymap [index - 1]; |
314 | key->state |= term_->ModMetaMask; |
258 | --index; |
315 | } |
259 | } |
316 | |
260 | |
317 | assert (bitcount (term_->ModNumLockMask) == 1 |
261 | sorted_keymap [index] = keymap [i]; |
318 | && "call me after ModNumLockMask was set!"); |
262 | ++hash_bucket_size [hashkey]; |
319 | if (key->state & NumLockMask) |
263 | } |
|
|
264 | |
|
|
265 | keymap.swap (sorted_keymap); |
|
|
266 | |
|
|
267 | #ifndef NDEBUG |
|
|
268 | // check for invariants |
|
|
269 | for (i = 0; i < KEYSYM_HASH_BUCKETS; ++i) |
|
|
270 | { |
|
|
271 | index = hash[i]; |
|
|
272 | for (int j = 0; j < hash_bucket_size [i]; ++j) |
320 | { |
273 | { |
321 | //key->state &= ~NumLockMask; |
|
|
322 | key->state |= term_->ModNumLockMask; |
|
|
323 | } |
|
|
324 | } |
|
|
325 | |
|
|
326 | setup_hash (); |
|
|
327 | } |
|
|
328 | |
|
|
329 | bool keyboard_manager::dispatch (KeySym keysym, unsigned int state) |
|
|
330 | { |
|
|
331 | assert(hash_[0] == 0 && "register_done() need to be called"); |
|
|
332 | |
|
|
333 | int index = find_keysym (keysym, state); |
|
|
334 | |
|
|
335 | if (index >= 0) |
|
|
336 | { |
|
|
337 | assert (term_ && keymap_[index] && keymap_[index]->handler); |
|
|
338 | keymap_[index]->handler (term_, keymap_[index], keysym, state); |
|
|
339 | return true; |
|
|
340 | } |
|
|
341 | else |
|
|
342 | { |
|
|
343 | // fprintf(stderr,"[%x:%x]",state,keysym); |
|
|
344 | return false; |
|
|
345 | } |
|
|
346 | } |
|
|
347 | |
|
|
348 | // purge duplicate keymap_ entries |
|
|
349 | void |
|
|
350 | keyboard_manager::purge_duplicate_keymap () |
|
|
351 | { |
|
|
352 | for (unsigned int i = 0; i < keymap_.size (); ++i) |
|
|
353 | { |
|
|
354 | for (unsigned int j = 0; j < i; ++j) |
|
|
355 | { |
|
|
356 | if (keymap_[i] == keymap_[j]) |
|
|
357 | { |
|
|
358 | while (keymap_[i] == keymap_.back ()) |
|
|
359 | keymap_.pop_back (); |
|
|
360 | if (i < keymap_.size ()) |
|
|
361 | { |
|
|
362 | keymap_[i] = keymap_.back (); |
|
|
363 | keymap_.pop_back (); |
|
|
364 | } |
|
|
365 | break; |
|
|
366 | } |
|
|
367 | } |
|
|
368 | } |
|
|
369 | } |
|
|
370 | |
|
|
371 | void |
|
|
372 | keyboard_manager::setup_hash () |
|
|
373 | { |
|
|
374 | unsigned int i, index, hashkey; |
|
|
375 | vector<keysym_t *> sorted_keymap; |
|
|
376 | u16 hash_budget_size[KEYSYM_HASH_BUDGETS]; // size of each budget |
|
|
377 | u16 hash_budget_counter[KEYSYM_HASH_BUDGETS]; // #elements in each budget |
|
|
378 | |
|
|
379 | memset (hash_budget_size, 0, sizeof (hash_budget_size)); |
|
|
380 | memset (hash_budget_counter, 0, sizeof (hash_budget_counter)); |
|
|
381 | |
|
|
382 | // count keysyms for corresponding hash budgets |
|
|
383 | for (i = 0; i < keymap_.size (); ++i) |
|
|
384 | { |
|
|
385 | assert (keymap_[i]); |
|
|
386 | hashkey = (keymap_[i]->keysym & KEYSYM_HASH_MASK); |
|
|
387 | ++hash_budget_size[hashkey]; |
|
|
388 | } |
|
|
389 | |
|
|
390 | // keysym A with range>1 is counted one more time for |
|
|
391 | // every keysym B lies in its range |
|
|
392 | for (i = 0; i < keymap_.size (); ++i) |
|
|
393 | { |
|
|
394 | if (keymap_[i]->range > 1) |
|
|
395 | { |
|
|
396 | for (int j = min (keymap_[i]->range, KEYSYM_HASH_BUDGETS) - 1;j > 0; --j) |
|
|
397 | { |
|
|
398 | hashkey = ((keymap_[i]->keysym + j) & KEYSYM_HASH_MASK); |
|
|
399 | if (hash_budget_size[hashkey]) |
|
|
400 | ++hash_budget_size[hashkey]; |
|
|
401 | } |
|
|
402 | } |
|
|
403 | } |
|
|
404 | |
|
|
405 | // now we know the size of each budget |
|
|
406 | // compute the index of each budget |
|
|
407 | hash_[0] = 0; |
|
|
408 | for (index = 0,i = 1; i < KEYSYM_HASH_BUDGETS; ++i) |
|
|
409 | { |
|
|
410 | index += hash_budget_size[i-1]; |
|
|
411 | hash_[i] = (hash_budget_size[i] ? index : hash_[i-1]); |
|
|
412 | } |
|
|
413 | // and allocate just enough space |
|
|
414 | //sorted_keymap.reserve (hash_[i - 1] + hash_budget_size[i - 1]); |
|
|
415 | sorted_keymap.insert (sorted_keymap.begin(), index + hash_budget_size[i - 1], 0); |
|
|
416 | |
|
|
417 | // fill in sorted_keymap |
|
|
418 | // it is sorted in each budget |
|
|
419 | for (i = 0; i < keymap_.size (); ++i) |
|
|
420 | { |
|
|
421 | for (int j = min (keymap_[i]->range, KEYSYM_HASH_BUDGETS) - 1;j >= 0; --j) |
|
|
422 | { |
|
|
423 | hashkey = ((keymap_[i]->keysym + j) & KEYSYM_HASH_MASK); |
|
|
424 | if (hash_budget_size[hashkey]) |
|
|
425 | { |
|
|
426 | index = hash_[hashkey] + hash_budget_counter[hashkey]; |
|
|
427 | while (index > hash_[hashkey] && |
|
|
428 | compare_priority (keymap_[i], |
|
|
429 | sorted_keymap[index - 1]) > 0) |
|
|
430 | { |
|
|
431 | sorted_keymap[index] = sorted_keymap[index - 1]; |
|
|
432 | --index; |
|
|
433 | } |
|
|
434 | sorted_keymap[index] = keymap_[i]; |
|
|
435 | ++hash_budget_counter[hashkey]; |
|
|
436 | } |
|
|
437 | } |
|
|
438 | } |
|
|
439 | |
|
|
440 | keymap_.swap (sorted_keymap); |
|
|
441 | |
|
|
442 | #if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD) |
|
|
443 | // check for invariants |
|
|
444 | for (i = 0; i < KEYSYM_HASH_BUDGETS; ++i) |
|
|
445 | { |
|
|
446 | index = hash_[i]; |
|
|
447 | for (int j = 0; j < hash_budget_size[i]; ++j) |
|
|
448 | { |
|
|
449 | if (keymap_[index + j]->range == 1) |
|
|
450 | assert (i == (keymap_[index + j]->keysym & KEYSYM_HASH_MASK)); |
274 | assert (i == (keymap [index + j]->keysym & KEYSYM_HASH_MASK)); |
|
|
275 | |
451 | if (j) |
276 | if (j) |
452 | assert (compare_priority (keymap_[index + j - 1], |
277 | assert (compare_priority (keymap [index + j - 1], |
453 | keymap_[index + j]) >= 0); |
278 | keymap [index + j]) >= 0); |
454 | } |
279 | } |
455 | } |
280 | } |
456 | |
281 | |
457 | // this should be able to detect most possible bugs |
282 | // this should be able to detect most possible bugs |
458 | for (i = 0; i < sorted_keymap.size (); ++i) |
283 | for (i = 0; i < sorted_keymap.size (); ++i) |
459 | { |
284 | { |
460 | keysym_t *a = sorted_keymap[i]; |
285 | keysym_t *a = sorted_keymap[i]; |
461 | for (int j = 0; j < a->range; ++j) |
|
|
462 | { |
|
|
463 | int index = find_keysym (a->keysym + j, a->state & AllModMask); |
286 | int index = find_keysym (a->keysym, a->state); |
|
|
287 | |
464 | assert (index >= 0); |
288 | assert (index >= 0); |
465 | keysym_t *b = keymap_[index]; |
289 | keysym_t *b = keymap [index]; |
466 | assert (i == (signed)index || // the normally expected result |
290 | assert (i == index // the normally expected result |
467 | (a->keysym + j) >= b->keysym && |
291 | || a->keysym == b->keysym |
468 | (a->keysym + j) <= (b->keysym + b->range) && |
|
|
469 | compare_priority (a, b) <= 0); // is effectively the same |
292 | && compare_priority (a, b) <= 0); // is effectively the same or a closer match |
470 | } |
|
|
471 | } |
293 | } |
472 | #endif |
294 | #endif |
473 | } |
295 | } |
474 | |
296 | |
475 | int |
297 | int |
476 | keyboard_manager::find_keysym (KeySym keysym, unsigned int state) |
298 | keyboard_manager::find_keysym (KeySym keysym, unsigned int state) |
477 | { |
299 | { |
478 | int hashkey = (keysym & KEYSYM_HASH_MASK); |
300 | int hashkey = keysym & KEYSYM_HASH_MASK; |
479 | unsigned int index = hash_[hashkey]; |
301 | unsigned int index = hash [hashkey]; |
|
|
302 | unsigned int end = hashkey < KEYSYM_HASH_BUCKETS - 1 |
|
|
303 | ? hash [hashkey + 1] |
|
|
304 | : keymap.size (); |
480 | |
305 | |
481 | for (;index < keymap_.size(); ++index) |
306 | for (; index < end; ++index) |
482 | { |
307 | { |
483 | keysym_t *key = keymap_[index]; |
308 | keysym_t *key = keymap [index]; |
484 | assert (key); |
309 | |
485 | if (key->keysym <= keysym && |
310 | if (key->keysym == keysym |
486 | key->keysym + key->range > keysym && |
|
|
487 | // match only the specified bits in state and ignore others |
311 | // match only the specified bits in state and ignore others |
488 | (key->state & (unsigned int)AllModMask) == (key->state & state)) |
312 | && (key->state & state) == key->state) |
489 | { |
|
|
490 | return index; |
313 | return index; |
491 | } |
|
|
492 | else if (key->keysym > keysym && |
|
|
493 | key->range == 1) |
|
|
494 | return -1; |
|
|
495 | } |
314 | } |
496 | |
315 | |
497 | return -1; |
316 | return -1; |
498 | } |
317 | } |
499 | |
318 | |