src/video/x11/SDL_x11events.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 21 Nov 2005 00:16:34 +0000
changeset 1178 9867f3d86e44
parent 1168 045f186426e1
child 1179 abb4267e7028
permissions -rw-r--r--
Real Unicode support for X11. Based on updated version of this patch:
http://lists.arabeyes.org/archives/developer/2004/June/msg00160.html

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