f2738bd6a40f1c7e97803870ea733409ae2251b3
[platform/upstream/libxkbcommon.git] / scripts / makeheader
1 #!/usr/bin/env python
2 from __future__ import print_function
3 import re
4 import os
5
6 # Expected format:
7 #     #define XF86XK_FooBar 0x1234         /* some optional comment */
8 # or:
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(
13     r"""^
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)\))
17     """,
18     re.VERBOSE,
19 )
20
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 */
24 # with:
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)")
28
29
30 def make_keysym_entry(m: re.Match[str]) -> str:
31     """
32     Perform the substitutions
33     """
34     if m.group("evdev"):
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}    "
41         else:
42             value_str = f"""_EVDEVK({m.group('value')})"""
43     else:
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}"""
50
51
52 prefix = os.environ.get("X11_HEADERS_PREFIX", "/usr")
53 HEADERS = [
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",
59 ]
60
61 print(
62     """#ifndef _XKBCOMMON_KEYSYMS_H
63 #define _XKBCOMMON_KEYSYMS_H
64
65 /* This file is autogenerated; please do not commit directly. */
66
67 /**
68  * @file
69  * Key symbols (keysyms) definitions.
70  */
71
72 #define XKB_KEY_NoSymbol                    0x000000  /* Special KeySym */
73 """
74 )
75 for path in HEADERS:
76     with open(path) as header:
77         for line in header:
78             if "#ifdef" in line or "#ifndef" in line or "#endif" in line:
79                 continue
80
81             # Remove #define _OSF_Keysyms and such.
82             if "#define _" in line:
83                 continue
84
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:
88                 continue
89
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)
93
94             print(line, end="")
95 print("\n\n#endif")