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 * ********************************************************/
56 static struct xkb_keymap *
57 xkb_keymap_new(struct xkb_context *ctx,
58 enum xkb_keymap_format format,
59 enum xkb_keymap_compile_flags flags)
61 struct xkb_keymap *keymap;
63 keymap = calloc(1, sizeof(*keymap));
68 keymap->ctx = xkb_context_ref(ctx);
70 keymap->format = format;
71 keymap->flags = flags;
76 XKB_EXPORT struct xkb_keymap *
77 xkb_keymap_ref(struct xkb_keymap *keymap)
84 xkb_keymap_unref(struct xkb_keymap *keymap)
89 if (!keymap || --keymap->refcnt > 0)
93 xkb_foreach_key(key, keymap) {
94 for (i = 0; i < key->num_groups; i++) {
95 for (j = 0; j < XkbKeyGroupWidth(key, i); j++)
96 if (key->groups[i].levels[j].num_syms > 1)
97 free(key->groups[i].levels[j].u.syms);
98 free(key->groups[i].levels);
104 for (i = 0; i < keymap->num_types; i++) {
105 free(keymap->types[i].entries);
106 free(keymap->types[i].level_names);
109 darray_free(keymap->sym_interprets);
110 free(keymap->key_aliases);
111 free(keymap->group_names);
112 darray_free(keymap->mods);
113 darray_free(keymap->leds);
114 free(keymap->keycodes_section_name);
115 free(keymap->symbols_section_name);
116 free(keymap->types_section_name);
117 free(keymap->compat_section_name);
118 xkb_context_unref(keymap->ctx);
122 static const struct xkb_keymap_format_ops *
123 get_keymap_format_ops(enum xkb_keymap_format format)
125 static const struct xkb_keymap_format_ops *keymap_format_ops[] = {
126 [XKB_KEYMAP_FORMAT_TEXT_V1] = &text_v1_keymap_format_ops,
129 if ((int) format < 0 || (int) format >= ARRAY_SIZE(keymap_format_ops))
132 return keymap_format_ops[format];
135 XKB_EXPORT struct xkb_keymap *
136 xkb_keymap_new_from_names(struct xkb_context *ctx,
137 const struct xkb_rule_names *rmlvo_in,
138 enum xkb_keymap_compile_flags flags)
140 struct xkb_keymap *keymap;
141 struct xkb_rule_names rmlvo;
142 const enum xkb_keymap_format format = XKB_KEYMAP_FORMAT_TEXT_V1;
143 const struct xkb_keymap_format_ops *ops;
145 ops = get_keymap_format_ops(format);
146 if (!ops || !ops->keymap_new_from_names) {
147 log_err_func(ctx, "unsupported keymap format: %d\n", format);
151 if (flags & ~(XKB_MAP_COMPILE_PLACEHOLDER)) {
152 log_err_func(ctx, "unrecognized flags: %#x\n", flags);
159 memset(&rmlvo, 0, sizeof(rmlvo));
161 if (isempty(rmlvo.rules))
162 rmlvo.rules = xkb_context_get_default_rules(ctx);
163 if (isempty(rmlvo.model))
164 rmlvo.model = xkb_context_get_default_model(ctx);
165 /* Layout and variant are tied together, so don't try to use one from
166 * the caller and one from the environment. */
167 if (isempty(rmlvo.layout)) {
168 rmlvo.layout = xkb_context_get_default_layout(ctx);
169 rmlvo.variant = xkb_context_get_default_variant(ctx);
171 /* Options can be empty, so respect that if passed in. */
172 if (rmlvo.options == NULL)
173 rmlvo.options = xkb_context_get_default_options(ctx);
175 keymap = xkb_keymap_new(ctx, format, flags);
179 if (!ops->keymap_new_from_names(keymap, &rmlvo)) {
180 xkb_keymap_unref(keymap);
187 XKB_EXPORT struct xkb_keymap *
188 xkb_keymap_new_from_string(struct xkb_context *ctx,
190 enum xkb_keymap_format format,
191 enum xkb_keymap_compile_flags flags)
193 struct xkb_keymap *keymap;
194 const struct xkb_keymap_format_ops *ops;
196 ops = get_keymap_format_ops(format);
197 if (!ops || !ops->keymap_new_from_string) {
198 log_err_func(ctx, "unsupported keymap format: %d\n", format);
202 if (flags & ~(XKB_MAP_COMPILE_PLACEHOLDER)) {
203 log_err_func(ctx, "unrecognized flags: %#x\n", flags);
208 log_err_func1(ctx, "no string specified\n");
212 keymap = xkb_keymap_new(ctx, format, flags);
216 if (!ops->keymap_new_from_string(keymap, string)) {
217 xkb_keymap_unref(keymap);
224 XKB_EXPORT struct xkb_keymap *
225 xkb_keymap_new_from_buffer(struct xkb_context *ctx,
226 const char *buffer, size_t length,
227 enum xkb_keymap_format format,
228 enum xkb_keymap_compile_flags flags)
230 struct xkb_keymap *keymap;
231 const struct xkb_keymap_format_ops *ops;
233 ops = get_keymap_format_ops(format);
234 if (!ops || !ops->keymap_new_from_string) {
235 log_err_func(ctx, "unsupported keymap format: %d\n", format);
239 if (flags & ~(XKB_MAP_COMPILE_PLACEHOLDER)) {
240 log_err_func(ctx, "unrecognized flags: %#x\n", flags);
245 log_err_func1(ctx, "no buffer specified\n");
249 keymap = xkb_keymap_new(ctx, format, flags);
253 if (!ops->keymap_new_from_buffer(keymap, buffer, length)) {
254 xkb_keymap_unref(keymap);
261 XKB_EXPORT struct xkb_keymap *
262 xkb_keymap_new_from_file(struct xkb_context *ctx,
264 enum xkb_keymap_format format,
265 enum xkb_keymap_compile_flags flags)
267 struct xkb_keymap *keymap;
268 const struct xkb_keymap_format_ops *ops;
270 ops = get_keymap_format_ops(format);
271 if (!ops || !ops->keymap_new_from_file) {
272 log_err_func(ctx, "unsupported keymap format: %d\n", format);
276 if (flags & ~(XKB_MAP_COMPILE_PLACEHOLDER)) {
277 log_err_func(ctx, "unrecognized flags: %#x\n", flags);
282 log_err_func1(ctx, "no file specified\n");
286 keymap = xkb_keymap_new(ctx, format, flags);
290 if (!ops->keymap_new_from_file(keymap, file)) {
291 xkb_keymap_unref(keymap);
299 xkb_keymap_get_as_string(struct xkb_keymap *keymap,
300 enum xkb_keymap_format format)
302 const struct xkb_keymap_format_ops *ops;
304 if (format == XKB_KEYMAP_USE_ORIGINAL_FORMAT)
305 format = keymap->format;
307 ops = get_keymap_format_ops(format);
308 if (!ops || !ops->keymap_get_as_string) {
309 log_err_func(keymap->ctx, "unsupported keymap format: %d\n", format);
313 return ops->keymap_get_as_string(keymap);
317 * Returns the total number of modifiers active in the keymap.
319 XKB_EXPORT xkb_mod_index_t
320 xkb_keymap_num_mods(struct xkb_keymap *keymap)
322 return darray_size(keymap->mods);
326 * Return the name for a given modifier.
328 XKB_EXPORT const char *
329 xkb_keymap_mod_get_name(struct xkb_keymap *keymap, xkb_mod_index_t idx)
331 if (idx >= darray_size(keymap->mods))
334 return xkb_atom_text(keymap->ctx, darray_item(keymap->mods, idx).name);
338 * Returns the index for a named modifier.
340 XKB_EXPORT xkb_mod_index_t
341 xkb_keymap_mod_get_index(struct xkb_keymap *keymap, const char *name)
345 const struct xkb_mod *mod;
347 atom = xkb_atom_lookup(keymap->ctx, name);
348 if (atom == XKB_ATOM_NONE)
349 return XKB_MOD_INVALID;
351 darray_enumerate(i, mod, keymap->mods)
352 if (mod->name == atom)
355 return XKB_MOD_INVALID;
359 * Return the total number of active groups in the keymap.
361 XKB_EXPORT xkb_layout_index_t
362 xkb_keymap_num_layouts(struct xkb_keymap *keymap)
364 return keymap->num_groups;
368 * Returns the name for a given group.
370 XKB_EXPORT const char *
371 xkb_keymap_layout_get_name(struct xkb_keymap *keymap, xkb_layout_index_t idx)
373 if (idx >= keymap->num_group_names)
376 return xkb_atom_text(keymap->ctx, keymap->group_names[idx]);
380 * Returns the index for a named layout.
382 XKB_EXPORT xkb_layout_index_t
383 xkb_keymap_layout_get_index(struct xkb_keymap *keymap, const char *name)
385 xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
386 xkb_layout_index_t i;
388 if (atom == XKB_ATOM_NONE)
389 return XKB_LAYOUT_INVALID;
391 for (i = 0; i < keymap->num_group_names; i++)
392 if (keymap->group_names[i] == atom)
395 return XKB_LAYOUT_INVALID;
399 * Returns the number of layouts active for a particular key.
401 XKB_EXPORT xkb_layout_index_t
402 xkb_keymap_num_layouts_for_key(struct xkb_keymap *keymap, xkb_keycode_t kc)
404 const struct xkb_key *key = XkbKey(keymap, kc);
409 return key->num_groups;
413 * Returns the number of levels active for a particular key and layout.
415 XKB_EXPORT xkb_level_index_t
416 xkb_keymap_num_levels_for_key(struct xkb_keymap *keymap, xkb_keycode_t kc,
417 xkb_layout_index_t layout)
419 const struct xkb_key *key = XkbKey(keymap, kc);
424 layout = wrap_group_into_range(layout, key->num_groups,
425 key->out_of_range_group_action,
426 key->out_of_range_group_number);
427 if (layout == XKB_LAYOUT_INVALID)
430 return XkbKeyGroupWidth(key, layout);
434 * Return the total number of LEDs in the keymap.
436 XKB_EXPORT xkb_led_index_t
437 xkb_keymap_num_leds(struct xkb_keymap *keymap)
439 return darray_size(keymap->leds);
443 * Returns the name for a given LED.
445 XKB_EXPORT const char *
446 xkb_keymap_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx)
448 if (idx >= darray_size(keymap->leds))
451 return xkb_atom_text(keymap->ctx, darray_item(keymap->leds, idx).name);
455 * Returns the index for a named LED.
457 XKB_EXPORT xkb_led_index_t
458 xkb_keymap_led_get_index(struct xkb_keymap *keymap, const char *name)
460 xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
462 const struct xkb_led *led;
464 if (atom == XKB_ATOM_NONE)
465 return XKB_LED_INVALID;
467 darray_enumerate(i, led, keymap->leds)
468 if (led->name == atom)
471 return XKB_LED_INVALID;
475 * As below, but takes an explicit layout/level rather than state.
478 xkb_keymap_key_get_syms_by_level(struct xkb_keymap *keymap,
480 xkb_layout_index_t layout,
481 xkb_level_index_t level,
482 const xkb_keysym_t **syms_out)
484 const struct xkb_key *key = XkbKey(keymap, kc);
490 layout = wrap_group_into_range(layout, key->num_groups,
491 key->out_of_range_group_action,
492 key->out_of_range_group_number);
493 if (layout == XKB_LAYOUT_INVALID)
496 if (level >= XkbKeyGroupWidth(key, layout))
499 num_syms = key->groups[layout].levels[level].num_syms;
504 *syms_out = &key->groups[layout].levels[level].u.sym;
506 *syms_out = key->groups[layout].levels[level].u.syms;
516 * Simple boolean specifying whether or not the key should repeat.
519 xkb_keymap_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t kc)
521 const struct xkb_key *key = XkbKey(keymap, kc);
530 XkbKeyByName(struct xkb_keymap *keymap, xkb_atom_t name, bool use_aliases)
534 xkb_foreach_key(key, keymap)
535 if (key->name == name)
539 xkb_atom_t new_name = XkbResolveKeyAlias(keymap, name);
540 if (new_name != XKB_ATOM_NONE)
541 return XkbKeyByName(keymap, new_name, false);
548 XkbResolveKeyAlias(struct xkb_keymap *keymap, xkb_atom_t name)
550 for (unsigned i = 0; i < keymap->num_key_aliases; i++)
551 if (keymap->key_aliases[i].alias == name)
552 return keymap->key_aliases[i].real;
554 return XKB_ATOM_NONE;