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