This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_x11events.c
1218 lines (1113 loc) · 40.2 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2006 Sam Lantinga
4
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
8
version 2.1 of the License, or (at your option) any later version.
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Lesser General Public License for more details.
14
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19
Sam Lantinga
20
slouken@libsdl.org
21
*/
22
#include "SDL_config.h"
23
24
25
26
27
28
29
30
31
32
/* Handle the event stream, converting X11 events into SDL events */
#include <setjmp.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#ifdef __SVR4
#include <X11/Sunkeysym.h>
#endif
33
#include <sys/types.h>
34
#include <sys/time.h>
35
#include <unistd.h>
36
37
#include "SDL_timer.h"
38
#include "SDL_syswm.h"
39
40
41
#include "../SDL_sysvideo.h"
#include "../../events/SDL_sysevents.h"
#include "../../events/SDL_events_c.h"
42
43
44
45
46
47
48
49
50
51
#include "SDL_x11video.h"
#include "SDL_x11dga_c.h"
#include "SDL_x11modes_c.h"
#include "SDL_x11image_c.h"
#include "SDL_x11gamma_c.h"
#include "SDL_x11wm_c.h"
#include "SDL_x11mouse_c.h"
#include "SDL_x11events_c.h"
52
53
54
/* Define this if you want to debug X11 events */
/*#define DEBUG_XEVENTS*/
55
56
57
/* The translation tables from an X11 keysym to a SDL keysym */
static SDLKey ODD_keymap[256];
static SDLKey MISC_keymap[256];
58
SDLKey X11_TranslateKeycode (Display * display, KeyCode kc);
59
60
61
#ifdef X_HAVE_UTF8_STRING
62
63
Uint32
Utf8ToUcs4 (const Uint8 * utf8)
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
Uint32 c;
int i = 1;
int noOctets = 0;
int firstOctetMask = 0;
unsigned char firstOctet = utf8[0];
if (firstOctet < 0x80) {
/*
Characters in the range:
00000000 to 01111111 (ASCII Range)
are stored in one octet:
0xxxxxxx (The same as its ASCII representation)
The least 6 significant bits of the first octet is the most 6 significant nonzero bits
of the UCS4 representation.
*/
noOctets = 1;
firstOctetMask = 0x7F; /* 0(1111111) - The most significant bit is ignored */
} else if ((firstOctet & 0xE0) /* get the most 3 significant bits by AND'ing with 11100000 */
== 0xC0) { /* see if those 3 bits are 110. If so, the char is in this range */
/*
Characters in the range:
00000000 10000000 to 00000111 11111111
are stored in two octets:
110xxxxx 10xxxxxx
The least 5 significant bits of the first octet is the most 5 significant nonzero bits
of the UCS4 representation.
*/
noOctets = 2;
firstOctetMask = 0x1F; /* 000(11111) - The most 3 significant bits are ignored */
} else if ((firstOctet & 0xF0) /* get the most 4 significant bits by AND'ing with 11110000 */
== 0xE0) { /* see if those 4 bits are 1110. If so, the char is in this range */
/*
Characters in the range:
00001000 00000000 to 11111111 11111111
are stored in three octets:
1110xxxx 10xxxxxx 10xxxxxx
The least 4 significant bits of the first octet is the most 4 significant nonzero bits
of the UCS4 representation.
*/
noOctets = 3;
firstOctetMask = 0x0F; /* 0000(1111) - The most 4 significant bits are ignored */
} else if ((firstOctet & 0xF8) /* get the most 5 significant bits by AND'ing with 11111000 */
== 0xF0) { /* see if those 5 bits are 11110. If so, the char is in this range */
/*
Characters in the range:
00000001 00000000 00000000 to 00011111 11111111 11111111
are stored in four octets:
11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
The least 3 significant bits of the first octet is the most 3 significant nonzero bits
of the UCS4 representation.
*/
noOctets = 4;
firstOctetMask = 0x07; /* 11110(111) - The most 5 significant bits are ignored */
} else if ((firstOctet & 0xFC) /* get the most 6 significant bits by AND'ing with 11111100 */
== 0xF8) { /* see if those 6 bits are 111110. If so, the char is in this range */
/*
Characters in the range:
00000000 00100000 00000000 00000000 to
00000011 11111111 11111111 11111111
are stored in five octets:
111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
The least 2 significant bits of the first octet is the most 2 significant nonzero bits
of the UCS4 representation.
*/
noOctets = 5;
firstOctetMask = 0x03; /* 111110(11) - The most 6 significant bits are ignored */
} else if ((firstOctet & 0xFE) /* get the most 7 significant bits by AND'ing with 11111110 */
== 0xFC) { /* see if those 7 bits are 1111110. If so, the char is in this range */
/*
Characters in the range:
00000100 00000000 00000000 00000000 to
01111111 11111111 11111111 11111111
are stored in six octets:
1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
The least significant bit of the first octet is the most significant nonzero bit
of the UCS4 representation.
*/
noOctets = 6;
firstOctetMask = 0x01; /* 1111110(1) - The most 7 significant bits are ignored */
} else
return 0; /* The given chunk is not a valid UTF-8 encoded Unicode character */
/*
The least noOctets significant bits of the first octet is the most 2 significant nonzero bits
of the UCS4 representation.
The first 6 bits of the UCS4 representation is the least 8-noOctets-1 significant bits of
firstOctet if the character is not ASCII. If so, it's the least 7 significant bits of firstOctet.
This done by AND'ing firstOctet with its mask to trim the bits used for identifying the
number of continuing octets (if any) and leave only the free bits (the x's)
Sample:
1-octet: 0xxxxxxx & 01111111 = 0xxxxxxx
2-octets: 110xxxxx & 00011111 = 000xxxxx
*/
c = firstOctet & firstOctetMask;
/* Now, start filling c.ucs4 with the bits from the continuing octets from utf8. */
for (i = 1; i < noOctets; i++) {
/* A valid continuing octet is of the form 10xxxxxx */
if ((utf8[i] & 0xC0) /* get the most 2 significant bits by AND'ing with 11000000 */
!=0x80)
/* see if those 2 bits are 10. If not, the is a malformed sequence. */
/*The given chunk is a partial sequence at the end of a string that could
begin a valid character */
return 0;
/* Make room for the next 6-bits */
c <<= 6;
/*
Take only the least 6 significance bits of the current octet (utf8[i]) and fill the created room
of c.ucs4 with them.
This done by AND'ing utf8[i] with 00111111 and the OR'ing the result with c.ucs4.
*/
c |= utf8[i] & 0x3F;
}
return c;
180
181
}
#endif
182
183
184
185
/* Check to see if this is a repeated key.
(idea shamelessly lifted from GII -- thanks guys! :)
*/
186
187
static int
X11_KeyRepeat (Display * display, XEvent * event)
188
{
189
190
191
192
193
194
195
196
197
198
199
200
201
202
XEvent peekevent;
int repeated;
repeated = 0;
if (XPending (display)) {
XPeekEvent (display, &peekevent);
if ((peekevent.type == KeyPress) &&
(peekevent.xkey.keycode == event->xkey.keycode) &&
((peekevent.xkey.time - event->xkey.time) < 2)) {
repeated = 1;
XNextEvent (display, &peekevent);
}
}
return (repeated);
203
204
205
206
207
208
209
210
211
}
/* Note: The X server buffers and accumulates mouse motion events, so
the motion event generated by the warp may not appear exactly as we
expect it to. We work around this (and improve performance) by only
warping the pointer when it reaches the edge, and then wait for it.
*/
#define MOUSE_FUDGE_FACTOR 8
212
213
static __inline__ int
X11_WarpedMotion (_THIS, XEvent * xevent)
214
{
215
216
217
218
219
220
221
222
int w, h, i;
int deltax, deltay;
int posted;
w = SDL_VideoSurface->w;
h = SDL_VideoSurface->h;
deltax = xevent->xmotion.x - mouse_last.x;
deltay = xevent->xmotion.y - mouse_last.y;
223
#ifdef DEBUG_MOTION
224
printf ("Warped mouse motion: %d,%d\n", deltax, deltay);
225
#endif
226
227
228
229
230
231
232
233
234
235
236
237
mouse_last.x = xevent->xmotion.x;
mouse_last.y = xevent->xmotion.y;
posted = SDL_PrivateMouseMotion (0, 1, deltax, deltay);
if ((xevent->xmotion.x < MOUSE_FUDGE_FACTOR) ||
(xevent->xmotion.x > (w - MOUSE_FUDGE_FACTOR)) ||
(xevent->xmotion.y < MOUSE_FUDGE_FACTOR) ||
(xevent->xmotion.y > (h - MOUSE_FUDGE_FACTOR))) {
/* Get the events that have accumulated */
while (XCheckTypedEvent (SDL_Display, MotionNotify, xevent)) {
deltax = xevent->xmotion.x - mouse_last.x;
deltay = xevent->xmotion.y - mouse_last.y;
238
#ifdef DEBUG_MOTION
239
printf ("Extra mouse motion: %d,%d\n", deltax, deltay);
240
#endif
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
mouse_last.x = xevent->xmotion.x;
mouse_last.y = xevent->xmotion.y;
posted += SDL_PrivateMouseMotion (0, 1, deltax, deltay);
}
mouse_last.x = w / 2;
mouse_last.y = h / 2;
XWarpPointer (SDL_Display, None, SDL_Window, 0, 0, 0, 0,
mouse_last.x, mouse_last.y);
for (i = 0; i < 10; ++i) {
XMaskEvent (SDL_Display, PointerMotionMask, xevent);
if ((xevent->xmotion.x >
(mouse_last.x - MOUSE_FUDGE_FACTOR)) &&
(xevent->xmotion.x <
(mouse_last.x + MOUSE_FUDGE_FACTOR)) &&
(xevent->xmotion.y >
(mouse_last.y - MOUSE_FUDGE_FACTOR)) &&
(xevent->xmotion.y < (mouse_last.y + MOUSE_FUDGE_FACTOR))) {
break;
}
260
#ifdef DEBUG_XEVENTS
261
262
printf ("Lost mouse motion: %d,%d\n", xevent->xmotion.x,
xevent->xmotion.y);
263
#endif
264
}
265
#ifdef DEBUG_XEVENTS
266
267
268
if (i == 10) {
printf ("Warning: didn't detect mouse warp motion\n");
}
269
#endif
270
271
}
return (posted);
272
273
}
274
275
static int
X11_DispatchEvent (_THIS)
276
{
277
278
int posted;
XEvent xevent;
279
280
281
SDL_memset (&xevent, '\0', sizeof (XEvent)); /* valgrind fix. --ryan. */
XNextEvent (SDL_Display, &xevent);
282
283
284
posted = 0;
switch (xevent.type) {
285
286
287
288
/* Gaining mouse coverage? */
case EnterNotify:
{
289
#ifdef DEBUG_XEVENTS
290
291
292
293
294
295
printf ("EnterNotify! (%d,%d)\n", xevent.xcrossing.x,
xevent.xcrossing.y);
if (xevent.xcrossing.mode == NotifyGrab)
printf ("Mode: NotifyGrab\n");
if (xevent.xcrossing.mode == NotifyUngrab)
printf ("Mode: NotifyUngrab\n");
296
#endif
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
if ((xevent.xcrossing.mode != NotifyGrab) &&
(xevent.xcrossing.mode != NotifyUngrab)) {
if (SDL_CurrentWindow.input_grab == SDL_GRAB_OFF) {
posted = SDL_PrivateAppActive (1, SDL_APPMOUSEFOCUS);
}
posted = SDL_PrivateMouseMotion (0, 0,
xevent.xcrossing.x,
xevent.xcrossing.y);
}
}
break;
/* Losing mouse coverage? */
case LeaveNotify:
{
312
#ifdef DEBUG_XEVENTS
313
314
315
316
317
318
printf ("LeaveNotify! (%d,%d)\n", xevent.xcrossing.x,
xevent.xcrossing.y);
if (xevent.xcrossing.mode == NotifyGrab)
printf ("Mode: NotifyGrab\n");
if (xevent.xcrossing.mode == NotifyUngrab)
printf ("Mode: NotifyUngrab\n");
319
#endif
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
if ((xevent.xcrossing.mode != NotifyGrab) &&
(xevent.xcrossing.mode != NotifyUngrab) &&
(xevent.xcrossing.detail != NotifyInferior)) {
if (SDL_CurrentWindow.input_grab == SDL_GRAB_OFF) {
posted = SDL_PrivateAppActive (0, SDL_APPMOUSEFOCUS);
} else {
posted = SDL_PrivateMouseMotion (0, 0,
xevent.xcrossing.x,
xevent.xcrossing.y);
}
}
}
break;
/* Gaining input focus? */
case FocusIn:
{
337
#ifdef DEBUG_XEVENTS
338
printf ("FocusIn!\n");
339
#endif
340
posted = SDL_PrivateAppActive (1, SDL_APPINPUTFOCUS);
341
342
#ifdef X_HAVE_UTF8_STRING
343
344
345
if (SDL_IC != NULL) {
XSetICFocus (SDL_IC);
}
346
#endif
347
348
349
350
351
352
353
354
355
/* Queue entry into fullscreen mode */
switch_waiting = 0x01 | SDL_FULLSCREEN;
switch_time = SDL_GetTicks () + 1500;
}
break;
/* Losing input focus? */
case FocusOut:
{
356
#ifdef DEBUG_XEVENTS
357
printf ("FocusOut!\n");
358
#endif
359
posted = SDL_PrivateAppActive (0, SDL_APPINPUTFOCUS);
360
361
#ifdef X_HAVE_UTF8_STRING
362
363
364
if (SDL_IC != NULL) {
XUnsetICFocus (SDL_IC);
}
365
#endif
366
367
368
369
370
371
372
373
374
/* Queue leaving fullscreen mode */
switch_waiting = 0x01;
switch_time = SDL_GetTicks () + 200;
}
break;
/* Generated upon EnterWindow and FocusIn */
case KeymapNotify:
{
375
#ifdef DEBUG_XEVENTS
376
printf ("KeymapNotify!\n");
377
#endif
378
379
380
381
382
383
384
385
386
387
X11_SetKeyboardState (SDL_Display, xevent.xkeymap.key_vector);
}
break;
/* Mouse motion? */
case MotionNotify:
{
if (SDL_VideoSurface) {
if (mouse_relative) {
if (using_dga & DGA_MOUSE) {
388
#ifdef DEBUG_MOTION
389
390
printf ("DGA motion: %d,%d\n",
xevent.xmotion.x_root, xevent.xmotion.y_root);
391
#endif
392
393
394
395
396
397
398
399
400
401
posted = SDL_PrivateMouseMotion (0, 1,
xevent.
xmotion.
x_root,
xevent.
xmotion.y_root);
} else {
posted = X11_WarpedMotion (_this, &xevent);
}
} else {
402
#ifdef DEBUG_MOTION
403
404
printf ("X11 motion: %d,%d\n", xevent.xmotion.x,
xevent.xmotion.y);
405
#endif
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
posted = SDL_PrivateMouseMotion (0, 0,
xevent.xmotion.x,
xevent.xmotion.y);
}
}
}
break;
/* Mouse button press? */
case ButtonPress:
{
posted = SDL_PrivateMouseButton (SDL_PRESSED,
xevent.xbutton.button, 0, 0);
}
break;
/* Mouse button release? */
case ButtonRelease:
{
posted = SDL_PrivateMouseButton (SDL_RELEASED,
xevent.xbutton.button, 0, 0);
}
break;
/* Key press? */
case KeyPress:
{
static SDL_keysym saved_keysym;
SDL_keysym keysym;
KeyCode keycode = xevent.xkey.keycode;
436
437
#ifdef DEBUG_XEVENTS
438
printf ("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
439
#endif
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/* Get the translated SDL virtual keysym */
if (keycode) {
keysym.scancode = keycode;
keysym.sym = X11_TranslateKeycode (SDL_Display, keycode);
keysym.mod = KMOD_NONE;
keysym.unicode = 0;
} else {
keysym = saved_keysym;
}
/* If we're not doing translation, we're done! */
if (!SDL_TranslateUNICODE) {
posted = SDL_PrivateKeyboard (SDL_PRESSED, &keysym);
break;
}
if (XFilterEvent (&xevent, None)) {
if (xevent.xkey.keycode) {
posted = SDL_PrivateKeyboard (SDL_PRESSED, &keysym);
} else {
/* Save event to be associated with IM text
In 1.3 we'll have a text event instead.. */
saved_keysym = keysym;
}
break;
}
/* Look up the translated value for the key event */
468
#ifdef X_HAVE_UTF8_STRING
469
470
471
472
473
474
475
476
477
478
if (SDL_IC != NULL) {
static Status state;
/* A UTF-8 character can be at most 6 bytes */
char keybuf[6];
if (Xutf8LookupString (SDL_IC, &xevent.xkey,
keybuf, sizeof (keybuf),
NULL, &state)) {
keysym.unicode = Utf8ToUcs4 ((Uint8 *) keybuf);
}
} else
479
#endif
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
{
static XComposeStatus state;
char keybuf[32];
if (XLookupString (&xevent.xkey,
keybuf, sizeof (keybuf), NULL, &state)) {
/*
* FIXME: XLookupString() may yield more than one
* character, so we need a mechanism to allow for
* this (perhaps null keypress events with a
* unicode value)
*/
keysym.unicode = (Uint8) keybuf[0];
}
}
posted = SDL_PrivateKeyboard (SDL_PRESSED, &keysym);
}
break;
/* Key release? */
case KeyRelease:
{
SDL_keysym keysym;
KeyCode keycode = xevent.xkey.keycode;
504
505
#ifdef DEBUG_XEVENTS
506
printf ("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
507
#endif
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/* Check to see if this is a repeated key */
if (X11_KeyRepeat (SDL_Display, &xevent)) {
break;
}
/* Get the translated SDL virtual keysym */
keysym.scancode = keycode;
keysym.sym = X11_TranslateKeycode (SDL_Display, keycode);
keysym.mod = KMOD_NONE;
keysym.unicode = 0;
posted = SDL_PrivateKeyboard (SDL_RELEASED, &keysym);
}
break;
/* Have we been iconified? */
case UnmapNotify:
{
526
#ifdef DEBUG_XEVENTS
527
printf ("UnmapNotify!\n");
528
#endif
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/* If we're active, make ourselves inactive */
if (SDL_GetAppState () & SDL_APPACTIVE) {
/* Swap out the gamma before we go inactive */
X11_SwapVidModeGamma (_this);
/* Send an internal deactivate event */
posted = SDL_PrivateAppActive (0,
SDL_APPACTIVE |
SDL_APPINPUTFOCUS);
}
}
break;
/* Have we been restored? */
case MapNotify:
{
545
#ifdef DEBUG_XEVENTS
546
printf ("MapNotify!\n");
547
#endif
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/* If we're not active, make ourselves active */
if (!(SDL_GetAppState () & SDL_APPACTIVE)) {
/* Send an internal activate event */
posted = SDL_PrivateAppActive (1, SDL_APPACTIVE);
/* Now that we're active, swap the gamma back */
X11_SwapVidModeGamma (_this);
}
if (SDL_VideoSurface &&
(SDL_VideoSurface->flags & SDL_FULLSCREEN)) {
X11_EnterFullScreen (_this);
} else {
X11_GrabInputNoLock (_this, SDL_CurrentWindow.input_grab);
}
X11_CheckMouseModeNoLock (_this);
if (SDL_VideoSurface) {
X11_RefreshDisplay (_this);
}
}
break;
/* Have we been resized or moved? */
case ConfigureNotify:
{
574
#ifdef DEBUG_XEVENTS
575
576
printf ("ConfigureNotify! (resize: %dx%d)\n",
xevent.xconfigure.width, xevent.xconfigure.height);
577
#endif
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
if (SDL_VideoSurface) {
if ((xevent.xconfigure.width != SDL_VideoSurface->w) ||
(xevent.xconfigure.height != SDL_VideoSurface->h)) {
/* FIXME: Find a better fix for the bug with KDE 1.2 */
if (!((xevent.xconfigure.width == 32) &&
(xevent.xconfigure.height == 32))) {
SDL_PrivateResize (xevent.xconfigure.width,
xevent.xconfigure.height);
}
} else {
/* OpenGL windows need to know about the change */
if (SDL_VideoSurface->flags & SDL_INTERNALOPENGL) {
SDL_PrivateExpose ();
}
}
}
}
break;
/* Have we been requested to quit (or another client message?) */
case ClientMessage:
{
if ((xevent.xclient.format == 32) &&
(xevent.xclient.data.l[0] == WM_DELETE_WINDOW)) {
posted = SDL_PrivateQuit ();
} else if (SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE) {
SDL_SysWMmsg wmmsg;
SDL_VERSION (&wmmsg.version);
wmmsg.subsystem = SDL_SYSWM_X11;
wmmsg.event.xevent = xevent;
posted = SDL_PrivateSysWMEvent (&wmmsg);
}
}
break;
/* Do we need to refresh ourselves? */
case Expose:
{
617
#ifdef DEBUG_XEVENTS
618
printf ("Expose (count = %d)\n", xevent.xexpose.count);
619
#endif
620
621
622
623
624
625
626
627
if (SDL_VideoSurface && (xevent.xexpose.count == 0)) {
X11_RefreshDisplay (_this);
}
}
break;
default:
{
628
#ifdef DEBUG_XEVENTS
629
printf ("Unhandled event %d\n", xevent.type);
630
#endif
631
632
633
634
635
636
637
638
639
640
641
642
643
/* Only post the event if we're watching for it */
if (SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE) {
SDL_SysWMmsg wmmsg;
SDL_VERSION (&wmmsg.version);
wmmsg.subsystem = SDL_SYSWM_X11;
wmmsg.event.xevent = xevent;
posted = SDL_PrivateSysWMEvent (&wmmsg);
}
}
break;
}
return (posted);
644
645
646
}
/* Ack! XPending() actually performs a blocking read if no events available */
647
648
int
X11_Pending (Display * display)
649
{
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/* Flush the display connection and look to see if events are queued */
XFlush (display);
if (XEventsQueued (display, QueuedAlready)) {
return (1);
}
/* More drastic measures are required -- see if X is ready to talk */
{
static struct timeval zero_time; /* static == 0 */
int x11_fd;
fd_set fdset;
x11_fd = ConnectionNumber (display);
FD_ZERO (&fdset);
FD_SET (x11_fd, &fdset);
if (select (x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1) {
return (XPending (display));
}
}
/* Oh well, nothing is ready .. */
return (0);
672
673
}
674
675
void
X11_PumpEvents (_THIS)
676
{
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
int pending;
/* Keep processing pending events */
pending = 0;
while (X11_Pending (SDL_Display)) {
X11_DispatchEvent (_this);
++pending;
}
if (switch_waiting) {
Uint32 now;
now = SDL_GetTicks ();
if (pending || !SDL_VideoSurface) {
/* Try again later... */
if (switch_waiting & SDL_FULLSCREEN) {
switch_time = now + 1500;
} else {
switch_time = now + 200;
}
} else if ((int) (switch_time - now) <= 0) {
Uint32 go_fullscreen;
go_fullscreen = switch_waiting & SDL_FULLSCREEN;
switch_waiting = 0;
if (SDL_VideoSurface->flags & SDL_FULLSCREEN) {
if (go_fullscreen) {
X11_EnterFullScreen (_this);
} else {
X11_LeaveFullScreen (_this);
}
}
/* Handle focus in/out when grabbed */
if (go_fullscreen) {
X11_GrabInputNoLock (_this, SDL_CurrentWindow.input_grab);
} else {
X11_GrabInputNoLock (_this, SDL_GRAB_OFF);
}
X11_CheckMouseModeNoLock (_this);
}
}
717
718
}
719
720
void
X11_InitKeymap (void)
721
{
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
int i;
/* Odd keys used in international keyboards */
for (i = 0; i < SDL_arraysize (ODD_keymap); ++i)
ODD_keymap[i] = SDLK_UNKNOWN;
/* Some of these might be mappable to an existing SDLK_ code */
ODD_keymap[XK_dead_grave & 0xFF] = SDLK_COMPOSE;
ODD_keymap[XK_dead_acute & 0xFF] = SDLK_COMPOSE;
ODD_keymap[XK_dead_tilde & 0xFF] = SDLK_COMPOSE;
ODD_keymap[XK_dead_macron & 0xFF] = SDLK_COMPOSE;
ODD_keymap[XK_dead_breve & 0xFF] = SDLK_COMPOSE;
ODD_keymap[XK_dead_abovedot & 0xFF] = SDLK_COMPOSE;
ODD_keymap[XK_dead_diaeresis & 0xFF] = SDLK_COMPOSE;
ODD_keymap[XK_dead_abovering & 0xFF] = SDLK_COMPOSE;
ODD_keymap[XK_dead_doubleacute & 0xFF] = SDLK_COMPOSE;
ODD_keymap[XK_dead_caron & 0xFF] = SDLK_COMPOSE;
ODD_keymap[XK_dead_cedilla & 0xFF] = SDLK_COMPOSE;
ODD_keymap[XK_dead_ogonek & 0xFF] = SDLK_COMPOSE;
ODD_keymap[XK_dead_iota & 0xFF] = SDLK_COMPOSE;
ODD_keymap[XK_dead_voiced_sound & 0xFF] = SDLK_COMPOSE;
ODD_keymap[XK_dead_semivoiced_sound & 0xFF] = SDLK_COMPOSE;
ODD_keymap[XK_dead_belowdot & 0xFF] = SDLK_COMPOSE;
745
#ifdef XK_dead_hook
746
ODD_keymap[XK_dead_hook & 0xFF] = SDLK_COMPOSE;
747
748
#endif
#ifdef XK_dead_horn
749
ODD_keymap[XK_dead_horn & 0xFF] = SDLK_COMPOSE;
750
#endif
751
752
#ifdef XK_dead_circumflex
753
754
/* These X keysyms have 0xFE as the high byte */
ODD_keymap[XK_dead_circumflex & 0xFF] = SDLK_CARET;
755
#endif
756
#ifdef XK_ISO_Level3_Shift
757
ODD_keymap[XK_ISO_Level3_Shift & 0xFF] = SDLK_MODE; /* "Alt Gr" key */
758
#endif
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
/* Map the miscellaneous keys */
for (i = 0; i < SDL_arraysize (MISC_keymap); ++i)
MISC_keymap[i] = SDLK_UNKNOWN;
/* These X keysyms have 0xFF as the high byte */
MISC_keymap[XK_BackSpace & 0xFF] = SDLK_BACKSPACE;
MISC_keymap[XK_Tab & 0xFF] = SDLK_TAB;
MISC_keymap[XK_Clear & 0xFF] = SDLK_CLEAR;
MISC_keymap[XK_Return & 0xFF] = SDLK_RETURN;
MISC_keymap[XK_Pause & 0xFF] = SDLK_PAUSE;
MISC_keymap[XK_Escape & 0xFF] = SDLK_ESCAPE;
MISC_keymap[XK_Delete & 0xFF] = SDLK_DELETE;
MISC_keymap[XK_KP_0 & 0xFF] = SDLK_KP0; /* Keypad 0-9 */
MISC_keymap[XK_KP_1 & 0xFF] = SDLK_KP1;
MISC_keymap[XK_KP_2 & 0xFF] = SDLK_KP2;
MISC_keymap[XK_KP_3 & 0xFF] = SDLK_KP3;
MISC_keymap[XK_KP_4 & 0xFF] = SDLK_KP4;
MISC_keymap[XK_KP_5 & 0xFF] = SDLK_KP5;
MISC_keymap[XK_KP_6 & 0xFF] = SDLK_KP6;
MISC_keymap[XK_KP_7 & 0xFF] = SDLK_KP7;
MISC_keymap[XK_KP_8 & 0xFF] = SDLK_KP8;
MISC_keymap[XK_KP_9 & 0xFF] = SDLK_KP9;
MISC_keymap[XK_KP_Insert & 0xFF] = SDLK_KP0;
MISC_keymap[XK_KP_End & 0xFF] = SDLK_KP1;
MISC_keymap[XK_KP_Down & 0xFF] = SDLK_KP2;
MISC_keymap[XK_KP_Page_Down & 0xFF] = SDLK_KP3;
MISC_keymap[XK_KP_Left & 0xFF] = SDLK_KP4;
MISC_keymap[XK_KP_Begin & 0xFF] = SDLK_KP5;
MISC_keymap[XK_KP_Right & 0xFF] = SDLK_KP6;
MISC_keymap[XK_KP_Home & 0xFF] = SDLK_KP7;
MISC_keymap[XK_KP_Up & 0xFF] = SDLK_KP8;
MISC_keymap[XK_KP_Page_Up & 0xFF] = SDLK_KP9;
MISC_keymap[XK_KP_Delete & 0xFF] = SDLK_KP_PERIOD;
MISC_keymap[XK_KP_Decimal & 0xFF] = SDLK_KP_PERIOD;
MISC_keymap[XK_KP_Divide & 0xFF] = SDLK_KP_DIVIDE;
MISC_keymap[XK_KP_Multiply & 0xFF] = SDLK_KP_MULTIPLY;
MISC_keymap[XK_KP_Subtract & 0xFF] = SDLK_KP_MINUS;
MISC_keymap[XK_KP_Add & 0xFF] = SDLK_KP_PLUS;
MISC_keymap[XK_KP_Enter & 0xFF] = SDLK_KP_ENTER;
MISC_keymap[XK_KP_Equal & 0xFF] = SDLK_KP_EQUALS;
MISC_keymap[XK_Up & 0xFF] = SDLK_UP;
MISC_keymap[XK_Down & 0xFF] = SDLK_DOWN;
MISC_keymap[XK_Right & 0xFF] = SDLK_RIGHT;
MISC_keymap[XK_Left & 0xFF] = SDLK_LEFT;
MISC_keymap[XK_Insert & 0xFF] = SDLK_INSERT;
MISC_keymap[XK_Home & 0xFF] = SDLK_HOME;
MISC_keymap[XK_End & 0xFF] = SDLK_END;
MISC_keymap[XK_Page_Up & 0xFF] = SDLK_PAGEUP;
MISC_keymap[XK_Page_Down & 0xFF] = SDLK_PAGEDOWN;
MISC_keymap[XK_F1 & 0xFF] = SDLK_F1;
MISC_keymap[XK_F2 & 0xFF] = SDLK_F2;
MISC_keymap[XK_F3 & 0xFF] = SDLK_F3;
MISC_keymap[XK_F4 & 0xFF] = SDLK_F4;
MISC_keymap[XK_F5 & 0xFF] = SDLK_F5;
MISC_keymap[XK_F6 & 0xFF] = SDLK_F6;
MISC_keymap[XK_F7 & 0xFF] = SDLK_F7;
MISC_keymap[XK_F8 & 0xFF] = SDLK_F8;
MISC_keymap[XK_F9 & 0xFF] = SDLK_F9;
MISC_keymap[XK_F10 & 0xFF] = SDLK_F10;
MISC_keymap[XK_F11 & 0xFF] = SDLK_F11;
MISC_keymap[XK_F12 & 0xFF] = SDLK_F12;
MISC_keymap[XK_F13 & 0xFF] = SDLK_F13;
MISC_keymap[XK_F14 & 0xFF] = SDLK_F14;
MISC_keymap[XK_F15 & 0xFF] = SDLK_F15;
MISC_keymap[XK_Num_Lock & 0xFF] = SDLK_NUMLOCK;
MISC_keymap[XK_Caps_Lock & 0xFF] = SDLK_CAPSLOCK;
MISC_keymap[XK_Scroll_Lock & 0xFF] = SDLK_SCROLLOCK;
MISC_keymap[XK_Shift_R & 0xFF] = SDLK_RSHIFT;
MISC_keymap[XK_Shift_L & 0xFF] = SDLK_LSHIFT;
MISC_keymap[XK_Control_R & 0xFF] = SDLK_RCTRL;
MISC_keymap[XK_Control_L & 0xFF] = SDLK_LCTRL;
MISC_keymap[XK_Alt_R & 0xFF] = SDLK_RALT;
MISC_keymap[XK_Alt_L & 0xFF] = SDLK_LALT;
MISC_keymap[XK_Meta_R & 0xFF] = SDLK_RMETA;
MISC_keymap[XK_Meta_L & 0xFF] = SDLK_LMETA;
MISC_keymap[XK_Super_L & 0xFF] = SDLK_LSUPER; /* Left "Windows" */
MISC_keymap[XK_Super_R & 0xFF] = SDLK_RSUPER; /* Right "Windows */
MISC_keymap[XK_Mode_switch & 0xFF] = SDLK_MODE; /* "Alt Gr" key */
MISC_keymap[XK_Multi_key & 0xFF] = SDLK_COMPOSE; /* Multi-key compose */
MISC_keymap[XK_Help & 0xFF] = SDLK_HELP;
MISC_keymap[XK_Print & 0xFF] = SDLK_PRINT;
MISC_keymap[XK_Sys_Req & 0xFF] = SDLK_SYSREQ;
MISC_keymap[XK_Break & 0xFF] = SDLK_BREAK;
MISC_keymap[XK_Menu & 0xFF] = SDLK_MENU;
MISC_keymap[XK_Hyper_R & 0xFF] = SDLK_MENU; /* Windows "Menu" key */
850
851
}
852
/* Get the translated SDL virtual keysym */
853
854
SDLKey
X11_TranslateKeycode (Display * display, KeyCode kc)
855
{
856
857
KeySym xsym;
SDLKey key;
858
859
xsym = XKeycodeToKeysym (display, kc, 0);
860
#ifdef DEBUG_KEYS
861
fprintf (stderr, "Translating key code %d -> 0x%.4x\n", kc, xsym);
862
#endif
863
864
865
866
key = SDLK_UNKNOWN;
if (xsym) {
switch (xsym >> 8) {
case 0x1005FF:
867
#ifdef SunXK_F36
868
869
if (xsym == SunXK_F36)
key = SDLK_F11;
870
871
#endif
#ifdef SunXK_F37
872
873
if (xsym == SunXK_F37)
key = SDLK_F12;
874
#endif
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
break;
case 0x00: /* Latin 1 */
key = (SDLKey) (xsym & 0xFF);
break;
case 0x01: /* Latin 2 */
case 0x02: /* Latin 3 */
case 0x03: /* Latin 4 */
case 0x04: /* Katakana */
case 0x05: /* Arabic */
case 0x06: /* Cyrillic */
case 0x07: /* Greek */
case 0x08: /* Technical */
case 0x0A: /* Publishing */
case 0x0C: /* Hebrew */
case 0x0D: /* Thai */
/* These are wrong, but it's better than nothing */
key = (SDLKey) (xsym & 0xFF);
break;
case 0xFE:
key = ODD_keymap[xsym & 0xFF];
break;
case 0xFF:
key = MISC_keymap[xsym & 0xFF];
break;
default:
/*
fprintf(stderr, "X11: Unhandled xsym, sym = 0x%04x\n",
(unsigned int)xsym);
*/
break;
}
} else {
/* X11 doesn't know how to translate the key! */
switch (kc) {
/* Caution:
These keycodes are from the Microsoft Keyboard
*/
case 115:
key = SDLK_LSUPER;
break;
case 116:
key = SDLK_RSUPER;
break;
case 117:
key = SDLK_MENU;
break;
default:
/*
* no point in an error message; happens for
* several keys when we get a keymap notify
*/
break;
}
}
return key;
930
931
932
933
934
935
}
/* X11 modifier masks for various keys */
static unsigned meta_l_mask, meta_r_mask, alt_l_mask, alt_r_mask;
static unsigned num_mask, mode_switch_mask;
936
937
static void
get_modifier_masks (Display * display)
938
{
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
static unsigned got_masks;
int i, j;
XModifierKeymap *xmods;
unsigned n;
if (got_masks)
return;
xmods = XGetModifierMapping (display);
n = xmods->max_keypermod;
for (i = 3; i < 8; i++) {
for (j = 0; j < n; j++) {
KeyCode kc = xmods->modifiermap[i * n + j];
KeySym ks = XKeycodeToKeysym (display, kc, 0);
unsigned mask = 1 << i;
switch (ks) {
case XK_Num_Lock:
num_mask = mask;
break;
case XK_Alt_L:
alt_l_mask = mask;
break;
case XK_Alt_R:
alt_r_mask = mask;
break;
case XK_Meta_L:
meta_l_mask = mask;
break;
case XK_Meta_R:
meta_r_mask = mask;
break;
case XK_Mode_switch:
mode_switch_mask = mask;
break;
}
}
}
XFreeModifiermap (xmods);
got_masks = 1;
978
979
980
981
982
983
984
985
986
987
988
989
990
}
/*
* This function is semi-official; it is not officially exported and should
* not be considered part of the SDL API, but may be used by client code
* that *really* needs it (including legacy code).
* It is slow, though, and should be avoided if possible.
*
* Note that it isn't completely accurate either; in particular, multi-key
* sequences (dead accents, compose key sequences) will not work since the
* state has been irrevocably lost.
*/
991
992
Uint16
X11_KeyToUnicode (SDLKey keysym, SDLMod modifiers)
993
{
994
995
996
997
998
999
1000
SDL_VideoDevice *_this = SDL_GetVideoDevice ();
char keybuf[32];
int i;
KeySym xsym = 0;
XKeyEvent xkey;
Uint16 unicode;