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