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 * ********************************************************/
55 * Returns the total number of modifiers active in the keymap.
57 _X_EXPORT xkb_mod_index_t
58 xkb_map_num_mods(struct xkb_keymap *keymap)
62 for (i = 0; i < XkbNumVirtualMods; i++)
63 if (!keymap->names->vmods[i])
66 /* We always have all the core modifiers (for now), plus any virtual
67 * modifiers we may have defined, and then shift to one-based indexing. */
68 return i + Mod5MapIndex + 1;
72 * Return the name for a given modifier.
74 _X_EXPORT const char *
75 xkb_map_mod_get_name(struct xkb_keymap *keymap, xkb_mod_index_t idx)
77 if (idx >= xkb_map_num_mods(keymap))
80 /* First try to find a legacy modifier name. */
102 /* If that fails, try to find a virtual mod name. */
103 return keymap->names->vmods[idx - Mod5MapIndex];
107 * Returns the index for a named modifier.
109 _X_EXPORT xkb_mod_index_t
110 xkb_map_mod_get_index(struct xkb_keymap *keymap, const char *name)
114 if (strcasecmp(name, "Shift") == 0)
115 return ShiftMapIndex;
116 if (strcasecmp(name, "Control") == 0)
117 return ControlMapIndex;
118 if (strcasecmp(name, "Caps Lock") == 0)
120 if (strcasecmp(name, "Mod1") == 0)
122 if (strcasecmp(name, "Mod2") == 0)
124 if (strcasecmp(name, "Mod3") == 0)
126 if (strcasecmp(name, "Mod4") == 0)
128 if (strcasecmp(name, "Mod5") == 0)
131 for (i = 0; i < XkbNumVirtualMods && keymap->names->vmods[i]; i++) {
132 if (strcasecmp(name, keymap->names->vmods[i]) == 0)
133 return i + Mod5MapIndex;
136 return XKB_GROUP_INVALID;
140 * Return the total number of active groups in the keymap.
142 _X_EXPORT xkb_group_index_t
143 xkb_map_num_groups(struct xkb_keymap *keymap)
145 xkb_group_index_t ret = 0;
148 for (i = 0; i < XkbNumKbdGroups; i++)
149 if (keymap->compat->groups[i].mask)
156 * Returns the name for a given group.
158 _X_EXPORT const char *
159 xkb_map_group_get_name(struct xkb_keymap *keymap, xkb_group_index_t idx)
161 if (idx >= xkb_map_num_groups(keymap))
164 return keymap->names->groups[idx];
168 * Returns the index for a named group.
170 _X_EXPORT xkb_group_index_t
171 xkb_map_group_get_index(struct xkb_keymap *keymap, const char *name)
173 xkb_group_index_t num_groups = xkb_map_num_groups(keymap);
176 for (i = 0; i < num_groups; i++) {
177 if (strcasecmp(keymap->names->groups[i], name) == 0)
181 return XKB_GROUP_INVALID;
185 * Returns the number of groups active for a particular key.
187 _X_EXPORT xkb_group_index_t
188 xkb_key_num_groups(struct xkb_keymap *keymap, xkb_keycode_t key)
190 return XkbKeyNumGroups(keymap, key);
194 * Return the total number of active LEDs in the keymap.
196 _X_EXPORT xkb_led_index_t
197 xkb_map_num_leds(struct xkb_keymap *keymap)
199 xkb_led_index_t ret = 0;
202 for (i = 0; i < XkbNumIndicators; i++)
203 if (keymap->indicators->maps[i].which_groups ||
204 keymap->indicators->maps[i].which_mods ||
205 keymap->indicators->maps[i].ctrls)
212 * Returns the name for a given group.
214 _X_EXPORT const char *
215 xkb_map_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx)
217 if (idx >= xkb_map_num_leds(keymap))
220 return keymap->names->indicators[idx];
224 * Returns the index for a named group.
226 _X_EXPORT xkb_group_index_t
227 xkb_map_led_get_index(struct xkb_keymap *keymap, const char *name)
229 xkb_led_index_t num_leds = xkb_map_num_leds(keymap);
232 for (i = 0; i < num_leds; i++) {
233 if (strcasecmp(keymap->names->indicators[i], name) == 0)
237 return XKB_LED_INVALID;
241 * Returns the level to use for the given key and state, or -1 if invalid.
243 _X_EXPORT unsigned int
244 xkb_key_get_level(struct xkb_state *state, xkb_keycode_t key,
247 struct xkb_keymap *keymap = xkb_state_get_map(state);
248 struct xkb_key_type *type = XkbKeyType(keymap, key, group);
249 struct xkb_kt_map_entry *entry;
250 unsigned int active_mods;
252 active_mods = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
253 active_mods &= type->mods.mask;
255 darray_foreach(entry, type->map) {
256 if (entry->mods.mask == active_mods)
264 * Returns the group to use for the given key and state, taking
265 * wrapping/clamping/etc into account.
267 _X_EXPORT unsigned int
268 xkb_key_get_group(struct xkb_state *state, xkb_keycode_t key)
270 struct xkb_keymap *keymap = xkb_state_get_map(state);
271 unsigned int info = XkbKeyGroupInfo(keymap, key);
272 unsigned int num_groups = XkbKeyNumGroups(keymap, key);
273 unsigned int ret = xkb_state_serialize_group(state, XKB_STATE_EFFECTIVE);
275 if (ret < XkbKeyNumGroups(keymap, key))
278 switch (XkbOutOfRangeGroupAction(info)) {
279 case XkbRedirectIntoRange:
280 ret = XkbOutOfRangeGroupInfo(info);
281 if (ret >= num_groups)
284 case XkbClampIntoRange:
285 ret = num_groups - 1;
287 case XkbWrapIntoRange:
297 * As below, but takes an explicit group/level rather than state.
300 xkb_key_get_syms_by_level(struct xkb_keymap *keymap, xkb_keycode_t key,
301 unsigned int group, unsigned int level,
302 const xkb_keysym_t **syms_out)
306 if (group >= XkbKeyNumGroups(keymap, key))
308 if (level >= XkbKeyGroupWidth(keymap, key, group))
311 num_syms = XkbKeyNumSyms(keymap, key, group, level);
315 *syms_out = XkbKeySymEntry(keymap, key, group, level);
324 * Provides the symbols to use for the given key and state. Returns the
325 * number of symbols pointed to in syms_out.
328 xkb_key_get_syms(struct xkb_state *state, xkb_keycode_t key,
329 const xkb_keysym_t **syms_out)
331 struct xkb_keymap *keymap = xkb_state_get_map(state);
335 if (!state || key < keymap->min_key_code || key > keymap->max_key_code)
338 group = xkb_key_get_group(state, key);
341 level = xkb_key_get_level(state, key, group);
345 return xkb_key_get_syms_by_level(keymap, key, group, level, syms_out);
353 * Simple boolean specifying whether or not the key should repeat.
356 xkb_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t key)
358 return !!(keymap->ctrls->per_key_repeat[key / 8] & (1 << (key % 8)));