move is_modifier_key to glfw.c and expose in Python

This commit is contained in:
Yuxin Wu 2022-12-30 23:32:46 -08:00
parent 13b09346b9
commit 832506d785
6 changed files with 37 additions and 30 deletions

View File

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

View File

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

View File

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

View File

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

11
kitty/key_encoding.py generated
View File

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

View File

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