From abc1e3f289286f454dcbb1d4c16790c41c126dbb Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 14 Jan 2021 22:46:22 +0530 Subject: [PATCH] Remove the function used to init glfw key events Instead use C99 struct initializers. Much less error prone. --- glfw/cocoa_window.m | 9 +++------ glfw/ibus_glfw.c | 3 +-- glfw/input.c | 10 ---------- glfw/internal.h | 1 - glfw/window.c | 3 +-- glfw/xkb_glfw.c | 6 ++---- 6 files changed, 7 insertions(+), 25 deletions(-) diff --git a/glfw/cocoa_window.m b/glfw/cocoa_window.m index d5cad8adc..fc8b3187e 100644 --- a/glfw/cocoa_window.m +++ b/glfw/cocoa_window.m @@ -983,8 +983,7 @@ is_ascii_control_char(char x) { const bool process_text = !window->ns.textInputFilterCallback || window->ns.textInputFilterCallback(key, mods, keycode, flags) != 1; [self unmarkText]; _glfw.ns.text[0] = 0; - GLFWkeyevent glfw_keyevent; - _glfwInitializeKeyEvent(&glfw_keyevent, key, keycode, GLFW_PRESS, mods); + GLFWkeyevent glfw_keyevent = {.key = key, .native_key = keycode, .action = GLFW_PRESS, .mods = mods}; if (!_glfw.ns.unicodeData) { // Using the cocoa API for key handling is disabled, as there is no // reliable way to handle dead keys using it. Only use it if the @@ -1092,8 +1091,7 @@ is_ascii_control_char(char x) { action = GLFW_PRESS; } - GLFWkeyevent glfw_keyevent; - _glfwInitializeKeyEvent(&glfw_keyevent, key, [event keyCode], action, mods); + GLFWkeyevent glfw_keyevent = {.key = key, .native_key = [event keyCode], .action = action, .mods = mods}; _glfwInputKeyboard(window, &glfw_keyevent); } @@ -1102,8 +1100,7 @@ is_ascii_control_char(char x) { const int key = translateKey([event keyCode], true); const int mods = translateFlags([event modifierFlags]); - GLFWkeyevent glfw_keyevent; - _glfwInitializeKeyEvent(&glfw_keyevent, key, [event keyCode], GLFW_RELEASE, mods); + GLFWkeyevent glfw_keyevent = {.key = key, .native_key = [event keyCode], .action = GLFW_RELEASE, .mods = mods}; _glfwInputKeyboard(window, &glfw_keyevent); } diff --git a/glfw/ibus_glfw.c b/glfw/ibus_glfw.c index c615c3bf1..83b0f4ac8 100644 --- a/glfw/ibus_glfw.c +++ b/glfw/ibus_glfw.c @@ -110,8 +110,7 @@ static inline void send_text(const char *text, int ime_state) { _GLFWwindow *w = _glfwFocusedWindow(); if (w && w->callbacks.keyboard) { - GLFWkeyevent fake_ev; - _glfwInitializeKeyEvent(&fake_ev, 0, 0, GLFW_PRESS, 0); + GLFWkeyevent fake_ev = {.action = GLFW_PRESS}; fake_ev.text = text; fake_ev.ime_state = ime_state; w->callbacks.keyboard((GLFWwindow*) w, &fake_ev); diff --git a/glfw/input.c b/glfw/input.c index c64082916..4f5c930eb 100644 --- a/glfw/input.c +++ b/glfw/input.c @@ -273,16 +273,6 @@ static bool parseMapping(_GLFWmapping* mapping, const char* string) ////// GLFW event API ////// ////////////////////////////////////////////////////////////////////////// -void _glfwInitializeKeyEvent(GLFWkeyevent *ev, int key, int native_key, int action, int mods) -{ - ev->key = key; - ev->native_key = native_key; - ev->action = action; - ev->mods = mods; - ev->text = NULL; - ev->ime_state = 0; -} - static void set_key_action(_GLFWwindow *window, uint32_t key, int val, int idx) { const unsigned sz = arraysz(window->activated_keys); diff --git a/glfw/internal.h b/glfw/internal.h index c7b7685ba..58edb4ce9 100644 --- a/glfw/internal.h +++ b/glfw/internal.h @@ -776,7 +776,6 @@ void _glfwInputWindowDamage(_GLFWwindow* window); void _glfwInputWindowCloseRequest(_GLFWwindow* window); void _glfwInputWindowMonitor(_GLFWwindow* window, _GLFWmonitor* monitor); -void _glfwInitializeKeyEvent(GLFWkeyevent *ev, int key, int native_key, int action, int mods); void _glfwInputKeyboard(_GLFWwindow *window, GLFWkeyevent *ev); void _glfwInputScroll(_GLFWwindow* window, double xoffset, double yoffset, int flags, int mods); void _glfwInputMouseClick(_GLFWwindow* window, int button, int action, int mods); diff --git a/glfw/window.c b/glfw/window.c index aee43e996..a006d9440 100644 --- a/glfw/window.c +++ b/glfw/window.c @@ -57,8 +57,7 @@ void _glfwInputWindowFocus(_GLFWwindow* window, bool focused) if (window->activated_keys[i].key > 0 && window->activated_keys[i].action == GLFW_PRESS) { const int native_key = _glfwPlatformGetNativeKeyForKey(window->activated_keys[i].key); - GLFWkeyevent ev; - _glfwInitializeKeyEvent(&ev, window->activated_keys[i].key, native_key, GLFW_RELEASE, 0); + GLFWkeyevent ev = {.key = window->activated_keys[i].key, .native_key = native_key, .action = GLFW_RELEASE}; _glfwInputKeyboard(window, &ev); } } diff --git a/glfw/xkb_glfw.c b/glfw/xkb_glfw.c index 26033eaf3..60ad0ba53 100644 --- a/glfw/xkb_glfw.c +++ b/glfw/xkb_glfw.c @@ -557,8 +557,7 @@ glfw_xkb_key_from_ime(_GLFWIBUSKeyEvent *ev, bool handled_by_ime, bool failed) { _GLFWwindow *window = _glfwWindowForId(ev->window_id); if (failed && window && window->callbacks.keyboard) { // notify application to remove any existing pre-edit text - GLFWkeyevent fake_ev; - _glfwInitializeKeyEvent(&fake_ev, 0, 0, GLFW_PRESS, 0); + GLFWkeyevent fake_ev = {.action = GLFW_PRESS}; fake_ev.ime_state = 1; window->callbacks.keyboard((GLFWwindow*) window, &fake_ev); } @@ -591,8 +590,7 @@ glfw_xkb_handle_key_event(_GLFWwindow *window, _GLFWXKBData *xkb, xkb_keycode_t const xkb_keysym_t *syms, *clean_syms, *default_syms; xkb_keysym_t xkb_sym; xkb_keycode_t code_for_sym = xkb_keycode, ibus_keycode = xkb_keycode; - GLFWkeyevent glfw_ev; - _glfwInitializeKeyEvent(&glfw_ev, 0, 0, GLFW_PRESS, 0); // init with default values + GLFWkeyevent glfw_ev = {.action = GLFW_PRESS}; #ifdef _GLFW_WAYLAND code_for_sym += 8; #else