X11: Fix X keycode ranges for XKB and core

This replaces the hardcoded keycode ranges and various kludgy range
checks with the actual ranges reported by Xlib and XKB.
This commit is contained in:
Camilla Löwy 2020-03-17 00:04:48 +01:00
parent 215a05af3d
commit ee45b58647
2 changed files with 14 additions and 9 deletions

View File

@ -43,10 +43,6 @@ static int translateKeyCode(int scancode)
{ {
int keySym; int keySym;
// Valid key code range is [8,255], according to the Xlib manual
if (scancode < 8 || scancode > 255)
return GLFW_KEY_UNKNOWN;
{ {
int dummy; int dummy;
KeySym* keySyms; KeySym* keySyms;
@ -200,7 +196,7 @@ static int translateKeyCode(int scancode)
// //
static void createKeyTables(void) static void createKeyTables(void)
{ {
int scancode, key; int scancode, key, scancodeMin, scancodeMax;
memset(_glfw.x11.keycodes, -1, sizeof(_glfw.x11.keycodes)); memset(_glfw.x11.keycodes, -1, sizeof(_glfw.x11.keycodes));
memset(_glfw.x11.scancodes, -1, sizeof(_glfw.x11.scancodes)); memset(_glfw.x11.scancodes, -1, sizeof(_glfw.x11.scancodes));
@ -214,8 +210,11 @@ static void createKeyTables(void)
XkbDescPtr desc = XkbGetMap(_glfw.x11.display, 0, XkbUseCoreKbd); XkbDescPtr desc = XkbGetMap(_glfw.x11.display, 0, XkbUseCoreKbd);
XkbGetNames(_glfw.x11.display, XkbKeyNamesMask, desc); XkbGetNames(_glfw.x11.display, XkbKeyNamesMask, desc);
scancodeMin = desc->min_key_code;
scancodeMax = desc->max_key_code;
// Find the X11 key code -> GLFW key code mapping // Find the X11 key code -> GLFW key code mapping
for (scancode = desc->min_key_code; scancode <= desc->max_key_code; scancode++) for (scancode = scancodeMin; scancode <= scancodeMax; scancode++)
{ {
memcpy(name, desc->names->keys[scancode].name, XkbKeyNameLength); memcpy(name, desc->names->keys[scancode].name, XkbKeyNameLength);
name[XkbKeyNameLength] = '\0'; name[XkbKeyNameLength] = '\0';
@ -345,15 +344,16 @@ static void createKeyTables(void)
else if (strcmp(name, "COMP") == 0) key = GLFW_KEY_MENU; else if (strcmp(name, "COMP") == 0) key = GLFW_KEY_MENU;
else key = GLFW_KEY_UNKNOWN; else key = GLFW_KEY_UNKNOWN;
if ((scancode >= 0) && (scancode < 256)) _glfw.x11.keycodes[scancode] = key;
_glfw.x11.keycodes[scancode] = key;
} }
XkbFreeNames(desc, XkbKeyNamesMask, True); XkbFreeNames(desc, XkbKeyNamesMask, True);
XkbFreeKeyboard(desc, 0, True); XkbFreeKeyboard(desc, 0, True);
} }
else
XDisplayKeycodes(_glfw.x11.display, &scancodeMin, &scancodeMax);
for (scancode = 0; scancode < 256; scancode++) for (scancode = scancodeMin; scancode <= scancodeMax; scancode++)
{ {
// Translate the un-translated key codes using traditional X11 KeySym // Translate the un-translated key codes using traditional X11 KeySym
// lookups // lookups
@ -1069,6 +1069,8 @@ int _glfwPlatformInit(void)
_glfw_dlsym(_glfw.x11.xlib.handle, "XDestroyIC"); _glfw_dlsym(_glfw.x11.xlib.handle, "XDestroyIC");
_glfw.x11.xlib.DestroyWindow = (PFN_XDestroyWindow) _glfw.x11.xlib.DestroyWindow = (PFN_XDestroyWindow)
_glfw_dlsym(_glfw.x11.xlib.handle, "XDestroyWindow"); _glfw_dlsym(_glfw.x11.xlib.handle, "XDestroyWindow");
_glfw.x11.xlib.DisplayKeycodes = (PFN_XDisplayKeycodes)
_glfw_dlsym(_glfw.x11.xlib.handle, "XDisplayKeycodes");
_glfw.x11.xlib.EventsQueued = (PFN_XEventsQueued) _glfw.x11.xlib.EventsQueued = (PFN_XEventsQueued)
_glfw_dlsym(_glfw.x11.xlib.handle, "XEventsQueued"); _glfw_dlsym(_glfw.x11.xlib.handle, "XEventsQueued");
_glfw.x11.xlib.FilterEvent = (PFN_XFilterEvent) _glfw.x11.xlib.FilterEvent = (PFN_XFilterEvent)

View File

@ -67,6 +67,7 @@ typedef int (* PFN_XDeleteContext)(Display*,XID,XContext);
typedef int (* PFN_XDeleteProperty)(Display*,Window,Atom); typedef int (* PFN_XDeleteProperty)(Display*,Window,Atom);
typedef void (* PFN_XDestroyIC)(XIC); typedef void (* PFN_XDestroyIC)(XIC);
typedef int (* PFN_XDestroyWindow)(Display*,Window); typedef int (* PFN_XDestroyWindow)(Display*,Window);
typedef int (* PFN_XDisplayKeycodes)(Display*,int*,int*);
typedef int (* PFN_XEventsQueued)(Display*,int); typedef int (* PFN_XEventsQueued)(Display*,int);
typedef Bool (* PFN_XFilterEvent)(XEvent*,Window); typedef Bool (* PFN_XFilterEvent)(XEvent*,Window);
typedef int (* PFN_XFindContext)(Display*,XID,XContext,XPointer*); typedef int (* PFN_XFindContext)(Display*,XID,XContext,XPointer*);
@ -166,6 +167,7 @@ typedef void (* PFN_Xutf8SetWMProperties)(Display*,Window,const char*,const char
#define XDeleteProperty _glfw.x11.xlib.DeleteProperty #define XDeleteProperty _glfw.x11.xlib.DeleteProperty
#define XDestroyIC _glfw.x11.xlib.DestroyIC #define XDestroyIC _glfw.x11.xlib.DestroyIC
#define XDestroyWindow _glfw.x11.xlib.DestroyWindow #define XDestroyWindow _glfw.x11.xlib.DestroyWindow
#define XDisplayKeycodes _glfw.x11.xlib.DisplayKeycodes
#define XEventsQueued _glfw.x11.xlib.EventsQueued #define XEventsQueued _glfw.x11.xlib.EventsQueued
#define XFilterEvent _glfw.x11.xlib.FilterEvent #define XFilterEvent _glfw.x11.xlib.FilterEvent
#define XFindContext _glfw.x11.xlib.FindContext #define XFindContext _glfw.x11.xlib.FindContext
@ -522,6 +524,7 @@ typedef struct _GLFWlibraryX11
PFN_XDeleteProperty DeleteProperty; PFN_XDeleteProperty DeleteProperty;
PFN_XDestroyIC DestroyIC; PFN_XDestroyIC DestroyIC;
PFN_XDestroyWindow DestroyWindow; PFN_XDestroyWindow DestroyWindow;
PFN_XDisplayKeycodes DisplayKeycodes;
PFN_XEventsQueued EventsQueued; PFN_XEventsQueued EventsQueued;
PFN_XFilterEvent FilterEvent; PFN_XFilterEvent FilterEvent;
PFN_XFindContext FindContext; PFN_XFindContext FindContext;