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 struct xkb_filter *filter,
188 const struct xkb_key *key,
189 const union xkb_action *action)
192 filter->func = xkb_filter_group_set_func;
193 filter->action = *action;
195 if (action->group.flags & ACTION_ABSOLUTE_SWITCH) {
196 filter->action.group.group = state->base_group;
197 state->base_group = action->group.group;
200 state->base_group += action->group.group;
207 xkb_filter_group_lock_func(struct xkb_state *state,
208 struct xkb_filter *filter,
209 const 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,
228 struct xkb_filter *filter,
229 const struct xkb_key *key,
230 const union xkb_action *action)
233 filter->func = xkb_filter_group_lock_func;
234 filter->action = *action;
236 if (action->group.flags & ACTION_ABSOLUTE_SWITCH)
237 state->locked_group = action->group.group;
239 state->locked_group += action->group.group;
245 xkb_filter_mod_set_func(struct xkb_state *state,
246 struct xkb_filter *filter,
247 const 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 state->clear_mods = filter->action.mods.mods.mask;
264 if (filter->action.mods.flags & ACTION_LOCK_CLEAR)
265 state->locked_mods &= ~filter->action.mods.mods.mask;
272 xkb_filter_mod_set_new(struct xkb_state *state,
273 struct xkb_filter *filter,
274 const struct xkb_key *key,
275 const union xkb_action *action)
278 filter->func = xkb_filter_mod_set_func;
279 filter->action = *action;
281 state->set_mods = action->mods.mods.mask;
287 xkb_filter_mod_lock_func(struct xkb_state *state,
288 struct xkb_filter *filter,
289 const 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 state->locked_mods &= ~filter->priv;
309 xkb_filter_mod_lock_new(struct xkb_state *state,
310 struct xkb_filter *filter,
311 const struct xkb_key *key,
312 const union xkb_action *action)
315 filter->func = xkb_filter_mod_lock_func;
316 filter->action = *action;
317 filter->priv = state->locked_mods & action->mods.mods.mask;
319 state->locked_mods |= action->mods.mods.mask;
324 enum xkb_key_latch_state {
331 xkb_action_breaks_latch(const union xkb_action *action)
333 switch (action->type) {
334 case ACTION_TYPE_NONE:
335 case ACTION_TYPE_PTR_BUTTON:
336 case ACTION_TYPE_PTR_LOCK:
337 case ACTION_TYPE_CTRL_SET:
338 case ACTION_TYPE_CTRL_LOCK:
339 case ACTION_TYPE_KEY_REDIRECT:
340 case ACTION_TYPE_SWITCH_VT:
341 case ACTION_TYPE_TERMINATE:
349 xkb_filter_mod_latch_func(struct xkb_state *state,
350 struct xkb_filter *filter,
351 const 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(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 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 state->set_mods = filter->action.mods.mods.mask;
377 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 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 (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 state->latched_mods &= ~filter->action.mods.mods.mask;
404 state->clear_mods = filter->action.mods.mods.mask;
405 state->locked_mods &= ~filter->action.mods.mods.mask;
409 latch = LATCH_PENDING;
410 state->clear_mods = filter->action.mods.mods.mask;
411 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,
430 struct xkb_filter *filter,
431 const struct xkb_key *key,
432 const union xkb_action *action)
434 enum xkb_key_latch_state latch = LATCH_KEY_DOWN;
437 filter->priv = latch;
438 filter->func = xkb_filter_mod_latch_func;
439 filter->action = *action;
441 state->set_mods = action->mods.mods.mask;
446 typedef int (*filter_action_new_func_t)(struct xkb_state *state,
447 struct xkb_filter *filter,
448 const struct xkb_key *key,
449 const union xkb_action *action);
451 static const filter_action_new_func_t
452 filter_action_new_funcs[_ACTION_TYPE_NUM_ENTRIES] = {
453 [ACTION_TYPE_MOD_SET] = xkb_filter_mod_set_new,
454 [ACTION_TYPE_MOD_LATCH] = xkb_filter_mod_latch_new,
455 [ACTION_TYPE_MOD_LOCK] = xkb_filter_mod_lock_new,
456 [ACTION_TYPE_GROUP_SET] = xkb_filter_group_set_new,
457 [ACTION_TYPE_GROUP_LOCK] = xkb_filter_group_lock_new,
461 * Applies any relevant filters to the key, first from the list of filters
462 * that are currently active, then if no filter has claimed the key, possibly
463 * apply a new filter from the key action.
466 xkb_filter_apply_all(struct xkb_state *state,
467 const struct xkb_key *key,
468 enum xkb_key_direction direction)
470 struct xkb_filter *filter;
471 const union xkb_action *act = NULL;
474 /* First run through all the currently active filters and see if any of
475 * them have claimed this event. */
476 darray_foreach(filter, state->filters) {
479 send &= filter->func(state, filter, key, direction);
482 if (!send || direction == XKB_KEY_UP)
485 act = xkb_key_get_action(state, key);
486 if (filter_action_new_funcs[act->type]) {
487 filter = xkb_filter_new(state);
490 send = filter_action_new_funcs[act->type](state, filter, key, act);
497 XKB_EXPORT struct xkb_state *
498 xkb_state_new(struct xkb_keymap *keymap)
500 struct xkb_state *ret;
502 ret = calloc(sizeof(*ret), 1);
507 ret->keymap = xkb_map_ref(keymap);
512 XKB_EXPORT struct xkb_state *
513 xkb_state_ref(struct xkb_state *state)
520 xkb_state_unref(struct xkb_state *state)
522 if (--state->refcnt > 0)
525 xkb_map_unref(state->keymap);
526 darray_free(state->filters);
530 XKB_EXPORT struct xkb_keymap *
531 xkb_state_get_map(struct xkb_state *state)
533 return state->keymap;
537 * Update the LED state to match the rest of the xkb_state.
540 xkb_state_led_update_all(struct xkb_state *state)
546 for (led = 0; led < XKB_NUM_INDICATORS; led++) {
547 struct xkb_indicator_map *map = &state->keymap->indicators[led];
548 xkb_mod_mask_t mod_mask = 0;
549 uint32_t group_mask = 0;
551 if (map->which_mods & XKB_STATE_DEPRESSED)
552 mod_mask |= state->base_mods;
553 if (map->which_mods & XKB_STATE_LATCHED)
554 mod_mask |= state->latched_mods;
555 if (map->which_mods & XKB_STATE_LOCKED)
556 mod_mask |= state->locked_mods;
557 if ((map->mods.mask & mod_mask))
558 state->leds |= (1 << led);
560 if (map->which_groups & XKB_STATE_DEPRESSED)
561 group_mask |= (1 << state->base_group);
562 if (map->which_groups & XKB_STATE_LATCHED)
563 group_mask |= (1 << state->latched_group);
564 if (map->which_groups & XKB_STATE_LOCKED)
565 group_mask |= (1 << state->locked_group);
566 if ((map->groups & group_mask))
567 state->leds |= (1 << led);
570 if ((map->ctrls & state->keymap->enabled_ctrls))
571 state->leds |= (1 << led);
577 * Calculates the derived state (effective mods/group and LEDs) from an
578 * up-to-date xkb_state.
581 xkb_state_update_derived(struct xkb_state *state)
584 (state->base_mods | state->latched_mods | state->locked_mods);
585 /* FIXME: Clamp/wrap locked_group */
586 state->group = state->locked_group + state->base_group +
587 state->latched_group;
588 /* FIXME: Clamp/wrap effective group */
590 xkb_state_led_update_all(state);
594 * Given a particular key event, updates the state structure to reflect the
598 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
599 enum xkb_key_direction direction)
603 const struct xkb_key *key = XkbKey(state->keymap, kc);
608 state->clear_mods = 0;
610 xkb_filter_apply_all(state, key, direction);
612 for (i = 0, bit = 1; state->set_mods; i++, bit <<= 1) {
613 if (state->set_mods & bit) {
614 state->mod_key_count[i]++;
615 state->base_mods |= bit;
616 state->set_mods &= ~bit;
620 for (i = 0, bit = 1; state->clear_mods; i++, bit <<= 1) {
621 if (state->clear_mods & bit) {
622 state->mod_key_count[i]--;
623 if (state->mod_key_count[i] <= 0) {
624 state->base_mods &= ~bit;
625 state->mod_key_count[i] = 0;
627 state->clear_mods &= ~bit;
631 xkb_state_update_derived(state);
635 * Updates the state from a set of explicit masks as gained from
636 * xkb_state_serialize_mods and xkb_state_serialize_groups. As noted in the
637 * documentation for these functions in xkbcommon.h, this round-trip is
638 * lossy, and should only be used to update a slave state mirroring the
639 * master, e.g. in a client/server window system.
642 xkb_state_update_mask(struct xkb_state *state,
643 xkb_mod_mask_t base_mods,
644 xkb_mod_mask_t latched_mods,
645 xkb_mod_mask_t locked_mods,
646 xkb_group_index_t base_group,
647 xkb_group_index_t latched_group,
648 xkb_group_index_t locked_group)
650 xkb_mod_index_t num_mods;
653 state->base_mods = 0;
654 state->latched_mods = 0;
655 state->locked_mods = 0;
656 num_mods = xkb_map_num_mods(state->keymap);
658 for (idx = 0; idx < num_mods; idx++) {
659 xkb_mod_mask_t mod = (1 << idx);
661 state->base_mods |= mod;
662 if (latched_mods & mod)
663 state->latched_mods |= mod;
664 if (locked_mods & mod)
665 state->locked_mods |= mod;
668 state->base_group = base_group;
669 state->latched_group = latched_group;
670 state->locked_group = locked_group;
672 xkb_state_update_derived(state);
676 * Serialises the requested modifier state into an xkb_mod_mask_t, with all
677 * the same disclaimers as in xkb_state_update_mask.
679 XKB_EXPORT xkb_mod_mask_t
680 xkb_state_serialize_mods(struct xkb_state *state,
681 enum xkb_state_component type)
683 xkb_mod_mask_t ret = 0;
685 if (type == XKB_STATE_EFFECTIVE)
688 if (type & XKB_STATE_DEPRESSED)
689 ret |= state->base_mods;
690 if (type & XKB_STATE_LATCHED)
691 ret |= state->latched_mods;
692 if (type & XKB_STATE_LOCKED)
693 ret |= state->locked_mods;
699 * Serialises the requested group state, with all the same disclaimers as
700 * in xkb_state_update_mask.
702 XKB_EXPORT xkb_group_index_t
703 xkb_state_serialize_group(struct xkb_state *state,
704 enum xkb_state_component type)
706 xkb_group_index_t ret = 0;
708 if (type == XKB_STATE_EFFECTIVE)
711 if (type & XKB_STATE_DEPRESSED)
712 ret += state->base_group;
713 if (type & XKB_STATE_LATCHED)
714 ret += state->latched_group;
715 if (type & XKB_STATE_LOCKED)
716 ret += state->locked_group;
722 * Returns 1 if the given modifier is active with the specified type(s), 0 if
723 * not, or -1 if the modifier is invalid.
726 xkb_state_mod_index_is_active(struct xkb_state *state,
728 enum xkb_state_component type)
732 if (idx >= xkb_map_num_mods(state->keymap))
735 if (type & XKB_STATE_DEPRESSED)
736 ret |= (state->base_mods & (1 << idx));
737 if (type & XKB_STATE_LATCHED)
738 ret |= (state->latched_mods & (1 << idx));
739 if (type & XKB_STATE_LOCKED)
740 ret |= (state->locked_mods & (1 << idx));
746 * Helper function for xkb_state_mod_indices_are_active and
747 * xkb_state_mod_names_are_active.
750 match_mod_masks(struct xkb_state *state, enum xkb_state_match match,
753 uint32_t active = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
755 if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
758 if (match & XKB_STATE_MATCH_ANY)
759 return !!(active & wanted);
761 return (active & wanted) == wanted;
767 * Returns 1 if the modifiers are active with the specified type(s), 0 if
768 * not, or -1 if any of the modifiers are invalid.
771 xkb_state_mod_indices_are_active(struct xkb_state *state,
772 enum xkb_state_component type,
773 enum xkb_state_match match,
777 xkb_mod_index_t idx = 0;
780 xkb_mod_index_t num_mods = xkb_map_num_mods(state->keymap);
784 idx = va_arg(ap, xkb_mod_index_t);
785 if (idx == XKB_MOD_INVALID)
787 if (idx >= num_mods) {
791 wanted |= (1 << idx);
798 return match_mod_masks(state, match, wanted);
802 * Returns 1 if the given modifier is active with the specified type(s), 0 if
803 * not, or -1 if the modifier is invalid.
806 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
807 enum xkb_state_component type)
809 xkb_mod_index_t idx = xkb_map_mod_get_index(state->keymap, name);
811 if (idx == XKB_MOD_INVALID)
814 return xkb_state_mod_index_is_active(state, idx, type);
818 * Returns 1 if the modifiers are active with the specified type(s), 0 if
819 * not, or -1 if any of the modifiers are invalid.
821 XKB_EXPORT ATTR_NULL_SENTINEL int
822 xkb_state_mod_names_are_active(struct xkb_state *state,
823 enum xkb_state_component type,
824 enum xkb_state_match match,
828 xkb_mod_index_t idx = 0;
835 str = va_arg(ap, const char *);
838 idx = xkb_map_mod_get_index(state->keymap, str);
839 if (idx == XKB_MOD_INVALID) {
843 wanted |= (1 << idx);
850 return match_mod_masks(state, match, wanted);
854 * Returns 1 if the given group is active with the specified type(s), 0 if
855 * not, or -1 if the group is invalid.
858 xkb_state_group_index_is_active(struct xkb_state *state,
859 xkb_group_index_t idx,
860 enum xkb_state_component type)
864 if (idx >= xkb_map_num_groups(state->keymap))
867 if (type & XKB_STATE_DEPRESSED)
868 ret |= (state->base_group == idx);
869 if (type & XKB_STATE_LATCHED)
870 ret |= (state->latched_group == idx);
871 if (type & XKB_STATE_LOCKED)
872 ret |= (state->locked_group == idx);
878 * Returns 1 if the given modifier is active with the specified type(s), 0 if
879 * not, or -1 if the modifier is invalid.
882 xkb_state_group_name_is_active(struct xkb_state *state, const char *name,
883 enum xkb_state_component type)
885 xkb_group_index_t idx = xkb_map_group_get_index(state->keymap, name);
887 if (idx == XKB_GROUP_INVALID)
890 return xkb_state_group_index_is_active(state, idx, type);
894 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
897 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
899 if (idx >= xkb_map_num_leds(state->keymap))
902 return !!(state->leds & (1 << idx));
906 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
909 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
911 xkb_led_index_t idx = xkb_map_led_get_index(state->keymap, name);
913 if (idx == XKB_LED_INVALID)
916 return xkb_state_led_index_is_active(state, idx);