Navigation Menu

Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Minimal implementation of textinput events for x11. It only works for…
Browse files Browse the repository at this point in the history
… latin-1.
  • Loading branch information
pendletonrc committed Jan 15, 2008
1 parent a40a5dc commit 4db28e6
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 11 deletions.
13 changes: 7 additions & 6 deletions include/SDL_events.h
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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 */
Expand Down
14 changes: 9 additions & 5 deletions src/events/SDL_keyboard.c
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions src/events/SDL_keyboard_c.h
Expand Up @@ -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);

Expand Down
10 changes: 10 additions & 0 deletions src/video/x11/SDL_x11events.c
Expand Up @@ -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);
Expand All @@ -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;

Expand Down
1 change: 1 addition & 0 deletions src/video/x11/SDL_x11sym.h
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions test/checkkeys.c
Expand Up @@ -89,6 +89,11 @@ PrintKey(SDL_keysym * sym, int pressed)
printf("\n");
}

static void
PrintText(char *text)
{
}

int
main(int argc, char *argv[])
{
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 4db28e6

Please sign in to comment.