state: use filter->priv instead of modifying the action
[platform/upstream/libxkbcommon.git] / src / state.c
1 /************************************************************
2  * Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
3  *
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.
15  *
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.
24  *
25  ********************************************************/
26
27 /*
28  * Copyright © 2012 Intel Corporation
29  * Copyright © 2012 Ran Benita <ran234@gmail.com>
30  *
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:
37  *
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
40  * Software.
41  *
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.
49  *
50  * Author: Daniel Stone <daniel@fooishbar.org>
51  */
52
53 /*
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)
60  */
61
62 #include <assert.h>
63 #include <stdarg.h>
64
65 #include "xkb-priv.h"
66
67 struct xkb_filter {
68     union xkb_action action;
69     const struct xkb_key *key;
70     uint32_t priv;
71     int (*func)(struct xkb_state *state,
72                 struct xkb_filter *filter,
73                 const struct xkb_key *key,
74                 enum xkb_key_direction direction);
75     int refcnt;
76 };
77
78 struct xkb_state {
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 */
83
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 */
88
89     /*
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.
92      */
93     xkb_mod_mask_t set_mods;
94     xkb_mod_mask_t clear_mods;
95     /*
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.
100      */
101     int16_t mod_key_count[sizeof(xkb_mod_mask_t) * 8];
102
103     uint32_t leds;
104
105     int refcnt;
106     darray(struct xkb_filter) filters;
107     struct xkb_keymap *keymap;
108 };
109
110 static const union xkb_action fake = { .type = ACTION_TYPE_NONE };
111
112 static const union xkb_action *
113 xkb_key_get_action(struct xkb_state *state, const struct xkb_key *key)
114 {
115     xkb_group_index_t group;
116     xkb_level_index_t level;
117
118     if (!key->actions)
119         return &fake;
120
121     group = xkb_key_get_group(state, key);
122     if (group == XKB_GROUP_INVALID)
123         return &fake;
124
125     level = xkb_key_get_level(state, key, group);
126     if (level == XKB_LEVEL_INVALID)
127         return &fake;
128
129     return XkbKeyActionEntry(key, group, level);
130 }
131
132 static struct xkb_filter *
133 xkb_filter_new(struct xkb_state *state)
134 {
135     struct xkb_filter *filter = NULL, *iter;
136
137     darray_foreach(iter, state->filters) {
138         if (iter->func)
139             continue;
140         filter = iter;
141         break;
142     }
143
144     if (!filter) {
145         darray_resize0(state->filters, darray_size(state->filters) + 1);
146         filter = &darray_item(state->filters, darray_size(state->filters) -1);
147     }
148
149     filter->refcnt = 1;
150     return filter;
151 }
152
153 /***====================================================================***/
154
155 static int
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)
160 {
161     if (key != filter->key) {
162         filter->action.group.flags &= ~ACTION_LOCK_CLEAR;
163         return 1;
164     }
165
166     if (direction == XKB_KEY_DOWN) {
167         filter->refcnt++;
168         return 0;
169     }
170     else if (--filter->refcnt > 0) {
171         return 0;
172     }
173
174     state->base_group = filter->priv;
175
176     if (filter->action.group.flags & ACTION_LOCK_CLEAR)
177         state->locked_group = 0;
178
179     filter->func = NULL;
180     return 1;
181 }
182
183 static void
184 xkb_filter_group_set_new(struct xkb_state *state, struct xkb_filter *filter)
185 {
186     filter->priv = state->base_group;
187     if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
188         state->base_group = filter->action.group.group;
189     else
190         state->base_group += filter->action.group.group;
191 }
192
193 static int
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)
198 {
199     if (key != filter->key)
200         return 1;
201
202     if (direction == XKB_KEY_DOWN) {
203         filter->refcnt++;
204         return 0;
205     }
206     if (--filter->refcnt > 0)
207         return 0;
208
209     filter->func = NULL;
210     return 1;
211 }
212
213 static void
214 xkb_filter_group_lock_new(struct xkb_state *state, struct xkb_filter *filter)
215 {
216     if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
217         state->locked_group = filter->action.group.group;
218     else
219         state->locked_group += filter->action.group.group;
220 }
221
222 static int
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)
227 {
228     if (key != filter->key) {
229         filter->action.mods.flags &= ~ACTION_LOCK_CLEAR;
230         return 1;
231     }
232
233     if (direction == XKB_KEY_DOWN) {
234         filter->refcnt++;
235         return 0;
236     }
237     else if (--filter->refcnt > 0) {
238         return 0;
239     }
240
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;
244
245     filter->func = NULL;
246     return 1;
247 }
248
249 static void
250 xkb_filter_mod_set_new(struct xkb_state *state, struct xkb_filter *filter)
251 {
252     state->set_mods = filter->action.mods.mods.mask;
253 }
254
255 static int
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)
260 {
261     if (key != filter->key)
262         return 1;
263
264     if (direction == XKB_KEY_DOWN) {
265         filter->refcnt++;
266         return 0;
267     }
268     if (--filter->refcnt > 0)
269         return 0;
270
271     state->locked_mods &= ~filter->priv;
272
273     filter->func = NULL;
274     return 1;
275 }
276
277 static void
278 xkb_filter_mod_lock_new(struct xkb_state *state, struct xkb_filter *filter)
279 {
280     filter->priv = state->locked_mods & filter->action.mods.mods.mask;
281     state->locked_mods |= filter->action.mods.mods.mask;
282 }
283
284 enum xkb_key_latch_state {
285     NO_LATCH,
286     LATCH_KEY_DOWN,
287     LATCH_PENDING,
288 };
289
290 static bool
291 xkb_action_breaks_latch(const union xkb_action *action)
292 {
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:
302         return true;
303     default:
304         return false;
305     }
306 }
307
308 static int
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)
313 {
314     enum xkb_key_latch_state latch = filter->priv;
315
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
320          * modifier. */
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;
330             }
331             else {
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;
335             }
336             filter->key = key;
337             state->latched_mods &= ~filter->action.mods.mods.mask;
338             /* XXX beep beep! */
339             return 0;
340         }
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;
345             filter->func = NULL;
346             return 1;
347         }
348     }
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
354          * latched. */
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;
363             else
364                 state->clear_mods = filter->action.mods.mods.mask;
365             state->locked_mods &= ~filter->action.mods.mods.mask;
366             filter->func = NULL;
367         }
368         else {
369             latch = LATCH_PENDING;
370             state->clear_mods = filter->action.mods.mods.mask;
371             state->latched_mods |= filter->action.mods.mods.mask;
372             /* XXX beep beep! */
373         }
374     }
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. */
380         latch = NO_LATCH;
381     }
382
383     filter->priv = latch;
384
385     return 1;
386 }
387
388 static void
389 xkb_filter_mod_latch_new(struct xkb_state *state, struct xkb_filter *filter)
390 {
391     filter->priv = LATCH_KEY_DOWN;
392     state->set_mods = filter->action.mods.mods.mask;
393 }
394
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 },
410 };
411
412 /**
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.
416  */
417 static void
418 xkb_filter_apply_all(struct xkb_state *state,
419                      const struct xkb_key *key,
420                      enum xkb_key_direction direction)
421 {
422     struct xkb_filter *filter;
423     const union xkb_action *action;
424     int send = 1;
425
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) {
429         if (!filter->func)
430             continue;
431         send &= filter->func(state, filter, key, direction);
432     }
433
434     if (!send || direction == XKB_KEY_UP)
435         return;
436
437     action = xkb_key_get_action(state, key);
438     if (!filter_action_funcs[action->type].new)
439         return;
440
441     filter = xkb_filter_new(state);
442     if (!filter)
443         return; /* WSGO */
444
445     filter->key = key;
446     filter->func = filter_action_funcs[action->type].func;
447     filter->action = *action;
448     filter_action_funcs[action->type].new(state, filter);
449 }
450
451 XKB_EXPORT struct xkb_state *
452 xkb_state_new(struct xkb_keymap *keymap)
453 {
454     struct xkb_state *ret;
455
456     ret = calloc(sizeof(*ret), 1);
457     if (!ret)
458         return NULL;
459
460     ret->refcnt = 1;
461     ret->keymap = xkb_map_ref(keymap);
462
463     return ret;
464 }
465
466 XKB_EXPORT struct xkb_state *
467 xkb_state_ref(struct xkb_state *state)
468 {
469     state->refcnt++;
470     return state;
471 }
472
473 XKB_EXPORT void
474 xkb_state_unref(struct xkb_state *state)
475 {
476     if (--state->refcnt > 0)
477         return;
478
479     xkb_map_unref(state->keymap);
480     darray_free(state->filters);
481     free(state);
482 }
483
484 XKB_EXPORT struct xkb_keymap *
485 xkb_state_get_map(struct xkb_state *state)
486 {
487     return state->keymap;
488 }
489
490 /**
491  * Update the LED state to match the rest of the xkb_state.
492  */
493 static void
494 xkb_state_led_update_all(struct xkb_state *state)
495 {
496     xkb_led_index_t led;
497
498     state->leds = 0;
499
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;
504
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);
513
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);
522
523         if (map->ctrls) {
524             if ((map->ctrls & state->keymap->enabled_ctrls))
525                 state->leds |= (1 << led);
526         }
527     }
528 }
529
530 /**
531  * Calculates the derived state (effective mods/group and LEDs) from an
532  * up-to-date xkb_state.
533  */
534 static void
535 xkb_state_update_derived(struct xkb_state *state)
536 {
537     state->mods =
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 */
543
544     xkb_state_led_update_all(state);
545 }
546
547 /**
548  * Given a particular key event, updates the state structure to reflect the
549  * new modifiers.
550  */
551 XKB_EXPORT void
552 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
553                      enum xkb_key_direction direction)
554 {
555     xkb_mod_index_t i;
556     xkb_mod_mask_t bit;
557     const struct xkb_key *key = XkbKey(state->keymap, kc);
558     if (!key)
559         return;
560
561     state->set_mods = 0;
562     state->clear_mods = 0;
563
564     xkb_filter_apply_all(state, key, direction);
565
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;
571         }
572     }
573
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;
580             }
581             state->clear_mods &= ~bit;
582         }
583     }
584
585     xkb_state_update_derived(state);
586 }
587
588 /**
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.
594  */
595 XKB_EXPORT void
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)
603 {
604     xkb_mod_index_t num_mods;
605     xkb_mod_index_t idx;
606
607     state->base_mods = 0;
608     state->latched_mods = 0;
609     state->locked_mods = 0;
610     num_mods = xkb_map_num_mods(state->keymap);
611
612     for (idx = 0; idx < num_mods; idx++) {
613         xkb_mod_mask_t mod = (1 << idx);
614         if (base_mods & mod)
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;
620     }
621
622     state->base_group = base_group;
623     state->latched_group = latched_group;
624     state->locked_group = locked_group;
625
626     xkb_state_update_derived(state);
627 }
628
629 /**
630  * Serialises the requested modifier state into an xkb_mod_mask_t, with all
631  * the same disclaimers as in xkb_state_update_mask.
632  */
633 XKB_EXPORT xkb_mod_mask_t
634 xkb_state_serialize_mods(struct xkb_state *state,
635                          enum xkb_state_component type)
636 {
637     xkb_mod_mask_t ret = 0;
638
639     if (type == XKB_STATE_EFFECTIVE)
640         return state->mods;
641
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;
648
649     return ret;
650 }
651
652 /**
653  * Serialises the requested group state, with all the same disclaimers as
654  * in xkb_state_update_mask.
655  */
656 XKB_EXPORT xkb_group_index_t
657 xkb_state_serialize_group(struct xkb_state *state,
658                           enum xkb_state_component type)
659 {
660     xkb_group_index_t ret = 0;
661
662     if (type == XKB_STATE_EFFECTIVE)
663         return state->group;
664
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;
671
672     return ret;
673 }
674
675 /**
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.
678  */
679 XKB_EXPORT int
680 xkb_state_mod_index_is_active(struct xkb_state *state,
681                               xkb_mod_index_t idx,
682                               enum xkb_state_component type)
683 {
684     int ret = 0;
685
686     if (idx >= xkb_map_num_mods(state->keymap))
687         return -1;
688
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));
695
696     return !!ret;
697 }
698
699 /**
700  * Helper function for xkb_state_mod_indices_are_active and
701  * xkb_state_mod_names_are_active.
702  */
703 static int
704 match_mod_masks(struct xkb_state *state, enum xkb_state_match match,
705                 uint32_t wanted)
706 {
707     uint32_t active = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
708
709     if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
710         return 0;
711
712     if (match & XKB_STATE_MATCH_ANY)
713         return !!(active & wanted);
714     else
715         return (active & wanted) == wanted;
716
717     return 0;
718 }
719
720 /**
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.
723  */
724 XKB_EXPORT int
725 xkb_state_mod_indices_are_active(struct xkb_state *state,
726                                  enum xkb_state_component type,
727                                  enum xkb_state_match match,
728                                  ...)
729 {
730     va_list ap;
731     xkb_mod_index_t idx = 0;
732     uint32_t wanted = 0;
733     int ret = 0;
734     xkb_mod_index_t num_mods = xkb_map_num_mods(state->keymap);
735
736     va_start(ap, match);
737     while (1) {
738         idx = va_arg(ap, xkb_mod_index_t);
739         if (idx == XKB_MOD_INVALID)
740             break;
741         if (idx >= num_mods) {
742             ret = -1;
743             break;
744         }
745         wanted |= (1 << idx);
746     }
747     va_end(ap);
748
749     if (ret == -1)
750         return ret;
751
752     return match_mod_masks(state, match, wanted);
753 }
754
755 /**
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.
758  */
759 XKB_EXPORT int
760 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
761                              enum xkb_state_component type)
762 {
763     xkb_mod_index_t idx = xkb_map_mod_get_index(state->keymap, name);
764
765     if (idx == XKB_MOD_INVALID)
766         return -1;
767
768     return xkb_state_mod_index_is_active(state, idx, type);
769 }
770
771 /**
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.
774  */
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,
779                                ...)
780 {
781     va_list ap;
782     xkb_mod_index_t idx = 0;
783     const char *str;
784     uint32_t wanted = 0;
785     int ret = 0;
786
787     va_start(ap, match);
788     while (1) {
789         str = va_arg(ap, const char *);
790         if (str == NULL)
791             break;
792         idx = xkb_map_mod_get_index(state->keymap, str);
793         if (idx == XKB_MOD_INVALID) {
794             ret = -1;
795             break;
796         }
797         wanted |= (1 << idx);
798     }
799     va_end(ap);
800
801     if (ret == -1)
802         return ret;
803
804     return match_mod_masks(state, match, wanted);
805 }
806
807 /**
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.
810  */
811 XKB_EXPORT int
812 xkb_state_group_index_is_active(struct xkb_state *state,
813                                 xkb_group_index_t idx,
814                                 enum xkb_state_component type)
815 {
816     int ret = 0;
817
818     if (idx >= xkb_map_num_groups(state->keymap))
819         return -1;
820
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);
827
828     return ret;
829 }
830
831 /**
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.
834  */
835 XKB_EXPORT int
836 xkb_state_group_name_is_active(struct xkb_state *state, const char *name,
837                                enum xkb_state_component type)
838 {
839     xkb_group_index_t idx = xkb_map_group_get_index(state->keymap, name);
840
841     if (idx == XKB_GROUP_INVALID)
842         return -1;
843
844     return xkb_state_group_index_is_active(state, idx, type);
845 }
846
847 /**
848  * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
849  */
850 XKB_EXPORT int
851 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
852 {
853     if (idx >= xkb_map_num_leds(state->keymap))
854         return -1;
855
856     return !!(state->leds & (1 << idx));
857 }
858
859 /**
860  * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
861  */
862 XKB_EXPORT int
863 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
864 {
865     xkb_led_index_t idx = xkb_map_led_get_index(state->keymap, name);
866
867     if (idx == XKB_LED_INVALID)
868         return -1;
869
870     return xkb_state_led_index_is_active(state, idx);
871 }