Skip to content

Commit

Permalink
Make events test output only UTF-8 in C locale
Browse files Browse the repository at this point in the history
  • Loading branch information
elmindreda committed Nov 11, 2019
1 parent c5f1ca3 commit 20e522c
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions tests/events.c
Expand Up @@ -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)
Expand Down Expand Up @@ -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[])
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 20e522c

Please sign in to comment.