--- rxvt-unicode/src/keyboard.C 2005/01/16 18:57:03 1.4 +++ rxvt-unicode/src/keyboard.C 2005/01/17 00:07:26 1.8 @@ -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 @@ -37,8 +38,6 @@ static void output_string (rxvt_term *rt, const char *str) { - assert (rt && str); - if (strncmp (str, "proto:", 6) == 0) rt->cmd_write ((unsigned char *)str + 6, strlen (str) - 6); else @@ -59,8 +58,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 +69,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,23 +82,25 @@ //////////////////////////////////////////////////////////////////////////////// // 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 compare_priority (keysym_t *a, keysym_t *b) { - assert (a && b); - // (the more '1's in state; the less range): the greater priority int ca = bitcount (a->state /* & OtherModMask */); int cb = bitcount (b->state /* & OtherModMask */); @@ -159,8 +155,6 @@ void keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const char *trans) { - assert (trans); - keysym_t *key = new keysym_t; wchar_t *wc = rxvt_mbstowcs (trans); const char *translation = rxvt_wcstoutf8 (wc); @@ -187,12 +181,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); @@ -209,9 +199,6 @@ void keyboard_manager::register_keymap (keysym_t *key) { - assert (key); - assert (key->range >= 1); - if (keymap.size () == keymap.capacity ()) keymap.reserve (keymap.size () * 2); @@ -238,11 +225,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 +236,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); @@ -354,13 +339,12 @@ // count keysyms for corresponding hash budgets for (i = 0; i < keymap.size (); ++i) { - assert (keymap [i]); - hashkey = (keymap [i]->keysym & KEYSYM_HASH_MASK); + hashkey = keymap [i]->keysym & KEYSYM_HASH_MASK; ++hash_budget_size [hashkey]; } - // keysym A with range>1 is counted one more time for - // every keysym B lies in its range + // a keysym_t with range>1 is counted one more time for every keysym that + // lies in its range for (i = 0; i < keymap.size (); ++i) { if (keymap[i]->range > 1) @@ -436,7 +420,8 @@ 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]; assert (i == (signed) index || // the normally expected result @@ -455,11 +440,10 @@ for (; index < keymap.size (); ++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;