From 8918df95e126878c13f32dea66b2b58f8c9bb394 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Mon, 16 Nov 2015 07:24:21 +0100 Subject: [PATCH] Add encoding_utf to libretro-common --- Makefile.common | 1 + input/drivers_keyboard/keyboard_event_x11.c | 58 ++-------------- libretro-common/encodings/encoding_utf.c | 75 +++++++++++++++++++++ libretro-common/include/encodings/utf.h | 40 +++++++++++ 4 files changed, 121 insertions(+), 53 deletions(-) create mode 100644 libretro-common/encodings/encoding_utf.c create mode 100644 libretro-common/include/encodings/utf.h diff --git a/Makefile.common b/Makefile.common index 5bb123061a..f61bc30414 100644 --- a/Makefile.common +++ b/Makefile.common @@ -127,6 +127,7 @@ OBJ += frontend/frontend.o \ runloop_msg.o \ tasks/task_file_transfer.o \ content.o \ + libretro-common/encodings/encoding_utf.o \ libretro-common/file/file_list.o \ libretro-common/file/dir_list.o \ libretro-common/file/retro_dirent.o \ diff --git a/input/drivers_keyboard/keyboard_event_x11.c b/input/drivers_keyboard/keyboard_event_x11.c index 045158d07a..4fae676557 100644 --- a/input/drivers_keyboard/keyboard_event_x11.c +++ b/input/drivers_keyboard/keyboard_event_x11.c @@ -14,9 +14,6 @@ * If not, see . */ -#include -#include - #include #include #include @@ -25,58 +22,13 @@ #include #endif +#include +#include +#include + #include "../keyboard_line.h" #include "../input_keymaps.h" -static INLINE unsigned leading_ones(uint8_t c) -{ - unsigned ones = 0; - while (c & 0x80) - { - ones++; - c <<= 1; - } - - return ones; -} - -/* Simple implementation. Assumes the sequence is - * properly synchronized and terminated. */ - -static size_t conv_utf8_utf32(uint32_t *out, - size_t out_chars, const char *in, size_t in_size) -{ - unsigned i; - size_t ret = 0; - while (in_size && out_chars) - { - unsigned ones, extra, shift; - uint32_t c; - uint8_t first = *in++; - - ones = leading_ones(first); - if (ones > 6 || ones == 1) /* Invalid or desync. */ - break; - - extra = ones ? ones - 1 : ones; - if (1 + extra > in_size) /* Overflow. */ - break; - - shift = (extra - 1) * 6; - c = (first & ((1 << (7 - ones)) - 1)) << (6 * extra); - - for (i = 0; i < extra; i++, in++, shift -= 6) - c |= (*in & 0x3f) << shift; - - *out++ = c; - in_size -= 1 + extra; - out_chars--; - ret++; - } - - return ret; -} - void x11_handle_key_event(XEvent *event, XIC ic, bool filter) { int i; @@ -101,7 +53,7 @@ void x11_handle_key_event(XEvent *event, XIC ic, bool filter) * which makes mbrtowc a bit impractical. * * Use custom UTF8 -> UTF-32 conversion. */ - num = conv_utf8_utf32(chars, ARRAY_SIZE(chars), keybuf, num); + num = utf8_conv_utf32(chars, ARRAY_SIZE(chars), keybuf, num); #else (void)ic; num = XLookupString(&event->xkey, keybuf, sizeof(keybuf), &keysym, NULL); /* ASCII only. */ diff --git a/libretro-common/encodings/encoding_utf.c b/libretro-common/encodings/encoding_utf.c new file mode 100644 index 0000000000..4fe5b3d3d0 --- /dev/null +++ b/libretro-common/encodings/encoding_utf.c @@ -0,0 +1,75 @@ +/* Copyright (C) 2010-2015 The RetroArch team + * + * --------------------------------------------------------------------------------------- + * The following license statement only applies to this file (encodings_utf.c). + * --------------------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, + * to any person obtaining a copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include + +#include + +static INLINE unsigned leading_ones(uint8_t c) +{ + unsigned ones = 0; + while (c & 0x80) + { + ones++; + c <<= 1; + } + + return ones; +} + +/* Simple implementation. Assumes the sequence is + * properly synchronized and terminated. */ + +size_t utf8_conv_utf32(uint32_t *out, size_t out_chars, + const char *in, size_t in_size) +{ + unsigned i; + size_t ret = 0; + while (in_size && out_chars) + { + unsigned extra, shift; + uint32_t c; + uint8_t first = *in++; + unsigned ones = leading_ones(first); + + if (ones > 6 || ones == 1) /* Invalid or desync. */ + break; + + extra = ones ? ones - 1 : ones; + if (1 + extra > in_size) /* Overflow. */ + break; + + shift = (extra - 1) * 6; + c = (first & ((1 << (7 - ones)) - 1)) << (6 * extra); + + for (i = 0; i < extra; i++, in++, shift -= 6) + c |= (*in & 0x3f) << shift; + + *out++ = c; + in_size -= 1 + extra; + out_chars--; + ret++; + } + + return ret; +} diff --git a/libretro-common/include/encodings/utf.h b/libretro-common/include/encodings/utf.h new file mode 100644 index 0000000000..ac49478ed1 --- /dev/null +++ b/libretro-common/include/encodings/utf.h @@ -0,0 +1,40 @@ +/* Copyright (C) 2010-2015 The RetroArch team + * + * --------------------------------------------------------------------------------------- + * The following license statement only applies to this file (utf.h). + * --------------------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, + * to any person obtaining a copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef _LIBRETRO_ENCODINGS_UTF_H +#define _LIBRETRO_ENCODINGS_UTF_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +size_t utf8_conv_utf32(uint32_t *out, size_t out_chars, + const char *in, size_t in_size); + +#ifdef __cplusplus +} +#endif + +#endif