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.8 by root, Mon Jan 17 00:07:26 2005 UTC vs.
Revision 1.11 by root, Sun Feb 6 15:18:01 2005 UTC

11//////////////////////////////////////////////////////////////////////////////// 11////////////////////////////////////////////////////////////////////////////////
12// default keycode translation map and keyevent handlers 12// default keycode translation map and keyevent handlers
13 13
14keysym_t keyboard_manager::stock_keymap[] = { 14keysym_t keyboard_manager::stock_keymap[] = {
15 /* examples */ 15 /* examples */
16 /* keysym, state, range, handler, str */ 16 /* keysym, state, range, handler, str */
17//{XK_ISO_Left_Tab, 0, 1, NORMAL, "\033[Z"}, 17//{XK_ISO_Left_Tab, 0, 1, keysym_t::NORMAL, "\033[Z"},
18//{ 'a', 0, 26, RANGE_META8, "a" "%c"}, 18//{ 'a', 0, 26, keysym_t::RANGE_META8, "a" "%c"},
19//{ 'a', ControlMask, 26, RANGE_META8, "" "%c"}, 19//{ 'a', ControlMask, 26, keysym_t::RANGE_META8, "" "%c"},
20//{ XK_Left, 0, 4, LIST, "DACBZ" "\033[Z"}, 20//{ XK_Left, 0, 4, keysym_t::LIST, ".\033[.DACB."},
21//{ XK_Left, ShiftMask, 4, LIST, "dacbZ" "\033[Z"}, 21//{ XK_Left, ShiftMask, 4, keysym_t::LIST, ".\033[.dacb."},
22//{ XK_Left, ControlMask, 4, LIST, "dacbZ" "\033OZ"}, 22//{ XK_Left, ControlMask, 4, keysym_t::LIST, ".\033O.dacb."},
23//{ XK_Tab, ControlMask, 1, NORMAL, "\033<C-Tab>"}, 23//{ XK_Tab, ControlMask, 1, keysym_t::NORMAL, "\033<C-Tab>"},
24//{ XK_apostrophe, ControlMask, 1, NORMAL, "\033<C-'>"}, 24//{ XK_apostrophe, ControlMask, 1, keysym_t::NORMAL, "\033<C-'>"},
25//{ XK_slash, ControlMask, 1, NORMAL, "\033<C-/>"}, 25//{ XK_slash, ControlMask, 1, keysym_t::NORMAL, "\033<C-/>"},
26//{ XK_semicolon, ControlMask, 1, NORMAL, "\033<C-;>"}, 26//{ XK_semicolon, ControlMask, 1, keysym_t::NORMAL, "\033<C-;>"},
27//{ XK_grave, ControlMask, 1, NORMAL, "\033<C-`>"}, 27//{ XK_grave, ControlMask, 1, keysym_t::NORMAL, "\033<C-`>"},
28//{ XK_comma, ControlMask, 1, NORMAL, "\033<C-\054>"}, 28//{ XK_comma, ControlMask, 1, keysym_t::NORMAL, "\033<C-\054>"},
29//{ XK_Return, ControlMask, 1, NORMAL, "\033<C-Return>"}, 29//{ XK_Return, ControlMask, 1, keysym_t::NORMAL, "\033<C-Return>"},
30//{ XK_Return, ShiftMask, 1, NORMAL, "\033<S-Return>"}, 30//{ XK_Return, ShiftMask, 1, keysym_t::NORMAL, "\033<S-Return>"},
31//{ ' ', ShiftMask, 1, NORMAL, "\033<S-Space>"}, 31//{ ' ', ShiftMask, 1, keysym_t::NORMAL, "\033<S-Space>"},
32//{ '.', ControlMask, 1, NORMAL, "\033<C-.>"}, 32//{ '.', ControlMask, 1, keysym_t::NORMAL, "\033<C-.>"},
33//{ '0', ControlMask, 10, RANGE, "0" "\033<C-%c>"}, 33//{ '0', ControlMask, 10, keysym_t::RANGE, "0" "\033<C-%c>"},
34//{ '0', MetaMask|ControlMask, 10, RANGE, "0" "\033<M-C-%c>"}, 34//{ '0', MetaMask|ControlMask, 10, keysym_t::RANGE, "0" "\033<M-C-%c>"},
35//{ 'a', MetaMask|ControlMask, 26, RANGE, "a" "\033<M-C-%c>"}, 35//{ 'a', MetaMask|ControlMask, 26, keysym_t::RANGE, "a" "\033<M-C-%c>"},
36}; 36};
37 37
38static void 38static void
39output_string (rxvt_term *rt, const char *str) 39output_string (rxvt_term *rt, const char *str)
40{ 40{
41 if (strncmp (str, "proto:", 6) == 0) 41 if (strncmp (str, "command:", 8) == 0)
42 rt->cmd_write ((unsigned char *)str + 6, strlen (str) - 6); 42 rt->cmd_write ((unsigned char *)str + 8, strlen (str) - 8);
43 else 43 else
44 rt->tt_write ((unsigned char *)str, strlen (str)); 44 rt->tt_write ((unsigned char *)str, strlen (str));
45} 45}
46 46
47static void 47static void
239 const keysym_t &key = *keymap [index]; 239 const keysym_t &key = *keymap [index];
240 240
241 int keysym_offset = keysym - key.keysym; 241 int keysym_offset = keysym - key.keysym;
242 242
243 wchar_t *wc = rxvt_utf8towcs (key.str); 243 wchar_t *wc = rxvt_utf8towcs (key.str);
244
245 char *str = rxvt_wcstombs (wc); 244 char *str = rxvt_wcstombs (wc);
246 // TODO: do translations, unescaping etc, here (allow \u escape etc.) 245 // TODO: do (some) translations, unescaping etc, here (allow \u escape etc.)
247 free (wc); 246 free (wc);
248 247
249 switch (key.type) 248 switch (key.type)
250 { 249 {
251 case keysym_t::NORMAL: 250 case keysym_t::NORMAL:
294 free (str); 293 free (str);
295 294
296 return true; 295 return true;
297 } 296 }
298 else 297 else
299 {
300 // fprintf(stderr,"[%x:%x]",state,keysym);
301 return false; 298 return false;
302 }
303} 299}
304 300
305// purge duplicate keymap entries 301// purge duplicate keymap entries
306void keyboard_manager::purge_duplicate_keymap () 302void keyboard_manager::purge_duplicate_keymap ()
307{ 303{
317 if (i < keymap.size ()) 313 if (i < keymap.size ())
318 { 314 {
319 keymap[i] = keymap.back (); 315 keymap[i] = keymap.back ();
320 keymap.pop_back (); 316 keymap.pop_back ();
321 } 317 }
318
322 break; 319 break;
323 } 320 }
324 } 321 }
325 } 322 }
326} 323}
334 uint16_t hash_budget_counter[KEYSYM_HASH_BUDGETS]; // #elements in each budget 331 uint16_t hash_budget_counter[KEYSYM_HASH_BUDGETS]; // #elements in each budget
335 332
336 memset (hash_budget_size, 0, sizeof (hash_budget_size)); 333 memset (hash_budget_size, 0, sizeof (hash_budget_size));
337 memset (hash_budget_counter, 0, sizeof (hash_budget_counter)); 334 memset (hash_budget_counter, 0, sizeof (hash_budget_counter));
338 335
339 // count keysyms for corresponding hash budgets 336 // determine hash bucket size
340 for (i = 0; i < keymap.size (); ++i) 337 for (i = 0; i < keymap.size (); ++i)
338 for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j)
341 { 339 {
342 hashkey = keymap [i]->keysym & KEYSYM_HASH_MASK; 340 hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK;
343 ++hash_budget_size [hashkey]; 341 ++hash_budget_size [hashkey];
344 }
345
346 // a keysym_t with range>1 is counted one more time for every keysym that
347 // lies in its range
348 for (i = 0; i < keymap.size (); ++i)
349 {
350 if (keymap[i]->range > 1)
351 {
352 for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j > 0; --j)
353 {
354 hashkey = ((keymap [i]->keysym + j) & KEYSYM_HASH_MASK);
355 if (hash_budget_size [hashkey])
356 ++hash_budget_size [hashkey];
357 }
358 } 342 }
359 }
360 343
361 // now we know the size of each budget 344 // now we know the size of each budget
362 // compute the index of each budget 345 // compute the index of each budget
363 hash [0] = 0; 346 hash [0] = 0;
364 for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i) 347 for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i)
365 { 348 {
366 index += hash_budget_size [i - 1]; 349 index += hash_budget_size [i - 1];
367 hash[i] = (hash_budget_size [i] ? index : hash [i - 1]); 350 hash [i] = index;
368 } 351 }
369 352
370 // and allocate just enough space 353 // and allocate just enough space
371 //sorted_keymap.reserve (hash[i - 1] + hash_budget_size[i - 1]);
372 sorted_keymap.insert (sorted_keymap.begin (), index + hash_budget_size [i - 1], 0); 354 sorted_keymap.insert (sorted_keymap.begin (), index + hash_budget_size [i - 1], 0);
373 355
374 // fill in sorted_keymap 356 // fill in sorted_keymap
375 // it is sorted in each budget 357 // it is sorted in each budget
376 for (i = 0; i < keymap.size (); ++i) 358 for (i = 0; i < keymap.size (); ++i)
377 {
378 for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j) 359 for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j)
379 { 360 {
380 hashkey = ((keymap [i]->keysym + j) & KEYSYM_HASH_MASK); 361 hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK;
381 362
382 if (hash_budget_size [hashkey])
383 {
384 index = hash [hashkey] + hash_budget_counter [hashkey]; 363 index = hash [hashkey] + hash_budget_counter [hashkey];
385 364
386 while (index > hash [hashkey] 365 while (index > hash [hashkey]
387 && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) 366 && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0)
388 { 367 {
389 sorted_keymap [index] = sorted_keymap [index - 1]; 368 sorted_keymap [index] = sorted_keymap [index - 1];
390 --index; 369 --index;
391 } 370 }
392 371
393 sorted_keymap [index] = keymap [i]; 372 sorted_keymap [index] = keymap [i];
394 ++hash_budget_counter [hashkey]; 373 ++hash_budget_counter [hashkey];
395 }
396 } 374 }
397 }
398 375
399 keymap.swap (sorted_keymap); 376 keymap.swap (sorted_keymap);
400 377
401#if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD) 378#if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD)
402 // check for invariants 379 // check for invariants
423 int index = find_keysym (a->keysym + j, a->state); 400 int index = find_keysym (a->keysym + j, a->state);
424 401
425 assert (index >= 0); 402 assert (index >= 0);
426 keysym_t *b = keymap [index]; 403 keysym_t *b = keymap [index];
427 assert (i == (signed) index || // the normally expected result 404 assert (i == (signed) index || // the normally expected result
428 (a->keysym + j) >= b->keysym && (a->keysym + j) <= (b->keysym + b->range) && compare_priority (a, b) <= 0); // is effectively the same 405 (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
429 } 406 }
430 } 407 }
431#endif 408#endif
432} 409}
433 410
434int 411int
435keyboard_manager::find_keysym (KeySym keysym, unsigned int state) 412keyboard_manager::find_keysym (KeySym keysym, unsigned int state)
436{ 413{
437 int hashkey = keysym & KEYSYM_HASH_MASK; 414 int hashkey = keysym & KEYSYM_HASH_MASK;
438 unsigned int index = hash [hashkey]; 415 unsigned int index = hash [hashkey];
416 unsigned int end = hashkey < KEYSYM_HASH_BUDGETS - 1
417 ? hash [hashkey + 1]
418 : keymap.size ();
439 419
440 for (; index < keymap.size (); ++index) 420 for (; index < end; ++index)
441 { 421 {
442 keysym_t *key = keymap [index]; 422 keysym_t *key = keymap [index];
443 423
444 if (key->keysym <= keysym && key->keysym + key->range > keysym 424 if (key->keysym <= keysym && keysym < key->keysym + key->range
445 // match only the specified bits in state and ignore others 425 // match only the specified bits in state and ignore others
446 && (key->state & state) == key->state) 426 && (key->state & state) == key->state)
447 return index; 427 return index;
448 else if (key->keysym > keysym && key->range == 1)
449 return -1;
450 } 428 }
451 429
452 return -1; 430 return -1;
453} 431}
454 432

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines