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