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;
121 static const struct xkb_key_type_entry *
122 get_entry_for_mods(const struct xkb_key_type *type, xkb_mod_mask_t mods)
124 for (unsigned i = 0; i < type->num_entries; i++)
125 if (entry_is_active(&type->entries[i]) &&
126 type->entries[i].mods.mask == mods)
127 return &type->entries[i];
131 static const struct xkb_key_type_entry *
132 get_entry_for_key_state(struct xkb_state *state, const struct xkb_key *key,
133 xkb_layout_index_t group)
135 const struct xkb_key_type *type = key->groups[group].type;
136 xkb_mod_mask_t active_mods = state->components.mods & type->mods.mask;
137 return get_entry_for_mods(type, active_mods);
141 * Returns the level to use for the given key and state, or
144 XKB_EXPORT xkb_level_index_t
145 xkb_state_key_get_level(struct xkb_state *state, xkb_keycode_t kc,
146 xkb_layout_index_t layout)
148 const struct xkb_key *key = XkbKey(state->keymap, kc);
149 const struct xkb_key_type_entry *entry;
151 if (!key || layout >= key->num_groups)
152 return XKB_LEVEL_INVALID;
154 /* If we don't find an explicit match the default is 0. */
155 entry = get_entry_for_key_state(state, key, layout);
163 XkbWrapGroupIntoRange(int32_t group,
164 xkb_layout_index_t num_groups,
165 enum xkb_range_exceed_type out_of_range_group_action,
166 xkb_layout_index_t out_of_range_group_number)
169 return XKB_LAYOUT_INVALID;
171 if (group >= 0 && (xkb_layout_index_t) group < num_groups)
174 switch (out_of_range_group_action) {
176 if (out_of_range_group_number >= num_groups)
178 return out_of_range_group_number;
184 return num_groups - 1;
189 * C99 says a negative dividend in a modulo operation always
190 * gives a negative result.
193 return ((int) num_groups + (group % (int) num_groups));
195 return group % num_groups;
200 * Returns the layout to use for the given key and state, taking
201 * wrapping/clamping/etc into account, or XKB_LAYOUT_INVALID.
203 XKB_EXPORT xkb_layout_index_t
204 xkb_state_key_get_layout(struct xkb_state *state, xkb_keycode_t kc)
206 const struct xkb_key *key = XkbKey(state->keymap, kc);
209 return XKB_LAYOUT_INVALID;
211 return XkbWrapGroupIntoRange(state->components.group, key->num_groups,
212 key->out_of_range_group_action,
213 key->out_of_range_group_number);
216 static const union xkb_action *
217 xkb_key_get_action(struct xkb_state *state, const struct xkb_key *key)
219 static const union xkb_action dummy = { .type = ACTION_TYPE_NONE };
221 xkb_layout_index_t layout;
222 xkb_level_index_t level;
224 layout = xkb_state_key_get_layout(state, key->keycode);
225 if (layout == XKB_LAYOUT_INVALID)
228 level = xkb_state_key_get_level(state, key->keycode, layout);
229 if (level == XKB_LEVEL_INVALID)
232 return &key->groups[layout].levels[level].action;
235 static struct xkb_filter *
236 xkb_filter_new(struct xkb_state *state)
238 struct xkb_filter *filter = NULL, *iter;
240 darray_foreach(iter, state->filters) {
248 darray_resize0(state->filters, darray_size(state->filters) + 1);
249 filter = &darray_item(state->filters, darray_size(state->filters) -1);
256 /***====================================================================***/
258 enum xkb_filter_result {
260 * The event is consumed by the filters.
262 * An event is always processed by all filters, but any filter can
263 * prevent it from being processed further by consuming it.
267 * The event may continue to be processed as far as this filter is
274 xkb_filter_group_set_new(struct xkb_state *state, struct xkb_filter *filter)
276 filter->priv = state->components.base_group;
277 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
278 state->components.base_group = filter->action.group.group;
280 state->components.base_group += filter->action.group.group;
284 xkb_filter_group_set_func(struct xkb_state *state,
285 struct xkb_filter *filter,
286 const struct xkb_key *key,
287 enum xkb_key_direction direction)
289 if (key != filter->key) {
290 filter->action.group.flags &= ~ACTION_LOCK_CLEAR;
291 return XKB_FILTER_CONTINUE;
294 if (direction == XKB_KEY_DOWN) {
296 return XKB_FILTER_CONSUME;
298 else if (--filter->refcnt > 0) {
299 return XKB_FILTER_CONSUME;
302 state->components.base_group = filter->priv;
304 if (filter->action.group.flags & ACTION_LOCK_CLEAR)
305 state->components.locked_group = 0;
308 return XKB_FILTER_CONTINUE;
312 xkb_filter_group_lock_new(struct xkb_state *state, struct xkb_filter *filter)
314 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
315 state->components.locked_group = filter->action.group.group;
317 state->components.locked_group += filter->action.group.group;
321 xkb_filter_group_lock_func(struct xkb_state *state,
322 struct xkb_filter *filter,
323 const struct xkb_key *key,
324 enum xkb_key_direction direction)
326 if (key != filter->key)
327 return XKB_FILTER_CONTINUE;
329 if (direction == XKB_KEY_DOWN) {
331 return XKB_FILTER_CONSUME;
333 if (--filter->refcnt > 0)
334 return XKB_FILTER_CONSUME;
337 return XKB_FILTER_CONTINUE;
341 xkb_filter_mod_set_new(struct xkb_state *state, struct xkb_filter *filter)
343 state->set_mods = filter->action.mods.mods.mask;
347 xkb_filter_mod_set_func(struct xkb_state *state,
348 struct xkb_filter *filter,
349 const struct xkb_key *key,
350 enum xkb_key_direction direction)
352 if (key != filter->key) {
353 filter->action.mods.flags &= ~ACTION_LOCK_CLEAR;
354 return XKB_FILTER_CONTINUE;
357 if (direction == XKB_KEY_DOWN) {
359 return XKB_FILTER_CONSUME;
361 else if (--filter->refcnt > 0) {
362 return XKB_FILTER_CONSUME;
365 state->clear_mods = filter->action.mods.mods.mask;
366 if (filter->action.mods.flags & ACTION_LOCK_CLEAR)
367 state->components.locked_mods &= ~filter->action.mods.mods.mask;
370 return XKB_FILTER_CONTINUE;
374 xkb_filter_mod_lock_new(struct xkb_state *state, struct xkb_filter *filter)
376 filter->priv = (state->components.locked_mods &
377 filter->action.mods.mods.mask);
378 state->set_mods |= filter->action.mods.mods.mask;
379 if (!(filter->action.mods.flags & ACTION_LOCK_NO_LOCK))
380 state->components.locked_mods |= filter->action.mods.mods.mask;
384 xkb_filter_mod_lock_func(struct xkb_state *state,
385 struct xkb_filter *filter,
386 const struct xkb_key *key,
387 enum xkb_key_direction direction)
389 if (key != filter->key)
390 return XKB_FILTER_CONTINUE;
392 if (direction == XKB_KEY_DOWN) {
394 return XKB_FILTER_CONSUME;
396 if (--filter->refcnt > 0)
397 return XKB_FILTER_CONSUME;
399 state->clear_mods |= filter->action.mods.mods.mask;
400 if (!(filter->action.mods.flags & ACTION_LOCK_NO_UNLOCK))
401 state->components.locked_mods &= ~filter->priv;
404 return XKB_FILTER_CONTINUE;
407 enum xkb_key_latch_state {
414 xkb_action_breaks_latch(const union xkb_action *action)
416 switch (action->type) {
417 case ACTION_TYPE_NONE:
418 case ACTION_TYPE_PTR_BUTTON:
419 case ACTION_TYPE_PTR_LOCK:
420 case ACTION_TYPE_CTRL_SET:
421 case ACTION_TYPE_CTRL_LOCK:
422 case ACTION_TYPE_SWITCH_VT:
423 case ACTION_TYPE_TERMINATE:
431 xkb_filter_mod_latch_new(struct xkb_state *state, struct xkb_filter *filter)
433 filter->priv = LATCH_KEY_DOWN;
434 state->set_mods = filter->action.mods.mods.mask;
438 xkb_filter_mod_latch_func(struct xkb_state *state,
439 struct xkb_filter *filter,
440 const struct xkb_key *key,
441 enum xkb_key_direction direction)
443 enum xkb_key_latch_state latch = filter->priv;
445 if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
446 /* If this is a new keypress and we're awaiting our single latched
447 * keypress, then either break the latch if any random key is pressed,
448 * or promote it to a lock or plain base set if it's the same
450 const union xkb_action *action = xkb_key_get_action(state, key);
451 if (action->type == ACTION_TYPE_MOD_LATCH &&
452 action->mods.flags == filter->action.mods.flags &&
453 action->mods.mods.mask == filter->action.mods.mods.mask) {
454 filter->action = *action;
455 if (filter->action.mods.flags & ACTION_LATCH_TO_LOCK) {
456 filter->action.type = ACTION_TYPE_MOD_LOCK;
457 filter->func = xkb_filter_mod_lock_func;
458 state->components.locked_mods |= filter->action.mods.mods.mask;
461 filter->action.type = ACTION_TYPE_MOD_SET;
462 filter->func = xkb_filter_mod_set_func;
463 state->set_mods = filter->action.mods.mods.mask;
466 state->components.latched_mods &= ~filter->action.mods.mods.mask;
468 return XKB_FILTER_CONSUME;
470 else if (xkb_action_breaks_latch(action)) {
471 /* XXX: This may be totally broken, we might need to break the
472 * latch in the next run after this press? */
473 state->components.latched_mods &= ~filter->action.mods.mods.mask;
475 return XKB_FILTER_CONTINUE;
478 else if (direction == XKB_KEY_UP && key == filter->key) {
479 /* Our key got released. If we've set it to clear locks, and we
480 * currently have the same modifiers locked, then release them and
481 * don't actually latch. Else we've actually hit the latching
482 * stage, so set PENDING and move our modifier from base to
484 if (latch == NO_LATCH ||
485 ((filter->action.mods.flags & ACTION_LOCK_CLEAR) &&
486 (state->components.locked_mods & filter->action.mods.mods.mask) ==
487 filter->action.mods.mods.mask)) {
488 /* XXX: We might be a bit overenthusiastic about clearing
489 * mods other filters have set here? */
490 if (latch == LATCH_PENDING)
491 state->components.latched_mods &=
492 ~filter->action.mods.mods.mask;
494 state->clear_mods = filter->action.mods.mods.mask;
495 state->components.locked_mods &= ~filter->action.mods.mods.mask;
499 latch = LATCH_PENDING;
500 state->clear_mods = filter->action.mods.mods.mask;
501 state->components.latched_mods |= filter->action.mods.mods.mask;
505 else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
506 /* Someone's pressed another key while we've still got the latching
507 * key held down, so keep the base modifier state active (from
508 * xkb_filter_mod_latch_new), but don't trip the latch, just clear
509 * it as soon as the modifier gets released. */
513 filter->priv = latch;
515 return XKB_FILTER_CONTINUE;
518 static const struct {
519 void (*new)(struct xkb_state *state, struct xkb_filter *filter);
520 bool (*func)(struct xkb_state *state, struct xkb_filter *filter,
521 const struct xkb_key *key, enum xkb_key_direction direction);
522 } filter_action_funcs[_ACTION_TYPE_NUM_ENTRIES] = {
523 [ACTION_TYPE_MOD_SET] = { xkb_filter_mod_set_new,
524 xkb_filter_mod_set_func },
525 [ACTION_TYPE_MOD_LATCH] = { xkb_filter_mod_latch_new,
526 xkb_filter_mod_latch_func },
527 [ACTION_TYPE_MOD_LOCK] = { xkb_filter_mod_lock_new,
528 xkb_filter_mod_lock_func },
529 [ACTION_TYPE_GROUP_SET] = { xkb_filter_group_set_new,
530 xkb_filter_group_set_func },
531 [ACTION_TYPE_GROUP_LOCK] = { xkb_filter_group_lock_new,
532 xkb_filter_group_lock_func },
536 * Applies any relevant filters to the key, first from the list of filters
537 * that are currently active, then if no filter has claimed the key, possibly
538 * apply a new filter from the key action.
541 xkb_filter_apply_all(struct xkb_state *state,
542 const struct xkb_key *key,
543 enum xkb_key_direction direction)
545 struct xkb_filter *filter;
546 const union xkb_action *action;
549 /* First run through all the currently active filters and see if any of
550 * them have consumed this event. */
552 darray_foreach(filter, state->filters) {
556 if (filter->func(state, filter, key, direction) == XKB_FILTER_CONSUME)
559 if (consumed || direction == XKB_KEY_UP)
562 action = xkb_key_get_action(state, key);
565 * It's possible for the keymap to set action->type explicitly, like so:
566 * interpret XF86_Next_VMode {
567 * action = Private(type=0x86, data="+VMode");
569 * We don't handle those.
571 if (action->type >= _ACTION_TYPE_NUM_ENTRIES)
574 if (!filter_action_funcs[action->type].new)
577 filter = xkb_filter_new(state);
579 filter->func = filter_action_funcs[action->type].func;
580 filter->action = *action;
581 filter_action_funcs[action->type].new(state, filter);
584 XKB_EXPORT struct xkb_state *
585 xkb_state_new(struct xkb_keymap *keymap)
587 struct xkb_state *ret;
589 ret = calloc(sizeof(*ret), 1);
594 ret->keymap = xkb_keymap_ref(keymap);
599 XKB_EXPORT struct xkb_state *
600 xkb_state_ref(struct xkb_state *state)
607 xkb_state_unref(struct xkb_state *state)
609 if (!state || --state->refcnt > 0)
612 xkb_keymap_unref(state->keymap);
613 darray_free(state->filters);
617 XKB_EXPORT struct xkb_keymap *
618 xkb_state_get_keymap(struct xkb_state *state)
620 return state->keymap;
624 * Update the LED state to match the rest of the xkb_state.
627 xkb_state_led_update_all(struct xkb_state *state)
630 const struct xkb_led *led;
632 state->components.leds = 0;
634 xkb_leds_enumerate(idx, led, state->keymap) {
635 xkb_mod_mask_t mod_mask = 0;
636 xkb_layout_mask_t group_mask = 0;
638 if (led->which_mods != 0 && led->mods.mask != 0) {
639 if (led->which_mods & XKB_STATE_MODS_EFFECTIVE)
640 mod_mask |= state->components.mods;
641 if (led->which_mods & XKB_STATE_MODS_DEPRESSED)
642 mod_mask |= state->components.base_mods;
643 if (led->which_mods & XKB_STATE_MODS_LATCHED)
644 mod_mask |= state->components.latched_mods;
645 if (led->which_mods & XKB_STATE_MODS_LOCKED)
646 mod_mask |= state->components.locked_mods;
648 if (led->mods.mask & mod_mask) {
649 state->components.leds |= (1u << idx);
654 if (led->which_groups != 0 && led->groups != 0) {
655 if (led->which_groups & XKB_STATE_LAYOUT_EFFECTIVE)
656 group_mask |= (1u << state->components.group);
657 if (led->which_groups & XKB_STATE_LAYOUT_DEPRESSED)
658 group_mask |= (1u << state->components.base_group);
659 if (led->which_groups & XKB_STATE_LAYOUT_LATCHED)
660 group_mask |= (1u << state->components.latched_group);
661 if (led->which_groups & XKB_STATE_LAYOUT_LOCKED)
662 group_mask |= (1u << state->components.locked_group);
664 if (led->groups & group_mask) {
665 state->components.leds |= (1u << idx);
670 if (led->ctrls & state->keymap->enabled_ctrls) {
671 state->components.leds |= (1u << idx);
678 * Calculates the derived state (effective mods/group and LEDs) from an
679 * up-to-date xkb_state.
682 xkb_state_update_derived(struct xkb_state *state)
684 xkb_layout_index_t wrapped;
686 state->components.mods = (state->components.base_mods |
687 state->components.latched_mods |
688 state->components.locked_mods);
690 /* TODO: Use groups_wrap control instead of always RANGE_WRAP. */
692 wrapped = XkbWrapGroupIntoRange(state->components.locked_group,
693 state->keymap->num_groups,
695 state->components.locked_group =
696 (wrapped == XKB_LAYOUT_INVALID ? 0 : wrapped);
698 wrapped = XkbWrapGroupIntoRange(state->components.base_group +
699 state->components.latched_group +
700 state->components.locked_group,
701 state->keymap->num_groups,
703 state->components.group =
704 (wrapped == XKB_LAYOUT_INVALID ? 0 : wrapped);
706 xkb_state_led_update_all(state);
709 static enum xkb_state_component
710 get_state_component_changes(const struct state_components *a,
711 const struct state_components *b)
713 xkb_mod_mask_t mask = 0;
715 if (a->group != b->group)
716 mask |= XKB_STATE_LAYOUT_EFFECTIVE;
717 if (a->base_group != b->base_group)
718 mask |= XKB_STATE_LAYOUT_DEPRESSED;
719 if (a->latched_group != b->latched_group)
720 mask |= XKB_STATE_LAYOUT_LATCHED;
721 if (a->locked_group != b->locked_group)
722 mask |= XKB_STATE_LAYOUT_LOCKED;
723 if (a->mods != b->mods)
724 mask |= XKB_STATE_MODS_EFFECTIVE;
725 if (a->base_mods != b->base_mods)
726 mask |= XKB_STATE_MODS_DEPRESSED;
727 if (a->latched_mods != b->latched_mods)
728 mask |= XKB_STATE_MODS_LATCHED;
729 if (a->locked_mods != b->locked_mods)
730 mask |= XKB_STATE_MODS_LOCKED;
731 if (a->leds != b->leds)
732 mask |= XKB_STATE_LEDS;
738 * Given a particular key event, updates the state structure to reflect the
741 XKB_EXPORT enum xkb_state_component
742 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
743 enum xkb_key_direction direction)
747 struct state_components prev_components;
748 const struct xkb_key *key = XkbKey(state->keymap, kc);
753 prev_components = state->components;
756 state->clear_mods = 0;
758 xkb_filter_apply_all(state, key, direction);
760 for (i = 0, bit = 1; state->set_mods; i++, bit <<= 1) {
761 if (state->set_mods & bit) {
762 state->mod_key_count[i]++;
763 state->components.base_mods |= bit;
764 state->set_mods &= ~bit;
768 for (i = 0, bit = 1; state->clear_mods; i++, bit <<= 1) {
769 if (state->clear_mods & bit) {
770 state->mod_key_count[i]--;
771 if (state->mod_key_count[i] <= 0) {
772 state->components.base_mods &= ~bit;
773 state->mod_key_count[i] = 0;
775 state->clear_mods &= ~bit;
779 xkb_state_update_derived(state);
781 return get_state_component_changes(&prev_components, &state->components);
785 * Updates the state from a set of explicit masks as gained from
786 * xkb_state_serialize_mods and xkb_state_serialize_groups. As noted in the
787 * documentation for these functions in xkbcommon.h, this round-trip is
788 * lossy, and should only be used to update a slave state mirroring the
789 * master, e.g. in a client/server window system.
791 XKB_EXPORT enum xkb_state_component
792 xkb_state_update_mask(struct xkb_state *state,
793 xkb_mod_mask_t base_mods,
794 xkb_mod_mask_t latched_mods,
795 xkb_mod_mask_t locked_mods,
796 xkb_layout_index_t base_group,
797 xkb_layout_index_t latched_group,
798 xkb_layout_index_t locked_group)
800 struct state_components prev_components;
803 prev_components = state->components;
805 /* Only include modifiers which exist in the keymap. */
806 mask = (xkb_mod_mask_t) ((1ull << xkb_keymap_num_mods(state->keymap)) - 1u);
808 state->components.base_mods = base_mods & mask;
809 state->components.latched_mods = latched_mods & mask;
810 state->components.locked_mods = locked_mods & mask;
812 /* Make sure the mods are fully resolved - since we get arbitrary
813 * input, they might not be.
815 * It might seem more reasonable to do this only for components.mods
816 * in xkb_state_update_derived(), rather than for each component
817 * seperately. That would allow to distinguish between "really"
818 * depressed mods (would be in MODS_DEPRESSED) and indirectly
819 * depressed to to a mapping (would only be in MODS_EFFECTIVE).
820 * However, the traditional behavior of xkb_state_update_key() is that
821 * if a vmod is depressed, its mappings are depressed with it; so we're
822 * expected to do the same here. Also, LEDs (usually) look if a real
823 * mod is locked, not just effective; otherwise it won't be lit.
825 * We OR here because mod_mask_get_effective() drops vmods. */
826 state->components.base_mods |=
827 mod_mask_get_effective(state->keymap, state->components.base_mods);
828 state->components.latched_mods |=
829 mod_mask_get_effective(state->keymap, state->components.latched_mods);
830 state->components.locked_mods |=
831 mod_mask_get_effective(state->keymap, state->components.locked_mods);
833 state->components.base_group = base_group;
834 state->components.latched_group = latched_group;
835 state->components.locked_group = locked_group;
837 xkb_state_update_derived(state);
839 return get_state_component_changes(&prev_components, &state->components);
843 * Provides the symbols to use for the given key and state. Returns the
844 * number of symbols pointed to in syms_out.
847 xkb_state_key_get_syms(struct xkb_state *state, xkb_keycode_t kc,
848 const xkb_keysym_t **syms_out)
850 xkb_layout_index_t layout;
851 xkb_level_index_t level;
853 layout = xkb_state_key_get_layout(state, kc);
854 if (layout == XKB_LAYOUT_INVALID)
857 level = xkb_state_key_get_level(state, kc, layout);
858 if (level == XKB_LEVEL_INVALID)
861 return xkb_keymap_key_get_syms_by_level(state->keymap, kc, layout, level,
870 * https://www.x.org/releases/current/doc/kbproto/xkbproto.html#Interpreting_the_Lock_Modifier
873 should_do_caps_transformation(struct xkb_state *state, xkb_keycode_t kc)
875 xkb_mod_index_t caps =
876 xkb_keymap_mod_get_index(state->keymap, XKB_MOD_NAME_CAPS);
879 xkb_state_mod_index_is_active(state, caps, XKB_STATE_MODS_EFFECTIVE) > 0 &&
880 xkb_state_mod_index_is_consumed(state, kc, caps) == 0;
884 * https://www.x.org/releases/current/doc/kbproto/xkbproto.html#Interpreting_the_Control_Modifier
887 should_do_ctrl_transformation(struct xkb_state *state, xkb_keycode_t kc)
889 xkb_mod_index_t ctrl =
890 xkb_keymap_mod_get_index(state->keymap, XKB_MOD_NAME_CTRL);
893 xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_MODS_EFFECTIVE) > 0 &&
894 xkb_state_mod_index_is_consumed(state, kc, ctrl) == 0;
897 /* Verbatim from libX11:src/xkb/XKBBind.c */
899 XkbToControl(char ch)
903 if ((c >= '@' && c < '\177') || c == ' ')
907 else if (c >= '3' && c <= '7')
917 * Provides either exactly one symbol, or XKB_KEY_NoSymbol.
919 XKB_EXPORT xkb_keysym_t
920 xkb_state_key_get_one_sym(struct xkb_state *state, xkb_keycode_t kc)
922 const xkb_keysym_t *syms;
926 num_syms = xkb_state_key_get_syms(state, kc, &syms);
928 return XKB_KEY_NoSymbol;
932 if (should_do_caps_transformation(state, kc))
933 sym = xkb_keysym_to_upper(sym);
939 * The caps and ctrl transformations require some special handling,
940 * so we cannot simply use xkb_state_get_one_sym() for them.
941 * In particular, if Control is set, we must try very hard to find
942 * some layout in which the keysym is ASCII and thus can be (maybe)
943 * converted to a control character. libX11 allows to disable this
944 * behavior with the XkbLC_ControlFallback (see XkbSetXlibControls(3)),
945 * but it is enabled by default, yippee.
948 get_one_sym_for_string(struct xkb_state *state, xkb_keycode_t kc)
950 xkb_level_index_t level;
951 xkb_layout_index_t layout, num_layouts;
952 const xkb_keysym_t *syms;
956 layout = xkb_state_key_get_layout(state, kc);
957 num_layouts = xkb_keymap_num_layouts_for_key(state->keymap, kc);
958 level = xkb_state_key_get_level(state, kc, layout);
959 if (layout == XKB_LAYOUT_INVALID || num_layouts == 0 ||
960 level == XKB_LEVEL_INVALID)
961 return XKB_KEY_NoSymbol;
963 nsyms = xkb_keymap_key_get_syms_by_level(state->keymap, kc,
964 layout, level, &syms);
966 return XKB_KEY_NoSymbol;
969 if (should_do_ctrl_transformation(state, kc) && sym > 127u) {
970 for (xkb_layout_index_t i = 0; i < num_layouts; i++) {
971 level = xkb_state_key_get_level(state, kc, i);
972 if (level == XKB_LEVEL_INVALID)
975 nsyms = xkb_keymap_key_get_syms_by_level(state->keymap, kc,
977 if (nsyms == 1 && syms[0] <= 127u) {
984 if (should_do_caps_transformation(state, kc)) {
985 sym = xkb_keysym_to_upper(sym);
992 xkb_state_key_get_utf8(struct xkb_state *state, xkb_keycode_t kc,
993 char *buffer, size_t size)
996 const xkb_keysym_t *syms;
1001 sym = get_one_sym_for_string(state, kc);
1002 if (sym != XKB_KEY_NoSymbol) {
1003 nsyms = 1; syms = &sym;
1006 nsyms = xkb_state_key_get_syms(state, kc, &syms);
1009 /* Make sure not to truncate in the middle of a UTF-8 sequence. */
1011 for (int i = 0; i < nsyms; i++) {
1012 int ret = xkb_keysym_to_utf8(syms[i], tmp, sizeof(tmp));
1017 if ((size_t) (offset + ret) <= size)
1018 memcpy(buffer + offset, tmp, ret);
1022 if ((size_t) offset >= size)
1024 buffer[offset] = '\0';
1026 if (!is_valid_utf8(buffer, offset))
1029 if (offset == 1 && (unsigned int) buffer[0] <= 127u &&
1030 should_do_ctrl_transformation(state, kc))
1031 buffer[0] = XkbToControl(buffer[0]);
1037 buffer[size - 1] = '\0';
1047 xkb_state_key_get_utf32(struct xkb_state *state, xkb_keycode_t kc)
1052 sym = get_one_sym_for_string(state, kc);
1053 cp = xkb_keysym_to_utf32(sym);
1055 if (cp <= 127u && should_do_ctrl_transformation(state, kc))
1056 cp = (uint32_t) XkbToControl((char) cp);
1062 * Serialises the requested modifier state into an xkb_mod_mask_t, with all
1063 * the same disclaimers as in xkb_state_update_mask.
1065 XKB_EXPORT xkb_mod_mask_t
1066 xkb_state_serialize_mods(struct xkb_state *state,
1067 enum xkb_state_component type)
1069 xkb_mod_mask_t ret = 0;
1071 if (type & XKB_STATE_MODS_EFFECTIVE)
1072 return state->components.mods;
1074 if (type & XKB_STATE_MODS_DEPRESSED)
1075 ret |= state->components.base_mods;
1076 if (type & XKB_STATE_MODS_LATCHED)
1077 ret |= state->components.latched_mods;
1078 if (type & XKB_STATE_MODS_LOCKED)
1079 ret |= state->components.locked_mods;
1085 * Serialises the requested group state, with all the same disclaimers as
1086 * in xkb_state_update_mask.
1088 XKB_EXPORT xkb_layout_index_t
1089 xkb_state_serialize_layout(struct xkb_state *state,
1090 enum xkb_state_component type)
1092 xkb_layout_index_t ret = 0;
1094 if (type & XKB_STATE_LAYOUT_EFFECTIVE)
1095 return state->components.group;
1097 if (type & XKB_STATE_LAYOUT_DEPRESSED)
1098 ret += state->components.base_group;
1099 if (type & XKB_STATE_LAYOUT_LATCHED)
1100 ret += state->components.latched_group;
1101 if (type & XKB_STATE_LAYOUT_LOCKED)
1102 ret += state->components.locked_group;
1108 * Gets a modifier mask and returns the resolved effective mask; this
1109 * is needed because some modifiers can also map to other modifiers, e.g.
1110 * the "NumLock" modifier usually also sets the "Mod2" modifier.
1113 mod_mask_get_effective(struct xkb_keymap *keymap, xkb_mod_mask_t mods)
1115 const struct xkb_mod *mod;
1117 xkb_mod_mask_t mask;
1119 /* The effective mask is only real mods for now. */
1120 mask = mods & MOD_REAL_MASK_ALL;
1122 xkb_mods_enumerate(i, mod, &keymap->mods)
1123 if (mods & (1u << i))
1124 mask |= mod->mapping;
1130 * Returns 1 if the given modifier is active with the specified type(s), 0 if
1131 * not, or -1 if the modifier is invalid.
1134 xkb_state_mod_index_is_active(struct xkb_state *state,
1135 xkb_mod_index_t idx,
1136 enum xkb_state_component type)
1138 if (idx >= xkb_keymap_num_mods(state->keymap))
1141 return !!(xkb_state_serialize_mods(state, type) & (1u << idx));
1145 * Helper function for xkb_state_mod_indices_are_active and
1146 * xkb_state_mod_names_are_active.
1149 match_mod_masks(struct xkb_state *state,
1150 enum xkb_state_component type,
1151 enum xkb_state_match match,
1152 xkb_mod_mask_t wanted)
1154 xkb_mod_mask_t active = xkb_state_serialize_mods(state, type);
1156 if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
1159 if (match & XKB_STATE_MATCH_ANY)
1160 return active & wanted;
1162 return (active & wanted) == wanted;
1166 * Returns 1 if the modifiers are active with the specified type(s), 0 if
1167 * not, or -1 if any of the modifiers are invalid.
1170 xkb_state_mod_indices_are_active(struct xkb_state *state,
1171 enum xkb_state_component type,
1172 enum xkb_state_match match,
1176 xkb_mod_mask_t wanted = 0;
1178 xkb_mod_index_t num_mods = xkb_keymap_num_mods(state->keymap);
1180 va_start(ap, match);
1182 xkb_mod_index_t idx = va_arg(ap, xkb_mod_index_t);
1183 if (idx == XKB_MOD_INVALID)
1185 if (idx >= num_mods) {
1189 wanted |= (1u << idx);
1196 return match_mod_masks(state, type, match, wanted);
1200 * Returns 1 if the given modifier is active with the specified type(s), 0 if
1201 * not, or -1 if the modifier is invalid.
1204 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
1205 enum xkb_state_component type)
1207 xkb_mod_index_t idx = xkb_keymap_mod_get_index(state->keymap, name);
1209 if (idx == XKB_MOD_INVALID)
1212 return xkb_state_mod_index_is_active(state, idx, type);
1216 * Returns 1 if the modifiers are active with the specified type(s), 0 if
1217 * not, or -1 if any of the modifiers are invalid.
1219 XKB_EXPORT ATTR_NULL_SENTINEL int
1220 xkb_state_mod_names_are_active(struct xkb_state *state,
1221 enum xkb_state_component type,
1222 enum xkb_state_match match,
1226 xkb_mod_mask_t wanted = 0;
1229 va_start(ap, match);
1231 xkb_mod_index_t idx;
1232 const char *str = va_arg(ap, const char *);
1235 idx = xkb_keymap_mod_get_index(state->keymap, str);
1236 if (idx == XKB_MOD_INVALID) {
1240 wanted |= (1u << idx);
1247 return match_mod_masks(state, type, match, wanted);
1251 * Returns 1 if the given group is active with the specified type(s), 0 if
1252 * not, or -1 if the group is invalid.
1255 xkb_state_layout_index_is_active(struct xkb_state *state,
1256 xkb_layout_index_t idx,
1257 enum xkb_state_component type)
1261 if (idx >= state->keymap->num_groups)
1264 if (type & XKB_STATE_LAYOUT_EFFECTIVE)
1265 ret |= (state->components.group == idx);
1266 if (type & XKB_STATE_LAYOUT_DEPRESSED)
1267 ret |= (state->components.base_group == (int32_t) idx);
1268 if (type & XKB_STATE_LAYOUT_LATCHED)
1269 ret |= (state->components.latched_group == (int32_t) idx);
1270 if (type & XKB_STATE_LAYOUT_LOCKED)
1271 ret |= (state->components.locked_group == (int32_t) idx);
1277 * Returns 1 if the given modifier is active with the specified type(s), 0 if
1278 * not, or -1 if the modifier is invalid.
1281 xkb_state_layout_name_is_active(struct xkb_state *state, const char *name,
1282 enum xkb_state_component type)
1284 xkb_layout_index_t idx = xkb_keymap_layout_get_index(state->keymap, name);
1286 if (idx == XKB_LAYOUT_INVALID)
1289 return xkb_state_layout_index_is_active(state, idx, type);
1293 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
1296 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
1298 if (idx >= state->keymap->num_leds ||
1299 state->keymap->leds[idx].name == XKB_ATOM_NONE)
1302 return !!(state->components.leds & (1u << idx));
1306 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
1309 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
1311 xkb_led_index_t idx = xkb_keymap_led_get_index(state->keymap, name);
1313 if (idx == XKB_LED_INVALID)
1316 return xkb_state_led_index_is_active(state, idx);
1321 * - XkbTranslateKeyCode(3), mod_rtrn return value, from libX11.
1322 * - MyEnhancedXkbTranslateKeyCode(), a modification of the above, from GTK+.
1324 static xkb_mod_mask_t
1325 key_get_consumed(struct xkb_state *state, const struct xkb_key *key,
1326 enum xkb_consumed_mode mode)
1328 const struct xkb_key_type *type;
1329 const struct xkb_key_type_entry *matching_entry;
1330 xkb_mod_mask_t preserve = 0;
1331 xkb_layout_index_t group;
1332 xkb_mod_mask_t consumed = 0;
1334 group = xkb_state_key_get_layout(state, key->keycode);
1335 if (group == XKB_LAYOUT_INVALID)
1338 type = key->groups[group].type;
1340 matching_entry = get_entry_for_key_state(state, key, group);
1342 preserve = matching_entry->preserve.mask;
1345 case XKB_CONSUMED_MODE_XKB:
1346 consumed = type->mods.mask;
1349 case XKB_CONSUMED_MODE_GTK: {
1350 const struct xkb_key_type_entry *no_mods_entry;
1351 xkb_level_index_t no_mods_leveli;
1352 const struct xkb_level *no_mods_level, *level;
1354 no_mods_entry = get_entry_for_mods(type, 0);
1355 no_mods_leveli = no_mods_entry ? no_mods_entry->level : 0;
1356 no_mods_level = &key->groups[group].levels[no_mods_leveli];
1358 for (unsigned i = 0; i < type->num_entries; i++) {
1359 const struct xkb_key_type_entry *entry = &type->entries[i];
1360 if (!entry_is_active(entry))
1363 level = &key->groups[group].levels[entry->level];
1364 if (XkbLevelsSameSyms(level, no_mods_level))
1367 if (entry == matching_entry || one_bit_set(entry->mods.mask))
1368 consumed |= entry->mods.mask & ~entry->preserve.mask;
1374 return consumed & ~preserve;
1378 xkb_state_mod_index_is_consumed2(struct xkb_state *state, xkb_keycode_t kc,
1379 xkb_mod_index_t idx,
1380 enum xkb_consumed_mode mode)
1382 const struct xkb_key *key = XkbKey(state->keymap, kc);
1384 if (!key || idx >= xkb_keymap_num_mods(state->keymap))
1387 return !!((1u << idx) & key_get_consumed(state, key, mode));
1391 xkb_state_mod_index_is_consumed(struct xkb_state *state, xkb_keycode_t kc,
1392 xkb_mod_index_t idx)
1394 return xkb_state_mod_index_is_consumed2(state, kc, idx,
1395 XKB_CONSUMED_MODE_XKB);
1398 XKB_EXPORT xkb_mod_mask_t
1399 xkb_state_mod_mask_remove_consumed(struct xkb_state *state, xkb_keycode_t kc,
1400 xkb_mod_mask_t mask)
1402 const struct xkb_key *key = XkbKey(state->keymap, kc);
1407 return mask & ~key_get_consumed(state, key, XKB_CONSUMED_MODE_XKB);
1410 XKB_EXPORT xkb_mod_mask_t
1411 xkb_state_key_get_consumed_mods2(struct xkb_state *state, xkb_keycode_t kc,
1412 enum xkb_consumed_mode mode)
1414 const struct xkb_key *key;
1417 case XKB_CONSUMED_MODE_XKB:
1418 case XKB_CONSUMED_MODE_GTK:
1421 log_err_func(state->keymap->ctx,
1422 "unrecognized consumed modifiers mode: %d\n", mode);
1426 key = XkbKey(state->keymap, kc);
1430 return key_get_consumed(state, key, mode);
1433 XKB_EXPORT xkb_mod_mask_t
1434 xkb_state_key_get_consumed_mods(struct xkb_state *state, xkb_keycode_t kc)
1436 return xkb_state_key_get_consumed_mods2(state, kc, XKB_CONSUMED_MODE_XKB);