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)
65 union xkb_action action;
66 const struct xkb_key *key;
68 int (*func)(struct xkb_state *state,
69 struct xkb_filter *filter,
70 const struct xkb_key *key,
71 enum xkb_key_direction direction);
76 /* These may be negative, because of -1 group actions. */
77 int32_t base_group; /**< depressed */
78 int32_t latched_group;
80 xkb_layout_index_t group; /**< effective */
82 xkb_mod_mask_t base_mods; /**< depressed */
83 xkb_mod_mask_t latched_mods;
84 xkb_mod_mask_t locked_mods;
85 xkb_mod_mask_t mods; /**< effective */
88 * At each event, we accumulate all the needed modifications to the base
89 * modifiers, and apply them at the end. These keep track of this state.
91 xkb_mod_mask_t set_mods;
92 xkb_mod_mask_t clear_mods;
94 * We mustn't clear a base modifier if there's another depressed key
95 * which affects it, e.g. given this sequence
96 * < Left Shift down, Right Shift down, Left Shift Up >
97 * the modifier should still be set. This keeps the count.
99 int16_t mod_key_count[sizeof(xkb_mod_mask_t) * 8];
104 darray(struct xkb_filter) filters;
105 struct xkb_keymap *keymap;
108 static const struct xkb_kt_map_entry *
109 get_entry_for_key_state(struct xkb_state *state, const struct xkb_key *key,
110 xkb_layout_index_t group)
112 const struct xkb_key_type *type;
113 xkb_mod_mask_t active_mods;
116 type = key->groups[group].type;
117 active_mods = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
118 active_mods &= type->mods.mask;
120 for (i = 0; i < type->num_entries; i++)
121 if (type->map[i].mods.mask == active_mods)
122 return &type->map[i];
128 * Returns the level to use for the given key and state, or
131 XKB_EXPORT xkb_level_index_t
132 xkb_state_key_get_level(struct xkb_state *state, xkb_keycode_t kc,
133 xkb_layout_index_t layout)
135 const struct xkb_key *key = XkbKey(state->keymap, kc);
136 const struct xkb_kt_map_entry *entry;
138 if (!key || layout >= key->num_groups)
139 return XKB_LEVEL_INVALID;
141 /* If we don't find an explicit match the default is 0. */
142 entry = get_entry_for_key_state(state, key, layout);
149 static xkb_layout_index_t
150 wrap_group_into_range(int32_t group,
151 xkb_layout_index_t num_groups,
152 enum xkb_range_exceed_type out_of_range_group_action,
153 xkb_layout_index_t out_of_range_group_number)
156 return XKB_LAYOUT_INVALID;
158 if (group < num_groups)
161 switch (out_of_range_group_action) {
163 if (out_of_range_group_number >= num_groups)
165 return out_of_range_group_number;
171 return num_groups - 1;
176 * C99 says a negative dividend in a modulo operation always
177 * gives a negative result.
180 return ((int) num_groups + (group % (int) num_groups));
182 return group % num_groups;
187 * Returns the layout to use for the given key and state, taking
188 * wrapping/clamping/etc into account, or XKB_LAYOUT_INVALID.
190 XKB_EXPORT xkb_layout_index_t
191 xkb_state_key_get_layout(struct xkb_state *state, xkb_keycode_t kc)
193 xkb_layout_index_t group =
194 xkb_state_serialize_layout(state, XKB_STATE_EFFECTIVE);
195 const struct xkb_key *key = XkbKey(state->keymap, kc);
198 return XKB_LAYOUT_INVALID;
200 return wrap_group_into_range(group, key->num_groups,
201 key->out_of_range_group_action,
202 key->out_of_range_group_number);
205 static const union xkb_action fake = { .type = ACTION_TYPE_NONE };
207 static const union xkb_action *
208 xkb_key_get_action(struct xkb_state *state, const struct xkb_key *key)
210 xkb_layout_index_t layout;
211 xkb_level_index_t level;
213 layout = xkb_state_key_get_layout(state, key->keycode);
214 if (layout == XKB_LAYOUT_INVALID)
217 level = xkb_state_key_get_level(state, key->keycode, layout);
218 if (level == XKB_LEVEL_INVALID)
221 return &key->groups[layout].levels[level].action;
224 static struct xkb_filter *
225 xkb_filter_new(struct xkb_state *state)
227 struct xkb_filter *filter = NULL, *iter;
229 darray_foreach(iter, state->filters) {
237 darray_resize0(state->filters, darray_size(state->filters) + 1);
238 filter = &darray_item(state->filters, darray_size(state->filters) -1);
245 /***====================================================================***/
248 xkb_filter_group_set_func(struct xkb_state *state,
249 struct xkb_filter *filter,
250 const struct xkb_key *key,
251 enum xkb_key_direction direction)
253 if (key != filter->key) {
254 filter->action.group.flags &= ~ACTION_LOCK_CLEAR;
258 if (direction == XKB_KEY_DOWN) {
262 else if (--filter->refcnt > 0) {
266 state->base_group = filter->priv;
268 if (filter->action.group.flags & ACTION_LOCK_CLEAR)
269 state->locked_group = 0;
276 xkb_filter_group_set_new(struct xkb_state *state, struct xkb_filter *filter)
278 filter->priv = state->base_group;
279 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
280 state->base_group = filter->action.group.group;
282 state->base_group += filter->action.group.group;
286 xkb_filter_group_lock_func(struct xkb_state *state,
287 struct xkb_filter *filter,
288 const struct xkb_key *key,
289 enum xkb_key_direction direction)
291 if (key != filter->key)
294 if (direction == XKB_KEY_DOWN) {
298 if (--filter->refcnt > 0)
306 xkb_filter_group_lock_new(struct xkb_state *state, struct xkb_filter *filter)
308 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
309 state->locked_group = filter->action.group.group;
311 state->locked_group += filter->action.group.group;
315 xkb_filter_mod_set_func(struct xkb_state *state,
316 struct xkb_filter *filter,
317 const struct xkb_key *key,
318 enum xkb_key_direction direction)
320 if (key != filter->key) {
321 filter->action.mods.flags &= ~ACTION_LOCK_CLEAR;
325 if (direction == XKB_KEY_DOWN) {
329 else if (--filter->refcnt > 0) {
333 state->clear_mods = filter->action.mods.mods.mask;
334 if (filter->action.mods.flags & ACTION_LOCK_CLEAR)
335 state->locked_mods &= ~filter->action.mods.mods.mask;
342 xkb_filter_mod_set_new(struct xkb_state *state, struct xkb_filter *filter)
344 state->set_mods = filter->action.mods.mods.mask;
348 xkb_filter_mod_lock_func(struct xkb_state *state,
349 struct xkb_filter *filter,
350 const struct xkb_key *key,
351 enum xkb_key_direction direction)
353 if (key != filter->key)
356 if (direction == XKB_KEY_DOWN) {
360 if (--filter->refcnt > 0)
363 state->clear_mods |= filter->action.mods.mods.mask;
364 if (!(filter->action.mods.flags & ACTION_LOCK_NO_UNLOCK))
365 state->locked_mods &= ~filter->priv;
372 xkb_filter_mod_lock_new(struct xkb_state *state, struct xkb_filter *filter)
374 filter->priv = state->locked_mods & filter->action.mods.mods.mask;
375 state->set_mods |= filter->action.mods.mods.mask;
376 if (!(filter->action.mods.flags & ACTION_LOCK_NO_LOCK))
377 state->locked_mods |= filter->action.mods.mods.mask;
380 enum xkb_key_latch_state {
387 xkb_action_breaks_latch(const union xkb_action *action)
389 switch (action->type) {
390 case ACTION_TYPE_NONE:
391 case ACTION_TYPE_PTR_BUTTON:
392 case ACTION_TYPE_PTR_LOCK:
393 case ACTION_TYPE_CTRL_SET:
394 case ACTION_TYPE_CTRL_LOCK:
395 case ACTION_TYPE_KEY_REDIRECT:
396 case ACTION_TYPE_SWITCH_VT:
397 case ACTION_TYPE_TERMINATE:
405 xkb_filter_mod_latch_func(struct xkb_state *state,
406 struct xkb_filter *filter,
407 const struct xkb_key *key,
408 enum xkb_key_direction direction)
410 enum xkb_key_latch_state latch = filter->priv;
412 if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
413 /* If this is a new keypress and we're awaiting our single latched
414 * keypress, then either break the latch if any random key is pressed,
415 * or promote it to a lock or plain base set if it's the same
417 const union xkb_action *action = xkb_key_get_action(state, key);
418 if (action->type == ACTION_TYPE_MOD_LATCH &&
419 action->mods.flags == filter->action.mods.flags &&
420 action->mods.mods.mask == filter->action.mods.mods.mask) {
421 filter->action = *action;
422 if (filter->action.mods.flags & ACTION_LATCH_TO_LOCK) {
423 filter->action.type = ACTION_TYPE_MOD_LOCK;
424 filter->func = xkb_filter_mod_lock_func;
425 state->locked_mods |= filter->action.mods.mods.mask;
428 filter->action.type = ACTION_TYPE_MOD_SET;
429 filter->func = xkb_filter_mod_set_func;
430 state->set_mods = filter->action.mods.mods.mask;
433 state->latched_mods &= ~filter->action.mods.mods.mask;
437 else if (xkb_action_breaks_latch(action)) {
438 /* XXX: This may be totally broken, we might need to break the
439 * latch in the next run after this press? */
440 state->latched_mods &= ~filter->action.mods.mods.mask;
445 else if (direction == XKB_KEY_UP && key == filter->key) {
446 /* Our key got released. If we've set it to clear locks, and we
447 * currently have the same modifiers locked, then release them and
448 * don't actually latch. Else we've actually hit the latching
449 * stage, so set PENDING and move our modifier from base to
451 if (latch == NO_LATCH ||
452 ((filter->action.mods.flags & ACTION_LOCK_CLEAR) &&
453 (state->locked_mods & filter->action.mods.mods.mask) ==
454 filter->action.mods.mods.mask)) {
455 /* XXX: We might be a bit overenthusiastic about clearing
456 * mods other filters have set here? */
457 if (latch == LATCH_PENDING)
458 state->latched_mods &= ~filter->action.mods.mods.mask;
460 state->clear_mods = filter->action.mods.mods.mask;
461 state->locked_mods &= ~filter->action.mods.mods.mask;
465 latch = LATCH_PENDING;
466 state->clear_mods = filter->action.mods.mods.mask;
467 state->latched_mods |= filter->action.mods.mods.mask;
471 else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
472 /* Someone's pressed another key while we've still got the latching
473 * key held down, so keep the base modifier state active (from
474 * xkb_filter_mod_latch_new), but don't trip the latch, just clear
475 * it as soon as the modifier gets released. */
479 filter->priv = latch;
485 xkb_filter_mod_latch_new(struct xkb_state *state, struct xkb_filter *filter)
487 filter->priv = LATCH_KEY_DOWN;
488 state->set_mods = filter->action.mods.mods.mask;
491 static const struct {
492 void (*new)(struct xkb_state *state, struct xkb_filter *filter);
493 int (*func)(struct xkb_state *state, struct xkb_filter *filter,
494 const struct xkb_key *key, enum xkb_key_direction direction);
495 } filter_action_funcs[_ACTION_TYPE_NUM_ENTRIES] = {
496 [ACTION_TYPE_MOD_SET] = { xkb_filter_mod_set_new,
497 xkb_filter_mod_set_func },
498 [ACTION_TYPE_MOD_LATCH] = { xkb_filter_mod_latch_new,
499 xkb_filter_mod_latch_func },
500 [ACTION_TYPE_MOD_LOCK] = { xkb_filter_mod_lock_new,
501 xkb_filter_mod_lock_func },
502 [ACTION_TYPE_GROUP_SET] = { xkb_filter_group_set_new,
503 xkb_filter_group_set_func },
504 [ACTION_TYPE_GROUP_LOCK] = { xkb_filter_group_lock_new,
505 xkb_filter_group_lock_func },
509 * Applies any relevant filters to the key, first from the list of filters
510 * that are currently active, then if no filter has claimed the key, possibly
511 * apply a new filter from the key action.
514 xkb_filter_apply_all(struct xkb_state *state,
515 const struct xkb_key *key,
516 enum xkb_key_direction direction)
518 struct xkb_filter *filter;
519 const union xkb_action *action;
522 /* First run through all the currently active filters and see if any of
523 * them have claimed this event. */
524 darray_foreach(filter, state->filters) {
527 send &= filter->func(state, filter, key, direction);
530 if (!send || direction == XKB_KEY_UP)
533 action = xkb_key_get_action(state, key);
534 if (!filter_action_funcs[action->type].new)
537 filter = xkb_filter_new(state);
542 filter->func = filter_action_funcs[action->type].func;
543 filter->action = *action;
544 filter_action_funcs[action->type].new(state, filter);
547 XKB_EXPORT struct xkb_state *
548 xkb_state_new(struct xkb_keymap *keymap)
550 struct xkb_state *ret;
552 ret = calloc(sizeof(*ret), 1);
557 ret->keymap = xkb_keymap_ref(keymap);
562 XKB_EXPORT struct xkb_state *
563 xkb_state_ref(struct xkb_state *state)
570 xkb_state_unref(struct xkb_state *state)
572 if (--state->refcnt > 0)
575 xkb_keymap_unref(state->keymap);
576 darray_free(state->filters);
580 XKB_EXPORT struct xkb_keymap *
581 xkb_state_get_keymap(struct xkb_state *state)
583 return state->keymap;
587 * Update the LED state to match the rest of the xkb_state.
590 xkb_state_led_update_all(struct xkb_state *state)
596 for (led = 0; led < XKB_NUM_INDICATORS; led++) {
597 struct xkb_indicator_map *map = &state->keymap->indicators[led];
598 xkb_mod_mask_t mod_mask = 0;
599 xkb_layout_mask_t group_mask = 0;
601 if (map->which_mods & XKB_STATE_DEPRESSED)
602 mod_mask |= state->base_mods;
603 if (map->which_mods & XKB_STATE_LATCHED)
604 mod_mask |= state->latched_mods;
605 if (map->which_mods & XKB_STATE_LOCKED)
606 mod_mask |= state->locked_mods;
607 if ((map->mods.mask & mod_mask))
608 state->leds |= (1 << led);
610 if (map->which_groups & XKB_STATE_DEPRESSED)
611 group_mask |= (1 << state->base_group);
612 if (map->which_groups & XKB_STATE_LATCHED)
613 group_mask |= (1 << state->latched_group);
614 if (map->which_groups & XKB_STATE_LOCKED)
615 group_mask |= (1 << state->locked_group);
616 if ((map->groups & group_mask))
617 state->leds |= (1 << led);
620 if ((map->ctrls & state->keymap->enabled_ctrls))
621 state->leds |= (1 << led);
627 * Calculates the derived state (effective mods/group and LEDs) from an
628 * up-to-date xkb_state.
631 xkb_state_update_derived(struct xkb_state *state)
633 xkb_layout_index_t num_groups = xkb_keymap_num_layouts(state->keymap);
635 state->mods = (state->base_mods |
636 state->latched_mods |
639 /* TODO: Use groups_wrap control instead of always RANGE_WRAP. */
641 state->locked_group = wrap_group_into_range(state->locked_group,
645 state->group = wrap_group_into_range(state->base_group +
646 state->latched_group +
651 xkb_state_led_update_all(state);
655 * Given a particular key event, updates the state structure to reflect the
659 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
660 enum xkb_key_direction direction)
664 const struct xkb_key *key = XkbKey(state->keymap, kc);
670 state->clear_mods = 0;
672 xkb_filter_apply_all(state, key, direction);
674 for (i = 0, bit = 1; state->set_mods; i++, bit <<= 1) {
675 if (state->set_mods & bit) {
676 state->mod_key_count[i]++;
677 state->base_mods |= bit;
678 state->set_mods &= ~bit;
682 for (i = 0, bit = 1; state->clear_mods; i++, bit <<= 1) {
683 if (state->clear_mods & bit) {
684 state->mod_key_count[i]--;
685 if (state->mod_key_count[i] <= 0) {
686 state->base_mods &= ~bit;
687 state->mod_key_count[i] = 0;
689 state->clear_mods &= ~bit;
693 xkb_state_update_derived(state);
697 * Updates the state from a set of explicit masks as gained from
698 * xkb_state_serialize_mods and xkb_state_serialize_groups. As noted in the
699 * documentation for these functions in xkbcommon.h, this round-trip is
700 * lossy, and should only be used to update a slave state mirroring the
701 * master, e.g. in a client/server window system.
704 xkb_state_update_mask(struct xkb_state *state,
705 xkb_mod_mask_t base_mods,
706 xkb_mod_mask_t latched_mods,
707 xkb_mod_mask_t locked_mods,
708 xkb_layout_index_t base_group,
709 xkb_layout_index_t latched_group,
710 xkb_layout_index_t locked_group)
712 xkb_mod_index_t num_mods;
715 state->base_mods = 0;
716 state->latched_mods = 0;
717 state->locked_mods = 0;
718 num_mods = xkb_keymap_num_mods(state->keymap);
720 for (idx = 0; idx < num_mods; idx++) {
721 xkb_mod_mask_t mod = (1 << idx);
723 state->base_mods |= mod;
724 if (latched_mods & mod)
725 state->latched_mods |= mod;
726 if (locked_mods & mod)
727 state->locked_mods |= mod;
730 state->base_group = base_group;
731 state->latched_group = latched_group;
732 state->locked_group = locked_group;
734 xkb_state_update_derived(state);
738 * Provides the symbols to use for the given key and state. Returns the
739 * number of symbols pointed to in syms_out.
742 xkb_state_key_get_syms(struct xkb_state *state, xkb_keycode_t kc,
743 const xkb_keysym_t **syms_out)
745 xkb_layout_index_t layout;
746 xkb_level_index_t level;
747 const struct xkb_key *key = XkbKey(state->keymap, kc);
752 layout = xkb_state_key_get_layout(state, kc);
753 if (layout == XKB_LAYOUT_INVALID)
756 level = xkb_state_key_get_level(state, kc, layout);
757 if (level == XKB_LEVEL_INVALID)
760 return xkb_keymap_key_get_syms_by_level(state->keymap, kc, layout, level,
769 * Provides either exactly one symbol, or XKB_KEY_NoSymbol.
771 XKB_EXPORT xkb_keysym_t
772 xkb_state_key_get_one_sym(struct xkb_state *state, xkb_keycode_t kc)
774 const xkb_keysym_t *syms;
777 num_syms = xkb_state_key_get_syms(state, kc, &syms);
779 return XKB_KEY_NoSymbol;
785 * Serialises the requested modifier state into an xkb_mod_mask_t, with all
786 * the same disclaimers as in xkb_state_update_mask.
788 XKB_EXPORT xkb_mod_mask_t
789 xkb_state_serialize_mods(struct xkb_state *state,
790 enum xkb_state_component type)
792 xkb_mod_mask_t ret = 0;
794 if (type == XKB_STATE_EFFECTIVE)
797 if (type & XKB_STATE_DEPRESSED)
798 ret |= state->base_mods;
799 if (type & XKB_STATE_LATCHED)
800 ret |= state->latched_mods;
801 if (type & XKB_STATE_LOCKED)
802 ret |= state->locked_mods;
808 * Serialises the requested group state, with all the same disclaimers as
809 * in xkb_state_update_mask.
811 XKB_EXPORT xkb_layout_index_t
812 xkb_state_serialize_layout(struct xkb_state *state,
813 enum xkb_state_component type)
815 xkb_layout_index_t ret = 0;
817 if (type == XKB_STATE_EFFECTIVE)
820 if (type & XKB_STATE_DEPRESSED)
821 ret += state->base_group;
822 if (type & XKB_STATE_LATCHED)
823 ret += state->latched_group;
824 if (type & XKB_STATE_LOCKED)
825 ret += state->locked_group;
831 * Returns 1 if the given modifier is active with the specified type(s), 0 if
832 * not, or -1 if the modifier is invalid.
835 xkb_state_mod_index_is_active(struct xkb_state *state,
837 enum xkb_state_component type)
841 if (idx >= xkb_keymap_num_mods(state->keymap))
844 if (type & XKB_STATE_EFFECTIVE)
845 return !!(state->mods & (1 << idx));
847 if (type & XKB_STATE_DEPRESSED)
848 ret |= (state->base_mods & (1 << idx));
849 if (type & XKB_STATE_LATCHED)
850 ret |= (state->latched_mods & (1 << idx));
851 if (type & XKB_STATE_LOCKED)
852 ret |= (state->locked_mods & (1 << idx));
858 * Helper function for xkb_state_mod_indices_are_active and
859 * xkb_state_mod_names_are_active.
862 match_mod_masks(struct xkb_state *state, enum xkb_state_match match,
863 xkb_mod_mask_t wanted)
865 xkb_mod_mask_t active = xkb_state_serialize_mods(state,
866 XKB_STATE_EFFECTIVE);
868 if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
871 if (match & XKB_STATE_MATCH_ANY)
872 return !!(active & wanted);
874 return (active & wanted) == wanted;
880 * Returns 1 if the modifiers are active with the specified type(s), 0 if
881 * not, or -1 if any of the modifiers are invalid.
884 xkb_state_mod_indices_are_active(struct xkb_state *state,
885 enum xkb_state_component type,
886 enum xkb_state_match match,
890 xkb_mod_index_t idx = 0;
891 xkb_mod_mask_t wanted = 0;
893 xkb_mod_index_t num_mods = xkb_keymap_num_mods(state->keymap);
897 idx = va_arg(ap, xkb_mod_index_t);
898 if (idx == XKB_MOD_INVALID)
900 if (idx >= num_mods) {
904 wanted |= (1 << idx);
911 return match_mod_masks(state, match, wanted);
915 * Returns 1 if the given modifier is active with the specified type(s), 0 if
916 * not, or -1 if the modifier is invalid.
919 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
920 enum xkb_state_component type)
922 xkb_mod_index_t idx = xkb_keymap_mod_get_index(state->keymap, name);
924 if (idx == XKB_MOD_INVALID)
927 return xkb_state_mod_index_is_active(state, idx, type);
931 * Returns 1 if the modifiers are active with the specified type(s), 0 if
932 * not, or -1 if any of the modifiers are invalid.
934 XKB_EXPORT ATTR_NULL_SENTINEL int
935 xkb_state_mod_names_are_active(struct xkb_state *state,
936 enum xkb_state_component type,
937 enum xkb_state_match match,
941 xkb_mod_index_t idx = 0;
943 xkb_mod_mask_t wanted = 0;
948 str = va_arg(ap, const char *);
951 idx = xkb_keymap_mod_get_index(state->keymap, str);
952 if (idx == XKB_MOD_INVALID) {
956 wanted |= (1 << idx);
963 return match_mod_masks(state, match, wanted);
967 * Returns 1 if the given group is active with the specified type(s), 0 if
968 * not, or -1 if the group is invalid.
971 xkb_state_layout_index_is_active(struct xkb_state *state,
972 xkb_layout_index_t idx,
973 enum xkb_state_component type)
977 if (idx >= xkb_keymap_num_layouts(state->keymap))
980 /* Can only have one effective group. */
981 if (type & XKB_STATE_EFFECTIVE)
982 return state->group == idx;
984 if (type & XKB_STATE_DEPRESSED)
985 ret |= (state->base_group == idx);
986 if (type & XKB_STATE_LATCHED)
987 ret |= (state->latched_group == idx);
988 if (type & XKB_STATE_LOCKED)
989 ret |= (state->locked_group == idx);
995 * Returns 1 if the given modifier is active with the specified type(s), 0 if
996 * not, or -1 if the modifier is invalid.
999 xkb_state_layout_name_is_active(struct xkb_state *state, const char *name,
1000 enum xkb_state_component type)
1002 xkb_layout_index_t idx = xkb_keymap_layout_get_index(state->keymap, name);
1004 if (idx == XKB_LAYOUT_INVALID)
1007 return xkb_state_layout_index_is_active(state, idx, type);
1011 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
1014 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
1016 if (idx >= xkb_keymap_num_leds(state->keymap))
1019 return !!(state->leds & (1 << idx));
1023 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
1026 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
1028 xkb_led_index_t idx = xkb_keymap_led_get_index(state->keymap, name);
1030 if (idx == XKB_LED_INVALID)
1033 return xkb_state_led_index_is_active(state, idx);
1036 static xkb_mod_mask_t
1037 key_get_consumed(struct xkb_state *state, const struct xkb_key *key)
1039 const struct xkb_kt_map_entry *entry;
1040 xkb_layout_index_t group;
1042 group = xkb_state_key_get_layout(state, key->keycode);
1043 if (group == XKB_LAYOUT_INVALID)
1046 entry = get_entry_for_key_state(state, key, group);
1050 return entry->mods.mask & ~entry->preserve.mask;
1054 * Tests to see if a modifier is used up by our translation of a
1055 * keycode to keysyms, taking note of the current modifier state and
1056 * the appropriate key type's preserve information, if any. This allows
1057 * the user to mask out the modifier in later processing of the
1058 * modifiers, e.g. when implementing hot keys or accelerators.
1060 * See also, for example:
1061 * - XkbTranslateKeyCode(3), mod_rtrn retrun value, from libX11.
1062 * - gdk_keymap_translate_keyboard_state, consumed_modifiers return value,
1066 xkb_state_mod_index_is_consumed(struct xkb_state *state, xkb_keycode_t kc,
1067 xkb_mod_index_t idx)
1069 const struct xkb_key *key = XkbKey(state->keymap, kc);
1074 return !!((1 << idx) & key_get_consumed(state, key));
1078 * Calculates which modifiers should be consumed during key processing,
1079 * and returns the mask with all these modifiers removed. e.g. if
1080 * given a state of Alt and Shift active for a two-level alphabetic
1081 * key containing plus and equal on the first and second level
1082 * respectively, will return a mask of only Alt, as Shift has been
1083 * consumed by the type handling.
1085 XKB_EXPORT xkb_mod_mask_t
1086 xkb_state_mod_mask_remove_consumed(struct xkb_state *state, xkb_keycode_t kc,
1087 xkb_mod_mask_t mask)
1089 const struct xkb_key *key = XkbKey(state->keymap, kc);
1094 return mask & ~key_get_consumed(state, key);