This commit is contained in:
Andrew Corrigan 2014-05-18 10:03:33 +00:00
commit f3f8d9ddc6
4 changed files with 157 additions and 124 deletions

View File

@ -261,10 +261,11 @@ if (_GLFW_X11)
# Check for Xkb (X keyboard extension)
if (NOT X11_Xkb_FOUND)
message(FATAL_ERROR "The X keyboard extension headers were not found")
endif()
message("The X keyboard extension headers were not found")
else()
list(APPEND glfw_INCLUDE_DIR ${X11_Xkb_INCLUDE_PATH})
set(_GLFW_HAS_XKB 1)
endif()
find_library(RT_LIBRARY rt)
mark_as_advanced(RT_LIBRARY)

View File

@ -70,6 +70,8 @@
#cmakedefine _GLFW_HAS_GLXGETPROCADDRESSEXT
// Define this to 1 if dlopen is available
#cmakedefine _GLFW_HAS_DLOPEN
// Define this to 1 if Xkb is available
#cmakedefine _GLFW_HAS_XKB
// Define this to 1 if glfwInit should change the current directory
#cmakedefine _GLFW_USE_CHDIR

View File

@ -40,11 +40,16 @@
static int translateKey(int keyCode)
{
int keySym;
int keysyms_per_keycode_return;
KeySym *keysyms;
// Valid key code range is [8,255], according to the XLib manual
if (keyCode < 8 || keyCode > 255)
return GLFW_KEY_UNKNOWN;
#if defined(_GLFW_HAS_XKB)
if(_glfw.x11.xkb.available)
{
// Try secondary keysym, for numeric keypad keys
// Note: This way we always force "NumLock = ON", which is intentional
// since the returned key code should correspond to a physical
@ -73,6 +78,19 @@ static int translateKey(int keyCode)
// should not be layout dependent (i.e. US layout and international
// layouts should give the same result).
keySym = XkbKeycodeToKeysym(_glfw.x11.display, keyCode, 0, 0);
}
else
#endif
{
keysyms =
XGetKeyboardMapping(_glfw.x11.display,
keyCode,
1,
&keysyms_per_keycode_return);
keySym = keysyms[0];
XFree(keysyms);
}
switch (keySym)
{
case XK_Escape: return GLFW_KEY_ESCAPE;
@ -217,14 +235,20 @@ static int translateKey(int keyCode)
//
static void updateKeyCodeLUT(void)
{
int i, keyCode, keyCodeGLFW;
int keyCode;
#if defined(_GLFW_HAS_XKB)
int keyCodeGLFW, i;
char name[XkbKeyNameLength + 1];
XkbDescPtr descr;
#endif
// Clear the LUT
for (keyCode = 0; keyCode < 256; keyCode++)
_glfw.x11.keyCodeLUT[keyCode] = GLFW_KEY_UNKNOWN;
#if defined(_GLFW_HAS_XKB)
if(_glfw.x11.xkb.available)
{
// Use XKB to determine physical key locations independently of the current
// keyboard layout
@ -303,6 +327,8 @@ static void updateKeyCodeLUT(void)
// Free the keyboard description
XkbFreeKeyboard(descr, 0, True);
}
#endif
// Translate the un-translated key codes using traditional X11 KeySym
// lookups
@ -431,7 +457,9 @@ static void detectEWMH(void)
//
static GLboolean initExtensions(void)
{
#if defined(_GLFW_HAS_XKB) // suppress unused variable warning
Bool supported;
#endif
// Find or create window manager atoms
_glfw.x11.WM_PROTOCOLS = XInternAtom(_glfw.x11.display,
@ -494,33 +522,32 @@ static GLboolean initExtensions(void)
}
// Check if Xkb is supported on this display
#if defined(_GLFW_HAS_XKB)
_glfw.x11.xkb.versionMajor = 1;
_glfw.x11.xkb.versionMinor = 0;
if (!XkbQueryExtension(_glfw.x11.display,
_glfw.x11.xkb.available =
XkbQueryExtension(_glfw.x11.display,
&_glfw.x11.xkb.majorOpcode,
&_glfw.x11.xkb.eventBase,
&_glfw.x11.xkb.errorBase,
&_glfw.x11.xkb.versionMajor,
&_glfw.x11.xkb.versionMinor))
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"X11: The keyboard extension is not available");
return GL_FALSE;
}
&_glfw.x11.xkb.versionMinor);
if (_glfw.x11.xkb.available)
{
if (!XkbSetDetectableAutoRepeat(_glfw.x11.display, True, &supported))
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"X11: Failed to set detectable key repeat");
return GL_FALSE;
// X11: Failed to set detectable key repeat
_glfw.x11.xkb.available = GL_FALSE;
}
if (!supported)
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"X11: Detectable key repeat is not supported");
return GL_FALSE;
// X11: Detectable key repeat is not supported
_glfw.x11.xkb.available = GL_FALSE;
}
}
#endif
// Update the key code LUT
// FIXME: We should listen to XkbMapNotify events to track changes to

View File

@ -46,7 +46,9 @@
#include <X11/extensions/XInput2.h>
// The Xkb extension provides improved keyboard support
#include <X11/XKBlib.h>
#if defined(_GLFW_HAS_XKB)
#include <X11/XKBlib.h>
#endif
#include "posix_tls.h"
@ -175,6 +177,7 @@ typedef struct _GLFWlibraryX11
} randr;
struct {
GLboolean available;
int majorOpcode;
int eventBase;
int errorBase;