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
30 * Permission is hereby granted, free of charge, to any person obtaining a
31 * copy of this software and associated documentation files (the "Software"),
32 * to deal in the Software without restriction, including without limitation
33 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
34 * and/or sell copies of the Software, and to permit persons to whom the
35 * Software is furnished to do so, subject to the following conditions:
37 * The above copyright notice and this permission notice (including the next
38 * paragraph) shall be included in all copies or substantial portions of the
41 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
42 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
43 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
44 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
45 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
46 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
47 * DEALINGS IN THE SOFTWARE.
49 * Author: Daniel Stone <daniel@fooishbar.org>
53 * This is a bastardised version of xkbActions.c from the X server which
54 * does not support, for the moment:
55 * - AccessX sticky/debounce/etc (will come later)
56 * - pointer keys (may come later)
57 * - key redirects (unlikely)
58 * - messages (very unlikely)
67 struct xkb_state *state;
68 union xkb_action action;
71 int (*func)(struct xkb_filter *filter, xkb_keycode_t kc,
72 enum xkb_key_direction direction);
74 struct xkb_filter *next;
78 xkb_group_index_t base_group; /**< depressed */
79 xkb_group_index_t latched_group;
80 xkb_group_index_t locked_group;
81 xkb_group_index_t group; /**< effective */
83 xkb_mod_mask_t base_mods; /**< depressed */
84 xkb_mod_mask_t latched_mods;
85 xkb_mod_mask_t locked_mods;
86 xkb_mod_mask_t mods; /**< effective */
89 * At each event, we accumulate all the needed modifications to the base
90 * modifiers, and apply them at the end. These keep track of this state.
92 xkb_mod_mask_t set_mods;
93 xkb_mod_mask_t clear_mods;
95 * We mustn't clear a base modifier if there's another depressed key
96 * which affects it, e.g. given this sequence
97 * < Left Shift down, Right Shift down, Left Shift Up >
98 * the modifier should still be set. This keeps the count.
100 int16_t mod_key_count[sizeof(xkb_mod_mask_t) * 8];
105 darray(struct xkb_filter) filters;
106 struct xkb_keymap *keymap;
109 static const union xkb_action fake = { .type = ACTION_TYPE_NONE };
111 static const union xkb_action *
112 xkb_key_get_action(struct xkb_state *state, xkb_keycode_t kc)
114 xkb_group_index_t group;
115 xkb_level_index_t level;
118 key = XkbKey(state->keymap, kc);
123 group = xkb_key_get_group(state, kc);
124 if (group == XKB_GROUP_INVALID)
127 level = xkb_key_get_level(state, kc, group);
128 if (level == XKB_LEVEL_INVALID)
131 return XkbKeyActionEntry(key, group, level);
134 static struct xkb_filter *
135 xkb_filter_new(struct xkb_state *state)
137 int old_size = darray_size(state->filters);
138 struct xkb_filter *filter = NULL, *iter;
140 darray_foreach(iter, state->filters) {
148 darray_resize0(state->filters, darray_size(state->filters) + 1);
149 filter = &darray_item(state->filters, old_size);
152 filter->state = state;
157 /***====================================================================***/
160 xkb_filter_group_set_func(struct xkb_filter *filter, xkb_keycode_t kc,
161 enum xkb_key_direction direction)
163 if (kc != filter->kc) {
164 filter->action.group.flags &= ~ACTION_LOCK_CLEAR;
168 if (direction == XKB_KEY_DOWN) {
172 else if (--filter->refcnt > 0) {
176 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
177 filter->state->base_group = filter->action.group.group;
179 filter->state->base_group = -filter->action.group.group;
180 if (filter->action.group.flags & ACTION_LOCK_CLEAR)
181 filter->state->locked_group = 0;
189 xkb_filter_group_set_new(struct xkb_state *state, xkb_keycode_t kc,
190 const union xkb_action *action)
192 struct xkb_filter *filter = xkb_filter_new(state);
194 if (!filter) /* WSGO */
197 filter->func = xkb_filter_group_set_func;
198 filter->action = *action;
200 if (action->group.flags & ACTION_ABSOLUTE_SWITCH) {
201 filter->action.group.group = filter->state->base_group;
202 filter->state->base_group = action->group.group;
205 filter->state->base_group += action->group.group;
212 xkb_filter_group_lock_func(struct xkb_filter *filter, xkb_keycode_t kc,
213 enum xkb_key_direction direction)
215 if (kc != filter->kc)
218 if (direction == XKB_KEY_DOWN) {
222 if (--filter->refcnt > 0)
230 xkb_filter_group_lock_new(struct xkb_state *state, xkb_keycode_t kc,
231 const union xkb_action *action)
233 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 filter->state->locked_group = action->group.group;
245 filter->state->locked_group += action->group.group;
251 xkb_filter_mod_set_func(struct xkb_filter *filter, xkb_keycode_t kc,
252 enum xkb_key_direction direction)
254 if (kc != filter->kc) {
255 filter->action.mods.flags &= ~ACTION_LOCK_CLEAR;
259 if (direction == XKB_KEY_DOWN) {
263 else if (--filter->refcnt > 0) {
267 filter->state->clear_mods = filter->action.mods.mods.mask;
268 if (filter->action.mods.flags & ACTION_LOCK_CLEAR)
269 filter->state->locked_mods &= ~filter->action.mods.mods.mask;
277 xkb_filter_mod_set_new(struct xkb_state *state, xkb_keycode_t kc,
278 const union xkb_action *action)
280 struct xkb_filter *filter = xkb_filter_new(state);
282 if (!filter) /* WSGO */
285 filter->func = xkb_filter_mod_set_func;
286 filter->action = *action;
288 filter->state->set_mods = action->mods.mods.mask;
294 xkb_filter_mod_lock_func(struct xkb_filter *filter, xkb_keycode_t kc,
295 enum xkb_key_direction direction)
297 if (kc != filter->kc)
300 if (direction == XKB_KEY_DOWN) {
304 if (--filter->refcnt > 0)
307 filter->state->locked_mods &= ~filter->priv;
313 xkb_filter_mod_lock_new(struct xkb_state *state, xkb_keycode_t kc,
314 const union xkb_action *action)
316 struct xkb_filter *filter = xkb_filter_new(state);
318 if (!filter) /* WSGO */
322 filter->func = xkb_filter_mod_lock_func;
323 filter->action = *action;
324 filter->priv = state->locked_mods & action->mods.mods.mask;
325 state->locked_mods |= action->mods.mods.mask;
330 enum xkb_key_latch_state {
337 xkb_action_breaks_latch(const union xkb_action *action)
339 switch (action->type) {
340 case ACTION_TYPE_NONE:
341 case ACTION_TYPE_PTR_BUTTON:
342 case ACTION_TYPE_PTR_LOCK:
343 case ACTION_TYPE_CTRL_SET:
344 case ACTION_TYPE_CTRL_LOCK:
345 case ACTION_TYPE_KEY_REDIRECT:
346 case ACTION_TYPE_SWITCH_VT:
347 case ACTION_TYPE_TERMINATE:
355 xkb_filter_mod_latch_func(struct xkb_filter *filter, xkb_keycode_t kc,
356 enum xkb_key_direction direction)
358 enum xkb_key_latch_state latch = filter->priv;
360 if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
361 /* If this is a new keypress and we're awaiting our single latched
362 * keypress, then either break the latch if any random key is pressed,
363 * or promote it to a lock or plain base set if it's the same
365 const union xkb_action *action = xkb_key_get_action(filter->state, kc);
366 if (action->type == ACTION_TYPE_MOD_LATCH &&
367 action->mods.flags == filter->action.mods.flags &&
368 action->mods.mods.mask == filter->action.mods.mods.mask) {
369 filter->action = *action;
370 if (filter->action.mods.flags & ACTION_LATCH_TO_LOCK) {
371 filter->action.type = ACTION_TYPE_MOD_LOCK;
372 filter->func = xkb_filter_mod_lock_func;
373 filter->state->locked_mods |= filter->action.mods.mods.mask;
376 filter->action.type = ACTION_TYPE_MOD_SET;
377 filter->func = xkb_filter_mod_set_func;
378 filter->state->set_mods = filter->action.mods.mods.mask;
381 filter->state->latched_mods &= ~filter->action.mods.mods.mask;
385 else if (xkb_action_breaks_latch(action)) {
386 /* XXX: This may be totally broken, we might need to break the
387 * latch in the next run after this press? */
388 filter->state->latched_mods &= ~filter->action.mods.mods.mask;
393 else if (direction == XKB_KEY_UP && kc == filter->kc) {
394 /* Our key got released. If we've set it to clear locks, and we
395 * currently have the same modifiers locked, then release them and
396 * don't actually latch. Else we've actually hit the latching
397 * stage, so set PENDING and move our modifier from base to
399 if (latch == NO_LATCH ||
400 ((filter->action.mods.flags & ACTION_LOCK_CLEAR) &&
401 (filter->state->locked_mods & filter->action.mods.mods.mask) ==
402 filter->action.mods.mods.mask)) {
403 /* XXX: We might be a bit overenthusiastic about clearing
404 * mods other filters have set here? */
405 if (latch == LATCH_PENDING)
406 filter->state->latched_mods &= ~filter->action.mods.mods.mask;
408 filter->state->clear_mods = filter->action.mods.mods.mask;
409 filter->state->locked_mods &= ~filter->action.mods.mods.mask;
413 latch = LATCH_PENDING;
414 filter->state->clear_mods = filter->action.mods.mods.mask;
415 filter->state->latched_mods |= filter->action.mods.mods.mask;
419 else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
420 /* Someone's pressed another key while we've still got the latching
421 * key held down, so keep the base modifier state active (from
422 * xkb_filter_mod_latch_new), but don't trip the latch, just clear
423 * it as soon as the modifier gets released. */
427 filter->priv = latch;
433 xkb_filter_mod_latch_new(struct xkb_state *state, xkb_keycode_t kc,
434 const union xkb_action *action)
436 struct xkb_filter *filter = xkb_filter_new(state);
437 enum xkb_key_latch_state latch = LATCH_KEY_DOWN;
439 if (!filter) /* WSGO */
442 filter->priv = latch;
443 filter->func = xkb_filter_mod_latch_func;
444 filter->action = *action;
446 filter->state->set_mods = action->mods.mods.mask;
452 * Applies any relevant filters to the key, first from the list of filters
453 * that are currently active, then if no filter has claimed the key, possibly
454 * apply a new filter from the key action.
457 xkb_filter_apply_all(struct xkb_state *state, xkb_keycode_t kc,
458 enum xkb_key_direction direction)
460 struct xkb_filter *filter;
461 const union xkb_action *act = NULL;
464 /* First run through all the currently active filters and see if any of
465 * them have claimed this event. */
466 darray_foreach(filter, state->filters) {
469 send &= filter->func(filter, kc, direction);
472 if (!send || direction == XKB_KEY_UP)
475 act = xkb_key_get_action(state, kc);
477 case ACTION_TYPE_MOD_SET:
478 send = xkb_filter_mod_set_new(state, kc, act);
480 case ACTION_TYPE_MOD_LATCH:
481 send = xkb_filter_mod_latch_new(state, kc, act);
483 case ACTION_TYPE_MOD_LOCK:
484 send = xkb_filter_mod_lock_new(state, kc, act);
486 case ACTION_TYPE_GROUP_SET:
487 send = xkb_filter_group_set_new(state, kc, act);
490 case ACTION_TYPE_GROUP_LATCH:
491 send = xkb_filter_mod_latch_new(state, key, act);
494 case ACTION_TYPE_GROUP_LOCK:
495 send = xkb_filter_group_lock_new(state, kc, act);
504 XKB_EXPORT struct xkb_state *
505 xkb_state_new(struct xkb_keymap *keymap)
507 struct xkb_state *ret;
509 ret = calloc(sizeof(*ret), 1);
514 ret->keymap = xkb_map_ref(keymap);
519 XKB_EXPORT struct xkb_state *
520 xkb_state_ref(struct xkb_state *state)
527 xkb_state_unref(struct xkb_state *state)
529 if (--state->refcnt > 0)
532 xkb_map_unref(state->keymap);
533 darray_free(state->filters);
537 XKB_EXPORT struct xkb_keymap *
538 xkb_state_get_map(struct xkb_state *state)
540 return state->keymap;
544 * Update the LED state to match the rest of the xkb_state.
547 xkb_state_led_update_all(struct xkb_state *state)
553 for (led = 0; led < XKB_NUM_INDICATORS; led++) {
554 struct xkb_indicator_map *map = &state->keymap->indicators[led];
555 xkb_mod_mask_t mod_mask = 0;
556 uint32_t group_mask = 0;
558 if (map->which_mods & XKB_STATE_DEPRESSED)
559 mod_mask |= state->base_mods;
560 if (map->which_mods & XKB_STATE_LATCHED)
561 mod_mask |= state->latched_mods;
562 if (map->which_mods & XKB_STATE_LOCKED)
563 mod_mask |= state->locked_mods;
564 if ((map->mods.mask & mod_mask))
565 state->leds |= (1 << led);
567 if (map->which_groups & XKB_STATE_DEPRESSED)
568 group_mask |= (1 << state->base_group);
569 if (map->which_groups & XKB_STATE_LATCHED)
570 group_mask |= (1 << state->latched_group);
571 if (map->which_groups & XKB_STATE_LOCKED)
572 group_mask |= (1 << state->locked_group);
573 if ((map->groups & group_mask))
574 state->leds |= (1 << led);
577 if ((map->ctrls & state->keymap->enabled_ctrls))
578 state->leds |= (1 << led);
584 * Calculates the derived state (effective mods/group and LEDs) from an
585 * up-to-date xkb_state.
588 xkb_state_update_derived(struct xkb_state *state)
591 (state->base_mods | state->latched_mods | state->locked_mods);
592 /* FIXME: Clamp/wrap locked_group */
593 state->group = state->locked_group + state->base_group +
594 state->latched_group;
595 /* FIXME: Clamp/wrap effective group */
597 xkb_state_led_update_all(state);
601 * Given a particular key event, updates the state structure to reflect the
605 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
606 enum xkb_key_direction direction)
611 if (!XkbKeycodeInRange(state->keymap, kc))
615 state->clear_mods = 0;
617 xkb_filter_apply_all(state, kc, direction);
619 for (i = 0, bit = 1; state->set_mods; i++, bit <<= 1) {
620 if (state->set_mods & bit) {
621 state->mod_key_count[i]++;
622 state->base_mods |= bit;
623 state->set_mods &= ~bit;
627 for (i = 0, bit = 1; state->clear_mods; i++, bit <<= 1) {
628 if (state->clear_mods & bit) {
629 state->mod_key_count[i]--;
630 if (state->mod_key_count[i] <= 0) {
631 state->base_mods &= ~bit;
632 state->mod_key_count[i] = 0;
634 state->clear_mods &= ~bit;
638 xkb_state_update_derived(state);
642 * Updates the state from a set of explicit masks as gained from
643 * xkb_state_serialize_mods and xkb_state_serialize_groups. As noted in the
644 * documentation for these functions in xkbcommon.h, this round-trip is
645 * lossy, and should only be used to update a slave state mirroring the
646 * master, e.g. in a client/server window system.
649 xkb_state_update_mask(struct xkb_state *state,
650 xkb_mod_mask_t base_mods,
651 xkb_mod_mask_t latched_mods,
652 xkb_mod_mask_t locked_mods,
653 xkb_group_index_t base_group,
654 xkb_group_index_t latched_group,
655 xkb_group_index_t locked_group)
657 xkb_mod_index_t num_mods;
660 state->base_mods = 0;
661 state->latched_mods = 0;
662 state->locked_mods = 0;
663 num_mods = xkb_map_num_mods(state->keymap);
665 for (idx = 0; idx < num_mods; idx++) {
666 xkb_mod_mask_t mod = (1 << idx);
668 state->base_mods |= mod;
669 if (latched_mods & mod)
670 state->latched_mods |= mod;
671 if (locked_mods & mod)
672 state->locked_mods |= mod;
675 state->base_group = base_group;
676 state->latched_group = latched_group;
677 state->locked_group = locked_group;
679 xkb_state_update_derived(state);
683 * Serialises the requested modifier state into an xkb_mod_mask_t, with all
684 * the same disclaimers as in xkb_state_update_mask.
686 XKB_EXPORT xkb_mod_mask_t
687 xkb_state_serialize_mods(struct xkb_state *state,
688 enum xkb_state_component type)
690 xkb_mod_mask_t ret = 0;
692 if (type == XKB_STATE_EFFECTIVE)
695 if (type & XKB_STATE_DEPRESSED)
696 ret |= state->base_mods;
697 if (type & XKB_STATE_LATCHED)
698 ret |= state->latched_mods;
699 if (type & XKB_STATE_LOCKED)
700 ret |= state->locked_mods;
706 * Serialises the requested group state, with all the same disclaimers as
707 * in xkb_state_update_mask.
709 XKB_EXPORT xkb_group_index_t
710 xkb_state_serialize_group(struct xkb_state *state,
711 enum xkb_state_component type)
713 xkb_group_index_t ret = 0;
715 if (type == XKB_STATE_EFFECTIVE)
718 if (type & XKB_STATE_DEPRESSED)
719 ret += state->base_group;
720 if (type & XKB_STATE_LATCHED)
721 ret += state->latched_group;
722 if (type & XKB_STATE_LOCKED)
723 ret += state->locked_group;
729 * Returns 1 if the given modifier is active with the specified type(s), 0 if
730 * not, or -1 if the modifier is invalid.
733 xkb_state_mod_index_is_active(struct xkb_state *state,
735 enum xkb_state_component type)
739 if (idx >= xkb_map_num_mods(state->keymap))
742 if (type & XKB_STATE_DEPRESSED)
743 ret |= (state->base_mods & (1 << idx));
744 if (type & XKB_STATE_LATCHED)
745 ret |= (state->latched_mods & (1 << idx));
746 if (type & XKB_STATE_LOCKED)
747 ret |= (state->locked_mods & (1 << idx));
753 * Helper function for xkb_state_mod_indices_are_active and
754 * xkb_state_mod_names_are_active.
757 match_mod_masks(struct xkb_state *state, enum xkb_state_match match,
760 uint32_t active = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
762 if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
765 if (match & XKB_STATE_MATCH_ANY)
766 return !!(active & wanted);
768 return (active & wanted) == wanted;
774 * Returns 1 if the modifiers are active with the specified type(s), 0 if
775 * not, or -1 if any of the modifiers are invalid.
778 xkb_state_mod_indices_are_active(struct xkb_state *state,
779 enum xkb_state_component type,
780 enum xkb_state_match match,
784 xkb_mod_index_t idx = 0;
787 xkb_mod_index_t num_mods = xkb_map_num_mods(state->keymap);
791 idx = va_arg(ap, xkb_mod_index_t);
792 if (idx == XKB_MOD_INVALID)
794 if (idx >= num_mods) {
798 wanted |= (1 << idx);
805 return match_mod_masks(state, match, wanted);
809 * Returns 1 if the given modifier is active with the specified type(s), 0 if
810 * not, or -1 if the modifier is invalid.
813 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
814 enum xkb_state_component type)
816 xkb_mod_index_t idx = xkb_map_mod_get_index(state->keymap, name);
818 if (idx == XKB_MOD_INVALID)
821 return xkb_state_mod_index_is_active(state, idx, type);
825 * Returns 1 if the modifiers are active with the specified type(s), 0 if
826 * not, or -1 if any of the modifiers are invalid.
828 XKB_EXPORT ATTR_NULL_SENTINEL int
829 xkb_state_mod_names_are_active(struct xkb_state *state,
830 enum xkb_state_component type,
831 enum xkb_state_match match,
835 xkb_mod_index_t idx = 0;
842 str = va_arg(ap, const char *);
845 idx = xkb_map_mod_get_index(state->keymap, str);
846 if (idx == XKB_MOD_INVALID) {
850 wanted |= (1 << idx);
857 return match_mod_masks(state, match, wanted);
861 * Returns 1 if the given group is active with the specified type(s), 0 if
862 * not, or -1 if the group is invalid.
865 xkb_state_group_index_is_active(struct xkb_state *state,
866 xkb_group_index_t idx,
867 enum xkb_state_component type)
871 if (idx >= xkb_map_num_groups(state->keymap))
874 if (type & XKB_STATE_DEPRESSED)
875 ret |= (state->base_group == idx);
876 if (type & XKB_STATE_LATCHED)
877 ret |= (state->latched_group == idx);
878 if (type & XKB_STATE_LOCKED)
879 ret |= (state->locked_group == idx);
885 * Returns 1 if the given modifier is active with the specified type(s), 0 if
886 * not, or -1 if the modifier is invalid.
889 xkb_state_group_name_is_active(struct xkb_state *state, const char *name,
890 enum xkb_state_component type)
892 xkb_group_index_t idx = xkb_map_group_get_index(state->keymap, name);
894 if (idx == XKB_GROUP_INVALID)
897 return xkb_state_group_index_is_active(state, idx, type);
901 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
904 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
906 if (idx >= xkb_map_num_leds(state->keymap))
909 return !!(state->leds & (1 << idx));
913 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
916 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
918 xkb_led_index_t idx = xkb_map_led_get_index(state->keymap, name);
920 if (idx == XKB_LED_INVALID)
923 return xkb_state_led_index_is_active(state, idx);