diff src/events/SDL_keyboard.c @ 2295:dbc6d1893869

Checking in Christian Walther's patch for x11 keyboard input. Minor code tweaks by Bob.
author Bob Pendleton <bob@pendleton.com>
date Tue, 08 Jan 2008 00:10:46 +0000
parents 4baee598306d
children c97ad1abe05b
line wrap: on
line diff
--- a/src/events/SDL_keyboard.c	Thu Jan 03 06:07:30 2008 +0000
+++ b/src/events/SDL_keyboard.c	Tue Jan 08 00:10:46 2008 +0000
@@ -36,6 +36,48 @@
 static int SDL_current_keyboard;
 static SDL_Keyboard **SDL_keyboards;
 
+/* Taken from SDL_iconv() */
+static char *
+encodeUtf8(Uint32 ch, char *dst)
+{
+    Uint8 *p = (Uint8 *) dst;
+    if (ch <= 0x7F) {
+        *p = (Uint8) ch;
+        ++dst;
+    } else if (ch <= 0x7FF) {
+        p[0] = 0xC0 | (Uint8) ((ch >> 6) & 0x1F);
+        p[1] = 0x80 | (Uint8) (ch & 0x3F);
+        dst += 2;
+    } else if (ch <= 0xFFFF) {
+        p[0] = 0xE0 | (Uint8) ((ch >> 12) & 0x0F);
+        p[1] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
+        p[2] = 0x80 | (Uint8) (ch & 0x3F);
+        dst += 3;
+    } else if (ch <= 0x1FFFFF) {
+        p[0] = 0xF0 | (Uint8) ((ch >> 18) & 0x07);
+        p[1] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
+        p[2] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
+        p[3] = 0x80 | (Uint8) (ch & 0x3F);
+        dst += 4;
+    } else if (ch <= 0x3FFFFFF) {
+        p[0] = 0xF8 | (Uint8) ((ch >> 24) & 0x03);
+        p[1] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
+        p[2] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
+        p[3] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
+        p[4] = 0x80 | (Uint8) (ch & 0x3F);
+        dst += 5;
+    } else {
+        p[0] = 0xFC | (Uint8) ((ch >> 30) & 0x01);
+        p[1] = 0x80 | (Uint8) ((ch >> 24) & 0x3F);
+        p[2] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
+        p[3] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
+        p[4] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
+        p[5] = 0x80 | (Uint8) (ch & 0x3F);
+        dst += 6;
+    }
+    return dst;
+}
+
 /* Public functions */
 int
 SDL_KeyboardInit(void)
@@ -227,21 +269,14 @@
         /* SDLK_INDEX(layoutKey) is the unicode code point of the character generated by the key */
         static char buffer[9];  /* 6 (maximal UTF-8 char length) + 2 ([] for keypad) + 1 (null teminator) */
         char *bufferPtr = &buffer[1];
-        SDL_iconv_t cd;
-        size_t inbytesleft = 4, outbytesleft = 8;
         Uint32 codepoint = SDLK_INDEX(layoutKey);
-        const char *codepointPtr = (const char *) &codepoint;
 
         /* Unaccented letter keys on latin keyboards are normally labeled in upper case (and probably on others like Greek or Cyrillic too, so if you happen to know for sure, please adapt this). */
         if (codepoint >= 'a' && codepoint <= 'z') {
             codepoint -= 32;
         }
 
-        cd = SDL_iconv_open("UTF-8", "UCS-4");
-        if (cd == (SDL_iconv_t) (-1))
-            return "";
-        SDL_iconv(cd, &codepointPtr, &inbytesleft, &bufferPtr, &outbytesleft);
-        SDL_iconv_close(cd);
+        bufferPtr = encodeUtf8(codepoint, bufferPtr);
         *bufferPtr = '\0';
 
         if ((layoutKey & SDL_KEY_KEYPAD_BIT) != 0) {