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.3 by root, Sun Jan 16 18:48:04 2005 UTC vs.
Revision 1.7 by root, Sun Jan 16 23:55:42 2005 UTC

1#include "../config.h" 1#include "../config.h"
2#include "rxvt.h" 2#include "rxvt.h"
3
4#ifdef KEYSYM_RESOURCE
5
6#include <cstring>
7
3#include "keyboard.h" 8#include "keyboard.h"
4#include "command.h" 9#include "command.h"
5#include <string.h>
6#include <X11/X.h>
7
8#ifdef KEYSYM_RESOURCE
9 10
10//////////////////////////////////////////////////////////////////////////////// 11////////////////////////////////////////////////////////////////////////////////
11// default keycode translation map and keyevent handlers 12// default keycode translation map and keyevent handlers
12 13
13keysym_t keyboard_manager::stock_keymap[] = { 14keysym_t keyboard_manager::stock_keymap[] = {
57 *ch |= 0x80; 58 *ch |= 0x80;
58 } 59 }
59 else if (rt->meta_char == C0_ESC) /* escape prefix */ 60 else if (rt->meta_char == C0_ESC) /* escape prefix */
60#endif 61#endif
61 { 62 {
62 const unsigned char 63 const unsigned char ch = C0_ESC;
63 ch = C0_ESC;
64 rt->tt_write (&ch, 1); 64 rt->tt_write (&ch, 1);
65 } 65 }
66 } 66 }
67 67
68 rt->tt_write ((unsigned char *) buf, buflen); 68 rt->tt_write ((unsigned char *) buf, buflen);
69} 69}
70 70
71static int 71static int
72format_keyrange_string (const char *str, int keysym_offset, char *buf, int bufsize) 72format_keyrange_string (const char *str, int keysym_offset, char *buf, int bufsize)
73{ 73{
74 int len = snprintf (buf, bufsize, str + 1, keysym_offset + str [0]); 74 size_t len = snprintf (buf, bufsize, str + 1, keysym_offset + str [0]);
75 75
76 if (len >= bufsize) 76 if (len >= (size_t)bufsize)
77 {
78 fprintf (stderr, "buffer overflowed!\n");
79 buf[bufsize - 1] = '\0';
80 } 77 {
81 else if (len < 0) 78 rxvt_warn ("format_keyrange_string: formatting failed, ignoring key.\n");
82 { 79 *buf = 0;
83 perror ("keyrange_translator()");
84 } 80 }
85 81
86 return len; 82 return len;
87} 83}
88 84
89//////////////////////////////////////////////////////////////////////////////// 85////////////////////////////////////////////////////////////////////////////////
90// return: #bits of '1' 86// return: #bits of '1'
87#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3)
88# define bitcount(n) (__extension__ ({ uint32_t n__ = (n); __builtin_popcount (n); }))
89#else
91static int 90static int
92bitcount (unsigned int n) 91bitcount (uint16_t n)
93{ 92{
94 int i; 93 int i;
95 94
96 for (i = 0; n; ++i, n &= (n - 1)) 95 for (i = 0; n; ++i, n &= n - 1)
97 ; 96 ;
98 97
99 return i; 98 return i;
100} 99}
100#endif
101 101
102// return: priority_of_a - priority_of_b 102// return: priority_of_a - priority_of_b
103static int 103static int
104compare_priority (keysym_t *a, keysym_t *b) 104compare_priority (keysym_t *a, keysym_t *b)
105{ 105{
119 119
120//////////////////////////////////////////////////////////////////////////////// 120////////////////////////////////////////////////////////////////////////////////
121keyboard_manager::keyboard_manager () 121keyboard_manager::keyboard_manager ()
122{ 122{
123 keymap.reserve (256); 123 keymap.reserve (256);
124 hash[0] = 1; // hash[0] != 0 indicates uninitialized data 124 hash [0] = 1; // hash[0] != 0 indicates uninitialized data
125} 125}
126 126
127keyboard_manager::~keyboard_manager () 127keyboard_manager::~keyboard_manager ()
128{ 128{
129 clear (); 129 clear ();
161{ 161{
162 assert (trans); 162 assert (trans);
163 163
164 keysym_t *key = new keysym_t; 164 keysym_t *key = new keysym_t;
165 wchar_t *wc = rxvt_mbstowcs (trans); 165 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]);
166 const char *translation = rxvt_wcstoutf8 (wc); 167 const char *translation = rxvt_wcstoutf8 (wc);
167 free (wc); 168 free (wc);
168 169
169 if (key && translation) 170 if (key && translation)
170 { 171 {
185 key->range = suffix - middle - 1; 186 key->range = suffix - middle - 1;
186 187
187 strcpy (translation, translation + 4); 188 strcpy (translation, translation + 4);
188 } 189 }
189 else 190 else
190 {
191 key->range = 1;
192 rxvt_warn ("cannot parse list-type keysym '%s', treating as normal keysym.\n", translation); 191 rxvt_warn ("cannot parse list-type keysym '%s', treating as normal keysym.\n", translation);
193 } 192 }
194 }
195 else
196 193
197 user_keymap.push_back (key); 194 user_keymap.push_back (key);
198 user_translations.push_back (translation); 195 user_translations.push_back (translation);
199 register_keymap (key); 196 register_keymap (key);
200 } 197 }
236bool 233bool
237keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state) 234keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state)
238{ 235{
239 assert (hash[0] == 0 && "register_done() need to be called"); 236 assert (hash[0] == 0 && "register_done() need to be called");
240 237
241 if (state & term->ModMetaMask) 238 if (state & term->ModMetaMask) state |= MetaMask;
242 state |= MetaMask;
243
244 if (state & term->ModNumLockMask) 239 if (state & term->ModNumLockMask) state |= NumLockMask;
245 state |= NumLockMask; 240 if (state & term->ModLevel3Mask) state |= Level3Mask;
246 241
247 if (!!(term->priv_modes & PrivMode_aplKP) != !!(state & ShiftMask)) 242 if (!!(term->priv_modes & PrivMode_aplKP) != !!(state & ShiftMask))
248 state |= AppKeypadMask; 243 state |= AppKeypadMask;
249 244
250 int index = find_keysym (keysym, state); 245 int index = find_keysym (keysym, state);
251 246
252 if (index >= 0) 247 if (index >= 0)
253 { 248 {
254 assert (term && keymap [index]);
255 const keysym_t &key = *keymap [index]; 249 const keysym_t &key = *keymap [index];
256 250
257 int keysym_offset = keysym - key.keysym; 251 int keysym_offset = keysym - key.keysym;
258 252
259 wchar_t *wc = rxvt_utf8towcs (key.str); 253 wchar_t *wc = rxvt_utf8towcs (key.str);
254
260 char *str = rxvt_wcstombs (wc); 255 char *str = rxvt_wcstombs (wc);
261 // TODO: do translations, unescaping etc, here (allow \u escape etc.) 256 // TODO: do translations, unescaping etc, here (allow \u escape etc.)
262 free (wc); 257 free (wc);
263 258
264 switch (key.type) 259 switch (key.type)
322{ 317{
323 for (unsigned int i = 0; i < keymap.size (); ++i) 318 for (unsigned int i = 0; i < keymap.size (); ++i)
324 { 319 {
325 for (unsigned int j = 0; j < i; ++j) 320 for (unsigned int j = 0; j < i; ++j)
326 { 321 {
327 if (keymap[i] == keymap[j]) 322 if (keymap [i] == keymap [j])
328 { 323 {
329 while (keymap[i] == keymap.back ()) 324 while (keymap [i] == keymap.back ())
330 keymap.pop_back (); 325 keymap.pop_back ();
331 326
332 if (i < keymap.size ()) 327 if (i < keymap.size ())
333 { 328 {
334 keymap[i] = keymap.back (); 329 keymap[i] = keymap.back ();
352 memset (hash_budget_counter, 0, sizeof (hash_budget_counter)); 347 memset (hash_budget_counter, 0, sizeof (hash_budget_counter));
353 348
354 // count keysyms for corresponding hash budgets 349 // count keysyms for corresponding hash budgets
355 for (i = 0; i < keymap.size (); ++i) 350 for (i = 0; i < keymap.size (); ++i)
356 { 351 {
357 assert (keymap[i]); 352 assert (keymap [i]);
358 hashkey = (keymap[i]->keysym & KEYSYM_HASH_MASK); 353 hashkey = (keymap [i]->keysym & KEYSYM_HASH_MASK);
359 ++hash_budget_size[hashkey]; 354 ++hash_budget_size [hashkey];
360 } 355 }
361 356
362 // keysym A with range>1 is counted one more time for 357 // keysym A with range>1 is counted one more time for
363 // every keysym B lies in its range 358 // every keysym B lies in its range
364 for (i = 0; i < keymap.size (); ++i) 359 for (i = 0; i < keymap.size (); ++i)
365 { 360 {
366 if (keymap[i]->range > 1) 361 if (keymap[i]->range > 1)
367 { 362 {
368 for (int j = min (keymap[i]->range, KEYSYM_HASH_BUDGETS) - 1; j > 0; --j) 363 for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j > 0; --j)
369 { 364 {
370 hashkey = ((keymap[i]->keysym + j) & KEYSYM_HASH_MASK); 365 hashkey = ((keymap [i]->keysym + j) & KEYSYM_HASH_MASK);
371 if (hash_budget_size[hashkey]) 366 if (hash_budget_size [hashkey])
372 ++hash_budget_size[hashkey]; 367 ++hash_budget_size [hashkey];
373 } 368 }
374 } 369 }
375 } 370 }
376 371
377 // now we know the size of each budget 372 // now we know the size of each budget
378 // compute the index of each budget 373 // compute the index of each budget
379 hash[0] = 0; 374 hash [0] = 0;
380 for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i) 375 for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i)
381 { 376 {
382 index += hash_budget_size[i - 1]; 377 index += hash_budget_size [i - 1];
383 hash[i] = (hash_budget_size[i] ? index : hash[i - 1]); 378 hash[i] = (hash_budget_size [i] ? index : hash [i - 1]);
384 } 379 }
385 380
386 // and allocate just enough space 381 // and allocate just enough space
387 //sorted_keymap.reserve (hash[i - 1] + hash_budget_size[i - 1]); 382 //sorted_keymap.reserve (hash[i - 1] + hash_budget_size[i - 1]);
388 sorted_keymap.insert (sorted_keymap.begin (), index + hash_budget_size[i - 1], 0); 383 sorted_keymap.insert (sorted_keymap.begin (), index + hash_budget_size [i - 1], 0);
389 384
390 // fill in sorted_keymap 385 // fill in sorted_keymap
391 // it is sorted in each budget 386 // it is sorted in each budget
392 for (i = 0; i < keymap.size (); ++i) 387 for (i = 0; i < keymap.size (); ++i)
393 { 388 {
394 for (int j = min (keymap[i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j) 389 for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j)
395 { 390 {
396 hashkey = ((keymap[i]->keysym + j) & KEYSYM_HASH_MASK); 391 hashkey = ((keymap [i]->keysym + j) & KEYSYM_HASH_MASK);
397 392
398 if (hash_budget_size[hashkey]) 393 if (hash_budget_size [hashkey])
399 { 394 {
400 index = hash[hashkey] + hash_budget_counter[hashkey]; 395 index = hash [hashkey] + hash_budget_counter [hashkey];
401 396
402 while (index > hash[hashkey] 397 while (index > hash [hashkey]
403 && compare_priority (keymap[i], sorted_keymap[index - 1]) > 0) 398 && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0)
404 { 399 {
405 sorted_keymap[index] = sorted_keymap[index - 1]; 400 sorted_keymap [index] = sorted_keymap [index - 1];
406 --index; 401 --index;
407 } 402 }
408 403
409 sorted_keymap[index] = keymap[i]; 404 sorted_keymap [index] = keymap [i];
410 ++hash_budget_counter[hashkey]; 405 ++hash_budget_counter [hashkey];
411 } 406 }
412 } 407 }
413 } 408 }
414 409
415 keymap.swap (sorted_keymap); 410 keymap.swap (sorted_keymap);
417#if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD) 412#if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD)
418 // check for invariants 413 // check for invariants
419 for (i = 0; i < KEYSYM_HASH_BUDGETS; ++i) 414 for (i = 0; i < KEYSYM_HASH_BUDGETS; ++i)
420 { 415 {
421 index = hash[i]; 416 index = hash[i];
422 for (int j = 0; j < hash_budget_size[i]; ++j) 417 for (int j = 0; j < hash_budget_size [i]; ++j)
423 { 418 {
424 if (keymap[index + j]->range == 1) 419 if (keymap [index + j]->range == 1)
425 assert (i == (keymap[index + j]->keysym & KEYSYM_HASH_MASK)); 420 assert (i == (keymap [index + j]->keysym & KEYSYM_HASH_MASK));
426 421
427 if (j) 422 if (j)
428 assert (compare_priority (keymap[index + j - 1], 423 assert (compare_priority (keymap [index + j - 1],
429 keymap[index + j]) >= 0); 424 keymap [index + j]) >= 0);
430 } 425 }
431 } 426 }
432 427
433 // this should be able to detect most possible bugs 428 // this should be able to detect most possible bugs
434 for (i = 0; i < sorted_keymap.size (); ++i) 429 for (i = 0; i < sorted_keymap.size (); ++i)
435 { 430 {
436 keysym_t *a = sorted_keymap[i]; 431 keysym_t *a = sorted_keymap[i];
437 for (int j = 0; j < a->range; ++j) 432 for (int j = 0; j < a->range; ++j)
438 { 433 {
439 int index = find_keysym (a->keysym + j, a->state & OtherModMask); 434 int index = find_keysym (a->keysym + j, a->state);
435
440 assert (index >= 0); 436 assert (index >= 0);
441 keysym_t *b = keymap[index]; 437 keysym_t *b = keymap [index];
442 assert (i == (signed) index || // the normally expected result 438 assert (i == (signed) index || // the normally expected result
443 (a->keysym + j) >= b->keysym && (a->keysym + j) <= (b->keysym + b->range) && compare_priority (a, b) <= 0); // is effectively the same 439 (a->keysym + j) >= b->keysym && (a->keysym + j) <= (b->keysym + b->range) && compare_priority (a, b) <= 0); // is effectively the same
444 } 440 }
445 } 441 }
446#endif 442#endif
452 int hashkey = keysym & KEYSYM_HASH_MASK; 448 int hashkey = keysym & KEYSYM_HASH_MASK;
453 unsigned int index = hash [hashkey]; 449 unsigned int index = hash [hashkey];
454 450
455 for (; index < keymap.size (); ++index) 451 for (; index < keymap.size (); ++index)
456 { 452 {
457 keysym_t *key = keymap[index]; 453 keysym_t *key = keymap [index];
458 assert (key); 454 assert (key);
459 455
460 if (key->keysym <= keysym && key->keysym + key->range > keysym 456 if (key->keysym <= keysym && key->keysym + key->range > keysym
461 // match only the specified bits in state and ignore others 457 // match only the specified bits in state and ignore others
462 && (key->state & OtherModMask) == (key->state & state)) 458 && (key->state & state) == key->state)
463 return index; 459 return index;
464 else if (key->keysym > keysym && key->range == 1) 460 else if (key->keysym > keysym && key->range == 1)
465 return -1; 461 return -1;
466 } 462 }
467 463

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines