From 5bb1466e07cf630dfe1cc9daec98df2d8249f0cc Mon Sep 17 00:00:00 2001 From: Diego Pino Garcia Date: Thu, 6 Jul 2023 11:28:52 +0800 Subject: [PATCH] Manage CAPS_LOCK and NUM_LOCK keys --- platform/gtk4/cog-platform-gtk4.c | 16 ++++++++++++++++ platform/wayland/cog-platform-wl.c | 16 ++++++++++++++++ platform/x11/cog-platform-x11.c | 8 ++++++-- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/platform/gtk4/cog-platform-gtk4.c b/platform/gtk4/cog-platform-gtk4.c index 4cf6f9c4..e62ce542 100644 --- a/platform/gtk4/cog-platform-gtk4.c +++ b/platform/gtk4/cog-platform-gtk4.c @@ -340,6 +340,18 @@ on_scroll(GtkEventControllerScroll* controller, double dx, return TRUE; } +static gboolean +is_pressed_key(guint keyval) +{ + static bool initialized = false; + + if (!initialized) { + GUniqueOutPtr keys; + int entriesCount; + } + return gdk_display_map_keyval(gdk_display_get_default(), keyval, &keys.outPtr(), &entriesCount) && entriesCount; +} + static gboolean dispatch_key_event(struct platform_window* win, guint keycode, guint hardware_keycode, gboolean pressed, GdkModifierType state) { @@ -351,6 +363,10 @@ dispatch_key_event(struct platform_window* win, guint keycode, guint hardware_ke modifiers |= wpe_input_keyboard_modifier_alt; if (state & GDK_SHIFT_MASK) modifiers |= wpe_input_keyboard_modifier_shift; + if (is_pressed_key(GDK_KEY_Caps_Lock)) + modifiers |= wpe_input_keyboard_modifier_capslock; + if (is_pressed_key(GDK_KEY_Num_Lock)) + modifiers |= wpe_input_keyboard_modifier_numlock; struct wpe_input_keyboard_event wpe_event = { .key_code = keycode, diff --git a/platform/wayland/cog-platform-wl.c b/platform/wayland/cog-platform-wl.c index 318b3125..2ebb3e9d 100644 --- a/platform/wayland/cog-platform-wl.c +++ b/platform/wayland/cog-platform-wl.c @@ -294,6 +294,8 @@ static struct { xkb_mod_index_t control; xkb_mod_index_t alt; xkb_mod_index_t shift; + xkb_mod_index_t capslock; + xkb_mod_index_t numlock; } indexes; uint8_t modifiers; } xkb_data = {NULL, }; @@ -1072,6 +1074,10 @@ keyboard_on_keymap (void *data, XKB_MOD_NAME_ALT); xkb_data.indexes.shift = xkb_keymap_mod_get_index (xkb_data.keymap, XKB_MOD_NAME_SHIFT); + xkb_data.indexes.capslock = xkb_state_led_name_is_active (xkb_data.state, + XKB_LED_NAME_CAPS); + xkb_data.indexes.numlock = xkb_state_led_name_is_active (xkb_data.state, + XKB_LED_NAME_NUM); } static void @@ -1232,6 +1238,16 @@ keyboard_on_modifiers (void *data, component)) { xkb_data.modifiers |= wpe_input_keyboard_modifier_shift; } + if (xkb_state_mod_index_is_active (xkb_data.state, + xkb_data.indexes.capslock, + component)) { + xkb_data.modifiers |= wpe_input_keyboard_modifier_capslock; + } + if (xkb_state_mod_index_is_active (xkb_data.state, + xkb_data.indexes.numlock, + component)) { + xkb_data.modifiers |= wpe_input_keyboard_modifier_numlock; + } } static void diff --git a/platform/x11/cog-platform-x11.c b/platform/x11/cog-platform-x11.c index 06c1c30f..768dc2c2 100644 --- a/platform/x11/cog-platform-x11.c +++ b/platform/x11/cog-platform-x11.c @@ -197,10 +197,14 @@ xcb_update_xkb_modifiers(uint32_t event_state) wpe_modifiers |= wpe_input_keyboard_modifier_alt; } - if (event_state & XCB_MOD_MASK_LOCK) + if (event_state & XCB_MOD_MASK_LOCK) { locked_mods |= s_display->xkb.caps_lock; - if (event_state & XCB_MOD_MASK_2) + wpe_modifiers |= wpe_input_keyboard_modifier_capslock; + } + if (event_state & XCB_MOD_MASK_2) { locked_mods |= s_display->xkb.num_lock; + wpe_modifiers |= wpe_input_keyboard_modifier_numlock; + } xkb_state_update_mask(s_display->xkb.state, depressed_mods, 0, locked_mods, 0, 0, 0); return wpe_modifiers; }