From 4db28e6d1972d47356045502257ba9f244471136 Mon Sep 17 00:00:00 2001 From: Bob Pendleton Date: Tue, 15 Jan 2008 22:37:17 +0000 Subject: [PATCH] Minimal implementation of textinput events for x11. It only works for latin-1. --- include/SDL_events.h | 13 +++++++------ src/events/SDL_keyboard.c | 14 +++++++++----- src/events/SDL_keyboard_c.h | 3 +++ src/video/x11/SDL_x11events.c | 10 ++++++++++ src/video/x11/SDL_x11sym.h | 1 + test/checkkeys.c | 8 ++++++++ 6 files changed, 38 insertions(+), 11 deletions(-) diff --git a/include/SDL_events.h b/include/SDL_events.h index f06a8ba3b..d26cf09d3 100644 --- a/include/SDL_events.h +++ b/include/SDL_events.h @@ -149,12 +149,13 @@ typedef struct SDL_KeyboardEvent * * \brief Keyboard text input event structure (event.text.*) */ +#define SDL_TEXTINPUTEVENT_TEXT_SIZE (32) typedef struct SDL_TextInputEvent { - Uint8 type; /**< SDL_TEXTINPUT */ - Uint8 which; /**< The keyboard device index */ - char text[32]; /**< The input text */ - SDL_WindowID windowID; /**< The window with keyboard focus, if any */ + Uint8 type; /**< SDL_TEXTINPUT */ + Uint8 which; /**< The keyboard device index */ + char text[SDL_TEXTINPUTEVENT_TEXT_SIZE]; /**< The input text */ + SDL_WindowID windowID; /**< The window with keyboard focus, if any */ } SDL_TextInputEvent; /** @@ -325,10 +326,10 @@ typedef union SDL_Event Uint8 type; /**< Event type, shared with all events */ SDL_WindowEvent window; /**< Window event data */ SDL_KeyboardEvent key; /**< Keyboard event data */ - SDL_TextInputEvent text; /**< Text input event data */ + SDL_TextInputEvent text; /**< Text input event data */ SDL_MouseMotionEvent motion; /**< Mouse motion event data */ SDL_MouseButtonEvent button; /**< Mouse button event data */ - SDL_MouseWheelEvent wheel; /**< Mouse wheel event data */ + SDL_MouseWheelEvent wheel; /**< Mouse wheel event data */ SDL_JoyAxisEvent jaxis; /**< Joystick axis event data */ SDL_JoyBallEvent jball; /**< Joystick ball event data */ SDL_JoyHatEvent jhat; /**< Joystick hat event data */ diff --git a/src/events/SDL_keyboard.c b/src/events/SDL_keyboard.c index 9e842e9d5..3b202b71b 100644 --- a/src/events/SDL_keyboard.c +++ b/src/events/SDL_keyboard.c @@ -37,8 +37,8 @@ static int SDL_current_keyboard; static SDL_Keyboard **SDL_keyboards; /* Taken from SDL_iconv() */ -static char * -encodeUtf8(Uint32 ch, char *dst) +char * +SDL_Ucs4ToUtf8(Uint32 ch, char *dst) { Uint8 *p = (Uint8 *) dst; if (ch <= 0x7F) { @@ -266,17 +266,21 @@ SDL_GetKeyName(SDLKey layoutKey) keyname = _this->GetSpecialKeyName(_this, layoutKey); } } else if ((layoutKey & SDL_KEY_CAN_BE_PHYSICAL_BIT) == 0) { - /* SDLK_INDEX(layoutKey) is the unicode code point of the character generated by the key */ + /* 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]; Uint32 codepoint = SDLK_INDEX(layoutKey); - /* 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). */ + /* 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; } - bufferPtr = encodeUtf8(codepoint, bufferPtr); + bufferPtr = SDL_Ucs4ToUtf8(codepoint, bufferPtr); *bufferPtr = '\0'; if ((layoutKey & SDL_KEY_KEYPAD_BIT) != 0) { diff --git a/src/events/SDL_keyboard_c.h b/src/events/SDL_keyboard_c.h index 226980908..f49b561cb 100644 --- a/src/events/SDL_keyboard_c.h +++ b/src/events/SDL_keyboard_c.h @@ -48,6 +48,9 @@ struct SDL_Keyboard #endif extern int SDL_TranslateUNICODE; +/* convert UCS4 to utf8 */ +extern char *SDL_Ucs4ToUtf8(Uint32 ch, char *dst); + /* Initialize the keyboard subsystem */ extern int SDL_KeyboardInit(void); diff --git a/src/video/x11/SDL_x11events.c b/src/video/x11/SDL_x11events.c index 5dbd97a8e..1d49fa2f2 100644 --- a/src/video/x11/SDL_x11events.c +++ b/src/video/x11/SDL_x11events.c @@ -167,6 +167,9 @@ X11_DispatchEvent(_THIS) /* Key press? */ case KeyPress:{ KeyCode keycode = xevent.xkey.keycode; + KeySym keysym = NoSymbol; + char text[sizeof(SDL_TEXTINPUTEVENT_TEXT_SIZE)]; + Uint32 ucs4 = 0; #ifdef DEBUG_XEVENTS printf("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode); @@ -183,6 +186,13 @@ X11_DispatchEvent(_THIS) keycode, 0)); } #endif + /* works for Latin-1 */ + SDL_memset(&text[0], 0, SDL_TEXTINPUTEVENT_TEXT_SIZE); + /* Xutf8LookupString() */ + XLookupString(&xevent, text, sizeof(text), &keysym, NULL); + if (0 != SDL_strlen(text)) { + SDL_SendKeyboardText(videodata->keyboard, text); + } } break; diff --git a/src/video/x11/SDL_x11sym.h b/src/video/x11/SDL_x11sym.h index 5f6a49250..d101c2ee8 100644 --- a/src/video/x11/SDL_x11sym.h +++ b/src/video/x11/SDL_x11sym.h @@ -76,6 +76,7 @@ SDL_X11_SYM(KeyCode,XKeysymToKeycode,(Display* a,KeySym b),(a,b),return) SDL_X11_SYM(int,XKillClient,(Display* a,XID b),(a,b),return) SDL_X11_SYM(Atom,XInternAtom,(Display* a,_Xconst char* b,Bool c),(a,b,c),return) SDL_X11_SYM(XPixmapFormatValues*,XListPixmapFormats,(Display* a,int* b),(a,b),return) +SDL_X11_SYM(KeySym,XLookupKeysym,(XKeyEvent* a,int b),(a,b),return) SDL_X11_SYM(int,XLookupString,(XKeyEvent* a,char* b,int c,KeySym* d,XComposeStatus* e),(a,b,c,d,e),return) SDL_X11_SYM(int,XMapRaised,(Display* a,Window b),(a,b),return) SDL_X11_SYM(int,XMapWindow,(Display* a,Window b),(a,b),return) diff --git a/test/checkkeys.c b/test/checkkeys.c index 0083fef9b..8863cb427 100644 --- a/test/checkkeys.c +++ b/test/checkkeys.c @@ -89,6 +89,11 @@ PrintKey(SDL_keysym * sym, int pressed) printf("\n"); } +static void +PrintText(char *text) +{ +} + int main(int argc, char *argv[]) { @@ -139,6 +144,9 @@ main(int argc, char *argv[]) case SDL_KEYUP: PrintKey(&event.key.keysym, 0); break; + case SDL_TEXTINPUT: + PrintText(event.text.text); + break; case SDL_MOUSEBUTTONDOWN: /* Any button press quits the app... */ case SDL_QUIT: