3 import re, sys, itertools
7 pattern = re.compile(r'^#define\s+XKB_KEY_(?P<name>\w+)\s+(?P<value>0x[0-9a-fA-F]+)\s')
8 matches = [pattern.match(line) for line in open(sys.argv[1])]
9 entries = [(m.group("name"), int(m.group("value"), 16)) for m in matches if m]
11 entries_isorted = sorted(entries, key=lambda e: e[0].lower())
12 entries_kssorted = sorted(entries, key=lambda e: e[1])
16 * This file comes from libxkbcommon and was generated by makekeys.py
17 * You can always fetch the latest version from:
18 * https://raw.github.com/xkbcommon/libxkbcommon/master/src/ks_tables.h
26 #pragma GCC diagnostic push
27 #pragma GCC diagnostic ignored "-Woverlength-strings"
29 static const char *keysym_names =
32 for (name, _) in entries_isorted:
33 entry_offsets[name] = offs
34 print(' "{name}\\0"'.format(name=name))
39 #pragma GCC diagnostic pop
45 static const uint16_t keysym_name_G[] = {
50 keysym_name_hash_f(const char *key, const char *T)
53 for (size_t i = 0; key[i] != '\0'; i++)
54 sum += T[i % $NS] * key[i];
59 keysym_name_perfect_hash(const char *key)
62 keysym_name_G[keysym_name_hash_f(key, "$S1")] +
63 keysym_name_G[keysym_name_hash_f(key, "$S2")]
67 print(perfect_hash.generate_code(
68 keys=[name for name, value in entries_isorted],
79 for (name, value) in x:
80 print(' {{ 0x{value:08x}, {offs} }}, /* {name} */'.format(offs=entry_offsets[name], value=value, name=name))
82 print('static const struct name_keysym name_to_keysym[] = {')
83 print_entries(entries_isorted)
86 # *.sort() is stable so we always get the first keysym for duplicate
87 print('static const struct name_keysym keysym_to_name[] = {')
88 print_entries(next(g[1]) for g in itertools.groupby(entries_kssorted, key=lambda e: e[1]))