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);
146 * Returns the layout to use for the given key and state, taking
147 * wrapping/clamping/etc into account, or XKB_LAYOUT_INVALID.
149 XKB_EXPORT xkb_layout_index_t
150 xkb_state_key_get_layout(struct xkb_state *state, xkb_keycode_t kc)
152 xkb_layout_index_t ret =
153 xkb_state_serialize_layout(state, XKB_STATE_EFFECTIVE);
154 const struct xkb_key *key = XkbKey(state->keymap, kc);
156 if (!key || key->num_groups == 0)
157 return XKB_LAYOUT_INVALID;
159 if (ret < key->num_groups)
162 switch (key->out_of_range_group_action) {
164 ret = key->out_of_range_group_number;
165 if (ret >= key->num_groups)
170 ret = key->num_groups - 1;
175 ret %= key->num_groups;
182 static const union xkb_action fake = { .type = ACTION_TYPE_NONE };
184 static const union xkb_action *
185 xkb_key_get_action(struct xkb_state *state, const struct xkb_key *key)
187 xkb_layout_index_t layout;
188 xkb_level_index_t level;
193 layout = xkb_state_key_get_layout(state, key->keycode);
194 if (layout == XKB_LAYOUT_INVALID)
197 level = xkb_state_key_get_level(state, key->keycode, layout);
198 if (level == XKB_LEVEL_INVALID)
201 return XkbKeyActionEntry(key, layout, level);
204 static struct xkb_filter *
205 xkb_filter_new(struct xkb_state *state)
207 struct xkb_filter *filter = NULL, *iter;
209 darray_foreach(iter, state->filters) {
217 darray_resize0(state->filters, darray_size(state->filters) + 1);
218 filter = &darray_item(state->filters, darray_size(state->filters) -1);
225 /***====================================================================***/
228 xkb_filter_group_set_func(struct xkb_state *state,
229 struct xkb_filter *filter,
230 const struct xkb_key *key,
231 enum xkb_key_direction direction)
233 if (key != filter->key) {
234 filter->action.group.flags &= ~ACTION_LOCK_CLEAR;
238 if (direction == XKB_KEY_DOWN) {
242 else if (--filter->refcnt > 0) {
246 state->base_group = filter->priv;
248 if (filter->action.group.flags & ACTION_LOCK_CLEAR)
249 state->locked_group = 0;
256 xkb_filter_group_set_new(struct xkb_state *state, struct xkb_filter *filter)
258 filter->priv = state->base_group;
259 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
260 state->base_group = filter->action.group.group;
262 state->base_group += filter->action.group.group;
266 xkb_filter_group_lock_func(struct xkb_state *state,
267 struct xkb_filter *filter,
268 const struct xkb_key *key,
269 enum xkb_key_direction direction)
271 if (key != filter->key)
274 if (direction == XKB_KEY_DOWN) {
278 if (--filter->refcnt > 0)
286 xkb_filter_group_lock_new(struct xkb_state *state, struct xkb_filter *filter)
288 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
289 state->locked_group = filter->action.group.group;
291 state->locked_group += filter->action.group.group;
295 xkb_filter_mod_set_func(struct xkb_state *state,
296 struct xkb_filter *filter,
297 const struct xkb_key *key,
298 enum xkb_key_direction direction)
300 if (key != filter->key) {
301 filter->action.mods.flags &= ~ACTION_LOCK_CLEAR;
305 if (direction == XKB_KEY_DOWN) {
309 else if (--filter->refcnt > 0) {
313 state->clear_mods = filter->action.mods.mods.mask;
314 if (filter->action.mods.flags & ACTION_LOCK_CLEAR)
315 state->locked_mods &= ~filter->action.mods.mods.mask;
322 xkb_filter_mod_set_new(struct xkb_state *state, struct xkb_filter *filter)
324 state->set_mods = filter->action.mods.mods.mask;
328 xkb_filter_mod_lock_func(struct xkb_state *state,
329 struct xkb_filter *filter,
330 const struct xkb_key *key,
331 enum xkb_key_direction direction)
333 if (key != filter->key)
336 if (direction == XKB_KEY_DOWN) {
340 if (--filter->refcnt > 0)
343 state->clear_mods |= filter->action.mods.mods.mask;
344 if (!(filter->action.mods.flags & ACTION_LOCK_NO_UNLOCK))
345 state->locked_mods &= ~filter->priv;
352 xkb_filter_mod_lock_new(struct xkb_state *state, struct xkb_filter *filter)
354 filter->priv = state->locked_mods & filter->action.mods.mods.mask;
355 state->set_mods |= filter->action.mods.mods.mask;
356 if (!(filter->action.mods.flags & ACTION_LOCK_NO_LOCK))
357 state->locked_mods |= filter->action.mods.mods.mask;
360 enum xkb_key_latch_state {
367 xkb_action_breaks_latch(const union xkb_action *action)
369 switch (action->type) {
370 case ACTION_TYPE_NONE:
371 case ACTION_TYPE_PTR_BUTTON:
372 case ACTION_TYPE_PTR_LOCK:
373 case ACTION_TYPE_CTRL_SET:
374 case ACTION_TYPE_CTRL_LOCK:
375 case ACTION_TYPE_KEY_REDIRECT:
376 case ACTION_TYPE_SWITCH_VT:
377 case ACTION_TYPE_TERMINATE:
385 xkb_filter_mod_latch_func(struct xkb_state *state,
386 struct xkb_filter *filter,
387 const struct xkb_key *key,
388 enum xkb_key_direction direction)
390 enum xkb_key_latch_state latch = filter->priv;
392 if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
393 /* If this is a new keypress and we're awaiting our single latched
394 * keypress, then either break the latch if any random key is pressed,
395 * or promote it to a lock or plain base set if it's the same
397 const union xkb_action *action = xkb_key_get_action(state, key);
398 if (action->type == ACTION_TYPE_MOD_LATCH &&
399 action->mods.flags == filter->action.mods.flags &&
400 action->mods.mods.mask == filter->action.mods.mods.mask) {
401 filter->action = *action;
402 if (filter->action.mods.flags & ACTION_LATCH_TO_LOCK) {
403 filter->action.type = ACTION_TYPE_MOD_LOCK;
404 filter->func = xkb_filter_mod_lock_func;
405 state->locked_mods |= filter->action.mods.mods.mask;
408 filter->action.type = ACTION_TYPE_MOD_SET;
409 filter->func = xkb_filter_mod_set_func;
410 state->set_mods = filter->action.mods.mods.mask;
413 state->latched_mods &= ~filter->action.mods.mods.mask;
417 else if (xkb_action_breaks_latch(action)) {
418 /* XXX: This may be totally broken, we might need to break the
419 * latch in the next run after this press? */
420 state->latched_mods &= ~filter->action.mods.mods.mask;
425 else if (direction == XKB_KEY_UP && key == filter->key) {
426 /* Our key got released. If we've set it to clear locks, and we
427 * currently have the same modifiers locked, then release them and
428 * don't actually latch. Else we've actually hit the latching
429 * stage, so set PENDING and move our modifier from base to
431 if (latch == NO_LATCH ||
432 ((filter->action.mods.flags & ACTION_LOCK_CLEAR) &&
433 (state->locked_mods & filter->action.mods.mods.mask) ==
434 filter->action.mods.mods.mask)) {
435 /* XXX: We might be a bit overenthusiastic about clearing
436 * mods other filters have set here? */
437 if (latch == LATCH_PENDING)
438 state->latched_mods &= ~filter->action.mods.mods.mask;
440 state->clear_mods = filter->action.mods.mods.mask;
441 state->locked_mods &= ~filter->action.mods.mods.mask;
445 latch = LATCH_PENDING;
446 state->clear_mods = filter->action.mods.mods.mask;
447 state->latched_mods |= filter->action.mods.mods.mask;
451 else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
452 /* Someone's pressed another key while we've still got the latching
453 * key held down, so keep the base modifier state active (from
454 * xkb_filter_mod_latch_new), but don't trip the latch, just clear
455 * it as soon as the modifier gets released. */
459 filter->priv = latch;
465 xkb_filter_mod_latch_new(struct xkb_state *state, struct xkb_filter *filter)
467 filter->priv = LATCH_KEY_DOWN;
468 state->set_mods = filter->action.mods.mods.mask;
471 static const struct {
472 void (*new)(struct xkb_state *state, struct xkb_filter *filter);
473 int (*func)(struct xkb_state *state, struct xkb_filter *filter,
474 const struct xkb_key *key, enum xkb_key_direction direction);
475 } filter_action_funcs[_ACTION_TYPE_NUM_ENTRIES] = {
476 [ACTION_TYPE_MOD_SET] = { xkb_filter_mod_set_new,
477 xkb_filter_mod_set_func },
478 [ACTION_TYPE_MOD_LATCH] = { xkb_filter_mod_latch_new,
479 xkb_filter_mod_latch_func },
480 [ACTION_TYPE_MOD_LOCK] = { xkb_filter_mod_lock_new,
481 xkb_filter_mod_lock_func },
482 [ACTION_TYPE_GROUP_SET] = { xkb_filter_group_set_new,
483 xkb_filter_group_set_func },
484 [ACTION_TYPE_GROUP_LOCK] = { xkb_filter_group_lock_new,
485 xkb_filter_group_lock_func },
489 * Applies any relevant filters to the key, first from the list of filters
490 * that are currently active, then if no filter has claimed the key, possibly
491 * apply a new filter from the key action.
494 xkb_filter_apply_all(struct xkb_state *state,
495 const struct xkb_key *key,
496 enum xkb_key_direction direction)
498 struct xkb_filter *filter;
499 const union xkb_action *action;
502 /* First run through all the currently active filters and see if any of
503 * them have claimed this event. */
504 darray_foreach(filter, state->filters) {
507 send &= filter->func(state, filter, key, direction);
510 if (!send || direction == XKB_KEY_UP)
513 action = xkb_key_get_action(state, key);
514 if (!filter_action_funcs[action->type].new)
517 filter = xkb_filter_new(state);
522 filter->func = filter_action_funcs[action->type].func;
523 filter->action = *action;
524 filter_action_funcs[action->type].new(state, filter);
527 XKB_EXPORT struct xkb_state *
528 xkb_state_new(struct xkb_keymap *keymap)
530 struct xkb_state *ret;
532 ret = calloc(sizeof(*ret), 1);
537 ret->keymap = xkb_keymap_ref(keymap);
542 XKB_EXPORT struct xkb_state *
543 xkb_state_ref(struct xkb_state *state)
550 xkb_state_unref(struct xkb_state *state)
552 if (--state->refcnt > 0)
555 xkb_keymap_unref(state->keymap);
556 darray_free(state->filters);
560 XKB_EXPORT struct xkb_keymap *
561 xkb_state_get_keymap(struct xkb_state *state)
563 return state->keymap;
567 * Update the LED state to match the rest of the xkb_state.
570 xkb_state_led_update_all(struct xkb_state *state)
576 for (led = 0; led < XKB_NUM_INDICATORS; led++) {
577 struct xkb_indicator_map *map = &state->keymap->indicators[led];
578 xkb_mod_mask_t mod_mask = 0;
579 uint32_t group_mask = 0;
581 if (map->which_mods & XKB_STATE_DEPRESSED)
582 mod_mask |= state->base_mods;
583 if (map->which_mods & XKB_STATE_LATCHED)
584 mod_mask |= state->latched_mods;
585 if (map->which_mods & XKB_STATE_LOCKED)
586 mod_mask |= state->locked_mods;
587 if ((map->mods.mask & mod_mask))
588 state->leds |= (1 << led);
590 if (map->which_groups & XKB_STATE_DEPRESSED)
591 group_mask |= (1 << state->base_group);
592 if (map->which_groups & XKB_STATE_LATCHED)
593 group_mask |= (1 << state->latched_group);
594 if (map->which_groups & XKB_STATE_LOCKED)
595 group_mask |= (1 << state->locked_group);
596 if ((map->groups & group_mask))
597 state->leds |= (1 << led);
600 if ((map->ctrls & state->keymap->enabled_ctrls))
601 state->leds |= (1 << led);
607 * Calculates the derived state (effective mods/group and LEDs) from an
608 * up-to-date xkb_state.
611 xkb_state_update_derived(struct xkb_state *state)
614 (state->base_mods | state->latched_mods | state->locked_mods);
615 /* FIXME: Clamp/wrap locked_group */
616 state->group = state->locked_group + state->base_group +
617 state->latched_group;
618 /* FIXME: Clamp/wrap effective group */
620 xkb_state_led_update_all(state);
624 * Given a particular key event, updates the state structure to reflect the
628 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
629 enum xkb_key_direction direction)
633 const struct xkb_key *key = XkbKey(state->keymap, kc);
639 state->clear_mods = 0;
641 xkb_filter_apply_all(state, key, direction);
643 for (i = 0, bit = 1; state->set_mods; i++, bit <<= 1) {
644 if (state->set_mods & bit) {
645 state->mod_key_count[i]++;
646 state->base_mods |= bit;
647 state->set_mods &= ~bit;
651 for (i = 0, bit = 1; state->clear_mods; i++, bit <<= 1) {
652 if (state->clear_mods & bit) {
653 state->mod_key_count[i]--;
654 if (state->mod_key_count[i] <= 0) {
655 state->base_mods &= ~bit;
656 state->mod_key_count[i] = 0;
658 state->clear_mods &= ~bit;
662 xkb_state_update_derived(state);
666 * Updates the state from a set of explicit masks as gained from
667 * xkb_state_serialize_mods and xkb_state_serialize_groups. As noted in the
668 * documentation for these functions in xkbcommon.h, this round-trip is
669 * lossy, and should only be used to update a slave state mirroring the
670 * master, e.g. in a client/server window system.
673 xkb_state_update_mask(struct xkb_state *state,
674 xkb_mod_mask_t base_mods,
675 xkb_mod_mask_t latched_mods,
676 xkb_mod_mask_t locked_mods,
677 xkb_layout_index_t base_group,
678 xkb_layout_index_t latched_group,
679 xkb_layout_index_t locked_group)
681 xkb_mod_index_t num_mods;
684 state->base_mods = 0;
685 state->latched_mods = 0;
686 state->locked_mods = 0;
687 num_mods = xkb_keymap_num_mods(state->keymap);
689 for (idx = 0; idx < num_mods; idx++) {
690 xkb_mod_mask_t mod = (1 << idx);
692 state->base_mods |= mod;
693 if (latched_mods & mod)
694 state->latched_mods |= mod;
695 if (locked_mods & mod)
696 state->locked_mods |= mod;
699 state->base_group = base_group;
700 state->latched_group = latched_group;
701 state->locked_group = locked_group;
703 xkb_state_update_derived(state);
707 * Provides the symbols to use for the given key and state. Returns the
708 * number of symbols pointed to in syms_out.
711 xkb_state_key_get_syms(struct xkb_state *state, xkb_keycode_t kc,
712 const xkb_keysym_t **syms_out)
714 xkb_layout_index_t layout;
715 xkb_level_index_t level;
716 const struct xkb_key *key = XkbKey(state->keymap, kc);
721 layout = xkb_state_key_get_layout(state, kc);
722 if (layout == XKB_LAYOUT_INVALID)
725 level = xkb_state_key_get_level(state, kc, layout);
726 if (level == XKB_LEVEL_INVALID)
729 return xkb_keymap_key_get_syms_by_level(state->keymap, kc, layout, level,
738 * Serialises the requested modifier state into an xkb_mod_mask_t, with all
739 * the same disclaimers as in xkb_state_update_mask.
741 XKB_EXPORT xkb_mod_mask_t
742 xkb_state_serialize_mods(struct xkb_state *state,
743 enum xkb_state_component type)
745 xkb_mod_mask_t ret = 0;
747 if (type == XKB_STATE_EFFECTIVE)
750 if (type & XKB_STATE_DEPRESSED)
751 ret |= state->base_mods;
752 if (type & XKB_STATE_LATCHED)
753 ret |= state->latched_mods;
754 if (type & XKB_STATE_LOCKED)
755 ret |= state->locked_mods;
761 * Serialises the requested group state, with all the same disclaimers as
762 * in xkb_state_update_mask.
764 XKB_EXPORT xkb_layout_index_t
765 xkb_state_serialize_layout(struct xkb_state *state,
766 enum xkb_state_component type)
768 xkb_layout_index_t ret = 0;
770 if (type == XKB_STATE_EFFECTIVE)
773 if (type & XKB_STATE_DEPRESSED)
774 ret += state->base_group;
775 if (type & XKB_STATE_LATCHED)
776 ret += state->latched_group;
777 if (type & XKB_STATE_LOCKED)
778 ret += state->locked_group;
784 * Returns 1 if the given modifier is active with the specified type(s), 0 if
785 * not, or -1 if the modifier is invalid.
788 xkb_state_mod_index_is_active(struct xkb_state *state,
790 enum xkb_state_component type)
794 if (idx >= xkb_keymap_num_mods(state->keymap))
797 if (type & XKB_STATE_DEPRESSED)
798 ret |= (state->base_mods & (1 << idx));
799 if (type & XKB_STATE_LATCHED)
800 ret |= (state->latched_mods & (1 << idx));
801 if (type & XKB_STATE_LOCKED)
802 ret |= (state->locked_mods & (1 << idx));
808 * Helper function for xkb_state_mod_indices_are_active and
809 * xkb_state_mod_names_are_active.
812 match_mod_masks(struct xkb_state *state, enum xkb_state_match match,
815 uint32_t active = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
817 if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
820 if (match & XKB_STATE_MATCH_ANY)
821 return !!(active & wanted);
823 return (active & wanted) == wanted;
829 * Returns 1 if the modifiers are active with the specified type(s), 0 if
830 * not, or -1 if any of the modifiers are invalid.
833 xkb_state_mod_indices_are_active(struct xkb_state *state,
834 enum xkb_state_component type,
835 enum xkb_state_match match,
839 xkb_mod_index_t idx = 0;
842 xkb_mod_index_t num_mods = xkb_keymap_num_mods(state->keymap);
846 idx = va_arg(ap, xkb_mod_index_t);
847 if (idx == XKB_MOD_INVALID)
849 if (idx >= num_mods) {
853 wanted |= (1 << idx);
860 return match_mod_masks(state, match, wanted);
864 * Returns 1 if the given modifier is active with the specified type(s), 0 if
865 * not, or -1 if the modifier is invalid.
868 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
869 enum xkb_state_component type)
871 xkb_mod_index_t idx = xkb_keymap_mod_get_index(state->keymap, name);
873 if (idx == XKB_MOD_INVALID)
876 return xkb_state_mod_index_is_active(state, idx, type);
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.
883 XKB_EXPORT ATTR_NULL_SENTINEL int
884 xkb_state_mod_names_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;
897 str = va_arg(ap, const char *);
900 idx = xkb_keymap_mod_get_index(state->keymap, str);
901 if (idx == XKB_MOD_INVALID) {
905 wanted |= (1 << idx);
912 return match_mod_masks(state, match, wanted);
916 * Returns 1 if the given group is active with the specified type(s), 0 if
917 * not, or -1 if the group is invalid.
920 xkb_state_layout_index_is_active(struct xkb_state *state,
921 xkb_layout_index_t idx,
922 enum xkb_state_component type)
926 if (idx >= xkb_keymap_num_layouts(state->keymap))
929 if (type & XKB_STATE_DEPRESSED)
930 ret |= (state->base_group == idx);
931 if (type & XKB_STATE_LATCHED)
932 ret |= (state->latched_group == idx);
933 if (type & XKB_STATE_LOCKED)
934 ret |= (state->locked_group == idx);
940 * Returns 1 if the given modifier is active with the specified type(s), 0 if
941 * not, or -1 if the modifier is invalid.
944 xkb_state_layout_name_is_active(struct xkb_state *state, const char *name,
945 enum xkb_state_component type)
947 xkb_layout_index_t idx = xkb_keymap_layout_get_index(state->keymap, name);
949 if (idx == XKB_LAYOUT_INVALID)
952 return xkb_state_layout_index_is_active(state, idx, type);
956 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
959 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
961 if (idx >= xkb_keymap_num_leds(state->keymap))
964 return !!(state->leds & (1 << idx));
968 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
971 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
973 xkb_led_index_t idx = xkb_keymap_led_get_index(state->keymap, name);
975 if (idx == XKB_LED_INVALID)
978 return xkb_state_led_index_is_active(state, idx);
981 static xkb_mod_mask_t
982 key_get_consumed(struct xkb_state *state, const struct xkb_key *key)
984 struct xkb_kt_map_entry *entry;
985 xkb_layout_index_t group;
987 group = xkb_state_key_get_layout(state, key->keycode);
988 if (group == XKB_LAYOUT_INVALID)
991 entry = get_entry_for_key_state(state, key, group);
995 return entry->mods.mask & ~entry->preserve.mask;
999 * Tests to see if a modifier is used up by our translation of a
1000 * keycode to keysyms, taking note of the current modifier state and
1001 * the appropriate key type's preserve information, if any. This allows
1002 * the user to mask out the modifier in later processing of the
1003 * modifiers, e.g. when implementing hot keys or accelerators.
1005 * See also, for example:
1006 * - XkbTranslateKeyCode(3), mod_rtrn retrun value, from libX11.
1007 * - gdk_keymap_translate_keyboard_state, consumed_modifiers return value,
1011 xkb_state_mod_index_is_consumed(struct xkb_state *state, xkb_keycode_t kc,
1012 xkb_mod_index_t idx)
1014 const struct xkb_key *key = XkbKey(state->keymap, kc);
1019 return !!((1 << idx) & key_get_consumed(state, key));
1023 * Calculates which modifiers should be consumed during key processing,
1024 * and returns the mask with all these modifiers removed. e.g. if
1025 * given a state of Alt and Shift active for a two-level alphabetic
1026 * key containing plus and equal on the first and second level
1027 * respectively, will return a mask of only Alt, as Shift has been
1028 * consumed by the type handling.
1030 XKB_EXPORT xkb_mod_mask_t
1031 xkb_state_mod_mask_remove_consumed(struct xkb_state *state, xkb_keycode_t kc,
1032 xkb_mod_mask_t mask)
1034 const struct xkb_key *key = XkbKey(state->keymap, kc);
1039 return mask & ~key_get_consumed(state, key);