kbproto unentanglement: XkbNumIndicators
[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 = xkb_atom_text(keymap->ctx,
141                              keymap->vmod_names[idx - XkbNumModifiers]);
142
143     return name;
144 }
145
146 /**
147  * Returns the index for a named modifier.
148  */
149 XKB_EXPORT xkb_mod_index_t
150 xkb_map_mod_get_index(struct xkb_keymap *keymap, const char *name)
151 {
152     xkb_mod_index_t i;
153     xkb_atom_t atom;
154
155     i = ModNameToIndex(name);
156     if (i != XKB_MOD_INVALID)
157         return i;
158
159     atom = xkb_atom_lookup(keymap->ctx, name);
160     if (atom == XKB_ATOM_NONE)
161         return XKB_MOD_INVALID;
162
163     for (i = 0; i < XkbNumVirtualMods; i++) {
164         if (keymap->vmod_names[i] == XKB_ATOM_NONE)
165             break;
166         if (keymap->vmod_names[i] == atom)
167             return i + XkbNumModifiers;
168     }
169
170     return XKB_MOD_INVALID;
171 }
172
173 /**
174  * Return the total number of active groups in the keymap.
175  */
176 XKB_EXPORT xkb_group_index_t
177 xkb_map_num_groups(struct xkb_keymap *keymap)
178 {
179     return keymap->num_groups;
180 }
181
182 /**
183  * Returns the name for a given group.
184  */
185 XKB_EXPORT const char *
186 xkb_map_group_get_name(struct xkb_keymap *keymap, xkb_group_index_t idx)
187 {
188     if (idx >= xkb_map_num_groups(keymap))
189         return NULL;
190
191     return xkb_atom_text(keymap->ctx, keymap->group_names[idx]);
192 }
193
194 /**
195  * Returns the index for a named group.
196  */
197 XKB_EXPORT xkb_group_index_t
198 xkb_map_group_get_index(struct xkb_keymap *keymap, const char *name)
199 {
200     xkb_group_index_t num_groups = xkb_map_num_groups(keymap);
201     xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
202     xkb_group_index_t i;
203
204     if (atom == XKB_ATOM_NONE)
205         return XKB_GROUP_INVALID;
206
207     for (i = 0; i < num_groups; i++)
208         if (keymap->group_names[i] == atom)
209             return i;
210
211     return XKB_GROUP_INVALID;
212 }
213
214 /**
215  * Returns the number of groups active for a particular key.
216  */
217 XKB_EXPORT xkb_group_index_t
218 xkb_key_num_groups(struct xkb_keymap *keymap, xkb_keycode_t kc)
219 {
220     if (!XkbKeycodeInRange(keymap, kc))
221         return 0;
222
223     return XkbKey(keymap, kc)->num_groups;
224 }
225
226 /**
227  * Return the total number of active LEDs in the keymap.
228  */
229 XKB_EXPORT xkb_led_index_t
230 xkb_map_num_leds(struct xkb_keymap *keymap)
231 {
232     xkb_led_index_t ret = 0;
233     xkb_led_index_t i;
234
235     for (i = 0; i < XKB_NUM_INDICATORS; i++)
236         if (keymap->indicators[i].which_groups ||
237             keymap->indicators[i].which_mods ||
238             keymap->indicators[i].ctrls)
239             ret++;
240
241     return ret;
242 }
243
244 /**
245  * Returns the name for a given group.
246  */
247 XKB_EXPORT const char *
248 xkb_map_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx)
249 {
250     if (idx >= xkb_map_num_leds(keymap))
251         return NULL;
252
253     return xkb_atom_text(keymap->ctx, keymap->indicators[idx].name);
254 }
255
256 /**
257  * Returns the index for a named group.
258  */
259 XKB_EXPORT xkb_group_index_t
260 xkb_map_led_get_index(struct xkb_keymap *keymap, const char *name)
261 {
262     xkb_led_index_t num_leds = xkb_map_num_leds(keymap);
263     xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
264     xkb_led_index_t i;
265
266     if (atom == XKB_ATOM_NONE)
267         return XKB_LED_INVALID;
268
269     for (i = 0; i < num_leds; i++)
270         if (keymap->indicators[i].name == atom)
271             return i;
272
273     return XKB_LED_INVALID;
274 }
275
276 static struct xkb_kt_map_entry *
277 get_entry_for_key_state(struct xkb_state *state, xkb_keycode_t kc)
278 {
279     struct xkb_keymap *keymap = xkb_state_get_map(state);
280     xkb_group_index_t group;
281     struct xkb_key_type *type;
282     xkb_mod_mask_t active_mods;
283     unsigned int i;
284
285     group = xkb_key_get_group(state, kc);
286     if (group == XKB_GROUP_INVALID)
287         return NULL;
288
289     type = XkbKeyType(keymap, XkbKey(keymap, kc), group);
290     active_mods = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
291     active_mods &= type->mods.mask;
292
293     for (i = 0; i < type->num_entries; i++)
294         if (type->map[i].mods.mask == active_mods)
295             return &type->map[i];
296
297     return NULL;
298 }
299
300 /**
301  * Returns the level to use for the given key and state, or
302  * XKB_LEVEL_INVALID.
303  */
304 xkb_level_index_t
305 xkb_key_get_level(struct xkb_state *state, xkb_keycode_t kc,
306                   xkb_group_index_t group)
307 {
308     struct xkb_kt_map_entry *entry;
309
310     /* If we don't find an explicit match the default is 0. */
311     entry = get_entry_for_key_state(state, kc);
312     if (!entry)
313         return 0;
314
315     return entry->level;
316 }
317
318 /**
319  * Returns the group to use for the given key and state, taking
320  * wrapping/clamping/etc into account, or XKB_GROUP_INVALID.
321  */
322 xkb_group_index_t
323 xkb_key_get_group(struct xkb_state *state, xkb_keycode_t kc)
324 {
325     struct xkb_keymap *keymap = xkb_state_get_map(state);
326     struct xkb_key *key;
327     xkb_group_index_t ret = xkb_state_serialize_group(state,
328                                                       XKB_STATE_EFFECTIVE);
329
330     key = XkbKey(keymap, kc);
331     if (key->num_groups == 0)
332         return XKB_GROUP_INVALID;
333
334     if (ret < key->num_groups)
335         return ret;
336
337     switch (key->out_of_range_group_action) {
338     case XkbRedirectIntoRange:
339         ret = key->out_of_range_group_number;
340         if (ret >= key->num_groups)
341             ret = 0;
342         break;
343
344     case XkbClampIntoRange:
345         ret = key->num_groups - 1;
346         break;
347
348     case XkbWrapIntoRange:
349     default:
350         ret %= key->num_groups;
351         break;
352     }
353
354     return ret;
355 }
356
357 /**
358  * As below, but takes an explicit group/level rather than state.
359  */
360 int
361 xkb_key_get_syms_by_level(struct xkb_keymap *keymap, struct xkb_key *key,
362                           xkb_group_index_t group, xkb_level_index_t level,
363                           const xkb_keysym_t **syms_out)
364 {
365     int num_syms;
366
367     if (group >= key->num_groups)
368         goto err;
369     if (level >= XkbKeyGroupWidth(keymap, key, group))
370         goto err;
371
372     num_syms = XkbKeyNumSyms(key, group, level);
373     if (num_syms == 0)
374         goto err;
375
376     *syms_out = XkbKeySymEntry(key, group, level);
377     return num_syms;
378
379 err:
380     *syms_out = NULL;
381     return 0;
382 }
383
384 /**
385  * Provides the symbols to use for the given key and state.  Returns the
386  * number of symbols pointed to in syms_out.
387  */
388 XKB_EXPORT int
389 xkb_key_get_syms(struct xkb_state *state, xkb_keycode_t kc,
390                  const xkb_keysym_t **syms_out)
391 {
392     struct xkb_keymap *keymap = xkb_state_get_map(state);
393     struct xkb_key *key;
394     xkb_group_index_t group;
395     xkb_level_index_t level;
396
397     if (!XkbKeycodeInRange(keymap, kc))
398         return -1;
399
400     key = XkbKey(keymap, kc);
401
402     group = xkb_key_get_group(state, kc);
403     if (group == XKB_GROUP_INVALID)
404         goto err;
405
406     level = xkb_key_get_level(state, kc, group);
407     if (level == XKB_LEVEL_INVALID)
408         goto err;
409
410     return xkb_key_get_syms_by_level(keymap, key, group, level, syms_out);
411
412 err:
413     *syms_out = NULL;
414     return 0;
415 }
416
417 /**
418  * Simple boolean specifying whether or not the key should repeat.
419  */
420 XKB_EXPORT int
421 xkb_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t kc)
422 {
423     if (!XkbKeycodeInRange(keymap, kc))
424         return 0;
425
426     return XkbKey(keymap, kc)->repeats;
427 }
428
429 static xkb_mod_mask_t
430 key_get_consumed(struct xkb_state *state, xkb_keycode_t kc)
431 {
432     struct xkb_kt_map_entry *entry;
433
434     entry = get_entry_for_key_state(state, kc);
435     if (!entry)
436         return 0;
437
438     return entry->mods.mask & ~entry->preserve.mask;
439 }
440
441 /**
442  * Tests to see if a modifier is used up by our translation of a
443  * keycode to keysyms, taking note of the current modifier state and
444  * the appropriate key type's preserve information, if any. This allows
445  * the user to mask out the modifier in later processing of the
446  * modifiers, e.g. when implementing hot keys or accelerators.
447  *
448  * See also, for example:
449  * - XkbTranslateKeyCode(3), mod_rtrn retrun value, from libX11.
450  * - gdk_keymap_translate_keyboard_state, consumed_modifiers return value,
451  *   from gtk+.
452  */
453 XKB_EXPORT int
454 xkb_key_mod_index_is_consumed(struct xkb_state *state, xkb_keycode_t kc,
455                               xkb_mod_index_t idx)
456 {
457     if (!XkbKeycodeInRange(xkb_state_get_map(state), kc))
458         return 0;
459
460     return !!((1 << idx) & key_get_consumed(state, kc));
461 }
462
463 /**
464  * Calculates which modifiers should be consumed during key processing,
465  * and returns the mask with all these modifiers removed.  e.g. if
466  * given a state of Alt and Shift active for a two-level alphabetic
467  * key containing plus and equal on the first and second level
468  * respectively, will return a mask of only Alt, as Shift has been
469  * consumed by the type handling.
470  */
471 XKB_EXPORT xkb_mod_mask_t
472 xkb_key_mod_mask_remove_consumed(struct xkb_state *state, xkb_keycode_t kc,
473                                  xkb_mod_mask_t mask)
474 {
475     if (!XkbKeycodeInRange(xkb_state_get_map(state), kc))
476         return mask;
477
478     return mask & ~key_get_consumed(state, kc);
479 }