rules: reformat ApplyRule
[profile/ivi/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     unsigned int active_mods = state->mods & type->mods.mask;
250     int i;
251
252     for (i = 0; i < type->map_count; i++) {
253         if (!type->map[i].active)
254             continue;
255         if (type->map[i].mods.mask == active_mods)
256             return type->map[i].level;
257     }
258
259     return 0;
260 }
261
262 /**
263  * Returns the group to use for the given key and state, taking
264  * wrapping/clamping/etc into account.
265  */
266 _X_EXPORT unsigned int
267 xkb_key_get_group(struct xkb_state *state, xkb_keycode_t key)
268 {
269     struct xkb_keymap *keymap = state->keymap;
270     unsigned int info = XkbKeyGroupInfo(keymap, key);
271     unsigned int num_groups = XkbKeyNumGroups(keymap, key);
272     unsigned int ret = state->group;
273
274     if (ret < XkbKeyNumGroups(keymap, key))
275         return ret;
276
277     switch (XkbOutOfRangeGroupAction(info)) {
278     case XkbRedirectIntoRange:
279         ret = XkbOutOfRangeGroupInfo(info);
280         if (ret >= num_groups)
281             ret = 0;
282         break;
283     case XkbClampIntoRange:
284         ret = num_groups - 1;
285         break;
286     case XkbWrapIntoRange:
287     default:
288         ret %= num_groups;
289         break;
290     }
291
292     return ret;
293 }
294
295 /**
296  * As below, but takes an explicit group/level rather than state.
297  */
298 int
299 xkb_key_get_syms_by_level(struct xkb_keymap *keymap, xkb_keycode_t key,
300                           unsigned int group, unsigned int level,
301                           const xkb_keysym_t **syms_out)
302 {
303     int num_syms;
304
305     if (group >= XkbKeyNumGroups(keymap, key))
306         goto err;
307     if (level >= XkbKeyGroupWidth(keymap, key, group))
308         goto err;
309
310     num_syms = XkbKeyNumSyms(keymap, key, group, level);
311     if (num_syms == 0)
312         goto err;
313
314     *syms_out = XkbKeySymEntry(keymap, 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 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 *keymap = state->keymap;
331     int group;
332     int level;
333
334     if (!state || key < keymap->min_key_code || key > keymap->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(keymap, key, group, level, syms_out);
345
346 err:
347     *syms_out = NULL;
348     return 0;
349 }