From 832506d7858079dfabfbbd20ac1bb7bb1cc1dd37 Mon Sep 17 00:00:00 2001 From: Yuxin Wu Date: Fri, 30 Dec 2022 23:32:46 -0800 Subject: [PATCH] move is_modifier_key to glfw.c and expose in Python --- kitty/boss.py | 6 +++--- kitty/fast_data_types.pyi | 4 ++++ kitty/glfw.c | 28 ++++++++++++++++++++++++++++ kitty/key_encoding.c | 15 --------------- kitty/key_encoding.py | 11 ----------- kitty/keys.h | 3 ++- 6 files changed, 37 insertions(+), 30 deletions(-) diff --git a/kitty/boss.py b/kitty/boss.py index 17503d6c5..14809d7ea 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -40,14 +40,14 @@ from .fast_data_types import ( cocoa_minimize_os_window, cocoa_set_menubar_title, create_os_window, current_application_quit_request, current_focused_os_window_id, current_os_window, destroy_global_data, focus_os_window, get_boss, get_options, get_os_window_size, - global_font_size, last_focused_os_window_id, mark_os_window_for_close, + glfw_is_modifier_key, global_font_size, last_focused_os_window_id, mark_os_window_for_close, os_window_font_size, patch_global_colors, redirect_mouse_handling, ring_bell, run_with_activation_token, safe_pipe, send_data_to_peer, set_application_quit_request, set_background_image, set_boss, set_in_sequence_mode, set_options, set_os_window_size, set_os_window_title, thread_write, toggle_fullscreen, toggle_maximized, toggle_secure_input, ) -from .key_encoding import get_name_to_functional_number_map, is_modifier_key +from .key_encoding import get_name_to_functional_number_map from .keys import get_shortcut, shortcut_matches from .layout.base import set_layout_options from .notify import notification_activated @@ -1193,7 +1193,7 @@ class Boss: if len(self.current_sequence): self.current_sequence.append(ev) - if ev.action == GLFW_RELEASE or is_modifier_key(ev.key): + if ev.action == GLFW_RELEASE or glfw_is_modifier_key(ev.key): return True # For a press/repeat event that's not a modifier, try matching with # kitty bindings: diff --git a/kitty/fast_data_types.pyi b/kitty/fast_data_types.pyi index b9df5db54..07c150faa 100644 --- a/kitty/fast_data_types.pyi +++ b/kitty/fast_data_types.pyi @@ -313,6 +313,10 @@ def glfw_get_key_name(key: int, native_key: int) -> Optional[str]: pass +def glfw_is_modifier_key(key: int) -> bool: + pass + + StartupCtx = NewType('StartupCtx', int) Display = NewType('Display', int) diff --git a/kitty/glfw.c b/kitty/glfw.c index 320340672..96a6a90c1 100644 --- a/kitty/glfw.c +++ b/kitty/glfw.c @@ -1256,6 +1256,33 @@ glfw_get_key_name(PyObject UNUSED *self, PyObject *args) { return Py_BuildValue("z", glfwGetKeyName(key, native_key)); } + +bool +is_modifier_key(const uint32_t key) { + START_ALLOW_CASE_RANGE + switch (key) { + case GLFW_FKEY_LEFT_SHIFT ... GLFW_FKEY_ISO_LEVEL5_SHIFT: + case GLFW_FKEY_CAPS_LOCK: + case GLFW_FKEY_SCROLL_LOCK: + case GLFW_FKEY_NUM_LOCK: + return true; + default: + return false; + } + END_ALLOW_CASE_RANGE +} + + +static PyObject* +glfw_is_modifier_key(PyObject UNUSED *self, PyObject *args) { + uint32_t key; + if (!PyArg_ParseTuple(args, "I", &key)) return NULL; + PyObject *ans = is_modifier_key(key) ? Py_True : Py_False; + Py_INCREF(ans); + return ans; +} + + static PyObject* glfw_window_hint(PyObject UNUSED *self, PyObject *args) { int key, val; @@ -1762,6 +1789,7 @@ static PyMethodDef module_methods[] = { {"glfw_terminate", (PyCFunction)glfw_terminate, METH_NOARGS, ""}, {"glfw_get_physical_dpi", (PyCFunction)glfw_get_physical_dpi, METH_NOARGS, ""}, {"glfw_get_key_name", (PyCFunction)glfw_get_key_name, METH_VARARGS, ""}, + METHODB(glfw_is_modifier_key, METH_VARARGS), {"glfw_primary_monitor_size", (PyCFunction)primary_monitor_size, METH_NOARGS, ""}, {"glfw_primary_monitor_content_scale", (PyCFunction)primary_monitor_content_scale, METH_NOARGS, ""}, {NULL, NULL, 0, NULL} /* Sentinel */ diff --git a/kitty/key_encoding.c b/kitty/key_encoding.c index a643b8c89..47a54f950 100644 --- a/kitty/key_encoding.c +++ b/kitty/key_encoding.c @@ -31,21 +31,6 @@ typedef struct { KeyAction action; } EncodingData; -bool -is_modifier_key(const uint32_t key) { - START_ALLOW_CASE_RANGE - switch (key) { - case GLFW_FKEY_LEFT_SHIFT ... GLFW_FKEY_ISO_LEVEL5_SHIFT: - case GLFW_FKEY_CAPS_LOCK: - case GLFW_FKEY_SCROLL_LOCK: - case GLFW_FKEY_NUM_LOCK: - return true; - default: - return false; - } - END_ALLOW_CASE_RANGE -} - static void convert_glfw_mods(int mods, KeyEvent *ev, const unsigned key_encoding_flags) { if (!key_encoding_flags) mods &= ~GLFW_LOCK_MASK; diff --git a/kitty/key_encoding.py b/kitty/key_encoding.py index 99b65819c..110359b61 100644 --- a/kitty/key_encoding.py +++ b/kitty/key_encoding.py @@ -426,14 +426,3 @@ def decode_key_event_as_window_system_key(text: str) -> Optional[WindowSystemKey except Exception: return None return k.as_window_system_event() - - -# The same as `bool is_modifier_key(key)` in key_encoding.c -def is_modifier_key(key: int) -> bool: - if defines.GLFW_FKEY_LEFT_SHIFT <= key <= defines.GLFW_FKEY_ISO_LEVEL5_SHIFT: - return True - if key == defines.GLFW_FKEY_CAPS_LOCK or \ - key == defines.GLFW_FKEY_SCROLL_LOCK or \ - key == defines.GLFW_FKEY_NUM_LOCK: - return True - return False diff --git a/kitty/keys.h b/kitty/keys.h index 2f70ca6d6..944dc4797 100644 --- a/kitty/keys.h +++ b/kitty/keys.h @@ -18,5 +18,6 @@ int encode_glfw_key_event(const GLFWkeyevent *e, const bool cursor_key_mode, const unsigned flags, char *output); -bool +// Defined in glfw.c +extern bool is_modifier_key(const uint32_t key);