keymap: move ModNameToIndex from text.c and use it in keymap.c
[platform/upstream/libxkbcommon.git] / src / keymap-priv.c
1 /**
2  * Copyright © 2012 Intel Corporation
3  * Copyright © 2012 Ran Benita <ran234@gmail.com>
4  *
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:
11  *
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
14  * Software.
15  *
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.
23  *
24  * Author: Daniel Stone <daniel@fooishbar.org>
25  */
26
27 #include "keymap.h"
28
29 static void
30 update_builtin_keymap_fields(struct xkb_keymap *keymap)
31 {
32     struct xkb_context *ctx = keymap->ctx;
33     const struct xkb_mod builtin_mods[] = {
34         { .name = xkb_atom_intern_literal(ctx, "Shift"),   .type = MOD_REAL },
35         { .name = xkb_atom_intern_literal(ctx, "Lock"),    .type = MOD_REAL },
36         { .name = xkb_atom_intern_literal(ctx, "Control"), .type = MOD_REAL },
37         { .name = xkb_atom_intern_literal(ctx, "Mod1"),    .type = MOD_REAL },
38         { .name = xkb_atom_intern_literal(ctx, "Mod2"),    .type = MOD_REAL },
39         { .name = xkb_atom_intern_literal(ctx, "Mod3"),    .type = MOD_REAL },
40         { .name = xkb_atom_intern_literal(ctx, "Mod4"),    .type = MOD_REAL },
41         { .name = xkb_atom_intern_literal(ctx, "Mod5"),    .type = MOD_REAL },
42     };
43
44     /*
45      * Add predefined (AKA real, core, X11) modifiers.
46      * The order is important!
47      */
48     darray_append_items(keymap->mods, builtin_mods, ARRAY_SIZE(builtin_mods));
49 }
50
51 struct xkb_keymap *
52 xkb_keymap_new(struct xkb_context *ctx,
53                enum xkb_keymap_format format,
54                enum xkb_keymap_compile_flags flags)
55 {
56     struct xkb_keymap *keymap;
57
58     keymap = calloc(1, sizeof(*keymap));
59     if (!keymap)
60         return NULL;
61
62     keymap->refcnt = 1;
63     keymap->ctx = xkb_context_ref(ctx);
64
65     keymap->format = format;
66     keymap->flags = flags;
67
68     update_builtin_keymap_fields(keymap);
69
70     return keymap;
71 }
72
73 struct xkb_key *
74 XkbKeyByName(struct xkb_keymap *keymap, xkb_atom_t name, bool use_aliases)
75 {
76     struct xkb_key *key;
77
78     xkb_foreach_key(key, keymap)
79         if (key->name == name)
80             return key;
81
82     if (use_aliases) {
83         xkb_atom_t new_name = XkbResolveKeyAlias(keymap, name);
84         if (new_name != XKB_ATOM_NONE)
85             return XkbKeyByName(keymap, new_name, false);
86     }
87
88     return NULL;
89 }
90
91 xkb_atom_t
92 XkbResolveKeyAlias(struct xkb_keymap *keymap, xkb_atom_t name)
93 {
94     for (unsigned i = 0; i < keymap->num_key_aliases; i++)
95         if (keymap->key_aliases[i].alias == name)
96             return keymap->key_aliases[i].real;
97
98     return XKB_ATOM_NONE;
99 }
100
101 void
102 XkbEscapeMapName(char *name)
103 {
104     /*
105      * All latin-1 alphanumerics, plus parens, slash, minus, underscore and
106      * wildcards.
107      */
108     static const unsigned char legal[] = {
109         0x00, 0x00, 0x00, 0x00, 0x00, 0xa7, 0xff, 0x83,
110         0xfe, 0xff, 0xff, 0x87, 0xfe, 0xff, 0xff, 0x07,
111         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
112         0xff, 0xff, 0x7f, 0xff, 0xff, 0xff, 0x7f, 0xff
113     };
114
115     if (!name)
116         return;
117
118     while (*name) {
119         if (!(legal[*name / 8] & (1 << (*name % 8))))
120             *name = '_';
121         name++;
122     }
123 }
124
125 xkb_mod_index_t
126 XkbModNameToIndex(const struct xkb_keymap *keymap, xkb_atom_t name,
127                   enum mod_type type)
128 {
129     xkb_mod_index_t i;
130     const struct xkb_mod *mod;
131
132     darray_enumerate(i, mod, keymap->mods)
133         if ((mod->type & type) && name == mod->name)
134             return i;
135
136     return XKB_MOD_INVALID;
137 }