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