Decode the UTF-8 input from SDL2's text input buffers

This commit is contained in:
Michael Martin
2019-09-29 20:18:37 -07:00
parent 4a8012091b
commit ab9a04979a
+38 -4
View File
@@ -427,21 +427,55 @@ ProcessInputEvent (const SDL_Event *Event)
if (Event->type == SDL_TEXTINPUT) if (Event->type == SDL_TEXTINPUT)
{ {
int newtail; int newtail;
/* TODO: Decode UTF-8 input */ int i = 0;
UniChar map_key = Event->text.text[0];
// dont care about the non-printable, non-char while (Event->text.text[i])
{
UniChar map_key = Event->text.text[i++];
/* Decode any UTF-8 keys */
if (map_key >= 0xC0 && map_key < 0xE0)
{
/* 2-byte UTF-8 */
map_key = (map_key & 0x1f) << 6;
map_key |= Event->text.text[i++] & 0x3f;
}
else if (map_key >= 0xE0 && map_key < 0xF0)
{
/* 3-byte UTF-8 */
map_key = (map_key & 0x0f) << 6;
map_key |= Event->text.text[i++] & 0x3f;
map_key <<= 6;
map_key |= Event->text.text[i++] & 0x3f;
}
else if (map_key >= 0xF0)
{
/* Out of the BMP, won't fit in a UniChar */
/* Use the replacement character instead */
map_key = 0xFFFD;
while (Event->text.text[i] > 0x7F)
{
++i;
}
}
/* dont care about the non-printable, non-char */
if (!map_key) if (!map_key)
return; return;
newtail = (kbdtail + 1) & (KBDBUFSIZE - 1); newtail = (kbdtail + 1) & (KBDBUFSIZE - 1);
// ignore the char if the buffer is full
/* ignore the char if the buffer is full */
if (newtail != kbdhead) if (newtail != kbdhead)
{ {
kbdbuf[kbdtail] = map_key; kbdbuf[kbdtail] = map_key;
kbdtail = newtail; kbdtail = newtail;
lastchar = map_key; lastchar = map_key;
} }
/* Loop back in case there are more chars in the
* text input buffer */
}
} }
} }