comparison 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
comparison
equal deleted inserted replaced
2294:386ebf50dd91 2295:dbc6d1893869
33 /* Global keyboard information */ 33 /* Global keyboard information */
34 int SDL_TranslateUNICODE = 0; 34 int SDL_TranslateUNICODE = 0;
35 static int SDL_num_keyboards; 35 static int SDL_num_keyboards;
36 static int SDL_current_keyboard; 36 static int SDL_current_keyboard;
37 static SDL_Keyboard **SDL_keyboards; 37 static SDL_Keyboard **SDL_keyboards;
38
39 /* Taken from SDL_iconv() */
40 static char *
41 encodeUtf8(Uint32 ch, char *dst)
42 {
43 Uint8 *p = (Uint8 *) dst;
44 if (ch <= 0x7F) {
45 *p = (Uint8) ch;
46 ++dst;
47 } else if (ch <= 0x7FF) {
48 p[0] = 0xC0 | (Uint8) ((ch >> 6) & 0x1F);
49 p[1] = 0x80 | (Uint8) (ch & 0x3F);
50 dst += 2;
51 } else if (ch <= 0xFFFF) {
52 p[0] = 0xE0 | (Uint8) ((ch >> 12) & 0x0F);
53 p[1] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
54 p[2] = 0x80 | (Uint8) (ch & 0x3F);
55 dst += 3;
56 } else if (ch <= 0x1FFFFF) {
57 p[0] = 0xF0 | (Uint8) ((ch >> 18) & 0x07);
58 p[1] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
59 p[2] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
60 p[3] = 0x80 | (Uint8) (ch & 0x3F);
61 dst += 4;
62 } else if (ch <= 0x3FFFFFF) {
63 p[0] = 0xF8 | (Uint8) ((ch >> 24) & 0x03);
64 p[1] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
65 p[2] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
66 p[3] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
67 p[4] = 0x80 | (Uint8) (ch & 0x3F);
68 dst += 5;
69 } else {
70 p[0] = 0xFC | (Uint8) ((ch >> 30) & 0x01);
71 p[1] = 0x80 | (Uint8) ((ch >> 24) & 0x3F);
72 p[2] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
73 p[3] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
74 p[4] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
75 p[5] = 0x80 | (Uint8) (ch & 0x3F);
76 dst += 6;
77 }
78 return dst;
79 }
38 80
39 /* Public functions */ 81 /* Public functions */
40 int 82 int
41 SDL_KeyboardInit(void) 83 SDL_KeyboardInit(void)
42 { 84 {
225 } 267 }
226 } else if ((layoutKey & SDL_KEY_CAN_BE_PHYSICAL_BIT) == 0) { 268 } else if ((layoutKey & SDL_KEY_CAN_BE_PHYSICAL_BIT) == 0) {
227 /* SDLK_INDEX(layoutKey) is the unicode code point of the character generated by the key */ 269 /* SDLK_INDEX(layoutKey) is the unicode code point of the character generated by the key */
228 static char buffer[9]; /* 6 (maximal UTF-8 char length) + 2 ([] for keypad) + 1 (null teminator) */ 270 static char buffer[9]; /* 6 (maximal UTF-8 char length) + 2 ([] for keypad) + 1 (null teminator) */
229 char *bufferPtr = &buffer[1]; 271 char *bufferPtr = &buffer[1];
230 SDL_iconv_t cd;
231 size_t inbytesleft = 4, outbytesleft = 8;
232 Uint32 codepoint = SDLK_INDEX(layoutKey); 272 Uint32 codepoint = SDLK_INDEX(layoutKey);
233 const char *codepointPtr = (const char *) &codepoint;
234 273
235 /* 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). */ 274 /* 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). */
236 if (codepoint >= 'a' && codepoint <= 'z') { 275 if (codepoint >= 'a' && codepoint <= 'z') {
237 codepoint -= 32; 276 codepoint -= 32;
238 } 277 }
239 278
240 cd = SDL_iconv_open("UTF-8", "UCS-4"); 279 bufferPtr = encodeUtf8(codepoint, bufferPtr);
241 if (cd == (SDL_iconv_t) (-1))
242 return "";
243 SDL_iconv(cd, &codepointPtr, &inbytesleft, &bufferPtr, &outbytesleft);
244 SDL_iconv_close(cd);
245 *bufferPtr = '\0'; 280 *bufferPtr = '\0';
246 281
247 if ((layoutKey & SDL_KEY_KEYPAD_BIT) != 0) { 282 if ((layoutKey & SDL_KEY_KEYPAD_BIT) != 0) {
248 buffer[0] = '['; 283 buffer[0] = '[';
249 *bufferPtr++ = ']'; 284 *bufferPtr++ = ']';