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