test/data: update host.xkb to match keymap-dump style
[platform/upstream/libxkbcommon.git] / scripts / makekeys
1 #!/usr/bin/env python
2
3 import re, sys, itertools
4
5 pattern = re.compile(r'^#define\s+XKB_KEY_(?P<name>\w+)\s+(?P<value>0x[0-9a-fA-F]+)\s')
6 matches = [pattern.match(line) for line in open(sys.argv[1])]
7 entries = [(m.group("name"), int(m.group("value"), 16)) for m in matches if m]
8
9 print('''
10 /**
11  * This file comes from libxkbcommon and was generated by makekeys.py
12  * You can always fetch the latest version from:
13  * https://raw.github.com/xkbcommon/libxkbcommon/master/src/ks_tables.h
14  */
15 ''')
16
17 entry_offsets = {}
18
19 print('''
20 #ifdef __GNUC__
21 #pragma GCC diagnostic push
22 #pragma GCC diagnostic ignored "-Woverlength-strings"
23 #endif
24 static const char *keysym_names =
25 '''.strip())
26 offs = 0
27 for (name, _) in sorted(entries, key=lambda e: e[0].lower()):
28     entry_offsets[name] = offs
29     print('    "{name}\\0"'.format(name=name))
30     offs += len(name) + 1
31 print('''
32 ;
33 #ifdef __GNUC__
34 #pragma GCC diagnostic pop
35 #endif
36 '''.strip())
37
38 print('''
39 struct name_keysym {
40     xkb_keysym_t keysym;
41     uint32_t offset;
42 };\n''')
43
44 def print_entries(x):
45     for (name, value) in x:
46         print('    {{ 0x{value:08x}, {offs} }}, /* {name} */'.format(offs=entry_offsets[name], value=value, name=name))
47
48 print('static const struct name_keysym name_to_keysym[] = {')
49 print_entries(sorted(entries, key=lambda e: e[0].lower()))
50 print('};\n')
51
52 # *.sort() is stable so we always get the first keysym for duplicate
53 print('static const struct name_keysym keysym_to_name[] = {')
54 print_entries(next(g[1]) for g in itertools.groupby(sorted(entries, key=lambda e: e[1]), key=lambda e: e[1]))
55 print('};')