2 * Copyright © 2012 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
23 * Author: Daniel Stone <daniel@fooishbar.org>
26 /************************************************************
27 * Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
29 * Permission to use, copy, modify, and distribute this
30 * software and its documentation for any purpose and without
31 * fee is hereby granted, provided that the above copyright
32 * notice appear in all copies and that both that copyright
33 * notice and this permission notice appear in supporting
34 * documentation, and that the name of Silicon Graphics not be
35 * used in advertising or publicity pertaining to distribution
36 * of the software without specific prior written permission.
37 * Silicon Graphics makes no representation about the suitability
38 * of this software for any purpose. It is provided "as is"
39 * without any express or implied warranty.
41 * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
42 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
43 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
44 * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
45 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
46 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
47 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
48 * THE USE OR PERFORMANCE OF THIS SOFTWARE.
50 * ********************************************************/
56 * Returns the total number of modifiers active in the keymap.
58 XKB_EXPORT xkb_mod_index_t
59 xkb_map_num_mods(struct xkb_keymap *keymap)
63 for (i = 0; i < XkbNumVirtualMods; i++)
64 if (!keymap->vmod_names[i])
67 /* We always have all the core modifiers (for now), plus any virtual
68 * modifiers we may have defined. */
69 return i + XkbNumModifiers;
73 * Return the name for a given modifier.
75 XKB_EXPORT const char *
76 xkb_map_mod_get_name(struct xkb_keymap *keymap, xkb_mod_index_t idx)
80 if (idx >= xkb_map_num_mods(keymap))
83 /* First try to find a legacy modifier name. If that fails, try to
84 * find a virtual mod name. */
85 name = ModIndexToName(idx);
87 name = keymap->vmod_names[idx - XkbNumModifiers];
93 * Returns the index for a named modifier.
95 XKB_EXPORT xkb_mod_index_t
96 xkb_map_mod_get_index(struct xkb_keymap *keymap, const char *name)
100 i = ModNameToIndex(name);
101 if (i != XKB_MOD_INVALID)
104 for (i = 0; i < XkbNumVirtualMods && keymap->vmod_names[i]; i++) {
105 if (istreq(name, keymap->vmod_names[i]))
106 return i + XkbNumModifiers;
109 return XKB_MOD_INVALID;
113 * Return the total number of active groups in the keymap.
115 XKB_EXPORT xkb_group_index_t
116 xkb_map_num_groups(struct xkb_keymap *keymap)
118 xkb_group_index_t ret = 0;
121 for (i = 0; i < XkbNumKbdGroups; i++)
122 if (keymap->groups[i].mask)
129 * Returns the name for a given group.
131 XKB_EXPORT const char *
132 xkb_map_group_get_name(struct xkb_keymap *keymap, xkb_group_index_t idx)
134 if (idx >= xkb_map_num_groups(keymap))
137 return keymap->group_names[idx];
141 * Returns the index for a named group.
143 XKB_EXPORT xkb_group_index_t
144 xkb_map_group_get_index(struct xkb_keymap *keymap, const char *name)
146 xkb_group_index_t num_groups = xkb_map_num_groups(keymap);
149 for (i = 0; i < num_groups; i++)
150 if (istreq(keymap->group_names[i], name))
153 return XKB_GROUP_INVALID;
157 * Returns the number of groups active for a particular key.
159 XKB_EXPORT xkb_group_index_t
160 xkb_key_num_groups(struct xkb_keymap *keymap, xkb_keycode_t kc)
162 if (XkbKeycodeInRange(keymap, kc))
163 return XkbKey(keymap, kc)->num_groups;
168 * Return the total number of active LEDs in the keymap.
170 XKB_EXPORT xkb_led_index_t
171 xkb_map_num_leds(struct xkb_keymap *keymap)
173 xkb_led_index_t ret = 0;
176 for (i = 0; i < XkbNumIndicators; i++)
177 if (keymap->indicators[i].which_groups ||
178 keymap->indicators[i].which_mods ||
179 keymap->indicators[i].ctrls)
186 * Returns the name for a given group.
188 XKB_EXPORT const char *
189 xkb_map_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx)
191 if (idx >= xkb_map_num_leds(keymap))
194 return keymap->indicator_names[idx];
198 * Returns the index for a named group.
200 XKB_EXPORT xkb_group_index_t
201 xkb_map_led_get_index(struct xkb_keymap *keymap, const char *name)
203 xkb_led_index_t num_leds = xkb_map_num_leds(keymap);
206 for (i = 0; i < num_leds; i++)
207 if (istreq(keymap->indicator_names[i], name))
210 return XKB_LED_INVALID;
214 * Returns the level to use for the given key and state, or -1 if invalid.
217 xkb_key_get_level(struct xkb_state *state, xkb_keycode_t kc,
218 xkb_group_index_t group)
220 struct xkb_keymap *keymap = xkb_state_get_map(state);
221 struct xkb_key_type *type;
222 struct xkb_kt_map_entry *entry;
223 xkb_mod_mask_t active_mods;
225 if (!XkbKeycodeInRange(keymap, kc))
228 type = XkbKeyType(keymap, XkbKey(keymap, kc), group);
229 active_mods = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
230 active_mods &= type->mods.mask;
232 darray_foreach(entry, type->map) {
233 if (entry->mods.mask == active_mods)
241 * Returns the group to use for the given key and state, taking
242 * wrapping/clamping/etc into account.
245 xkb_key_get_group(struct xkb_state *state, xkb_keycode_t kc)
247 struct xkb_keymap *keymap = xkb_state_get_map(state);
248 xkb_group_index_t ret = xkb_state_serialize_group(state,
249 XKB_STATE_EFFECTIVE);
252 if (!XkbKeycodeInRange(keymap, kc))
254 key = XkbKey(keymap, kc);
256 if (ret < key->num_groups)
259 switch (key->out_of_range_group_action) {
260 case XkbRedirectIntoRange:
261 ret = key->out_of_range_group_number;
262 if (ret >= key->num_groups)
266 case XkbClampIntoRange:
267 ret = key->num_groups - 1;
270 case XkbWrapIntoRange:
272 ret %= key->num_groups;
280 * As below, but takes an explicit group/level rather than state.
283 xkb_key_get_syms_by_level(struct xkb_keymap *keymap, struct xkb_key *key,
284 xkb_group_index_t group, xkb_level_index_t level,
285 const xkb_keysym_t **syms_out)
289 if (group >= key->num_groups)
291 if (level >= XkbKeyGroupWidth(keymap, key, group))
294 num_syms = XkbKeyNumSyms(key, group, level);
298 *syms_out = XkbKeySymEntry(key, group, level);
307 * Provides the symbols to use for the given key and state. Returns the
308 * number of symbols pointed to in syms_out.
311 xkb_key_get_syms(struct xkb_state *state, xkb_keycode_t kc,
312 const xkb_keysym_t **syms_out)
314 struct xkb_keymap *keymap = xkb_state_get_map(state);
316 xkb_group_index_t group;
317 xkb_level_index_t level;
319 if (!state || !XkbKeycodeInRange(keymap, kc))
322 key = XkbKey(keymap, kc);
324 group = xkb_key_get_group(state, kc);
328 level = xkb_key_get_level(state, kc, group);
332 return xkb_key_get_syms_by_level(keymap, key, group, level, syms_out);
340 * Simple boolean specifying whether or not the key should repeat.
343 xkb_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t kc)
345 if (!XkbKeycodeInRange(keymap, kc))
347 return XkbKey(keymap, kc)->repeats;