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