keymap-dump: use ActionTypeText
[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  *
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:
36  *
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
39  * Software.
40  *
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.
48  *
49  * Author: Daniel Stone <daniel@fooishbar.org>
50  */
51
52 /*
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)
59  */
60
61 #include <assert.h>
62 #include <stdarg.h>
63
64 #include "xkb-priv.h"
65
66 struct xkb_filter {
67     struct xkb_state *state;
68     union xkb_action action;
69     xkb_keycode_t kc;
70     uint32_t priv;
71     int (*func)(struct xkb_filter *filter, xkb_keycode_t kc,
72                 enum xkb_key_direction direction);
73     int refcnt;
74     struct xkb_filter *next;
75 };
76
77 struct xkb_state {
78     xkb_group_index_t base_group; /**< depressed */
79     xkb_group_index_t latched_group;
80     xkb_group_index_t locked_group;
81     xkb_group_index_t group; /**< effective */
82
83     xkb_mod_mask_t base_mods; /**< depressed */
84     xkb_mod_mask_t latched_mods;
85     xkb_mod_mask_t locked_mods;
86     xkb_mod_mask_t mods; /**< effective */
87
88     /*
89      * At each event, we accumulate all the needed modifications to the base
90      * modifiers, and apply them at the end. These keep track of this state.
91      */
92     xkb_mod_mask_t set_mods;
93     xkb_mod_mask_t clear_mods;
94     /*
95      * We mustn't clear a base modifier if there's another depressed key
96      * which affects it, e.g. given this sequence
97      * < Left Shift down, Right Shift down, Left Shift Up >
98      * the modifier should still be set. This keeps the count.
99      */
100     int16_t mod_key_count[sizeof(xkb_mod_mask_t) * 8];
101
102     uint32_t leds;
103
104     int refcnt;
105     darray(struct xkb_filter) filters;
106     struct xkb_keymap *keymap;
107 };
108
109 static union xkb_action *
110 xkb_key_get_action(struct xkb_state *state, xkb_keycode_t kc)
111 {
112     xkb_group_index_t group;
113     unsigned int level;
114     struct xkb_key *key = NULL;
115
116     if (XkbKeycodeInRange(state->keymap, kc))
117         key = XkbKey(state->keymap, kc);
118
119     if (!key || !XkbKeyHasActions(key)) {
120         static union xkb_action fake;
121         memset(&fake, 0, sizeof(fake));
122         fake.type = XkbSA_NoAction;
123         return &fake;
124     }
125
126     group = xkb_key_get_group(state, kc);
127     level = xkb_key_get_level(state, kc, group);
128
129     return XkbKeyActionEntry(state->keymap, key, group, level);
130 }
131
132 static struct xkb_filter *
133 xkb_filter_new(struct xkb_state *state)
134 {
135     int old_size = darray_size(state->filters);
136     struct xkb_filter *filter = NULL, *iter;
137
138     darray_foreach(iter, state->filters) {
139         if (iter->func)
140             continue;
141         filter = iter;
142         break;
143     }
144
145     if (!filter) {
146         darray_resize0(state->filters, darray_size(state->filters) + 1);
147         filter = &darray_item(state->filters, old_size);
148     }
149
150     filter->state = state;
151     filter->refcnt = 1;
152     return filter;
153 }
154
155 /***====================================================================***/
156
157 static int
158 xkb_filter_group_set_func(struct xkb_filter *filter, xkb_keycode_t kc,
159                           enum xkb_key_direction direction)
160 {
161     if (kc != filter->kc) {
162         filter->action.group.flags &= ~XkbSA_ClearLocks;
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 & XkbSA_GroupAbsolute)
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 & XkbSA_ClearLocks)
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, xkb_keycode_t kc,
188                          union xkb_action *action)
189 {
190     struct xkb_filter *filter = xkb_filter_new(state);
191
192     if (!filter) /* WSGO */
193         return -1;
194     filter->kc = kc;
195     filter->func = xkb_filter_group_set_func;
196     filter->action = *action;
197
198     if (action->group.flags & XkbSA_GroupAbsolute) {
199         filter->action.group.group = filter->state->base_group;
200         filter->state->base_group = action->group.group;
201     }
202     else {
203         filter->state->base_group += action->group.group;
204     }
205
206     return 1;
207 }
208
209 static int
210 xkb_filter_group_lock_func(struct xkb_filter *filter, xkb_keycode_t kc,
211                            enum xkb_key_direction direction)
212 {
213     if (kc != filter->kc)
214         return 1;
215
216     if (direction == XKB_KEY_DOWN) {
217         filter->refcnt++;
218         return 0;
219     }
220     if (--filter->refcnt > 0)
221         return 0;
222
223     filter->func = NULL;
224     return 1;
225 }
226
227 static int
228 xkb_filter_group_lock_new(struct xkb_state *state, xkb_keycode_t kc,
229                           union xkb_action *action)
230 {
231     struct xkb_filter *filter = xkb_filter_new(state);
232
233     if (!filter)
234         return 0;
235
236     filter->kc = kc;
237     filter->func = xkb_filter_group_lock_func;
238     filter->action = *action;
239
240     if (action->group.flags & XkbSA_GroupAbsolute)
241         filter->state->locked_group = action->group.group;
242     else
243         filter->state->locked_group += action->group.group;
244
245     return 1;
246 }
247
248 static int
249 xkb_filter_mod_set_func(struct xkb_filter *filter, xkb_keycode_t kc,
250                         enum xkb_key_direction direction)
251 {
252     if (kc != filter->kc) {
253         filter->action.mods.flags &= ~XkbSA_ClearLocks;
254         return 1;
255     }
256
257     if (direction == XKB_KEY_DOWN) {
258         filter->refcnt++;
259         return 0;
260     }
261     else if (--filter->refcnt > 0) {
262         return 0;
263     }
264
265     filter->state->clear_mods = filter->action.mods.mask;
266     if (filter->action.mods.flags & XkbSA_ClearLocks)
267         filter->state->locked_mods &= ~filter->action.mods.mask;
268
269     filter->func = NULL;
270
271     return 1;
272 }
273
274 static int
275 xkb_filter_mod_set_new(struct xkb_state *state, xkb_keycode_t kc,
276                        union xkb_action *action)
277 {
278     struct xkb_filter *filter = xkb_filter_new(state);
279
280     if (!filter) /* WSGO */
281         return -1;
282     filter->kc = kc;
283     filter->func = xkb_filter_mod_set_func;
284     filter->action = *action;
285
286     filter->state->set_mods = action->mods.mask;
287
288     return 1;
289 }
290
291 static int
292 xkb_filter_mod_lock_func(struct xkb_filter *filter, xkb_keycode_t kc,
293                          enum xkb_key_direction direction)
294 {
295     if (kc != filter->kc)
296         return 1;
297
298     if (direction == XKB_KEY_DOWN) {
299         filter->refcnt++;
300         return 0;
301     }
302     if (--filter->refcnt > 0)
303         return 0;
304
305     filter->state->locked_mods &= ~filter->priv;
306     filter->func = NULL;
307     return 1;
308 }
309
310 static int
311 xkb_filter_mod_lock_new(struct xkb_state *state, xkb_keycode_t kc,
312                         union xkb_action *action)
313 {
314     struct xkb_filter *filter = xkb_filter_new(state);
315
316     if (!filter) /* WSGO */
317         return 0;
318
319     filter->kc = kc;
320     filter->func = xkb_filter_mod_lock_func;
321     filter->action = *action;
322     filter->priv = state->locked_mods & action->mods.mask;
323     state->locked_mods |= action->mods.mask;
324
325     return 1;
326 }
327
328 enum xkb_key_latch_state {
329     NO_LATCH,
330     LATCH_KEY_DOWN,
331     LATCH_PENDING,
332 };
333
334 static int
335 xkb_filter_mod_latch_func(struct xkb_filter *filter, xkb_keycode_t kc,
336                           enum xkb_key_direction direction)
337 {
338     enum xkb_key_latch_state latch = filter->priv;
339
340     if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
341         /* If this is a new keypress and we're awaiting our single latched
342          * keypress, then either break the latch if any random key is pressed,
343          * or promote it to a lock or plain base set if it's the same
344          * modifier. */
345         union xkb_action *action = xkb_key_get_action(filter->state, kc);
346         if (action->type == XkbSA_LatchMods &&
347             action->mods.flags == filter->action.mods.flags &&
348             action->mods.mask == filter->action.mods.mask) {
349             filter->action = *action;
350             if (filter->action.mods.flags & XkbSA_LatchToLock) {
351                 filter->action.type = XkbSA_LockMods;
352                 filter->func = xkb_filter_mod_lock_func;
353                 filter->state->locked_mods |= filter->action.mods.mask;
354             }
355             else {
356                 filter->action.type = XkbSA_SetMods;
357                 filter->func = xkb_filter_mod_set_func;
358                 filter->state->set_mods = filter->action.mods.mask;
359             }
360             filter->kc = kc;
361             filter->state->latched_mods &= ~filter->action.mods.mask;
362             /* XXX beep beep! */
363             return 0;
364         }
365         else if (((1 << action->type) & XkbSA_BreakLatch)) {
366             /* XXX: This may be totally broken, we might need to break the
367              *      latch in the next run after this press? */
368             filter->state->latched_mods &= ~filter->action.mods.mask;
369             filter->func = NULL;
370             return 1;
371         }
372     }
373     else if (direction == XKB_KEY_UP && kc == filter->kc) {
374         /* Our key got released.  If we've set it to clear locks, and we
375          * currently have the same modifiers locked, then release them and
376          * don't actually latch.  Else we've actually hit the latching
377          * stage, so set PENDING and move our modifier from base to
378          * latched. */
379         if (latch == NO_LATCH ||
380             ((filter->action.mods.flags & XkbSA_ClearLocks) &&
381              (filter->state->locked_mods & filter->action.mods.mask) ==
382              filter->action.mods.mask)) {
383             /* XXX: We might be a bit overenthusiastic about clearing
384              *      mods other filters have set here? */
385             if (latch == LATCH_PENDING)
386                 filter->state->latched_mods &= ~filter->action.mods.mask;
387             else
388                 filter->state->clear_mods = filter->action.mods.mask;
389             filter->state->locked_mods &= ~filter->action.mods.mask;
390             filter->func = NULL;
391         }
392         else {
393             latch = LATCH_PENDING;
394             filter->state->clear_mods = filter->action.mods.mask;
395             filter->state->latched_mods |= filter->action.mods.mask;
396             /* XXX beep beep! */
397         }
398     }
399     else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
400         /* Someone's pressed another key while we've still got the latching
401          * key held down, so keep the base modifier state active (from
402          * xkb_filter_mod_latch_new), but don't trip the latch, just clear
403          * it as soon as the modifier gets released. */
404         latch = NO_LATCH;
405     }
406
407     filter->priv = latch;
408
409     return 1;
410 }
411
412 static int
413 xkb_filter_mod_latch_new(struct xkb_state *state, xkb_keycode_t kc,
414                          union xkb_action *action)
415 {
416     struct xkb_filter *filter = xkb_filter_new(state);
417     enum xkb_key_latch_state latch = LATCH_KEY_DOWN;
418
419     if (!filter) /* WSGO */
420         return -1;
421     filter->kc = kc;
422     filter->priv = latch;
423     filter->func = xkb_filter_mod_latch_func;
424     filter->action = *action;
425
426     filter->state->set_mods = action->mods.mask;
427
428     return 1;
429 }
430
431 /**
432  * Applies any relevant filters to the key, first from the list of filters
433  * that are currently active, then if no filter has claimed the key, possibly
434  * apply a new filter from the key action.
435  */
436 static void
437 xkb_filter_apply_all(struct xkb_state *state, xkb_keycode_t kc,
438                      enum xkb_key_direction direction)
439 {
440     struct xkb_filter *filter;
441     union xkb_action *act = NULL;
442     int send = 1;
443
444     /* First run through all the currently active filters and see if any of
445      * them have claimed this event. */
446     darray_foreach(filter, state->filters) {
447         if (!filter->func)
448             continue;
449         send &= filter->func(filter, kc, direction);
450     }
451
452     if (!send || direction == XKB_KEY_UP)
453         return;
454
455     act = xkb_key_get_action(state, kc);
456     switch (act->type) {
457     case XkbSA_SetMods:
458         send = xkb_filter_mod_set_new(state, kc, act);
459         break;
460     case XkbSA_LatchMods:
461         send = xkb_filter_mod_latch_new(state, kc, act);
462         break;
463     case XkbSA_LockMods:
464         send = xkb_filter_mod_lock_new(state, kc, act);
465         break;
466     case XkbSA_SetGroup:
467         send = xkb_filter_group_set_new(state, kc, act);
468         break;
469 #if 0
470     case XkbSA_LatchGroup:
471         send = xkb_filter_mod_latch_new(state, key, act);
472         break;
473 #endif
474     case XkbSA_LockGroup:
475         send = xkb_filter_group_lock_new(state, kc, act);
476         break;
477     }
478
479     return;
480 }
481
482 XKB_EXPORT struct xkb_state *
483 xkb_state_new(struct xkb_keymap *keymap)
484 {
485     struct xkb_state *ret;
486
487     if (!keymap)
488         return NULL;
489
490     ret = calloc(sizeof(*ret), 1);
491     if (!ret)
492         return NULL;
493
494     ret->refcnt = 1;
495     ret->keymap = xkb_map_ref(keymap);
496
497     return ret;
498 }
499
500 XKB_EXPORT struct xkb_state *
501 xkb_state_ref(struct xkb_state *state)
502 {
503     state->refcnt++;
504     return state;
505 }
506
507 XKB_EXPORT void
508 xkb_state_unref(struct xkb_state *state)
509 {
510     state->refcnt--;
511     assert(state->refcnt >= 0);
512     if (state->refcnt > 0)
513         return;
514
515     xkb_map_unref(state->keymap);
516     darray_free(state->filters);
517     free(state);
518 }
519
520 XKB_EXPORT struct xkb_keymap *
521 xkb_state_get_map(struct xkb_state *state)
522 {
523     return state->keymap;
524 }
525
526 /**
527  * Update the LED state to match the rest of the xkb_state.
528  */
529 static void
530 xkb_state_led_update_all(struct xkb_state *state)
531 {
532     xkb_led_index_t led;
533
534     state->leds = 0;
535
536     for (led = 0; led < XkbNumIndicators; led++) {
537         struct xkb_indicator_map *map = &state->keymap->indicators[led];
538         uint32_t mod_mask = 0;
539         uint32_t group_mask = 0;
540
541         if (!map->which_mods && !map->which_groups && !map->ctrls)
542             continue;
543
544         if (map->which_mods) {
545             if (map->which_mods & XkbIM_UseBase)
546                 mod_mask |= state->base_mods;
547             if (map->which_mods & XkbIM_UseLatched)
548                 mod_mask |= state->latched_mods;
549             if (map->which_mods & XkbIM_UseLocked)
550                 mod_mask |= state->locked_mods;
551             if (map->which_mods & XkbIM_UseEffective)
552                 mod_mask |= state->mods;
553             if ((map->mods.mask & mod_mask))
554                 state->leds |= (1 << led);
555         }
556         else if (map->which_groups) {
557             if (map->which_mods & XkbIM_UseBase)
558                 group_mask |= (1 << state->base_group);
559             if (map->which_mods & XkbIM_UseLatched)
560                 group_mask |= (1 << state->latched_group);
561             if (map->which_mods & XkbIM_UseLocked)
562                 group_mask |= (1 << state->locked_group);
563             if (map->which_mods & XkbIM_UseEffective)
564                 group_mask |= (1 << state->group);
565             if ((map->groups & group_mask))
566                 state->leds |= (1 << led);
567         }
568         else if (map->ctrls) {
569             if ((map->ctrls & state->keymap->enabled_ctrls))
570                 state->leds |= (1 << led);
571         }
572     }
573 }
574
575 /**
576  * Calculates the derived state (effective mods/group and LEDs) from an
577  * up-to-date xkb_state.
578  */
579 static void
580 xkb_state_update_derived(struct xkb_state *state)
581 {
582     state->mods =
583         (state->base_mods | state->latched_mods | state->locked_mods);
584     /* FIXME: Clamp/wrap locked_group */
585     state->group = state->locked_group + state->base_group +
586                    state->latched_group;
587     /* FIXME: Clamp/wrap effective group */
588
589     xkb_state_led_update_all(state);
590 }
591
592 /**
593  * Given a particular key event, updates the state structure to reflect the
594  * new modifiers.
595  */
596 XKB_EXPORT void
597 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
598                      enum xkb_key_direction direction)
599 {
600     xkb_mod_index_t i;
601     xkb_mod_mask_t bit;
602
603     state->set_mods = 0;
604     state->clear_mods = 0;
605
606     xkb_filter_apply_all(state, kc, direction);
607
608     for (i = 0, bit = 1; state->set_mods; i++, bit <<= 1) {
609         if (state->set_mods & bit) {
610             state->mod_key_count[i]++;
611             state->base_mods |= bit;
612             state->set_mods &= ~bit;
613         }
614     }
615
616     for (i = 0, bit = 1; state->clear_mods; i++, bit <<= 1) {
617         if (state->clear_mods & bit) {
618             state->mod_key_count[i]--;
619             if (state->mod_key_count[i] <= 0) {
620                 state->base_mods &= ~bit;
621                 state->mod_key_count[i] = 0;
622             }
623             state->clear_mods &= ~bit;
624         }
625     }
626
627     xkb_state_update_derived(state);
628 }
629
630 /**
631  * Updates the state from a set of explicit masks as gained from
632  * xkb_state_serialize_mods and xkb_state_serialize_groups.  As noted in the
633  * documentation for these functions in xkbcommon.h, this round-trip is
634  * lossy, and should only be used to update a slave state mirroring the
635  * master, e.g. in a client/server window system.
636  */
637 XKB_EXPORT void
638 xkb_state_update_mask(struct xkb_state *state,
639                       xkb_mod_mask_t base_mods,
640                       xkb_mod_mask_t latched_mods,
641                       xkb_mod_mask_t locked_mods,
642                       xkb_group_index_t base_group,
643                       xkb_group_index_t latched_group,
644                       xkb_group_index_t locked_group)
645 {
646     xkb_mod_mask_t mod;
647
648     state->base_mods = 0;
649     state->latched_mods = 0;
650     state->locked_mods = 0;
651     for (mod = 0; mod < xkb_map_num_mods(state->keymap); mod++) {
652         xkb_mod_mask_t idx = (1 << mod);
653         if (base_mods & idx)
654             state->base_mods |= idx;
655         if (latched_mods & idx)
656             state->latched_mods |= idx;
657         if (locked_mods & idx)
658             state->locked_mods |= idx;
659     }
660
661     state->base_group = base_group;
662     state->latched_group = latched_group;
663     state->locked_group = locked_group;
664
665     xkb_state_update_derived(state);
666 }
667
668 /**
669  * Serialises the requested modifier state into an xkb_mod_mask_t, with all
670  * the same disclaimers as in xkb_state_update_mask.
671  */
672 XKB_EXPORT xkb_mod_mask_t
673 xkb_state_serialize_mods(struct xkb_state *state,
674                          enum xkb_state_component type)
675 {
676     xkb_mod_mask_t ret = 0;
677
678     if (type == XKB_STATE_EFFECTIVE)
679         return state->mods;
680
681     if (type & XKB_STATE_DEPRESSED)
682         ret |= state->base_mods;
683     if (type & XKB_STATE_LATCHED)
684         ret |= state->latched_mods;
685     if (type & XKB_STATE_LOCKED)
686         ret |= state->locked_mods;
687
688     return ret;
689 }
690
691 /**
692  * Serialises the requested group state, with all the same disclaimers as
693  * in xkb_state_update_mask.
694  */
695 XKB_EXPORT xkb_group_index_t
696 xkb_state_serialize_group(struct xkb_state *state,
697                           enum xkb_state_component type)
698 {
699     xkb_group_index_t ret = 0;
700
701     if (type == XKB_STATE_EFFECTIVE)
702         return state->group;
703
704     if (type & XKB_STATE_DEPRESSED)
705         ret += state->base_group;
706     if (type & XKB_STATE_LATCHED)
707         ret += state->latched_group;
708     if (type & XKB_STATE_LOCKED)
709         ret += state->locked_group;
710
711     return ret;
712 }
713
714 /**
715  * Returns 1 if the given modifier is active with the specified type(s), 0 if
716  * not, or -1 if the modifier is invalid.
717  */
718 XKB_EXPORT int
719 xkb_state_mod_index_is_active(struct xkb_state *state,
720                               xkb_mod_index_t idx,
721                               enum xkb_state_component type)
722 {
723     int ret = 0;
724
725     if (idx >= xkb_map_num_mods(state->keymap))
726         return -1;
727
728     if (type & XKB_STATE_DEPRESSED)
729         ret |= (state->base_mods & (1 << idx));
730     if (type & XKB_STATE_LATCHED)
731         ret |= (state->latched_mods & (1 << idx));
732     if (type & XKB_STATE_LOCKED)
733         ret |= (state->locked_mods & (1 << idx));
734
735     return ret;
736 }
737
738 /**
739  * Helper function for xkb_state_mod_indices_are_active and
740  * xkb_state_mod_names_are_active.
741  */
742 static int
743 match_mod_masks(struct xkb_state *state, enum xkb_state_match match,
744                 uint32_t wanted)
745 {
746     uint32_t active = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
747
748     if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
749         return 0;
750
751     if (match & XKB_STATE_MATCH_ANY)
752         return !!(active & wanted);
753     else
754         return (active & wanted) == wanted;
755
756     return 0;
757 }
758
759 /**
760  * Returns 1 if the modifiers are active with the specified type(s), 0 if
761  * not, or -1 if any of the modifiers are invalid.
762  */
763 XKB_EXPORT int
764 xkb_state_mod_indices_are_active(struct xkb_state *state,
765                                  enum xkb_state_component type,
766                                  enum xkb_state_match match,
767                                  ...)
768 {
769     va_list ap;
770     xkb_mod_index_t idx = 0;
771     uint32_t wanted = 0;
772     int ret = 0;
773
774     va_start(ap, match);
775     while (1) {
776         idx = va_arg(ap, xkb_mod_index_t);
777         if (idx == XKB_MOD_INVALID ||
778             idx >= xkb_map_num_mods(state->keymap)) {
779             ret = -1;
780             break;
781         }
782         wanted |= (1 << idx);
783     }
784     va_end(ap);
785
786     if (ret == -1)
787         return ret;
788
789     return match_mod_masks(state, match, wanted);
790 }
791
792 /**
793  * Returns 1 if the given modifier is active with the specified type(s), 0 if
794  * not, or -1 if the modifier is invalid.
795  */
796 XKB_EXPORT int
797 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
798                              enum xkb_state_component type)
799 {
800     xkb_mod_index_t idx = xkb_map_mod_get_index(state->keymap, name);
801
802     if (idx == XKB_MOD_INVALID)
803         return -1;
804
805     return xkb_state_mod_index_is_active(state, idx, type);
806 }
807
808 /**
809  * Returns 1 if the modifiers are active with the specified type(s), 0 if
810  * not, or -1 if any of the modifiers are invalid.
811  */
812 XKB_EXPORT int
813 xkb_state_mod_names_are_active(struct xkb_state *state,
814                                enum xkb_state_component type,
815                                enum xkb_state_match match,
816                                ...)
817 {
818     va_list ap;
819     xkb_mod_index_t idx = 0;
820     const char *str;
821     uint32_t wanted = 0;
822     int ret = 0;
823
824     va_start(ap, match);
825     while (1) {
826         str = va_arg(ap, const char *);
827         if (str == NULL)
828             break;
829         idx = xkb_map_mod_get_index(state->keymap, str);
830         if (idx == XKB_MOD_INVALID) {
831             ret = -1;
832             break;
833         }
834         wanted |= (1 << idx);
835     }
836     va_end(ap);
837
838     if (ret == -1)
839         return ret;
840
841     return match_mod_masks(state, match, wanted);
842 }
843
844 /**
845  * Returns 1 if the given group is active with the specified type(s), 0 if
846  * not, or -1 if the group is invalid.
847  */
848 XKB_EXPORT int
849 xkb_state_group_index_is_active(struct xkb_state *state,
850                                 xkb_group_index_t idx,
851                                 enum xkb_state_component type)
852 {
853     int ret = 0;
854
855     if (idx >= xkb_map_num_groups(state->keymap))
856         return -1;
857
858     if (type & XKB_STATE_DEPRESSED)
859         ret |= (state->base_group == idx);
860     if (type & XKB_STATE_LATCHED)
861         ret |= (state->latched_group == idx);
862     if (type & XKB_STATE_LOCKED)
863         ret |= (state->locked_group == idx);
864
865     return ret;
866 }
867
868 /**
869  * Returns 1 if the given modifier is active with the specified type(s), 0 if
870  * not, or -1 if the modifier is invalid.
871  */
872 XKB_EXPORT int
873 xkb_state_group_name_is_active(struct xkb_state *state, const char *name,
874                                enum xkb_state_component type)
875 {
876     xkb_group_index_t idx = xkb_map_group_get_index(state->keymap, name);
877
878     if (idx == XKB_GROUP_INVALID)
879         return -1;
880
881     return xkb_state_group_index_is_active(state, idx, type);
882 }
883
884 /**
885  * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
886  */
887 XKB_EXPORT int
888 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
889 {
890     if (idx >= xkb_map_num_leds(state->keymap))
891         return -1;
892
893     return !!(state->leds & (1 << idx));
894 }
895
896 /**
897  * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
898  */
899 XKB_EXPORT int
900 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
901 {
902     xkb_led_index_t idx = xkb_map_led_get_index(state->keymap, name);
903
904     if (idx == XKB_LED_INVALID)
905         return -1;
906
907     return xkb_state_led_index_is_active(state, idx);
908 }