diff options
author | wm4 <wm4@mplayer2.org> | 2012-01-13 07:38:40 +0100 |
---|---|---|
committer | Uoti Urpala <uau@mplayer2.org> | 2012-03-25 22:30:37 +0300 |
commit | 166a7de4cf94a78c34040b47a929a72d12f2945f (patch) | |
tree | 3b326a949d7add071c3dc92a1ab1eb5d86e194da /input/input.c | |
parent | 3e6e80a32c04e38c7d2fa77e4bcf1401e792dc7a (diff) |
input: allow unicode keys and reassign internal key codes
This moves all key codes above the highest valid unicode code point
(which is 0x10FFFF). All key codes below MP_KEY_BASE now directly map
to unicode (KEY_ENTER is 13, carriage return). Configuration files
(input.conf) can contain unicode characters in UTF-8 to map non-ASCII
characters/keys.
This shouldn't change anything user visible, except that "direct key
codes" (as used in input.conf) will change their meaning.
Parts of the bstr functions taken from libavutil's GET_UTF8 and
slightly modified.
Diffstat (limited to 'input/input.c')
-rw-r--r-- | input/input.c | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/input/input.c b/input/input.c index cde1b5e027..cc967bb4fa 100644 --- a/input/input.c +++ b/input/input.c @@ -38,6 +38,7 @@ #include "keycodes.h" #include "osdep/timer.h" #include "libavutil/avstring.h" +#include "libavutil/common.h" #include "mp_msg.h" #include "m_config.h" #include "m_option.h" @@ -649,6 +650,17 @@ static const m_option_t mp_input_opts[] = { static int default_cmd_func(int fd, char *buf, int l); +// Encode the unicode codepoint as UTF-8, and append to the end of the +// talloc'ed buffer. +static char *append_utf8_buffer(char *buffer, uint32_t codepoint) +{ + char data[8]; + uint8_t tmp; + char *output = data; + PUT_UTF8(codepoint, tmp, *output++ = tmp;); + return talloc_strndup_append_buffer(buffer, data, output - data); +} + static char *get_key_name(int key, char *ret) { for (int i = 0; modifier_names[i].name; i++) { @@ -663,8 +675,9 @@ static char *get_key_name(int key, char *ret) return talloc_asprintf_append_buffer(ret, "%s", key_names[i].name); } - if (isascii(key)) - return talloc_asprintf_append_buffer(ret, "%c", key); + // printable, and valid unicode range + if (key >= 32 && key <= 0x10FFFF) + return append_utf8_buffer(ret, key); // Print the hex key code return talloc_asprintf_append_buffer(ret, "%#-8x", key); @@ -1173,7 +1186,7 @@ static mp_cmd_t *interpret_key(struct input_ctx *ictx, int code) * shift modifier is still kept for special keys like arrow keys. */ int unmod = code & ~KEY_MODIFIER_MASK; - if (unmod < 256 && unmod != KEY_ENTER && unmod != KEY_TAB) + if (unmod >= 32 && unmod < MP_KEY_BASE) code &= ~KEY_MODIFIER_SHIFT; if (code & MP_KEY_DOWN) { @@ -1491,10 +1504,15 @@ int mp_input_get_key_from_name(const char *name) found: name = p + 1; } - int len = strlen(name); - if (len == 1) // Direct key code - return (unsigned char)name[0] + modifiers; - else if (len > 2 && strncasecmp("0x", name, 2) == 0) + + struct bstr bname = bstr(name); + + struct bstr rest; + int code = bstr_decode_utf8(bname, &rest); + if (code >= 0 && rest.len == 0) + return code + modifiers; + + if (bstr_startswith0(bname, "0x")) return strtol(name, NULL, 16) + modifiers; for (int i = 0; key_names[i].name != NULL; i++) { |