From 1885ca1f2aeab6a4ee6fe89bb04f5f8b8d2bd8db Mon Sep 17 00:00:00 2001 From: Pierre Le Marre Date: Thu, 4 Jul 2024 16:22:13 +0200 Subject: [PATCH] state: Add support for group latch action Surprisingly, the latch group action was not yet implemented. Added tests to ensure we get the tricky bits right. --- src/state.c | 212 +++++++++++++++++++++++++------- test/data/rules/evdev | 6 + test/data/symbols/group | 48 ++++++++ test/keyseq.c | 264 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 489 insertions(+), 41 deletions(-) diff --git a/src/state.c b/src/state.c index 543658bef..cabc1619c 100644 --- a/src/state.c +++ b/src/state.c @@ -240,11 +240,13 @@ xkb_filter_new(struct xkb_state *state) darray_foreach(iter, state->filters) { if (iter->func) continue; + /* Use available slot */ filter = iter; break; } if (!filter) { + /* No available slot: resize the filters array */ darray_resize0(state->filters, darray_size(state->filters) + 1); filter = &darray_item(state->filters, darray_size(state->filters) -1); } @@ -270,14 +272,23 @@ enum xkb_filter_result { XKB_FILTER_CONTINUE, }; +/* Modify a group component, depending on the ACTION_ABSOLUTE_SWITCH flag */ +#define apply_group_delta(filter_, state_, component_) \ + if (filter_->action.group.flags & ACTION_ABSOLUTE_SWITCH) \ + state_->components.component_ = filter_->action.group.group; \ + else \ + state_->components.component_ += filter_->action.group.group + +#define compute_group_delta(filter_, state_) \ + (filter_->action.group.flags & ACTION_ABSOLUTE_SWITCH) \ + ? filter_->action.group.group - state_->components.base_group \ + : filter_->action.group.group + static void xkb_filter_group_set_new(struct xkb_state *state, struct xkb_filter *filter) { filter->priv = state->components.base_group; - if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH) - state->components.base_group = filter->action.group.group; - else - state->components.base_group += filter->action.group.group; + apply_group_delta(filter, state, base_group); } static bool @@ -311,10 +322,7 @@ xkb_filter_group_set_func(struct xkb_state *state, static void xkb_filter_group_lock_new(struct xkb_state *state, struct xkb_filter *filter) { - if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH) - state->components.locked_group = filter->action.group.group; - else - state->components.locked_group += filter->action.group.group; + apply_group_delta(filter, state, locked_group); } static bool @@ -337,6 +345,144 @@ xkb_filter_group_lock_func(struct xkb_state *state, return XKB_FILTER_CONTINUE; } +enum xkb_key_latch_state { + NO_LATCH, + LATCH_KEY_DOWN, + LATCH_PENDING, + _KEY_LATCH_STATE_NUM_ENTRIES +}; + +static bool +xkb_action_breaks_latch(const union xkb_action *action) +{ + switch (action->type) { + case ACTION_TYPE_NONE: + case ACTION_TYPE_PTR_BUTTON: + case ACTION_TYPE_PTR_LOCK: + case ACTION_TYPE_CTRL_SET: + case ACTION_TYPE_CTRL_LOCK: + case ACTION_TYPE_SWITCH_VT: + case ACTION_TYPE_TERMINATE: + return true; + default: + return false; + } +} + +#define MAX_XKB_KEY_LATCH_STATE_LOG2 2 +#if (_KEY_LATCH_STATE_NUM_ENTRIES > (1 << MAX_XKB_KEY_LATCH_STATE_LOG2)) || \ + (-XKB_MAX_GROUPS) < (INT32_MIN >> MAX_XKB_KEY_LATCH_STATE_LOG2) || \ + XKB_MAX_GROUPS > (INT32_MAX >> MAX_XKB_KEY_LATCH_STATE_LOG2) +#error "Cannot represent priv field of the group latch filter" +#endif + +/* Hold the latch state *and* the group delta */ +union group_latch_priv { + uint32_t priv; + struct { + /* The type is really: enum xkb_key_latch_state, but it is problematic + * on Windows, because it is interpreted as signed and leads to wrong + * negative values. */ + unsigned int latch:MAX_XKB_KEY_LATCH_STATE_LOG2; + int32_t group_delta:(32 - MAX_XKB_KEY_LATCH_STATE_LOG2); + }; +}; + +static void +xkb_filter_group_latch_new(struct xkb_state *state, struct xkb_filter *filter) +{ + const union group_latch_priv priv = { + .latch = LATCH_KEY_DOWN, + .group_delta = compute_group_delta(filter, state) + }; + filter->priv = priv.priv; + /* Like group set */ + apply_group_delta(filter, state, base_group); +} + +static bool +xkb_filter_group_latch_func(struct xkb_state *state, + struct xkb_filter *filter, + const struct xkb_key *key, + enum xkb_key_direction direction) +{ + union group_latch_priv priv = {.priv = filter->priv}; + enum xkb_key_latch_state latch = priv.latch; + + if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) { + /* If this is a new keypress and we're awaiting our single latched + * keypress, then either break the latch if any random key is pressed, + * or promote it to a lock or plain base set if it's the same + * group delta & flags. */ + const union xkb_action *action = xkb_key_get_action(state, key); + if (action->type == ACTION_TYPE_GROUP_LATCH && + action->group.group == filter->action.group.group && + action->group.flags == filter->action.group.flags) { + filter->action = *action; + if (filter->action.group.flags & ACTION_LATCH_TO_LOCK && + filter->action.group.group != 0) { + /* Promote to lock */ + filter->action.type = ACTION_TYPE_GROUP_LOCK; + filter->func = xkb_filter_group_lock_func; + xkb_filter_group_lock_new(state, filter); + } + else { + /* Degrade to plain set */ + filter->action.type = ACTION_TYPE_GROUP_SET; + filter->func = xkb_filter_group_set_func; + xkb_filter_group_set_new(state, filter); + } + filter->key = key; + state->components.latched_group -= priv.group_delta; + /* XXX beep beep! */ + return XKB_FILTER_CONSUME; + } + else if (xkb_action_breaks_latch(action)) { + /* Breaks the latch */ + state->components.latched_group = 0; + filter->func = NULL; + return XKB_FILTER_CONTINUE; + } + } + else if (direction == XKB_KEY_UP && key == filter->key) { + /* Our key got released. If we've set it to clear locks, and we + * currently have a group locked, then release it and + * don't actually latch. Else we've actually hit the latching + * stage, so set PENDING and move our group from base to + * latched. */ + if (latch == NO_LATCH || + ((filter->action.group.flags & ACTION_LOCK_CLEAR) && + state->components.locked_group)) { + if (latch == LATCH_PENDING) + state->components.latched_group -= priv.group_delta; + else + state->components.base_group -= priv.group_delta; + if (filter->action.group.flags & ACTION_LOCK_CLEAR) + state->components.locked_group = 0; + filter->func = NULL; + } + else { + latch = LATCH_PENDING; + /* Switch from set to latch */ + state->components.base_group -= priv.group_delta; + state->components.latched_group += priv.group_delta; + /* XXX beep beep! */ + } + } + else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) { + /* Another key was pressed while we've still got the latching + * key held down, so keep the base group active (from + * xkb_filter_group_latch_new), but don't trip the latch, just clear + * it as soon as the group key gets released. */ + latch = NO_LATCH; + } + + priv.latch = latch; + filter->priv = priv.priv; + + return XKB_FILTER_CONTINUE; +} + static void xkb_filter_mod_set_new(struct xkb_state *state, struct xkb_filter *filter) { @@ -404,29 +550,6 @@ xkb_filter_mod_lock_func(struct xkb_state *state, return XKB_FILTER_CONTINUE; } -enum xkb_key_latch_state { - NO_LATCH, - LATCH_KEY_DOWN, - LATCH_PENDING, -}; - -static bool -xkb_action_breaks_latch(const union xkb_action *action) -{ - switch (action->type) { - case ACTION_TYPE_NONE: - case ACTION_TYPE_PTR_BUTTON: - case ACTION_TYPE_PTR_LOCK: - case ACTION_TYPE_CTRL_SET: - case ACTION_TYPE_CTRL_LOCK: - case ACTION_TYPE_SWITCH_VT: - case ACTION_TYPE_TERMINATE: - return true; - default: - return false; - } -} - static void xkb_filter_mod_latch_new(struct xkb_state *state, struct xkb_filter *filter) { @@ -520,16 +643,18 @@ static const struct { bool (*func)(struct xkb_state *state, struct xkb_filter *filter, const struct xkb_key *key, enum xkb_key_direction direction); } filter_action_funcs[_ACTION_TYPE_NUM_ENTRIES] = { - [ACTION_TYPE_MOD_SET] = { xkb_filter_mod_set_new, - xkb_filter_mod_set_func }, - [ACTION_TYPE_MOD_LATCH] = { xkb_filter_mod_latch_new, - xkb_filter_mod_latch_func }, - [ACTION_TYPE_MOD_LOCK] = { xkb_filter_mod_lock_new, - xkb_filter_mod_lock_func }, - [ACTION_TYPE_GROUP_SET] = { xkb_filter_group_set_new, - xkb_filter_group_set_func }, - [ACTION_TYPE_GROUP_LOCK] = { xkb_filter_group_lock_new, - xkb_filter_group_lock_func }, + [ACTION_TYPE_MOD_SET] = { xkb_filter_mod_set_new, + xkb_filter_mod_set_func }, + [ACTION_TYPE_MOD_LATCH] = { xkb_filter_mod_latch_new, + xkb_filter_mod_latch_func }, + [ACTION_TYPE_MOD_LOCK] = { xkb_filter_mod_lock_new, + xkb_filter_mod_lock_func }, + [ACTION_TYPE_GROUP_SET] = { xkb_filter_group_set_new, + xkb_filter_group_set_func }, + [ACTION_TYPE_GROUP_LATCH] = { xkb_filter_group_latch_new, + xkb_filter_group_latch_func }, + [ACTION_TYPE_GROUP_LOCK] = { xkb_filter_group_lock_new, + xkb_filter_group_lock_func }, }; /** @@ -559,6 +684,7 @@ xkb_filter_apply_all(struct xkb_state *state, if (consumed || direction == XKB_KEY_UP) return; + /* No filter consumed this event, so proceed with the key action */ action = xkb_key_get_action(state, key); /* @@ -571,9 +697,11 @@ xkb_filter_apply_all(struct xkb_state *state, if (action->type >= _ACTION_TYPE_NUM_ENTRIES) return; + /* Return if no corresponding action */ if (!filter_action_funcs[action->type].new) return; + /* Add a new filter and run the corresponding initial action */ filter = xkb_filter_new(state); filter->key = key; filter->func = filter_action_funcs[action->type].func; @@ -689,12 +817,14 @@ xkb_state_update_derived(struct xkb_state *state) /* TODO: Use groups_wrap control instead of always RANGE_WRAP. */ + /* Lock group must be adjusted, but not base nor latched groups */ wrapped = XkbWrapGroupIntoRange(state->components.locked_group, state->keymap->num_groups, RANGE_WRAP, 0); state->components.locked_group = (wrapped == XKB_LAYOUT_INVALID ? 0 : wrapped); + /* Effective group must be adjusted */ wrapped = XkbWrapGroupIntoRange(state->components.base_group + state->components.latched_group + state->components.locked_group, diff --git a/test/data/rules/evdev b/test/data/rules/evdev index 32c8ed3ef..fab0fa80c 100644 --- a/test/data/rules/evdev +++ b/test/data/rules/evdev @@ -1016,6 +1016,12 @@ grp:alt_shift_toggle = +group(alt_shift_toggle) grp:alt_shift_toggle_bidir = +group(alt_shift_toggle_bidir) grp:lctrl_lwin_toggle = +group(lctrl_lwin_toggle) + grp:menu_latch_group2 = +group(menu_latch_group2) + grp:menu_latch_group2_lock = +group(menu_latch_group2_lock) + grp:menu_latch = +group(menu_latch) + grp:menu_latch_lock = +group(menu_latch_lock) + grp:menu_latch_negative = +group(menu_latch_negative) + grp:menu_latch_negative_lock = +group(menu_latch_negative_lock) lv3:switch = +level3(switch) lv3:ralt_switch = +level3(ralt_switch) lv3:ralt_switch_multikey = +level3(ralt_switch_multikey) diff --git a/test/data/symbols/group b/test/data/symbols/group index 694fc6f02..3661e7937 100644 --- a/test/data/symbols/group +++ b/test/data/symbols/group @@ -466,3 +466,51 @@ xkb_symbols "lctrl_lwin_toggle" { symbols[ Group1 ] = [ Super_L, ISO_Next_Group ] }; }; + +partial modifier_keys +xkb_symbols "menu_latch_group2" { + key { + symbols[1] = [ ISO_Group_Latch ], + actions[1] = [ LatchGroup(group=2) ] + }; +}; + +partial modifier_keys +xkb_symbols "menu_latch_group2_lock" { + key { + symbols[1] = [ ISO_Group_Latch ], + actions[1] = [ LatchGroup(group=2, latchToLock=yes) ] + }; +}; + +partial modifier_keys +xkb_symbols "menu_latch" { + key { + symbols[1] = [ ISO_Group_Latch ], + actions[1] = [ LatchGroup(group=+1) ] + }; +}; + +partial modifier_keys +xkb_symbols "menu_latch_lock" { + key { + symbols[1] = [ ISO_Group_Latch ], + actions[1] = [ LatchGroup(group=+1, latchToLock=yes) ] + }; +}; + +partial modifier_keys +xkb_symbols "menu_latch_negative" { + key { + symbols[1] = [ ISO_Group_Latch ], + actions[1] = [ LatchGroup(group=-1) ] + }; +}; + +partial modifier_keys +xkb_symbols "menu_latch_negative_lock" { + key { + symbols[1] = [ ISO_Group_Latch ], + actions[1] = [ LatchGroup(group=-1, latchToLock=yes) ] + }; +}; diff --git a/test/keyseq.c b/test/keyseq.c index 53de4232e..17d133565 100644 --- a/test/keyseq.c +++ b/test/keyseq.c @@ -26,6 +26,267 @@ #include "evdev-scancodes.h" #include "test.h" +static void +test_group_latch(struct xkb_context *ctx) +{ + /* Absolute group, no lock */ + struct xkb_keymap *keymap = test_compile_rules( + ctx, "evdev", "evdev", + "us,il,ru,de", ",,phonetic,neo", + "grp:menu_latch_group2,grp:sclk_toggle"); + assert(keymap); + + /* Set only */ +#define test_set_only(keymap_) \ + assert(test_key_seq(keymap_, \ + KEY_H, BOTH, XKB_KEY_h, NEXT, \ + KEY_E, BOTH, XKB_KEY_e, NEXT, \ + KEY_COMPOSE, DOWN, XKB_KEY_ISO_Group_Latch, NEXT, \ + KEY_H, BOTH, XKB_KEY_hebrew_yod, NEXT, \ + KEY_E, BOTH, XKB_KEY_hebrew_qoph, NEXT, \ + KEY_COMPOSE, UP, XKB_KEY_ISO_Group_Latch, NEXT, \ + /* Lock the second group */ \ + KEY_SCROLLLOCK, BOTH, XKB_KEY_ISO_Next_Group, NEXT, \ + KEY_H, BOTH, XKB_KEY_hebrew_yod, NEXT, \ + KEY_E, BOTH, XKB_KEY_hebrew_qoph, NEXT, \ + KEY_COMPOSE, DOWN, XKB_KEY_ISO_Group_Latch, NEXT, \ + /* Event if the latch group is absolute, it sums with \ + * the locked group (see spec) */ \ + KEY_H, BOTH, XKB_KEY_Cyrillic_ha, NEXT, \ + KEY_E, BOTH, XKB_KEY_Cyrillic_ie, NEXT, \ + KEY_COMPOSE, UP, XKB_KEY_ISO_Group_Latch, NEXT, \ + KEY_H, BOTH, XKB_KEY_hebrew_yod, NEXT, \ + KEY_E, BOTH, XKB_KEY_hebrew_qoph, FINISH)) + test_set_only(keymap); + + /* Latch only */ +#define test_latch_only(keymap_) \ + assert(test_key_seq(keymap_, \ + KEY_H, BOTH, XKB_KEY_h, NEXT, \ + KEY_COMPOSE, BOTH, XKB_KEY_ISO_Group_Latch, NEXT, \ + KEY_H, BOTH, XKB_KEY_hebrew_yod, NEXT, \ + KEY_H, BOTH, XKB_KEY_h, NEXT, \ + /* Lock the second group */ \ + KEY_SCROLLLOCK, BOTH, XKB_KEY_ISO_Next_Group, NEXT, \ + KEY_H, BOTH, XKB_KEY_hebrew_yod, NEXT, \ + KEY_E, BOTH, XKB_KEY_hebrew_qoph, NEXT, \ + KEY_COMPOSE, BOTH, XKB_KEY_ISO_Group_Latch, NEXT, \ + /* Event if the latch group is absolute, it sums with \ + * the locked group (see spec) */ \ + KEY_H, BOTH, XKB_KEY_Cyrillic_ha, NEXT, \ + KEY_H, BOTH, XKB_KEY_hebrew_yod, FINISH)) + test_latch_only(keymap); + + /* Latch not broken by modifier */ +#define test_latch_not_broken_by_modifier(keymap_) \ + assert(test_key_seq(keymap_, \ + KEY_H, BOTH, XKB_KEY_h, NEXT, \ + KEY_COMPOSE, BOTH, XKB_KEY_ISO_Group_Latch, NEXT, \ + KEY_LEFTALT, DOWN, XKB_KEY_Alt_L, NEXT, \ + KEY_H, BOTH, XKB_KEY_hebrew_yod, NEXT, \ + KEY_H, BOTH, XKB_KEY_h, FINISH)) + test_latch_not_broken_by_modifier(keymap); + + /* Mo lock */ +#define test_no_latch_to_lock(keymap_) \ + assert(test_key_seq(keymap_, \ + KEY_H, BOTH, XKB_KEY_h, NEXT, \ + /* No latch-to-lock */ \ + KEY_COMPOSE, BOTH, XKB_KEY_ISO_Group_Latch, NEXT, \ + KEY_COMPOSE, BOTH, XKB_KEY_ISO_Group_Latch, NEXT, \ + KEY_H, BOTH, XKB_KEY_h, NEXT, \ + KEY_E, BOTH, XKB_KEY_e, NEXT, \ + /* Lock the second group */ \ + KEY_SCROLLLOCK, BOTH, XKB_KEY_ISO_Next_Group, NEXT, \ + KEY_H, BOTH, XKB_KEY_hebrew_yod, NEXT, \ + KEY_E, BOTH, XKB_KEY_hebrew_qoph, NEXT, \ + /* No latch-to-lock */ \ + KEY_COMPOSE, BOTH, XKB_KEY_ISO_Group_Latch, NEXT, \ + KEY_COMPOSE, BOTH, XKB_KEY_ISO_Group_Latch, NEXT, \ + KEY_H, BOTH, XKB_KEY_hebrew_yod, NEXT, \ + KEY_E, BOTH, XKB_KEY_hebrew_qoph, FINISH)) + test_no_latch_to_lock(keymap); + + xkb_keymap_unref(keymap); + + /* Absolute group, latch-to-lock */ + keymap = test_compile_rules( + ctx, "evdev", "evdev", + "us,il,ru,de", ",,phonetic,neo", + "grp:menu_latch_group2_lock,grp:sclk_toggle"); + assert(keymap); + + test_set_only(keymap); + test_latch_only(keymap); + test_latch_not_broken_by_modifier(keymap); + + /* Lock */ + assert(test_key_seq(keymap, + KEY_H, BOTH, XKB_KEY_h, NEXT, + /* Lock the second group via latch-to-lock */ + KEY_COMPOSE, BOTH, XKB_KEY_ISO_Group_Latch, NEXT, + KEY_COMPOSE, BOTH, XKB_KEY_ISO_Group_Latch, NEXT, + KEY_H, BOTH, XKB_KEY_hebrew_yod, NEXT, + KEY_E, BOTH, XKB_KEY_hebrew_qoph, NEXT, + /* Lock the third group via usual lock */ + KEY_SCROLLLOCK, BOTH, XKB_KEY_ISO_Next_Group, NEXT, + KEY_H, BOTH, XKB_KEY_Cyrillic_ha, NEXT, + KEY_E, BOTH, XKB_KEY_Cyrillic_ie, NEXT, + /* Lock the second group via latch-to-lock */ + KEY_COMPOSE, BOTH, XKB_KEY_ISO_Group_Latch, NEXT, + KEY_COMPOSE, BOTH, XKB_KEY_ISO_Group_Latch, NEXT, + KEY_H, BOTH, XKB_KEY_hebrew_yod, NEXT, + KEY_E, BOTH, XKB_KEY_hebrew_qoph, FINISH)); + + xkb_keymap_unref(keymap); + + /* Relative group (positive), no lock */ + keymap = test_compile_rules( + ctx, "evdev", "evdev", + "us,il,ru,de", ",,phonetic,neo", + "grp:menu_latch,grp:sclk_toggle"); + assert(keymap); + + test_set_only(keymap); + test_latch_only(keymap); + test_latch_not_broken_by_modifier(keymap); + test_no_latch_to_lock(keymap); + + xkb_keymap_unref(keymap); + + /* Relative group (positive), latch-to-lock */ + keymap = test_compile_rules( + ctx, "evdev", "evdev", + "us,il,ru,de", ",,phonetic,neo", + "grp:menu_latch_lock,grp:sclk_toggle"); + assert(keymap); + + test_set_only(keymap); + test_latch_only(keymap); + test_latch_not_broken_by_modifier(keymap); + + /* Lock */ + assert(test_key_seq(keymap, + KEY_H, BOTH, XKB_KEY_h, NEXT, + /* Lock the second group via latch-to-lock */ + KEY_COMPOSE, BOTH, XKB_KEY_ISO_Group_Latch, NEXT, + KEY_COMPOSE, BOTH, XKB_KEY_ISO_Group_Latch, NEXT, + KEY_H, BOTH, XKB_KEY_hebrew_yod, NEXT, + KEY_E, BOTH, XKB_KEY_hebrew_qoph, NEXT, + /* Lock the third group via usual lock */ + KEY_SCROLLLOCK, BOTH, XKB_KEY_ISO_Next_Group, NEXT, + KEY_H, BOTH, XKB_KEY_Cyrillic_ha, NEXT, + KEY_E, BOTH, XKB_KEY_Cyrillic_ie, NEXT, + /* Lock the fourth group via latch-to-lock */ + KEY_COMPOSE, BOTH, XKB_KEY_ISO_Group_Latch, NEXT, + KEY_COMPOSE, BOTH, XKB_KEY_ISO_Group_Latch, NEXT, + KEY_H, BOTH, XKB_KEY_s, NEXT, + KEY_E, BOTH, XKB_KEY_l, FINISH)); + + xkb_keymap_unref(keymap); + +#undef test_set_only +#undef test_latch_only +#undef test_latch_not_broken_by_modifier + + /* Relative group (negative), no lock */ + keymap = test_compile_rules( + ctx, "evdev", "evdev", + "us,il,ru,de", ",,phonetic,neo", + "grp:menu_latch_negative,grp:sclk_toggle"); + assert(keymap); + + /* Set only */ +#define test_set_only(keymap_) \ + assert(test_key_seq(keymap_, \ + KEY_H, BOTH, XKB_KEY_h, NEXT, \ + KEY_E, BOTH, XKB_KEY_e, NEXT, \ + KEY_COMPOSE, DOWN, XKB_KEY_ISO_Group_Latch, NEXT, \ + KEY_H, BOTH, XKB_KEY_s, NEXT, \ + KEY_E, BOTH, XKB_KEY_l, NEXT, \ + KEY_COMPOSE, UP, XKB_KEY_ISO_Group_Latch, NEXT, \ + KEY_H, BOTH, XKB_KEY_h, NEXT, \ + /* Lock the second group */ \ + KEY_SCROLLLOCK, BOTH, XKB_KEY_ISO_Next_Group, NEXT, \ + KEY_H, BOTH, XKB_KEY_hebrew_yod, NEXT, \ + KEY_E, BOTH, XKB_KEY_hebrew_qoph, NEXT, \ + KEY_COMPOSE, DOWN, XKB_KEY_ISO_Group_Latch, NEXT, \ + KEY_H, BOTH, XKB_KEY_h, NEXT, \ + KEY_E, BOTH, XKB_KEY_e, NEXT, \ + KEY_COMPOSE, UP, XKB_KEY_ISO_Group_Latch, NEXT, \ + KEY_H, BOTH, XKB_KEY_hebrew_yod, NEXT, \ + KEY_E, BOTH, XKB_KEY_hebrew_qoph, FINISH)) + test_set_only(keymap); + + /* Latch only */ +#define test_latch_only(keymap_) \ + assert(test_key_seq(keymap_, \ + KEY_H, BOTH, XKB_KEY_h, NEXT, \ + KEY_COMPOSE, BOTH, XKB_KEY_ISO_Group_Latch, NEXT, \ + KEY_H, BOTH, XKB_KEY_s, NEXT, \ + KEY_H, BOTH, XKB_KEY_h, NEXT, \ + /* Lock the second group */ \ + KEY_SCROLLLOCK, BOTH, XKB_KEY_ISO_Next_Group, NEXT, \ + KEY_H, BOTH, XKB_KEY_hebrew_yod, NEXT, \ + KEY_E, BOTH, XKB_KEY_hebrew_qoph, NEXT, \ + KEY_COMPOSE, BOTH, XKB_KEY_ISO_Group_Latch, NEXT, \ + KEY_H, BOTH, XKB_KEY_h, NEXT, \ + KEY_H, BOTH, XKB_KEY_hebrew_yod, FINISH)) + test_latch_only(keymap); + + /* Latch not broken by modifier */ +#define test_latch_not_broken_by_modifier(keymap_) \ + assert(test_key_seq(keymap_, \ + KEY_H, BOTH, XKB_KEY_h, NEXT, \ + KEY_COMPOSE, BOTH, XKB_KEY_ISO_Group_Latch, NEXT, \ + KEY_LEFTALT, DOWN, XKB_KEY_Alt_L, NEXT, \ + KEY_H, BOTH, XKB_KEY_s, NEXT, \ + KEY_H, BOTH, XKB_KEY_h, FINISH)) + test_latch_not_broken_by_modifier(keymap); + + test_no_latch_to_lock(keymap); + + xkb_keymap_unref(keymap); + + /* Relative group (negative), no lock */ + keymap = test_compile_rules( + ctx, "evdev", "evdev", + "us,il,ru,de", ",,phonetic,neo", + "grp:menu_latch_negative_lock,grp:sclk_toggle"); + assert(keymap); + + test_set_only(keymap); + test_latch_only(keymap); + test_latch_not_broken_by_modifier(keymap); + + /* Lock */ + assert(test_key_seq(keymap, + KEY_H, BOTH, XKB_KEY_h, NEXT, + /* Lock the fourth group via latch-to-lock */ + KEY_COMPOSE, BOTH, XKB_KEY_ISO_Group_Latch, NEXT, + KEY_COMPOSE, BOTH, XKB_KEY_ISO_Group_Latch, NEXT, + KEY_H, BOTH, XKB_KEY_s, NEXT, + KEY_E, BOTH, XKB_KEY_l, NEXT, + /* Lock the third group via usual lock */ + KEY_SCROLLLOCK, BOTH, XKB_KEY_ISO_Next_Group, NEXT, + KEY_SCROLLLOCK, BOTH, XKB_KEY_ISO_Next_Group, NEXT, + KEY_SCROLLLOCK, BOTH, XKB_KEY_ISO_Next_Group, NEXT, + KEY_H, BOTH, XKB_KEY_Cyrillic_ha, NEXT, + KEY_E, BOTH, XKB_KEY_Cyrillic_ie, NEXT, + /* Lock the second group via latch-to-lock */ + KEY_COMPOSE, BOTH, XKB_KEY_ISO_Group_Latch, NEXT, + KEY_COMPOSE, BOTH, XKB_KEY_ISO_Group_Latch, NEXT, + KEY_H, BOTH, XKB_KEY_hebrew_yod, NEXT, + KEY_E, BOTH, XKB_KEY_hebrew_qoph, FINISH)); + + xkb_keymap_unref(keymap); + +#undef test_set_only +#undef test_latch_only +#undef test_latch_not_broken_by_modifier +#undef test_no_latch_to_lock +} + int main(void) { @@ -35,6 +296,9 @@ main(void) struct xkb_keymap *keymap; assert(ctx); + + test_group_latch(ctx); + keymap = test_compile_rules(ctx, "evdev", "evdev", "us,il,ru,de", ",,phonetic,neo", "grp:alt_shift_toggle,grp:menu_toggle");