keymap: don't use XKB_NUM_GROUPS for key->kt_index
[platform/upstream/libxkbcommon.git] / src / keymap.c
1 /**
2  * Copyright © 2012 Intel Corporation
3  * Copyright © 2012 Ran Benita <ran234@gmail.com>
4  *
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:
11  *
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
14  * Software.
15  *
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.
23  *
24  * Author: Daniel Stone <daniel@fooishbar.org>
25  */
26
27 /************************************************************
28  * Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
29  *
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.
41  *
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.
50  *
51  * ********************************************************/
52
53 #include "keymap.h"
54 #include "text.h"
55
56 struct xkb_keymap *
57 xkb_keymap_new(struct xkb_context *ctx,
58                enum xkb_keymap_format format,
59                enum xkb_keymap_compile_flags flags)
60 {
61     struct xkb_keymap *keymap;
62
63     keymap = calloc(1, sizeof(*keymap));
64     if (!keymap)
65         return NULL;
66
67     keymap->refcnt = 1;
68     keymap->ctx = xkb_context_ref(ctx);
69
70     keymap->format = format;
71     keymap->flags = flags;
72
73     return keymap;
74 }
75
76 XKB_EXPORT struct xkb_keymap *
77 xkb_keymap_ref(struct xkb_keymap *keymap)
78 {
79     keymap->refcnt++;
80     return keymap;
81 }
82
83 XKB_EXPORT void
84 xkb_keymap_unref(struct xkb_keymap *keymap)
85 {
86     unsigned int i;
87     struct xkb_key *key;
88
89     if (!keymap || --keymap->refcnt > 0)
90         return;
91
92     for (i = 0; i < keymap->num_types; i++) {
93         free(keymap->types[i].map);
94         free(keymap->types[i].level_names);
95     }
96     free(keymap->types);
97     darray_foreach(key, keymap->keys) {
98         free(key->sym_index);
99         free(key->num_syms);
100         free(key->syms);
101         free(key->actions);
102         free(key->kt_index);
103     }
104     darray_free(keymap->keys);
105     darray_free(keymap->sym_interpret);
106     darray_free(keymap->key_aliases);
107     free(keymap->keycodes_section_name);
108     free(keymap->symbols_section_name);
109     free(keymap->types_section_name);
110     free(keymap->compat_section_name);
111     xkb_context_unref(keymap->ctx);
112     free(keymap);
113 }
114
115 /**
116  * Returns the total number of modifiers active in the keymap.
117  */
118 XKB_EXPORT xkb_mod_index_t
119 xkb_keymap_num_mods(struct xkb_keymap *keymap)
120 {
121     xkb_mod_index_t i;
122
123     for (i = 0; i < XKB_NUM_VIRTUAL_MODS; i++)
124         if (!keymap->vmod_names[i])
125             break;
126
127     /* We always have all the core modifiers (for now), plus any virtual
128      * modifiers we may have defined. */
129     return i + XKB_NUM_CORE_MODS;
130 }
131
132 /**
133  * Return the name for a given modifier.
134  */
135 XKB_EXPORT const char *
136 xkb_keymap_mod_get_name(struct xkb_keymap *keymap, xkb_mod_index_t idx)
137 {
138     const char *name;
139
140     if (idx >= xkb_keymap_num_mods(keymap))
141         return NULL;
142
143     /* First try to find a legacy modifier name.  If that fails, try to
144      * find a virtual mod name. */
145     name = ModIndexToName(idx);
146     if (!name)
147         name = xkb_atom_text(keymap->ctx,
148                              keymap->vmod_names[idx - XKB_NUM_CORE_MODS]);
149
150     return name;
151 }
152
153 /**
154  * Returns the index for a named modifier.
155  */
156 XKB_EXPORT xkb_mod_index_t
157 xkb_keymap_mod_get_index(struct xkb_keymap *keymap, const char *name)
158 {
159     xkb_mod_index_t i;
160     xkb_atom_t atom;
161
162     i = ModNameToIndex(name);
163     if (i != XKB_MOD_INVALID)
164         return i;
165
166     atom = xkb_atom_lookup(keymap->ctx, name);
167     if (atom == XKB_ATOM_NONE)
168         return XKB_MOD_INVALID;
169
170     for (i = 0; i < XKB_NUM_VIRTUAL_MODS; i++) {
171         if (keymap->vmod_names[i] == XKB_ATOM_NONE)
172             break;
173         if (keymap->vmod_names[i] == atom)
174             return i + XKB_NUM_CORE_MODS;
175     }
176
177     return XKB_MOD_INVALID;
178 }
179
180 /**
181  * Return the total number of active groups in the keymap.
182  */
183 XKB_EXPORT xkb_layout_index_t
184 xkb_keymap_num_layouts(struct xkb_keymap *keymap)
185 {
186     return keymap->num_groups;
187 }
188
189 /**
190  * Returns the name for a given group.
191  */
192 XKB_EXPORT const char *
193 xkb_keymap_layout_get_name(struct xkb_keymap *keymap, xkb_layout_index_t idx)
194 {
195     if (idx >= xkb_keymap_num_layouts(keymap))
196         return NULL;
197
198     return xkb_atom_text(keymap->ctx, keymap->group_names[idx]);
199 }
200
201 /**
202  * Returns the index for a named layout.
203  */
204 XKB_EXPORT xkb_layout_index_t
205 xkb_keymap_layout_get_index(struct xkb_keymap *keymap, const char *name)
206 {
207     xkb_layout_index_t num_groups = xkb_keymap_num_layouts(keymap);
208     xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
209     xkb_layout_index_t i;
210
211     if (atom == XKB_ATOM_NONE)
212         return XKB_LAYOUT_INVALID;
213
214     for (i = 0; i < num_groups; i++)
215         if (keymap->group_names[i] == atom)
216             return i;
217
218     return XKB_LAYOUT_INVALID;
219 }
220
221 /**
222  * Returns the number of layouts active for a particular key.
223  */
224 XKB_EXPORT xkb_layout_index_t
225 xkb_keymap_num_layouts_for_key(struct xkb_keymap *keymap, xkb_keycode_t kc)
226 {
227     const struct xkb_key *key = XkbKey(keymap, kc);
228
229     if (!key)
230         return 0;
231
232     return key->num_groups;
233 }
234
235 /**
236  * Returns the number of levels active for a particular key and layout.
237  */
238 XKB_EXPORT xkb_level_index_t
239 xkb_keymap_num_levels_for_key(struct xkb_keymap *keymap, xkb_keycode_t kc,
240                               xkb_layout_index_t layout)
241 {
242     const struct xkb_key *key = XkbKey(keymap, kc);
243
244     if (!key)
245         return 0;
246
247     return XkbKeyGroupWidth(keymap, key, layout);
248 }
249
250 /**
251  * Return the total number of active LEDs in the keymap.
252  */
253 XKB_EXPORT xkb_led_index_t
254 xkb_keymap_num_leds(struct xkb_keymap *keymap)
255 {
256     xkb_led_index_t ret = 0;
257     xkb_led_index_t i;
258
259     for (i = 0; i < XKB_NUM_INDICATORS; i++)
260         if (keymap->indicators[i].which_groups ||
261             keymap->indicators[i].which_mods ||
262             keymap->indicators[i].ctrls)
263             ret++;
264
265     return ret;
266 }
267
268 /**
269  * Returns the name for a given group.
270  */
271 XKB_EXPORT const char *
272 xkb_keymap_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx)
273 {
274     if (idx >= xkb_keymap_num_leds(keymap))
275         return NULL;
276
277     return xkb_atom_text(keymap->ctx, keymap->indicators[idx].name);
278 }
279
280 /**
281  * Returns the index for a named group.
282  */
283 XKB_EXPORT xkb_layout_index_t
284 xkb_keymap_led_get_index(struct xkb_keymap *keymap, const char *name)
285 {
286     xkb_led_index_t num_leds = xkb_keymap_num_leds(keymap);
287     xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
288     xkb_led_index_t i;
289
290     if (atom == XKB_ATOM_NONE)
291         return XKB_LED_INVALID;
292
293     for (i = 0; i < num_leds; i++)
294         if (keymap->indicators[i].name == atom)
295             return i;
296
297     return XKB_LED_INVALID;
298 }
299
300 /**
301  * As below, but takes an explicit layout/level rather than state.
302  */
303 XKB_EXPORT int
304 xkb_keymap_key_get_syms_by_level(struct xkb_keymap *keymap,
305                                  xkb_keycode_t kc,
306                                  xkb_layout_index_t layout,
307                                  xkb_level_index_t level,
308                                  const xkb_keysym_t **syms_out)
309 {
310     const struct xkb_key *key = XkbKey(keymap, kc);
311     int num_syms;
312
313     if (!key)
314         goto err;
315     if (layout >= key->num_groups)
316         goto err;
317     if (level >= XkbKeyGroupWidth(keymap, key, layout))
318         goto err;
319
320     num_syms = XkbKeyNumSyms(key, layout, level);
321     if (num_syms == 0)
322         goto err;
323
324     *syms_out = XkbKeySymEntry(key, layout, level);
325     return num_syms;
326
327 err:
328     *syms_out = NULL;
329     return 0;
330 }
331
332 /**
333  * Simple boolean specifying whether or not the key should repeat.
334  */
335 XKB_EXPORT int
336 xkb_keymap_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t kc)
337 {
338     const struct xkb_key *key = XkbKey(keymap, kc);
339
340     if (!key)
341         return 0;
342
343     return key->repeats;
344 }