Fixed dead keys in X11. The library will now try to create an X input context to handle dead keys properly on international keyboards.

This makes it possible to enter for example an e with accent grave on a German keyboard without further efforts.
A fallback mechanism is provided in case the client does not support X input method / context creation.
In that case, the library will behave as it did before.
This commit is contained in:
Lucas Hinderberger 2013-10-06 21:19:38 +02:00
parent 161065ac19
commit 2da8742e24
3 changed files with 117 additions and 9 deletions

View File

@ -420,6 +420,8 @@ static void detectEWMH(void)
static GLboolean initExtensions(void)
{
Bool supported;
unsigned int u;
XIMStyles * styles = NULL;
// Find or create window manager atoms
_glfw.x11.WM_STATE = XInternAtom(_glfw.x11.display, "WM_STATE", False);
@ -537,6 +539,44 @@ static GLboolean initExtensions(void)
_glfw.x11.SAVE_TARGETS =
XInternAtom(_glfw.x11.display, "SAVE_TARGETS", False);
// ------------------------------------------------------------------------
// Optional extensions (function returns always true from here!)
// Open input method
if (!XSupportsLocale())
return GL_TRUE;
XSetLocaleModifiers("");
_glfw.x11.im = XOpenIM(_glfw.x11.display, 0, 0, 0);
if (!_glfw.x11.im)
return GL_TRUE;
// Get available input styles
if (XGetIMValues(_glfw.x11.im, XNQueryInputStyle, &styles, NULL) || !styles)
{
XCloseIM(_glfw.x11.im);
_glfw.x11.im = NULL;
return GL_TRUE;
}
// Search for needed input style
for (u = 0; u < styles->count_styles; u++)
{
if (styles->supported_styles[u] == (XIMPreeditNothing | XIMStatusNothing))
break;
}
if (u >= styles->count_styles)
{
XFree(styles);
XCloseIM(_glfw.x11.im);
_glfw.x11.im = NULL;
return GL_TRUE;
}
XFree(styles);
return GL_TRUE;
}
@ -637,6 +677,8 @@ int _glfwPlatformInit(void)
{
XInitThreads();
_glfw.x11.im = NULL;
_glfw.x11.display = XOpenDisplay(NULL);
if (!_glfw.x11.display)
{
@ -673,6 +715,12 @@ void _glfwPlatformTerminate(void)
free(_glfw.x11.selection.string);
if (_glfw.x11.im)
{
XCloseIM(_glfw.x11.im);
_glfw.x11.im = NULL;
}
_glfwTerminateJoysticks();
_glfwTerminateContextAPI();
terminateDisplay();

View File

@ -92,6 +92,8 @@ typedef struct _GLFWwindowX11
// The last position the cursor was warped to by GLFW
int warpPosX, warpPosY;
// The window's input context
XIC ic;
} _GLFWwindowX11;
@ -200,6 +202,9 @@ typedef struct _GLFWlibraryX11
char* name;
} joystick[GLFW_JOYSTICK_LAST + 1];
// Input method and context
XIM im;
} _GLFWlibraryX11;

View File

@ -86,15 +86,54 @@ static int translateKey(int keycode)
// Translates an X Window event to Unicode
//
static int translateChar(XKeyEvent* event)
static wchar_t * translateChar(XEvent * event, _GLFWwindow * window, int * count)
{
KeySym keysym;
static wchar_t buffer[16];
// Get X11 keysym
XLookupString(event, NULL, 0, &keysym, NULL);
// If there is no input method / context available, use the old fallback
// mechanism
if (!window || !window->x11.ic)
{
long uc;
// Convert to Unicode (see x11_unicode.c)
return (int) _glfwKeySym2Unicode(keysym);
// Get X11 keysym
XLookupString(&event->xkey, NULL, 0, &keysym, NULL);
// Convert to Unicode (see x11_unicode.c)
uc = _glfwKeySym2Unicode(keysym);
if (uc < 0 || uc > 0xFFFF)
{
*count = 0;
return NULL;
}
buffer[0] = (unsigned int)uc;
*count = 1;
}
// Else lookup the wide char string with respect to dead characters
else
{
Status dummy;
// Check if the given event is a dead char. In that case, it does not
// produce a unicode char.
if (XFilterEvent(event, None))
{
*count = 0;
return NULL;
}
// Retrieve unicode string
*count = XwcLookupString(window->x11.ic, &event->xkey, buffer, 16 * sizeof(wchar_t), 0, &dummy);
if (*count < 0)
{
*count = 0;
return NULL;
}
}
return buffer;
}
// Create the X11 window (and its colormap)
@ -106,6 +145,8 @@ static GLboolean createWindow(_GLFWwindow* window,
XSetWindowAttributes wa;
XVisualInfo* visual = _GLFW_X11_CONTEXT_VISUAL;
window->x11.ic = NULL;
// Every window needs a colormap
// Create one based on the visual used by the current context
// TODO: Decouple this from context creation
@ -301,6 +342,13 @@ static GLboolean createWindow(_GLFWwindow* window,
XRRSelectInput(_glfw.x11.display, window->x11.handle,
RRScreenChangeNotifyMask);
// Try to create an input context. If this function returns NULL, ic is
// set to NULL and we know we have to use fallback mechanisms to parse
// char events.
window->x11.ic = XCreateIC(_glfw.x11.im, XNInputStyle,
XIMPreeditNothing | XIMStatusNothing, XNClientWindow,
window->x11.handle, XNFocusWindow, window->x11.handle, NULL);
_glfwPlatformGetWindowPos(window, &window->x11.xpos, &window->x11.ypos);
_glfwPlatformGetWindowSize(window, &window->x11.width, &window->x11.height);
@ -524,14 +572,15 @@ static void processEvent(XEvent *event)
{
case KeyPress:
{
int i, n_chars;
const int key = translateKey(event->xkey.keycode);
const int mods = translateState(event->xkey.state);
const int character = translateChar(&event->xkey);
const wchar_t * characters = translateChar(event, window, &n_chars);
_glfwInputKey(window, key, event->xkey.keycode, GLFW_PRESS, mods);
if (character != -1)
_glfwInputChar(window, character);
for (i = 0; i < n_chars; i++)
_glfwInputChar(window, (unsigned int)characters[i]);
break;
}
@ -948,6 +997,12 @@ void _glfwPlatformDestroyWindow(_GLFWwindow* window)
if (window->monitor)
leaveFullscreenMode(window);
if (window->x11.ic)
{
XDestroyIC(window->x11.ic);
window->x11.ic = NULL;
}
_glfwDestroyContext(window);
if (window->x11.handle)