src/events/SDL_keyboard.c
changeset 2295 dbc6d1893869
parent 2268 4baee598306d
child 2300 c97ad1abe05b
     1.1 --- a/src/events/SDL_keyboard.c	Thu Jan 03 06:07:30 2008 +0000
     1.2 +++ b/src/events/SDL_keyboard.c	Tue Jan 08 00:10:46 2008 +0000
     1.3 @@ -36,6 +36,48 @@
     1.4  static int SDL_current_keyboard;
     1.5  static SDL_Keyboard **SDL_keyboards;
     1.6  
     1.7 +/* Taken from SDL_iconv() */
     1.8 +static char *
     1.9 +encodeUtf8(Uint32 ch, char *dst)
    1.10 +{
    1.11 +    Uint8 *p = (Uint8 *) dst;
    1.12 +    if (ch <= 0x7F) {
    1.13 +        *p = (Uint8) ch;
    1.14 +        ++dst;
    1.15 +    } else if (ch <= 0x7FF) {
    1.16 +        p[0] = 0xC0 | (Uint8) ((ch >> 6) & 0x1F);
    1.17 +        p[1] = 0x80 | (Uint8) (ch & 0x3F);
    1.18 +        dst += 2;
    1.19 +    } else if (ch <= 0xFFFF) {
    1.20 +        p[0] = 0xE0 | (Uint8) ((ch >> 12) & 0x0F);
    1.21 +        p[1] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
    1.22 +        p[2] = 0x80 | (Uint8) (ch & 0x3F);
    1.23 +        dst += 3;
    1.24 +    } else if (ch <= 0x1FFFFF) {
    1.25 +        p[0] = 0xF0 | (Uint8) ((ch >> 18) & 0x07);
    1.26 +        p[1] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
    1.27 +        p[2] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
    1.28 +        p[3] = 0x80 | (Uint8) (ch & 0x3F);
    1.29 +        dst += 4;
    1.30 +    } else if (ch <= 0x3FFFFFF) {
    1.31 +        p[0] = 0xF8 | (Uint8) ((ch >> 24) & 0x03);
    1.32 +        p[1] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
    1.33 +        p[2] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
    1.34 +        p[3] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
    1.35 +        p[4] = 0x80 | (Uint8) (ch & 0x3F);
    1.36 +        dst += 5;
    1.37 +    } else {
    1.38 +        p[0] = 0xFC | (Uint8) ((ch >> 30) & 0x01);
    1.39 +        p[1] = 0x80 | (Uint8) ((ch >> 24) & 0x3F);
    1.40 +        p[2] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
    1.41 +        p[3] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
    1.42 +        p[4] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
    1.43 +        p[5] = 0x80 | (Uint8) (ch & 0x3F);
    1.44 +        dst += 6;
    1.45 +    }
    1.46 +    return dst;
    1.47 +}
    1.48 +
    1.49  /* Public functions */
    1.50  int
    1.51  SDL_KeyboardInit(void)
    1.52 @@ -227,21 +269,14 @@
    1.53          /* SDLK_INDEX(layoutKey) is the unicode code point of the character generated by the key */
    1.54          static char buffer[9];  /* 6 (maximal UTF-8 char length) + 2 ([] for keypad) + 1 (null teminator) */
    1.55          char *bufferPtr = &buffer[1];
    1.56 -        SDL_iconv_t cd;
    1.57 -        size_t inbytesleft = 4, outbytesleft = 8;
    1.58          Uint32 codepoint = SDLK_INDEX(layoutKey);
    1.59 -        const char *codepointPtr = (const char *) &codepoint;
    1.60  
    1.61          /* 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). */
    1.62          if (codepoint >= 'a' && codepoint <= 'z') {
    1.63              codepoint -= 32;
    1.64          }
    1.65  
    1.66 -        cd = SDL_iconv_open("UTF-8", "UCS-4");
    1.67 -        if (cd == (SDL_iconv_t) (-1))
    1.68 -            return "";
    1.69 -        SDL_iconv(cd, &codepointPtr, &inbytesleft, &bufferPtr, &outbytesleft);
    1.70 -        SDL_iconv_close(cd);
    1.71 +        bufferPtr = encodeUtf8(codepoint, bufferPtr);
    1.72          *bufferPtr = '\0';
    1.73  
    1.74          if ((layoutKey & SDL_KEY_KEYPAD_BIT) != 0) {