From 34d06fa3e9de402049fb4aca5dc74f96221b36cf Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 23 Mar 2021 16:05:22 +0530 Subject: [PATCH] Use a struct for IME update events Allows for easier extension in the future --- glfw/cocoa_window.m | 4 ++-- glfw/glfw3.h | 20 +++++++++++++------- glfw/input.c | 4 ++-- glfw/internal.h | 2 +- glfw/wl_window.c | 4 ++-- glfw/x11_window.c | 4 ++-- glfw/xkb_glfw.c | 10 +++++----- glfw/xkb_glfw.h | 2 +- kitty/glfw-wrapper.h | 14 ++++++++++++-- kitty/glfw.c | 3 ++- kitty/keys.c | 4 +++- 11 files changed, 45 insertions(+), 26 deletions(-) diff --git a/glfw/cocoa_window.m b/glfw/cocoa_window.m index afed53938..1ad25a46c 100644 --- a/glfw/cocoa_window.m +++ b/glfw/cocoa_window.m @@ -1295,8 +1295,8 @@ is_ascii_control_char(char x) { [[markedText mutableString] setString:@""]; } -void _glfwPlatformUpdateIMEState(_GLFWwindow *w, GLFWIMEUpdateState which, int a, int b, int c, int d) { - [w->ns.view updateIMEStateFor: which left:(CGFloat)a top:(CGFloat)b cellWidth:(CGFloat)c cellHeight:(CGFloat)d]; +void _glfwPlatformUpdateIMEState(_GLFWwindow *w, const GLFWIMEUpdateEvent *ev) { + [w->ns.view updateIMEStateFor: which left:(CGFloat)ev->cursor.left top:(CGFloat)ev->cursor.top cellWidth:(CGFloat)ev->cursor.width cellHeight:(CGFloat)ev->cursor.height]; } - (void)updateIMEStateFor:(GLFWIMEUpdateState)which diff --git a/glfw/glfw3.h b/glfw/glfw3.h index 18518e85a..14fce1227 100644 --- a/glfw/glfw3.h +++ b/glfw/glfw3.h @@ -1195,7 +1195,17 @@ typedef enum { typedef enum { GLFW_IME_UPDATE_FOCUS = 1, GLFW_IME_UPDATE_CURSOR_POSITION = 2 -} GLFWIMEUpdateState; +} GLFWIMEUpdateType; + +typedef struct GLFWIMEUpdateEvent { + GLFWIMEUpdateType type; + const char *before_text, *at_text, *after_text; + bool focused; + struct { + int left, top, width, height; + } cursor; +} GLFWIMEUpdateEvent; + typedef struct GLFWkeyevent { @@ -4545,16 +4555,12 @@ GLFWAPI GLFWkeyboardfun glfwSetKeyboardCallback(GLFWwindow* window, GLFWkeyboard * Used to notify the IME system of changes in state such as focus gained/lost * and text cursor position. * - * @param which: What data to notify. - * @param a, b, c, d: Interpreted based on the value of which. When which is GLFW_IME_UPDATE_FOCUS - * a is interpreted as a boolean indicating focus gained/lost. When which is GLFW_IME_UPDATE_CURSOR_POSITION - * a, b, c, d are the cursor x, y, width and height values (in the window co-ordinate - * system). + * @param ev: What data to notify. * * @ingroup input * @since Added in version 4.0 */ -GLFWAPI void glfwUpdateIMEState(GLFWwindow* window, GLFWIMEUpdateState which, int a, int b, int c, int d); +GLFWAPI void glfwUpdateIMEState(GLFWwindow* window, const GLFWIMEUpdateEvent *ev); /*! @brief Sets the mouse button callback. diff --git a/glfw/input.c b/glfw/input.c index 7ba7922df..f07bccfed 100644 --- a/glfw/input.c +++ b/glfw/input.c @@ -1010,13 +1010,13 @@ GLFWAPI GLFWkeyboardfun glfwSetKeyboardCallback(GLFWwindow* handle, GLFWkeyboard return cbfun; } -GLFWAPI void glfwUpdateIMEState(GLFWwindow* handle, GLFWIMEUpdateState which, int a, int b, int c, int d) { +GLFWAPI void glfwUpdateIMEState(GLFWwindow* handle, const GLFWIMEUpdateEvent *ev) { _GLFWwindow* window = (_GLFWwindow*) handle; assert(window != NULL); _GLFW_REQUIRE_INIT(); #if defined(_GLFW_X11) || defined(_GLFW_WAYLAND) || defined(_GLFW_COCOA) - _glfwPlatformUpdateIMEState(window, which, a, b, c, d); + _glfwPlatformUpdateIMEState(window, ev); #else (void)window; (void)which; (void)a; (void)b; (void)c; (void)d; #endif diff --git a/glfw/internal.h b/glfw/internal.h index 88076bed1..85e6da567 100644 --- a/glfw/internal.h +++ b/glfw/internal.h @@ -728,7 +728,7 @@ void _glfwPlatformSetWindowDecorated(_GLFWwindow* window, bool enabled); void _glfwPlatformSetWindowFloating(_GLFWwindow* window, bool enabled); void _glfwPlatformSetWindowMousePassthrough(_GLFWwindow* window, bool enabled); void _glfwPlatformSetWindowOpacity(_GLFWwindow* window, float opacity); -void _glfwPlatformUpdateIMEState(_GLFWwindow *w, GLFWIMEUpdateState which, int a, int b, int c, int d); +void _glfwPlatformUpdateIMEState(_GLFWwindow *w, const GLFWIMEUpdateEvent *ev); void _glfwPlatformPollEvents(void); void _glfwPlatformWaitEvents(void); diff --git a/glfw/wl_window.c b/glfw/wl_window.c index b3eb96a59..4c7bf5dc5 100644 --- a/glfw/wl_window.c +++ b/glfw/wl_window.c @@ -2105,8 +2105,8 @@ VkResult _glfwPlatformCreateWindowSurface(VkInstance instance, } void -_glfwPlatformUpdateIMEState(_GLFWwindow *w, GLFWIMEUpdateState which, int a, int b, int c, int d) { - glfw_xkb_update_ime_state(w, &_glfw.wl.xkb, which, a, b, c, d); +_glfwPlatformUpdateIMEState(_GLFWwindow *w, const GLFWIMEUpdateEvent *ev) { + glfw_xkb_update_ime_state(w, &_glfw.wl.xkb, ev); } static void diff --git a/glfw/x11_window.c b/glfw/x11_window.c index e7d6149df..e00545c6c 100644 --- a/glfw/x11_window.c +++ b/glfw/x11_window.c @@ -3085,8 +3085,8 @@ VkResult _glfwPlatformCreateWindowSurface(VkInstance instance, } void -_glfwPlatformUpdateIMEState(_GLFWwindow *w, GLFWIMEUpdateState which, int a, int b, int c, int d) { - glfw_xkb_update_ime_state(w, &_glfw.x11.xkb, which, a, b, c, d); +_glfwPlatformUpdateIMEState(_GLFWwindow *w, const GLFWIMEUpdateEvent *ev) { + glfw_xkb_update_ime_state(w, &_glfw.x11.xkb, ev); } ////////////////////////////////////////////////////////////////////////// diff --git a/glfw/xkb_glfw.c b/glfw/xkb_glfw.c index 47b16d5a0..6c92e7e87 100644 --- a/glfw/xkb_glfw.c +++ b/glfw/xkb_glfw.c @@ -555,16 +555,16 @@ format_xkb_mods(_GLFWXKBData *xkb, const char* name, xkb_mod_mask_t mods) { } void -glfw_xkb_update_ime_state(_GLFWwindow *w, _GLFWXKBData *xkb, GLFWIMEUpdateState which, int a, int b, int c, int d) { +glfw_xkb_update_ime_state(_GLFWwindow *w, _GLFWXKBData *xkb, const GLFWIMEUpdateEvent *ev) { int x = 0, y = 0; - switch(which) { + switch(ev->type) { case GLFW_IME_UPDATE_FOCUS: - glfw_ibus_set_focused(&xkb->ibus, a ? true : false); + glfw_ibus_set_focused(&xkb->ibus, ev->focused); break; case GLFW_IME_UPDATE_CURSOR_POSITION: _glfwPlatformGetWindowPos(w, &x, &y); - x += a; y += b; - glfw_ibus_set_cursor_geometry(&xkb->ibus, x, y, c, d); + x += ev->cursor.left; y += ev->cursor.top; + glfw_ibus_set_cursor_geometry(&xkb->ibus, x, y, ev->cursor.width, ev->cursor.height); break; } } diff --git a/glfw/xkb_glfw.h b/glfw/xkb_glfw.h index 68d7ccd18..d2c086a3f 100644 --- a/glfw/xkb_glfw.h +++ b/glfw/xkb_glfw.h @@ -92,5 +92,5 @@ const char* glfw_xkb_keysym_name(xkb_keysym_t sym); xkb_keysym_t glfw_xkb_sym_for_key(uint32_t key); void glfw_xkb_handle_key_event(_GLFWwindow *window, _GLFWXKBData *xkb, xkb_keycode_t keycode, int action); int glfw_xkb_keysym_from_name(const char *name, bool case_sensitive); -void glfw_xkb_update_ime_state(_GLFWwindow *w, _GLFWXKBData *xkb, GLFWIMEUpdateState which, int a, int b, int c, int d); +void glfw_xkb_update_ime_state(_GLFWwindow *w, _GLFWXKBData *xkb, const GLFWIMEUpdateEvent *ev); void glfw_xkb_key_from_ime(_GLFWIBUSKeyEvent *ev, bool handled_by_ime, bool failed); diff --git a/kitty/glfw-wrapper.h b/kitty/glfw-wrapper.h index a5856b1ec..101cde393 100644 --- a/kitty/glfw-wrapper.h +++ b/kitty/glfw-wrapper.h @@ -933,7 +933,17 @@ typedef enum { typedef enum { GLFW_IME_UPDATE_FOCUS = 1, GLFW_IME_UPDATE_CURSOR_POSITION = 2 -} GLFWIMEUpdateState; +} GLFWIMEUpdateType; + +typedef struct GLFWIMEUpdateEvent { + GLFWIMEUpdateType type; + const char *before_text, *at_text, *after_text; + bool focused; + struct { + int left, top, width, height; + } cursor; +} GLFWIMEUpdateEvent; + typedef struct GLFWkeyevent { @@ -1932,7 +1942,7 @@ typedef GLFWkeyboardfun (*glfwSetKeyboardCallback_func)(GLFWwindow*, GLFWkeyboar GFW_EXTERN glfwSetKeyboardCallback_func glfwSetKeyboardCallback_impl; #define glfwSetKeyboardCallback glfwSetKeyboardCallback_impl -typedef void (*glfwUpdateIMEState_func)(GLFWwindow*, GLFWIMEUpdateState, int, int, int, int); +typedef void (*glfwUpdateIMEState_func)(GLFWwindow*, const GLFWIMEUpdateEvent*); GFW_EXTERN glfwUpdateIMEState_func glfwUpdateIMEState_impl; #define glfwUpdateIMEState glfwUpdateIMEState_impl diff --git a/kitty/glfw.c b/kitty/glfw.c index b0ff141b3..6e8b87a1f 100644 --- a/kitty/glfw.c +++ b/kitty/glfw.c @@ -343,7 +343,8 @@ window_focus_callback(GLFWwindow *w, int focused) { global_state.callback_os_window->cursor_blink_zero_time = now; if (is_window_ready_for_callbacks()) { WINDOW_CALLBACK(on_focus, "O", focused ? Py_True : Py_False); - glfwUpdateIMEState(global_state.callback_os_window->handle, 1, focused, 0, 0, 0); + GLFWIMEUpdateEvent ev = { .type = GLFW_IME_UPDATE_FOCUS, .focused = focused }; + glfwUpdateIMEState(global_state.callback_os_window->handle, &ev); } request_tick_callback(); global_state.callback_os_window = NULL; diff --git a/kitty/keys.c b/kitty/keys.c index f958b51f4..cde7bf4e3 100644 --- a/kitty/keys.c +++ b/kitty/keys.c @@ -85,7 +85,9 @@ update_ime_position(OSWindow *os_window, Window* w, Screen *screen) { unsigned int left = w->geometry.left, top = w->geometry.top; left += screen->cursor->x * cell_width; top += screen->cursor->y * cell_height; - glfwUpdateIMEState(global_state.callback_os_window->handle, 2, left, top, cell_width, cell_height); + GLFWIMEUpdateEvent ev = { .type = GLFW_IME_UPDATE_CURSOR_POSITION }; + ev.cursor.left = left; ev.cursor.top = top; ev.cursor.width = cell_width; ev.cursor.height = cell_height; + glfwUpdateIMEState(global_state.callback_os_window->handle, &ev); } void