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 xkb_map_new(struct xkb_context *ctx)
58 struct xkb_keymap *keymap;
60 keymap = calloc(1, sizeof(*keymap));
65 keymap->ctx = xkb_context_ref(ctx);
70 XKB_EXPORT struct xkb_keymap *
71 xkb_map_ref(struct xkb_keymap *keymap)
78 xkb_map_unref(struct xkb_keymap *keymap)
83 if (!keymap || --keymap->refcnt > 0)
86 for (i = 0; i < keymap->num_types; i++) {
87 free(keymap->types[i].map);
88 free(keymap->types[i].level_names);
91 darray_foreach(key, keymap->keys) {
94 darray_free(key->syms);
97 darray_free(keymap->keys);
98 darray_free(keymap->sym_interpret);
99 darray_free(keymap->key_aliases);
100 free(keymap->keycodes_section_name);
101 free(keymap->symbols_section_name);
102 free(keymap->types_section_name);
103 free(keymap->compat_section_name);
104 xkb_context_unref(keymap->ctx);
109 * Returns the total number of modifiers active in the keymap.
111 XKB_EXPORT xkb_mod_index_t
112 xkb_map_num_mods(struct xkb_keymap *keymap)
116 for (i = 0; i < XkbNumVirtualMods; i++)
117 if (!keymap->vmod_names[i])
120 /* We always have all the core modifiers (for now), plus any virtual
121 * modifiers we may have defined. */
122 return i + XkbNumModifiers;
126 * Return the name for a given modifier.
128 XKB_EXPORT const char *
129 xkb_map_mod_get_name(struct xkb_keymap *keymap, xkb_mod_index_t idx)
133 if (idx >= xkb_map_num_mods(keymap))
136 /* First try to find a legacy modifier name. If that fails, try to
137 * find a virtual mod name. */
138 name = ModIndexToName(idx);
140 name = keymap->vmod_names[idx - XkbNumModifiers];
146 * Returns the index for a named modifier.
148 XKB_EXPORT xkb_mod_index_t
149 xkb_map_mod_get_index(struct xkb_keymap *keymap, const char *name)
153 i = ModNameToIndex(name);
154 if (i != XKB_MOD_INVALID)
157 for (i = 0; i < XkbNumVirtualMods && keymap->vmod_names[i]; i++) {
158 if (istreq(name, keymap->vmod_names[i]))
159 return i + XkbNumModifiers;
162 return XKB_MOD_INVALID;
166 * Return the total number of active groups in the keymap.
168 XKB_EXPORT xkb_group_index_t
169 xkb_map_num_groups(struct xkb_keymap *keymap)
171 xkb_group_index_t ret = 0;
174 for (i = 0; i < XkbNumKbdGroups; i++)
175 if (keymap->groups[i].mask)
182 * Returns the name for a given group.
184 XKB_EXPORT const char *
185 xkb_map_group_get_name(struct xkb_keymap *keymap, xkb_group_index_t idx)
187 if (idx >= xkb_map_num_groups(keymap))
190 return keymap->group_names[idx];
194 * Returns the index for a named group.
196 XKB_EXPORT xkb_group_index_t
197 xkb_map_group_get_index(struct xkb_keymap *keymap, const char *name)
199 xkb_group_index_t num_groups = xkb_map_num_groups(keymap);
202 for (i = 0; i < num_groups; i++)
203 if (istreq(keymap->group_names[i], name))
206 return XKB_GROUP_INVALID;
210 * Returns the number of groups active for a particular key.
212 XKB_EXPORT xkb_group_index_t
213 xkb_key_num_groups(struct xkb_keymap *keymap, xkb_keycode_t kc)
215 if (XkbKeycodeInRange(keymap, kc))
216 return XkbKey(keymap, kc)->num_groups;
221 * Return the total number of active LEDs in the keymap.
223 XKB_EXPORT xkb_led_index_t
224 xkb_map_num_leds(struct xkb_keymap *keymap)
226 xkb_led_index_t ret = 0;
229 for (i = 0; i < XkbNumIndicators; i++)
230 if (keymap->indicators[i].which_groups ||
231 keymap->indicators[i].which_mods ||
232 keymap->indicators[i].ctrls)
239 * Returns the name for a given group.
241 XKB_EXPORT const char *
242 xkb_map_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx)
244 if (idx >= xkb_map_num_leds(keymap))
247 return keymap->indicator_names[idx];
251 * Returns the index for a named group.
253 XKB_EXPORT xkb_group_index_t
254 xkb_map_led_get_index(struct xkb_keymap *keymap, const char *name)
256 xkb_led_index_t num_leds = xkb_map_num_leds(keymap);
259 for (i = 0; i < num_leds; i++)
260 if (istreq(keymap->indicator_names[i], name))
263 return XKB_LED_INVALID;
266 static struct xkb_kt_map_entry *
267 get_entry_for_key_state(struct xkb_state *state, xkb_keycode_t kc)
269 struct xkb_keymap *keymap = xkb_state_get_map(state);
270 xkb_group_index_t group;
271 struct xkb_key_type *type;
272 xkb_mod_mask_t active_mods;
275 group = xkb_key_get_group(state, kc);
276 if (group == XKB_GROUP_INVALID)
279 type = XkbKeyType(keymap, XkbKey(keymap, kc), group);
280 active_mods = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
281 active_mods &= type->mods.mask;
283 for (i = 0; i < type->num_entries; i++)
284 if (type->map[i].mods.mask == active_mods)
285 return &type->map[i];
291 * Returns the level to use for the given key and state, or
295 xkb_key_get_level(struct xkb_state *state, xkb_keycode_t kc,
296 xkb_group_index_t group)
298 struct xkb_kt_map_entry *entry;
300 if (!XkbKeycodeInRange(xkb_state_get_map(state), kc))
301 return XKB_LEVEL_INVALID;
303 entry = get_entry_for_key_state(state, kc);
305 /* If we don't find an explicit match the default is 0. */
313 * Returns the group to use for the given key and state, taking
314 * wrapping/clamping/etc into account, or XKB_GROUP_INVALID.
317 xkb_key_get_group(struct xkb_state *state, xkb_keycode_t kc)
319 struct xkb_keymap *keymap = xkb_state_get_map(state);
320 xkb_group_index_t ret = xkb_state_serialize_group(state,
321 XKB_STATE_EFFECTIVE);
324 if (!XkbKeycodeInRange(keymap, kc))
325 return XKB_GROUP_INVALID;
327 key = XkbKey(keymap, kc);
328 if (key->num_groups == 0)
329 return XKB_GROUP_INVALID;
331 if (ret < key->num_groups)
334 switch (key->out_of_range_group_action) {
335 case XkbRedirectIntoRange:
336 ret = key->out_of_range_group_number;
337 if (ret >= key->num_groups)
341 case XkbClampIntoRange:
342 ret = key->num_groups - 1;
345 case XkbWrapIntoRange:
347 ret %= key->num_groups;
355 * As below, but takes an explicit group/level rather than state.
358 xkb_key_get_syms_by_level(struct xkb_keymap *keymap, struct xkb_key *key,
359 xkb_group_index_t group, xkb_level_index_t level,
360 const xkb_keysym_t **syms_out)
364 if (group >= key->num_groups)
366 if (level >= XkbKeyGroupWidth(keymap, key, group))
369 num_syms = XkbKeyNumSyms(key, group, level);
373 *syms_out = XkbKeySymEntry(key, group, level);
382 * Provides the symbols to use for the given key and state. Returns the
383 * number of symbols pointed to in syms_out.
386 xkb_key_get_syms(struct xkb_state *state, xkb_keycode_t kc,
387 const xkb_keysym_t **syms_out)
389 struct xkb_keymap *keymap = xkb_state_get_map(state);
391 xkb_group_index_t group;
392 xkb_level_index_t level;
394 if (!state || !XkbKeycodeInRange(keymap, kc))
397 key = XkbKey(keymap, kc);
399 group = xkb_key_get_group(state, kc);
400 if (group == XKB_GROUP_INVALID)
403 level = xkb_key_get_level(state, kc, group);
404 if (level == XKB_LEVEL_INVALID)
407 return xkb_key_get_syms_by_level(keymap, key, group, level, syms_out);
415 * Simple boolean specifying whether or not the key should repeat.
418 xkb_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t kc)
420 if (!XkbKeycodeInRange(keymap, kc))
422 return XkbKey(keymap, kc)->repeats;
425 static xkb_mod_mask_t
426 key_get_consumed(struct xkb_state *state, xkb_keycode_t kc)
428 struct xkb_kt_map_entry *entry;
430 entry = get_entry_for_key_state(state, kc);
434 return entry->mods.mask & ~entry->preserve.mask;
438 * Tests to see if a modifier is used up by our translation of a
439 * keycode to keysyms, taking note of the current modifier state and
440 * the appropriate key type's preserve information, if any. This allows
441 * the user to mask out the modifier in later processing of the
442 * modifiers, e.g. when implementing hot keys or accelerators.
444 * See also, for example:
445 * - XkbTranslateKeyCode(3), mod_rtrn retrun value, from libX11.
446 * - gdk_keymap_translate_keyboard_state, consumed_modifiers return value,
450 xkb_key_mod_index_is_consumed(struct xkb_state *state, xkb_keycode_t kc,
453 if (!XkbKeycodeInRange(xkb_state_get_map(state), kc))
456 return !!((1 << idx) & key_get_consumed(state, kc));
460 * Calculates which modifiers should be consumed during key processing,
461 * and returns the mask with all these modifiers removed. e.g. if
462 * given a state of Alt and Shift active for a two-level alphabetic
463 * key containing plus and equal on the first and second level
464 * respectively, will return a mask of only Alt, as Shift has been
465 * consumed by the type handling.
467 XKB_EXPORT xkb_mod_mask_t
468 xkb_key_mod_mask_remove_consumed(struct xkb_state *state, xkb_keycode_t kc,
471 if (!XkbKeycodeInRange(xkb_state_get_map(state), kc))
474 return mask & ~key_get_consumed(state, kc);