Unify key name string handling

This makes key names per-key static strings for all supported platforms.

Fixes #1200.

(cherry picked from commit 56ca0cb3b3)
This commit is contained in:
Camilla Löwy 2019-08-22 21:31:46 +02:00
parent 2032a8f0dc
commit 79f4ec6822
5 changed files with 16 additions and 11 deletions

View File

@ -4193,9 +4193,11 @@ GLFWAPI int glfwRawMouseMotionSupported(void);
* @errors Possible errors include @ref GLFW_NOT_INITIALIZED and @ref * @errors Possible errors include @ref GLFW_NOT_INITIALIZED and @ref
* GLFW_PLATFORM_ERROR. * 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 * @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 * should not free it yourself. It is valid until the library is terminated.
* glfwGetKeyName, or until the library is terminated.
* *
* @thread_safety This function must only be called from the main thread. * @thread_safety This function must only be called from the main thread.
* *

View File

@ -139,7 +139,7 @@ typedef struct _GLFWlibraryNS
id keyUpMonitor; id keyUpMonitor;
id nibObjects; id nibObjects;
char keyName[64]; char keynames[GLFW_KEY_LAST + 1][17];
short int keycodes[256]; short int keycodes[256];
short int scancodes[GLFW_KEY_LAST + 1]; short int scancodes[GLFW_KEY_LAST + 1];
char* clipboardString; char* clipboardString;

View File

@ -1503,8 +1503,10 @@ const char* _glfwPlatformGetScancodeName(int scancode)
{ {
@autoreleasepool { @autoreleasepool {
const int key = _glfw.ns.keycodes[scancode];
UInt32 deadKeyState = 0; UInt32 deadKeyState = 0;
UniChar characters[8]; UniChar characters[4];
UniCharCount characterCount = 0; UniCharCount characterCount = 0;
if (UCKeyTranslate([(NSData*) _glfw.ns.unicodeData bytes], if (UCKeyTranslate([(NSData*) _glfw.ns.unicodeData bytes],
@ -1529,12 +1531,12 @@ const char* _glfwPlatformGetScancodeName(int scancode)
characterCount, characterCount,
kCFAllocatorNull); kCFAllocatorNull);
CFStringGetCString(string, CFStringGetCString(string,
_glfw.ns.keyName, _glfw.ns.keynames[key],
sizeof(_glfw.ns.keyName), sizeof(_glfw.ns.keynames[key]),
kCFStringEncodingUTF8); kCFStringEncodingUTF8);
CFRelease(string); CFRelease(string);
return _glfw.ns.keyName; return _glfw.ns.keynames[key];
} // autoreleasepool } // autoreleasepool
} }

View File

@ -228,7 +228,7 @@ typedef struct _GLFWlibraryX11
// Clipboard string (while the selection is owned) // Clipboard string (while the selection is owned)
char* clipboardString; char* clipboardString;
// Key name string // Key name string
char keyName[5]; char keynames[GLFW_KEY_LAST + 1][5];
// X11 keycode to GLFW key LUT // X11 keycode to GLFW key LUT
short int keycodes[256]; short int keycodes[256];
// GLFW key to X11 keycode LUT // GLFW key to X11 keycode LUT

View File

@ -2792,6 +2792,7 @@ const char* _glfwPlatformGetScancodeName(int scancode)
if (!_glfw.x11.xkb.available) if (!_glfw.x11.xkb.available)
return NULL; return NULL;
const int key = _glfw.x11.keycodes[scancode];
const KeySym keysym = XkbKeycodeToKeysym(_glfw.x11.display, const KeySym keysym = XkbKeycodeToKeysym(_glfw.x11.display,
scancode, _glfw.x11.xkb.group, 0); scancode, _glfw.x11.xkb.group, 0);
if (keysym == NoSymbol) if (keysym == NoSymbol)
@ -2801,12 +2802,12 @@ const char* _glfwPlatformGetScancodeName(int scancode)
if (ch == -1) if (ch == -1)
return NULL; 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) if (count == 0)
return NULL; return NULL;
_glfw.x11.keyName[count] = '\0'; _glfw.x11.keynames[key][count] = '\0';
return _glfw.x11.keyName; return _glfw.x11.keynames[key];
} }
int _glfwPlatformGetKeyScancode(int key) int _glfwPlatformGetKeyScancode(int key)