ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/keyboard.C
(Generate patch)

Comparing rxvt-unicode/src/keyboard.C (file contents):
Revision 1.37 by sf-exg, Tue Aug 24 23:57:04 2010 UTC vs.
Revision 1.46 by sf-exg, Sun Nov 27 10:14:51 2011 UTC

2 * File: keyboard.C 2 * File: keyboard.C
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * 4 *
5 * All portions of code are copyright by their respective author/s. 5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 2005 WU Fengguang 6 * Copyright (c) 2005 WU Fengguang
7 * Copyright (c) 2005-2006 Marc Lehmann <pcg@goof.com> 7 * Copyright (c) 2005-2006 Marc Lehmann <schmorp@schmorp.de>
8 * 8 *
9 * This program is free software; you can redistribute it and/or modify 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 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 11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version. 12 * (at your option) any later version.
66 66
67static void 67static void
68output_string (rxvt_term *rt, const char *str) 68output_string (rxvt_term *rt, const char *str)
69{ 69{
70 if (strncmp (str, "command:", 8) == 0) 70 if (strncmp (str, "command:", 8) == 0)
71 rt->cmd_write (str + 8, strlen (str) - 8); 71 rt->cmdbuf_append (str + 8, strlen (str) - 8);
72 else if (strncmp (str, "perl:", 5) == 0) 72 else if (strncmp (str, "perl:", 5) == 0)
73 HOOK_INVOKE((rt, HOOK_USER_COMMAND, DT_STR, str + 5, DT_END)); 73 HOOK_INVOKE((rt, HOOK_USER_COMMAND, DT_STR, str + 5, DT_END));
74 else 74 else
75 rt->tt_write (str, strlen (str)); 75 rt->tt_write (str, strlen (str));
76} 76}
78// return: priority_of_a - priority_of_b 78// return: priority_of_a - priority_of_b
79static int 79static int
80compare_priority (keysym_t *a, keysym_t *b) 80compare_priority (keysym_t *a, keysym_t *b)
81{ 81{
82 // (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
83 int ca = rxvt_popcount (a->state /* & OtherModMask */); 83 int ca = ecb_popcount32 (a->state /* & OtherModMask */);
84 int cb = rxvt_popcount (b->state /* & OtherModMask */); 84 int cb = ecb_popcount32 (b->state /* & OtherModMask */);
85 85
86 if (ca != cb) 86 if (ca != cb)
87 return ca - cb; 87 return ca - cb;
88//else if (a->state != b->state) // this behavior is to be discussed 88//else if (a->state != b->state) // this behavior is to be discussed
89// return b->state - a->state; 89// return b->state - a->state;
90 else 90 else
91 return b->range - a->range; 91 return 0;
92} 92}
93 93
94//////////////////////////////////////////////////////////////////////////////// 94////////////////////////////////////////////////////////////////////////////////
95keyboard_manager::keyboard_manager () 95keyboard_manager::keyboard_manager ()
96{ 96{
108{ 108{
109 hash [0] = 2; 109 hash [0] = 2;
110 110
111 for (unsigned int i = 0; i < keymap.size (); ++i) 111 for (unsigned int i = 0; i < keymap.size (); ++i)
112 { 112 {
113 free ((void *)keymap [i]->str); 113 free (keymap [i]->str);
114 delete keymap [i]; 114 delete keymap [i];
115 keymap [i] = 0; 115 keymap [i] = 0;
116 } 116 }
117 117
118 keymap.clear (); 118 keymap.clear ();
119} 119}
120 120
121// a wrapper for register_keymap, 121// a wrapper for register_translation that converts the input string
122// so that outside codes don't have to know so much details. 122// to utf-8 and expands 'list' syntax.
123//
124// the string 'trans' is copied to an internal managed buffer,
125// so the caller can free memory of 'trans' at any time.
126void 123void
127keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const char *trans) 124keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const char *trans)
128{ 125{
129 keysym_t *key = new keysym_t;
130 wchar_t *wc = rxvt_mbstowcs (trans); 126 wchar_t *wc = rxvt_mbstowcs (trans);
131 char *translation = rxvt_wcstoutf8 (wc); 127 char *translation = rxvt_wcstoutf8 (wc);
132 free (wc); 128 free (wc);
133 129
130 if (strncmp (translation, "list", 4) == 0 && translation [4]
131 && strlen (translation) < STRING_MAX)
132 {
133 char *prefix = translation + 4;
134 char *middle = strchr (prefix + 1, translation [4]);
135 char *suffix = strrchr (prefix + 1, translation [4]);
136
137 if (suffix && middle && suffix > middle + 1)
138 {
139 int range = suffix - middle - 1;
140 int prefix_len = middle - prefix - 1;
141 char buf[STRING_MAX];
142
143 memcpy (buf, prefix + 1, prefix_len);
144 strcpy (buf + prefix_len + 1, suffix + 1);
145
146 for (int i = 0; i < range; i++)
147 {
148 buf [prefix_len] = middle [i + 1];
149 register_translation (keysym + i, state, strdup (buf));
150 }
151
152 free (translation);
153 return;
154 }
155 else
156 rxvt_warn ("unable to parse list-type keysym '%s', processing as normal keysym.\n", translation);
157 }
158
159 register_translation (keysym, state, translation);
160}
161
162void
163keyboard_manager::register_translation (KeySym keysym, unsigned int state, char *translation)
164{
165 keysym_t *key = new keysym_t;
166
134 if (key && translation) 167 if (key && translation)
135 { 168 {
136 key->keysym = keysym; 169 key->keysym = keysym;
137 key->state = state; 170 key->state = state;
138 key->range = 1;
139 key->str = translation; 171 key->str = translation;
140 key->type = keysym_t::STRING; 172 key->type = keysym_t::STRING;
141 173
142 if (strncmp (translation, "list", 4) == 0 && translation [4])
143 {
144 char *middle = strchr (translation + 5, translation [4]);
145 char *suffix = strrchr (translation + 5, translation [4]);
146
147 if (suffix && middle && suffix > middle + 1)
148 {
149 key->type = keysym_t::LIST;
150 key->range = suffix - middle - 1;
151
152 memmove (translation, translation + 4, strlen (translation + 4) + 1);
153 }
154 else
155 rxvt_warn ("cannot parse list-type keysym '%s', treating as normal keysym.\n", translation);
156 }
157 else if (strncmp (translation, "builtin:", 8) == 0) 174 if (strncmp (translation, "builtin:", 8) == 0)
158 key->type = keysym_t::BUILTIN; 175 key->type = keysym_t::BUILTIN;
159 176
160 register_keymap (key); 177 register_keymap (key);
161 } 178 }
162 else 179 else
163 { 180 {
164 delete key; 181 delete key;
165 free ((void *)translation); 182 free (translation);
166 rxvt_fatal ("out of memory, aborting.\n"); 183 rxvt_fatal ("memory allocation failure. aborting.\n");
167 } 184 }
168} 185}
169 186
170void 187void
171keyboard_manager::register_keymap (keysym_t *key) 188keyboard_manager::register_keymap (keysym_t *key)
184} 201}
185 202
186bool 203bool
187keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state) 204keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state)
188{ 205{
189 assert (hash[0] == 0 && "register_done() need to be called"); 206 assert (("register_done() need to be called", hash[0] == 0));
190 207
191 state &= OtherModMask; // mask out uninteresting modifiers 208 state &= OtherModMask; // mask out uninteresting modifiers
192 209
193 if (state & term->ModMetaMask) state |= MetaMask; 210 if (state & term->ModMetaMask) state |= MetaMask;
194 if (state & term->ModNumLockMask) state |= NumLockMask; 211 if (state & term->ModNumLockMask) state |= NumLockMask;
203 { 220 {
204 const keysym_t &key = *keymap [index]; 221 const keysym_t &key = *keymap [index];
205 222
206 if (key.type != keysym_t::BUILTIN) 223 if (key.type != keysym_t::BUILTIN)
207 { 224 {
208 int keysym_offset = keysym - key.keysym;
209
210 wchar_t *wc = rxvt_utf8towcs (key.str); 225 wchar_t *wc = rxvt_utf8towcs (key.str);
211 char *str = rxvt_wcstombs (wc); 226 char *str = rxvt_wcstombs (wc);
212 // TODO: do (some) translations, unescaping etc, here (allow \u escape etc.) 227 // TODO: do (some) translations, unescaping etc, here (allow \u escape etc.)
213 free (wc); 228 free (wc);
214 229
215 switch (key.type)
216 {
217 case keysym_t::STRING:
218 output_string (term, str); 230 output_string (term, str);
219 break;
220
221 case keysym_t::LIST:
222 {
223 char buf[STRING_MAX];
224
225 char *prefix, *middle, *suffix;
226
227 prefix = str;
228 middle = strchr (prefix + 1, *prefix);
229 suffix = strrchr (middle + 1, *prefix);
230
231 memcpy (buf, prefix + 1, middle - prefix - 1);
232 buf [middle - prefix - 1] = middle [keysym_offset + 1];
233 strcpy (buf + (middle - prefix), suffix + 1);
234
235 output_string (term, buf);
236 }
237 break;
238 }
239 231
240 free (str); 232 free (str);
241 233
242 return true; 234 return true;
243 } 235 }
250keyboard_manager::setup_hash () 242keyboard_manager::setup_hash ()
251{ 243{
252 unsigned int i, index, hashkey; 244 unsigned int i, index, hashkey;
253 vector <keysym_t *> sorted_keymap; 245 vector <keysym_t *> sorted_keymap;
254 uint16_t hash_bucket_size[KEYSYM_HASH_BUCKETS]; // size of each bucket 246 uint16_t hash_bucket_size[KEYSYM_HASH_BUCKETS]; // size of each bucket
255 uint16_t hash_bucket_counter[KEYSYM_HASH_BUCKETS]; // #elements in each bucket
256 247
257 memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); 248 memset (hash_bucket_size, 0, sizeof (hash_bucket_size));
258 memset (hash_bucket_counter, 0, sizeof (hash_bucket_counter));
259 249
260 // determine hash bucket size 250 // determine hash bucket size
261 for (i = 0; i < keymap.size (); ++i) 251 for (i = 0; i < keymap.size (); ++i)
262 for (int j = min (keymap [i]->range, KEYSYM_HASH_BUCKETS) - 1; j >= 0; --j)
263 { 252 {
264 hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; 253 hashkey = keymap [i]->keysym & KEYSYM_HASH_MASK;
265 ++hash_bucket_size [hashkey]; 254 ++hash_bucket_size [hashkey];
266 } 255 }
267 256
268 // now we know the size of each bucket 257 // now we know the size of each bucket
269 // compute the index of each bucket 258 // compute the index of each bucket
270 hash [0] = 0; 259 hash [0] = 0;
271 for (index = 0, i = 1; i < KEYSYM_HASH_BUCKETS; ++i) 260 for (index = 0, i = 1; i < KEYSYM_HASH_BUCKETS; ++i)
275 } 264 }
276 265
277 // and allocate just enough space 266 // and allocate just enough space
278 sorted_keymap.insert (sorted_keymap.begin (), index + hash_bucket_size [i - 1], 0); 267 sorted_keymap.insert (sorted_keymap.begin (), index + hash_bucket_size [i - 1], 0);
279 268
269 memset (hash_bucket_size, 0, sizeof (hash_bucket_size));
270
280 // fill in sorted_keymap 271 // fill in sorted_keymap
281 // it is sorted in each bucket 272 // it is sorted in each bucket
282 for (i = 0; i < keymap.size (); ++i) 273 for (i = 0; i < keymap.size (); ++i)
283 for (int j = min (keymap [i]->range, KEYSYM_HASH_BUCKETS) - 1; j >= 0; --j)
284 { 274 {
285 hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; 275 hashkey = keymap [i]->keysym & KEYSYM_HASH_MASK;
286 276
287 index = hash [hashkey] + hash_bucket_counter [hashkey]; 277 index = hash [hashkey] + hash_bucket_size [hashkey];
288 278
289 while (index > hash [hashkey] 279 while (index > hash [hashkey]
290 && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) 280 && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0)
291 { 281 {
292 sorted_keymap [index] = sorted_keymap [index - 1]; 282 sorted_keymap [index] = sorted_keymap [index - 1];
293 --index; 283 --index;
294 } 284 }
295 285
296 sorted_keymap [index] = keymap [i]; 286 sorted_keymap [index] = keymap [i];
297 ++hash_bucket_counter [hashkey]; 287 ++hash_bucket_size [hashkey];
298 } 288 }
299 289
300 keymap.swap (sorted_keymap); 290 keymap.swap (sorted_keymap);
301 291
302#ifndef NDEBUG 292#ifndef NDEBUG
303 // check for invariants 293 // check for invariants
304 for (i = 0; i < KEYSYM_HASH_BUCKETS; ++i) 294 for (i = 0; i < KEYSYM_HASH_BUCKETS; ++i)
305 { 295 {
306 index = hash[i]; 296 index = hash[i];
307 for (int j = 0; j < hash_bucket_size [i]; ++j) 297 for (int j = 0; j < hash_bucket_size [i]; ++j)
308 { 298 {
309 if (keymap [index + j]->range == 1)
310 assert (i == (keymap [index + j]->keysym & KEYSYM_HASH_MASK)); 299 assert (i == (keymap [index + j]->keysym & KEYSYM_HASH_MASK));
311 300
312 if (j) 301 if (j)
313 assert (compare_priority (keymap [index + j - 1], 302 assert (compare_priority (keymap [index + j - 1],
314 keymap [index + j]) >= 0); 303 keymap [index + j]) >= 0);
315 } 304 }
317 306
318 // this should be able to detect most possible bugs 307 // this should be able to detect most possible bugs
319 for (i = 0; i < sorted_keymap.size (); ++i) 308 for (i = 0; i < sorted_keymap.size (); ++i)
320 { 309 {
321 keysym_t *a = sorted_keymap[i]; 310 keysym_t *a = sorted_keymap[i];
322 for (int j = 0; j < a->range; ++j)
323 {
324 int index = find_keysym (a->keysym + j, a->state); 311 int index = find_keysym (a->keysym, a->state);
325 312
326 assert (index >= 0); 313 assert (index >= 0);
327 keysym_t *b = keymap [index]; 314 keysym_t *b = keymap [index];
328 assert (i == index // the normally expected result 315 assert (i == index // the normally expected result
329 || IN_RANGE_INC (a->keysym + j, b->keysym, b->keysym + b->range) 316 || a->keysym == b->keysym
330 && compare_priority (a, b) <= 0); // is effectively the same or a closer match 317 && compare_priority (a, b) <= 0); // is effectively the same or a closer match
331 }
332 } 318 }
333#endif 319#endif
334} 320}
335 321
336int 322int
344 330
345 for (; index < end; ++index) 331 for (; index < end; ++index)
346 { 332 {
347 keysym_t *key = keymap [index]; 333 keysym_t *key = keymap [index];
348 334
349 if (key->keysym <= keysym && keysym < key->keysym + key->range 335 if (key->keysym == keysym
350 // match only the specified bits in state and ignore others 336 // match only the specified bits in state and ignore others
351 && (key->state & state) == key->state) 337 && (key->state & state) == key->state)
352 return index; 338 return index;
353 } 339 }
354 340

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines