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;
119 static const struct xkb_key_type_entry *
120 get_entry_for_key_state(struct xkb_state *state, const struct xkb_key *key,
121 xkb_layout_index_t group)
123 const struct xkb_key_type *type = key->groups[group].type;
124 xkb_mod_mask_t active_mods = state->components.mods & type->mods.mask;
126 for (unsigned i = 0; i < type->num_entries; i++) {
128 * If the virtual modifiers are not bound to anything, we're
129 * supposed to skip the entry (xserver does this with cached
130 * entry->active field).
132 if (!type->entries[i].mods.mask)
135 if (type->entries[i].mods.mask == active_mods)
136 return &type->entries[i];
143 * Returns the level to use for the given key and state, or
146 XKB_EXPORT xkb_level_index_t
147 xkb_state_key_get_level(struct xkb_state *state, xkb_keycode_t kc,
148 xkb_layout_index_t layout)
150 const struct xkb_key *key = XkbKey(state->keymap, kc);
151 const struct xkb_key_type_entry *entry;
153 if (!key || layout >= key->num_groups)
154 return XKB_LEVEL_INVALID;
156 /* If we don't find an explicit match the default is 0. */
157 entry = get_entry_for_key_state(state, key, layout);
165 XkbWrapGroupIntoRange(int32_t group,
166 xkb_layout_index_t num_groups,
167 enum xkb_range_exceed_type out_of_range_group_action,
168 xkb_layout_index_t out_of_range_group_number)
171 return XKB_LAYOUT_INVALID;
173 if (group >= 0 && (xkb_layout_index_t) group < num_groups)
176 switch (out_of_range_group_action) {
178 if (out_of_range_group_number >= num_groups)
180 return out_of_range_group_number;
186 return num_groups - 1;
191 * C99 says a negative dividend in a modulo operation always
192 * gives a negative result.
195 return ((int) num_groups + (group % (int) num_groups));
197 return group % num_groups;
202 * Returns the layout to use for the given key and state, taking
203 * wrapping/clamping/etc into account, or XKB_LAYOUT_INVALID.
205 XKB_EXPORT xkb_layout_index_t
206 xkb_state_key_get_layout(struct xkb_state *state, xkb_keycode_t kc)
208 const struct xkb_key *key = XkbKey(state->keymap, kc);
211 return XKB_LAYOUT_INVALID;
213 return XkbWrapGroupIntoRange(state->components.group, key->num_groups,
214 key->out_of_range_group_action,
215 key->out_of_range_group_number);
218 static const union xkb_action fake = { .type = ACTION_TYPE_NONE };
220 static const union xkb_action *
221 xkb_key_get_action(struct xkb_state *state, const struct xkb_key *key)
223 xkb_layout_index_t layout;
224 xkb_level_index_t level;
226 layout = xkb_state_key_get_layout(state, key->keycode);
227 if (layout == XKB_LAYOUT_INVALID)
230 level = xkb_state_key_get_level(state, key->keycode, layout);
231 if (level == XKB_LEVEL_INVALID)
234 return &key->groups[layout].levels[level].action;
237 static struct xkb_filter *
238 xkb_filter_new(struct xkb_state *state)
240 struct xkb_filter *filter = NULL, *iter;
242 darray_foreach(iter, state->filters) {
250 darray_resize0(state->filters, darray_size(state->filters) + 1);
251 filter = &darray_item(state->filters, darray_size(state->filters) -1);
258 /***====================================================================***/
261 xkb_filter_group_set_func(struct xkb_state *state,
262 struct xkb_filter *filter,
263 const struct xkb_key *key,
264 enum xkb_key_direction direction)
266 if (key != filter->key) {
267 filter->action.group.flags &= ~ACTION_LOCK_CLEAR;
271 if (direction == XKB_KEY_DOWN) {
275 else if (--filter->refcnt > 0) {
279 state->components.base_group = filter->priv;
281 if (filter->action.group.flags & ACTION_LOCK_CLEAR)
282 state->components.locked_group = 0;
289 xkb_filter_group_set_new(struct xkb_state *state, struct xkb_filter *filter)
291 filter->priv = state->components.base_group;
292 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
293 state->components.base_group = filter->action.group.group;
295 state->components.base_group += filter->action.group.group;
299 xkb_filter_group_lock_func(struct xkb_state *state,
300 struct xkb_filter *filter,
301 const struct xkb_key *key,
302 enum xkb_key_direction direction)
304 if (key != filter->key)
307 if (direction == XKB_KEY_DOWN) {
311 if (--filter->refcnt > 0)
319 xkb_filter_group_lock_new(struct xkb_state *state, struct xkb_filter *filter)
321 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
322 state->components.locked_group = filter->action.group.group;
324 state->components.locked_group += filter->action.group.group;
328 xkb_filter_mod_set_func(struct xkb_state *state,
329 struct xkb_filter *filter,
330 const struct xkb_key *key,
331 enum xkb_key_direction direction)
333 if (key != filter->key) {
334 filter->action.mods.flags &= ~ACTION_LOCK_CLEAR;
338 if (direction == XKB_KEY_DOWN) {
342 else if (--filter->refcnt > 0) {
346 state->clear_mods = filter->action.mods.mods.mask;
347 if (filter->action.mods.flags & ACTION_LOCK_CLEAR)
348 state->components.locked_mods &= ~filter->action.mods.mods.mask;
355 xkb_filter_mod_set_new(struct xkb_state *state, struct xkb_filter *filter)
357 state->set_mods = filter->action.mods.mods.mask;
361 xkb_filter_mod_lock_func(struct xkb_state *state,
362 struct xkb_filter *filter,
363 const struct xkb_key *key,
364 enum xkb_key_direction direction)
366 if (key != filter->key)
369 if (direction == XKB_KEY_DOWN) {
373 if (--filter->refcnt > 0)
376 state->clear_mods |= filter->action.mods.mods.mask;
377 if (!(filter->action.mods.flags & ACTION_LOCK_NO_UNLOCK))
378 state->components.locked_mods &= ~filter->priv;
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;
394 enum xkb_key_latch_state {
401 xkb_action_breaks_latch(const union xkb_action *action)
403 switch (action->type) {
404 case ACTION_TYPE_NONE:
405 case ACTION_TYPE_PTR_BUTTON:
406 case ACTION_TYPE_PTR_LOCK:
407 case ACTION_TYPE_CTRL_SET:
408 case ACTION_TYPE_CTRL_LOCK:
409 case ACTION_TYPE_SWITCH_VT:
410 case ACTION_TYPE_TERMINATE:
418 xkb_filter_mod_latch_func(struct xkb_state *state,
419 struct xkb_filter *filter,
420 const struct xkb_key *key,
421 enum xkb_key_direction direction)
423 enum xkb_key_latch_state latch = filter->priv;
425 if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
426 /* If this is a new keypress and we're awaiting our single latched
427 * keypress, then either break the latch if any random key is pressed,
428 * or promote it to a lock or plain base set if it's the same
430 const union xkb_action *action = xkb_key_get_action(state, key);
431 if (action->type == ACTION_TYPE_MOD_LATCH &&
432 action->mods.flags == filter->action.mods.flags &&
433 action->mods.mods.mask == filter->action.mods.mods.mask) {
434 filter->action = *action;
435 if (filter->action.mods.flags & ACTION_LATCH_TO_LOCK) {
436 filter->action.type = ACTION_TYPE_MOD_LOCK;
437 filter->func = xkb_filter_mod_lock_func;
438 state->components.locked_mods |= filter->action.mods.mods.mask;
441 filter->action.type = ACTION_TYPE_MOD_SET;
442 filter->func = xkb_filter_mod_set_func;
443 state->set_mods = filter->action.mods.mods.mask;
446 state->components.latched_mods &= ~filter->action.mods.mods.mask;
450 else if (xkb_action_breaks_latch(action)) {
451 /* XXX: This may be totally broken, we might need to break the
452 * latch in the next run after this press? */
453 state->components.latched_mods &= ~filter->action.mods.mods.mask;
458 else if (direction == XKB_KEY_UP && key == filter->key) {
459 /* Our key got released. If we've set it to clear locks, and we
460 * currently have the same modifiers locked, then release them and
461 * don't actually latch. Else we've actually hit the latching
462 * stage, so set PENDING and move our modifier from base to
464 if (latch == NO_LATCH ||
465 ((filter->action.mods.flags & ACTION_LOCK_CLEAR) &&
466 (state->components.locked_mods & filter->action.mods.mods.mask) ==
467 filter->action.mods.mods.mask)) {
468 /* XXX: We might be a bit overenthusiastic about clearing
469 * mods other filters have set here? */
470 if (latch == LATCH_PENDING)
471 state->components.latched_mods &=
472 ~filter->action.mods.mods.mask;
474 state->clear_mods = filter->action.mods.mods.mask;
475 state->components.locked_mods &= ~filter->action.mods.mods.mask;
479 latch = LATCH_PENDING;
480 state->clear_mods = filter->action.mods.mods.mask;
481 state->components.latched_mods |= filter->action.mods.mods.mask;
485 else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
486 /* Someone's pressed another key while we've still got the latching
487 * key held down, so keep the base modifier state active (from
488 * xkb_filter_mod_latch_new), but don't trip the latch, just clear
489 * it as soon as the modifier gets released. */
493 filter->priv = latch;
499 xkb_filter_mod_latch_new(struct xkb_state *state, struct xkb_filter *filter)
501 filter->priv = LATCH_KEY_DOWN;
502 state->set_mods = filter->action.mods.mods.mask;
505 static const struct {
506 void (*new)(struct xkb_state *state, struct xkb_filter *filter);
507 bool (*func)(struct xkb_state *state, struct xkb_filter *filter,
508 const struct xkb_key *key, enum xkb_key_direction direction);
509 } filter_action_funcs[_ACTION_TYPE_NUM_ENTRIES] = {
510 [ACTION_TYPE_MOD_SET] = { xkb_filter_mod_set_new,
511 xkb_filter_mod_set_func },
512 [ACTION_TYPE_MOD_LATCH] = { xkb_filter_mod_latch_new,
513 xkb_filter_mod_latch_func },
514 [ACTION_TYPE_MOD_LOCK] = { xkb_filter_mod_lock_new,
515 xkb_filter_mod_lock_func },
516 [ACTION_TYPE_GROUP_SET] = { xkb_filter_group_set_new,
517 xkb_filter_group_set_func },
518 [ACTION_TYPE_GROUP_LOCK] = { xkb_filter_group_lock_new,
519 xkb_filter_group_lock_func },
523 * Applies any relevant filters to the key, first from the list of filters
524 * that are currently active, then if no filter has claimed the key, possibly
525 * apply a new filter from the key action.
528 xkb_filter_apply_all(struct xkb_state *state,
529 const struct xkb_key *key,
530 enum xkb_key_direction direction)
532 struct xkb_filter *filter;
533 const union xkb_action *action;
536 /* First run through all the currently active filters and see if any of
537 * them have claimed this event. */
538 darray_foreach(filter, state->filters) {
541 send = filter->func(state, filter, key, direction) && send;
544 if (!send || direction == XKB_KEY_UP)
547 action = xkb_key_get_action(state, key);
550 * It's possible for the keymap to set action->type explicitly, like so:
551 * interpret XF86_Next_VMode {
552 * action = Private(type=0x86, data="+VMode");
554 * We don't handle those.
556 if (action->type >= _ACTION_TYPE_NUM_ENTRIES)
559 if (!filter_action_funcs[action->type].new)
562 filter = xkb_filter_new(state);
567 filter->func = filter_action_funcs[action->type].func;
568 filter->action = *action;
569 filter_action_funcs[action->type].new(state, filter);
572 XKB_EXPORT struct xkb_state *
573 xkb_state_new(struct xkb_keymap *keymap)
575 struct xkb_state *ret;
577 ret = calloc(sizeof(*ret), 1);
582 ret->keymap = xkb_keymap_ref(keymap);
587 XKB_EXPORT struct xkb_state *
588 xkb_state_ref(struct xkb_state *state)
595 xkb_state_unref(struct xkb_state *state)
597 if (!state || --state->refcnt > 0)
600 xkb_keymap_unref(state->keymap);
601 darray_free(state->filters);
605 XKB_EXPORT struct xkb_keymap *
606 xkb_state_get_keymap(struct xkb_state *state)
608 return state->keymap;
612 * Update the LED state to match the rest of the xkb_state.
615 xkb_state_led_update_all(struct xkb_state *state)
618 const struct xkb_led *led;
620 state->components.leds = 0;
622 xkb_leds_enumerate(idx, led, state->keymap) {
623 xkb_mod_mask_t mod_mask = 0;
624 xkb_layout_mask_t group_mask = 0;
626 if (led->which_mods != 0 && led->mods.mask != 0) {
627 if (led->which_mods & XKB_STATE_MODS_EFFECTIVE)
628 mod_mask |= state->components.mods;
629 if (led->which_mods & XKB_STATE_MODS_DEPRESSED)
630 mod_mask |= state->components.base_mods;
631 if (led->which_mods & XKB_STATE_MODS_LATCHED)
632 mod_mask |= state->components.latched_mods;
633 if (led->which_mods & XKB_STATE_MODS_LOCKED)
634 mod_mask |= state->components.locked_mods;
636 if (led->mods.mask & mod_mask) {
637 state->components.leds |= (1u << idx);
642 if (led->which_groups != 0 && led->groups != 0) {
643 if (led->which_groups & XKB_STATE_LAYOUT_EFFECTIVE)
644 group_mask |= (1u << state->components.group);
645 if (led->which_groups & XKB_STATE_LAYOUT_DEPRESSED)
646 group_mask |= (1u << state->components.base_group);
647 if (led->which_groups & XKB_STATE_LAYOUT_LATCHED)
648 group_mask |= (1u << state->components.latched_group);
649 if (led->which_groups & XKB_STATE_LAYOUT_LOCKED)
650 group_mask |= (1u << state->components.locked_group);
652 if (led->groups & group_mask) {
653 state->components.leds |= (1u << idx);
658 if (led->ctrls & state->keymap->enabled_ctrls) {
659 state->components.leds |= (1u << idx);
666 * Calculates the derived state (effective mods/group and LEDs) from an
667 * up-to-date xkb_state.
670 xkb_state_update_derived(struct xkb_state *state)
672 xkb_layout_index_t wrapped;
674 state->components.mods = (state->components.base_mods |
675 state->components.latched_mods |
676 state->components.locked_mods);
678 /* TODO: Use groups_wrap control instead of always RANGE_WRAP. */
680 wrapped = XkbWrapGroupIntoRange(state->components.locked_group,
681 state->keymap->num_groups,
683 state->components.locked_group =
684 (wrapped == XKB_LAYOUT_INVALID ? 0 : wrapped);
686 wrapped = XkbWrapGroupIntoRange(state->components.base_group +
687 state->components.latched_group +
688 state->components.locked_group,
689 state->keymap->num_groups,
691 state->components.group =
692 (wrapped == XKB_LAYOUT_INVALID ? 0 : wrapped);
694 xkb_state_led_update_all(state);
697 static enum xkb_state_component
698 get_state_component_changes(const struct state_components *a,
699 const struct state_components *b)
701 xkb_mod_mask_t mask = 0;
703 if (a->group != b->group)
704 mask |= XKB_STATE_LAYOUT_EFFECTIVE;
705 if (a->base_group != b->base_group)
706 mask |= XKB_STATE_LAYOUT_DEPRESSED;
707 if (a->latched_group != b->latched_group)
708 mask |= XKB_STATE_LAYOUT_LATCHED;
709 if (a->locked_group != b->locked_group)
710 mask |= XKB_STATE_LAYOUT_LOCKED;
711 if (a->mods != b->mods)
712 mask |= XKB_STATE_MODS_EFFECTIVE;
713 if (a->base_mods != b->base_mods)
714 mask |= XKB_STATE_MODS_DEPRESSED;
715 if (a->latched_mods != b->latched_mods)
716 mask |= XKB_STATE_MODS_LATCHED;
717 if (a->locked_mods != b->locked_mods)
718 mask |= XKB_STATE_MODS_LOCKED;
719 if (a->leds != b->leds)
720 mask |= XKB_STATE_LEDS;
726 * Given a particular key event, updates the state structure to reflect the
729 XKB_EXPORT enum xkb_state_component
730 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
731 enum xkb_key_direction direction)
735 struct state_components prev_components;
736 const struct xkb_key *key = XkbKey(state->keymap, kc);
741 prev_components = state->components;
744 state->clear_mods = 0;
746 xkb_filter_apply_all(state, key, direction);
748 for (i = 0, bit = 1; state->set_mods; i++, bit <<= 1) {
749 if (state->set_mods & bit) {
750 state->mod_key_count[i]++;
751 state->components.base_mods |= bit;
752 state->set_mods &= ~bit;
756 for (i = 0, bit = 1; state->clear_mods; i++, bit <<= 1) {
757 if (state->clear_mods & bit) {
758 state->mod_key_count[i]--;
759 if (state->mod_key_count[i] <= 0) {
760 state->components.base_mods &= ~bit;
761 state->mod_key_count[i] = 0;
763 state->clear_mods &= ~bit;
767 xkb_state_update_derived(state);
769 return get_state_component_changes(&prev_components, &state->components);
773 * Updates the state from a set of explicit masks as gained from
774 * xkb_state_serialize_mods and xkb_state_serialize_groups. As noted in the
775 * documentation for these functions in xkbcommon.h, this round-trip is
776 * lossy, and should only be used to update a slave state mirroring the
777 * master, e.g. in a client/server window system.
779 XKB_EXPORT enum xkb_state_component
780 xkb_state_update_mask(struct xkb_state *state,
781 xkb_mod_mask_t base_mods,
782 xkb_mod_mask_t latched_mods,
783 xkb_mod_mask_t locked_mods,
784 xkb_layout_index_t base_group,
785 xkb_layout_index_t latched_group,
786 xkb_layout_index_t locked_group)
788 struct state_components prev_components;
791 prev_components = state->components;
793 /* Only include modifiers which exist in the keymap. */
794 mask = (xkb_mod_mask_t) ((1ull << xkb_keymap_num_mods(state->keymap)) - 1u);
796 state->components.base_mods = base_mods & mask;
797 state->components.latched_mods = latched_mods & mask;
798 state->components.locked_mods = locked_mods & mask;
800 state->components.base_group = base_group;
801 state->components.latched_group = latched_group;
802 state->components.locked_group = locked_group;
804 xkb_state_update_derived(state);
806 return get_state_component_changes(&prev_components, &state->components);
810 * Provides the symbols to use for the given key and state. Returns the
811 * number of symbols pointed to in syms_out.
814 xkb_state_key_get_syms(struct xkb_state *state, xkb_keycode_t kc,
815 const xkb_keysym_t **syms_out)
817 xkb_layout_index_t layout;
818 xkb_level_index_t level;
820 layout = xkb_state_key_get_layout(state, kc);
821 if (layout == XKB_LAYOUT_INVALID)
824 level = xkb_state_key_get_level(state, kc, layout);
825 if (level == XKB_LEVEL_INVALID)
828 return xkb_keymap_key_get_syms_by_level(state->keymap, kc, layout, level,
837 * http://www.x.org/releases/current/doc/kbproto/xkbproto.html#Interpreting_the_Lock_Modifier
840 should_do_caps_transformation(struct xkb_state *state, xkb_keycode_t kc)
842 xkb_mod_index_t caps =
843 xkb_keymap_mod_get_index(state->keymap, XKB_MOD_NAME_CAPS);
846 xkb_state_mod_index_is_active(state, caps, XKB_STATE_MODS_EFFECTIVE) > 0 &&
847 xkb_state_mod_index_is_consumed(state, kc, caps) == 0;
851 * http://www.x.org/releases/current/doc/kbproto/xkbproto.html#Interpreting_the_Control_Modifier
854 should_do_ctrl_transformation(struct xkb_state *state, xkb_keycode_t kc)
856 xkb_mod_index_t ctrl =
857 xkb_keymap_mod_get_index(state->keymap, XKB_MOD_NAME_CTRL);
860 xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_MODS_EFFECTIVE) > 0 &&
861 xkb_state_mod_index_is_consumed(state, kc, ctrl) == 0;
864 /* Verbatim from libX11:src/xkb/XKBBind.c */
866 XkbToControl(char ch)
870 if ((c >= '@' && c < '\177') || c == ' ')
874 else if (c >= '3' && c <= '7')
884 * Provides either exactly one symbol, or XKB_KEY_NoSymbol.
886 XKB_EXPORT xkb_keysym_t
887 xkb_state_key_get_one_sym(struct xkb_state *state, xkb_keycode_t kc)
889 const xkb_keysym_t *syms;
893 num_syms = xkb_state_key_get_syms(state, kc, &syms);
895 return XKB_KEY_NoSymbol;
899 if (should_do_caps_transformation(state, kc))
900 sym = xkb_keysym_to_upper(sym);
906 * The caps and ctrl transformations require some special handling,
907 * so we cannot simply use xkb_state_get_one_sym() for them.
908 * In particular, if Control is set, we must try very hard to find
909 * some layout in which the keysym is ASCII and thus can be (maybe)
910 * converted to a control character. libX11 allows to disable this
911 * behavior with the XkbLC_ControlFallback (see XkbSetXlibControls(3)),
912 * but it is enabled by default, yippee.
915 get_one_sym_for_string(struct xkb_state *state, xkb_keycode_t kc)
917 xkb_level_index_t level;
918 xkb_layout_index_t layout, num_layouts;
919 const xkb_keysym_t *syms;
923 layout = xkb_state_key_get_layout(state, kc);
924 num_layouts = xkb_keymap_num_layouts_for_key(state->keymap, kc);
925 level = xkb_state_key_get_level(state, kc, layout);
926 if (layout == XKB_LAYOUT_INVALID || num_layouts == 0 ||
927 level == XKB_LEVEL_INVALID)
928 return XKB_KEY_NoSymbol;
930 nsyms = xkb_keymap_key_get_syms_by_level(state->keymap, kc,
931 layout, level, &syms);
933 return XKB_KEY_NoSymbol;
936 if (should_do_ctrl_transformation(state, kc) && sym > 127u) {
937 for (xkb_layout_index_t i = 0; i < num_layouts; i++) {
938 level = xkb_state_key_get_level(state, kc, i);
939 if (level == XKB_LEVEL_INVALID)
942 nsyms = xkb_keymap_key_get_syms_by_level(state->keymap, kc,
944 if (nsyms == 1 && syms[0] <= 127u) {
951 if (should_do_caps_transformation(state, kc)) {
952 sym = xkb_keysym_to_upper(sym);
959 xkb_state_key_get_utf8(struct xkb_state *state, xkb_keycode_t kc,
960 char *buffer, size_t size)
963 const xkb_keysym_t *syms;
968 sym = get_one_sym_for_string(state, kc);
969 if (sym != XKB_KEY_NoSymbol) {
970 nsyms = 1; syms = &sym;
973 nsyms = xkb_state_key_get_syms(state, kc, &syms);
976 /* Make sure not to truncate in the middle of a UTF-8 sequence. */
978 for (int i = 0; i < nsyms; i++) {
979 int ret = xkb_keysym_to_utf8(syms[i], tmp, sizeof(tmp));
984 if ((size_t) (offset + ret) <= size)
985 memcpy(buffer + offset, tmp, ret);
989 if ((size_t) offset >= size)
991 buffer[offset] = '\0';
993 if (!is_valid_utf8(buffer, offset))
996 if (offset == 1 && (unsigned int) buffer[0] <= 127u &&
997 should_do_ctrl_transformation(state, kc))
998 buffer[0] = XkbToControl(buffer[0]);
1004 buffer[size - 1] = '\0';
1014 xkb_state_key_get_utf32(struct xkb_state *state, xkb_keycode_t kc)
1019 sym = get_one_sym_for_string(state, kc);
1020 cp = xkb_keysym_to_utf32(sym);
1022 if (cp <= 127u && should_do_ctrl_transformation(state, kc))
1023 cp = (uint32_t) XkbToControl((char) cp);
1029 * Serialises the requested modifier state into an xkb_mod_mask_t, with all
1030 * the same disclaimers as in xkb_state_update_mask.
1032 XKB_EXPORT xkb_mod_mask_t
1033 xkb_state_serialize_mods(struct xkb_state *state,
1034 enum xkb_state_component type)
1036 xkb_mod_mask_t ret = 0;
1038 if (type & XKB_STATE_MODS_EFFECTIVE)
1039 return state->components.mods;
1041 if (type & XKB_STATE_MODS_DEPRESSED)
1042 ret |= state->components.base_mods;
1043 if (type & XKB_STATE_MODS_LATCHED)
1044 ret |= state->components.latched_mods;
1045 if (type & XKB_STATE_MODS_LOCKED)
1046 ret |= state->components.locked_mods;
1052 * Serialises the requested group state, with all the same disclaimers as
1053 * in xkb_state_update_mask.
1055 XKB_EXPORT xkb_layout_index_t
1056 xkb_state_serialize_layout(struct xkb_state *state,
1057 enum xkb_state_component type)
1059 xkb_layout_index_t ret = 0;
1061 if (type & XKB_STATE_LAYOUT_EFFECTIVE)
1062 return state->components.group;
1064 if (type & XKB_STATE_LAYOUT_DEPRESSED)
1065 ret += state->components.base_group;
1066 if (type & XKB_STATE_LAYOUT_LATCHED)
1067 ret += state->components.latched_group;
1068 if (type & XKB_STATE_LAYOUT_LOCKED)
1069 ret += state->components.locked_group;
1075 * Gets a modifier mask and returns the resolved effective mask; this
1076 * is needed because some modifiers can also map to other modifiers, e.g.
1077 * the "NumLock" modifier usually also sets the "Mod2" modifier.
1080 mod_mask_get_effective(struct xkb_keymap *keymap, xkb_mod_mask_t mods)
1082 const struct xkb_mod *mod;
1084 xkb_mod_mask_t mask;
1086 /* The effective mask is only real mods for now. */
1087 mask = mods & MOD_REAL_MASK_ALL;
1089 xkb_mods_enumerate(i, mod, &keymap->mods)
1090 if (mods & (1u << i))
1091 mask |= mod->mapping;
1097 * Returns 1 if the given modifier is active with the specified type(s), 0 if
1098 * not, or -1 if the modifier is invalid.
1101 xkb_state_mod_index_is_active(struct xkb_state *state,
1102 xkb_mod_index_t idx,
1103 enum xkb_state_component type)
1105 if (idx >= xkb_keymap_num_mods(state->keymap))
1108 return !!(xkb_state_serialize_mods(state, type) & (1u << idx));
1112 * Helper function for xkb_state_mod_indices_are_active and
1113 * xkb_state_mod_names_are_active.
1116 match_mod_masks(struct xkb_state *state,
1117 enum xkb_state_component type,
1118 enum xkb_state_match match,
1119 xkb_mod_mask_t wanted)
1121 xkb_mod_mask_t active = xkb_state_serialize_mods(state, type);
1123 if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
1126 if (match & XKB_STATE_MATCH_ANY)
1127 return !!(active & wanted);
1129 return (active & wanted) == wanted;
1135 * Returns 1 if the modifiers are active with the specified type(s), 0 if
1136 * not, or -1 if any of the modifiers are invalid.
1139 xkb_state_mod_indices_are_active(struct xkb_state *state,
1140 enum xkb_state_component type,
1141 enum xkb_state_match match,
1145 xkb_mod_index_t idx = 0;
1146 xkb_mod_mask_t wanted = 0;
1148 xkb_mod_index_t num_mods = xkb_keymap_num_mods(state->keymap);
1150 va_start(ap, match);
1152 idx = va_arg(ap, xkb_mod_index_t);
1153 if (idx == XKB_MOD_INVALID)
1155 if (idx >= num_mods) {
1159 wanted |= (1u << idx);
1166 return match_mod_masks(state, type, match, wanted);
1170 * Returns 1 if the given modifier is active with the specified type(s), 0 if
1171 * not, or -1 if the modifier is invalid.
1174 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
1175 enum xkb_state_component type)
1177 xkb_mod_index_t idx = xkb_keymap_mod_get_index(state->keymap, name);
1179 if (idx == XKB_MOD_INVALID)
1182 return xkb_state_mod_index_is_active(state, idx, type);
1186 * Returns 1 if the modifiers are active with the specified type(s), 0 if
1187 * not, or -1 if any of the modifiers are invalid.
1189 XKB_EXPORT ATTR_NULL_SENTINEL int
1190 xkb_state_mod_names_are_active(struct xkb_state *state,
1191 enum xkb_state_component type,
1192 enum xkb_state_match match,
1196 xkb_mod_index_t idx = 0;
1197 xkb_mod_mask_t wanted = 0;
1200 va_start(ap, match);
1202 const char *str = va_arg(ap, const char *);
1205 idx = xkb_keymap_mod_get_index(state->keymap, str);
1206 if (idx == XKB_MOD_INVALID) {
1210 wanted |= (1u << idx);
1217 return match_mod_masks(state, type, match, wanted);
1221 * Returns 1 if the given group is active with the specified type(s), 0 if
1222 * not, or -1 if the group is invalid.
1225 xkb_state_layout_index_is_active(struct xkb_state *state,
1226 xkb_layout_index_t idx,
1227 enum xkb_state_component type)
1231 if (idx >= state->keymap->num_groups)
1234 if (type & XKB_STATE_LAYOUT_EFFECTIVE)
1235 ret |= (state->components.group == idx);
1236 if (type & XKB_STATE_LAYOUT_DEPRESSED)
1237 ret |= (state->components.base_group == (int32_t) idx);
1238 if (type & XKB_STATE_LAYOUT_LATCHED)
1239 ret |= (state->components.latched_group == (int32_t) idx);
1240 if (type & XKB_STATE_LAYOUT_LOCKED)
1241 ret |= (state->components.locked_group == (int32_t) idx);
1247 * Returns 1 if the given modifier is active with the specified type(s), 0 if
1248 * not, or -1 if the modifier is invalid.
1251 xkb_state_layout_name_is_active(struct xkb_state *state, const char *name,
1252 enum xkb_state_component type)
1254 xkb_layout_index_t idx = xkb_keymap_layout_get_index(state->keymap, name);
1256 if (idx == XKB_LAYOUT_INVALID)
1259 return xkb_state_layout_index_is_active(state, idx, type);
1263 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
1266 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
1268 if (idx >= state->keymap->num_leds ||
1269 state->keymap->leds[idx].name == XKB_ATOM_NONE)
1272 return !!(state->components.leds & (1u << idx));
1276 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
1279 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
1281 xkb_led_index_t idx = xkb_keymap_led_get_index(state->keymap, name);
1283 if (idx == XKB_LED_INVALID)
1286 return xkb_state_led_index_is_active(state, idx);
1289 static xkb_mod_mask_t
1290 key_get_consumed(struct xkb_state *state, const struct xkb_key *key)
1292 const struct xkb_key_type *type;
1293 const struct xkb_key_type_entry *entry;
1294 xkb_mod_mask_t preserve;
1295 xkb_layout_index_t group;
1297 group = xkb_state_key_get_layout(state, key->keycode);
1298 if (group == XKB_LAYOUT_INVALID)
1301 type = key->groups[group].type;
1303 entry = get_entry_for_key_state(state, key, group);
1305 preserve = entry->preserve.mask;
1309 return type->mods.mask & ~preserve;
1313 * Tests to see if a modifier is used up by our translation of a
1314 * keycode to keysyms, taking note of the current modifier state and
1315 * the appropriate key type's preserve information, if any. This allows
1316 * the user to mask out the modifier in later processing of the
1317 * modifiers, e.g. when implementing hot keys or accelerators.
1319 * See also, for example:
1320 * - XkbTranslateKeyCode(3), mod_rtrn return value, from libX11.
1321 * - gdk_keymap_translate_keyboard_state, consumed_modifiers return value,
1325 xkb_state_mod_index_is_consumed(struct xkb_state *state, xkb_keycode_t kc,
1326 xkb_mod_index_t idx)
1328 const struct xkb_key *key = XkbKey(state->keymap, kc);
1330 if (!key || idx >= xkb_keymap_num_mods(state->keymap))
1333 return !!((1u << idx) & key_get_consumed(state, key));
1337 * Calculates which modifiers should be consumed during key processing,
1338 * and returns the mask with all these modifiers removed. e.g. if
1339 * given a state of Alt and Shift active for a two-level alphabetic
1340 * key containing plus and equal on the first and second level
1341 * respectively, will return a mask of only Alt, as Shift has been
1342 * consumed by the type handling.
1344 XKB_EXPORT xkb_mod_mask_t
1345 xkb_state_mod_mask_remove_consumed(struct xkb_state *state, xkb_keycode_t kc,
1346 xkb_mod_mask_t mask)
1348 const struct xkb_key *key = XkbKey(state->keymap, kc);
1353 return mask & ~key_get_consumed(state, key);
1356 XKB_EXPORT xkb_mod_mask_t
1357 xkb_state_key_get_consumed_mods(struct xkb_state *state, xkb_keycode_t kc)
1359 const struct xkb_key *key = XkbKey(state->keymap, kc);
1364 return key_get_consumed(state, key);