Commit daddb897 authored by Ryan C. Gordon's avatar Ryan C. Gordon

Significantly improved XIM support.

   Fixes Bugzilla #429.


Selected notes from the patch's README:

= FIXES =

This patch fixes the above issues as follows.

== X11 events ==

Moved XFilterEvent just after XNextEvent so that all events are passed
to it.  Also, XFilterEvent will receive masks indicated by IM through
XNFilterEvents IC value as well as masks surpplied by SDL.

X11_KeyRepeat is called between XNextEvent and XFilterEvent, after
testing an event is a KeyRelease.  I'm not 100% comfortable to do so,
but I couldn't find a better timing to call it, and use of the
function is inevitable.

== Xutf8LookupString ==

Used a longer buffer to receive UTF-8 string.  If it is insufficient,
a dynamic storage of the requested size will be allocated.  The
initial size of the buffer is set to 32, because the Japanese text
converted from the most widely used benchmark key sequence for
Japanese IM, "WATASHINONAMAEHANAKANODESU." has ten Japanese characters
in it, that occupies 30 bytes when encoded in UTF-8.

== SDL_keysym.unicode ==

On Windows version of SDL implementation, SDL_keysym.unicode stores
UTF-16 encoded unicode characters, one UTF-16 encoding unit per an SDL
event.  A Unicode supplementary characters are sent to an application
as two events.  (One with a high surrogate and another with a low
surrogate.)  The behavior seems reasonable since it is upward
compatible with existing handling of BMP characters.

I wrote a UTF-8 to UTF-16 conversion function for the purpose.  It is
designed with the execution speed in mind, having a minimum set of
features that my patch requires.

--HG--
branch : SDL-1.2
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/branches/SDL-1.2%402378
parent 0f38978e
......@@ -98,6 +98,7 @@ static void X11_GetSym(const char *fnname, int *rc, void **fn)
/* Annoying varargs entry point... */
#ifdef X_HAVE_UTF8_STRING
XIC (*pXCreateIC)(XIM,...) = NULL;
char *(*pXGetICValues)(XIC, ...) = NULL;
#endif
/* These SDL_X11_HAVE_* flags are here whether you have dynamic X11 or not. */
......@@ -127,6 +128,7 @@ void SDL_X11_UnloadSymbols(void)
#ifdef X_HAVE_UTF8_STRING
pXCreateIC = NULL;
pXGetICValues = NULL;
#endif
for (i = 0; i < SDL_TABLESIZE(x11libs); i++) {
......@@ -163,6 +165,7 @@ int SDL_X11_LoadSymbols(void)
#ifdef X_HAVE_UTF8_STRING
X11_GetSym("XCreateIC",&SDL_X11_HAVE_UTF8,(void **)&pXCreateIC);
X11_GetSym("XGetICValues",&SDL_X11_HAVE_UTF8,(void **)&pXGetICValues);
#endif
if (SDL_X11_HAVE_BASEXLIB) { /* all required symbols loaded. */
......@@ -178,6 +181,7 @@ int SDL_X11_LoadSymbols(void)
#endif
#ifdef X_HAVE_UTF8_STRING
pXCreateIC = XCreateIC;
pXGetICValues = XGetICValues;
#endif
#endif
......
......@@ -68,6 +68,7 @@ void SDL_X11_UnloadSymbols(void);
/* That's really annoying...make this a function pointer no matter what. */
#ifdef X_HAVE_UTF8_STRING
extern XIC (*pXCreateIC)(XIM,...);
extern char *(*pXGetICValues)(XIC, ...);
#endif
/* These SDL_X11_HAVE_* flags are here whether you have dynamic X11 or not. */
......
This diff is collapsed.
......@@ -143,8 +143,10 @@ SDL_X11_SYM(int,Xutf8LookupString,(XIC a,XKeyPressedEvent* b,char* c,int d,KeySy
SDL_X11_SYM(void,XDestroyIC,(XIC a),(a),)
SDL_X11_SYM(void,XSetICFocus,(XIC a),(a),)
SDL_X11_SYM(void,XUnsetICFocus,(XIC a),(a),)
/*SDL_X11_SYM(char*,XGetICValues,(XIC a, ...),return)*/
SDL_X11_SYM(XIM,XOpenIM,(Display* a,struct _XrmHashBucketRec* b,char* c,char* d),(a,b,c,d),return)
SDL_X11_SYM(Status,XCloseIM,(XIM a),(a),return)
SDL_X11_SYM(char*,XSetLocaleModifiers,(_Xconst char* a),(a),return)
#endif
#ifndef NO_SHARED_MEMORY
......
......@@ -54,6 +54,10 @@
#include "SDL_x11gamma_c.h"
#include "../blank_cursor.h"
#ifdef X_HAVE_UTF8_STRING
#include <locale.h>
#endif
/* Initialization/Query functions */
static int X11_VideoInit(_THIS, SDL_PixelFormat *vformat);
static SDL_Surface *X11_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
......@@ -105,8 +109,10 @@ static SDL_VideoDevice *X11_CreateDevice(int devindex)
SDL_memset(device, 0, (sizeof *device));
device->hidden = (struct SDL_PrivateVideoData *)
SDL_malloc((sizeof *device->hidden));
SDL_memset(device->hidden, 0, (sizeof *device->hidden));
device->gl_data = (struct SDL_PrivateGLData *)
SDL_malloc((sizeof *device->gl_data));
SDL_memset(device->gl_data, 0, (sizeof *device->gl_data));
}
if ( (device == NULL) || (device->hidden == NULL) ||
(device->gl_data == NULL) ) {
......@@ -314,6 +320,7 @@ static void create_aux_windows(_THIS)
char classname[1024];
XSetWindowAttributes xattr;
XWMHints *hints;
unsigned long app_event_mask;
int def_vis = (SDL_Visual == DefaultVisual(SDL_Display, SDL_Screen));
/* Look up some useful Atoms */
......@@ -391,9 +398,9 @@ static void create_aux_windows(_THIS)
XFree(hints);
X11_SetCaptionNoLock(this, this->wm_title, this->wm_icon);
XSelectInput(SDL_Display, WMwindow,
FocusChangeMask | KeyPressMask | KeyReleaseMask
| PropertyChangeMask | StructureNotifyMask | KeymapStateMask);
app_event_mask = FocusChangeMask | KeyPressMask | KeyReleaseMask
| PropertyChangeMask | StructureNotifyMask | KeymapStateMask;
XSelectInput(SDL_Display, WMwindow, app_event_mask);
/* Set the class hints so we can get an icon (AfterStep) */
get_classname(classname, sizeof(classname));
......@@ -409,15 +416,107 @@ static void create_aux_windows(_THIS)
}
/* Setup the communication with the IM server */
SDL_IM = NULL;
SDL_IC = NULL;
/* create_aux_windows may be called several times against the same
Display. We should reuse the SDL_IM if one has been opened for
the Display, so we should not simply reset SDL_IM here. */
#ifdef X_HAVE_UTF8_STRING
if (SDL_X11_HAVE_UTF8) {
/* Discard obsolete resources if any. */
if (SDL_IM != NULL && SDL_Display != XDisplayOfIM(SDL_IM)) {
/* Just a double check. I don't think this
code is ever executed. */
SDL_SetError("display has changed while an IM is kept");
if (SDL_IC) {
XUnsetICFocus(SDL_IC);
XDestroyIC(SDL_IC);
SDL_IC = NULL;
}
XCloseIM(SDL_IM);
SDL_IM = NULL;
}
/* Open an input method. */
if (SDL_IM == NULL) {
char *old_locale, *old_modifiers;
/* I'm not comfortable to do locale setup
here. However, we need C library locale
(and xlib modifiers) to be set based on the
user's preference to use XIM, and many
existing game programs doesn't take care of
users' locale preferences, so someone other
than the game program should do it.
Moreover, ones say that some game programs
heavily rely on the C locale behaviour,
e.g., strcol()'s, and we can't change the C
library locale. Given the situation, I
couldn't find better place to do the
job... */
/* Save the current (application program's)
locale settings. */
old_locale = setlocale(LC_ALL, NULL);
old_modifiers = XSetLocaleModifiers(NULL);
if (old_locale == NULL || old_modifiers == NULL) {
/* The specs guarantee that the query
calls to above functions never
fail, so we should never come
here. */
SDL_SetError("failed to retreive current locale settings");
old_locale = NULL;
old_modifiers = NULL;
} else {
/* Save retreived values in our own
storage, since they may be
overwritten by the successive calls
to
setlocale/XSetLocaleModifiers. */
char const *p;
p = old_locale;
old_locale = SDL_malloc(strlen(p) + 1);
strcpy(old_locale, p);
p = old_modifiers;
old_modifiers = SDL_malloc(strlen(p) + 1);
strcpy(old_modifiers, p);
}
/* Fetch the user's preferences and open the
input method with them. */
setlocale(LC_ALL, "");
XSetLocaleModifiers("");
SDL_IM = XOpenIM(SDL_Display, NULL, classname, classname);
/* Restore the application's locale settings
so that we don't break the application's
expected behaviour. */
if (old_locale != NULL && old_modifiers != NULL) {
/* We need to restore the C library
locale first, since the
interpretation of the X modifier
may depend on it. */
setlocale(LC_ALL, old_locale);
SDL_free(old_locale);
XSetLocaleModifiers(old_modifiers);
SDL_free(old_modifiers);
}
}
/* Create a new input context for the new window just created. */
SDL_IM = XOpenIM(SDL_Display, NULL, classname, classname);
if (SDL_IM == NULL) {
SDL_SetError("no input method could be opened");
} else {
if (SDL_IC != NULL) {
/* Discard the old IC before creating new one. */
XUnsetICFocus(SDL_IC);
XDestroyIC(SDL_IC);
}
/* Theoretically we should check the current IM supports
PreeditNothing+StatusNothing style (i.e., root window method)
before creating the IC. However, it is the bottom line method,
and we supports any other options. If the IM didn't support
root window method, the following call fails, and SDL falls
back to pre-XIM keyboard handling. */
SDL_IC = pXCreateIC(SDL_IM,
XNClientWindow, WMwindow,
XNFocusWindow, WMwindow,
......@@ -430,6 +529,19 @@ static void create_aux_windows(_THIS)
SDL_SetError("no input context could be created");
XCloseIM(SDL_IM);
SDL_IM = NULL;
} else {
/* We need to receive X events that an IM wants and to pass
them to the IM through XFilterEvent. The set of events may
vary depending on the IM implementation and the options
specified through various routes. Although unlikely, the
xlib specification allows IM to change the event requirement
with its own circumstances, it is safe to call SelectInput
whenever we re-create an IC. */
unsigned long mask = 0;
char *ret = pXGetICValues(SDL_IC, XNFilterEvents, &mask, NULL);
&ic_event_mask, NULL);
XSelectInput(SDL_Display, WMwindow, app_event_mask | mask);
XSetICFocus(SDL_IC);
}
}
}
......@@ -1350,6 +1462,7 @@ void X11_VideoQuit(_THIS)
/* Close the connection with the IM server */
#ifdef X_HAVE_UTF8_STRING
if (SDL_IC != NULL) {
XUnsetICFocus(SDL_IC);
XDestroyIC(SDL_IC);
SDL_IC = NULL;
}
......
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