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.2 by root, Sun Jan 16 18:05:37 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 }
228 for (i = 0; i < n; ++i) 225 for (i = 0; i < n; ++i)
229 register_keymap (&stock_keymap[i]); 226 register_keymap (&stock_keymap[i]);
230 227
231 purge_duplicate_keymap (); 228 purge_duplicate_keymap ();
232 229
233#if TO_BE_DONE_INSIDE_dispatch
234 for (i = 0; i < keymap.size (); ++i)
235 {
236 keysym_t *key = keymap[i];
237
238 assert (bitcount (term_->ModMetaMask) == 1 && "call me after ModMetaMask was set!");
239
240 if (key->state & MetaMask)
241 {
242 //key->state &= ~MetaMask;
243 key->state |= term_->ModMetaMask;
244 }
245
246 assert (bitcount (term_->ModNumLockMask) == 1 && "call me after ModNumLockMask was set!");
247
248 if (key->state & NumLockMask)
249 {
250 //key->state &= ~NumLockMask;
251 key->state |= term_->ModNumLockMask;
252 }
253 }
254#endif
255
256 setup_hash (); 230 setup_hash ();
257} 231}
258 232
259bool 233bool
260keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state) 234keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state)
261{ 235{
262 assert (hash[0] == 0 && "register_done() need to be called"); 236 assert (hash[0] == 0 && "register_done() need to be called");
263 237
238 if (state & term->ModMetaMask) state |= MetaMask;
239 if (state & term->ModNumLockMask) state |= NumLockMask;
240 if (state & term->ModLevel3Mask) state |= Level3Mask;
241
242 if (!!(term->priv_modes & PrivMode_aplKP) != !!(state & ShiftMask))
243 state |= AppKeypadMask;
244
264 int index = find_keysym (keysym, state); 245 int index = find_keysym (keysym, state);
265 246
266 if (index >= 0) 247 if (index >= 0)
267 { 248 {
268 assert (term && keymap [index]);
269 const keysym_t &key = *keymap [index]; 249 const keysym_t &key = *keymap [index];
270 250
271 int keysym_offset = keysym - key.keysym; 251 int keysym_offset = keysym - key.keysym;
272 252
273 wchar_t *wc = rxvt_utf8towcs (key.str); 253 wchar_t *wc = rxvt_utf8towcs (key.str);
254
274 char *str = rxvt_wcstombs (wc); 255 char *str = rxvt_wcstombs (wc);
275 // TODO: do translations, unescaping etc, here (allow \u escape etc.) 256 // TODO: do translations, unescaping etc, here (allow \u escape etc.)
276 free (wc); 257 free (wc);
277 258
278 switch (key.type) 259 switch (key.type)
336{ 317{
337 for (unsigned int i = 0; i < keymap.size (); ++i) 318 for (unsigned int i = 0; i < keymap.size (); ++i)
338 { 319 {
339 for (unsigned int j = 0; j < i; ++j) 320 for (unsigned int j = 0; j < i; ++j)
340 { 321 {
341 if (keymap[i] == keymap[j]) 322 if (keymap [i] == keymap [j])
342 { 323 {
343 while (keymap[i] == keymap.back ()) 324 while (keymap [i] == keymap.back ())
344 keymap.pop_back (); 325 keymap.pop_back ();
345 326
346 if (i < keymap.size ()) 327 if (i < keymap.size ())
347 { 328 {
348 keymap[i] = keymap.back (); 329 keymap[i] = keymap.back ();
366 memset (hash_budget_counter, 0, sizeof (hash_budget_counter)); 347 memset (hash_budget_counter, 0, sizeof (hash_budget_counter));
367 348
368 // count keysyms for corresponding hash budgets 349 // count keysyms for corresponding hash budgets
369 for (i = 0; i < keymap.size (); ++i) 350 for (i = 0; i < keymap.size (); ++i)
370 { 351 {
371 assert (keymap[i]); 352 assert (keymap [i]);
372 hashkey = (keymap[i]->keysym & KEYSYM_HASH_MASK); 353 hashkey = (keymap [i]->keysym & KEYSYM_HASH_MASK);
373 ++hash_budget_size[hashkey]; 354 ++hash_budget_size [hashkey];
374 } 355 }
375 356
376 // keysym A with range>1 is counted one more time for 357 // keysym A with range>1 is counted one more time for
377 // every keysym B lies in its range 358 // every keysym B lies in its range
378 for (i = 0; i < keymap.size (); ++i) 359 for (i = 0; i < keymap.size (); ++i)
379 { 360 {
380 if (keymap[i]->range > 1) 361 if (keymap[i]->range > 1)
381 { 362 {
382 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)
383 { 364 {
384 hashkey = ((keymap[i]->keysym + j) & KEYSYM_HASH_MASK); 365 hashkey = ((keymap [i]->keysym + j) & KEYSYM_HASH_MASK);
385 if (hash_budget_size[hashkey]) 366 if (hash_budget_size [hashkey])
386 ++hash_budget_size[hashkey]; 367 ++hash_budget_size [hashkey];
387 } 368 }
388 } 369 }
389 } 370 }
390 371
391 // now we know the size of each budget 372 // now we know the size of each budget
392 // compute the index of each budget 373 // compute the index of each budget
393 hash[0] = 0; 374 hash [0] = 0;
394 for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i) 375 for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i)
395 { 376 {
396 index += hash_budget_size[i - 1]; 377 index += hash_budget_size [i - 1];
397 hash[i] = (hash_budget_size[i] ? index : hash[i - 1]); 378 hash[i] = (hash_budget_size [i] ? index : hash [i - 1]);
398 } 379 }
399 380
400 // and allocate just enough space 381 // and allocate just enough space
401 //sorted_keymap.reserve (hash[i - 1] + hash_budget_size[i - 1]); 382 //sorted_keymap.reserve (hash[i - 1] + hash_budget_size[i - 1]);
402 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);
403 384
404 // fill in sorted_keymap 385 // fill in sorted_keymap
405 // it is sorted in each budget 386 // it is sorted in each budget
406 for (i = 0; i < keymap.size (); ++i) 387 for (i = 0; i < keymap.size (); ++i)
407 { 388 {
408 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)
409 { 390 {
410 hashkey = ((keymap[i]->keysym + j) & KEYSYM_HASH_MASK); 391 hashkey = ((keymap [i]->keysym + j) & KEYSYM_HASH_MASK);
411 392
412 if (hash_budget_size[hashkey]) 393 if (hash_budget_size [hashkey])
413 { 394 {
414 index = hash[hashkey] + hash_budget_counter[hashkey]; 395 index = hash [hashkey] + hash_budget_counter [hashkey];
415 396
416 while (index > hash[hashkey] 397 while (index > hash [hashkey]
417 && compare_priority (keymap[i], sorted_keymap[index - 1]) > 0) 398 && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0)
418 { 399 {
419 sorted_keymap[index] = sorted_keymap[index - 1]; 400 sorted_keymap [index] = sorted_keymap [index - 1];
420 --index; 401 --index;
421 } 402 }
422 403
423 sorted_keymap[index] = keymap[i]; 404 sorted_keymap [index] = keymap [i];
424 ++hash_budget_counter[hashkey]; 405 ++hash_budget_counter [hashkey];
425 } 406 }
426 } 407 }
427 } 408 }
428 409
429 keymap.swap (sorted_keymap); 410 keymap.swap (sorted_keymap);
431#if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD) 412#if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD)
432 // check for invariants 413 // check for invariants
433 for (i = 0; i < KEYSYM_HASH_BUDGETS; ++i) 414 for (i = 0; i < KEYSYM_HASH_BUDGETS; ++i)
434 { 415 {
435 index = hash[i]; 416 index = hash[i];
436 for (int j = 0; j < hash_budget_size[i]; ++j) 417 for (int j = 0; j < hash_budget_size [i]; ++j)
437 { 418 {
438 if (keymap[index + j]->range == 1) 419 if (keymap [index + j]->range == 1)
439 assert (i == (keymap[index + j]->keysym & KEYSYM_HASH_MASK)); 420 assert (i == (keymap [index + j]->keysym & KEYSYM_HASH_MASK));
440 421
441 if (j) 422 if (j)
442 assert (compare_priority (keymap[index + j - 1], 423 assert (compare_priority (keymap [index + j - 1],
443 keymap[index + j]) >= 0); 424 keymap [index + j]) >= 0);
444 } 425 }
445 } 426 }
446 427
447 // this should be able to detect most possible bugs 428 // this should be able to detect most possible bugs
448 for (i = 0; i < sorted_keymap.size (); ++i) 429 for (i = 0; i < sorted_keymap.size (); ++i)
449 { 430 {
450 keysym_t *a = sorted_keymap[i]; 431 keysym_t *a = sorted_keymap[i];
451 for (int j = 0; j < a->range; ++j) 432 for (int j = 0; j < a->range; ++j)
452 { 433 {
453 int index = find_keysym (a->keysym + j, a->state & OtherModMask); 434 int index = find_keysym (a->keysym + j, a->state);
435
454 assert (index >= 0); 436 assert (index >= 0);
455 keysym_t *b = keymap[index]; 437 keysym_t *b = keymap [index];
456 assert (i == (signed) index || // the normally expected result 438 assert (i == (signed) index || // the normally expected result
457 (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
458 } 440 }
459 } 441 }
460#endif 442#endif
466 int hashkey = keysym & KEYSYM_HASH_MASK; 448 int hashkey = keysym & KEYSYM_HASH_MASK;
467 unsigned int index = hash [hashkey]; 449 unsigned int index = hash [hashkey];
468 450
469 for (; index < keymap.size (); ++index) 451 for (; index < keymap.size (); ++index)
470 { 452 {
471 keysym_t *key = keymap[index]; 453 keysym_t *key = keymap [index];
472 assert (key); 454 assert (key);
473 455
474 if (key->keysym <= keysym && key->keysym + key->range > keysym 456 if (key->keysym <= keysym && key->keysym + key->range > keysym
475 // match only the specified bits in state and ignore others 457 // match only the specified bits in state and ignore others
476 && (key->state & OtherModMask) == (key->state & state)) 458 && (key->state & state) == key->state)
477 return index; 459 return index;
478 else if (key->keysym > keysym && key->range == 1) 460 else if (key->keysym > keysym && key->range == 1)
479 return -1; 461 return -1;
480 } 462 }
481 463

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines