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)
69 union xkb_action action;
70 const struct xkb_key *key;
72 bool (*func)(struct xkb_state *state,
73 struct xkb_filter *filter,
74 const struct xkb_key *key,
75 enum xkb_key_direction direction);
79 struct state_components {
80 /* These may be negative, because of -1 group actions. */
81 int32_t base_group; /**< depressed */
82 int32_t latched_group;
84 xkb_layout_index_t group; /**< effective */
86 xkb_mod_mask_t base_mods; /**< depressed */
87 xkb_mod_mask_t latched_mods;
88 xkb_mod_mask_t locked_mods;
89 xkb_mod_mask_t mods; /**< effective */
96 * Before updating the state, we keep a copy of just this struct. This
97 * allows us to report which components of the state have changed.
99 struct state_components components;
102 * At each event, we accumulate all the needed modifications to the base
103 * modifiers, and apply them at the end. These keep track of this state.
105 xkb_mod_mask_t set_mods;
106 xkb_mod_mask_t clear_mods;
109 * We mustn't clear a base modifier if there's another depressed key
110 * which affects it, e.g. given this sequence
111 * < Left Shift down, Right Shift down, Left Shift Up >
112 * the modifier should still be set. This keeps the count.
114 int16_t mod_key_count[XKB_MAX_MODS];
117 darray(struct xkb_filter) filters;
118 struct xkb_keymap *keymap;
122 * If the virtual modifiers are not bound to anything, the entry
123 * is not active and should be skipped. xserver does this with
124 * cached entry->active field.
127 entry_is_active(const struct xkb_key_type_entry *entry)
129 return entry->mods.mods == 0 || entry->mods.mask != 0;
132 static const struct xkb_key_type_entry *
133 get_entry_for_mods(const struct xkb_key_type *type, xkb_mod_mask_t mods)
135 for (unsigned i = 0; i < type->num_entries; i++)
136 if (entry_is_active(&type->entries[i]) &&
137 type->entries[i].mods.mask == mods)
138 return &type->entries[i];
142 static const struct xkb_key_type_entry *
143 get_entry_for_key_state(struct xkb_state *state, const struct xkb_key *key,
144 xkb_layout_index_t group)
146 const struct xkb_key_type *type = key->groups[group].type;
147 xkb_mod_mask_t active_mods = state->components.mods & type->mods.mask;
148 return get_entry_for_mods(type, active_mods);
152 * Returns the level to use for the given key and state, or
155 XKB_EXPORT xkb_level_index_t
156 xkb_state_key_get_level(struct xkb_state *state, xkb_keycode_t kc,
157 xkb_layout_index_t layout)
159 const struct xkb_key *key = XkbKey(state->keymap, kc);
160 const struct xkb_key_type_entry *entry;
162 if (!key || layout >= key->num_groups)
163 return XKB_LEVEL_INVALID;
165 /* If we don't find an explicit match the default is 0. */
166 entry = get_entry_for_key_state(state, key, layout);
174 XkbWrapGroupIntoRange(int32_t group,
175 xkb_layout_index_t num_groups,
176 enum xkb_range_exceed_type out_of_range_group_action,
177 xkb_layout_index_t out_of_range_group_number)
180 return XKB_LAYOUT_INVALID;
182 if (group >= 0 && (xkb_layout_index_t) group < num_groups)
185 switch (out_of_range_group_action) {
187 if (out_of_range_group_number >= num_groups)
189 return out_of_range_group_number;
195 return num_groups - 1;
200 * C99 says a negative dividend in a modulo operation always
201 * gives a negative result.
204 return ((int) num_groups + (group % (int) num_groups));
206 return group % num_groups;
211 * Returns the layout to use for the given key and state, taking
212 * wrapping/clamping/etc into account, or XKB_LAYOUT_INVALID.
214 XKB_EXPORT xkb_layout_index_t
215 xkb_state_key_get_layout(struct xkb_state *state, xkb_keycode_t kc)
217 const struct xkb_key *key = XkbKey(state->keymap, kc);
220 return XKB_LAYOUT_INVALID;
222 return XkbWrapGroupIntoRange(state->components.group, key->num_groups,
223 key->out_of_range_group_action,
224 key->out_of_range_group_number);
227 static const union xkb_action *
228 xkb_key_get_action(struct xkb_state *state, const struct xkb_key *key)
230 static const union xkb_action dummy = { .type = ACTION_TYPE_NONE };
232 xkb_layout_index_t layout;
233 xkb_level_index_t level;
235 layout = xkb_state_key_get_layout(state, key->keycode);
236 if (layout == XKB_LAYOUT_INVALID)
239 level = xkb_state_key_get_level(state, key->keycode, layout);
240 if (level == XKB_LEVEL_INVALID)
243 return &key->groups[layout].levels[level].action;
246 static struct xkb_filter *
247 xkb_filter_new(struct xkb_state *state)
249 struct xkb_filter *filter = NULL, *iter;
251 darray_foreach(iter, state->filters) {
259 darray_resize0(state->filters, darray_size(state->filters) + 1);
260 filter = &darray_item(state->filters, darray_size(state->filters) -1);
267 /***====================================================================***/
269 enum xkb_filter_result {
271 * The event is consumed by the filters.
273 * An event is always processed by all filters, but any filter can
274 * prevent it from being processed further by consuming it.
278 * The event may continue to be processed as far as this filter is
285 xkb_filter_group_set_new(struct xkb_state *state, struct xkb_filter *filter)
287 filter->priv = state->components.base_group;
288 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
289 state->components.base_group = filter->action.group.group;
291 state->components.base_group += filter->action.group.group;
295 xkb_filter_group_set_func(struct xkb_state *state,
296 struct xkb_filter *filter,
297 const struct xkb_key *key,
298 enum xkb_key_direction direction)
300 if (key != filter->key) {
301 filter->action.group.flags &= ~ACTION_LOCK_CLEAR;
302 return XKB_FILTER_CONTINUE;
305 if (direction == XKB_KEY_DOWN) {
307 return XKB_FILTER_CONSUME;
309 else if (--filter->refcnt > 0) {
310 return XKB_FILTER_CONSUME;
313 state->components.base_group = filter->priv;
315 if (filter->action.group.flags & ACTION_LOCK_CLEAR)
316 state->components.locked_group = 0;
319 return XKB_FILTER_CONTINUE;
323 xkb_filter_group_lock_new(struct xkb_state *state, struct xkb_filter *filter)
325 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
326 state->components.locked_group = filter->action.group.group;
328 state->components.locked_group += filter->action.group.group;
332 xkb_filter_group_lock_func(struct xkb_state *state,
333 struct xkb_filter *filter,
334 const struct xkb_key *key,
335 enum xkb_key_direction direction)
337 if (key != filter->key)
338 return XKB_FILTER_CONTINUE;
340 if (direction == XKB_KEY_DOWN) {
342 return XKB_FILTER_CONSUME;
344 if (--filter->refcnt > 0)
345 return XKB_FILTER_CONSUME;
348 return XKB_FILTER_CONTINUE;
352 xkb_filter_mod_set_new(struct xkb_state *state, struct xkb_filter *filter)
354 state->set_mods = filter->action.mods.mods.mask;
358 xkb_filter_mod_set_func(struct xkb_state *state,
359 struct xkb_filter *filter,
360 const struct xkb_key *key,
361 enum xkb_key_direction direction)
363 if (key != filter->key) {
364 filter->action.mods.flags &= ~ACTION_LOCK_CLEAR;
365 return XKB_FILTER_CONTINUE;
368 if (direction == XKB_KEY_DOWN) {
370 return XKB_FILTER_CONSUME;
372 else if (--filter->refcnt > 0) {
373 return XKB_FILTER_CONSUME;
376 state->clear_mods = filter->action.mods.mods.mask;
377 if (filter->action.mods.flags & ACTION_LOCK_CLEAR)
378 state->components.locked_mods &= ~filter->action.mods.mods.mask;
381 return XKB_FILTER_CONTINUE;
385 xkb_filter_mod_lock_new(struct xkb_state *state, struct xkb_filter *filter)
387 filter->priv = (state->components.locked_mods &
388 filter->action.mods.mods.mask);
389 state->set_mods |= filter->action.mods.mods.mask;
390 if (!(filter->action.mods.flags & ACTION_LOCK_NO_LOCK))
391 state->components.locked_mods |= filter->action.mods.mods.mask;
395 xkb_filter_mod_lock_func(struct xkb_state *state,
396 struct xkb_filter *filter,
397 const struct xkb_key *key,
398 enum xkb_key_direction direction)
400 if (key != filter->key)
401 return XKB_FILTER_CONTINUE;
403 if (direction == XKB_KEY_DOWN) {
405 return XKB_FILTER_CONSUME;
407 if (--filter->refcnt > 0)
408 return XKB_FILTER_CONSUME;
410 state->clear_mods |= filter->action.mods.mods.mask;
411 if (!(filter->action.mods.flags & ACTION_LOCK_NO_UNLOCK))
412 state->components.locked_mods &= ~filter->priv;
415 return XKB_FILTER_CONTINUE;
418 enum xkb_key_latch_state {
425 xkb_action_breaks_latch(const union xkb_action *action)
427 switch (action->type) {
428 case ACTION_TYPE_NONE:
429 case ACTION_TYPE_PTR_BUTTON:
430 case ACTION_TYPE_PTR_LOCK:
431 case ACTION_TYPE_CTRL_SET:
432 case ACTION_TYPE_CTRL_LOCK:
433 case ACTION_TYPE_SWITCH_VT:
434 case ACTION_TYPE_TERMINATE:
442 xkb_filter_mod_latch_new(struct xkb_state *state, struct xkb_filter *filter)
444 filter->priv = LATCH_KEY_DOWN;
445 state->set_mods = filter->action.mods.mods.mask;
449 xkb_filter_mod_latch_func(struct xkb_state *state,
450 struct xkb_filter *filter,
451 const struct xkb_key *key,
452 enum xkb_key_direction direction)
454 enum xkb_key_latch_state latch = filter->priv;
456 if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
457 /* If this is a new keypress and we're awaiting our single latched
458 * keypress, then either break the latch if any random key is pressed,
459 * or promote it to a lock or plain base set if it's the same
461 const union xkb_action *action = xkb_key_get_action(state, key);
462 if (action->type == ACTION_TYPE_MOD_LATCH &&
463 action->mods.flags == filter->action.mods.flags &&
464 action->mods.mods.mask == filter->action.mods.mods.mask) {
465 filter->action = *action;
466 if (filter->action.mods.flags & ACTION_LATCH_TO_LOCK) {
467 filter->action.type = ACTION_TYPE_MOD_LOCK;
468 filter->func = xkb_filter_mod_lock_func;
469 state->components.locked_mods |= filter->action.mods.mods.mask;
472 filter->action.type = ACTION_TYPE_MOD_SET;
473 filter->func = xkb_filter_mod_set_func;
474 state->set_mods = filter->action.mods.mods.mask;
477 state->components.latched_mods &= ~filter->action.mods.mods.mask;
479 return XKB_FILTER_CONSUME;
481 else if (xkb_action_breaks_latch(action)) {
482 /* XXX: This may be totally broken, we might need to break the
483 * latch in the next run after this press? */
484 state->components.latched_mods &= ~filter->action.mods.mods.mask;
486 return XKB_FILTER_CONTINUE;
489 else if (direction == XKB_KEY_UP && key == filter->key) {
490 /* Our key got released. If we've set it to clear locks, and we
491 * currently have the same modifiers locked, then release them and
492 * don't actually latch. Else we've actually hit the latching
493 * stage, so set PENDING and move our modifier from base to
495 if (latch == NO_LATCH ||
496 ((filter->action.mods.flags & ACTION_LOCK_CLEAR) &&
497 (state->components.locked_mods & filter->action.mods.mods.mask) ==
498 filter->action.mods.mods.mask)) {
499 /* XXX: We might be a bit overenthusiastic about clearing
500 * mods other filters have set here? */
501 if (latch == LATCH_PENDING)
502 state->components.latched_mods &=
503 ~filter->action.mods.mods.mask;
505 state->clear_mods = filter->action.mods.mods.mask;
506 state->components.locked_mods &= ~filter->action.mods.mods.mask;
510 latch = LATCH_PENDING;
511 state->clear_mods = filter->action.mods.mods.mask;
512 state->components.latched_mods |= filter->action.mods.mods.mask;
516 else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
517 /* Someone's pressed another key while we've still got the latching
518 * key held down, so keep the base modifier state active (from
519 * xkb_filter_mod_latch_new), but don't trip the latch, just clear
520 * it as soon as the modifier gets released. */
524 filter->priv = latch;
526 return XKB_FILTER_CONTINUE;
529 static const struct {
530 void (*new)(struct xkb_state *state, struct xkb_filter *filter);
531 bool (*func)(struct xkb_state *state, struct xkb_filter *filter,
532 const struct xkb_key *key, enum xkb_key_direction direction);
533 } filter_action_funcs[_ACTION_TYPE_NUM_ENTRIES] = {
534 [ACTION_TYPE_MOD_SET] = { xkb_filter_mod_set_new,
535 xkb_filter_mod_set_func },
536 [ACTION_TYPE_MOD_LATCH] = { xkb_filter_mod_latch_new,
537 xkb_filter_mod_latch_func },
538 [ACTION_TYPE_MOD_LOCK] = { xkb_filter_mod_lock_new,
539 xkb_filter_mod_lock_func },
540 [ACTION_TYPE_GROUP_SET] = { xkb_filter_group_set_new,
541 xkb_filter_group_set_func },
542 [ACTION_TYPE_GROUP_LOCK] = { xkb_filter_group_lock_new,
543 xkb_filter_group_lock_func },
547 * Applies any relevant filters to the key, first from the list of filters
548 * that are currently active, then if no filter has claimed the key, possibly
549 * apply a new filter from the key action.
552 xkb_filter_apply_all(struct xkb_state *state,
553 const struct xkb_key *key,
554 enum xkb_key_direction direction)
556 struct xkb_filter *filter;
557 const union xkb_action *action;
560 /* First run through all the currently active filters and see if any of
561 * them have consumed this event. */
563 darray_foreach(filter, state->filters) {
567 if (filter->func(state, filter, key, direction) == XKB_FILTER_CONSUME)
570 if (consumed || direction == XKB_KEY_UP)
573 action = xkb_key_get_action(state, key);
576 * It's possible for the keymap to set action->type explicitly, like so:
577 * interpret XF86_Next_VMode {
578 * action = Private(type=0x86, data="+VMode");
580 * We don't handle those.
582 if (action->type >= _ACTION_TYPE_NUM_ENTRIES)
585 if (!filter_action_funcs[action->type].new)
588 filter = xkb_filter_new(state);
590 filter->func = filter_action_funcs[action->type].func;
591 filter->action = *action;
592 filter_action_funcs[action->type].new(state, filter);
595 XKB_EXPORT struct xkb_state *
596 xkb_state_new(struct xkb_keymap *keymap)
598 struct xkb_state *ret;
600 ret = calloc(sizeof(*ret), 1);
605 ret->keymap = xkb_keymap_ref(keymap);
610 XKB_EXPORT struct xkb_state *
611 xkb_state_ref(struct xkb_state *state)
618 xkb_state_unref(struct xkb_state *state)
620 if (!state || --state->refcnt > 0)
623 xkb_keymap_unref(state->keymap);
624 darray_free(state->filters);
628 XKB_EXPORT struct xkb_keymap *
629 xkb_state_get_keymap(struct xkb_state *state)
631 return state->keymap;
635 * Update the LED state to match the rest of the xkb_state.
638 xkb_state_led_update_all(struct xkb_state *state)
641 const struct xkb_led *led;
643 state->components.leds = 0;
645 xkb_leds_enumerate(idx, led, state->keymap) {
646 xkb_mod_mask_t mod_mask = 0;
647 xkb_layout_mask_t group_mask = 0;
649 if (led->which_mods != 0 && led->mods.mask != 0) {
650 if (led->which_mods & XKB_STATE_MODS_EFFECTIVE)
651 mod_mask |= state->components.mods;
652 if (led->which_mods & XKB_STATE_MODS_DEPRESSED)
653 mod_mask |= state->components.base_mods;
654 if (led->which_mods & XKB_STATE_MODS_LATCHED)
655 mod_mask |= state->components.latched_mods;
656 if (led->which_mods & XKB_STATE_MODS_LOCKED)
657 mod_mask |= state->components.locked_mods;
659 if (led->mods.mask & mod_mask) {
660 state->components.leds |= (1u << idx);
665 if (led->which_groups != 0 && led->groups != 0) {
666 if (led->which_groups & XKB_STATE_LAYOUT_EFFECTIVE)
667 group_mask |= (1u << state->components.group);
668 if (led->which_groups & XKB_STATE_LAYOUT_DEPRESSED)
669 group_mask |= (1u << state->components.base_group);
670 if (led->which_groups & XKB_STATE_LAYOUT_LATCHED)
671 group_mask |= (1u << state->components.latched_group);
672 if (led->which_groups & XKB_STATE_LAYOUT_LOCKED)
673 group_mask |= (1u << state->components.locked_group);
675 if (led->groups & group_mask) {
676 state->components.leds |= (1u << idx);
681 if (led->ctrls & state->keymap->enabled_ctrls) {
682 state->components.leds |= (1u << idx);
689 * Calculates the derived state (effective mods/group and LEDs) from an
690 * up-to-date xkb_state.
693 xkb_state_update_derived(struct xkb_state *state)
695 xkb_layout_index_t wrapped;
697 state->components.mods = (state->components.base_mods |
698 state->components.latched_mods |
699 state->components.locked_mods);
701 /* TODO: Use groups_wrap control instead of always RANGE_WRAP. */
703 wrapped = XkbWrapGroupIntoRange(state->components.locked_group,
704 state->keymap->num_groups,
706 state->components.locked_group =
707 (wrapped == XKB_LAYOUT_INVALID ? 0 : wrapped);
709 wrapped = XkbWrapGroupIntoRange(state->components.base_group +
710 state->components.latched_group +
711 state->components.locked_group,
712 state->keymap->num_groups,
714 state->components.group =
715 (wrapped == XKB_LAYOUT_INVALID ? 0 : wrapped);
717 xkb_state_led_update_all(state);
720 static enum xkb_state_component
721 get_state_component_changes(const struct state_components *a,
722 const struct state_components *b)
724 xkb_mod_mask_t mask = 0;
726 if (a->group != b->group)
727 mask |= XKB_STATE_LAYOUT_EFFECTIVE;
728 if (a->base_group != b->base_group)
729 mask |= XKB_STATE_LAYOUT_DEPRESSED;
730 if (a->latched_group != b->latched_group)
731 mask |= XKB_STATE_LAYOUT_LATCHED;
732 if (a->locked_group != b->locked_group)
733 mask |= XKB_STATE_LAYOUT_LOCKED;
734 if (a->mods != b->mods)
735 mask |= XKB_STATE_MODS_EFFECTIVE;
736 if (a->base_mods != b->base_mods)
737 mask |= XKB_STATE_MODS_DEPRESSED;
738 if (a->latched_mods != b->latched_mods)
739 mask |= XKB_STATE_MODS_LATCHED;
740 if (a->locked_mods != b->locked_mods)
741 mask |= XKB_STATE_MODS_LOCKED;
742 if (a->leds != b->leds)
743 mask |= XKB_STATE_LEDS;
749 * Given a particular key event, updates the state structure to reflect the
752 XKB_EXPORT enum xkb_state_component
753 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
754 enum xkb_key_direction direction)
758 struct state_components prev_components;
759 const struct xkb_key *key = XkbKey(state->keymap, kc);
764 prev_components = state->components;
767 state->clear_mods = 0;
769 xkb_filter_apply_all(state, key, direction);
771 for (i = 0, bit = 1; state->set_mods; i++, bit <<= 1) {
772 if (state->set_mods & bit) {
773 state->mod_key_count[i]++;
774 state->components.base_mods |= bit;
775 state->set_mods &= ~bit;
779 for (i = 0, bit = 1; state->clear_mods; i++, bit <<= 1) {
780 if (state->clear_mods & bit) {
781 state->mod_key_count[i]--;
782 if (state->mod_key_count[i] <= 0) {
783 state->components.base_mods &= ~bit;
784 state->mod_key_count[i] = 0;
786 state->clear_mods &= ~bit;
790 xkb_state_update_derived(state);
792 return get_state_component_changes(&prev_components, &state->components);
796 * Updates the state from a set of explicit masks as gained from
797 * xkb_state_serialize_mods and xkb_state_serialize_groups. As noted in the
798 * documentation for these functions in xkbcommon.h, this round-trip is
799 * lossy, and should only be used to update a slave state mirroring the
800 * master, e.g. in a client/server window system.
802 XKB_EXPORT enum xkb_state_component
803 xkb_state_update_mask(struct xkb_state *state,
804 xkb_mod_mask_t base_mods,
805 xkb_mod_mask_t latched_mods,
806 xkb_mod_mask_t locked_mods,
807 xkb_layout_index_t base_group,
808 xkb_layout_index_t latched_group,
809 xkb_layout_index_t locked_group)
811 struct state_components prev_components;
814 prev_components = state->components;
816 /* Only include modifiers which exist in the keymap. */
817 mask = (xkb_mod_mask_t) ((1ull << xkb_keymap_num_mods(state->keymap)) - 1u);
819 state->components.base_mods = base_mods & mask;
820 state->components.latched_mods = latched_mods & mask;
821 state->components.locked_mods = locked_mods & mask;
823 /* Make sure the mods are fully resolved - since we get arbitrary
824 * input, they might not be.
826 * It might seem more reasonable to do this only for components.mods
827 * in xkb_state_update_derived(), rather than for each component
828 * seperately. That would allow to distinguish between "really"
829 * depressed mods (would be in MODS_DEPRESSED) and indirectly
830 * depressed to to a mapping (would only be in MODS_EFFECTIVE).
831 * However, the traditional behavior of xkb_state_update_key() is that
832 * if a vmod is depressed, its mappings are depressed with it; so we're
833 * expected to do the same here. Also, LEDs (usually) look if a real
834 * mod is locked, not just effective; otherwise it won't be lit.
836 * We OR here because mod_mask_get_effective() drops vmods. */
837 state->components.base_mods |=
838 mod_mask_get_effective(state->keymap, state->components.base_mods);
839 state->components.latched_mods |=
840 mod_mask_get_effective(state->keymap, state->components.latched_mods);
841 state->components.locked_mods |=
842 mod_mask_get_effective(state->keymap, state->components.locked_mods);
844 state->components.base_group = base_group;
845 state->components.latched_group = latched_group;
846 state->components.locked_group = locked_group;
848 xkb_state_update_derived(state);
850 return get_state_component_changes(&prev_components, &state->components);
854 * Provides the symbols to use for the given key and state. Returns the
855 * number of symbols pointed to in syms_out.
858 xkb_state_key_get_syms(struct xkb_state *state, xkb_keycode_t kc,
859 const xkb_keysym_t **syms_out)
861 xkb_layout_index_t layout;
862 xkb_level_index_t level;
864 layout = xkb_state_key_get_layout(state, kc);
865 if (layout == XKB_LAYOUT_INVALID)
868 level = xkb_state_key_get_level(state, kc, layout);
869 if (level == XKB_LEVEL_INVALID)
872 return xkb_keymap_key_get_syms_by_level(state->keymap, kc, layout, level,
881 * https://www.x.org/releases/current/doc/kbproto/xkbproto.html#Interpreting_the_Lock_Modifier
884 should_do_caps_transformation(struct xkb_state *state, xkb_keycode_t kc)
886 xkb_mod_index_t caps =
887 xkb_keymap_mod_get_index(state->keymap, XKB_MOD_NAME_CAPS);
890 xkb_state_mod_index_is_active(state, caps, XKB_STATE_MODS_EFFECTIVE) > 0 &&
891 xkb_state_mod_index_is_consumed(state, kc, caps) == 0;
895 * https://www.x.org/releases/current/doc/kbproto/xkbproto.html#Interpreting_the_Control_Modifier
898 should_do_ctrl_transformation(struct xkb_state *state, xkb_keycode_t kc)
900 xkb_mod_index_t ctrl =
901 xkb_keymap_mod_get_index(state->keymap, XKB_MOD_NAME_CTRL);
904 xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_MODS_EFFECTIVE) > 0 &&
905 xkb_state_mod_index_is_consumed(state, kc, ctrl) == 0;
908 /* Verbatim from libX11:src/xkb/XKBBind.c */
910 XkbToControl(char ch)
914 if ((c >= '@' && c < '\177') || c == ' ')
918 else if (c >= '3' && c <= '7')
928 * Provides either exactly one symbol, or XKB_KEY_NoSymbol.
930 XKB_EXPORT xkb_keysym_t
931 xkb_state_key_get_one_sym(struct xkb_state *state, xkb_keycode_t kc)
933 const xkb_keysym_t *syms;
937 num_syms = xkb_state_key_get_syms(state, kc, &syms);
939 return XKB_KEY_NoSymbol;
943 if (should_do_caps_transformation(state, kc))
944 sym = xkb_keysym_to_upper(sym);
950 * The caps and ctrl transformations require some special handling,
951 * so we cannot simply use xkb_state_get_one_sym() for them.
952 * In particular, if Control is set, we must try very hard to find
953 * some layout in which the keysym is ASCII and thus can be (maybe)
954 * converted to a control character. libX11 allows to disable this
955 * behavior with the XkbLC_ControlFallback (see XkbSetXlibControls(3)),
956 * but it is enabled by default, yippee.
959 get_one_sym_for_string(struct xkb_state *state, xkb_keycode_t kc)
961 xkb_level_index_t level;
962 xkb_layout_index_t layout, num_layouts;
963 const xkb_keysym_t *syms;
967 layout = xkb_state_key_get_layout(state, kc);
968 num_layouts = xkb_keymap_num_layouts_for_key(state->keymap, kc);
969 level = xkb_state_key_get_level(state, kc, layout);
970 if (layout == XKB_LAYOUT_INVALID || num_layouts == 0 ||
971 level == XKB_LEVEL_INVALID)
972 return XKB_KEY_NoSymbol;
974 nsyms = xkb_keymap_key_get_syms_by_level(state->keymap, kc,
975 layout, level, &syms);
977 return XKB_KEY_NoSymbol;
980 if (should_do_ctrl_transformation(state, kc) && sym > 127u) {
981 for (xkb_layout_index_t i = 0; i < num_layouts; i++) {
982 level = xkb_state_key_get_level(state, kc, i);
983 if (level == XKB_LEVEL_INVALID)
986 nsyms = xkb_keymap_key_get_syms_by_level(state->keymap, kc,
988 if (nsyms == 1 && syms[0] <= 127u) {
995 if (should_do_caps_transformation(state, kc)) {
996 sym = xkb_keysym_to_upper(sym);
1003 xkb_state_key_get_utf8(struct xkb_state *state, xkb_keycode_t kc,
1004 char *buffer, size_t size)
1007 const xkb_keysym_t *syms;
1012 sym = get_one_sym_for_string(state, kc);
1013 if (sym != XKB_KEY_NoSymbol) {
1014 nsyms = 1; syms = &sym;
1017 nsyms = xkb_state_key_get_syms(state, kc, &syms);
1020 /* Make sure not to truncate in the middle of a UTF-8 sequence. */
1022 for (int i = 0; i < nsyms; i++) {
1023 int ret = xkb_keysym_to_utf8(syms[i], tmp, sizeof(tmp));
1028 if ((size_t) (offset + ret) <= size)
1029 memcpy(buffer + offset, tmp, ret);
1033 if ((size_t) offset >= size)
1035 buffer[offset] = '\0';
1037 if (!is_valid_utf8(buffer, offset))
1040 if (offset == 1 && (unsigned int) buffer[0] <= 127u &&
1041 should_do_ctrl_transformation(state, kc))
1042 buffer[0] = XkbToControl(buffer[0]);
1048 buffer[size - 1] = '\0';
1058 xkb_state_key_get_utf32(struct xkb_state *state, xkb_keycode_t kc)
1063 sym = get_one_sym_for_string(state, kc);
1064 cp = xkb_keysym_to_utf32(sym);
1066 if (cp <= 127u && should_do_ctrl_transformation(state, kc))
1067 cp = (uint32_t) XkbToControl((char) cp);
1073 * Serialises the requested modifier state into an xkb_mod_mask_t, with all
1074 * the same disclaimers as in xkb_state_update_mask.
1076 XKB_EXPORT xkb_mod_mask_t
1077 xkb_state_serialize_mods(struct xkb_state *state,
1078 enum xkb_state_component type)
1080 xkb_mod_mask_t ret = 0;
1082 if (type & XKB_STATE_MODS_EFFECTIVE)
1083 return state->components.mods;
1085 if (type & XKB_STATE_MODS_DEPRESSED)
1086 ret |= state->components.base_mods;
1087 if (type & XKB_STATE_MODS_LATCHED)
1088 ret |= state->components.latched_mods;
1089 if (type & XKB_STATE_MODS_LOCKED)
1090 ret |= state->components.locked_mods;
1096 * Serialises the requested group state, with all the same disclaimers as
1097 * in xkb_state_update_mask.
1099 XKB_EXPORT xkb_layout_index_t
1100 xkb_state_serialize_layout(struct xkb_state *state,
1101 enum xkb_state_component type)
1103 xkb_layout_index_t ret = 0;
1105 if (type & XKB_STATE_LAYOUT_EFFECTIVE)
1106 return state->components.group;
1108 if (type & XKB_STATE_LAYOUT_DEPRESSED)
1109 ret += state->components.base_group;
1110 if (type & XKB_STATE_LAYOUT_LATCHED)
1111 ret += state->components.latched_group;
1112 if (type & XKB_STATE_LAYOUT_LOCKED)
1113 ret += state->components.locked_group;
1119 * Gets a modifier mask and returns the resolved effective mask; this
1120 * is needed because some modifiers can also map to other modifiers, e.g.
1121 * the "NumLock" modifier usually also sets the "Mod2" modifier.
1124 mod_mask_get_effective(struct xkb_keymap *keymap, xkb_mod_mask_t mods)
1126 const struct xkb_mod *mod;
1128 xkb_mod_mask_t mask;
1130 /* The effective mask is only real mods for now. */
1131 mask = mods & MOD_REAL_MASK_ALL;
1133 xkb_mods_enumerate(i, mod, &keymap->mods)
1134 if (mods & (1u << i))
1135 mask |= mod->mapping;
1141 * Returns 1 if the given modifier is active with the specified type(s), 0 if
1142 * not, or -1 if the modifier is invalid.
1145 xkb_state_mod_index_is_active(struct xkb_state *state,
1146 xkb_mod_index_t idx,
1147 enum xkb_state_component type)
1149 if (idx >= xkb_keymap_num_mods(state->keymap))
1152 return !!(xkb_state_serialize_mods(state, type) & (1u << idx));
1156 * Helper function for xkb_state_mod_indices_are_active and
1157 * xkb_state_mod_names_are_active.
1160 match_mod_masks(struct xkb_state *state,
1161 enum xkb_state_component type,
1162 enum xkb_state_match match,
1163 xkb_mod_mask_t wanted)
1165 xkb_mod_mask_t active = xkb_state_serialize_mods(state, type);
1167 if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
1170 if (match & XKB_STATE_MATCH_ANY)
1171 return active & wanted;
1173 return (active & wanted) == wanted;
1177 * Returns 1 if the modifiers are active with the specified type(s), 0 if
1178 * not, or -1 if any of the modifiers are invalid.
1181 xkb_state_mod_indices_are_active(struct xkb_state *state,
1182 enum xkb_state_component type,
1183 enum xkb_state_match match,
1187 xkb_mod_mask_t wanted = 0;
1189 xkb_mod_index_t num_mods = xkb_keymap_num_mods(state->keymap);
1191 va_start(ap, match);
1193 xkb_mod_index_t idx = va_arg(ap, xkb_mod_index_t);
1194 if (idx == XKB_MOD_INVALID)
1196 if (idx >= num_mods) {
1200 wanted |= (1u << idx);
1207 return match_mod_masks(state, type, match, wanted);
1211 * Returns 1 if the given modifier is active with the specified type(s), 0 if
1212 * not, or -1 if the modifier is invalid.
1215 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
1216 enum xkb_state_component type)
1218 xkb_mod_index_t idx = xkb_keymap_mod_get_index(state->keymap, name);
1220 if (idx == XKB_MOD_INVALID)
1223 return xkb_state_mod_index_is_active(state, idx, type);
1227 * Returns 1 if the modifiers are active with the specified type(s), 0 if
1228 * not, or -1 if any of the modifiers are invalid.
1230 XKB_EXPORT ATTR_NULL_SENTINEL int
1231 xkb_state_mod_names_are_active(struct xkb_state *state,
1232 enum xkb_state_component type,
1233 enum xkb_state_match match,
1237 xkb_mod_mask_t wanted = 0;
1240 va_start(ap, match);
1242 xkb_mod_index_t idx;
1243 const char *str = va_arg(ap, const char *);
1246 idx = xkb_keymap_mod_get_index(state->keymap, str);
1247 if (idx == XKB_MOD_INVALID) {
1251 wanted |= (1u << idx);
1258 return match_mod_masks(state, type, match, wanted);
1262 * Returns 1 if the given group is active with the specified type(s), 0 if
1263 * not, or -1 if the group is invalid.
1266 xkb_state_layout_index_is_active(struct xkb_state *state,
1267 xkb_layout_index_t idx,
1268 enum xkb_state_component type)
1272 if (idx >= state->keymap->num_groups)
1275 if (type & XKB_STATE_LAYOUT_EFFECTIVE)
1276 ret |= (state->components.group == idx);
1277 if (type & XKB_STATE_LAYOUT_DEPRESSED)
1278 ret |= (state->components.base_group == (int32_t) idx);
1279 if (type & XKB_STATE_LAYOUT_LATCHED)
1280 ret |= (state->components.latched_group == (int32_t) idx);
1281 if (type & XKB_STATE_LAYOUT_LOCKED)
1282 ret |= (state->components.locked_group == (int32_t) idx);
1288 * Returns 1 if the given modifier is active with the specified type(s), 0 if
1289 * not, or -1 if the modifier is invalid.
1292 xkb_state_layout_name_is_active(struct xkb_state *state, const char *name,
1293 enum xkb_state_component type)
1295 xkb_layout_index_t idx = xkb_keymap_layout_get_index(state->keymap, name);
1297 if (idx == XKB_LAYOUT_INVALID)
1300 return xkb_state_layout_index_is_active(state, idx, type);
1304 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
1307 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
1309 if (idx >= state->keymap->num_leds ||
1310 state->keymap->leds[idx].name == XKB_ATOM_NONE)
1313 return !!(state->components.leds & (1u << idx));
1317 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
1320 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
1322 xkb_led_index_t idx = xkb_keymap_led_get_index(state->keymap, name);
1324 if (idx == XKB_LED_INVALID)
1327 return xkb_state_led_index_is_active(state, idx);
1332 * - XkbTranslateKeyCode(3), mod_rtrn return value, from libX11.
1333 * - MyEnhancedXkbTranslateKeyCode(), a modification of the above, from GTK+.
1335 static xkb_mod_mask_t
1336 key_get_consumed(struct xkb_state *state, const struct xkb_key *key,
1337 enum xkb_consumed_mode mode)
1339 const struct xkb_key_type *type;
1340 const struct xkb_key_type_entry *matching_entry;
1341 xkb_mod_mask_t preserve = 0;
1342 xkb_layout_index_t group;
1343 xkb_mod_mask_t consumed = 0;
1345 group = xkb_state_key_get_layout(state, key->keycode);
1346 if (group == XKB_LAYOUT_INVALID)
1349 type = key->groups[group].type;
1351 matching_entry = get_entry_for_key_state(state, key, group);
1353 preserve = matching_entry->preserve.mask;
1356 case XKB_CONSUMED_MODE_XKB:
1357 consumed = type->mods.mask;
1360 case XKB_CONSUMED_MODE_GTK: {
1361 const struct xkb_key_type_entry *no_mods_entry;
1362 xkb_level_index_t no_mods_leveli;
1363 const struct xkb_level *no_mods_level, *level;
1365 no_mods_entry = get_entry_for_mods(type, 0);
1366 no_mods_leveli = no_mods_entry ? no_mods_entry->level : 0;
1367 no_mods_level = &key->groups[group].levels[no_mods_leveli];
1369 for (unsigned i = 0; i < type->num_entries; i++) {
1370 const struct xkb_key_type_entry *entry = &type->entries[i];
1371 if (!entry_is_active(entry))
1374 level = &key->groups[group].levels[entry->level];
1375 if (XkbLevelsSameSyms(level, no_mods_level))
1378 if (entry == matching_entry || one_bit_set(entry->mods.mask))
1379 consumed |= entry->mods.mask & ~entry->preserve.mask;
1385 return consumed & ~preserve;
1389 xkb_state_mod_index_is_consumed2(struct xkb_state *state, xkb_keycode_t kc,
1390 xkb_mod_index_t idx,
1391 enum xkb_consumed_mode mode)
1393 const struct xkb_key *key = XkbKey(state->keymap, kc);
1395 if (!key || idx >= xkb_keymap_num_mods(state->keymap))
1398 return !!((1u << idx) & key_get_consumed(state, key, mode));
1402 xkb_state_mod_index_is_consumed(struct xkb_state *state, xkb_keycode_t kc,
1403 xkb_mod_index_t idx)
1405 return xkb_state_mod_index_is_consumed2(state, kc, idx,
1406 XKB_CONSUMED_MODE_XKB);
1409 XKB_EXPORT xkb_mod_mask_t
1410 xkb_state_mod_mask_remove_consumed(struct xkb_state *state, xkb_keycode_t kc,
1411 xkb_mod_mask_t mask)
1413 const struct xkb_key *key = XkbKey(state->keymap, kc);
1418 return mask & ~key_get_consumed(state, key, XKB_CONSUMED_MODE_XKB);
1421 XKB_EXPORT xkb_mod_mask_t
1422 xkb_state_key_get_consumed_mods2(struct xkb_state *state, xkb_keycode_t kc,
1423 enum xkb_consumed_mode mode)
1425 const struct xkb_key *key;
1428 case XKB_CONSUMED_MODE_XKB:
1429 case XKB_CONSUMED_MODE_GTK:
1432 log_err_func(state->keymap->ctx,
1433 "unrecognized consumed modifiers mode: %d\n", mode);
1437 key = XkbKey(state->keymap, kc);
1441 return key_get_consumed(state, key, mode);
1444 XKB_EXPORT xkb_mod_mask_t
1445 xkb_state_key_get_consumed_mods(struct xkb_state *state, xkb_keycode_t kc)
1447 return xkb_state_key_get_consumed_mods2(state, kc, XKB_CONSUMED_MODE_XKB);