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 cur;
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->cur.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->cur.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->cur.base_group = filter->priv;
271 if (filter->action.group.flags & ACTION_LOCK_CLEAR)
272 state->cur.locked_group = 0;
279 xkb_filter_group_set_new(struct xkb_state *state, struct xkb_filter *filter)
281 filter->priv = state->cur.base_group;
282 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
283 state->cur.base_group = filter->action.group.group;
285 state->cur.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->cur.locked_group = filter->action.group.group;
314 state->cur.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->cur.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->cur.locked_mods &= ~filter->priv;
375 xkb_filter_mod_lock_new(struct xkb_state *state, struct xkb_filter *filter)
377 filter->priv = state->cur.locked_mods & filter->action.mods.mods.mask;
378 state->set_mods |= filter->action.mods.mods.mask;
379 if (!(filter->action.mods.flags & ACTION_LOCK_NO_LOCK))
380 state->cur.locked_mods |= filter->action.mods.mods.mask;
383 enum xkb_key_latch_state {
390 xkb_action_breaks_latch(const union xkb_action *action)
392 switch (action->type) {
393 case ACTION_TYPE_NONE:
394 case ACTION_TYPE_PTR_BUTTON:
395 case ACTION_TYPE_PTR_LOCK:
396 case ACTION_TYPE_CTRL_SET:
397 case ACTION_TYPE_CTRL_LOCK:
398 case ACTION_TYPE_KEY_REDIRECT:
399 case ACTION_TYPE_SWITCH_VT:
400 case ACTION_TYPE_TERMINATE:
408 xkb_filter_mod_latch_func(struct xkb_state *state,
409 struct xkb_filter *filter,
410 const struct xkb_key *key,
411 enum xkb_key_direction direction)
413 enum xkb_key_latch_state latch = filter->priv;
415 if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
416 /* If this is a new keypress and we're awaiting our single latched
417 * keypress, then either break the latch if any random key is pressed,
418 * or promote it to a lock or plain base set if it's the same
420 const union xkb_action *action = xkb_key_get_action(state, key);
421 if (action->type == ACTION_TYPE_MOD_LATCH &&
422 action->mods.flags == filter->action.mods.flags &&
423 action->mods.mods.mask == filter->action.mods.mods.mask) {
424 filter->action = *action;
425 if (filter->action.mods.flags & ACTION_LATCH_TO_LOCK) {
426 filter->action.type = ACTION_TYPE_MOD_LOCK;
427 filter->func = xkb_filter_mod_lock_func;
428 state->cur.locked_mods |= filter->action.mods.mods.mask;
431 filter->action.type = ACTION_TYPE_MOD_SET;
432 filter->func = xkb_filter_mod_set_func;
433 state->set_mods = filter->action.mods.mods.mask;
436 state->cur.latched_mods &= ~filter->action.mods.mods.mask;
440 else if (xkb_action_breaks_latch(action)) {
441 /* XXX: This may be totally broken, we might need to break the
442 * latch in the next run after this press? */
443 state->cur.latched_mods &= ~filter->action.mods.mods.mask;
448 else if (direction == XKB_KEY_UP && key == filter->key) {
449 /* Our key got released. If we've set it to clear locks, and we
450 * currently have the same modifiers locked, then release them and
451 * don't actually latch. Else we've actually hit the latching
452 * stage, so set PENDING and move our modifier from base to
454 if (latch == NO_LATCH ||
455 ((filter->action.mods.flags & ACTION_LOCK_CLEAR) &&
456 (state->cur.locked_mods & filter->action.mods.mods.mask) ==
457 filter->action.mods.mods.mask)) {
458 /* XXX: We might be a bit overenthusiastic about clearing
459 * mods other filters have set here? */
460 if (latch == LATCH_PENDING)
461 state->cur.latched_mods &= ~filter->action.mods.mods.mask;
463 state->clear_mods = filter->action.mods.mods.mask;
464 state->cur.locked_mods &= ~filter->action.mods.mods.mask;
468 latch = LATCH_PENDING;
469 state->clear_mods = filter->action.mods.mods.mask;
470 state->cur.latched_mods |= filter->action.mods.mods.mask;
474 else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
475 /* Someone's pressed another key while we've still got the latching
476 * key held down, so keep the base modifier state active (from
477 * xkb_filter_mod_latch_new), but don't trip the latch, just clear
478 * it as soon as the modifier gets released. */
482 filter->priv = latch;
488 xkb_filter_mod_latch_new(struct xkb_state *state, struct xkb_filter *filter)
490 filter->priv = LATCH_KEY_DOWN;
491 state->set_mods = filter->action.mods.mods.mask;
494 static const struct {
495 void (*new)(struct xkb_state *state, struct xkb_filter *filter);
496 int (*func)(struct xkb_state *state, struct xkb_filter *filter,
497 const struct xkb_key *key, enum xkb_key_direction direction);
498 } filter_action_funcs[_ACTION_TYPE_NUM_ENTRIES] = {
499 [ACTION_TYPE_MOD_SET] = { xkb_filter_mod_set_new,
500 xkb_filter_mod_set_func },
501 [ACTION_TYPE_MOD_LATCH] = { xkb_filter_mod_latch_new,
502 xkb_filter_mod_latch_func },
503 [ACTION_TYPE_MOD_LOCK] = { xkb_filter_mod_lock_new,
504 xkb_filter_mod_lock_func },
505 [ACTION_TYPE_GROUP_SET] = { xkb_filter_group_set_new,
506 xkb_filter_group_set_func },
507 [ACTION_TYPE_GROUP_LOCK] = { xkb_filter_group_lock_new,
508 xkb_filter_group_lock_func },
512 * Applies any relevant filters to the key, first from the list of filters
513 * that are currently active, then if no filter has claimed the key, possibly
514 * apply a new filter from the key action.
517 xkb_filter_apply_all(struct xkb_state *state,
518 const struct xkb_key *key,
519 enum xkb_key_direction direction)
521 struct xkb_filter *filter;
522 const union xkb_action *action;
525 /* First run through all the currently active filters and see if any of
526 * them have claimed this event. */
527 darray_foreach(filter, state->filters) {
530 send &= filter->func(state, filter, key, direction);
533 if (!send || direction == XKB_KEY_UP)
536 action = xkb_key_get_action(state, key);
539 * It's possible for the keymap to set action->type explicitly, like so:
540 * interpret XF86_Next_VMode {
541 * action = Private(type=0x86, data="+VMode");
543 * We don't handle those.
545 if (action->type >= _ACTION_TYPE_NUM_ENTRIES)
548 if (!filter_action_funcs[action->type].new)
551 filter = xkb_filter_new(state);
556 filter->func = filter_action_funcs[action->type].func;
557 filter->action = *action;
558 filter_action_funcs[action->type].new(state, filter);
561 XKB_EXPORT struct xkb_state *
562 xkb_state_new(struct xkb_keymap *keymap)
564 struct xkb_state *ret;
566 ret = calloc(sizeof(*ret), 1);
571 ret->keymap = xkb_keymap_ref(keymap);
576 XKB_EXPORT struct xkb_state *
577 xkb_state_ref(struct xkb_state *state)
584 xkb_state_unref(struct xkb_state *state)
586 if (!state || --state->refcnt > 0)
589 xkb_keymap_unref(state->keymap);
590 darray_free(state->filters);
594 XKB_EXPORT struct xkb_keymap *
595 xkb_state_get_keymap(struct xkb_state *state)
597 return state->keymap;
601 * Update the LED state to match the rest of the xkb_state.
604 xkb_state_led_update_all(struct xkb_state *state)
607 const struct xkb_indicator_map *map;
611 darray_enumerate(led, map, state->keymap->indicators) {
612 xkb_mod_mask_t mod_mask = 0;
613 xkb_layout_mask_t group_mask = 0;
615 if (map->which_mods & XKB_STATE_MODS_EFFECTIVE)
616 mod_mask |= state->cur.mods;
617 if (map->which_mods & XKB_STATE_MODS_DEPRESSED)
618 mod_mask |= state->cur.base_mods;
619 if (map->which_mods & XKB_STATE_MODS_LATCHED)
620 mod_mask |= state->cur.latched_mods;
621 if (map->which_mods & XKB_STATE_MODS_LOCKED)
622 mod_mask |= state->cur.locked_mods;
623 if (map->mods.mask & mod_mask)
624 state->cur.leds |= (1 << led);
626 if (map->which_groups & XKB_STATE_LAYOUT_EFFECTIVE)
627 group_mask |= (1 << state->cur.group);
628 if (map->which_groups & XKB_STATE_LAYOUT_DEPRESSED)
629 group_mask |= (1 << state->cur.base_group);
630 if (map->which_groups & XKB_STATE_LAYOUT_LATCHED)
631 group_mask |= (1 << state->cur.latched_group);
632 if (map->which_groups & XKB_STATE_LAYOUT_LOCKED)
633 group_mask |= (1 << state->cur.locked_group);
634 if (map->groups & group_mask)
635 state->cur.leds |= (1 << led);
637 if (map->ctrls & state->keymap->enabled_ctrls)
638 state->cur.leds |= (1 << led);
643 * Calculates the derived state (effective mods/group and LEDs) from an
644 * up-to-date xkb_state.
647 xkb_state_update_derived(struct xkb_state *state)
649 state->cur.mods = (state->cur.base_mods |
650 state->cur.latched_mods |
651 state->cur.locked_mods);
653 /* TODO: Use groups_wrap control instead of always RANGE_WRAP. */
655 state->cur.locked_group = wrap_group_into_range(state->cur.locked_group,
656 state->keymap->num_groups,
659 state->cur.group = wrap_group_into_range(state->cur.base_group +
660 state->cur.latched_group +
661 state->cur.locked_group,
662 state->keymap->num_groups,
665 xkb_state_led_update_all(state);
668 static enum xkb_state_component
669 get_state_component_changes(const struct state_components *a,
670 const struct state_components *b)
672 xkb_mod_mask_t mask = 0;
674 if (a->group != b->group)
675 mask |= XKB_STATE_LAYOUT_EFFECTIVE;
676 if (a->base_group != b->base_group)
677 mask |= XKB_STATE_LAYOUT_DEPRESSED;
678 if (a->latched_group != b->latched_group)
679 mask |= XKB_STATE_LAYOUT_LATCHED;
680 if (a->locked_group != b->locked_group)
681 mask |= XKB_STATE_LAYOUT_LOCKED;
682 if (a->mods != b->mods)
683 mask |= XKB_STATE_MODS_EFFECTIVE;
684 if (a->base_mods != b->base_mods)
685 mask |= XKB_STATE_MODS_DEPRESSED;
686 if (a->latched_mods != b->latched_mods)
687 mask |= XKB_STATE_MODS_LATCHED;
688 if (a->locked_mods != b->locked_mods)
689 mask |= XKB_STATE_MODS_LOCKED;
690 if (a->leds != b->leds)
691 mask |= XKB_STATE_LEDS;
697 * Given a particular key event, updates the state structure to reflect the
700 XKB_EXPORT enum xkb_state_component
701 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
702 enum xkb_key_direction direction)
706 struct state_components prev_components;
707 const struct xkb_key *key = XkbKey(state->keymap, kc);
712 prev_components = state->cur;
715 state->clear_mods = 0;
717 xkb_filter_apply_all(state, key, direction);
719 for (i = 0, bit = 1; state->set_mods; i++, bit <<= 1) {
720 if (state->set_mods & bit) {
721 state->mod_key_count[i]++;
722 state->cur.base_mods |= bit;
723 state->set_mods &= ~bit;
727 for (i = 0, bit = 1; state->clear_mods; i++, bit <<= 1) {
728 if (state->clear_mods & bit) {
729 state->mod_key_count[i]--;
730 if (state->mod_key_count[i] <= 0) {
731 state->cur.base_mods &= ~bit;
732 state->mod_key_count[i] = 0;
734 state->clear_mods &= ~bit;
738 xkb_state_update_derived(state);
740 return get_state_component_changes(&prev_components, &state->cur);
744 * Updates the state from a set of explicit masks as gained from
745 * xkb_state_serialize_mods and xkb_state_serialize_groups. As noted in the
746 * documentation for these functions in xkbcommon.h, this round-trip is
747 * lossy, and should only be used to update a slave state mirroring the
748 * master, e.g. in a client/server window system.
750 XKB_EXPORT enum xkb_state_component
751 xkb_state_update_mask(struct xkb_state *state,
752 xkb_mod_mask_t base_mods,
753 xkb_mod_mask_t latched_mods,
754 xkb_mod_mask_t locked_mods,
755 xkb_layout_index_t base_group,
756 xkb_layout_index_t latched_group,
757 xkb_layout_index_t locked_group)
759 struct state_components prev_components;
760 xkb_mod_index_t num_mods;
763 prev_components = state->cur;
765 state->cur.base_mods = 0;
766 state->cur.latched_mods = 0;
767 state->cur.locked_mods = 0;
768 num_mods = xkb_keymap_num_mods(state->keymap);
770 for (idx = 0; idx < num_mods; idx++) {
771 xkb_mod_mask_t mod = (1 << idx);
773 state->cur.base_mods |= mod;
774 if (latched_mods & mod)
775 state->cur.latched_mods |= mod;
776 if (locked_mods & mod)
777 state->cur.locked_mods |= mod;
780 state->cur.base_group = base_group;
781 state->cur.latched_group = latched_group;
782 state->cur.locked_group = locked_group;
784 xkb_state_update_derived(state);
786 return get_state_component_changes(&prev_components, &state->cur);
790 * Provides the symbols to use for the given key and state. Returns the
791 * number of symbols pointed to in syms_out.
794 xkb_state_key_get_syms(struct xkb_state *state, xkb_keycode_t kc,
795 const xkb_keysym_t **syms_out)
797 xkb_layout_index_t layout;
798 xkb_level_index_t level;
800 layout = xkb_state_key_get_layout(state, kc);
801 if (layout == XKB_LAYOUT_INVALID)
804 level = xkb_state_key_get_level(state, kc, layout);
805 if (level == XKB_LEVEL_INVALID)
808 return xkb_keymap_key_get_syms_by_level(state->keymap, kc, layout, level,
817 * Provides either exactly one symbol, or XKB_KEY_NoSymbol.
819 XKB_EXPORT xkb_keysym_t
820 xkb_state_key_get_one_sym(struct xkb_state *state, xkb_keycode_t kc)
822 const xkb_keysym_t *syms;
825 num_syms = xkb_state_key_get_syms(state, kc, &syms);
827 return XKB_KEY_NoSymbol;
833 * Serialises the requested modifier state into an xkb_mod_mask_t, with all
834 * the same disclaimers as in xkb_state_update_mask.
836 XKB_EXPORT xkb_mod_mask_t
837 xkb_state_serialize_mods(struct xkb_state *state,
838 enum xkb_state_component type)
840 xkb_mod_mask_t ret = 0;
842 if (type & XKB_STATE_MODS_EFFECTIVE)
843 return state->cur.mods;
845 if (type & XKB_STATE_MODS_DEPRESSED)
846 ret |= state->cur.base_mods;
847 if (type & XKB_STATE_MODS_LATCHED)
848 ret |= state->cur.latched_mods;
849 if (type & XKB_STATE_MODS_LOCKED)
850 ret |= state->cur.locked_mods;
856 * Serialises the requested group state, with all the same disclaimers as
857 * in xkb_state_update_mask.
859 XKB_EXPORT xkb_layout_index_t
860 xkb_state_serialize_layout(struct xkb_state *state,
861 enum xkb_state_component type)
863 xkb_layout_index_t ret = 0;
865 if (type & XKB_STATE_LAYOUT_EFFECTIVE)
866 return state->cur.group;
868 if (type & XKB_STATE_LAYOUT_DEPRESSED)
869 ret += state->cur.base_group;
870 if (type & XKB_STATE_LAYOUT_LATCHED)
871 ret += state->cur.latched_group;
872 if (type & XKB_STATE_LAYOUT_LOCKED)
873 ret += state->cur.locked_group;
879 * Returns 1 if the given modifier is active with the specified type(s), 0 if
880 * not, or -1 if the modifier is invalid.
883 xkb_state_mod_index_is_active(struct xkb_state *state,
885 enum xkb_state_component type)
887 if (idx >= xkb_keymap_num_mods(state->keymap))
890 return !!(xkb_state_serialize_mods(state, type) & (1 << idx));
894 * Helper function for xkb_state_mod_indices_are_active and
895 * xkb_state_mod_names_are_active.
898 match_mod_masks(struct xkb_state *state,
899 enum xkb_state_component type,
900 enum xkb_state_match match,
901 xkb_mod_mask_t wanted)
903 xkb_mod_mask_t active = xkb_state_serialize_mods(state, type);
905 if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
908 if (match & XKB_STATE_MATCH_ANY)
909 return !!(active & wanted);
911 return (active & wanted) == wanted;
917 * Returns 1 if the modifiers are active with the specified type(s), 0 if
918 * not, or -1 if any of the modifiers are invalid.
921 xkb_state_mod_indices_are_active(struct xkb_state *state,
922 enum xkb_state_component type,
923 enum xkb_state_match match,
927 xkb_mod_index_t idx = 0;
928 xkb_mod_mask_t wanted = 0;
930 xkb_mod_index_t num_mods = xkb_keymap_num_mods(state->keymap);
934 idx = va_arg(ap, xkb_mod_index_t);
935 if (idx == XKB_MOD_INVALID)
937 if (idx >= num_mods) {
941 wanted |= (1 << idx);
948 return match_mod_masks(state, type, match, wanted);
952 * Returns 1 if the given modifier is active with the specified type(s), 0 if
953 * not, or -1 if the modifier is invalid.
956 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
957 enum xkb_state_component type)
959 xkb_mod_index_t idx = xkb_keymap_mod_get_index(state->keymap, name);
961 if (idx == XKB_MOD_INVALID)
964 return xkb_state_mod_index_is_active(state, idx, type);
968 * Returns 1 if the modifiers are active with the specified type(s), 0 if
969 * not, or -1 if any of the modifiers are invalid.
971 XKB_EXPORT ATTR_NULL_SENTINEL int
972 xkb_state_mod_names_are_active(struct xkb_state *state,
973 enum xkb_state_component type,
974 enum xkb_state_match match,
978 xkb_mod_index_t idx = 0;
980 xkb_mod_mask_t wanted = 0;
985 str = va_arg(ap, const char *);
988 idx = xkb_keymap_mod_get_index(state->keymap, str);
989 if (idx == XKB_MOD_INVALID) {
993 wanted |= (1 << idx);
1000 return match_mod_masks(state, type, match, wanted);
1004 * Returns 1 if the given group is active with the specified type(s), 0 if
1005 * not, or -1 if the group is invalid.
1008 xkb_state_layout_index_is_active(struct xkb_state *state,
1009 xkb_layout_index_t idx,
1010 enum xkb_state_component type)
1014 if (idx >= state->keymap->num_groups)
1017 if (type & XKB_STATE_LAYOUT_EFFECTIVE)
1018 ret |= (state->cur.group == idx);
1019 if (type & XKB_STATE_LAYOUT_DEPRESSED)
1020 ret |= (state->cur.base_group == idx);
1021 if (type & XKB_STATE_LAYOUT_LATCHED)
1022 ret |= (state->cur.latched_group == idx);
1023 if (type & XKB_STATE_LAYOUT_LOCKED)
1024 ret |= (state->cur.locked_group == idx);
1030 * Returns 1 if the given modifier is active with the specified type(s), 0 if
1031 * not, or -1 if the modifier is invalid.
1034 xkb_state_layout_name_is_active(struct xkb_state *state, const char *name,
1035 enum xkb_state_component type)
1037 xkb_layout_index_t idx = xkb_keymap_layout_get_index(state->keymap, name);
1039 if (idx == XKB_LAYOUT_INVALID)
1042 return xkb_state_layout_index_is_active(state, idx, type);
1046 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
1049 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
1051 if (idx >= darray_size(state->keymap->indicators) ||
1052 darray_item(state->keymap->indicators, idx).name == XKB_ATOM_NONE)
1055 return !!(state->cur.leds & (1 << idx));
1059 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
1062 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
1064 xkb_led_index_t idx = xkb_keymap_led_get_index(state->keymap, name);
1066 if (idx == XKB_LED_INVALID)
1069 return xkb_state_led_index_is_active(state, idx);
1072 static xkb_mod_mask_t
1073 key_get_consumed(struct xkb_state *state, const struct xkb_key *key)
1075 const struct xkb_kt_map_entry *entry;
1076 xkb_layout_index_t group;
1078 group = xkb_state_key_get_layout(state, key->keycode);
1079 if (group == XKB_LAYOUT_INVALID)
1082 entry = get_entry_for_key_state(state, key, group);
1086 return entry->mods.mask & ~entry->preserve.mask;
1090 * Tests to see if a modifier is used up by our translation of a
1091 * keycode to keysyms, taking note of the current modifier state and
1092 * the appropriate key type's preserve information, if any. This allows
1093 * the user to mask out the modifier in later processing of the
1094 * modifiers, e.g. when implementing hot keys or accelerators.
1096 * See also, for example:
1097 * - XkbTranslateKeyCode(3), mod_rtrn retrun value, from libX11.
1098 * - gdk_keymap_translate_keyboard_state, consumed_modifiers return value,
1102 xkb_state_mod_index_is_consumed(struct xkb_state *state, xkb_keycode_t kc,
1103 xkb_mod_index_t idx)
1105 const struct xkb_key *key = XkbKey(state->keymap, kc);
1107 if (!key || idx >= xkb_keymap_num_mods(state->keymap))
1110 return !!((1 << idx) & key_get_consumed(state, key));
1114 * Calculates which modifiers should be consumed during key processing,
1115 * and returns the mask with all these modifiers removed. e.g. if
1116 * given a state of Alt and Shift active for a two-level alphabetic
1117 * key containing plus and equal on the first and second level
1118 * respectively, will return a mask of only Alt, as Shift has been
1119 * consumed by the type handling.
1121 XKB_EXPORT xkb_mod_mask_t
1122 xkb_state_mod_mask_remove_consumed(struct xkb_state *state, xkb_keycode_t kc,
1123 xkb_mod_mask_t mask)
1125 const struct xkb_key *key = XkbKey(state->keymap, kc);
1130 return mask & ~key_get_consumed(state, key);