Edit

kc3-lang/libxkbcommon/scripts/makekeys

Branch :

  • Browse

    Commit

  • Author : Ran Benita
    Date : 2021-03-28 15:51:01
    Hash : 7d84809f
    Message : keysym: fast path for case sensitive xkb_keysym_from_name xkb_keysym_from_name() is called a lot in Compose file parsing. The lower case handling slows things down a lot (particularly given we can't use the optimized strcasecmp() due to locale issues). So add separate handling for the non-case-sensitive case which is used by Compose. To do this we need to add another version of the ks_tables table. This adds ~20kb to the shared library binary. We can probably do something better here but I think it's fine. Signed-off-by: Ran Benita <ran@unusedvar.com>

  • scripts/makekeys
  • #!/usr/bin/env python
    
    import re, sys, itertools
    
    pattern = re.compile(r'^#define\s+XKB_KEY_(?P<name>\w+)\s+(?P<value>0x[0-9a-fA-F]+)\s')
    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]
    
    print('''
    /**
     * 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
     */
    ''')
    
    entry_offsets = {}
    
    print('''
    #ifdef __GNUC__
    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Woverlength-strings"
    #endif
    static const char *keysym_names =
    '''.strip())
    offs = 0
    for (name, _) in sorted(entries, key=lambda e: e[0].lower()):
        entry_offsets[name] = offs
        print('    "{name}\\0"'.format(name=name))
        offs += len(name) + 1
    print('''
    ;
    #ifdef __GNUC__
    #pragma GCC diagnostic pop
    #endif
    '''.strip())
    
    print('''
    struct name_keysym {
        xkb_keysym_t keysym;
        uint32_t offset;
    };\n''')
    
    def print_entries(x):
        for (name, value) in x:
            print('    {{ 0x{value:08x}, {offs} }}, /* {name} */'.format(offs=entry_offsets[name], value=value, name=name))
    
    print('static const struct name_keysym name_to_keysym[] = {')
    print_entries(sorted(entries, key=lambda e: e[0]))
    print('};\n')
    
    print('static const struct name_keysym name_to_keysym_icase[] = {')
    print_entries(sorted(entries, key=lambda e: e[0].lower()))
    print('};\n')
    
    # *.sort() is stable so we always get the first keysym for duplicate
    print('static const struct name_keysym keysym_to_name[] = {')
    print_entries(next(g[1]) for g in itertools.groupby(sorted(entries, key=lambda e: e[1]), key=lambda e: e[1]))
    print('};')