Minimal implementation of textinput events for x11. It only works for latin-1.
authorBob Pendleton <bob@pendleton.com>
Tue, 15 Jan 2008 22:37:17 +0000
changeset 2300c97ad1abe05b
parent 2299 a7cbc25071b6
child 2301 ebcebb426771
Minimal implementation of textinput events for x11. It only works for latin-1.
include/SDL_events.h
src/events/SDL_keyboard.c
src/events/SDL_keyboard_c.h
src/video/x11/SDL_x11events.c
src/video/x11/SDL_x11sym.h
test/checkkeys.c
     1.1 --- a/include/SDL_events.h	Sat Jan 12 18:07:06 2008 +0000
     1.2 +++ b/include/SDL_events.h	Tue Jan 15 22:37:17 2008 +0000
     1.3 @@ -149,12 +149,13 @@
     1.4   *
     1.5   * \brief Keyboard text input event structure (event.text.*)
     1.6   */
     1.7 +#define SDL_TEXTINPUTEVENT_TEXT_SIZE (32)
     1.8  typedef struct SDL_TextInputEvent
     1.9  {
    1.10 -    Uint8 type;             /**< SDL_TEXTINPUT */
    1.11 -    Uint8 which;            /**< The keyboard device index */
    1.12 -    char text[32];          /**< The input text */
    1.13 -    SDL_WindowID windowID;  /**< The window with keyboard focus, if any */
    1.14 +    Uint8 type;                               /**< SDL_TEXTINPUT */
    1.15 +    Uint8 which;                              /**< The keyboard device index */
    1.16 +    char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];  /**< The input text */
    1.17 +    SDL_WindowID windowID;                    /**< The window with keyboard focus, if any */
    1.18  } SDL_TextInputEvent;
    1.19  
    1.20  /**
    1.21 @@ -325,10 +326,10 @@
    1.22      Uint8 type;                     /**< Event type, shared with all events */
    1.23      SDL_WindowEvent window;         /**< Window event data */
    1.24      SDL_KeyboardEvent key;          /**< Keyboard event data */
    1.25 -    SDL_TextInputEvent text;                    /**< Text input event data */
    1.26 +    SDL_TextInputEvent text;        /**< Text input event data */
    1.27      SDL_MouseMotionEvent motion;    /**< Mouse motion event data */
    1.28      SDL_MouseButtonEvent button;    /**< Mouse button event data */
    1.29 -    SDL_MouseWheelEvent wheel;                  /**< Mouse wheel event data */
    1.30 +    SDL_MouseWheelEvent wheel;      /**< Mouse wheel event data */
    1.31      SDL_JoyAxisEvent jaxis;         /**< Joystick axis event data */
    1.32      SDL_JoyBallEvent jball;         /**< Joystick ball event data */
    1.33      SDL_JoyHatEvent jhat;           /**< Joystick hat event data */
     2.1 --- a/src/events/SDL_keyboard.c	Sat Jan 12 18:07:06 2008 +0000
     2.2 +++ b/src/events/SDL_keyboard.c	Tue Jan 15 22:37:17 2008 +0000
     2.3 @@ -37,8 +37,8 @@
     2.4  static SDL_Keyboard **SDL_keyboards;
     2.5  
     2.6  /* Taken from SDL_iconv() */
     2.7 -static char *
     2.8 -encodeUtf8(Uint32 ch, char *dst)
     2.9 +char *
    2.10 +SDL_Ucs4ToUtf8(Uint32 ch, char *dst)
    2.11  {
    2.12      Uint8 *p = (Uint8 *) dst;
    2.13      if (ch <= 0x7F) {
    2.14 @@ -266,17 +266,21 @@
    2.15              keyname = _this->GetSpecialKeyName(_this, layoutKey);
    2.16          }
    2.17      } else if ((layoutKey & SDL_KEY_CAN_BE_PHYSICAL_BIT) == 0) {
    2.18 -        /* SDLK_INDEX(layoutKey) is the unicode code point of the character generated by the key */
    2.19 +        /* SDLK_INDEX(layoutKey) is the unicode code point of the
    2.20 +           character generated by the key */
    2.21          static char buffer[9];  /* 6 (maximal UTF-8 char length) + 2 ([] for keypad) + 1 (null teminator) */
    2.22          char *bufferPtr = &buffer[1];
    2.23          Uint32 codepoint = SDLK_INDEX(layoutKey);
    2.24  
    2.25 -        /* 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). */
    2.26 +        /* Unaccented letter keys on latin keyboards are normally
    2.27 +           labeled in upper case (and probably on others like Greek or
    2.28 +           Cyrillic too, so if you happen to know for sure, please
    2.29 +           adapt this). */
    2.30          if (codepoint >= 'a' && codepoint <= 'z') {
    2.31              codepoint -= 32;
    2.32          }
    2.33  
    2.34 -        bufferPtr = encodeUtf8(codepoint, bufferPtr);
    2.35 +        bufferPtr = SDL_Ucs4ToUtf8(codepoint, bufferPtr);
    2.36          *bufferPtr = '\0';
    2.37  
    2.38          if ((layoutKey & SDL_KEY_KEYPAD_BIT) != 0) {
     3.1 --- a/src/events/SDL_keyboard_c.h	Sat Jan 12 18:07:06 2008 +0000
     3.2 +++ b/src/events/SDL_keyboard_c.h	Tue Jan 15 22:37:17 2008 +0000
     3.3 @@ -48,6 +48,9 @@
     3.4  #endif
     3.5  extern int SDL_TranslateUNICODE;
     3.6  
     3.7 +/* convert UCS4 to utf8 */
     3.8 +extern char *SDL_Ucs4ToUtf8(Uint32 ch, char *dst);
     3.9 +
    3.10  /* Initialize the keyboard subsystem */
    3.11  extern int SDL_KeyboardInit(void);
    3.12  
     4.1 --- a/src/video/x11/SDL_x11events.c	Sat Jan 12 18:07:06 2008 +0000
     4.2 +++ b/src/video/x11/SDL_x11events.c	Tue Jan 15 22:37:17 2008 +0000
     4.3 @@ -167,6 +167,9 @@
     4.4          /* Key press? */
     4.5      case KeyPress:{
     4.6              KeyCode keycode = xevent.xkey.keycode;
     4.7 +            KeySym keysym = NoSymbol;
     4.8 +            char text[sizeof(SDL_TEXTINPUTEVENT_TEXT_SIZE)];
     4.9 +            Uint32 ucs4 = 0;
    4.10  
    4.11  #ifdef DEBUG_XEVENTS
    4.12              printf("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
    4.13 @@ -183,6 +186,13 @@
    4.14                                                          keycode, 0));
    4.15              }
    4.16  #endif
    4.17 +            /* works for Latin-1 */
    4.18 +            SDL_memset(&text[0], 0, SDL_TEXTINPUTEVENT_TEXT_SIZE);
    4.19 +            /* Xutf8LookupString() */
    4.20 +            XLookupString(&xevent, text, sizeof(text), &keysym, NULL);
    4.21 +            if (0 != SDL_strlen(text)) {
    4.22 +                SDL_SendKeyboardText(videodata->keyboard, text);
    4.23 +            }
    4.24          }
    4.25          break;
    4.26  
     5.1 --- a/src/video/x11/SDL_x11sym.h	Sat Jan 12 18:07:06 2008 +0000
     5.2 +++ b/src/video/x11/SDL_x11sym.h	Tue Jan 15 22:37:17 2008 +0000
     5.3 @@ -76,6 +76,7 @@
     5.4  SDL_X11_SYM(int,XKillClient,(Display* a,XID b),(a,b),return)
     5.5  SDL_X11_SYM(Atom,XInternAtom,(Display* a,_Xconst char* b,Bool c),(a,b,c),return)
     5.6  SDL_X11_SYM(XPixmapFormatValues*,XListPixmapFormats,(Display* a,int* b),(a,b),return)
     5.7 +SDL_X11_SYM(KeySym,XLookupKeysym,(XKeyEvent* a,int b),(a,b),return)
     5.8  SDL_X11_SYM(int,XLookupString,(XKeyEvent* a,char* b,int c,KeySym* d,XComposeStatus* e),(a,b,c,d,e),return)
     5.9  SDL_X11_SYM(int,XMapRaised,(Display* a,Window b),(a,b),return)
    5.10  SDL_X11_SYM(int,XMapWindow,(Display* a,Window b),(a,b),return)
     6.1 --- a/test/checkkeys.c	Sat Jan 12 18:07:06 2008 +0000
     6.2 +++ b/test/checkkeys.c	Tue Jan 15 22:37:17 2008 +0000
     6.3 @@ -89,6 +89,11 @@
     6.4      printf("\n");
     6.5  }
     6.6  
     6.7 +static void
     6.8 +PrintText(char *text)
     6.9 +{
    6.10 +}
    6.11 +
    6.12  int
    6.13  main(int argc, char *argv[])
    6.14  {
    6.15 @@ -139,6 +144,9 @@
    6.16          case SDL_KEYUP:
    6.17              PrintKey(&event.key.keysym, 0);
    6.18              break;
    6.19 +        case SDL_TEXTINPUT:
    6.20 +            PrintText(event.text.text);
    6.21 +            break;
    6.22          case SDL_MOUSEBUTTONDOWN:
    6.23              /* Any button press quits the app... */
    6.24          case SDL_QUIT: