Skip to content

Commit

Permalink
state: Avoid keycode lookup when key ref is available
Browse files Browse the repository at this point in the history
  • Loading branch information
wismill committed Dec 3, 2024
1 parent e0130e3 commit 3a54d92
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 29 deletions.
3 changes: 1 addition & 2 deletions src/keymap-priv.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,11 @@ XkbWrapGroupIntoRange(int32_t group,

unsigned int
xkb_keymap_key_get_actions_by_level(struct xkb_keymap *keymap,
xkb_keycode_t kc,
const struct xkb_key *key,
xkb_layout_index_t layout,
xkb_level_index_t level,
const union xkb_action **actions)
{
const struct xkb_key *key = XkbKey(keymap, kc);
if (!key)
goto err;

Expand Down
2 changes: 1 addition & 1 deletion src/keymap.h
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ mod_mask_get_effective(struct xkb_keymap *keymap, xkb_mod_mask_t mods);

unsigned int
xkb_keymap_key_get_actions_by_level(struct xkb_keymap *keymap,
xkb_keycode_t kc,
const struct xkb_key *key,
xkb_layout_index_t layout,
xkb_level_index_t level,
const union xkb_action **actions);
Expand Down
59 changes: 34 additions & 25 deletions src/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,23 @@ get_entry_for_key_state(struct xkb_state *state, const struct xkb_key *key,
return get_entry_for_mods(type, active_mods);
}

static inline xkb_level_index_t
_xkb_state_key_get_level(struct xkb_state *state, const struct xkb_key *key,
xkb_layout_index_t layout)
{
if (layout >= key->num_groups)
return XKB_LEVEL_INVALID;

/* If we don't find an explicit match the default is 0. */
const struct xkb_key_type_entry *entry =
get_entry_for_key_state(state, key, layout);

if (!entry)
return 0;

return entry->level;
}

/**
* Returns the level to use for the given key and state, or
* XKB_LEVEL_INVALID.
Expand All @@ -146,17 +163,19 @@ xkb_state_key_get_level(struct xkb_state *state, xkb_keycode_t kc,
xkb_layout_index_t layout)
{
const struct xkb_key *key = XkbKey(state->keymap, kc);
const struct xkb_key_type_entry *entry;

if (!key || layout >= key->num_groups)
if (!key)
return XKB_LEVEL_INVALID;

/* If we don't find an explicit match the default is 0. */
entry = get_entry_for_key_state(state, key, layout);
if (!entry)
return 0;
return _xkb_state_key_get_level(state, key, layout);
}

return entry->level;
static inline xkb_layout_index_t
_xkb_state_key_get_layout(struct xkb_state *state, const struct xkb_key *key)
{
return XkbWrapGroupIntoRange(state->components.group, key->num_groups,
key->out_of_range_group_action,
key->out_of_range_group_number);
}

/**
Expand All @@ -171,32 +190,22 @@ xkb_state_key_get_layout(struct xkb_state *state, xkb_keycode_t kc)
if (!key)
return XKB_LAYOUT_INVALID;

return XkbWrapGroupIntoRange(state->components.group, key->num_groups,
key->out_of_range_group_action,
key->out_of_range_group_number);
return _xkb_state_key_get_layout(state, key);
}

static unsigned int
xkb_key_get_actions(struct xkb_state *state, const struct xkb_key *key,
const union xkb_action **actions)
{
xkb_layout_index_t layout;
xkb_level_index_t level;

layout = xkb_state_key_get_layout(state, key->keycode);
if (layout == XKB_LAYOUT_INVALID)
goto err;

level = xkb_state_key_get_level(state, key->keycode, layout);
if (level == XKB_LEVEL_INVALID)
goto err;
xkb_layout_index_t layout = _xkb_state_key_get_layout(state, key);
xkb_level_index_t level = _xkb_state_key_get_level(state, key, layout);
if (level == XKB_LEVEL_INVALID) {
*actions = NULL;
return 0;
}

return xkb_keymap_key_get_actions_by_level(state->keymap, key->keycode,
return xkb_keymap_key_get_actions_by_level(state->keymap, key,
layout, level, actions);

err:
*actions = NULL;
return 0;
}

static struct xkb_filter *
Expand Down
2 changes: 1 addition & 1 deletion src/xkbcomp/keymap-dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ write_actions(struct xkb_keymap *keymap, struct buf *buf, struct buf *buf2,
if (level != 0)
write_buf(buf, ", ");

count = xkb_keymap_key_get_actions_by_level(keymap, key->keycode,
count = xkb_keymap_key_get_actions_by_level(keymap, key,
group, level, &actions);
buf2->size = 0;
if (count == 0) {
Expand Down

0 comments on commit 3a54d92

Please sign in to comment.