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