From 79f4ec6822742e587a7c295aff2bee297edc12ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Thu, 22 Aug 2019 21:31:46 +0200 Subject: [PATCH] Unify key name string handling This makes key names per-key static strings for all supported platforms. Fixes #1200. (cherry picked from commit 56ca0cb3b30ecb1310752360e9c875fc1b2bab5e) --- include/GLFW/glfw3.h | 6 ++++-- src/cocoa_platform.h | 2 +- src/cocoa_window.m | 10 ++++++---- src/x11_platform.h | 2 +- src/x11_window.c | 7 ++++--- 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index f631eb30..343ed188 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -4193,9 +4193,11 @@ GLFWAPI int glfwRawMouseMotionSupported(void); * @errors Possible errors include @ref GLFW_NOT_INITIALIZED and @ref * GLFW_PLATFORM_ERROR. * + * @remark The contents of the returned string may change when a keyboard + * layout change event is received. + * * @pointer_lifetime The returned string is allocated and freed by GLFW. You - * should not free it yourself. It is valid until the next call to @ref - * glfwGetKeyName, or until the library is terminated. + * should not free it yourself. It is valid until the library is terminated. * * @thread_safety This function must only be called from the main thread. * diff --git a/src/cocoa_platform.h b/src/cocoa_platform.h index 2847f36b..43639a34 100644 --- a/src/cocoa_platform.h +++ b/src/cocoa_platform.h @@ -139,7 +139,7 @@ typedef struct _GLFWlibraryNS id keyUpMonitor; id nibObjects; - char keyName[64]; + char keynames[GLFW_KEY_LAST + 1][17]; short int keycodes[256]; short int scancodes[GLFW_KEY_LAST + 1]; char* clipboardString; diff --git a/src/cocoa_window.m b/src/cocoa_window.m index f9383b3e..5f7e0c3e 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -1503,8 +1503,10 @@ const char* _glfwPlatformGetScancodeName(int scancode) { @autoreleasepool { + const int key = _glfw.ns.keycodes[scancode]; + UInt32 deadKeyState = 0; - UniChar characters[8]; + UniChar characters[4]; UniCharCount characterCount = 0; if (UCKeyTranslate([(NSData*) _glfw.ns.unicodeData bytes], @@ -1529,12 +1531,12 @@ const char* _glfwPlatformGetScancodeName(int scancode) characterCount, kCFAllocatorNull); CFStringGetCString(string, - _glfw.ns.keyName, - sizeof(_glfw.ns.keyName), + _glfw.ns.keynames[key], + sizeof(_glfw.ns.keynames[key]), kCFStringEncodingUTF8); CFRelease(string); - return _glfw.ns.keyName; + return _glfw.ns.keynames[key]; } // autoreleasepool } diff --git a/src/x11_platform.h b/src/x11_platform.h index 5b365241..d34bc587 100644 --- a/src/x11_platform.h +++ b/src/x11_platform.h @@ -228,7 +228,7 @@ typedef struct _GLFWlibraryX11 // Clipboard string (while the selection is owned) char* clipboardString; // Key name string - char keyName[5]; + char keynames[GLFW_KEY_LAST + 1][5]; // X11 keycode to GLFW key LUT short int keycodes[256]; // GLFW key to X11 keycode LUT diff --git a/src/x11_window.c b/src/x11_window.c index 7fd09b54..0ddbc0d4 100644 --- a/src/x11_window.c +++ b/src/x11_window.c @@ -2792,6 +2792,7 @@ const char* _glfwPlatformGetScancodeName(int scancode) if (!_glfw.x11.xkb.available) return NULL; + const int key = _glfw.x11.keycodes[scancode]; const KeySym keysym = XkbKeycodeToKeysym(_glfw.x11.display, scancode, _glfw.x11.xkb.group, 0); if (keysym == NoSymbol) @@ -2801,12 +2802,12 @@ const char* _glfwPlatformGetScancodeName(int scancode) if (ch == -1) return NULL; - const size_t count = encodeUTF8(_glfw.x11.keyName, (unsigned int) ch); + const size_t count = encodeUTF8(_glfw.x11.keynames[key], (unsigned int) ch); if (count == 0) return NULL; - _glfw.x11.keyName[count] = '\0'; - return _glfw.x11.keyName; + _glfw.x11.keynames[key][count] = '\0'; + return _glfw.x11.keynames[key]; } int _glfwPlatformGetKeyScancode(int key)