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