Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Checking in Christian Walther's patch for x11 keyboard input. Minor c…
…ode tweaks by Bob.
  • Loading branch information
pendletonrc committed Jan 8, 2008
1 parent 29c2b33 commit b582738
Show file tree
Hide file tree
Showing 13 changed files with 1,484 additions and 105 deletions.
5 changes: 4 additions & 1 deletion include/SDL_compat.h
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions include/SDL_keysym.h
Expand Up @@ -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,

Expand Down
51 changes: 43 additions & 8 deletions src/events/SDL_keyboard.c
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/joystick/darwin/SDL_sysjoystick.c
Expand Up @@ -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:
Expand Down
20 changes: 10 additions & 10 deletions src/video/SDL_renderer_gl.c
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
105 changes: 38 additions & 67 deletions src/video/x11/SDL_x11events.c
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit b582738

Please sign in to comment.