From 20e522cdf4b6b772e3609e95eb9773c29ed9fee1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Thu, 26 Sep 2019 19:22:36 +0200 Subject: [PATCH] Make events test output only UTF-8 in C locale --- tests/events.c | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/tests/events.c b/tests/events.c index 251e15d9..f29dfbb0 100644 --- a/tests/events.c +++ b/tests/events.c @@ -253,17 +253,32 @@ static const char* get_mods_name(int mods) return name; } -static const char* get_character_string(int codepoint) +static size_t encode_utf8(char* s, unsigned int ch) { - // This assumes UTF-8, which is stupid - static char result[6 + 1]; + size_t count = 0; - int length = wctomb(result, codepoint); - if (length == -1) - length = 0; + if (ch < 0x80) + s[count++] = (char) ch; + else if (ch < 0x800) + { + s[count++] = (ch >> 6) | 0xc0; + s[count++] = (ch & 0x3f) | 0x80; + } + else if (ch < 0x10000) + { + s[count++] = (ch >> 12) | 0xe0; + s[count++] = ((ch >> 6) & 0x3f) | 0x80; + s[count++] = (ch & 0x3f) | 0x80; + } + else if (ch < 0x110000) + { + s[count++] = (ch >> 18) | 0xf0; + s[count++] = ((ch >> 12) & 0x3f) | 0x80; + s[count++] = ((ch >> 6) & 0x3f) | 0x80; + s[count++] = (ch & 0x3f) | 0x80; + } - result[length] = '\0'; - return result; + return count; } static void error_callback(int error, const char* description) @@ -425,9 +440,11 @@ static void key_callback(GLFWwindow* window, int key, int scancode, int action, static void char_callback(GLFWwindow* window, unsigned int codepoint) { Slot* slot = glfwGetWindowUserPointer(window); + char string[5] = ""; + + encode_utf8(string, codepoint); printf("%08x to %i at %0.3f: Character 0x%08x (%s) input\n", - counter++, slot->number, glfwGetTime(), codepoint, - get_character_string(codepoint)); + counter++, slot->number, glfwGetTime(), codepoint, string); } static void drop_callback(GLFWwindow* window, int count, const char* paths[]) @@ -500,8 +517,6 @@ int main(int argc, char** argv) GLFWmonitor* monitor = NULL; int ch, i, width, height, count = 1; - setlocale(LC_ALL, ""); - glfwSetErrorCallback(error_callback); if (!glfwInit())