state, map: check XkbKey != NULL where missing
[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 "map.h"
63
64 struct xkb_filter {
65     union xkb_action action;
66     const struct xkb_key *key;
67     uint32_t priv;
68     int (*func)(struct xkb_state *state,
69                 struct xkb_filter *filter,
70                 const struct xkb_key *key,
71                 enum xkb_key_direction direction);
72     int refcnt;
73 };
74
75 struct xkb_state {
76     xkb_layout_index_t base_group; /**< depressed */
77     xkb_layout_index_t latched_group;
78     xkb_layout_index_t locked_group;
79     xkb_layout_index_t group; /**< effective */
80
81     xkb_mod_mask_t base_mods; /**< depressed */
82     xkb_mod_mask_t latched_mods;
83     xkb_mod_mask_t locked_mods;
84     xkb_mod_mask_t mods; /**< effective */
85
86     /*
87      * At each event, we accumulate all the needed modifications to the base
88      * modifiers, and apply them at the end. These keep track of this state.
89      */
90     xkb_mod_mask_t set_mods;
91     xkb_mod_mask_t clear_mods;
92     /*
93      * We mustn't clear a base modifier if there's another depressed key
94      * which affects it, e.g. given this sequence
95      * < Left Shift down, Right Shift down, Left Shift Up >
96      * the modifier should still be set. This keeps the count.
97      */
98     int16_t mod_key_count[sizeof(xkb_mod_mask_t) * 8];
99
100     uint32_t leds;
101
102     int refcnt;
103     darray(struct xkb_filter) filters;
104     struct xkb_keymap *keymap;
105 };
106
107 static struct xkb_kt_map_entry *
108 get_entry_for_key_state(struct xkb_state *state, const struct xkb_key *key,
109                         xkb_layout_index_t group)
110 {
111     struct xkb_key_type *type;
112     xkb_mod_mask_t active_mods;
113     unsigned int i;
114
115     type = XkbKeyType(state->keymap, key, group);
116     active_mods = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
117     active_mods &= type->mods.mask;
118
119     for (i = 0; i < type->num_entries; i++)
120         if (type->map[i].mods.mask == active_mods)
121             return &type->map[i];
122
123     return NULL;
124 }
125
126 /**
127  * Returns the level to use for the given key and state, or
128  * XKB_LEVEL_INVALID.
129  */
130 xkb_level_index_t
131 xkb_state_key_get_level(struct xkb_state *state, xkb_keycode_t kc,
132                         xkb_layout_index_t layout)
133 {
134     const struct xkb_key *key = XkbKey(state->keymap, kc);
135     struct xkb_kt_map_entry *entry;
136
137     /* If we don't find an explicit match the default is 0. */
138     entry = get_entry_for_key_state(state, key, layout);
139     if (!entry)
140         return 0;
141
142     return entry->level;
143 }
144
145 /**
146  * Returns the layout to use for the given key and state, taking
147  * wrapping/clamping/etc into account, or XKB_LAYOUT_INVALID.
148  */
149 XKB_EXPORT xkb_layout_index_t
150 xkb_state_key_get_layout(struct xkb_state *state, xkb_keycode_t kc)
151 {
152     xkb_layout_index_t ret =
153         xkb_state_serialize_layout(state, XKB_STATE_EFFECTIVE);
154     const struct xkb_key *key = XkbKey(state->keymap, kc);
155
156     if (!key || key->num_groups == 0)
157         return XKB_LAYOUT_INVALID;
158
159     if (ret < key->num_groups)
160         return ret;
161
162     switch (key->out_of_range_group_action) {
163     case RANGE_REDIRECT:
164         ret = key->out_of_range_group_number;
165         if (ret >= key->num_groups)
166             ret = 0;
167         break;
168
169     case RANGE_SATURATE:
170         ret = key->num_groups - 1;
171         break;
172
173     case RANGE_WRAP:
174     default:
175         ret %= key->num_groups;
176         break;
177     }
178
179     return ret;
180 }
181
182 static const union xkb_action fake = { .type = ACTION_TYPE_NONE };
183
184 static const union xkb_action *
185 xkb_key_get_action(struct xkb_state *state, const struct xkb_key *key)
186 {
187     xkb_layout_index_t layout;
188     xkb_level_index_t level;
189
190     if (!key->actions)
191         return &fake;
192
193     layout = xkb_state_key_get_layout(state, key->keycode);
194     if (layout == XKB_LAYOUT_INVALID)
195         return &fake;
196
197     level = xkb_state_key_get_level(state, key->keycode, layout);
198     if (level == XKB_LEVEL_INVALID)
199         return &fake;
200
201     return XkbKeyActionEntry(key, layout, level);
202 }
203
204 static struct xkb_filter *
205 xkb_filter_new(struct xkb_state *state)
206 {
207     struct xkb_filter *filter = NULL, *iter;
208
209     darray_foreach(iter, state->filters) {
210         if (iter->func)
211             continue;
212         filter = iter;
213         break;
214     }
215
216     if (!filter) {
217         darray_resize0(state->filters, darray_size(state->filters) + 1);
218         filter = &darray_item(state->filters, darray_size(state->filters) -1);
219     }
220
221     filter->refcnt = 1;
222     return filter;
223 }
224
225 /***====================================================================***/
226
227 static int
228 xkb_filter_group_set_func(struct xkb_state *state,
229                           struct xkb_filter *filter,
230                           const struct xkb_key *key,
231                           enum xkb_key_direction direction)
232 {
233     if (key != filter->key) {
234         filter->action.group.flags &= ~ACTION_LOCK_CLEAR;
235         return 1;
236     }
237
238     if (direction == XKB_KEY_DOWN) {
239         filter->refcnt++;
240         return 0;
241     }
242     else if (--filter->refcnt > 0) {
243         return 0;
244     }
245
246     state->base_group = filter->priv;
247
248     if (filter->action.group.flags & ACTION_LOCK_CLEAR)
249         state->locked_group = 0;
250
251     filter->func = NULL;
252     return 1;
253 }
254
255 static void
256 xkb_filter_group_set_new(struct xkb_state *state, struct xkb_filter *filter)
257 {
258     filter->priv = state->base_group;
259     if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
260         state->base_group = filter->action.group.group;
261     else
262         state->base_group += filter->action.group.group;
263 }
264
265 static int
266 xkb_filter_group_lock_func(struct xkb_state *state,
267                            struct xkb_filter *filter,
268                            const struct xkb_key *key,
269                            enum xkb_key_direction direction)
270 {
271     if (key != filter->key)
272         return 1;
273
274     if (direction == XKB_KEY_DOWN) {
275         filter->refcnt++;
276         return 0;
277     }
278     if (--filter->refcnt > 0)
279         return 0;
280
281     filter->func = NULL;
282     return 1;
283 }
284
285 static void
286 xkb_filter_group_lock_new(struct xkb_state *state, struct xkb_filter *filter)
287 {
288     if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
289         state->locked_group = filter->action.group.group;
290     else
291         state->locked_group += filter->action.group.group;
292 }
293
294 static int
295 xkb_filter_mod_set_func(struct xkb_state *state,
296                         struct xkb_filter *filter,
297                         const struct xkb_key *key,
298                         enum xkb_key_direction direction)
299 {
300     if (key != filter->key) {
301         filter->action.mods.flags &= ~ACTION_LOCK_CLEAR;
302         return 1;
303     }
304
305     if (direction == XKB_KEY_DOWN) {
306         filter->refcnt++;
307         return 0;
308     }
309     else if (--filter->refcnt > 0) {
310         return 0;
311     }
312
313     state->clear_mods = filter->action.mods.mods.mask;
314     if (filter->action.mods.flags & ACTION_LOCK_CLEAR)
315         state->locked_mods &= ~filter->action.mods.mods.mask;
316
317     filter->func = NULL;
318     return 1;
319 }
320
321 static void
322 xkb_filter_mod_set_new(struct xkb_state *state, struct xkb_filter *filter)
323 {
324     state->set_mods = filter->action.mods.mods.mask;
325 }
326
327 static int
328 xkb_filter_mod_lock_func(struct xkb_state *state,
329                          struct xkb_filter *filter,
330                          const struct xkb_key *key,
331                          enum xkb_key_direction direction)
332 {
333     if (key != filter->key)
334         return 1;
335
336     if (direction == XKB_KEY_DOWN) {
337         filter->refcnt++;
338         return 0;
339     }
340     if (--filter->refcnt > 0)
341         return 0;
342
343     state->clear_mods |= filter->action.mods.mods.mask;
344     if (!(filter->action.mods.flags & ACTION_LOCK_NO_UNLOCK))
345         state->locked_mods &= ~filter->priv;
346
347     filter->func = NULL;
348     return 1;
349 }
350
351 static void
352 xkb_filter_mod_lock_new(struct xkb_state *state, struct xkb_filter *filter)
353 {
354     filter->priv = state->locked_mods & filter->action.mods.mods.mask;
355     state->set_mods |= filter->action.mods.mods.mask;
356     if (!(filter->action.mods.flags & ACTION_LOCK_NO_LOCK))
357         state->locked_mods |= filter->action.mods.mods.mask;
358 }
359
360 enum xkb_key_latch_state {
361     NO_LATCH,
362     LATCH_KEY_DOWN,
363     LATCH_PENDING,
364 };
365
366 static bool
367 xkb_action_breaks_latch(const union xkb_action *action)
368 {
369     switch (action->type) {
370     case ACTION_TYPE_NONE:
371     case ACTION_TYPE_PTR_BUTTON:
372     case ACTION_TYPE_PTR_LOCK:
373     case ACTION_TYPE_CTRL_SET:
374     case ACTION_TYPE_CTRL_LOCK:
375     case ACTION_TYPE_KEY_REDIRECT:
376     case ACTION_TYPE_SWITCH_VT:
377     case ACTION_TYPE_TERMINATE:
378         return true;
379     default:
380         return false;
381     }
382 }
383
384 static int
385 xkb_filter_mod_latch_func(struct xkb_state *state,
386                           struct xkb_filter *filter,
387                           const struct xkb_key *key,
388                           enum xkb_key_direction direction)
389 {
390     enum xkb_key_latch_state latch = filter->priv;
391
392     if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
393         /* If this is a new keypress and we're awaiting our single latched
394          * keypress, then either break the latch if any random key is pressed,
395          * or promote it to a lock or plain base set if it's the same
396          * modifier. */
397         const union xkb_action *action = xkb_key_get_action(state, key);
398         if (action->type == ACTION_TYPE_MOD_LATCH &&
399             action->mods.flags == filter->action.mods.flags &&
400             action->mods.mods.mask == filter->action.mods.mods.mask) {
401             filter->action = *action;
402             if (filter->action.mods.flags & ACTION_LATCH_TO_LOCK) {
403                 filter->action.type = ACTION_TYPE_MOD_LOCK;
404                 filter->func = xkb_filter_mod_lock_func;
405                 state->locked_mods |= filter->action.mods.mods.mask;
406             }
407             else {
408                 filter->action.type = ACTION_TYPE_MOD_SET;
409                 filter->func = xkb_filter_mod_set_func;
410                 state->set_mods = filter->action.mods.mods.mask;
411             }
412             filter->key = key;
413             state->latched_mods &= ~filter->action.mods.mods.mask;
414             /* XXX beep beep! */
415             return 0;
416         }
417         else if (xkb_action_breaks_latch(action)) {
418             /* XXX: This may be totally broken, we might need to break the
419              *      latch in the next run after this press? */
420             state->latched_mods &= ~filter->action.mods.mods.mask;
421             filter->func = NULL;
422             return 1;
423         }
424     }
425     else if (direction == XKB_KEY_UP && key == filter->key) {
426         /* Our key got released.  If we've set it to clear locks, and we
427          * currently have the same modifiers locked, then release them and
428          * don't actually latch.  Else we've actually hit the latching
429          * stage, so set PENDING and move our modifier from base to
430          * latched. */
431         if (latch == NO_LATCH ||
432             ((filter->action.mods.flags & ACTION_LOCK_CLEAR) &&
433              (state->locked_mods & filter->action.mods.mods.mask) ==
434              filter->action.mods.mods.mask)) {
435             /* XXX: We might be a bit overenthusiastic about clearing
436              *      mods other filters have set here? */
437             if (latch == LATCH_PENDING)
438                 state->latched_mods &= ~filter->action.mods.mods.mask;
439             else
440                 state->clear_mods = filter->action.mods.mods.mask;
441             state->locked_mods &= ~filter->action.mods.mods.mask;
442             filter->func = NULL;
443         }
444         else {
445             latch = LATCH_PENDING;
446             state->clear_mods = filter->action.mods.mods.mask;
447             state->latched_mods |= filter->action.mods.mods.mask;
448             /* XXX beep beep! */
449         }
450     }
451     else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
452         /* Someone's pressed another key while we've still got the latching
453          * key held down, so keep the base modifier state active (from
454          * xkb_filter_mod_latch_new), but don't trip the latch, just clear
455          * it as soon as the modifier gets released. */
456         latch = NO_LATCH;
457     }
458
459     filter->priv = latch;
460
461     return 1;
462 }
463
464 static void
465 xkb_filter_mod_latch_new(struct xkb_state *state, struct xkb_filter *filter)
466 {
467     filter->priv = LATCH_KEY_DOWN;
468     state->set_mods = filter->action.mods.mods.mask;
469 }
470
471 static const struct {
472     void (*new)(struct xkb_state *state, struct xkb_filter *filter);
473     int (*func)(struct xkb_state *state, struct xkb_filter *filter,
474                 const struct xkb_key *key, enum xkb_key_direction direction);
475 } filter_action_funcs[_ACTION_TYPE_NUM_ENTRIES] = {
476     [ACTION_TYPE_MOD_SET]    = { xkb_filter_mod_set_new,
477                                  xkb_filter_mod_set_func },
478     [ACTION_TYPE_MOD_LATCH]  = { xkb_filter_mod_latch_new,
479                                  xkb_filter_mod_latch_func },
480     [ACTION_TYPE_MOD_LOCK]   = { xkb_filter_mod_lock_new,
481                                  xkb_filter_mod_lock_func },
482     [ACTION_TYPE_GROUP_SET]  = { xkb_filter_group_set_new,
483                                  xkb_filter_group_set_func },
484     [ACTION_TYPE_GROUP_LOCK] = { xkb_filter_group_lock_new,
485                                  xkb_filter_group_lock_func },
486 };
487
488 /**
489  * Applies any relevant filters to the key, first from the list of filters
490  * that are currently active, then if no filter has claimed the key, possibly
491  * apply a new filter from the key action.
492  */
493 static void
494 xkb_filter_apply_all(struct xkb_state *state,
495                      const struct xkb_key *key,
496                      enum xkb_key_direction direction)
497 {
498     struct xkb_filter *filter;
499     const union xkb_action *action;
500     int send = 1;
501
502     /* First run through all the currently active filters and see if any of
503      * them have claimed this event. */
504     darray_foreach(filter, state->filters) {
505         if (!filter->func)
506             continue;
507         send &= filter->func(state, filter, key, direction);
508     }
509
510     if (!send || direction == XKB_KEY_UP)
511         return;
512
513     action = xkb_key_get_action(state, key);
514     if (!filter_action_funcs[action->type].new)
515         return;
516
517     filter = xkb_filter_new(state);
518     if (!filter)
519         return; /* WSGO */
520
521     filter->key = key;
522     filter->func = filter_action_funcs[action->type].func;
523     filter->action = *action;
524     filter_action_funcs[action->type].new(state, filter);
525 }
526
527 XKB_EXPORT struct xkb_state *
528 xkb_state_new(struct xkb_keymap *keymap)
529 {
530     struct xkb_state *ret;
531
532     ret = calloc(sizeof(*ret), 1);
533     if (!ret)
534         return NULL;
535
536     ret->refcnt = 1;
537     ret->keymap = xkb_keymap_ref(keymap);
538
539     return ret;
540 }
541
542 XKB_EXPORT struct xkb_state *
543 xkb_state_ref(struct xkb_state *state)
544 {
545     state->refcnt++;
546     return state;
547 }
548
549 XKB_EXPORT void
550 xkb_state_unref(struct xkb_state *state)
551 {
552     if (--state->refcnt > 0)
553         return;
554
555     xkb_keymap_unref(state->keymap);
556     darray_free(state->filters);
557     free(state);
558 }
559
560 XKB_EXPORT struct xkb_keymap *
561 xkb_state_get_keymap(struct xkb_state *state)
562 {
563     return state->keymap;
564 }
565
566 /**
567  * Update the LED state to match the rest of the xkb_state.
568  */
569 static void
570 xkb_state_led_update_all(struct xkb_state *state)
571 {
572     xkb_led_index_t led;
573
574     state->leds = 0;
575
576     for (led = 0; led < XKB_NUM_INDICATORS; led++) {
577         struct xkb_indicator_map *map = &state->keymap->indicators[led];
578         xkb_mod_mask_t mod_mask = 0;
579         uint32_t group_mask = 0;
580
581         if (map->which_mods & XKB_STATE_DEPRESSED)
582             mod_mask |= state->base_mods;
583         if (map->which_mods & XKB_STATE_LATCHED)
584             mod_mask |= state->latched_mods;
585         if (map->which_mods & XKB_STATE_LOCKED)
586             mod_mask |= state->locked_mods;
587         if ((map->mods.mask & mod_mask))
588             state->leds |= (1 << led);
589
590         if (map->which_groups & XKB_STATE_DEPRESSED)
591             group_mask |= (1 << state->base_group);
592         if (map->which_groups & XKB_STATE_LATCHED)
593             group_mask |= (1 << state->latched_group);
594         if (map->which_groups & XKB_STATE_LOCKED)
595             group_mask |= (1 << state->locked_group);
596         if ((map->groups & group_mask))
597             state->leds |= (1 << led);
598
599         if (map->ctrls) {
600             if ((map->ctrls & state->keymap->enabled_ctrls))
601                 state->leds |= (1 << led);
602         }
603     }
604 }
605
606 /**
607  * Calculates the derived state (effective mods/group and LEDs) from an
608  * up-to-date xkb_state.
609  */
610 static void
611 xkb_state_update_derived(struct xkb_state *state)
612 {
613     state->mods =
614         (state->base_mods | state->latched_mods | state->locked_mods);
615     /* FIXME: Clamp/wrap locked_group */
616     state->group = state->locked_group + state->base_group +
617                    state->latched_group;
618     /* FIXME: Clamp/wrap effective group */
619
620     xkb_state_led_update_all(state);
621 }
622
623 /**
624  * Given a particular key event, updates the state structure to reflect the
625  * new modifiers.
626  */
627 XKB_EXPORT void
628 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
629                      enum xkb_key_direction direction)
630 {
631     xkb_mod_index_t i;
632     xkb_mod_mask_t bit;
633     const struct xkb_key *key = XkbKey(state->keymap, kc);
634
635     if (!key)
636         return;
637
638     state->set_mods = 0;
639     state->clear_mods = 0;
640
641     xkb_filter_apply_all(state, key, direction);
642
643     for (i = 0, bit = 1; state->set_mods; i++, bit <<= 1) {
644         if (state->set_mods & bit) {
645             state->mod_key_count[i]++;
646             state->base_mods |= bit;
647             state->set_mods &= ~bit;
648         }
649     }
650
651     for (i = 0, bit = 1; state->clear_mods; i++, bit <<= 1) {
652         if (state->clear_mods & bit) {
653             state->mod_key_count[i]--;
654             if (state->mod_key_count[i] <= 0) {
655                 state->base_mods &= ~bit;
656                 state->mod_key_count[i] = 0;
657             }
658             state->clear_mods &= ~bit;
659         }
660     }
661
662     xkb_state_update_derived(state);
663 }
664
665 /**
666  * Updates the state from a set of explicit masks as gained from
667  * xkb_state_serialize_mods and xkb_state_serialize_groups.  As noted in the
668  * documentation for these functions in xkbcommon.h, this round-trip is
669  * lossy, and should only be used to update a slave state mirroring the
670  * master, e.g. in a client/server window system.
671  */
672 XKB_EXPORT void
673 xkb_state_update_mask(struct xkb_state *state,
674                       xkb_mod_mask_t base_mods,
675                       xkb_mod_mask_t latched_mods,
676                       xkb_mod_mask_t locked_mods,
677                       xkb_layout_index_t base_group,
678                       xkb_layout_index_t latched_group,
679                       xkb_layout_index_t locked_group)
680 {
681     xkb_mod_index_t num_mods;
682     xkb_mod_index_t idx;
683
684     state->base_mods = 0;
685     state->latched_mods = 0;
686     state->locked_mods = 0;
687     num_mods = xkb_keymap_num_mods(state->keymap);
688
689     for (idx = 0; idx < num_mods; idx++) {
690         xkb_mod_mask_t mod = (1 << idx);
691         if (base_mods & mod)
692             state->base_mods |= mod;
693         if (latched_mods & mod)
694             state->latched_mods |= mod;
695         if (locked_mods & mod)
696             state->locked_mods |= mod;
697     }
698
699     state->base_group = base_group;
700     state->latched_group = latched_group;
701     state->locked_group = locked_group;
702
703     xkb_state_update_derived(state);
704 }
705
706 /**
707  * Provides the symbols to use for the given key and state.  Returns the
708  * number of symbols pointed to in syms_out.
709  */
710 XKB_EXPORT int
711 xkb_state_key_get_syms(struct xkb_state *state, xkb_keycode_t kc,
712                        const xkb_keysym_t **syms_out)
713 {
714     xkb_layout_index_t layout;
715     xkb_level_index_t level;
716     const struct xkb_key *key = XkbKey(state->keymap, kc);
717
718     if (!key)
719         return -1;
720
721     layout = xkb_state_key_get_layout(state, kc);
722     if (layout == XKB_LAYOUT_INVALID)
723         goto err;
724
725     level = xkb_state_key_get_level(state, kc, layout);
726     if (level == XKB_LEVEL_INVALID)
727         goto err;
728
729     return xkb_keymap_key_get_syms_by_level(state->keymap, kc, layout, level,
730                                             syms_out);
731
732 err:
733     *syms_out = NULL;
734     return 0;
735 }
736
737 /**
738  * Serialises the requested modifier state into an xkb_mod_mask_t, with all
739  * the same disclaimers as in xkb_state_update_mask.
740  */
741 XKB_EXPORT xkb_mod_mask_t
742 xkb_state_serialize_mods(struct xkb_state *state,
743                          enum xkb_state_component type)
744 {
745     xkb_mod_mask_t ret = 0;
746
747     if (type == XKB_STATE_EFFECTIVE)
748         return state->mods;
749
750     if (type & XKB_STATE_DEPRESSED)
751         ret |= state->base_mods;
752     if (type & XKB_STATE_LATCHED)
753         ret |= state->latched_mods;
754     if (type & XKB_STATE_LOCKED)
755         ret |= state->locked_mods;
756
757     return ret;
758 }
759
760 /**
761  * Serialises the requested group state, with all the same disclaimers as
762  * in xkb_state_update_mask.
763  */
764 XKB_EXPORT xkb_layout_index_t
765 xkb_state_serialize_layout(struct xkb_state *state,
766                            enum xkb_state_component type)
767 {
768     xkb_layout_index_t ret = 0;
769
770     if (type == XKB_STATE_EFFECTIVE)
771         return state->group;
772
773     if (type & XKB_STATE_DEPRESSED)
774         ret += state->base_group;
775     if (type & XKB_STATE_LATCHED)
776         ret += state->latched_group;
777     if (type & XKB_STATE_LOCKED)
778         ret += state->locked_group;
779
780     return ret;
781 }
782
783 /**
784  * Returns 1 if the given modifier is active with the specified type(s), 0 if
785  * not, or -1 if the modifier is invalid.
786  */
787 XKB_EXPORT int
788 xkb_state_mod_index_is_active(struct xkb_state *state,
789                               xkb_mod_index_t idx,
790                               enum xkb_state_component type)
791 {
792     int ret = 0;
793
794     if (idx >= xkb_keymap_num_mods(state->keymap))
795         return -1;
796
797     if (type & XKB_STATE_DEPRESSED)
798         ret |= (state->base_mods & (1 << idx));
799     if (type & XKB_STATE_LATCHED)
800         ret |= (state->latched_mods & (1 << idx));
801     if (type & XKB_STATE_LOCKED)
802         ret |= (state->locked_mods & (1 << idx));
803
804     return !!ret;
805 }
806
807 /**
808  * Helper function for xkb_state_mod_indices_are_active and
809  * xkb_state_mod_names_are_active.
810  */
811 static int
812 match_mod_masks(struct xkb_state *state, enum xkb_state_match match,
813                 uint32_t wanted)
814 {
815     uint32_t active = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
816
817     if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
818         return 0;
819
820     if (match & XKB_STATE_MATCH_ANY)
821         return !!(active & wanted);
822     else
823         return (active & wanted) == wanted;
824
825     return 0;
826 }
827
828 /**
829  * Returns 1 if the modifiers are active with the specified type(s), 0 if
830  * not, or -1 if any of the modifiers are invalid.
831  */
832 XKB_EXPORT int
833 xkb_state_mod_indices_are_active(struct xkb_state *state,
834                                  enum xkb_state_component type,
835                                  enum xkb_state_match match,
836                                  ...)
837 {
838     va_list ap;
839     xkb_mod_index_t idx = 0;
840     uint32_t wanted = 0;
841     int ret = 0;
842     xkb_mod_index_t num_mods = xkb_keymap_num_mods(state->keymap);
843
844     va_start(ap, match);
845     while (1) {
846         idx = va_arg(ap, xkb_mod_index_t);
847         if (idx == XKB_MOD_INVALID)
848             break;
849         if (idx >= num_mods) {
850             ret = -1;
851             break;
852         }
853         wanted |= (1 << idx);
854     }
855     va_end(ap);
856
857     if (ret == -1)
858         return ret;
859
860     return match_mod_masks(state, match, wanted);
861 }
862
863 /**
864  * Returns 1 if the given modifier is active with the specified type(s), 0 if
865  * not, or -1 if the modifier is invalid.
866  */
867 XKB_EXPORT int
868 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
869                              enum xkb_state_component type)
870 {
871     xkb_mod_index_t idx = xkb_keymap_mod_get_index(state->keymap, name);
872
873     if (idx == XKB_MOD_INVALID)
874         return -1;
875
876     return xkb_state_mod_index_is_active(state, idx, type);
877 }
878
879 /**
880  * Returns 1 if the modifiers are active with the specified type(s), 0 if
881  * not, or -1 if any of the modifiers are invalid.
882  */
883 XKB_EXPORT ATTR_NULL_SENTINEL int
884 xkb_state_mod_names_are_active(struct xkb_state *state,
885                                enum xkb_state_component type,
886                                enum xkb_state_match match,
887                                ...)
888 {
889     va_list ap;
890     xkb_mod_index_t idx = 0;
891     const char *str;
892     uint32_t wanted = 0;
893     int ret = 0;
894
895     va_start(ap, match);
896     while (1) {
897         str = va_arg(ap, const char *);
898         if (str == NULL)
899             break;
900         idx = xkb_keymap_mod_get_index(state->keymap, str);
901         if (idx == XKB_MOD_INVALID) {
902             ret = -1;
903             break;
904         }
905         wanted |= (1 << idx);
906     }
907     va_end(ap);
908
909     if (ret == -1)
910         return ret;
911
912     return match_mod_masks(state, match, wanted);
913 }
914
915 /**
916  * Returns 1 if the given group is active with the specified type(s), 0 if
917  * not, or -1 if the group is invalid.
918  */
919 XKB_EXPORT int
920 xkb_state_layout_index_is_active(struct xkb_state *state,
921                                 xkb_layout_index_t idx,
922                                 enum xkb_state_component type)
923 {
924     int ret = 0;
925
926     if (idx >= xkb_keymap_num_layouts(state->keymap))
927         return -1;
928
929     if (type & XKB_STATE_DEPRESSED)
930         ret |= (state->base_group == idx);
931     if (type & XKB_STATE_LATCHED)
932         ret |= (state->latched_group == idx);
933     if (type & XKB_STATE_LOCKED)
934         ret |= (state->locked_group == idx);
935
936     return ret;
937 }
938
939 /**
940  * Returns 1 if the given modifier is active with the specified type(s), 0 if
941  * not, or -1 if the modifier is invalid.
942  */
943 XKB_EXPORT int
944 xkb_state_layout_name_is_active(struct xkb_state *state, const char *name,
945                                 enum xkb_state_component type)
946 {
947     xkb_layout_index_t idx = xkb_keymap_layout_get_index(state->keymap, name);
948
949     if (idx == XKB_LAYOUT_INVALID)
950         return -1;
951
952     return xkb_state_layout_index_is_active(state, idx, type);
953 }
954
955 /**
956  * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
957  */
958 XKB_EXPORT int
959 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
960 {
961     if (idx >= xkb_keymap_num_leds(state->keymap))
962         return -1;
963
964     return !!(state->leds & (1 << idx));
965 }
966
967 /**
968  * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
969  */
970 XKB_EXPORT int
971 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
972 {
973     xkb_led_index_t idx = xkb_keymap_led_get_index(state->keymap, name);
974
975     if (idx == XKB_LED_INVALID)
976         return -1;
977
978     return xkb_state_led_index_is_active(state, idx);
979 }
980
981 static xkb_mod_mask_t
982 key_get_consumed(struct xkb_state *state, const struct xkb_key *key)
983 {
984     struct xkb_kt_map_entry *entry;
985     xkb_layout_index_t group;
986
987     group = xkb_state_key_get_layout(state, key->keycode);
988     if (group == XKB_LAYOUT_INVALID)
989         return 0;
990
991     entry = get_entry_for_key_state(state, key, group);
992     if (!entry)
993         return 0;
994
995     return entry->mods.mask & ~entry->preserve.mask;
996 }
997
998 /**
999  * Tests to see if a modifier is used up by our translation of a
1000  * keycode to keysyms, taking note of the current modifier state and
1001  * the appropriate key type's preserve information, if any. This allows
1002  * the user to mask out the modifier in later processing of the
1003  * modifiers, e.g. when implementing hot keys or accelerators.
1004  *
1005  * See also, for example:
1006  * - XkbTranslateKeyCode(3), mod_rtrn retrun value, from libX11.
1007  * - gdk_keymap_translate_keyboard_state, consumed_modifiers return value,
1008  *   from gtk+.
1009  */
1010 XKB_EXPORT int
1011 xkb_state_mod_index_is_consumed(struct xkb_state *state, xkb_keycode_t kc,
1012                                 xkb_mod_index_t idx)
1013 {
1014     const struct xkb_key *key = XkbKey(state->keymap, kc);
1015
1016     if (!key)
1017         return 0;
1018
1019     return !!((1 << idx) & key_get_consumed(state, key));
1020 }
1021
1022 /**
1023  * Calculates which modifiers should be consumed during key processing,
1024  * and returns the mask with all these modifiers removed.  e.g. if
1025  * given a state of Alt and Shift active for a two-level alphabetic
1026  * key containing plus and equal on the first and second level
1027  * respectively, will return a mask of only Alt, as Shift has been
1028  * consumed by the type handling.
1029  */
1030 XKB_EXPORT xkb_mod_mask_t
1031 xkb_state_mod_mask_remove_consumed(struct xkb_state *state, xkb_keycode_t kc,
1032                                    xkb_mod_mask_t mask)
1033 {
1034     const struct xkb_key *key = XkbKey(state->keymap, kc);
1035
1036     if (!key)
1037         return 0;
1038
1039     return mask & ~key_get_consumed(state, key);
1040 }