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 * ********************************************************/
57 update_builtin_keymap_fields(struct xkb_keymap *keymap)
59 struct xkb_context *ctx = keymap->ctx;
62 * Add predefined (AKA real, core, X11) modifiers.
63 * The order is important!
65 darray_appends_t(keymap->mods, struct xkb_mod,
66 { .name = xkb_atom_intern_literal(ctx, "Shift"), .type = MOD_REAL },
67 { .name = xkb_atom_intern_literal(ctx, "Lock"), .type = MOD_REAL },
68 { .name = xkb_atom_intern_literal(ctx, "Control"), .type = MOD_REAL },
69 { .name = xkb_atom_intern_literal(ctx, "Mod1"), .type = MOD_REAL },
70 { .name = xkb_atom_intern_literal(ctx, "Mod2"), .type = MOD_REAL },
71 { .name = xkb_atom_intern_literal(ctx, "Mod3"), .type = MOD_REAL },
72 { .name = xkb_atom_intern_literal(ctx, "Mod4"), .type = MOD_REAL },
73 { .name = xkb_atom_intern_literal(ctx, "Mod5"), .type = MOD_REAL });
76 static struct xkb_keymap *
77 xkb_keymap_new(struct xkb_context *ctx,
78 enum xkb_keymap_format format,
79 enum xkb_keymap_compile_flags flags)
81 struct xkb_keymap *keymap;
83 keymap = calloc(1, sizeof(*keymap));
88 keymap->ctx = xkb_context_ref(ctx);
90 keymap->format = format;
91 keymap->flags = flags;
93 update_builtin_keymap_fields(keymap);
98 XKB_EXPORT struct xkb_keymap *
99 xkb_keymap_ref(struct xkb_keymap *keymap)
106 xkb_keymap_unref(struct xkb_keymap *keymap)
111 if (!keymap || --keymap->refcnt > 0)
115 xkb_foreach_key(key, keymap) {
117 for (i = 0; i < key->num_groups; i++) {
118 if (key->groups[i].levels) {
119 for (j = 0; j < XkbKeyGroupWidth(key, i); j++)
120 if (key->groups[i].levels[j].num_syms > 1)
121 free(key->groups[i].levels[j].u.syms);
122 free(key->groups[i].levels);
131 for (i = 0; i < keymap->num_types; i++) {
132 free(keymap->types[i].entries);
133 free(keymap->types[i].level_names);
137 free(keymap->sym_interprets);
138 free(keymap->key_aliases);
139 free(keymap->group_names);
140 darray_free(keymap->mods);
141 darray_free(keymap->leds);
142 free(keymap->keycodes_section_name);
143 free(keymap->symbols_section_name);
144 free(keymap->types_section_name);
145 free(keymap->compat_section_name);
146 xkb_context_unref(keymap->ctx);
150 static const struct xkb_keymap_format_ops *
151 get_keymap_format_ops(enum xkb_keymap_format format)
153 static const struct xkb_keymap_format_ops *keymap_format_ops[] = {
154 [XKB_KEYMAP_FORMAT_TEXT_V1] = &text_v1_keymap_format_ops,
157 if ((int) format < 0 || (int) format >= ARRAY_SIZE(keymap_format_ops))
160 return keymap_format_ops[format];
163 XKB_EXPORT struct xkb_keymap *
164 xkb_keymap_new_from_names(struct xkb_context *ctx,
165 const struct xkb_rule_names *rmlvo_in,
166 enum xkb_keymap_compile_flags flags)
168 struct xkb_keymap *keymap;
169 struct xkb_rule_names rmlvo;
170 const enum xkb_keymap_format format = XKB_KEYMAP_FORMAT_TEXT_V1;
171 const struct xkb_keymap_format_ops *ops;
173 ops = get_keymap_format_ops(format);
174 if (!ops || !ops->keymap_new_from_names) {
175 log_err_func(ctx, "unsupported keymap format: %d\n", format);
179 if (flags & ~(XKB_MAP_COMPILE_PLACEHOLDER)) {
180 log_err_func(ctx, "unrecognized flags: %#x\n", flags);
187 memset(&rmlvo, 0, sizeof(rmlvo));
189 if (isempty(rmlvo.rules))
190 rmlvo.rules = xkb_context_get_default_rules(ctx);
191 if (isempty(rmlvo.model))
192 rmlvo.model = xkb_context_get_default_model(ctx);
193 /* Layout and variant are tied together, so don't try to use one from
194 * the caller and one from the environment. */
195 if (isempty(rmlvo.layout)) {
196 rmlvo.layout = xkb_context_get_default_layout(ctx);
197 rmlvo.variant = xkb_context_get_default_variant(ctx);
199 /* Options can be empty, so respect that if passed in. */
200 if (rmlvo.options == NULL)
201 rmlvo.options = xkb_context_get_default_options(ctx);
203 keymap = xkb_keymap_new(ctx, format, flags);
207 if (!ops->keymap_new_from_names(keymap, &rmlvo)) {
208 xkb_keymap_unref(keymap);
215 XKB_EXPORT struct xkb_keymap *
216 xkb_keymap_new_from_string(struct xkb_context *ctx,
218 enum xkb_keymap_format format,
219 enum xkb_keymap_compile_flags flags)
221 return xkb_keymap_new_from_buffer(ctx, string, strlen(string),
225 XKB_EXPORT struct xkb_keymap *
226 xkb_keymap_new_from_buffer(struct xkb_context *ctx,
227 const char *buffer, size_t length,
228 enum xkb_keymap_format format,
229 enum xkb_keymap_compile_flags flags)
231 struct xkb_keymap *keymap;
232 const struct xkb_keymap_format_ops *ops;
234 ops = get_keymap_format_ops(format);
235 if (!ops || !ops->keymap_new_from_string) {
236 log_err_func(ctx, "unsupported keymap format: %d\n", format);
240 if (flags & ~(XKB_MAP_COMPILE_PLACEHOLDER)) {
241 log_err_func(ctx, "unrecognized flags: %#x\n", flags);
246 log_err_func1(ctx, "no buffer specified\n");
250 keymap = xkb_keymap_new(ctx, format, flags);
254 if (!ops->keymap_new_from_string(keymap, buffer, length)) {
255 xkb_keymap_unref(keymap);
262 XKB_EXPORT struct xkb_keymap *
263 xkb_keymap_new_from_file(struct xkb_context *ctx,
265 enum xkb_keymap_format format,
266 enum xkb_keymap_compile_flags flags)
268 struct xkb_keymap *keymap;
269 const struct xkb_keymap_format_ops *ops;
271 ops = get_keymap_format_ops(format);
272 if (!ops || !ops->keymap_new_from_file) {
273 log_err_func(ctx, "unsupported keymap format: %d\n", format);
277 if (flags & ~(XKB_MAP_COMPILE_PLACEHOLDER)) {
278 log_err_func(ctx, "unrecognized flags: %#x\n", flags);
283 log_err_func1(ctx, "no file specified\n");
287 keymap = xkb_keymap_new(ctx, format, flags);
291 if (!ops->keymap_new_from_file(keymap, file)) {
292 xkb_keymap_unref(keymap);
300 xkb_keymap_get_as_string(struct xkb_keymap *keymap,
301 enum xkb_keymap_format format)
303 const struct xkb_keymap_format_ops *ops;
305 if (format == XKB_KEYMAP_USE_ORIGINAL_FORMAT)
306 format = keymap->format;
308 ops = get_keymap_format_ops(format);
309 if (!ops || !ops->keymap_get_as_string) {
310 log_err_func(keymap->ctx, "unsupported keymap format: %d\n", format);
314 return ops->keymap_get_as_string(keymap);
318 * Returns the total number of modifiers active in the keymap.
320 XKB_EXPORT xkb_mod_index_t
321 xkb_keymap_num_mods(struct xkb_keymap *keymap)
323 return darray_size(keymap->mods);
327 * Return the name for a given modifier.
329 XKB_EXPORT const char *
330 xkb_keymap_mod_get_name(struct xkb_keymap *keymap, xkb_mod_index_t idx)
332 if (idx >= darray_size(keymap->mods))
335 return xkb_atom_text(keymap->ctx, darray_item(keymap->mods, idx).name);
339 * Returns the index for a named modifier.
341 XKB_EXPORT xkb_mod_index_t
342 xkb_keymap_mod_get_index(struct xkb_keymap *keymap, const char *name)
346 const struct xkb_mod *mod;
348 atom = xkb_atom_lookup(keymap->ctx, name);
349 if (atom == XKB_ATOM_NONE)
350 return XKB_MOD_INVALID;
352 darray_enumerate(i, mod, keymap->mods)
353 if (mod->name == atom)
356 return XKB_MOD_INVALID;
360 * Return the total number of active groups in the keymap.
362 XKB_EXPORT xkb_layout_index_t
363 xkb_keymap_num_layouts(struct xkb_keymap *keymap)
365 return keymap->num_groups;
369 * Returns the name for a given group.
371 XKB_EXPORT const char *
372 xkb_keymap_layout_get_name(struct xkb_keymap *keymap, xkb_layout_index_t idx)
374 if (idx >= keymap->num_group_names)
377 return xkb_atom_text(keymap->ctx, keymap->group_names[idx]);
381 * Returns the index for a named layout.
383 XKB_EXPORT xkb_layout_index_t
384 xkb_keymap_layout_get_index(struct xkb_keymap *keymap, const char *name)
386 xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
387 xkb_layout_index_t i;
389 if (atom == XKB_ATOM_NONE)
390 return XKB_LAYOUT_INVALID;
392 for (i = 0; i < keymap->num_group_names; i++)
393 if (keymap->group_names[i] == atom)
396 return XKB_LAYOUT_INVALID;
400 * Returns the number of layouts active for a particular key.
402 XKB_EXPORT xkb_layout_index_t
403 xkb_keymap_num_layouts_for_key(struct xkb_keymap *keymap, xkb_keycode_t kc)
405 const struct xkb_key *key = XkbKey(keymap, kc);
410 return key->num_groups;
414 * Returns the number of levels active for a particular key and layout.
416 XKB_EXPORT xkb_level_index_t
417 xkb_keymap_num_levels_for_key(struct xkb_keymap *keymap, xkb_keycode_t kc,
418 xkb_layout_index_t layout)
420 const struct xkb_key *key = XkbKey(keymap, kc);
425 layout = wrap_group_into_range(layout, key->num_groups,
426 key->out_of_range_group_action,
427 key->out_of_range_group_number);
428 if (layout == XKB_LAYOUT_INVALID)
431 return XkbKeyGroupWidth(key, layout);
435 * Return the total number of LEDs in the keymap.
437 XKB_EXPORT xkb_led_index_t
438 xkb_keymap_num_leds(struct xkb_keymap *keymap)
440 return darray_size(keymap->leds);
444 * Returns the name for a given LED.
446 XKB_EXPORT const char *
447 xkb_keymap_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx)
449 if (idx >= darray_size(keymap->leds))
452 return xkb_atom_text(keymap->ctx, darray_item(keymap->leds, idx).name);
456 * Returns the index for a named LED.
458 XKB_EXPORT xkb_led_index_t
459 xkb_keymap_led_get_index(struct xkb_keymap *keymap, const char *name)
461 xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
463 const struct xkb_led *led;
465 if (atom == XKB_ATOM_NONE)
466 return XKB_LED_INVALID;
468 darray_enumerate(i, led, keymap->leds)
469 if (led->name == atom)
472 return XKB_LED_INVALID;
476 * As below, but takes an explicit layout/level rather than state.
479 xkb_keymap_key_get_syms_by_level(struct xkb_keymap *keymap,
481 xkb_layout_index_t layout,
482 xkb_level_index_t level,
483 const xkb_keysym_t **syms_out)
485 const struct xkb_key *key = XkbKey(keymap, kc);
491 layout = wrap_group_into_range(layout, key->num_groups,
492 key->out_of_range_group_action,
493 key->out_of_range_group_number);
494 if (layout == XKB_LAYOUT_INVALID)
497 if (level >= XkbKeyGroupWidth(key, layout))
500 num_syms = key->groups[layout].levels[level].num_syms;
505 *syms_out = &key->groups[layout].levels[level].u.sym;
507 *syms_out = key->groups[layout].levels[level].u.syms;
516 XKB_EXPORT xkb_keycode_t
517 xkb_keymap_min_keycode(struct xkb_keymap *keymap)
519 return keymap->min_key_code;
522 XKB_EXPORT xkb_keycode_t
523 xkb_keymap_max_keycode(struct xkb_keymap *keymap)
525 return keymap->max_key_code;
529 xkb_keymap_key_for_each(struct xkb_keymap *keymap, xkb_keymap_key_iter_t iter,
534 xkb_foreach_key(key, keymap)
535 iter(keymap, key->keycode, data);
539 * Simple boolean specifying whether or not the key should repeat.
542 xkb_keymap_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t kc)
544 const struct xkb_key *key = XkbKey(keymap, kc);
553 XkbKeyByName(struct xkb_keymap *keymap, xkb_atom_t name, bool use_aliases)
557 xkb_foreach_key(key, keymap)
558 if (key->name == name)
562 xkb_atom_t new_name = XkbResolveKeyAlias(keymap, name);
563 if (new_name != XKB_ATOM_NONE)
564 return XkbKeyByName(keymap, new_name, false);
571 XkbResolveKeyAlias(struct xkb_keymap *keymap, xkb_atom_t name)
573 for (unsigned i = 0; i < keymap->num_key_aliases; i++)
574 if (keymap->key_aliases[i].alias == name)
575 return keymap->key_aliases[i].real;
577 return XKB_ATOM_NONE;