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