--- rxvt-unicode/src/keyboard.C 2012/05/18 00:10:47 1.59 +++ rxvt-unicode/src/keyboard.C 2015/03/17 09:23:08 1.72 @@ -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, @@ -63,17 +63,6 @@ * Ni(the size of group i) = hash_bucket_size[Ii]. */ -static void -output_string (rxvt_term *term, const char *str) -{ - if (strncmp (str, "command:", 8) == 0) - term->cmdbuf_append (str + 8, strlen (str) - 8); - else if (strncmp (str, "perl:", 5) == 0) - HOOK_INVOKE((term, HOOK_USER_COMMAND, DT_STR, str + 5, DT_END)); - else - term->tt_write (str, strlen (str)); -} - // return: priority_of_a - priority_of_b static int compare_priority (keysym_t *a, keysym_t *b) @@ -82,12 +71,7 @@ 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 0; + return ca - cb; } //////////////////////////////////////////////////////////////////////////////// @@ -107,19 +91,41 @@ } void -keyboard_manager::register_user_translation (KeySym keysym, unsigned int state, const wchar_t *ws) +keyboard_manager::unregister_action (KeySym keysym, unsigned int state) +{ + 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]; + + if (i < keymap.size () - 1) + keymap [i] = keymap [keymap.size () - 1]; + keymap.pop_back (); + + break; + } +} + +void +keyboard_manager::register_action (KeySym keysym, unsigned int state, const wchar_t *ws) { - char *translation = rxvt_wcstoutf8 (ws); + char *action = rxvt_wcstoutf8 (ws); keysym_t *key = new keysym_t; key->keysym = keysym; key->state = state; - key->str = translation; + key->str = action; key->type = keysym_t::STRING; - if (strncmp (translation, "builtin:", 8) == 0) + 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); @@ -128,8 +134,8 @@ hash[0] = 3; } -bool -keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state) +keysym_t * +keyboard_manager::lookup_keysym (rxvt_term *term, KeySym keysym, unsigned int state) { assert (("register_done() need to be called", hash[0] == 0)); @@ -144,18 +150,41 @@ int index = find_keysym (keysym, state); - if (index >= 0) - { - keysym_t *key = keymap [index]; + return index >= 0 ? keymap [index] : 0; +} + +bool +keyboard_manager::dispatch (rxvt_term *term, KeySym keysym, unsigned int state, const char *kbuf, int len) +{ + keysym_t *key = lookup_keysym (term, keysym, state); - if (key->type != keysym_t::BUILTIN) + if (key) + { + if (key->type == keysym_t::BUILTIN_STRING) + { + 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 (ws); - output_string (term, str); + if (char *colon = strchr (str, ':')) + { + 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); @@ -183,15 +212,14 @@ // 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 - simplevec sorted_keymap (index + hash_bucket_size [i - 1], 0); + simplevec sorted_keymap (index, 0); memset (hash_bucket_size, 0, sizeof (hash_bucket_size));