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 #pragma GCC diagnostic push 21 #pragma GCC diagnostic ignored "-Woverlength-strings" 22 static const char *keysym_names = 23 '''.strip()) 24 offs = 0 25 for (name, _) in sorted(entries, key=lambda e: e[0].lower()): 26 entry_offsets[name] = offs 27 print(' "{name}\\0"'.format(name=name)) 28 offs += len(name) + 1 29 print(''' 30 ; 31 #pragma GCC diagnostic pop 32 '''.strip()) 33 34 print(''' 35 struct name_keysym { 36 xkb_keysym_t keysym; 37 uint32_t offset; 38 };\n''') 39 40 def print_entries(x): 41 for (name, value) in x: 42 print(' {{ 0x{value:08x}, {offs} }}, /* {name} */'.format(offs=entry_offsets[name], value=value, name=name)) 43 44 print('static const struct name_keysym name_to_keysym[] = {') 45 print_entries(sorted(entries, key=lambda e: e[0].lower())) 46 print('};\n') 47 48 # *.sort() is stable so we always get the first keysym for duplicate 49 print('static const struct name_keysym keysym_to_name[] = {') 50 print_entries(next(g[1]) for g in itertools.groupby(sorted(entries, key=lambda e: e[1]), key=lambda e: e[1])) 51 print('};') 52