Make events test output only UTF-8 in C locale

This commit is contained in:
Camilla Löwy 2019-09-26 19:22:36 +02:00
parent c5f1ca3e41
commit 20e522cdf4

View File

@ -253,17 +253,32 @@ static const char* get_mods_name(int mods)
return name; 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 size_t count = 0;
static char result[6 + 1];
int length = wctomb(result, codepoint); if (ch < 0x80)
if (length == -1) s[count++] = (char) ch;
length = 0; 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 count;
return result;
} }
static void error_callback(int error, const char* description) 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) static void char_callback(GLFWwindow* window, unsigned int codepoint)
{ {
Slot* slot = glfwGetWindowUserPointer(window); Slot* slot = glfwGetWindowUserPointer(window);
char string[5] = "";
encode_utf8(string, codepoint);
printf("%08x to %i at %0.3f: Character 0x%08x (%s) input\n", printf("%08x to %i at %0.3f: Character 0x%08x (%s) input\n",
counter++, slot->number, glfwGetTime(), codepoint, counter++, slot->number, glfwGetTime(), codepoint, string);
get_character_string(codepoint));
} }
static void drop_callback(GLFWwindow* window, int count, const char* paths[]) static void drop_callback(GLFWwindow* window, int count, const char* paths[])
@ -500,8 +517,6 @@ int main(int argc, char** argv)
GLFWmonitor* monitor = NULL; GLFWmonitor* monitor = NULL;
int ch, i, width, height, count = 1; int ch, i, width, height, count = 1;
setlocale(LC_ALL, "");
glfwSetErrorCallback(error_callback); glfwSetErrorCallback(error_callback);
if (!glfwInit()) if (!glfwInit())