Update xkb_filter_group_lock_func for xkb_key_direction
[platform/upstream/libxkbcommon.git] / src / state.c
1 /************************************************************
2 Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
3
4 Permission to use, copy, modify, and distribute this
5 software and its documentation for any purpose and without
6 fee is hereby granted, provided that the above copyright
7 notice appear in all copies and that both that copyright
8 notice and this permission notice appear in supporting
9 documentation, and that the name of Silicon Graphics not be
10 used in advertising or publicity pertaining to distribution
11 of the software without specific prior written permission.
12 Silicon Graphics makes no representation about the suitability
13 of this software for any purpose. It is provided "as is"
14 without any express or implied warranty.
15
16 SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17 SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18 AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
23 THE USE OR PERFORMANCE OF THIS SOFTWARE.
24
25 ********************************************************/
26
27 /*
28  * Copyright © 2012 Intel Corporation
29  *
30  * Permission is hereby granted, free of charge, to any person obtaining a
31  * copy of this software and associated documentation files (the "Software"),
32  * to deal in the Software without restriction, including without limitation
33  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
34  * and/or sell copies of the Software, and to permit persons to whom the
35  * Software is furnished to do so, subject to the following conditions:
36  *
37  * The above copyright notice and this permission notice (including the next
38  * paragraph) shall be included in all copies or substantial portions of the
39  * Software.
40  *
41  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
42  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
43  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
44  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
45  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
46  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
47  * DEALINGS IN THE SOFTWARE.
48  *
49  * Author: Daniel Stone <daniel@fooishbar.org>
50  */
51
52 /*
53  * This is a bastardised version of xkbActions.c from the X server which
54  * does not support, for the moment:
55  *   - AccessX sticky/debounce/etc (will come later)
56  *   - pointer keys (may come later)
57  *   - key redirects (unlikely)
58  *   - messages (very unlikely)
59  */
60
61 #ifdef HAVE_CONFIG_H
62 #include <config.h>
63 #endif
64
65 #include <assert.h>
66
67 #include "xkbcommon/xkbcommon.h"
68 #include "XKBcommonint.h"
69
70 struct xkb_filter {
71     struct xkb_state *state;
72     union xkb_action action;
73     xkb_keycode_t keycode;
74     uint32_t priv;
75     int (*func)(struct xkb_filter *filter, xkb_keycode_t key,
76                 enum xkb_key_direction direction);
77     int refcnt;
78     struct xkb_filter *next;
79 };
80
81 static union xkb_action *
82 xkb_key_get_action(struct xkb_state *state, xkb_keycode_t key)
83 {
84     int group, level;
85
86     if (!XkbKeyHasActions(state->xkb, key) ||
87         !XkbKeycodeInRange(state->xkb, key)) {
88         static union xkb_action fake;
89         memset(&fake, 0, sizeof(fake));
90         fake.type = XkbSA_NoAction;
91         return &fake;
92     }
93
94     group = xkb_key_get_group(state, key);
95     level = xkb_key_get_level(state, key, group);
96
97     return XkbKeyActionEntry(state->xkb, key, level, group);
98 }
99
100 static struct xkb_filter *
101 xkb_filter_new(struct xkb_state *state)
102 {
103     int i;
104     int old_size = state->num_filters;
105     struct xkb_filter *filters = state->filters;
106
107     for (i = 0; i < state->num_filters; i++) {
108         if (filters[i].func)
109             continue;
110         filters[i].state = state;
111         filters[i].refcnt = 1;
112         return &filters[i];
113     }
114
115     if (state->num_filters > 0)
116         state->num_filters *= 2;
117     else
118         state->num_filters = 4;
119     filters = realloc(filters, state->num_filters * sizeof(*filters));
120     if (!filters) { /* WSGO */
121         state->num_filters = old_size;
122         return NULL;
123     }
124     state->filters = filters;
125     memset(&filters[old_size], 0,
126            (state->num_filters - old_size) * sizeof(*filters));
127
128     filters[old_size].state = state;
129     filters[old_size].refcnt = 1;
130
131     return &filters[old_size];
132 }
133
134 /***====================================================================***/
135
136 static int
137 xkb_filter_group_set_func(struct xkb_filter *filter, xkb_keycode_t keycode,
138                           enum xkb_key_direction direction)
139 {
140     if (keycode != filter->keycode) {
141         filter->action.group.flags &= ~XkbSA_ClearLocks;
142         return 1;
143     }
144
145     if (direction == XKB_KEY_DOWN) {
146         filter->refcnt++;
147         return 0;
148     }
149     else if (--filter->refcnt > 0) {
150         return 0;
151     }
152
153     if (filter->action.group.flags & XkbSA_GroupAbsolute)
154         filter->state->base_group = filter->action.group.group;
155     else
156         filter->state->base_group = -filter->action.group.group;
157     if (filter->action.group.flags & XkbSA_ClearLocks)
158         filter->state->locked_group = 0;
159
160     filter->func = NULL;
161
162     return 1;
163 }
164
165 static int
166 xkb_filter_group_set_new(struct xkb_state *state, xkb_keycode_t keycode,
167                          union xkb_action *action)
168 {
169     struct xkb_filter *filter = xkb_filter_new(state);
170
171     if (!filter) /* WSGO */
172         return -1;
173     filter->keycode = keycode;
174     filter->func = xkb_filter_group_set_func;
175     filter->action = *action;
176
177     if (action->group.flags & XkbSA_GroupAbsolute) {
178         filter->action.group.group = filter->state->base_group;
179         filter->state->base_group = action->group.group;
180     }
181     else {
182         filter->state->base_group += action->group.group;
183     }
184
185     return 1;
186 }
187
188 static int
189 xkb_filter_group_lock_func(struct xkb_filter *filter, xkb_keycode_t keycode,
190                            enum xkb_key_direction direction)
191 {
192     if (keycode != filter->keycode)
193         return 1;
194
195     if (direction == XKB_KEY_DOWN) {
196         filter->refcnt++;
197         return 0;
198     }
199     if (--filter->refcnt > 0)
200         return 0;
201
202     filter->func = NULL;
203     return 1;
204 }
205
206 static int
207 xkb_filter_group_lock_new(struct xkb_state *state, xkb_keycode_t keycode,
208                           union xkb_action *action)
209 {
210     struct xkb_filter *filter = xkb_filter_new(state);
211
212     if (!filter)
213         return 0;
214
215     filter->keycode = keycode;
216     filter->func = xkb_filter_group_lock_func;
217     filter->action = *action;
218
219     if (action->group.flags & XkbSA_GroupAbsolute)
220         filter->state->locked_group = action->group.group;
221     else
222         filter->state->locked_group += action->group.group;
223
224     return 1;
225 }
226
227 static int
228 xkb_filter_mod_set_func(struct xkb_filter *filter, xkb_keycode_t keycode,
229                         enum xkb_key_direction direction)
230 {
231     if (keycode != filter->keycode) {
232         filter->action.mods.flags &= ~XkbSA_ClearLocks;
233         return 1;
234     }
235
236     if (direction == XKB_KEY_DOWN) {
237         filter->refcnt++;
238         return 0;
239     }
240     else if (--filter->refcnt > 0) {
241         return 0;
242     }
243
244     filter->state->base_mods &= ~(filter->action.mods.mask);
245     if (filter->action.mods.flags & XkbSA_ClearLocks)
246         filter->state->locked_mods &= ~filter->action.mods.mask;
247
248     filter->func = NULL;
249
250     return 1;
251 }
252
253 static int
254 xkb_filter_mod_set_new(struct xkb_state *state, xkb_keycode_t keycode,
255                        union xkb_action *action)
256 {
257     struct xkb_filter *filter = xkb_filter_new(state);
258
259     if (!filter) /* WSGO */
260         return -1;
261     filter->keycode = keycode;
262     filter->func = xkb_filter_mod_set_func;
263     filter->action = *action;
264
265     filter->state->base_mods |= action->mods.mask;
266
267     return 1;
268 }
269
270 static int
271 xkb_filter_mod_lock_func(struct xkb_filter *filter, xkb_keycode_t keycode,
272                          enum xkb_key_direction direction)
273 {
274     if (keycode != filter->keycode)
275         return 1;
276
277     if (direction == XKB_KEY_DOWN) {
278         filter->refcnt++;
279         return 0;
280     }
281     if (--filter->refcnt > 0)
282         return 0;
283
284     filter->state->locked_mods &= ~filter->priv;
285     filter->func = NULL;
286     return 1;
287 }
288
289 static int
290 xkb_filter_mod_lock_new(struct xkb_state *state, xkb_keycode_t keycode,
291                         union xkb_action *action)
292 {
293     struct xkb_filter *filter = xkb_filter_new(state);
294
295     if (!filter) /* WSGO */
296         return 0;
297
298     filter->keycode = keycode;
299     filter->func = xkb_filter_mod_lock_func;
300     filter->action = *action;
301     filter->priv = state->locked_mods & action->mods.mask;
302     state->locked_mods |= action->mods.mask;
303
304     return 1;
305 }
306
307 enum xkb_key_latch_state {
308     NO_LATCH,
309     LATCH_KEY_DOWN,
310     LATCH_PENDING,
311 };
312
313 static int
314 xkb_filter_mod_latch_func(struct xkb_filter *filter, xkb_keycode_t keycode,
315                           enum xkb_key_direction direction)
316 {
317     enum xkb_key_latch_state latch = filter->priv;
318
319     if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
320         /* If this is a new keypress and we're awaiting our single latched
321          * keypress, then either break the latch if any random key is pressed,
322          * or promote it to a lock or plain base set if it's the same
323          * modifier. */
324         union xkb_action *action = xkb_key_get_action(filter->state, keycode);
325         if (action->type == XkbSA_LatchMods &&
326             action->mods.flags == filter->action.mods.flags &&
327             action->mods.mask == filter->action.mods.mask) {
328             filter->action = *action;
329             if (filter->action.mods.flags & XkbSA_LatchToLock) {
330                 filter->action.type = XkbSA_LockMods;
331                 filter->func = xkb_filter_mod_lock_func;
332                 filter->state->locked_mods |= filter->action.mods.mask;
333             }
334             else {
335                 filter->action.type = XkbSA_SetMods;
336                 filter->func = xkb_filter_mod_set_func;
337                 filter->state->base_mods |= filter->action.mods.mask;
338             }
339             filter->keycode = keycode;
340             filter->state->latched_mods &= ~filter->action.mods.mask;
341             /* XXX beep beep! */
342             return 0;
343         }
344         else if (((1 << action->type) & XkbSA_BreakLatch)) {
345             /* XXX: This may be totally broken, we might need to break the
346              *      latch in the next run after this press? */
347             filter->state->latched_mods &= ~filter->action.mods.mask;
348             filter->func = NULL;
349             return 1;
350         }
351     }
352     else if (direction == XKB_KEY_UP && keycode == filter->keycode) {
353         /* Our key got released.  If we've set it to clear locks, and we
354          * currently have the same modifiers locked, then release them and
355          * don't actually latch.  Else we've actually hit the latching
356          * stage, so set PENDING and move our modifier from base to
357          * latched. */
358         if (latch == NO_LATCH ||
359             ((filter->action.mods.flags & XkbSA_ClearLocks) &&
360              (filter->state->locked_mods & filter->action.mods.mask) ==
361              filter->action.mods.mask)) {
362             /* XXX: We might be a bit overenthusiastic about clearing
363              *      mods other filters have set here? */
364             if (latch == LATCH_PENDING)
365                 filter->state->latched_mods &= ~filter->action.mods.mask;
366             else
367                 filter->state->base_mods &= ~filter->action.mods.mask;
368             filter->state->locked_mods &= ~filter->action.mods.mask;
369             filter->func = NULL;
370         }
371         else {
372             latch = LATCH_PENDING;
373             filter->state->base_mods &= ~filter->action.mods.mask;
374             filter->state->latched_mods |= filter->action.mods.mask;
375             /* XXX beep beep! */
376         }
377     }
378     else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
379         /* Someone's pressed another key while we've still got the latching
380          * key held down, so keep the base modifier state active (from
381          * xkb_filter_mod_latch_new), but don't trip the latch, just clear
382          * it as soon as the modifier gets released. */
383         latch = NO_LATCH;
384     }
385
386     filter->priv = latch;
387
388     return 1;
389 }
390
391 static int
392 xkb_filter_mod_latch_new(struct xkb_state *state, xkb_keycode_t keycode,
393                          union xkb_action *action)
394 {
395     struct xkb_filter *filter = xkb_filter_new(state);
396     enum xkb_key_latch_state latch = LATCH_KEY_DOWN;
397
398     if (!filter) /* WSGO */
399         return -1;
400     filter->keycode = keycode;
401     filter->priv = latch;
402     filter->func = xkb_filter_mod_latch_func;
403     filter->action = *action;
404
405     filter->state->base_mods |= action->mods.mask;
406
407     return 1;
408 }
409
410 /**
411  * Applies any relevant filters to the key, first from the list of filters
412  * that are currently active, then if no filter has claimed the key, possibly
413  * apply a new filter from the key action.
414  */
415 static void
416 xkb_filter_apply_all(struct xkb_state *state, xkb_keycode_t key,
417                      enum xkb_key_direction direction)
418 {
419     struct xkb_filter *filters = state->filters;
420     union xkb_action *act = NULL;
421     int send = 1;
422     int i;
423
424     /* First run through all the currently active filters and see if any of
425      * them have claimed this event. */
426     for (i = 0; i < state->num_filters; i++) {
427         if (!filters[i].func)
428             continue;
429         send &= (*filters[i].func)(&filters[i], key, direction);
430     }
431
432     if (!send || direction == XKB_KEY_UP)
433         return;
434
435     act = xkb_key_get_action(state, key);
436     switch (act->type) {
437     case XkbSA_SetMods:
438         send = xkb_filter_mod_set_new(state, key, act);
439         break;
440     case XkbSA_LatchMods:
441         send = xkb_filter_mod_latch_new(state, key, act);
442         break;
443     case XkbSA_LockMods:
444         send = xkb_filter_mod_lock_new(state, key, act);
445         break;
446     case XkbSA_SetGroup:
447         send = xkb_filter_group_set_new(state, key, act);
448         break;
449 #if 0
450     case XkbSA_LatchGroup:
451         send = xkb_filter_mod_latch_new(state, key, act);
452         break;
453 #endif
454     case XkbSA_LockGroup:
455         send = xkb_filter_group_lock_new(state, key, act);
456         break;
457     }
458
459     return;
460 }
461
462 struct xkb_state *
463 xkb_state_new(struct xkb_desc *xkb)
464 {
465     struct xkb_state *ret;
466
467     if (!xkb)
468         return NULL;
469
470     ret = calloc(sizeof(*ret), 1);
471     if (!ret)
472         return NULL;
473
474     ret->refcnt = 1;
475     ret->xkb = xkb;
476     xkb_map_ref(xkb);
477
478     return ret;
479 }
480
481 void
482 xkb_state_unref(struct xkb_state *state)
483 {
484     state->refcnt--;
485     assert(state->refcnt >= 0);
486     if (state->refcnt > 0)
487         return;
488
489     xkb_map_unref(state->xkb);
490     free(state->filters);
491     free(state);
492 }
493
494 /**
495  * Update the LED state to match the rest of the xkb_state.
496  */
497 static void
498 xkb_state_led_update_all(struct xkb_state *state)
499 {
500     xkb_led_index_t led;
501
502     state->leds = 0;
503
504     for (led = 0; led < XkbNumIndicators; led++) {
505         struct xkb_indicator_map *map = &state->xkb->indicators->maps[led];
506         uint32_t mod_mask = 0;
507         uint32_t group_mask = 0;
508
509         if (!map->which_mods && !map->which_groups && !map->ctrls)
510             continue;
511
512         if (map->which_mods) {
513             if (map->which_mods & XkbIM_UseBase)
514                 mod_mask |= state->base_mods;
515             if (map->which_mods & XkbIM_UseLatched)
516                 mod_mask |= state->latched_mods;
517             if (map->which_mods & XkbIM_UseLocked)
518                 mod_mask |= state->locked_mods;
519             if (map->which_mods & XkbIM_UseEffective)
520                 mod_mask |= state->mods;
521             if ((map->mods.mask & mod_mask))
522                 state->leds |= (1 << led);
523         }
524         else if (map->which_groups) {
525             if (map->which_mods & XkbIM_UseBase)
526                 group_mask |= (1 << state->base_group);
527             if (map->which_mods & XkbIM_UseLatched)
528                 group_mask |= (1 << state->latched_group);
529             if (map->which_mods & XkbIM_UseLocked)
530                 group_mask |= (1 << state->locked_group);
531             if (map->which_mods & XkbIM_UseEffective)
532                 group_mask |= (1 << state->group);
533             if ((map->groups & group_mask))
534                 state->leds |= (1 << led);
535         }
536         else if (map->ctrls) {
537             if ((map->ctrls & state->xkb->ctrls->enabled_ctrls))
538                 state->leds |= (1 << led);
539         }
540     }
541 }
542
543 /**
544  * Calculates the derived state (effective mods/group and LEDs) from an
545  * up-to-date xkb_state.
546  */
547 static void
548 xkb_state_update_derived(struct xkb_state *state)
549 {
550     state->mods = (state->base_mods | state->latched_mods | state->locked_mods);
551     /* FIXME: Clamp/wrap locked_group */
552     state->group = state->locked_group + state->base_group +
553                    state->latched_group;
554     /* FIXME: Clamp/wrap effective group */
555
556     xkb_state_led_update_all(state);
557 }
558
559 /**
560  * Given a particular key event, updates the state structure to reflect the
561  * new modifiers.
562  */
563 void
564 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t key,
565                      enum xkb_key_direction direction)
566 {
567     xkb_filter_apply_all(state, key, direction);
568     xkb_state_update_derived(state);
569 }
570
571 /**
572  * Updates the state from a set of explicit masks as gained from
573  * xkb_state_serialise_mods and xkb_state_serialise_groups.  As noted in the
574  * documentation for these functions in xkbcommon.h, this round-trip is
575  * lossy, and should only be used to update a slave state mirroring the
576  * master, e.g. in a client/server window system.
577  */
578 void
579 xkb_state_update_mask(struct xkb_state *state,
580                       xkb_mod_mask_t base_mods,
581                       xkb_mod_mask_t latched_mods,
582                       xkb_mod_mask_t locked_mods,
583                       xkb_group_index_t base_group,
584                       xkb_group_index_t latched_group,
585                       xkb_group_index_t locked_group)
586 {
587     xkb_mod_mask_t mod;
588
589     state->base_mods = 0;
590     state->latched_mods = 0;
591     state->locked_mods = 0;
592     for (mod = 0; mod < xkb_map_num_mods(state->xkb); mod++) {
593         xkb_mod_mask_t idx = (1 << mod);
594         if (base_mods & idx)
595             state->base_mods |= idx;
596         if (latched_mods & idx)
597             state->latched_mods |= idx;
598         if (locked_mods & idx)
599             state->locked_mods |= idx;
600     }
601
602     state->base_group = base_group;
603     state->latched_group = latched_group;
604     state->locked_group = locked_group;
605
606     xkb_state_update_derived(state);
607 }
608
609 /**
610  * Serialises the requested modifier state into an xkb_mod_mask_t, with all
611  * the same disclaimers as in xkb_state_update_mask.
612  */
613 xkb_mod_mask_t
614 xkb_state_serialise_mods(struct xkb_state *state,
615                          enum xkb_state_component type)
616 {
617     xkb_mod_mask_t ret = 0;
618
619     if (type == XKB_STATE_EFFECTIVE)
620         return state->mods;
621
622     if (type & XKB_STATE_DEPRESSED)
623         ret |= state->base_mods;
624     if (type & XKB_STATE_LATCHED)
625         ret |= state->latched_mods;
626     if (type & XKB_STATE_LOCKED)
627         ret |= state->locked_mods;
628
629     return ret;
630 }
631
632 /**
633  * Serialises the requested group state, with all the same disclaimers as
634  * in xkb_state_update_mask.
635  */
636 xkb_group_index_t
637 xkb_state_serialise_group(struct xkb_state *state,
638                           enum xkb_state_component type)
639 {
640     xkb_group_index_t ret = 0;
641
642     if (type == XKB_STATE_EFFECTIVE)
643         return state->group;
644
645     if (type & XKB_STATE_DEPRESSED)
646         ret += state->base_group;
647     if (type & XKB_STATE_LATCHED)
648         ret += state->latched_group;
649     if (type & XKB_STATE_LOCKED)
650         ret += state->locked_group;
651
652     return ret;
653 }
654
655 /**
656  * Returns 1 if the given modifier is active with the specified type(s), 0 if
657  * not, or -1 if the modifier is invalid.
658  */
659 int
660 xkb_state_mod_index_is_active(struct xkb_state *state,
661                               xkb_mod_index_t idx,
662                               enum xkb_state_component type)
663 {
664     int ret = 0;
665
666     if (idx >= xkb_map_num_mods(state->xkb))
667         return -1;
668
669     if (type & XKB_STATE_DEPRESSED)
670         ret |= (state->base_mods & (1 << idx));
671     if (type & XKB_STATE_LATCHED)
672         ret |= (state->latched_mods & (1 << idx));
673     if (type & XKB_STATE_LOCKED)
674         ret |= (state->locked_mods & (1 << idx));
675
676     return ret;
677 }
678
679 /**
680  * Returns 1 if the given modifier is active with the specified type(s), 0 if
681  * not, or -1 if the modifier is invalid.
682  */
683 int
684 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
685                              enum xkb_state_component type)
686 {
687     xkb_mod_index_t idx = xkb_map_mod_get_index(state->xkb, name);
688
689     if (idx == XKB_MOD_INVALID)
690         return -1;
691
692     return xkb_state_mod_index_is_active(state, idx, type);
693 }
694
695 /**
696  * Returns 1 if the given group is active with the specified type(s), 0 if
697  * not, or -1 if the group is invalid.
698  */
699 int
700 xkb_state_group_index_is_active(struct xkb_state *state,
701                                 xkb_group_index_t idx,
702                                 enum xkb_state_component type)
703 {
704     int ret = 0;
705
706     if (idx >= xkb_map_num_groups(state->xkb))
707         return -1;
708
709     if (type & XKB_STATE_DEPRESSED)
710         ret |= (state->base_group == idx);
711     if (type & XKB_STATE_LATCHED)
712         ret |= (state->latched_group == idx);
713     if (type & XKB_STATE_LOCKED)
714         ret |= (state->locked_group == idx);
715
716     return ret;
717 }
718
719 /**
720  * Returns 1 if the given modifier is active with the specified type(s), 0 if
721  * not, or -1 if the modifier is invalid.
722  */
723 int
724 xkb_state_group_name_is_active(struct xkb_state *state, const char *name,
725                                enum xkb_state_component type)
726 {
727     xkb_group_index_t idx = xkb_map_group_get_index(state->xkb, name);
728
729     if (idx == XKB_GROUP_INVALID)
730         return -1;
731
732     return xkb_state_group_index_is_active(state, idx, type);
733 }
734
735 /**
736  * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
737  */
738 int
739 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
740 {
741     if (idx >= xkb_map_num_leds(state->xkb))
742         return -1;
743
744     return !!(state->leds & (1 << idx));
745 }
746
747 /**
748  * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
749  */
750 int
751 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
752 {
753     xkb_led_index_t idx = xkb_map_led_get_index(state->xkb, name);
754
755     if (idx == XKB_LED_INVALID)
756         return -1;
757
758     return xkb_state_led_index_is_active(state, idx);
759 }