1 /************************************************************
2 * Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
4 * Permission to use, copy, modify, and distribute this
5 * software and its documentation for any purpose and without
6 * fee is hereby granted, provided that the above copyright
7 * notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting
9 * documentation, and that the name of Silicon Graphics not be
10 * used in advertising or publicity pertaining to distribution
11 * of the software without specific prior written permission.
12 * Silicon Graphics makes no representation about the suitability
13 * of this software for any purpose. It is provided "as is"
14 * without any express or implied warranty.
16 * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
23 * THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 ********************************************************/
28 * Copyright © 2012 Intel Corporation
29 * Copyright © 2012 Ran Benita <ran234@gmail.com>
31 * Permission is hereby granted, free of charge, to any person obtaining a
32 * copy of this software and associated documentation files (the "Software"),
33 * to deal in the Software without restriction, including without limitation
34 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
35 * and/or sell copies of the Software, and to permit persons to whom the
36 * Software is furnished to do so, subject to the following conditions:
38 * The above copyright notice and this permission notice (including the next
39 * paragraph) shall be included in all copies or substantial portions of the
42 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
45 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48 * DEALINGS IN THE SOFTWARE.
50 * Author: Daniel Stone <daniel@fooishbar.org>
54 * This is a bastardised version of xkbActions.c from the X server which
55 * does not support, for the moment:
56 * - AccessX sticky/debounce/etc (will come later)
57 * - pointer keys (may come later)
58 * - key redirects (unlikely)
59 * - messages (very unlikely)
66 union xkb_action action;
67 const struct xkb_key *key;
69 bool (*func)(struct xkb_state *state,
70 struct xkb_filter *filter,
71 const struct xkb_key *key,
72 enum xkb_key_direction direction);
76 struct state_components {
77 /* These may be negative, because of -1 group actions. */
78 int32_t base_group; /**< depressed */
79 int32_t latched_group;
81 xkb_layout_index_t group; /**< effective */
83 xkb_mod_mask_t base_mods; /**< depressed */
84 xkb_mod_mask_t latched_mods;
85 xkb_mod_mask_t locked_mods;
86 xkb_mod_mask_t mods; /**< effective */
93 * Before updating the state, we keep a copy of just this struct. This
94 * allows us to report which components of the state have changed.
96 struct state_components components;
99 * At each event, we accumulate all the needed modifications to the base
100 * modifiers, and apply them at the end. These keep track of this state.
102 xkb_mod_mask_t set_mods;
103 xkb_mod_mask_t clear_mods;
106 * We mustn't clear a base modifier if there's another depressed key
107 * which affects it, e.g. given this sequence
108 * < Left Shift down, Right Shift down, Left Shift Up >
109 * the modifier should still be set. This keeps the count.
111 int16_t mod_key_count[XKB_MAX_MODS];
114 darray(struct xkb_filter) filters;
115 struct xkb_keymap *keymap;
118 static const struct xkb_key_type_entry *
119 get_entry_for_key_state(struct xkb_state *state, const struct xkb_key *key,
120 xkb_layout_index_t group)
122 const struct xkb_key_type *type = key->groups[group].type;
123 xkb_mod_mask_t active_mods = state->components.mods & type->mods.mask;
125 for (unsigned i = 0; i < type->num_entries; i++) {
127 * If the virtual modifiers are not bound to anything, we're
128 * supposed to skip the entry (xserver does this with cached
129 * entry->active field).
131 if (!type->entries[i].mods.mask)
134 if (type->entries[i].mods.mask == active_mods)
135 return &type->entries[i];
142 * Returns the level to use for the given key and state, or
145 XKB_EXPORT xkb_level_index_t
146 xkb_state_key_get_level(struct xkb_state *state, xkb_keycode_t kc,
147 xkb_layout_index_t layout)
149 const struct xkb_key *key = XkbKey(state->keymap, kc);
150 const struct xkb_key_type_entry *entry;
152 if (!key || layout >= key->num_groups)
153 return XKB_LEVEL_INVALID;
155 /* If we don't find an explicit match the default is 0. */
156 entry = get_entry_for_key_state(state, key, layout);
164 wrap_group_into_range(int32_t group,
165 xkb_layout_index_t num_groups,
166 enum xkb_range_exceed_type out_of_range_group_action,
167 xkb_layout_index_t out_of_range_group_number)
170 return XKB_LAYOUT_INVALID;
172 if (group >= 0 && (xkb_layout_index_t) group < num_groups)
175 switch (out_of_range_group_action) {
177 if (out_of_range_group_number >= num_groups)
179 return out_of_range_group_number;
185 return num_groups - 1;
190 * C99 says a negative dividend in a modulo operation always
191 * gives a negative result.
194 return ((int) num_groups + (group % (int) num_groups));
196 return group % num_groups;
201 * Returns the layout to use for the given key and state, taking
202 * wrapping/clamping/etc into account, or XKB_LAYOUT_INVALID.
204 XKB_EXPORT xkb_layout_index_t
205 xkb_state_key_get_layout(struct xkb_state *state, xkb_keycode_t kc)
207 const struct xkb_key *key = XkbKey(state->keymap, kc);
210 return XKB_LAYOUT_INVALID;
212 return wrap_group_into_range(state->components.group, key->num_groups,
213 key->out_of_range_group_action,
214 key->out_of_range_group_number);
217 static const union xkb_action fake = { .type = ACTION_TYPE_NONE };
219 static const union xkb_action *
220 xkb_key_get_action(struct xkb_state *state, const struct xkb_key *key)
222 xkb_layout_index_t layout;
223 xkb_level_index_t level;
225 layout = xkb_state_key_get_layout(state, key->keycode);
226 if (layout == XKB_LAYOUT_INVALID)
229 level = xkb_state_key_get_level(state, key->keycode, layout);
230 if (level == XKB_LEVEL_INVALID)
233 return &key->groups[layout].levels[level].action;
236 static struct xkb_filter *
237 xkb_filter_new(struct xkb_state *state)
239 struct xkb_filter *filter = NULL, *iter;
241 darray_foreach(iter, state->filters) {
249 darray_resize0(state->filters, darray_size(state->filters) + 1);
250 filter = &darray_item(state->filters, darray_size(state->filters) -1);
257 /***====================================================================***/
260 xkb_filter_group_set_func(struct xkb_state *state,
261 struct xkb_filter *filter,
262 const struct xkb_key *key,
263 enum xkb_key_direction direction)
265 if (key != filter->key) {
266 filter->action.group.flags &= ~ACTION_LOCK_CLEAR;
270 if (direction == XKB_KEY_DOWN) {
274 else if (--filter->refcnt > 0) {
278 state->components.base_group = filter->priv;
280 if (filter->action.group.flags & ACTION_LOCK_CLEAR)
281 state->components.locked_group = 0;
288 xkb_filter_group_set_new(struct xkb_state *state, struct xkb_filter *filter)
290 filter->priv = state->components.base_group;
291 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
292 state->components.base_group = filter->action.group.group;
294 state->components.base_group += filter->action.group.group;
298 xkb_filter_group_lock_func(struct xkb_state *state,
299 struct xkb_filter *filter,
300 const struct xkb_key *key,
301 enum xkb_key_direction direction)
303 if (key != filter->key)
306 if (direction == XKB_KEY_DOWN) {
310 if (--filter->refcnt > 0)
318 xkb_filter_group_lock_new(struct xkb_state *state, struct xkb_filter *filter)
320 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
321 state->components.locked_group = filter->action.group.group;
323 state->components.locked_group += filter->action.group.group;
327 xkb_filter_mod_set_func(struct xkb_state *state,
328 struct xkb_filter *filter,
329 const struct xkb_key *key,
330 enum xkb_key_direction direction)
332 if (key != filter->key) {
333 filter->action.mods.flags &= ~ACTION_LOCK_CLEAR;
337 if (direction == XKB_KEY_DOWN) {
341 else if (--filter->refcnt > 0) {
345 state->clear_mods = filter->action.mods.mods.mask;
346 if (filter->action.mods.flags & ACTION_LOCK_CLEAR)
347 state->components.locked_mods &= ~filter->action.mods.mods.mask;
354 xkb_filter_mod_set_new(struct xkb_state *state, struct xkb_filter *filter)
356 state->set_mods = filter->action.mods.mods.mask;
360 xkb_filter_mod_lock_func(struct xkb_state *state,
361 struct xkb_filter *filter,
362 const struct xkb_key *key,
363 enum xkb_key_direction direction)
365 if (key != filter->key)
368 if (direction == XKB_KEY_DOWN) {
372 if (--filter->refcnt > 0)
375 state->clear_mods |= filter->action.mods.mods.mask;
376 if (!(filter->action.mods.flags & ACTION_LOCK_NO_UNLOCK))
377 state->components.locked_mods &= ~filter->priv;
384 xkb_filter_mod_lock_new(struct xkb_state *state, struct xkb_filter *filter)
386 filter->priv = (state->components.locked_mods &
387 filter->action.mods.mods.mask);
388 state->set_mods |= filter->action.mods.mods.mask;
389 if (!(filter->action.mods.flags & ACTION_LOCK_NO_LOCK))
390 state->components.locked_mods |= filter->action.mods.mods.mask;
393 enum xkb_key_latch_state {
400 xkb_action_breaks_latch(const union xkb_action *action)
402 switch (action->type) {
403 case ACTION_TYPE_NONE:
404 case ACTION_TYPE_PTR_BUTTON:
405 case ACTION_TYPE_PTR_LOCK:
406 case ACTION_TYPE_CTRL_SET:
407 case ACTION_TYPE_CTRL_LOCK:
408 case ACTION_TYPE_SWITCH_VT:
409 case ACTION_TYPE_TERMINATE:
417 xkb_filter_mod_latch_func(struct xkb_state *state,
418 struct xkb_filter *filter,
419 const struct xkb_key *key,
420 enum xkb_key_direction direction)
422 enum xkb_key_latch_state latch = filter->priv;
424 if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
425 /* If this is a new keypress and we're awaiting our single latched
426 * keypress, then either break the latch if any random key is pressed,
427 * or promote it to a lock or plain base set if it's the same
429 const union xkb_action *action = xkb_key_get_action(state, key);
430 if (action->type == ACTION_TYPE_MOD_LATCH &&
431 action->mods.flags == filter->action.mods.flags &&
432 action->mods.mods.mask == filter->action.mods.mods.mask) {
433 filter->action = *action;
434 if (filter->action.mods.flags & ACTION_LATCH_TO_LOCK) {
435 filter->action.type = ACTION_TYPE_MOD_LOCK;
436 filter->func = xkb_filter_mod_lock_func;
437 state->components.locked_mods |= filter->action.mods.mods.mask;
440 filter->action.type = ACTION_TYPE_MOD_SET;
441 filter->func = xkb_filter_mod_set_func;
442 state->set_mods = filter->action.mods.mods.mask;
445 state->components.latched_mods &= ~filter->action.mods.mods.mask;
449 else if (xkb_action_breaks_latch(action)) {
450 /* XXX: This may be totally broken, we might need to break the
451 * latch in the next run after this press? */
452 state->components.latched_mods &= ~filter->action.mods.mods.mask;
457 else if (direction == XKB_KEY_UP && key == filter->key) {
458 /* Our key got released. If we've set it to clear locks, and we
459 * currently have the same modifiers locked, then release them and
460 * don't actually latch. Else we've actually hit the latching
461 * stage, so set PENDING and move our modifier from base to
463 if (latch == NO_LATCH ||
464 ((filter->action.mods.flags & ACTION_LOCK_CLEAR) &&
465 (state->components.locked_mods & filter->action.mods.mods.mask) ==
466 filter->action.mods.mods.mask)) {
467 /* XXX: We might be a bit overenthusiastic about clearing
468 * mods other filters have set here? */
469 if (latch == LATCH_PENDING)
470 state->components.latched_mods &=
471 ~filter->action.mods.mods.mask;
473 state->clear_mods = filter->action.mods.mods.mask;
474 state->components.locked_mods &= ~filter->action.mods.mods.mask;
478 latch = LATCH_PENDING;
479 state->clear_mods = filter->action.mods.mods.mask;
480 state->components.latched_mods |= filter->action.mods.mods.mask;
484 else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
485 /* Someone's pressed another key while we've still got the latching
486 * key held down, so keep the base modifier state active (from
487 * xkb_filter_mod_latch_new), but don't trip the latch, just clear
488 * it as soon as the modifier gets released. */
492 filter->priv = latch;
498 xkb_filter_mod_latch_new(struct xkb_state *state, struct xkb_filter *filter)
500 filter->priv = LATCH_KEY_DOWN;
501 state->set_mods = filter->action.mods.mods.mask;
504 static const struct {
505 void (*new)(struct xkb_state *state, struct xkb_filter *filter);
506 bool (*func)(struct xkb_state *state, struct xkb_filter *filter,
507 const struct xkb_key *key, enum xkb_key_direction direction);
508 } filter_action_funcs[_ACTION_TYPE_NUM_ENTRIES] = {
509 [ACTION_TYPE_MOD_SET] = { xkb_filter_mod_set_new,
510 xkb_filter_mod_set_func },
511 [ACTION_TYPE_MOD_LATCH] = { xkb_filter_mod_latch_new,
512 xkb_filter_mod_latch_func },
513 [ACTION_TYPE_MOD_LOCK] = { xkb_filter_mod_lock_new,
514 xkb_filter_mod_lock_func },
515 [ACTION_TYPE_GROUP_SET] = { xkb_filter_group_set_new,
516 xkb_filter_group_set_func },
517 [ACTION_TYPE_GROUP_LOCK] = { xkb_filter_group_lock_new,
518 xkb_filter_group_lock_func },
522 * Applies any relevant filters to the key, first from the list of filters
523 * that are currently active, then if no filter has claimed the key, possibly
524 * apply a new filter from the key action.
527 xkb_filter_apply_all(struct xkb_state *state,
528 const struct xkb_key *key,
529 enum xkb_key_direction direction)
531 struct xkb_filter *filter;
532 const union xkb_action *action;
535 /* First run through all the currently active filters and see if any of
536 * them have claimed this event. */
537 darray_foreach(filter, state->filters) {
540 send = filter->func(state, filter, key, direction) && send;
543 if (!send || direction == XKB_KEY_UP)
546 action = xkb_key_get_action(state, key);
549 * It's possible for the keymap to set action->type explicitly, like so:
550 * interpret XF86_Next_VMode {
551 * action = Private(type=0x86, data="+VMode");
553 * We don't handle those.
555 if (action->type >= _ACTION_TYPE_NUM_ENTRIES)
558 if (!filter_action_funcs[action->type].new)
561 filter = xkb_filter_new(state);
566 filter->func = filter_action_funcs[action->type].func;
567 filter->action = *action;
568 filter_action_funcs[action->type].new(state, filter);
571 XKB_EXPORT struct xkb_state *
572 xkb_state_new(struct xkb_keymap *keymap)
574 struct xkb_state *ret;
576 ret = calloc(sizeof(*ret), 1);
581 ret->keymap = xkb_keymap_ref(keymap);
586 XKB_EXPORT struct xkb_state *
587 xkb_state_ref(struct xkb_state *state)
594 xkb_state_unref(struct xkb_state *state)
596 if (!state || --state->refcnt > 0)
599 xkb_keymap_unref(state->keymap);
600 darray_free(state->filters);
604 XKB_EXPORT struct xkb_keymap *
605 xkb_state_get_keymap(struct xkb_state *state)
607 return state->keymap;
611 * Update the LED state to match the rest of the xkb_state.
614 xkb_state_led_update_all(struct xkb_state *state)
617 const struct xkb_led *led;
619 state->components.leds = 0;
621 darray_enumerate(idx, led, state->keymap->leds) {
622 xkb_mod_mask_t mod_mask = 0;
623 xkb_layout_mask_t group_mask = 0;
625 if (led->which_mods & XKB_STATE_MODS_EFFECTIVE)
626 mod_mask |= state->components.mods;
627 if (led->which_mods & XKB_STATE_MODS_DEPRESSED)
628 mod_mask |= state->components.base_mods;
629 if (led->which_mods & XKB_STATE_MODS_LATCHED)
630 mod_mask |= state->components.latched_mods;
631 if (led->which_mods & XKB_STATE_MODS_LOCKED)
632 mod_mask |= state->components.locked_mods;
633 if (led->mods.mask & mod_mask)
634 state->components.leds |= (1u << idx);
636 if (led->which_groups & XKB_STATE_LAYOUT_EFFECTIVE)
637 group_mask |= (1u << state->components.group);
638 if (led->which_groups & XKB_STATE_LAYOUT_DEPRESSED)
639 group_mask |= (1u << state->components.base_group);
640 if (led->which_groups & XKB_STATE_LAYOUT_LATCHED)
641 group_mask |= (1u << state->components.latched_group);
642 if (led->which_groups & XKB_STATE_LAYOUT_LOCKED)
643 group_mask |= (1u << state->components.locked_group);
644 if (led->groups & group_mask)
645 state->components.leds |= (1u << idx);
647 if (led->ctrls & state->keymap->enabled_ctrls)
648 state->components.leds |= (1u << idx);
653 * Calculates the derived state (effective mods/group and LEDs) from an
654 * up-to-date xkb_state.
657 xkb_state_update_derived(struct xkb_state *state)
659 state->components.mods = (state->components.base_mods |
660 state->components.latched_mods |
661 state->components.locked_mods);
663 /* TODO: Use groups_wrap control instead of always RANGE_WRAP. */
665 state->components.locked_group =
666 wrap_group_into_range(state->components.locked_group,
667 state->keymap->num_groups,
670 state->components.group =
671 wrap_group_into_range(state->components.base_group +
672 state->components.latched_group +
673 state->components.locked_group,
674 state->keymap->num_groups,
677 xkb_state_led_update_all(state);
680 static enum xkb_state_component
681 get_state_component_changes(const struct state_components *a,
682 const struct state_components *b)
684 xkb_mod_mask_t mask = 0;
686 if (a->group != b->group)
687 mask |= XKB_STATE_LAYOUT_EFFECTIVE;
688 if (a->base_group != b->base_group)
689 mask |= XKB_STATE_LAYOUT_DEPRESSED;
690 if (a->latched_group != b->latched_group)
691 mask |= XKB_STATE_LAYOUT_LATCHED;
692 if (a->locked_group != b->locked_group)
693 mask |= XKB_STATE_LAYOUT_LOCKED;
694 if (a->mods != b->mods)
695 mask |= XKB_STATE_MODS_EFFECTIVE;
696 if (a->base_mods != b->base_mods)
697 mask |= XKB_STATE_MODS_DEPRESSED;
698 if (a->latched_mods != b->latched_mods)
699 mask |= XKB_STATE_MODS_LATCHED;
700 if (a->locked_mods != b->locked_mods)
701 mask |= XKB_STATE_MODS_LOCKED;
702 if (a->leds != b->leds)
703 mask |= XKB_STATE_LEDS;
709 * Given a particular key event, updates the state structure to reflect the
712 XKB_EXPORT enum xkb_state_component
713 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
714 enum xkb_key_direction direction)
718 struct state_components prev_components;
719 const struct xkb_key *key = XkbKey(state->keymap, kc);
724 prev_components = state->components;
727 state->clear_mods = 0;
729 xkb_filter_apply_all(state, key, direction);
731 for (i = 0, bit = 1; state->set_mods; i++, bit <<= 1) {
732 if (state->set_mods & bit) {
733 state->mod_key_count[i]++;
734 state->components.base_mods |= bit;
735 state->set_mods &= ~bit;
739 for (i = 0, bit = 1; state->clear_mods; i++, bit <<= 1) {
740 if (state->clear_mods & bit) {
741 state->mod_key_count[i]--;
742 if (state->mod_key_count[i] <= 0) {
743 state->components.base_mods &= ~bit;
744 state->mod_key_count[i] = 0;
746 state->clear_mods &= ~bit;
750 xkb_state_update_derived(state);
752 return get_state_component_changes(&prev_components, &state->components);
756 * Updates the state from a set of explicit masks as gained from
757 * xkb_state_serialize_mods and xkb_state_serialize_groups. As noted in the
758 * documentation for these functions in xkbcommon.h, this round-trip is
759 * lossy, and should only be used to update a slave state mirroring the
760 * master, e.g. in a client/server window system.
762 XKB_EXPORT enum xkb_state_component
763 xkb_state_update_mask(struct xkb_state *state,
764 xkb_mod_mask_t base_mods,
765 xkb_mod_mask_t latched_mods,
766 xkb_mod_mask_t locked_mods,
767 xkb_layout_index_t base_group,
768 xkb_layout_index_t latched_group,
769 xkb_layout_index_t locked_group)
771 struct state_components prev_components;
772 xkb_mod_index_t num_mods;
775 prev_components = state->components;
777 state->components.base_mods = 0;
778 state->components.latched_mods = 0;
779 state->components.locked_mods = 0;
780 num_mods = xkb_keymap_num_mods(state->keymap);
782 for (idx = 0; idx < num_mods; idx++) {
783 xkb_mod_mask_t mod = (1u << idx);
785 state->components.base_mods |= mod;
786 if (latched_mods & mod)
787 state->components.latched_mods |= mod;
788 if (locked_mods & mod)
789 state->components.locked_mods |= mod;
792 state->components.base_group = base_group;
793 state->components.latched_group = latched_group;
794 state->components.locked_group = locked_group;
796 xkb_state_update_derived(state);
798 return get_state_component_changes(&prev_components, &state->components);
802 * Provides the symbols to use for the given key and state. Returns the
803 * number of symbols pointed to in syms_out.
806 xkb_state_key_get_syms(struct xkb_state *state, xkb_keycode_t kc,
807 const xkb_keysym_t **syms_out)
809 xkb_layout_index_t layout;
810 xkb_level_index_t level;
812 layout = xkb_state_key_get_layout(state, kc);
813 if (layout == XKB_LAYOUT_INVALID)
816 level = xkb_state_key_get_level(state, kc, layout);
817 if (level == XKB_LEVEL_INVALID)
820 return xkb_keymap_key_get_syms_by_level(state->keymap, kc, layout, level,
829 * Provides either exactly one symbol, or XKB_KEY_NoSymbol.
831 XKB_EXPORT xkb_keysym_t
832 xkb_state_key_get_one_sym(struct xkb_state *state, xkb_keycode_t kc)
834 const xkb_keysym_t *syms;
837 xkb_mod_index_t caps;
839 num_syms = xkb_state_key_get_syms(state, kc, &syms);
841 return XKB_KEY_NoSymbol;
846 * Perform capitalization transformation, see:
847 * http://www.x.org/releases/current/doc/kbproto/xkbproto.html#Interpreting_the_Lock_Modifier
849 caps = xkb_keymap_mod_get_index(state->keymap, XKB_MOD_NAME_CAPS);
850 if (xkb_state_mod_index_is_active(state, caps, XKB_STATE_MODS_EFFECTIVE) > 0 &&
851 xkb_state_mod_index_is_consumed(state, kc, caps) == 0)
852 sym = xkb_keysym_to_upper(sym);
858 * Serialises the requested modifier state into an xkb_mod_mask_t, with all
859 * the same disclaimers as in xkb_state_update_mask.
861 XKB_EXPORT xkb_mod_mask_t
862 xkb_state_serialize_mods(struct xkb_state *state,
863 enum xkb_state_component type)
865 xkb_mod_mask_t ret = 0;
867 if (type & XKB_STATE_MODS_EFFECTIVE)
868 return state->components.mods;
870 if (type & XKB_STATE_MODS_DEPRESSED)
871 ret |= state->components.base_mods;
872 if (type & XKB_STATE_MODS_LATCHED)
873 ret |= state->components.latched_mods;
874 if (type & XKB_STATE_MODS_LOCKED)
875 ret |= state->components.locked_mods;
881 * Serialises the requested group state, with all the same disclaimers as
882 * in xkb_state_update_mask.
884 XKB_EXPORT xkb_layout_index_t
885 xkb_state_serialize_layout(struct xkb_state *state,
886 enum xkb_state_component type)
888 xkb_layout_index_t ret = 0;
890 if (type & XKB_STATE_LAYOUT_EFFECTIVE)
891 return state->components.group;
893 if (type & XKB_STATE_LAYOUT_DEPRESSED)
894 ret += state->components.base_group;
895 if (type & XKB_STATE_LAYOUT_LATCHED)
896 ret += state->components.latched_group;
897 if (type & XKB_STATE_LAYOUT_LOCKED)
898 ret += state->components.locked_group;
904 * Returns 1 if the given modifier is active with the specified type(s), 0 if
905 * not, or -1 if the modifier is invalid.
908 xkb_state_mod_index_is_active(struct xkb_state *state,
910 enum xkb_state_component type)
912 if (idx >= xkb_keymap_num_mods(state->keymap))
915 return !!(xkb_state_serialize_mods(state, type) & (1u << idx));
919 * Helper function for xkb_state_mod_indices_are_active and
920 * xkb_state_mod_names_are_active.
923 match_mod_masks(struct xkb_state *state,
924 enum xkb_state_component type,
925 enum xkb_state_match match,
926 xkb_mod_mask_t wanted)
928 xkb_mod_mask_t active = xkb_state_serialize_mods(state, type);
930 if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
933 if (match & XKB_STATE_MATCH_ANY)
934 return !!(active & wanted);
936 return (active & wanted) == wanted;
942 * Returns 1 if the modifiers are active with the specified type(s), 0 if
943 * not, or -1 if any of the modifiers are invalid.
946 xkb_state_mod_indices_are_active(struct xkb_state *state,
947 enum xkb_state_component type,
948 enum xkb_state_match match,
952 xkb_mod_index_t idx = 0;
953 xkb_mod_mask_t wanted = 0;
955 xkb_mod_index_t num_mods = xkb_keymap_num_mods(state->keymap);
959 idx = va_arg(ap, xkb_mod_index_t);
960 if (idx == XKB_MOD_INVALID)
962 if (idx >= num_mods) {
966 wanted |= (1u << idx);
973 return match_mod_masks(state, type, match, wanted);
977 * Returns 1 if the given modifier is active with the specified type(s), 0 if
978 * not, or -1 if the modifier is invalid.
981 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
982 enum xkb_state_component type)
984 xkb_mod_index_t idx = xkb_keymap_mod_get_index(state->keymap, name);
986 if (idx == XKB_MOD_INVALID)
989 return xkb_state_mod_index_is_active(state, idx, type);
993 * Returns 1 if the modifiers are active with the specified type(s), 0 if
994 * not, or -1 if any of the modifiers are invalid.
996 XKB_EXPORT ATTR_NULL_SENTINEL int
997 xkb_state_mod_names_are_active(struct xkb_state *state,
998 enum xkb_state_component type,
999 enum xkb_state_match match,
1003 xkb_mod_index_t idx = 0;
1004 xkb_mod_mask_t wanted = 0;
1007 va_start(ap, match);
1009 const char *str = va_arg(ap, const char *);
1012 idx = xkb_keymap_mod_get_index(state->keymap, str);
1013 if (idx == XKB_MOD_INVALID) {
1017 wanted |= (1u << idx);
1024 return match_mod_masks(state, type, match, wanted);
1028 * Returns 1 if the given group is active with the specified type(s), 0 if
1029 * not, or -1 if the group is invalid.
1032 xkb_state_layout_index_is_active(struct xkb_state *state,
1033 xkb_layout_index_t idx,
1034 enum xkb_state_component type)
1038 if (idx >= state->keymap->num_groups)
1041 if (type & XKB_STATE_LAYOUT_EFFECTIVE)
1042 ret |= (state->components.group == idx);
1043 if (type & XKB_STATE_LAYOUT_DEPRESSED)
1044 ret |= (state->components.base_group == (int32_t) idx);
1045 if (type & XKB_STATE_LAYOUT_LATCHED)
1046 ret |= (state->components.latched_group == (int32_t) idx);
1047 if (type & XKB_STATE_LAYOUT_LOCKED)
1048 ret |= (state->components.locked_group == (int32_t) idx);
1054 * Returns 1 if the given modifier is active with the specified type(s), 0 if
1055 * not, or -1 if the modifier is invalid.
1058 xkb_state_layout_name_is_active(struct xkb_state *state, const char *name,
1059 enum xkb_state_component type)
1061 xkb_layout_index_t idx = xkb_keymap_layout_get_index(state->keymap, name);
1063 if (idx == XKB_LAYOUT_INVALID)
1066 return xkb_state_layout_index_is_active(state, idx, type);
1070 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
1073 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
1075 if (idx >= darray_size(state->keymap->leds) ||
1076 darray_item(state->keymap->leds, idx).name == XKB_ATOM_NONE)
1079 return !!(state->components.leds & (1u << idx));
1083 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
1086 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
1088 xkb_led_index_t idx = xkb_keymap_led_get_index(state->keymap, name);
1090 if (idx == XKB_LED_INVALID)
1093 return xkb_state_led_index_is_active(state, idx);
1096 static xkb_mod_mask_t
1097 key_get_consumed(struct xkb_state *state, const struct xkb_key *key)
1099 const struct xkb_key_type_entry *entry;
1100 xkb_layout_index_t group;
1102 group = xkb_state_key_get_layout(state, key->keycode);
1103 if (group == XKB_LAYOUT_INVALID)
1106 entry = get_entry_for_key_state(state, key, group);
1110 return entry->mods.mask & ~entry->preserve.mask;
1114 * Tests to see if a modifier is used up by our translation of a
1115 * keycode to keysyms, taking note of the current modifier state and
1116 * the appropriate key type's preserve information, if any. This allows
1117 * the user to mask out the modifier in later processing of the
1118 * modifiers, e.g. when implementing hot keys or accelerators.
1120 * See also, for example:
1121 * - XkbTranslateKeyCode(3), mod_rtrn return value, from libX11.
1122 * - gdk_keymap_translate_keyboard_state, consumed_modifiers return value,
1126 xkb_state_mod_index_is_consumed(struct xkb_state *state, xkb_keycode_t kc,
1127 xkb_mod_index_t idx)
1129 const struct xkb_key *key = XkbKey(state->keymap, kc);
1131 if (!key || idx >= xkb_keymap_num_mods(state->keymap))
1134 return !!((1u << idx) & key_get_consumed(state, key));
1138 * Calculates which modifiers should be consumed during key processing,
1139 * and returns the mask with all these modifiers removed. e.g. if
1140 * given a state of Alt and Shift active for a two-level alphabetic
1141 * key containing plus and equal on the first and second level
1142 * respectively, will return a mask of only Alt, as Shift has been
1143 * consumed by the type handling.
1145 XKB_EXPORT xkb_mod_mask_t
1146 xkb_state_mod_mask_remove_consumed(struct xkb_state *state, xkb_keycode_t kc,
1147 xkb_mod_mask_t mask)
1149 const struct xkb_key *key = XkbKey(state->keymap, kc);
1154 return mask & ~key_get_consumed(state, key);