… | |
… | |
24 | #include "../config.h" |
24 | #include "../config.h" |
25 | #include "rxvt.h" |
25 | #include "rxvt.h" |
26 | |
26 | |
27 | #ifdef KEYSYM_RESOURCE |
27 | #ifdef KEYSYM_RESOURCE |
28 | |
28 | |
29 | #include <cstring> |
29 | #include <string.h> |
30 | |
30 | |
31 | #include "rxvtperl.h" |
31 | #include "rxvtperl.h" |
32 | #include "keyboard.h" |
32 | #include "keyboard.h" |
33 | #include "command.h" |
|
|
34 | |
33 | |
35 | /* an intro to the data structure: |
34 | /* an intro to the data structure: |
36 | * |
35 | * |
37 | * vector keymap[] is grouped. |
36 | * vector keymap[] is grouped. |
38 | * |
37 | * |
… | |
… | |
81 | { |
80 | { |
82 | // (the more '1's in state; the less range): the greater priority |
81 | // (the more '1's in state; the less range): the greater priority |
83 | int ca = ecb_popcount32 (a->state /* & OtherModMask */); |
82 | int ca = ecb_popcount32 (a->state /* & OtherModMask */); |
84 | int cb = ecb_popcount32 (b->state /* & OtherModMask */); |
83 | int cb = ecb_popcount32 (b->state /* & OtherModMask */); |
85 | |
84 | |
86 | if (ca != cb) |
|
|
87 | return ca - cb; |
85 | return ca - cb; |
88 | //else if (a->state != b->state) // this behavior is to be discussed |
|
|
89 | // return b->state - a->state; |
|
|
90 | else |
|
|
91 | return 0; |
|
|
92 | } |
86 | } |
93 | |
87 | |
94 | //////////////////////////////////////////////////////////////////////////////// |
88 | //////////////////////////////////////////////////////////////////////////////// |
95 | keyboard_manager::keyboard_manager () |
89 | keyboard_manager::keyboard_manager () |
96 | { |
90 | { |
… | |
… | |
105 | free (keymap [i]->str); |
99 | free (keymap [i]->str); |
106 | delete keymap [i]; |
100 | delete keymap [i]; |
107 | } |
101 | } |
108 | } |
102 | } |
109 | |
103 | |
110 | // a wrapper for register_translation that converts the input string |
|
|
111 | // to utf-8 and expands 'list' syntax. |
|
|
112 | void |
104 | void |
113 | keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const char *trans) |
105 | keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const wchar_t *ws) |
114 | { |
106 | { |
115 | wchar_t *wc = rxvt_mbstowcs (trans); |
|
|
116 | char *translation = rxvt_wcstoutf8 (wc); |
107 | char *translation = rxvt_wcstoutf8 (ws); |
117 | free (wc); |
|
|
118 | |
108 | |
119 | if (strncmp (translation, "list", 4) == 0 && translation [4] |
|
|
120 | && strlen (translation) < STRING_MAX) |
|
|
121 | { |
|
|
122 | char *prefix = translation + 4; |
|
|
123 | char *middle = strchr (prefix + 1, translation [4]); |
|
|
124 | char *suffix = strrchr (prefix + 1, translation [4]); |
|
|
125 | |
|
|
126 | if (suffix && middle && suffix > middle + 1) |
|
|
127 | { |
|
|
128 | int range = suffix - middle - 1; |
|
|
129 | int prefix_len = middle - prefix - 1; |
|
|
130 | char buf[STRING_MAX]; |
|
|
131 | |
|
|
132 | memcpy (buf, prefix + 1, prefix_len); |
|
|
133 | strcpy (buf + prefix_len + 1, suffix + 1); |
|
|
134 | |
|
|
135 | for (int i = 0; i < range; i++) |
|
|
136 | { |
|
|
137 | buf [prefix_len] = middle [i + 1]; |
|
|
138 | register_translation (keysym + i, state, strdup (buf)); |
|
|
139 | } |
|
|
140 | |
|
|
141 | free (translation); |
|
|
142 | return; |
|
|
143 | } |
|
|
144 | else |
|
|
145 | rxvt_warn ("unable to parse list-type keysym '%s', processing as normal keysym.\n", translation); |
|
|
146 | } |
|
|
147 | |
|
|
148 | register_translation (keysym, state, translation); |
|
|
149 | } |
|
|
150 | |
|
|
151 | void |
|
|
152 | keyboard_manager::register_translation (KeySym keysym, unsigned int state, char *translation) |
|
|
153 | { |
|
|
154 | keysym_t *key = new keysym_t; |
109 | keysym_t *key = new keysym_t; |
155 | |
110 | |
156 | if (key && translation) |
|
|
157 | { |
|
|
158 | key->keysym = keysym; |
111 | key->keysym = keysym; |
159 | key->state = state; |
112 | key->state = state; |
160 | key->str = translation; |
113 | key->str = translation; |
161 | key->type = keysym_t::STRING; |
114 | key->type = keysym_t::STRING; |
162 | |
115 | |
163 | if (strncmp (translation, "builtin:", 8) == 0) |
116 | if (strncmp (translation, "builtin:", 8) == 0) |
164 | key->type = keysym_t::BUILTIN; |
117 | key->type = keysym_t::BUILTIN; |
165 | |
118 | |
166 | if (keymap.size () == keymap.capacity ()) |
119 | if (keymap.size () == keymap.capacity ()) |
167 | keymap.reserve (keymap.size () * 2); |
120 | keymap.reserve (keymap.size () * 2); |
168 | |
121 | |
169 | keymap.push_back (key); |
122 | keymap.push_back (key); |
170 | hash[0] = 3; |
123 | hash[0] = 3; |
171 | } |
|
|
172 | else |
|
|
173 | { |
|
|
174 | delete key; |
|
|
175 | free (translation); |
|
|
176 | rxvt_fatal ("memory allocation failure. aborting.\n"); |
|
|
177 | } |
|
|
178 | } |
124 | } |
179 | |
125 | |
180 | bool |
126 | bool |
181 | keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state) |
127 | keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state, const char *kbuf, int len) |
182 | { |
128 | { |
183 | assert (("register_done() need to be called", hash[0] == 0)); |
129 | assert (("register_done() need to be called", hash[0] == 0)); |
184 | |
130 | |
185 | state &= OtherModMask; // mask out uninteresting modifiers |
131 | state &= OtherModMask; // mask out uninteresting modifiers |
186 | |
132 | |
… | |
… | |
197 | { |
143 | { |
198 | keysym_t *key = keymap [index]; |
144 | keysym_t *key = keymap [index]; |
199 | |
145 | |
200 | if (key->type != keysym_t::BUILTIN) |
146 | if (key->type != keysym_t::BUILTIN) |
201 | { |
147 | { |
202 | wchar_t *wc = rxvt_utf8towcs (key->str); |
148 | wchar_t *ws = rxvt_utf8towcs (key->str); |
203 | char *str = rxvt_wcstombs (wc); |
149 | char *str = rxvt_wcstombs (ws); |
204 | // TODO: do (some) translations, unescaping etc, here (allow \u escape etc.) |
150 | // TODO: do (some) translations, unescaping etc, here (allow \u escape etc.) |
205 | free (wc); |
151 | free (ws); |
206 | |
152 | |
207 | output_string (term, str); |
153 | output_string (term, str); |
208 | |
154 | |
209 | free (str); |
155 | free (str); |
210 | |
156 | |
… | |
… | |
217 | |
163 | |
218 | void |
164 | void |
219 | keyboard_manager::register_done () |
165 | keyboard_manager::register_done () |
220 | { |
166 | { |
221 | unsigned int i, index, hashkey; |
167 | unsigned int i, index, hashkey; |
222 | vector <keysym_t *> sorted_keymap; |
|
|
223 | uint16_t hash_bucket_size[KEYSYM_HASH_BUCKETS]; // size of each bucket |
168 | uint16_t hash_bucket_size[KEYSYM_HASH_BUCKETS]; // size of each bucket |
224 | |
169 | |
225 | memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); |
170 | memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); |
226 | |
171 | |
227 | // determine hash bucket size |
172 | // determine hash bucket size |
… | |
… | |
231 | ++hash_bucket_size [hashkey]; |
176 | ++hash_bucket_size [hashkey]; |
232 | } |
177 | } |
233 | |
178 | |
234 | // now we know the size of each bucket |
179 | // now we know the size of each bucket |
235 | // compute the index of each bucket |
180 | // compute the index of each bucket |
236 | hash [0] = 0; |
|
|
237 | for (index = 0, i = 1; i < KEYSYM_HASH_BUCKETS; ++i) |
181 | for (index = 0, i = 0; i < KEYSYM_HASH_BUCKETS; ++i) |
238 | { |
182 | { |
239 | index += hash_bucket_size [i - 1]; |
|
|
240 | hash [i] = index; |
183 | hash [i] = index; |
|
|
184 | index += hash_bucket_size [i]; |
241 | } |
185 | } |
242 | |
186 | |
243 | // and allocate just enough space |
187 | // and allocate just enough space |
244 | sorted_keymap.insert (sorted_keymap.begin (), index + hash_bucket_size [i - 1], 0); |
188 | simplevec <keysym_t *> sorted_keymap (index, 0); |
245 | |
189 | |
246 | memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); |
190 | memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); |
247 | |
191 | |
248 | // fill in sorted_keymap |
192 | // fill in sorted_keymap |
249 | // it is sorted in each bucket |
193 | // it is sorted in each bucket |