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)
68 union xkb_action action;
69 const struct xkb_key *key;
71 int (*func)(struct xkb_state *state,
72 struct xkb_filter *filter,
73 const struct xkb_key *key,
74 enum xkb_key_direction direction);
79 xkb_group_index_t base_group; /**< depressed */
80 xkb_group_index_t latched_group;
81 xkb_group_index_t locked_group;
82 xkb_group_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 */
90 * At each event, we accumulate all the needed modifications to the base
91 * modifiers, and apply them at the end. These keep track of this state.
93 xkb_mod_mask_t set_mods;
94 xkb_mod_mask_t clear_mods;
96 * We mustn't clear a base modifier if there's another depressed key
97 * which affects it, e.g. given this sequence
98 * < Left Shift down, Right Shift down, Left Shift Up >
99 * the modifier should still be set. This keeps the count.
101 int16_t mod_key_count[sizeof(xkb_mod_mask_t) * 8];
106 darray(struct xkb_filter) filters;
107 struct xkb_keymap *keymap;
110 static const union xkb_action fake = { .type = ACTION_TYPE_NONE };
112 static const union xkb_action *
113 xkb_key_get_action(struct xkb_state *state, const struct xkb_key *key)
115 xkb_group_index_t group;
116 xkb_level_index_t level;
121 group = xkb_key_get_group(state, key);
122 if (group == XKB_GROUP_INVALID)
125 level = xkb_key_get_level(state, key, group);
126 if (level == XKB_LEVEL_INVALID)
129 return XkbKeyActionEntry(key, group, level);
132 static struct xkb_filter *
133 xkb_filter_new(struct xkb_state *state)
135 struct xkb_filter *filter = NULL, *iter;
137 darray_foreach(iter, state->filters) {
145 darray_resize0(state->filters, darray_size(state->filters) + 1);
146 filter = &darray_item(state->filters, darray_size(state->filters) -1);
153 /***====================================================================***/
156 xkb_filter_group_set_func(struct xkb_state *state,
157 struct xkb_filter *filter,
158 const struct xkb_key *key,
159 enum xkb_key_direction direction)
161 if (key != filter->key) {
162 filter->action.group.flags &= ~ACTION_LOCK_CLEAR;
166 if (direction == XKB_KEY_DOWN) {
170 else if (--filter->refcnt > 0) {
174 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
175 state->base_group = filter->action.group.group;
177 state->base_group = -filter->action.group.group;
178 if (filter->action.group.flags & ACTION_LOCK_CLEAR)
179 state->locked_group = 0;
186 xkb_filter_group_set_new(struct xkb_state *state,
187 const struct xkb_key *key,
188 const union xkb_action *action)
190 struct xkb_filter *filter = xkb_filter_new(state);
195 filter->func = xkb_filter_group_set_func;
196 filter->action = *action;
198 if (action->group.flags & ACTION_ABSOLUTE_SWITCH) {
199 filter->action.group.group = state->base_group;
200 state->base_group = action->group.group;
203 state->base_group += action->group.group;
210 xkb_filter_group_lock_func(struct xkb_state *state,
211 struct xkb_filter *filter,
212 const struct xkb_key *key,
213 enum xkb_key_direction direction)
215 if (key != filter->key)
218 if (direction == XKB_KEY_DOWN) {
222 if (--filter->refcnt > 0)
230 xkb_filter_group_lock_new(struct xkb_state *state,
231 const struct xkb_key *key,
232 const union xkb_action *action)
234 struct xkb_filter *filter = xkb_filter_new(state);
239 filter->func = xkb_filter_group_lock_func;
240 filter->action = *action;
242 if (action->group.flags & ACTION_ABSOLUTE_SWITCH)
243 state->locked_group = action->group.group;
245 state->locked_group += action->group.group;
251 xkb_filter_mod_set_func(struct xkb_state *state,
252 struct xkb_filter *filter,
253 const struct xkb_key *key,
254 enum xkb_key_direction direction)
256 if (key != filter->key) {
257 filter->action.mods.flags &= ~ACTION_LOCK_CLEAR;
261 if (direction == XKB_KEY_DOWN) {
265 else if (--filter->refcnt > 0) {
269 state->clear_mods = filter->action.mods.mods.mask;
270 if (filter->action.mods.flags & ACTION_LOCK_CLEAR)
271 state->locked_mods &= ~filter->action.mods.mods.mask;
278 xkb_filter_mod_set_new(struct xkb_state *state,
279 const struct xkb_key *key,
280 const union xkb_action *action)
282 struct xkb_filter *filter = xkb_filter_new(state);
287 filter->func = xkb_filter_mod_set_func;
288 filter->action = *action;
290 state->set_mods = action->mods.mods.mask;
296 xkb_filter_mod_lock_func(struct xkb_state *state,
297 struct xkb_filter *filter,
298 const struct xkb_key *key,
299 enum xkb_key_direction direction)
301 if (key != filter->key)
304 if (direction == XKB_KEY_DOWN) {
308 if (--filter->refcnt > 0)
311 state->locked_mods &= ~filter->priv;
318 xkb_filter_mod_lock_new(struct xkb_state *state,
319 const struct xkb_key *key,
320 const union xkb_action *action)
322 struct xkb_filter *filter = xkb_filter_new(state);
327 filter->func = xkb_filter_mod_lock_func;
328 filter->action = *action;
329 filter->priv = state->locked_mods & action->mods.mods.mask;
331 state->locked_mods |= action->mods.mods.mask;
336 enum xkb_key_latch_state {
343 xkb_action_breaks_latch(const union xkb_action *action)
345 switch (action->type) {
346 case ACTION_TYPE_NONE:
347 case ACTION_TYPE_PTR_BUTTON:
348 case ACTION_TYPE_PTR_LOCK:
349 case ACTION_TYPE_CTRL_SET:
350 case ACTION_TYPE_CTRL_LOCK:
351 case ACTION_TYPE_KEY_REDIRECT:
352 case ACTION_TYPE_SWITCH_VT:
353 case ACTION_TYPE_TERMINATE:
361 xkb_filter_mod_latch_func(struct xkb_state *state,
362 struct xkb_filter *filter,
363 const struct xkb_key *key,
364 enum xkb_key_direction direction)
366 enum xkb_key_latch_state latch = filter->priv;
368 if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
369 /* If this is a new keypress and we're awaiting our single latched
370 * keypress, then either break the latch if any random key is pressed,
371 * or promote it to a lock or plain base set if it's the same
373 const union xkb_action *action = xkb_key_get_action(state, key);
374 if (action->type == ACTION_TYPE_MOD_LATCH &&
375 action->mods.flags == filter->action.mods.flags &&
376 action->mods.mods.mask == filter->action.mods.mods.mask) {
377 filter->action = *action;
378 if (filter->action.mods.flags & ACTION_LATCH_TO_LOCK) {
379 filter->action.type = ACTION_TYPE_MOD_LOCK;
380 filter->func = xkb_filter_mod_lock_func;
381 state->locked_mods |= filter->action.mods.mods.mask;
384 filter->action.type = ACTION_TYPE_MOD_SET;
385 filter->func = xkb_filter_mod_set_func;
386 state->set_mods = filter->action.mods.mods.mask;
389 state->latched_mods &= ~filter->action.mods.mods.mask;
393 else if (xkb_action_breaks_latch(action)) {
394 /* XXX: This may be totally broken, we might need to break the
395 * latch in the next run after this press? */
396 state->latched_mods &= ~filter->action.mods.mods.mask;
401 else if (direction == XKB_KEY_UP && key == filter->key) {
402 /* Our key got released. If we've set it to clear locks, and we
403 * currently have the same modifiers locked, then release them and
404 * don't actually latch. Else we've actually hit the latching
405 * stage, so set PENDING and move our modifier from base to
407 if (latch == NO_LATCH ||
408 ((filter->action.mods.flags & ACTION_LOCK_CLEAR) &&
409 (state->locked_mods & filter->action.mods.mods.mask) ==
410 filter->action.mods.mods.mask)) {
411 /* XXX: We might be a bit overenthusiastic about clearing
412 * mods other filters have set here? */
413 if (latch == LATCH_PENDING)
414 state->latched_mods &= ~filter->action.mods.mods.mask;
416 state->clear_mods = filter->action.mods.mods.mask;
417 state->locked_mods &= ~filter->action.mods.mods.mask;
421 latch = LATCH_PENDING;
422 state->clear_mods = filter->action.mods.mods.mask;
423 state->latched_mods |= filter->action.mods.mods.mask;
427 else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
428 /* Someone's pressed another key while we've still got the latching
429 * key held down, so keep the base modifier state active (from
430 * xkb_filter_mod_latch_new), but don't trip the latch, just clear
431 * it as soon as the modifier gets released. */
435 filter->priv = latch;
441 xkb_filter_mod_latch_new(struct xkb_state *state,
442 const struct xkb_key *key,
443 const union xkb_action *action)
445 enum xkb_key_latch_state latch = LATCH_KEY_DOWN;
446 struct xkb_filter *filter = xkb_filter_new(state);
451 filter->priv = latch;
452 filter->func = xkb_filter_mod_latch_func;
453 filter->action = *action;
455 state->set_mods = action->mods.mods.mask;
460 typedef int (*filter_action_new_func_t)(struct xkb_state *state,
461 const struct xkb_key *key,
462 const union xkb_action *action);
464 static const filter_action_new_func_t
465 filter_action_new_funcs[_ACTION_TYPE_NUM_ENTRIES] = {
466 [ACTION_TYPE_MOD_SET] = xkb_filter_mod_set_new,
467 [ACTION_TYPE_MOD_LATCH] = xkb_filter_mod_latch_new,
468 [ACTION_TYPE_MOD_LOCK] = xkb_filter_mod_lock_new,
469 [ACTION_TYPE_GROUP_SET] = xkb_filter_group_set_new,
470 [ACTION_TYPE_GROUP_LOCK] = xkb_filter_group_lock_new,
474 * Applies any relevant filters to the key, first from the list of filters
475 * that are currently active, then if no filter has claimed the key, possibly
476 * apply a new filter from the key action.
479 xkb_filter_apply_all(struct xkb_state *state,
480 const struct xkb_key *key,
481 enum xkb_key_direction direction)
483 struct xkb_filter *filter;
484 const union xkb_action *act = NULL;
487 /* First run through all the currently active filters and see if any of
488 * them have claimed this event. */
489 darray_foreach(filter, state->filters) {
492 send &= filter->func(state, filter, key, direction);
495 if (!send || direction == XKB_KEY_UP)
498 act = xkb_key_get_action(state, key);
499 if (filter_action_new_funcs[act->type])
500 send = filter_action_new_funcs[act->type](state, key, act);
503 XKB_EXPORT struct xkb_state *
504 xkb_state_new(struct xkb_keymap *keymap)
506 struct xkb_state *ret;
508 ret = calloc(sizeof(*ret), 1);
513 ret->keymap = xkb_map_ref(keymap);
518 XKB_EXPORT struct xkb_state *
519 xkb_state_ref(struct xkb_state *state)
526 xkb_state_unref(struct xkb_state *state)
528 if (--state->refcnt > 0)
531 xkb_map_unref(state->keymap);
532 darray_free(state->filters);
536 XKB_EXPORT struct xkb_keymap *
537 xkb_state_get_map(struct xkb_state *state)
539 return state->keymap;
543 * Update the LED state to match the rest of the xkb_state.
546 xkb_state_led_update_all(struct xkb_state *state)
552 for (led = 0; led < XKB_NUM_INDICATORS; led++) {
553 struct xkb_indicator_map *map = &state->keymap->indicators[led];
554 xkb_mod_mask_t mod_mask = 0;
555 uint32_t group_mask = 0;
557 if (map->which_mods & XKB_STATE_DEPRESSED)
558 mod_mask |= state->base_mods;
559 if (map->which_mods & XKB_STATE_LATCHED)
560 mod_mask |= state->latched_mods;
561 if (map->which_mods & XKB_STATE_LOCKED)
562 mod_mask |= state->locked_mods;
563 if ((map->mods.mask & mod_mask))
564 state->leds |= (1 << led);
566 if (map->which_groups & XKB_STATE_DEPRESSED)
567 group_mask |= (1 << state->base_group);
568 if (map->which_groups & XKB_STATE_LATCHED)
569 group_mask |= (1 << state->latched_group);
570 if (map->which_groups & XKB_STATE_LOCKED)
571 group_mask |= (1 << state->locked_group);
572 if ((map->groups & group_mask))
573 state->leds |= (1 << led);
576 if ((map->ctrls & state->keymap->enabled_ctrls))
577 state->leds |= (1 << led);
583 * Calculates the derived state (effective mods/group and LEDs) from an
584 * up-to-date xkb_state.
587 xkb_state_update_derived(struct xkb_state *state)
590 (state->base_mods | state->latched_mods | state->locked_mods);
591 /* FIXME: Clamp/wrap locked_group */
592 state->group = state->locked_group + state->base_group +
593 state->latched_group;
594 /* FIXME: Clamp/wrap effective group */
596 xkb_state_led_update_all(state);
600 * Given a particular key event, updates the state structure to reflect the
604 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
605 enum xkb_key_direction direction)
609 const struct xkb_key *key = XkbKey(state->keymap, kc);
614 state->clear_mods = 0;
616 xkb_filter_apply_all(state, key, direction);
618 for (i = 0, bit = 1; state->set_mods; i++, bit <<= 1) {
619 if (state->set_mods & bit) {
620 state->mod_key_count[i]++;
621 state->base_mods |= bit;
622 state->set_mods &= ~bit;
626 for (i = 0, bit = 1; state->clear_mods; i++, bit <<= 1) {
627 if (state->clear_mods & bit) {
628 state->mod_key_count[i]--;
629 if (state->mod_key_count[i] <= 0) {
630 state->base_mods &= ~bit;
631 state->mod_key_count[i] = 0;
633 state->clear_mods &= ~bit;
637 xkb_state_update_derived(state);
641 * Updates the state from a set of explicit masks as gained from
642 * xkb_state_serialize_mods and xkb_state_serialize_groups. As noted in the
643 * documentation for these functions in xkbcommon.h, this round-trip is
644 * lossy, and should only be used to update a slave state mirroring the
645 * master, e.g. in a client/server window system.
648 xkb_state_update_mask(struct xkb_state *state,
649 xkb_mod_mask_t base_mods,
650 xkb_mod_mask_t latched_mods,
651 xkb_mod_mask_t locked_mods,
652 xkb_group_index_t base_group,
653 xkb_group_index_t latched_group,
654 xkb_group_index_t locked_group)
656 xkb_mod_index_t num_mods;
659 state->base_mods = 0;
660 state->latched_mods = 0;
661 state->locked_mods = 0;
662 num_mods = xkb_map_num_mods(state->keymap);
664 for (idx = 0; idx < num_mods; idx++) {
665 xkb_mod_mask_t mod = (1 << idx);
667 state->base_mods |= mod;
668 if (latched_mods & mod)
669 state->latched_mods |= mod;
670 if (locked_mods & mod)
671 state->locked_mods |= mod;
674 state->base_group = base_group;
675 state->latched_group = latched_group;
676 state->locked_group = locked_group;
678 xkb_state_update_derived(state);
682 * Serialises the requested modifier state into an xkb_mod_mask_t, with all
683 * the same disclaimers as in xkb_state_update_mask.
685 XKB_EXPORT xkb_mod_mask_t
686 xkb_state_serialize_mods(struct xkb_state *state,
687 enum xkb_state_component type)
689 xkb_mod_mask_t ret = 0;
691 if (type == XKB_STATE_EFFECTIVE)
694 if (type & XKB_STATE_DEPRESSED)
695 ret |= state->base_mods;
696 if (type & XKB_STATE_LATCHED)
697 ret |= state->latched_mods;
698 if (type & XKB_STATE_LOCKED)
699 ret |= state->locked_mods;
705 * Serialises the requested group state, with all the same disclaimers as
706 * in xkb_state_update_mask.
708 XKB_EXPORT xkb_group_index_t
709 xkb_state_serialize_group(struct xkb_state *state,
710 enum xkb_state_component type)
712 xkb_group_index_t ret = 0;
714 if (type == XKB_STATE_EFFECTIVE)
717 if (type & XKB_STATE_DEPRESSED)
718 ret += state->base_group;
719 if (type & XKB_STATE_LATCHED)
720 ret += state->latched_group;
721 if (type & XKB_STATE_LOCKED)
722 ret += state->locked_group;
728 * Returns 1 if the given modifier is active with the specified type(s), 0 if
729 * not, or -1 if the modifier is invalid.
732 xkb_state_mod_index_is_active(struct xkb_state *state,
734 enum xkb_state_component type)
738 if (idx >= xkb_map_num_mods(state->keymap))
741 if (type & XKB_STATE_DEPRESSED)
742 ret |= (state->base_mods & (1 << idx));
743 if (type & XKB_STATE_LATCHED)
744 ret |= (state->latched_mods & (1 << idx));
745 if (type & XKB_STATE_LOCKED)
746 ret |= (state->locked_mods & (1 << idx));
752 * Helper function for xkb_state_mod_indices_are_active and
753 * xkb_state_mod_names_are_active.
756 match_mod_masks(struct xkb_state *state, enum xkb_state_match match,
759 uint32_t active = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
761 if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
764 if (match & XKB_STATE_MATCH_ANY)
765 return !!(active & wanted);
767 return (active & wanted) == wanted;
773 * Returns 1 if the modifiers are active with the specified type(s), 0 if
774 * not, or -1 if any of the modifiers are invalid.
777 xkb_state_mod_indices_are_active(struct xkb_state *state,
778 enum xkb_state_component type,
779 enum xkb_state_match match,
783 xkb_mod_index_t idx = 0;
786 xkb_mod_index_t num_mods = xkb_map_num_mods(state->keymap);
790 idx = va_arg(ap, xkb_mod_index_t);
791 if (idx == XKB_MOD_INVALID)
793 if (idx >= num_mods) {
797 wanted |= (1 << idx);
804 return match_mod_masks(state, match, wanted);
808 * Returns 1 if the given modifier is active with the specified type(s), 0 if
809 * not, or -1 if the modifier is invalid.
812 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
813 enum xkb_state_component type)
815 xkb_mod_index_t idx = xkb_map_mod_get_index(state->keymap, name);
817 if (idx == XKB_MOD_INVALID)
820 return xkb_state_mod_index_is_active(state, idx, type);
824 * Returns 1 if the modifiers are active with the specified type(s), 0 if
825 * not, or -1 if any of the modifiers are invalid.
827 XKB_EXPORT ATTR_NULL_SENTINEL int
828 xkb_state_mod_names_are_active(struct xkb_state *state,
829 enum xkb_state_component type,
830 enum xkb_state_match match,
834 xkb_mod_index_t idx = 0;
841 str = va_arg(ap, const char *);
844 idx = xkb_map_mod_get_index(state->keymap, str);
845 if (idx == XKB_MOD_INVALID) {
849 wanted |= (1 << idx);
856 return match_mod_masks(state, match, wanted);
860 * Returns 1 if the given group is active with the specified type(s), 0 if
861 * not, or -1 if the group is invalid.
864 xkb_state_group_index_is_active(struct xkb_state *state,
865 xkb_group_index_t idx,
866 enum xkb_state_component type)
870 if (idx >= xkb_map_num_groups(state->keymap))
873 if (type & XKB_STATE_DEPRESSED)
874 ret |= (state->base_group == idx);
875 if (type & XKB_STATE_LATCHED)
876 ret |= (state->latched_group == idx);
877 if (type & XKB_STATE_LOCKED)
878 ret |= (state->locked_group == idx);
884 * Returns 1 if the given modifier is active with the specified type(s), 0 if
885 * not, or -1 if the modifier is invalid.
888 xkb_state_group_name_is_active(struct xkb_state *state, const char *name,
889 enum xkb_state_component type)
891 xkb_group_index_t idx = xkb_map_group_get_index(state->keymap, name);
893 if (idx == XKB_GROUP_INVALID)
896 return xkb_state_group_index_is_active(state, idx, type);
900 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
903 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
905 if (idx >= xkb_map_num_leds(state->keymap))
908 return !!(state->leds & (1 << idx));
912 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
915 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
917 xkb_led_index_t idx = xkb_map_led_get_index(state->keymap, name);
919 if (idx == XKB_LED_INVALID)
922 return xkb_state_led_index_is_active(state, idx);