From 82284b86ebde945951f35bbb20bfe582f2d7322b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Tue, 11 Jul 2017 23:00:17 +0200 Subject: [PATCH] Cleanup Thanks to glfwGetKeyScancode we can now pass only a scancode to the platform layer for glfwGetKeyName. --- src/cocoa_window.m | 8 +------- src/input.c | 22 ++++++++++++++-------- src/internal.h | 6 +----- src/mir_window.c | 2 +- src/null_window.c | 2 +- src/win32_window.c | 8 +------- src/wl_window.c | 2 +- src/x11_window.c | 8 +------- 8 files changed, 21 insertions(+), 37 deletions(-) diff --git a/src/cocoa_window.m b/src/cocoa_window.m index 2c94a8a2..2acfa98b 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -1560,14 +1560,8 @@ void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode) updateCursorImage(window); } -const char* _glfwPlatformGetKeyName(int key, int scancode) +const char* _glfwPlatformGetScancodeName(int scancode) { - if (key != GLFW_KEY_UNKNOWN) - scancode = _glfw.ns.scancodes[key]; - - if (!_glfwIsPrintable(_glfw.ns.keycodes[scancode])) - return NULL; - UInt32 deadKeyState = 0; UniChar characters[8]; UniCharCount characterCount = 0; diff --git a/src/input.c b/src/input.c index 523bfa98..f4542b4e 100644 --- a/src/input.c +++ b/src/input.c @@ -289,13 +289,6 @@ void _glfwInputJoystickHat(_GLFWjoystick* js, int hat, char value) ////// GLFW internal API ////// ////////////////////////////////////////////////////////////////////////// -GLFWbool _glfwIsPrintable(int key) -{ - return (key >= GLFW_KEY_APOSTROPHE && key <= GLFW_KEY_WORLD_2) || - (key >= GLFW_KEY_KP_0 && key <= GLFW_KEY_KP_ADD) || - key == GLFW_KEY_KP_EQUAL; -} - _GLFWjoystick* _glfwAllocJoystick(const char* name, const char* guid, int axisCount, @@ -450,7 +443,20 @@ GLFWAPI void glfwSetInputMode(GLFWwindow* handle, int mode, int value) GLFWAPI const char* glfwGetKeyName(int key, int scancode) { _GLFW_REQUIRE_INIT_OR_RETURN(NULL); - return _glfwPlatformGetKeyName(key, scancode); + + if (key != GLFW_KEY_UNKNOWN) + { + if (key != GLFW_KEY_KP_EQUAL && + (key < GLFW_KEY_KP_0 || key > GLFW_KEY_KP_ADD) && + (key < GLFW_KEY_APOSTROPHE || key > GLFW_KEY_WORLD_2)) + { + return NULL; + } + + scancode = _glfwPlatformGetKeyScancode(key); + } + + return _glfwPlatformGetScancodeName(scancode); } GLFWAPI int glfwGetKeyScancode(int key) diff --git a/src/internal.h b/src/internal.h index d00c7fc0..e3ea78de 100644 --- a/src/internal.h +++ b/src/internal.h @@ -630,7 +630,7 @@ int _glfwPlatformCreateStandardCursor(_GLFWcursor* cursor, int shape); void _glfwPlatformDestroyCursor(_GLFWcursor* cursor); void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor); -const char* _glfwPlatformGetKeyName(int key, int scancode); +const char* _glfwPlatformGetScancodeName(int scancode); int _glfwPlatformGetKeyScancode(int key); void _glfwPlatformGetMonitorPos(_GLFWmonitor* monitor, int* xpos, int* ypos); @@ -990,10 +990,6 @@ _GLFWjoystick* _glfwAllocJoystick(const char* name, */ void _glfwFreeJoystick(_GLFWjoystick* js); -/*! @ingroup utility - */ -GLFWbool _glfwIsPrintable(int key); - /*! @ingroup utility */ GLFWbool _glfwInitVulkan(int mode); diff --git a/src/mir_window.c b/src/mir_window.c index 566f571c..3e8d5c5b 100644 --- a/src/mir_window.c +++ b/src/mir_window.c @@ -830,7 +830,7 @@ void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode) } } -const char* _glfwPlatformGetKeyName(int key, int scancode) +const char* _glfwPlatformGetScancodeName(int scancode) { _glfwInputError(GLFW_PLATFORM_ERROR, "Mir: Unsupported function %s", __PRETTY_FUNCTION__); diff --git a/src/null_window.c b/src/null_window.c index 137f80c8..2e627672 100644 --- a/src/null_window.c +++ b/src/null_window.c @@ -261,7 +261,7 @@ const char* _glfwPlatformGetClipboardString(_GLFWwindow* window) return NULL; } -const char* _glfwPlatformGetKeyName(int key, int scancode) +const char* _glfwPlatformGetScancodeName(int scancode) { return ""; } diff --git a/src/win32_window.c b/src/win32_window.c index 93abd68d..ef05898a 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -1609,16 +1609,10 @@ void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode) updateCursorImage(window); } -const char* _glfwPlatformGetKeyName(int key, int scancode) +const char* _glfwPlatformGetScancodeName(int scancode) { WCHAR name[16]; - if (key != GLFW_KEY_UNKNOWN) - scancode = _glfw.win32.scancodes[key]; - - if (!_glfwIsPrintable(_glfw.win32.keycodes[scancode])) - return NULL; - if (!GetKeyNameTextW(scancode << 16, name, sizeof(name) / sizeof(WCHAR))) return NULL; diff --git a/src/wl_window.c b/src/wl_window.c index e9c817d5..fbd58ffb 100644 --- a/src/wl_window.c +++ b/src/wl_window.c @@ -721,7 +721,7 @@ void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode) _glfwPlatformSetCursor(window, window->wl.currentCursor); } -const char* _glfwPlatformGetKeyName(int key, int scancode) +const char* _glfwPlatformGetScancodeName(int scancode) { // TODO return NULL; diff --git a/src/x11_window.c b/src/x11_window.c index ed5a0f29..dcda51ea 100644 --- a/src/x11_window.c +++ b/src/x11_window.c @@ -2474,17 +2474,11 @@ void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode) XFlush(_glfw.x11.display); } -const char* _glfwPlatformGetKeyName(int key, int scancode) +const char* _glfwPlatformGetScancodeName(int scancode) { if (!_glfw.x11.xkb.available) return NULL; - if (key != GLFW_KEY_UNKNOWN) - scancode = _glfw.x11.scancodes[key]; - - if (!_glfwIsPrintable(_glfw.x11.keycodes[scancode])) - return NULL; - const KeySym keysym = XkbKeycodeToKeysym(_glfw.x11.display, scancode, 0, 0); if (keysym == NoSymbol) return NULL;