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