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 struct xkb_state *state;
69 union xkb_action action;
72 int (*func)(struct xkb_filter *filter,
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, 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);
149 filter->state = state;
154 /***====================================================================***/
157 xkb_filter_group_set_func(struct xkb_filter *filter, struct xkb_key *key,
158 enum xkb_key_direction direction)
160 if (key != filter->key) {
161 filter->action.group.flags &= ~ACTION_LOCK_CLEAR;
165 if (direction == XKB_KEY_DOWN) {
169 else if (--filter->refcnt > 0) {
173 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
174 filter->state->base_group = filter->action.group.group;
176 filter->state->base_group = -filter->action.group.group;
177 if (filter->action.group.flags & ACTION_LOCK_CLEAR)
178 filter->state->locked_group = 0;
186 xkb_filter_group_set_new(struct xkb_state *state, struct xkb_key *key,
187 const union xkb_action *action)
189 struct xkb_filter *filter = xkb_filter_new(state);
194 filter->func = xkb_filter_group_set_func;
195 filter->action = *action;
197 if (action->group.flags & ACTION_ABSOLUTE_SWITCH) {
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, struct xkb_key *key,
210 enum xkb_key_direction direction)
212 if (key != filter->key)
215 if (direction == XKB_KEY_DOWN) {
219 if (--filter->refcnt > 0)
227 xkb_filter_group_lock_new(struct xkb_state *state, struct xkb_key *key,
228 const union xkb_action *action)
230 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 & ACTION_ABSOLUTE_SWITCH)
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, struct xkb_key *key,
248 enum xkb_key_direction direction)
250 if (key != filter->key) {
251 filter->action.mods.flags &= ~ACTION_LOCK_CLEAR;
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 & ACTION_LOCK_CLEAR)
265 filter->state->locked_mods &= ~filter->action.mods.mods.mask;
272 xkb_filter_mod_set_new(struct xkb_state *state, struct xkb_key *key,
273 const union xkb_action *action)
275 struct xkb_filter *filter = xkb_filter_new(state);
280 filter->func = xkb_filter_mod_set_func;
281 filter->action = *action;
283 filter->state->set_mods = action->mods.mods.mask;
289 xkb_filter_mod_lock_func(struct xkb_filter *filter, struct xkb_key *key,
290 enum xkb_key_direction direction)
292 if (key != filter->key)
295 if (direction == XKB_KEY_DOWN) {
299 if (--filter->refcnt > 0)
302 filter->state->locked_mods &= ~filter->priv;
309 xkb_filter_mod_lock_new(struct xkb_state *state, struct xkb_key *key,
310 const union xkb_action *action)
312 struct xkb_filter *filter = xkb_filter_new(state);
317 filter->func = xkb_filter_mod_lock_func;
318 filter->action = *action;
319 filter->priv = state->locked_mods & action->mods.mods.mask;
321 filter->state->locked_mods |= action->mods.mods.mask;
326 enum xkb_key_latch_state {
333 xkb_action_breaks_latch(const union xkb_action *action)
335 switch (action->type) {
336 case ACTION_TYPE_NONE:
337 case ACTION_TYPE_PTR_BUTTON:
338 case ACTION_TYPE_PTR_LOCK:
339 case ACTION_TYPE_CTRL_SET:
340 case ACTION_TYPE_CTRL_LOCK:
341 case ACTION_TYPE_KEY_REDIRECT:
342 case ACTION_TYPE_SWITCH_VT:
343 case ACTION_TYPE_TERMINATE:
351 xkb_filter_mod_latch_func(struct xkb_filter *filter, struct xkb_key *key,
352 enum xkb_key_direction direction)
354 enum xkb_key_latch_state latch = filter->priv;
356 if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
357 /* If this is a new keypress and we're awaiting our single latched
358 * keypress, then either break the latch if any random key is pressed,
359 * or promote it to a lock or plain base set if it's the same
361 const union xkb_action *action = xkb_key_get_action(filter->state, key);
362 if (action->type == ACTION_TYPE_MOD_LATCH &&
363 action->mods.flags == filter->action.mods.flags &&
364 action->mods.mods.mask == filter->action.mods.mods.mask) {
365 filter->action = *action;
366 if (filter->action.mods.flags & ACTION_LATCH_TO_LOCK) {
367 filter->action.type = ACTION_TYPE_MOD_LOCK;
368 filter->func = xkb_filter_mod_lock_func;
369 filter->state->locked_mods |= filter->action.mods.mods.mask;
372 filter->action.type = ACTION_TYPE_MOD_SET;
373 filter->func = xkb_filter_mod_set_func;
374 filter->state->set_mods = filter->action.mods.mods.mask;
377 filter->state->latched_mods &= ~filter->action.mods.mods.mask;
381 else if (xkb_action_breaks_latch(action)) {
382 /* XXX: This may be totally broken, we might need to break the
383 * latch in the next run after this press? */
384 filter->state->latched_mods &= ~filter->action.mods.mods.mask;
389 else if (direction == XKB_KEY_UP && key == filter->key) {
390 /* Our key got released. If we've set it to clear locks, and we
391 * currently have the same modifiers locked, then release them and
392 * don't actually latch. Else we've actually hit the latching
393 * stage, so set PENDING and move our modifier from base to
395 if (latch == NO_LATCH ||
396 ((filter->action.mods.flags & ACTION_LOCK_CLEAR) &&
397 (filter->state->locked_mods & filter->action.mods.mods.mask) ==
398 filter->action.mods.mods.mask)) {
399 /* XXX: We might be a bit overenthusiastic about clearing
400 * mods other filters have set here? */
401 if (latch == LATCH_PENDING)
402 filter->state->latched_mods &= ~filter->action.mods.mods.mask;
404 filter->state->clear_mods = filter->action.mods.mods.mask;
405 filter->state->locked_mods &= ~filter->action.mods.mods.mask;
409 latch = LATCH_PENDING;
410 filter->state->clear_mods = filter->action.mods.mods.mask;
411 filter->state->latched_mods |= filter->action.mods.mods.mask;
415 else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
416 /* Someone's pressed another key while we've still got the latching
417 * key held down, so keep the base modifier state active (from
418 * xkb_filter_mod_latch_new), but don't trip the latch, just clear
419 * it as soon as the modifier gets released. */
423 filter->priv = latch;
429 xkb_filter_mod_latch_new(struct xkb_state *state, struct xkb_key *key,
430 const union xkb_action *action)
432 enum xkb_key_latch_state latch = LATCH_KEY_DOWN;
433 struct xkb_filter *filter = xkb_filter_new(state);
438 filter->priv = latch;
439 filter->func = xkb_filter_mod_latch_func;
440 filter->action = *action;
442 filter->state->set_mods = action->mods.mods.mask;
448 * Applies any relevant filters to the key, first from the list of filters
449 * that are currently active, then if no filter has claimed the key, possibly
450 * apply a new filter from the key action.
453 xkb_filter_apply_all(struct xkb_state *state, struct xkb_key *key,
454 enum xkb_key_direction direction)
456 struct xkb_filter *filter;
457 const union xkb_action *act = NULL;
460 /* First run through all the currently active filters and see if any of
461 * them have claimed this event. */
462 darray_foreach(filter, state->filters) {
465 send &= filter->func(filter, key, direction);
468 if (!send || direction == XKB_KEY_UP)
471 act = xkb_key_get_action(state, key);
473 case ACTION_TYPE_MOD_SET:
474 send = xkb_filter_mod_set_new(state, key, act);
476 case ACTION_TYPE_MOD_LATCH:
477 send = xkb_filter_mod_latch_new(state, key, act);
479 case ACTION_TYPE_MOD_LOCK:
480 send = xkb_filter_mod_lock_new(state, key, act);
482 case ACTION_TYPE_GROUP_SET:
483 send = xkb_filter_group_set_new(state, key, act);
486 case ACTION_TYPE_GROUP_LATCH:
487 send = xkb_filter_mod_latch_new(state, key, act);
490 case ACTION_TYPE_GROUP_LOCK:
491 send = xkb_filter_group_lock_new(state, key, act);
500 XKB_EXPORT struct xkb_state *
501 xkb_state_new(struct xkb_keymap *keymap)
503 struct xkb_state *ret;
505 ret = calloc(sizeof(*ret), 1);
510 ret->keymap = xkb_map_ref(keymap);
515 XKB_EXPORT struct xkb_state *
516 xkb_state_ref(struct xkb_state *state)
523 xkb_state_unref(struct xkb_state *state)
525 if (--state->refcnt > 0)
528 xkb_map_unref(state->keymap);
529 darray_free(state->filters);
533 XKB_EXPORT struct xkb_keymap *
534 xkb_state_get_map(struct xkb_state *state)
536 return state->keymap;
540 * Update the LED state to match the rest of the xkb_state.
543 xkb_state_led_update_all(struct xkb_state *state)
549 for (led = 0; led < XKB_NUM_INDICATORS; led++) {
550 struct xkb_indicator_map *map = &state->keymap->indicators[led];
551 xkb_mod_mask_t mod_mask = 0;
552 uint32_t group_mask = 0;
554 if (map->which_mods & XKB_STATE_DEPRESSED)
555 mod_mask |= state->base_mods;
556 if (map->which_mods & XKB_STATE_LATCHED)
557 mod_mask |= state->latched_mods;
558 if (map->which_mods & XKB_STATE_LOCKED)
559 mod_mask |= state->locked_mods;
560 if ((map->mods.mask & mod_mask))
561 state->leds |= (1 << led);
563 if (map->which_groups & XKB_STATE_DEPRESSED)
564 group_mask |= (1 << state->base_group);
565 if (map->which_groups & XKB_STATE_LATCHED)
566 group_mask |= (1 << state->latched_group);
567 if (map->which_groups & XKB_STATE_LOCKED)
568 group_mask |= (1 << state->locked_group);
569 if ((map->groups & group_mask))
570 state->leds |= (1 << led);
573 if ((map->ctrls & state->keymap->enabled_ctrls))
574 state->leds |= (1 << led);
580 * Calculates the derived state (effective mods/group and LEDs) from an
581 * up-to-date xkb_state.
584 xkb_state_update_derived(struct xkb_state *state)
587 (state->base_mods | state->latched_mods | state->locked_mods);
588 /* FIXME: Clamp/wrap locked_group */
589 state->group = state->locked_group + state->base_group +
590 state->latched_group;
591 /* FIXME: Clamp/wrap effective group */
593 xkb_state_led_update_all(state);
597 * Given a particular key event, updates the state structure to reflect the
601 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
602 enum xkb_key_direction direction)
608 key = XkbKey(state->keymap, kc);
613 state->clear_mods = 0;
615 xkb_filter_apply_all(state, key, direction);
617 for (i = 0, bit = 1; state->set_mods; i++, bit <<= 1) {
618 if (state->set_mods & bit) {
619 state->mod_key_count[i]++;
620 state->base_mods |= bit;
621 state->set_mods &= ~bit;
625 for (i = 0, bit = 1; state->clear_mods; i++, bit <<= 1) {
626 if (state->clear_mods & bit) {
627 state->mod_key_count[i]--;
628 if (state->mod_key_count[i] <= 0) {
629 state->base_mods &= ~bit;
630 state->mod_key_count[i] = 0;
632 state->clear_mods &= ~bit;
636 xkb_state_update_derived(state);
640 * Updates the state from a set of explicit masks as gained from
641 * xkb_state_serialize_mods and xkb_state_serialize_groups. As noted in the
642 * documentation for these functions in xkbcommon.h, this round-trip is
643 * lossy, and should only be used to update a slave state mirroring the
644 * master, e.g. in a client/server window system.
647 xkb_state_update_mask(struct xkb_state *state,
648 xkb_mod_mask_t base_mods,
649 xkb_mod_mask_t latched_mods,
650 xkb_mod_mask_t locked_mods,
651 xkb_group_index_t base_group,
652 xkb_group_index_t latched_group,
653 xkb_group_index_t locked_group)
655 xkb_mod_index_t num_mods;
658 state->base_mods = 0;
659 state->latched_mods = 0;
660 state->locked_mods = 0;
661 num_mods = xkb_map_num_mods(state->keymap);
663 for (idx = 0; idx < num_mods; idx++) {
664 xkb_mod_mask_t mod = (1 << idx);
666 state->base_mods |= mod;
667 if (latched_mods & mod)
668 state->latched_mods |= mod;
669 if (locked_mods & mod)
670 state->locked_mods |= mod;
673 state->base_group = base_group;
674 state->latched_group = latched_group;
675 state->locked_group = locked_group;
677 xkb_state_update_derived(state);
681 * Serialises the requested modifier state into an xkb_mod_mask_t, with all
682 * the same disclaimers as in xkb_state_update_mask.
684 XKB_EXPORT xkb_mod_mask_t
685 xkb_state_serialize_mods(struct xkb_state *state,
686 enum xkb_state_component type)
688 xkb_mod_mask_t ret = 0;
690 if (type == XKB_STATE_EFFECTIVE)
693 if (type & XKB_STATE_DEPRESSED)
694 ret |= state->base_mods;
695 if (type & XKB_STATE_LATCHED)
696 ret |= state->latched_mods;
697 if (type & XKB_STATE_LOCKED)
698 ret |= state->locked_mods;
704 * Serialises the requested group state, with all the same disclaimers as
705 * in xkb_state_update_mask.
707 XKB_EXPORT xkb_group_index_t
708 xkb_state_serialize_group(struct xkb_state *state,
709 enum xkb_state_component type)
711 xkb_group_index_t ret = 0;
713 if (type == XKB_STATE_EFFECTIVE)
716 if (type & XKB_STATE_DEPRESSED)
717 ret += state->base_group;
718 if (type & XKB_STATE_LATCHED)
719 ret += state->latched_group;
720 if (type & XKB_STATE_LOCKED)
721 ret += state->locked_group;
727 * Returns 1 if the given modifier is active with the specified type(s), 0 if
728 * not, or -1 if the modifier is invalid.
731 xkb_state_mod_index_is_active(struct xkb_state *state,
733 enum xkb_state_component type)
737 if (idx >= xkb_map_num_mods(state->keymap))
740 if (type & XKB_STATE_DEPRESSED)
741 ret |= (state->base_mods & (1 << idx));
742 if (type & XKB_STATE_LATCHED)
743 ret |= (state->latched_mods & (1 << idx));
744 if (type & XKB_STATE_LOCKED)
745 ret |= (state->locked_mods & (1 << idx));
751 * Helper function for xkb_state_mod_indices_are_active and
752 * xkb_state_mod_names_are_active.
755 match_mod_masks(struct xkb_state *state, enum xkb_state_match match,
758 uint32_t active = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
760 if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
763 if (match & XKB_STATE_MATCH_ANY)
764 return !!(active & wanted);
766 return (active & wanted) == wanted;
772 * Returns 1 if the modifiers are active with the specified type(s), 0 if
773 * not, or -1 if any of the modifiers are invalid.
776 xkb_state_mod_indices_are_active(struct xkb_state *state,
777 enum xkb_state_component type,
778 enum xkb_state_match match,
782 xkb_mod_index_t idx = 0;
785 xkb_mod_index_t num_mods = xkb_map_num_mods(state->keymap);
789 idx = va_arg(ap, xkb_mod_index_t);
790 if (idx == XKB_MOD_INVALID)
792 if (idx >= num_mods) {
796 wanted |= (1 << idx);
803 return match_mod_masks(state, match, wanted);
807 * Returns 1 if the given modifier is active with the specified type(s), 0 if
808 * not, or -1 if the modifier is invalid.
811 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
812 enum xkb_state_component type)
814 xkb_mod_index_t idx = xkb_map_mod_get_index(state->keymap, name);
816 if (idx == XKB_MOD_INVALID)
819 return xkb_state_mod_index_is_active(state, idx, type);
823 * Returns 1 if the modifiers are active with the specified type(s), 0 if
824 * not, or -1 if any of the modifiers are invalid.
826 XKB_EXPORT ATTR_NULL_SENTINEL int
827 xkb_state_mod_names_are_active(struct xkb_state *state,
828 enum xkb_state_component type,
829 enum xkb_state_match match,
833 xkb_mod_index_t idx = 0;
840 str = va_arg(ap, const char *);
843 idx = xkb_map_mod_get_index(state->keymap, str);
844 if (idx == XKB_MOD_INVALID) {
848 wanted |= (1 << idx);
855 return match_mod_masks(state, match, wanted);
859 * Returns 1 if the given group is active with the specified type(s), 0 if
860 * not, or -1 if the group is invalid.
863 xkb_state_group_index_is_active(struct xkb_state *state,
864 xkb_group_index_t idx,
865 enum xkb_state_component type)
869 if (idx >= xkb_map_num_groups(state->keymap))
872 if (type & XKB_STATE_DEPRESSED)
873 ret |= (state->base_group == idx);
874 if (type & XKB_STATE_LATCHED)
875 ret |= (state->latched_group == idx);
876 if (type & XKB_STATE_LOCKED)
877 ret |= (state->locked_group == idx);
883 * Returns 1 if the given modifier is active with the specified type(s), 0 if
884 * not, or -1 if the modifier is invalid.
887 xkb_state_group_name_is_active(struct xkb_state *state, const char *name,
888 enum xkb_state_component type)
890 xkb_group_index_t idx = xkb_map_group_get_index(state->keymap, name);
892 if (idx == XKB_GROUP_INVALID)
895 return xkb_state_group_index_is_active(state, idx, type);
899 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
902 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
904 if (idx >= xkb_map_num_leds(state->keymap))
907 return !!(state->leds & (1 << idx));
911 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
914 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
916 xkb_led_index_t idx = xkb_map_led_get_index(state->keymap, name);
918 if (idx == XKB_LED_INVALID)
921 return xkb_state_led_index_is_active(state, idx);