changeset 2300:c97ad1abe05b

Minimal implementation of textinput events for x11. It only works for latin-1.
author Bob Pendleton <bob@pendleton.com>
date Tue, 15 Jan 2008 22:37:17 +0000
parents a7cbc25071b6
children ebcebb426771
files 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
diffstat 6 files changed, 38 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/include/SDL_events.h	Sat Jan 12 18:07:06 2008 +0000
+++ b/include/SDL_events.h	Tue Jan 15 22:37:17 2008 +0000
@@ -149,12 +149,13 @@
  *
  * \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 @@
     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 */
--- a/src/events/SDL_keyboard.c	Sat Jan 12 18:07:06 2008 +0000
+++ b/src/events/SDL_keyboard.c	Tue Jan 15 22:37:17 2008 +0000
@@ -37,8 +37,8 @@
 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 @@
             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) {
--- a/src/events/SDL_keyboard_c.h	Sat Jan 12 18:07:06 2008 +0000
+++ b/src/events/SDL_keyboard_c.h	Tue Jan 15 22:37:17 2008 +0000
@@ -48,6 +48,9 @@
 #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);
 
--- a/src/video/x11/SDL_x11events.c	Sat Jan 12 18:07:06 2008 +0000
+++ b/src/video/x11/SDL_x11events.c	Tue Jan 15 22:37:17 2008 +0000
@@ -167,6 +167,9 @@
         /* 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 @@
                                                         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;
 
--- a/src/video/x11/SDL_x11sym.h	Sat Jan 12 18:07:06 2008 +0000
+++ b/src/video/x11/SDL_x11sym.h	Tue Jan 15 22:37:17 2008 +0000
@@ -76,6 +76,7 @@
 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)
--- a/test/checkkeys.c	Sat Jan 12 18:07:06 2008 +0000
+++ b/test/checkkeys.c	Tue Jan 15 22:37:17 2008 +0000
@@ -89,6 +89,11 @@
     printf("\n");
 }
 
+static void
+PrintText(char *text)
+{
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -139,6 +144,9 @@
         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: