keymap/x11: Check for the group earlier when finding keycode from keyval

This effectively changes meta_keymap_x11_get_entries_for_keyval() to
meta_keymap_x11_get_entry_for_keyval() and moves the check if the
keycode maps to the keyval in the current group there. This simplifies
the code a bit and will allow a followup fix.

As a side effect this now also causes the reserved kecodes to be
searched, if no keycodes were found, rather than just when only ones
matching the wrong groups.

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1955>
This commit is contained in:
Sebastian Keller 2021-08-04 18:09:02 +02:00 committed by Marge Bot
parent 9179d4a4b1
commit bb24e4ac9e

View File

@ -705,19 +705,15 @@ meta_keymap_x11_get_is_modifier (MetaKeymapX11 *keymap,
} }
static gboolean static gboolean
meta_keymap_x11_get_entries_for_keyval (MetaKeymapX11 *keymap_x11, meta_keymap_x11_get_entry_for_keyval (MetaKeymapX11 *keymap_x11,
uint32_t keyval, uint32_t keyval,
ClutterKeymapKey **keys, uint32_t target_group,
int *n_keys) ClutterKeymapKey *key)
{ {
if (keymap_x11->use_xkb) if (keymap_x11->use_xkb)
{ {
XkbDescRec *xkb = get_xkb (keymap_x11); XkbDescRec *xkb = get_xkb (keymap_x11);
GArray *retval; int keycode = keymap_x11->min_keycode;
int keycode;
keycode = keymap_x11->min_keycode;
retval = g_array_new (FALSE, FALSE, sizeof (ClutterKeymapKey));
while (keycode <= keymap_x11->max_keycode) while (keycode <= keymap_x11->max_keycode)
{ {
@ -738,18 +734,16 @@ meta_keymap_x11_get_entries_for_keyval (MetaKeymapX11 *keymap_x11,
{ {
g_assert (i == (group * max_shift_levels + level)); g_assert (i == (group * max_shift_levels + level));
if (entry[i] == keyval) if (entry[i] == keyval && group == target_group)
{ {
ClutterKeymapKey key; key->keycode = keycode;
key->group = group;
key.keycode = keycode; key->level = level;
key.group = group;
key.level = level;
g_array_append_val (retval, key);
g_assert (XkbKeySymEntry (xkb, keycode, level, group) == g_assert (XkbKeySymEntry (xkb, keycode, level, group) ==
keyval); keyval);
return TRUE;
} }
++level; ++level;
@ -766,20 +760,7 @@ meta_keymap_x11_get_entries_for_keyval (MetaKeymapX11 *keymap_x11,
++keycode; ++keycode;
} }
if (retval->len > 0) return FALSE;
{
*keys = (ClutterKeymapKey*) retval->data;
*n_keys = retval->len;
}
else
{
*keys = NULL;
*n_keys = 0;
}
g_array_free (retval, retval->len > 0 ? FALSE : TRUE);
return *n_keys > 0;
} }
else else
{ {
@ -902,8 +883,8 @@ meta_keymap_x11_keycode_for_keyval (MetaKeymapX11 *keymap_x11,
uint32_t *keycode_out, uint32_t *keycode_out,
uint32_t *level_out) uint32_t *level_out)
{ {
ClutterKeymapKey *keys; ClutterKeymapKey key;
int i, n_keys, group; int group;
gboolean found = FALSE; gboolean found = FALSE;
g_return_val_if_fail (keycode_out != NULL, FALSE); g_return_val_if_fail (keycode_out != NULL, FALSE);
@ -911,20 +892,13 @@ meta_keymap_x11_keycode_for_keyval (MetaKeymapX11 *keymap_x11,
group = meta_keymap_x11_get_current_group (keymap_x11); group = meta_keymap_x11_get_current_group (keymap_x11);
if (!meta_keymap_x11_get_entries_for_keyval (keymap_x11, keyval, &keys, &n_keys)) if (meta_keymap_x11_get_entry_for_keyval (keymap_x11, keyval, group, &key))
return FALSE;
for (i = 0; i < n_keys && !found; i++)
{ {
if (keys[i].group == group) *keycode_out = key.keycode;
{ *level_out = key.level;
*keycode_out = keys[i].keycode; found = TRUE;
*level_out = keys[i].level;
found = TRUE;
}
} }
else
if (!found)
{ {
GHashTableIter iter; GHashTableIter iter;
gpointer key, value; gpointer key, value;
@ -944,6 +918,5 @@ meta_keymap_x11_keycode_for_keyval (MetaKeymapX11 *keymap_x11,
} }
} }
g_free (keys);
return found; return found;
} }