2 * Copyright © 2012 Intel Corporation
3 * Copyright © 2012 Ran Benita <ran234@gmail.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
24 * Author: Daniel Stone <daniel@fooishbar.org>
27 /************************************************************
28 * Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
30 * Permission to use, copy, modify, and distribute this
31 * software and its documentation for any purpose and without
32 * fee is hereby granted, provided that the above copyright
33 * notice appear in all copies and that both that copyright
34 * notice and this permission notice appear in supporting
35 * documentation, and that the name of Silicon Graphics not be
36 * used in advertising or publicity pertaining to distribution
37 * of the software without specific prior written permission.
38 * Silicon Graphics makes no representation about the suitability
39 * of this software for any purpose. It is provided "as is"
40 * without any express or implied warranty.
42 * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
43 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
44 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
45 * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
47 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
48 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
49 * THE USE OR PERFORMANCE OF THIS SOFTWARE.
51 * ********************************************************/
57 xkb_keymap_new(struct xkb_context *ctx,
58 enum xkb_keymap_format format,
59 enum xkb_keymap_compile_flags flags)
61 struct xkb_keymap *keymap;
63 keymap = calloc(1, sizeof(*keymap));
68 keymap->ctx = xkb_context_ref(ctx);
70 keymap->format = format;
71 keymap->flags = flags;
76 XKB_EXPORT struct xkb_keymap *
77 xkb_keymap_ref(struct xkb_keymap *keymap)
84 xkb_keymap_unref(struct xkb_keymap *keymap)
89 if (!keymap || --keymap->refcnt > 0)
92 darray_foreach(key, keymap->keys) {
93 for (i = 0; i < key->num_groups; i++) {
94 for (j = 0; j < XkbKeyGroupWidth(key, i); j++)
95 if (key->groups[i].levels[j].num_syms > 1)
96 free(key->groups[i].levels[j].u.syms);
97 free(key->groups[i].levels);
101 darray_free(keymap->keys);
102 for (i = 0; i < keymap->num_types; i++) {
103 free(keymap->types[i].map);
104 free(keymap->types[i].level_names);
107 darray_free(keymap->sym_interpret);
108 darray_free(keymap->key_aliases);
109 darray_free(keymap->group_names);
110 darray_free(keymap->vmods);
111 free(keymap->keycodes_section_name);
112 free(keymap->symbols_section_name);
113 free(keymap->types_section_name);
114 free(keymap->compat_section_name);
115 xkb_context_unref(keymap->ctx);
120 * Returns the total number of modifiers active in the keymap.
122 XKB_EXPORT xkb_mod_index_t
123 xkb_keymap_num_mods(struct xkb_keymap *keymap)
125 /* We always have all the core modifiers (for now), plus any virtual
126 * modifiers we may have defined. */
127 return XKB_NUM_CORE_MODS + darray_size(keymap->vmods);
131 * Return the name for a given modifier.
133 XKB_EXPORT const char *
134 xkb_keymap_mod_get_name(struct xkb_keymap *keymap, xkb_mod_index_t idx)
137 const struct xkb_vmod *vmod;
139 if (idx >= xkb_keymap_num_mods(keymap))
142 /* First try to find a legacy modifier name. If that fails, try to
143 * find a virtual mod name. */
144 name = ModIndexToName(idx);
148 vmod = &darray_item(keymap->vmods, idx - XKB_NUM_CORE_MODS);
149 return xkb_atom_text(keymap->ctx, vmod->name);
153 * Returns the index for a named modifier.
155 XKB_EXPORT xkb_mod_index_t
156 xkb_keymap_mod_get_index(struct xkb_keymap *keymap, const char *name)
160 const struct xkb_vmod *vmod;
162 i = ModNameToIndex(name);
163 if (i != XKB_MOD_INVALID)
166 atom = xkb_atom_lookup(keymap->ctx, name);
167 if (atom == XKB_ATOM_NONE)
168 return XKB_MOD_INVALID;
170 darray_enumerate(i, vmod, keymap->vmods)
171 if (vmod->name == atom)
172 return i + XKB_NUM_CORE_MODS;
174 return XKB_MOD_INVALID;
178 * Return the total number of active groups in the keymap.
180 XKB_EXPORT xkb_layout_index_t
181 xkb_keymap_num_layouts(struct xkb_keymap *keymap)
183 return keymap->num_groups;
187 * Returns the name for a given group.
189 XKB_EXPORT const char *
190 xkb_keymap_layout_get_name(struct xkb_keymap *keymap, xkb_layout_index_t idx)
192 if (idx >= xkb_keymap_num_layouts(keymap) ||
193 idx >= darray_size(keymap->group_names))
196 return xkb_atom_text(keymap->ctx, darray_item(keymap->group_names, idx));
200 * Returns the index for a named layout.
202 XKB_EXPORT xkb_layout_index_t
203 xkb_keymap_layout_get_index(struct xkb_keymap *keymap, const char *name)
205 xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
206 xkb_atom_t *group_name;
207 xkb_layout_index_t i;
209 if (atom == XKB_ATOM_NONE)
210 return XKB_LAYOUT_INVALID;
212 darray_enumerate(i, group_name, keymap->group_names)
213 if (*group_name == atom)
216 return XKB_LAYOUT_INVALID;
220 * Returns the number of layouts active for a particular key.
222 XKB_EXPORT xkb_layout_index_t
223 xkb_keymap_num_layouts_for_key(struct xkb_keymap *keymap, xkb_keycode_t kc)
225 const struct xkb_key *key = XkbKey(keymap, kc);
230 return key->num_groups;
234 * Returns the number of levels active for a particular key and layout.
236 XKB_EXPORT xkb_level_index_t
237 xkb_keymap_num_levels_for_key(struct xkb_keymap *keymap, xkb_keycode_t kc,
238 xkb_layout_index_t layout)
240 const struct xkb_key *key = XkbKey(keymap, kc);
245 return XkbKeyGroupWidth(key, layout);
249 * Return the total number of active LEDs in the keymap.
251 XKB_EXPORT xkb_led_index_t
252 xkb_keymap_num_leds(struct xkb_keymap *keymap)
254 xkb_led_index_t ret = 0;
257 for (i = 0; i < XKB_NUM_INDICATORS; i++)
258 if (keymap->indicators[i].which_groups ||
259 keymap->indicators[i].which_mods ||
260 keymap->indicators[i].ctrls)
267 * Returns the name for a given group.
269 XKB_EXPORT const char *
270 xkb_keymap_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx)
272 if (idx >= xkb_keymap_num_leds(keymap))
275 return xkb_atom_text(keymap->ctx, keymap->indicators[idx].name);
279 * Returns the index for a named group.
281 XKB_EXPORT xkb_layout_index_t
282 xkb_keymap_led_get_index(struct xkb_keymap *keymap, const char *name)
284 xkb_led_index_t num_leds = xkb_keymap_num_leds(keymap);
285 xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
288 if (atom == XKB_ATOM_NONE)
289 return XKB_LED_INVALID;
291 for (i = 0; i < num_leds; i++)
292 if (keymap->indicators[i].name == atom)
295 return XKB_LED_INVALID;
299 * As below, but takes an explicit layout/level rather than state.
302 xkb_keymap_key_get_syms_by_level(struct xkb_keymap *keymap,
304 xkb_layout_index_t layout,
305 xkb_level_index_t level,
306 const xkb_keysym_t **syms_out)
308 const struct xkb_key *key = XkbKey(keymap, kc);
313 if (layout >= key->num_groups)
315 if (level >= XkbKeyGroupWidth(key, layout))
318 num_syms = key->groups[layout].levels[level].num_syms;
323 *syms_out = &key->groups[layout].levels[level].u.sym;
325 *syms_out = key->groups[layout].levels[level].u.syms;
335 * Simple boolean specifying whether or not the key should repeat.
338 xkb_keymap_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t kc)
340 const struct xkb_key *key = XkbKey(keymap, kc);