From 753ad68ca910afc7229573eb43004a330b14576c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 13 Jan 2021 11:40:16 +0530 Subject: [PATCH] Add tests for event type reporting --- docs/keyboard-protocol.rst | 20 +++++++++++--------- kitty/key_encoding.c | 1 + kitty_tests/keys.py | 17 ++++++++++++++--- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/docs/keyboard-protocol.rst b/docs/keyboard-protocol.rst index 8dc510fba..b84faf8a8 100644 --- a/docs/keyboard-protocol.rst +++ b/docs/keyboard-protocol.rst @@ -114,16 +114,18 @@ Event types ~~~~~~~~~~~~~~~~ There are three key event types: ``press, repeat and release``. They are -reported (if requested) as a sub-field of the modifiers field (separated by a -colon). If no modifiers are present, the modifiers field must have the value -``1`` and the event type sub-field the type of event. The ``press`` event type -has value ``1`` and is the default if no event type sub field is present. The -``repeat`` type is ``2`` and the ``release`` type is ``3``. So for example:: +reported (if requested ``0b10``) as a sub-field of the modifiers field +(separated by a colon). If no modifiers are present, the modifiers field must +have the value ``1`` and the event type sub-field the type of event. The +``press`` event type has value ``1`` and is the default if no event type sub +field is present. The ``repeat`` type is ``2`` and the ``release`` type is +``3``. So for example:: - CSI key-code;1 # this is a press event - CSI key-code;1:1 # this is a press event - CSI key-code;1:2 # this is a repeat event - CSI key-code:1:3 # this is a release event + CSI key-code # this is a press event + CSI key-code;modifier # this is a press event + CSI key-code;modifier:1 # this is a press event + CSI key-code;modifier:2 # this is a repeat event + CSI key-code:modifier:3 # this is a release event .. note:: Key events that result in text are reported as plain UTF-8 text, so diff --git a/kitty/key_encoding.c b/kitty/key_encoding.c index f7b11fac1..5a735ee59 100644 --- a/kitty/key_encoding.c +++ b/kitty/key_encoding.c @@ -53,6 +53,7 @@ init_encoding_data(EncodingData *ans, const KeyEvent *ev) { ans->has_mods = ev->mods.encoded[0] && ev->mods.encoded[0] != '1'; ans->add_alternates = ev->report_alternate_key && (ev->shifted_key > 0 || ev->alternate_key > 0); if (ans->add_alternates) { ans->shifted_key = ev->shifted_key; ans->alternate_key = ev->alternate_key; } + ans->action = ev->action; ans->key = ev->key; memcpy(ans->encoded_mods, ev->mods.encoded, sizeof(ans->encoded_mods)); } diff --git a/kitty_tests/keys.py b/kitty_tests/keys.py index 435b69937..a65e221c6 100644 --- a/kitty_tests/keys.py +++ b/kitty_tests/keys.py @@ -15,11 +15,13 @@ class TestKeys(BaseTest): shift, alt, ctrl, super = defines.GLFW_MOD_SHIFT, defines.GLFW_MOD_ALT, defines.GLFW_MOD_CONTROL, defines.GLFW_MOD_SUPER # noqa press, repeat, release = defines.GLFW_PRESS, defines.GLFW_REPEAT, defines.GLFW_RELEASE # noqa - def csi(mods=0, num=1, trailer='u'): + def csi(mods=0, num=1, action=1, trailer='u'): ans = '\033[' + if isinstance(num, str): + num = ord(num) if num != 1 or mods: ans += f'{num}' - if mods: + if mods or action > 1: m = 0 if mods & shift: m |= 1 @@ -30,6 +32,8 @@ class TestKeys(BaseTest): if mods & super: m |= 8 ans += f';{m+1}' + if action > 1: + ans += f':{action}' return ans + trailer def mods_test(key, plain=None, shift=None, ctrl=None, alt=None, calt=None, cshift=None, ashift=None, csi_num=None, trailer='u'): @@ -390,7 +394,14 @@ class TestKeys(BaseTest): ae(dq(k), csi(num=k)) ae(dq(k, mods=ctrl), csi(ctrl, num=k)) ae(dq(defines.GLFW_FKEY_UP), '\x1b[A') - ae(dq(defines.GLFW_FKEY_UP, mods=ctrl), csi(ctrl, 1, 'A')) + ae(dq(defines.GLFW_FKEY_UP, mods=ctrl), csi(ctrl, 1, trailer='A')) + + # test event type reporting + tq = partial(enc, key_encoding_flags=0b10) + ae(tq(ord('a')), 'a') + ae(tq(ord('a'), action=defines.GLFW_REPEAT), csi(num='a', action=2)) + ae(tq(ord('a'), action=defines.GLFW_RELEASE), csi(num='a', action=3)) + ae(tq(ord('a'), action=defines.GLFW_RELEASE, mods=shift), csi(shift, num='a', action=3)) def test_encode_mouse_event(self): NORMAL_PROTOCOL, UTF8_PROTOCOL, SGR_PROTOCOL, URXVT_PROTOCOL = range(4)