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 union xkb_action *
110 xkb_key_get_action(struct xkb_state *state, xkb_keycode_t kc)
112 unsigned int group, level;
113 struct xkb_key *key = NULL;
115 if (XkbKeycodeInRange(state->keymap, kc))
116 key = XkbKey(state->keymap, kc);
118 if (!key || !XkbKeyHasActions(key)) {
119 static union xkb_action fake;
120 memset(&fake, 0, sizeof(fake));
121 fake.type = XkbSA_NoAction;
125 group = xkb_key_get_group(state, kc);
126 level = xkb_key_get_level(state, kc, group);
128 return XkbKeyActionEntry(state->keymap, key, group, level);
131 static struct xkb_filter *
132 xkb_filter_new(struct xkb_state *state)
134 int old_size = darray_size(state->filters);
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, old_size);
149 filter->state = state;
154 /***====================================================================***/
157 xkb_filter_group_set_func(struct xkb_filter *filter, xkb_keycode_t kc,
158 enum xkb_key_direction direction)
160 if (kc != filter->kc) {
161 filter->action.group.flags &= ~XkbSA_ClearLocks;
165 if (direction == XKB_KEY_DOWN) {
169 else if (--filter->refcnt > 0) {
173 if (filter->action.group.flags & XkbSA_GroupAbsolute)
174 filter->state->base_group = filter->action.group.group;
176 filter->state->base_group = -filter->action.group.group;
177 if (filter->action.group.flags & XkbSA_ClearLocks)
178 filter->state->locked_group = 0;
186 xkb_filter_group_set_new(struct xkb_state *state, xkb_keycode_t kc,
187 union xkb_action *action)
189 struct xkb_filter *filter = xkb_filter_new(state);
191 if (!filter) /* WSGO */
194 filter->func = xkb_filter_group_set_func;
195 filter->action = *action;
197 if (action->group.flags & XkbSA_GroupAbsolute) {
198 filter->action.group.group = filter->state->base_group;
199 filter->state->base_group = action->group.group;
202 filter->state->base_group += action->group.group;
209 xkb_filter_group_lock_func(struct xkb_filter *filter, xkb_keycode_t kc,
210 enum xkb_key_direction direction)
212 if (kc != filter->kc)
215 if (direction == XKB_KEY_DOWN) {
219 if (--filter->refcnt > 0)
227 xkb_filter_group_lock_new(struct xkb_state *state, xkb_keycode_t kc,
228 union xkb_action *action)
230 struct xkb_filter *filter = xkb_filter_new(state);
236 filter->func = xkb_filter_group_lock_func;
237 filter->action = *action;
239 if (action->group.flags & XkbSA_GroupAbsolute)
240 filter->state->locked_group = action->group.group;
242 filter->state->locked_group += action->group.group;
248 xkb_filter_mod_set_func(struct xkb_filter *filter, xkb_keycode_t kc,
249 enum xkb_key_direction direction)
251 if (kc != filter->kc) {
252 filter->action.mods.flags &= ~XkbSA_ClearLocks;
256 if (direction == XKB_KEY_DOWN) {
260 else if (--filter->refcnt > 0) {
264 filter->state->clear_mods = filter->action.mods.mask;
265 if (filter->action.mods.flags & XkbSA_ClearLocks)
266 filter->state->locked_mods &= ~filter->action.mods.mask;
274 xkb_filter_mod_set_new(struct xkb_state *state, xkb_keycode_t kc,
275 union xkb_action *action)
277 struct xkb_filter *filter = xkb_filter_new(state);
279 if (!filter) /* WSGO */
282 filter->func = xkb_filter_mod_set_func;
283 filter->action = *action;
285 filter->state->set_mods = action->mods.mask;
291 xkb_filter_mod_lock_func(struct xkb_filter *filter, xkb_keycode_t kc,
292 enum xkb_key_direction direction)
294 if (kc != filter->kc)
297 if (direction == XKB_KEY_DOWN) {
301 if (--filter->refcnt > 0)
304 filter->state->locked_mods &= ~filter->priv;
310 xkb_filter_mod_lock_new(struct xkb_state *state, xkb_keycode_t kc,
311 union xkb_action *action)
313 struct xkb_filter *filter = xkb_filter_new(state);
315 if (!filter) /* WSGO */
319 filter->func = xkb_filter_mod_lock_func;
320 filter->action = *action;
321 filter->priv = state->locked_mods & action->mods.mask;
322 state->locked_mods |= action->mods.mask;
327 enum xkb_key_latch_state {
334 xkb_filter_mod_latch_func(struct xkb_filter *filter, xkb_keycode_t kc,
335 enum xkb_key_direction direction)
337 enum xkb_key_latch_state latch = filter->priv;
339 if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
340 /* If this is a new keypress and we're awaiting our single latched
341 * keypress, then either break the latch if any random key is pressed,
342 * or promote it to a lock or plain base set if it's the same
344 union xkb_action *action = xkb_key_get_action(filter->state, kc);
345 if (action->type == XkbSA_LatchMods &&
346 action->mods.flags == filter->action.mods.flags &&
347 action->mods.mask == filter->action.mods.mask) {
348 filter->action = *action;
349 if (filter->action.mods.flags & XkbSA_LatchToLock) {
350 filter->action.type = XkbSA_LockMods;
351 filter->func = xkb_filter_mod_lock_func;
352 filter->state->locked_mods |= filter->action.mods.mask;
355 filter->action.type = XkbSA_SetMods;
356 filter->func = xkb_filter_mod_set_func;
357 filter->state->set_mods = filter->action.mods.mask;
360 filter->state->latched_mods &= ~filter->action.mods.mask;
364 else if (((1 << action->type) & XkbSA_BreakLatch)) {
365 /* XXX: This may be totally broken, we might need to break the
366 * latch in the next run after this press? */
367 filter->state->latched_mods &= ~filter->action.mods.mask;
372 else if (direction == XKB_KEY_UP && kc == filter->kc) {
373 /* Our key got released. If we've set it to clear locks, and we
374 * currently have the same modifiers locked, then release them and
375 * don't actually latch. Else we've actually hit the latching
376 * stage, so set PENDING and move our modifier from base to
378 if (latch == NO_LATCH ||
379 ((filter->action.mods.flags & XkbSA_ClearLocks) &&
380 (filter->state->locked_mods & filter->action.mods.mask) ==
381 filter->action.mods.mask)) {
382 /* XXX: We might be a bit overenthusiastic about clearing
383 * mods other filters have set here? */
384 if (latch == LATCH_PENDING)
385 filter->state->latched_mods &= ~filter->action.mods.mask;
387 filter->state->clear_mods = filter->action.mods.mask;
388 filter->state->locked_mods &= ~filter->action.mods.mask;
392 latch = LATCH_PENDING;
393 filter->state->clear_mods = filter->action.mods.mask;
394 filter->state->latched_mods |= filter->action.mods.mask;
398 else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
399 /* Someone's pressed another key while we've still got the latching
400 * key held down, so keep the base modifier state active (from
401 * xkb_filter_mod_latch_new), but don't trip the latch, just clear
402 * it as soon as the modifier gets released. */
406 filter->priv = latch;
412 xkb_filter_mod_latch_new(struct xkb_state *state, xkb_keycode_t kc,
413 union xkb_action *action)
415 struct xkb_filter *filter = xkb_filter_new(state);
416 enum xkb_key_latch_state latch = LATCH_KEY_DOWN;
418 if (!filter) /* WSGO */
421 filter->priv = latch;
422 filter->func = xkb_filter_mod_latch_func;
423 filter->action = *action;
425 filter->state->set_mods = action->mods.mask;
431 * Applies any relevant filters to the key, first from the list of filters
432 * that are currently active, then if no filter has claimed the key, possibly
433 * apply a new filter from the key action.
436 xkb_filter_apply_all(struct xkb_state *state, xkb_keycode_t kc,
437 enum xkb_key_direction direction)
439 struct xkb_filter *filter;
440 union xkb_action *act = NULL;
443 /* First run through all the currently active filters and see if any of
444 * them have claimed this event. */
445 darray_foreach(filter, state->filters) {
448 send &= filter->func(filter, kc, direction);
451 if (!send || direction == XKB_KEY_UP)
454 act = xkb_key_get_action(state, kc);
457 send = xkb_filter_mod_set_new(state, kc, act);
459 case XkbSA_LatchMods:
460 send = xkb_filter_mod_latch_new(state, kc, act);
463 send = xkb_filter_mod_lock_new(state, kc, act);
466 send = xkb_filter_group_set_new(state, kc, act);
469 case XkbSA_LatchGroup:
470 send = xkb_filter_mod_latch_new(state, key, act);
473 case XkbSA_LockGroup:
474 send = xkb_filter_group_lock_new(state, kc, act);
481 _X_EXPORT struct xkb_state *
482 xkb_state_new(struct xkb_keymap *keymap)
484 struct xkb_state *ret;
489 ret = calloc(sizeof(*ret), 1);
494 ret->keymap = xkb_map_ref(keymap);
499 _X_EXPORT struct xkb_state *
500 xkb_state_ref(struct xkb_state *state)
507 xkb_state_unref(struct xkb_state *state)
510 assert(state->refcnt >= 0);
511 if (state->refcnt > 0)
514 xkb_map_unref(state->keymap);
515 darray_free(state->filters);
519 _X_EXPORT struct xkb_keymap *
520 xkb_state_get_map(struct xkb_state *state)
522 return state->keymap;
526 * Update the LED state to match the rest of the xkb_state.
529 xkb_state_led_update_all(struct xkb_state *state)
535 for (led = 0; led < XkbNumIndicators; led++) {
536 struct xkb_indicator_map *map = &state->keymap->indicators[led];
537 uint32_t mod_mask = 0;
538 uint32_t group_mask = 0;
540 if (!map->which_mods && !map->which_groups && !map->ctrls)
543 if (map->which_mods) {
544 if (map->which_mods & XkbIM_UseBase)
545 mod_mask |= state->base_mods;
546 if (map->which_mods & XkbIM_UseLatched)
547 mod_mask |= state->latched_mods;
548 if (map->which_mods & XkbIM_UseLocked)
549 mod_mask |= state->locked_mods;
550 if (map->which_mods & XkbIM_UseEffective)
551 mod_mask |= state->mods;
552 if ((map->mods.mask & mod_mask))
553 state->leds |= (1 << led);
555 else if (map->which_groups) {
556 if (map->which_mods & XkbIM_UseBase)
557 group_mask |= (1 << state->base_group);
558 if (map->which_mods & XkbIM_UseLatched)
559 group_mask |= (1 << state->latched_group);
560 if (map->which_mods & XkbIM_UseLocked)
561 group_mask |= (1 << state->locked_group);
562 if (map->which_mods & XkbIM_UseEffective)
563 group_mask |= (1 << state->group);
564 if ((map->groups & group_mask))
565 state->leds |= (1 << led);
567 else if (map->ctrls) {
568 if ((map->ctrls & state->keymap->enabled_ctrls))
569 state->leds |= (1 << led);
575 * Calculates the derived state (effective mods/group and LEDs) from an
576 * up-to-date xkb_state.
579 xkb_state_update_derived(struct xkb_state *state)
582 (state->base_mods | state->latched_mods | state->locked_mods);
583 /* FIXME: Clamp/wrap locked_group */
584 state->group = state->locked_group + state->base_group +
585 state->latched_group;
586 /* FIXME: Clamp/wrap effective group */
588 xkb_state_led_update_all(state);
592 * Given a particular key event, updates the state structure to reflect the
596 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
597 enum xkb_key_direction direction)
603 state->clear_mods = 0;
605 xkb_filter_apply_all(state, kc, direction);
607 for (i = 0, bit = 1; state->set_mods; i++, bit <<= 1) {
608 if (state->set_mods & bit) {
609 state->mod_key_count[i]++;
610 state->base_mods |= bit;
611 state->set_mods &= ~bit;
615 for (i = 0, bit = 1; state->clear_mods; i++, bit <<= 1) {
616 if (state->clear_mods & bit) {
617 state->mod_key_count[i]--;
618 if (state->mod_key_count[i] <= 0) {
619 state->base_mods &= ~bit;
620 state->mod_key_count[i] = 0;
622 state->clear_mods &= ~bit;
626 xkb_state_update_derived(state);
630 * Updates the state from a set of explicit masks as gained from
631 * xkb_state_serialize_mods and xkb_state_serialize_groups. As noted in the
632 * documentation for these functions in xkbcommon.h, this round-trip is
633 * lossy, and should only be used to update a slave state mirroring the
634 * master, e.g. in a client/server window system.
637 xkb_state_update_mask(struct xkb_state *state,
638 xkb_mod_mask_t base_mods,
639 xkb_mod_mask_t latched_mods,
640 xkb_mod_mask_t locked_mods,
641 xkb_group_index_t base_group,
642 xkb_group_index_t latched_group,
643 xkb_group_index_t locked_group)
647 state->base_mods = 0;
648 state->latched_mods = 0;
649 state->locked_mods = 0;
650 for (mod = 0; mod < xkb_map_num_mods(state->keymap); mod++) {
651 xkb_mod_mask_t idx = (1 << mod);
653 state->base_mods |= idx;
654 if (latched_mods & idx)
655 state->latched_mods |= idx;
656 if (locked_mods & idx)
657 state->locked_mods |= idx;
660 state->base_group = base_group;
661 state->latched_group = latched_group;
662 state->locked_group = locked_group;
664 xkb_state_update_derived(state);
668 * Serialises the requested modifier state into an xkb_mod_mask_t, with all
669 * the same disclaimers as in xkb_state_update_mask.
671 _X_EXPORT xkb_mod_mask_t
672 xkb_state_serialize_mods(struct xkb_state *state,
673 enum xkb_state_component type)
675 xkb_mod_mask_t ret = 0;
677 if (type == XKB_STATE_EFFECTIVE)
680 if (type & XKB_STATE_DEPRESSED)
681 ret |= state->base_mods;
682 if (type & XKB_STATE_LATCHED)
683 ret |= state->latched_mods;
684 if (type & XKB_STATE_LOCKED)
685 ret |= state->locked_mods;
691 * Serialises the requested group state, with all the same disclaimers as
692 * in xkb_state_update_mask.
694 _X_EXPORT xkb_group_index_t
695 xkb_state_serialize_group(struct xkb_state *state,
696 enum xkb_state_component type)
698 xkb_group_index_t ret = 0;
700 if (type == XKB_STATE_EFFECTIVE)
703 if (type & XKB_STATE_DEPRESSED)
704 ret += state->base_group;
705 if (type & XKB_STATE_LATCHED)
706 ret += state->latched_group;
707 if (type & XKB_STATE_LOCKED)
708 ret += state->locked_group;
714 * Returns 1 if the given modifier is active with the specified type(s), 0 if
715 * not, or -1 if the modifier is invalid.
718 xkb_state_mod_index_is_active(struct xkb_state *state,
720 enum xkb_state_component type)
724 if (idx >= xkb_map_num_mods(state->keymap))
727 if (type & XKB_STATE_DEPRESSED)
728 ret |= (state->base_mods & (1 << idx));
729 if (type & XKB_STATE_LATCHED)
730 ret |= (state->latched_mods & (1 << idx));
731 if (type & XKB_STATE_LOCKED)
732 ret |= (state->locked_mods & (1 << idx));
738 * Helper function for xkb_state_mod_indices_are_active and
739 * xkb_state_mod_names_are_active.
742 match_mod_masks(struct xkb_state *state, enum xkb_state_match match,
745 uint32_t active = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
747 if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
750 if (match & XKB_STATE_MATCH_ANY)
751 return !!(active & wanted);
753 return (active & wanted) == wanted;
759 * Returns 1 if the modifiers are active with the specified type(s), 0 if
760 * not, or -1 if any of the modifiers are invalid.
763 xkb_state_mod_indices_are_active(struct xkb_state *state,
764 enum xkb_state_component type,
765 enum xkb_state_match match,
769 xkb_mod_index_t idx = 0;
775 idx = va_arg(ap, xkb_mod_index_t);
776 if (idx == XKB_MOD_INVALID ||
777 idx >= xkb_map_num_mods(state->keymap)) {
781 wanted |= (1 << idx);
788 return match_mod_masks(state, match, wanted);
792 * Returns 1 if the given modifier is active with the specified type(s), 0 if
793 * not, or -1 if the modifier is invalid.
796 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
797 enum xkb_state_component type)
799 xkb_mod_index_t idx = xkb_map_mod_get_index(state->keymap, name);
801 if (idx == XKB_MOD_INVALID)
804 return xkb_state_mod_index_is_active(state, idx, type);
808 * Returns 1 if the modifiers are active with the specified type(s), 0 if
809 * not, or -1 if any of the modifiers are invalid.
812 xkb_state_mod_names_are_active(struct xkb_state *state,
813 enum xkb_state_component type,
814 enum xkb_state_match match,
818 xkb_mod_index_t idx = 0;
825 str = va_arg(ap, const char *);
828 idx = xkb_map_mod_get_index(state->keymap, str);
829 if (idx == XKB_MOD_INVALID) {
833 wanted |= (1 << idx);
840 return match_mod_masks(state, match, wanted);
844 * Returns 1 if the given group is active with the specified type(s), 0 if
845 * not, or -1 if the group is invalid.
848 xkb_state_group_index_is_active(struct xkb_state *state,
849 xkb_group_index_t idx,
850 enum xkb_state_component type)
854 if (idx >= xkb_map_num_groups(state->keymap))
857 if (type & XKB_STATE_DEPRESSED)
858 ret |= (state->base_group == idx);
859 if (type & XKB_STATE_LATCHED)
860 ret |= (state->latched_group == idx);
861 if (type & XKB_STATE_LOCKED)
862 ret |= (state->locked_group == idx);
868 * Returns 1 if the given modifier is active with the specified type(s), 0 if
869 * not, or -1 if the modifier is invalid.
872 xkb_state_group_name_is_active(struct xkb_state *state, const char *name,
873 enum xkb_state_component type)
875 xkb_group_index_t idx = xkb_map_group_get_index(state->keymap, name);
877 if (idx == XKB_GROUP_INVALID)
880 return xkb_state_group_index_is_active(state, idx, type);
884 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
887 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
889 if (idx >= xkb_map_num_leds(state->keymap))
892 return !!(state->leds & (1 << idx));
896 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
899 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
901 xkb_led_index_t idx = xkb_map_led_get_index(state->keymap, name);
903 if (idx == XKB_LED_INVALID)
906 return xkb_state_led_index_is_active(state, idx);