Use (1u << idx) instead of (1 << idx) where appropriate
[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 #include "keysym.h"
64
65 struct xkb_filter {
66     union xkb_action action;
67     const struct xkb_key *key;
68     uint32_t priv;
69     bool (*func)(struct xkb_state *state,
70                  struct xkb_filter *filter,
71                  const struct xkb_key *key,
72                  enum xkb_key_direction direction);
73     int refcnt;
74 };
75
76 struct state_components {
77     /* These may be negative, because of -1 group actions. */
78     int32_t base_group; /**< depressed */
79     int32_t latched_group;
80     int32_t locked_group;
81     xkb_layout_index_t group; /**< effective */
82
83     xkb_mod_mask_t base_mods; /**< depressed */
84     xkb_mod_mask_t latched_mods;
85     xkb_mod_mask_t locked_mods;
86     xkb_mod_mask_t mods; /**< effective */
87
88     xkb_led_mask_t leds;
89 };
90
91 struct xkb_state {
92     /*
93      * Before updating the state, we keep a copy of just this struct. This
94      * allows us to report which components of the state have changed.
95      */
96     struct state_components components;
97
98     /*
99      * At each event, we accumulate all the needed modifications to the base
100      * modifiers, and apply them at the end. These keep track of this state.
101      */
102     xkb_mod_mask_t set_mods;
103     xkb_mod_mask_t clear_mods;
104
105     /*
106      * We mustn't clear a base modifier if there's another depressed key
107      * which affects it, e.g. given this sequence
108      * < Left Shift down, Right Shift down, Left Shift Up >
109      * the modifier should still be set. This keeps the count.
110      */
111     int16_t mod_key_count[XKB_MAX_MODS];
112
113     int refcnt;
114     darray(struct xkb_filter) filters;
115     struct xkb_keymap *keymap;
116 };
117
118 static const struct xkb_key_type_entry *
119 get_entry_for_key_state(struct xkb_state *state, const struct xkb_key *key,
120                         xkb_layout_index_t group)
121 {
122     const struct xkb_key_type *type = key->groups[group].type;
123     xkb_mod_mask_t active_mods = state->components.mods & type->mods.mask;
124
125     for (unsigned 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->entries[i].mods.mask)
132             continue;
133
134         if (type->entries[i].mods.mask == active_mods)
135             return &type->entries[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_key_type_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 >= 0 && (xkb_layout_index_t) 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 bool
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 true;
268     }
269
270     if (direction == XKB_KEY_DOWN) {
271         filter->refcnt++;
272         return false;
273     }
274     else if (--filter->refcnt > 0) {
275         return false;
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 true;
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 bool
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 true;
305
306     if (direction == XKB_KEY_DOWN) {
307         filter->refcnt++;
308         return false;
309     }
310     if (--filter->refcnt > 0)
311         return false;
312
313     filter->func = NULL;
314     return true;
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 bool
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 true;
335     }
336
337     if (direction == XKB_KEY_DOWN) {
338         filter->refcnt++;
339         return false;
340     }
341     else if (--filter->refcnt > 0) {
342         return false;
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 true;
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 bool
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 true;
367
368     if (direction == XKB_KEY_DOWN) {
369         filter->refcnt++;
370         return false;
371     }
372     if (--filter->refcnt > 0)
373         return false;
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 true;
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_SWITCH_VT:
409     case ACTION_TYPE_TERMINATE:
410         return true;
411     default:
412         return false;
413     }
414 }
415
416 static bool
417 xkb_filter_mod_latch_func(struct xkb_state *state,
418                           struct xkb_filter *filter,
419                           const struct xkb_key *key,
420                           enum xkb_key_direction direction)
421 {
422     enum xkb_key_latch_state latch = filter->priv;
423
424     if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
425         /* If this is a new keypress and we're awaiting our single latched
426          * keypress, then either break the latch if any random key is pressed,
427          * or promote it to a lock or plain base set if it's the same
428          * modifier. */
429         const union xkb_action *action = xkb_key_get_action(state, key);
430         if (action->type == ACTION_TYPE_MOD_LATCH &&
431             action->mods.flags == filter->action.mods.flags &&
432             action->mods.mods.mask == filter->action.mods.mods.mask) {
433             filter->action = *action;
434             if (filter->action.mods.flags & ACTION_LATCH_TO_LOCK) {
435                 filter->action.type = ACTION_TYPE_MOD_LOCK;
436                 filter->func = xkb_filter_mod_lock_func;
437                 state->components.locked_mods |= filter->action.mods.mods.mask;
438             }
439             else {
440                 filter->action.type = ACTION_TYPE_MOD_SET;
441                 filter->func = xkb_filter_mod_set_func;
442                 state->set_mods = filter->action.mods.mods.mask;
443             }
444             filter->key = key;
445             state->components.latched_mods &= ~filter->action.mods.mods.mask;
446             /* XXX beep beep! */
447             return false;
448         }
449         else if (xkb_action_breaks_latch(action)) {
450             /* XXX: This may be totally broken, we might need to break the
451              *      latch in the next run after this press? */
452             state->components.latched_mods &= ~filter->action.mods.mods.mask;
453             filter->func = NULL;
454             return true;
455         }
456     }
457     else if (direction == XKB_KEY_UP && key == filter->key) {
458         /* Our key got released.  If we've set it to clear locks, and we
459          * currently have the same modifiers locked, then release them and
460          * don't actually latch.  Else we've actually hit the latching
461          * stage, so set PENDING and move our modifier from base to
462          * latched. */
463         if (latch == NO_LATCH ||
464             ((filter->action.mods.flags & ACTION_LOCK_CLEAR) &&
465              (state->components.locked_mods & filter->action.mods.mods.mask) ==
466              filter->action.mods.mods.mask)) {
467             /* XXX: We might be a bit overenthusiastic about clearing
468              *      mods other filters have set here? */
469             if (latch == LATCH_PENDING)
470                 state->components.latched_mods &=
471                     ~filter->action.mods.mods.mask;
472             else
473                 state->clear_mods = filter->action.mods.mods.mask;
474             state->components.locked_mods &= ~filter->action.mods.mods.mask;
475             filter->func = NULL;
476         }
477         else {
478             latch = LATCH_PENDING;
479             state->clear_mods = filter->action.mods.mods.mask;
480             state->components.latched_mods |= filter->action.mods.mods.mask;
481             /* XXX beep beep! */
482         }
483     }
484     else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
485         /* Someone's pressed another key while we've still got the latching
486          * key held down, so keep the base modifier state active (from
487          * xkb_filter_mod_latch_new), but don't trip the latch, just clear
488          * it as soon as the modifier gets released. */
489         latch = NO_LATCH;
490     }
491
492     filter->priv = latch;
493
494     return true;
495 }
496
497 static void
498 xkb_filter_mod_latch_new(struct xkb_state *state, struct xkb_filter *filter)
499 {
500     filter->priv = LATCH_KEY_DOWN;
501     state->set_mods = filter->action.mods.mods.mask;
502 }
503
504 static const struct {
505     void (*new)(struct xkb_state *state, struct xkb_filter *filter);
506     bool (*func)(struct xkb_state *state, struct xkb_filter *filter,
507                  const struct xkb_key *key, enum xkb_key_direction direction);
508 } filter_action_funcs[_ACTION_TYPE_NUM_ENTRIES] = {
509     [ACTION_TYPE_MOD_SET]    = { xkb_filter_mod_set_new,
510                                  xkb_filter_mod_set_func },
511     [ACTION_TYPE_MOD_LATCH]  = { xkb_filter_mod_latch_new,
512                                  xkb_filter_mod_latch_func },
513     [ACTION_TYPE_MOD_LOCK]   = { xkb_filter_mod_lock_new,
514                                  xkb_filter_mod_lock_func },
515     [ACTION_TYPE_GROUP_SET]  = { xkb_filter_group_set_new,
516                                  xkb_filter_group_set_func },
517     [ACTION_TYPE_GROUP_LOCK] = { xkb_filter_group_lock_new,
518                                  xkb_filter_group_lock_func },
519 };
520
521 /**
522  * Applies any relevant filters to the key, first from the list of filters
523  * that are currently active, then if no filter has claimed the key, possibly
524  * apply a new filter from the key action.
525  */
526 static void
527 xkb_filter_apply_all(struct xkb_state *state,
528                      const struct xkb_key *key,
529                      enum xkb_key_direction direction)
530 {
531     struct xkb_filter *filter;
532     const union xkb_action *action;
533     bool send = true;
534
535     /* First run through all the currently active filters and see if any of
536      * them have claimed this event. */
537     darray_foreach(filter, state->filters) {
538         if (!filter->func)
539             continue;
540         send = filter->func(state, filter, key, direction) && send;
541     }
542
543     if (!send || direction == XKB_KEY_UP)
544         return;
545
546     action = xkb_key_get_action(state, key);
547
548     /*
549      * It's possible for the keymap to set action->type explicitly, like so:
550      *     interpret XF86_Next_VMode {
551      *         action = Private(type=0x86, data="+VMode");
552      *     };
553      * We don't handle those.
554      */
555     if (action->type >= _ACTION_TYPE_NUM_ENTRIES)
556         return;
557
558     if (!filter_action_funcs[action->type].new)
559         return;
560
561     filter = xkb_filter_new(state);
562     if (!filter)
563         return; /* WSGO */
564
565     filter->key = key;
566     filter->func = filter_action_funcs[action->type].func;
567     filter->action = *action;
568     filter_action_funcs[action->type].new(state, filter);
569 }
570
571 XKB_EXPORT struct xkb_state *
572 xkb_state_new(struct xkb_keymap *keymap)
573 {
574     struct xkb_state *ret;
575
576     ret = calloc(sizeof(*ret), 1);
577     if (!ret)
578         return NULL;
579
580     ret->refcnt = 1;
581     ret->keymap = xkb_keymap_ref(keymap);
582
583     return ret;
584 }
585
586 XKB_EXPORT struct xkb_state *
587 xkb_state_ref(struct xkb_state *state)
588 {
589     state->refcnt++;
590     return state;
591 }
592
593 XKB_EXPORT void
594 xkb_state_unref(struct xkb_state *state)
595 {
596     if (!state || --state->refcnt > 0)
597         return;
598
599     xkb_keymap_unref(state->keymap);
600     darray_free(state->filters);
601     free(state);
602 }
603
604 XKB_EXPORT struct xkb_keymap *
605 xkb_state_get_keymap(struct xkb_state *state)
606 {
607     return state->keymap;
608 }
609
610 /**
611  * Update the LED state to match the rest of the xkb_state.
612  */
613 static void
614 xkb_state_led_update_all(struct xkb_state *state)
615 {
616     xkb_led_index_t idx;
617     const struct xkb_led *led;
618
619     state->components.leds = 0;
620
621     darray_enumerate(idx, led, state->keymap->leds) {
622         xkb_mod_mask_t mod_mask = 0;
623         xkb_layout_mask_t group_mask = 0;
624
625         if (led->which_mods & XKB_STATE_MODS_EFFECTIVE)
626             mod_mask |= state->components.mods;
627         if (led->which_mods & XKB_STATE_MODS_DEPRESSED)
628             mod_mask |= state->components.base_mods;
629         if (led->which_mods & XKB_STATE_MODS_LATCHED)
630             mod_mask |= state->components.latched_mods;
631         if (led->which_mods & XKB_STATE_MODS_LOCKED)
632             mod_mask |= state->components.locked_mods;
633         if (led->mods.mask & mod_mask)
634             state->components.leds |= (1u << idx);
635
636         if (led->which_groups & XKB_STATE_LAYOUT_EFFECTIVE)
637             group_mask |= (1u << state->components.group);
638         if (led->which_groups & XKB_STATE_LAYOUT_DEPRESSED)
639             group_mask |= (1u << state->components.base_group);
640         if (led->which_groups & XKB_STATE_LAYOUT_LATCHED)
641             group_mask |= (1u << state->components.latched_group);
642         if (led->which_groups & XKB_STATE_LAYOUT_LOCKED)
643             group_mask |= (1u << state->components.locked_group);
644         if (led->groups & group_mask)
645             state->components.leds |= (1u << idx);
646
647         if (led->ctrls & state->keymap->enabled_ctrls)
648             state->components.leds |= (1u << idx);
649     }
650 }
651
652 /**
653  * Calculates the derived state (effective mods/group and LEDs) from an
654  * up-to-date xkb_state.
655  */
656 static void
657 xkb_state_update_derived(struct xkb_state *state)
658 {
659     state->components.mods = (state->components.base_mods |
660                               state->components.latched_mods |
661                               state->components.locked_mods);
662
663     /* TODO: Use groups_wrap control instead of always RANGE_WRAP. */
664
665     state->components.locked_group =
666         wrap_group_into_range(state->components.locked_group,
667                               state->keymap->num_groups,
668                               RANGE_WRAP, 0);
669
670     state->components.group =
671         wrap_group_into_range(state->components.base_group +
672                               state->components.latched_group +
673                               state->components.locked_group,
674                               state->keymap->num_groups,
675                               RANGE_WRAP, 0);
676
677     xkb_state_led_update_all(state);
678 }
679
680 static enum xkb_state_component
681 get_state_component_changes(const struct state_components *a,
682                             const struct state_components *b)
683 {
684     xkb_mod_mask_t mask = 0;
685
686     if (a->group != b->group)
687         mask |= XKB_STATE_LAYOUT_EFFECTIVE;
688     if (a->base_group != b->base_group)
689         mask |= XKB_STATE_LAYOUT_DEPRESSED;
690     if (a->latched_group != b->latched_group)
691         mask |= XKB_STATE_LAYOUT_LATCHED;
692     if (a->locked_group != b->locked_group)
693         mask |= XKB_STATE_LAYOUT_LOCKED;
694     if (a->mods != b->mods)
695         mask |= XKB_STATE_MODS_EFFECTIVE;
696     if (a->base_mods != b->base_mods)
697         mask |= XKB_STATE_MODS_DEPRESSED;
698     if (a->latched_mods != b->latched_mods)
699         mask |= XKB_STATE_MODS_LATCHED;
700     if (a->locked_mods != b->locked_mods)
701         mask |= XKB_STATE_MODS_LOCKED;
702     if (a->leds != b->leds)
703         mask |= XKB_STATE_LEDS;
704
705     return mask;
706 }
707
708 /**
709  * Given a particular key event, updates the state structure to reflect the
710  * new modifiers.
711  */
712 XKB_EXPORT enum xkb_state_component
713 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
714                      enum xkb_key_direction direction)
715 {
716     xkb_mod_index_t i;
717     xkb_mod_mask_t bit;
718     struct state_components prev_components;
719     const struct xkb_key *key = XkbKey(state->keymap, kc);
720
721     if (!key)
722         return 0;
723
724     prev_components = state->components;
725
726     state->set_mods = 0;
727     state->clear_mods = 0;
728
729     xkb_filter_apply_all(state, key, direction);
730
731     for (i = 0, bit = 1; state->set_mods; i++, bit <<= 1) {
732         if (state->set_mods & bit) {
733             state->mod_key_count[i]++;
734             state->components.base_mods |= bit;
735             state->set_mods &= ~bit;
736         }
737     }
738
739     for (i = 0, bit = 1; state->clear_mods; i++, bit <<= 1) {
740         if (state->clear_mods & bit) {
741             state->mod_key_count[i]--;
742             if (state->mod_key_count[i] <= 0) {
743                 state->components.base_mods &= ~bit;
744                 state->mod_key_count[i] = 0;
745             }
746             state->clear_mods &= ~bit;
747         }
748     }
749
750     xkb_state_update_derived(state);
751
752     return get_state_component_changes(&prev_components, &state->components);
753 }
754
755 /**
756  * Updates the state from a set of explicit masks as gained from
757  * xkb_state_serialize_mods and xkb_state_serialize_groups.  As noted in the
758  * documentation for these functions in xkbcommon.h, this round-trip is
759  * lossy, and should only be used to update a slave state mirroring the
760  * master, e.g. in a client/server window system.
761  */
762 XKB_EXPORT enum xkb_state_component
763 xkb_state_update_mask(struct xkb_state *state,
764                       xkb_mod_mask_t base_mods,
765                       xkb_mod_mask_t latched_mods,
766                       xkb_mod_mask_t locked_mods,
767                       xkb_layout_index_t base_group,
768                       xkb_layout_index_t latched_group,
769                       xkb_layout_index_t locked_group)
770 {
771     struct state_components prev_components;
772     xkb_mod_index_t num_mods;
773     xkb_mod_index_t idx;
774
775     prev_components = state->components;
776
777     state->components.base_mods = 0;
778     state->components.latched_mods = 0;
779     state->components.locked_mods = 0;
780     num_mods = xkb_keymap_num_mods(state->keymap);
781
782     for (idx = 0; idx < num_mods; idx++) {
783         xkb_mod_mask_t mod = (1u << idx);
784         if (base_mods & mod)
785             state->components.base_mods |= mod;
786         if (latched_mods & mod)
787             state->components.latched_mods |= mod;
788         if (locked_mods & mod)
789             state->components.locked_mods |= mod;
790     }
791
792     state->components.base_group = base_group;
793     state->components.latched_group = latched_group;
794     state->components.locked_group = locked_group;
795
796     xkb_state_update_derived(state);
797
798     return get_state_component_changes(&prev_components, &state->components);
799 }
800
801 /**
802  * Provides the symbols to use for the given key and state.  Returns the
803  * number of symbols pointed to in syms_out.
804  */
805 XKB_EXPORT int
806 xkb_state_key_get_syms(struct xkb_state *state, xkb_keycode_t kc,
807                        const xkb_keysym_t **syms_out)
808 {
809     xkb_layout_index_t layout;
810     xkb_level_index_t level;
811
812     layout = xkb_state_key_get_layout(state, kc);
813     if (layout == XKB_LAYOUT_INVALID)
814         goto err;
815
816     level = xkb_state_key_get_level(state, kc, layout);
817     if (level == XKB_LEVEL_INVALID)
818         goto err;
819
820     return xkb_keymap_key_get_syms_by_level(state->keymap, kc, layout, level,
821                                             syms_out);
822
823 err:
824     *syms_out = NULL;
825     return 0;
826 }
827
828 /**
829  * Provides either exactly one symbol, or XKB_KEY_NoSymbol.
830  */
831 XKB_EXPORT xkb_keysym_t
832 xkb_state_key_get_one_sym(struct xkb_state *state, xkb_keycode_t kc)
833 {
834     const xkb_keysym_t *syms;
835     xkb_keysym_t sym;
836     int num_syms;
837     xkb_mod_index_t caps;
838
839     num_syms = xkb_state_key_get_syms(state, kc, &syms);
840     if (num_syms != 1)
841         return XKB_KEY_NoSymbol;
842
843     sym = syms[0];
844
845     /*
846      * Perform capitalization transformation, see:
847      * http://www.x.org/releases/current/doc/kbproto/xkbproto.html#Interpreting_the_Lock_Modifier
848      */
849     caps = xkb_keymap_mod_get_index(state->keymap, XKB_MOD_NAME_CAPS);
850     if (xkb_state_mod_index_is_active(state, caps, XKB_STATE_MODS_EFFECTIVE) > 0 &&
851         xkb_state_mod_index_is_consumed(state, kc, caps) == 0)
852         sym = xkb_keysym_to_upper(sym);
853
854     return sym;
855 }
856
857 /**
858  * Serialises the requested modifier state into an xkb_mod_mask_t, with all
859  * the same disclaimers as in xkb_state_update_mask.
860  */
861 XKB_EXPORT xkb_mod_mask_t
862 xkb_state_serialize_mods(struct xkb_state *state,
863                          enum xkb_state_component type)
864 {
865     xkb_mod_mask_t ret = 0;
866
867     if (type & XKB_STATE_MODS_EFFECTIVE)
868         return state->components.mods;
869
870     if (type & XKB_STATE_MODS_DEPRESSED)
871         ret |= state->components.base_mods;
872     if (type & XKB_STATE_MODS_LATCHED)
873         ret |= state->components.latched_mods;
874     if (type & XKB_STATE_MODS_LOCKED)
875         ret |= state->components.locked_mods;
876
877     return ret;
878 }
879
880 /**
881  * Serialises the requested group state, with all the same disclaimers as
882  * in xkb_state_update_mask.
883  */
884 XKB_EXPORT xkb_layout_index_t
885 xkb_state_serialize_layout(struct xkb_state *state,
886                            enum xkb_state_component type)
887 {
888     xkb_layout_index_t ret = 0;
889
890     if (type & XKB_STATE_LAYOUT_EFFECTIVE)
891         return state->components.group;
892
893     if (type & XKB_STATE_LAYOUT_DEPRESSED)
894         ret += state->components.base_group;
895     if (type & XKB_STATE_LAYOUT_LATCHED)
896         ret += state->components.latched_group;
897     if (type & XKB_STATE_LAYOUT_LOCKED)
898         ret += state->components.locked_group;
899
900     return ret;
901 }
902
903 /**
904  * Returns 1 if the given modifier is active with the specified type(s), 0 if
905  * not, or -1 if the modifier is invalid.
906  */
907 XKB_EXPORT int
908 xkb_state_mod_index_is_active(struct xkb_state *state,
909                               xkb_mod_index_t idx,
910                               enum xkb_state_component type)
911 {
912     if (idx >= xkb_keymap_num_mods(state->keymap))
913         return -1;
914
915     return !!(xkb_state_serialize_mods(state, type) & (1u << idx));
916 }
917
918 /**
919  * Helper function for xkb_state_mod_indices_are_active and
920  * xkb_state_mod_names_are_active.
921  */
922 static int
923 match_mod_masks(struct xkb_state *state,
924                 enum xkb_state_component type,
925                 enum xkb_state_match match,
926                 xkb_mod_mask_t wanted)
927 {
928     xkb_mod_mask_t active = xkb_state_serialize_mods(state, type);
929
930     if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
931         return 0;
932
933     if (match & XKB_STATE_MATCH_ANY)
934         return !!(active & wanted);
935     else
936         return (active & wanted) == wanted;
937
938     return 0;
939 }
940
941 /**
942  * Returns 1 if the modifiers are active with the specified type(s), 0 if
943  * not, or -1 if any of the modifiers are invalid.
944  */
945 XKB_EXPORT int
946 xkb_state_mod_indices_are_active(struct xkb_state *state,
947                                  enum xkb_state_component type,
948                                  enum xkb_state_match match,
949                                  ...)
950 {
951     va_list ap;
952     xkb_mod_index_t idx = 0;
953     xkb_mod_mask_t wanted = 0;
954     int ret = 0;
955     xkb_mod_index_t num_mods = xkb_keymap_num_mods(state->keymap);
956
957     va_start(ap, match);
958     while (1) {
959         idx = va_arg(ap, xkb_mod_index_t);
960         if (idx == XKB_MOD_INVALID)
961             break;
962         if (idx >= num_mods) {
963             ret = -1;
964             break;
965         }
966         wanted |= (1u << idx);
967     }
968     va_end(ap);
969
970     if (ret == -1)
971         return ret;
972
973     return match_mod_masks(state, type, match, wanted);
974 }
975
976 /**
977  * Returns 1 if the given modifier is active with the specified type(s), 0 if
978  * not, or -1 if the modifier is invalid.
979  */
980 XKB_EXPORT int
981 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
982                              enum xkb_state_component type)
983 {
984     xkb_mod_index_t idx = xkb_keymap_mod_get_index(state->keymap, name);
985
986     if (idx == XKB_MOD_INVALID)
987         return -1;
988
989     return xkb_state_mod_index_is_active(state, idx, type);
990 }
991
992 /**
993  * Returns 1 if the modifiers are active with the specified type(s), 0 if
994  * not, or -1 if any of the modifiers are invalid.
995  */
996 XKB_EXPORT ATTR_NULL_SENTINEL int
997 xkb_state_mod_names_are_active(struct xkb_state *state,
998                                enum xkb_state_component type,
999                                enum xkb_state_match match,
1000                                ...)
1001 {
1002     va_list ap;
1003     xkb_mod_index_t idx = 0;
1004     xkb_mod_mask_t wanted = 0;
1005     int ret = 0;
1006
1007     va_start(ap, match);
1008     while (1) {
1009         const char *str = va_arg(ap, const char *);
1010         if (str == NULL)
1011             break;
1012         idx = xkb_keymap_mod_get_index(state->keymap, str);
1013         if (idx == XKB_MOD_INVALID) {
1014             ret = -1;
1015             break;
1016         }
1017         wanted |= (1u << idx);
1018     }
1019     va_end(ap);
1020
1021     if (ret == -1)
1022         return ret;
1023
1024     return match_mod_masks(state, type, match, wanted);
1025 }
1026
1027 /**
1028  * Returns 1 if the given group is active with the specified type(s), 0 if
1029  * not, or -1 if the group is invalid.
1030  */
1031 XKB_EXPORT int
1032 xkb_state_layout_index_is_active(struct xkb_state *state,
1033                                 xkb_layout_index_t idx,
1034                                 enum xkb_state_component type)
1035 {
1036     int ret = 0;
1037
1038     if (idx >= state->keymap->num_groups)
1039         return -1;
1040
1041     if (type & XKB_STATE_LAYOUT_EFFECTIVE)
1042         ret |= (state->components.group == idx);
1043     if (type & XKB_STATE_LAYOUT_DEPRESSED)
1044         ret |= (state->components.base_group == (int32_t) idx);
1045     if (type & XKB_STATE_LAYOUT_LATCHED)
1046         ret |= (state->components.latched_group == (int32_t) idx);
1047     if (type & XKB_STATE_LAYOUT_LOCKED)
1048         ret |= (state->components.locked_group == (int32_t) idx);
1049
1050     return ret;
1051 }
1052
1053 /**
1054  * Returns 1 if the given modifier is active with the specified type(s), 0 if
1055  * not, or -1 if the modifier is invalid.
1056  */
1057 XKB_EXPORT int
1058 xkb_state_layout_name_is_active(struct xkb_state *state, const char *name,
1059                                 enum xkb_state_component type)
1060 {
1061     xkb_layout_index_t idx = xkb_keymap_layout_get_index(state->keymap, name);
1062
1063     if (idx == XKB_LAYOUT_INVALID)
1064         return -1;
1065
1066     return xkb_state_layout_index_is_active(state, idx, type);
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_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
1074 {
1075     if (idx >= darray_size(state->keymap->leds) ||
1076         darray_item(state->keymap->leds, idx).name == XKB_ATOM_NONE)
1077         return -1;
1078
1079     return !!(state->components.leds & (1u << idx));
1080 }
1081
1082 /**
1083  * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
1084  */
1085 XKB_EXPORT int
1086 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
1087 {
1088     xkb_led_index_t idx = xkb_keymap_led_get_index(state->keymap, name);
1089
1090     if (idx == XKB_LED_INVALID)
1091         return -1;
1092
1093     return xkb_state_led_index_is_active(state, idx);
1094 }
1095
1096 static xkb_mod_mask_t
1097 key_get_consumed(struct xkb_state *state, const struct xkb_key *key)
1098 {
1099     const struct xkb_key_type_entry *entry;
1100     xkb_layout_index_t group;
1101
1102     group = xkb_state_key_get_layout(state, key->keycode);
1103     if (group == XKB_LAYOUT_INVALID)
1104         return 0;
1105
1106     entry = get_entry_for_key_state(state, key, group);
1107     if (!entry)
1108         return 0;
1109
1110     return entry->mods.mask & ~entry->preserve.mask;
1111 }
1112
1113 /**
1114  * Tests to see if a modifier is used up by our translation of a
1115  * keycode to keysyms, taking note of the current modifier state and
1116  * the appropriate key type's preserve information, if any. This allows
1117  * the user to mask out the modifier in later processing of the
1118  * modifiers, e.g. when implementing hot keys or accelerators.
1119  *
1120  * See also, for example:
1121  * - XkbTranslateKeyCode(3), mod_rtrn return value, from libX11.
1122  * - gdk_keymap_translate_keyboard_state, consumed_modifiers return value,
1123  *   from gtk+.
1124  */
1125 XKB_EXPORT int
1126 xkb_state_mod_index_is_consumed(struct xkb_state *state, xkb_keycode_t kc,
1127                                 xkb_mod_index_t idx)
1128 {
1129     const struct xkb_key *key = XkbKey(state->keymap, kc);
1130
1131     if (!key || idx >= xkb_keymap_num_mods(state->keymap))
1132         return -1;
1133
1134     return !!((1u << idx) & key_get_consumed(state, key));
1135 }
1136
1137 /**
1138  * Calculates which modifiers should be consumed during key processing,
1139  * and returns the mask with all these modifiers removed.  e.g. if
1140  * given a state of Alt and Shift active for a two-level alphabetic
1141  * key containing plus and equal on the first and second level
1142  * respectively, will return a mask of only Alt, as Shift has been
1143  * consumed by the type handling.
1144  */
1145 XKB_EXPORT xkb_mod_mask_t
1146 xkb_state_mod_mask_remove_consumed(struct xkb_state *state, xkb_keycode_t kc,
1147                                    xkb_mod_mask_t mask)
1148 {
1149     const struct xkb_key *key = XkbKey(state->keymap, kc);
1150
1151     if (!key)
1152         return 0;
1153
1154     return mask & ~key_get_consumed(state, key);
1155 }