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 "config.h"
63
64 #include "keymap.h"
65 #include "keysym.h"
66 #include "utf8.h"
67
68 struct xkb_filter {
69 union xkb_action action;
70 const struct xkb_key *key;
71 uint32_t priv;
72 bool (*func)(struct xkb_state *state,
73 struct xkb_filter *filter,
74 const struct xkb_key *key,
75 enum xkb_key_direction direction);
76 int refcnt;
77 };
78
79 struct state_components {
80 /* These may be negative, because of -1 group actions. */
81 int32_t base_group; /**< depressed */
82 int32_t latched_group;
83 int32_t locked_group;
84 xkb_layout_index_t group; /**< effective */
85
86 xkb_mod_mask_t base_mods; /**< depressed */
87 xkb_mod_mask_t latched_mods;
88 xkb_mod_mask_t locked_mods;
89 xkb_mod_mask_t mods; /**< effective */
90
91 xkb_led_mask_t leds;
92 };
93
94 struct xkb_state {
95 /*
96 * Before updating the state, we keep a copy of just this struct. This
97 * allows us to report which components of the state have changed.
98 */
99 struct state_components components;
100
101 /*
102 * At each event, we accumulate all the needed modifications to the base
103 * modifiers, and apply them at the end. These keep track of this state.
104 */
105 xkb_mod_mask_t set_mods;
106 xkb_mod_mask_t clear_mods;
107
108 /*
109 * We mustn't clear a base modifier if there's another depressed key
110 * which affects it, e.g. given this sequence
111 * < Left Shift down, Right Shift down, Left Shift Up >
112 * the modifier should still be set. This keeps the count.
113 */
114 int16_t mod_key_count[XKB_MAX_MODS];
115
116 int refcnt;
117 darray(struct xkb_filter) filters;
118 struct xkb_keymap *keymap;
119 };
120
121 static const struct xkb_key_type_entry *
get_entry_for_mods(const struct xkb_key_type * type,xkb_mod_mask_t mods)122 get_entry_for_mods(const struct xkb_key_type *type, xkb_mod_mask_t mods)
123 {
124 for (unsigned i = 0; i < type->num_entries; i++)
125 if (entry_is_active(&type->entries[i]) &&
126 type->entries[i].mods.mask == mods)
127 return &type->entries[i];
128 return NULL;
129 }
130
131 static const struct xkb_key_type_entry *
get_entry_for_key_state(struct xkb_state * state,const struct xkb_key * key,xkb_layout_index_t group)132 get_entry_for_key_state(struct xkb_state *state, const struct xkb_key *key,
133 xkb_layout_index_t group)
134 {
135 const struct xkb_key_type *type = key->groups[group].type;
136 xkb_mod_mask_t active_mods = state->components.mods & type->mods.mask;
137 return get_entry_for_mods(type, active_mods);
138 }
139
140 /**
141 * Returns the level to use for the given key and state, or
142 * XKB_LEVEL_INVALID.
143 */
144 XKB_EXPORT xkb_level_index_t
xkb_state_key_get_level(struct xkb_state * state,xkb_keycode_t kc,xkb_layout_index_t layout)145 xkb_state_key_get_level(struct xkb_state *state, xkb_keycode_t kc,
146 xkb_layout_index_t layout)
147 {
148 const struct xkb_key *key = XkbKey(state->keymap, kc);
149 const struct xkb_key_type_entry *entry;
150
151 if (!key || layout >= key->num_groups)
152 return XKB_LEVEL_INVALID;
153
154 /* If we don't find an explicit match the default is 0. */
155 entry = get_entry_for_key_state(state, key, layout);
156 if (!entry)
157 return 0;
158
159 return entry->level;
160 }
161
162 xkb_layout_index_t
XkbWrapGroupIntoRange(int32_t group,xkb_layout_index_t num_groups,enum xkb_range_exceed_type out_of_range_group_action,xkb_layout_index_t out_of_range_group_number)163 XkbWrapGroupIntoRange(int32_t group,
164 xkb_layout_index_t num_groups,
165 enum xkb_range_exceed_type out_of_range_group_action,
166 xkb_layout_index_t out_of_range_group_number)
167 {
168 if (num_groups == 0)
169 return XKB_LAYOUT_INVALID;
170
171 if (group >= 0 && (xkb_layout_index_t) group < num_groups)
172 return group;
173
174 switch (out_of_range_group_action) {
175 case RANGE_REDIRECT:
176 if (out_of_range_group_number >= num_groups)
177 return 0;
178 return out_of_range_group_number;
179
180 case RANGE_SATURATE:
181 if (group < 0)
182 return 0;
183 else
184 return num_groups - 1;
185
186 case RANGE_WRAP:
187 default:
188 /*
189 * C99 says a negative dividend in a modulo operation always
190 * gives a negative result.
191 */
192 if (group < 0)
193 return ((int) num_groups + (group % (int) num_groups));
194 else
195 return group % num_groups;
196 }
197 }
198
199 /**
200 * Returns the layout to use for the given key and state, taking
201 * wrapping/clamping/etc into account, or XKB_LAYOUT_INVALID.
202 */
203 XKB_EXPORT xkb_layout_index_t
xkb_state_key_get_layout(struct xkb_state * state,xkb_keycode_t kc)204 xkb_state_key_get_layout(struct xkb_state *state, xkb_keycode_t kc)
205 {
206 const struct xkb_key *key = XkbKey(state->keymap, kc);
207
208 if (!key)
209 return XKB_LAYOUT_INVALID;
210
211 return XkbWrapGroupIntoRange(state->components.group, key->num_groups,
212 key->out_of_range_group_action,
213 key->out_of_range_group_number);
214 }
215
216 static const union xkb_action *
xkb_key_get_action(struct xkb_state * state,const struct xkb_key * key)217 xkb_key_get_action(struct xkb_state *state, const struct xkb_key *key)
218 {
219 static const union xkb_action dummy = { .type = ACTION_TYPE_NONE };
220
221 xkb_layout_index_t layout;
222 xkb_level_index_t level;
223
224 layout = xkb_state_key_get_layout(state, key->keycode);
225 if (layout == XKB_LAYOUT_INVALID)
226 return &dummy;
227
228 level = xkb_state_key_get_level(state, key->keycode, layout);
229 if (level == XKB_LEVEL_INVALID)
230 return &dummy;
231
232 return &key->groups[layout].levels[level].action;
233 }
234
235 static struct xkb_filter *
xkb_filter_new(struct xkb_state * state)236 xkb_filter_new(struct xkb_state *state)
237 {
238 struct xkb_filter *filter = NULL, *iter;
239
240 darray_foreach(iter, state->filters) {
241 if (iter->func)
242 continue;
243 filter = iter;
244 break;
245 }
246
247 if (!filter) {
248 darray_resize0(state->filters, darray_size(state->filters) + 1);
249 filter = &darray_item(state->filters, darray_size(state->filters) -1);
250 }
251
252 filter->refcnt = 1;
253 return filter;
254 }
255
256 /***====================================================================***/
257
258 enum xkb_filter_result {
259 /*
260 * The event is consumed by the filters.
261 *
262 * An event is always processed by all filters, but any filter can
263 * prevent it from being processed further by consuming it.
264 */
265 XKB_FILTER_CONSUME,
266 /*
267 * The event may continue to be processed as far as this filter is
268 * concerned.
269 */
270 XKB_FILTER_CONTINUE,
271 };
272
273 static void
xkb_filter_group_set_new(struct xkb_state * state,struct xkb_filter * filter)274 xkb_filter_group_set_new(struct xkb_state *state, struct xkb_filter *filter)
275 {
276 filter->priv = state->components.base_group;
277 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
278 state->components.base_group = filter->action.group.group;
279 else
280 state->components.base_group += filter->action.group.group;
281 }
282
283 static bool
xkb_filter_group_set_func(struct xkb_state * state,struct xkb_filter * filter,const struct xkb_key * key,enum xkb_key_direction direction)284 xkb_filter_group_set_func(struct xkb_state *state,
285 struct xkb_filter *filter,
286 const struct xkb_key *key,
287 enum xkb_key_direction direction)
288 {
289 if (key != filter->key) {
290 filter->action.group.flags &= ~ACTION_LOCK_CLEAR;
291 return XKB_FILTER_CONTINUE;
292 }
293
294 if (direction == XKB_KEY_DOWN) {
295 filter->refcnt++;
296 return XKB_FILTER_CONSUME;
297 }
298 else if (--filter->refcnt > 0) {
299 return XKB_FILTER_CONSUME;
300 }
301
302 state->components.base_group = filter->priv;
303
304 if (filter->action.group.flags & ACTION_LOCK_CLEAR)
305 state->components.locked_group = 0;
306
307 filter->func = NULL;
308 return XKB_FILTER_CONTINUE;
309 }
310
311 static void
xkb_filter_group_lock_new(struct xkb_state * state,struct xkb_filter * filter)312 xkb_filter_group_lock_new(struct xkb_state *state, struct xkb_filter *filter)
313 {
314 if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
315 state->components.locked_group = filter->action.group.group;
316 else
317 state->components.locked_group += filter->action.group.group;
318 }
319
320 static bool
xkb_filter_group_lock_func(struct xkb_state * state,struct xkb_filter * filter,const struct xkb_key * key,enum xkb_key_direction direction)321 xkb_filter_group_lock_func(struct xkb_state *state,
322 struct xkb_filter *filter,
323 const struct xkb_key *key,
324 enum xkb_key_direction direction)
325 {
326 if (key != filter->key)
327 return XKB_FILTER_CONTINUE;
328
329 if (direction == XKB_KEY_DOWN) {
330 filter->refcnt++;
331 return XKB_FILTER_CONSUME;
332 }
333 if (--filter->refcnt > 0)
334 return XKB_FILTER_CONSUME;
335
336 filter->func = NULL;
337 return XKB_FILTER_CONTINUE;
338 }
339
340 static void
xkb_filter_mod_set_new(struct xkb_state * state,struct xkb_filter * filter)341 xkb_filter_mod_set_new(struct xkb_state *state, struct xkb_filter *filter)
342 {
343 state->set_mods = filter->action.mods.mods.mask;
344 }
345
346 static bool
xkb_filter_mod_set_func(struct xkb_state * state,struct xkb_filter * filter,const struct xkb_key * key,enum xkb_key_direction direction)347 xkb_filter_mod_set_func(struct xkb_state *state,
348 struct xkb_filter *filter,
349 const struct xkb_key *key,
350 enum xkb_key_direction direction)
351 {
352 if (key != filter->key) {
353 filter->action.mods.flags &= ~ACTION_LOCK_CLEAR;
354 return XKB_FILTER_CONTINUE;
355 }
356
357 if (direction == XKB_KEY_DOWN) {
358 filter->refcnt++;
359 return XKB_FILTER_CONSUME;
360 }
361 else if (--filter->refcnt > 0) {
362 return XKB_FILTER_CONSUME;
363 }
364
365 state->clear_mods = filter->action.mods.mods.mask;
366 if (filter->action.mods.flags & ACTION_LOCK_CLEAR)
367 state->components.locked_mods &= ~filter->action.mods.mods.mask;
368
369 filter->func = NULL;
370 return XKB_FILTER_CONTINUE;
371 }
372
373 static void
xkb_filter_mod_lock_new(struct xkb_state * state,struct xkb_filter * filter)374 xkb_filter_mod_lock_new(struct xkb_state *state, struct xkb_filter *filter)
375 {
376 filter->priv = (state->components.locked_mods &
377 filter->action.mods.mods.mask);
378 state->set_mods |= filter->action.mods.mods.mask;
379 if (!(filter->action.mods.flags & ACTION_LOCK_NO_LOCK))
380 state->components.locked_mods |= filter->action.mods.mods.mask;
381 }
382
383 static bool
xkb_filter_mod_lock_func(struct xkb_state * state,struct xkb_filter * filter,const struct xkb_key * key,enum xkb_key_direction direction)384 xkb_filter_mod_lock_func(struct xkb_state *state,
385 struct xkb_filter *filter,
386 const struct xkb_key *key,
387 enum xkb_key_direction direction)
388 {
389 if (key != filter->key)
390 return XKB_FILTER_CONTINUE;
391
392 if (direction == XKB_KEY_DOWN) {
393 filter->refcnt++;
394 return XKB_FILTER_CONSUME;
395 }
396 if (--filter->refcnt > 0)
397 return XKB_FILTER_CONSUME;
398
399 state->clear_mods |= filter->action.mods.mods.mask;
400 if (!(filter->action.mods.flags & ACTION_LOCK_NO_UNLOCK))
401 state->components.locked_mods &= ~filter->priv;
402
403 filter->func = NULL;
404 return XKB_FILTER_CONTINUE;
405 }
406
407 enum xkb_key_latch_state {
408 NO_LATCH,
409 LATCH_KEY_DOWN,
410 LATCH_PENDING,
411 };
412
413 static bool
xkb_action_breaks_latch(const union xkb_action * action)414 xkb_action_breaks_latch(const union xkb_action *action)
415 {
416 switch (action->type) {
417 case ACTION_TYPE_NONE:
418 case ACTION_TYPE_PTR_BUTTON:
419 case ACTION_TYPE_PTR_LOCK:
420 case ACTION_TYPE_CTRL_SET:
421 case ACTION_TYPE_CTRL_LOCK:
422 case ACTION_TYPE_SWITCH_VT:
423 case ACTION_TYPE_TERMINATE:
424 return true;
425 default:
426 return false;
427 }
428 }
429
430 static void
xkb_filter_mod_latch_new(struct xkb_state * state,struct xkb_filter * filter)431 xkb_filter_mod_latch_new(struct xkb_state *state, struct xkb_filter *filter)
432 {
433 filter->priv = LATCH_KEY_DOWN;
434 state->set_mods = filter->action.mods.mods.mask;
435 }
436
437 static bool
xkb_filter_mod_latch_func(struct xkb_state * state,struct xkb_filter * filter,const struct xkb_key * key,enum xkb_key_direction direction)438 xkb_filter_mod_latch_func(struct xkb_state *state,
439 struct xkb_filter *filter,
440 const struct xkb_key *key,
441 enum xkb_key_direction direction)
442 {
443 enum xkb_key_latch_state latch = filter->priv;
444
445 if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
446 /* If this is a new keypress and we're awaiting our single latched
447 * keypress, then either break the latch if any random key is pressed,
448 * or promote it to a lock or plain base set if it's the same
449 * modifier. */
450 const union xkb_action *action = xkb_key_get_action(state, key);
451 if (action->type == ACTION_TYPE_MOD_LATCH &&
452 action->mods.flags == filter->action.mods.flags &&
453 action->mods.mods.mask == filter->action.mods.mods.mask) {
454 filter->action = *action;
455 if (filter->action.mods.flags & ACTION_LATCH_TO_LOCK) {
456 filter->action.type = ACTION_TYPE_MOD_LOCK;
457 filter->func = xkb_filter_mod_lock_func;
458 state->components.locked_mods |= filter->action.mods.mods.mask;
459 }
460 else {
461 filter->action.type = ACTION_TYPE_MOD_SET;
462 filter->func = xkb_filter_mod_set_func;
463 state->set_mods = filter->action.mods.mods.mask;
464 }
465 filter->key = key;
466 state->components.latched_mods &= ~filter->action.mods.mods.mask;
467 /* XXX beep beep! */
468 return XKB_FILTER_CONSUME;
469 }
470 else if (xkb_action_breaks_latch(action)) {
471 /* XXX: This may be totally broken, we might need to break the
472 * latch in the next run after this press? */
473 state->components.latched_mods &= ~filter->action.mods.mods.mask;
474 filter->func = NULL;
475 return XKB_FILTER_CONTINUE;
476 }
477 }
478 else if (direction == XKB_KEY_UP && key == filter->key) {
479 /* Our key got released. If we've set it to clear locks, and we
480 * currently have the same modifiers locked, then release them and
481 * don't actually latch. Else we've actually hit the latching
482 * stage, so set PENDING and move our modifier from base to
483 * latched. */
484 if (latch == NO_LATCH ||
485 ((filter->action.mods.flags & ACTION_LOCK_CLEAR) &&
486 (state->components.locked_mods & filter->action.mods.mods.mask) ==
487 filter->action.mods.mods.mask)) {
488 /* XXX: We might be a bit overenthusiastic about clearing
489 * mods other filters have set here? */
490 if (latch == LATCH_PENDING)
491 state->components.latched_mods &=
492 ~filter->action.mods.mods.mask;
493 else
494 state->clear_mods = filter->action.mods.mods.mask;
495 state->components.locked_mods &= ~filter->action.mods.mods.mask;
496 filter->func = NULL;
497 }
498 else {
499 latch = LATCH_PENDING;
500 state->clear_mods = filter->action.mods.mods.mask;
501 state->components.latched_mods |= filter->action.mods.mods.mask;
502 /* XXX beep beep! */
503 }
504 }
505 else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
506 /* Someone's pressed another key while we've still got the latching
507 * key held down, so keep the base modifier state active (from
508 * xkb_filter_mod_latch_new), but don't trip the latch, just clear
509 * it as soon as the modifier gets released. */
510 latch = NO_LATCH;
511 }
512
513 filter->priv = latch;
514
515 return XKB_FILTER_CONTINUE;
516 }
517
518 static const struct {
519 void (*new)(struct xkb_state *state, struct xkb_filter *filter);
520 bool (*func)(struct xkb_state *state, struct xkb_filter *filter,
521 const struct xkb_key *key, enum xkb_key_direction direction);
522 } filter_action_funcs[_ACTION_TYPE_NUM_ENTRIES] = {
523 [ACTION_TYPE_MOD_SET] = { xkb_filter_mod_set_new,
524 xkb_filter_mod_set_func },
525 [ACTION_TYPE_MOD_LATCH] = { xkb_filter_mod_latch_new,
526 xkb_filter_mod_latch_func },
527 [ACTION_TYPE_MOD_LOCK] = { xkb_filter_mod_lock_new,
528 xkb_filter_mod_lock_func },
529 [ACTION_TYPE_GROUP_SET] = { xkb_filter_group_set_new,
530 xkb_filter_group_set_func },
531 [ACTION_TYPE_GROUP_LOCK] = { xkb_filter_group_lock_new,
532 xkb_filter_group_lock_func },
533 };
534
535 /**
536 * Applies any relevant filters to the key, first from the list of filters
537 * that are currently active, then if no filter has claimed the key, possibly
538 * apply a new filter from the key action.
539 */
540 static void
xkb_filter_apply_all(struct xkb_state * state,const struct xkb_key * key,enum xkb_key_direction direction)541 xkb_filter_apply_all(struct xkb_state *state,
542 const struct xkb_key *key,
543 enum xkb_key_direction direction)
544 {
545 struct xkb_filter *filter;
546 const union xkb_action *action;
547 bool consumed;
548
549 /* First run through all the currently active filters and see if any of
550 * them have consumed this event. */
551 consumed = false;
552 darray_foreach(filter, state->filters) {
553 if (!filter->func)
554 continue;
555
556 if (filter->func(state, filter, key, direction) == XKB_FILTER_CONSUME)
557 consumed = true;
558 }
559 if (consumed || direction == XKB_KEY_UP)
560 return;
561
562 action = xkb_key_get_action(state, key);
563
564 /*
565 * It's possible for the keymap to set action->type explicitly, like so:
566 * interpret XF86_Next_VMode {
567 * action = Private(type=0x86, data="+VMode");
568 * };
569 * We don't handle those.
570 */
571 if (action->type >= _ACTION_TYPE_NUM_ENTRIES)
572 return;
573
574 if (!filter_action_funcs[action->type].new)
575 return;
576
577 filter = xkb_filter_new(state);
578 filter->key = key;
579 filter->func = filter_action_funcs[action->type].func;
580 filter->action = *action;
581 filter_action_funcs[action->type].new(state, filter);
582 }
583
584 XKB_EXPORT struct xkb_state *
xkb_state_new(struct xkb_keymap * keymap)585 xkb_state_new(struct xkb_keymap *keymap)
586 {
587 struct xkb_state *ret;
588
589 ret = calloc(sizeof(*ret), 1);
590 if (!ret)
591 return NULL;
592
593 ret->refcnt = 1;
594 ret->keymap = xkb_keymap_ref(keymap);
595
596 return ret;
597 }
598
599 XKB_EXPORT struct xkb_state *
xkb_state_ref(struct xkb_state * state)600 xkb_state_ref(struct xkb_state *state)
601 {
602 state->refcnt++;
603 return state;
604 }
605
606 XKB_EXPORT void
xkb_state_unref(struct xkb_state * state)607 xkb_state_unref(struct xkb_state *state)
608 {
609 if (!state || --state->refcnt > 0)
610 return;
611
612 xkb_keymap_unref(state->keymap);
613 darray_free(state->filters);
614 free(state);
615 }
616
617 XKB_EXPORT struct xkb_keymap *
xkb_state_get_keymap(struct xkb_state * state)618 xkb_state_get_keymap(struct xkb_state *state)
619 {
620 return state->keymap;
621 }
622
623 /**
624 * Update the LED state to match the rest of the xkb_state.
625 */
626 static void
xkb_state_led_update_all(struct xkb_state * state)627 xkb_state_led_update_all(struct xkb_state *state)
628 {
629 xkb_led_index_t idx;
630 const struct xkb_led *led;
631
632 state->components.leds = 0;
633
634 xkb_leds_enumerate(idx, led, state->keymap) {
635 xkb_mod_mask_t mod_mask = 0;
636 xkb_layout_mask_t group_mask = 0;
637
638 if (led->which_mods != 0 && led->mods.mask != 0) {
639 if (led->which_mods & XKB_STATE_MODS_EFFECTIVE)
640 mod_mask |= state->components.mods;
641 if (led->which_mods & XKB_STATE_MODS_DEPRESSED)
642 mod_mask |= state->components.base_mods;
643 if (led->which_mods & XKB_STATE_MODS_LATCHED)
644 mod_mask |= state->components.latched_mods;
645 if (led->which_mods & XKB_STATE_MODS_LOCKED)
646 mod_mask |= state->components.locked_mods;
647
648 if (led->mods.mask & mod_mask) {
649 state->components.leds |= (1u << idx);
650 continue;
651 }
652 }
653
654 if (led->which_groups != 0 && led->groups != 0) {
655 if (led->which_groups & XKB_STATE_LAYOUT_EFFECTIVE)
656 group_mask |= (1u << state->components.group);
657 if (led->which_groups & XKB_STATE_LAYOUT_DEPRESSED)
658 group_mask |= (1u << state->components.base_group);
659 if (led->which_groups & XKB_STATE_LAYOUT_LATCHED)
660 group_mask |= (1u << state->components.latched_group);
661 if (led->which_groups & XKB_STATE_LAYOUT_LOCKED)
662 group_mask |= (1u << state->components.locked_group);
663
664 if (led->groups & group_mask) {
665 state->components.leds |= (1u << idx);
666 continue;
667 }
668 }
669
670 if (led->ctrls & state->keymap->enabled_ctrls) {
671 state->components.leds |= (1u << idx);
672 continue;
673 }
674 }
675 }
676
677 /**
678 * Calculates the derived state (effective mods/group and LEDs) from an
679 * up-to-date xkb_state.
680 */
681 static void
xkb_state_update_derived(struct xkb_state * state)682 xkb_state_update_derived(struct xkb_state *state)
683 {
684 xkb_layout_index_t wrapped;
685
686 state->components.mods = (state->components.base_mods |
687 state->components.latched_mods |
688 state->components.locked_mods);
689
690 /* TODO: Use groups_wrap control instead of always RANGE_WRAP. */
691
692 wrapped = XkbWrapGroupIntoRange(state->components.locked_group,
693 state->keymap->num_groups,
694 RANGE_WRAP, 0);
695 state->components.locked_group =
696 (wrapped == XKB_LAYOUT_INVALID ? 0 : wrapped);
697
698 wrapped = XkbWrapGroupIntoRange(state->components.base_group +
699 state->components.latched_group +
700 state->components.locked_group,
701 state->keymap->num_groups,
702 RANGE_WRAP, 0);
703 state->components.group =
704 (wrapped == XKB_LAYOUT_INVALID ? 0 : wrapped);
705
706 xkb_state_led_update_all(state);
707 }
708
709 static enum xkb_state_component
get_state_component_changes(const struct state_components * a,const struct state_components * b)710 get_state_component_changes(const struct state_components *a,
711 const struct state_components *b)
712 {
713 xkb_mod_mask_t mask = 0;
714
715 if (a->group != b->group)
716 mask |= XKB_STATE_LAYOUT_EFFECTIVE;
717 if (a->base_group != b->base_group)
718 mask |= XKB_STATE_LAYOUT_DEPRESSED;
719 if (a->latched_group != b->latched_group)
720 mask |= XKB_STATE_LAYOUT_LATCHED;
721 if (a->locked_group != b->locked_group)
722 mask |= XKB_STATE_LAYOUT_LOCKED;
723 if (a->mods != b->mods)
724 mask |= XKB_STATE_MODS_EFFECTIVE;
725 if (a->base_mods != b->base_mods)
726 mask |= XKB_STATE_MODS_DEPRESSED;
727 if (a->latched_mods != b->latched_mods)
728 mask |= XKB_STATE_MODS_LATCHED;
729 if (a->locked_mods != b->locked_mods)
730 mask |= XKB_STATE_MODS_LOCKED;
731 if (a->leds != b->leds)
732 mask |= XKB_STATE_LEDS;
733
734 return mask;
735 }
736
737 /**
738 * Given a particular key event, updates the state structure to reflect the
739 * new modifiers.
740 */
741 XKB_EXPORT enum xkb_state_component
xkb_state_update_key(struct xkb_state * state,xkb_keycode_t kc,enum xkb_key_direction direction)742 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
743 enum xkb_key_direction direction)
744 {
745 xkb_mod_index_t i;
746 xkb_mod_mask_t bit;
747 struct state_components prev_components;
748 const struct xkb_key *key = XkbKey(state->keymap, kc);
749
750 if (!key)
751 return 0;
752
753 prev_components = state->components;
754
755 state->set_mods = 0;
756 state->clear_mods = 0;
757
758 xkb_filter_apply_all(state, key, direction);
759
760 for (i = 0, bit = 1; state->set_mods; i++, bit <<= 1) {
761 if (state->set_mods & bit) {
762 state->mod_key_count[i]++;
763 state->components.base_mods |= bit;
764 state->set_mods &= ~bit;
765 }
766 }
767
768 for (i = 0, bit = 1; state->clear_mods; i++, bit <<= 1) {
769 if (state->clear_mods & bit) {
770 state->mod_key_count[i]--;
771 if (state->mod_key_count[i] <= 0) {
772 state->components.base_mods &= ~bit;
773 state->mod_key_count[i] = 0;
774 }
775 state->clear_mods &= ~bit;
776 }
777 }
778
779 xkb_state_update_derived(state);
780
781 return get_state_component_changes(&prev_components, &state->components);
782 }
783
784 /**
785 * Updates the state from a set of explicit masks as gained from
786 * xkb_state_serialize_mods and xkb_state_serialize_groups. As noted in the
787 * documentation for these functions in xkbcommon.h, this round-trip is
788 * lossy, and should only be used to update a slave state mirroring the
789 * master, e.g. in a client/server window system.
790 */
791 XKB_EXPORT enum xkb_state_component
xkb_state_update_mask(struct xkb_state * state,xkb_mod_mask_t base_mods,xkb_mod_mask_t latched_mods,xkb_mod_mask_t locked_mods,xkb_layout_index_t base_group,xkb_layout_index_t latched_group,xkb_layout_index_t locked_group)792 xkb_state_update_mask(struct xkb_state *state,
793 xkb_mod_mask_t base_mods,
794 xkb_mod_mask_t latched_mods,
795 xkb_mod_mask_t locked_mods,
796 xkb_layout_index_t base_group,
797 xkb_layout_index_t latched_group,
798 xkb_layout_index_t locked_group)
799 {
800 struct state_components prev_components;
801 xkb_mod_mask_t mask;
802
803 prev_components = state->components;
804
805 /* Only include modifiers which exist in the keymap. */
806 mask = (xkb_mod_mask_t) ((1ull << xkb_keymap_num_mods(state->keymap)) - 1u);
807
808 state->components.base_mods = base_mods & mask;
809 state->components.latched_mods = latched_mods & mask;
810 state->components.locked_mods = locked_mods & mask;
811
812 /* Make sure the mods are fully resolved - since we get arbitrary
813 * input, they might not be.
814 *
815 * It might seem more reasonable to do this only for components.mods
816 * in xkb_state_update_derived(), rather than for each component
817 * seperately. That would allow to distinguish between "really"
818 * depressed mods (would be in MODS_DEPRESSED) and indirectly
819 * depressed to to a mapping (would only be in MODS_EFFECTIVE).
820 * However, the traditional behavior of xkb_state_update_key() is that
821 * if a vmod is depressed, its mappings are depressed with it; so we're
822 * expected to do the same here. Also, LEDs (usually) look if a real
823 * mod is locked, not just effective; otherwise it won't be lit.
824 *
825 * We OR here because mod_mask_get_effective() drops vmods. */
826 state->components.base_mods |=
827 mod_mask_get_effective(state->keymap, state->components.base_mods);
828 state->components.latched_mods |=
829 mod_mask_get_effective(state->keymap, state->components.latched_mods);
830 state->components.locked_mods |=
831 mod_mask_get_effective(state->keymap, state->components.locked_mods);
832
833 state->components.base_group = base_group;
834 state->components.latched_group = latched_group;
835 state->components.locked_group = locked_group;
836
837 xkb_state_update_derived(state);
838
839 return get_state_component_changes(&prev_components, &state->components);
840 }
841
842 /**
843 * Provides the symbols to use for the given key and state. Returns the
844 * number of symbols pointed to in syms_out.
845 */
846 XKB_EXPORT int
xkb_state_key_get_syms(struct xkb_state * state,xkb_keycode_t kc,const xkb_keysym_t ** syms_out)847 xkb_state_key_get_syms(struct xkb_state *state, xkb_keycode_t kc,
848 const xkb_keysym_t **syms_out)
849 {
850 xkb_layout_index_t layout;
851 xkb_level_index_t level;
852
853 layout = xkb_state_key_get_layout(state, kc);
854 if (layout == XKB_LAYOUT_INVALID)
855 goto err;
856
857 level = xkb_state_key_get_level(state, kc, layout);
858 if (level == XKB_LEVEL_INVALID)
859 goto err;
860
861 return xkb_keymap_key_get_syms_by_level(state->keymap, kc, layout, level,
862 syms_out);
863
864 err:
865 *syms_out = NULL;
866 return 0;
867 }
868
869 /*
870 * https://www.x.org/releases/current/doc/kbproto/xkbproto.html#Interpreting_the_Lock_Modifier
871 */
872 static bool
should_do_caps_transformation(struct xkb_state * state,xkb_keycode_t kc)873 should_do_caps_transformation(struct xkb_state *state, xkb_keycode_t kc)
874 {
875 xkb_mod_index_t caps =
876 xkb_keymap_mod_get_index(state->keymap, XKB_MOD_NAME_CAPS);
877
878 return
879 xkb_state_mod_index_is_active(state, caps, XKB_STATE_MODS_EFFECTIVE) > 0 &&
880 xkb_state_mod_index_is_consumed(state, kc, caps) == 0;
881 }
882
883 /*
884 * https://www.x.org/releases/current/doc/kbproto/xkbproto.html#Interpreting_the_Control_Modifier
885 */
886 static bool
should_do_ctrl_transformation(struct xkb_state * state,xkb_keycode_t kc)887 should_do_ctrl_transformation(struct xkb_state *state, xkb_keycode_t kc)
888 {
889 xkb_mod_index_t ctrl =
890 xkb_keymap_mod_get_index(state->keymap, XKB_MOD_NAME_CTRL);
891
892 return
893 xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_MODS_EFFECTIVE) > 0 &&
894 xkb_state_mod_index_is_consumed(state, kc, ctrl) == 0;
895 }
896
897 /* Verbatim from libX11:src/xkb/XKBBind.c */
898 static char
XkbToControl(char ch)899 XkbToControl(char ch)
900 {
901 char c = ch;
902
903 if ((c >= '@' && c < '\177') || c == ' ')
904 c &= 0x1F;
905 else if (c == '2')
906 c = '\000';
907 else if (c >= '3' && c <= '7')
908 c -= ('3' - '\033');
909 else if (c == '8')
910 c = '\177';
911 else if (c == '/')
912 c = '_' & 0x1F;
913 return c;
914 }
915
916 /**
917 * Provides either exactly one symbol, or XKB_KEY_NoSymbol.
918 */
919 XKB_EXPORT xkb_keysym_t
xkb_state_key_get_one_sym(struct xkb_state * state,xkb_keycode_t kc)920 xkb_state_key_get_one_sym(struct xkb_state *state, xkb_keycode_t kc)
921 {
922 const xkb_keysym_t *syms;
923 xkb_keysym_t sym;
924 int num_syms;
925
926 num_syms = xkb_state_key_get_syms(state, kc, &syms);
927 if (num_syms != 1)
928 return XKB_KEY_NoSymbol;
929
930 sym = syms[0];
931
932 if (should_do_caps_transformation(state, kc))
933 sym = xkb_keysym_to_upper(sym);
934
935 return sym;
936 }
937
938 /*
939 * The caps and ctrl transformations require some special handling,
940 * so we cannot simply use xkb_state_get_one_sym() for them.
941 * In particular, if Control is set, we must try very hard to find
942 * some layout in which the keysym is ASCII and thus can be (maybe)
943 * converted to a control character. libX11 allows to disable this
944 * behavior with the XkbLC_ControlFallback (see XkbSetXlibControls(3)),
945 * but it is enabled by default, yippee.
946 */
947 static xkb_keysym_t
get_one_sym_for_string(struct xkb_state * state,xkb_keycode_t kc)948 get_one_sym_for_string(struct xkb_state *state, xkb_keycode_t kc)
949 {
950 xkb_level_index_t level;
951 xkb_layout_index_t layout, num_layouts;
952 const xkb_keysym_t *syms;
953 int nsyms;
954 xkb_keysym_t sym;
955
956 layout = xkb_state_key_get_layout(state, kc);
957 num_layouts = xkb_keymap_num_layouts_for_key(state->keymap, kc);
958 level = xkb_state_key_get_level(state, kc, layout);
959 if (layout == XKB_LAYOUT_INVALID || num_layouts == 0 ||
960 level == XKB_LEVEL_INVALID)
961 return XKB_KEY_NoSymbol;
962
963 nsyms = xkb_keymap_key_get_syms_by_level(state->keymap, kc,
964 layout, level, &syms);
965 if (nsyms != 1)
966 return XKB_KEY_NoSymbol;
967 sym = syms[0];
968
969 if (should_do_ctrl_transformation(state, kc) && sym > 127u) {
970 for (xkb_layout_index_t i = 0; i < num_layouts; i++) {
971 level = xkb_state_key_get_level(state, kc, i);
972 if (level == XKB_LEVEL_INVALID)
973 continue;
974
975 nsyms = xkb_keymap_key_get_syms_by_level(state->keymap, kc,
976 i, level, &syms);
977 if (nsyms == 1 && syms[0] <= 127u) {
978 sym = syms[0];
979 break;
980 }
981 }
982 }
983
984 if (should_do_caps_transformation(state, kc)) {
985 sym = xkb_keysym_to_upper(sym);
986 }
987
988 return sym;
989 }
990
991 XKB_EXPORT int
xkb_state_key_get_utf8(struct xkb_state * state,xkb_keycode_t kc,char * buffer,size_t size)992 xkb_state_key_get_utf8(struct xkb_state *state, xkb_keycode_t kc,
993 char *buffer, size_t size)
994 {
995 xkb_keysym_t sym;
996 const xkb_keysym_t *syms;
997 int nsyms;
998 int offset;
999 char tmp[7];
1000
1001 sym = get_one_sym_for_string(state, kc);
1002 if (sym != XKB_KEY_NoSymbol) {
1003 nsyms = 1; syms = &sym;
1004 }
1005 else {
1006 nsyms = xkb_state_key_get_syms(state, kc, &syms);
1007 }
1008
1009 /* Make sure not to truncate in the middle of a UTF-8 sequence. */
1010 offset = 0;
1011 for (int i = 0; i < nsyms; i++) {
1012 int ret = xkb_keysym_to_utf8(syms[i], tmp, sizeof(tmp));
1013 if (ret <= 0)
1014 goto err_bad;
1015
1016 ret--;
1017 if ((size_t) (offset + ret) <= size)
1018 memcpy(buffer + offset, tmp, ret);
1019 offset += ret;
1020 }
1021
1022 if ((size_t) offset >= size)
1023 goto err_trunc;
1024 buffer[offset] = '\0';
1025
1026 if (!is_valid_utf8(buffer, offset))
1027 goto err_bad;
1028
1029 if (offset == 1 && (unsigned int) buffer[0] <= 127u &&
1030 should_do_ctrl_transformation(state, kc))
1031 buffer[0] = XkbToControl(buffer[0]);
1032
1033 return offset;
1034
1035 err_trunc:
1036 if (size > 0)
1037 buffer[size - 1] = '\0';
1038 return offset;
1039
1040 err_bad:
1041 if (size > 0)
1042 buffer[0] = '\0';
1043 return 0;
1044 }
1045
1046 XKB_EXPORT uint32_t
xkb_state_key_get_utf32(struct xkb_state * state,xkb_keycode_t kc)1047 xkb_state_key_get_utf32(struct xkb_state *state, xkb_keycode_t kc)
1048 {
1049 xkb_keysym_t sym;
1050 uint32_t cp;
1051
1052 sym = get_one_sym_for_string(state, kc);
1053 cp = xkb_keysym_to_utf32(sym);
1054
1055 if (cp <= 127u && should_do_ctrl_transformation(state, kc))
1056 cp = (uint32_t) XkbToControl((char) cp);
1057
1058 return cp;
1059 }
1060
1061 /**
1062 * Serialises the requested modifier state into an xkb_mod_mask_t, with all
1063 * the same disclaimers as in xkb_state_update_mask.
1064 */
1065 XKB_EXPORT xkb_mod_mask_t
xkb_state_serialize_mods(struct xkb_state * state,enum xkb_state_component type)1066 xkb_state_serialize_mods(struct xkb_state *state,
1067 enum xkb_state_component type)
1068 {
1069 xkb_mod_mask_t ret = 0;
1070
1071 if (type & XKB_STATE_MODS_EFFECTIVE)
1072 return state->components.mods;
1073
1074 if (type & XKB_STATE_MODS_DEPRESSED)
1075 ret |= state->components.base_mods;
1076 if (type & XKB_STATE_MODS_LATCHED)
1077 ret |= state->components.latched_mods;
1078 if (type & XKB_STATE_MODS_LOCKED)
1079 ret |= state->components.locked_mods;
1080
1081 return ret;
1082 }
1083
1084 /**
1085 * Serialises the requested group state, with all the same disclaimers as
1086 * in xkb_state_update_mask.
1087 */
1088 XKB_EXPORT xkb_layout_index_t
xkb_state_serialize_layout(struct xkb_state * state,enum xkb_state_component type)1089 xkb_state_serialize_layout(struct xkb_state *state,
1090 enum xkb_state_component type)
1091 {
1092 xkb_layout_index_t ret = 0;
1093
1094 if (type & XKB_STATE_LAYOUT_EFFECTIVE)
1095 return state->components.group;
1096
1097 if (type & XKB_STATE_LAYOUT_DEPRESSED)
1098 ret += state->components.base_group;
1099 if (type & XKB_STATE_LAYOUT_LATCHED)
1100 ret += state->components.latched_group;
1101 if (type & XKB_STATE_LAYOUT_LOCKED)
1102 ret += state->components.locked_group;
1103
1104 return ret;
1105 }
1106
1107 /**
1108 * Gets a modifier mask and returns the resolved effective mask; this
1109 * is needed because some modifiers can also map to other modifiers, e.g.
1110 * the "NumLock" modifier usually also sets the "Mod2" modifier.
1111 */
1112 xkb_mod_mask_t
mod_mask_get_effective(struct xkb_keymap * keymap,xkb_mod_mask_t mods)1113 mod_mask_get_effective(struct xkb_keymap *keymap, xkb_mod_mask_t mods)
1114 {
1115 const struct xkb_mod *mod;
1116 xkb_mod_index_t i;
1117 xkb_mod_mask_t mask;
1118
1119 /* The effective mask is only real mods for now. */
1120 mask = mods & MOD_REAL_MASK_ALL;
1121
1122 xkb_mods_enumerate(i, mod, &keymap->mods)
1123 if (mods & (1u << i))
1124 mask |= mod->mapping;
1125
1126 return mask;
1127 }
1128
1129 /**
1130 * Returns 1 if the given modifier is active with the specified type(s), 0 if
1131 * not, or -1 if the modifier is invalid.
1132 */
1133 XKB_EXPORT int
xkb_state_mod_index_is_active(struct xkb_state * state,xkb_mod_index_t idx,enum xkb_state_component type)1134 xkb_state_mod_index_is_active(struct xkb_state *state,
1135 xkb_mod_index_t idx,
1136 enum xkb_state_component type)
1137 {
1138 if (idx >= xkb_keymap_num_mods(state->keymap))
1139 return -1;
1140
1141 return !!(xkb_state_serialize_mods(state, type) & (1u << idx));
1142 }
1143
1144 /**
1145 * Helper function for xkb_state_mod_indices_are_active and
1146 * xkb_state_mod_names_are_active.
1147 */
1148 static bool
match_mod_masks(struct xkb_state * state,enum xkb_state_component type,enum xkb_state_match match,xkb_mod_mask_t wanted)1149 match_mod_masks(struct xkb_state *state,
1150 enum xkb_state_component type,
1151 enum xkb_state_match match,
1152 xkb_mod_mask_t wanted)
1153 {
1154 xkb_mod_mask_t active = xkb_state_serialize_mods(state, type);
1155
1156 if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
1157 return false;
1158
1159 if (match & XKB_STATE_MATCH_ANY)
1160 return active & wanted;
1161
1162 return (active & wanted) == wanted;
1163 }
1164
1165 /**
1166 * Returns 1 if the modifiers are active with the specified type(s), 0 if
1167 * not, or -1 if any of the modifiers are invalid.
1168 */
1169 XKB_EXPORT int
xkb_state_mod_indices_are_active(struct xkb_state * state,enum xkb_state_component type,enum xkb_state_match match,...)1170 xkb_state_mod_indices_are_active(struct xkb_state *state,
1171 enum xkb_state_component type,
1172 enum xkb_state_match match,
1173 ...)
1174 {
1175 va_list ap;
1176 xkb_mod_mask_t wanted = 0;
1177 int ret = 0;
1178 xkb_mod_index_t num_mods = xkb_keymap_num_mods(state->keymap);
1179
1180 va_start(ap, match);
1181 while (1) {
1182 xkb_mod_index_t idx = va_arg(ap, xkb_mod_index_t);
1183 if (idx == XKB_MOD_INVALID)
1184 break;
1185 if (idx >= num_mods) {
1186 ret = -1;
1187 break;
1188 }
1189 wanted |= (1u << idx);
1190 }
1191 va_end(ap);
1192
1193 if (ret == -1)
1194 return ret;
1195
1196 return match_mod_masks(state, type, match, wanted);
1197 }
1198
1199 /**
1200 * Returns 1 if the given modifier is active with the specified type(s), 0 if
1201 * not, or -1 if the modifier is invalid.
1202 */
1203 XKB_EXPORT int
xkb_state_mod_name_is_active(struct xkb_state * state,const char * name,enum xkb_state_component type)1204 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
1205 enum xkb_state_component type)
1206 {
1207 xkb_mod_index_t idx = xkb_keymap_mod_get_index(state->keymap, name);
1208
1209 if (idx == XKB_MOD_INVALID)
1210 return -1;
1211
1212 return xkb_state_mod_index_is_active(state, idx, type);
1213 }
1214
1215 /**
1216 * Returns 1 if the modifiers are active with the specified type(s), 0 if
1217 * not, or -1 if any of the modifiers are invalid.
1218 */
1219 XKB_EXPORT ATTR_NULL_SENTINEL int
xkb_state_mod_names_are_active(struct xkb_state * state,enum xkb_state_component type,enum xkb_state_match match,...)1220 xkb_state_mod_names_are_active(struct xkb_state *state,
1221 enum xkb_state_component type,
1222 enum xkb_state_match match,
1223 ...)
1224 {
1225 va_list ap;
1226 xkb_mod_mask_t wanted = 0;
1227 int ret = 0;
1228
1229 va_start(ap, match);
1230 while (1) {
1231 xkb_mod_index_t idx;
1232 const char *str = va_arg(ap, const char *);
1233 if (str == NULL)
1234 break;
1235 idx = xkb_keymap_mod_get_index(state->keymap, str);
1236 if (idx == XKB_MOD_INVALID) {
1237 ret = -1;
1238 break;
1239 }
1240 wanted |= (1u << idx);
1241 }
1242 va_end(ap);
1243
1244 if (ret == -1)
1245 return ret;
1246
1247 return match_mod_masks(state, type, match, wanted);
1248 }
1249
1250 /**
1251 * Returns 1 if the given group is active with the specified type(s), 0 if
1252 * not, or -1 if the group is invalid.
1253 */
1254 XKB_EXPORT int
xkb_state_layout_index_is_active(struct xkb_state * state,xkb_layout_index_t idx,enum xkb_state_component type)1255 xkb_state_layout_index_is_active(struct xkb_state *state,
1256 xkb_layout_index_t idx,
1257 enum xkb_state_component type)
1258 {
1259 int ret = 0;
1260
1261 if (idx >= state->keymap->num_groups)
1262 return -1;
1263
1264 if (type & XKB_STATE_LAYOUT_EFFECTIVE)
1265 ret |= (state->components.group == idx);
1266 if (type & XKB_STATE_LAYOUT_DEPRESSED)
1267 ret |= (state->components.base_group == (int32_t) idx);
1268 if (type & XKB_STATE_LAYOUT_LATCHED)
1269 ret |= (state->components.latched_group == (int32_t) idx);
1270 if (type & XKB_STATE_LAYOUT_LOCKED)
1271 ret |= (state->components.locked_group == (int32_t) idx);
1272
1273 return ret;
1274 }
1275
1276 /**
1277 * Returns 1 if the given modifier is active with the specified type(s), 0 if
1278 * not, or -1 if the modifier is invalid.
1279 */
1280 XKB_EXPORT int
xkb_state_layout_name_is_active(struct xkb_state * state,const char * name,enum xkb_state_component type)1281 xkb_state_layout_name_is_active(struct xkb_state *state, const char *name,
1282 enum xkb_state_component type)
1283 {
1284 xkb_layout_index_t idx = xkb_keymap_layout_get_index(state->keymap, name);
1285
1286 if (idx == XKB_LAYOUT_INVALID)
1287 return -1;
1288
1289 return xkb_state_layout_index_is_active(state, idx, type);
1290 }
1291
1292 /**
1293 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
1294 */
1295 XKB_EXPORT int
xkb_state_led_index_is_active(struct xkb_state * state,xkb_led_index_t idx)1296 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
1297 {
1298 if (idx >= state->keymap->num_leds ||
1299 state->keymap->leds[idx].name == XKB_ATOM_NONE)
1300 return -1;
1301
1302 return !!(state->components.leds & (1u << idx));
1303 }
1304
1305 /**
1306 * Returns 1 if the given LED is active, 0 if not, or -1 if the LED is invalid.
1307 */
1308 XKB_EXPORT int
xkb_state_led_name_is_active(struct xkb_state * state,const char * name)1309 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
1310 {
1311 xkb_led_index_t idx = xkb_keymap_led_get_index(state->keymap, name);
1312
1313 if (idx == XKB_LED_INVALID)
1314 return -1;
1315
1316 return xkb_state_led_index_is_active(state, idx);
1317 }
1318
1319 /**
1320 * See:
1321 * - XkbTranslateKeyCode(3), mod_rtrn return value, from libX11.
1322 * - MyEnhancedXkbTranslateKeyCode(), a modification of the above, from GTK+.
1323 */
1324 static xkb_mod_mask_t
key_get_consumed(struct xkb_state * state,const struct xkb_key * key,enum xkb_consumed_mode mode)1325 key_get_consumed(struct xkb_state *state, const struct xkb_key *key,
1326 enum xkb_consumed_mode mode)
1327 {
1328 const struct xkb_key_type *type;
1329 const struct xkb_key_type_entry *matching_entry;
1330 xkb_mod_mask_t preserve = 0;
1331 xkb_layout_index_t group;
1332 xkb_mod_mask_t consumed = 0;
1333
1334 group = xkb_state_key_get_layout(state, key->keycode);
1335 if (group == XKB_LAYOUT_INVALID)
1336 return 0;
1337
1338 type = key->groups[group].type;
1339
1340 matching_entry = get_entry_for_key_state(state, key, group);
1341 if (matching_entry)
1342 preserve = matching_entry->preserve.mask;
1343
1344 switch (mode) {
1345 case XKB_CONSUMED_MODE_XKB:
1346 consumed = type->mods.mask;
1347 break;
1348
1349 case XKB_CONSUMED_MODE_GTK: {
1350 const struct xkb_key_type_entry *no_mods_entry;
1351 xkb_level_index_t no_mods_leveli;
1352 const struct xkb_level *no_mods_level, *level;
1353
1354 no_mods_entry = get_entry_for_mods(type, 0);
1355 no_mods_leveli = no_mods_entry ? no_mods_entry->level : 0;
1356 no_mods_level = &key->groups[group].levels[no_mods_leveli];
1357
1358 for (unsigned i = 0; i < type->num_entries; i++) {
1359 const struct xkb_key_type_entry *entry = &type->entries[i];
1360 if (!entry_is_active(entry))
1361 continue;
1362
1363 level = &key->groups[group].levels[entry->level];
1364 if (XkbLevelsSameSyms(level, no_mods_level))
1365 continue;
1366
1367 if (entry == matching_entry || one_bit_set(entry->mods.mask))
1368 consumed |= entry->mods.mask & ~entry->preserve.mask;
1369 }
1370 break;
1371 }
1372 }
1373
1374 return consumed & ~preserve;
1375 }
1376
1377 XKB_EXPORT int
xkb_state_mod_index_is_consumed2(struct xkb_state * state,xkb_keycode_t kc,xkb_mod_index_t idx,enum xkb_consumed_mode mode)1378 xkb_state_mod_index_is_consumed2(struct xkb_state *state, xkb_keycode_t kc,
1379 xkb_mod_index_t idx,
1380 enum xkb_consumed_mode mode)
1381 {
1382 const struct xkb_key *key = XkbKey(state->keymap, kc);
1383
1384 if (!key || idx >= xkb_keymap_num_mods(state->keymap))
1385 return -1;
1386
1387 return !!((1u << idx) & key_get_consumed(state, key, mode));
1388 }
1389
1390 XKB_EXPORT int
xkb_state_mod_index_is_consumed(struct xkb_state * state,xkb_keycode_t kc,xkb_mod_index_t idx)1391 xkb_state_mod_index_is_consumed(struct xkb_state *state, xkb_keycode_t kc,
1392 xkb_mod_index_t idx)
1393 {
1394 return xkb_state_mod_index_is_consumed2(state, kc, idx,
1395 XKB_CONSUMED_MODE_XKB);
1396 }
1397
1398 XKB_EXPORT xkb_mod_mask_t
xkb_state_mod_mask_remove_consumed(struct xkb_state * state,xkb_keycode_t kc,xkb_mod_mask_t mask)1399 xkb_state_mod_mask_remove_consumed(struct xkb_state *state, xkb_keycode_t kc,
1400 xkb_mod_mask_t mask)
1401 {
1402 const struct xkb_key *key = XkbKey(state->keymap, kc);
1403
1404 if (!key)
1405 return 0;
1406
1407 return mask & ~key_get_consumed(state, key, XKB_CONSUMED_MODE_XKB);
1408 }
1409
1410 XKB_EXPORT xkb_mod_mask_t
xkb_state_key_get_consumed_mods2(struct xkb_state * state,xkb_keycode_t kc,enum xkb_consumed_mode mode)1411 xkb_state_key_get_consumed_mods2(struct xkb_state *state, xkb_keycode_t kc,
1412 enum xkb_consumed_mode mode)
1413 {
1414 const struct xkb_key *key;
1415
1416 switch (mode) {
1417 case XKB_CONSUMED_MODE_XKB:
1418 case XKB_CONSUMED_MODE_GTK:
1419 break;
1420 default:
1421 log_err_func(state->keymap->ctx,
1422 "unrecognized consumed modifiers mode: %d\n", mode);
1423 return 0;
1424 }
1425
1426 key = XkbKey(state->keymap, kc);
1427 if (!key)
1428 return 0;
1429
1430 return key_get_consumed(state, key, mode);
1431 }
1432
1433 XKB_EXPORT xkb_mod_mask_t
xkb_state_key_get_consumed_mods(struct xkb_state * state,xkb_keycode_t kc)1434 xkb_state_key_get_consumed_mods(struct xkb_state *state, xkb_keycode_t kc)
1435 {
1436 return xkb_state_key_get_consumed_mods2(state, kc, XKB_CONSUMED_MODE_XKB);
1437 }
1438