utils: remove Xfuncproto.h and use our own macros
[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 XKB_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->vmod_names[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 XKB_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->vmod_names[idx - Mod5MapIndex];
104 }
105
106 /**
107  * Returns the index for a named modifier.
108  */
109 XKB_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->vmod_names[i]; i++) {
132         if (strcasecmp(name, keymap->vmod_names[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 XKB_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->groups[i].mask)
150             ret++;
151
152     return ret;
153 }
154
155 /**
156  * Returns the name for a given group.
157  */
158 XKB_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->group_names[idx];
165 }
166
167 /**
168  * Returns the index for a named group.
169  */
170 XKB_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->group_names[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 XKB_EXPORT xkb_group_index_t
188 xkb_key_num_groups(struct xkb_keymap *keymap, xkb_keycode_t kc)
189 {
190     if (XkbKeycodeInRange(keymap, kc))
191         return XkbKey(keymap, kc)->num_groups;
192     return 0;
193 }
194
195 /**
196  * Return the total number of active LEDs in the keymap.
197  */
198 XKB_EXPORT xkb_led_index_t
199 xkb_map_num_leds(struct xkb_keymap *keymap)
200 {
201     xkb_led_index_t ret = 0;
202     xkb_led_index_t i;
203
204     for (i = 0; i < XkbNumIndicators; i++)
205         if (keymap->indicators[i].which_groups ||
206             keymap->indicators[i].which_mods ||
207             keymap->indicators[i].ctrls)
208             ret++;
209
210     return ret;
211 }
212
213 /**
214  * Returns the name for a given group.
215  */
216 XKB_EXPORT const char *
217 xkb_map_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx)
218 {
219     if (idx >= xkb_map_num_leds(keymap))
220         return NULL;
221
222     return keymap->indicator_names[idx];
223 }
224
225 /**
226  * Returns the index for a named group.
227  */
228 XKB_EXPORT xkb_group_index_t
229 xkb_map_led_get_index(struct xkb_keymap *keymap, const char *name)
230 {
231     xkb_led_index_t num_leds = xkb_map_num_leds(keymap);
232     xkb_led_index_t i;
233
234     for (i = 0; i < num_leds; i++) {
235         if (strcasecmp(keymap->indicator_names[i], name) == 0)
236             return i;
237     }
238
239     return XKB_LED_INVALID;
240 }
241
242 /**
243  * Returns the level to use for the given key and state, or -1 if invalid.
244  */
245 unsigned int
246 xkb_key_get_level(struct xkb_state *state, xkb_keycode_t kc,
247                   xkb_group_index_t group)
248 {
249     struct xkb_keymap *keymap = xkb_state_get_map(state);
250     struct xkb_key_type *type;
251     struct xkb_kt_map_entry *entry;
252     unsigned int active_mods;
253
254     if (!XkbKeycodeInRange(keymap, kc))
255         return 0;
256
257     type = XkbKeyType(keymap, XkbKey(keymap, kc), group);
258     active_mods = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
259     active_mods &= type->mods.mask;
260
261     darray_foreach(entry, type->map) {
262         if (entry->mods.mask == active_mods)
263             return entry->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 xkb_group_index_t
274 xkb_key_get_group(struct xkb_state *state, xkb_keycode_t kc)
275 {
276     struct xkb_keymap *keymap = xkb_state_get_map(state);
277     xkb_group_index_t ret = xkb_state_serialize_group(state,
278                                                       XKB_STATE_EFFECTIVE);
279     struct xkb_key *key;
280
281     if (!XkbKeycodeInRange(keymap, kc))
282         return 0;
283     key = XkbKey(keymap, kc);
284
285     if (ret < key->num_groups)
286         return ret;
287
288     switch (key->out_of_range_group_action) {
289     case XkbRedirectIntoRange:
290         ret = key->out_of_range_group_number;
291         if (ret >= key->num_groups)
292             ret = 0;
293         break;
294
295     case XkbClampIntoRange:
296         ret = key->num_groups - 1;
297         break;
298
299     case XkbWrapIntoRange:
300     default:
301         ret %= key->num_groups;
302         break;
303     }
304
305     return ret;
306 }
307
308 /**
309  * As below, but takes an explicit group/level rather than state.
310  */
311 int
312 xkb_key_get_syms_by_level(struct xkb_keymap *keymap, struct xkb_key *key,
313                           xkb_group_index_t group, unsigned int level,
314                           const xkb_keysym_t **syms_out)
315 {
316     int num_syms;
317
318     if (group >= key->num_groups)
319         goto err;
320     if (level >= XkbKeyGroupWidth(keymap, key, group))
321         goto err;
322
323     num_syms = XkbKeyNumSyms(key, group, level);
324     if (num_syms == 0)
325         goto err;
326
327     *syms_out = XkbKeySymEntry(key, group, level);
328     return num_syms;
329
330 err:
331     *syms_out = NULL;
332     return 0;
333 }
334
335 /**
336  * Provides the symbols to use for the given key and state.  Returns the
337  * number of symbols pointed to in syms_out.
338  */
339 XKB_EXPORT int
340 xkb_key_get_syms(struct xkb_state *state, xkb_keycode_t kc,
341                  const xkb_keysym_t **syms_out)
342 {
343     struct xkb_keymap *keymap = xkb_state_get_map(state);
344     struct xkb_key *key;
345     xkb_group_index_t group;
346     unsigned int level;
347
348     if (!state || !XkbKeycodeInRange(keymap, kc))
349         return -1;
350
351     key = XkbKey(keymap, kc);
352
353     group = xkb_key_get_group(state, kc);
354     if (group == -1)
355         goto err;
356
357     level = xkb_key_get_level(state, kc, group);
358     if (level == -1)
359         goto err;
360
361     return xkb_key_get_syms_by_level(keymap, key, group, level, syms_out);
362
363 err:
364     *syms_out = NULL;
365     return 0;
366 }
367
368 /**
369  * Simple boolean specifying whether or not the key should repeat.
370  */
371 XKB_EXPORT int
372 xkb_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t kc)
373 {
374     if (!XkbKeycodeInRange(keymap, kc))
375         return 0;
376     return XkbKey(keymap, kc)->repeats;
377 }