1#!/usr/bin/env python 2 3import re, sys, itertools 4 5pattern = re.compile(r'^#define\s+XKB_KEY_(?P<name>\w+)\s+(?P<value>0x[0-9a-fA-F]+)\s') 6matches = [pattern.match(line) for line in open(sys.argv[1])] 7entries = [(m.group("name"), int(m.group("value"), 16)) for m in matches if m] 8 9print(''' 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 17entry_offsets = {} 18 19print(''' 20#pragma GCC diagnostic push 21#pragma GCC diagnostic ignored "-Woverlength-strings" 22static const char *keysym_names = 23'''.strip()) 24offs = 0 25for (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 29print(''' 30; 31#pragma GCC diagnostic pop 32'''.strip()) 33 34print(''' 35struct name_keysym { 36 xkb_keysym_t keysym; 37 uint32_t offset; 38};\n''') 39 40def 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 44print('static const struct name_keysym name_to_keysym[] = {') 45print_entries(sorted(entries, key=lambda e: e[0].lower())) 46print('};\n') 47 48# *.sort() is stable so we always get the first keysym for duplicate 49print('static const struct name_keysym keysym_to_name[] = {') 50print_entries(next(g[1]) for g in itertools.groupby(sorted(entries, key=lambda e: e[1]), key=lambda e: e[1])) 51print('};') 52