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>
32 update_builtin_keymap_fields(struct xkb_keymap *keymap)
34 /* Predefined (AKA real, core, X11) modifiers. The order is important! */
35 static const char *const builtin_mods[] = {
46 for (unsigned i = 0; i < ARRAY_SIZE(builtin_mods); i++) {
47 keymap->mods.mods[i].name = xkb_atom_intern(keymap->ctx,
49 strlen(builtin_mods[i]));
50 keymap->mods.mods[i].type = MOD_REAL;
52 keymap->mods.num_mods = ARRAY_SIZE(builtin_mods);
56 xkb_keymap_new(struct xkb_context *ctx,
57 enum xkb_keymap_format format,
58 enum xkb_keymap_compile_flags flags)
60 struct xkb_keymap *keymap;
62 keymap = calloc(1, sizeof(*keymap));
67 keymap->ctx = xkb_context_ref(ctx);
69 keymap->format = format;
70 keymap->flags = flags;
72 update_builtin_keymap_fields(keymap);
78 XkbKeyByName(struct xkb_keymap *keymap, xkb_atom_t name, bool use_aliases)
82 xkb_keys_foreach(key, keymap)
83 if (key->name == name)
87 xkb_atom_t new_name = XkbResolveKeyAlias(keymap, name);
88 if (new_name != XKB_ATOM_NONE)
89 return XkbKeyByName(keymap, new_name, false);
96 XkbResolveKeyAlias(const struct xkb_keymap *keymap, xkb_atom_t name)
98 for (unsigned i = 0; i < keymap->num_key_aliases; i++)
99 if (keymap->key_aliases[i].alias == name)
100 return keymap->key_aliases[i].real;
102 return XKB_ATOM_NONE;
106 XkbEscapeMapName(char *name)
109 * All latin-1 alphanumerics, plus parens, slash, minus, underscore and
112 static const unsigned char legal[] = {
113 0x00, 0x00, 0x00, 0x00, 0x00, 0xa7, 0xff, 0x83,
114 0xfe, 0xff, 0xff, 0x87, 0xfe, 0xff, 0xff, 0x07,
115 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
116 0xff, 0xff, 0x7f, 0xff, 0xff, 0xff, 0x7f, 0xff
123 unsigned char c = *name;
124 if (!(legal[c / 8] & (1 << (c % 8))))
131 XkbModNameToIndex(const struct xkb_mod_set *mods, xkb_atom_t name,
135 const struct xkb_mod *mod;
137 xkb_mods_enumerate(i, mod, mods)
138 if ((mod->type & type) && name == mod->name)
141 return XKB_MOD_INVALID;
145 XkbLevelsSameSyms(const struct xkb_level *a, const struct xkb_level *b)
147 if (a->num_syms != b->num_syms)
149 if (a->num_syms <= 1)
150 return a->u.sym == b->u.sym;
151 return memcmp(a->u.syms, b->u.syms, sizeof(*a->u.syms) * a->num_syms) == 0;