Change 'indicator' to 'led' everywhere possible
[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     if (keymap->keys) {
93         xkb_foreach_key(key, keymap) {
94             for (i = 0; i < key->num_groups; i++) {
95                 for (j = 0; j < XkbKeyGroupWidth(key, i); j++)
96                     if (key->groups[i].levels[j].num_syms > 1)
97                         free(key->groups[i].levels[j].u.syms);
98                 free(key->groups[i].levels);
99             }
100             free(key->groups);
101         }
102         free(keymap->keys);
103     }
104     for (i = 0; i < keymap->num_types; i++) {
105         free(keymap->types[i].map);
106         free(keymap->types[i].level_names);
107     }
108     free(keymap->types);
109     darray_free(keymap->sym_interprets);
110     darray_free(keymap->key_aliases);
111     free(keymap->group_names);
112     darray_free(keymap->mods);
113     darray_free(keymap->leds);
114     free(keymap->keycodes_section_name);
115     free(keymap->symbols_section_name);
116     free(keymap->types_section_name);
117     free(keymap->compat_section_name);
118     xkb_context_unref(keymap->ctx);
119     free(keymap);
120 }
121
122 /**
123  * Returns the total number of modifiers active in the keymap.
124  */
125 XKB_EXPORT xkb_mod_index_t
126 xkb_keymap_num_mods(struct xkb_keymap *keymap)
127 {
128     return darray_size(keymap->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     if (idx >= darray_size(keymap->mods))
138         return NULL;
139
140     return xkb_atom_text(keymap->ctx, darray_item(keymap->mods, idx).name);
141 }
142
143 /**
144  * Returns the index for a named modifier.
145  */
146 XKB_EXPORT xkb_mod_index_t
147 xkb_keymap_mod_get_index(struct xkb_keymap *keymap, const char *name)
148 {
149     xkb_mod_index_t i;
150     xkb_atom_t atom;
151     const struct xkb_mod *mod;
152
153     atom = xkb_atom_lookup(keymap->ctx, name);
154     if (atom == XKB_ATOM_NONE)
155         return XKB_MOD_INVALID;
156
157     darray_enumerate(i, mod, keymap->mods)
158         if (mod->name == atom)
159             return i;
160
161     return XKB_MOD_INVALID;
162 }
163
164 /**
165  * Return the total number of active groups in the keymap.
166  */
167 XKB_EXPORT xkb_layout_index_t
168 xkb_keymap_num_layouts(struct xkb_keymap *keymap)
169 {
170     return keymap->num_groups;
171 }
172
173 /**
174  * Returns the name for a given group.
175  */
176 XKB_EXPORT const char *
177 xkb_keymap_layout_get_name(struct xkb_keymap *keymap, xkb_layout_index_t idx)
178 {
179     if (idx >= keymap->num_group_names)
180         return NULL;
181
182     return xkb_atom_text(keymap->ctx, keymap->group_names[idx]);
183 }
184
185 /**
186  * Returns the index for a named layout.
187  */
188 XKB_EXPORT xkb_layout_index_t
189 xkb_keymap_layout_get_index(struct xkb_keymap *keymap, const char *name)
190 {
191     xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
192     xkb_layout_index_t i;
193
194     if (atom == XKB_ATOM_NONE)
195         return XKB_LAYOUT_INVALID;
196
197     for (i = 0; i < keymap->num_group_names; i++)
198         if (keymap->group_names[i] == atom)
199             return i;
200
201     return XKB_LAYOUT_INVALID;
202 }
203
204 /**
205  * Returns the number of layouts active for a particular key.
206  */
207 XKB_EXPORT xkb_layout_index_t
208 xkb_keymap_num_layouts_for_key(struct xkb_keymap *keymap, xkb_keycode_t kc)
209 {
210     const struct xkb_key *key = XkbKey(keymap, kc);
211
212     if (!key)
213         return 0;
214
215     return key->num_groups;
216 }
217
218 /**
219  * Returns the number of levels active for a particular key and layout.
220  */
221 XKB_EXPORT xkb_level_index_t
222 xkb_keymap_num_levels_for_key(struct xkb_keymap *keymap, xkb_keycode_t kc,
223                               xkb_layout_index_t layout)
224 {
225     const struct xkb_key *key = XkbKey(keymap, kc);
226
227     if (!key)
228         return 0;
229
230     layout = wrap_group_into_range(layout, key->num_groups,
231                                    key->out_of_range_group_action,
232                                    key->out_of_range_group_number);
233     if (layout == XKB_LAYOUT_INVALID)
234         return 0;
235
236     return XkbKeyGroupWidth(key, layout);
237 }
238
239 /**
240  * Return the total number of LEDs in the keymap.
241  */
242 XKB_EXPORT xkb_led_index_t
243 xkb_keymap_num_leds(struct xkb_keymap *keymap)
244 {
245     return darray_size(keymap->leds);
246 }
247
248 /**
249  * Returns the name for a given LED.
250  */
251 XKB_EXPORT const char *
252 xkb_keymap_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx)
253 {
254     if (idx >= darray_size(keymap->leds))
255         return NULL;
256
257     return xkb_atom_text(keymap->ctx, darray_item(keymap->leds, idx).name);
258 }
259
260 /**
261  * Returns the index for a named LED.
262  */
263 XKB_EXPORT xkb_led_index_t
264 xkb_keymap_led_get_index(struct xkb_keymap *keymap, const char *name)
265 {
266     xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
267     xkb_led_index_t i;
268     const struct xkb_led *led;
269
270     if (atom == XKB_ATOM_NONE)
271         return XKB_LED_INVALID;
272
273     darray_enumerate(i, led, keymap->leds)
274         if (led->name == atom)
275             return i;
276
277     return XKB_LED_INVALID;
278 }
279
280 /**
281  * As below, but takes an explicit layout/level rather than state.
282  */
283 XKB_EXPORT int
284 xkb_keymap_key_get_syms_by_level(struct xkb_keymap *keymap,
285                                  xkb_keycode_t kc,
286                                  xkb_layout_index_t layout,
287                                  xkb_level_index_t level,
288                                  const xkb_keysym_t **syms_out)
289 {
290     const struct xkb_key *key = XkbKey(keymap, kc);
291     int num_syms;
292
293     if (!key)
294         goto err;
295
296     layout = wrap_group_into_range(layout, key->num_groups,
297                                    key->out_of_range_group_action,
298                                    key->out_of_range_group_number);
299     if (layout == XKB_LAYOUT_INVALID)
300         goto err;
301
302     if (level >= XkbKeyGroupWidth(key, layout))
303         goto err;
304
305     num_syms = key->groups[layout].levels[level].num_syms;
306     if (num_syms == 0)
307         goto err;
308
309     if (num_syms == 1)
310         *syms_out = &key->groups[layout].levels[level].u.sym;
311     else
312         *syms_out = key->groups[layout].levels[level].u.syms;
313
314     return num_syms;
315
316 err:
317     *syms_out = NULL;
318     return 0;
319 }
320
321 /**
322  * Simple boolean specifying whether or not the key should repeat.
323  */
324 XKB_EXPORT int
325 xkb_keymap_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t kc)
326 {
327     const struct xkb_key *key = XkbKey(keymap, kc);
328
329     if (!key)
330         return 0;
331
332     return key->repeats;
333 }