xref: /aosp_15_r20/external/libxkbcommon/scripts/makeheader (revision 2b949d0487e80d67f1fda82db69e101e761f8064)
1#!/usr/bin/env python
2from __future__ import print_function
3import re
4import os
5
6# expected format:
7# #define XF86XK_FooBar _EVDEVK(0x123) /* some optional comment */
8evdev_pattern = re.compile(r'^#define\s+XF86XK_(?P<name>\w+)\s+_EVDEVK\((?P<value>0x[0-9A-Fa-f]+)\)')
9
10prefix = os.environ.get('X11_HEADERS_PREFIX', '/usr')
11HEADERS = [
12    prefix + '/include/X11/keysymdef.h',
13    prefix + '/include/X11/XF86keysym.h',
14    prefix + '/include/X11/Sunkeysym.h',
15    prefix + '/include/X11/DECkeysym.h',
16    prefix + '/include/X11/HPkeysym.h',
17]
18
19print('''#ifndef _XKBCOMMON_KEYSYMS_H
20#define _XKBCOMMON_KEYSYMS_H
21
22/* This file is autogenerated; please do not commit directly. */
23
24#define XKB_KEY_NoSymbol                    0x000000  /* Special KeySym */
25''')
26for path in HEADERS:
27    with open(path) as header:
28        for line in header:
29            if '#ifdef' in line or '#ifndef' in line or '#endif' in line:
30                continue
31
32            # Remove #define _OSF_Keysyms and such.
33            if '#define _' in line:
34                continue
35
36            # Handle a duplicate definition in HPkeysyms.h which kicks in if
37            # it's not already defined.
38            if 'XK_Ydiaeresis' in line and '0x100000ee' in line:
39                continue
40
41            # Replace the xorgproto _EVDEVK macro with the actual value
42            # 0x10081000 is the base, the evdev hex code is added to that.
43            # We replace to make parsing of the keys later easier.
44            match = re.match(evdev_pattern, line)
45            if match:
46                value = 0x10081000 + int(match.group('value'), 16)
47                line = re.sub(r'_EVDEVK\(0x([0-9A-Fa-f]+)\)', '{:#x}'.format(value), line)
48
49            line = re.sub(r'#define\s*(\w*)XK_', r'#define XKB_KEY_\1', line)
50
51            print(line, end='')
52print('\n\n#endif')
53