2 * Copyright © 2012 Intel Corporation
3 * Copyright © 2012 Ran Benita <ran234@gmail.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
24 * Author: Daniel Stone <daniel@fooishbar.org>
27 /************************************************************
28 * Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
30 * Permission to use, copy, modify, and distribute this
31 * software and its documentation for any purpose and without
32 * fee is hereby granted, provided that the above copyright
33 * notice appear in all copies and that both that copyright
34 * notice and this permission notice appear in supporting
35 * documentation, and that the name of Silicon Graphics not be
36 * used in advertising or publicity pertaining to distribution
37 * of the software without specific prior written permission.
38 * Silicon Graphics makes no representation about the suitability
39 * of this software for any purpose. It is provided "as is"
40 * without any express or implied warranty.
42 * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
43 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
44 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
45 * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
47 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
48 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
49 * THE USE OR PERFORMANCE OF THIS SOFTWARE.
51 * ********************************************************/
58 XKB_EXPORT struct xkb_keymap *
59 xkb_keymap_ref(struct xkb_keymap *keymap)
66 xkb_keymap_unref(struct xkb_keymap *keymap)
68 if (!keymap || --keymap->refcnt > 0)
73 xkb_keys_foreach(key, keymap) {
75 for (unsigned i = 0; i < key->num_groups; i++) {
76 if (key->groups[i].levels) {
77 for (unsigned j = 0; j < XkbKeyNumLevels(key, i); j++)
78 if (key->groups[i].levels[j].num_syms > 1)
79 free(key->groups[i].levels[j].u.syms);
80 free(key->groups[i].levels);
89 for (unsigned i = 0; i < keymap->num_types; i++) {
90 free(keymap->types[i].entries);
91 free(keymap->types[i].level_names);
95 free(keymap->sym_interprets);
96 free(keymap->key_aliases);
97 free(keymap->group_names);
98 free(keymap->keycodes_section_name);
99 free(keymap->symbols_section_name);
100 free(keymap->types_section_name);
101 free(keymap->compat_section_name);
102 xkb_context_unref(keymap->ctx);
106 static const struct xkb_keymap_format_ops *
107 get_keymap_format_ops(enum xkb_keymap_format format)
109 static const struct xkb_keymap_format_ops *keymap_format_ops[] = {
110 [XKB_KEYMAP_FORMAT_TEXT_V1] = &text_v1_keymap_format_ops,
113 if ((int) format < 0 || (int) format >= (int) ARRAY_SIZE(keymap_format_ops))
116 return keymap_format_ops[(int) format];
119 XKB_EXPORT struct xkb_keymap *
120 xkb_keymap_new_from_names(struct xkb_context *ctx,
121 const struct xkb_rule_names *rmlvo_in,
122 enum xkb_keymap_compile_flags flags)
124 struct xkb_keymap *keymap;
125 struct xkb_rule_names rmlvo;
126 const enum xkb_keymap_format format = XKB_KEYMAP_FORMAT_TEXT_V1;
127 const struct xkb_keymap_format_ops *ops;
129 ops = get_keymap_format_ops(format);
130 if (!ops || !ops->keymap_new_from_names) {
131 log_err_func(ctx, "unsupported keymap format: %d\n", format);
135 if (flags & ~(XKB_KEYMAP_COMPILE_NO_FLAGS)) {
136 log_err_func(ctx, "unrecognized flags: %#x\n", flags);
140 keymap = xkb_keymap_new(ctx, format, flags);
147 memset(&rmlvo, 0, sizeof(rmlvo));
148 xkb_context_sanitize_rule_names(ctx, &rmlvo);
150 if (!ops->keymap_new_from_names(keymap, &rmlvo)) {
151 xkb_keymap_unref(keymap);
158 XKB_EXPORT struct xkb_keymap *
159 xkb_keymap_new_from_string(struct xkb_context *ctx,
161 enum xkb_keymap_format format,
162 enum xkb_keymap_compile_flags flags)
164 return xkb_keymap_new_from_buffer(ctx, string, strlen(string),
168 XKB_EXPORT struct xkb_keymap *
169 xkb_keymap_new_from_buffer(struct xkb_context *ctx,
170 const char *buffer, size_t length,
171 enum xkb_keymap_format format,
172 enum xkb_keymap_compile_flags flags)
174 struct xkb_keymap *keymap;
175 const struct xkb_keymap_format_ops *ops;
177 ops = get_keymap_format_ops(format);
178 if (!ops || !ops->keymap_new_from_string) {
179 log_err_func(ctx, "unsupported keymap format: %d\n", format);
183 if (flags & ~(XKB_KEYMAP_COMPILE_NO_FLAGS)) {
184 log_err_func(ctx, "unrecognized flags: %#x\n", flags);
189 log_err_func1(ctx, "no buffer specified\n");
193 keymap = xkb_keymap_new(ctx, format, flags);
197 /* Allow a zero-terminated string as a buffer */
198 if (length > 0 && buffer[length - 1] == '\0')
201 if (!ops->keymap_new_from_string(keymap, buffer, length)) {
202 xkb_keymap_unref(keymap);
209 XKB_EXPORT struct xkb_keymap *
210 xkb_keymap_new_from_file(struct xkb_context *ctx,
212 enum xkb_keymap_format format,
213 enum xkb_keymap_compile_flags flags)
215 struct xkb_keymap *keymap;
216 const struct xkb_keymap_format_ops *ops;
218 ops = get_keymap_format_ops(format);
219 if (!ops || !ops->keymap_new_from_file) {
220 log_err_func(ctx, "unsupported keymap format: %d\n", format);
224 if (flags & ~(XKB_KEYMAP_COMPILE_NO_FLAGS)) {
225 log_err_func(ctx, "unrecognized flags: %#x\n", flags);
230 log_err_func1(ctx, "no file specified\n");
234 keymap = xkb_keymap_new(ctx, format, flags);
238 if (!ops->keymap_new_from_file(keymap, file)) {
239 xkb_keymap_unref(keymap);
247 xkb_keymap_get_as_string(struct xkb_keymap *keymap,
248 enum xkb_keymap_format format)
250 const struct xkb_keymap_format_ops *ops;
252 if (format == XKB_KEYMAP_USE_ORIGINAL_FORMAT)
253 format = keymap->format;
255 ops = get_keymap_format_ops(format);
256 if (!ops || !ops->keymap_get_as_string) {
257 log_err_func(keymap->ctx, "unsupported keymap format: %d\n", format);
261 return ops->keymap_get_as_string(keymap);
265 * Returns the total number of modifiers active in the keymap.
267 XKB_EXPORT xkb_mod_index_t
268 xkb_keymap_num_mods(struct xkb_keymap *keymap)
270 return keymap->mods.num_mods;
274 * Return the name for a given modifier.
276 XKB_EXPORT const char *
277 xkb_keymap_mod_get_name(struct xkb_keymap *keymap, xkb_mod_index_t idx)
279 if (idx >= keymap->mods.num_mods)
282 return xkb_atom_text(keymap->ctx, keymap->mods.mods[idx].name);
286 * Returns the index for a named modifier.
288 XKB_EXPORT xkb_mod_index_t
289 xkb_keymap_mod_get_index(struct xkb_keymap *keymap, const char *name)
293 atom = xkb_atom_lookup(keymap->ctx, name);
294 if (atom == XKB_ATOM_NONE)
295 return XKB_MOD_INVALID;
297 return XkbModNameToIndex(&keymap->mods, atom, MOD_BOTH);
301 * Return the total number of active groups in the keymap.
303 XKB_EXPORT xkb_layout_index_t
304 xkb_keymap_num_layouts(struct xkb_keymap *keymap)
306 return keymap->num_groups;
310 * Returns the name for a given group.
312 XKB_EXPORT const char *
313 xkb_keymap_layout_get_name(struct xkb_keymap *keymap, xkb_layout_index_t idx)
315 if (idx >= keymap->num_group_names)
318 return xkb_atom_text(keymap->ctx, keymap->group_names[idx]);
322 * Returns the index for a named layout.
324 XKB_EXPORT xkb_layout_index_t
325 xkb_keymap_layout_get_index(struct xkb_keymap *keymap, const char *name)
327 xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
328 xkb_layout_index_t i;
330 if (atom == XKB_ATOM_NONE)
331 return XKB_LAYOUT_INVALID;
333 for (i = 0; i < keymap->num_group_names; i++)
334 if (keymap->group_names[i] == atom)
337 return XKB_LAYOUT_INVALID;
341 * Returns the number of layouts active for a particular key.
343 XKB_EXPORT xkb_layout_index_t
344 xkb_keymap_num_layouts_for_key(struct xkb_keymap *keymap, xkb_keycode_t kc)
346 const struct xkb_key *key = XkbKey(keymap, kc);
351 return key->num_groups;
355 * Returns the number of levels active for a particular key and layout.
357 XKB_EXPORT xkb_level_index_t
358 xkb_keymap_num_levels_for_key(struct xkb_keymap *keymap, xkb_keycode_t kc,
359 xkb_layout_index_t layout)
361 const struct xkb_key *key = XkbKey(keymap, kc);
366 layout = XkbWrapGroupIntoRange(layout, key->num_groups,
367 key->out_of_range_group_action,
368 key->out_of_range_group_number);
369 if (layout == XKB_LAYOUT_INVALID)
372 return XkbKeyNumLevels(key, layout);
376 * Return the total number of LEDs in the keymap.
378 XKB_EXPORT xkb_led_index_t
379 xkb_keymap_num_leds(struct xkb_keymap *keymap)
381 return keymap->num_leds;
385 * Returns the name for a given LED.
387 XKB_EXPORT const char *
388 xkb_keymap_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx)
390 if (idx >= keymap->num_leds)
393 return xkb_atom_text(keymap->ctx, keymap->leds[idx].name);
397 * Returns the index for a named LED.
399 XKB_EXPORT xkb_led_index_t
400 xkb_keymap_led_get_index(struct xkb_keymap *keymap, const char *name)
402 xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
404 const struct xkb_led *led;
406 if (atom == XKB_ATOM_NONE)
407 return XKB_LED_INVALID;
409 xkb_leds_enumerate(i, led, keymap)
410 if (led->name == atom)
413 return XKB_LED_INVALID;
417 xkb_keymap_key_get_mods_for_level(struct xkb_keymap *keymap,
419 xkb_layout_index_t layout,
420 xkb_level_index_t level,
421 xkb_mod_mask_t *masks_out,
424 const struct xkb_key *key = XkbKey(keymap, kc);
428 layout = XkbWrapGroupIntoRange(layout, key->num_groups,
429 key->out_of_range_group_action,
430 key->out_of_range_group_number);
431 if (layout == XKB_LAYOUT_INVALID)
434 if (level >= XkbKeyNumLevels(key, layout))
437 const struct xkb_key_type *type = key->groups[layout].type;
442 * If the active set of modifiers doesn't match any explicit entry of
443 * the key type, the resulting level is 0 (i.e. Level 1).
444 * So, if we are asked to find the modifiers for level==0, we can offer
445 * an ~infinite supply, which is not very workable.
446 * What we do instead, is special case the empty set of modifiers for
447 * this purpose. If the empty set isn't explicit mapped to a level, we
448 * take it to map to Level 1.
449 * This is almost always what we want. If applicable, given it priority
450 * over other ways to generate the level.
453 bool empty_mapped = false;
454 for (unsigned i = 0; i < type->num_entries && count < masks_size; i++)
455 if (entry_is_active(&type->entries[i]) &&
456 type->entries[i].mods.mask == 0) {
460 if (!empty_mapped && count < masks_size) {
461 masks_out[count++] = 0;
465 /* Now search explicit mappings. */
466 for (unsigned i = 0; i < type->num_entries && count < masks_size; i++) {
467 if (entry_is_active(&type->entries[i]) &&
468 type->entries[i].level == level) {
469 masks_out[count++] = type->entries[i].mods.mask;
477 * As below, but takes an explicit layout/level rather than state.
480 xkb_keymap_key_get_syms_by_level(struct xkb_keymap *keymap,
482 xkb_layout_index_t layout,
483 xkb_level_index_t level,
484 const xkb_keysym_t **syms_out)
486 const struct xkb_key *key = XkbKey(keymap, kc);
492 layout = XkbWrapGroupIntoRange(layout, key->num_groups,
493 key->out_of_range_group_action,
494 key->out_of_range_group_number);
495 if (layout == XKB_LAYOUT_INVALID)
498 if (level >= XkbKeyNumLevels(key, layout))
501 num_syms = key->groups[layout].levels[level].num_syms;
506 *syms_out = &key->groups[layout].levels[level].u.sym;
508 *syms_out = key->groups[layout].levels[level].u.syms;
517 XKB_EXPORT xkb_keycode_t
518 xkb_keymap_min_keycode(struct xkb_keymap *keymap)
520 return keymap->min_key_code;
523 XKB_EXPORT xkb_keycode_t
524 xkb_keymap_max_keycode(struct xkb_keymap *keymap)
526 return keymap->max_key_code;
530 xkb_keymap_key_for_each(struct xkb_keymap *keymap, xkb_keymap_key_iter_t iter,
535 xkb_keys_foreach(key, keymap)
536 iter(keymap, key->keycode, data);
539 XKB_EXPORT const char *
540 xkb_keymap_key_get_name(struct xkb_keymap *keymap, xkb_keycode_t kc)
542 const struct xkb_key *key = XkbKey(keymap, kc);
547 return xkb_atom_text(keymap->ctx, key->name);
550 XKB_EXPORT xkb_keycode_t
551 xkb_keymap_key_by_name(struct xkb_keymap *keymap, const char *name)
556 atom = xkb_atom_lookup(keymap->ctx, name);
558 xkb_atom_t ratom = XkbResolveKeyAlias(keymap, atom);
563 return XKB_KEYCODE_INVALID;
565 xkb_keys_foreach(key, keymap) {
566 if (key->name == atom)
570 return XKB_KEYCODE_INVALID;
574 * Simple boolean specifying whether or not the key should repeat.
577 xkb_keymap_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t kc)
579 const struct xkb_key *key = XkbKey(keymap, kc);