Implicitly include config.h in all files
[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 "utils.h"
53 #include "xkbcommon/xkbcommon.h"
54 #include "XKBcommonint.h"
55 #include "xkballoc.h"
56
57 /**
58  * Returns the total number of modifiers active in the keymap.
59  */
60 _X_EXPORT xkb_mod_index_t
61 xkb_map_num_mods(struct xkb_keymap *xkb)
62 {
63     xkb_mod_index_t i;
64
65     for (i = 0; i < XkbNumVirtualMods; i++)
66         if (!xkb->names->vmods[i])
67             break;
68
69     /* We always have all the core modifiers (for now), plus any virtual
70      * modifiers we may have defined, and then shift to one-based indexing. */
71     return i + Mod5MapIndex + 1;
72 }
73
74 /**
75  * Return the name for a given modifier.
76  */
77 _X_EXPORT const char *
78 xkb_map_mod_get_name(struct xkb_keymap *xkb, xkb_mod_index_t idx)
79 {
80     if (idx >= xkb_map_num_mods(xkb))
81         return NULL;
82
83     /* First try to find a legacy modifier name. */
84     switch (idx) {
85     case ShiftMapIndex:
86         return "Shift";
87     case ControlMapIndex:
88         return "Control";
89     case LockMapIndex:
90         return "Caps Lock";
91     case Mod1MapIndex:
92         return "Mod1";
93     case Mod2MapIndex:
94         return "Mod2";
95     case Mod3MapIndex:
96         return "Mod3";
97     case Mod4MapIndex:
98         return "Mod4";
99     case Mod5MapIndex:
100         return "Mod5";
101     default:
102         break;
103     }
104
105     /* If that fails, try to find a virtual mod name. */
106     return xkb->names->vmods[idx - Mod5MapIndex];
107 }
108
109 /**
110  * Returns the index for a named modifier.
111  */
112 _X_EXPORT xkb_mod_index_t
113 xkb_map_mod_get_index(struct xkb_keymap *xkb, const char *name)
114 {
115     xkb_mod_index_t i;
116
117     if (strcasecmp(name, "Shift") == 0)
118         return ShiftMapIndex;
119     if (strcasecmp(name, "Control") == 0)
120         return ControlMapIndex;
121     if (strcasecmp(name, "Caps Lock") == 0)
122         return LockMapIndex;
123     if (strcasecmp(name, "Mod1") == 0)
124         return Mod1MapIndex;
125     if (strcasecmp(name, "Mod2") == 0)
126         return Mod2MapIndex;
127     if (strcasecmp(name, "Mod3") == 0)
128         return Mod3MapIndex;
129     if (strcasecmp(name, "Mod4") == 0)
130         return Mod4MapIndex;
131     if (strcasecmp(name, "Mod5") == 0)
132         return Mod5MapIndex;
133
134     for (i = 0; i < XkbNumVirtualMods && xkb->names->vmods[i]; i++) {
135         if (strcasecmp(name, xkb->names->vmods[i]) == 0)
136             return i + Mod5MapIndex;
137     }
138
139     return XKB_GROUP_INVALID;
140 }
141
142 /**
143  * Return the total number of active groups in the keymap.
144  */
145 _X_EXPORT xkb_group_index_t
146 xkb_map_num_groups(struct xkb_keymap *xkb)
147 {
148     xkb_group_index_t ret = 0;
149     xkb_group_index_t i;
150
151     for (i = 0; i < XkbNumKbdGroups; i++)
152         if (xkb->compat->groups[i].mask)
153             ret++;
154
155     return ret;
156 }
157
158 /**
159  * Returns the name for a given group.
160  */
161 _X_EXPORT const char *
162 xkb_map_group_get_name(struct xkb_keymap *xkb, xkb_group_index_t idx)
163 {
164     if (idx >= xkb_map_num_groups(xkb))
165         return NULL;
166
167     return xkb->names->groups[idx];
168 }
169
170 /**
171  * Returns the index for a named group.
172  */
173 _X_EXPORT xkb_group_index_t
174 xkb_map_group_get_index(struct xkb_keymap *xkb, const char *name)
175 {
176     xkb_group_index_t num_groups = xkb_map_num_groups(xkb);
177     xkb_group_index_t i;
178
179     for (i = 0; i < num_groups; i++) {
180         if (strcasecmp(xkb->names->groups[i], name) == 0)
181             return i;
182     }
183
184     return XKB_GROUP_INVALID;
185 }
186
187 /**
188  * Returns the number of groups active for a particular key.
189  */
190 _X_EXPORT xkb_group_index_t
191 xkb_key_num_groups(struct xkb_keymap *xkb, xkb_keycode_t key)
192 {
193     return XkbKeyNumGroups(xkb, key);
194 }
195
196 /**
197  * Return the total number of active LEDs in the keymap.
198  */
199 _X_EXPORT xkb_led_index_t
200 xkb_map_num_leds(struct xkb_keymap *xkb)
201 {
202     xkb_led_index_t ret = 0;
203     xkb_led_index_t i;
204
205     for (i = 0; i < XkbNumIndicators; i++)
206         if (xkb->indicators->maps[i].which_groups ||
207             xkb->indicators->maps[i].which_mods ||
208             xkb->indicators->maps[i].ctrls)
209             ret++;
210
211     return ret;
212 }
213
214 /**
215  * Returns the name for a given group.
216  */
217 _X_EXPORT const char *
218 xkb_map_led_get_name(struct xkb_keymap *xkb, xkb_led_index_t idx)
219 {
220     if (idx >= xkb_map_num_leds(xkb))
221         return NULL;
222
223     return xkb->names->indicators[idx];
224 }
225
226 /**
227  * Returns the index for a named group.
228  */
229 _X_EXPORT xkb_group_index_t
230 xkb_map_led_get_index(struct xkb_keymap *xkb, const char *name)
231 {
232     xkb_led_index_t num_leds = xkb_map_num_leds(xkb);
233     xkb_led_index_t i;
234
235     for (i = 0; i < num_leds; i++) {
236         if (strcasecmp(xkb->names->indicators[i], name) == 0)
237             return i;
238     }
239
240     return XKB_LED_INVALID;
241 }
242
243 /**
244  * Returns the level to use for the given key and state, or -1 if invalid.
245  */
246 _X_EXPORT unsigned int
247 xkb_key_get_level(struct xkb_state *state, xkb_keycode_t key,
248                   unsigned int group)
249 {
250     struct xkb_key_type *type = XkbKeyType(state->xkb, key, group);
251     unsigned int active_mods = state->mods & type->mods.mask;
252     int i;
253
254     for (i = 0; i < type->map_count; i++) {
255         if (!type->map[i].active)
256             continue;
257         if (type->map[i].mods.mask == active_mods)
258             return type->map[i].level;
259     }
260
261     return 0;
262 }
263
264 /**
265  * Returns the group to use for the given key and state, taking
266  * wrapping/clamping/etc into account.
267  */
268 _X_EXPORT unsigned int
269 xkb_key_get_group(struct xkb_state *state, xkb_keycode_t key)
270 {
271     unsigned int info = XkbKeyGroupInfo(state->xkb, key);
272     unsigned int num_groups = XkbKeyNumGroups(state->xkb, key);
273     unsigned int ret = state->group;
274
275     if (ret < XkbKeyNumGroups(state->xkb, 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 _X_EXPORT unsigned int
300 xkb_key_get_syms_by_level(struct xkb_keymap *xkb, xkb_keycode_t key, unsigned int group,
301                           unsigned int level, const xkb_keysym_t **syms_out)
302 {
303     int num_syms;
304
305     if (group >= XkbKeyNumGroups(xkb, key))
306         goto err;
307     if (level >= XkbKeyGroupWidth(xkb, key, group))
308         goto err;
309
310     num_syms = XkbKeyNumSyms(xkb, key, group, level);
311     if (num_syms == 0)
312         goto err;
313
314     *syms_out = XkbKeySymEntry(xkb, key, group, level);
315     return num_syms;
316
317 err:
318     *syms_out = NULL;
319     return 0;
320 }
321
322 /**
323  * Provides the symbols to use for the given key and state.  Returns the
324  * number of symbols pointed to in syms_out.
325  */
326 _X_EXPORT unsigned int
327 xkb_key_get_syms(struct xkb_state *state, xkb_keycode_t key,
328                  const xkb_keysym_t **syms_out)
329 {
330     struct xkb_keymap *xkb = state->xkb;
331     int group;
332     int level;
333
334     if (!state || key < xkb->min_key_code || key > xkb->max_key_code)
335         return -1;
336
337     group = xkb_key_get_group(state, key);
338     if (group == -1)
339         goto err;
340     level = xkb_key_get_level(state, key, group);
341     if (level == -1)
342         goto err;
343
344     return xkb_key_get_syms_by_level(xkb, key, group, level, syms_out);
345
346 err:
347     *syms_out = NULL;
348     return 0;
349 }