bb0db591eec6102ee87ac264822d823a5f22c47a
[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 _EVDEVK(0x123) /* some optional comment */
8 evdev_pattern = re.compile(r'^#define\s+XF86XK_(?P<name>\w+)\s+_EVDEVK\((?P<value>0x[0-9A-Fa-f]+)\)')
9
10 prefix = os.environ.get('X11_HEADERS_PREFIX', '/usr')
11 HEADERS = [
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
19 print('''#ifndef _XKBCOMMON_KEYSYMS_H
20 #define _XKBCOMMON_KEYSYMS_H
21
22 /* This file is autogenerated; please do not commit directly. */
23
24 /**
25  * @file
26  * Key symbols (keysyms) definitions.
27  */
28
29 #define XKB_KEY_NoSymbol                    0x000000  /* Special KeySym */
30 ''')
31 for path in HEADERS:
32     with open(path) as header:
33         for line in header:
34             if '#ifdef' in line or '#ifndef' in line or '#endif' in line:
35                 continue
36
37             # Remove #define _OSF_Keysyms and such.
38             if '#define _' in line:
39                 continue
40
41             # Handle a duplicate definition in HPkeysyms.h which kicks in if
42             # it's not already defined.
43             if 'XK_Ydiaeresis' in line and '0x100000ee' in line:
44                 continue
45
46             # Replace the xorgproto _EVDEVK macro with the actual value
47             # 0x10081000 is the base, the evdev hex code is added to that.
48             # We replace to make parsing of the keys later easier.
49             match = re.match(evdev_pattern, line)
50             if match:
51                 value = 0x10081000 + int(match.group('value'), 16)
52                 line = re.sub(r'_EVDEVK\(0x([0-9A-Fa-f]+)\)', '{:#x}'.format(value), line)
53
54             line = re.sub(r'#define\s*(\w*)XK_', r'#define XKB_KEY_\1', line)
55
56             print(line, end='')
57 print('\n\n#endif')