state: don't keep the xkb_state in the filters
[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     if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
175         state->base_group = filter->action.group.group;
176     else
177         state->base_group = -filter->action.group.group;
178     if (filter->action.group.flags & ACTION_LOCK_CLEAR)
179         state->locked_group = 0;
180
181     filter->func = NULL;
182     return 1;
183 }
184
185 static int
186 xkb_filter_group_set_new(struct xkb_state *state,
187                          const struct xkb_key *key,
188                          const union xkb_action *action)
189 {
190     struct xkb_filter *filter = xkb_filter_new(state);
191     if (!filter)
192         return -1;
193
194     filter->key = key;
195     filter->func = xkb_filter_group_set_func;
196     filter->action = *action;
197
198     if (action->group.flags & ACTION_ABSOLUTE_SWITCH) {
199         filter->action.group.group = state->base_group;
200         state->base_group = action->group.group;
201     }
202     else {
203         state->base_group += action->group.group;
204     }
205
206     return 1;
207 }
208
209 static int
210 xkb_filter_group_lock_func(struct xkb_state *state,
211                            struct xkb_filter *filter,
212                            const struct xkb_key *key,
213                            enum xkb_key_direction direction)
214 {
215     if (key != filter->key)
216         return 1;
217
218     if (direction == XKB_KEY_DOWN) {
219         filter->refcnt++;
220         return 0;
221     }
222     if (--filter->refcnt > 0)
223         return 0;
224
225     filter->func = NULL;
226     return 1;
227 }
228
229 static int
230 xkb_filter_group_lock_new(struct xkb_state *state,
231                           const struct xkb_key *key,
232                           const union xkb_action *action)
233 {
234     struct xkb_filter *filter = xkb_filter_new(state);
235     if (!filter)
236         return -1;
237
238     filter->key = key;
239     filter->func = xkb_filter_group_lock_func;
240     filter->action = *action;
241
242     if (action->group.flags & ACTION_ABSOLUTE_SWITCH)
243         state->locked_group = action->group.group;
244     else
245         state->locked_group += action->group.group;
246
247     return 1;
248 }
249
250 static int
251 xkb_filter_mod_set_func(struct xkb_state *state,
252                         struct xkb_filter *filter,
253                         const struct xkb_key *key,
254                         enum xkb_key_direction direction)
255 {
256     if (key != filter->key) {
257         filter->action.mods.flags &= ~ACTION_LOCK_CLEAR;
258         return 1;
259     }
260
261     if (direction == XKB_KEY_DOWN) {
262         filter->refcnt++;
263         return 0;
264     }
265     else if (--filter->refcnt > 0) {
266         return 0;
267     }
268
269     state->clear_mods = filter->action.mods.mods.mask;
270     if (filter->action.mods.flags & ACTION_LOCK_CLEAR)
271         state->locked_mods &= ~filter->action.mods.mods.mask;
272
273     filter->func = NULL;
274     return 1;
275 }
276
277 static int
278 xkb_filter_mod_set_new(struct xkb_state *state,
279                        const struct xkb_key *key,
280                        const union xkb_action *action)
281 {
282     struct xkb_filter *filter = xkb_filter_new(state);
283     if (!filter)
284         return -1;
285
286     filter->key = key;
287     filter->func = xkb_filter_mod_set_func;
288     filter->action = *action;
289
290     state->set_mods = action->mods.mods.mask;
291
292     return 1;
293 }
294
295 static int
296 xkb_filter_mod_lock_func(struct xkb_state *state,
297                          struct xkb_filter *filter,
298                          const struct xkb_key *key,
299                          enum xkb_key_direction direction)
300 {
301     if (key != filter->key)
302         return 1;
303
304     if (direction == XKB_KEY_DOWN) {
305         filter->refcnt++;
306         return 0;
307     }
308     if (--filter->refcnt > 0)
309         return 0;
310
311     state->locked_mods &= ~filter->priv;
312
313     filter->func = NULL;
314     return 1;
315 }
316
317 static int
318 xkb_filter_mod_lock_new(struct xkb_state *state,
319                         const struct xkb_key *key,
320                         const union xkb_action *action)
321 {
322     struct xkb_filter *filter = xkb_filter_new(state);
323     if (!filter)
324         return -1;
325
326     filter->key = key;
327     filter->func = xkb_filter_mod_lock_func;
328     filter->action = *action;
329     filter->priv = state->locked_mods & action->mods.mods.mask;
330
331     state->locked_mods |= action->mods.mods.mask;
332
333     return 1;
334 }
335
336 enum xkb_key_latch_state {
337     NO_LATCH,
338     LATCH_KEY_DOWN,
339     LATCH_PENDING,
340 };
341
342 static bool
343 xkb_action_breaks_latch(const union xkb_action *action)
344 {
345     switch (action->type) {
346     case ACTION_TYPE_NONE:
347     case ACTION_TYPE_PTR_BUTTON:
348     case ACTION_TYPE_PTR_LOCK:
349     case ACTION_TYPE_CTRL_SET:
350     case ACTION_TYPE_CTRL_LOCK:
351     case ACTION_TYPE_KEY_REDIRECT:
352     case ACTION_TYPE_SWITCH_VT:
353     case ACTION_TYPE_TERMINATE:
354         return true;
355     default:
356         return false;
357     }
358 }
359
360 static int
361 xkb_filter_mod_latch_func(struct xkb_state *state,
362                           struct xkb_filter *filter,
363                           const struct xkb_key *key,
364                           enum xkb_key_direction direction)
365 {
366     enum xkb_key_latch_state latch = filter->priv;
367
368     if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
369         /* If this is a new keypress and we're awaiting our single latched
370          * keypress, then either break the latch if any random key is pressed,
371          * or promote it to a lock or plain base set if it's the same
372          * modifier. */
373         const union xkb_action *action = xkb_key_get_action(state, key);
374         if (action->type == ACTION_TYPE_MOD_LATCH &&
375             action->mods.flags == filter->action.mods.flags &&
376             action->mods.mods.mask == filter->action.mods.mods.mask) {
377             filter->action = *action;
378             if (filter->action.mods.flags & ACTION_LATCH_TO_LOCK) {
379                 filter->action.type = ACTION_TYPE_MOD_LOCK;
380                 filter->func = xkb_filter_mod_lock_func;
381                 state->locked_mods |= filter->action.mods.mods.mask;
382             }
383             else {
384                 filter->action.type = ACTION_TYPE_MOD_SET;
385                 filter->func = xkb_filter_mod_set_func;
386                 state->set_mods = filter->action.mods.mods.mask;
387             }
388             filter->key = key;
389             state->latched_mods &= ~filter->action.mods.mods.mask;
390             /* XXX beep beep! */
391             return 0;
392         }
393         else if (xkb_action_breaks_latch(action)) {
394             /* XXX: This may be totally broken, we might need to break the
395              *      latch in the next run after this press? */
396             state->latched_mods &= ~filter->action.mods.mods.mask;
397             filter->func = NULL;
398             return 1;
399         }
400     }
401     else if (direction == XKB_KEY_UP && key == filter->key) {
402         /* Our key got released.  If we've set it to clear locks, and we
403          * currently have the same modifiers locked, then release them and
404          * don't actually latch.  Else we've actually hit the latching
405          * stage, so set PENDING and move our modifier from base to
406          * latched. */
407         if (latch == NO_LATCH ||
408             ((filter->action.mods.flags & ACTION_LOCK_CLEAR) &&
409              (state->locked_mods & filter->action.mods.mods.mask) ==
410              filter->action.mods.mods.mask)) {
411             /* XXX: We might be a bit overenthusiastic about clearing
412              *      mods other filters have set here? */
413             if (latch == LATCH_PENDING)
414                 state->latched_mods &= ~filter->action.mods.mods.mask;
415             else
416                 state->clear_mods = filter->action.mods.mods.mask;
417             state->locked_mods &= ~filter->action.mods.mods.mask;
418             filter->func = NULL;
419         }
420         else {
421             latch = LATCH_PENDING;
422             state->clear_mods = filter->action.mods.mods.mask;
423             state->latched_mods |= filter->action.mods.mods.mask;
424             /* XXX beep beep! */
425         }
426     }
427     else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
428         /* Someone's pressed another key while we've still got the latching
429          * key held down, so keep the base modifier state active (from
430          * xkb_filter_mod_latch_new), but don't trip the latch, just clear
431          * it as soon as the modifier gets released. */
432         latch = NO_LATCH;
433     }
434
435     filter->priv = latch;
436
437     return 1;
438 }
439
440 static int
441 xkb_filter_mod_latch_new(struct xkb_state *state,
442                          const struct xkb_key *key,
443                          const union xkb_action *action)
444 {
445     enum xkb_key_latch_state latch = LATCH_KEY_DOWN;
446     struct xkb_filter *filter = xkb_filter_new(state);
447     if (!filter)
448         return -1;
449
450     filter->key = key;
451     filter->priv = latch;
452     filter->func = xkb_filter_mod_latch_func;
453     filter->action = *action;
454
455     state->set_mods = action->mods.mods.mask;
456
457     return 1;
458 }
459
460 /**
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.
464  */
465 static void
466 xkb_filter_apply_all(struct xkb_state *state,
467                      const struct xkb_key *key,
468                      enum xkb_key_direction direction)
469 {
470     struct xkb_filter *filter;
471     const union xkb_action *act = NULL;
472     int send = 1;
473
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) {
477         if (!filter->func)
478             continue;
479         send &= filter->func(state, filter, key, direction);
480     }
481
482     if (!send || direction == XKB_KEY_UP)
483         return;
484
485     act = xkb_key_get_action(state, key);
486     switch (act->type) {
487     case ACTION_TYPE_MOD_SET:
488         send = xkb_filter_mod_set_new(state, key, act);
489         break;
490     case ACTION_TYPE_MOD_LATCH:
491         send = xkb_filter_mod_latch_new(state, key, act);
492         break;
493     case ACTION_TYPE_MOD_LOCK:
494         send = xkb_filter_mod_lock_new(state, key, act);
495         break;
496     case ACTION_TYPE_GROUP_SET:
497         send = xkb_filter_group_set_new(state, key, act);
498         break;
499 #if 0
500     case ACTION_TYPE_GROUP_LATCH:
501         send = xkb_filter_mod_latch_new(state, key, act);
502         break;
503 #endif
504     case ACTION_TYPE_GROUP_LOCK:
505         send = xkb_filter_group_lock_new(state, key, act);
506         break;
507     default:
508         break;
509     }
510
511     return;
512 }
513
514 XKB_EXPORT struct xkb_state *
515 xkb_state_new(struct xkb_keymap *keymap)
516 {
517     struct xkb_state *ret;
518
519     ret = calloc(sizeof(*ret), 1);
520     if (!ret)
521         return NULL;
522
523     ret->refcnt = 1;
524     ret->keymap = xkb_map_ref(keymap);
525
526     return ret;
527 }
528
529 XKB_EXPORT struct xkb_state *
530 xkb_state_ref(struct xkb_state *state)
531 {
532     state->refcnt++;
533     return state;
534 }
535
536 XKB_EXPORT void
537 xkb_state_unref(struct xkb_state *state)
538 {
539     if (--state->refcnt > 0)
540         return;
541
542     xkb_map_unref(state->keymap);
543     darray_free(state->filters);
544     free(state);
545 }
546
547 XKB_EXPORT struct xkb_keymap *
548 xkb_state_get_map(struct xkb_state *state)
549 {
550     return state->keymap;
551 }
552
553 /**
554  * Update the LED state to match the rest of the xkb_state.
555  */
556 static void
557 xkb_state_led_update_all(struct xkb_state *state)
558 {
559     xkb_led_index_t led;
560
561     state->leds = 0;
562
563     for (led = 0; led < XKB_NUM_INDICATORS; led++) {
564         struct xkb_indicator_map *map = &state->keymap->indicators[led];
565         xkb_mod_mask_t mod_mask = 0;
566         uint32_t group_mask = 0;
567
568         if (map->which_mods & XKB_STATE_DEPRESSED)
569             mod_mask |= state->base_mods;
570         if (map->which_mods & XKB_STATE_LATCHED)
571             mod_mask |= state->latched_mods;
572         if (map->which_mods & XKB_STATE_LOCKED)
573             mod_mask |= state->locked_mods;
574         if ((map->mods.mask & mod_mask))
575             state->leds |= (1 << led);
576
577         if (map->which_groups & XKB_STATE_DEPRESSED)
578             group_mask |= (1 << state->base_group);
579         if (map->which_groups & XKB_STATE_LATCHED)
580             group_mask |= (1 << state->latched_group);
581         if (map->which_groups & XKB_STATE_LOCKED)
582             group_mask |= (1 << state->locked_group);
583         if ((map->groups & group_mask))
584             state->leds |= (1 << led);
585
586         if (map->ctrls) {
587             if ((map->ctrls & state->keymap->enabled_ctrls))
588                 state->leds |= (1 << led);
589         }
590     }
591 }
592
593 /**
594  * Calculates the derived state (effective mods/group and LEDs) from an
595  * up-to-date xkb_state.
596  */
597 static void
598 xkb_state_update_derived(struct xkb_state *state)
599 {
600     state->mods =
601         (state->base_mods | state->latched_mods | state->locked_mods);
602     /* FIXME: Clamp/wrap locked_group */
603     state->group = state->locked_group + state->base_group +
604                    state->latched_group;
605     /* FIXME: Clamp/wrap effective group */
606
607     xkb_state_led_update_all(state);
608 }
609
610 /**
611  * Given a particular key event, updates the state structure to reflect the
612  * new modifiers.
613  */
614 XKB_EXPORT void
615 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
616                      enum xkb_key_direction direction)
617 {
618     xkb_mod_index_t i;
619     xkb_mod_mask_t bit;
620     const struct xkb_key *key = XkbKey(state->keymap, kc);
621     if (!key)
622         return;
623
624     state->set_mods = 0;
625     state->clear_mods = 0;
626
627     xkb_filter_apply_all(state, key, direction);
628
629     for (i = 0, bit = 1; state->set_mods; i++, bit <<= 1) {
630         if (state->set_mods & bit) {
631             state->mod_key_count[i]++;
632             state->base_mods |= bit;
633             state->set_mods &= ~bit;
634         }
635     }
636
637     for (i = 0, bit = 1; state->clear_mods; i++, bit <<= 1) {
638         if (state->clear_mods & bit) {
639             state->mod_key_count[i]--;
640             if (state->mod_key_count[i] <= 0) {
641                 state->base_mods &= ~bit;
642                 state->mod_key_count[i] = 0;
643             }
644             state->clear_mods &= ~bit;
645         }
646     }
647
648     xkb_state_update_derived(state);
649 }
650
651 /**
652  * Updates the state from a set of explicit masks as gained from
653  * xkb_state_serialize_mods and xkb_state_serialize_groups.  As noted in the
654  * documentation for these functions in xkbcommon.h, this round-trip is
655  * lossy, and should only be used to update a slave state mirroring the
656  * master, e.g. in a client/server window system.
657  */
658 XKB_EXPORT void
659 xkb_state_update_mask(struct xkb_state *state,
660                       xkb_mod_mask_t base_mods,
661                       xkb_mod_mask_t latched_mods,
662                       xkb_mod_mask_t locked_mods,
663                       xkb_group_index_t base_group,
664                       xkb_group_index_t latched_group,
665                       xkb_group_index_t locked_group)
666 {
667     xkb_mod_index_t num_mods;
668     xkb_mod_index_t idx;
669
670     state->base_mods = 0;
671     state->latched_mods = 0;
672     state->locked_mods = 0;
673     num_mods = xkb_map_num_mods(state->keymap);
674
675     for (idx = 0; idx < num_mods; idx++) {
676         xkb_mod_mask_t mod = (1 << idx);
677         if (base_mods & mod)
678             state->base_mods |= mod;
679         if (latched_mods & mod)
680             state->latched_mods |= mod;
681         if (locked_mods & mod)
682             state->locked_mods |= mod;
683     }
684
685     state->base_group = base_group;
686     state->latched_group = latched_group;
687     state->locked_group = locked_group;
688
689     xkb_state_update_derived(state);
690 }
691
692 /**
693  * Serialises the requested modifier state into an xkb_mod_mask_t, with all
694  * the same disclaimers as in xkb_state_update_mask.
695  */
696 XKB_EXPORT xkb_mod_mask_t
697 xkb_state_serialize_mods(struct xkb_state *state,
698                          enum xkb_state_component type)
699 {
700     xkb_mod_mask_t ret = 0;
701
702     if (type == XKB_STATE_EFFECTIVE)
703         return state->mods;
704
705     if (type & XKB_STATE_DEPRESSED)
706         ret |= state->base_mods;
707     if (type & XKB_STATE_LATCHED)
708         ret |= state->latched_mods;
709     if (type & XKB_STATE_LOCKED)
710         ret |= state->locked_mods;
711
712     return ret;
713 }
714
715 /**
716  * Serialises the requested group state, with all the same disclaimers as
717  * in xkb_state_update_mask.
718  */
719 XKB_EXPORT xkb_group_index_t
720 xkb_state_serialize_group(struct xkb_state *state,
721                           enum xkb_state_component type)
722 {
723     xkb_group_index_t ret = 0;
724
725     if (type == XKB_STATE_EFFECTIVE)
726         return state->group;
727
728     if (type & XKB_STATE_DEPRESSED)
729         ret += state->base_group;
730     if (type & XKB_STATE_LATCHED)
731         ret += state->latched_group;
732     if (type & XKB_STATE_LOCKED)
733         ret += state->locked_group;
734
735     return ret;
736 }
737
738 /**
739  * Returns 1 if the given modifier is active with the specified type(s), 0 if
740  * not, or -1 if the modifier is invalid.
741  */
742 XKB_EXPORT int
743 xkb_state_mod_index_is_active(struct xkb_state *state,
744                               xkb_mod_index_t idx,
745                               enum xkb_state_component type)
746 {
747     int ret = 0;
748
749     if (idx >= xkb_map_num_mods(state->keymap))
750         return -1;
751
752     if (type & XKB_STATE_DEPRESSED)
753         ret |= (state->base_mods & (1 << idx));
754     if (type & XKB_STATE_LATCHED)
755         ret |= (state->latched_mods & (1 << idx));
756     if (type & XKB_STATE_LOCKED)
757         ret |= (state->locked_mods & (1 << idx));
758
759     return !!ret;
760 }
761
762 /**
763  * Helper function for xkb_state_mod_indices_are_active and
764  * xkb_state_mod_names_are_active.
765  */
766 static int
767 match_mod_masks(struct xkb_state *state, enum xkb_state_match match,
768                 uint32_t wanted)
769 {
770     uint32_t active = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
771
772     if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
773         return 0;
774
775     if (match & XKB_STATE_MATCH_ANY)
776         return !!(active & wanted);
777     else
778         return (active & wanted) == wanted;
779
780     return 0;
781 }
782
783 /**
784  * Returns 1 if the modifiers are active with the specified type(s), 0 if
785  * not, or -1 if any of the modifiers are invalid.
786  */
787 XKB_EXPORT int
788 xkb_state_mod_indices_are_active(struct xkb_state *state,
789                                  enum xkb_state_component type,
790                                  enum xkb_state_match match,
791                                  ...)
792 {
793     va_list ap;
794     xkb_mod_index_t idx = 0;
795     uint32_t wanted = 0;
796     int ret = 0;
797     xkb_mod_index_t num_mods = xkb_map_num_mods(state->keymap);
798
799     va_start(ap, match);
800     while (1) {
801         idx = va_arg(ap, xkb_mod_index_t);
802         if (idx == XKB_MOD_INVALID)
803             break;
804         if (idx >= num_mods) {
805             ret = -1;
806             break;
807         }
808         wanted |= (1 << idx);
809     }
810     va_end(ap);
811
812     if (ret == -1)
813         return ret;
814
815     return match_mod_masks(state, match, wanted);
816 }
817
818 /**
819  * Returns 1 if the given modifier is active with the specified type(s), 0 if
820  * not, or -1 if the modifier is invalid.
821  */
822 XKB_EXPORT int
823 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
824                              enum xkb_state_component type)
825 {
826     xkb_mod_index_t idx = xkb_map_mod_get_index(state->keymap, name);
827
828     if (idx == XKB_MOD_INVALID)
829         return -1;
830
831     return xkb_state_mod_index_is_active(state, idx, type);
832 }
833
834 /**
835  * Returns 1 if the modifiers are active with the specified type(s), 0 if
836  * not, or -1 if any of the modifiers are invalid.
837  */
838 XKB_EXPORT ATTR_NULL_SENTINEL int
839 xkb_state_mod_names_are_active(struct xkb_state *state,
840                                enum xkb_state_component type,
841                                enum xkb_state_match match,
842                                ...)
843 {
844     va_list ap;
845     xkb_mod_index_t idx = 0;
846     const char *str;
847     uint32_t wanted = 0;
848     int ret = 0;
849
850     va_start(ap, match);
851     while (1) {
852         str = va_arg(ap, const char *);
853         if (str == NULL)
854             break;
855         idx = xkb_map_mod_get_index(state->keymap, str);
856         if (idx == XKB_MOD_INVALID) {
857             ret = -1;
858             break;
859         }
860         wanted |= (1 << idx);
861     }
862     va_end(ap);
863
864     if (ret == -1)
865         return ret;
866
867     return match_mod_masks(state, match, wanted);
868 }
869
870 /**
871  * Returns 1 if the given group is active with the specified type(s), 0 if
872  * not, or -1 if the group is invalid.
873  */
874 XKB_EXPORT int
875 xkb_state_group_index_is_active(struct xkb_state *state,
876                                 xkb_group_index_t idx,
877                                 enum xkb_state_component type)
878 {
879     int ret = 0;
880
881     if (idx >= xkb_map_num_groups(state->keymap))
882         return -1;
883
884     if (type & XKB_STATE_DEPRESSED)
885         ret |= (state->base_group == idx);
886     if (type & XKB_STATE_LATCHED)
887         ret |= (state->latched_group == idx);
888     if (type & XKB_STATE_LOCKED)
889         ret |= (state->locked_group == idx);
890
891     return ret;
892 }
893
894 /**
895  * Returns 1 if the given modifier is active with the specified type(s), 0 if
896  * not, or -1 if the modifier is invalid.
897  */
898 XKB_EXPORT int
899 xkb_state_group_name_is_active(struct xkb_state *state, const char *name,
900                                enum xkb_state_component type)
901 {
902     xkb_group_index_t idx = xkb_map_group_get_index(state->keymap, name);
903
904     if (idx == XKB_GROUP_INVALID)
905         return -1;
906
907     return xkb_state_group_index_is_active(state, idx, type);
908 }
909
910 /**
911  * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
912  */
913 XKB_EXPORT int
914 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
915 {
916     if (idx >= xkb_map_num_leds(state->keymap))
917         return -1;
918
919     return !!(state->leds & (1 << idx));
920 }
921
922 /**
923  * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
924  */
925 XKB_EXPORT int
926 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
927 {
928     xkb_led_index_t idx = xkb_map_led_get_index(state->keymap, name);
929
930     if (idx == XKB_LED_INVALID)
931         return -1;
932
933     return xkb_state_led_index_is_active(state, idx);
934 }