Use a struct for IME update events

Allows for easier extension in the future
This commit is contained in:
Kovid Goyal 2021-03-23 16:05:22 +05:30
parent a981b46ec9
commit 34d06fa3e9
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
11 changed files with 45 additions and 26 deletions

View File

@ -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

20
glfw/glfw3.h vendored
View File

@ -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.

4
glfw/input.c vendored
View File

@ -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

2
glfw/internal.h vendored
View File

@ -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);

4
glfw/wl_window.c vendored
View File

@ -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

4
glfw/x11_window.c vendored
View File

@ -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);
}
//////////////////////////////////////////////////////////////////////////

10
glfw/xkb_glfw.c vendored
View File

@ -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;
}
}

2
glfw/xkb_glfw.h vendored
View File

@ -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);

14
kitty/glfw-wrapper.h generated
View File

@ -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

View File

@ -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;

View File

@ -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