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);
75 struct state_components {
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 */
92 * Before updating the state, we keep a copy of just this struct. This
93 * allows us to report which components of the state have changed.
95 struct state_components components;
98 * At each event, we accumulate all the needed modifications to the base
99 * modifiers, and apply them at the end. These keep track of this state.
101 xkb_mod_mask_t set_mods;
102 xkb_mod_mask_t clear_mods;
105 * We mustn't clear a base modifier if there's another depressed key
106 * which affects it, e.g. given this sequence
107 * < Left Shift down, Right Shift down, Left Shift Up >
108 * the modifier should still be set. This keeps the count.
110 int16_t mod_key_count[sizeof(xkb_mod_mask_t) * 8];
113 darray(struct xkb_filter) filters;
114 struct xkb_keymap *keymap;
117 static const struct xkb_kt_map_entry *
118 get_entry_for_key_state(struct xkb_state *state, const struct xkb_key *key,
119 xkb_layout_index_t group)
121 const struct xkb_key_type *type = key->groups[group].type;
122 xkb_mod_mask_t active_mods = state->components.mods & type->mods.mask;
125 for (i = 0; i < type->num_entries; i++)
126 if (type->map[i].mods.mask == active_mods)
127 return &type->map[i];
133 * Returns the level to use for the given key and state, or
136 XKB_EXPORT xkb_level_index_t
137 xkb_state_key_get_level(struct xkb_state *state, xkb_keycode_t kc,
138 xkb_layout_index_t layout)
140 const struct xkb_key *key = XkbKey(state->keymap, kc);
141 const struct xkb_kt_map_entry *entry;
143 if (!key || layout >= key->num_groups)
144 return XKB_LEVEL_INVALID;
146 /* If we don't find an explicit match the default is 0. */
147 entry = get_entry_for_key_state(state, key, layout);
155 wrap_group_into_range(int32_t group,
156 xkb_layout_index_t num_groups,
157 enum xkb_range_exceed_type out_of_range_group_action,
158 xkb_layout_index_t out_of_range_group_number)
161 return XKB_LAYOUT_INVALID;
163 if (group < num_groups)
166 switch (out_of_range_group_action) {
168 if (out_of_range_group_number >= num_groups)
170 return out_of_range_group_number;
176 return num_groups - 1;
181 * C99 says a negative dividend in a modulo operation always
182 * gives a negative result.
185 return ((int) num_groups + (group % (int) num_groups));
187 return group % num_groups;
192 * Returns the layout to use for the given key and state, taking
193 * wrapping/clamping/etc into account, or XKB_LAYOUT_INVALID.
195 XKB_EXPORT xkb_layout_index_t
196 xkb_state_key_get_layout(struct xkb_state *state, xkb_keycode_t kc)
198 const struct xkb_key *key = XkbKey(state->keymap, kc);
201 return XKB_LAYOUT_INVALID;
203 return wrap_group_into_range(state->components.group, key->num_groups,
204 key->out_of_range_group_action,
205 key->out_of_range_group_number);
208 static const union xkb_action fake = { .type = ACTION_TYPE_NONE };
210 static const union xkb_action *
211 xkb_key_get_action(struct xkb_state *state, const struct xkb_key *key)
213 xkb_layout_index_t layout;
214 xkb_level_index_t level;
216 layout = xkb_state_key_get_layout(state, key->keycode);
217 if (layout == XKB_LAYOUT_INVALID)
220 level = xkb_state_key_get_level(state, key->keycode, layout);
221 if (level == XKB_LEVEL_INVALID)
224 return &key->groups[layout].levels[level].action;
227 static struct xkb_filter *
228 xkb_filter_new(struct xkb_state *state)
230 struct xkb_filter *filter = NULL, *iter;
232 darray_foreach(iter, state->filters) {
240 darray_resize0(state->filters, darray_size(state->filters) + 1);
241 filter = &darray_item(state->filters, darray_size(state->filters) -1);
248 /***====================================================================***/
251 xkb_filter_group_set_func(struct xkb_state *state,
252 struct xkb_filter *filter,
253 const struct xkb_key *key,
254 enum xkb_key_direction direction)
256 if (key != filter->key) {
257 filter->action.group.flags &= ~ACTION_LOCK_CLEAR;
261 if (direction == XKB_KEY_DOWN) {
265 else if (--filter->refcnt > 0) {
269 state->components.base_group = filter->priv;
271 if (filter->action.group.flags & ACTION_LOCK_CLEAR)
272 state->components.locked_group = 0;
279 xkb_filter_group_set_new(struct xkb_state *state, struct xkb_filter *filter)
281 filter->priv = state->components.base_group;
282 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
283 state->components.base_group = filter->action.group.group;
285 state->components.base_group += filter->action.group.group;
289 xkb_filter_group_lock_func(struct xkb_state *state,
290 struct xkb_filter *filter,
291 const struct xkb_key *key,
292 enum xkb_key_direction direction)
294 if (key != filter->key)
297 if (direction == XKB_KEY_DOWN) {
301 if (--filter->refcnt > 0)
309 xkb_filter_group_lock_new(struct xkb_state *state, struct xkb_filter *filter)
311 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
312 state->components.locked_group = filter->action.group.group;
314 state->components.locked_group += filter->action.group.group;
318 xkb_filter_mod_set_func(struct xkb_state *state,
319 struct xkb_filter *filter,
320 const struct xkb_key *key,
321 enum xkb_key_direction direction)
323 if (key != filter->key) {
324 filter->action.mods.flags &= ~ACTION_LOCK_CLEAR;
328 if (direction == XKB_KEY_DOWN) {
332 else if (--filter->refcnt > 0) {
336 state->clear_mods = filter->action.mods.mods.mask;
337 if (filter->action.mods.flags & ACTION_LOCK_CLEAR)
338 state->components.locked_mods &= ~filter->action.mods.mods.mask;
345 xkb_filter_mod_set_new(struct xkb_state *state, struct xkb_filter *filter)
347 state->set_mods = filter->action.mods.mods.mask;
351 xkb_filter_mod_lock_func(struct xkb_state *state,
352 struct xkb_filter *filter,
353 const struct xkb_key *key,
354 enum xkb_key_direction direction)
356 if (key != filter->key)
359 if (direction == XKB_KEY_DOWN) {
363 if (--filter->refcnt > 0)
366 state->clear_mods |= filter->action.mods.mods.mask;
367 if (!(filter->action.mods.flags & ACTION_LOCK_NO_UNLOCK))
368 state->components.locked_mods &= ~filter->priv;
375 xkb_filter_mod_lock_new(struct xkb_state *state, struct xkb_filter *filter)
377 filter->priv = (state->components.locked_mods &
378 filter->action.mods.mods.mask);
379 state->set_mods |= filter->action.mods.mods.mask;
380 if (!(filter->action.mods.flags & ACTION_LOCK_NO_LOCK))
381 state->components.locked_mods |= filter->action.mods.mods.mask;
384 enum xkb_key_latch_state {
391 xkb_action_breaks_latch(const union xkb_action *action)
393 switch (action->type) {
394 case ACTION_TYPE_NONE:
395 case ACTION_TYPE_PTR_BUTTON:
396 case ACTION_TYPE_PTR_LOCK:
397 case ACTION_TYPE_CTRL_SET:
398 case ACTION_TYPE_CTRL_LOCK:
399 case ACTION_TYPE_KEY_REDIRECT:
400 case ACTION_TYPE_SWITCH_VT:
401 case ACTION_TYPE_TERMINATE:
409 xkb_filter_mod_latch_func(struct xkb_state *state,
410 struct xkb_filter *filter,
411 const struct xkb_key *key,
412 enum xkb_key_direction direction)
414 enum xkb_key_latch_state latch = filter->priv;
416 if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
417 /* If this is a new keypress and we're awaiting our single latched
418 * keypress, then either break the latch if any random key is pressed,
419 * or promote it to a lock or plain base set if it's the same
421 const union xkb_action *action = xkb_key_get_action(state, key);
422 if (action->type == ACTION_TYPE_MOD_LATCH &&
423 action->mods.flags == filter->action.mods.flags &&
424 action->mods.mods.mask == filter->action.mods.mods.mask) {
425 filter->action = *action;
426 if (filter->action.mods.flags & ACTION_LATCH_TO_LOCK) {
427 filter->action.type = ACTION_TYPE_MOD_LOCK;
428 filter->func = xkb_filter_mod_lock_func;
429 state->components.locked_mods |= filter->action.mods.mods.mask;
432 filter->action.type = ACTION_TYPE_MOD_SET;
433 filter->func = xkb_filter_mod_set_func;
434 state->set_mods = filter->action.mods.mods.mask;
437 state->components.latched_mods &= ~filter->action.mods.mods.mask;
441 else if (xkb_action_breaks_latch(action)) {
442 /* XXX: This may be totally broken, we might need to break the
443 * latch in the next run after this press? */
444 state->components.latched_mods &= ~filter->action.mods.mods.mask;
449 else if (direction == XKB_KEY_UP && key == filter->key) {
450 /* Our key got released. If we've set it to clear locks, and we
451 * currently have the same modifiers locked, then release them and
452 * don't actually latch. Else we've actually hit the latching
453 * stage, so set PENDING and move our modifier from base to
455 if (latch == NO_LATCH ||
456 ((filter->action.mods.flags & ACTION_LOCK_CLEAR) &&
457 (state->components.locked_mods & filter->action.mods.mods.mask) ==
458 filter->action.mods.mods.mask)) {
459 /* XXX: We might be a bit overenthusiastic about clearing
460 * mods other filters have set here? */
461 if (latch == LATCH_PENDING)
462 state->components.latched_mods &=
463 ~filter->action.mods.mods.mask;
465 state->clear_mods = filter->action.mods.mods.mask;
466 state->components.locked_mods &= ~filter->action.mods.mods.mask;
470 latch = LATCH_PENDING;
471 state->clear_mods = filter->action.mods.mods.mask;
472 state->components.latched_mods |= filter->action.mods.mods.mask;
476 else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
477 /* Someone's pressed another key while we've still got the latching
478 * key held down, so keep the base modifier state active (from
479 * xkb_filter_mod_latch_new), but don't trip the latch, just clear
480 * it as soon as the modifier gets released. */
484 filter->priv = latch;
490 xkb_filter_mod_latch_new(struct xkb_state *state, struct xkb_filter *filter)
492 filter->priv = LATCH_KEY_DOWN;
493 state->set_mods = filter->action.mods.mods.mask;
496 static const struct {
497 void (*new)(struct xkb_state *state, struct xkb_filter *filter);
498 int (*func)(struct xkb_state *state, struct xkb_filter *filter,
499 const struct xkb_key *key, enum xkb_key_direction direction);
500 } filter_action_funcs[_ACTION_TYPE_NUM_ENTRIES] = {
501 [ACTION_TYPE_MOD_SET] = { xkb_filter_mod_set_new,
502 xkb_filter_mod_set_func },
503 [ACTION_TYPE_MOD_LATCH] = { xkb_filter_mod_latch_new,
504 xkb_filter_mod_latch_func },
505 [ACTION_TYPE_MOD_LOCK] = { xkb_filter_mod_lock_new,
506 xkb_filter_mod_lock_func },
507 [ACTION_TYPE_GROUP_SET] = { xkb_filter_group_set_new,
508 xkb_filter_group_set_func },
509 [ACTION_TYPE_GROUP_LOCK] = { xkb_filter_group_lock_new,
510 xkb_filter_group_lock_func },
514 * Applies any relevant filters to the key, first from the list of filters
515 * that are currently active, then if no filter has claimed the key, possibly
516 * apply a new filter from the key action.
519 xkb_filter_apply_all(struct xkb_state *state,
520 const struct xkb_key *key,
521 enum xkb_key_direction direction)
523 struct xkb_filter *filter;
524 const union xkb_action *action;
527 /* First run through all the currently active filters and see if any of
528 * them have claimed this event. */
529 darray_foreach(filter, state->filters) {
532 send &= filter->func(state, filter, key, direction);
535 if (!send || direction == XKB_KEY_UP)
538 action = xkb_key_get_action(state, key);
541 * It's possible for the keymap to set action->type explicitly, like so:
542 * interpret XF86_Next_VMode {
543 * action = Private(type=0x86, data="+VMode");
545 * We don't handle those.
547 if (action->type >= _ACTION_TYPE_NUM_ENTRIES)
550 if (!filter_action_funcs[action->type].new)
553 filter = xkb_filter_new(state);
558 filter->func = filter_action_funcs[action->type].func;
559 filter->action = *action;
560 filter_action_funcs[action->type].new(state, filter);
563 XKB_EXPORT struct xkb_state *
564 xkb_state_new(struct xkb_keymap *keymap)
566 struct xkb_state *ret;
568 ret = calloc(sizeof(*ret), 1);
573 ret->keymap = xkb_keymap_ref(keymap);
578 XKB_EXPORT struct xkb_state *
579 xkb_state_ref(struct xkb_state *state)
586 xkb_state_unref(struct xkb_state *state)
588 if (!state || --state->refcnt > 0)
591 xkb_keymap_unref(state->keymap);
592 darray_free(state->filters);
596 XKB_EXPORT struct xkb_keymap *
597 xkb_state_get_keymap(struct xkb_state *state)
599 return state->keymap;
603 * Update the LED state to match the rest of the xkb_state.
606 xkb_state_led_update_all(struct xkb_state *state)
609 const struct xkb_indicator_map *map;
611 state->components.leds = 0;
613 darray_enumerate(led, map, state->keymap->indicators) {
614 xkb_mod_mask_t mod_mask = 0;
615 xkb_layout_mask_t group_mask = 0;
617 if (map->which_mods & XKB_STATE_MODS_EFFECTIVE)
618 mod_mask |= state->components.mods;
619 if (map->which_mods & XKB_STATE_MODS_DEPRESSED)
620 mod_mask |= state->components.base_mods;
621 if (map->which_mods & XKB_STATE_MODS_LATCHED)
622 mod_mask |= state->components.latched_mods;
623 if (map->which_mods & XKB_STATE_MODS_LOCKED)
624 mod_mask |= state->components.locked_mods;
625 if (map->mods.mask & mod_mask)
626 state->components.leds |= (1 << led);
628 if (map->which_groups & XKB_STATE_LAYOUT_EFFECTIVE)
629 group_mask |= (1 << state->components.group);
630 if (map->which_groups & XKB_STATE_LAYOUT_DEPRESSED)
631 group_mask |= (1 << state->components.base_group);
632 if (map->which_groups & XKB_STATE_LAYOUT_LATCHED)
633 group_mask |= (1 << state->components.latched_group);
634 if (map->which_groups & XKB_STATE_LAYOUT_LOCKED)
635 group_mask |= (1 << state->components.locked_group);
636 if (map->groups & group_mask)
637 state->components.leds |= (1 << led);
639 if (map->ctrls & state->keymap->enabled_ctrls)
640 state->components.leds |= (1 << led);
645 * Calculates the derived state (effective mods/group and LEDs) from an
646 * up-to-date xkb_state.
649 xkb_state_update_derived(struct xkb_state *state)
651 state->components.mods = (state->components.base_mods |
652 state->components.latched_mods |
653 state->components.locked_mods);
655 /* TODO: Use groups_wrap control instead of always RANGE_WRAP. */
657 state->components.locked_group = wrap_group_into_range(state->components.locked_group,
658 state->keymap->num_groups,
661 state->components.group = wrap_group_into_range(state->components.base_group +
662 state->components.latched_group +
663 state->components.locked_group,
664 state->keymap->num_groups,
667 xkb_state_led_update_all(state);
670 static enum xkb_state_component
671 get_state_component_changes(const struct state_components *a,
672 const struct state_components *b)
674 xkb_mod_mask_t mask = 0;
676 if (a->group != b->group)
677 mask |= XKB_STATE_LAYOUT_EFFECTIVE;
678 if (a->base_group != b->base_group)
679 mask |= XKB_STATE_LAYOUT_DEPRESSED;
680 if (a->latched_group != b->latched_group)
681 mask |= XKB_STATE_LAYOUT_LATCHED;
682 if (a->locked_group != b->locked_group)
683 mask |= XKB_STATE_LAYOUT_LOCKED;
684 if (a->mods != b->mods)
685 mask |= XKB_STATE_MODS_EFFECTIVE;
686 if (a->base_mods != b->base_mods)
687 mask |= XKB_STATE_MODS_DEPRESSED;
688 if (a->latched_mods != b->latched_mods)
689 mask |= XKB_STATE_MODS_LATCHED;
690 if (a->locked_mods != b->locked_mods)
691 mask |= XKB_STATE_MODS_LOCKED;
692 if (a->leds != b->leds)
693 mask |= XKB_STATE_LEDS;
699 * Given a particular key event, updates the state structure to reflect the
702 XKB_EXPORT enum xkb_state_component
703 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
704 enum xkb_key_direction direction)
708 struct state_components prev_components;
709 const struct xkb_key *key = XkbKey(state->keymap, kc);
714 prev_components = state->components;
717 state->clear_mods = 0;
719 xkb_filter_apply_all(state, key, direction);
721 for (i = 0, bit = 1; state->set_mods; i++, bit <<= 1) {
722 if (state->set_mods & bit) {
723 state->mod_key_count[i]++;
724 state->components.base_mods |= bit;
725 state->set_mods &= ~bit;
729 for (i = 0, bit = 1; state->clear_mods; i++, bit <<= 1) {
730 if (state->clear_mods & bit) {
731 state->mod_key_count[i]--;
732 if (state->mod_key_count[i] <= 0) {
733 state->components.base_mods &= ~bit;
734 state->mod_key_count[i] = 0;
736 state->clear_mods &= ~bit;
740 xkb_state_update_derived(state);
742 return get_state_component_changes(&prev_components, &state->components);
746 * Updates the state from a set of explicit masks as gained from
747 * xkb_state_serialize_mods and xkb_state_serialize_groups. As noted in the
748 * documentation for these functions in xkbcommon.h, this round-trip is
749 * lossy, and should only be used to update a slave state mirroring the
750 * master, e.g. in a client/server window system.
752 XKB_EXPORT enum xkb_state_component
753 xkb_state_update_mask(struct xkb_state *state,
754 xkb_mod_mask_t base_mods,
755 xkb_mod_mask_t latched_mods,
756 xkb_mod_mask_t locked_mods,
757 xkb_layout_index_t base_group,
758 xkb_layout_index_t latched_group,
759 xkb_layout_index_t locked_group)
761 struct state_components prev_components;
762 xkb_mod_index_t num_mods;
765 prev_components = state->components;
767 state->components.base_mods = 0;
768 state->components.latched_mods = 0;
769 state->components.locked_mods = 0;
770 num_mods = xkb_keymap_num_mods(state->keymap);
772 for (idx = 0; idx < num_mods; idx++) {
773 xkb_mod_mask_t mod = (1 << idx);
775 state->components.base_mods |= mod;
776 if (latched_mods & mod)
777 state->components.latched_mods |= mod;
778 if (locked_mods & mod)
779 state->components.locked_mods |= mod;
782 state->components.base_group = base_group;
783 state->components.latched_group = latched_group;
784 state->components.locked_group = locked_group;
786 xkb_state_update_derived(state);
788 return get_state_component_changes(&prev_components, &state->components);
792 * Provides the symbols to use for the given key and state. Returns the
793 * number of symbols pointed to in syms_out.
796 xkb_state_key_get_syms(struct xkb_state *state, xkb_keycode_t kc,
797 const xkb_keysym_t **syms_out)
799 xkb_layout_index_t layout;
800 xkb_level_index_t level;
802 layout = xkb_state_key_get_layout(state, kc);
803 if (layout == XKB_LAYOUT_INVALID)
806 level = xkb_state_key_get_level(state, kc, layout);
807 if (level == XKB_LEVEL_INVALID)
810 return xkb_keymap_key_get_syms_by_level(state->keymap, kc, layout, level,
819 * Provides either exactly one symbol, or XKB_KEY_NoSymbol.
821 XKB_EXPORT xkb_keysym_t
822 xkb_state_key_get_one_sym(struct xkb_state *state, xkb_keycode_t kc)
824 const xkb_keysym_t *syms;
827 num_syms = xkb_state_key_get_syms(state, kc, &syms);
829 return XKB_KEY_NoSymbol;
835 * Serialises the requested modifier state into an xkb_mod_mask_t, with all
836 * the same disclaimers as in xkb_state_update_mask.
838 XKB_EXPORT xkb_mod_mask_t
839 xkb_state_serialize_mods(struct xkb_state *state,
840 enum xkb_state_component type)
842 xkb_mod_mask_t ret = 0;
844 if (type & XKB_STATE_MODS_EFFECTIVE)
845 return state->components.mods;
847 if (type & XKB_STATE_MODS_DEPRESSED)
848 ret |= state->components.base_mods;
849 if (type & XKB_STATE_MODS_LATCHED)
850 ret |= state->components.latched_mods;
851 if (type & XKB_STATE_MODS_LOCKED)
852 ret |= state->components.locked_mods;
858 * Serialises the requested group state, with all the same disclaimers as
859 * in xkb_state_update_mask.
861 XKB_EXPORT xkb_layout_index_t
862 xkb_state_serialize_layout(struct xkb_state *state,
863 enum xkb_state_component type)
865 xkb_layout_index_t ret = 0;
867 if (type & XKB_STATE_LAYOUT_EFFECTIVE)
868 return state->components.group;
870 if (type & XKB_STATE_LAYOUT_DEPRESSED)
871 ret += state->components.base_group;
872 if (type & XKB_STATE_LAYOUT_LATCHED)
873 ret += state->components.latched_group;
874 if (type & XKB_STATE_LAYOUT_LOCKED)
875 ret += state->components.locked_group;
881 * Returns 1 if the given modifier is active with the specified type(s), 0 if
882 * not, or -1 if the modifier is invalid.
885 xkb_state_mod_index_is_active(struct xkb_state *state,
887 enum xkb_state_component type)
889 if (idx >= xkb_keymap_num_mods(state->keymap))
892 return !!(xkb_state_serialize_mods(state, type) & (1 << idx));
896 * Helper function for xkb_state_mod_indices_are_active and
897 * xkb_state_mod_names_are_active.
900 match_mod_masks(struct xkb_state *state,
901 enum xkb_state_component type,
902 enum xkb_state_match match,
903 xkb_mod_mask_t wanted)
905 xkb_mod_mask_t active = xkb_state_serialize_mods(state, type);
907 if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
910 if (match & XKB_STATE_MATCH_ANY)
911 return !!(active & wanted);
913 return (active & wanted) == wanted;
919 * Returns 1 if the modifiers are active with the specified type(s), 0 if
920 * not, or -1 if any of the modifiers are invalid.
923 xkb_state_mod_indices_are_active(struct xkb_state *state,
924 enum xkb_state_component type,
925 enum xkb_state_match match,
929 xkb_mod_index_t idx = 0;
930 xkb_mod_mask_t wanted = 0;
932 xkb_mod_index_t num_mods = xkb_keymap_num_mods(state->keymap);
936 idx = va_arg(ap, xkb_mod_index_t);
937 if (idx == XKB_MOD_INVALID)
939 if (idx >= num_mods) {
943 wanted |= (1 << idx);
950 return match_mod_masks(state, type, match, wanted);
954 * Returns 1 if the given modifier is active with the specified type(s), 0 if
955 * not, or -1 if the modifier is invalid.
958 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
959 enum xkb_state_component type)
961 xkb_mod_index_t idx = xkb_keymap_mod_get_index(state->keymap, name);
963 if (idx == XKB_MOD_INVALID)
966 return xkb_state_mod_index_is_active(state, idx, type);
970 * Returns 1 if the modifiers are active with the specified type(s), 0 if
971 * not, or -1 if any of the modifiers are invalid.
973 XKB_EXPORT ATTR_NULL_SENTINEL int
974 xkb_state_mod_names_are_active(struct xkb_state *state,
975 enum xkb_state_component type,
976 enum xkb_state_match match,
980 xkb_mod_index_t idx = 0;
982 xkb_mod_mask_t wanted = 0;
987 str = va_arg(ap, const char *);
990 idx = xkb_keymap_mod_get_index(state->keymap, str);
991 if (idx == XKB_MOD_INVALID) {
995 wanted |= (1 << idx);
1002 return match_mod_masks(state, type, match, wanted);
1006 * Returns 1 if the given group is active with the specified type(s), 0 if
1007 * not, or -1 if the group is invalid.
1010 xkb_state_layout_index_is_active(struct xkb_state *state,
1011 xkb_layout_index_t idx,
1012 enum xkb_state_component type)
1016 if (idx >= state->keymap->num_groups)
1019 if (type & XKB_STATE_LAYOUT_EFFECTIVE)
1020 ret |= (state->components.group == idx);
1021 if (type & XKB_STATE_LAYOUT_DEPRESSED)
1022 ret |= (state->components.base_group == idx);
1023 if (type & XKB_STATE_LAYOUT_LATCHED)
1024 ret |= (state->components.latched_group == idx);
1025 if (type & XKB_STATE_LAYOUT_LOCKED)
1026 ret |= (state->components.locked_group == idx);
1032 * Returns 1 if the given modifier is active with the specified type(s), 0 if
1033 * not, or -1 if the modifier is invalid.
1036 xkb_state_layout_name_is_active(struct xkb_state *state, const char *name,
1037 enum xkb_state_component type)
1039 xkb_layout_index_t idx = xkb_keymap_layout_get_index(state->keymap, name);
1041 if (idx == XKB_LAYOUT_INVALID)
1044 return xkb_state_layout_index_is_active(state, idx, type);
1048 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
1051 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
1053 if (idx >= darray_size(state->keymap->indicators) ||
1054 darray_item(state->keymap->indicators, idx).name == XKB_ATOM_NONE)
1057 return !!(state->components.leds & (1 << idx));
1061 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
1064 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
1066 xkb_led_index_t idx = xkb_keymap_led_get_index(state->keymap, name);
1068 if (idx == XKB_LED_INVALID)
1071 return xkb_state_led_index_is_active(state, idx);
1074 static xkb_mod_mask_t
1075 key_get_consumed(struct xkb_state *state, const struct xkb_key *key)
1077 const struct xkb_kt_map_entry *entry;
1078 xkb_layout_index_t group;
1080 group = xkb_state_key_get_layout(state, key->keycode);
1081 if (group == XKB_LAYOUT_INVALID)
1084 entry = get_entry_for_key_state(state, key, group);
1088 return entry->mods.mask & ~entry->preserve.mask;
1092 * Tests to see if a modifier is used up by our translation of a
1093 * keycode to keysyms, taking note of the current modifier state and
1094 * the appropriate key type's preserve information, if any. This allows
1095 * the user to mask out the modifier in later processing of the
1096 * modifiers, e.g. when implementing hot keys or accelerators.
1098 * See also, for example:
1099 * - XkbTranslateKeyCode(3), mod_rtrn retrun value, from libX11.
1100 * - gdk_keymap_translate_keyboard_state, consumed_modifiers return value,
1104 xkb_state_mod_index_is_consumed(struct xkb_state *state, xkb_keycode_t kc,
1105 xkb_mod_index_t idx)
1107 const struct xkb_key *key = XkbKey(state->keymap, kc);
1109 if (!key || idx >= xkb_keymap_num_mods(state->keymap))
1112 return !!((1 << idx) & key_get_consumed(state, key));
1116 * Calculates which modifiers should be consumed during key processing,
1117 * and returns the mask with all these modifiers removed. e.g. if
1118 * given a state of Alt and Shift active for a two-level alphabetic
1119 * key containing plus and equal on the first and second level
1120 * respectively, will return a mask of only Alt, as Shift has been
1121 * consumed by the type handling.
1123 XKB_EXPORT xkb_mod_mask_t
1124 xkb_state_mod_mask_remove_consumed(struct xkb_state *state, xkb_keycode_t kc,
1125 xkb_mod_mask_t mask)
1127 const struct xkb_key *key = XkbKey(state->keymap, kc);
1132 return mask & ~key_get_consumed(state, key);