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 #include "xkbcommon/xkbcommon.h"
57 #include "XKBcommonint.h"
62 * Returns the total number of modifiers active in the keymap.
65 xkb_map_num_mods(struct xkb_desc *xkb)
69 for (i = 0; i < XkbNumVirtualMods; i++)
70 if (!xkb->names->vmods[i])
73 /* We always have all the core modifiers (for now), plus any virtual
74 * modifiers we may have defined, and then shift to one-based indexing. */
75 return i + Mod5MapIndex + 1;
79 * Return the name for a given modifier.
82 xkb_map_mod_get_name(struct xkb_desc *xkb, xkb_mod_index_t idx)
84 if (idx >= xkb_map_num_mods(xkb))
87 /* First try to find a legacy modifier name. */
109 /* If that fails, try to find a virtual mod name. */
110 return xkb->names->vmods[idx - Mod5MapIndex];
114 * Returns the index for a named modifier.
117 xkb_map_mod_get_index(struct xkb_desc *xkb, const char *name)
121 if (strcasecmp(name, "Shift") == 0)
122 return ShiftMapIndex;
123 if (strcasecmp(name, "Control") == 0)
124 return ControlMapIndex;
125 if (strcasecmp(name, "Caps Lock") == 0)
127 if (strcasecmp(name, "Mod1") == 0)
129 if (strcasecmp(name, "Mod2") == 0)
131 if (strcasecmp(name, "Mod3") == 0)
133 if (strcasecmp(name, "Mod4") == 0)
135 if (strcasecmp(name, "Mod5") == 0)
138 for (i = 0; i < XkbNumVirtualMods && xkb->names->vmods[i]; i++) {
139 if (strcasecmp(name, xkb->names->vmods[i]) == 0)
140 return i + Mod5MapIndex;
143 return XKB_GROUP_INVALID;
147 * Return the total number of active groups in the keymap.
150 xkb_map_num_groups(struct xkb_desc *xkb)
152 xkb_group_index_t ret = 0;
155 for (i = 0; i < XkbNumKbdGroups; i++)
156 if (xkb->compat->groups[i].mask)
163 * Returns the name for a given group.
166 xkb_map_group_get_name(struct xkb_desc *xkb, xkb_group_index_t idx)
168 if (idx >= xkb_map_num_groups(xkb))
171 return xkb->names->groups[idx];
175 * Returns the index for a named group.
178 xkb_map_group_get_index(struct xkb_desc *xkb, const char *name)
180 xkb_group_index_t num_groups = xkb_map_num_groups(xkb);
183 for (i = 0; i < num_groups; i++) {
184 if (strcasecmp(xkb->names->groups[i], name) == 0)
188 return XKB_GROUP_INVALID;
192 * Returns the number of groups active for a particular key.
195 xkb_key_num_groups(struct xkb_desc *xkb, xkb_keycode_t key)
197 return XkbKeyNumGroups(xkb, key);
201 * Return the total number of active LEDs in the keymap.
204 xkb_map_num_leds(struct xkb_desc *xkb)
206 xkb_led_index_t ret = 0;
209 for (i = 0; i < XkbNumIndicators; i++)
210 if (xkb->indicators->maps[i].which_groups ||
211 xkb->indicators->maps[i].which_mods ||
212 xkb->indicators->maps[i].ctrls)
219 * Returns the name for a given group.
222 xkb_map_led_get_name(struct xkb_desc *xkb, xkb_led_index_t idx)
224 if (idx >= xkb_map_num_leds(xkb))
227 return xkb->names->indicators[idx];
231 * Returns the index for a named group.
234 xkb_map_led_get_index(struct xkb_desc *xkb, const char *name)
236 xkb_led_index_t num_leds = xkb_map_num_leds(xkb);
239 for (i = 0; i < num_leds; i++) {
240 if (strcasecmp(xkb->names->indicators[i], name) == 0)
244 return XKB_LED_INVALID;
248 * Returns the level to use for the given key and state, or -1 if invalid.
251 xkb_key_get_level(struct xkb_state *state, xkb_keycode_t key,
254 struct xkb_key_type *type = XkbKeyType(state->xkb, key, group);
255 unsigned int active_mods = state->mods & type->mods.mask;
258 for (i = 0; i < type->map_count; i++) {
259 if (!type->map[i].active)
261 if (type->map[i].mods.mask == active_mods)
262 return type->map[i].level;
269 * Returns the group to use for the given key and state, taking
270 * wrapping/clamping/etc into account.
273 xkb_key_get_group(struct xkb_state *state, xkb_keycode_t key)
275 unsigned int info = XkbKeyGroupInfo(state->xkb, key);
276 unsigned int num_groups = XkbKeyNumGroups(state->xkb, key);
277 int ret = state->group;
279 if (ret < XkbKeyNumGroups(state->xkb, key))
282 switch (XkbOutOfRangeGroupAction(info)) {
283 case XkbRedirectIntoRange:
284 ret = XkbOutOfRangeGroupInfo(info);
285 if (ret >= num_groups)
288 case XkbClampIntoRange:
289 ret = num_groups - 1;
291 case XkbWrapIntoRange:
301 * As below, but takes an explicit group/level rather than state.
304 xkb_key_get_syms_by_level(struct xkb_desc *xkb, xkb_keycode_t key, unsigned int group,
305 unsigned int level, xkb_keysym_t **syms_out)
307 *syms_out = &(XkbKeySymEntry(xkb, key, level, group));
308 if (**syms_out == NoSymbol)
319 * Provides the symbols to use for the given key and state. Returns the
320 * number of symbols pointed to in syms_out.
323 xkb_key_get_syms(struct xkb_state *state, xkb_keycode_t key,
324 xkb_keysym_t **syms_out)
326 struct xkb_desc *xkb = state->xkb;
330 if (!state || key < xkb->min_key_code || key > xkb->max_key_code)
333 group = xkb_key_get_group(state, key);
336 level = xkb_key_get_level(state, key, group);
340 return xkb_key_get_syms_by_level(xkb, key, group, level, syms_out);