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