2012-10-16 08:05:33 -06:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2023-09-28 01:50:43 -06:00
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
import itertools
|
2012-10-16 08:05:33 -06:00
|
|
|
|
2021-03-28 11:22:54 -06:00
|
|
|
import perfect_hash
|
|
|
|
|
2023-09-28 01:50:43 -06:00
|
|
|
pattern = re.compile(r"^#define\s+XKB_KEY_(?P<name>\w+)\s+(?P<value>0x[0-9a-fA-F]+)\s")
|
2012-10-16 08:05:33 -06:00
|
|
|
matches = [pattern.match(line) for line in open(sys.argv[1])]
|
|
|
|
entries = [(m.group("name"), int(m.group("value"), 16)) for m in matches if m]
|
|
|
|
|
2021-03-28 11:22:54 -06:00
|
|
|
entries_isorted = sorted(entries, key=lambda e: e[0].lower())
|
|
|
|
entries_kssorted = sorted(entries, key=lambda e: e[1])
|
|
|
|
|
2023-09-28 01:50:43 -06:00
|
|
|
print(
|
|
|
|
"""
|
2013-07-17 16:56:03 -06:00
|
|
|
/**
|
|
|
|
* This file comes from libxkbcommon and was generated by makekeys.py
|
|
|
|
* You can always fetch the latest version from:
|
|
|
|
* https://raw.github.com/xkbcommon/libxkbcommon/master/src/ks_tables.h
|
|
|
|
*/
|
2023-09-28 01:50:43 -06:00
|
|
|
"""
|
|
|
|
)
|
2013-07-17 15:49:10 -06:00
|
|
|
|
2013-07-17 16:07:31 -06:00
|
|
|
entry_offsets = {}
|
|
|
|
|
2023-09-28 01:50:43 -06:00
|
|
|
print(
|
|
|
|
"""
|
2019-12-27 06:03:10 -07:00
|
|
|
#ifdef __GNUC__
|
2014-01-01 11:01:12 -07:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Woverlength-strings"
|
2019-12-27 06:03:10 -07:00
|
|
|
#endif
|
2014-01-10 15:26:58 -07:00
|
|
|
static const char *keysym_names =
|
2023-09-28 01:50:43 -06:00
|
|
|
""".strip()
|
|
|
|
)
|
2013-07-17 16:07:31 -06:00
|
|
|
offs = 0
|
2023-09-28 01:50:43 -06:00
|
|
|
for name, _ in entries_isorted:
|
2013-07-17 16:07:31 -06:00
|
|
|
entry_offsets[name] = offs
|
|
|
|
print(' "{name}\\0"'.format(name=name))
|
|
|
|
offs += len(name) + 1
|
2023-09-28 01:50:43 -06:00
|
|
|
print(
|
|
|
|
"""
|
2014-01-01 11:01:12 -07:00
|
|
|
;
|
2019-12-27 06:03:10 -07:00
|
|
|
#ifdef __GNUC__
|
2014-01-01 11:01:12 -07:00
|
|
|
#pragma GCC diagnostic pop
|
2019-12-27 06:03:10 -07:00
|
|
|
#endif
|
2023-09-28 01:50:43 -06:00
|
|
|
""".strip()
|
|
|
|
)
|
2013-07-17 16:07:31 -06:00
|
|
|
|
2021-03-28 11:22:54 -06:00
|
|
|
|
2023-09-28 01:50:43 -06:00
|
|
|
template = r"""
|
2021-03-28 11:22:54 -06:00
|
|
|
static const uint16_t keysym_name_G[] = {
|
|
|
|
$G
|
|
|
|
};
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
keysym_name_hash_f(const char *key, const char *T)
|
|
|
|
{
|
|
|
|
size_t sum = 0;
|
|
|
|
for (size_t i = 0; key[i] != '\0'; i++)
|
|
|
|
sum += T[i % $NS] * key[i];
|
|
|
|
return sum % $NG;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
keysym_name_perfect_hash(const char *key)
|
|
|
|
{
|
|
|
|
return (
|
|
|
|
keysym_name_G[keysym_name_hash_f(key, "$S1")] +
|
|
|
|
keysym_name_G[keysym_name_hash_f(key, "$S2")]
|
|
|
|
) % $NG;
|
|
|
|
}
|
2023-09-28 01:50:43 -06:00
|
|
|
"""
|
|
|
|
print(
|
|
|
|
perfect_hash.generate_code(
|
|
|
|
keys=[name for name, value in entries_isorted],
|
|
|
|
template=template,
|
|
|
|
)
|
|
|
|
)
|
2021-03-28 11:22:54 -06:00
|
|
|
|
2023-09-28 01:50:43 -06:00
|
|
|
print(
|
|
|
|
"""
|
2013-07-17 15:49:10 -06:00
|
|
|
struct name_keysym {
|
2012-10-16 08:05:33 -06:00
|
|
|
xkb_keysym_t keysym;
|
2013-07-17 16:07:31 -06:00
|
|
|
uint32_t offset;
|
2023-09-28 01:50:43 -06:00
|
|
|
};\n"""
|
|
|
|
)
|
|
|
|
|
2012-10-16 08:05:33 -06:00
|
|
|
|
2013-07-17 16:07:31 -06:00
|
|
|
def print_entries(x):
|
2023-09-28 01:50:43 -06:00
|
|
|
for name, value in x:
|
|
|
|
print(
|
|
|
|
" {{ 0x{value:08x}, {offs} }}, /* {name} */".format(
|
|
|
|
offs=entry_offsets[name], value=value, name=name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-07-17 16:07:31 -06:00
|
|
|
|
2023-09-28 01:50:43 -06:00
|
|
|
print("static const struct name_keysym name_to_keysym[] = {")
|
2021-03-28 11:22:54 -06:00
|
|
|
print_entries(entries_isorted)
|
2023-09-28 01:50:43 -06:00
|
|
|
print("};\n")
|
2012-10-16 08:05:33 -06:00
|
|
|
|
|
|
|
# *.sort() is stable so we always get the first keysym for duplicate
|
2023-09-28 01:50:43 -06:00
|
|
|
print("static const struct name_keysym keysym_to_name[] = {")
|
|
|
|
print_entries(
|
|
|
|
next(g[1]) for g in itertools.groupby(entries_kssorted, key=lambda e: e[1])
|
|
|
|
)
|
|
|
|
print("};")
|