From 0ce17ef3ea3722c1cfe7af38a55616afb3ba1b27 Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Wed, 20 Jan 2016 11:40:43 -0500 Subject: keymap: add xkb_keymap_key_by_name(), xkb_keymap_key_get_name(), tests xkb_keymap_key_by_name() allows finding a keycode from a given keyname and is useful for generating keyboard events to use in regression tests during CI xkb_keymap_key_get_name() is the inverse of xkb_keymap_key_by_name() Signed-off-by: Mike Blumenkrantz [ran: some stylistic tweaks + another test case] Signed-off-by: Ran Benita --- src/keymap.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'src/keymap.c') diff --git a/src/keymap.c b/src/keymap.c index a9fc9b4..859c64a 100644 --- a/src/keymap.c +++ b/src/keymap.c @@ -470,6 +470,40 @@ xkb_keymap_key_for_each(struct xkb_keymap *keymap, xkb_keymap_key_iter_t iter, iter(keymap, key->keycode, data); } +XKB_EXPORT const char * +xkb_keymap_key_get_name(struct xkb_keymap *keymap, xkb_keycode_t kc) +{ + const struct xkb_key *key = XkbKey(keymap, kc); + + if (!key) + return NULL; + + return xkb_atom_text(keymap->ctx, key->name); +} + +XKB_EXPORT xkb_keycode_t +xkb_keymap_key_by_name(struct xkb_keymap *keymap, const char *name) +{ + struct xkb_key *key; + xkb_atom_t atom; + + atom = xkb_atom_lookup(keymap->ctx, name); + if (atom) { + xkb_atom_t ratom = XkbResolveKeyAlias(keymap, atom); + if (ratom) + atom = ratom; + } + if (!atom) + return XKB_KEYCODE_INVALID; + + xkb_keys_foreach(key, keymap) { + if (key->name == atom) + return key->keycode; + } + + return XKB_KEYCODE_INVALID; +} + /** * Simple boolean specifying whether or not the key should repeat. */ -- cgit v1.2.1