Tools: Improve xkbcli help messages and manual pages
[platform/upstream/libxkbcommon.git] / scripts / makeheader
1 #!/usr/bin/env python3
2 from __future__ import print_function
3 import re
4 import os
5 from pathlib import Path
6
7 # Expected format:
8 #     #define XF86XK_FooBar 0x1234         /* some optional comment */
9 # or:
10 #     #define XF86XK_FooBar _EVDEVK(0x123) /* some optional comment */
11 # We also need to match commented evdev entries:
12 #     /* Use: XF86XK_FooBar _EVDEVK(0x123)    some optional comment */
13 keysym_entry_pattern = re.compile(
14     r"""^
15     (?P<define>\#define|/\*\s+Use:)\s+
16     (?P<prefix>\w*)XK_(?P<name>\w+)(?P<spacing>\s+)
17     (?P<evdev>_EVDEVK\()?(?P<value>0x[0-9A-Fa-f]+)(?(evdev)\))
18     """,
19     re.VERBOSE,
20 )
21
22 # Match remaining XK_ references in the comments, e.g we will replace:
23 #       XF86XK_CamelCaseKernelName      _EVDEVK(kernel value)
24 #       #define XKB_KEY_SunCompose              0x0000FF20      /* Same as XK_Multi_key */
25 # with:
26 #       XKB_KEY_XF86CamelCaseKernelName _EVDEVK(kernel value)
27 #       #define XKB_KEY_SunCompose              0x0000FF20      /* Same as XKB_KEY_Multi_key */
28 xorgproto_keysym_prefix_pattern = re.compile(r"\b(?P<prefix>\w*)XK_(?!KOREAN\b)")
29
30
31 def make_keysym_entry(m: re.Match[str]) -> str:
32     """
33     Perform the substitutions
34     """
35     if m.group("evdev"):
36         if m.group("define").startswith("#"):
37             # Replace the xorgproto _EVDEVK macro with the actual value:
38             # 0x10081000 is the base, the evdev hex code is added to that.
39             # We replace to make parsing of the keys later easier.
40             value = 0x10081000 + int(m.group("value"), 16)
41             value_str = f"{value:#x}    "
42         else:
43             value_str = f"""_EVDEVK({m.group('value')})"""
44     else:
45         value_str = m.group("value")
46     define = m.group("define")
47     prefix = m.group("prefix") or ""
48     name = m.group("name")
49     spacing = m.group("spacing")
50     return f"""{define} XKB_KEY_{prefix}{name}{spacing}{value_str}"""
51
52
53 prefix = Path(os.environ.get("X11_HEADERS_PREFIX", "/usr"))
54 HEADERS = (
55     prefix / "include/X11/keysymdef.h",
56     prefix / "include/X11/XF86keysym.h",
57     prefix / "include/X11/Sunkeysym.h",
58     prefix / "include/X11/DECkeysym.h",
59     prefix / "include/X11/HPkeysym.h",
60 )
61
62 print(
63     """#ifndef _XKBCOMMON_KEYSYMS_H
64 #define _XKBCOMMON_KEYSYMS_H
65
66 /* This file is autogenerated; please do not commit directly. */
67
68 /**
69  * @file
70  * Key symbols (keysyms) definitions.
71  */
72
73 #define XKB_KEY_NoSymbol                    0x000000  /* Special KeySym */
74 """
75 )
76
77 for path in HEADERS:
78     with path.open("rt", encoding="utf-8") as header:
79         for line in header:
80             if "#ifdef" in line or "#ifndef" in line or "#endif" in line:
81                 continue
82
83             # Remove #define _OSF_Keysyms and such.
84             if "#define _" in line:
85                 continue
86
87             # Handle a duplicate definition in HPkeysyms.h which kicks in if
88             # it's not already defined.
89             if "XK_Ydiaeresis" in line and "0x100000ee" in line:
90                 continue
91
92             # Perform _EVDEV and XK_ substitutions
93             line = keysym_entry_pattern.sub(make_keysym_entry, line)
94             line = xorgproto_keysym_prefix_pattern.sub(r"XKB_KEY_\1", line)
95
96             print(line, end="")
97 print("\n\n#endif")