Skip to content

Commit

Permalink
keymap: add xkb_keymap_keyname_get_keycode()
Browse files Browse the repository at this point in the history
this function allows finding a keycode from a given keyname and
is useful for generating keyboard events to use in regression tests
during CI

Signed-off-by: Mike Blumenkrantz <[email protected]>
  • Loading branch information
Mike Blumenkrantz committed Jan 20, 2016
1 parent 7f3bb16 commit 76d1c50
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,33 @@ xkb_keymap_key_for_each(struct xkb_keymap *keymap, xkb_keymap_key_iter_t iter,
iter(keymap, key->keycode, data);
}

XKB_EXPORT xkb_keycode_t
xkb_keymap_keyname_get_keycode(struct xkb_keymap *keymap, const char *keyname)
{
struct xkb_key *key;
xkb_atom_t name;

name = xkb_atom_lookup(keymap->ctx, keyname);
if (!name)
return XKB_KEYCODE_INVALID;

xkb_keys_foreach(key, keymap) {
if (key->name == name)
return key->keycode;
}

name = XkbResolveKeyAlias(keymap, name);
if (!name)
return XKB_KEYCODE_INVALID;

xkb_keys_foreach(key, keymap) {
if (key->name == name)
return key->keycode;
}

return XKB_KEYCODE_INVALID;
}

/**
* Simple boolean specifying whether or not the key should repeat.
*/
Expand Down
12 changes: 12 additions & 0 deletions xkbcommon/xkbcommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,18 @@ void
xkb_keymap_key_for_each(struct xkb_keymap *keymap, xkb_keymap_key_iter_t iter,
void *data);

/**
* Find the keycode for a given keyname in a keymap.
*
* If no corresponding key is found in the keymap, returns 0.
*
* @sa xkb_keycode_t
* @memberof xkb_keymap
* @since 0.6.0
*/
xkb_keycode_t
xkb_keymap_keyname_get_keycode(struct xkb_keymap *keymap, const char *keyname);

/**
* Get the number of modifiers in the keymap.
*
Expand Down

0 comments on commit 76d1c50

Please sign in to comment.