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.7 by root, Sun Jan 16 23:55:42 2005 UTC vs.
Revision 1.17 by root, Mon Nov 28 19:35:04 2005 UTC

6#include <cstring> 6#include <cstring>
7 7
8#include "keyboard.h" 8#include "keyboard.h"
9#include "command.h" 9#include "command.h"
10 10
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 */
42
43#if STOCK_KEYMAP
11//////////////////////////////////////////////////////////////////////////////// 44////////////////////////////////////////////////////////////////////////////////
12// default keycode translation map and keyevent handlers 45// default keycode translation map and keyevent handlers
13 46
14keysym_t keyboard_manager::stock_keymap[] = { 47keysym_t keyboard_manager::stock_keymap[] = {
15 /* examples */ 48 /* examples */
16 /* keysym, state, range, handler, str */ 49 /* keysym, state, range, handler, str */
17//{XK_ISO_Left_Tab, 0, 1, NORMAL, "\033[Z"}, 50//{XK_ISO_Left_Tab, 0, 1, keysym_t::NORMAL, "\033[Z"},
18//{ 'a', 0, 26, RANGE_META8, "a" "%c"}, 51//{ 'a', 0, 26, keysym_t::RANGE_META8, "a" "%c"},
19//{ 'a', ControlMask, 26, RANGE_META8, "" "%c"}, 52//{ 'a', ControlMask, 26, keysym_t::RANGE_META8, "" "%c"},
20//{ XK_Left, 0, 4, LIST, "DACBZ" "\033[Z"}, 53//{ XK_Left, 0, 4, keysym_t::LIST, ".\033[.DACB."},
21//{ XK_Left, ShiftMask, 4, LIST, "dacbZ" "\033[Z"}, 54//{ XK_Left, ShiftMask, 4, keysym_t::LIST, ".\033[.dacb."},
22//{ XK_Left, ControlMask, 4, LIST, "dacbZ" "\033OZ"}, 55//{ XK_Left, ControlMask, 4, keysym_t::LIST, ".\033O.dacb."},
23//{ XK_Tab, ControlMask, 1, NORMAL, "\033<C-Tab>"}, 56//{ XK_Tab, ControlMask, 1, keysym_t::NORMAL, "\033<C-Tab>"},
24//{ XK_apostrophe, ControlMask, 1, NORMAL, "\033<C-'>"}, 57//{ XK_apostrophe, ControlMask, 1, keysym_t::NORMAL, "\033<C-'>"},
25//{ XK_slash, ControlMask, 1, NORMAL, "\033<C-/>"}, 58//{ XK_slash, ControlMask, 1, keysym_t::NORMAL, "\033<C-/>"},
26//{ XK_semicolon, ControlMask, 1, NORMAL, "\033<C-;>"}, 59//{ XK_semicolon, ControlMask, 1, keysym_t::NORMAL, "\033<C-;>"},
27//{ XK_grave, ControlMask, 1, NORMAL, "\033<C-`>"}, 60//{ XK_grave, ControlMask, 1, keysym_t::NORMAL, "\033<C-`>"},
28//{ XK_comma, ControlMask, 1, NORMAL, "\033<C-\054>"}, 61//{ XK_comma, ControlMask, 1, keysym_t::NORMAL, "\033<C-\054>"},
29//{ XK_Return, ControlMask, 1, NORMAL, "\033<C-Return>"}, 62//{ XK_Return, ControlMask, 1, keysym_t::NORMAL, "\033<C-Return>"},
30//{ XK_Return, ShiftMask, 1, NORMAL, "\033<S-Return>"}, 63//{ XK_Return, ShiftMask, 1, keysym_t::NORMAL, "\033<S-Return>"},
31//{ ' ', ShiftMask, 1, NORMAL, "\033<S-Space>"}, 64//{ ' ', ShiftMask, 1, keysym_t::NORMAL, "\033<S-Space>"},
32//{ '.', ControlMask, 1, NORMAL, "\033<C-.>"}, 65//{ '.', ControlMask, 1, keysym_t::NORMAL, "\033<C-.>"},
33//{ '0', ControlMask, 10, RANGE, "0" "\033<C-%c>"}, 66//{ '0', ControlMask, 10, keysym_t::RANGE, "0" "\033<C-%c>"},
34//{ '0', MetaMask|ControlMask, 10, RANGE, "0" "\033<M-C-%c>"}, 67//{ '0', MetaMask|ControlMask, 10, keysym_t::RANGE, "0" "\033<M-C-%c>"},
35//{ 'a', MetaMask|ControlMask, 26, RANGE, "a" "\033<M-C-%c>"}, 68//{ 'a', MetaMask|ControlMask, 26, keysym_t::RANGE, "a" "\033<M-C-%c>"},
36}; 69};
70#endif
37 71
38static void 72static void
39output_string (rxvt_term *rt, const char *str) 73output_string (rxvt_term *rt, const char *str)
40{ 74{
41 assert (rt && str);
42
43 if (strncmp (str, "proto:", 6) == 0) 75 if (strncmp (str, "command:", 8) == 0)
44 rt->cmd_write ((unsigned char *)str + 6, strlen (str) - 6); 76 rt->cmd_write ((unsigned char *)str + 8, strlen (str) - 8);
45 else 77 else
46 rt->tt_write ((unsigned char *)str, strlen (str)); 78 rt->tt_write ((unsigned char *)str, strlen (str));
47} 79}
48 80
49static void 81static void
83} 115}
84 116
85//////////////////////////////////////////////////////////////////////////////// 117////////////////////////////////////////////////////////////////////////////////
86// return: #bits of '1' 118// return: #bits of '1'
87#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3) 119#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3)
88# define bitcount(n) (__extension__ ({ uint32_t n__ = (n); __builtin_popcount (n); })) 120# define bitcount(n) (__extension__ ({ uint32_t n__ = (n); __builtin_popcount (n__); }))
89#else 121#else
90static int 122static int
91bitcount (uint16_t n) 123bitcount (uint16_t n)
92{ 124{
93 int i; 125 int i;
101 133
102// return: priority_of_a - priority_of_b 134// return: priority_of_a - priority_of_b
103static int 135static int
104compare_priority (keysym_t *a, keysym_t *b) 136compare_priority (keysym_t *a, keysym_t *b)
105{ 137{
106 assert (a && b);
107
108 // (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
109 int ca = bitcount (a->state /* & OtherModMask */); 139 int ca = bitcount (a->state /* & OtherModMask */);
110 int cb = bitcount (b->state /* & OtherModMask */); 140 int cb = bitcount (b->state /* & OtherModMask */);
111 141
112 if (ca != cb) 142 if (ca != cb)
157// the string 'trans' is copied to an internal managed buffer, 187// the string 'trans' is copied to an internal managed buffer,
158// so the caller can free memory of 'trans' at any time. 188// so the caller can free memory of 'trans' at any time.
159void 189void
160keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const char *trans) 190keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const char *trans)
161{ 191{
162 assert (trans);
163
164 keysym_t *key = new keysym_t; 192 keysym_t *key = new keysym_t;
165 wchar_t *wc = rxvt_mbstowcs (trans); 193 wchar_t *wc = rxvt_mbstowcs (trans);
166printf ("CONV <%s> %x %x %x %x\n", trans, (int)wc[0], (int)wc[1], (int)wc[2], (int)wc[3]);
167 const char *translation = rxvt_wcstoutf8 (wc); 194 const char *translation = rxvt_wcstoutf8 (wc);
168 free (wc); 195 free (wc);
169 196
170 if (key && translation) 197 if (key && translation)
171 { 198 {
188 strcpy (translation, translation + 4); 215 strcpy (translation, translation + 4);
189 } 216 }
190 else 217 else
191 rxvt_warn ("cannot parse list-type keysym '%s', treating as normal keysym.\n", translation); 218 rxvt_warn ("cannot parse list-type keysym '%s', treating as normal keysym.\n", translation);
192 } 219 }
220 else if (strncmp (translation, "builtin:", 8) == 0)
221 key->type = keysym_t::BUILTIN;
193 222
194 user_keymap.push_back (key); 223 user_keymap.push_back (key);
195 user_translations.push_back (translation); 224 user_translations.push_back (translation);
196 register_keymap (key); 225 register_keymap (key);
197 } 226 }
204} 233}
205 234
206void 235void
207keyboard_manager::register_keymap (keysym_t *key) 236keyboard_manager::register_keymap (keysym_t *key)
208{ 237{
209 assert (key);
210 assert (key->range >= 1);
211
212 if (keymap.size () == keymap.capacity ()) 238 if (keymap.size () == keymap.capacity ())
213 keymap.reserve (keymap.size () * 2); 239 keymap.reserve (keymap.size () * 2);
214 240
215 keymap.push_back (key); 241 keymap.push_back (key);
216 hash[0] = 3; 242 hash[0] = 3;
217} 243}
218 244
219void 245void
220keyboard_manager::register_done () 246keyboard_manager::register_done ()
221{ 247{
248#if STOCK_KEYMAP
222 unsigned int i, n = sizeof (stock_keymap) / sizeof (keysym_t); 249 int n = sizeof (stock_keymap) / sizeof (keysym_t);
223 250
251 //TODO: shield against repeated calls and empty keymap
224 if (keymap.back () != &stock_keymap[n - 1]) 252 //if (keymap.back () != &stock_keymap[n - 1])
225 for (i = 0; i < n; ++i) 253 for (int i = 0; i < n; ++i)
226 register_keymap (&stock_keymap[i]); 254 register_keymap (&stock_keymap[i]);
255#endif
227 256
228 purge_duplicate_keymap (); 257 purge_duplicate_keymap ();
229 258
230 setup_hash (); 259 setup_hash ();
231} 260}
232 261
233bool 262bool
234keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state) 263keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state)
235{ 264{
236 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
237 268
238 if (state & term->ModMetaMask) state |= MetaMask; 269 if (state & term->ModMetaMask) state |= MetaMask;
239 if (state & term->ModNumLockMask) state |= NumLockMask; 270 if (state & term->ModNumLockMask) state |= NumLockMask;
240 if (state & term->ModLevel3Mask) state |= Level3Mask; 271 if (state & term->ModLevel3Mask) state |= Level3Mask;
241 272
246 277
247 if (index >= 0) 278 if (index >= 0)
248 { 279 {
249 const keysym_t &key = *keymap [index]; 280 const keysym_t &key = *keymap [index];
250 281
282 if (key.type != keysym_t::BUILTIN)
283 {
251 int keysym_offset = keysym - key.keysym; 284 int keysym_offset = keysym - key.keysym;
252 285
253 wchar_t *wc = rxvt_utf8towcs (key.str); 286 wchar_t *wc = rxvt_utf8towcs (key.str);
254
255 char *str = rxvt_wcstombs (wc); 287 char *str = rxvt_wcstombs (wc);
256 // TODO: do translations, unescaping etc, here (allow \u escape etc.) 288 // TODO: do (some) translations, unescaping etc, here (allow \u escape etc.)
257 free (wc); 289 free (wc);
258 290
259 switch (key.type) 291 switch (key.type)
260 {
261 case keysym_t::NORMAL:
262 output_string (term, str);
263 break;
264
265 case keysym_t::RANGE:
266 { 292 {
293 case keysym_t::NORMAL:
294 output_string (term, str);
295 break;
296
297 case keysym_t::RANGE:
298 {
267 char buf[STRING_MAX]; 299 char buf[STRING_MAX];
268 300
269 if (format_keyrange_string (str, keysym_offset, buf, sizeof (buf)) > 0) 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
270 output_string (term, buf); 331 output_string (term, buf);
332 }
333 break;
271 } 334 }
272 break;
273 335
274 case keysym_t::RANGE_META8:
275 {
276 int len;
277 char buf[STRING_MAX];
278
279 len = format_keyrange_string (str, keysym_offset, buf, sizeof (buf));
280 if (len > 0)
281 output_string_meta8 (term, state, buf, len);
282 }
283 break;
284
285 case keysym_t::LIST:
286 {
287 char buf[STRING_MAX];
288
289 char *prefix, *middle, *suffix;
290
291 prefix = str;
292 middle = strchr (prefix + 1, *prefix);
293 suffix = strrchr (middle + 1, *prefix);
294
295 memcpy (buf, prefix + 1, middle - prefix - 1);
296 buf [middle - prefix - 1] = middle [keysym_offset + 1];
297 strcpy (buf + (middle - prefix), suffix + 1);
298
299 output_string (term, buf);
300 }
301 break;
302 }
303
304 free (str); 336 free (str);
305 337
306 return true; 338 return true;
307 } 339 }
308 else
309 { 340 }
310 // fprintf(stderr,"[%x:%x]",state,keysym); 341
311 return false; 342 return false;
312 }
313} 343}
314 344
315// purge duplicate keymap entries 345// purge duplicate keymap entries
316void keyboard_manager::purge_duplicate_keymap () 346void keyboard_manager::purge_duplicate_keymap ()
317{ 347{
327 if (i < keymap.size ()) 357 if (i < keymap.size ())
328 { 358 {
329 keymap[i] = keymap.back (); 359 keymap[i] = keymap.back ();
330 keymap.pop_back (); 360 keymap.pop_back ();
331 } 361 }
362
332 break; 363 break;
333 } 364 }
334 } 365 }
335 } 366 }
336} 367}
344 uint16_t hash_budget_counter[KEYSYM_HASH_BUDGETS]; // #elements in each budget 375 uint16_t hash_budget_counter[KEYSYM_HASH_BUDGETS]; // #elements in each budget
345 376
346 memset (hash_budget_size, 0, sizeof (hash_budget_size)); 377 memset (hash_budget_size, 0, sizeof (hash_budget_size));
347 memset (hash_budget_counter, 0, sizeof (hash_budget_counter)); 378 memset (hash_budget_counter, 0, sizeof (hash_budget_counter));
348 379
349 // count keysyms for corresponding hash budgets 380 // determine hash bucket size
350 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)
351 { 383 {
352 assert (keymap [i]);
353 hashkey = (keymap [i]->keysym & KEYSYM_HASH_MASK); 384 hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK;
354 ++hash_budget_size [hashkey]; 385 ++hash_budget_size [hashkey];
355 }
356
357 // keysym A with range>1 is counted one more time for
358 // every keysym B lies in its range
359 for (i = 0; i < keymap.size (); ++i)
360 {
361 if (keymap[i]->range > 1)
362 {
363 for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j > 0; --j)
364 {
365 hashkey = ((keymap [i]->keysym + j) & KEYSYM_HASH_MASK);
366 if (hash_budget_size [hashkey])
367 ++hash_budget_size [hashkey];
368 }
369 } 386 }
370 }
371 387
372 // now we know the size of each budget 388 // now we know the size of each budget
373 // compute the index of each budget 389 // compute the index of each budget
374 hash [0] = 0; 390 hash [0] = 0;
375 for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i) 391 for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i)
376 { 392 {
377 index += hash_budget_size [i - 1]; 393 index += hash_budget_size [i - 1];
378 hash[i] = (hash_budget_size [i] ? index : hash [i - 1]); 394 hash [i] = index;
379 } 395 }
380 396
381 // and allocate just enough space 397 // and allocate just enough space
382 //sorted_keymap.reserve (hash[i - 1] + hash_budget_size[i - 1]);
383 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);
384 399
385 // fill in sorted_keymap 400 // fill in sorted_keymap
386 // it is sorted in each budget 401 // it is sorted in each budget
387 for (i = 0; i < keymap.size (); ++i) 402 for (i = 0; i < keymap.size (); ++i)
388 {
389 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)
390 { 404 {
391 hashkey = ((keymap [i]->keysym + j) & KEYSYM_HASH_MASK); 405 hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK;
392 406
393 if (hash_budget_size [hashkey])
394 {
395 index = hash [hashkey] + hash_budget_counter [hashkey]; 407 index = hash [hashkey] + hash_budget_counter [hashkey];
396 408
397 while (index > hash [hashkey] 409 while (index > hash [hashkey]
398 && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) 410 && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0)
399 { 411 {
400 sorted_keymap [index] = sorted_keymap [index - 1]; 412 sorted_keymap [index] = sorted_keymap [index - 1];
401 --index; 413 --index;
402 } 414 }
403 415
404 sorted_keymap [index] = keymap [i]; 416 sorted_keymap [index] = keymap [i];
405 ++hash_budget_counter [hashkey]; 417 ++hash_budget_counter [hashkey];
406 }
407 } 418 }
408 }
409 419
410 keymap.swap (sorted_keymap); 420 keymap.swap (sorted_keymap);
411 421
412#if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD) 422#if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD)
413 // check for invariants 423 // check for invariants
434 int index = find_keysym (a->keysym + j, a->state); 444 int index = find_keysym (a->keysym + j, a->state);
435 445
436 assert (index >= 0); 446 assert (index >= 0);
437 keysym_t *b = keymap [index]; 447 keysym_t *b = keymap [index];
438 assert (i == (signed) index || // the normally expected result 448 assert (i == (signed) index || // the normally expected result
439 (a->keysym + j) >= b->keysym && (a->keysym + j) <= (b->keysym + b->range) && compare_priority (a, b) <= 0); // is effectively the same 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
440 } 450 }
441 } 451 }
442#endif 452#endif
443} 453}
444 454
445int 455int
446keyboard_manager::find_keysym (KeySym keysym, unsigned int state) 456keyboard_manager::find_keysym (KeySym keysym, unsigned int state)
447{ 457{
448 int hashkey = keysym & KEYSYM_HASH_MASK; 458 int hashkey = keysym & KEYSYM_HASH_MASK;
449 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 ();
450 463
451 for (; index < keymap.size (); ++index) 464 for (; index < end; ++index)
452 { 465 {
453 keysym_t *key = keymap [index]; 466 keysym_t *key = keymap [index];
454 assert (key);
455 467
456 if (key->keysym <= keysym && key->keysym + key->range > keysym 468 if (key->keysym <= keysym && keysym < key->keysym + key->range
457 // match only the specified bits in state and ignore others 469 // match only the specified bits in state and ignore others
458 && (key->state & state) == key->state) 470 && (key->state & state) == key->state)
459 return index; 471 return index;
460 else if (key->keysym > keysym && key->range == 1)
461 return -1;
462 } 472 }
463 473
464 return -1; 474 return -1;
465} 475}
466 476

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines