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