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