keymap: store a pointer to the type in xkb_group instead of 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, j;
87     struct xkb_key *key;
88
89     if (!keymap || --keymap->refcnt > 0)
90         return;
91
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);
98         }
99         free(key->groups);
100     }
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);
105     }
106     free(keymap->types);
107     darray_free(keymap->sym_interpret);
108     darray_free(keymap->key_aliases);
109     darray_free(keymap->group_names);
110     free(keymap->keycodes_section_name);
111     free(keymap->symbols_section_name);
112     free(keymap->types_section_name);
113     free(keymap->compat_section_name);
114     xkb_context_unref(keymap->ctx);
115     free(keymap);
116 }
117
118 /**
119  * Returns the total number of modifiers active in the keymap.
120  */
121 XKB_EXPORT xkb_mod_index_t
122 xkb_keymap_num_mods(struct xkb_keymap *keymap)
123 {
124     xkb_mod_index_t i;
125
126     for (i = 0; i < XKB_NUM_VIRTUAL_MODS; i++)
127         if (!keymap->vmod_names[i])
128             break;
129
130     /* We always have all the core modifiers (for now), plus any virtual
131      * modifiers we may have defined. */
132     return i + XKB_NUM_CORE_MODS;
133 }
134
135 /**
136  * Return the name for a given modifier.
137  */
138 XKB_EXPORT const char *
139 xkb_keymap_mod_get_name(struct xkb_keymap *keymap, xkb_mod_index_t idx)
140 {
141     const char *name;
142
143     if (idx >= xkb_keymap_num_mods(keymap))
144         return NULL;
145
146     /* First try to find a legacy modifier name.  If that fails, try to
147      * find a virtual mod name. */
148     name = ModIndexToName(idx);
149     if (!name)
150         name = xkb_atom_text(keymap->ctx,
151                              keymap->vmod_names[idx - XKB_NUM_CORE_MODS]);
152
153     return name;
154 }
155
156 /**
157  * Returns the index for a named modifier.
158  */
159 XKB_EXPORT xkb_mod_index_t
160 xkb_keymap_mod_get_index(struct xkb_keymap *keymap, const char *name)
161 {
162     xkb_mod_index_t i;
163     xkb_atom_t atom;
164
165     i = ModNameToIndex(name);
166     if (i != XKB_MOD_INVALID)
167         return i;
168
169     atom = xkb_atom_lookup(keymap->ctx, name);
170     if (atom == XKB_ATOM_NONE)
171         return XKB_MOD_INVALID;
172
173     for (i = 0; i < XKB_NUM_VIRTUAL_MODS; i++) {
174         if (keymap->vmod_names[i] == XKB_ATOM_NONE)
175             break;
176         if (keymap->vmod_names[i] == atom)
177             return i + XKB_NUM_CORE_MODS;
178     }
179
180     return XKB_MOD_INVALID;
181 }
182
183 /**
184  * Return the total number of active groups in the keymap.
185  */
186 XKB_EXPORT xkb_layout_index_t
187 xkb_keymap_num_layouts(struct xkb_keymap *keymap)
188 {
189     return keymap->num_groups;
190 }
191
192 /**
193  * Returns the name for a given group.
194  */
195 XKB_EXPORT const char *
196 xkb_keymap_layout_get_name(struct xkb_keymap *keymap, xkb_layout_index_t idx)
197 {
198     if (idx >= xkb_keymap_num_layouts(keymap) ||
199         idx >= darray_size(keymap->group_names))
200         return NULL;
201
202     return xkb_atom_text(keymap->ctx, darray_item(keymap->group_names, idx));
203 }
204
205 /**
206  * Returns the index for a named layout.
207  */
208 XKB_EXPORT xkb_layout_index_t
209 xkb_keymap_layout_get_index(struct xkb_keymap *keymap, const char *name)
210 {
211     xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
212     xkb_atom_t *group_name;
213     xkb_layout_index_t i;
214
215     if (atom == XKB_ATOM_NONE)
216         return XKB_LAYOUT_INVALID;
217
218     darray_enumerate(i, group_name, keymap->group_names)
219         if (*group_name == atom)
220             return i;
221
222     return XKB_LAYOUT_INVALID;
223 }
224
225 /**
226  * Returns the number of layouts active for a particular key.
227  */
228 XKB_EXPORT xkb_layout_index_t
229 xkb_keymap_num_layouts_for_key(struct xkb_keymap *keymap, xkb_keycode_t kc)
230 {
231     const struct xkb_key *key = XkbKey(keymap, kc);
232
233     if (!key)
234         return 0;
235
236     return key->num_groups;
237 }
238
239 /**
240  * Returns the number of levels active for a particular key and layout.
241  */
242 XKB_EXPORT xkb_level_index_t
243 xkb_keymap_num_levels_for_key(struct xkb_keymap *keymap, xkb_keycode_t kc,
244                               xkb_layout_index_t layout)
245 {
246     const struct xkb_key *key = XkbKey(keymap, kc);
247
248     if (!key)
249         return 0;
250
251     return XkbKeyGroupWidth(key, layout);
252 }
253
254 /**
255  * Return the total number of active LEDs in the keymap.
256  */
257 XKB_EXPORT xkb_led_index_t
258 xkb_keymap_num_leds(struct xkb_keymap *keymap)
259 {
260     xkb_led_index_t ret = 0;
261     xkb_led_index_t i;
262
263     for (i = 0; i < XKB_NUM_INDICATORS; i++)
264         if (keymap->indicators[i].which_groups ||
265             keymap->indicators[i].which_mods ||
266             keymap->indicators[i].ctrls)
267             ret++;
268
269     return ret;
270 }
271
272 /**
273  * Returns the name for a given group.
274  */
275 XKB_EXPORT const char *
276 xkb_keymap_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx)
277 {
278     if (idx >= xkb_keymap_num_leds(keymap))
279         return NULL;
280
281     return xkb_atom_text(keymap->ctx, keymap->indicators[idx].name);
282 }
283
284 /**
285  * Returns the index for a named group.
286  */
287 XKB_EXPORT xkb_layout_index_t
288 xkb_keymap_led_get_index(struct xkb_keymap *keymap, const char *name)
289 {
290     xkb_led_index_t num_leds = xkb_keymap_num_leds(keymap);
291     xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
292     xkb_led_index_t i;
293
294     if (atom == XKB_ATOM_NONE)
295         return XKB_LED_INVALID;
296
297     for (i = 0; i < num_leds; i++)
298         if (keymap->indicators[i].name == atom)
299             return i;
300
301     return XKB_LED_INVALID;
302 }
303
304 /**
305  * As below, but takes an explicit layout/level rather than state.
306  */
307 XKB_EXPORT int
308 xkb_keymap_key_get_syms_by_level(struct xkb_keymap *keymap,
309                                  xkb_keycode_t kc,
310                                  xkb_layout_index_t layout,
311                                  xkb_level_index_t level,
312                                  const xkb_keysym_t **syms_out)
313 {
314     const struct xkb_key *key = XkbKey(keymap, kc);
315     int num_syms;
316
317     if (!key)
318         goto err;
319     if (layout >= key->num_groups)
320         goto err;
321     if (level >= XkbKeyGroupWidth(key, layout))
322         goto err;
323
324     num_syms = XkbKeyNumSyms(key, layout, level);
325     if (num_syms == 0)
326         goto err;
327
328     *syms_out = XkbKeySymEntry(key, layout, level);
329     return num_syms;
330
331 err:
332     *syms_out = NULL;
333     return 0;
334 }
335
336 /**
337  * Simple boolean specifying whether or not the key should repeat.
338  */
339 XKB_EXPORT int
340 xkb_keymap_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t kc)
341 {
342     const struct xkb_key *key = XkbKey(keymap, kc);
343
344     if (!key)
345         return 0;
346
347     return key->repeats;
348 }