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