--- rxvt-unicode/src/keyboard.C 2011/03/24 17:34:28 1.40 +++ rxvt-unicode/src/keyboard.C 2015/01/15 14:30:04 1.71 @@ -8,7 +8,7 @@ * * 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 + * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, @@ -26,11 +26,10 @@ #ifdef KEYSYM_RESOURCE -#include +#include #include "rxvtperl.h" #include "keyboard.h" -#include "command.h" /* an intro to the data structure: * @@ -64,31 +63,15 @@ * Ni(the size of group i) = hash_bucket_size[Ii]. */ -static void -output_string (rxvt_term *rt, const char *str) -{ - 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 (str, strlen (str)); -} - // return: priority_of_a - priority_of_b static int compare_priority (keysym_t *a, keysym_t *b) { // (the more '1's in state; the less range): the greater priority - int ca = rxvt_popcount (a->state /* & OtherModMask */); - int cb = rxvt_popcount (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 discussed -// return b->state - a->state; - else - return b->range - a->range; + return ca - cb; } //////////////////////////////////////////////////////////////////////////////// @@ -100,76 +83,50 @@ keyboard_manager::~keyboard_manager () { - clear (); -} - -void -keyboard_manager::clear () -{ - hash [0] = 2; - for (unsigned int i = 0; i < keymap.size (); ++i) { - free ((void *)keymap [i]->str); + free (keymap [i]->str); delete keymap [i]; - keymap [i] = 0; } - - 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. void -keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const char *trans) +keyboard_manager::unregister_action (KeySym keysym, unsigned int state) { - keysym_t *key = new keysym_t; - wchar_t *wc = rxvt_mbstowcs (trans); - char *translation = rxvt_wcstoutf8 (wc); - free (wc); - - if (key && translation) - { - key->keysym = keysym; - key->state = state; - key->range = 1; - key->str = translation; - key->type = keysym_t::STRING; - - if (strncmp (translation, "list", 4) == 0 && translation [4]) - { - 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; + for (unsigned int i = 0; i < keymap.size (); ++i) + if (keymap [i]->keysym == keysym + && keymap [i]->state == state) + { + free (keymap [i]->str); + delete keymap [i]; - memmove (translation, translation + 4, strlen (translation + 4) + 1); - } - else - rxvt_warn ("cannot parse list-type keysym '%s', treating as normal keysym.\n", translation); - } - else if (strncmp (translation, "builtin:", 8) == 0) - key->type = keysym_t::BUILTIN; + if (i < keymap.size () - 1) + keymap [i] = keymap [keymap.size () - 1]; + keymap.pop_back (); - register_keymap (key); - } - else - { - delete key; - free ((void *)translation); - rxvt_fatal ("out of memory, aborting.\n"); - } + break; + } } void -keyboard_manager::register_keymap (keysym_t *key) +keyboard_manager::register_action (KeySym keysym, unsigned int state, const wchar_t *ws) { + char *action = rxvt_wcstoutf8 (ws); + + keysym_t *key = new keysym_t; + + key->keysym = keysym; + key->state = state; + key->str = action; + key->type = keysym_t::STRING; + + if (strncmp (action, "builtin:", 8) == 0) + key->type = keysym_t::BUILTIN; + else if (strncmp (action, "builtin-string:", 15) == 0) + key->type = keysym_t::BUILTIN_STRING; + + unregister_action (keysym, state); + if (keymap.size () == keymap.capacity ()) keymap.reserve (keymap.size () * 2); @@ -177,16 +134,10 @@ hash[0] = 3; } -void -keyboard_manager::register_done () -{ - setup_hash (); -} - bool -keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state) +keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state, const char *kbuf, int len) { - assert (hash[0] == 0 && "register_done() need to be called"); + assert (("register_done() need to be called", hash[0] == 0)); state &= OtherModMask; // mask out uninteresting modifiers @@ -201,41 +152,33 @@ if (index >= 0) { - const keysym_t &key = *keymap [index]; + keysym_t *key = keymap [index]; - if (key.type != keysym_t::BUILTIN) + if (key->type == keysym_t::BUILTIN_STRING) { - int keysym_offset = keysym - key.keysym; - - wchar_t *wc = rxvt_utf8towcs (key.str); - char *str = rxvt_wcstombs (wc); + term->tt_write_user_input (kbuf, len); + return true; + } + else if (key->type != keysym_t::BUILTIN) + { + wchar_t *ws = rxvt_utf8towcs (key->str); + char *str = rxvt_wcstombs (ws); // TODO: do (some) translations, unescaping etc, here (allow \u escape etc.) - free (wc); + free (ws); - switch (key.type) + if (char *colon = strchr (str, ':')) { - case keysym_t::STRING: - output_string (term, str); - break; - - case keysym_t::LIST: - { - char buf[STRING_MAX]; - - char *prefix, *middle, *suffix; - - 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; + if (strncmp (str, "command:", 8) == 0) + term->cmdbuf_append (str + 8, strlen (str) - 8); + else if (strncmp (str, "string:", 7) == 0) + term->tt_write_user_input (colon + 1, strlen (colon + 1)); + else if (strncmp (str, "perl:", 5) == 0) + HOOK_INVOKE ((term, HOOK_USER_COMMAND, DT_STR, colon + 1, DT_END)); + else + HOOK_INVOKE ((term, HOOK_ACTION, DT_STR_LEN, str, colon - str, DT_STR, colon + 1, DT_INT, 0, DT_STR_LEN, kbuf, len, DT_END)); } + else + term->tt_write_user_input (str, strlen (str)); free (str); @@ -247,55 +190,51 @@ } void -keyboard_manager::setup_hash () +keyboard_manager::register_done () { unsigned int i, index, hashkey; - vector sorted_keymap; uint16_t hash_bucket_size[KEYSYM_HASH_BUCKETS]; // size of each bucket memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); // determine hash bucket size for (i = 0; i < keymap.size (); ++i) - for (int j = min (keymap [i]->range, KEYSYM_HASH_BUCKETS) - 1; j >= 0; --j) - { - hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; - ++hash_bucket_size [hashkey]; - } + { + hashkey = keymap [i]->keysym & KEYSYM_HASH_MASK; + ++hash_bucket_size [hashkey]; + } // 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_BUCKETS; ++i) + for (index = 0, i = 0; i < KEYSYM_HASH_BUCKETS; ++i) { - index += hash_bucket_size [i - 1]; hash [i] = index; + index += hash_bucket_size [i]; } // and allocate just enough space - sorted_keymap.insert (sorted_keymap.begin (), index + hash_bucket_size [i - 1], 0); + simplevec sorted_keymap (index, 0); memset (hash_bucket_size, 0, sizeof (hash_bucket_size)); // fill in sorted_keymap // it is sorted in each bucket for (i = 0; i < keymap.size (); ++i) - for (int j = min (keymap [i]->range, KEYSYM_HASH_BUCKETS) - 1; j >= 0; --j) - { - hashkey = (keymap [i]->keysym + j) & KEYSYM_HASH_MASK; + { + hashkey = keymap [i]->keysym & KEYSYM_HASH_MASK; - index = hash [hashkey] + hash_bucket_size [hashkey]; + index = hash [hashkey] + hash_bucket_size [hashkey]; - while (index > hash [hashkey] - && compare_priority (keymap [i], sorted_keymap [index - 1]) > 0) - { - sorted_keymap [index] = sorted_keymap [index - 1]; - --index; - } + 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]; - } + sorted_keymap [index] = keymap [i]; + ++hash_bucket_size [hashkey]; + } keymap.swap (sorted_keymap); @@ -306,8 +245,7 @@ index = hash[i]; 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], @@ -319,16 +257,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); + int index = find_keysym (a->keysym, a->state); - assert (index >= 0); - keysym_t *b = keymap [index]; - assert (i == index // the normally expected result - || IN_RANGE_INC (a->keysym + j, b->keysym, b->keysym + b->range) - && compare_priority (a, b) <= 0); // is effectively the same or a closer match - } + 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 } @@ -346,7 +281,7 @@ { keysym_t *key = keymap [index]; - if (key->keysym <= keysym && keysym < key->keysym + key->range + if (key->keysym == keysym // match only the specified bits in state and ignore others && (key->state & state) == key->state) return index;