From 56ca0cb3b30ecb1310752360e9c875fc1b2bab5e 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. --- 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 72f93692..e8c9b154 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -4190,9 +4190,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 97679732..f8bfe3e6 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 1674795c..49635c95 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 dbbe9d1f..33145e63 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 1398cb70..d8355ae8 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)