keytypes: use darray for xkb_kt_map_entry's
[platform/upstream/libxkbcommon.git] / src / map.c
1 /**
2  * Copyright © 2012 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Author: Daniel Stone <daniel@fooishbar.org>
24  */
25
26 /************************************************************
27  * Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
28  *
29  * Permission to use, copy, modify, and distribute this
30  * software and its documentation for any purpose and without
31  * fee is hereby granted, provided that the above copyright
32  * notice appear in all copies and that both that copyright
33  * notice and this permission notice appear in supporting
34  * documentation, and that the name of Silicon Graphics not be
35  * used in advertising or publicity pertaining to distribution
36  * of the software without specific prior written permission.
37  * Silicon Graphics makes no representation about the suitability
38  * of this software for any purpose. It is provided "as is"
39  * without any express or implied warranty.
40  *
41  * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
42  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
43  * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
44  * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
45  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
46  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
47  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
48  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
49  *
50  * ********************************************************/
51
52 #include "xkb-priv.h"
53
54 /**
55  * Returns the total number of modifiers active in the keymap.
56  */
57 _X_EXPORT xkb_mod_index_t
58 xkb_map_num_mods(struct xkb_keymap *keymap)
59 {
60     xkb_mod_index_t i;
61
62     for (i = 0; i < XkbNumVirtualMods; i++)
63         if (!keymap->names->vmods[i])
64             break;
65
66     /* We always have all the core modifiers (for now), plus any virtual
67      * modifiers we may have defined, and then shift to one-based indexing. */
68     return i + Mod5MapIndex + 1;
69 }
70
71 /**
72  * Return the name for a given modifier.
73  */
74 _X_EXPORT const char *
75 xkb_map_mod_get_name(struct xkb_keymap *keymap, xkb_mod_index_t idx)
76 {
77     if (idx >= xkb_map_num_mods(keymap))
78         return NULL;
79
80     /* First try to find a legacy modifier name. */
81     switch (idx) {
82     case ShiftMapIndex:
83         return "Shift";
84     case ControlMapIndex:
85         return "Control";
86     case LockMapIndex:
87         return "Caps Lock";
88     case Mod1MapIndex:
89         return "Mod1";
90     case Mod2MapIndex:
91         return "Mod2";
92     case Mod3MapIndex:
93         return "Mod3";
94     case Mod4MapIndex:
95         return "Mod4";
96     case Mod5MapIndex:
97         return "Mod5";
98     default:
99         break;
100     }
101
102     /* If that fails, try to find a virtual mod name. */
103     return keymap->names->vmods[idx - Mod5MapIndex];
104 }
105
106 /**
107  * Returns the index for a named modifier.
108  */
109 _X_EXPORT xkb_mod_index_t
110 xkb_map_mod_get_index(struct xkb_keymap *keymap, const char *name)
111 {
112     xkb_mod_index_t i;
113
114     if (strcasecmp(name, "Shift") == 0)
115         return ShiftMapIndex;
116     if (strcasecmp(name, "Control") == 0)
117         return ControlMapIndex;
118     if (strcasecmp(name, "Caps Lock") == 0)
119         return LockMapIndex;
120     if (strcasecmp(name, "Mod1") == 0)
121         return Mod1MapIndex;
122     if (strcasecmp(name, "Mod2") == 0)
123         return Mod2MapIndex;
124     if (strcasecmp(name, "Mod3") == 0)
125         return Mod3MapIndex;
126     if (strcasecmp(name, "Mod4") == 0)
127         return Mod4MapIndex;
128     if (strcasecmp(name, "Mod5") == 0)
129         return Mod5MapIndex;
130
131     for (i = 0; i < XkbNumVirtualMods && keymap->names->vmods[i]; i++) {
132         if (strcasecmp(name, keymap->names->vmods[i]) == 0)
133             return i + Mod5MapIndex;
134     }
135
136     return XKB_GROUP_INVALID;
137 }
138
139 /**
140  * Return the total number of active groups in the keymap.
141  */
142 _X_EXPORT xkb_group_index_t
143 xkb_map_num_groups(struct xkb_keymap *keymap)
144 {
145     xkb_group_index_t ret = 0;
146     xkb_group_index_t i;
147
148     for (i = 0; i < XkbNumKbdGroups; i++)
149         if (keymap->compat->groups[i].mask)
150             ret++;
151
152     return ret;
153 }
154
155 /**
156  * Returns the name for a given group.
157  */
158 _X_EXPORT const char *
159 xkb_map_group_get_name(struct xkb_keymap *keymap, xkb_group_index_t idx)
160 {
161     if (idx >= xkb_map_num_groups(keymap))
162         return NULL;
163
164     return keymap->names->groups[idx];
165 }
166
167 /**
168  * Returns the index for a named group.
169  */
170 _X_EXPORT xkb_group_index_t
171 xkb_map_group_get_index(struct xkb_keymap *keymap, const char *name)
172 {
173     xkb_group_index_t num_groups = xkb_map_num_groups(keymap);
174     xkb_group_index_t i;
175
176     for (i = 0; i < num_groups; i++) {
177         if (strcasecmp(keymap->names->groups[i], name) == 0)
178             return i;
179     }
180
181     return XKB_GROUP_INVALID;
182 }
183
184 /**
185  * Returns the number of groups active for a particular key.
186  */
187 _X_EXPORT xkb_group_index_t
188 xkb_key_num_groups(struct xkb_keymap *keymap, xkb_keycode_t key)
189 {
190     return XkbKeyNumGroups(keymap, key);
191 }
192
193 /**
194  * Return the total number of active LEDs in the keymap.
195  */
196 _X_EXPORT xkb_led_index_t
197 xkb_map_num_leds(struct xkb_keymap *keymap)
198 {
199     xkb_led_index_t ret = 0;
200     xkb_led_index_t i;
201
202     for (i = 0; i < XkbNumIndicators; i++)
203         if (keymap->indicators->maps[i].which_groups ||
204             keymap->indicators->maps[i].which_mods ||
205             keymap->indicators->maps[i].ctrls)
206             ret++;
207
208     return ret;
209 }
210
211 /**
212  * Returns the name for a given group.
213  */
214 _X_EXPORT const char *
215 xkb_map_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx)
216 {
217     if (idx >= xkb_map_num_leds(keymap))
218         return NULL;
219
220     return keymap->names->indicators[idx];
221 }
222
223 /**
224  * Returns the index for a named group.
225  */
226 _X_EXPORT xkb_group_index_t
227 xkb_map_led_get_index(struct xkb_keymap *keymap, const char *name)
228 {
229     xkb_led_index_t num_leds = xkb_map_num_leds(keymap);
230     xkb_led_index_t i;
231
232     for (i = 0; i < num_leds; i++) {
233         if (strcasecmp(keymap->names->indicators[i], name) == 0)
234             return i;
235     }
236
237     return XKB_LED_INVALID;
238 }
239
240 /**
241  * Returns the level to use for the given key and state, or -1 if invalid.
242  */
243 _X_EXPORT unsigned int
244 xkb_key_get_level(struct xkb_state *state, xkb_keycode_t key,
245                   unsigned int group)
246 {
247     struct xkb_keymap *keymap = state->keymap;
248     struct xkb_key_type *type = XkbKeyType(keymap, key, group);
249     struct xkb_kt_map_entry *entry;
250     unsigned int active_mods = state->mods & type->mods.mask;
251
252     darray_foreach(entry, type->map) {
253         if (!entry->active)
254             continue;
255
256         if (entry->mods.mask == active_mods)
257             return entry->level;
258     }
259
260     return 0;
261 }
262
263 /**
264  * Returns the group to use for the given key and state, taking
265  * wrapping/clamping/etc into account.
266  */
267 _X_EXPORT unsigned int
268 xkb_key_get_group(struct xkb_state *state, xkb_keycode_t key)
269 {
270     struct xkb_keymap *keymap = state->keymap;
271     unsigned int info = XkbKeyGroupInfo(keymap, key);
272     unsigned int num_groups = XkbKeyNumGroups(keymap, key);
273     unsigned int ret = state->group;
274
275     if (ret < XkbKeyNumGroups(keymap, key))
276         return ret;
277
278     switch (XkbOutOfRangeGroupAction(info)) {
279     case XkbRedirectIntoRange:
280         ret = XkbOutOfRangeGroupInfo(info);
281         if (ret >= num_groups)
282             ret = 0;
283         break;
284     case XkbClampIntoRange:
285         ret = num_groups - 1;
286         break;
287     case XkbWrapIntoRange:
288     default:
289         ret %= num_groups;
290         break;
291     }
292
293     return ret;
294 }
295
296 /**
297  * As below, but takes an explicit group/level rather than state.
298  */
299 int
300 xkb_key_get_syms_by_level(struct xkb_keymap *keymap, xkb_keycode_t key,
301                           unsigned int group, unsigned int level,
302                           const xkb_keysym_t **syms_out)
303 {
304     int num_syms;
305
306     if (group >= XkbKeyNumGroups(keymap, key))
307         goto err;
308     if (level >= XkbKeyGroupWidth(keymap, key, group))
309         goto err;
310
311     num_syms = XkbKeyNumSyms(keymap, key, group, level);
312     if (num_syms == 0)
313         goto err;
314
315     *syms_out = XkbKeySymEntry(keymap, key, group, level);
316     return num_syms;
317
318 err:
319     *syms_out = NULL;
320     return 0;
321 }
322
323 /**
324  * Provides the symbols to use for the given key and state.  Returns the
325  * number of symbols pointed to in syms_out.
326  */
327 _X_EXPORT int
328 xkb_key_get_syms(struct xkb_state *state, xkb_keycode_t key,
329                  const xkb_keysym_t **syms_out)
330 {
331     struct xkb_keymap *keymap = state->keymap;
332     int group;
333     int level;
334
335     if (!state || key < keymap->min_key_code || key > keymap->max_key_code)
336         return -1;
337
338     group = xkb_key_get_group(state, key);
339     if (group == -1)
340         goto err;
341     level = xkb_key_get_level(state, key, group);
342     if (level == -1)
343         goto err;
344
345     return xkb_key_get_syms_by_level(keymap, key, group, level, syms_out);
346
347 err:
348     *syms_out = NULL;
349     return 0;
350 }