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.8 by root, Mon Jan 17 00:07:26 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[] = {
35}; 36};
36 37
37static void 38static void
38output_string (rxvt_term *rt, const char *str) 39output_string (rxvt_term *rt, const char *str)
39{ 40{
40 assert (rt && str);
41
42 if (strncmp (str, "proto:", 6) == 0) 41 if (strncmp (str, "proto:", 6) == 0)
43 rt->cmd_write ((unsigned char *)str + 6, strlen (str) - 6); 42 rt->cmd_write ((unsigned char *)str + 6, strlen (str) - 6);
44 else 43 else
45 rt->tt_write ((unsigned char *)str, strlen (str)); 44 rt->tt_write ((unsigned char *)str, strlen (str));
46} 45}
57 *ch |= 0x80; 56 *ch |= 0x80;
58 } 57 }
59 else if (rt->meta_char == C0_ESC) /* escape prefix */ 58 else if (rt->meta_char == C0_ESC) /* escape prefix */
60#endif 59#endif
61 { 60 {
62 const unsigned char 61 const unsigned char ch = C0_ESC;
63 ch = C0_ESC;
64 rt->tt_write (&ch, 1); 62 rt->tt_write (&ch, 1);
65 } 63 }
66 } 64 }
67 65
68 rt->tt_write ((unsigned char *) buf, buflen); 66 rt->tt_write ((unsigned char *) buf, buflen);
69} 67}
70 68
71static int 69static int
72format_keyrange_string (const char *str, int keysym_offset, char *buf, int bufsize) 70format_keyrange_string (const char *str, int keysym_offset, char *buf, int bufsize)
73{ 71{
74 int len = snprintf (buf, bufsize, str + 1, keysym_offset + str [0]); 72 size_t len = snprintf (buf, bufsize, str + 1, keysym_offset + str [0]);
75 73
76 if (len >= bufsize) 74 if (len >= (size_t)bufsize)
77 {
78 fprintf (stderr, "buffer overflowed!\n");
79 buf[bufsize - 1] = '\0';
80 } 75 {
81 else if (len < 0) 76 rxvt_warn ("format_keyrange_string: formatting failed, ignoring key.\n");
82 { 77 *buf = 0;
83 perror ("keyrange_translator()");
84 } 78 }
85 79
86 return len; 80 return len;
87} 81}
88 82
89//////////////////////////////////////////////////////////////////////////////// 83////////////////////////////////////////////////////////////////////////////////
90// return: #bits of '1' 84// return: #bits of '1'
85#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3)
86# define bitcount(n) (__extension__ ({ uint32_t n__ = (n); __builtin_popcount (n); }))
87#else
91static int 88static int
92bitcount (unsigned int n) 89bitcount (uint16_t n)
93{ 90{
94 int i; 91 int i;
95 92
96 for (i = 0; n; ++i, n &= (n - 1)) 93 for (i = 0; n; ++i, n &= n - 1)
97 ; 94 ;
98 95
99 return i; 96 return i;
100} 97}
98#endif
101 99
102// return: priority_of_a - priority_of_b 100// return: priority_of_a - priority_of_b
103static int 101static int
104compare_priority (keysym_t *a, keysym_t *b) 102compare_priority (keysym_t *a, keysym_t *b)
105{ 103{
106 assert (a && b);
107
108 // (the more '1's in state; the less range): the greater priority 104 // (the more '1's in state; the less range): the greater priority
109 int ca = bitcount (a->state /* & OtherModMask */); 105 int ca = bitcount (a->state /* & OtherModMask */);
110 int cb = bitcount (b->state /* & OtherModMask */); 106 int cb = bitcount (b->state /* & OtherModMask */);
111 107
112 if (ca != cb) 108 if (ca != cb)
119 115
120//////////////////////////////////////////////////////////////////////////////// 116////////////////////////////////////////////////////////////////////////////////
121keyboard_manager::keyboard_manager () 117keyboard_manager::keyboard_manager ()
122{ 118{
123 keymap.reserve (256); 119 keymap.reserve (256);
124 hash[0] = 1; // hash[0] != 0 indicates uninitialized data 120 hash [0] = 1; // hash[0] != 0 indicates uninitialized data
125} 121}
126 122
127keyboard_manager::~keyboard_manager () 123keyboard_manager::~keyboard_manager ()
128{ 124{
129 clear (); 125 clear ();
157// the string 'trans' is copied to an internal managed buffer, 153// the string 'trans' is copied to an internal managed buffer,
158// so the caller can free memory of 'trans' at any time. 154// so the caller can free memory of 'trans' at any time.
159void 155void
160keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const char *trans) 156keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const char *trans)
161{ 157{
162 assert (trans);
163
164 keysym_t *key = new keysym_t; 158 keysym_t *key = new keysym_t;
165 wchar_t *wc = rxvt_mbstowcs (trans); 159 wchar_t *wc = rxvt_mbstowcs (trans);
166 const char *translation = rxvt_wcstoutf8 (wc); 160 const char *translation = rxvt_wcstoutf8 (wc);
167 free (wc); 161 free (wc);
168 162
185 key->range = suffix - middle - 1; 179 key->range = suffix - middle - 1;
186 180
187 strcpy (translation, translation + 4); 181 strcpy (translation, translation + 4);
188 } 182 }
189 else 183 else
190 {
191 key->range = 1;
192 rxvt_warn ("cannot parse list-type keysym '%s', treating as normal keysym.\n", translation); 184 rxvt_warn ("cannot parse list-type keysym '%s', treating as normal keysym.\n", translation);
193 } 185 }
194 }
195 else
196 186
197 user_keymap.push_back (key); 187 user_keymap.push_back (key);
198 user_translations.push_back (translation); 188 user_translations.push_back (translation);
199 register_keymap (key); 189 register_keymap (key);
200 } 190 }
207} 197}
208 198
209void 199void
210keyboard_manager::register_keymap (keysym_t *key) 200keyboard_manager::register_keymap (keysym_t *key)
211{ 201{
212 assert (key);
213 assert (key->range >= 1);
214
215 if (keymap.size () == keymap.capacity ()) 202 if (keymap.size () == keymap.capacity ())
216 keymap.reserve (keymap.size () * 2); 203 keymap.reserve (keymap.size () * 2);
217 204
218 keymap.push_back (key); 205 keymap.push_back (key);
219 hash[0] = 3; 206 hash[0] = 3;
228 for (i = 0; i < n; ++i) 215 for (i = 0; i < n; ++i)
229 register_keymap (&stock_keymap[i]); 216 register_keymap (&stock_keymap[i]);
230 217
231 purge_duplicate_keymap (); 218 purge_duplicate_keymap ();
232 219
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 (); 220 setup_hash ();
257} 221}
258 222
259bool 223bool
260keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state) 224keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state)
261{ 225{
262 assert (hash[0] == 0 && "register_done() need to be called"); 226 assert (hash[0] == 0 && "register_done() need to be called");
263 227
228 if (state & term->ModMetaMask) state |= MetaMask;
229 if (state & term->ModNumLockMask) state |= NumLockMask;
230 if (state & term->ModLevel3Mask) state |= Level3Mask;
231
232 if (!!(term->priv_modes & PrivMode_aplKP) != !!(state & ShiftMask))
233 state |= AppKeypadMask;
234
264 int index = find_keysym (keysym, state); 235 int index = find_keysym (keysym, state);
265 236
266 if (index >= 0) 237 if (index >= 0)
267 { 238 {
268 assert (term && keymap [index]);
269 const keysym_t &key = *keymap [index]; 239 const keysym_t &key = *keymap [index];
270 240
271 int keysym_offset = keysym - key.keysym; 241 int keysym_offset = keysym - key.keysym;
272 242
273 wchar_t *wc = rxvt_utf8towcs (key.str); 243 wchar_t *wc = rxvt_utf8towcs (key.str);
244
274 char *str = rxvt_wcstombs (wc); 245 char *str = rxvt_wcstombs (wc);
275 // TODO: do translations, unescaping etc, here (allow \u escape etc.) 246 // TODO: do translations, unescaping etc, here (allow \u escape etc.)
276 free (wc); 247 free (wc);
277 248
278 switch (key.type) 249 switch (key.type)
336{ 307{
337 for (unsigned int i = 0; i < keymap.size (); ++i) 308 for (unsigned int i = 0; i < keymap.size (); ++i)
338 { 309 {
339 for (unsigned int j = 0; j < i; ++j) 310 for (unsigned int j = 0; j < i; ++j)
340 { 311 {
341 if (keymap[i] == keymap[j]) 312 if (keymap [i] == keymap [j])
342 { 313 {
343 while (keymap[i] == keymap.back ()) 314 while (keymap [i] == keymap.back ())
344 keymap.pop_back (); 315 keymap.pop_back ();
345 316
346 if (i < keymap.size ()) 317 if (i < keymap.size ())
347 { 318 {
348 keymap[i] = keymap.back (); 319 keymap[i] = keymap.back ();
366 memset (hash_budget_counter, 0, sizeof (hash_budget_counter)); 337 memset (hash_budget_counter, 0, sizeof (hash_budget_counter));
367 338
368 // count keysyms for corresponding hash budgets 339 // count keysyms for corresponding hash budgets
369 for (i = 0; i < keymap.size (); ++i) 340 for (i = 0; i < keymap.size (); ++i)
370 { 341 {
371 assert (keymap[i]);
372 hashkey = (keymap[i]->keysym & KEYSYM_HASH_MASK); 342 hashkey = keymap [i]->keysym & KEYSYM_HASH_MASK;
373 ++hash_budget_size[hashkey]; 343 ++hash_budget_size [hashkey];
374 } 344 }
375 345
376 // keysym A with range>1 is counted one more time for 346 // a keysym_t with range>1 is counted one more time for every keysym that
377 // every keysym B lies in its range 347 // lies in its range
378 for (i = 0; i < keymap.size (); ++i) 348 for (i = 0; i < keymap.size (); ++i)
379 { 349 {
380 if (keymap[i]->range > 1) 350 if (keymap[i]->range > 1)
381 { 351 {
382 for (int j = min (keymap[i]->range, KEYSYM_HASH_BUDGETS) - 1; j > 0; --j) 352 for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j > 0; --j)
383 { 353 {
384 hashkey = ((keymap[i]->keysym + j) & KEYSYM_HASH_MASK); 354 hashkey = ((keymap [i]->keysym + j) & KEYSYM_HASH_MASK);
385 if (hash_budget_size[hashkey]) 355 if (hash_budget_size [hashkey])
386 ++hash_budget_size[hashkey]; 356 ++hash_budget_size [hashkey];
387 } 357 }
388 } 358 }
389 } 359 }
390 360
391 // now we know the size of each budget 361 // now we know the size of each budget
392 // compute the index of each budget 362 // compute the index of each budget
393 hash[0] = 0; 363 hash [0] = 0;
394 for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i) 364 for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i)
395 { 365 {
396 index += hash_budget_size[i - 1]; 366 index += hash_budget_size [i - 1];
397 hash[i] = (hash_budget_size[i] ? index : hash[i - 1]); 367 hash[i] = (hash_budget_size [i] ? index : hash [i - 1]);
398 } 368 }
399 369
400 // and allocate just enough space 370 // and allocate just enough space
401 //sorted_keymap.reserve (hash[i - 1] + hash_budget_size[i - 1]); 371 //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); 372 sorted_keymap.insert (sorted_keymap.begin (), index + hash_budget_size [i - 1], 0);
403 373
404 // fill in sorted_keymap 374 // fill in sorted_keymap
405 // it is sorted in each budget 375 // it is sorted in each budget
406 for (i = 0; i < keymap.size (); ++i) 376 for (i = 0; i < keymap.size (); ++i)
407 { 377 {
408 for (int j = min (keymap[i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j) 378 for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j)
409 { 379 {
410 hashkey = ((keymap[i]->keysym + j) & KEYSYM_HASH_MASK); 380 hashkey = ((keymap [i]->keysym + j) & KEYSYM_HASH_MASK);
411 381
412 if (hash_budget_size[hashkey]) 382 if (hash_budget_size [hashkey])
413 { 383 {
414 index = hash[hashkey] + hash_budget_counter[hashkey]; 384 index = hash [hashkey] + hash_budget_counter [hashkey];
415 385
416 while (index > hash[hashkey] 386 while (index > hash [hashkey]
417 && compare_priority (keymap[i], sorted_keymap[index - 1]) > 0) 387 && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0)
418 { 388 {
419 sorted_keymap[index] = sorted_keymap[index - 1]; 389 sorted_keymap [index] = sorted_keymap [index - 1];
420 --index; 390 --index;
421 } 391 }
422 392
423 sorted_keymap[index] = keymap[i]; 393 sorted_keymap [index] = keymap [i];
424 ++hash_budget_counter[hashkey]; 394 ++hash_budget_counter [hashkey];
425 } 395 }
426 } 396 }
427 } 397 }
428 398
429 keymap.swap (sorted_keymap); 399 keymap.swap (sorted_keymap);
431#if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD) 401#if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD)
432 // check for invariants 402 // check for invariants
433 for (i = 0; i < KEYSYM_HASH_BUDGETS; ++i) 403 for (i = 0; i < KEYSYM_HASH_BUDGETS; ++i)
434 { 404 {
435 index = hash[i]; 405 index = hash[i];
436 for (int j = 0; j < hash_budget_size[i]; ++j) 406 for (int j = 0; j < hash_budget_size [i]; ++j)
437 { 407 {
438 if (keymap[index + j]->range == 1) 408 if (keymap [index + j]->range == 1)
439 assert (i == (keymap[index + j]->keysym & KEYSYM_HASH_MASK)); 409 assert (i == (keymap [index + j]->keysym & KEYSYM_HASH_MASK));
440 410
441 if (j) 411 if (j)
442 assert (compare_priority (keymap[index + j - 1], 412 assert (compare_priority (keymap [index + j - 1],
443 keymap[index + j]) >= 0); 413 keymap [index + j]) >= 0);
444 } 414 }
445 } 415 }
446 416
447 // this should be able to detect most possible bugs 417 // this should be able to detect most possible bugs
448 for (i = 0; i < sorted_keymap.size (); ++i) 418 for (i = 0; i < sorted_keymap.size (); ++i)
449 { 419 {
450 keysym_t *a = sorted_keymap[i]; 420 keysym_t *a = sorted_keymap[i];
451 for (int j = 0; j < a->range; ++j) 421 for (int j = 0; j < a->range; ++j)
452 { 422 {
453 int index = find_keysym (a->keysym + j, a->state & OtherModMask); 423 int index = find_keysym (a->keysym + j, a->state);
424
454 assert (index >= 0); 425 assert (index >= 0);
455 keysym_t *b = keymap[index]; 426 keysym_t *b = keymap [index];
456 assert (i == (signed) index || // the normally expected result 427 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 428 (a->keysym + j) >= b->keysym && (a->keysym + j) <= (b->keysym + b->range) && compare_priority (a, b) <= 0); // is effectively the same
458 } 429 }
459 } 430 }
460#endif 431#endif
466 int hashkey = keysym & KEYSYM_HASH_MASK; 437 int hashkey = keysym & KEYSYM_HASH_MASK;
467 unsigned int index = hash [hashkey]; 438 unsigned int index = hash [hashkey];
468 439
469 for (; index < keymap.size (); ++index) 440 for (; index < keymap.size (); ++index)
470 { 441 {
471 keysym_t *key = keymap[index]; 442 keysym_t *key = keymap [index];
472 assert (key);
473 443
474 if (key->keysym <= keysym && key->keysym + key->range > keysym 444 if (key->keysym <= keysym && key->keysym + key->range > keysym
475 // match only the specified bits in state and ignore others 445 // match only the specified bits in state and ignore others
476 && (key->state & OtherModMask) == (key->state & state)) 446 && (key->state & state) == key->state)
477 return index; 447 return index;
478 else if (key->keysym > keysym && key->range == 1) 448 else if (key->keysym > keysym && key->range == 1)
479 return -1; 449 return -1;
480 } 450 }
481 451

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines