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