Commit e558eee5 authored by Bob Pendleton's avatar Bob Pendleton

Checking in Christian Walther's patch for x11 keyboard input. Minor code tweaks by Bob.

--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%402704
parent 6f2082d5
......@@ -185,7 +185,10 @@ struct SDL_SysWMinfo;
#define SDLK_SCROLLOCK SDLK_SCROLLLOCK
#define SDLK_PRINT SDLK_PRINTSCREEN
/* These key constants are obsoleted the new keyboard handling, their definitions here correspond to how they appear on a US keyboard. */
/* These key constants are obsoleted by the new keyboard handling,
their definitions here correspond to how they appear on a US
keyboard. */
#define SDLK_EXCLAIM SDLK_1
#define SDLK_QUOTEDBL SDLK_APOSTROPHE
#define SDLK_HASH SDLK_3
......
......@@ -300,9 +300,10 @@ enum SDLPhysicalKey
SDLK_MUTE = SDL_PHYSICAL_KEY(127),
SDLK_VOLUMEUP = SDL_PHYSICAL_KEY(128),
SDLK_VOLUMEDOWN = SDL_PHYSICAL_KEY(129),
/*SDLK_LOCKINGCAPSLOCK = SDL_PHYSICAL_KEY(130), not sure whether there's a reason to enable these
SDLK_LOCKINGNUMLOCK = SDL_PHYSICAL_KEY(131),
SDLK_LOCKINGSCROLLLOCK = SDL_PHYSICAL_KEY(132), */
/* not sure whether there's a reason to enable these */
/* SDLK_LOCKINGCAPSLOCK = SDL_PHYSICAL_KEY(130), */
/* SDLK_LOCKINGNUMLOCK = SDL_PHYSICAL_KEY(131), */
/* SDLK_LOCKINGSCROLLLOCK = SDL_PHYSICAL_KEY(132), */
SDLK_KP_COMMA = SDL_PHYSICAL_KEY(133) | SDL_KEY_KEYPAD_BIT,
SDLK_KP_EQUALSAS400 = SDL_PHYSICAL_KEY(134) | SDL_KEY_KEYPAD_BIT,
......
......@@ -36,6 +36,48 @@ static int SDL_num_keyboards;
static int SDL_current_keyboard;
static SDL_Keyboard **SDL_keyboards;
/* Taken from SDL_iconv() */
static char *
encodeUtf8(Uint32 ch, char *dst)
{
Uint8 *p = (Uint8 *) dst;
if (ch <= 0x7F) {
*p = (Uint8) ch;
++dst;
} else if (ch <= 0x7FF) {
p[0] = 0xC0 | (Uint8) ((ch >> 6) & 0x1F);
p[1] = 0x80 | (Uint8) (ch & 0x3F);
dst += 2;
} else if (ch <= 0xFFFF) {
p[0] = 0xE0 | (Uint8) ((ch >> 12) & 0x0F);
p[1] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
p[2] = 0x80 | (Uint8) (ch & 0x3F);
dst += 3;
} else if (ch <= 0x1FFFFF) {
p[0] = 0xF0 | (Uint8) ((ch >> 18) & 0x07);
p[1] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
p[2] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
p[3] = 0x80 | (Uint8) (ch & 0x3F);
dst += 4;
} else if (ch <= 0x3FFFFFF) {
p[0] = 0xF8 | (Uint8) ((ch >> 24) & 0x03);
p[1] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
p[2] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
p[3] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
p[4] = 0x80 | (Uint8) (ch & 0x3F);
dst += 5;
} else {
p[0] = 0xFC | (Uint8) ((ch >> 30) & 0x01);
p[1] = 0x80 | (Uint8) ((ch >> 24) & 0x3F);
p[2] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
p[3] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
p[4] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
p[5] = 0x80 | (Uint8) (ch & 0x3F);
dst += 6;
}
return dst;
}
/* Public functions */
int
SDL_KeyboardInit(void)
......@@ -227,21 +269,14 @@ SDL_GetKeyName(SDLKey layoutKey)
/* SDLK_INDEX(layoutKey) is the unicode code point of the character generated by the key */
static char buffer[9]; /* 6 (maximal UTF-8 char length) + 2 ([] for keypad) + 1 (null teminator) */
char *bufferPtr = &buffer[1];
SDL_iconv_t cd;
size_t inbytesleft = 4, outbytesleft = 8;
Uint32 codepoint = SDLK_INDEX(layoutKey);
const char *codepointPtr = (const char *) &codepoint;
/* Unaccented letter keys on latin keyboards are normally labeled in upper case (and probably on others like Greek or Cyrillic too, so if you happen to know for sure, please adapt this). */
if (codepoint >= 'a' && codepoint <= 'z') {
codepoint -= 32;
}
cd = SDL_iconv_open("UTF-8", "UCS-4");
if (cd == (SDL_iconv_t) (-1))
return "";
SDL_iconv(cd, &codepointPtr, &inbytesleft, &bufferPtr, &outbytesleft);
SDL_iconv_close(cd);
bufferPtr = encodeUtf8(codepoint, bufferPtr);
*bufferPtr = '\0';
if ((layoutKey & SDL_KEY_KEYPAD_BIT) != 0) {
......
......@@ -820,9 +820,9 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
range = (element->max - element->min + 1);
value = HIDGetElementValue(device, element) - element->min;
if (range == 4) /* 4 position hatswitch - scale up value */
if (range == 4) /* 4 position hatswitch - scale up value */
value *= 2;
else if (range != 8) /* Neither a 4 nor 8 positions - fall back to default position (centered) */
else if (range != 8) /* Neither a 4 nor 8 positions - fall back to default position (centered) */
value = -1;
switch (value) {
case 0:
......
......@@ -295,12 +295,11 @@ GL_CreateRenderer(SDL_Window * window, Uint32 flags)
GL_DestroyRenderer(renderer);
return NULL;
}
#ifdef __MACOSX__
/* Enable multi-threaded rendering */
/* Disabled until Ryan finishes his VBO/PBO code...
CGLEnable(CGLGetCurrentContext(), kCGLCEMPEngine);
*/
CGLEnable(CGLGetCurrentContext(), kCGLCEMPEngine);
*/
#endif
if (flags & SDL_RENDERER_PRESENTVSYNC) {
......@@ -579,14 +578,15 @@ GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
renderdata->glTexParameteri(data->type, GL_TEXTURE_STORAGE_HINT_APPLE,
GL_STORAGE_CACHED_APPLE);
}
if (texture->access == SDL_TEXTUREACCESS_STREAMING && texture->format == SDL_PIXELFORMAT_ARGB8888 ) {
if (texture->access == SDL_TEXTUREACCESS_STREAMING
&& texture->format == SDL_PIXELFORMAT_ARGB8888) {
/*
if (renderdata->glTextureRangeAPPLE) {
renderdata->glTextureRangeAPPLE(data->type,
texture->h * data->pitch,
data->pixels);
}
*/
if (renderdata->glTextureRangeAPPLE) {
renderdata->glTextureRangeAPPLE(data->type,
texture->h * data->pitch,
data->pixels);
}
*/
renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
texture_h, 0, format, type, data->pixels);
......
......@@ -30,6 +30,28 @@
#include "../../events/SDL_events_c.h"
/* Check to see if this is a repeated key.
(idea shamelessly lifted from GII -- thanks guys! :)
*/
static int
X11_KeyRepeat(Display * display, XEvent * event)
{
XEvent peekevent;
int repeated;
repeated = 0;
if (XPending(display)) {
XPeekEvent(display, &peekevent);
if ((peekevent.type == KeyPress) &&
(peekevent.xkey.keycode == event->xkey.keycode) &&
((peekevent.xkey.time - event->xkey.time) < 2)) {
repeated = 1;
XNextEvent(display, &peekevent);
}
}
return (repeated);
}
static void
X11_DispatchEvent(_THIS)
{
......@@ -167,95 +189,44 @@ X11_DispatchEvent(_THIS)
/* Key press? */
case KeyPress:{
#if 0 /* FIXME */
static SDL_keysym saved_keysym;
SDL_keysym keysym;
KeyCode keycode = xevent.xkey.keycode;
#ifdef DEBUG_XEVENTS
printf("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
#endif
/* Get the translated SDL virtual keysym */
if (keycode) {
keysym.scancode = keycode;
keysym.sym = X11_TranslateKeycode(SDL_Display, keycode);
keysym.mod = KMOD_NONE;
keysym.unicode = 0;
} else {
keysym = saved_keysym;
}
/* If we're not doing translation, we're done! */
if (!SDL_TranslateUNICODE) {
posted = SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
break;
}
if (XFilterEvent(&xevent, None)) {
if (xevent.xkey.keycode) {
posted = SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
} else {
/* Save event to be associated with IM text
In 1.3 we'll have a text event instead.. */
saved_keysym = keysym;
}
break;
}
/* Look up the translated value for the key event */
#ifdef X_HAVE_UTF8_STRING
if (data->ic != NULL) {
static Status state;
/* A UTF-8 character can be at most 6 bytes */
char keybuf[6];
if (Xutf8LookupString(data->ic, &xevent.xkey,
keybuf, sizeof(keybuf), NULL, &state)) {
keysym.unicode = Utf8ToUcs4((Uint8 *) keybuf);
if (!X11_KeyRepeat(videodata->display, &xevent)) {
SDLKey physicalKey = videodata->keyCodeToSDLKTable[keycode];
SDL_SendKeyboardKey(videodata->keyboard, SDL_PRESSED,
(Uint8) keycode, physicalKey);
#if 1
if (physicalKey == SDLK_UNKNOWN) {
fprintf(stderr,
"The key you just pressed is not recognized by SDL. To help get this fixed, report this to the SDL mailing list <sdl@libsdl.org> or to Christian Walther <cwalther@gmx.ch>. X11 KeyCode is %d, X11 KeySym 0x%X.\n",
(int) keycode,
(unsigned int) XKeycodeToKeysym(videodata->
display, keycode,
0));
}
} else
#endif
{
static XComposeStatus state;
char keybuf[32];
if (XLookupString(&xevent.xkey,
keybuf, sizeof(keybuf), NULL, &state)) {
/*
* FIXME: XLookupString() may yield more than one
* character, so we need a mechanism to allow for
* this (perhaps null keypress events with a
* unicode value)
*/
keysym.unicode = (Uint8) keybuf[0];
}
}
posted = SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
#endif // 0
}
break;
/* Key release? */
case KeyRelease:{
#if 0 /* FIXME */
SDL_keysym keysym;
KeyCode keycode = xevent.xkey.keycode;
#ifdef DEBUG_XEVENTS
printf("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
#endif
/* Check to see if this is a repeated key */
if (X11_KeyRepeat(SDL_Display, &xevent)) {
if (X11_KeyRepeat(videodata->display, &xevent)) {
break;
}
/* Get the translated SDL virtual keysym */
keysym.scancode = keycode;
keysym.sym = X11_TranslateKeycode(SDL_Display, keycode);
keysym.mod = KMOD_NONE;
keysym.unicode = 0;
posted = SDL_PrivateKeyboard(SDL_RELEASED, &keysym);
#endif // 0
SDL_SendKeyboardKey(videodata->keyboard, SDL_RELEASED,
(Uint8) keycode,
videodata->keyCodeToSDLKTable[keycode]);
}
break;
......
This diff is collapsed.
......@@ -24,7 +24,8 @@
#ifndef _SDL_x11keyboard_h
#define _SDL_x11keyboard_h
extern void X11_InitKeyboard(_THIS);
extern int X11_InitKeyboard(_THIS);
extern SDLKey X11_GetLayoutKey(_THIS, SDLKey physicalKey);
extern void X11_QuitKeyboard(_THIS);
#endif /* _SDL_x11keyboard_h */
......
......@@ -171,6 +171,7 @@ X11_CreateDevice(int devindex)
device->SetDisplayGammaRamp = X11_SetDisplayGammaRamp;
device->GetDisplayGammaRamp = X11_GetDisplayGammaRamp;
device->PumpEvents = X11_PumpEvents;
device->GetLayoutKey = X11_GetLayoutKey;
device->CreateWindow = X11_CreateWindow;
device->CreateWindowFrom = X11_CreateWindowFrom;
......@@ -235,14 +236,9 @@ X11_VideoInit(_THIS)
X11_InitModes(_this);
//#if SDL_VIDEO_RENDER_D3D
// D3D_AddRenderDriver(_this);
//#endif
//#if SDL_VIDEO_RENDER_GDI
// GDI_AddRenderDriver(_this);
//#endif
X11_InitKeyboard(_this);
if (X11_InitKeyboard(_this) != 0) {
return -1;
}
X11_InitMouse(_this);
return 0;
......
......@@ -70,6 +70,7 @@ typedef struct SDL_VideoData
int mouse;
int keyboard;
Atom WM_DELETE_WINDOW;
SDLKey *keyCodeToSDLKTable;
} SDL_VideoData;
#endif /* _SDL_x11video_h */
......
This diff is collapsed.
#ifndef _imKStoUCS_h
#define _imKStoUCS_h
/* Copyright (C) 1994-2003 The XFree86 Project, Inc. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is fur-
nished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FIT-
NESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CON-
NECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of the XFree86 Project shall not
be used in advertising or otherwise to promote the sale, use or other deal-
ings in this Software without prior written authorization from the XFree86
Project.
*/
extern unsigned int X11_KeySymToUcs4(KeySym keysym);
#endif /*_imKStoUCS_h */
......@@ -57,11 +57,15 @@ PrintKey(SDL_keysym * sym, int pressed)
{
/* Print the keycode, name and state */
if (sym->sym) {
printf("Key %s: %d-%s ", pressed ? "pressed" : "released",
sym->sym, SDL_GetKeyName(sym->sym));
printf("Key %s: physical 0x%08X = %s, layout 0x%08X = %s ",
pressed ? "pressed " : "released",
sym->sym,
SDL_GetKeyName(sym->sym),
SDL_GetLayoutKey(sym->sym),
SDL_GetKeyName(SDL_GetLayoutKey(sym->sym)));
} else {
printf("Unknown Key (scancode = %d) %s ", sym->scancode,
pressed ? "pressed" : "released");
printf("Unknown Key (scancode = 0x%08X) %s ",
sym->scancode, pressed ? "pressed" : "released");
}
/* Print the translated character, if one exists */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment