Commit b39a4daf04da3cff44ff5f019eb3964bf22240df

Gabriel Jacobo 2013-10-03T10:28:10

SDL_TEXTINPUT support for EVDEV

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
diff --git a/README-raspberrypi.txt b/README-raspberrypi.txt
index a95932d..b0f0b25 100644
--- a/README-raspberrypi.txt
+++ b/README-raspberrypi.txt
@@ -20,7 +20,7 @@ Raspbian (other Linux distros may work as well).
  Raspbian Build Dependencies
 ================================================================================
 
-sudo apt-get install libudev-dev libasound2-dev
+sudo apt-get install libudev-dev libasound2-dev libdbus-1-dev
 
 You also need the VideoCore binary stuff that ships in /opt/vc for EGL and 
 OpenGL ES 2.x, it usually comes pre installed, but in any case:
@@ -28,6 +28,14 @@ OpenGL ES 2.x, it usually comes pre installed, but in any case:
 sudo apt-get install libraspberrypi0 libraspberrypi-bin libraspberrypi-dev
 
 ================================================================================
+ No input
+================================================================================
+
+Make sure you belong to the "input" group.
+
+    sudo usermod -aG input `whoami`
+
+================================================================================
  No HDMI Audio
 ================================================================================
 
@@ -40,9 +48,40 @@ to your config.txt file and reboot.
 Reference: http://www.raspberrypi.org/phpBB3/viewtopic.php?t=5062
 
 ================================================================================
+ Text Input API support
+================================================================================
+
+The Text Input API is supported, with translation of scan codes done via the
+kernel symbol tables. For this to work, SDL needs access to a valid console.
+If you notice there's no SDL_TEXTINPUT message being emmited, double check that
+your app has read access to one of the following:
+    
+* /proc/self/fd/0
+* /dev/tty
+* /dev/tty[0...6]
+* /dev/vc/0
+* /dev/console
+
+This is usually not a problem if you run from the physical terminal (as opposed
+to running from a pseudo terminal, such as via SSH). If running from a PTS, a 
+quick workaround is to run your app as root or add yourself to the tty group,
+then re login to the system.
+
+   sudo usermod -aG tty `whoami`
+    
+The keyboard layout used by SDL is the same as the one the kernel uses.
+To configure the layout on Raspbian:
+    
+    sudo dpkg-reconfigure keyboard-configuration
+    
+To configure the locale, which controls which keys are interpreted as letters,
+this determining the CAPS LOCK behavior:
+
+    sudo dpkg-reconfigure locales
+
+================================================================================
  Notes
 ================================================================================
 
 * Building has only been tested natively (i.e. not cross compiled). Cross
   compilation might work though, feedback is welcome!
-* No Text Input yet.
\ No newline at end of file
diff --git a/configure b/configure
index c38982c..68b62c5 100755
--- a/configure
+++ b/configure
@@ -20754,6 +20754,45 @@ $as_echo "#define SDL_INPUT_LINUXEV 1" >>confdefs.h
         fi
 }
 
+CheckInputKD()
+{
+
+    { $as_echo "$as_me:${as_lineno-$LINENO}: checking for Linux kd.h" >&5
+$as_echo_n "checking for Linux kd.h... " >&6; }
+    use_input_kd=no
+    cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+      #include <linux/kd.h>
+      #include <linux/keyboard.h>
+
+int
+main ()
+{
+
+        struct kbentry kbe;
+        kbe.kb_table = KG_CTRL;
+        ioctl(0, KDGKBENT, &kbe);
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+
+    use_input_kd=yes
+
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+    { $as_echo "$as_me:${as_lineno-$LINENO}: result: $use_input_kd" >&5
+$as_echo "$use_input_kd" >&6; }
+    if test x$use_input_kd = xyes; then
+
+$as_echo "#define SDL_INPUT_LINUXKD 1" >>confdefs.h
+
+    fi
+}
+
 CheckLibUDev()
 {
     # Check whether --enable-libudev was given.
@@ -22080,6 +22119,7 @@ case "$host" in
         CheckLibUDev
         CheckDBus
         CheckInputEvents
+        CheckInputKD
         CheckTslib
         CheckUSBHID
         CheckPTHREAD
diff --git a/configure.in b/configure.in
index f1d6449..f6810cb 100644
--- a/configure.in
+++ b/configure.in
@@ -1791,6 +1791,28 @@ CheckInputEvents()
         fi
 }
 
+dnl See if we can use the kernel kd.h header
+CheckInputKD()
+{
+
+    AC_MSG_CHECKING(for Linux kd.h)
+    use_input_kd=no
+    AC_TRY_COMPILE([
+      #include <linux/kd.h>
+      #include <linux/keyboard.h>
+    ],[
+        struct kbentry kbe;
+        kbe.kb_table = KG_CTRL;
+        ioctl(0, KDGKBENT, &kbe);
+    ],[
+    use_input_kd=yes
+    ])
+    AC_MSG_RESULT($use_input_kd)
+    if test x$use_input_kd = xyes; then
+        AC_DEFINE(SDL_INPUT_LINUXKD, 1, [ ])
+    fi
+}
+
 dnl See if the platform offers libudev for device enumeration and hotplugging.
 CheckLibUDev()
 {
@@ -2395,6 +2417,7 @@ case "$host" in
         CheckLibUDev
         CheckDBus
         CheckInputEvents
+        CheckInputKD
         CheckTslib
         CheckUSBHID
         CheckPTHREAD
diff --git a/include/SDL_config.h.in b/include/SDL_config.h.in
index 0829941..b2daf6f 100644
--- a/include/SDL_config.h.in
+++ b/include/SDL_config.h.in
@@ -217,6 +217,7 @@
 
 /* Enable various input drivers */
 #undef SDL_INPUT_LINUXEV
+#undef SDL_INPUT_LINUXKD
 #undef SDL_INPUT_TSLIB
 #undef SDL_JOYSTICK_BEOS
 #undef SDL_JOYSTICK_DINPUT
diff --git a/src/events/SDL_keyboard.c b/src/events/SDL_keyboard.c
index 607046e..34d94d0 100644
--- a/src/events/SDL_keyboard.c
+++ b/src/events/SDL_keyboard.c
@@ -507,7 +507,7 @@ static const char *SDL_scancode_names[SDL_NUM_SCANCODES] = {
 };
 
 /* Taken from SDL_iconv() */
-static char *
+char *
 SDL_UCS4ToUTF8(Uint32 ch, char *dst)
 {
     Uint8 *p = (Uint8 *) dst;
diff --git a/src/events/SDL_keyboard_c.h b/src/events/SDL_keyboard_c.h
index 1720399..cd9f1bb 100644
--- a/src/events/SDL_keyboard_c.h
+++ b/src/events/SDL_keyboard_c.h
@@ -59,6 +59,9 @@ extern int SDL_SendEditingText(const char *text, int start, int end);
 /* Shutdown the keyboard subsystem */
 extern void SDL_KeyboardQuit(void);
 
+/* Convert to UTF-8 */
+extern char *SDL_UCS4ToUTF8(Uint32 ch, char *dst);
+
 #endif /* _SDL_keyboard_c_h */
 
 /* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/input/evdev/SDL_evdev.c b/src/input/evdev/SDL_evdev.c
index 4581ac0..b9b6473 100644
--- a/src/input/evdev/SDL_evdev.c
+++ b/src/input/evdev/SDL_evdev.c
@@ -39,7 +39,11 @@ static _THIS = NULL;
 #include <fcntl.h>
 #include <sys/ioctl.h>
 #include <limits.h>             /* For the definition of PATH_MAX */
-
+#include <linux/input.h>
+#ifdef SDL_INPUT_LINUXKD
+#include <linux/kd.h>
+#include <linux/keyboard.h>
+#endif
 
 #include "SDL.h"
 #include "SDL_assert.h"
@@ -325,12 +329,54 @@ static Uint8 EVDEV_MouseButtons[] = {
     SDL_BUTTON_X2 + 3           /*  BTN_TASK        0x117 */
 };
 
+static char* EVDEV_consoles[] = {
+    "/proc/self/fd/0",
+    "/dev/tty",
+    "/dev/tty0",
+    "/dev/tty1",
+    "/dev/tty2",
+    "/dev/tty3",
+    "/dev/tty4",
+    "/dev/tty5",
+    "/dev/tty6",
+    "/dev/vc/0",
+    "/dev/console"
+};
+
+#define IS_CONSOLE(fd) isatty (fd) && ioctl(fd, KDGKBTYPE, &arg) == 0 && ((arg == KB_101) || (arg == KB_84))
+
+static int SDL_EVDEV_get_console_fd(void)
+{
+    int fd, i;
+    char arg = 0;
+    
+    /* Try a few consoles to see which one we have read access to */
+    
+    for( i = 0; i < SDL_arraysize(EVDEV_consoles); i++) {
+        fd = open(EVDEV_consoles[i], O_RDONLY);
+        if (fd >= 0) {
+            if (IS_CONSOLE(fd)) return fd;
+            close(fd);
+        }
+    }
+    
+    /* Try stdin, stdout, stderr */
+    
+    for( fd = 0; fd < 3; fd++) {
+        if (IS_CONSOLE(fd)) return fd;
+    }
+    
+    /* We won't be able to send SDL_TEXTINPUT events */
+    return -1;
+}
+
 int
 SDL_EVDEV_Init(void)
 {
     int retval = 0;
     
     if (_this == NULL) {
+        
         _this = (SDL_EVDEV_PrivateData *) SDL_calloc(1, sizeof(*_this));
         if(_this == NULL) {
             return SDL_OutOfMemory();
@@ -354,6 +400,9 @@ SDL_EVDEV_Init(void)
 #else
         /* TODO: Scan the devices manually, like a caveman */
 #endif /* SDL_USE_LIBUDEV */
+        
+        /* We need a physical terminal (not PTS) to be able to translate key code to symbols via the kernel tables */
+        _this->console_fd = SDL_EVDEV_get_console_fd();
 
     }
     
@@ -378,6 +427,9 @@ SDL_EVDEV_Quit(void)
         SDL_UDEV_Quit();
 #endif /* SDL_USE_LIBUDEV */
        
+        if (_this->console_fd >= 0) {
+            close(_this->console_fd);
+        }
         /* Remove existing devices */
         while(_this->first != NULL) {
             SDL_EVDEV_device_removed(_this->first->path);
@@ -443,11 +495,18 @@ SDL_EVDEV_Poll(void)
     SDL_Scancode scan_code;
     int mouse_button;
     SDL_Mouse *mouse;
-    
+#ifdef SDL_INPUT_LINUXKD
+    Uint16 modstate;
+    struct kbentry kbe;
+    static char keysym[8];
+    char *end;
+    Uint32 kval;
+#endif
+
 #if SDL_USE_LIBUDEV
     SDL_UDEV_Poll();
 #endif
-    
+
     for (item = _this->first; item != NULL; item = item->next) {
         while ((len = read(item->fd, events, (sizeof events))) > 0) {
             len /= sizeof(events[0]);
@@ -455,20 +514,57 @@ SDL_EVDEV_Poll(void)
                 switch(item->devclass) {
                     case SDL_EVDEV_DEVICE_KEYBOARD:
                         switch (events[i].type) {
-                            case EV_KEY:
-                                scan_code = SDL_EVDEV_translate_keycode(events[i].code);
-                                if (scan_code != SDL_SCANCODE_UNKNOWN) {
-                                    if (events[i].value == 0) {
-                                        SDL_SendKeyboardKey(SDL_RELEASED, scan_code);
-                                    }
-                                    else if (events[i].value == 1) {
-                                        SDL_SendKeyboardKey(SDL_PRESSED, scan_code);
-                                    }
-                                    else if (events[i].value == 2) {
-                                        /* Key repeated */
-                                        SDL_SendKeyboardKey(SDL_PRESSED, scan_code);
+                        case EV_KEY:
+                            scan_code = SDL_EVDEV_translate_keycode(events[i].code);
+                            if (scan_code != SDL_SCANCODE_UNKNOWN) {
+                                if (events[i].value == 0) {
+                                    SDL_SendKeyboardKey(SDL_RELEASED, scan_code);
+                                }
+                                else if (events[i].value == 1 || events[i].value == 2 /* Key repeated */ ) {
+                                    SDL_SendKeyboardKey(SDL_PRESSED, scan_code);
+#ifdef SDL_INPUT_LINUXKD
+                                    if (_this->console_fd >= 0) {
+                                        kbe.kb_index = events[i].code;
+                                        /* Convert the key to an UTF-8 char */
+                                        /* Ref: http://www.linuxjournal.com/article/2783 */
+                                        modstate = SDL_GetModState();
+                                        kbe.kb_table = 0;
+                                        
+                                        /* Ref: http://graphics.stanford.edu/~seander/bithacks.html#ConditionalSetOrClearBitsWithoutBranching */
+                                        kbe.kb_table |= -( (modstate & KMOD_LCTRL) != 0) & (1 << KG_CTRLL | 1 << KG_CTRL);
+                                        kbe.kb_table |= -( (modstate & KMOD_RCTRL) != 0) & (1 << KG_CTRLR | 1 << KG_CTRL);
+                                        kbe.kb_table |= -( (modstate & KMOD_LSHIFT) != 0) & (1 << KG_SHIFTL | 1 << KG_SHIFT);
+                                        kbe.kb_table |= -( (modstate & KMOD_RSHIFT) != 0) & (1 << KG_SHIFTR | 1 << KG_SHIFT);
+                                        kbe.kb_table |= -( (modstate & KMOD_LALT) != 0) & (1 << KG_ALT);
+                                        kbe.kb_table |= -( (modstate & KMOD_RALT) != 0) & (1 << KG_ALTGR);
+
+                                        if(ioctl(_this->console_fd, KDGKBENT, (unsigned long)&kbe) == 0 && 
+                                            ( (KTYP(kbe.kb_value) == KT_LATIN) || (KTYP(kbe.kb_value) == KT_ASCII) || (KTYP(kbe.kb_value) == KT_LETTER) )) 
+                                        {
+                                            kval = KVAL(kbe.kb_value);
+                                            
+                                            /* While there's a KG_CAPSSHIFT symbol, it's not useful to build the table index with it
+                                             * because 1 << KG_CAPSSHIFT overflows the 8 bits of kb_table 
+                                             * So, we do the CAPS LOCK logic here. Note that isalpha depends on the locale!
+                                             */
+                                            if ( modstate & KMOD_CAPS && isalpha(kval) ) {
+                                                if ( isupper(kval) ) {
+                                                    kval = tolower(kval);
+                                                }
+                                                else {
+                                                    kval = toupper(kval);
+                                                }
+                                            }
+                                             
+                                            /* Convert to UTF-8 and send */
+                                            end = SDL_UCS4ToUTF8( kval, keysym);
+                                            *end = '\0';
+                                            SDL_SendKeyboardText(keysym);
+                                        }
                                     }
+#endif    
                                 }
+                            }
                                 break;
 
                             default:
@@ -651,3 +747,4 @@ SDL_EVDEV_device_removed(const char *devpath)
 #endif /* SDL_INPUT_LINUXEV */
 
 /* vi: set ts=4 sw=4 expandtab: */
+
diff --git a/src/input/evdev/SDL_evdev.h b/src/input/evdev/SDL_evdev.h
index 5a6038f..d63b87f 100644
--- a/src/input/evdev/SDL_evdev.h
+++ b/src/input/evdev/SDL_evdev.h
@@ -19,8 +19,6 @@
   3. This notice may not be removed or altered from any source distribution.
 */
 
-#include <linux/input.h>
-
 #include "SDL_config.h"
 
 #ifndef _SDL_evdev_h
@@ -42,7 +40,7 @@ typedef struct SDL_evdevlist_item
     char *path;
     int fd;
     SDL_EVDEV_deviceclass devclass;
-    struct SDL_evdevlist_item *next;   
+    struct SDL_evdevlist_item *next;
 } SDL_evdevlist_item;
 
 typedef struct SDL_EVDEV_PrivateData
@@ -51,6 +49,7 @@ typedef struct SDL_EVDEV_PrivateData
     SDL_evdevlist_item *last;
     int numdevices;
     int ref_count;
+    int console_fd;
 } SDL_EVDEV_PrivateData;
 
 extern int SDL_EVDEV_Init(void);