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 state->base_group = filter->priv;
176 if (filter->action.group.flags & ACTION_LOCK_CLEAR)
177 state->locked_group = 0;
184 xkb_filter_group_set_new(struct xkb_state *state, struct xkb_filter *filter)
186 filter->priv = state->base_group;
187 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
188 state->base_group = filter->action.group.group;
190 state->base_group += filter->action.group.group;
194 xkb_filter_group_lock_func(struct xkb_state *state,
195 struct xkb_filter *filter,
196 const struct xkb_key *key,
197 enum xkb_key_direction direction)
199 if (key != filter->key)
202 if (direction == XKB_KEY_DOWN) {
206 if (--filter->refcnt > 0)
214 xkb_filter_group_lock_new(struct xkb_state *state, struct xkb_filter *filter)
216 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
217 state->locked_group = filter->action.group.group;
219 state->locked_group += filter->action.group.group;
223 xkb_filter_mod_set_func(struct xkb_state *state,
224 struct xkb_filter *filter,
225 const struct xkb_key *key,
226 enum xkb_key_direction direction)
228 if (key != filter->key) {
229 filter->action.mods.flags &= ~ACTION_LOCK_CLEAR;
233 if (direction == XKB_KEY_DOWN) {
237 else if (--filter->refcnt > 0) {
241 state->clear_mods = filter->action.mods.mods.mask;
242 if (filter->action.mods.flags & ACTION_LOCK_CLEAR)
243 state->locked_mods &= ~filter->action.mods.mods.mask;
250 xkb_filter_mod_set_new(struct xkb_state *state, struct xkb_filter *filter)
252 state->set_mods = filter->action.mods.mods.mask;
256 xkb_filter_mod_lock_func(struct xkb_state *state,
257 struct xkb_filter *filter,
258 const struct xkb_key *key,
259 enum xkb_key_direction direction)
261 if (key != filter->key)
264 if (direction == XKB_KEY_DOWN) {
268 if (--filter->refcnt > 0)
271 state->locked_mods &= ~filter->priv;
278 xkb_filter_mod_lock_new(struct xkb_state *state, struct xkb_filter *filter)
280 filter->priv = state->locked_mods & filter->action.mods.mods.mask;
281 state->locked_mods |= filter->action.mods.mods.mask;
284 enum xkb_key_latch_state {
291 xkb_action_breaks_latch(const union xkb_action *action)
293 switch (action->type) {
294 case ACTION_TYPE_NONE:
295 case ACTION_TYPE_PTR_BUTTON:
296 case ACTION_TYPE_PTR_LOCK:
297 case ACTION_TYPE_CTRL_SET:
298 case ACTION_TYPE_CTRL_LOCK:
299 case ACTION_TYPE_KEY_REDIRECT:
300 case ACTION_TYPE_SWITCH_VT:
301 case ACTION_TYPE_TERMINATE:
309 xkb_filter_mod_latch_func(struct xkb_state *state,
310 struct xkb_filter *filter,
311 const struct xkb_key *key,
312 enum xkb_key_direction direction)
314 enum xkb_key_latch_state latch = filter->priv;
316 if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
317 /* If this is a new keypress and we're awaiting our single latched
318 * keypress, then either break the latch if any random key is pressed,
319 * or promote it to a lock or plain base set if it's the same
321 const union xkb_action *action = xkb_key_get_action(state, key);
322 if (action->type == ACTION_TYPE_MOD_LATCH &&
323 action->mods.flags == filter->action.mods.flags &&
324 action->mods.mods.mask == filter->action.mods.mods.mask) {
325 filter->action = *action;
326 if (filter->action.mods.flags & ACTION_LATCH_TO_LOCK) {
327 filter->action.type = ACTION_TYPE_MOD_LOCK;
328 filter->func = xkb_filter_mod_lock_func;
329 state->locked_mods |= filter->action.mods.mods.mask;
332 filter->action.type = ACTION_TYPE_MOD_SET;
333 filter->func = xkb_filter_mod_set_func;
334 state->set_mods = filter->action.mods.mods.mask;
337 state->latched_mods &= ~filter->action.mods.mods.mask;
341 else if (xkb_action_breaks_latch(action)) {
342 /* XXX: This may be totally broken, we might need to break the
343 * latch in the next run after this press? */
344 state->latched_mods &= ~filter->action.mods.mods.mask;
349 else if (direction == XKB_KEY_UP && key == filter->key) {
350 /* Our key got released. If we've set it to clear locks, and we
351 * currently have the same modifiers locked, then release them and
352 * don't actually latch. Else we've actually hit the latching
353 * stage, so set PENDING and move our modifier from base to
355 if (latch == NO_LATCH ||
356 ((filter->action.mods.flags & ACTION_LOCK_CLEAR) &&
357 (state->locked_mods & filter->action.mods.mods.mask) ==
358 filter->action.mods.mods.mask)) {
359 /* XXX: We might be a bit overenthusiastic about clearing
360 * mods other filters have set here? */
361 if (latch == LATCH_PENDING)
362 state->latched_mods &= ~filter->action.mods.mods.mask;
364 state->clear_mods = filter->action.mods.mods.mask;
365 state->locked_mods &= ~filter->action.mods.mods.mask;
369 latch = LATCH_PENDING;
370 state->clear_mods = filter->action.mods.mods.mask;
371 state->latched_mods |= filter->action.mods.mods.mask;
375 else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
376 /* Someone's pressed another key while we've still got the latching
377 * key held down, so keep the base modifier state active (from
378 * xkb_filter_mod_latch_new), but don't trip the latch, just clear
379 * it as soon as the modifier gets released. */
383 filter->priv = latch;
389 xkb_filter_mod_latch_new(struct xkb_state *state, struct xkb_filter *filter)
391 filter->priv = LATCH_KEY_DOWN;
392 state->set_mods = filter->action.mods.mods.mask;
395 static const struct {
396 void (*new)(struct xkb_state *state, struct xkb_filter *filter);
397 int (*func)(struct xkb_state *state, struct xkb_filter *filter,
398 const struct xkb_key *key, enum xkb_key_direction direction);
399 } filter_action_funcs[_ACTION_TYPE_NUM_ENTRIES] = {
400 [ACTION_TYPE_MOD_SET] = { xkb_filter_mod_set_new,
401 xkb_filter_mod_set_func },
402 [ACTION_TYPE_MOD_LATCH] = { xkb_filter_mod_latch_new,
403 xkb_filter_mod_latch_func },
404 [ACTION_TYPE_MOD_LOCK] = { xkb_filter_mod_lock_new,
405 xkb_filter_mod_lock_func },
406 [ACTION_TYPE_GROUP_SET] = { xkb_filter_group_set_new,
407 xkb_filter_group_set_func },
408 [ACTION_TYPE_GROUP_LOCK] = { xkb_filter_group_lock_new,
409 xkb_filter_group_lock_func },
413 * Applies any relevant filters to the key, first from the list of filters
414 * that are currently active, then if no filter has claimed the key, possibly
415 * apply a new filter from the key action.
418 xkb_filter_apply_all(struct xkb_state *state,
419 const struct xkb_key *key,
420 enum xkb_key_direction direction)
422 struct xkb_filter *filter;
423 const union xkb_action *action;
426 /* First run through all the currently active filters and see if any of
427 * them have claimed this event. */
428 darray_foreach(filter, state->filters) {
431 send &= filter->func(state, filter, key, direction);
434 if (!send || direction == XKB_KEY_UP)
437 action = xkb_key_get_action(state, key);
438 if (!filter_action_funcs[action->type].new)
441 filter = xkb_filter_new(state);
446 filter->func = filter_action_funcs[action->type].func;
447 filter->action = *action;
448 filter_action_funcs[action->type].new(state, filter);
451 XKB_EXPORT struct xkb_state *
452 xkb_state_new(struct xkb_keymap *keymap)
454 struct xkb_state *ret;
456 ret = calloc(sizeof(*ret), 1);
461 ret->keymap = xkb_map_ref(keymap);
466 XKB_EXPORT struct xkb_state *
467 xkb_state_ref(struct xkb_state *state)
474 xkb_state_unref(struct xkb_state *state)
476 if (--state->refcnt > 0)
479 xkb_map_unref(state->keymap);
480 darray_free(state->filters);
484 XKB_EXPORT struct xkb_keymap *
485 xkb_state_get_map(struct xkb_state *state)
487 return state->keymap;
491 * Update the LED state to match the rest of the xkb_state.
494 xkb_state_led_update_all(struct xkb_state *state)
500 for (led = 0; led < XKB_NUM_INDICATORS; led++) {
501 struct xkb_indicator_map *map = &state->keymap->indicators[led];
502 xkb_mod_mask_t mod_mask = 0;
503 uint32_t group_mask = 0;
505 if (map->which_mods & XKB_STATE_DEPRESSED)
506 mod_mask |= state->base_mods;
507 if (map->which_mods & XKB_STATE_LATCHED)
508 mod_mask |= state->latched_mods;
509 if (map->which_mods & XKB_STATE_LOCKED)
510 mod_mask |= state->locked_mods;
511 if ((map->mods.mask & mod_mask))
512 state->leds |= (1 << led);
514 if (map->which_groups & XKB_STATE_DEPRESSED)
515 group_mask |= (1 << state->base_group);
516 if (map->which_groups & XKB_STATE_LATCHED)
517 group_mask |= (1 << state->latched_group);
518 if (map->which_groups & XKB_STATE_LOCKED)
519 group_mask |= (1 << state->locked_group);
520 if ((map->groups & group_mask))
521 state->leds |= (1 << led);
524 if ((map->ctrls & state->keymap->enabled_ctrls))
525 state->leds |= (1 << led);
531 * Calculates the derived state (effective mods/group and LEDs) from an
532 * up-to-date xkb_state.
535 xkb_state_update_derived(struct xkb_state *state)
538 (state->base_mods | state->latched_mods | state->locked_mods);
539 /* FIXME: Clamp/wrap locked_group */
540 state->group = state->locked_group + state->base_group +
541 state->latched_group;
542 /* FIXME: Clamp/wrap effective group */
544 xkb_state_led_update_all(state);
548 * Given a particular key event, updates the state structure to reflect the
552 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
553 enum xkb_key_direction direction)
557 const struct xkb_key *key = XkbKey(state->keymap, kc);
562 state->clear_mods = 0;
564 xkb_filter_apply_all(state, key, direction);
566 for (i = 0, bit = 1; state->set_mods; i++, bit <<= 1) {
567 if (state->set_mods & bit) {
568 state->mod_key_count[i]++;
569 state->base_mods |= bit;
570 state->set_mods &= ~bit;
574 for (i = 0, bit = 1; state->clear_mods; i++, bit <<= 1) {
575 if (state->clear_mods & bit) {
576 state->mod_key_count[i]--;
577 if (state->mod_key_count[i] <= 0) {
578 state->base_mods &= ~bit;
579 state->mod_key_count[i] = 0;
581 state->clear_mods &= ~bit;
585 xkb_state_update_derived(state);
589 * Updates the state from a set of explicit masks as gained from
590 * xkb_state_serialize_mods and xkb_state_serialize_groups. As noted in the
591 * documentation for these functions in xkbcommon.h, this round-trip is
592 * lossy, and should only be used to update a slave state mirroring the
593 * master, e.g. in a client/server window system.
596 xkb_state_update_mask(struct xkb_state *state,
597 xkb_mod_mask_t base_mods,
598 xkb_mod_mask_t latched_mods,
599 xkb_mod_mask_t locked_mods,
600 xkb_group_index_t base_group,
601 xkb_group_index_t latched_group,
602 xkb_group_index_t locked_group)
604 xkb_mod_index_t num_mods;
607 state->base_mods = 0;
608 state->latched_mods = 0;
609 state->locked_mods = 0;
610 num_mods = xkb_map_num_mods(state->keymap);
612 for (idx = 0; idx < num_mods; idx++) {
613 xkb_mod_mask_t mod = (1 << idx);
615 state->base_mods |= mod;
616 if (latched_mods & mod)
617 state->latched_mods |= mod;
618 if (locked_mods & mod)
619 state->locked_mods |= mod;
622 state->base_group = base_group;
623 state->latched_group = latched_group;
624 state->locked_group = locked_group;
626 xkb_state_update_derived(state);
630 * Serialises the requested modifier state into an xkb_mod_mask_t, with all
631 * the same disclaimers as in xkb_state_update_mask.
633 XKB_EXPORT xkb_mod_mask_t
634 xkb_state_serialize_mods(struct xkb_state *state,
635 enum xkb_state_component type)
637 xkb_mod_mask_t ret = 0;
639 if (type == XKB_STATE_EFFECTIVE)
642 if (type & XKB_STATE_DEPRESSED)
643 ret |= state->base_mods;
644 if (type & XKB_STATE_LATCHED)
645 ret |= state->latched_mods;
646 if (type & XKB_STATE_LOCKED)
647 ret |= state->locked_mods;
653 * Serialises the requested group state, with all the same disclaimers as
654 * in xkb_state_update_mask.
656 XKB_EXPORT xkb_group_index_t
657 xkb_state_serialize_group(struct xkb_state *state,
658 enum xkb_state_component type)
660 xkb_group_index_t ret = 0;
662 if (type == XKB_STATE_EFFECTIVE)
665 if (type & XKB_STATE_DEPRESSED)
666 ret += state->base_group;
667 if (type & XKB_STATE_LATCHED)
668 ret += state->latched_group;
669 if (type & XKB_STATE_LOCKED)
670 ret += state->locked_group;
676 * Returns 1 if the given modifier is active with the specified type(s), 0 if
677 * not, or -1 if the modifier is invalid.
680 xkb_state_mod_index_is_active(struct xkb_state *state,
682 enum xkb_state_component type)
686 if (idx >= xkb_map_num_mods(state->keymap))
689 if (type & XKB_STATE_DEPRESSED)
690 ret |= (state->base_mods & (1 << idx));
691 if (type & XKB_STATE_LATCHED)
692 ret |= (state->latched_mods & (1 << idx));
693 if (type & XKB_STATE_LOCKED)
694 ret |= (state->locked_mods & (1 << idx));
700 * Helper function for xkb_state_mod_indices_are_active and
701 * xkb_state_mod_names_are_active.
704 match_mod_masks(struct xkb_state *state, enum xkb_state_match match,
707 uint32_t active = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
709 if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
712 if (match & XKB_STATE_MATCH_ANY)
713 return !!(active & wanted);
715 return (active & wanted) == wanted;
721 * Returns 1 if the modifiers are active with the specified type(s), 0 if
722 * not, or -1 if any of the modifiers are invalid.
725 xkb_state_mod_indices_are_active(struct xkb_state *state,
726 enum xkb_state_component type,
727 enum xkb_state_match match,
731 xkb_mod_index_t idx = 0;
734 xkb_mod_index_t num_mods = xkb_map_num_mods(state->keymap);
738 idx = va_arg(ap, xkb_mod_index_t);
739 if (idx == XKB_MOD_INVALID)
741 if (idx >= num_mods) {
745 wanted |= (1 << idx);
752 return match_mod_masks(state, match, wanted);
756 * Returns 1 if the given modifier is active with the specified type(s), 0 if
757 * not, or -1 if the modifier is invalid.
760 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
761 enum xkb_state_component type)
763 xkb_mod_index_t idx = xkb_map_mod_get_index(state->keymap, name);
765 if (idx == XKB_MOD_INVALID)
768 return xkb_state_mod_index_is_active(state, idx, type);
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.
775 XKB_EXPORT ATTR_NULL_SENTINEL int
776 xkb_state_mod_names_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;
789 str = va_arg(ap, const char *);
792 idx = xkb_map_mod_get_index(state->keymap, str);
793 if (idx == XKB_MOD_INVALID) {
797 wanted |= (1 << idx);
804 return match_mod_masks(state, match, wanted);
808 * Returns 1 if the given group is active with the specified type(s), 0 if
809 * not, or -1 if the group is invalid.
812 xkb_state_group_index_is_active(struct xkb_state *state,
813 xkb_group_index_t idx,
814 enum xkb_state_component type)
818 if (idx >= xkb_map_num_groups(state->keymap))
821 if (type & XKB_STATE_DEPRESSED)
822 ret |= (state->base_group == idx);
823 if (type & XKB_STATE_LATCHED)
824 ret |= (state->latched_group == idx);
825 if (type & XKB_STATE_LOCKED)
826 ret |= (state->locked_group == idx);
832 * Returns 1 if the given modifier is active with the specified type(s), 0 if
833 * not, or -1 if the modifier is invalid.
836 xkb_state_group_name_is_active(struct xkb_state *state, const char *name,
837 enum xkb_state_component type)
839 xkb_group_index_t idx = xkb_map_group_get_index(state->keymap, name);
841 if (idx == XKB_GROUP_INVALID)
844 return xkb_state_group_index_is_active(state, idx, type);
848 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
851 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
853 if (idx >= xkb_map_num_leds(state->keymap))
856 return !!(state->leds & (1 << idx));
860 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
863 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
865 xkb_led_index_t idx = xkb_map_led_get_index(state->keymap, name);
867 if (idx == XKB_LED_INVALID)
870 return xkb_state_led_index_is_active(state, idx);