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 = XkbSA_NoAction };
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;
116 struct xkb_key *key = NULL;
118 if (XkbKeycodeInRange(state->keymap, kc))
119 key = XkbKey(state->keymap, kc);
121 if (!key || !XkbKeyHasActions(key))
124 group = xkb_key_get_group(state, kc);
125 level = xkb_key_get_level(state, kc, group);
127 return XkbKeyActionEntry(state->keymap, key, group, level);
130 static struct xkb_filter *
131 xkb_filter_new(struct xkb_state *state)
133 int old_size = darray_size(state->filters);
134 struct xkb_filter *filter = NULL, *iter;
136 darray_foreach(iter, state->filters) {
144 darray_resize0(state->filters, darray_size(state->filters) + 1);
145 filter = &darray_item(state->filters, old_size);
148 filter->state = state;
153 /***====================================================================***/
156 xkb_filter_group_set_func(struct xkb_filter *filter, xkb_keycode_t kc,
157 enum xkb_key_direction direction)
159 if (kc != filter->kc) {
160 filter->action.group.flags &= ~XkbSA_ClearLocks;
164 if (direction == XKB_KEY_DOWN) {
168 else if (--filter->refcnt > 0) {
172 if (filter->action.group.flags & XkbSA_GroupAbsolute)
173 filter->state->base_group = filter->action.group.group;
175 filter->state->base_group = -filter->action.group.group;
176 if (filter->action.group.flags & XkbSA_ClearLocks)
177 filter->state->locked_group = 0;
185 xkb_filter_group_set_new(struct xkb_state *state, xkb_keycode_t kc,
186 const union xkb_action *action)
188 struct xkb_filter *filter = xkb_filter_new(state);
190 if (!filter) /* WSGO */
193 filter->func = xkb_filter_group_set_func;
194 filter->action = *action;
196 if (action->group.flags & XkbSA_GroupAbsolute) {
197 filter->action.group.group = filter->state->base_group;
198 filter->state->base_group = action->group.group;
201 filter->state->base_group += action->group.group;
208 xkb_filter_group_lock_func(struct xkb_filter *filter, xkb_keycode_t kc,
209 enum xkb_key_direction direction)
211 if (kc != filter->kc)
214 if (direction == XKB_KEY_DOWN) {
218 if (--filter->refcnt > 0)
226 xkb_filter_group_lock_new(struct xkb_state *state, xkb_keycode_t kc,
227 const union xkb_action *action)
229 struct xkb_filter *filter = xkb_filter_new(state);
235 filter->func = xkb_filter_group_lock_func;
236 filter->action = *action;
238 if (action->group.flags & XkbSA_GroupAbsolute)
239 filter->state->locked_group = action->group.group;
241 filter->state->locked_group += action->group.group;
247 xkb_filter_mod_set_func(struct xkb_filter *filter, xkb_keycode_t kc,
248 enum xkb_key_direction direction)
250 if (kc != filter->kc) {
251 filter->action.mods.flags &= ~XkbSA_ClearLocks;
255 if (direction == XKB_KEY_DOWN) {
259 else if (--filter->refcnt > 0) {
263 filter->state->clear_mods = filter->action.mods.mods.mask;
264 if (filter->action.mods.flags & XkbSA_ClearLocks)
265 filter->state->locked_mods &= ~filter->action.mods.mods.mask;
273 xkb_filter_mod_set_new(struct xkb_state *state, xkb_keycode_t kc,
274 const union xkb_action *action)
276 struct xkb_filter *filter = xkb_filter_new(state);
278 if (!filter) /* WSGO */
281 filter->func = xkb_filter_mod_set_func;
282 filter->action = *action;
284 filter->state->set_mods = action->mods.mods.mask;
290 xkb_filter_mod_lock_func(struct xkb_filter *filter, xkb_keycode_t kc,
291 enum xkb_key_direction direction)
293 if (kc != filter->kc)
296 if (direction == XKB_KEY_DOWN) {
300 if (--filter->refcnt > 0)
303 filter->state->locked_mods &= ~filter->priv;
309 xkb_filter_mod_lock_new(struct xkb_state *state, xkb_keycode_t kc,
310 const union xkb_action *action)
312 struct xkb_filter *filter = xkb_filter_new(state);
314 if (!filter) /* WSGO */
318 filter->func = xkb_filter_mod_lock_func;
319 filter->action = *action;
320 filter->priv = state->locked_mods & action->mods.mods.mask;
321 state->locked_mods |= action->mods.mods.mask;
326 enum xkb_key_latch_state {
333 xkb_filter_mod_latch_func(struct xkb_filter *filter, xkb_keycode_t kc,
334 enum xkb_key_direction direction)
336 enum xkb_key_latch_state latch = filter->priv;
338 if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
339 /* If this is a new keypress and we're awaiting our single latched
340 * keypress, then either break the latch if any random key is pressed,
341 * or promote it to a lock or plain base set if it's the same
343 const union xkb_action *action = xkb_key_get_action(filter->state, kc);
344 if (action->type == XkbSA_LatchMods &&
345 action->mods.flags == filter->action.mods.flags &&
346 action->mods.mods.mask == filter->action.mods.mods.mask) {
347 filter->action = *action;
348 if (filter->action.mods.flags & XkbSA_LatchToLock) {
349 filter->action.type = XkbSA_LockMods;
350 filter->func = xkb_filter_mod_lock_func;
351 filter->state->locked_mods |= filter->action.mods.mods.mask;
354 filter->action.type = XkbSA_SetMods;
355 filter->func = xkb_filter_mod_set_func;
356 filter->state->set_mods = filter->action.mods.mods.mask;
359 filter->state->latched_mods &= ~filter->action.mods.mods.mask;
363 else if (((1 << action->type) & XkbSA_BreakLatch)) {
364 /* XXX: This may be totally broken, we might need to break the
365 * latch in the next run after this press? */
366 filter->state->latched_mods &= ~filter->action.mods.mods.mask;
371 else if (direction == XKB_KEY_UP && kc == filter->kc) {
372 /* Our key got released. If we've set it to clear locks, and we
373 * currently have the same modifiers locked, then release them and
374 * don't actually latch. Else we've actually hit the latching
375 * stage, so set PENDING and move our modifier from base to
377 if (latch == NO_LATCH ||
378 ((filter->action.mods.flags & XkbSA_ClearLocks) &&
379 (filter->state->locked_mods & filter->action.mods.mods.mask) ==
380 filter->action.mods.mods.mask)) {
381 /* XXX: We might be a bit overenthusiastic about clearing
382 * mods other filters have set here? */
383 if (latch == LATCH_PENDING)
384 filter->state->latched_mods &= ~filter->action.mods.mods.mask;
386 filter->state->clear_mods = filter->action.mods.mods.mask;
387 filter->state->locked_mods &= ~filter->action.mods.mods.mask;
391 latch = LATCH_PENDING;
392 filter->state->clear_mods = filter->action.mods.mods.mask;
393 filter->state->latched_mods |= filter->action.mods.mods.mask;
397 else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
398 /* Someone's pressed another key while we've still got the latching
399 * key held down, so keep the base modifier state active (from
400 * xkb_filter_mod_latch_new), but don't trip the latch, just clear
401 * it as soon as the modifier gets released. */
405 filter->priv = latch;
411 xkb_filter_mod_latch_new(struct xkb_state *state, xkb_keycode_t kc,
412 const union xkb_action *action)
414 struct xkb_filter *filter = xkb_filter_new(state);
415 enum xkb_key_latch_state latch = LATCH_KEY_DOWN;
417 if (!filter) /* WSGO */
420 filter->priv = latch;
421 filter->func = xkb_filter_mod_latch_func;
422 filter->action = *action;
424 filter->state->set_mods = action->mods.mods.mask;
430 * Applies any relevant filters to the key, first from the list of filters
431 * that are currently active, then if no filter has claimed the key, possibly
432 * apply a new filter from the key action.
435 xkb_filter_apply_all(struct xkb_state *state, xkb_keycode_t kc,
436 enum xkb_key_direction direction)
438 struct xkb_filter *filter;
439 const union xkb_action *act = NULL;
442 /* First run through all the currently active filters and see if any of
443 * them have claimed this event. */
444 darray_foreach(filter, state->filters) {
447 send &= filter->func(filter, kc, direction);
450 if (!send || direction == XKB_KEY_UP)
453 act = xkb_key_get_action(state, kc);
456 send = xkb_filter_mod_set_new(state, kc, act);
458 case XkbSA_LatchMods:
459 send = xkb_filter_mod_latch_new(state, kc, act);
462 send = xkb_filter_mod_lock_new(state, kc, act);
465 send = xkb_filter_group_set_new(state, kc, act);
468 case XkbSA_LatchGroup:
469 send = xkb_filter_mod_latch_new(state, key, act);
472 case XkbSA_LockGroup:
473 send = xkb_filter_group_lock_new(state, kc, act);
480 XKB_EXPORT struct xkb_state *
481 xkb_state_new(struct xkb_keymap *keymap)
483 struct xkb_state *ret;
488 ret = calloc(sizeof(*ret), 1);
493 ret->keymap = xkb_map_ref(keymap);
498 XKB_EXPORT struct xkb_state *
499 xkb_state_ref(struct xkb_state *state)
506 xkb_state_unref(struct xkb_state *state)
509 assert(state->refcnt >= 0);
510 if (state->refcnt > 0)
513 xkb_map_unref(state->keymap);
514 darray_free(state->filters);
518 XKB_EXPORT struct xkb_keymap *
519 xkb_state_get_map(struct xkb_state *state)
521 return state->keymap;
525 * Update the LED state to match the rest of the xkb_state.
528 xkb_state_led_update_all(struct xkb_state *state)
534 for (led = 0; led < XkbNumIndicators; led++) {
535 struct xkb_indicator_map *map = &state->keymap->indicators[led];
536 uint32_t mod_mask = 0;
537 uint32_t group_mask = 0;
539 if (!map->which_mods && !map->which_groups && !map->ctrls)
542 if (map->which_mods) {
543 if (map->which_mods & XkbIM_UseBase)
544 mod_mask |= state->base_mods;
545 if (map->which_mods & XkbIM_UseLatched)
546 mod_mask |= state->latched_mods;
547 if (map->which_mods & XkbIM_UseLocked)
548 mod_mask |= state->locked_mods;
549 if (map->which_mods & XkbIM_UseEffective)
550 mod_mask |= state->mods;
551 if ((map->mods.mask & mod_mask))
552 state->leds |= (1 << led);
554 else if (map->which_groups) {
555 if (map->which_mods & XkbIM_UseBase)
556 group_mask |= (1 << state->base_group);
557 if (map->which_mods & XkbIM_UseLatched)
558 group_mask |= (1 << state->latched_group);
559 if (map->which_mods & XkbIM_UseLocked)
560 group_mask |= (1 << state->locked_group);
561 if (map->which_mods & XkbIM_UseEffective)
562 group_mask |= (1 << state->group);
563 if ((map->groups & group_mask))
564 state->leds |= (1 << led);
566 else if (map->ctrls) {
567 if ((map->ctrls & state->keymap->enabled_ctrls))
568 state->leds |= (1 << led);
574 * Calculates the derived state (effective mods/group and LEDs) from an
575 * up-to-date xkb_state.
578 xkb_state_update_derived(struct xkb_state *state)
581 (state->base_mods | state->latched_mods | state->locked_mods);
582 /* FIXME: Clamp/wrap locked_group */
583 state->group = state->locked_group + state->base_group +
584 state->latched_group;
585 /* FIXME: Clamp/wrap effective group */
587 xkb_state_led_update_all(state);
591 * Given a particular key event, updates the state structure to reflect the
595 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
596 enum xkb_key_direction direction)
602 state->clear_mods = 0;
604 xkb_filter_apply_all(state, kc, direction);
606 for (i = 0, bit = 1; state->set_mods; i++, bit <<= 1) {
607 if (state->set_mods & bit) {
608 state->mod_key_count[i]++;
609 state->base_mods |= bit;
610 state->set_mods &= ~bit;
614 for (i = 0, bit = 1; state->clear_mods; i++, bit <<= 1) {
615 if (state->clear_mods & bit) {
616 state->mod_key_count[i]--;
617 if (state->mod_key_count[i] <= 0) {
618 state->base_mods &= ~bit;
619 state->mod_key_count[i] = 0;
621 state->clear_mods &= ~bit;
625 xkb_state_update_derived(state);
629 * Updates the state from a set of explicit masks as gained from
630 * xkb_state_serialize_mods and xkb_state_serialize_groups. As noted in the
631 * documentation for these functions in xkbcommon.h, this round-trip is
632 * lossy, and should only be used to update a slave state mirroring the
633 * master, e.g. in a client/server window system.
636 xkb_state_update_mask(struct xkb_state *state,
637 xkb_mod_mask_t base_mods,
638 xkb_mod_mask_t latched_mods,
639 xkb_mod_mask_t locked_mods,
640 xkb_group_index_t base_group,
641 xkb_group_index_t latched_group,
642 xkb_group_index_t locked_group)
646 state->base_mods = 0;
647 state->latched_mods = 0;
648 state->locked_mods = 0;
649 for (mod = 0; mod < xkb_map_num_mods(state->keymap); mod++) {
650 xkb_mod_mask_t idx = (1 << mod);
652 state->base_mods |= idx;
653 if (latched_mods & idx)
654 state->latched_mods |= idx;
655 if (locked_mods & idx)
656 state->locked_mods |= idx;
659 state->base_group = base_group;
660 state->latched_group = latched_group;
661 state->locked_group = locked_group;
663 xkb_state_update_derived(state);
667 * Serialises the requested modifier state into an xkb_mod_mask_t, with all
668 * the same disclaimers as in xkb_state_update_mask.
670 XKB_EXPORT xkb_mod_mask_t
671 xkb_state_serialize_mods(struct xkb_state *state,
672 enum xkb_state_component type)
674 xkb_mod_mask_t ret = 0;
676 if (type == XKB_STATE_EFFECTIVE)
679 if (type & XKB_STATE_DEPRESSED)
680 ret |= state->base_mods;
681 if (type & XKB_STATE_LATCHED)
682 ret |= state->latched_mods;
683 if (type & XKB_STATE_LOCKED)
684 ret |= state->locked_mods;
690 * Serialises the requested group state, with all the same disclaimers as
691 * in xkb_state_update_mask.
693 XKB_EXPORT xkb_group_index_t
694 xkb_state_serialize_group(struct xkb_state *state,
695 enum xkb_state_component type)
697 xkb_group_index_t ret = 0;
699 if (type == XKB_STATE_EFFECTIVE)
702 if (type & XKB_STATE_DEPRESSED)
703 ret += state->base_group;
704 if (type & XKB_STATE_LATCHED)
705 ret += state->latched_group;
706 if (type & XKB_STATE_LOCKED)
707 ret += state->locked_group;
713 * Returns 1 if the given modifier is active with the specified type(s), 0 if
714 * not, or -1 if the modifier is invalid.
717 xkb_state_mod_index_is_active(struct xkb_state *state,
719 enum xkb_state_component type)
723 if (idx >= xkb_map_num_mods(state->keymap))
726 if (type & XKB_STATE_DEPRESSED)
727 ret |= (state->base_mods & (1 << idx));
728 if (type & XKB_STATE_LATCHED)
729 ret |= (state->latched_mods & (1 << idx));
730 if (type & XKB_STATE_LOCKED)
731 ret |= (state->locked_mods & (1 << idx));
737 * Helper function for xkb_state_mod_indices_are_active and
738 * xkb_state_mod_names_are_active.
741 match_mod_masks(struct xkb_state *state, enum xkb_state_match match,
744 uint32_t active = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
746 if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
749 if (match & XKB_STATE_MATCH_ANY)
750 return !!(active & wanted);
752 return (active & wanted) == wanted;
758 * Returns 1 if the modifiers are active with the specified type(s), 0 if
759 * not, or -1 if any of the modifiers are invalid.
762 xkb_state_mod_indices_are_active(struct xkb_state *state,
763 enum xkb_state_component type,
764 enum xkb_state_match match,
768 xkb_mod_index_t idx = 0;
774 idx = va_arg(ap, xkb_mod_index_t);
775 if (idx == XKB_MOD_INVALID ||
776 idx >= xkb_map_num_mods(state->keymap)) {
780 wanted |= (1 << idx);
787 return match_mod_masks(state, match, wanted);
791 * Returns 1 if the given modifier is active with the specified type(s), 0 if
792 * not, or -1 if the modifier is invalid.
795 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
796 enum xkb_state_component type)
798 xkb_mod_index_t idx = xkb_map_mod_get_index(state->keymap, name);
800 if (idx == XKB_MOD_INVALID)
803 return xkb_state_mod_index_is_active(state, idx, type);
807 * Returns 1 if the modifiers are active with the specified type(s), 0 if
808 * not, or -1 if any of the modifiers are invalid.
811 xkb_state_mod_names_are_active(struct xkb_state *state,
812 enum xkb_state_component type,
813 enum xkb_state_match match,
817 xkb_mod_index_t idx = 0;
824 str = va_arg(ap, const char *);
827 idx = xkb_map_mod_get_index(state->keymap, str);
828 if (idx == XKB_MOD_INVALID) {
832 wanted |= (1 << idx);
839 return match_mod_masks(state, match, wanted);
843 * Returns 1 if the given group is active with the specified type(s), 0 if
844 * not, or -1 if the group is invalid.
847 xkb_state_group_index_is_active(struct xkb_state *state,
848 xkb_group_index_t idx,
849 enum xkb_state_component type)
853 if (idx >= xkb_map_num_groups(state->keymap))
856 if (type & XKB_STATE_DEPRESSED)
857 ret |= (state->base_group == idx);
858 if (type & XKB_STATE_LATCHED)
859 ret |= (state->latched_group == idx);
860 if (type & XKB_STATE_LOCKED)
861 ret |= (state->locked_group == idx);
867 * Returns 1 if the given modifier is active with the specified type(s), 0 if
868 * not, or -1 if the modifier is invalid.
871 xkb_state_group_name_is_active(struct xkb_state *state, const char *name,
872 enum xkb_state_component type)
874 xkb_group_index_t idx = xkb_map_group_get_index(state->keymap, name);
876 if (idx == XKB_GROUP_INVALID)
879 return xkb_state_group_index_is_active(state, idx, type);
883 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
886 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
888 if (idx >= xkb_map_num_leds(state->keymap))
891 return !!(state->leds & (1 << idx));
895 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
898 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
900 xkb_led_index_t idx = xkb_map_led_get_index(state->keymap, name);
902 if (idx == XKB_LED_INVALID)
905 return xkb_state_led_index_is_active(state, idx);