--- rxvt-unicode/src/keyboard.C 2005/01/16 18:48:04 1.3 +++ rxvt-unicode/src/keyboard.C 2005/01/16 23:55:42 1.7 @@ -1,12 +1,13 @@ #include "../config.h" #include "rxvt.h" -#include "keyboard.h" -#include "command.h" -#include -#include #ifdef KEYSYM_RESOURCE +#include + +#include "keyboard.h" +#include "command.h" + //////////////////////////////////////////////////////////////////////////////// // default keycode translation map and keyevent handlers @@ -59,8 +60,7 @@ else if (rt->meta_char == C0_ESC) /* escape prefix */ #endif { - const unsigned char - ch = C0_ESC; + const unsigned char ch = C0_ESC; rt->tt_write (&ch, 1); } } @@ -71,16 +71,12 @@ static int format_keyrange_string (const char *str, int keysym_offset, char *buf, int bufsize) { - int len = snprintf (buf, bufsize, str + 1, keysym_offset + str [0]); + size_t len = snprintf (buf, bufsize, str + 1, keysym_offset + str [0]); - if (len >= bufsize) - { - fprintf (stderr, "buffer overflowed!\n"); - buf[bufsize - 1] = '\0'; - } - else if (len < 0) + if (len >= (size_t)bufsize) { - perror ("keyrange_translator()"); + rxvt_warn ("format_keyrange_string: formatting failed, ignoring key.\n"); + *buf = 0; } return len; @@ -88,16 +84,20 @@ //////////////////////////////////////////////////////////////////////////////// // return: #bits of '1' +#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3) +# define bitcount(n) (__extension__ ({ uint32_t n__ = (n); __builtin_popcount (n); })) +#else static int -bitcount (unsigned int n) +bitcount (uint16_t n) { int i; - for (i = 0; n; ++i, n &= (n - 1)) + for (i = 0; n; ++i, n &= n - 1) ; return i; } +#endif // return: priority_of_a - priority_of_b static int @@ -121,7 +121,7 @@ keyboard_manager::keyboard_manager () { keymap.reserve (256); - hash[0] = 1; // hash[0] != 0 indicates uninitialized data + hash [0] = 1; // hash[0] != 0 indicates uninitialized data } keyboard_manager::~keyboard_manager () @@ -163,6 +163,7 @@ keysym_t *key = new keysym_t; wchar_t *wc = rxvt_mbstowcs (trans); +printf ("CONV <%s> %x %x %x %x\n", trans, (int)wc[0], (int)wc[1], (int)wc[2], (int)wc[3]); const char *translation = rxvt_wcstoutf8 (wc); free (wc); @@ -187,12 +188,8 @@ strcpy (translation, translation + 4); } else - { - key->range = 1; - rxvt_warn ("cannot parse list-type keysym '%s', treating as normal keysym.\n", translation); - } + rxvt_warn ("cannot parse list-type keysym '%s', treating as normal keysym.\n", translation); } - else user_keymap.push_back (key); user_translations.push_back (translation); @@ -238,11 +235,9 @@ { assert (hash[0] == 0 && "register_done() need to be called"); - if (state & term->ModMetaMask) - state |= MetaMask; - - if (state & term->ModNumLockMask) - state |= NumLockMask; + if (state & term->ModMetaMask) state |= MetaMask; + if (state & term->ModNumLockMask) state |= NumLockMask; + if (state & term->ModLevel3Mask) state |= Level3Mask; if (!!(term->priv_modes & PrivMode_aplKP) != !!(state & ShiftMask)) state |= AppKeypadMask; @@ -251,12 +246,12 @@ if (index >= 0) { - assert (term && keymap [index]); const keysym_t &key = *keymap [index]; int keysym_offset = keysym - key.keysym; wchar_t *wc = rxvt_utf8towcs (key.str); + char *str = rxvt_wcstombs (wc); // TODO: do translations, unescaping etc, here (allow \u escape etc.) free (wc); @@ -324,9 +319,9 @@ { for (unsigned int j = 0; j < i; ++j) { - if (keymap[i] == keymap[j]) + if (keymap [i] == keymap [j]) { - while (keymap[i] == keymap.back ()) + while (keymap [i] == keymap.back ()) keymap.pop_back (); if (i < keymap.size ()) @@ -354,9 +349,9 @@ // count keysyms for corresponding hash budgets for (i = 0; i < keymap.size (); ++i) { - assert (keymap[i]); - hashkey = (keymap[i]->keysym & KEYSYM_HASH_MASK); - ++hash_budget_size[hashkey]; + assert (keymap [i]); + hashkey = (keymap [i]->keysym & KEYSYM_HASH_MASK); + ++hash_budget_size [hashkey]; } // keysym A with range>1 is counted one more time for @@ -365,49 +360,49 @@ { if (keymap[i]->range > 1) { - for (int j = min (keymap[i]->range, KEYSYM_HASH_BUDGETS) - 1; j > 0; --j) + for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j > 0; --j) { - hashkey = ((keymap[i]->keysym + j) & KEYSYM_HASH_MASK); - if (hash_budget_size[hashkey]) - ++hash_budget_size[hashkey]; + hashkey = ((keymap [i]->keysym + j) & KEYSYM_HASH_MASK); + if (hash_budget_size [hashkey]) + ++hash_budget_size [hashkey]; } } } // now we know the size of each budget // compute the index of each budget - hash[0] = 0; + hash [0] = 0; for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i) { - index += hash_budget_size[i - 1]; - hash[i] = (hash_budget_size[i] ? index : hash[i - 1]); + index += hash_budget_size [i - 1]; + hash[i] = (hash_budget_size [i] ? index : hash [i - 1]); } // and allocate just enough space //sorted_keymap.reserve (hash[i - 1] + hash_budget_size[i - 1]); - sorted_keymap.insert (sorted_keymap.begin (), index + hash_budget_size[i - 1], 0); + sorted_keymap.insert (sorted_keymap.begin (), index + hash_budget_size [i - 1], 0); // fill in sorted_keymap // it is sorted in each budget for (i = 0; i < keymap.size (); ++i) { - for (int j = min (keymap[i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j) + for (int j = min (keymap [i]->range, KEYSYM_HASH_BUDGETS) - 1; j >= 0; --j) { - hashkey = ((keymap[i]->keysym + j) & KEYSYM_HASH_MASK); + hashkey = ((keymap [i]->keysym + j) & KEYSYM_HASH_MASK); - if (hash_budget_size[hashkey]) + if (hash_budget_size [hashkey]) { - index = hash[hashkey] + hash_budget_counter[hashkey]; + index = hash [hashkey] + hash_budget_counter [hashkey]; - while (index > hash[hashkey] - && compare_priority (keymap[i], sorted_keymap[index - 1]) > 0) + while (index > hash [hashkey] + && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) { - sorted_keymap[index] = sorted_keymap[index - 1]; + sorted_keymap [index] = sorted_keymap [index - 1]; --index; } - sorted_keymap[index] = keymap[i]; - ++hash_budget_counter[hashkey]; + sorted_keymap [index] = keymap [i]; + ++hash_budget_counter [hashkey]; } } } @@ -419,14 +414,14 @@ for (i = 0; i < KEYSYM_HASH_BUDGETS; ++i) { index = hash[i]; - for (int j = 0; j < hash_budget_size[i]; ++j) + for (int j = 0; j < hash_budget_size [i]; ++j) { - if (keymap[index + j]->range == 1) - assert (i == (keymap[index + j]->keysym & KEYSYM_HASH_MASK)); + if (keymap [index + j]->range == 1) + assert (i == (keymap [index + j]->keysym & KEYSYM_HASH_MASK)); if (j) - assert (compare_priority (keymap[index + j - 1], - keymap[index + j]) >= 0); + assert (compare_priority (keymap [index + j - 1], + keymap [index + j]) >= 0); } } @@ -436,9 +431,10 @@ keysym_t *a = sorted_keymap[i]; for (int j = 0; j < a->range; ++j) { - int index = find_keysym (a->keysym + j, a->state & OtherModMask); + int index = find_keysym (a->keysym + j, a->state); + assert (index >= 0); - keysym_t *b = keymap[index]; + keysym_t *b = keymap [index]; assert (i == (signed) index || // the normally expected result (a->keysym + j) >= b->keysym && (a->keysym + j) <= (b->keysym + b->range) && compare_priority (a, b) <= 0); // is effectively the same } @@ -454,12 +450,12 @@ for (; index < keymap.size (); ++index) { - keysym_t *key = keymap[index]; + keysym_t *key = keymap [index]; assert (key); if (key->keysym <= keysym && key->keysym + key->range > keysym // match only the specified bits in state and ignore others - && (key->state & OtherModMask) == (key->state & state)) + && (key->state & state) == key->state) return index; else if (key->keysym > keysym && key->range == 1) return -1;