Keysyms: improve generator (#364) Motivation: normalization of keysyms header files in `xorgproto`. See: https://gitlab.freedesktop.org/xorg/proto/xorgproto/-/merge_requests/80 Improve `scripts/makeheader`: - Simplify `evdev` and `XK_` substitution and improve alignment. Also, perform some additional `XK_` substitutions in comments. - Format with `black`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
diff --git a/scripts/makeheader b/scripts/makeheader
index bb0db59..f2738bd 100755
--- a/scripts/makeheader
+++ b/scripts/makeheader
@@ -3,20 +3,63 @@ from __future__ import print_function
import re
import os
-# expected format:
-# #define XF86XK_FooBar _EVDEVK(0x123) /* some optional comment */
-evdev_pattern = re.compile(r'^#define\s+XF86XK_(?P<name>\w+)\s+_EVDEVK\((?P<value>0x[0-9A-Fa-f]+)\)')
+# Expected format:
+# #define XF86XK_FooBar 0x1234 /* some optional comment */
+# or:
+# #define XF86XK_FooBar _EVDEVK(0x123) /* some optional comment */
+# We also need to match commented evdev entries:
+# /* Use: XF86XK_FooBar _EVDEVK(0x123) some optional comment */
+keysym_entry_pattern = re.compile(
+ r"""^
+ (?P<define>\#define|/\*\s+Use:)\s+
+ (?P<prefix>\w*)XK_(?P<name>\w+)(?P<spacing>\s+)
+ (?P<evdev>_EVDEVK\()?(?P<value>0x[0-9A-Fa-f]+)(?(evdev)\))
+ """,
+ re.VERBOSE,
+)
-prefix = os.environ.get('X11_HEADERS_PREFIX', '/usr')
+# Match remaining XK_ references in the comments, e.g we will replace:
+# XF86XK_CamelCaseKernelName _EVDEVK(kernel value)
+# #define XKB_KEY_SunCompose 0x0000FF20 /* Same as XK_Multi_key */
+# with:
+# XKB_KEY_XF86CamelCaseKernelName _EVDEVK(kernel value)
+# #define XKB_KEY_SunCompose 0x0000FF20 /* Same as XKB_KEY_Multi_key */
+xorgproto_keysym_prefix_pattern = re.compile(r"\b(?P<prefix>\w*)XK_(?!KOREAN\b)")
+
+
+def make_keysym_entry(m: re.Match[str]) -> str:
+ """
+ Perform the substitutions
+ """
+ if m.group("evdev"):
+ if m.group("define").startswith("#"):
+ # Replace the xorgproto _EVDEVK macro with the actual value:
+ # 0x10081000 is the base, the evdev hex code is added to that.
+ # We replace to make parsing of the keys later easier.
+ value = 0x10081000 + int(m.group("value"), 16)
+ value_str = f"{value:#x} "
+ else:
+ value_str = f"""_EVDEVK({m.group('value')})"""
+ else:
+ value_str = m.group("value")
+ define = m.group("define")
+ prefix = m.group("prefix") or ""
+ name = m.group("name")
+ spacing = m.group("spacing")
+ return f"""{define} XKB_KEY_{prefix}{name}{spacing}{value_str}"""
+
+
+prefix = os.environ.get("X11_HEADERS_PREFIX", "/usr")
HEADERS = [
- prefix + '/include/X11/keysymdef.h',
- prefix + '/include/X11/XF86keysym.h',
- prefix + '/include/X11/Sunkeysym.h',
- prefix + '/include/X11/DECkeysym.h',
- prefix + '/include/X11/HPkeysym.h',
+ prefix + "/include/X11/keysymdef.h",
+ prefix + "/include/X11/XF86keysym.h",
+ prefix + "/include/X11/Sunkeysym.h",
+ prefix + "/include/X11/DECkeysym.h",
+ prefix + "/include/X11/HPkeysym.h",
]
-print('''#ifndef _XKBCOMMON_KEYSYMS_H
+print(
+ """#ifndef _XKBCOMMON_KEYSYMS_H
#define _XKBCOMMON_KEYSYMS_H
/* This file is autogenerated; please do not commit directly. */
@@ -27,31 +70,26 @@ print('''#ifndef _XKBCOMMON_KEYSYMS_H
*/
#define XKB_KEY_NoSymbol 0x000000 /* Special KeySym */
-''')
+"""
+)
for path in HEADERS:
with open(path) as header:
for line in header:
- if '#ifdef' in line or '#ifndef' in line or '#endif' in line:
+ if "#ifdef" in line or "#ifndef" in line or "#endif" in line:
continue
# Remove #define _OSF_Keysyms and such.
- if '#define _' in line:
+ if "#define _" in line:
continue
# Handle a duplicate definition in HPkeysyms.h which kicks in if
# it's not already defined.
- if 'XK_Ydiaeresis' in line and '0x100000ee' in line:
+ if "XK_Ydiaeresis" in line and "0x100000ee" in line:
continue
- # Replace the xorgproto _EVDEVK macro with the actual value
- # 0x10081000 is the base, the evdev hex code is added to that.
- # We replace to make parsing of the keys later easier.
- match = re.match(evdev_pattern, line)
- if match:
- value = 0x10081000 + int(match.group('value'), 16)
- line = re.sub(r'_EVDEVK\(0x([0-9A-Fa-f]+)\)', '{:#x}'.format(value), line)
-
- line = re.sub(r'#define\s*(\w*)XK_', r'#define XKB_KEY_\1', line)
+ # Perform _EVDEV and XK_ substitutions
+ line = keysym_entry_pattern.sub(make_keysym_entry, line)
+ line = xorgproto_keysym_prefix_pattern.sub(r"XKB_KEY_\1", line)
- print(line, end='')
-print('\n\n#endif')
+ print(line, end="")
+print("\n\n#endif")