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_key_state(struct xkb_state *state, const struct xkb_key *key,
132 xkb_layout_index_t group)
134 const struct xkb_key_type *type = key->groups[group].type;
135 xkb_mod_mask_t active_mods = state->components.mods & type->mods.mask;
137 for (unsigned i = 0; i < type->num_entries; i++)
138 if (entry_is_active(&type->entries[i]) &&
139 type->entries[i].mods.mask == active_mods)
140 return &type->entries[i];
146 * Returns the level to use for the given key and state, or
149 XKB_EXPORT xkb_level_index_t
150 xkb_state_key_get_level(struct xkb_state *state, xkb_keycode_t kc,
151 xkb_layout_index_t layout)
153 const struct xkb_key *key = XkbKey(state->keymap, kc);
154 const struct xkb_key_type_entry *entry;
156 if (!key || layout >= key->num_groups)
157 return XKB_LEVEL_INVALID;
159 /* If we don't find an explicit match the default is 0. */
160 entry = get_entry_for_key_state(state, key, layout);
168 XkbWrapGroupIntoRange(int32_t group,
169 xkb_layout_index_t num_groups,
170 enum xkb_range_exceed_type out_of_range_group_action,
171 xkb_layout_index_t out_of_range_group_number)
174 return XKB_LAYOUT_INVALID;
176 if (group >= 0 && (xkb_layout_index_t) group < num_groups)
179 switch (out_of_range_group_action) {
181 if (out_of_range_group_number >= num_groups)
183 return out_of_range_group_number;
189 return num_groups - 1;
194 * C99 says a negative dividend in a modulo operation always
195 * gives a negative result.
198 return ((int) num_groups + (group % (int) num_groups));
200 return group % num_groups;
205 * Returns the layout to use for the given key and state, taking
206 * wrapping/clamping/etc into account, or XKB_LAYOUT_INVALID.
208 XKB_EXPORT xkb_layout_index_t
209 xkb_state_key_get_layout(struct xkb_state *state, xkb_keycode_t kc)
211 const struct xkb_key *key = XkbKey(state->keymap, kc);
214 return XKB_LAYOUT_INVALID;
216 return XkbWrapGroupIntoRange(state->components.group, key->num_groups,
217 key->out_of_range_group_action,
218 key->out_of_range_group_number);
221 static const union xkb_action *
222 xkb_key_get_action(struct xkb_state *state, const struct xkb_key *key)
224 static const union xkb_action dummy = { .type = ACTION_TYPE_NONE };
226 xkb_layout_index_t layout;
227 xkb_level_index_t level;
229 layout = xkb_state_key_get_layout(state, key->keycode);
230 if (layout == XKB_LAYOUT_INVALID)
233 level = xkb_state_key_get_level(state, key->keycode, layout);
234 if (level == XKB_LEVEL_INVALID)
237 return &key->groups[layout].levels[level].action;
240 static struct xkb_filter *
241 xkb_filter_new(struct xkb_state *state)
243 struct xkb_filter *filter = NULL, *iter;
245 darray_foreach(iter, state->filters) {
253 darray_resize0(state->filters, darray_size(state->filters) + 1);
254 filter = &darray_item(state->filters, darray_size(state->filters) -1);
261 /***====================================================================***/
264 xkb_filter_group_set_func(struct xkb_state *state,
265 struct xkb_filter *filter,
266 const struct xkb_key *key,
267 enum xkb_key_direction direction)
269 if (key != filter->key) {
270 filter->action.group.flags &= ~ACTION_LOCK_CLEAR;
274 if (direction == XKB_KEY_DOWN) {
278 else if (--filter->refcnt > 0) {
282 state->components.base_group = filter->priv;
284 if (filter->action.group.flags & ACTION_LOCK_CLEAR)
285 state->components.locked_group = 0;
292 xkb_filter_group_set_new(struct xkb_state *state, struct xkb_filter *filter)
294 filter->priv = state->components.base_group;
295 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
296 state->components.base_group = filter->action.group.group;
298 state->components.base_group += filter->action.group.group;
302 xkb_filter_group_lock_func(struct xkb_state *state,
303 struct xkb_filter *filter,
304 const struct xkb_key *key,
305 enum xkb_key_direction direction)
307 if (key != filter->key)
310 if (direction == XKB_KEY_DOWN) {
314 if (--filter->refcnt > 0)
322 xkb_filter_group_lock_new(struct xkb_state *state, struct xkb_filter *filter)
324 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
325 state->components.locked_group = filter->action.group.group;
327 state->components.locked_group += filter->action.group.group;
331 xkb_filter_mod_set_func(struct xkb_state *state,
332 struct xkb_filter *filter,
333 const struct xkb_key *key,
334 enum xkb_key_direction direction)
336 if (key != filter->key) {
337 filter->action.mods.flags &= ~ACTION_LOCK_CLEAR;
341 if (direction == XKB_KEY_DOWN) {
345 else if (--filter->refcnt > 0) {
349 state->clear_mods = filter->action.mods.mods.mask;
350 if (filter->action.mods.flags & ACTION_LOCK_CLEAR)
351 state->components.locked_mods &= ~filter->action.mods.mods.mask;
358 xkb_filter_mod_set_new(struct xkb_state *state, struct xkb_filter *filter)
360 state->set_mods = filter->action.mods.mods.mask;
364 xkb_filter_mod_lock_func(struct xkb_state *state,
365 struct xkb_filter *filter,
366 const struct xkb_key *key,
367 enum xkb_key_direction direction)
369 if (key != filter->key)
372 if (direction == XKB_KEY_DOWN) {
376 if (--filter->refcnt > 0)
379 state->clear_mods |= filter->action.mods.mods.mask;
380 if (!(filter->action.mods.flags & ACTION_LOCK_NO_UNLOCK))
381 state->components.locked_mods &= ~filter->priv;
388 xkb_filter_mod_lock_new(struct xkb_state *state, struct xkb_filter *filter)
390 filter->priv = (state->components.locked_mods &
391 filter->action.mods.mods.mask);
392 state->set_mods |= filter->action.mods.mods.mask;
393 if (!(filter->action.mods.flags & ACTION_LOCK_NO_LOCK))
394 state->components.locked_mods |= filter->action.mods.mods.mask;
397 enum xkb_key_latch_state {
404 xkb_action_breaks_latch(const union xkb_action *action)
406 switch (action->type) {
407 case ACTION_TYPE_NONE:
408 case ACTION_TYPE_PTR_BUTTON:
409 case ACTION_TYPE_PTR_LOCK:
410 case ACTION_TYPE_CTRL_SET:
411 case ACTION_TYPE_CTRL_LOCK:
412 case ACTION_TYPE_SWITCH_VT:
413 case ACTION_TYPE_TERMINATE:
421 xkb_filter_mod_latch_func(struct xkb_state *state,
422 struct xkb_filter *filter,
423 const struct xkb_key *key,
424 enum xkb_key_direction direction)
426 enum xkb_key_latch_state latch = filter->priv;
428 if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
429 /* If this is a new keypress and we're awaiting our single latched
430 * keypress, then either break the latch if any random key is pressed,
431 * or promote it to a lock or plain base set if it's the same
433 const union xkb_action *action = xkb_key_get_action(state, key);
434 if (action->type == ACTION_TYPE_MOD_LATCH &&
435 action->mods.flags == filter->action.mods.flags &&
436 action->mods.mods.mask == filter->action.mods.mods.mask) {
437 filter->action = *action;
438 if (filter->action.mods.flags & ACTION_LATCH_TO_LOCK) {
439 filter->action.type = ACTION_TYPE_MOD_LOCK;
440 filter->func = xkb_filter_mod_lock_func;
441 state->components.locked_mods |= filter->action.mods.mods.mask;
444 filter->action.type = ACTION_TYPE_MOD_SET;
445 filter->func = xkb_filter_mod_set_func;
446 state->set_mods = filter->action.mods.mods.mask;
449 state->components.latched_mods &= ~filter->action.mods.mods.mask;
453 else if (xkb_action_breaks_latch(action)) {
454 /* XXX: This may be totally broken, we might need to break the
455 * latch in the next run after this press? */
456 state->components.latched_mods &= ~filter->action.mods.mods.mask;
461 else if (direction == XKB_KEY_UP && key == filter->key) {
462 /* Our key got released. If we've set it to clear locks, and we
463 * currently have the same modifiers locked, then release them and
464 * don't actually latch. Else we've actually hit the latching
465 * stage, so set PENDING and move our modifier from base to
467 if (latch == NO_LATCH ||
468 ((filter->action.mods.flags & ACTION_LOCK_CLEAR) &&
469 (state->components.locked_mods & filter->action.mods.mods.mask) ==
470 filter->action.mods.mods.mask)) {
471 /* XXX: We might be a bit overenthusiastic about clearing
472 * mods other filters have set here? */
473 if (latch == LATCH_PENDING)
474 state->components.latched_mods &=
475 ~filter->action.mods.mods.mask;
477 state->clear_mods = filter->action.mods.mods.mask;
478 state->components.locked_mods &= ~filter->action.mods.mods.mask;
482 latch = LATCH_PENDING;
483 state->clear_mods = filter->action.mods.mods.mask;
484 state->components.latched_mods |= filter->action.mods.mods.mask;
488 else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
489 /* Someone's pressed another key while we've still got the latching
490 * key held down, so keep the base modifier state active (from
491 * xkb_filter_mod_latch_new), but don't trip the latch, just clear
492 * it as soon as the modifier gets released. */
496 filter->priv = latch;
502 xkb_filter_mod_latch_new(struct xkb_state *state, struct xkb_filter *filter)
504 filter->priv = LATCH_KEY_DOWN;
505 state->set_mods = filter->action.mods.mods.mask;
508 static const struct {
509 void (*new)(struct xkb_state *state, struct xkb_filter *filter);
510 bool (*func)(struct xkb_state *state, struct xkb_filter *filter,
511 const struct xkb_key *key, enum xkb_key_direction direction);
512 } filter_action_funcs[_ACTION_TYPE_NUM_ENTRIES] = {
513 [ACTION_TYPE_MOD_SET] = { xkb_filter_mod_set_new,
514 xkb_filter_mod_set_func },
515 [ACTION_TYPE_MOD_LATCH] = { xkb_filter_mod_latch_new,
516 xkb_filter_mod_latch_func },
517 [ACTION_TYPE_MOD_LOCK] = { xkb_filter_mod_lock_new,
518 xkb_filter_mod_lock_func },
519 [ACTION_TYPE_GROUP_SET] = { xkb_filter_group_set_new,
520 xkb_filter_group_set_func },
521 [ACTION_TYPE_GROUP_LOCK] = { xkb_filter_group_lock_new,
522 xkb_filter_group_lock_func },
526 * Applies any relevant filters to the key, first from the list of filters
527 * that are currently active, then if no filter has claimed the key, possibly
528 * apply a new filter from the key action.
531 xkb_filter_apply_all(struct xkb_state *state,
532 const struct xkb_key *key,
533 enum xkb_key_direction direction)
535 struct xkb_filter *filter;
536 const union xkb_action *action;
539 /* First run through all the currently active filters and see if any of
540 * them have claimed this event. */
541 darray_foreach(filter, state->filters) {
544 send = filter->func(state, filter, key, direction) && send;
547 if (!send || direction == XKB_KEY_UP)
550 action = xkb_key_get_action(state, key);
553 * It's possible for the keymap to set action->type explicitly, like so:
554 * interpret XF86_Next_VMode {
555 * action = Private(type=0x86, data="+VMode");
557 * We don't handle those.
559 if (action->type >= _ACTION_TYPE_NUM_ENTRIES)
562 if (!filter_action_funcs[action->type].new)
565 filter = xkb_filter_new(state);
570 filter->func = filter_action_funcs[action->type].func;
571 filter->action = *action;
572 filter_action_funcs[action->type].new(state, filter);
575 XKB_EXPORT struct xkb_state *
576 xkb_state_new(struct xkb_keymap *keymap)
578 struct xkb_state *ret;
580 ret = calloc(sizeof(*ret), 1);
585 ret->keymap = xkb_keymap_ref(keymap);
590 XKB_EXPORT struct xkb_state *
591 xkb_state_ref(struct xkb_state *state)
598 xkb_state_unref(struct xkb_state *state)
600 if (!state || --state->refcnt > 0)
603 xkb_keymap_unref(state->keymap);
604 darray_free(state->filters);
608 XKB_EXPORT struct xkb_keymap *
609 xkb_state_get_keymap(struct xkb_state *state)
611 return state->keymap;
615 * Update the LED state to match the rest of the xkb_state.
618 xkb_state_led_update_all(struct xkb_state *state)
621 const struct xkb_led *led;
623 state->components.leds = 0;
625 xkb_leds_enumerate(idx, led, state->keymap) {
626 xkb_mod_mask_t mod_mask = 0;
627 xkb_layout_mask_t group_mask = 0;
629 if (led->which_mods != 0 && led->mods.mask != 0) {
630 if (led->which_mods & XKB_STATE_MODS_EFFECTIVE)
631 mod_mask |= state->components.mods;
632 if (led->which_mods & XKB_STATE_MODS_DEPRESSED)
633 mod_mask |= state->components.base_mods;
634 if (led->which_mods & XKB_STATE_MODS_LATCHED)
635 mod_mask |= state->components.latched_mods;
636 if (led->which_mods & XKB_STATE_MODS_LOCKED)
637 mod_mask |= state->components.locked_mods;
639 if (led->mods.mask & mod_mask) {
640 state->components.leds |= (1u << idx);
645 if (led->which_groups != 0 && led->groups != 0) {
646 if (led->which_groups & XKB_STATE_LAYOUT_EFFECTIVE)
647 group_mask |= (1u << state->components.group);
648 if (led->which_groups & XKB_STATE_LAYOUT_DEPRESSED)
649 group_mask |= (1u << state->components.base_group);
650 if (led->which_groups & XKB_STATE_LAYOUT_LATCHED)
651 group_mask |= (1u << state->components.latched_group);
652 if (led->which_groups & XKB_STATE_LAYOUT_LOCKED)
653 group_mask |= (1u << state->components.locked_group);
655 if (led->groups & group_mask) {
656 state->components.leds |= (1u << idx);
661 if (led->ctrls & state->keymap->enabled_ctrls) {
662 state->components.leds |= (1u << idx);
669 * Calculates the derived state (effective mods/group and LEDs) from an
670 * up-to-date xkb_state.
673 xkb_state_update_derived(struct xkb_state *state)
675 xkb_layout_index_t wrapped;
677 state->components.mods = (state->components.base_mods |
678 state->components.latched_mods |
679 state->components.locked_mods);
681 /* TODO: Use groups_wrap control instead of always RANGE_WRAP. */
683 wrapped = XkbWrapGroupIntoRange(state->components.locked_group,
684 state->keymap->num_groups,
686 state->components.locked_group =
687 (wrapped == XKB_LAYOUT_INVALID ? 0 : wrapped);
689 wrapped = XkbWrapGroupIntoRange(state->components.base_group +
690 state->components.latched_group +
691 state->components.locked_group,
692 state->keymap->num_groups,
694 state->components.group =
695 (wrapped == XKB_LAYOUT_INVALID ? 0 : wrapped);
697 xkb_state_led_update_all(state);
700 static enum xkb_state_component
701 get_state_component_changes(const struct state_components *a,
702 const struct state_components *b)
704 xkb_mod_mask_t mask = 0;
706 if (a->group != b->group)
707 mask |= XKB_STATE_LAYOUT_EFFECTIVE;
708 if (a->base_group != b->base_group)
709 mask |= XKB_STATE_LAYOUT_DEPRESSED;
710 if (a->latched_group != b->latched_group)
711 mask |= XKB_STATE_LAYOUT_LATCHED;
712 if (a->locked_group != b->locked_group)
713 mask |= XKB_STATE_LAYOUT_LOCKED;
714 if (a->mods != b->mods)
715 mask |= XKB_STATE_MODS_EFFECTIVE;
716 if (a->base_mods != b->base_mods)
717 mask |= XKB_STATE_MODS_DEPRESSED;
718 if (a->latched_mods != b->latched_mods)
719 mask |= XKB_STATE_MODS_LATCHED;
720 if (a->locked_mods != b->locked_mods)
721 mask |= XKB_STATE_MODS_LOCKED;
722 if (a->leds != b->leds)
723 mask |= XKB_STATE_LEDS;
729 * Given a particular key event, updates the state structure to reflect the
732 XKB_EXPORT enum xkb_state_component
733 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
734 enum xkb_key_direction direction)
738 struct state_components prev_components;
739 const struct xkb_key *key = XkbKey(state->keymap, kc);
744 prev_components = state->components;
747 state->clear_mods = 0;
749 xkb_filter_apply_all(state, key, direction);
751 for (i = 0, bit = 1; state->set_mods; i++, bit <<= 1) {
752 if (state->set_mods & bit) {
753 state->mod_key_count[i]++;
754 state->components.base_mods |= bit;
755 state->set_mods &= ~bit;
759 for (i = 0, bit = 1; state->clear_mods; i++, bit <<= 1) {
760 if (state->clear_mods & bit) {
761 state->mod_key_count[i]--;
762 if (state->mod_key_count[i] <= 0) {
763 state->components.base_mods &= ~bit;
764 state->mod_key_count[i] = 0;
766 state->clear_mods &= ~bit;
770 xkb_state_update_derived(state);
772 return get_state_component_changes(&prev_components, &state->components);
776 * Updates the state from a set of explicit masks as gained from
777 * xkb_state_serialize_mods and xkb_state_serialize_groups. As noted in the
778 * documentation for these functions in xkbcommon.h, this round-trip is
779 * lossy, and should only be used to update a slave state mirroring the
780 * master, e.g. in a client/server window system.
782 XKB_EXPORT enum xkb_state_component
783 xkb_state_update_mask(struct xkb_state *state,
784 xkb_mod_mask_t base_mods,
785 xkb_mod_mask_t latched_mods,
786 xkb_mod_mask_t locked_mods,
787 xkb_layout_index_t base_group,
788 xkb_layout_index_t latched_group,
789 xkb_layout_index_t locked_group)
791 struct state_components prev_components;
794 prev_components = state->components;
796 /* Only include modifiers which exist in the keymap. */
797 mask = (xkb_mod_mask_t) ((1ull << xkb_keymap_num_mods(state->keymap)) - 1u);
799 state->components.base_mods = base_mods & mask;
800 state->components.latched_mods = latched_mods & mask;
801 state->components.locked_mods = locked_mods & mask;
803 /* Make sure the mods are fully resolved - since we get arbitrary
804 * input, they might not be.
806 * It might seem more reasonable to do this only for components.mods
807 * in xkb_state_update_derived(), rather than for each component
808 * seperately. That would allow to distinguish between "really"
809 * depressed mods (would be in MODS_DEPRESSED) and indirectly
810 * depressed to to a mapping (would only be in MODS_EFFECTIVE).
811 * However, the traditional behavior of xkb_state_update_key() is that
812 * if a vmod is depressed, its mappings are depressed with it; so we're
813 * expected to do the same here. Also, LEDs (usually) look if a real
814 * mod is locked, not just effective; otherwise it won't be lit.
816 * We OR here because mod_mask_get_effective() drops vmods. */
817 state->components.base_mods |=
818 mod_mask_get_effective(state->keymap, state->components.base_mods);
819 state->components.latched_mods |=
820 mod_mask_get_effective(state->keymap, state->components.latched_mods);
821 state->components.locked_mods |=
822 mod_mask_get_effective(state->keymap, state->components.locked_mods);
824 state->components.base_group = base_group;
825 state->components.latched_group = latched_group;
826 state->components.locked_group = locked_group;
828 xkb_state_update_derived(state);
830 return get_state_component_changes(&prev_components, &state->components);
834 * Provides the symbols to use for the given key and state. Returns the
835 * number of symbols pointed to in syms_out.
838 xkb_state_key_get_syms(struct xkb_state *state, xkb_keycode_t kc,
839 const xkb_keysym_t **syms_out)
841 xkb_layout_index_t layout;
842 xkb_level_index_t level;
844 layout = xkb_state_key_get_layout(state, kc);
845 if (layout == XKB_LAYOUT_INVALID)
848 level = xkb_state_key_get_level(state, kc, layout);
849 if (level == XKB_LEVEL_INVALID)
852 return xkb_keymap_key_get_syms_by_level(state->keymap, kc, layout, level,
861 * http://www.x.org/releases/current/doc/kbproto/xkbproto.html#Interpreting_the_Lock_Modifier
864 should_do_caps_transformation(struct xkb_state *state, xkb_keycode_t kc)
866 xkb_mod_index_t caps =
867 xkb_keymap_mod_get_index(state->keymap, XKB_MOD_NAME_CAPS);
870 xkb_state_mod_index_is_active(state, caps, XKB_STATE_MODS_EFFECTIVE) > 0 &&
871 xkb_state_mod_index_is_consumed(state, kc, caps) == 0;
875 * http://www.x.org/releases/current/doc/kbproto/xkbproto.html#Interpreting_the_Control_Modifier
878 should_do_ctrl_transformation(struct xkb_state *state, xkb_keycode_t kc)
880 xkb_mod_index_t ctrl =
881 xkb_keymap_mod_get_index(state->keymap, XKB_MOD_NAME_CTRL);
884 xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_MODS_EFFECTIVE) > 0 &&
885 xkb_state_mod_index_is_consumed(state, kc, ctrl) == 0;
888 /* Verbatim from libX11:src/xkb/XKBBind.c */
890 XkbToControl(char ch)
894 if ((c >= '@' && c < '\177') || c == ' ')
898 else if (c >= '3' && c <= '7')
908 * Provides either exactly one symbol, or XKB_KEY_NoSymbol.
910 XKB_EXPORT xkb_keysym_t
911 xkb_state_key_get_one_sym(struct xkb_state *state, xkb_keycode_t kc)
913 const xkb_keysym_t *syms;
917 num_syms = xkb_state_key_get_syms(state, kc, &syms);
919 return XKB_KEY_NoSymbol;
923 if (should_do_caps_transformation(state, kc))
924 sym = xkb_keysym_to_upper(sym);
930 * The caps and ctrl transformations require some special handling,
931 * so we cannot simply use xkb_state_get_one_sym() for them.
932 * In particular, if Control is set, we must try very hard to find
933 * some layout in which the keysym is ASCII and thus can be (maybe)
934 * converted to a control character. libX11 allows to disable this
935 * behavior with the XkbLC_ControlFallback (see XkbSetXlibControls(3)),
936 * but it is enabled by default, yippee.
939 get_one_sym_for_string(struct xkb_state *state, xkb_keycode_t kc)
941 xkb_level_index_t level;
942 xkb_layout_index_t layout, num_layouts;
943 const xkb_keysym_t *syms;
947 layout = xkb_state_key_get_layout(state, kc);
948 num_layouts = xkb_keymap_num_layouts_for_key(state->keymap, kc);
949 level = xkb_state_key_get_level(state, kc, layout);
950 if (layout == XKB_LAYOUT_INVALID || num_layouts == 0 ||
951 level == XKB_LEVEL_INVALID)
952 return XKB_KEY_NoSymbol;
954 nsyms = xkb_keymap_key_get_syms_by_level(state->keymap, kc,
955 layout, level, &syms);
957 return XKB_KEY_NoSymbol;
960 if (should_do_ctrl_transformation(state, kc) && sym > 127u) {
961 for (xkb_layout_index_t i = 0; i < num_layouts; i++) {
962 level = xkb_state_key_get_level(state, kc, i);
963 if (level == XKB_LEVEL_INVALID)
966 nsyms = xkb_keymap_key_get_syms_by_level(state->keymap, kc,
968 if (nsyms == 1 && syms[0] <= 127u) {
975 if (should_do_caps_transformation(state, kc)) {
976 sym = xkb_keysym_to_upper(sym);
983 xkb_state_key_get_utf8(struct xkb_state *state, xkb_keycode_t kc,
984 char *buffer, size_t size)
987 const xkb_keysym_t *syms;
992 sym = get_one_sym_for_string(state, kc);
993 if (sym != XKB_KEY_NoSymbol) {
994 nsyms = 1; syms = &sym;
997 nsyms = xkb_state_key_get_syms(state, kc, &syms);
1000 /* Make sure not to truncate in the middle of a UTF-8 sequence. */
1002 for (int i = 0; i < nsyms; i++) {
1003 int ret = xkb_keysym_to_utf8(syms[i], tmp, sizeof(tmp));
1008 if ((size_t) (offset + ret) <= size)
1009 memcpy(buffer + offset, tmp, ret);
1013 if ((size_t) offset >= size)
1015 buffer[offset] = '\0';
1017 if (!is_valid_utf8(buffer, offset))
1020 if (offset == 1 && (unsigned int) buffer[0] <= 127u &&
1021 should_do_ctrl_transformation(state, kc))
1022 buffer[0] = XkbToControl(buffer[0]);
1028 buffer[size - 1] = '\0';
1038 xkb_state_key_get_utf32(struct xkb_state *state, xkb_keycode_t kc)
1043 sym = get_one_sym_for_string(state, kc);
1044 cp = xkb_keysym_to_utf32(sym);
1046 if (cp <= 127u && should_do_ctrl_transformation(state, kc))
1047 cp = (uint32_t) XkbToControl((char) cp);
1053 * Serialises the requested modifier state into an xkb_mod_mask_t, with all
1054 * the same disclaimers as in xkb_state_update_mask.
1056 XKB_EXPORT xkb_mod_mask_t
1057 xkb_state_serialize_mods(struct xkb_state *state,
1058 enum xkb_state_component type)
1060 xkb_mod_mask_t ret = 0;
1062 if (type & XKB_STATE_MODS_EFFECTIVE)
1063 return state->components.mods;
1065 if (type & XKB_STATE_MODS_DEPRESSED)
1066 ret |= state->components.base_mods;
1067 if (type & XKB_STATE_MODS_LATCHED)
1068 ret |= state->components.latched_mods;
1069 if (type & XKB_STATE_MODS_LOCKED)
1070 ret |= state->components.locked_mods;
1076 * Serialises the requested group state, with all the same disclaimers as
1077 * in xkb_state_update_mask.
1079 XKB_EXPORT xkb_layout_index_t
1080 xkb_state_serialize_layout(struct xkb_state *state,
1081 enum xkb_state_component type)
1083 xkb_layout_index_t ret = 0;
1085 if (type & XKB_STATE_LAYOUT_EFFECTIVE)
1086 return state->components.group;
1088 if (type & XKB_STATE_LAYOUT_DEPRESSED)
1089 ret += state->components.base_group;
1090 if (type & XKB_STATE_LAYOUT_LATCHED)
1091 ret += state->components.latched_group;
1092 if (type & XKB_STATE_LAYOUT_LOCKED)
1093 ret += state->components.locked_group;
1099 * Gets a modifier mask and returns the resolved effective mask; this
1100 * is needed because some modifiers can also map to other modifiers, e.g.
1101 * the "NumLock" modifier usually also sets the "Mod2" modifier.
1104 mod_mask_get_effective(struct xkb_keymap *keymap, xkb_mod_mask_t mods)
1106 const struct xkb_mod *mod;
1108 xkb_mod_mask_t mask;
1110 /* The effective mask is only real mods for now. */
1111 mask = mods & MOD_REAL_MASK_ALL;
1113 xkb_mods_enumerate(i, mod, &keymap->mods)
1114 if (mods & (1u << i))
1115 mask |= mod->mapping;
1121 * Returns 1 if the given modifier is active with the specified type(s), 0 if
1122 * not, or -1 if the modifier is invalid.
1125 xkb_state_mod_index_is_active(struct xkb_state *state,
1126 xkb_mod_index_t idx,
1127 enum xkb_state_component type)
1129 if (idx >= xkb_keymap_num_mods(state->keymap))
1132 return !!(xkb_state_serialize_mods(state, type) & (1u << idx));
1136 * Helper function for xkb_state_mod_indices_are_active and
1137 * xkb_state_mod_names_are_active.
1140 match_mod_masks(struct xkb_state *state,
1141 enum xkb_state_component type,
1142 enum xkb_state_match match,
1143 xkb_mod_mask_t wanted)
1145 xkb_mod_mask_t active = xkb_state_serialize_mods(state, type);
1147 if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
1150 if (match & XKB_STATE_MATCH_ANY)
1151 return !!(active & wanted);
1153 return (active & wanted) == wanted;
1159 * Returns 1 if the modifiers are active with the specified type(s), 0 if
1160 * not, or -1 if any of the modifiers are invalid.
1163 xkb_state_mod_indices_are_active(struct xkb_state *state,
1164 enum xkb_state_component type,
1165 enum xkb_state_match match,
1169 xkb_mod_mask_t wanted = 0;
1171 xkb_mod_index_t num_mods = xkb_keymap_num_mods(state->keymap);
1173 va_start(ap, match);
1175 xkb_mod_index_t idx = va_arg(ap, xkb_mod_index_t);
1176 if (idx == XKB_MOD_INVALID)
1178 if (idx >= num_mods) {
1182 wanted |= (1u << idx);
1189 return match_mod_masks(state, type, match, wanted);
1193 * Returns 1 if the given modifier is active with the specified type(s), 0 if
1194 * not, or -1 if the modifier is invalid.
1197 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
1198 enum xkb_state_component type)
1200 xkb_mod_index_t idx = xkb_keymap_mod_get_index(state->keymap, name);
1202 if (idx == XKB_MOD_INVALID)
1205 return xkb_state_mod_index_is_active(state, idx, type);
1209 * Returns 1 if the modifiers are active with the specified type(s), 0 if
1210 * not, or -1 if any of the modifiers are invalid.
1212 XKB_EXPORT ATTR_NULL_SENTINEL int
1213 xkb_state_mod_names_are_active(struct xkb_state *state,
1214 enum xkb_state_component type,
1215 enum xkb_state_match match,
1219 xkb_mod_mask_t wanted = 0;
1222 va_start(ap, match);
1224 xkb_mod_index_t idx;
1225 const char *str = va_arg(ap, const char *);
1228 idx = xkb_keymap_mod_get_index(state->keymap, str);
1229 if (idx == XKB_MOD_INVALID) {
1233 wanted |= (1u << idx);
1240 return match_mod_masks(state, type, match, wanted);
1244 * Returns 1 if the given group is active with the specified type(s), 0 if
1245 * not, or -1 if the group is invalid.
1248 xkb_state_layout_index_is_active(struct xkb_state *state,
1249 xkb_layout_index_t idx,
1250 enum xkb_state_component type)
1254 if (idx >= state->keymap->num_groups)
1257 if (type & XKB_STATE_LAYOUT_EFFECTIVE)
1258 ret |= (state->components.group == idx);
1259 if (type & XKB_STATE_LAYOUT_DEPRESSED)
1260 ret |= (state->components.base_group == (int32_t) idx);
1261 if (type & XKB_STATE_LAYOUT_LATCHED)
1262 ret |= (state->components.latched_group == (int32_t) idx);
1263 if (type & XKB_STATE_LAYOUT_LOCKED)
1264 ret |= (state->components.locked_group == (int32_t) idx);
1270 * Returns 1 if the given modifier is active with the specified type(s), 0 if
1271 * not, or -1 if the modifier is invalid.
1274 xkb_state_layout_name_is_active(struct xkb_state *state, const char *name,
1275 enum xkb_state_component type)
1277 xkb_layout_index_t idx = xkb_keymap_layout_get_index(state->keymap, name);
1279 if (idx == XKB_LAYOUT_INVALID)
1282 return xkb_state_layout_index_is_active(state, idx, type);
1286 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
1289 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
1291 if (idx >= state->keymap->num_leds ||
1292 state->keymap->leds[idx].name == XKB_ATOM_NONE)
1295 return !!(state->components.leds & (1u << idx));
1299 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
1302 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
1304 xkb_led_index_t idx = xkb_keymap_led_get_index(state->keymap, name);
1306 if (idx == XKB_LED_INVALID)
1309 return xkb_state_led_index_is_active(state, idx);
1312 static xkb_mod_mask_t
1313 key_get_consumed(struct xkb_state *state, const struct xkb_key *key)
1315 const struct xkb_key_type *type;
1316 const struct xkb_key_type_entry *entry;
1317 xkb_mod_mask_t preserve;
1318 xkb_layout_index_t group;
1320 group = xkb_state_key_get_layout(state, key->keycode);
1321 if (group == XKB_LAYOUT_INVALID)
1324 type = key->groups[group].type;
1326 entry = get_entry_for_key_state(state, key, group);
1328 preserve = entry->preserve.mask;
1332 return type->mods.mask & ~preserve;
1336 * Tests to see if a modifier is used up by our translation of a
1337 * keycode to keysyms, taking note of the current modifier state and
1338 * the appropriate key type's preserve information, if any. This allows
1339 * the user to mask out the modifier in later processing of the
1340 * modifiers, e.g. when implementing hot keys or accelerators.
1342 * See also, for example:
1343 * - XkbTranslateKeyCode(3), mod_rtrn return value, from libX11.
1344 * - gdk_keymap_translate_keyboard_state, consumed_modifiers return value,
1348 xkb_state_mod_index_is_consumed(struct xkb_state *state, xkb_keycode_t kc,
1349 xkb_mod_index_t idx)
1351 const struct xkb_key *key = XkbKey(state->keymap, kc);
1353 if (!key || idx >= xkb_keymap_num_mods(state->keymap))
1356 return !!((1u << idx) & key_get_consumed(state, key));
1360 * Calculates which modifiers should be consumed during key processing,
1361 * and returns the mask with all these modifiers removed. e.g. if
1362 * given a state of Alt and Shift active for a two-level alphabetic
1363 * key containing plus and equal on the first and second level
1364 * respectively, will return a mask of only Alt, as Shift has been
1365 * consumed by the type handling.
1367 XKB_EXPORT xkb_mod_mask_t
1368 xkb_state_mod_mask_remove_consumed(struct xkb_state *state, xkb_keycode_t kc,
1369 xkb_mod_mask_t mask)
1371 const struct xkb_key *key = XkbKey(state->keymap, kc);
1376 return mask & ~key_get_consumed(state, key);
1379 XKB_EXPORT xkb_mod_mask_t
1380 xkb_state_key_get_consumed_mods(struct xkb_state *state, xkb_keycode_t kc)
1382 const struct xkb_key *key = XkbKey(state->keymap, kc);
1387 return key_get_consumed(state, key);