Mercurial > sdl-ios-xcode
annotate src/video/x11/SDL_x11events.c @ 14:c3e9d4a623c1
Fixed stuck keys when changing the video mode
author | Sam Lantinga <slouken@lokigames.com> |
---|---|
date | Tue, 01 May 2001 21:12:57 +0000 |
parents | 34d956b20f75 |
children | b0ae59d0f3ee |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga | |
4 | |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 Sam Lantinga | |
20 slouken@devolution.com | |
21 */ | |
22 | |
23 #ifdef SAVE_RCSID | |
24 static char rcsid = | |
25 "@(#) $Id$"; | |
26 #endif | |
27 | |
28 /* Handle the event stream, converting X11 events into SDL events */ | |
29 | |
30 #include <stdio.h> | |
31 #include <stdlib.h> | |
32 #include <string.h> | |
33 #include <setjmp.h> | |
34 #include <X11/Xlib.h> | |
35 #include <X11/Xutil.h> | |
36 #include <X11/keysym.h> | |
37 #ifdef __SVR4 | |
38 #include <X11/Sunkeysym.h> | |
39 #endif | |
40 #include <sys/time.h> | |
41 | |
42 #include "SDL.h" | |
43 #include "SDL_syswm.h" | |
44 #include "SDL_sysevents.h" | |
45 #include "SDL_sysvideo.h" | |
46 #include "SDL_events_c.h" | |
47 #include "SDL_x11video.h" | |
48 #include "SDL_x11dga_c.h" | |
49 #include "SDL_x11modes_c.h" | |
50 #include "SDL_x11image_c.h" | |
51 #include "SDL_x11gamma_c.h" | |
52 #include "SDL_x11wm_c.h" | |
53 #include "SDL_x11mouse_c.h" | |
54 #include "SDL_x11events_c.h" | |
55 | |
56 | |
14
c3e9d4a623c1
Fixed stuck keys when changing the video mode
Sam Lantinga <slouken@lokigames.com>
parents:
12
diff
changeset
|
57 /* Define this if you want to debug X11 events */ |
c3e9d4a623c1
Fixed stuck keys when changing the video mode
Sam Lantinga <slouken@lokigames.com>
parents:
12
diff
changeset
|
58 /*#define DEBUG_XEVENTS*/ |
c3e9d4a623c1
Fixed stuck keys when changing the video mode
Sam Lantinga <slouken@lokigames.com>
parents:
12
diff
changeset
|
59 |
0 | 60 /* The translation tables from an X11 keysym to a SDL keysym */ |
61 static SDLKey ODD_keymap[256]; | |
62 static SDLKey MISC_keymap[256]; | |
63 SDL_keysym *X11_TranslateKey(Display *display, XKeyEvent *xkey, KeyCode kc, | |
64 SDL_keysym *keysym); | |
65 | |
66 /* Check to see if this is a repeated key. | |
67 (idea shamelessly lifted from GII -- thanks guys! :) | |
68 */ | |
69 static int X11_KeyRepeat(Display *display, XEvent *event) | |
70 { | |
71 XEvent peekevent; | |
72 int repeated; | |
73 | |
74 repeated = 0; | |
75 if ( XPending(display) ) { | |
76 XPeekEvent(display, &peekevent); | |
77 if ( (peekevent.type == KeyPress) && | |
78 (peekevent.xkey.keycode == event->xkey.keycode) && | |
12
34d956b20f75
Fix key repeat detection on newer X servers
Sam Lantinga <slouken@lokigames.com>
parents:
8
diff
changeset
|
79 ((peekevent.xkey.time-event->xkey.time) < 2) ) { |
0 | 80 repeated = 1; |
81 XNextEvent(display, &peekevent); | |
82 } | |
83 } | |
84 return(repeated); | |
85 } | |
86 | |
87 /* Note: The X server buffers and accumulates mouse motion events, so | |
88 the motion event generated by the warp may not appear exactly as we | |
89 expect it to. We work around this (and improve performance) by only | |
90 warping the pointer when it reaches the edge, and then wait for it. | |
91 */ | |
92 #define MOUSE_FUDGE_FACTOR 8 | |
93 | |
94 static __inline__ int X11_WarpedMotion(_THIS, XEvent *xevent) | |
95 { | |
96 int w, h, i; | |
97 int deltax, deltay; | |
98 int posted; | |
99 | |
100 w = SDL_VideoSurface->w; | |
101 h = SDL_VideoSurface->h; | |
102 deltax = xevent->xmotion.x - mouse_last.x; | |
103 deltay = xevent->xmotion.y - mouse_last.y; | |
104 #ifdef DEBUG_MOTION | |
105 printf("Warped mouse motion: %d,%d\n", deltax, deltay); | |
106 #endif | |
107 mouse_last.x = xevent->xmotion.x; | |
108 mouse_last.y = xevent->xmotion.y; | |
109 posted = SDL_PrivateMouseMotion(0, 1, deltax, deltay); | |
110 | |
111 if ( (xevent->xmotion.x < MOUSE_FUDGE_FACTOR) || | |
112 (xevent->xmotion.x > (w-MOUSE_FUDGE_FACTOR)) || | |
113 (xevent->xmotion.y < MOUSE_FUDGE_FACTOR) || | |
114 (xevent->xmotion.y > (h-MOUSE_FUDGE_FACTOR)) ) { | |
115 /* Get the events that have accumulated */ | |
116 while ( XCheckTypedEvent(SDL_Display, MotionNotify, xevent) ) { | |
117 deltax = xevent->xmotion.x - mouse_last.x; | |
118 deltay = xevent->xmotion.y - mouse_last.y; | |
119 #ifdef DEBUG_MOTION | |
120 printf("Extra mouse motion: %d,%d\n", deltax, deltay); | |
121 #endif | |
122 mouse_last.x = xevent->xmotion.x; | |
123 mouse_last.y = xevent->xmotion.y; | |
124 posted += SDL_PrivateMouseMotion(0, 1, deltax, deltay); | |
125 } | |
126 mouse_last.x = w/2; | |
127 mouse_last.y = h/2; | |
128 XWarpPointer(SDL_Display, None, SDL_Window, 0, 0, 0, 0, | |
129 mouse_last.x, mouse_last.y); | |
130 for ( i=0; i<10; ++i ) { | |
131 XMaskEvent(SDL_Display, PointerMotionMask, xevent); | |
132 if ( (xevent->xmotion.x > | |
133 (mouse_last.x-MOUSE_FUDGE_FACTOR)) && | |
134 (xevent->xmotion.x < | |
135 (mouse_last.x+MOUSE_FUDGE_FACTOR)) && | |
136 (xevent->xmotion.y > | |
137 (mouse_last.y-MOUSE_FUDGE_FACTOR)) && | |
138 (xevent->xmotion.y < | |
139 (mouse_last.y+MOUSE_FUDGE_FACTOR)) ) { | |
140 break; | |
141 } | |
142 #ifdef DEBUG_XEVENTS | |
143 printf("Lost mouse motion: %d,%d\n", xevent->xmotion.x, xevent->xmotion.y); | |
144 #endif | |
145 } | |
146 #ifdef DEBUG_XEVENTS | |
147 if ( i == 10 ) { | |
148 printf("Warning: didn't detect mouse warp motion\n"); | |
149 } | |
150 #endif | |
151 } | |
152 return(posted); | |
153 } | |
154 | |
155 static int X11_DispatchEvent(_THIS) | |
156 { | |
157 int posted; | |
158 XEvent xevent; | |
159 | |
160 XNextEvent(SDL_Display, &xevent); | |
161 | |
162 posted = 0; | |
163 switch (xevent.type) { | |
164 | |
165 /* Gaining mouse coverage? */ | |
166 case EnterNotify: { | |
167 #ifdef DEBUG_XEVENTS | |
168 printf("EnterNotify!\n"); | |
169 if ( xevent.xcrossing.mode == NotifyGrab ) | |
170 printf("Mode: NotifyGrab\n"); | |
171 if ( xevent.xcrossing.mode == NotifyUngrab ) | |
172 printf("Mode: NotifyUngrab\n"); | |
173 #endif | |
174 if ( (xevent.xcrossing.mode != NotifyGrab) && | |
175 (xevent.xcrossing.mode != NotifyUngrab) ) { | |
176 posted = SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS); | |
177 } | |
178 } | |
179 break; | |
180 | |
181 /* Losing mouse coverage? */ | |
182 case LeaveNotify: { | |
183 #ifdef DEBUG_XEVENTS | |
184 printf("LeaveNotify!\n"); | |
185 if ( xevent.xcrossing.mode == NotifyGrab ) | |
186 printf("Mode: NotifyGrab\n"); | |
187 if ( xevent.xcrossing.mode == NotifyUngrab ) | |
188 printf("Mode: NotifyUngrab\n"); | |
189 #endif | |
190 if ( (xevent.xcrossing.mode != NotifyGrab) && | |
191 (xevent.xcrossing.mode != NotifyUngrab) ) { | |
192 posted = SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS); | |
193 } | |
194 } | |
195 break; | |
196 | |
197 /* Gaining input focus? */ | |
198 case FocusIn: { | |
199 #ifdef DEBUG_XEVENTS | |
200 printf("FocusIn!\n"); | |
201 #endif | |
202 posted = SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS); | |
203 | |
204 /* Queue entry into fullscreen mode */ | |
205 switch_waiting = 0x01 | SDL_FULLSCREEN; | |
206 switch_time = SDL_GetTicks() + 1500; | |
207 } | |
208 break; | |
209 | |
210 /* Losing input focus? */ | |
211 case FocusOut: { | |
212 #ifdef DEBUG_XEVENTS | |
213 printf("FocusOut!\n"); | |
214 #endif | |
215 posted = SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS); | |
216 | |
217 /* Queue leaving fullscreen mode */ | |
218 switch_waiting = 0x01; | |
219 switch_time = SDL_GetTicks() + 200; | |
220 } | |
221 break; | |
222 | |
223 /* Generated upon EnterWindow and FocusIn */ | |
224 case KeymapNotify: { | |
14
c3e9d4a623c1
Fixed stuck keys when changing the video mode
Sam Lantinga <slouken@lokigames.com>
parents:
12
diff
changeset
|
225 #ifdef DEBUG_XEVENTS |
c3e9d4a623c1
Fixed stuck keys when changing the video mode
Sam Lantinga <slouken@lokigames.com>
parents:
12
diff
changeset
|
226 printf("KeymapNotify!\n"); |
c3e9d4a623c1
Fixed stuck keys when changing the video mode
Sam Lantinga <slouken@lokigames.com>
parents:
12
diff
changeset
|
227 #endif |
0 | 228 X11_SetKeyboardState(SDL_Display, xevent.xkeymap.key_vector); |
229 } | |
230 break; | |
231 | |
232 /* Mouse motion? */ | |
233 case MotionNotify: { | |
234 if ( SDL_VideoSurface ) { | |
235 if ( mouse_relative ) { | |
236 if ( using_dga & DGA_MOUSE ) { | |
237 #ifdef DEBUG_MOTION | |
238 printf("DGA motion: %d,%d\n", xevent.xmotion.x_root, xevent.xmotion.y_root); | |
239 #endif | |
240 posted = SDL_PrivateMouseMotion(0, 1, | |
241 xevent.xmotion.x_root, | |
242 xevent.xmotion.y_root); | |
243 } else { | |
244 posted = X11_WarpedMotion(this,&xevent); | |
245 } | |
246 } else { | |
247 posted = SDL_PrivateMouseMotion(0, 0, | |
248 xevent.xmotion.x, | |
249 xevent.xmotion.y); | |
250 } | |
251 } | |
252 } | |
253 break; | |
254 | |
255 /* Mouse button press? */ | |
256 case ButtonPress: { | |
257 posted = SDL_PrivateMouseButton(SDL_PRESSED, | |
258 xevent.xbutton.button, 0, 0); | |
259 } | |
260 break; | |
261 | |
262 /* Mouse button release? */ | |
263 case ButtonRelease: { | |
264 posted = SDL_PrivateMouseButton(SDL_RELEASED, | |
265 xevent.xbutton.button, 0, 0); | |
266 } | |
267 break; | |
268 | |
269 /* Key press? */ | |
270 case KeyPress: { | |
271 SDL_keysym keysym; | |
14
c3e9d4a623c1
Fixed stuck keys when changing the video mode
Sam Lantinga <slouken@lokigames.com>
parents:
12
diff
changeset
|
272 |
c3e9d4a623c1
Fixed stuck keys when changing the video mode
Sam Lantinga <slouken@lokigames.com>
parents:
12
diff
changeset
|
273 #ifdef DEBUG_XEVENTS |
c3e9d4a623c1
Fixed stuck keys when changing the video mode
Sam Lantinga <slouken@lokigames.com>
parents:
12
diff
changeset
|
274 printf("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode); |
c3e9d4a623c1
Fixed stuck keys when changing the video mode
Sam Lantinga <slouken@lokigames.com>
parents:
12
diff
changeset
|
275 #endif |
0 | 276 posted = SDL_PrivateKeyboard(SDL_PRESSED, |
277 X11_TranslateKey(SDL_Display, &xevent.xkey, | |
278 xevent.xkey.keycode, | |
279 &keysym)); | |
280 } | |
281 break; | |
282 | |
283 /* Key release? */ | |
284 case KeyRelease: { | |
285 SDL_keysym keysym; | |
286 | |
14
c3e9d4a623c1
Fixed stuck keys when changing the video mode
Sam Lantinga <slouken@lokigames.com>
parents:
12
diff
changeset
|
287 #ifdef DEBUG_XEVENTS |
c3e9d4a623c1
Fixed stuck keys when changing the video mode
Sam Lantinga <slouken@lokigames.com>
parents:
12
diff
changeset
|
288 printf("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode); |
c3e9d4a623c1
Fixed stuck keys when changing the video mode
Sam Lantinga <slouken@lokigames.com>
parents:
12
diff
changeset
|
289 #endif |
0 | 290 /* Check to see if this is a repeated key */ |
291 if ( ! X11_KeyRepeat(SDL_Display, &xevent) ) { | |
292 posted = SDL_PrivateKeyboard(SDL_RELEASED, | |
293 X11_TranslateKey(SDL_Display, &xevent.xkey, | |
294 xevent.xkey.keycode, | |
295 &keysym)); | |
296 } | |
297 } | |
298 break; | |
299 | |
300 /* Have we been iconified? */ | |
301 case UnmapNotify: { | |
302 #ifdef DEBUG_XEVENTS | |
303 printf("UnmapNotify!\n"); | |
304 #endif | |
305 /* If we're active, make ourselves inactive */ | |
306 if ( SDL_GetAppState() & SDL_APPACTIVE ) { | |
307 /* Swap out the gamma before we go inactive */ | |
308 X11_SwapVidModeGamma(this); | |
309 | |
310 /* Send an internal deactivate event */ | |
311 posted = SDL_PrivateAppActive(0, | |
312 SDL_APPACTIVE|SDL_APPINPUTFOCUS); | |
313 } | |
314 } | |
315 break; | |
316 | |
317 /* Have we been restored? */ | |
318 case MapNotify: { | |
319 #ifdef DEBUG_XEVENTS | |
320 printf("MapNotify!\n"); | |
321 #endif | |
322 /* If we're not active, make ourselves active */ | |
323 if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) { | |
324 /* Send an internal activate event */ | |
325 posted = SDL_PrivateAppActive(1, SDL_APPACTIVE); | |
326 | |
327 /* Now that we're active, swap the gamma back */ | |
328 X11_SwapVidModeGamma(this); | |
329 } | |
330 | |
331 if ( SDL_VideoSurface && | |
332 (SDL_VideoSurface->flags & SDL_FULLSCREEN) ) { | |
333 #ifdef GRAB_FULLSCREEN | |
334 X11_EnterFullScreen(this); | |
335 #else | |
336 /* Queue entry into fullscreen mode */ | |
337 switch_waiting = 0x01 | SDL_FULLSCREEN; | |
338 switch_time = SDL_GetTicks() + 1500; | |
339 #endif | |
340 } else { | |
341 X11_GrabInputNoLock(this, this->input_grab); | |
342 } | |
8
5574376c451d
Fixed relative motion checking after restore under X11
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
343 X11_CheckMouseModeNoLock(this); |
5574376c451d
Fixed relative motion checking after restore under X11
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
344 |
0 | 345 if ( SDL_VideoSurface ) { |
346 X11_RefreshDisplay(this); | |
347 } | |
348 } | |
349 break; | |
350 | |
351 /* Have we been resized or moved? */ | |
352 case ConfigureNotify: { | |
353 #ifdef DEBUG_XEVENTS | |
354 printf("ConfigureNotify! (resize: %dx%d)\n", xevent.xconfigure.width, xevent.xconfigure.height); | |
355 #endif | |
356 if ( SDL_VideoSurface ) { | |
357 if ((xevent.xconfigure.width != SDL_VideoSurface->w) || | |
358 (xevent.xconfigure.height != SDL_VideoSurface->h)) { | |
359 /* FIXME: Find a better fix for the bug with KDE 1.2 */ | |
360 if ( ! ((xevent.xconfigure.width == 32) && | |
361 (xevent.xconfigure.height == 32)) ) { | |
362 SDL_PrivateResize(xevent.xconfigure.width, | |
363 xevent.xconfigure.height); | |
364 } | |
365 } else { | |
366 /* OpenGL windows need to know about the change */ | |
367 if ( SDL_VideoSurface->flags & SDL_OPENGL ) { | |
368 SDL_PrivateExpose(); | |
369 } | |
370 } | |
371 } | |
372 } | |
373 break; | |
374 | |
375 /* Have we been requested to quit (or another client message?) */ | |
376 case ClientMessage: { | |
377 if ( (xevent.xclient.format == 32) && | |
378 (xevent.xclient.data.l[0] == WM_DELETE_WINDOW) ) | |
379 { | |
380 posted = SDL_PrivateQuit(); | |
381 } else | |
382 if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) { | |
383 SDL_SysWMmsg wmmsg; | |
384 | |
385 SDL_VERSION(&wmmsg.version); | |
386 wmmsg.subsystem = SDL_SYSWM_X11; | |
387 wmmsg.event.xevent = xevent; | |
388 posted = SDL_PrivateSysWMEvent(&wmmsg); | |
389 } | |
390 } | |
391 break; | |
392 | |
393 /* Do we need to refresh ourselves? */ | |
394 case Expose: { | |
395 #ifdef DEBUG_XEVENTS | |
396 printf("Expose (count = %d)\n", xevent.xexpose.count); | |
397 #endif | |
398 if ( SDL_VideoSurface && (xevent.xexpose.count == 0) ) { | |
399 if ( SDL_VideoSurface->flags & SDL_OPENGL ) { | |
400 SDL_PrivateExpose(); | |
401 } else { | |
402 X11_RefreshDisplay(this); | |
403 } | |
404 } | |
405 } | |
406 break; | |
407 | |
408 default: { | |
409 #ifdef DEBUG_XEVENTS | |
410 printf("Unhandled event %d\n", xevent.type); | |
411 #endif | |
412 /* Only post the event if we're watching for it */ | |
413 if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) { | |
414 SDL_SysWMmsg wmmsg; | |
415 | |
416 SDL_VERSION(&wmmsg.version); | |
417 wmmsg.subsystem = SDL_SYSWM_X11; | |
418 wmmsg.event.xevent = xevent; | |
419 posted = SDL_PrivateSysWMEvent(&wmmsg); | |
420 } | |
421 } | |
422 break; | |
423 } | |
424 return(posted); | |
425 } | |
426 | |
427 /* Ack! XPending() actually performs a blocking read if no events available */ | |
428 int X11_Pending(Display *display) | |
429 { | |
430 /* Flush the display connection and look to see if events are queued */ | |
431 XFlush(display); | |
432 if ( XEventsQueued(display, QueuedAlready) ) { | |
433 return(1); | |
434 } | |
435 | |
436 /* More drastic measures are required -- see if X is ready to talk */ | |
437 { | |
438 static struct timeval zero_time; /* static == 0 */ | |
439 int x11_fd; | |
440 fd_set fdset; | |
441 | |
442 x11_fd = ConnectionNumber(display); | |
443 FD_ZERO(&fdset); | |
444 FD_SET(x11_fd, &fdset); | |
445 if ( select(x11_fd+1, &fdset, NULL, NULL, &zero_time) == 1 ) { | |
446 return(XPending(display)); | |
447 } | |
448 } | |
449 | |
450 /* Oh well, nothing is ready .. */ | |
451 return(0); | |
452 } | |
453 | |
454 void X11_PumpEvents(_THIS) | |
455 { | |
456 int pending; | |
457 | |
458 /* Keep processing pending events */ | |
459 pending = 0; | |
460 while ( X11_Pending(SDL_Display) ) { | |
461 X11_DispatchEvent(this); | |
462 ++pending; | |
463 } | |
464 if ( switch_waiting ) { | |
465 Uint32 now; | |
466 | |
467 now = SDL_GetTicks(); | |
468 if ( pending || !SDL_VideoSurface ) { | |
469 /* Try again later... */ | |
470 if ( switch_waiting & SDL_FULLSCREEN ) { | |
471 switch_time = now + 1500; | |
472 } else { | |
473 switch_time = now + 200; | |
474 } | |
475 } else if ( now >= switch_time ) { | |
476 Uint32 go_fullscreen; | |
477 | |
478 go_fullscreen = switch_waiting & SDL_FULLSCREEN; | |
479 switch_waiting = 0; | |
480 if ( SDL_VideoSurface->flags & SDL_FULLSCREEN ) { | |
481 if ( go_fullscreen ) { | |
482 X11_EnterFullScreen(this); | |
483 } else { | |
484 X11_LeaveFullScreen(this); | |
485 } | |
486 } | |
487 /* Handle focus in/out when grabbed */ | |
488 if ( go_fullscreen ) { | |
489 X11_GrabInputNoLock(this, this->input_grab); | |
490 } else { | |
491 X11_GrabInputNoLock(this, SDL_GRAB_OFF); | |
492 } | |
493 X11_CheckMouseModeNoLock(this); | |
494 } | |
495 } | |
496 } | |
497 | |
498 void X11_InitKeymap(void) | |
499 { | |
500 int i; | |
501 | |
502 /* Odd keys used in international keyboards */ | |
503 for ( i=0; i<SDL_TABLESIZE(ODD_keymap); ++i ) | |
504 ODD_keymap[i] = SDLK_UNKNOWN; | |
505 | |
506 #ifdef XK_dead_circumflex | |
507 /* These X keysyms have 0xFE as the high byte */ | |
508 ODD_keymap[XK_dead_circumflex&0xFF] = SDLK_CARET; | |
509 #endif | |
510 | |
511 /* Map the miscellaneous keys */ | |
512 for ( i=0; i<SDL_TABLESIZE(MISC_keymap); ++i ) | |
513 MISC_keymap[i] = SDLK_UNKNOWN; | |
514 | |
515 /* These X keysyms have 0xFF as the high byte */ | |
516 MISC_keymap[XK_BackSpace&0xFF] = SDLK_BACKSPACE; | |
517 MISC_keymap[XK_Tab&0xFF] = SDLK_TAB; | |
518 MISC_keymap[XK_Clear&0xFF] = SDLK_CLEAR; | |
519 MISC_keymap[XK_Return&0xFF] = SDLK_RETURN; | |
520 MISC_keymap[XK_Pause&0xFF] = SDLK_PAUSE; | |
521 MISC_keymap[XK_Escape&0xFF] = SDLK_ESCAPE; | |
522 MISC_keymap[XK_Delete&0xFF] = SDLK_DELETE; | |
523 | |
524 MISC_keymap[XK_KP_0&0xFF] = SDLK_KP0; /* Keypad 0-9 */ | |
525 MISC_keymap[XK_KP_1&0xFF] = SDLK_KP1; | |
526 MISC_keymap[XK_KP_2&0xFF] = SDLK_KP2; | |
527 MISC_keymap[XK_KP_3&0xFF] = SDLK_KP3; | |
528 MISC_keymap[XK_KP_4&0xFF] = SDLK_KP4; | |
529 MISC_keymap[XK_KP_5&0xFF] = SDLK_KP5; | |
530 MISC_keymap[XK_KP_6&0xFF] = SDLK_KP6; | |
531 MISC_keymap[XK_KP_7&0xFF] = SDLK_KP7; | |
532 MISC_keymap[XK_KP_8&0xFF] = SDLK_KP8; | |
533 MISC_keymap[XK_KP_9&0xFF] = SDLK_KP9; | |
534 MISC_keymap[XK_KP_Insert&0xFF] = SDLK_KP0; | |
535 MISC_keymap[XK_KP_End&0xFF] = SDLK_KP1; | |
536 MISC_keymap[XK_KP_Down&0xFF] = SDLK_KP2; | |
537 MISC_keymap[XK_KP_Page_Down&0xFF] = SDLK_KP3; | |
538 MISC_keymap[XK_KP_Left&0xFF] = SDLK_KP4; | |
539 MISC_keymap[XK_KP_Begin&0xFF] = SDLK_KP5; | |
540 MISC_keymap[XK_KP_Right&0xFF] = SDLK_KP6; | |
541 MISC_keymap[XK_KP_Home&0xFF] = SDLK_KP7; | |
542 MISC_keymap[XK_KP_Up&0xFF] = SDLK_KP8; | |
543 MISC_keymap[XK_KP_Page_Up&0xFF] = SDLK_KP9; | |
544 MISC_keymap[XK_KP_Delete&0xFF] = SDLK_KP_PERIOD; | |
545 MISC_keymap[XK_KP_Decimal&0xFF] = SDLK_KP_PERIOD; | |
546 MISC_keymap[XK_KP_Divide&0xFF] = SDLK_KP_DIVIDE; | |
547 MISC_keymap[XK_KP_Multiply&0xFF] = SDLK_KP_MULTIPLY; | |
548 MISC_keymap[XK_KP_Subtract&0xFF] = SDLK_KP_MINUS; | |
549 MISC_keymap[XK_KP_Add&0xFF] = SDLK_KP_PLUS; | |
550 MISC_keymap[XK_KP_Enter&0xFF] = SDLK_KP_ENTER; | |
551 MISC_keymap[XK_KP_Equal&0xFF] = SDLK_KP_EQUALS; | |
552 | |
553 MISC_keymap[XK_Up&0xFF] = SDLK_UP; | |
554 MISC_keymap[XK_Down&0xFF] = SDLK_DOWN; | |
555 MISC_keymap[XK_Right&0xFF] = SDLK_RIGHT; | |
556 MISC_keymap[XK_Left&0xFF] = SDLK_LEFT; | |
557 MISC_keymap[XK_Insert&0xFF] = SDLK_INSERT; | |
558 MISC_keymap[XK_Home&0xFF] = SDLK_HOME; | |
559 MISC_keymap[XK_End&0xFF] = SDLK_END; | |
560 MISC_keymap[XK_Page_Up&0xFF] = SDLK_PAGEUP; | |
561 MISC_keymap[XK_Page_Down&0xFF] = SDLK_PAGEDOWN; | |
562 | |
563 MISC_keymap[XK_F1&0xFF] = SDLK_F1; | |
564 MISC_keymap[XK_F2&0xFF] = SDLK_F2; | |
565 MISC_keymap[XK_F3&0xFF] = SDLK_F3; | |
566 MISC_keymap[XK_F4&0xFF] = SDLK_F4; | |
567 MISC_keymap[XK_F5&0xFF] = SDLK_F5; | |
568 MISC_keymap[XK_F6&0xFF] = SDLK_F6; | |
569 MISC_keymap[XK_F7&0xFF] = SDLK_F7; | |
570 MISC_keymap[XK_F8&0xFF] = SDLK_F8; | |
571 MISC_keymap[XK_F9&0xFF] = SDLK_F9; | |
572 MISC_keymap[XK_F10&0xFF] = SDLK_F10; | |
573 MISC_keymap[XK_F11&0xFF] = SDLK_F11; | |
574 MISC_keymap[XK_F12&0xFF] = SDLK_F12; | |
575 MISC_keymap[XK_F13&0xFF] = SDLK_F13; | |
576 MISC_keymap[XK_F14&0xFF] = SDLK_F14; | |
577 MISC_keymap[XK_F15&0xFF] = SDLK_F15; | |
578 | |
579 MISC_keymap[XK_Num_Lock&0xFF] = SDLK_NUMLOCK; | |
580 MISC_keymap[XK_Caps_Lock&0xFF] = SDLK_CAPSLOCK; | |
581 MISC_keymap[XK_Scroll_Lock&0xFF] = SDLK_SCROLLOCK; | |
582 MISC_keymap[XK_Shift_R&0xFF] = SDLK_RSHIFT; | |
583 MISC_keymap[XK_Shift_L&0xFF] = SDLK_LSHIFT; | |
584 MISC_keymap[XK_Control_R&0xFF] = SDLK_RCTRL; | |
585 MISC_keymap[XK_Control_L&0xFF] = SDLK_LCTRL; | |
586 MISC_keymap[XK_Alt_R&0xFF] = SDLK_RALT; | |
587 MISC_keymap[XK_Alt_L&0xFF] = SDLK_LALT; | |
588 MISC_keymap[XK_Meta_R&0xFF] = SDLK_RMETA; | |
589 MISC_keymap[XK_Meta_L&0xFF] = SDLK_LMETA; | |
590 MISC_keymap[XK_Super_L&0xFF] = SDLK_LSUPER; /* Left "Windows" */ | |
591 MISC_keymap[XK_Super_R&0xFF] = SDLK_RSUPER; /* Right "Windows */ | |
592 MISC_keymap[XK_Mode_switch&0xFF] = SDLK_MODE; /* "Alt Gr" key */ | |
593 MISC_keymap[XK_Multi_key&0xFF] = SDLK_COMPOSE; /* Multi-key compose */ | |
594 | |
595 MISC_keymap[XK_Help&0xFF] = SDLK_HELP; | |
596 MISC_keymap[XK_Print&0xFF] = SDLK_PRINT; | |
597 MISC_keymap[XK_Sys_Req&0xFF] = SDLK_SYSREQ; | |
598 MISC_keymap[XK_Break&0xFF] = SDLK_BREAK; | |
599 MISC_keymap[XK_Menu&0xFF] = SDLK_MENU; | |
600 MISC_keymap[XK_Hyper_R&0xFF] = SDLK_MENU; /* Windows "Menu" key */ | |
601 } | |
602 | |
603 SDL_keysym *X11_TranslateKey(Display *display, XKeyEvent *xkey, KeyCode kc, | |
604 SDL_keysym *keysym) | |
605 { | |
606 KeySym xsym; | |
607 | |
608 /* Get the raw keyboard scancode */ | |
609 keysym->scancode = kc; | |
610 xsym = XKeycodeToKeysym(display, kc, 0); | |
611 #ifdef DEBUG_KEYS | |
612 fprintf(stderr, "Translating key 0x%.4x (%d)\n", xsym, kc); | |
613 #endif | |
614 /* Get the translated SDL virtual keysym */ | |
615 keysym->sym = SDLK_UNKNOWN; | |
616 if ( xsym ) { | |
617 switch (xsym>>8) { | |
618 case 0x1005FF: | |
619 #ifdef SunXK_F36 | |
620 if ( xsym == SunXK_F36 ) | |
621 keysym->sym = SDLK_F11; | |
622 #endif | |
623 #ifdef SunXK_F37 | |
624 if ( xsym == SunXK_F37 ) | |
625 keysym->sym = SDLK_F12; | |
626 #endif | |
627 break; | |
628 case 0x00: /* Latin 1 */ | |
629 case 0x01: /* Latin 2 */ | |
630 case 0x02: /* Latin 3 */ | |
631 case 0x03: /* Latin 4 */ | |
632 case 0x04: /* Katakana */ | |
633 case 0x05: /* Arabic */ | |
634 case 0x06: /* Cyrillic */ | |
635 case 0x07: /* Greek */ | |
636 case 0x08: /* Technical */ | |
637 case 0x0A: /* Publishing */ | |
638 case 0x0C: /* Hebrew */ | |
639 case 0x0D: /* Thai */ | |
640 keysym->sym = (SDLKey)(xsym&0xFF); | |
641 /* Map capital letter syms to lowercase */ | |
642 if ((keysym->sym >= 'A')&&(keysym->sym <= 'Z')) | |
643 keysym->sym += ('a'-'A'); | |
644 break; | |
645 case 0xFE: | |
646 keysym->sym = ODD_keymap[xsym&0xFF]; | |
647 break; | |
648 case 0xFF: | |
649 keysym->sym = MISC_keymap[xsym&0xFF]; | |
650 break; | |
651 default: | |
652 fprintf(stderr, | |
653 "X11: Unknown xsym, sym = 0x%04x\n", | |
654 (unsigned int)xsym); | |
655 break; | |
656 } | |
657 } else { | |
658 /* X11 doesn't know how to translate the key! */ | |
659 switch (kc) { | |
660 /* Caution: | |
661 These keycodes are from the Microsoft Keyboard | |
662 */ | |
663 case 115: | |
664 keysym->sym = SDLK_LSUPER; | |
665 break; | |
666 case 116: | |
667 keysym->sym = SDLK_RSUPER; | |
668 break; | |
669 case 117: | |
670 keysym->sym = SDLK_MENU; | |
671 break; | |
672 default: | |
673 /* | |
674 * no point in an error message; happens for | |
675 * several keys when we get a keymap notify | |
676 */ | |
677 break; | |
678 } | |
679 } | |
680 keysym->mod = KMOD_NONE; | |
681 | |
682 /* If UNICODE is on, get the UNICODE value for the key */ | |
683 keysym->unicode = 0; | |
684 if ( SDL_TranslateUNICODE && xkey ) { | |
685 static XComposeStatus state; | |
686 /* Until we handle the IM protocol, use XLookupString() */ | |
687 unsigned char keybuf[32]; | |
688 | |
689 #define BROKEN_XFREE86_INTERNATIONAL_KBD | |
690 /* This appears to be a magical flag that is used with AltGr on | |
691 international keyboards to signal alternate key translations. | |
692 The flag doesn't show up when in fullscreen mode (?) | |
693 FIXME: Check to see if this code is safe for other servers. | |
694 */ | |
695 #ifdef BROKEN_XFREE86_INTERNATIONAL_KBD | |
696 /* Work around what appears to be a bug in XFree86 */ | |
697 if ( SDL_GetModState() & KMOD_MODE ) { | |
698 xkey->state |= (1<<13); | |
699 } | |
700 #endif | |
701 /* Look up the translated value for the key event */ | |
702 if ( XLookupString(xkey, (char *)keybuf, sizeof(keybuf), | |
703 NULL, &state) ) { | |
704 /* | |
705 * FIXME,: XLookupString() may yield more than one | |
706 * character, so we need a mechanism to allow for | |
707 * this (perhaps generate null keypress events with | |
708 * a unicode value) | |
709 */ | |
710 keysym->unicode = keybuf[0]; | |
711 } | |
712 } | |
713 return(keysym); | |
714 } | |
715 | |
716 /* X11 modifier masks for various keys */ | |
717 static unsigned meta_l_mask, meta_r_mask, alt_l_mask, alt_r_mask; | |
718 static unsigned num_mask, mode_switch_mask; | |
719 | |
720 static void get_modifier_masks(Display *display) | |
721 { | |
722 static unsigned got_masks; | |
723 int i, j; | |
724 XModifierKeymap *xmods; | |
725 unsigned n; | |
726 | |
727 if(got_masks) | |
728 return; | |
729 | |
730 xmods = XGetModifierMapping(display); | |
731 n = xmods->max_keypermod; | |
732 for(i = 3; i < 8; i++) { | |
733 for(j = 0; j < n; j++) { | |
734 KeyCode kc = xmods->modifiermap[i * n + j]; | |
735 KeySym ks = XKeycodeToKeysym(display, kc, 0); | |
736 unsigned mask = 1 << i; | |
737 switch(ks) { | |
738 case XK_Num_Lock: | |
739 num_mask = mask; break; | |
740 case XK_Alt_L: | |
741 alt_l_mask = mask; break; | |
742 case XK_Alt_R: | |
743 alt_r_mask = mask; break; | |
744 case XK_Meta_L: | |
745 meta_l_mask = mask; break; | |
746 case XK_Meta_R: | |
747 meta_r_mask = mask; break; | |
748 case XK_Mode_switch: | |
749 mode_switch_mask = mask; break; | |
750 } | |
751 } | |
752 } | |
753 XFreeModifiermap(xmods); | |
754 got_masks = 1; | |
755 } | |
756 | |
757 | |
758 /* | |
759 * This function is semi-official; it is not officially exported and should | |
760 * not be considered part of the SDL API, but may be used by client code | |
761 * that *really* needs it (including legacy code). | |
762 * It is slow, though, and should be avoided if possible. | |
763 * | |
764 * Note that it isn't completely accurate either; in particular, multi-key | |
765 * sequences (dead accents, compose key sequences) will not work since the | |
766 * state has been irrevocably lost. | |
767 */ | |
768 Uint16 X11_KeyToUnicode(SDLKey keysym, SDLMod modifiers) | |
769 { | |
770 struct SDL_VideoDevice *this = current_video; | |
771 char keybuf[32]; | |
772 int i; | |
773 KeySym xsym = 0; | |
774 XKeyEvent xkey; | |
775 Uint16 unicode; | |
776 | |
777 if ( !this || !SDL_Display ) { | |
778 return 0; | |
779 } | |
780 | |
781 memset(&xkey, 0, sizeof(xkey)); | |
782 xkey.display = SDL_Display; | |
783 | |
784 xsym = keysym; /* last resort if not found */ | |
785 for (i = 0; i < 256; ++i) { | |
786 if ( MISC_keymap[i] == keysym ) { | |
787 xsym = 0xFF00 | i; | |
788 break; | |
789 } else if ( ODD_keymap[i] == keysym ) { | |
790 xsym = 0xFE00 | i; | |
791 break; | |
792 } | |
793 } | |
794 | |
795 xkey.keycode = XKeysymToKeycode(xkey.display, xsym); | |
796 | |
797 get_modifier_masks(SDL_Display); | |
798 if(modifiers & KMOD_SHIFT) | |
799 xkey.state |= ShiftMask; | |
800 if(modifiers & KMOD_CAPS) | |
801 xkey.state |= LockMask; | |
802 if(modifiers & KMOD_CTRL) | |
803 xkey.state |= ControlMask; | |
804 if(modifiers & KMOD_MODE) | |
805 xkey.state |= mode_switch_mask; | |
806 if(modifiers & KMOD_LALT) | |
807 xkey.state |= alt_l_mask; | |
808 if(modifiers & KMOD_RALT) | |
809 xkey.state |= alt_r_mask; | |
810 if(modifiers & KMOD_LMETA) | |
811 xkey.state |= meta_l_mask; | |
812 if(modifiers & KMOD_RMETA) | |
813 xkey.state |= meta_r_mask; | |
814 if(modifiers & KMOD_NUM) | |
815 xkey.state |= num_mask; | |
816 | |
817 unicode = 0; | |
818 if ( XLookupString(&xkey, keybuf, sizeof(keybuf), NULL, NULL) ) | |
819 unicode = (unsigned char)keybuf[0]; | |
820 return(unicode); | |
821 } | |
822 | |
823 /* | |
824 * Called when focus is regained, to read the keyboard state and generate | |
825 * synthetic keypress/release events. | |
826 * key_vec is a bit vector of keycodes (256 bits) | |
827 */ | |
828 void X11_SetKeyboardState(Display *display, const char *key_vec) | |
829 { | |
830 char keys_return[32]; | |
831 int i, gen_event; | |
832 KeyCode xcode[SDLK_LAST]; | |
833 Uint8 new_kstate[SDLK_LAST]; | |
834 Uint8 *kstate = SDL_GetKeyState(NULL); | |
835 | |
836 /* The first time the window is mapped, we initialize key state */ | |
837 if ( ! key_vec ) { | |
838 key_vec = keys_return; | |
839 XQueryKeymap(display, keys_return); | |
840 gen_event = 0; | |
841 } else { | |
842 gen_event = 1; | |
843 } | |
844 | |
845 /* Zero the new state and generate it */ | |
846 memset(new_kstate, 0, sizeof(new_kstate)); | |
847 /* | |
848 * An obvious optimisation is to check entire longwords at a time in | |
849 * both loops, but we can't be sure the arrays are aligned so it's not | |
850 * worth the extra complexity | |
851 */ | |
852 for(i = 0; i < 32; i++) { | |
853 int j; | |
854 if(!key_vec[i]) | |
855 continue; | |
856 for(j = 0; j < 8; j++) { | |
857 if(key_vec[i] & (1 << j)) { | |
858 SDL_keysym sk; | |
859 KeyCode kc = i << 3 | j; | |
860 X11_TranslateKey(display, NULL, kc, &sk); | |
861 new_kstate[sk.sym] = 1; | |
862 xcode[sk.sym] = kc; | |
863 } | |
864 } | |
865 } | |
866 for(i = SDLK_FIRST+1; i < SDLK_LAST; i++) { | |
867 int st; | |
868 SDL_keysym sk; | |
869 | |
870 if(kstate[i] == new_kstate[i]) | |
871 continue; | |
872 /* | |
873 * Send a fake keyboard event correcting the difference between | |
874 * SDL's keyboard state and the actual. Note that there is no | |
875 * way to find out the scancode for key releases, but since all | |
876 * keys are released when focus is lost only keypresses should | |
877 * be sent here | |
878 */ | |
879 st = new_kstate[i] ? SDL_PRESSED : SDL_RELEASED; | |
880 memset(&sk, 0, sizeof(sk)); | |
881 sk.sym = i; | |
882 sk.scancode = xcode[i]; /* only valid for key press */ | |
883 if ( gen_event ) { | |
884 SDL_PrivateKeyboard(st, &sk); | |
885 } else { | |
886 kstate[i] = new_kstate[i]; | |
887 } | |
888 } | |
889 } | |
890 | |
891 void X11_InitOSKeymap(_THIS) | |
892 { | |
893 X11_InitKeymap(); | |
894 } | |
895 |