Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Minimal implementation of textinput events for x11. It only works for…

… latin-1.
  • Loading branch information
pendletonrc committed Jan 15, 2008
1 parent a40a5dc commit 4db28e6d1972d47356045502257ba9f244471136
@@ -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 */
@@ -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) {
@@ -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);

@@ -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;

@@ -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)
@@ -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:

0 comments on commit 4db28e6

Please sign in to comment.