--- rxvt-unicode/src/keyboard.C 2005/01/16 19:03:45 1.5 +++ rxvt-unicode/src/keyboard.C 2011/11/27 09:07:48 1.44 @@ -1,120 +1,94 @@ +/*----------------------------------------------------------------------* + * File: keyboard.C + *----------------------------------------------------------------------* + * + * All portions of code are copyright by their respective author/s. + * Copyright (c) 2005 WU Fengguang + * Copyright (c) 2005-2006 Marc Lehmann + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + *----------------------------------------------------------------------*/ + #include "../config.h" #include "rxvt.h" -#include "keyboard.h" -#include "command.h" -#include -#include #ifdef KEYSYM_RESOURCE -//////////////////////////////////////////////////////////////////////////////// -// default keycode translation map and keyevent handlers +#include -keysym_t keyboard_manager::stock_keymap[] = { - /* examples */ - /* keysym, state, range, handler, str */ -//{XK_ISO_Left_Tab, 0, 1, NORMAL, "\033[Z"}, -//{ 'a', 0, 26, RANGE_META8, "a" "%c"}, -//{ 'a', ControlMask, 26, RANGE_META8, "" "%c"}, -//{ XK_Left, 0, 4, LIST, "DACBZ" "\033[Z"}, -//{ XK_Left, ShiftMask, 4, LIST, "dacbZ" "\033[Z"}, -//{ XK_Left, ControlMask, 4, LIST, "dacbZ" "\033OZ"}, -//{ XK_Tab, ControlMask, 1, NORMAL, "\033"}, -//{ XK_apostrophe, ControlMask, 1, NORMAL, "\033"}, -//{ XK_slash, ControlMask, 1, NORMAL, "\033"}, -//{ XK_semicolon, ControlMask, 1, NORMAL, "\033"}, -//{ XK_grave, ControlMask, 1, NORMAL, "\033"}, -//{ XK_comma, ControlMask, 1, NORMAL, "\033"}, -//{ XK_Return, ControlMask, 1, NORMAL, "\033"}, -//{ XK_Return, ShiftMask, 1, NORMAL, "\033"}, -//{ ' ', ShiftMask, 1, NORMAL, "\033"}, -//{ '.', ControlMask, 1, NORMAL, "\033"}, -//{ '0', ControlMask, 10, RANGE, "0" "\033"}, -//{ '0', MetaMask|ControlMask, 10, RANGE, "0" "\033"}, -//{ 'a', MetaMask|ControlMask, 26, RANGE, "a" "\033"}, -}; +#include "rxvtperl.h" +#include "keyboard.h" +#include "command.h" + +/* an intro to the data structure: + * + * vector keymap[] is grouped. + * + * inside each group, elements are sorted by the criteria given by compare_priority(). + * the lookup of keysym is done in two steps: + * 1) locate the group corresponds to the keysym; + * 2) do a linear search inside the group. + * + * array hash[] effectively defines a map from a keysym to a group in keymap[]. + * + * each group has its address(the index of first group element in keymap[]), + * which is computed and stored in hash[]. + * hash[] stores the addresses in the form of: + * index: 0 I1 I2 I3 In + * value: 0...0, A1...A1, A2...A2, A3...A3, ..., An...An + * where + * A1 = 0; + * Ai+1 = N1 + N2 + ... + Ni. + * it is computed from hash_bucket_size[]: + * index: 0 I1 I2 I3 In + * value: 0...0, N1, 0...0, N2, 0...0, N3, ..., Nn, 0...0 + * 0...0, 0.......0, N1.....N1, N1+N2...N1+N2, ... (the computation of hash[]) + * or we can say + * hash_bucket_size[Ii] = Ni; hash_bucket_size[elsewhere] = 0, + * where + * set {I1, I2, ..., In} = { hashkey of keymap[0]->keysym, ..., keymap[keymap.size-1]->keysym } + * where hashkey of keymap[i]->keysym = keymap[i]->keysym & KEYSYM_HASH_MASK + * n(the number of groups) = the number of non-zero member of hash_bucket_size[]; + * Ni(the size of group i) = hash_bucket_size[Ii]. + */ 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); + if (strncmp (str, "command:", 8) == 0) + rt->cmdbuf_append (str + 8, strlen (str) - 8); + else if (strncmp (str, "perl:", 5) == 0) + HOOK_INVOKE((rt, HOOK_USER_COMMAND, DT_STR, str + 5, DT_END)); else - rt->tt_write ((unsigned char *)str, strlen (str)); -} - -static void -output_string_meta8 (rxvt_term *rt, unsigned int state, char *buf, int buflen) -{ - if (state & rt->ModMetaMask) - { -#ifdef META8_OPTION - if (rt->meta_char == 0x80) /* set 8-bit on */ - { - for (char *ch = buf; ch < buf + buflen; ch++) - *ch |= 0x80; - } - else if (rt->meta_char == C0_ESC) /* escape prefix */ -#endif - { - const unsigned char ch = C0_ESC; - rt->tt_write (&ch, 1); - } - } - - rt->tt_write ((unsigned char *) buf, buflen); -} - -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]); - - if (len >= bufsize) - { - rxvt_warn ("buffer overflowed!\n"); - *buf = 0; - } - else if (len < 0) - { - rxvt_warn ("keyrange_translator(), snprintf error"); - *buf = 0; - } - - return len; -} - -//////////////////////////////////////////////////////////////////////////////// -// return: #bits of '1' -static int -bitcount (unsigned int n) -{ - int i; - - for (i = 0; n; ++i, n &= (n - 1)) - ; - - return i; + rt->tt_write (str, strlen (str)); } // 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 */); + int ca = ecb_popcount32 (a->state /* & OtherModMask */); + int cb = ecb_popcount32 (b->state /* & OtherModMask */); if (ca != cb) return ca - cb; -//else if (a->state != b->state) // this behavior is to be disscussed +//else if (a->state != b->state) // this behavior is to be discussed // return b->state - a->state; else - return b->range - a->range; + return 0; } //////////////////////////////////////////////////////////////////////////////// @@ -132,70 +106,74 @@ void keyboard_manager::clear () { - keymap.clear (); hash [0] = 2; - for (unsigned int i = 0; i < user_translations.size (); ++i) - { - free ((void *)user_translations [i]); - user_translations [i] = 0; - } - - for (unsigned int i = 0; i < user_keymap.size (); ++i) + for (unsigned int i = 0; i < keymap.size (); ++i) { - delete user_keymap [i]; - user_keymap [i] = 0; + free ((void *)keymap [i]->str); + delete keymap [i]; + keymap [i] = 0; } - user_keymap.clear (); - user_translations.clear (); + keymap.clear (); } -// a wrapper for register_keymap, -// so that outside codes don't have to know so much details. -// -// the string 'trans' is copied to an internal managed buffer, -// so the caller can free memory of 'trans' at any time. +// a wrapper for register_translation that converts the input string +// to utf-8 and expands 'list' syntax. 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); + char *translation = rxvt_wcstoutf8 (wc); free (wc); - if (key && translation) + if (strncmp (translation, "list", 4) == 0 && translation [4] + && strlen (translation) < STRING_MAX) { - key->keysym = keysym; - key->state = state; - key->range = 1; - key->str = translation; - key->type = keysym_t::NORMAL; + char *prefix = translation + 4; + char *middle = strchr (prefix + 1, translation [4]); + char *suffix = strrchr (prefix + 1, translation [4]); - if (strncmp (translation, "list", 4) == 0 && translation [4]) + if (suffix && middle && suffix > middle + 1) { - char *middle = strchr (translation + 5, translation [4]); - char *suffix = strrchr (translation + 5, translation [4]); - - if (suffix && middle && suffix > middle + 1) - { - key->type = keysym_t::LIST; - key->range = suffix - middle - 1; + int range = suffix - middle - 1; + int prefix_len = middle - prefix - 1; + char buf[STRING_MAX]; - strcpy (translation, translation + 4); - } - else + memcpy (buf, prefix + 1, prefix_len); + strcpy (buf + prefix_len + 1, suffix + 1); + + for (int i = 0; i < range; i++) { - key->range = 1; - rxvt_warn ("cannot parse list-type keysym '%s', treating as normal keysym.\n", translation); + buf [prefix_len] = middle [i + 1]; + register_translation (keysym + i, state, strdup (buf)); } + + free (translation); + return; } else + rxvt_warn ("cannot parse list-type keysym '%s', processing as normal keysym.\n", translation); + } + + register_translation (keysym, state, translation); +} + +void +keyboard_manager::register_translation (KeySym keysym, unsigned int state, char *translation) +{ + keysym_t *key = new keysym_t; + + if (key && translation) + { + key->keysym = keysym; + key->state = state; + key->str = translation; + key->type = keysym_t::STRING; + + if (strncmp (translation, "builtin:", 8) == 0) + key->type = keysym_t::BUILTIN; - user_keymap.push_back (key); - user_translations.push_back (translation); register_keymap (key); } else @@ -209,9 +187,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); @@ -222,14 +197,6 @@ void keyboard_manager::register_done () { - unsigned int i, n = sizeof (stock_keymap) / sizeof (keysym_t); - - if (keymap.back () != &stock_keymap[n - 1]) - for (i = 0; i < n; ++i) - register_keymap (&stock_keymap[i]); - - purge_duplicate_keymap (); - setup_hash (); } @@ -238,11 +205,11 @@ { assert (hash[0] == 0 && "register_done() need to be called"); - if (state & term->ModMetaMask) - state |= MetaMask; + state &= OtherModMask; // mask out uninteresting modifiers - 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,93 +218,24 @@ 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); - - switch (key.type) + if (key.type != keysym_t::BUILTIN) { - case keysym_t::NORMAL: - output_string (term, str); - break; - - case keysym_t::RANGE: - { - char buf[STRING_MAX]; - - if (format_keyrange_string (str, keysym_offset, buf, sizeof (buf)) > 0) - output_string (term, buf); - } - break; - - case keysym_t::RANGE_META8: - { - int len; - char buf[STRING_MAX]; - - len = format_keyrange_string (str, keysym_offset, buf, sizeof (buf)); - if (len > 0) - output_string_meta8 (term, state, buf, len); - } - break; + wchar_t *wc = rxvt_utf8towcs (key.str); + char *str = rxvt_wcstombs (wc); + // TODO: do (some) translations, unescaping etc, here (allow \u escape etc.) + free (wc); - case keysym_t::LIST: - { - char buf[STRING_MAX]; + output_string (term, str); - char *prefix, *middle, *suffix; + free (str); - prefix = str; - middle = strchr (prefix + 1, *prefix); - suffix = strrchr (middle + 1, *prefix); - - memcpy (buf, prefix + 1, middle - prefix - 1); - buf [middle - prefix - 1] = middle [keysym_offset + 1]; - strcpy (buf + (middle - prefix), suffix + 1); - - output_string (term, buf); - } - break; + return true; } - - free (str); - - return true; } - else - { - // fprintf(stderr,"[%x:%x]",state,keysym); - return false; - } -} - -// purge duplicate keymap entries -void keyboard_manager::purge_duplicate_keymap () -{ - for (unsigned int i = 0; i < keymap.size (); ++i) - { - for (unsigned int j = 0; j < i; ++j) - { - if (keymap [i] == keymap [j]) - { - while (keymap [i] == keymap.back ()) - keymap.pop_back (); - if (i < keymap.size ()) - { - keymap[i] = keymap.back (); - keymap.pop_back (); - } - break; - } - } - } + return false; } void @@ -345,84 +243,60 @@ { unsigned int i, index, hashkey; vector sorted_keymap; - uint16_t hash_budget_size[KEYSYM_HASH_BUDGETS]; // size of each budget - uint16_t hash_budget_counter[KEYSYM_HASH_BUDGETS]; // #elements in each budget + uint16_t hash_bucket_size[KEYSYM_HASH_BUCKETS]; // size of each bucket - memset (hash_budget_size, 0, sizeof (hash_budget_size)); - memset (hash_budget_counter, 0, sizeof (hash_budget_counter)); + memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); - // count keysyms for corresponding hash budgets + // determine hash bucket size for (i = 0; i < keymap.size (); ++i) { - assert (keymap [i]); - hashkey = (keymap [i]->keysym & KEYSYM_HASH_MASK); - ++hash_budget_size [hashkey]; + hashkey = keymap [i]->keysym & KEYSYM_HASH_MASK; + ++hash_bucket_size [hashkey]; } - // keysym A with range>1 is counted one more time for - // every keysym B lies in its range - for (i = 0; i < keymap.size (); ++i) - { - if (keymap[i]->range > 1) - { - 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]; - } - } - } - - // now we know the size of each budget - // compute the index of each budget + // now we know the size of each bucket + // compute the index of each bucket hash [0] = 0; - for (index = 0, i = 1; i < KEYSYM_HASH_BUDGETS; ++i) + for (index = 0, i = 1; i < KEYSYM_HASH_BUCKETS; ++i) { - index += hash_budget_size [i - 1]; - hash[i] = (hash_budget_size [i] ? index : hash [i - 1]); + index += hash_bucket_size [i - 1]; + hash [i] = index; } // 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_bucket_size [i - 1], 0); + + memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); // fill in sorted_keymap - // it is sorted in each budget + // it is sorted in each bucket for (i = 0; i < keymap.size (); ++i) { - 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]) - { - index = hash [hashkey] + hash_budget_counter [hashkey]; + hashkey = keymap [i]->keysym & KEYSYM_HASH_MASK; - while (index > hash [hashkey] - && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) - { - sorted_keymap [index] = sorted_keymap [index - 1]; - --index; - } + index = hash [hashkey] + hash_bucket_size [hashkey]; - sorted_keymap [index] = keymap [i]; - ++hash_budget_counter [hashkey]; - } + while (index > hash [hashkey] + && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) + { + sorted_keymap [index] = sorted_keymap [index - 1]; + --index; } + + sorted_keymap [index] = keymap [i]; + ++hash_bucket_size [hashkey]; } keymap.swap (sorted_keymap); -#if defined (DEBUG_STRICT) || defined (DEBUG_KEYBOARD) +#ifndef NDEBUG // check for invariants - for (i = 0; i < KEYSYM_HASH_BUDGETS; ++i) + for (i = 0; i < KEYSYM_HASH_BUCKETS; ++i) { index = hash[i]; - for (int j = 0; j < hash_budget_size [i]; ++j) + for (int j = 0; j < hash_bucket_size [i]; ++j) { - if (keymap [index + j]->range == 1) - assert (i == (keymap [index + j]->keysym & KEYSYM_HASH_MASK)); + assert (i == (keymap [index + j]->keysym & KEYSYM_HASH_MASK)); if (j) assert (compare_priority (keymap [index + j - 1], @@ -434,14 +308,13 @@ for (i = 0; i < sorted_keymap.size (); ++i) { keysym_t *a = sorted_keymap[i]; - for (int j = 0; j < a->range; ++j) - { - int index = find_keysym (a->keysym + j, a->state & OtherModMask); - assert (index >= 0); - 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 - } + int index = find_keysym (a->keysym, a->state); + + assert (index >= 0); + keysym_t *b = keymap [index]; + assert (i == index // the normally expected result + || a->keysym == b->keysym + && compare_priority (a, b) <= 0); // is effectively the same or a closer match } #endif } @@ -451,18 +324,18 @@ { int hashkey = keysym & KEYSYM_HASH_MASK; unsigned int index = hash [hashkey]; + unsigned int end = hashkey < KEYSYM_HASH_BUCKETS - 1 + ? hash [hashkey + 1] + : keymap.size (); - for (; index < keymap.size (); ++index) + for (; index < end; ++index) { keysym_t *key = keymap [index]; - assert (key); - if (key->keysym <= keysym && key->keysym + key->range > keysym + if (key->keysym == 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; } return -1;