comparison src/video/x11/SDL_x11events.c @ 2295:dbc6d1893869

Checking in Christian Walther's patch for x11 keyboard input. Minor code tweaks by Bob.
author Bob Pendleton <bob@pendleton.com>
date Tue, 08 Jan 2008 00:10:46 +0000
parents da8332c8f480
children a7cbc25071b6
comparison
equal deleted inserted replaced
2294:386ebf50dd91 2295:dbc6d1893869
27 27
28 #include "SDL_syswm.h" 28 #include "SDL_syswm.h"
29 #include "SDL_x11video.h" 29 #include "SDL_x11video.h"
30 #include "../../events/SDL_events_c.h" 30 #include "../../events/SDL_events_c.h"
31 31
32
33 /* Check to see if this is a repeated key.
34 (idea shamelessly lifted from GII -- thanks guys! :)
35 */
36 static int
37 X11_KeyRepeat(Display * display, XEvent * event)
38 {
39 XEvent peekevent;
40 int repeated;
41
42 repeated = 0;
43 if (XPending(display)) {
44 XPeekEvent(display, &peekevent);
45 if ((peekevent.type == KeyPress) &&
46 (peekevent.xkey.keycode == event->xkey.keycode) &&
47 ((peekevent.xkey.time - event->xkey.time) < 2)) {
48 repeated = 1;
49 XNextEvent(display, &peekevent);
50 }
51 }
52 return (repeated);
53 }
32 54
33 static void 55 static void
34 X11_DispatchEvent(_THIS) 56 X11_DispatchEvent(_THIS)
35 { 57 {
36 SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata; 58 SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
165 } 187 }
166 break; 188 break;
167 189
168 /* Key press? */ 190 /* Key press? */
169 case KeyPress:{ 191 case KeyPress:{
170 #if 0 /* FIXME */
171 static SDL_keysym saved_keysym;
172 SDL_keysym keysym;
173 KeyCode keycode = xevent.xkey.keycode; 192 KeyCode keycode = xevent.xkey.keycode;
174 193
175 #ifdef DEBUG_XEVENTS 194 #ifdef DEBUG_XEVENTS
176 printf("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode); 195 printf("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
177 #endif 196 #endif
178 /* Get the translated SDL virtual keysym */ 197 if (!X11_KeyRepeat(videodata->display, &xevent)) {
179 if (keycode) { 198 SDLKey physicalKey = videodata->keyCodeToSDLKTable[keycode];
180 keysym.scancode = keycode; 199 SDL_SendKeyboardKey(videodata->keyboard, SDL_PRESSED,
181 keysym.sym = X11_TranslateKeycode(SDL_Display, keycode); 200 (Uint8) keycode, physicalKey);
182 keysym.mod = KMOD_NONE; 201 #if 1
183 keysym.unicode = 0; 202 if (physicalKey == SDLK_UNKNOWN) {
184 } else { 203 fprintf(stderr,
185 keysym = saved_keysym; 204 "The key you just pressed is not recognized by SDL. To help get this fixed, report this to the SDL mailing list <sdl@libsdl.org> or to Christian Walther <cwalther@gmx.ch>. X11 KeyCode is %d, X11 KeySym 0x%X.\n",
186 } 205 (int) keycode,
187 206 (unsigned int) XKeycodeToKeysym(videodata->
188 /* If we're not doing translation, we're done! */ 207 display, keycode,
189 if (!SDL_TranslateUNICODE) { 208 0));
190 posted = SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
191 break;
192 }
193
194 if (XFilterEvent(&xevent, None)) {
195 if (xevent.xkey.keycode) {
196 posted = SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
197 } else {
198 /* Save event to be associated with IM text
199 In 1.3 we'll have a text event instead.. */
200 saved_keysym = keysym;
201 } 209 }
202 break; 210 #endif
203 } 211 }
204
205 /* Look up the translated value for the key event */
206 #ifdef X_HAVE_UTF8_STRING
207 if (data->ic != NULL) {
208 static Status state;
209 /* A UTF-8 character can be at most 6 bytes */
210 char keybuf[6];
211 if (Xutf8LookupString(data->ic, &xevent.xkey,
212 keybuf, sizeof(keybuf), NULL, &state)) {
213 keysym.unicode = Utf8ToUcs4((Uint8 *) keybuf);
214 }
215 } else
216 #endif
217 {
218 static XComposeStatus state;
219 char keybuf[32];
220
221 if (XLookupString(&xevent.xkey,
222 keybuf, sizeof(keybuf), NULL, &state)) {
223 /*
224 * FIXME: XLookupString() may yield more than one
225 * character, so we need a mechanism to allow for
226 * this (perhaps null keypress events with a
227 * unicode value)
228 */
229 keysym.unicode = (Uint8) keybuf[0];
230 }
231 }
232 posted = SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
233 #endif // 0
234 } 212 }
235 break; 213 break;
236 214
237 /* Key release? */ 215 /* Key release? */
238 case KeyRelease:{ 216 case KeyRelease:{
239 #if 0 /* FIXME */
240 SDL_keysym keysym;
241 KeyCode keycode = xevent.xkey.keycode; 217 KeyCode keycode = xevent.xkey.keycode;
242 218
243 #ifdef DEBUG_XEVENTS 219 #ifdef DEBUG_XEVENTS
244 printf("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode); 220 printf("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
245 #endif 221 #endif
246 /* Check to see if this is a repeated key */ 222 /* Check to see if this is a repeated key */
247 if (X11_KeyRepeat(SDL_Display, &xevent)) { 223 if (X11_KeyRepeat(videodata->display, &xevent)) {
248 break; 224 break;
249 } 225 }
250 226
251 /* Get the translated SDL virtual keysym */ 227 SDL_SendKeyboardKey(videodata->keyboard, SDL_RELEASED,
252 keysym.scancode = keycode; 228 (Uint8) keycode,
253 keysym.sym = X11_TranslateKeycode(SDL_Display, keycode); 229 videodata->keyCodeToSDLKTable[keycode]);
254 keysym.mod = KMOD_NONE;
255 keysym.unicode = 0;
256
257 posted = SDL_PrivateKeyboard(SDL_RELEASED, &keysym);
258 #endif // 0
259 } 230 }
260 break; 231 break;
261 232
262 /* Have we been iconified? */ 233 /* Have we been iconified? */
263 case UnmapNotify:{ 234 case UnmapNotify:{