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 XKB_EXPORT xkb_mod_index_t
58 xkb_map_num_mods(struct xkb_keymap *keymap)
62 for (i = 0; i < XkbNumVirtualMods; i++)
63 if (!keymap->vmod_names[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 XKB_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->vmod_names[idx - Mod5MapIndex];
107 * Returns the index for a named modifier.
109 XKB_EXPORT xkb_mod_index_t
110 xkb_map_mod_get_index(struct xkb_keymap *keymap, const char *name)
114 if (istreq(name, "Shift"))
115 return ShiftMapIndex;
116 if (istreq(name, "Control"))
117 return ControlMapIndex;
118 if (istreq(name, "Caps Lock"))
120 if (istreq(name, "Mod1"))
122 if (istreq(name, "Mod2"))
124 if (istreq(name, "Mod3"))
126 if (istreq(name, "Mod4"))
128 if (istreq(name, "Mod5"))
131 for (i = 0; i < XkbNumVirtualMods && keymap->vmod_names[i]; i++) {
132 if (istreq(name, keymap->vmod_names[i]))
133 return i + Mod5MapIndex;
136 return XKB_GROUP_INVALID;
140 * Return the total number of active groups in the keymap.
142 XKB_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->groups[i].mask)
156 * Returns the name for a given group.
158 XKB_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->group_names[idx];
168 * Returns the index for a named group.
170 XKB_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 (istreq(keymap->group_names[i], name))
180 return XKB_GROUP_INVALID;
184 * Returns the number of groups active for a particular key.
186 XKB_EXPORT xkb_group_index_t
187 xkb_key_num_groups(struct xkb_keymap *keymap, xkb_keycode_t kc)
189 if (XkbKeycodeInRange(keymap, kc))
190 return XkbKey(keymap, kc)->num_groups;
195 * Return the total number of active LEDs in the keymap.
197 XKB_EXPORT xkb_led_index_t
198 xkb_map_num_leds(struct xkb_keymap *keymap)
200 xkb_led_index_t ret = 0;
203 for (i = 0; i < XkbNumIndicators; i++)
204 if (keymap->indicators[i].which_groups ||
205 keymap->indicators[i].which_mods ||
206 keymap->indicators[i].ctrls)
213 * Returns the name for a given group.
215 XKB_EXPORT const char *
216 xkb_map_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx)
218 if (idx >= xkb_map_num_leds(keymap))
221 return keymap->indicator_names[idx];
225 * Returns the index for a named group.
227 XKB_EXPORT xkb_group_index_t
228 xkb_map_led_get_index(struct xkb_keymap *keymap, const char *name)
230 xkb_led_index_t num_leds = xkb_map_num_leds(keymap);
233 for (i = 0; i < num_leds; i++)
234 if (istreq(keymap->indicator_names[i], name))
237 return XKB_LED_INVALID;
241 * Returns the level to use for the given key and state, or -1 if invalid.
244 xkb_key_get_level(struct xkb_state *state, xkb_keycode_t kc,
245 xkb_group_index_t group)
247 struct xkb_keymap *keymap = xkb_state_get_map(state);
248 struct xkb_key_type *type;
249 struct xkb_kt_map_entry *entry;
250 unsigned int active_mods;
252 if (!XkbKeycodeInRange(keymap, kc))
255 type = XkbKeyType(keymap, XkbKey(keymap, kc), group);
256 active_mods = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
257 active_mods &= type->mods.mask;
259 darray_foreach(entry, type->map) {
260 if (entry->mods.mask == active_mods)
268 * Returns the group to use for the given key and state, taking
269 * wrapping/clamping/etc into account.
272 xkb_key_get_group(struct xkb_state *state, xkb_keycode_t kc)
274 struct xkb_keymap *keymap = xkb_state_get_map(state);
275 xkb_group_index_t ret = xkb_state_serialize_group(state,
276 XKB_STATE_EFFECTIVE);
279 if (!XkbKeycodeInRange(keymap, kc))
281 key = XkbKey(keymap, kc);
283 if (ret < key->num_groups)
286 switch (key->out_of_range_group_action) {
287 case XkbRedirectIntoRange:
288 ret = key->out_of_range_group_number;
289 if (ret >= key->num_groups)
293 case XkbClampIntoRange:
294 ret = key->num_groups - 1;
297 case XkbWrapIntoRange:
299 ret %= key->num_groups;
307 * As below, but takes an explicit group/level rather than state.
310 xkb_key_get_syms_by_level(struct xkb_keymap *keymap, struct xkb_key *key,
311 xkb_group_index_t group, unsigned int level,
312 const xkb_keysym_t **syms_out)
316 if (group >= key->num_groups)
318 if (level >= XkbKeyGroupWidth(keymap, key, group))
321 num_syms = XkbKeyNumSyms(key, group, level);
325 *syms_out = XkbKeySymEntry(key, group, level);
334 * Provides the symbols to use for the given key and state. Returns the
335 * number of symbols pointed to in syms_out.
338 xkb_key_get_syms(struct xkb_state *state, xkb_keycode_t kc,
339 const xkb_keysym_t **syms_out)
341 struct xkb_keymap *keymap = xkb_state_get_map(state);
343 xkb_group_index_t group;
346 if (!state || !XkbKeycodeInRange(keymap, kc))
349 key = XkbKey(keymap, kc);
351 group = xkb_key_get_group(state, kc);
355 level = xkb_key_get_level(state, kc, group);
359 return xkb_key_get_syms_by_level(keymap, key, group, level, syms_out);
367 * Simple boolean specifying whether or not the key should repeat.
370 xkb_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t kc)
372 if (!XkbKeycodeInRange(keymap, kc))
374 return XkbKey(keymap, kc)->repeats;