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 #include "xkbcommon/xkbcommon.h"
68 #include "XKBcommonint.h"
72 struct xkb_state *state;
73 union xkb_action action;
74 xkb_keycode_t keycode;
76 int (*func)(struct xkb_filter *filter, xkb_keycode_t key,
77 enum xkb_key_direction direction);
79 struct xkb_filter *next;
82 static union xkb_action *
83 xkb_key_get_action(struct xkb_state *state, xkb_keycode_t key)
87 if (!XkbKeyHasActions(state->xkb, key) ||
88 !XkbKeycodeInRange(state->xkb, key)) {
89 static union xkb_action fake;
90 memset(&fake, 0, sizeof(fake));
91 fake.type = XkbSA_NoAction;
95 group = xkb_key_get_group(state, key);
96 level = xkb_key_get_level(state, key, group);
98 return XkbKeyActionEntry(state->xkb, key, level, group);
101 static struct xkb_filter *
102 xkb_filter_new(struct xkb_state *state)
105 int old_size = state->num_filters;
106 struct xkb_filter *filters = state->filters;
108 for (i = 0; i < state->num_filters; i++) {
111 filters[i].state = state;
112 filters[i].refcnt = 1;
116 if (state->num_filters > 0)
117 state->num_filters *= 2;
119 state->num_filters = 4;
120 filters = realloc(filters, state->num_filters * sizeof(*filters));
121 if (!filters) { /* WSGO */
122 state->num_filters = old_size;
125 state->filters = filters;
126 memset(&filters[old_size], 0,
127 (state->num_filters - old_size) * sizeof(*filters));
129 filters[old_size].state = state;
130 filters[old_size].refcnt = 1;
132 return &filters[old_size];
135 /***====================================================================***/
138 xkb_filter_group_set_func(struct xkb_filter *filter, xkb_keycode_t keycode,
139 enum xkb_key_direction direction)
141 if (keycode != filter->keycode) {
142 filter->action.group.flags &= ~XkbSA_ClearLocks;
146 if (direction == XKB_KEY_DOWN) {
150 else if (--filter->refcnt > 0) {
154 if (filter->action.group.flags & XkbSA_GroupAbsolute)
155 filter->state->base_group = filter->action.group.group;
157 filter->state->base_group = -filter->action.group.group;
158 if (filter->action.group.flags & XkbSA_ClearLocks)
159 filter->state->locked_group = 0;
167 xkb_filter_group_set_new(struct xkb_state *state, xkb_keycode_t keycode,
168 union xkb_action *action)
170 struct xkb_filter *filter = xkb_filter_new(state);
172 if (!filter) /* WSGO */
174 filter->keycode = keycode;
175 filter->func = xkb_filter_group_set_func;
176 filter->action = *action;
178 if (action->group.flags & XkbSA_GroupAbsolute) {
179 filter->action.group.group = filter->state->base_group;
180 filter->state->base_group = action->group.group;
183 filter->state->base_group += action->group.group;
190 xkb_filter_group_lock_func(struct xkb_filter *filter, xkb_keycode_t keycode,
191 enum xkb_key_direction direction)
193 if (keycode != filter->keycode)
196 if (direction == XKB_KEY_DOWN) {
200 if (--filter->refcnt > 0)
208 xkb_filter_group_lock_new(struct xkb_state *state, xkb_keycode_t keycode,
209 union xkb_action *action)
211 struct xkb_filter *filter = xkb_filter_new(state);
216 filter->keycode = keycode;
217 filter->func = xkb_filter_group_lock_func;
218 filter->action = *action;
220 if (action->group.flags & XkbSA_GroupAbsolute)
221 filter->state->locked_group = action->group.group;
223 filter->state->locked_group += action->group.group;
229 xkb_filter_mod_set_func(struct xkb_filter *filter, xkb_keycode_t keycode,
230 enum xkb_key_direction direction)
232 if (keycode != filter->keycode) {
233 filter->action.mods.flags &= ~XkbSA_ClearLocks;
237 if (direction == XKB_KEY_DOWN) {
241 else if (--filter->refcnt > 0) {
245 filter->state->base_mods &= ~(filter->action.mods.mask);
246 if (filter->action.mods.flags & XkbSA_ClearLocks)
247 filter->state->locked_mods &= ~filter->action.mods.mask;
255 xkb_filter_mod_set_new(struct xkb_state *state, xkb_keycode_t keycode,
256 union xkb_action *action)
258 struct xkb_filter *filter = xkb_filter_new(state);
260 if (!filter) /* WSGO */
262 filter->keycode = keycode;
263 filter->func = xkb_filter_mod_set_func;
264 filter->action = *action;
266 filter->state->base_mods |= action->mods.mask;
272 xkb_filter_mod_lock_func(struct xkb_filter *filter, xkb_keycode_t keycode,
273 enum xkb_key_direction direction)
275 if (keycode != filter->keycode)
278 if (direction == XKB_KEY_DOWN) {
282 if (--filter->refcnt > 0)
285 filter->state->locked_mods &= ~filter->priv;
291 xkb_filter_mod_lock_new(struct xkb_state *state, xkb_keycode_t keycode,
292 union xkb_action *action)
294 struct xkb_filter *filter = xkb_filter_new(state);
296 if (!filter) /* WSGO */
299 filter->keycode = keycode;
300 filter->func = xkb_filter_mod_lock_func;
301 filter->action = *action;
302 filter->priv = state->locked_mods & action->mods.mask;
303 state->locked_mods |= action->mods.mask;
308 enum xkb_key_latch_state {
315 xkb_filter_mod_latch_func(struct xkb_filter *filter, xkb_keycode_t keycode,
316 enum xkb_key_direction direction)
318 enum xkb_key_latch_state latch = filter->priv;
320 if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
321 /* If this is a new keypress and we're awaiting our single latched
322 * keypress, then either break the latch if any random key is pressed,
323 * or promote it to a lock or plain base set if it's the same
325 union xkb_action *action = xkb_key_get_action(filter->state, keycode);
326 if (action->type == XkbSA_LatchMods &&
327 action->mods.flags == filter->action.mods.flags &&
328 action->mods.mask == filter->action.mods.mask) {
329 filter->action = *action;
330 if (filter->action.mods.flags & XkbSA_LatchToLock) {
331 filter->action.type = XkbSA_LockMods;
332 filter->func = xkb_filter_mod_lock_func;
333 filter->state->locked_mods |= filter->action.mods.mask;
336 filter->action.type = XkbSA_SetMods;
337 filter->func = xkb_filter_mod_set_func;
338 filter->state->base_mods |= filter->action.mods.mask;
340 filter->keycode = keycode;
341 filter->state->latched_mods &= ~filter->action.mods.mask;
345 else if (((1 << action->type) & XkbSA_BreakLatch)) {
346 /* XXX: This may be totally broken, we might need to break the
347 * latch in the next run after this press? */
348 filter->state->latched_mods &= ~filter->action.mods.mask;
353 else if (direction == XKB_KEY_UP && keycode == filter->keycode) {
354 /* Our key got released. If we've set it to clear locks, and we
355 * currently have the same modifiers locked, then release them and
356 * don't actually latch. Else we've actually hit the latching
357 * stage, so set PENDING and move our modifier from base to
359 if (latch == NO_LATCH ||
360 ((filter->action.mods.flags & XkbSA_ClearLocks) &&
361 (filter->state->locked_mods & filter->action.mods.mask) ==
362 filter->action.mods.mask)) {
363 /* XXX: We might be a bit overenthusiastic about clearing
364 * mods other filters have set here? */
365 if (latch == LATCH_PENDING)
366 filter->state->latched_mods &= ~filter->action.mods.mask;
368 filter->state->base_mods &= ~filter->action.mods.mask;
369 filter->state->locked_mods &= ~filter->action.mods.mask;
373 latch = LATCH_PENDING;
374 filter->state->base_mods &= ~filter->action.mods.mask;
375 filter->state->latched_mods |= filter->action.mods.mask;
379 else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
380 /* Someone's pressed another key while we've still got the latching
381 * key held down, so keep the base modifier state active (from
382 * xkb_filter_mod_latch_new), but don't trip the latch, just clear
383 * it as soon as the modifier gets released. */
387 filter->priv = latch;
393 xkb_filter_mod_latch_new(struct xkb_state *state, xkb_keycode_t keycode,
394 union xkb_action *action)
396 struct xkb_filter *filter = xkb_filter_new(state);
397 enum xkb_key_latch_state latch = LATCH_KEY_DOWN;
399 if (!filter) /* WSGO */
401 filter->keycode = keycode;
402 filter->priv = latch;
403 filter->func = xkb_filter_mod_latch_func;
404 filter->action = *action;
406 filter->state->base_mods |= action->mods.mask;
412 * Applies any relevant filters to the key, first from the list of filters
413 * that are currently active, then if no filter has claimed the key, possibly
414 * apply a new filter from the key action.
417 xkb_filter_apply_all(struct xkb_state *state, xkb_keycode_t key,
418 enum xkb_key_direction direction)
420 struct xkb_filter *filters = state->filters;
421 union xkb_action *act = NULL;
425 /* First run through all the currently active filters and see if any of
426 * them have claimed this event. */
427 for (i = 0; i < state->num_filters; i++) {
428 if (!filters[i].func)
430 send &= (*filters[i].func)(&filters[i], key, direction);
433 if (!send || direction == XKB_KEY_UP)
436 act = xkb_key_get_action(state, key);
439 send = xkb_filter_mod_set_new(state, key, act);
441 case XkbSA_LatchMods:
442 send = xkb_filter_mod_latch_new(state, key, act);
445 send = xkb_filter_mod_lock_new(state, key, act);
448 send = xkb_filter_group_set_new(state, key, act);
451 case XkbSA_LatchGroup:
452 send = xkb_filter_mod_latch_new(state, key, act);
455 case XkbSA_LockGroup:
456 send = xkb_filter_group_lock_new(state, key, act);
463 _X_EXPORT struct xkb_state *
464 xkb_state_new(struct xkb_keymap *xkb)
466 struct xkb_state *ret;
471 ret = calloc(sizeof(*ret), 1);
476 ret->xkb = xkb_map_ref(xkb);
481 _X_EXPORT struct xkb_state *
482 xkb_state_ref(struct xkb_state *state)
489 xkb_state_unref(struct xkb_state *state)
492 assert(state->refcnt >= 0);
493 if (state->refcnt > 0)
496 xkb_map_unref(state->xkb);
497 free(state->filters);
502 * Update the LED state to match the rest of the xkb_state.
505 xkb_state_led_update_all(struct xkb_state *state)
511 for (led = 0; led < XkbNumIndicators; led++) {
512 struct xkb_indicator_map *map = &state->xkb->indicators->maps[led];
513 uint32_t mod_mask = 0;
514 uint32_t group_mask = 0;
516 if (!map->which_mods && !map->which_groups && !map->ctrls)
519 if (map->which_mods) {
520 if (map->which_mods & XkbIM_UseBase)
521 mod_mask |= state->base_mods;
522 if (map->which_mods & XkbIM_UseLatched)
523 mod_mask |= state->latched_mods;
524 if (map->which_mods & XkbIM_UseLocked)
525 mod_mask |= state->locked_mods;
526 if (map->which_mods & XkbIM_UseEffective)
527 mod_mask |= state->mods;
528 if ((map->mods.mask & mod_mask))
529 state->leds |= (1 << led);
531 else if (map->which_groups) {
532 if (map->which_mods & XkbIM_UseBase)
533 group_mask |= (1 << state->base_group);
534 if (map->which_mods & XkbIM_UseLatched)
535 group_mask |= (1 << state->latched_group);
536 if (map->which_mods & XkbIM_UseLocked)
537 group_mask |= (1 << state->locked_group);
538 if (map->which_mods & XkbIM_UseEffective)
539 group_mask |= (1 << state->group);
540 if ((map->groups & group_mask))
541 state->leds |= (1 << led);
543 else if (map->ctrls) {
544 if ((map->ctrls & state->xkb->ctrls->enabled_ctrls))
545 state->leds |= (1 << led);
551 * Calculates the derived state (effective mods/group and LEDs) from an
552 * up-to-date xkb_state.
555 xkb_state_update_derived(struct xkb_state *state)
557 state->mods = (state->base_mods | state->latched_mods | state->locked_mods);
558 /* FIXME: Clamp/wrap locked_group */
559 state->group = state->locked_group + state->base_group +
560 state->latched_group;
561 /* FIXME: Clamp/wrap effective group */
563 xkb_state_led_update_all(state);
567 * Given a particular key event, updates the state structure to reflect the
571 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t key,
572 enum xkb_key_direction direction)
574 xkb_filter_apply_all(state, key, direction);
575 xkb_state_update_derived(state);
579 * Updates the state from a set of explicit masks as gained from
580 * xkb_state_serialise_mods and xkb_state_serialise_groups. As noted in the
581 * documentation for these functions in xkbcommon.h, this round-trip is
582 * lossy, and should only be used to update a slave state mirroring the
583 * master, e.g. in a client/server window system.
586 xkb_state_update_mask(struct xkb_state *state,
587 xkb_mod_mask_t base_mods,
588 xkb_mod_mask_t latched_mods,
589 xkb_mod_mask_t locked_mods,
590 xkb_group_index_t base_group,
591 xkb_group_index_t latched_group,
592 xkb_group_index_t locked_group)
596 state->base_mods = 0;
597 state->latched_mods = 0;
598 state->locked_mods = 0;
599 for (mod = 0; mod < xkb_map_num_mods(state->xkb); mod++) {
600 xkb_mod_mask_t idx = (1 << mod);
602 state->base_mods |= idx;
603 if (latched_mods & idx)
604 state->latched_mods |= idx;
605 if (locked_mods & idx)
606 state->locked_mods |= idx;
609 state->base_group = base_group;
610 state->latched_group = latched_group;
611 state->locked_group = locked_group;
613 xkb_state_update_derived(state);
617 * Serialises the requested modifier state into an xkb_mod_mask_t, with all
618 * the same disclaimers as in xkb_state_update_mask.
620 _X_EXPORT xkb_mod_mask_t
621 xkb_state_serialise_mods(struct xkb_state *state,
622 enum xkb_state_component type)
624 xkb_mod_mask_t ret = 0;
626 if (type == XKB_STATE_EFFECTIVE)
629 if (type & XKB_STATE_DEPRESSED)
630 ret |= state->base_mods;
631 if (type & XKB_STATE_LATCHED)
632 ret |= state->latched_mods;
633 if (type & XKB_STATE_LOCKED)
634 ret |= state->locked_mods;
640 * Serialises the requested group state, with all the same disclaimers as
641 * in xkb_state_update_mask.
643 _X_EXPORT xkb_group_index_t
644 xkb_state_serialise_group(struct xkb_state *state,
645 enum xkb_state_component type)
647 xkb_group_index_t ret = 0;
649 if (type == XKB_STATE_EFFECTIVE)
652 if (type & XKB_STATE_DEPRESSED)
653 ret += state->base_group;
654 if (type & XKB_STATE_LATCHED)
655 ret += state->latched_group;
656 if (type & XKB_STATE_LOCKED)
657 ret += state->locked_group;
663 * Returns 1 if the given modifier is active with the specified type(s), 0 if
664 * not, or -1 if the modifier is invalid.
667 xkb_state_mod_index_is_active(struct xkb_state *state,
669 enum xkb_state_component type)
673 if (idx >= xkb_map_num_mods(state->xkb))
676 if (type & XKB_STATE_DEPRESSED)
677 ret |= (state->base_mods & (1 << idx));
678 if (type & XKB_STATE_LATCHED)
679 ret |= (state->latched_mods & (1 << idx));
680 if (type & XKB_STATE_LOCKED)
681 ret |= (state->locked_mods & (1 << idx));
687 * Returns 1 if the given modifier is active with the specified type(s), 0 if
688 * not, or -1 if the modifier is invalid.
691 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
692 enum xkb_state_component type)
694 xkb_mod_index_t idx = xkb_map_mod_get_index(state->xkb, name);
696 if (idx == XKB_MOD_INVALID)
699 return xkb_state_mod_index_is_active(state, idx, type);
703 * Returns 1 if the given group is active with the specified type(s), 0 if
704 * not, or -1 if the group is invalid.
707 xkb_state_group_index_is_active(struct xkb_state *state,
708 xkb_group_index_t idx,
709 enum xkb_state_component type)
713 if (idx >= xkb_map_num_groups(state->xkb))
716 if (type & XKB_STATE_DEPRESSED)
717 ret |= (state->base_group == idx);
718 if (type & XKB_STATE_LATCHED)
719 ret |= (state->latched_group == idx);
720 if (type & XKB_STATE_LOCKED)
721 ret |= (state->locked_group == idx);
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_group_name_is_active(struct xkb_state *state, const char *name,
732 enum xkb_state_component type)
734 xkb_group_index_t idx = xkb_map_group_get_index(state->xkb, name);
736 if (idx == XKB_GROUP_INVALID)
739 return xkb_state_group_index_is_active(state, idx, type);
743 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
746 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
748 if (idx >= xkb_map_num_leds(state->xkb))
751 return !!(state->leds & (1 << idx));
755 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
758 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
760 xkb_led_index_t idx = xkb_map_led_get_index(state->xkb, name);
762 if (idx == XKB_LED_INVALID)
765 return xkb_state_led_index_is_active(state, idx);