summaryrefslogtreecommitdiffstats
path: root/bstr.c
diff options
context:
space:
mode:
authorwm4 <wm4@mplayer2.org>2012-01-13 07:38:40 +0100
committerUoti Urpala <uau@mplayer2.org>2012-03-25 22:30:37 +0300
commit166a7de4cf94a78c34040b47a929a72d12f2945f (patch)
tree3b326a949d7add071c3dc92a1ab1eb5d86e194da /bstr.c
parent3e6e80a32c04e38c7d2fa77e4bcf1401e792dc7a (diff)
downloadmpv-166a7de4cf94a78c34040b47a929a72d12f2945f.tar.bz2
mpv-166a7de4cf94a78c34040b47a929a72d12f2945f.tar.xz
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 'bstr.c')
-rw-r--r--bstr.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/bstr.c b/bstr.c
index 219c136d7c..0c46b1d9b0 100644
--- a/bstr.c
+++ b/bstr.c
@@ -201,3 +201,35 @@ int bstr_sscanf(struct bstr str, const char *format, ...)
talloc_free(ptr);
return ret;
}
+
+int bstr_parse_utf8_code_length(unsigned char b)
+{
+ if (b < 128)
+ return 1;
+ int bytes = 7 - av_log2(b ^ 255);
+ return (bytes >= 2 && bytes <= 4) ? bytes : -1;
+}
+
+int bstr_decode_utf8(struct bstr s, struct bstr *out_next)
+{
+ if (s.len == 0)
+ return -1;
+ unsigned int codepoint = s.start[0];
+ s.start++; s.len--;
+ if (codepoint >= 128) {
+ int bytes = bstr_parse_utf8_code_length(codepoint);
+ if (bytes < 0 || s.len < bytes - 1)
+ return -1;
+ codepoint &= 127 >> bytes;
+ for (int n = 1; n < bytes; n++) {
+ int tmp = s.start[0];
+ if ((tmp & 0xC0) != 0x80)
+ return -1;
+ codepoint = (codepoint << 6) | (tmp & ~0xC0);
+ s.start++; s.len--;
+ }
+ }
+ if (out_next)
+ *out_next = s;
+ return codepoint;
+}