input: handle UTF-8 terminal input

This assumes the terminal uses UTF-8. If invalid UTF-8 is encountered (for
example because the terminal uses a legacy encoding), the code falls back
to the old method and feeds each byte as key code to the input code.

In theory, UTF-8 input could randomly fail, because the code in getch2.c
doesn't try to fill the input buffer correctly with input sequences
longer than a byte. This is a problem with the design of the existing
code.
This commit is contained in:
wm4 2012-01-13 07:59:21 +01:00 committed by Uoti Urpala
parent 166a7de4cf
commit 7ea5c4c26c
1 changed files with 11 additions and 0 deletions

View File

@ -57,6 +57,7 @@
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include "bstr.h"
#include "mp_fifo.h" #include "mp_fifo.h"
#include "input/keycodes.h" #include "input/keycodes.h"
#include "getch2.h" #include "getch2.h"
@ -201,6 +202,16 @@ void getch2(struct mp_fifo *fifo)
len = 2; len = 2;
} }
code = KEY_ENTER; code = KEY_ENTER;
} else {
int utf8len = bstr_parse_utf8_code_length(code);
if (utf8len > 0 && utf8len <= getch2_len) {
struct bstr s = { getch2_buf, utf8len };
int unicode = bstr_decode_utf8(s, NULL);
if (unicode > 0) {
len = utf8len;
code = unicode;
}
}
} }
} }
else if (getch2_len > 1) { else if (getch2_len > 1) {