From 63fdbd3fa03654b5b978392efde34461c932b113 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 23 Aug 2022 21:06:08 +0530 Subject: [PATCH] Start work on decoding key events in Go --- gen-key-constants.py | 23 ++++++++++++++++++++--- tools/tui/key-encoding.go | 12 ++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 tools/tui/key-encoding.go diff --git a/gen-key-constants.py b/gen-key-constants.py index f52050be1..79cca17c6 100755 --- a/gen-key-constants.py +++ b/gen-key-constants.py @@ -2,8 +2,8 @@ # License: GPLv3 Copyright: 2021, Kovid Goyal import string -from typing import Dict, List, Any from pprint import pformat +from typing import Any, Dict, List, Union functional_key_defs = '''# {{{ # kitty XKB macVK macU @@ -248,6 +248,19 @@ def serialize_dict(x: Dict[Any, Any]) -> str: return pformat(x, indent=4).replace('{', '{\n ', 1) +def serialize_go_dict(x: Union[Dict[str, int], Dict[int, str], Dict[int, int]]) -> str: + ans = [] + + def s(x: Union[int, str]) -> str: + if isinstance(x, int): + return str(x) + return f'"{x}"' + + for k, v in x.items(): + ans.append(f'{s(k)}: {s(v)}') + return '{' + ', '.join(ans) + '}' + + def generate_glfw_header() -> None: lines = [ 'typedef enum {', @@ -309,14 +322,18 @@ def generate_functional_table() -> None: patch_file('kitty/key_encoding.c', 'special numbers', '\n'.join(enc_lines)) code_to_name = {v: k.upper() for k, v in name_to_code.items()} csi_map = {v: name_to_code[k] for k, v in functional_encoding_overrides.items()} - letter_trailer_codes = { - v: functional_encoding_overrides.get(k, name_to_code.get(k)) + letter_trailer_codes: Dict[str, int] = { + v: functional_encoding_overrides.get(k, name_to_code.get(k, 0)) for k, v in different_trailer_functionals.items() if v in 'ABCDEHFPQRSZ'} text = f'functional_key_number_to_name_map = {serialize_dict(code_to_name)}' text += f'\ncsi_number_to_functional_number_map = {serialize_dict(csi_map)}' text += f'\nletter_trailer_to_csi_number_map = {letter_trailer_codes!r}' text += f'\ntilde_trailers = {tilde_trailers!r}' patch_file('kitty/key_encoding.py', 'csi mapping', text, start_marker='# ', end_marker='') + text = f'var functional_key_number_to_name_map = map[int]string{serialize_go_dict(code_to_name)}\n' + text += f'\nvar csi_number_to_functional_number_map = map[int]int{serialize_go_dict(csi_map)}\n' + text += f'\nvar letter_trailer_to_csi_number_map = map[string]int{serialize_go_dict(letter_trailer_codes)}\n' + patch_file('tools/tui/key-encoding.go', 'csi mapping', text, start_marker='// ', end_marker='') def generate_legacy_text_key_maps() -> None: diff --git a/tools/tui/key-encoding.go b/tools/tui/key-encoding.go new file mode 100644 index 000000000..38a954c11 --- /dev/null +++ b/tools/tui/key-encoding.go @@ -0,0 +1,12 @@ +package tui + +// key encoding mappings {{{ +// start csi mapping (auto generated by gen-key-constants.py do not edit) +var functional_key_number_to_name_map = map[int]string{57344: "ESCAPE", 57345: "ENTER", 57346: "TAB", 57347: "BACKSPACE", 57348: "INSERT", 57349: "DELETE", 57350: "LEFT", 57351: "RIGHT", 57352: "UP", 57353: "DOWN", 57354: "PAGE_UP", 57355: "PAGE_DOWN", 57356: "HOME", 57357: "END", 57358: "CAPS_LOCK", 57359: "SCROLL_LOCK", 57360: "NUM_LOCK", 57361: "PRINT_SCREEN", 57362: "PAUSE", 57363: "MENU", 57364: "F1", 57365: "F2", 57366: "F3", 57367: "F4", 57368: "F5", 57369: "F6", 57370: "F7", 57371: "F8", 57372: "F9", 57373: "F10", 57374: "F11", 57375: "F12", 57376: "F13", 57377: "F14", 57378: "F15", 57379: "F16", 57380: "F17", 57381: "F18", 57382: "F19", 57383: "F20", 57384: "F21", 57385: "F22", 57386: "F23", 57387: "F24", 57388: "F25", 57389: "F26", 57390: "F27", 57391: "F28", 57392: "F29", 57393: "F30", 57394: "F31", 57395: "F32", 57396: "F33", 57397: "F34", 57398: "F35", 57399: "KP_0", 57400: "KP_1", 57401: "KP_2", 57402: "KP_3", 57403: "KP_4", 57404: "KP_5", 57405: "KP_6", 57406: "KP_7", 57407: "KP_8", 57408: "KP_9", 57409: "KP_DECIMAL", 57410: "KP_DIVIDE", 57411: "KP_MULTIPLY", 57412: "KP_SUBTRACT", 57413: "KP_ADD", 57414: "KP_ENTER", 57415: "KP_EQUAL", 57416: "KP_SEPARATOR", 57417: "KP_LEFT", 57418: "KP_RIGHT", 57419: "KP_UP", 57420: "KP_DOWN", 57421: "KP_PAGE_UP", 57422: "KP_PAGE_DOWN", 57423: "KP_HOME", 57424: "KP_END", 57425: "KP_INSERT", 57426: "KP_DELETE", 57427: "KP_BEGIN", 57428: "MEDIA_PLAY", 57429: "MEDIA_PAUSE", 57430: "MEDIA_PLAY_PAUSE", 57431: "MEDIA_REVERSE", 57432: "MEDIA_STOP", 57433: "MEDIA_FAST_FORWARD", 57434: "MEDIA_REWIND", 57435: "MEDIA_TRACK_NEXT", 57436: "MEDIA_TRACK_PREVIOUS", 57437: "MEDIA_RECORD", 57438: "LOWER_VOLUME", 57439: "RAISE_VOLUME", 57440: "MUTE_VOLUME", 57441: "LEFT_SHIFT", 57442: "LEFT_CONTROL", 57443: "LEFT_ALT", 57444: "LEFT_SUPER", 57445: "LEFT_HYPER", 57446: "LEFT_META", 57447: "RIGHT_SHIFT", 57448: "RIGHT_CONTROL", 57449: "RIGHT_ALT", 57450: "RIGHT_SUPER", 57451: "RIGHT_HYPER", 57452: "RIGHT_META", 57453: "ISO_LEVEL3_SHIFT", 57454: "ISO_LEVEL5_SHIFT"} + +var csi_number_to_functional_number_map = map[int]int{2: 57348, 3: 57349, 5: 57354, 6: 57355, 7: 57356, 8: 57357, 9: 57346, 11: 57364, 12: 57365, 13: 57345, 14: 57367, 15: 57368, 17: 57369, 18: 57370, 19: 57371, 20: 57372, 21: 57373, 23: 57374, 24: 57375, 27: 57344, 127: 57347} + +var letter_trailer_to_csi_number_map = map[string]int{"A": 57352, "B": 57353, "C": 57351, "D": 57350, "E": 57427, "F": 8, "H": 7, "P": 11, "Q": 12, "R": 13, "S": 14} + +// end csi mapping +// }}}