… | |
… | |
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 wchar_t *ws) |
105 | keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const wchar_t *ws) |
114 | { |
106 | { |
115 | char *translation = rxvt_wcstoutf8 (ws); |
107 | char *translation = rxvt_wcstoutf8 (ws); |
116 | |
108 | |
117 | if (strncmp (translation, "list", 4) == 0 && translation [4] |
|
|
118 | && strlen (translation) < STRING_MAX) |
|
|
119 | { |
|
|
120 | char *prefix = translation + 4; |
|
|
121 | char *middle = strchr (prefix + 1, translation [4]); |
|
|
122 | char *suffix = strrchr (prefix + 1, translation [4]); |
|
|
123 | |
|
|
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); |
|
|
144 | } |
|
|
145 | |
|
|
146 | register_translation (keysym, state, translation); |
|
|
147 | } |
|
|
148 | |
|
|
149 | void |
|
|
150 | keyboard_manager::register_translation (KeySym keysym, unsigned int state, char *translation) |
|
|
151 | { |
|
|
152 | keysym_t *key = new keysym_t; |
109 | keysym_t *key = new keysym_t; |
153 | |
110 | |
154 | if (key && translation) |
|
|
155 | { |
|
|
156 | key->keysym = keysym; |
111 | key->keysym = keysym; |
157 | key->state = state; |
112 | key->state = state; |
158 | key->str = translation; |
113 | key->str = translation; |
159 | key->type = keysym_t::STRING; |
114 | key->type = keysym_t::STRING; |
160 | |
115 | |
161 | if (strncmp (translation, "builtin:", 8) == 0) |
116 | if (strncmp (translation, "builtin:", 8) == 0) |
162 | key->type = keysym_t::BUILTIN; |
117 | key->type = keysym_t::BUILTIN; |
163 | |
118 | |
164 | if (keymap.size () == keymap.capacity ()) |
119 | if (keymap.size () == keymap.capacity ()) |
165 | keymap.reserve (keymap.size () * 2); |
120 | keymap.reserve (keymap.size () * 2); |
166 | |
121 | |
167 | keymap.push_back (key); |
122 | keymap.push_back (key); |
168 | hash[0] = 3; |
123 | hash[0] = 3; |
169 | } |
|
|
170 | else |
|
|
171 | { |
|
|
172 | delete key; |
|
|
173 | free (translation); |
|
|
174 | rxvt_fatal ("memory allocation failure. aborting.\n"); |
|
|
175 | } |
|
|
176 | } |
124 | } |
177 | |
125 | |
178 | bool |
126 | bool |
179 | keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state) |
127 | keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state) |
180 | { |
128 | { |
… | |
… | |
215 | |
163 | |
216 | void |
164 | void |
217 | keyboard_manager::register_done () |
165 | keyboard_manager::register_done () |
218 | { |
166 | { |
219 | unsigned int i, index, hashkey; |
167 | unsigned int i, index, hashkey; |
220 | vector <keysym_t *> sorted_keymap; |
|
|
221 | 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 |
222 | |
169 | |
223 | memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); |
170 | memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); |
224 | |
171 | |
225 | // determine hash bucket size |
172 | // determine hash bucket size |
… | |
… | |
229 | ++hash_bucket_size [hashkey]; |
176 | ++hash_bucket_size [hashkey]; |
230 | } |
177 | } |
231 | |
178 | |
232 | // now we know the size of each bucket |
179 | // now we know the size of each bucket |
233 | // compute the index of each bucket |
180 | // compute the index of each bucket |
234 | hash [0] = 0; |
|
|
235 | for (index = 0, i = 1; i < KEYSYM_HASH_BUCKETS; ++i) |
181 | for (index = 0, i = 0; i < KEYSYM_HASH_BUCKETS; ++i) |
236 | { |
182 | { |
237 | index += hash_bucket_size [i - 1]; |
|
|
238 | hash [i] = index; |
183 | hash [i] = index; |
|
|
184 | index += hash_bucket_size [i]; |
239 | } |
185 | } |
240 | |
186 | |
241 | // and allocate just enough space |
187 | // and allocate just enough space |
242 | sorted_keymap.insert (sorted_keymap.begin (), index + hash_bucket_size [i - 1], 0); |
188 | simplevec <keysym_t *> sorted_keymap (index, 0); |
243 | |
189 | |
244 | memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); |
190 | memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); |
245 | |
191 | |
246 | // fill in sorted_keymap |
192 | // fill in sorted_keymap |
247 | // it is sorted in each bucket |
193 | // it is sorted in each bucket |