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)
67 union xkb_action action;
68 const struct xkb_key *key;
70 bool (*func)(struct xkb_state *state,
71 struct xkb_filter *filter,
72 const struct xkb_key *key,
73 enum xkb_key_direction direction);
77 struct state_components {
78 /* These may be negative, because of -1 group actions. */
79 int32_t base_group; /**< depressed */
80 int32_t latched_group;
82 xkb_layout_index_t group; /**< effective */
84 xkb_mod_mask_t base_mods; /**< depressed */
85 xkb_mod_mask_t latched_mods;
86 xkb_mod_mask_t locked_mods;
87 xkb_mod_mask_t mods; /**< effective */
94 * Before updating the state, we keep a copy of just this struct. This
95 * allows us to report which components of the state have changed.
97 struct state_components components;
100 * At each event, we accumulate all the needed modifications to the base
101 * modifiers, and apply them at the end. These keep track of this state.
103 xkb_mod_mask_t set_mods;
104 xkb_mod_mask_t clear_mods;
107 * We mustn't clear a base modifier if there's another depressed key
108 * which affects it, e.g. given this sequence
109 * < Left Shift down, Right Shift down, Left Shift Up >
110 * the modifier should still be set. This keeps the count.
112 int16_t mod_key_count[XKB_MAX_MODS];
115 darray(struct xkb_filter) filters;
116 struct xkb_keymap *keymap;
120 * If the virtual modifiers are not bound to anything, the entry
121 * is not active and should be skipped. xserver does this with
122 * cached entry->active field.
125 entry_is_active(const struct xkb_key_type_entry *entry)
127 return entry->mods.mods == 0 || entry->mods.mask != 0;
130 static const struct xkb_key_type_entry *
131 get_entry_for_mods(const struct xkb_key_type *type, xkb_mod_mask_t mods)
133 for (unsigned i = 0; i < type->num_entries; i++)
134 if (entry_is_active(&type->entries[i]) &&
135 type->entries[i].mods.mask == mods)
136 return &type->entries[i];
140 static const struct xkb_key_type_entry *
141 get_entry_for_key_state(struct xkb_state *state, const struct xkb_key *key,
142 xkb_layout_index_t group)
144 const struct xkb_key_type *type = key->groups[group].type;
145 xkb_mod_mask_t active_mods = state->components.mods & type->mods.mask;
146 return get_entry_for_mods(type, active_mods);
150 * Returns the level to use for the given key and state, or
153 XKB_EXPORT xkb_level_index_t
154 xkb_state_key_get_level(struct xkb_state *state, xkb_keycode_t kc,
155 xkb_layout_index_t layout)
157 const struct xkb_key *key = XkbKey(state->keymap, kc);
158 const struct xkb_key_type_entry *entry;
160 if (!key || layout >= key->num_groups)
161 return XKB_LEVEL_INVALID;
163 /* If we don't find an explicit match the default is 0. */
164 entry = get_entry_for_key_state(state, key, layout);
172 XkbWrapGroupIntoRange(int32_t group,
173 xkb_layout_index_t num_groups,
174 enum xkb_range_exceed_type out_of_range_group_action,
175 xkb_layout_index_t out_of_range_group_number)
178 return XKB_LAYOUT_INVALID;
180 if (group >= 0 && (xkb_layout_index_t) group < num_groups)
183 switch (out_of_range_group_action) {
185 if (out_of_range_group_number >= num_groups)
187 return out_of_range_group_number;
193 return num_groups - 1;
198 * C99 says a negative dividend in a modulo operation always
199 * gives a negative result.
202 return ((int) num_groups + (group % (int) num_groups));
204 return group % num_groups;
209 * Returns the layout to use for the given key and state, taking
210 * wrapping/clamping/etc into account, or XKB_LAYOUT_INVALID.
212 XKB_EXPORT xkb_layout_index_t
213 xkb_state_key_get_layout(struct xkb_state *state, xkb_keycode_t kc)
215 const struct xkb_key *key = XkbKey(state->keymap, kc);
218 return XKB_LAYOUT_INVALID;
220 return XkbWrapGroupIntoRange(state->components.group, key->num_groups,
221 key->out_of_range_group_action,
222 key->out_of_range_group_number);
225 static const union xkb_action *
226 xkb_key_get_action(struct xkb_state *state, const struct xkb_key *key)
228 static const union xkb_action dummy = { .type = ACTION_TYPE_NONE };
230 xkb_layout_index_t layout;
231 xkb_level_index_t level;
233 layout = xkb_state_key_get_layout(state, key->keycode);
234 if (layout == XKB_LAYOUT_INVALID)
237 level = xkb_state_key_get_level(state, key->keycode, layout);
238 if (level == XKB_LEVEL_INVALID)
241 return &key->groups[layout].levels[level].action;
244 static struct xkb_filter *
245 xkb_filter_new(struct xkb_state *state)
247 struct xkb_filter *filter = NULL, *iter;
249 darray_foreach(iter, state->filters) {
257 darray_resize0(state->filters, darray_size(state->filters) + 1);
258 filter = &darray_item(state->filters, darray_size(state->filters) -1);
265 /***====================================================================***/
268 xkb_filter_group_set_func(struct xkb_state *state,
269 struct xkb_filter *filter,
270 const struct xkb_key *key,
271 enum xkb_key_direction direction)
273 if (key != filter->key) {
274 filter->action.group.flags &= ~ACTION_LOCK_CLEAR;
278 if (direction == XKB_KEY_DOWN) {
282 else if (--filter->refcnt > 0) {
286 state->components.base_group = filter->priv;
288 if (filter->action.group.flags & ACTION_LOCK_CLEAR)
289 state->components.locked_group = 0;
296 xkb_filter_group_set_new(struct xkb_state *state, struct xkb_filter *filter)
298 filter->priv = state->components.base_group;
299 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
300 state->components.base_group = filter->action.group.group;
302 state->components.base_group += filter->action.group.group;
306 xkb_filter_group_lock_func(struct xkb_state *state,
307 struct xkb_filter *filter,
308 const struct xkb_key *key,
309 enum xkb_key_direction direction)
311 if (key != filter->key)
314 if (direction == XKB_KEY_DOWN) {
318 if (--filter->refcnt > 0)
326 xkb_filter_group_lock_new(struct xkb_state *state, struct xkb_filter *filter)
328 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
329 state->components.locked_group = filter->action.group.group;
331 state->components.locked_group += filter->action.group.group;
335 xkb_filter_mod_set_func(struct xkb_state *state,
336 struct xkb_filter *filter,
337 const struct xkb_key *key,
338 enum xkb_key_direction direction)
340 if (key != filter->key) {
341 filter->action.mods.flags &= ~ACTION_LOCK_CLEAR;
345 if (direction == XKB_KEY_DOWN) {
349 else if (--filter->refcnt > 0) {
353 state->clear_mods = filter->action.mods.mods.mask;
354 if (filter->action.mods.flags & ACTION_LOCK_CLEAR)
355 state->components.locked_mods &= ~filter->action.mods.mods.mask;
362 xkb_filter_mod_set_new(struct xkb_state *state, struct xkb_filter *filter)
364 state->set_mods = filter->action.mods.mods.mask;
368 xkb_filter_mod_lock_func(struct xkb_state *state,
369 struct xkb_filter *filter,
370 const struct xkb_key *key,
371 enum xkb_key_direction direction)
373 if (key != filter->key)
376 if (direction == XKB_KEY_DOWN) {
380 if (--filter->refcnt > 0)
383 state->clear_mods |= filter->action.mods.mods.mask;
384 if (!(filter->action.mods.flags & ACTION_LOCK_NO_UNLOCK))
385 state->components.locked_mods &= ~filter->priv;
392 xkb_filter_mod_lock_new(struct xkb_state *state, struct xkb_filter *filter)
394 filter->priv = (state->components.locked_mods &
395 filter->action.mods.mods.mask);
396 state->set_mods |= filter->action.mods.mods.mask;
397 if (!(filter->action.mods.flags & ACTION_LOCK_NO_LOCK))
398 state->components.locked_mods |= filter->action.mods.mods.mask;
401 enum xkb_key_latch_state {
408 xkb_action_breaks_latch(const union xkb_action *action)
410 switch (action->type) {
411 case ACTION_TYPE_NONE:
412 case ACTION_TYPE_PTR_BUTTON:
413 case ACTION_TYPE_PTR_LOCK:
414 case ACTION_TYPE_CTRL_SET:
415 case ACTION_TYPE_CTRL_LOCK:
416 case ACTION_TYPE_SWITCH_VT:
417 case ACTION_TYPE_TERMINATE:
425 xkb_filter_mod_latch_func(struct xkb_state *state,
426 struct xkb_filter *filter,
427 const struct xkb_key *key,
428 enum xkb_key_direction direction)
430 enum xkb_key_latch_state latch = filter->priv;
432 if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
433 /* If this is a new keypress and we're awaiting our single latched
434 * keypress, then either break the latch if any random key is pressed,
435 * or promote it to a lock or plain base set if it's the same
437 const union xkb_action *action = xkb_key_get_action(state, key);
438 if (action->type == ACTION_TYPE_MOD_LATCH &&
439 action->mods.flags == filter->action.mods.flags &&
440 action->mods.mods.mask == filter->action.mods.mods.mask) {
441 filter->action = *action;
442 if (filter->action.mods.flags & ACTION_LATCH_TO_LOCK) {
443 filter->action.type = ACTION_TYPE_MOD_LOCK;
444 filter->func = xkb_filter_mod_lock_func;
445 state->components.locked_mods |= filter->action.mods.mods.mask;
448 filter->action.type = ACTION_TYPE_MOD_SET;
449 filter->func = xkb_filter_mod_set_func;
450 state->set_mods = filter->action.mods.mods.mask;
453 state->components.latched_mods &= ~filter->action.mods.mods.mask;
457 else if (xkb_action_breaks_latch(action)) {
458 /* XXX: This may be totally broken, we might need to break the
459 * latch in the next run after this press? */
460 state->components.latched_mods &= ~filter->action.mods.mods.mask;
465 else if (direction == XKB_KEY_UP && key == filter->key) {
466 /* Our key got released. If we've set it to clear locks, and we
467 * currently have the same modifiers locked, then release them and
468 * don't actually latch. Else we've actually hit the latching
469 * stage, so set PENDING and move our modifier from base to
471 if (latch == NO_LATCH ||
472 ((filter->action.mods.flags & ACTION_LOCK_CLEAR) &&
473 (state->components.locked_mods & filter->action.mods.mods.mask) ==
474 filter->action.mods.mods.mask)) {
475 /* XXX: We might be a bit overenthusiastic about clearing
476 * mods other filters have set here? */
477 if (latch == LATCH_PENDING)
478 state->components.latched_mods &=
479 ~filter->action.mods.mods.mask;
481 state->clear_mods = filter->action.mods.mods.mask;
482 state->components.locked_mods &= ~filter->action.mods.mods.mask;
486 latch = LATCH_PENDING;
487 state->clear_mods = filter->action.mods.mods.mask;
488 state->components.latched_mods |= filter->action.mods.mods.mask;
492 else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
493 /* Someone's pressed another key while we've still got the latching
494 * key held down, so keep the base modifier state active (from
495 * xkb_filter_mod_latch_new), but don't trip the latch, just clear
496 * it as soon as the modifier gets released. */
500 filter->priv = latch;
506 xkb_filter_mod_latch_new(struct xkb_state *state, struct xkb_filter *filter)
508 filter->priv = LATCH_KEY_DOWN;
509 state->set_mods = filter->action.mods.mods.mask;
512 static const struct {
513 void (*new)(struct xkb_state *state, struct xkb_filter *filter);
514 bool (*func)(struct xkb_state *state, struct xkb_filter *filter,
515 const struct xkb_key *key, enum xkb_key_direction direction);
516 } filter_action_funcs[_ACTION_TYPE_NUM_ENTRIES] = {
517 [ACTION_TYPE_MOD_SET] = { xkb_filter_mod_set_new,
518 xkb_filter_mod_set_func },
519 [ACTION_TYPE_MOD_LATCH] = { xkb_filter_mod_latch_new,
520 xkb_filter_mod_latch_func },
521 [ACTION_TYPE_MOD_LOCK] = { xkb_filter_mod_lock_new,
522 xkb_filter_mod_lock_func },
523 [ACTION_TYPE_GROUP_SET] = { xkb_filter_group_set_new,
524 xkb_filter_group_set_func },
525 [ACTION_TYPE_GROUP_LOCK] = { xkb_filter_group_lock_new,
526 xkb_filter_group_lock_func },
530 * Applies any relevant filters to the key, first from the list of filters
531 * that are currently active, then if no filter has claimed the key, possibly
532 * apply a new filter from the key action.
535 xkb_filter_apply_all(struct xkb_state *state,
536 const struct xkb_key *key,
537 enum xkb_key_direction direction)
539 struct xkb_filter *filter;
540 const union xkb_action *action;
543 /* First run through all the currently active filters and see if any of
544 * them have claimed this event. */
545 darray_foreach(filter, state->filters) {
548 send = filter->func(state, filter, key, direction) && send;
551 if (!send || direction == XKB_KEY_UP)
554 action = xkb_key_get_action(state, key);
557 * It's possible for the keymap to set action->type explicitly, like so:
558 * interpret XF86_Next_VMode {
559 * action = Private(type=0x86, data="+VMode");
561 * We don't handle those.
563 if (action->type >= _ACTION_TYPE_NUM_ENTRIES)
566 if (!filter_action_funcs[action->type].new)
569 filter = xkb_filter_new(state);
574 filter->func = filter_action_funcs[action->type].func;
575 filter->action = *action;
576 filter_action_funcs[action->type].new(state, filter);
579 XKB_EXPORT struct xkb_state *
580 xkb_state_new(struct xkb_keymap *keymap)
582 struct xkb_state *ret;
584 ret = calloc(sizeof(*ret), 1);
589 ret->keymap = xkb_keymap_ref(keymap);
594 XKB_EXPORT struct xkb_state *
595 xkb_state_ref(struct xkb_state *state)
602 xkb_state_unref(struct xkb_state *state)
604 if (!state || --state->refcnt > 0)
607 xkb_keymap_unref(state->keymap);
608 darray_free(state->filters);
612 XKB_EXPORT struct xkb_keymap *
613 xkb_state_get_keymap(struct xkb_state *state)
615 return state->keymap;
619 * Update the LED state to match the rest of the xkb_state.
622 xkb_state_led_update_all(struct xkb_state *state)
625 const struct xkb_led *led;
627 state->components.leds = 0;
629 xkb_leds_enumerate(idx, led, state->keymap) {
630 xkb_mod_mask_t mod_mask = 0;
631 xkb_layout_mask_t group_mask = 0;
633 if (led->which_mods != 0 && led->mods.mask != 0) {
634 if (led->which_mods & XKB_STATE_MODS_EFFECTIVE)
635 mod_mask |= state->components.mods;
636 if (led->which_mods & XKB_STATE_MODS_DEPRESSED)
637 mod_mask |= state->components.base_mods;
638 if (led->which_mods & XKB_STATE_MODS_LATCHED)
639 mod_mask |= state->components.latched_mods;
640 if (led->which_mods & XKB_STATE_MODS_LOCKED)
641 mod_mask |= state->components.locked_mods;
643 if (led->mods.mask & mod_mask) {
644 state->components.leds |= (1u << idx);
649 if (led->which_groups != 0 && led->groups != 0) {
650 if (led->which_groups & XKB_STATE_LAYOUT_EFFECTIVE)
651 group_mask |= (1u << state->components.group);
652 if (led->which_groups & XKB_STATE_LAYOUT_DEPRESSED)
653 group_mask |= (1u << state->components.base_group);
654 if (led->which_groups & XKB_STATE_LAYOUT_LATCHED)
655 group_mask |= (1u << state->components.latched_group);
656 if (led->which_groups & XKB_STATE_LAYOUT_LOCKED)
657 group_mask |= (1u << state->components.locked_group);
659 if (led->groups & group_mask) {
660 state->components.leds |= (1u << idx);
665 if (led->ctrls & state->keymap->enabled_ctrls) {
666 state->components.leds |= (1u << idx);
673 * Calculates the derived state (effective mods/group and LEDs) from an
674 * up-to-date xkb_state.
677 xkb_state_update_derived(struct xkb_state *state)
679 xkb_layout_index_t wrapped;
681 state->components.mods = (state->components.base_mods |
682 state->components.latched_mods |
683 state->components.locked_mods);
685 /* TODO: Use groups_wrap control instead of always RANGE_WRAP. */
687 wrapped = XkbWrapGroupIntoRange(state->components.locked_group,
688 state->keymap->num_groups,
690 state->components.locked_group =
691 (wrapped == XKB_LAYOUT_INVALID ? 0 : wrapped);
693 wrapped = XkbWrapGroupIntoRange(state->components.base_group +
694 state->components.latched_group +
695 state->components.locked_group,
696 state->keymap->num_groups,
698 state->components.group =
699 (wrapped == XKB_LAYOUT_INVALID ? 0 : wrapped);
701 xkb_state_led_update_all(state);
704 static enum xkb_state_component
705 get_state_component_changes(const struct state_components *a,
706 const struct state_components *b)
708 xkb_mod_mask_t mask = 0;
710 if (a->group != b->group)
711 mask |= XKB_STATE_LAYOUT_EFFECTIVE;
712 if (a->base_group != b->base_group)
713 mask |= XKB_STATE_LAYOUT_DEPRESSED;
714 if (a->latched_group != b->latched_group)
715 mask |= XKB_STATE_LAYOUT_LATCHED;
716 if (a->locked_group != b->locked_group)
717 mask |= XKB_STATE_LAYOUT_LOCKED;
718 if (a->mods != b->mods)
719 mask |= XKB_STATE_MODS_EFFECTIVE;
720 if (a->base_mods != b->base_mods)
721 mask |= XKB_STATE_MODS_DEPRESSED;
722 if (a->latched_mods != b->latched_mods)
723 mask |= XKB_STATE_MODS_LATCHED;
724 if (a->locked_mods != b->locked_mods)
725 mask |= XKB_STATE_MODS_LOCKED;
726 if (a->leds != b->leds)
727 mask |= XKB_STATE_LEDS;
733 * Given a particular key event, updates the state structure to reflect the
736 XKB_EXPORT enum xkb_state_component
737 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
738 enum xkb_key_direction direction)
742 struct state_components prev_components;
743 const struct xkb_key *key = XkbKey(state->keymap, kc);
748 prev_components = state->components;
751 state->clear_mods = 0;
753 xkb_filter_apply_all(state, key, direction);
755 for (i = 0, bit = 1; state->set_mods; i++, bit <<= 1) {
756 if (state->set_mods & bit) {
757 state->mod_key_count[i]++;
758 state->components.base_mods |= bit;
759 state->set_mods &= ~bit;
763 for (i = 0, bit = 1; state->clear_mods; i++, bit <<= 1) {
764 if (state->clear_mods & bit) {
765 state->mod_key_count[i]--;
766 if (state->mod_key_count[i] <= 0) {
767 state->components.base_mods &= ~bit;
768 state->mod_key_count[i] = 0;
770 state->clear_mods &= ~bit;
774 xkb_state_update_derived(state);
776 return get_state_component_changes(&prev_components, &state->components);
780 * Updates the state from a set of explicit masks as gained from
781 * xkb_state_serialize_mods and xkb_state_serialize_groups. As noted in the
782 * documentation for these functions in xkbcommon.h, this round-trip is
783 * lossy, and should only be used to update a slave state mirroring the
784 * master, e.g. in a client/server window system.
786 XKB_EXPORT enum xkb_state_component
787 xkb_state_update_mask(struct xkb_state *state,
788 xkb_mod_mask_t base_mods,
789 xkb_mod_mask_t latched_mods,
790 xkb_mod_mask_t locked_mods,
791 xkb_layout_index_t base_group,
792 xkb_layout_index_t latched_group,
793 xkb_layout_index_t locked_group)
795 struct state_components prev_components;
798 prev_components = state->components;
800 /* Only include modifiers which exist in the keymap. */
801 mask = (xkb_mod_mask_t) ((1ull << xkb_keymap_num_mods(state->keymap)) - 1u);
803 state->components.base_mods = base_mods & mask;
804 state->components.latched_mods = latched_mods & mask;
805 state->components.locked_mods = locked_mods & mask;
807 /* Make sure the mods are fully resolved - since we get arbitrary
808 * input, they might not be.
810 * It might seem more reasonable to do this only for components.mods
811 * in xkb_state_update_derived(), rather than for each component
812 * seperately. That would allow to distinguish between "really"
813 * depressed mods (would be in MODS_DEPRESSED) and indirectly
814 * depressed to to a mapping (would only be in MODS_EFFECTIVE).
815 * However, the traditional behavior of xkb_state_update_key() is that
816 * if a vmod is depressed, its mappings are depressed with it; so we're
817 * expected to do the same here. Also, LEDs (usually) look if a real
818 * mod is locked, not just effective; otherwise it won't be lit.
820 * We OR here because mod_mask_get_effective() drops vmods. */
821 state->components.base_mods |=
822 mod_mask_get_effective(state->keymap, state->components.base_mods);
823 state->components.latched_mods |=
824 mod_mask_get_effective(state->keymap, state->components.latched_mods);
825 state->components.locked_mods |=
826 mod_mask_get_effective(state->keymap, state->components.locked_mods);
828 state->components.base_group = base_group;
829 state->components.latched_group = latched_group;
830 state->components.locked_group = locked_group;
832 xkb_state_update_derived(state);
834 return get_state_component_changes(&prev_components, &state->components);
838 * Provides the symbols to use for the given key and state. Returns the
839 * number of symbols pointed to in syms_out.
842 xkb_state_key_get_syms(struct xkb_state *state, xkb_keycode_t kc,
843 const xkb_keysym_t **syms_out)
845 xkb_layout_index_t layout;
846 xkb_level_index_t level;
848 layout = xkb_state_key_get_layout(state, kc);
849 if (layout == XKB_LAYOUT_INVALID)
852 level = xkb_state_key_get_level(state, kc, layout);
853 if (level == XKB_LEVEL_INVALID)
856 return xkb_keymap_key_get_syms_by_level(state->keymap, kc, layout, level,
865 * http://www.x.org/releases/current/doc/kbproto/xkbproto.html#Interpreting_the_Lock_Modifier
868 should_do_caps_transformation(struct xkb_state *state, xkb_keycode_t kc)
870 xkb_mod_index_t caps =
871 xkb_keymap_mod_get_index(state->keymap, XKB_MOD_NAME_CAPS);
874 xkb_state_mod_index_is_active(state, caps, XKB_STATE_MODS_EFFECTIVE) > 0 &&
875 xkb_state_mod_index_is_consumed(state, kc, caps) == 0;
879 * http://www.x.org/releases/current/doc/kbproto/xkbproto.html#Interpreting_the_Control_Modifier
882 should_do_ctrl_transformation(struct xkb_state *state, xkb_keycode_t kc)
884 xkb_mod_index_t ctrl =
885 xkb_keymap_mod_get_index(state->keymap, XKB_MOD_NAME_CTRL);
888 xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_MODS_EFFECTIVE) > 0 &&
889 xkb_state_mod_index_is_consumed(state, kc, ctrl) == 0;
892 /* Verbatim from libX11:src/xkb/XKBBind.c */
894 XkbToControl(char ch)
898 if ((c >= '@' && c < '\177') || c == ' ')
902 else if (c >= '3' && c <= '7')
912 * Provides either exactly one symbol, or XKB_KEY_NoSymbol.
914 XKB_EXPORT xkb_keysym_t
915 xkb_state_key_get_one_sym(struct xkb_state *state, xkb_keycode_t kc)
917 const xkb_keysym_t *syms;
921 num_syms = xkb_state_key_get_syms(state, kc, &syms);
923 return XKB_KEY_NoSymbol;
927 if (should_do_caps_transformation(state, kc))
928 sym = xkb_keysym_to_upper(sym);
934 * The caps and ctrl transformations require some special handling,
935 * so we cannot simply use xkb_state_get_one_sym() for them.
936 * In particular, if Control is set, we must try very hard to find
937 * some layout in which the keysym is ASCII and thus can be (maybe)
938 * converted to a control character. libX11 allows to disable this
939 * behavior with the XkbLC_ControlFallback (see XkbSetXlibControls(3)),
940 * but it is enabled by default, yippee.
943 get_one_sym_for_string(struct xkb_state *state, xkb_keycode_t kc)
945 xkb_level_index_t level;
946 xkb_layout_index_t layout, num_layouts;
947 const xkb_keysym_t *syms;
951 layout = xkb_state_key_get_layout(state, kc);
952 num_layouts = xkb_keymap_num_layouts_for_key(state->keymap, kc);
953 level = xkb_state_key_get_level(state, kc, layout);
954 if (layout == XKB_LAYOUT_INVALID || num_layouts == 0 ||
955 level == XKB_LEVEL_INVALID)
956 return XKB_KEY_NoSymbol;
958 nsyms = xkb_keymap_key_get_syms_by_level(state->keymap, kc,
959 layout, level, &syms);
961 return XKB_KEY_NoSymbol;
964 if (should_do_ctrl_transformation(state, kc) && sym > 127u) {
965 for (xkb_layout_index_t i = 0; i < num_layouts; i++) {
966 level = xkb_state_key_get_level(state, kc, i);
967 if (level == XKB_LEVEL_INVALID)
970 nsyms = xkb_keymap_key_get_syms_by_level(state->keymap, kc,
972 if (nsyms == 1 && syms[0] <= 127u) {
979 if (should_do_caps_transformation(state, kc)) {
980 sym = xkb_keysym_to_upper(sym);
987 xkb_state_key_get_utf8(struct xkb_state *state, xkb_keycode_t kc,
988 char *buffer, size_t size)
991 const xkb_keysym_t *syms;
996 sym = get_one_sym_for_string(state, kc);
997 if (sym != XKB_KEY_NoSymbol) {
998 nsyms = 1; syms = &sym;
1001 nsyms = xkb_state_key_get_syms(state, kc, &syms);
1004 /* Make sure not to truncate in the middle of a UTF-8 sequence. */
1006 for (int i = 0; i < nsyms; i++) {
1007 int ret = xkb_keysym_to_utf8(syms[i], tmp, sizeof(tmp));
1012 if ((size_t) (offset + ret) <= size)
1013 memcpy(buffer + offset, tmp, ret);
1017 if ((size_t) offset >= size)
1019 buffer[offset] = '\0';
1021 if (!is_valid_utf8(buffer, offset))
1024 if (offset == 1 && (unsigned int) buffer[0] <= 127u &&
1025 should_do_ctrl_transformation(state, kc))
1026 buffer[0] = XkbToControl(buffer[0]);
1032 buffer[size - 1] = '\0';
1042 xkb_state_key_get_utf32(struct xkb_state *state, xkb_keycode_t kc)
1047 sym = get_one_sym_for_string(state, kc);
1048 cp = xkb_keysym_to_utf32(sym);
1050 if (cp <= 127u && should_do_ctrl_transformation(state, kc))
1051 cp = (uint32_t) XkbToControl((char) cp);
1057 * Serialises the requested modifier state into an xkb_mod_mask_t, with all
1058 * the same disclaimers as in xkb_state_update_mask.
1060 XKB_EXPORT xkb_mod_mask_t
1061 xkb_state_serialize_mods(struct xkb_state *state,
1062 enum xkb_state_component type)
1064 xkb_mod_mask_t ret = 0;
1066 if (type & XKB_STATE_MODS_EFFECTIVE)
1067 return state->components.mods;
1069 if (type & XKB_STATE_MODS_DEPRESSED)
1070 ret |= state->components.base_mods;
1071 if (type & XKB_STATE_MODS_LATCHED)
1072 ret |= state->components.latched_mods;
1073 if (type & XKB_STATE_MODS_LOCKED)
1074 ret |= state->components.locked_mods;
1080 * Serialises the requested group state, with all the same disclaimers as
1081 * in xkb_state_update_mask.
1083 XKB_EXPORT xkb_layout_index_t
1084 xkb_state_serialize_layout(struct xkb_state *state,
1085 enum xkb_state_component type)
1087 xkb_layout_index_t ret = 0;
1089 if (type & XKB_STATE_LAYOUT_EFFECTIVE)
1090 return state->components.group;
1092 if (type & XKB_STATE_LAYOUT_DEPRESSED)
1093 ret += state->components.base_group;
1094 if (type & XKB_STATE_LAYOUT_LATCHED)
1095 ret += state->components.latched_group;
1096 if (type & XKB_STATE_LAYOUT_LOCKED)
1097 ret += state->components.locked_group;
1103 * Gets a modifier mask and returns the resolved effective mask; this
1104 * is needed because some modifiers can also map to other modifiers, e.g.
1105 * the "NumLock" modifier usually also sets the "Mod2" modifier.
1108 mod_mask_get_effective(struct xkb_keymap *keymap, xkb_mod_mask_t mods)
1110 const struct xkb_mod *mod;
1112 xkb_mod_mask_t mask;
1114 /* The effective mask is only real mods for now. */
1115 mask = mods & MOD_REAL_MASK_ALL;
1117 xkb_mods_enumerate(i, mod, &keymap->mods)
1118 if (mods & (1u << i))
1119 mask |= mod->mapping;
1125 * Returns 1 if the given modifier is active with the specified type(s), 0 if
1126 * not, or -1 if the modifier is invalid.
1129 xkb_state_mod_index_is_active(struct xkb_state *state,
1130 xkb_mod_index_t idx,
1131 enum xkb_state_component type)
1133 if (idx >= xkb_keymap_num_mods(state->keymap))
1136 return !!(xkb_state_serialize_mods(state, type) & (1u << idx));
1140 * Helper function for xkb_state_mod_indices_are_active and
1141 * xkb_state_mod_names_are_active.
1144 match_mod_masks(struct xkb_state *state,
1145 enum xkb_state_component type,
1146 enum xkb_state_match match,
1147 xkb_mod_mask_t wanted)
1149 xkb_mod_mask_t active = xkb_state_serialize_mods(state, type);
1151 if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
1154 if (match & XKB_STATE_MATCH_ANY)
1155 return !!(active & wanted);
1157 return (active & wanted) == wanted;
1163 * Returns 1 if the modifiers are active with the specified type(s), 0 if
1164 * not, or -1 if any of the modifiers are invalid.
1167 xkb_state_mod_indices_are_active(struct xkb_state *state,
1168 enum xkb_state_component type,
1169 enum xkb_state_match match,
1173 xkb_mod_mask_t wanted = 0;
1175 xkb_mod_index_t num_mods = xkb_keymap_num_mods(state->keymap);
1177 va_start(ap, match);
1179 xkb_mod_index_t idx = va_arg(ap, xkb_mod_index_t);
1180 if (idx == XKB_MOD_INVALID)
1182 if (idx >= num_mods) {
1186 wanted |= (1u << idx);
1193 return match_mod_masks(state, type, match, wanted);
1197 * Returns 1 if the given modifier is active with the specified type(s), 0 if
1198 * not, or -1 if the modifier is invalid.
1201 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
1202 enum xkb_state_component type)
1204 xkb_mod_index_t idx = xkb_keymap_mod_get_index(state->keymap, name);
1206 if (idx == XKB_MOD_INVALID)
1209 return xkb_state_mod_index_is_active(state, idx, type);
1213 * Returns 1 if the modifiers are active with the specified type(s), 0 if
1214 * not, or -1 if any of the modifiers are invalid.
1216 XKB_EXPORT ATTR_NULL_SENTINEL int
1217 xkb_state_mod_names_are_active(struct xkb_state *state,
1218 enum xkb_state_component type,
1219 enum xkb_state_match match,
1223 xkb_mod_mask_t wanted = 0;
1226 va_start(ap, match);
1228 xkb_mod_index_t idx;
1229 const char *str = va_arg(ap, const char *);
1232 idx = xkb_keymap_mod_get_index(state->keymap, str);
1233 if (idx == XKB_MOD_INVALID) {
1237 wanted |= (1u << idx);
1244 return match_mod_masks(state, type, match, wanted);
1248 * Returns 1 if the given group is active with the specified type(s), 0 if
1249 * not, or -1 if the group is invalid.
1252 xkb_state_layout_index_is_active(struct xkb_state *state,
1253 xkb_layout_index_t idx,
1254 enum xkb_state_component type)
1258 if (idx >= state->keymap->num_groups)
1261 if (type & XKB_STATE_LAYOUT_EFFECTIVE)
1262 ret |= (state->components.group == idx);
1263 if (type & XKB_STATE_LAYOUT_DEPRESSED)
1264 ret |= (state->components.base_group == (int32_t) idx);
1265 if (type & XKB_STATE_LAYOUT_LATCHED)
1266 ret |= (state->components.latched_group == (int32_t) idx);
1267 if (type & XKB_STATE_LAYOUT_LOCKED)
1268 ret |= (state->components.locked_group == (int32_t) idx);
1274 * Returns 1 if the given modifier is active with the specified type(s), 0 if
1275 * not, or -1 if the modifier is invalid.
1278 xkb_state_layout_name_is_active(struct xkb_state *state, const char *name,
1279 enum xkb_state_component type)
1281 xkb_layout_index_t idx = xkb_keymap_layout_get_index(state->keymap, name);
1283 if (idx == XKB_LAYOUT_INVALID)
1286 return xkb_state_layout_index_is_active(state, idx, type);
1290 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
1293 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
1295 if (idx >= state->keymap->num_leds ||
1296 state->keymap->leds[idx].name == XKB_ATOM_NONE)
1299 return !!(state->components.leds & (1u << idx));
1303 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
1306 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
1308 xkb_led_index_t idx = xkb_keymap_led_get_index(state->keymap, name);
1310 if (idx == XKB_LED_INVALID)
1313 return xkb_state_led_index_is_active(state, idx);
1316 static xkb_mod_mask_t
1317 key_get_consumed(struct xkb_state *state, const struct xkb_key *key)
1319 const struct xkb_key_type *type;
1320 const struct xkb_key_type_entry *entry;
1321 xkb_mod_mask_t preserve;
1322 xkb_layout_index_t group;
1324 group = xkb_state_key_get_layout(state, key->keycode);
1325 if (group == XKB_LAYOUT_INVALID)
1328 type = key->groups[group].type;
1330 entry = get_entry_for_key_state(state, key, group);
1332 preserve = entry->preserve.mask;
1336 return type->mods.mask & ~preserve;
1340 * Tests to see if a modifier is used up by our translation of a
1341 * keycode to keysyms, taking note of the current modifier state and
1342 * the appropriate key type's preserve information, if any. This allows
1343 * the user to mask out the modifier in later processing of the
1344 * modifiers, e.g. when implementing hot keys or accelerators.
1346 * See also, for example:
1347 * - XkbTranslateKeyCode(3), mod_rtrn return value, from libX11.
1348 * - gdk_keymap_translate_keyboard_state, consumed_modifiers return value,
1352 xkb_state_mod_index_is_consumed(struct xkb_state *state, xkb_keycode_t kc,
1353 xkb_mod_index_t idx)
1355 const struct xkb_key *key = XkbKey(state->keymap, kc);
1357 if (!key || idx >= xkb_keymap_num_mods(state->keymap))
1360 return !!((1u << idx) & key_get_consumed(state, key));
1364 * Calculates which modifiers should be consumed during key processing,
1365 * and returns the mask with all these modifiers removed. e.g. if
1366 * given a state of Alt and Shift active for a two-level alphabetic
1367 * key containing plus and equal on the first and second level
1368 * respectively, will return a mask of only Alt, as Shift has been
1369 * consumed by the type handling.
1371 XKB_EXPORT xkb_mod_mask_t
1372 xkb_state_mod_mask_remove_consumed(struct xkb_state *state, xkb_keycode_t kc,
1373 xkb_mod_mask_t mask)
1375 const struct xkb_key *key = XkbKey(state->keymap, kc);
1380 return mask & ~key_get_consumed(state, key);
1383 XKB_EXPORT xkb_mod_mask_t
1384 xkb_state_key_get_consumed_mods(struct xkb_state *state, xkb_keycode_t kc)
1386 const struct xkb_key *key = XkbKey(state->keymap, kc);
1391 return key_get_consumed(state, key);