19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Handle the event stream, converting X11 events into SDL events */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <setjmp.h>
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <X11/Xlib.h>
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <X11/Xutil.h>
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <X11/keysym.h>
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __SVR4
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <X11/Sunkeysym.h>
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/types.h>
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/time.h>
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h>
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_timer.h"
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_syswm.h"
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_sysvideo.h"
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_sysevents.h"
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_events_c.h"
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_x11video.h"
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_x11dga_c.h"
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_x11modes_c.h"
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_x11image_c.h"
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_x11gamma_c.h"
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_x11wm_c.h"
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_x11mouse_c.h"
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_x11events_c.h"
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Define this if you want to debug X11 events */
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*#define DEBUG_XEVENTS*/
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The translation tables from an X11 keysym to a SDL keysym */
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDLKey ODD_keymap[256];
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDLKey MISC_keymap[256];
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDLKey X11_TranslateKeycode(Display *display, KeyCode kc);
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Pending resize target for ConfigureNotify (so outdated events don't
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall cause inappropriate resize events)
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint X11_PendingConfigureNotifyWidth = -1;
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint X11_PendingConfigureNotifyHeight = -1;
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef X_HAVE_UTF8_STRING
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint32 Utf8ToUcs4(const Uint8 *utf8)
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 c;
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i = 1;
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int noOctets = 0;
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int firstOctetMask = 0;
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	unsigned char firstOctet = utf8[0];
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (firstOctet < 0x80) {
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/*
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  Characters in the range:
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    00000000 to 01111111 (ASCII Range)
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  are stored in one octet:
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    0xxxxxxx (The same as its ASCII representation)
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  The least 6 significant bits of the first octet is the most 6 significant nonzero bits
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  of the UCS4 representation.
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*/
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		noOctets = 1;
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		firstOctetMask = 0x7F;  /* 0(1111111) - The most significant bit is ignored */
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else if ((firstOctet & 0xE0) /* get the most 3 significant bits by AND'ing with 11100000 */
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	              == 0xC0 ) {  /* see if those 3 bits are 110. If so, the char is in this range */
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/*
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  Characters in the range:
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    00000000 10000000 to 00000111 11111111
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  are stored in two octets:
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    110xxxxx 10xxxxxx
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  The least 5 significant bits of the first octet is the most 5 significant nonzero bits
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  of the UCS4 representation.
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*/
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		noOctets = 2;
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		firstOctetMask = 0x1F;  /* 000(11111) - The most 3 significant bits are ignored */
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else if ((firstOctet & 0xF0) /* get the most 4 significant bits by AND'ing with 11110000 */
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	              == 0xE0) {  /* see if those 4 bits are 1110. If so, the char is in this range */
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/*
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  Characters in the range:
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    00001000 00000000 to 11111111 11111111
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  are stored in three octets:
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    1110xxxx 10xxxxxx 10xxxxxx
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  The least 4 significant bits of the first octet is the most 4 significant nonzero bits
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  of the UCS4 representation.
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*/
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		noOctets = 3;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		firstOctetMask = 0x0F; /* 0000(1111) - The most 4 significant bits are ignored */
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else if ((firstOctet & 0xF8) /* get the most 5 significant bits by AND'ing with 11111000 */
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	              == 0xF0) {  /* see if those 5 bits are 11110. If so, the char is in this range */
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/*
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  Characters in the range:
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    00000001 00000000 00000000 to 00011111 11111111 11111111
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  are stored in four octets:
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  The least 3 significant bits of the first octet is the most 3 significant nonzero bits
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  of the UCS4 representation.
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*/
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		noOctets = 4;
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		firstOctetMask = 0x07; /* 11110(111) - The most 5 significant bits are ignored */
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else if ((firstOctet & 0xFC) /* get the most 6 significant bits by AND'ing with 11111100 */
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	              == 0xF8) { /* see if those 6 bits are 111110. If so, the char is in this range */
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/*
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  Characters in the range:
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    00000000 00100000 00000000 00000000 to
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    00000011 11111111 11111111 11111111
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  are stored in five octets:
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  The least 2 significant bits of the first octet is the most 2 significant nonzero bits
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  of the UCS4 representation.
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*/
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		noOctets = 5;
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		firstOctetMask = 0x03; /* 111110(11) - The most 6 significant bits are ignored */
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else if ((firstOctet & 0xFE) /* get the most 7 significant bits by AND'ing with 11111110 */
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	              == 0xFC) { /* see if those 7 bits are 1111110. If so, the char is in this range */
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/*
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  Characters in the range:
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    00000100 00000000 00000000 00000000 to
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    01111111 11111111 11111111 11111111
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  are stored in six octets:
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  The least significant bit of the first octet is the most significant nonzero bit
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  of the UCS4 representation.
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*/
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		noOctets = 6;
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		firstOctetMask = 0x01; /* 1111110(1) - The most 7 significant bits are ignored */
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;  /* The given chunk is not a valid UTF-8 encoded Unicode character */
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/*
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  The least noOctets significant bits of the first octet is the most 2 significant nonzero bits
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  of the UCS4 representation.
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  The first 6 bits of the UCS4 representation is the least 8-noOctets-1 significant bits of
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  firstOctet if the character is not ASCII. If so, it's the least 7 significant bits of firstOctet.
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  This done by AND'ing firstOctet with its mask to trim the bits used for identifying the
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  number of continuing octets (if any) and leave only the free bits (the x's)
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  Sample:
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  1-octet:    0xxxxxxx  &  01111111 = 0xxxxxxx
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  2-octets:  110xxxxx  &  00011111 = 000xxxxx
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*/
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	c = firstOctet & firstOctetMask;
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Now, start filling c.ucs4 with the bits from the continuing octets from utf8. */
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (i = 1; i < noOctets; i++) {
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* A valid continuing octet is of the form 10xxxxxx */
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ((utf8[i] & 0xC0) /* get the most 2 significant bits by AND'ing with 11000000 */
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    != 0x80) /* see if those 2 bits are 10. If not, the is a malformed sequence. */
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/*The given chunk is a partial sequence at the end of a string that could
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			   begin a valid character */
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return 0;
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Make room for the next 6-bits */
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		c <<= 6;
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/*
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  Take only the least 6 significance bits of the current octet (utf8[i]) and fill the created room
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  of c.ucs4 with them.
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  This done by AND'ing utf8[i] with 00111111 and the OR'ing the result with c.ucs4.
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*/
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		c |= utf8[i] & 0x3F;
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return c;
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Given a UTF-8 encoded string pointed to by utf8 of length length in
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   bytes, returns the corresponding UTF-16 encoded string in the
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   buffer pointed to by utf16.  The maximum number of UTF-16 encoding
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   units (i.e., Unit16s) allowed in the buffer is specified in
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   utf16_max_length.  The return value is the number of UTF-16
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   encoding units placed in the output buffer pointed to by utf16.
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   In case of an error, -1 is returned, leaving some unusable partial
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   results in the output buffer.
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   The caller must estimate the size of utf16 buffer by itself before
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   calling this function.  Insufficient output buffer is considered as
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   an error, and once an error occured, this function doesn't give any
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   clue how large the result will be.
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   The error cases include following:
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   - Invalid byte sequences were in the input UTF-8 bytes.  The caller
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     has no way to know what point in the input buffer was the
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     errornous byte.
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   - The input contained a character (a valid UTF-8 byte sequence)
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     whose scalar value exceeded the range that UTF-16 can represent
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     (i.e., characters whose Unicode scalar value above 0x110000).
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   - The output buffer has no enough space to hold entire utf16 data.
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   Please note:
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   - '\0'-termination is not assumed both on the input UTF-8 string
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     and on the output UTF-16 string; any legal zero byte in the input
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     UTF-8 string will be converted to a 16-bit zero in output.  As a
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     side effect, the last UTF-16 encoding unit stored in the output
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     buffer will have a non-zero value if the input UTF-8 was not
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     '\0'-terminated.
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   - UTF-8 aliases are *not* considered as an error.  They are
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     converted to UTF-16.  For example, 0xC0 0xA0, 0xE0 0x80 0xA0,
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     and 0xF0 0x80 0x80 0xA0 are all mapped to a single UTF-16
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     encoding unit 0x0020.
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   - Three byte UTF-8 sequences whose value corresponds to a surrogate
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     code or other reserved scalar value are not considered as an
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     error either.  They may cause an invalid UTF-16 data (e.g., those
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     containing unpaired surrogates).
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Utf8ToUtf16(const Uint8 *utf8, const int utf8_length, Uint16 *utf16, const int utf16_max_length) {
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* p moves over the output buffer.  max_ptr points to the next to the last slot of the buffer.  */
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Uint16 *p = utf16;
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Uint16 const *const max_ptr = utf16 + utf16_max_length;
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* end_of_input points to the last byte of input as opposed to the next to the last byte.  */
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Uint8 const *const end_of_input = utf8 + utf8_length - 1;
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while (utf8 <= end_of_input) {
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint8 const c = *utf8;
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (p >= max_ptr) {
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* No more output space.  */
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    return -1;
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (c < 0x80) {
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* One byte ASCII.  */
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    *p++ = c;
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    utf8 += 1;
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else if (c < 0xC0) {
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Follower byte without preceeding leader bytes.  */
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    return -1;
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else if (c < 0xE0) {
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Two byte sequence.  We need one follower byte.  */
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    if (end_of_input - utf8 < 1 || (((utf8[1] ^ 0x80)) & 0xC0)) {
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    *p++ = (Uint16)(0xCF80 + (c << 6) + utf8[1]);
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    utf8 += 2;
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else if (c < 0xF0) {
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Three byte sequence.  We need two follower byte.  */
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    if (end_of_input - utf8 < 2 || (((utf8[1] ^ 0x80) | (utf8[2] ^ 0x80)) & 0xC0)) {
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    *p++ = (Uint16)(0xDF80 + (c << 12) + (utf8[1] << 6) + utf8[2]);
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    utf8 += 3;
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else if (c < 0xF8) {
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    int plane;
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Four byte sequence.  We need three follower bytes.  */
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    if (end_of_input - utf8 < 3 || (((utf8[1] ^ 0x80) | (utf8[2] ^0x80) | (utf8[3] ^ 0x80)) & 0xC0)) {
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    plane = (-0xC8 + (c << 2) + (utf8[1] >> 4));
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    if (plane == 0) {
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* This four byte sequence is an alias that
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                   corresponds to a Unicode scalar value in BMP.
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   It fits in an UTF-16 encoding unit.  */
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*p++ = (Uint16)(0xDF80 + (utf8[1] << 12) + (utf8[2] << 6) + utf8[3]);
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    } else if (plane <= 16) {
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* This is a legal four byte sequence that corresponds to a surrogate pair.  */
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (p + 1 >= max_ptr) {
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    /* No enough space on the output buffer for the pair.  */
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    return -1;
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*p++ = (Uint16)(0xE5B8 + (c << 8) + (utf8[1] << 2) + (utf8[2] >> 4));
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*p++ = (Uint16)(0xDB80 + ((utf8[2] & 0x0F) << 6) + utf8[3]);
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    } else {
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* This four byte sequence is out of UTF-16 code space.  */
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    utf8 += 4;
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Longer sequence or unused byte.  */
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    return -1;
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return p - utf16;
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Check to see if this is a repeated key.
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   (idea shamelessly lifted from GII -- thanks guys! :)
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int X11_KeyRepeat(Display *display, XEvent *event)
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	XEvent peekevent;
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int repeated;
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	repeated = 0;
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( XPending(display) ) {
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		XPeekEvent(display, &peekevent);
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (peekevent.type == KeyPress) &&
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     (peekevent.xkey.keycode == event->xkey.keycode) &&
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     ((peekevent.xkey.time-event->xkey.time) < 2) ) {
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			repeated = 1;
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			XNextEvent(display, &peekevent);
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(repeated);
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Note:  The X server buffers and accumulates mouse motion events, so
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   the motion event generated by the warp may not appear exactly as we
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   expect it to.  We work around this (and improve performance) by only
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   warping the pointer when it reaches the edge, and then wait for it.
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define MOUSE_FUDGE_FACTOR	8
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic __inline__ int X11_WarpedMotion(_THIS, XEvent *xevent)
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int w, h, i;
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int deltax, deltay;
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int posted;
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	w = SDL_VideoSurface->w;
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	h = SDL_VideoSurface->h;
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	deltax = xevent->xmotion.x - mouse_last.x;
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	deltay = xevent->xmotion.y - mouse_last.y;
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_MOTION
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("Warped mouse motion: %d,%d\n", deltax, deltay);
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mouse_last.x = xevent->xmotion.x;
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mouse_last.y = xevent->xmotion.y;
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	posted = SDL_PrivateMouseMotion(0, 1, deltax, deltay);
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (xevent->xmotion.x < MOUSE_FUDGE_FACTOR) ||
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     (xevent->xmotion.x > (w-MOUSE_FUDGE_FACTOR)) ||
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     (xevent->xmotion.y < MOUSE_FUDGE_FACTOR) ||
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     (xevent->xmotion.y > (h-MOUSE_FUDGE_FACTOR)) ) {
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Get the events that have accumulated */
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		while ( XCheckTypedEvent(SDL_Display, MotionNotify, xevent) ) {
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			deltax = xevent->xmotion.x - mouse_last.x;
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			deltay = xevent->xmotion.y - mouse_last.y;
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_MOTION
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("Extra mouse motion: %d,%d\n", deltax, deltay);
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mouse_last.x = xevent->xmotion.x;
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mouse_last.y = xevent->xmotion.y;
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			posted += SDL_PrivateMouseMotion(0, 1, deltax, deltay);
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mouse_last.x = w/2;
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mouse_last.y = h/2;
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		XWarpPointer(SDL_Display, None, SDL_Window, 0, 0, 0, 0,
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					mouse_last.x, mouse_last.y);
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( i=0; i<10; ++i ) {
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        		XMaskEvent(SDL_Display, PointerMotionMask, xevent);
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( (xevent->xmotion.x >
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			          (mouse_last.x-MOUSE_FUDGE_FACTOR)) &&
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     (xevent->xmotion.x <
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			          (mouse_last.x+MOUSE_FUDGE_FACTOR)) &&
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     (xevent->xmotion.y >
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			          (mouse_last.y-MOUSE_FUDGE_FACTOR)) &&
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     (xevent->xmotion.y <
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			          (mouse_last.y+MOUSE_FUDGE_FACTOR)) ) {
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_XEVENTS
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("Lost mouse motion: %d,%d\n", xevent->xmotion.x, xevent->xmotion.y);
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_XEVENTS
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( i == 10 ) {
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			printf("Warning: didn't detect mouse warp motion\n");
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(posted);
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int X11_DispatchEvent(_THIS)
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int posted;
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	XEvent xevent;
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(&xevent, '\0', sizeof (XEvent));  /* valgrind fix. --ryan. */
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	XNextEvent(SDL_Display, &xevent);
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Discard KeyRelease and KeyPress events generated by auto-repeat.
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   We need to do it before passing event to XFilterEvent.  Otherwise,
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   KeyRelease aware IMs are confused...  */
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( xevent.type == KeyRelease
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     && X11_KeyRepeat(SDL_Display, &xevent) ) {
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef X_HAVE_UTF8_STRING
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* If we are translating with IM, we need to pass all events
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   to XFilterEvent, and discard those filtered events immediately.  */
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_TranslateUNICODE
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     && SDL_IM != NULL
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     && XFilterEvent(&xevent, None) ) {
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	posted = 0;
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (xevent.type) {
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Gaining mouse coverage? */
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case EnterNotify: {
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_XEVENTS
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallprintf("EnterNotify! (%d,%d)\n", xevent.xcrossing.x, xevent.xcrossing.y);
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallif ( xevent.xcrossing.mode == NotifyGrab )
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallprintf("Mode: NotifyGrab\n");
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallif ( xevent.xcrossing.mode == NotifyUngrab )
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallprintf("Mode: NotifyUngrab\n");
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( this->input_grab == SDL_GRAB_OFF ) {
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			posted = SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		posted = SDL_PrivateMouseMotion(0, 0,
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				xevent.xcrossing.x,
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				xevent.xcrossing.y);
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    break;
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Losing mouse coverage? */
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case LeaveNotify: {
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_XEVENTS
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallprintf("LeaveNotify! (%d,%d)\n", xevent.xcrossing.x, xevent.xcrossing.y);
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallif ( xevent.xcrossing.mode == NotifyGrab )
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallprintf("Mode: NotifyGrab\n");
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallif ( xevent.xcrossing.mode == NotifyUngrab )
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallprintf("Mode: NotifyUngrab\n");
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (xevent.xcrossing.mode != NotifyGrab) &&
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     (xevent.xcrossing.mode != NotifyUngrab) &&
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     (xevent.xcrossing.detail != NotifyInferior) ) {
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall               		if ( this->input_grab == SDL_GRAB_OFF ) {
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				posted = SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else {
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				posted = SDL_PrivateMouseMotion(0, 0,
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						xevent.xcrossing.x,
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						xevent.xcrossing.y);
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    break;
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Gaining input focus? */
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case FocusIn: {
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_XEVENTS
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallprintf("FocusIn!\n");
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		posted = SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef X_HAVE_UTF8_STRING
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_IC != NULL ) {
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			XSetICFocus(SDL_IC);
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Queue entry into fullscreen mode */
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch_waiting = 0x01 | SDL_FULLSCREEN;
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch_time = SDL_GetTicks() + 1500;
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    break;
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Losing input focus? */
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case FocusOut: {
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_XEVENTS
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallprintf("FocusOut!\n");
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		posted = SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef X_HAVE_UTF8_STRING
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_IC != NULL ) {
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			XUnsetICFocus(SDL_IC);
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Queue leaving fullscreen mode */
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch_waiting = 0x01;
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch_time = SDL_GetTicks() + 200;
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    break;
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef X_HAVE_UTF8_STRING
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Some IM requires MappingNotify to be passed to
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	       XRefreshKeyboardMapping by the app.  */
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case MappingNotify: {
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		XRefreshKeyboardMapping(&xevent.xmapping);
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    break;
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* X_HAVE_UTF8_STRING */
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Generated upon EnterWindow and FocusIn */
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case KeymapNotify: {
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_XEVENTS
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallprintf("KeymapNotify!\n");
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		X11_SetKeyboardState(SDL_Display,  xevent.xkeymap.key_vector);
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    break;
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Mouse motion? */
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case MotionNotify: {
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_VideoSurface ) {
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( mouse_relative ) {
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( using_dga & DGA_MOUSE ) {
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_MOTION
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("DGA motion: %d,%d\n", xevent.xmotion.x_root, xevent.xmotion.y_root);
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					posted = SDL_PrivateMouseMotion(0, 1,
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							xevent.xmotion.x_root,
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							xevent.xmotion.y_root);
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else {
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					posted = X11_WarpedMotion(this,&xevent);
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else {
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_MOTION
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y);
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				posted = SDL_PrivateMouseMotion(0, 0,
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						xevent.xmotion.x,
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						xevent.xmotion.y);
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    break;
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Mouse button press? */
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case ButtonPress: {
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		posted = SDL_PrivateMouseButton(SDL_PRESSED,
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					xevent.xbutton.button, 0, 0);
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    break;
5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Mouse button release? */
5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case ButtonRelease: {
5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		posted = SDL_PrivateMouseButton(SDL_RELEASED,
5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					xevent.xbutton.button, 0, 0);
5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    break;
5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Key press? */
5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case KeyPress: {
5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_keysym keysym;
5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		KeyCode keycode = xevent.xkey.keycode;
5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_XEVENTS
5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallprintf("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* If we're not doing translation, we're done! */
5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( !SDL_TranslateUNICODE ) {
5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Get the translated SDL virtual keysym and put it on the queue.*/
5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			keysym.scancode = keycode;
5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			keysym.sym = X11_TranslateKeycode(SDL_Display, keycode);
5719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			keysym.mod = KMOD_NONE;
5729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			keysym.unicode = 0;
5739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			posted = SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
5749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
5759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Look up the translated value for the key event */
5789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef X_HAVE_UTF8_STRING
5799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_IC != NULL ) {
5809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Status status;
5819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			KeySym xkeysym;
5829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			int i;
5839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* A UTF-8 character can be at most 6 bytes */
5849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* ... It's true, but Xutf8LookupString can
5859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			   return more than one characters.  Moreover,
5869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			   the spec. put no upper bound, so we should
5879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			   be ready for longer strings.  */
5889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			char keybuf[32];
5899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			char *keydata = keybuf;
5909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			int count;
5919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Uint16 utf16buf[32];
5929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Uint16 *utf16data = utf16buf;
5939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			int utf16size;
5949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			int utf16length;
5959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			count = Xutf8LookupString(SDL_IC, &xevent.xkey, keydata, sizeof(keybuf), &xkeysym, &status);
5979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (XBufferOverflow == status) {
5989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  /* The IM has just generated somewhat long
5999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     string.  We need a longer buffer in this
6009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     case.  */
6019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  keydata = SDL_malloc(count);
6029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  if ( keydata == NULL ) {
6039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    SDL_OutOfMemory();
6049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    break;
6059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  }
6069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  count = Xutf8LookupString(SDL_IC, &xevent.xkey, keydata, count, &xkeysym, &status);
6079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
6089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			switch (status) {
6109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case XBufferOverflow: {
6129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  /* Oops!  We have allocated the bytes as
6139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     requested by Xutf8LookupString, so the
6149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     length of the buffer must be
6159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     sufficient.  This case should never
6169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     happen! */
6179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  SDL_SetError("Xutf8LookupString indicated a double buffer overflow!");
6189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  break;
6199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
6209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case XLookupChars:
6229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case XLookupBoth: {
6239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  if (0 == count) {
6249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    break;
6259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  }
6269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  /* We got a converted string from IM.  Make
6289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     sure to deliver all characters to the
6299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     application as SDL events.  Note that
6309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     an SDL event can only carry one UTF-16
6319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     encoding unit, and a surrogate pair is
6329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     delivered as two SDL events.  I guess
6339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     this behaviour is probably _imported_
6349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     from Windows or MacOS.  To do so, we need
6359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     to convert the UTF-8 data into UTF-16
6369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     data (not UCS4/UTF-32!).  We need an
6379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     estimate of the number of UTF-16 encoding
6389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     units here.  The worst case is pure ASCII
6399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     string.  Assume so. */
6409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  /* In 1.3 SDL may have a text event instead, that
6419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     carries the whole UTF-8 string with it. */
6429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  utf16size = count * sizeof(Uint16);
6439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  if (utf16size > sizeof(utf16buf)) {
6449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    utf16data = (Uint16 *) SDL_malloc(utf16size);
6459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    if (utf16data == NULL) {
6469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			      SDL_OutOfMemory();
6479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			      break;
6489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    }
6499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  }
6509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  utf16length = Utf8ToUtf16((Uint8 *)keydata, count, utf16data, utf16size);
6519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  if (utf16length < 0) {
6529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    /* The keydata contained an invalid byte
6539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			       sequence.  It should be a bug of the IM
6549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			       or Xlib... */
6559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    SDL_SetError("Oops! Xutf8LookupString returned an invalid UTF-8 sequence!");
6569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    break;
6579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  }
6589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  /* Deliver all UTF-16 encoding units.  At
6609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     this moment, SDL event queue has a
6619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     fixed size (128 events), and an SDL
6629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     event can hold just one UTF-16 encoding
6639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     unit.  So, if we receive more than 128
6649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     UTF-16 encoding units from a commit,
6659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     exceeded characters will be lost.  */
6669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  for (i = 0; i < utf16length - 1; i++) {
6679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    keysym.scancode = 0;
6689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    keysym.sym = SDLK_UNKNOWN;
6699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    keysym.mod = KMOD_NONE;
6709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    keysym.unicode = utf16data[i];
6719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    posted = SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
6729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  }
6739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  /* The keysym for the last character carries the
6749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     scancode and symbol that corresponds to the X11
6759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     keycode.  */
6769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  if (utf16length > 0) {
6779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    keysym.scancode = keycode;
6789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    keysym.sym = (keycode ? X11_TranslateKeycode(SDL_Display, keycode) : 0);
6799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    keysym.mod = KMOD_NONE;
6809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    keysym.unicode = utf16data[utf16length - 1];
6819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    posted = SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
6829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  }
6839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  break;
6849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
6859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case XLookupKeySym: {
6879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  /* I'm not sure whether it is possible that
6889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     a zero keycode makes XLookupKeySym
6899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     status.  What I'm sure is that a
6909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     combination of a zero scan code and a non
6919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     zero sym makes SDL_PrivateKeyboard
6929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     strange state...  So, just discard it.
6939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     If this doesn't work, I'm receiving bug
6949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     reports, and I can know under what
6959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     condition this case happens.  */
6969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  if (keycode) {
6979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    keysym.scancode = keycode;
6989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    keysym.sym = X11_TranslateKeycode(SDL_Display, keycode);
6999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    keysym.mod = KMOD_NONE;
7009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    keysym.unicode = 0;
7019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    posted = SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
7029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  }
7039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  break;
7049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
7059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case XLookupNone: {
7079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  /* IM has eaten the event.  */
7089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  break;
7099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
7109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			default:
7129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  /* An unknown status from Xutf8LookupString.  */
7139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  SDL_SetError("Oops! Xutf8LookupStringreturned an unknown status");
7149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
7159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Release dynamic buffers if allocated.  */
7179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (keydata != NULL && keybuf != keydata) {
7189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  SDL_free(keydata);
7199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
7209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (utf16data != NULL && utf16buf != utf16data) {
7219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  SDL_free(utf16data);
7229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
7239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
7249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		else
7259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
7269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
7279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			static XComposeStatus state;
7289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			char keybuf[32];
7299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			keysym.scancode = keycode;
7319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			keysym.sym = X11_TranslateKeycode(SDL_Display, keycode);
7329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			keysym.mod = KMOD_NONE;
7339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			keysym.unicode = 0;
7349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( XLookupString(&xevent.xkey,
7359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			                    keybuf, sizeof(keybuf),
7369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			                    NULL, &state) ) {
7379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/*
7389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				* FIXME: XLookupString() may yield more than one
7399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				* character, so we need a mechanism to allow for
7409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				* this (perhaps null keypress events with a
7419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				* unicode value)
7429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*/
7439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				keysym.unicode = (Uint8)keybuf[0];
7449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
7459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			posted = SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
7479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
7489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
7499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    break;
7509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Key release? */
7529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case KeyRelease: {
7539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_keysym keysym;
7549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		KeyCode keycode = xevent.xkey.keycode;
7559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (keycode == 0) {
7579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  /* There should be no KeyRelease for keycode == 0,
7589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     since it is a notification from IM but a real
7599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     keystroke.  */
7609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  /* We need to emit some diagnostic message here.  */
7619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  break;
7629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
7639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_XEVENTS
7659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallprintf("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
7669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
7679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Get the translated SDL virtual keysym */
7699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		keysym.scancode = keycode;
7709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		keysym.sym = X11_TranslateKeycode(SDL_Display, keycode);
7719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		keysym.mod = KMOD_NONE;
7729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		keysym.unicode = 0;
7739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		posted = SDL_PrivateKeyboard(SDL_RELEASED, &keysym);
7759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
7769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    break;
7779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Have we been iconified? */
7799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case UnmapNotify: {
7809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_XEVENTS
7819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallprintf("UnmapNotify!\n");
7829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
7839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* If we're active, make ourselves inactive */
7849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_GetAppState() & SDL_APPACTIVE ) {
7859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Swap out the gamma before we go inactive */
7869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			X11_SwapVidModeGamma(this);
7879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Send an internal deactivate event */
7899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			posted = SDL_PrivateAppActive(0,
7909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDL_APPACTIVE|SDL_APPINPUTFOCUS);
7919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
7929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
7939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    break;
7949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Have we been restored? */
7969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case MapNotify: {
7979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_XEVENTS
7989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallprintf("MapNotify!\n");
7999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
8009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* If we're not active, make ourselves active */
8019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
8029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Send an internal activate event */
8039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			posted = SDL_PrivateAppActive(1, SDL_APPACTIVE);
8049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Now that we're active, swap the gamma back */
8069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			X11_SwapVidModeGamma(this);
8079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_VideoSurface &&
8109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     (SDL_VideoSurface->flags & SDL_FULLSCREEN) ) {
8119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			X11_EnterFullScreen(this);
8129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
8139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			X11_GrabInputNoLock(this, this->input_grab);
8149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		X11_CheckMouseModeNoLock(this);
8169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_VideoSurface ) {
8189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			X11_RefreshDisplay(this);
8199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
8219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    break;
8229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Have we been resized or moved? */
8249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case ConfigureNotify: {
8259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_XEVENTS
8269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallprintf("ConfigureNotify! (resize: %dx%d)\n", xevent.xconfigure.width, xevent.xconfigure.height);
8279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
8289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ((X11_PendingConfigureNotifyWidth != -1) &&
8299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    (X11_PendingConfigureNotifyHeight != -1)) {
8309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    if ((xevent.xconfigure.width != X11_PendingConfigureNotifyWidth) &&
8319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			(xevent.xconfigure.height != X11_PendingConfigureNotifyHeight)) {
8329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    /* Event is from before the resize, so ignore. */
8339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    break;
8349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    }
8359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    X11_PendingConfigureNotifyWidth = -1;
8369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    X11_PendingConfigureNotifyHeight = -1;
8379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_VideoSurface ) {
8399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    if ((xevent.xconfigure.width != SDL_VideoSurface->w) ||
8409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		        (xevent.xconfigure.height != SDL_VideoSurface->h)) {
8419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* FIXME: Find a better fix for the bug with KDE 1.2 */
8429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( ! ((xevent.xconfigure.width == 32) &&
8439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			        (xevent.xconfigure.height == 32)) ) {
8449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_PrivateResize(xevent.xconfigure.width,
8459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				                  xevent.xconfigure.height);
8469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
8479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    } else {
8489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* OpenGL windows need to know about the change */
8499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( SDL_VideoSurface->flags & SDL_OPENGL ) {
8509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_PrivateExpose();
8519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
8529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    }
8539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
8559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    break;
8569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Have we been requested to quit (or another client message?) */
8589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case ClientMessage: {
8599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (xevent.xclient.format == 32) &&
8609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     (xevent.xclient.data.l[0] == WM_DELETE_WINDOW) )
8619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
8629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			posted = SDL_PrivateQuit();
8639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else
8649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) {
8659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SysWMmsg wmmsg;
8669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_VERSION(&wmmsg.version);
8689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			wmmsg.subsystem = SDL_SYSWM_X11;
8699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			wmmsg.event.xevent = xevent;
8709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			posted = SDL_PrivateSysWMEvent(&wmmsg);
8719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
8739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    break;
8749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Do we need to refresh ourselves? */
8769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case Expose: {
8779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_XEVENTS
8789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallprintf("Expose (count = %d)\n", xevent.xexpose.count);
8799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
8809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_VideoSurface && (xevent.xexpose.count == 0) ) {
8819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			X11_RefreshDisplay(this);
8829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
8849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    break;
8859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    default: {
8879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_XEVENTS
8889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallprintf("Unhandled event %d\n", xevent.type);
8899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
8909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Only post the event if we're watching for it */
8919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) {
8929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SysWMmsg wmmsg;
8939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_VERSION(&wmmsg.version);
8959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			wmmsg.subsystem = SDL_SYSWM_X11;
8969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			wmmsg.event.xevent = xevent;
8979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			posted = SDL_PrivateSysWMEvent(&wmmsg);
8989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
9009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    break;
9019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
9029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(posted);
9039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
9049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Ack!  XPending() actually performs a blocking read if no events available */
9069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint X11_Pending(Display *display)
9079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
9089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Flush the display connection and look to see if events are queued */
9099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	XFlush(display);
9109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( XEventsQueued(display, QueuedAlready) ) {
9119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(1);
9129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
9139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* More drastic measures are required -- see if X is ready to talk */
9159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
9169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		static struct timeval zero_time;	/* static == 0 */
9179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int x11_fd;
9189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fd_set fdset;
9199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		x11_fd = ConnectionNumber(display);
9219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		FD_ZERO(&fdset);
9229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		FD_SET(x11_fd, &fdset);
9239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( select(x11_fd+1, &fdset, NULL, NULL, &zero_time) == 1 ) {
9249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(XPending(display));
9259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
9269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
9279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Oh well, nothing is ready .. */
9299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
9309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
9319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid X11_PumpEvents(_THIS)
9339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
9349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int pending;
9359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Update activity every five seconds to prevent screensaver. --ryan. */
9379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!allow_screensaver) {
9389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		static Uint32 screensaverTicks;
9399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Uint32 nowTicks = SDL_GetTicks();
9409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ((nowTicks - screensaverTicks) > 5000) {
9419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			XResetScreenSaver(SDL_Display);
9429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			screensaverTicks = nowTicks;
9439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
9449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
9459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Keep processing pending events */
9479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pending = 0;
9489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while ( X11_Pending(SDL_Display) ) {
9499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		X11_DispatchEvent(this);
9509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		++pending;
9519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
9529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( switch_waiting ) {
9539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Uint32 now;
9549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		now  = SDL_GetTicks();
9569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( pending || !SDL_VideoSurface ) {
9579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Try again later... */
9589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( switch_waiting & SDL_FULLSCREEN ) {
9599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				switch_time = now + 1500;
9609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else {
9619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				switch_time = now + 200;
9629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
9639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else if ( (int)(switch_time-now) <= 0 ) {
9649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Uint32 go_fullscreen;
9659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			go_fullscreen = switch_waiting & SDL_FULLSCREEN;
9679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			switch_waiting = 0;
9689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( SDL_VideoSurface->flags & SDL_FULLSCREEN ) {
9699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( go_fullscreen ) {
9709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					X11_EnterFullScreen(this);
9719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else {
9729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					X11_LeaveFullScreen(this);
9739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
9749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
9759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Handle focus in/out when grabbed */
9769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( go_fullscreen ) {
9779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				X11_GrabInputNoLock(this, this->input_grab);
9789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else {
9799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				X11_GrabInputNoLock(this, SDL_GRAB_OFF);
9809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
9819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			X11_CheckMouseModeNoLock(this);
9829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
9839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
9849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
9859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid X11_InitKeymap(void)
9879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
9889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
9899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Odd keys used in international keyboards */
9919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<SDL_arraysize(ODD_keymap); ++i )
9929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ODD_keymap[i] = SDLK_UNKNOWN;
9939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	/* Some of these might be mappable to an existing SDLK_ code */
9959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	ODD_keymap[XK_dead_grave&0xFF] = SDLK_COMPOSE;
9969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	ODD_keymap[XK_dead_acute&0xFF] = SDLK_COMPOSE;
9979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	ODD_keymap[XK_dead_tilde&0xFF] = SDLK_COMPOSE;
9989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	ODD_keymap[XK_dead_macron&0xFF] = SDLK_COMPOSE;
9999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	ODD_keymap[XK_dead_breve&0xFF] = SDLK_COMPOSE;
10009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	ODD_keymap[XK_dead_abovedot&0xFF] = SDLK_COMPOSE;
10019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	ODD_keymap[XK_dead_diaeresis&0xFF] = SDLK_COMPOSE;
10029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	ODD_keymap[XK_dead_abovering&0xFF] = SDLK_COMPOSE;
10039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	ODD_keymap[XK_dead_doubleacute&0xFF] = SDLK_COMPOSE;
10049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	ODD_keymap[XK_dead_caron&0xFF] = SDLK_COMPOSE;
10059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	ODD_keymap[XK_dead_cedilla&0xFF] = SDLK_COMPOSE;
10069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	ODD_keymap[XK_dead_ogonek&0xFF] = SDLK_COMPOSE;
10079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	ODD_keymap[XK_dead_iota&0xFF] = SDLK_COMPOSE;
10089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	ODD_keymap[XK_dead_voiced_sound&0xFF] = SDLK_COMPOSE;
10099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	ODD_keymap[XK_dead_semivoiced_sound&0xFF] = SDLK_COMPOSE;
10109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	ODD_keymap[XK_dead_belowdot&0xFF] = SDLK_COMPOSE;
10119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef XK_dead_hook
10129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	ODD_keymap[XK_dead_hook&0xFF] = SDLK_COMPOSE;
10139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
10149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef XK_dead_horn
10159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 	ODD_keymap[XK_dead_horn&0xFF] = SDLK_COMPOSE;
10169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
10179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef XK_dead_circumflex
10199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* These X keysyms have 0xFE as the high byte */
10209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ODD_keymap[XK_dead_circumflex&0xFF] = SDLK_CARET;
10219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
10229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef XK_ISO_Level3_Shift
10239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ODD_keymap[XK_ISO_Level3_Shift&0xFF] = SDLK_MODE; /* "Alt Gr" key */
10249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
10259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Map the miscellaneous keys */
10279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<SDL_arraysize(MISC_keymap); ++i )
10289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		MISC_keymap[i] = SDLK_UNKNOWN;
10299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* These X keysyms have 0xFF as the high byte */
10319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_BackSpace&0xFF] = SDLK_BACKSPACE;
10329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Tab&0xFF] = SDLK_TAB;
10339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Clear&0xFF] = SDLK_CLEAR;
10349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Return&0xFF] = SDLK_RETURN;
10359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Pause&0xFF] = SDLK_PAUSE;
10369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Escape&0xFF] = SDLK_ESCAPE;
10379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Delete&0xFF] = SDLK_DELETE;
10389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_0&0xFF] = SDLK_KP0;		/* Keypad 0-9 */
10409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_1&0xFF] = SDLK_KP1;
10419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_2&0xFF] = SDLK_KP2;
10429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_3&0xFF] = SDLK_KP3;
10439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_4&0xFF] = SDLK_KP4;
10449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_5&0xFF] = SDLK_KP5;
10459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_6&0xFF] = SDLK_KP6;
10469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_7&0xFF] = SDLK_KP7;
10479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_8&0xFF] = SDLK_KP8;
10489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_9&0xFF] = SDLK_KP9;
10499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_Insert&0xFF] = SDLK_KP0;
10509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_End&0xFF] = SDLK_KP1;
10519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_Down&0xFF] = SDLK_KP2;
10529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_Page_Down&0xFF] = SDLK_KP3;
10539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_Left&0xFF] = SDLK_KP4;
10549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_Begin&0xFF] = SDLK_KP5;
10559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_Right&0xFF] = SDLK_KP6;
10569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_Home&0xFF] = SDLK_KP7;
10579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_Up&0xFF] = SDLK_KP8;
10589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_Page_Up&0xFF] = SDLK_KP9;
10599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_Delete&0xFF] = SDLK_KP_PERIOD;
10609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_Decimal&0xFF] = SDLK_KP_PERIOD;
10619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_Divide&0xFF] = SDLK_KP_DIVIDE;
10629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_Multiply&0xFF] = SDLK_KP_MULTIPLY;
10639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_Subtract&0xFF] = SDLK_KP_MINUS;
10649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_Add&0xFF] = SDLK_KP_PLUS;
10659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_Enter&0xFF] = SDLK_KP_ENTER;
10669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_KP_Equal&0xFF] = SDLK_KP_EQUALS;
10679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Up&0xFF] = SDLK_UP;
10699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Down&0xFF] = SDLK_DOWN;
10709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Right&0xFF] = SDLK_RIGHT;
10719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Left&0xFF] = SDLK_LEFT;
10729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Insert&0xFF] = SDLK_INSERT;
10739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Home&0xFF] = SDLK_HOME;
10749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_End&0xFF] = SDLK_END;
10759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Page_Up&0xFF] = SDLK_PAGEUP;
10769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Page_Down&0xFF] = SDLK_PAGEDOWN;
10779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_F1&0xFF] = SDLK_F1;
10799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_F2&0xFF] = SDLK_F2;
10809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_F3&0xFF] = SDLK_F3;
10819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_F4&0xFF] = SDLK_F4;
10829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_F5&0xFF] = SDLK_F5;
10839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_F6&0xFF] = SDLK_F6;
10849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_F7&0xFF] = SDLK_F7;
10859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_F8&0xFF] = SDLK_F8;
10869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_F9&0xFF] = SDLK_F9;
10879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_F10&0xFF] = SDLK_F10;
10889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_F11&0xFF] = SDLK_F11;
10899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_F12&0xFF] = SDLK_F12;
10909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_F13&0xFF] = SDLK_F13;
10919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_F14&0xFF] = SDLK_F14;
10929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_F15&0xFF] = SDLK_F15;
10939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Num_Lock&0xFF] = SDLK_NUMLOCK;
10959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Caps_Lock&0xFF] = SDLK_CAPSLOCK;
10969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Scroll_Lock&0xFF] = SDLK_SCROLLOCK;
10979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Shift_R&0xFF] = SDLK_RSHIFT;
10989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Shift_L&0xFF] = SDLK_LSHIFT;
10999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Control_R&0xFF] = SDLK_RCTRL;
11009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Control_L&0xFF] = SDLK_LCTRL;
11019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Alt_R&0xFF] = SDLK_RALT;
11029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Alt_L&0xFF] = SDLK_LALT;
11039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Meta_R&0xFF] = SDLK_RMETA;
11049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Meta_L&0xFF] = SDLK_LMETA;
11059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Super_L&0xFF] = SDLK_LSUPER; /* Left "Windows" */
11069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Super_R&0xFF] = SDLK_RSUPER; /* Right "Windows */
11079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Mode_switch&0xFF] = SDLK_MODE; /* "Alt Gr" key */
11089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Multi_key&0xFF] = SDLK_COMPOSE; /* Multi-key compose */
11099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Help&0xFF] = SDLK_HELP;
11119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Print&0xFF] = SDLK_PRINT;
11129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Sys_Req&0xFF] = SDLK_SYSREQ;
11139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Break&0xFF] = SDLK_BREAK;
11149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Menu&0xFF] = SDLK_MENU;
11159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MISC_keymap[XK_Hyper_R&0xFF] = SDLK_MENU;   /* Windows "Menu" key */
11169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
11179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Get the translated SDL virtual keysym */
11199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDLKey X11_TranslateKeycode(Display *display, KeyCode kc)
11209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
11219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	KeySym xsym;
11229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDLKey key;
11239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	xsym = XKeycodeToKeysym(display, kc, 0);
11259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_KEYS
11269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fprintf(stderr, "Translating key code %d -> 0x%.4x\n", kc, xsym);
11279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
11289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	key = SDLK_UNKNOWN;
11299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( xsym ) {
11309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch (xsym>>8) {
11319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 0x1005FF:
11329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SunXK_F36
11339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( xsym == SunXK_F36 )
11349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				key = SDLK_F11;
11359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
11369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SunXK_F37
11379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( xsym == SunXK_F37 )
11389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				key = SDLK_F12;
11399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
11409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
11419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 0x00:	/* Latin 1 */
11429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			key = (SDLKey)(xsym & 0xFF);
11439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
11449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 0x01:	/* Latin 2 */
11459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 0x02:	/* Latin 3 */
11469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 0x03:	/* Latin 4 */
11479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 0x04:	/* Katakana */
11489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 0x05:	/* Arabic */
11499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 0x06:	/* Cyrillic */
11509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 0x07:	/* Greek */
11519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 0x08:	/* Technical */
11529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 0x0A:	/* Publishing */
11539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 0x0C:	/* Hebrew */
11549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 0x0D:	/* Thai */
11559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* These are wrong, but it's better than nothing */
11569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			key = (SDLKey)(xsym & 0xFF);
11579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
11589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 0xFE:
11599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			key = ODD_keymap[xsym&0xFF];
11609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
11619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 0xFF:
11629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			key = MISC_keymap[xsym&0xFF];
11639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
11649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    default:
11659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/*
11669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			fprintf(stderr, "X11: Unhandled xsym, sym = 0x%04x\n",
11679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					(unsigned int)xsym);
11689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*/
11699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
11709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
11719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
11729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* X11 doesn't know how to translate the key! */
11739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch (kc) {
11749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    /* Caution:
11759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		       These keycodes are from the Microsoft Keyboard
11769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     */
11779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 115:
11789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			key = SDLK_LSUPER;
11799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
11809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 116:
11819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			key = SDLK_RSUPER;
11829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
11839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 117:
11849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			key = SDLK_MENU;
11859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
11869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    default:
11879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/*
11889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 * no point in an error message; happens for
11899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 * several keys when we get a keymap notify
11909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 */
11919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
11929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
11939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
11949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return key;
11959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
11969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* X11 modifier masks for various keys */
11989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic unsigned meta_l_mask, meta_r_mask, alt_l_mask, alt_r_mask;
11999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic unsigned num_mask, mode_switch_mask;
12009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void get_modifier_masks(Display *display)
12029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
12039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	static unsigned got_masks;
12049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, j;
12059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	XModifierKeymap *xmods;
12069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	unsigned n;
12079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(got_masks)
12099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return;
12109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	xmods = XGetModifierMapping(display);
12129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	n = xmods->max_keypermod;
12139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for(i = 3; i < 8; i++) {
12149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for(j = 0; j < n; j++) {
12159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			KeyCode kc = xmods->modifiermap[i * n + j];
12169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			KeySym ks = XKeycodeToKeysym(display, kc, 0);
12179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			unsigned mask = 1 << i;
12189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			switch(ks) {
12199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case XK_Num_Lock:
12209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				num_mask = mask; break;
12219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case XK_Alt_L:
12229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				alt_l_mask = mask; break;
12239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case XK_Alt_R:
12249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				alt_r_mask = mask; break;
12259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case XK_Meta_L:
12269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				meta_l_mask = mask; break;
12279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case XK_Meta_R:
12289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				meta_r_mask = mask; break;
12299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case XK_Mode_switch:
12309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				mode_switch_mask = mask; break;
12319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
12329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
12339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
12349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	XFreeModifiermap(xmods);
12359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	got_masks = 1;
12369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
12379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
12409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * This function is semi-official; it is not officially exported and should
12419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * not be considered part of the SDL API, but may be used by client code
12429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * that *really* needs it (including legacy code).
12439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * It is slow, though, and should be avoided if possible.
12449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *
12459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Note that it isn't completely accurate either; in particular, multi-key
12469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * sequences (dead accents, compose key sequences) will not work since the
12479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * state has been irrevocably lost.
12489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
12499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint16 X11_KeyToUnicode(SDLKey keysym, SDLMod modifiers)
12509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
12519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct SDL_VideoDevice *this = current_video;
12529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char keybuf[32];
12539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
12549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	KeySym xsym = 0;
12559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	XKeyEvent xkey;
12569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint16 unicode;
12579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !this || !SDL_Display ) {
12599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
12609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
12619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(&xkey, 0, sizeof(xkey));
12639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	xkey.display = SDL_Display;
12649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	xsym = keysym;		/* last resort if not found */
12669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (i = 0; i < 256; ++i) {
12679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( MISC_keymap[i] == keysym ) {
12689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			xsym = 0xFF00 | i;
12699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
12709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else if ( ODD_keymap[i] == keysym ) {
12719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			xsym = 0xFE00 | i;
12729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
12739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
12749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
12759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	xkey.keycode = XKeysymToKeycode(xkey.display, xsym);
12779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	get_modifier_masks(SDL_Display);
12799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(modifiers & KMOD_SHIFT)
12809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		xkey.state |= ShiftMask;
12819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(modifiers & KMOD_CAPS)
12829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		xkey.state |= LockMask;
12839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(modifiers & KMOD_CTRL)
12849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		xkey.state |= ControlMask;
12859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(modifiers & KMOD_MODE)
12869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		xkey.state |= mode_switch_mask;
12879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(modifiers & KMOD_LALT)
12889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		xkey.state |= alt_l_mask;
12899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(modifiers & KMOD_RALT)
12909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		xkey.state |= alt_r_mask;
12919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(modifiers & KMOD_LMETA)
12929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		xkey.state |= meta_l_mask;
12939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(modifiers & KMOD_RMETA)
12949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		xkey.state |= meta_r_mask;
12959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(modifiers & KMOD_NUM)
12969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		xkey.state |= num_mask;
12979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	unicode = 0;
12999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( XLookupString(&xkey, keybuf, sizeof(keybuf), NULL, NULL) )
13009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		unicode = (unsigned char)keybuf[0];
13019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(unicode);
13029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
13039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
13049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
13059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
13069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Called when focus is regained, to read the keyboard state and generate
13079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * synthetic keypress/release events.
13089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * key_vec is a bit vector of keycodes (256 bits)
13099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
13109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid X11_SetKeyboardState(Display *display, const char *key_vec)
13119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
13129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char keys_return[32];
13139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
13149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint8 *kstate = SDL_GetKeyState(NULL);
13159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDLMod modstate;
13169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Window junk_window;
13179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int x, y;
13189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	unsigned int mask;
13199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
13209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* The first time the window is mapped, we initialize key state */
13219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! key_vec ) {
13229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		XQueryKeymap(display, keys_return);
13239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		key_vec = keys_return;
13249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
13259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
13269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Get the keyboard modifier state */
13279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	modstate = 0;
13289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	get_modifier_masks(display);
13299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( XQueryPointer(display, DefaultRootWindow(display),
13309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		&junk_window, &junk_window, &x, &y, &x, &y, &mask) ) {
13319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( mask & LockMask ) {
13329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			modstate |= KMOD_CAPS;
13339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
13349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( mask & mode_switch_mask ) {
13359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			modstate |= KMOD_MODE;
13369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
13379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( mask & num_mask ) {
13389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			modstate |= KMOD_NUM;
13399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
13409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
13419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
13429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Zero the new keyboard state and generate it */
13439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(kstate, 0, SDLK_LAST);
13449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/*
13459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * An obvious optimisation is to check entire longwords at a time in
13469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * both loops, but we can't be sure the arrays are aligned so it's not
13479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * worth the extra complexity
13489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
13499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i = 0; i < 32; i++ ) {
13509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int j;
13519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( !key_vec[i] )
13529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			continue;
13539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( j = 0; j < 8; j++ ) {
13549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( key_vec[i] & (1 << j) ) {
13559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDLKey key;
13569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				KeyCode kc = (i << 3 | j);
13579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				key = X11_TranslateKeycode(display, kc);
13589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( key == SDLK_UNKNOWN ) {
13599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					continue;
13609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
13619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				kstate[key] = SDL_PRESSED;
13629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				switch (key) {
13639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				    case SDLK_LSHIFT:
13649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					modstate |= KMOD_LSHIFT;
13659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
13669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				    case SDLK_RSHIFT:
13679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					modstate |= KMOD_RSHIFT;
13689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
13699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				    case SDLK_LCTRL:
13709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					modstate |= KMOD_LCTRL;
13719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
13729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				    case SDLK_RCTRL:
13739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					modstate |= KMOD_RCTRL;
13749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
13759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				    case SDLK_LALT:
13769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					modstate |= KMOD_LALT;
13779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
13789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				    case SDLK_RALT:
13799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					modstate |= KMOD_RALT;
13809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
13819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				    case SDLK_LMETA:
13829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					modstate |= KMOD_LMETA;
13839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
13849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				    case SDLK_RMETA:
13859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					modstate |= KMOD_RMETA;
13869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
13879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				    default:
13889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
13899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
13909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
13919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
13929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
13939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
13949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Hack - set toggle key state */
13959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( modstate & KMOD_CAPS ) {
13969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		kstate[SDLK_CAPSLOCK] = SDL_PRESSED;
13979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
13989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		kstate[SDLK_CAPSLOCK] = SDL_RELEASED;
13999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
14009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( modstate & KMOD_NUM ) {
14019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		kstate[SDLK_NUMLOCK] = SDL_PRESSED;
14029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
14039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		kstate[SDLK_NUMLOCK] = SDL_RELEASED;
14049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
14059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
14069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the final modifier state */
14079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetModState(modstate);
14089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
14099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
14109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid X11_InitOSKeymap(_THIS)
14119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
14129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	X11_InitKeymap();
14139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
14149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1415