Edit

kc3-lang/libxkbcommon/makekeys.py

Branch :

  • Show log

    Commit

  • Author : David Herrmann
    Date : 2012-10-16 16:05:34
    Hash : 7b3bd11f
    Message : Add xkb_keysym_from_name() flags argument for case-insensitive search This adds a flags argument to xkb_keysym_from_name() so we can perform a case-insensitive search. This should really be supported as many keysyms have really weird capitalization-rules. However, as this may produce conflicts, users must be warned to only use this for fallback paths or error-recovery. This is also the reason why the internal XKB parsers still use the case-sensitive search. This also adds some test-cases so the expected results are really produced. The binary-size does _not_ change with this patch. However, case-sensitive search may be slightly slower with this patch. But this is barely measurable. [ran: use bool instead of int for icase, add a recommendation to the doc, and test a couple "thorny" cases.] Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>

  • makekeys.py
  • #!/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('''struct name_keysym {
        const char *name;
        xkb_keysym_t keysym;
    };\n''')
    
    print('static const struct name_keysym name_to_keysym[] = {');
    for (name, _) in sorted(entries, key=lambda e: e[0].lower()):
        print('    {{ "{name}", XKB_KEY_{name} }},'.format(name=name))
    print('};\n')
    
    # *.sort() is stable so we always get the first keysym for duplicate
    print('static const struct name_keysym keysym_to_name[] = {');
    for (name, _) in (next(g[1]) for g in itertools.groupby(sorted(entries, key=lambda e: e[1]), key=lambda e: e[1])):
        print('    {{ "{name}", XKB_KEY_{name} }},'.format(name=name))
    print('};')