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 xkb_layout_index_t base_group; /**< depressed */
77 xkb_layout_index_t latched_group;
78 xkb_layout_index_t locked_group;
79 xkb_layout_index_t group; /**< effective */
81 xkb_mod_mask_t base_mods; /**< depressed */
82 xkb_mod_mask_t latched_mods;
83 xkb_mod_mask_t locked_mods;
84 xkb_mod_mask_t mods; /**< effective */
87 * At each event, we accumulate all the needed modifications to the base
88 * modifiers, and apply them at the end. These keep track of this state.
90 xkb_mod_mask_t set_mods;
91 xkb_mod_mask_t clear_mods;
93 * We mustn't clear a base modifier if there's another depressed key
94 * which affects it, e.g. given this sequence
95 * < Left Shift down, Right Shift down, Left Shift Up >
96 * the modifier should still be set. This keeps the count.
98 int16_t mod_key_count[sizeof(xkb_mod_mask_t) * 8];
103 darray(struct xkb_filter) filters;
104 struct xkb_keymap *keymap;
107 static struct xkb_kt_map_entry *
108 get_entry_for_key_state(struct xkb_state *state, const struct xkb_key *key,
109 xkb_layout_index_t group)
111 struct xkb_key_type *type;
112 xkb_mod_mask_t active_mods;
115 type = XkbKeyType(state->keymap, key, group);
116 active_mods = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
117 active_mods &= type->mods.mask;
119 for (i = 0; i < type->num_entries; i++)
120 if (type->map[i].mods.mask == active_mods)
121 return &type->map[i];
127 * Returns the level to use for the given key and state, or
131 xkb_state_key_get_level(struct xkb_state *state, xkb_keycode_t kc,
132 xkb_layout_index_t layout)
134 const struct xkb_key *key = XkbKey(state->keymap, kc);
135 struct xkb_kt_map_entry *entry;
137 /* If we don't find an explicit match the default is 0. */
138 entry = get_entry_for_key_state(state, key, layout);
145 static xkb_layout_index_t
146 wrap_group_into_range(xkb_layout_index_t group,
147 xkb_layout_index_t num_groups,
148 enum xkb_range_exceed_type out_of_range_group_action,
149 xkb_layout_index_t out_of_range_group_number)
152 return XKB_LAYOUT_INVALID;
154 if (group < num_groups)
157 switch (out_of_range_group_action) {
159 if (out_of_range_group_number >= num_groups)
161 return out_of_range_group_number;
164 return num_groups - 1;
168 return group % num_groups;
173 * Returns the layout to use for the given key and state, taking
174 * wrapping/clamping/etc into account, or XKB_LAYOUT_INVALID.
176 XKB_EXPORT xkb_layout_index_t
177 xkb_state_key_get_layout(struct xkb_state *state, xkb_keycode_t kc)
179 xkb_layout_index_t group =
180 xkb_state_serialize_layout(state, XKB_STATE_EFFECTIVE);
181 const struct xkb_key *key = XkbKey(state->keymap, kc);
184 return XKB_LAYOUT_INVALID;
186 return wrap_group_into_range(group, key->num_groups,
187 key->out_of_range_group_action,
188 key->out_of_range_group_number);
191 static const union xkb_action fake = { .type = ACTION_TYPE_NONE };
193 static const union xkb_action *
194 xkb_key_get_action(struct xkb_state *state, const struct xkb_key *key)
196 xkb_layout_index_t layout;
197 xkb_level_index_t level;
202 layout = xkb_state_key_get_layout(state, key->keycode);
203 if (layout == XKB_LAYOUT_INVALID)
206 level = xkb_state_key_get_level(state, key->keycode, layout);
207 if (level == XKB_LEVEL_INVALID)
210 return XkbKeyActionEntry(key, layout, level);
213 static struct xkb_filter *
214 xkb_filter_new(struct xkb_state *state)
216 struct xkb_filter *filter = NULL, *iter;
218 darray_foreach(iter, state->filters) {
226 darray_resize0(state->filters, darray_size(state->filters) + 1);
227 filter = &darray_item(state->filters, darray_size(state->filters) -1);
234 /***====================================================================***/
237 xkb_filter_group_set_func(struct xkb_state *state,
238 struct xkb_filter *filter,
239 const struct xkb_key *key,
240 enum xkb_key_direction direction)
242 if (key != filter->key) {
243 filter->action.group.flags &= ~ACTION_LOCK_CLEAR;
247 if (direction == XKB_KEY_DOWN) {
251 else if (--filter->refcnt > 0) {
255 state->base_group = filter->priv;
257 if (filter->action.group.flags & ACTION_LOCK_CLEAR)
258 state->locked_group = 0;
265 xkb_filter_group_set_new(struct xkb_state *state, struct xkb_filter *filter)
267 filter->priv = state->base_group;
268 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
269 state->base_group = filter->action.group.group;
271 state->base_group += filter->action.group.group;
275 xkb_filter_group_lock_func(struct xkb_state *state,
276 struct xkb_filter *filter,
277 const struct xkb_key *key,
278 enum xkb_key_direction direction)
280 if (key != filter->key)
283 if (direction == XKB_KEY_DOWN) {
287 if (--filter->refcnt > 0)
295 xkb_filter_group_lock_new(struct xkb_state *state, struct xkb_filter *filter)
297 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
298 state->locked_group = filter->action.group.group;
300 state->locked_group += filter->action.group.group;
304 xkb_filter_mod_set_func(struct xkb_state *state,
305 struct xkb_filter *filter,
306 const struct xkb_key *key,
307 enum xkb_key_direction direction)
309 if (key != filter->key) {
310 filter->action.mods.flags &= ~ACTION_LOCK_CLEAR;
314 if (direction == XKB_KEY_DOWN) {
318 else if (--filter->refcnt > 0) {
322 state->clear_mods = filter->action.mods.mods.mask;
323 if (filter->action.mods.flags & ACTION_LOCK_CLEAR)
324 state->locked_mods &= ~filter->action.mods.mods.mask;
331 xkb_filter_mod_set_new(struct xkb_state *state, struct xkb_filter *filter)
333 state->set_mods = filter->action.mods.mods.mask;
337 xkb_filter_mod_lock_func(struct xkb_state *state,
338 struct xkb_filter *filter,
339 const struct xkb_key *key,
340 enum xkb_key_direction direction)
342 if (key != filter->key)
345 if (direction == XKB_KEY_DOWN) {
349 if (--filter->refcnt > 0)
352 state->clear_mods |= filter->action.mods.mods.mask;
353 if (!(filter->action.mods.flags & ACTION_LOCK_NO_UNLOCK))
354 state->locked_mods &= ~filter->priv;
361 xkb_filter_mod_lock_new(struct xkb_state *state, struct xkb_filter *filter)
363 filter->priv = state->locked_mods & filter->action.mods.mods.mask;
364 state->set_mods |= filter->action.mods.mods.mask;
365 if (!(filter->action.mods.flags & ACTION_LOCK_NO_LOCK))
366 state->locked_mods |= filter->action.mods.mods.mask;
369 enum xkb_key_latch_state {
376 xkb_action_breaks_latch(const union xkb_action *action)
378 switch (action->type) {
379 case ACTION_TYPE_NONE:
380 case ACTION_TYPE_PTR_BUTTON:
381 case ACTION_TYPE_PTR_LOCK:
382 case ACTION_TYPE_CTRL_SET:
383 case ACTION_TYPE_CTRL_LOCK:
384 case ACTION_TYPE_KEY_REDIRECT:
385 case ACTION_TYPE_SWITCH_VT:
386 case ACTION_TYPE_TERMINATE:
394 xkb_filter_mod_latch_func(struct xkb_state *state,
395 struct xkb_filter *filter,
396 const struct xkb_key *key,
397 enum xkb_key_direction direction)
399 enum xkb_key_latch_state latch = filter->priv;
401 if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
402 /* If this is a new keypress and we're awaiting our single latched
403 * keypress, then either break the latch if any random key is pressed,
404 * or promote it to a lock or plain base set if it's the same
406 const union xkb_action *action = xkb_key_get_action(state, key);
407 if (action->type == ACTION_TYPE_MOD_LATCH &&
408 action->mods.flags == filter->action.mods.flags &&
409 action->mods.mods.mask == filter->action.mods.mods.mask) {
410 filter->action = *action;
411 if (filter->action.mods.flags & ACTION_LATCH_TO_LOCK) {
412 filter->action.type = ACTION_TYPE_MOD_LOCK;
413 filter->func = xkb_filter_mod_lock_func;
414 state->locked_mods |= filter->action.mods.mods.mask;
417 filter->action.type = ACTION_TYPE_MOD_SET;
418 filter->func = xkb_filter_mod_set_func;
419 state->set_mods = filter->action.mods.mods.mask;
422 state->latched_mods &= ~filter->action.mods.mods.mask;
426 else if (xkb_action_breaks_latch(action)) {
427 /* XXX: This may be totally broken, we might need to break the
428 * latch in the next run after this press? */
429 state->latched_mods &= ~filter->action.mods.mods.mask;
434 else if (direction == XKB_KEY_UP && key == filter->key) {
435 /* Our key got released. If we've set it to clear locks, and we
436 * currently have the same modifiers locked, then release them and
437 * don't actually latch. Else we've actually hit the latching
438 * stage, so set PENDING and move our modifier from base to
440 if (latch == NO_LATCH ||
441 ((filter->action.mods.flags & ACTION_LOCK_CLEAR) &&
442 (state->locked_mods & filter->action.mods.mods.mask) ==
443 filter->action.mods.mods.mask)) {
444 /* XXX: We might be a bit overenthusiastic about clearing
445 * mods other filters have set here? */
446 if (latch == LATCH_PENDING)
447 state->latched_mods &= ~filter->action.mods.mods.mask;
449 state->clear_mods = filter->action.mods.mods.mask;
450 state->locked_mods &= ~filter->action.mods.mods.mask;
454 latch = LATCH_PENDING;
455 state->clear_mods = filter->action.mods.mods.mask;
456 state->latched_mods |= filter->action.mods.mods.mask;
460 else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
461 /* Someone's pressed another key while we've still got the latching
462 * key held down, so keep the base modifier state active (from
463 * xkb_filter_mod_latch_new), but don't trip the latch, just clear
464 * it as soon as the modifier gets released. */
468 filter->priv = latch;
474 xkb_filter_mod_latch_new(struct xkb_state *state, struct xkb_filter *filter)
476 filter->priv = LATCH_KEY_DOWN;
477 state->set_mods = filter->action.mods.mods.mask;
480 static const struct {
481 void (*new)(struct xkb_state *state, struct xkb_filter *filter);
482 int (*func)(struct xkb_state *state, struct xkb_filter *filter,
483 const struct xkb_key *key, enum xkb_key_direction direction);
484 } filter_action_funcs[_ACTION_TYPE_NUM_ENTRIES] = {
485 [ACTION_TYPE_MOD_SET] = { xkb_filter_mod_set_new,
486 xkb_filter_mod_set_func },
487 [ACTION_TYPE_MOD_LATCH] = { xkb_filter_mod_latch_new,
488 xkb_filter_mod_latch_func },
489 [ACTION_TYPE_MOD_LOCK] = { xkb_filter_mod_lock_new,
490 xkb_filter_mod_lock_func },
491 [ACTION_TYPE_GROUP_SET] = { xkb_filter_group_set_new,
492 xkb_filter_group_set_func },
493 [ACTION_TYPE_GROUP_LOCK] = { xkb_filter_group_lock_new,
494 xkb_filter_group_lock_func },
498 * Applies any relevant filters to the key, first from the list of filters
499 * that are currently active, then if no filter has claimed the key, possibly
500 * apply a new filter from the key action.
503 xkb_filter_apply_all(struct xkb_state *state,
504 const struct xkb_key *key,
505 enum xkb_key_direction direction)
507 struct xkb_filter *filter;
508 const union xkb_action *action;
511 /* First run through all the currently active filters and see if any of
512 * them have claimed this event. */
513 darray_foreach(filter, state->filters) {
516 send &= filter->func(state, filter, key, direction);
519 if (!send || direction == XKB_KEY_UP)
522 action = xkb_key_get_action(state, key);
523 if (!filter_action_funcs[action->type].new)
526 filter = xkb_filter_new(state);
531 filter->func = filter_action_funcs[action->type].func;
532 filter->action = *action;
533 filter_action_funcs[action->type].new(state, filter);
536 XKB_EXPORT struct xkb_state *
537 xkb_state_new(struct xkb_keymap *keymap)
539 struct xkb_state *ret;
541 ret = calloc(sizeof(*ret), 1);
546 ret->keymap = xkb_keymap_ref(keymap);
551 XKB_EXPORT struct xkb_state *
552 xkb_state_ref(struct xkb_state *state)
559 xkb_state_unref(struct xkb_state *state)
561 if (--state->refcnt > 0)
564 xkb_keymap_unref(state->keymap);
565 darray_free(state->filters);
569 XKB_EXPORT struct xkb_keymap *
570 xkb_state_get_keymap(struct xkb_state *state)
572 return state->keymap;
576 * Update the LED state to match the rest of the xkb_state.
579 xkb_state_led_update_all(struct xkb_state *state)
585 for (led = 0; led < XKB_NUM_INDICATORS; led++) {
586 struct xkb_indicator_map *map = &state->keymap->indicators[led];
587 xkb_mod_mask_t mod_mask = 0;
588 uint32_t group_mask = 0;
590 if (map->which_mods & XKB_STATE_DEPRESSED)
591 mod_mask |= state->base_mods;
592 if (map->which_mods & XKB_STATE_LATCHED)
593 mod_mask |= state->latched_mods;
594 if (map->which_mods & XKB_STATE_LOCKED)
595 mod_mask |= state->locked_mods;
596 if ((map->mods.mask & mod_mask))
597 state->leds |= (1 << led);
599 if (map->which_groups & XKB_STATE_DEPRESSED)
600 group_mask |= (1 << state->base_group);
601 if (map->which_groups & XKB_STATE_LATCHED)
602 group_mask |= (1 << state->latched_group);
603 if (map->which_groups & XKB_STATE_LOCKED)
604 group_mask |= (1 << state->locked_group);
605 if ((map->groups & group_mask))
606 state->leds |= (1 << led);
609 if ((map->ctrls & state->keymap->enabled_ctrls))
610 state->leds |= (1 << led);
616 * Calculates the derived state (effective mods/group and LEDs) from an
617 * up-to-date xkb_state.
620 xkb_state_update_derived(struct xkb_state *state)
623 (state->base_mods | state->latched_mods | state->locked_mods);
624 /* FIXME: Clamp/wrap locked_group */
625 state->group = state->locked_group + state->base_group +
626 state->latched_group;
627 /* FIXME: Clamp/wrap effective group */
629 xkb_state_led_update_all(state);
633 * Given a particular key event, updates the state structure to reflect the
637 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
638 enum xkb_key_direction direction)
642 const struct xkb_key *key = XkbKey(state->keymap, kc);
648 state->clear_mods = 0;
650 xkb_filter_apply_all(state, key, direction);
652 for (i = 0, bit = 1; state->set_mods; i++, bit <<= 1) {
653 if (state->set_mods & bit) {
654 state->mod_key_count[i]++;
655 state->base_mods |= bit;
656 state->set_mods &= ~bit;
660 for (i = 0, bit = 1; state->clear_mods; i++, bit <<= 1) {
661 if (state->clear_mods & bit) {
662 state->mod_key_count[i]--;
663 if (state->mod_key_count[i] <= 0) {
664 state->base_mods &= ~bit;
665 state->mod_key_count[i] = 0;
667 state->clear_mods &= ~bit;
671 xkb_state_update_derived(state);
675 * Updates the state from a set of explicit masks as gained from
676 * xkb_state_serialize_mods and xkb_state_serialize_groups. As noted in the
677 * documentation for these functions in xkbcommon.h, this round-trip is
678 * lossy, and should only be used to update a slave state mirroring the
679 * master, e.g. in a client/server window system.
682 xkb_state_update_mask(struct xkb_state *state,
683 xkb_mod_mask_t base_mods,
684 xkb_mod_mask_t latched_mods,
685 xkb_mod_mask_t locked_mods,
686 xkb_layout_index_t base_group,
687 xkb_layout_index_t latched_group,
688 xkb_layout_index_t locked_group)
690 xkb_mod_index_t num_mods;
693 state->base_mods = 0;
694 state->latched_mods = 0;
695 state->locked_mods = 0;
696 num_mods = xkb_keymap_num_mods(state->keymap);
698 for (idx = 0; idx < num_mods; idx++) {
699 xkb_mod_mask_t mod = (1 << idx);
701 state->base_mods |= mod;
702 if (latched_mods & mod)
703 state->latched_mods |= mod;
704 if (locked_mods & mod)
705 state->locked_mods |= mod;
708 state->base_group = base_group;
709 state->latched_group = latched_group;
710 state->locked_group = locked_group;
712 xkb_state_update_derived(state);
716 * Provides the symbols to use for the given key and state. Returns the
717 * number of symbols pointed to in syms_out.
720 xkb_state_key_get_syms(struct xkb_state *state, xkb_keycode_t kc,
721 const xkb_keysym_t **syms_out)
723 xkb_layout_index_t layout;
724 xkb_level_index_t level;
725 const struct xkb_key *key = XkbKey(state->keymap, kc);
730 layout = xkb_state_key_get_layout(state, kc);
731 if (layout == XKB_LAYOUT_INVALID)
734 level = xkb_state_key_get_level(state, kc, layout);
735 if (level == XKB_LEVEL_INVALID)
738 return xkb_keymap_key_get_syms_by_level(state->keymap, kc, layout, level,
747 * Serialises the requested modifier state into an xkb_mod_mask_t, with all
748 * the same disclaimers as in xkb_state_update_mask.
750 XKB_EXPORT xkb_mod_mask_t
751 xkb_state_serialize_mods(struct xkb_state *state,
752 enum xkb_state_component type)
754 xkb_mod_mask_t ret = 0;
756 if (type == XKB_STATE_EFFECTIVE)
759 if (type & XKB_STATE_DEPRESSED)
760 ret |= state->base_mods;
761 if (type & XKB_STATE_LATCHED)
762 ret |= state->latched_mods;
763 if (type & XKB_STATE_LOCKED)
764 ret |= state->locked_mods;
770 * Serialises the requested group state, with all the same disclaimers as
771 * in xkb_state_update_mask.
773 XKB_EXPORT xkb_layout_index_t
774 xkb_state_serialize_layout(struct xkb_state *state,
775 enum xkb_state_component type)
777 xkb_layout_index_t ret = 0;
779 if (type == XKB_STATE_EFFECTIVE)
782 if (type & XKB_STATE_DEPRESSED)
783 ret += state->base_group;
784 if (type & XKB_STATE_LATCHED)
785 ret += state->latched_group;
786 if (type & XKB_STATE_LOCKED)
787 ret += state->locked_group;
793 * Returns 1 if the given modifier is active with the specified type(s), 0 if
794 * not, or -1 if the modifier is invalid.
797 xkb_state_mod_index_is_active(struct xkb_state *state,
799 enum xkb_state_component type)
803 if (idx >= xkb_keymap_num_mods(state->keymap))
806 if (type & XKB_STATE_EFFECTIVE)
807 return !!(state->mods & (1 << idx));
809 if (type & XKB_STATE_DEPRESSED)
810 ret |= (state->base_mods & (1 << idx));
811 if (type & XKB_STATE_LATCHED)
812 ret |= (state->latched_mods & (1 << idx));
813 if (type & XKB_STATE_LOCKED)
814 ret |= (state->locked_mods & (1 << idx));
820 * Helper function for xkb_state_mod_indices_are_active and
821 * xkb_state_mod_names_are_active.
824 match_mod_masks(struct xkb_state *state, enum xkb_state_match match,
827 uint32_t active = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
829 if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
832 if (match & XKB_STATE_MATCH_ANY)
833 return !!(active & wanted);
835 return (active & wanted) == wanted;
841 * Returns 1 if the modifiers are active with the specified type(s), 0 if
842 * not, or -1 if any of the modifiers are invalid.
845 xkb_state_mod_indices_are_active(struct xkb_state *state,
846 enum xkb_state_component type,
847 enum xkb_state_match match,
851 xkb_mod_index_t idx = 0;
854 xkb_mod_index_t num_mods = xkb_keymap_num_mods(state->keymap);
858 idx = va_arg(ap, xkb_mod_index_t);
859 if (idx == XKB_MOD_INVALID)
861 if (idx >= num_mods) {
865 wanted |= (1 << idx);
872 return match_mod_masks(state, match, wanted);
876 * Returns 1 if the given modifier is active with the specified type(s), 0 if
877 * not, or -1 if the modifier is invalid.
880 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
881 enum xkb_state_component type)
883 xkb_mod_index_t idx = xkb_keymap_mod_get_index(state->keymap, name);
885 if (idx == XKB_MOD_INVALID)
888 return xkb_state_mod_index_is_active(state, idx, type);
892 * Returns 1 if the modifiers are active with the specified type(s), 0 if
893 * not, or -1 if any of the modifiers are invalid.
895 XKB_EXPORT ATTR_NULL_SENTINEL int
896 xkb_state_mod_names_are_active(struct xkb_state *state,
897 enum xkb_state_component type,
898 enum xkb_state_match match,
902 xkb_mod_index_t idx = 0;
909 str = va_arg(ap, const char *);
912 idx = xkb_keymap_mod_get_index(state->keymap, str);
913 if (idx == XKB_MOD_INVALID) {
917 wanted |= (1 << idx);
924 return match_mod_masks(state, match, wanted);
928 * Returns 1 if the given group is active with the specified type(s), 0 if
929 * not, or -1 if the group is invalid.
932 xkb_state_layout_index_is_active(struct xkb_state *state,
933 xkb_layout_index_t idx,
934 enum xkb_state_component type)
938 if (idx >= xkb_keymap_num_layouts(state->keymap))
941 /* Can only have one effective group. */
942 if (type & XKB_STATE_EFFECTIVE)
943 return state->group == idx;
945 if (type & XKB_STATE_DEPRESSED)
946 ret |= (state->base_group == idx);
947 if (type & XKB_STATE_LATCHED)
948 ret |= (state->latched_group == idx);
949 if (type & XKB_STATE_LOCKED)
950 ret |= (state->locked_group == idx);
956 * Returns 1 if the given modifier is active with the specified type(s), 0 if
957 * not, or -1 if the modifier is invalid.
960 xkb_state_layout_name_is_active(struct xkb_state *state, const char *name,
961 enum xkb_state_component type)
963 xkb_layout_index_t idx = xkb_keymap_layout_get_index(state->keymap, name);
965 if (idx == XKB_LAYOUT_INVALID)
968 return xkb_state_layout_index_is_active(state, idx, type);
972 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
975 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
977 if (idx >= xkb_keymap_num_leds(state->keymap))
980 return !!(state->leds & (1 << idx));
984 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
987 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
989 xkb_led_index_t idx = xkb_keymap_led_get_index(state->keymap, name);
991 if (idx == XKB_LED_INVALID)
994 return xkb_state_led_index_is_active(state, idx);
997 static xkb_mod_mask_t
998 key_get_consumed(struct xkb_state *state, const struct xkb_key *key)
1000 struct xkb_kt_map_entry *entry;
1001 xkb_layout_index_t group;
1003 group = xkb_state_key_get_layout(state, key->keycode);
1004 if (group == XKB_LAYOUT_INVALID)
1007 entry = get_entry_for_key_state(state, key, group);
1011 return entry->mods.mask & ~entry->preserve.mask;
1015 * Tests to see if a modifier is used up by our translation of a
1016 * keycode to keysyms, taking note of the current modifier state and
1017 * the appropriate key type's preserve information, if any. This allows
1018 * the user to mask out the modifier in later processing of the
1019 * modifiers, e.g. when implementing hot keys or accelerators.
1021 * See also, for example:
1022 * - XkbTranslateKeyCode(3), mod_rtrn retrun value, from libX11.
1023 * - gdk_keymap_translate_keyboard_state, consumed_modifiers return value,
1027 xkb_state_mod_index_is_consumed(struct xkb_state *state, xkb_keycode_t kc,
1028 xkb_mod_index_t idx)
1030 const struct xkb_key *key = XkbKey(state->keymap, kc);
1035 return !!((1 << idx) & key_get_consumed(state, key));
1039 * Calculates which modifiers should be consumed during key processing,
1040 * and returns the mask with all these modifiers removed. e.g. if
1041 * given a state of Alt and Shift active for a two-level alphabetic
1042 * key containing plus and equal on the first and second level
1043 * respectively, will return a mask of only Alt, as Shift has been
1044 * consumed by the type handling.
1046 XKB_EXPORT xkb_mod_mask_t
1047 xkb_state_mod_mask_remove_consumed(struct xkb_state *state, xkb_keycode_t kc,
1048 xkb_mod_mask_t mask)
1050 const struct xkb_key *key = XkbKey(state->keymap, kc);
1055 return mask & ~key_get_consumed(state, key);