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