src/video/x11/SDL_x11events.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 06 Mar 2002 11:23:08 +0000
changeset 297 f6ffac90895c
parent 252 e8157fcb3114
child 412 ac59b067815b
permissions -rw-r--r--
Updated copyright information for 2002
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@297
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  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];
slouken@0
    65
SDL_keysym *X11_TranslateKey(Display *display, 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;
slouken@0
    77
	if ( XPending(display) ) {
slouken@0
    78
		XPeekEvent(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;
slouken@0
    83
			XNextEvent(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 */
slouken@0
   118
		while ( XCheckTypedEvent(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;
slouken@0
   130
		XWarpPointer(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 ) {
slouken@0
   133
        		XMaskEvent(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
slouken@0
   162
	XNextEvent(SDL_Display, &xevent);
slouken@0
   163
slouken@0
   164
	posted = 0;
slouken@0
   165
	switch (xevent.type) {
slouken@0
   166
slouken@0
   167
	    /* Gaining mouse coverage? */
slouken@0
   168
	    case EnterNotify: {
slouken@0
   169
#ifdef DEBUG_XEVENTS
slouken@82
   170
printf("EnterNotify! (%d,%d)\n", xevent.xcrossing.x, xevent.xcrossing.y);
slouken@0
   171
if ( xevent.xcrossing.mode == NotifyGrab )
slouken@0
   172
printf("Mode: NotifyGrab\n");
slouken@0
   173
if ( xevent.xcrossing.mode == NotifyUngrab )
slouken@0
   174
printf("Mode: NotifyUngrab\n");
slouken@0
   175
#endif
slouken@0
   176
		if ( (xevent.xcrossing.mode != NotifyGrab) &&
slouken@0
   177
		     (xevent.xcrossing.mode != NotifyUngrab) ) {
slouken@82
   178
			if ( this->input_grab == SDL_GRAB_OFF ) {
slouken@82
   179
				posted = SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
slouken@82
   180
			} else {
slouken@82
   181
				posted = SDL_PrivateMouseMotion(0, 0,
slouken@82
   182
						xevent.xcrossing.x,
slouken@82
   183
						xevent.xcrossing.y);
slouken@82
   184
			}
slouken@0
   185
		}
slouken@0
   186
	    }
slouken@0
   187
	    break;
slouken@0
   188
slouken@0
   189
	    /* Losing mouse coverage? */
slouken@0
   190
	    case LeaveNotify: {
slouken@0
   191
#ifdef DEBUG_XEVENTS
slouken@82
   192
printf("LeaveNotify! (%d,%d)\n", xevent.xcrossing.x, xevent.xcrossing.y);
slouken@0
   193
if ( xevent.xcrossing.mode == NotifyGrab )
slouken@0
   194
printf("Mode: NotifyGrab\n");
slouken@0
   195
if ( xevent.xcrossing.mode == NotifyUngrab )
slouken@0
   196
printf("Mode: NotifyUngrab\n");
slouken@0
   197
#endif
slouken@0
   198
		if ( (xevent.xcrossing.mode != NotifyGrab) &&
slouken@0
   199
		     (xevent.xcrossing.mode != NotifyUngrab) ) {
slouken@82
   200
			if ( this->input_grab == SDL_GRAB_OFF ) {
slouken@82
   201
				posted = SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
slouken@82
   202
			} else {
slouken@82
   203
				posted = SDL_PrivateMouseMotion(0, 0,
slouken@82
   204
						xevent.xcrossing.x,
slouken@82
   205
						xevent.xcrossing.y);
slouken@82
   206
			}
slouken@0
   207
		}
slouken@0
   208
	    }
slouken@0
   209
	    break;
slouken@0
   210
slouken@0
   211
	    /* Gaining input focus? */
slouken@0
   212
	    case FocusIn: {
slouken@0
   213
#ifdef DEBUG_XEVENTS
slouken@0
   214
printf("FocusIn!\n");
slouken@0
   215
#endif
slouken@0
   216
		posted = SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
slouken@0
   217
slouken@0
   218
		/* Queue entry into fullscreen mode */
slouken@0
   219
		switch_waiting = 0x01 | SDL_FULLSCREEN;
slouken@0
   220
		switch_time = SDL_GetTicks() + 1500;
slouken@0
   221
	    }
slouken@0
   222
	    break;
slouken@0
   223
slouken@0
   224
	    /* Losing input focus? */
slouken@0
   225
	    case FocusOut: {
slouken@0
   226
#ifdef DEBUG_XEVENTS
slouken@0
   227
printf("FocusOut!\n");
slouken@0
   228
#endif
slouken@0
   229
		posted = SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
slouken@0
   230
slouken@0
   231
		/* Queue leaving fullscreen mode */
slouken@0
   232
		switch_waiting = 0x01;
slouken@0
   233
		switch_time = SDL_GetTicks() + 200;
slouken@0
   234
	    }
slouken@0
   235
	    break;
slouken@0
   236
slouken@0
   237
	    /* Generated upon EnterWindow and FocusIn */
slouken@0
   238
	    case KeymapNotify: {
slouken@14
   239
#ifdef DEBUG_XEVENTS
slouken@14
   240
printf("KeymapNotify!\n");
slouken@14
   241
#endif
slouken@0
   242
		X11_SetKeyboardState(SDL_Display, xevent.xkeymap.key_vector);
slouken@0
   243
	    }
slouken@0
   244
	    break;
slouken@0
   245
slouken@0
   246
	    /* Mouse motion? */
slouken@0
   247
	    case MotionNotify: {
slouken@0
   248
		if ( SDL_VideoSurface ) {
slouken@0
   249
			if ( mouse_relative ) {
slouken@0
   250
				if ( using_dga & DGA_MOUSE ) {
slouken@0
   251
#ifdef DEBUG_MOTION
slouken@0
   252
  printf("DGA motion: %d,%d\n", xevent.xmotion.x_root, xevent.xmotion.y_root);
slouken@0
   253
#endif
slouken@0
   254
					posted = SDL_PrivateMouseMotion(0, 1,
slouken@0
   255
							xevent.xmotion.x_root,
slouken@0
   256
							xevent.xmotion.y_root);
slouken@0
   257
				} else {
slouken@0
   258
					posted = X11_WarpedMotion(this,&xevent);
slouken@0
   259
				}
slouken@0
   260
			} else {
slouken@82
   261
#ifdef DEBUG_MOTION
slouken@82
   262
  printf("X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y);
slouken@82
   263
#endif
slouken@0
   264
				posted = SDL_PrivateMouseMotion(0, 0,
slouken@0
   265
						xevent.xmotion.x,
slouken@0
   266
						xevent.xmotion.y);
slouken@0
   267
			}
slouken@0
   268
		}
slouken@0
   269
	    }
slouken@0
   270
	    break;
slouken@0
   271
slouken@0
   272
	    /* Mouse button press? */
slouken@0
   273
	    case ButtonPress: {
slouken@0
   274
		posted = SDL_PrivateMouseButton(SDL_PRESSED, 
slouken@0
   275
					xevent.xbutton.button, 0, 0);
slouken@0
   276
	    }
slouken@0
   277
	    break;
slouken@0
   278
slouken@0
   279
	    /* Mouse button release? */
slouken@0
   280
	    case ButtonRelease: {
slouken@0
   281
		posted = SDL_PrivateMouseButton(SDL_RELEASED, 
slouken@0
   282
					xevent.xbutton.button, 0, 0);
slouken@0
   283
	    }
slouken@0
   284
	    break;
slouken@0
   285
slouken@0
   286
	    /* Key press? */
slouken@0
   287
	    case KeyPress: {
slouken@0
   288
		SDL_keysym keysym;
slouken@14
   289
slouken@14
   290
#ifdef DEBUG_XEVENTS
slouken@14
   291
printf("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
slouken@14
   292
#endif
slouken@0
   293
		posted = SDL_PrivateKeyboard(SDL_PRESSED,
slouken@0
   294
				X11_TranslateKey(SDL_Display, &xevent.xkey,
slouken@0
   295
						 xevent.xkey.keycode,
slouken@0
   296
						 &keysym));
slouken@0
   297
	    }
slouken@0
   298
	    break;
slouken@0
   299
slouken@0
   300
	    /* Key release? */
slouken@0
   301
	    case KeyRelease: {
slouken@0
   302
		SDL_keysym keysym;
slouken@0
   303
slouken@14
   304
#ifdef DEBUG_XEVENTS
slouken@14
   305
printf("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
slouken@14
   306
#endif
slouken@0
   307
		/* Check to see if this is a repeated key */
slouken@0
   308
		if ( ! X11_KeyRepeat(SDL_Display, &xevent) ) {
slouken@0
   309
			posted = SDL_PrivateKeyboard(SDL_RELEASED, 
slouken@0
   310
				X11_TranslateKey(SDL_Display, &xevent.xkey,
slouken@0
   311
						 xevent.xkey.keycode,
slouken@0
   312
						 &keysym));
slouken@0
   313
		}
slouken@0
   314
	    }
slouken@0
   315
	    break;
slouken@0
   316
slouken@0
   317
	    /* Have we been iconified? */
slouken@0
   318
	    case UnmapNotify: {
slouken@0
   319
#ifdef DEBUG_XEVENTS
slouken@0
   320
printf("UnmapNotify!\n");
slouken@0
   321
#endif
slouken@0
   322
		/* If we're active, make ourselves inactive */
slouken@0
   323
		if ( SDL_GetAppState() & SDL_APPACTIVE ) {
slouken@0
   324
			/* Swap out the gamma before we go inactive */
slouken@0
   325
			X11_SwapVidModeGamma(this);
slouken@0
   326
slouken@0
   327
			/* Send an internal deactivate event */
slouken@0
   328
			posted = SDL_PrivateAppActive(0,
slouken@0
   329
					SDL_APPACTIVE|SDL_APPINPUTFOCUS);
slouken@0
   330
		}
slouken@0
   331
	    }
slouken@0
   332
	    break;
slouken@0
   333
slouken@0
   334
	    /* Have we been restored? */
slouken@0
   335
	    case MapNotify: {
slouken@0
   336
#ifdef DEBUG_XEVENTS
slouken@0
   337
printf("MapNotify!\n");
slouken@0
   338
#endif
slouken@0
   339
		/* If we're not active, make ourselves active */
slouken@0
   340
		if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
slouken@0
   341
			/* Send an internal activate event */
slouken@0
   342
			posted = SDL_PrivateAppActive(1, SDL_APPACTIVE);
slouken@0
   343
slouken@0
   344
			/* Now that we're active, swap the gamma back */
slouken@0
   345
			X11_SwapVidModeGamma(this);
slouken@0
   346
		}
slouken@0
   347
slouken@0
   348
		if ( SDL_VideoSurface &&
slouken@0
   349
		     (SDL_VideoSurface->flags & SDL_FULLSCREEN) ) {
slouken@0
   350
			X11_EnterFullScreen(this);
slouken@0
   351
		} else {
slouken@0
   352
			X11_GrabInputNoLock(this, this->input_grab);
slouken@0
   353
		}
slouken@8
   354
		X11_CheckMouseModeNoLock(this);
slouken@8
   355
slouken@0
   356
		if ( SDL_VideoSurface ) {
slouken@0
   357
			X11_RefreshDisplay(this);
slouken@0
   358
		}
slouken@0
   359
	    }
slouken@0
   360
	    break;
slouken@0
   361
slouken@0
   362
	    /* Have we been resized or moved? */
slouken@0
   363
	    case ConfigureNotify: {
slouken@0
   364
#ifdef DEBUG_XEVENTS
slouken@0
   365
printf("ConfigureNotify! (resize: %dx%d)\n", xevent.xconfigure.width, xevent.xconfigure.height);
slouken@0
   366
#endif
slouken@0
   367
		if ( SDL_VideoSurface ) {
slouken@0
   368
		    if ((xevent.xconfigure.width != SDL_VideoSurface->w) ||
slouken@0
   369
		        (xevent.xconfigure.height != SDL_VideoSurface->h)) {
slouken@0
   370
			/* FIXME: Find a better fix for the bug with KDE 1.2 */
slouken@0
   371
			if ( ! ((xevent.xconfigure.width == 32) &&
slouken@0
   372
			        (xevent.xconfigure.height == 32)) ) {
slouken@0
   373
				SDL_PrivateResize(xevent.xconfigure.width,
slouken@0
   374
				                  xevent.xconfigure.height);
slouken@0
   375
			}
slouken@0
   376
		    } else {
slouken@0
   377
			/* OpenGL windows need to know about the change */
slouken@0
   378
			if ( SDL_VideoSurface->flags & SDL_OPENGL ) {
slouken@0
   379
				SDL_PrivateExpose();
slouken@0
   380
			}
slouken@0
   381
		    }
slouken@0
   382
		}
slouken@0
   383
	    }
slouken@0
   384
	    break;
slouken@0
   385
slouken@0
   386
	    /* Have we been requested to quit (or another client message?) */
slouken@0
   387
	    case ClientMessage: {
slouken@0
   388
		if ( (xevent.xclient.format == 32) &&
slouken@0
   389
		     (xevent.xclient.data.l[0] == WM_DELETE_WINDOW) )
slouken@0
   390
		{
slouken@0
   391
			posted = SDL_PrivateQuit();
slouken@0
   392
		} else
slouken@0
   393
		if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) {
slouken@0
   394
			SDL_SysWMmsg wmmsg;
slouken@0
   395
slouken@0
   396
			SDL_VERSION(&wmmsg.version);
slouken@0
   397
			wmmsg.subsystem = SDL_SYSWM_X11;
slouken@0
   398
			wmmsg.event.xevent = xevent;
slouken@0
   399
			posted = SDL_PrivateSysWMEvent(&wmmsg);
slouken@0
   400
		}
slouken@0
   401
	    }
slouken@0
   402
	    break;
slouken@0
   403
slouken@0
   404
	    /* Do we need to refresh ourselves? */
slouken@0
   405
	    case Expose: {
slouken@0
   406
#ifdef DEBUG_XEVENTS
slouken@0
   407
printf("Expose (count = %d)\n", xevent.xexpose.count);
slouken@0
   408
#endif
slouken@0
   409
		if ( SDL_VideoSurface && (xevent.xexpose.count == 0) ) {
slouken@161
   410
			X11_RefreshDisplay(this);
slouken@0
   411
		}
slouken@0
   412
	    }
slouken@0
   413
	    break;
slouken@0
   414
slouken@0
   415
	    default: {
slouken@0
   416
#ifdef DEBUG_XEVENTS
slouken@0
   417
printf("Unhandled event %d\n", xevent.type);
slouken@0
   418
#endif
slouken@0
   419
		/* Only post the event if we're watching for it */
slouken@0
   420
		if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) {
slouken@0
   421
			SDL_SysWMmsg wmmsg;
slouken@0
   422
slouken@0
   423
			SDL_VERSION(&wmmsg.version);
slouken@0
   424
			wmmsg.subsystem = SDL_SYSWM_X11;
slouken@0
   425
			wmmsg.event.xevent = xevent;
slouken@0
   426
			posted = SDL_PrivateSysWMEvent(&wmmsg);
slouken@0
   427
		}
slouken@0
   428
	    }
slouken@0
   429
	    break;
slouken@0
   430
	}
slouken@0
   431
	return(posted);
slouken@0
   432
}
slouken@0
   433
slouken@0
   434
/* Ack!  XPending() actually performs a blocking read if no events available */
slouken@0
   435
int X11_Pending(Display *display)
slouken@0
   436
{
slouken@0
   437
	/* Flush the display connection and look to see if events are queued */
slouken@0
   438
	XFlush(display);
slouken@0
   439
	if ( XEventsQueued(display, QueuedAlready) ) {
slouken@0
   440
		return(1);
slouken@0
   441
	}
slouken@0
   442
slouken@0
   443
	/* More drastic measures are required -- see if X is ready to talk */
slouken@0
   444
	{
slouken@0
   445
		static struct timeval zero_time;	/* static == 0 */
slouken@0
   446
		int x11_fd;
slouken@0
   447
		fd_set fdset;
slouken@0
   448
slouken@0
   449
		x11_fd = ConnectionNumber(display);
slouken@0
   450
		FD_ZERO(&fdset);
slouken@0
   451
		FD_SET(x11_fd, &fdset);
slouken@0
   452
		if ( select(x11_fd+1, &fdset, NULL, NULL, &zero_time) == 1 ) {
slouken@0
   453
			return(XPending(display));
slouken@0
   454
		}
slouken@0
   455
	}
slouken@0
   456
slouken@0
   457
	/* Oh well, nothing is ready .. */
slouken@0
   458
	return(0);
slouken@0
   459
}
slouken@0
   460
slouken@0
   461
void X11_PumpEvents(_THIS)
slouken@0
   462
{
slouken@0
   463
	int pending;
slouken@0
   464
slouken@0
   465
	/* Keep processing pending events */
slouken@0
   466
	pending = 0;
slouken@0
   467
	while ( X11_Pending(SDL_Display) ) {
slouken@0
   468
		X11_DispatchEvent(this);
slouken@0
   469
		++pending;
slouken@0
   470
	}
slouken@0
   471
	if ( switch_waiting ) {
slouken@0
   472
		Uint32 now;
slouken@0
   473
slouken@0
   474
		now  = SDL_GetTicks();
slouken@0
   475
		if ( pending || !SDL_VideoSurface ) {
slouken@0
   476
			/* Try again later... */
slouken@0
   477
			if ( switch_waiting & SDL_FULLSCREEN ) {
slouken@0
   478
				switch_time = now + 1500;
slouken@0
   479
			} else {
slouken@0
   480
				switch_time = now + 200;
slouken@0
   481
			}
slouken@0
   482
		} else if ( now >= switch_time ) {
slouken@0
   483
			Uint32 go_fullscreen;
slouken@0
   484
slouken@0
   485
			go_fullscreen = switch_waiting & SDL_FULLSCREEN;
slouken@0
   486
			switch_waiting = 0;
slouken@0
   487
			if ( SDL_VideoSurface->flags & SDL_FULLSCREEN ) {
slouken@0
   488
				if ( go_fullscreen ) {
slouken@0
   489
					X11_EnterFullScreen(this);
slouken@0
   490
				} else {
slouken@0
   491
					X11_LeaveFullScreen(this);
slouken@0
   492
				}
slouken@0
   493
			}
slouken@0
   494
			/* Handle focus in/out when grabbed */
slouken@0
   495
			if ( go_fullscreen ) {
slouken@0
   496
				X11_GrabInputNoLock(this, this->input_grab);
slouken@0
   497
			} else {
slouken@0
   498
				X11_GrabInputNoLock(this, SDL_GRAB_OFF);
slouken@0
   499
			}
slouken@0
   500
			X11_CheckMouseModeNoLock(this);
slouken@0
   501
		}
slouken@0
   502
	}
slouken@0
   503
}
slouken@0
   504
slouken@0
   505
void X11_InitKeymap(void)
slouken@0
   506
{
slouken@0
   507
	int i;
slouken@0
   508
slouken@0
   509
	/* Odd keys used in international keyboards */
slouken@0
   510
	for ( i=0; i<SDL_TABLESIZE(ODD_keymap); ++i )
slouken@0
   511
		ODD_keymap[i] = SDLK_UNKNOWN;
slouken@0
   512
slouken@0
   513
#ifdef XK_dead_circumflex
slouken@0
   514
	/* These X keysyms have 0xFE as the high byte */
slouken@0
   515
	ODD_keymap[XK_dead_circumflex&0xFF] = SDLK_CARET;
slouken@0
   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
slouken@0
   610
SDL_keysym *X11_TranslateKey(Display *display, XKeyEvent *xkey, KeyCode kc,
slouken@0
   611
			     SDL_keysym *keysym)
slouken@0
   612
{
slouken@0
   613
	KeySym xsym;
slouken@0
   614
slouken@0
   615
	/* Get the raw keyboard scancode */
slouken@0
   616
	keysym->scancode = kc;
slouken@0
   617
	xsym = XKeycodeToKeysym(display, kc, 0);
slouken@0
   618
#ifdef DEBUG_KEYS
slouken@0
   619
	fprintf(stderr, "Translating key 0x%.4x (%d)\n", xsym, kc);
slouken@0
   620
#endif
slouken@0
   621
	/* Get the translated SDL virtual keysym */
slouken@0
   622
	keysym->sym = SDLK_UNKNOWN;
slouken@0
   623
	if ( xsym ) {
slouken@0
   624
		switch (xsym>>8) {
slouken@0
   625
			case 0x1005FF:
slouken@0
   626
#ifdef SunXK_F36
slouken@0
   627
				if ( xsym == SunXK_F36 )
slouken@0
   628
					keysym->sym = SDLK_F11;
slouken@0
   629
#endif
slouken@0
   630
#ifdef SunXK_F37
slouken@0
   631
				if ( xsym == SunXK_F37 )
slouken@0
   632
					keysym->sym = SDLK_F12;
slouken@0
   633
#endif
slouken@0
   634
				break;
slouken@0
   635
			case 0x00:	/* Latin 1 */
slouken@0
   636
			case 0x01:	/* Latin 2 */
slouken@0
   637
			case 0x02:	/* Latin 3 */
slouken@0
   638
			case 0x03:	/* Latin 4 */
slouken@0
   639
			case 0x04:	/* Katakana */
slouken@0
   640
			case 0x05:	/* Arabic */
slouken@0
   641
			case 0x06:	/* Cyrillic */
slouken@0
   642
			case 0x07:	/* Greek */
slouken@0
   643
			case 0x08:	/* Technical */
slouken@0
   644
			case 0x0A:	/* Publishing */
slouken@0
   645
			case 0x0C:	/* Hebrew */
slouken@0
   646
			case 0x0D:	/* Thai */
slouken@0
   647
				keysym->sym = (SDLKey)(xsym&0xFF);
slouken@0
   648
				/* Map capital letter syms to lowercase */
slouken@0
   649
				if ((keysym->sym >= 'A')&&(keysym->sym <= 'Z'))
slouken@0
   650
					keysym->sym += ('a'-'A');
slouken@0
   651
				break;
slouken@0
   652
			case 0xFE:
slouken@0
   653
				keysym->sym = ODD_keymap[xsym&0xFF];
slouken@0
   654
				break;
slouken@0
   655
			case 0xFF:
slouken@0
   656
				keysym->sym = MISC_keymap[xsym&0xFF];
slouken@0
   657
				break;
slouken@0
   658
			default:
slouken@0
   659
				fprintf(stderr,
slouken@0
   660
					"X11: Unknown xsym, sym = 0x%04x\n",
slouken@0
   661
					(unsigned int)xsym);
slouken@0
   662
				break;
slouken@0
   663
		}
slouken@0
   664
	} else {
slouken@0
   665
		/* X11 doesn't know how to translate the key! */
slouken@0
   666
		switch (kc) {
slouken@0
   667
			/* Caution:
slouken@0
   668
			   These keycodes are from the Microsoft Keyboard
slouken@0
   669
			 */
slouken@0
   670
			case 115:
slouken@0
   671
				keysym->sym = SDLK_LSUPER;
slouken@0
   672
				break;
slouken@0
   673
			case 116:
slouken@0
   674
				keysym->sym = SDLK_RSUPER;
slouken@0
   675
				break;
slouken@0
   676
			case 117:
slouken@0
   677
				keysym->sym = SDLK_MENU;
slouken@0
   678
				break;
slouken@0
   679
			default:
slouken@0
   680
				/*
slouken@0
   681
				 * no point in an error message; happens for
slouken@0
   682
				 * several keys when we get a keymap notify
slouken@0
   683
				 */
slouken@0
   684
				break;
slouken@0
   685
		}
slouken@0
   686
	}
slouken@0
   687
	keysym->mod = KMOD_NONE;
slouken@0
   688
slouken@0
   689
	/* If UNICODE is on, get the UNICODE value for the key */
slouken@0
   690
	keysym->unicode = 0;
slouken@0
   691
	if ( SDL_TranslateUNICODE && xkey ) {
slouken@0
   692
		static XComposeStatus state;
slouken@0
   693
		/* Until we handle the IM protocol, use XLookupString() */
slouken@0
   694
		unsigned char keybuf[32];
slouken@0
   695
slouken@0
   696
#define BROKEN_XFREE86_INTERNATIONAL_KBD
slouken@0
   697
/* This appears to be a magical flag that is used with AltGr on
slouken@0
   698
   international keyboards to signal alternate key translations.
slouken@0
   699
   The flag doesn't show up when in fullscreen mode (?)
slouken@0
   700
   FIXME:  Check to see if this code is safe for other servers.
slouken@0
   701
*/
slouken@0
   702
#ifdef BROKEN_XFREE86_INTERNATIONAL_KBD
slouken@0
   703
		/* Work around what appears to be a bug in XFree86 */
slouken@0
   704
		if ( SDL_GetModState() & KMOD_MODE ) {
slouken@0
   705
			xkey->state |= (1<<13);
slouken@0
   706
		}
slouken@0
   707
#endif
slouken@0
   708
		/* Look up the translated value for the key event */
slouken@0
   709
		if ( XLookupString(xkey, (char *)keybuf, sizeof(keybuf),
slouken@0
   710
							NULL, &state) ) {
slouken@0
   711
			/*
slouken@0
   712
			 * FIXME,: XLookupString() may yield more than one
slouken@0
   713
			 * character, so we need a mechanism to allow for
slouken@0
   714
			 * this (perhaps generate null keypress events with
slouken@0
   715
			 * a unicode value)
slouken@0
   716
			 */
slouken@0
   717
			keysym->unicode = keybuf[0];
slouken@0
   718
		}
slouken@0
   719
	}
slouken@0
   720
	return(keysym);
slouken@0
   721
}
slouken@0
   722
slouken@0
   723
/* X11 modifier masks for various keys */
slouken@0
   724
static unsigned meta_l_mask, meta_r_mask, alt_l_mask, alt_r_mask;
slouken@0
   725
static unsigned num_mask, mode_switch_mask;
slouken@0
   726
slouken@0
   727
static void get_modifier_masks(Display *display)
slouken@0
   728
{
slouken@0
   729
	static unsigned got_masks;
slouken@0
   730
	int i, j;
slouken@0
   731
	XModifierKeymap *xmods;
slouken@0
   732
	unsigned n;
slouken@0
   733
slouken@0
   734
	if(got_masks)
slouken@0
   735
		return;
slouken@0
   736
slouken@0
   737
	xmods = XGetModifierMapping(display);
slouken@0
   738
	n = xmods->max_keypermod;
slouken@0
   739
	for(i = 3; i < 8; i++) {
slouken@0
   740
		for(j = 0; j < n; j++) {
slouken@0
   741
			KeyCode kc = xmods->modifiermap[i * n + j];
slouken@0
   742
			KeySym ks = XKeycodeToKeysym(display, kc, 0);
slouken@0
   743
			unsigned mask = 1 << i;
slouken@0
   744
			switch(ks) {
slouken@0
   745
			case XK_Num_Lock:
slouken@0
   746
				num_mask = mask; break;
slouken@0
   747
			case XK_Alt_L:
slouken@0
   748
				alt_l_mask = mask; break;
slouken@0
   749
			case XK_Alt_R:
slouken@0
   750
				alt_r_mask = mask; break;
slouken@0
   751
			case XK_Meta_L:
slouken@0
   752
				meta_l_mask = mask; break;
slouken@0
   753
			case XK_Meta_R:
slouken@0
   754
				meta_r_mask = mask; break;
slouken@0
   755
			case XK_Mode_switch:
slouken@0
   756
				mode_switch_mask = mask; break;
slouken@0
   757
			}
slouken@0
   758
		}
slouken@0
   759
	}
slouken@0
   760
	XFreeModifiermap(xmods);
slouken@0
   761
	got_masks = 1;
slouken@0
   762
}
slouken@0
   763
slouken@0
   764
slouken@0
   765
/*
slouken@0
   766
 * This function is semi-official; it is not officially exported and should
slouken@0
   767
 * not be considered part of the SDL API, but may be used by client code
slouken@0
   768
 * that *really* needs it (including legacy code).
slouken@0
   769
 * It is slow, though, and should be avoided if possible.
slouken@0
   770
 *
slouken@0
   771
 * Note that it isn't completely accurate either; in particular, multi-key
slouken@0
   772
 * sequences (dead accents, compose key sequences) will not work since the
slouken@0
   773
 * state has been irrevocably lost.
slouken@0
   774
 */
slouken@0
   775
Uint16 X11_KeyToUnicode(SDLKey keysym, SDLMod modifiers)
slouken@0
   776
{
slouken@0
   777
	struct SDL_VideoDevice *this = current_video;
slouken@0
   778
	char keybuf[32];
slouken@0
   779
	int i;
slouken@0
   780
	KeySym xsym = 0;
slouken@0
   781
	XKeyEvent xkey;
slouken@0
   782
	Uint16 unicode;
slouken@0
   783
slouken@0
   784
	if ( !this || !SDL_Display ) {
slouken@0
   785
		return 0;
slouken@0
   786
	}
slouken@0
   787
slouken@0
   788
	memset(&xkey, 0, sizeof(xkey));
slouken@0
   789
	xkey.display = SDL_Display;
slouken@0
   790
slouken@0
   791
	xsym = keysym;		/* last resort if not found */
slouken@0
   792
	for (i = 0; i < 256; ++i) {
slouken@0
   793
		if ( MISC_keymap[i] == keysym ) {
slouken@0
   794
			xsym = 0xFF00 | i;
slouken@0
   795
			break;
slouken@0
   796
		} else if ( ODD_keymap[i] == keysym ) {
slouken@0
   797
			xsym = 0xFE00 | i;
slouken@0
   798
			break;
slouken@0
   799
		}
slouken@0
   800
	}
slouken@0
   801
slouken@0
   802
	xkey.keycode = XKeysymToKeycode(xkey.display, xsym);
slouken@0
   803
slouken@0
   804
	get_modifier_masks(SDL_Display);
slouken@0
   805
	if(modifiers & KMOD_SHIFT)
slouken@0
   806
		xkey.state |= ShiftMask;
slouken@0
   807
	if(modifiers & KMOD_CAPS)
slouken@0
   808
		xkey.state |= LockMask;
slouken@0
   809
	if(modifiers & KMOD_CTRL)
slouken@0
   810
		xkey.state |= ControlMask;
slouken@0
   811
	if(modifiers & KMOD_MODE)
slouken@0
   812
		xkey.state |= mode_switch_mask;
slouken@0
   813
	if(modifiers & KMOD_LALT)
slouken@0
   814
		xkey.state |= alt_l_mask;
slouken@0
   815
	if(modifiers & KMOD_RALT)
slouken@0
   816
		xkey.state |= alt_r_mask;
slouken@0
   817
	if(modifiers & KMOD_LMETA)
slouken@0
   818
		xkey.state |= meta_l_mask;
slouken@0
   819
	if(modifiers & KMOD_RMETA)
slouken@0
   820
		xkey.state |= meta_r_mask;
slouken@0
   821
	if(modifiers & KMOD_NUM)
slouken@0
   822
		xkey.state |= num_mask;
slouken@0
   823
slouken@0
   824
	unicode = 0;
slouken@0
   825
	if ( XLookupString(&xkey, keybuf, sizeof(keybuf), NULL, NULL) )
slouken@0
   826
		unicode = (unsigned char)keybuf[0];
slouken@0
   827
	return(unicode);
slouken@0
   828
}
slouken@0
   829
slouken@0
   830
/*
slouken@0
   831
 * Called when focus is regained, to read the keyboard state and generate
slouken@0
   832
 * synthetic keypress/release events.
slouken@0
   833
 * key_vec is a bit vector of keycodes (256 bits)
slouken@0
   834
 */
slouken@0
   835
void X11_SetKeyboardState(Display *display, const char *key_vec)
slouken@0
   836
{
slouken@0
   837
	char keys_return[32];
slouken@0
   838
	int i, gen_event;
slouken@0
   839
	KeyCode xcode[SDLK_LAST];
slouken@0
   840
	Uint8 new_kstate[SDLK_LAST];
slouken@0
   841
	Uint8 *kstate = SDL_GetKeyState(NULL);
slouken@111
   842
	SDLMod modstate;
slouken@111
   843
	Window junk_window;
slouken@111
   844
	int x, y;
slouken@111
   845
	unsigned int mask;
slouken@0
   846
slouken@0
   847
	/* The first time the window is mapped, we initialize key state */
slouken@0
   848
	if ( ! key_vec ) {
slouken@0
   849
		key_vec = keys_return;
slouken@0
   850
		XQueryKeymap(display, keys_return);
slouken@0
   851
		gen_event = 0;
slouken@0
   852
	} else {
slouken@111
   853
#if 1 /* We no longer generate key down events, just update state */
slouken@111
   854
		gen_event = 0;
slouken@111
   855
#else
slouken@0
   856
		gen_event = 1;
slouken@111
   857
#endif
slouken@0
   858
	}
slouken@0
   859
slouken@111
   860
	/* Get the keyboard modifier state */
slouken@111
   861
	modstate = 0;
slouken@111
   862
	get_modifier_masks(display);
slouken@111
   863
	if ( XQueryPointer(display, DefaultRootWindow(display),
slouken@111
   864
		&junk_window, &junk_window, &x, &y, &x, &y, &mask) ) {
slouken@111
   865
		if ( mask & LockMask ) {
slouken@111
   866
			modstate |= KMOD_CAPS;
slouken@111
   867
		}
slouken@111
   868
		if ( mask & mode_switch_mask ) {
slouken@111
   869
			modstate |= KMOD_MODE;
slouken@111
   870
		}
slouken@111
   871
		if ( mask & num_mask ) {
slouken@111
   872
			modstate |= KMOD_NUM;
slouken@111
   873
		}
slouken@111
   874
	}
slouken@111
   875
slouken@111
   876
	/* Zero the new keyboard state and generate it */
slouken@111
   877
	memset(new_kstate, SDL_RELEASED, sizeof(new_kstate));
slouken@0
   878
	/*
slouken@0
   879
	 * An obvious optimisation is to check entire longwords at a time in
slouken@0
   880
	 * both loops, but we can't be sure the arrays are aligned so it's not
slouken@0
   881
	 * worth the extra complexity
slouken@0
   882
	 */
slouken@0
   883
	for(i = 0; i < 32; i++) {
slouken@0
   884
		int j;
slouken@0
   885
		if(!key_vec[i])
slouken@0
   886
			continue;
slouken@0
   887
		for(j = 0; j < 8; j++) {
slouken@0
   888
			if(key_vec[i] & (1 << j)) {
slouken@0
   889
				SDL_keysym sk;
slouken@0
   890
				KeyCode kc = i << 3 | j;
slouken@0
   891
				X11_TranslateKey(display, NULL, kc, &sk);
slouken@111
   892
				new_kstate[sk.sym] = SDL_PRESSED;
slouken@0
   893
				xcode[sk.sym] = kc;
slouken@0
   894
			}
slouken@0
   895
		}
slouken@0
   896
	}
slouken@0
   897
	for(i = SDLK_FIRST+1; i < SDLK_LAST; i++) {
slouken@111
   898
		int state = new_kstate[i];
slouken@0
   899
slouken@111
   900
		if ( state == SDL_PRESSED ) {
slouken@111
   901
			switch (i) {
slouken@111
   902
				case SDLK_LSHIFT:
slouken@111
   903
					modstate |= KMOD_LSHIFT;
slouken@111
   904
					break;
slouken@111
   905
				case SDLK_RSHIFT:
slouken@111
   906
					modstate |= KMOD_RSHIFT;
slouken@111
   907
					break;
slouken@111
   908
				case SDLK_LCTRL:
slouken@111
   909
					modstate |= KMOD_LCTRL;
slouken@111
   910
					break;
slouken@111
   911
				case SDLK_RCTRL:
slouken@111
   912
					modstate |= KMOD_RCTRL;
slouken@111
   913
					break;
slouken@111
   914
				case SDLK_LALT:
slouken@111
   915
					modstate |= KMOD_LALT;
slouken@111
   916
					break;
slouken@111
   917
				case SDLK_RALT:
slouken@111
   918
					modstate |= KMOD_RALT;
slouken@111
   919
					break;
slouken@111
   920
				case SDLK_LMETA:
slouken@111
   921
					modstate |= KMOD_LMETA;
slouken@111
   922
					break;
slouken@111
   923
				case SDLK_RMETA:
slouken@111
   924
					modstate |= KMOD_RMETA;
slouken@111
   925
					break;
slouken@111
   926
				default:
slouken@111
   927
					break;
slouken@111
   928
			}
slouken@111
   929
		}
slouken@111
   930
		if ( kstate[i] == state )
slouken@0
   931
			continue;
slouken@111
   932
slouken@0
   933
		/*
slouken@0
   934
		 * Send a fake keyboard event correcting the difference between
slouken@0
   935
		 * SDL's keyboard state and the actual. Note that there is no
slouken@0
   936
		 * way to find out the scancode for key releases, but since all
slouken@0
   937
		 * keys are released when focus is lost only keypresses should
slouken@0
   938
		 * be sent here
slouken@0
   939
		 */
slouken@0
   940
		if ( gen_event ) {
slouken@111
   941
			SDL_keysym sk;
slouken@111
   942
			memset(&sk, 0, sizeof(sk));
slouken@111
   943
			sk.sym = i;
slouken@111
   944
			sk.scancode = xcode[i];	/* only valid for key press */
slouken@111
   945
			SDL_PrivateKeyboard(state, &sk);
slouken@0
   946
		} else {
slouken@111
   947
			kstate[i] = state;
slouken@0
   948
		}
slouken@0
   949
	}
slouken@111
   950
slouken@111
   951
	/* Hack - set toggle key state */
slouken@111
   952
	if ( modstate & KMOD_CAPS ) {
slouken@111
   953
		kstate[SDLK_CAPSLOCK] = SDL_PRESSED;
slouken@111
   954
	} else {
slouken@111
   955
		kstate[SDLK_CAPSLOCK] = SDL_RELEASED;
slouken@111
   956
	}
slouken@111
   957
	if ( modstate & KMOD_NUM ) {
slouken@111
   958
		kstate[SDLK_NUMLOCK] = SDL_PRESSED;
slouken@111
   959
	} else {
slouken@111
   960
		kstate[SDLK_NUMLOCK] = SDL_RELEASED;
slouken@111
   961
	}
slouken@111
   962
slouken@111
   963
	/* Set the final modifier state */
slouken@111
   964
	SDL_SetModState(modstate);
slouken@0
   965
}
slouken@0
   966
slouken@0
   967
void X11_InitOSKeymap(_THIS)
slouken@0
   968
{
slouken@0
   969
	X11_InitKeymap();
slouken@0
   970
}
slouken@0
   971