Organize xkbcomp/ header files
[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 struct xkb_keymap *
56 xkb_map_new(struct xkb_context *ctx)
57 {
58     struct xkb_keymap *keymap;
59
60     keymap = calloc(1, sizeof(*keymap));
61     if (!keymap)
62         return NULL;
63
64     keymap->refcnt = 1;
65     keymap->ctx = xkb_context_ref(ctx);
66
67     return keymap;
68 }
69
70 XKB_EXPORT struct xkb_keymap *
71 xkb_map_ref(struct xkb_keymap *keymap)
72 {
73     keymap->refcnt++;
74     return keymap;
75 }
76
77 XKB_EXPORT void
78 xkb_map_unref(struct xkb_keymap *keymap)
79 {
80     unsigned int i;
81     struct xkb_key *key;
82
83     if (!keymap || --keymap->refcnt > 0)
84         return;
85
86     for (i = 0; i < keymap->num_types; i++) {
87         free(keymap->types[i].map);
88         free(keymap->types[i].level_names);
89     }
90     free(keymap->types);
91     darray_foreach(key, keymap->keys) {
92         free(key->sym_index);
93         free(key->num_syms);
94         darray_free(key->syms);
95         free(key->actions);
96     }
97     darray_free(keymap->keys);
98     darray_free(keymap->sym_interpret);
99     darray_free(keymap->key_aliases);
100     free(keymap->keycodes_section_name);
101     free(keymap->symbols_section_name);
102     free(keymap->types_section_name);
103     free(keymap->compat_section_name);
104     xkb_context_unref(keymap->ctx);
105     free(keymap);
106 }
107
108 /**
109  * Returns the total number of modifiers active in the keymap.
110  */
111 XKB_EXPORT xkb_mod_index_t
112 xkb_map_num_mods(struct xkb_keymap *keymap)
113 {
114     xkb_mod_index_t i;
115
116     for (i = 0; i < XkbNumVirtualMods; i++)
117         if (!keymap->vmod_names[i])
118             break;
119
120     /* We always have all the core modifiers (for now), plus any virtual
121      * modifiers we may have defined. */
122     return i + XkbNumModifiers;
123 }
124
125 /**
126  * Return the name for a given modifier.
127  */
128 XKB_EXPORT const char *
129 xkb_map_mod_get_name(struct xkb_keymap *keymap, xkb_mod_index_t idx)
130 {
131     const char *name;
132
133     if (idx >= xkb_map_num_mods(keymap))
134         return NULL;
135
136     /* First try to find a legacy modifier name.  If that fails, try to
137      * find a virtual mod name. */
138     name = ModIndexToName(idx);
139     if (!name)
140         name = keymap->vmod_names[idx - XkbNumModifiers];
141
142     return name;
143 }
144
145 /**
146  * Returns the index for a named modifier.
147  */
148 XKB_EXPORT xkb_mod_index_t
149 xkb_map_mod_get_index(struct xkb_keymap *keymap, const char *name)
150 {
151     xkb_mod_index_t i;
152
153     i = ModNameToIndex(name);
154     if (i != XKB_MOD_INVALID)
155         return i;
156
157     for (i = 0; i < XkbNumVirtualMods && keymap->vmod_names[i]; i++) {
158         if (istreq(name, keymap->vmod_names[i]))
159             return i + XkbNumModifiers;
160     }
161
162     return XKB_MOD_INVALID;
163 }
164
165 /**
166  * Return the total number of active groups in the keymap.
167  */
168 XKB_EXPORT xkb_group_index_t
169 xkb_map_num_groups(struct xkb_keymap *keymap)
170 {
171     xkb_group_index_t ret = 0;
172     xkb_group_index_t i;
173
174     for (i = 0; i < XkbNumKbdGroups; i++)
175         if (keymap->groups[i].mask)
176             ret++;
177
178     return ret;
179 }
180
181 /**
182  * Returns the name for a given group.
183  */
184 XKB_EXPORT const char *
185 xkb_map_group_get_name(struct xkb_keymap *keymap, xkb_group_index_t idx)
186 {
187     if (idx >= xkb_map_num_groups(keymap))
188         return NULL;
189
190     return keymap->group_names[idx];
191 }
192
193 /**
194  * Returns the index for a named group.
195  */
196 XKB_EXPORT xkb_group_index_t
197 xkb_map_group_get_index(struct xkb_keymap *keymap, const char *name)
198 {
199     xkb_group_index_t num_groups = xkb_map_num_groups(keymap);
200     xkb_group_index_t i;
201
202     for (i = 0; i < num_groups; i++)
203         if (istreq(keymap->group_names[i], name))
204             return i;
205
206     return XKB_GROUP_INVALID;
207 }
208
209 /**
210  * Returns the number of groups active for a particular key.
211  */
212 XKB_EXPORT xkb_group_index_t
213 xkb_key_num_groups(struct xkb_keymap *keymap, xkb_keycode_t kc)
214 {
215     if (XkbKeycodeInRange(keymap, kc))
216         return XkbKey(keymap, kc)->num_groups;
217     return 0;
218 }
219
220 /**
221  * Return the total number of active LEDs in the keymap.
222  */
223 XKB_EXPORT xkb_led_index_t
224 xkb_map_num_leds(struct xkb_keymap *keymap)
225 {
226     xkb_led_index_t ret = 0;
227     xkb_led_index_t i;
228
229     for (i = 0; i < XkbNumIndicators; i++)
230         if (keymap->indicators[i].which_groups ||
231             keymap->indicators[i].which_mods ||
232             keymap->indicators[i].ctrls)
233             ret++;
234
235     return ret;
236 }
237
238 /**
239  * Returns the name for a given group.
240  */
241 XKB_EXPORT const char *
242 xkb_map_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx)
243 {
244     if (idx >= xkb_map_num_leds(keymap))
245         return NULL;
246
247     return keymap->indicator_names[idx];
248 }
249
250 /**
251  * Returns the index for a named group.
252  */
253 XKB_EXPORT xkb_group_index_t
254 xkb_map_led_get_index(struct xkb_keymap *keymap, const char *name)
255 {
256     xkb_led_index_t num_leds = xkb_map_num_leds(keymap);
257     xkb_led_index_t i;
258
259     for (i = 0; i < num_leds; i++)
260         if (istreq(keymap->indicator_names[i], name))
261             return i;
262
263     return XKB_LED_INVALID;
264 }
265
266 static struct xkb_kt_map_entry *
267 get_entry_for_key_state(struct xkb_state *state, xkb_keycode_t kc)
268 {
269     struct xkb_keymap *keymap = xkb_state_get_map(state);
270     xkb_group_index_t group;
271     struct xkb_key_type *type;
272     xkb_mod_mask_t active_mods;
273     unsigned int i;
274
275     group = xkb_key_get_group(state, kc);
276     if (group == XKB_GROUP_INVALID)
277         return NULL;
278
279     type = XkbKeyType(keymap, XkbKey(keymap, kc), group);
280     active_mods = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
281     active_mods &= type->mods.mask;
282
283     for (i = 0; i < type->num_entries; i++)
284         if (type->map[i].mods.mask == active_mods)
285             return &type->map[i];
286
287     return NULL;
288 }
289
290 /**
291  * Returns the level to use for the given key and state, or
292  * XKB_LEVEL_INVALID.
293  */
294 xkb_level_index_t
295 xkb_key_get_level(struct xkb_state *state, xkb_keycode_t kc,
296                   xkb_group_index_t group)
297 {
298     struct xkb_kt_map_entry *entry;
299
300     if (!XkbKeycodeInRange(xkb_state_get_map(state), kc))
301         return XKB_LEVEL_INVALID;
302
303     entry = get_entry_for_key_state(state, kc);
304
305     /* If we don't find an explicit match the default is 0. */
306     if (!entry)
307         return 0;
308
309     return entry->level;
310 }
311
312 /**
313  * Returns the group to use for the given key and state, taking
314  * wrapping/clamping/etc into account, or XKB_GROUP_INVALID.
315  */
316 xkb_group_index_t
317 xkb_key_get_group(struct xkb_state *state, xkb_keycode_t kc)
318 {
319     struct xkb_keymap *keymap = xkb_state_get_map(state);
320     xkb_group_index_t ret = xkb_state_serialize_group(state,
321                                                       XKB_STATE_EFFECTIVE);
322     struct xkb_key *key;
323
324     if (!XkbKeycodeInRange(keymap, kc))
325         return XKB_GROUP_INVALID;
326
327     key = XkbKey(keymap, kc);
328     if (key->num_groups == 0)
329         return XKB_GROUP_INVALID;
330
331     if (ret < key->num_groups)
332         return ret;
333
334     switch (key->out_of_range_group_action) {
335     case XkbRedirectIntoRange:
336         ret = key->out_of_range_group_number;
337         if (ret >= key->num_groups)
338             ret = 0;
339         break;
340
341     case XkbClampIntoRange:
342         ret = key->num_groups - 1;
343         break;
344
345     case XkbWrapIntoRange:
346     default:
347         ret %= key->num_groups;
348         break;
349     }
350
351     return ret;
352 }
353
354 /**
355  * As below, but takes an explicit group/level rather than state.
356  */
357 int
358 xkb_key_get_syms_by_level(struct xkb_keymap *keymap, struct xkb_key *key,
359                           xkb_group_index_t group, xkb_level_index_t level,
360                           const xkb_keysym_t **syms_out)
361 {
362     int num_syms;
363
364     if (group >= key->num_groups)
365         goto err;
366     if (level >= XkbKeyGroupWidth(keymap, key, group))
367         goto err;
368
369     num_syms = XkbKeyNumSyms(key, group, level);
370     if (num_syms == 0)
371         goto err;
372
373     *syms_out = XkbKeySymEntry(key, group, level);
374     return num_syms;
375
376 err:
377     *syms_out = NULL;
378     return 0;
379 }
380
381 /**
382  * Provides the symbols to use for the given key and state.  Returns the
383  * number of symbols pointed to in syms_out.
384  */
385 XKB_EXPORT int
386 xkb_key_get_syms(struct xkb_state *state, xkb_keycode_t kc,
387                  const xkb_keysym_t **syms_out)
388 {
389     struct xkb_keymap *keymap = xkb_state_get_map(state);
390     struct xkb_key *key;
391     xkb_group_index_t group;
392     xkb_level_index_t level;
393
394     if (!state || !XkbKeycodeInRange(keymap, kc))
395         return -1;
396
397     key = XkbKey(keymap, kc);
398
399     group = xkb_key_get_group(state, kc);
400     if (group == XKB_GROUP_INVALID)
401         goto err;
402
403     level = xkb_key_get_level(state, kc, group);
404     if (level == XKB_LEVEL_INVALID)
405         goto err;
406
407     return xkb_key_get_syms_by_level(keymap, key, group, level, syms_out);
408
409 err:
410     *syms_out = NULL;
411     return 0;
412 }
413
414 /**
415  * Simple boolean specifying whether or not the key should repeat.
416  */
417 XKB_EXPORT int
418 xkb_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t kc)
419 {
420     if (!XkbKeycodeInRange(keymap, kc))
421         return 0;
422     return XkbKey(keymap, kc)->repeats;
423 }
424
425 static xkb_mod_mask_t
426 key_get_consumed(struct xkb_state *state, xkb_keycode_t kc)
427 {
428     struct xkb_kt_map_entry *entry;
429
430     entry = get_entry_for_key_state(state, kc);
431     if (!entry)
432         return 0;
433
434     return entry->mods.mask & ~entry->preserve.mask;
435 }
436
437 /**
438  * Tests to see if a modifier is used up by our translation of a
439  * keycode to keysyms, taking note of the current modifier state and
440  * the appropriate key type's preserve information, if any. This allows
441  * the user to mask out the modifier in later processing of the
442  * modifiers, e.g. when implementing hot keys or accelerators.
443  *
444  * See also, for example:
445  * - XkbTranslateKeyCode(3), mod_rtrn retrun value, from libX11.
446  * - gdk_keymap_translate_keyboard_state, consumed_modifiers return value,
447  *   from gtk+.
448  */
449 XKB_EXPORT int
450 xkb_key_mod_index_is_consumed(struct xkb_state *state, xkb_keycode_t kc,
451                               xkb_mod_index_t idx)
452 {
453     if (!XkbKeycodeInRange(xkb_state_get_map(state), kc))
454         return 0;
455
456     return !!((1 << idx) & key_get_consumed(state, kc));
457 }
458
459 /**
460  * Calculates which modifiers should be consumed during key processing,
461  * and returns the mask with all these modifiers removed.  e.g. if
462  * given a state of Alt and Shift active for a two-level alphabetic
463  * key containing plus and equal on the first and second level
464  * respectively, will return a mask of only Alt, as Shift has been
465  * consumed by the type handling.
466  */
467 XKB_EXPORT xkb_mod_mask_t
468 xkb_key_mod_mask_remove_consumed(struct xkb_state *state, xkb_keycode_t kc,
469                                  xkb_mod_mask_t mask)
470 {
471     if (!XkbKeycodeInRange(xkb_state_get_map(state), kc))
472         return mask;
473
474     return mask & ~key_get_consumed(state, kc);
475 }