2 from __future__ import print_function
7 # #define XF86XK_FooBar 0x1234 /* some optional comment */
9 # #define XF86XK_FooBar _EVDEVK(0x123) /* some optional comment */
10 # We also need to match commented evdev entries:
11 # /* Use: XF86XK_FooBar _EVDEVK(0x123) some optional comment */
12 keysym_entry_pattern = re.compile(
14 (?P<define>\#define|/\*\s+Use:)\s+
15 (?P<prefix>\w*)XK_(?P<name>\w+)(?P<spacing>\s+)
16 (?P<evdev>_EVDEVK\()?(?P<value>0x[0-9A-Fa-f]+)(?(evdev)\))
21 # Match remaining XK_ references in the comments, e.g we will replace:
22 # XF86XK_CamelCaseKernelName _EVDEVK(kernel value)
23 # #define XKB_KEY_SunCompose 0x0000FF20 /* Same as XK_Multi_key */
25 # XKB_KEY_XF86CamelCaseKernelName _EVDEVK(kernel value)
26 # #define XKB_KEY_SunCompose 0x0000FF20 /* Same as XKB_KEY_Multi_key */
27 xorgproto_keysym_prefix_pattern = re.compile(r"\b(?P<prefix>\w*)XK_(?!KOREAN\b)")
30 def make_keysym_entry(m: re.Match[str]) -> str:
32 Perform the substitutions
35 if m.group("define").startswith("#"):
36 # Replace the xorgproto _EVDEVK macro with the actual value:
37 # 0x10081000 is the base, the evdev hex code is added to that.
38 # We replace to make parsing of the keys later easier.
39 value = 0x10081000 + int(m.group("value"), 16)
40 value_str = f"{value:#x} "
42 value_str = f"""_EVDEVK({m.group('value')})"""
44 value_str = m.group("value")
45 define = m.group("define")
46 prefix = m.group("prefix") or ""
47 name = m.group("name")
48 spacing = m.group("spacing")
49 return f"""{define} XKB_KEY_{prefix}{name}{spacing}{value_str}"""
52 prefix = os.environ.get("X11_HEADERS_PREFIX", "/usr")
54 prefix + "/include/X11/keysymdef.h",
55 prefix + "/include/X11/XF86keysym.h",
56 prefix + "/include/X11/Sunkeysym.h",
57 prefix + "/include/X11/DECkeysym.h",
58 prefix + "/include/X11/HPkeysym.h",
62 """#ifndef _XKBCOMMON_KEYSYMS_H
63 #define _XKBCOMMON_KEYSYMS_H
65 /* This file is autogenerated; please do not commit directly. */
69 * Key symbols (keysyms) definitions.
72 #define XKB_KEY_NoSymbol 0x000000 /* Special KeySym */
76 with open(path) as header:
78 if "#ifdef" in line or "#ifndef" in line or "#endif" in line:
81 # Remove #define _OSF_Keysyms and such.
82 if "#define _" in line:
85 # Handle a duplicate definition in HPkeysyms.h which kicks in if
86 # it's not already defined.
87 if "XK_Ydiaeresis" in line and "0x100000ee" in line:
90 # Perform _EVDEV and XK_ substitutions
91 line = keysym_entry_pattern.sub(make_keysym_entry, line)
92 line = xorgproto_keysym_prefix_pattern.sub(r"XKB_KEY_\1", line)