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 < XKB_NUM_VIRTUAL_MODS; 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 + XKB_NUM_CORE_MODS;
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 = xkb_atom_text(keymap->ctx,
141 keymap->vmod_names[idx - XKB_NUM_CORE_MODS]);
147 * Returns the index for a named modifier.
149 XKB_EXPORT xkb_mod_index_t
150 xkb_map_mod_get_index(struct xkb_keymap *keymap, const char *name)
155 i = ModNameToIndex(name);
156 if (i != XKB_MOD_INVALID)
159 atom = xkb_atom_lookup(keymap->ctx, name);
160 if (atom == XKB_ATOM_NONE)
161 return XKB_MOD_INVALID;
163 for (i = 0; i < XKB_NUM_VIRTUAL_MODS; i++) {
164 if (keymap->vmod_names[i] == XKB_ATOM_NONE)
166 if (keymap->vmod_names[i] == atom)
167 return i + XKB_NUM_CORE_MODS;
170 return XKB_MOD_INVALID;
174 * Return the total number of active groups in the keymap.
176 XKB_EXPORT xkb_group_index_t
177 xkb_map_num_groups(struct xkb_keymap *keymap)
179 return keymap->num_groups;
183 * Returns the name for a given group.
185 XKB_EXPORT const char *
186 xkb_map_group_get_name(struct xkb_keymap *keymap, xkb_group_index_t idx)
188 if (idx >= xkb_map_num_groups(keymap))
191 return xkb_atom_text(keymap->ctx, keymap->group_names[idx]);
195 * Returns the index for a named group.
197 XKB_EXPORT xkb_group_index_t
198 xkb_map_group_get_index(struct xkb_keymap *keymap, const char *name)
200 xkb_group_index_t num_groups = xkb_map_num_groups(keymap);
201 xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
204 if (atom == XKB_ATOM_NONE)
205 return XKB_GROUP_INVALID;
207 for (i = 0; i < num_groups; i++)
208 if (keymap->group_names[i] == atom)
211 return XKB_GROUP_INVALID;
215 * Returns the number of groups active for a particular key.
217 XKB_EXPORT xkb_group_index_t
218 xkb_key_num_groups(struct xkb_keymap *keymap, xkb_keycode_t kc)
220 if (!XkbKeycodeInRange(keymap, kc))
223 return XkbKey(keymap, kc)->num_groups;
227 * Return the total number of active LEDs in the keymap.
229 XKB_EXPORT xkb_led_index_t
230 xkb_map_num_leds(struct xkb_keymap *keymap)
232 xkb_led_index_t ret = 0;
235 for (i = 0; i < XKB_NUM_INDICATORS; i++)
236 if (keymap->indicators[i].which_groups ||
237 keymap->indicators[i].which_mods ||
238 keymap->indicators[i].ctrls)
245 * Returns the name for a given group.
247 XKB_EXPORT const char *
248 xkb_map_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx)
250 if (idx >= xkb_map_num_leds(keymap))
253 return xkb_atom_text(keymap->ctx, keymap->indicators[idx].name);
257 * Returns the index for a named group.
259 XKB_EXPORT xkb_group_index_t
260 xkb_map_led_get_index(struct xkb_keymap *keymap, const char *name)
262 xkb_led_index_t num_leds = xkb_map_num_leds(keymap);
263 xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
266 if (atom == XKB_ATOM_NONE)
267 return XKB_LED_INVALID;
269 for (i = 0; i < num_leds; i++)
270 if (keymap->indicators[i].name == atom)
273 return XKB_LED_INVALID;
276 static struct xkb_kt_map_entry *
277 get_entry_for_key_state(struct xkb_state *state, xkb_keycode_t kc)
279 struct xkb_keymap *keymap = xkb_state_get_map(state);
280 xkb_group_index_t group;
281 struct xkb_key_type *type;
282 xkb_mod_mask_t active_mods;
285 group = xkb_key_get_group(state, kc);
286 if (group == XKB_GROUP_INVALID)
289 type = XkbKeyType(keymap, XkbKey(keymap, kc), group);
290 active_mods = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
291 active_mods &= type->mods.mask;
293 for (i = 0; i < type->num_entries; i++)
294 if (type->map[i].mods.mask == active_mods)
295 return &type->map[i];
301 * Returns the level to use for the given key and state, or
305 xkb_key_get_level(struct xkb_state *state, xkb_keycode_t kc,
306 xkb_group_index_t group)
308 struct xkb_kt_map_entry *entry;
310 /* If we don't find an explicit match the default is 0. */
311 entry = get_entry_for_key_state(state, kc);
319 * Returns the group to use for the given key and state, taking
320 * wrapping/clamping/etc into account, or XKB_GROUP_INVALID.
323 xkb_key_get_group(struct xkb_state *state, xkb_keycode_t kc)
325 struct xkb_keymap *keymap = xkb_state_get_map(state);
327 xkb_group_index_t ret = xkb_state_serialize_group(state,
328 XKB_STATE_EFFECTIVE);
330 key = XkbKey(keymap, kc);
331 if (key->num_groups == 0)
332 return XKB_GROUP_INVALID;
334 if (ret < key->num_groups)
337 switch (key->out_of_range_group_action) {
338 case XkbRedirectIntoRange:
339 ret = key->out_of_range_group_number;
340 if (ret >= key->num_groups)
344 case XkbClampIntoRange:
345 ret = key->num_groups - 1;
348 case XkbWrapIntoRange:
350 ret %= key->num_groups;
358 * As below, but takes an explicit group/level rather than state.
361 xkb_key_get_syms_by_level(struct xkb_keymap *keymap, struct xkb_key *key,
362 xkb_group_index_t group, xkb_level_index_t level,
363 const xkb_keysym_t **syms_out)
367 if (group >= key->num_groups)
369 if (level >= XkbKeyGroupWidth(keymap, key, group))
372 num_syms = XkbKeyNumSyms(key, group, level);
376 *syms_out = XkbKeySymEntry(key, group, level);
385 * Provides the symbols to use for the given key and state. Returns the
386 * number of symbols pointed to in syms_out.
389 xkb_key_get_syms(struct xkb_state *state, xkb_keycode_t kc,
390 const xkb_keysym_t **syms_out)
392 struct xkb_keymap *keymap = xkb_state_get_map(state);
394 xkb_group_index_t group;
395 xkb_level_index_t level;
397 if (!XkbKeycodeInRange(keymap, kc))
400 key = XkbKey(keymap, kc);
402 group = xkb_key_get_group(state, kc);
403 if (group == XKB_GROUP_INVALID)
406 level = xkb_key_get_level(state, kc, group);
407 if (level == XKB_LEVEL_INVALID)
410 return xkb_key_get_syms_by_level(keymap, key, group, level, syms_out);
418 * Simple boolean specifying whether or not the key should repeat.
421 xkb_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t kc)
423 if (!XkbKeycodeInRange(keymap, kc))
426 return XkbKey(keymap, kc)->repeats;
429 static xkb_mod_mask_t
430 key_get_consumed(struct xkb_state *state, xkb_keycode_t kc)
432 struct xkb_kt_map_entry *entry;
434 entry = get_entry_for_key_state(state, kc);
438 return entry->mods.mask & ~entry->preserve.mask;
442 * Tests to see if a modifier is used up by our translation of a
443 * keycode to keysyms, taking note of the current modifier state and
444 * the appropriate key type's preserve information, if any. This allows
445 * the user to mask out the modifier in later processing of the
446 * modifiers, e.g. when implementing hot keys or accelerators.
448 * See also, for example:
449 * - XkbTranslateKeyCode(3), mod_rtrn retrun value, from libX11.
450 * - gdk_keymap_translate_keyboard_state, consumed_modifiers return value,
454 xkb_key_mod_index_is_consumed(struct xkb_state *state, xkb_keycode_t kc,
457 if (!XkbKeycodeInRange(xkb_state_get_map(state), kc))
460 return !!((1 << idx) & key_get_consumed(state, kc));
464 * Calculates which modifiers should be consumed during key processing,
465 * and returns the mask with all these modifiers removed. e.g. if
466 * given a state of Alt and Shift active for a two-level alphabetic
467 * key containing plus and equal on the first and second level
468 * respectively, will return a mask of only Alt, as Shift has been
469 * consumed by the type handling.
471 XKB_EXPORT xkb_mod_mask_t
472 xkb_key_mod_mask_remove_consumed(struct xkb_state *state, xkb_keycode_t kc,
475 if (!XkbKeycodeInRange(xkb_state_get_map(state), kc))
478 return mask & ~key_get_consumed(state, kc);