keymap: split private functions to keymap-priv.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
34     /*
35      * Add predefined (AKA real, core, X11) modifiers.
36      * The order is important!
37      */
38     darray_appends_t(keymap->mods, struct xkb_mod,
39         { .name = xkb_atom_intern_literal(ctx, "Shift"),   .type = MOD_REAL },
40         { .name = xkb_atom_intern_literal(ctx, "Lock"),    .type = MOD_REAL },
41         { .name = xkb_atom_intern_literal(ctx, "Control"), .type = MOD_REAL },
42         { .name = xkb_atom_intern_literal(ctx, "Mod1"),    .type = MOD_REAL },
43         { .name = xkb_atom_intern_literal(ctx, "Mod2"),    .type = MOD_REAL },
44         { .name = xkb_atom_intern_literal(ctx, "Mod3"),    .type = MOD_REAL },
45         { .name = xkb_atom_intern_literal(ctx, "Mod4"),    .type = MOD_REAL },
46         { .name = xkb_atom_intern_literal(ctx, "Mod5"),    .type = MOD_REAL });
47 }
48
49 struct xkb_keymap *
50 xkb_keymap_new(struct xkb_context *ctx,
51                enum xkb_keymap_format format,
52                enum xkb_keymap_compile_flags flags)
53 {
54     struct xkb_keymap *keymap;
55
56     keymap = calloc(1, sizeof(*keymap));
57     if (!keymap)
58         return NULL;
59
60     keymap->refcnt = 1;
61     keymap->ctx = xkb_context_ref(ctx);
62
63     keymap->format = format;
64     keymap->flags = flags;
65
66     update_builtin_keymap_fields(keymap);
67
68     return keymap;
69 }
70
71 struct xkb_key *
72 XkbKeyByName(struct xkb_keymap *keymap, xkb_atom_t name, bool use_aliases)
73 {
74     struct xkb_key *key;
75
76     xkb_foreach_key(key, keymap)
77         if (key->name == name)
78             return key;
79
80     if (use_aliases) {
81         xkb_atom_t new_name = XkbResolveKeyAlias(keymap, name);
82         if (new_name != XKB_ATOM_NONE)
83             return XkbKeyByName(keymap, new_name, false);
84     }
85
86     return NULL;
87 }
88
89 xkb_atom_t
90 XkbResolveKeyAlias(struct xkb_keymap *keymap, xkb_atom_t name)
91 {
92     for (unsigned i = 0; i < keymap->num_key_aliases; i++)
93         if (keymap->key_aliases[i].alias == name)
94             return keymap->key_aliases[i].real;
95
96     return XKB_ATOM_NONE;
97 }
98
99 void
100 XkbEscapeMapName(char *name)
101 {
102     /*
103      * All latin-1 alphanumerics, plus parens, slash, minus, underscore and
104      * wildcards.
105      */
106     static const unsigned char legal[] = {
107         0x00, 0x00, 0x00, 0x00, 0x00, 0xa7, 0xff, 0x83,
108         0xfe, 0xff, 0xff, 0x87, 0xfe, 0xff, 0xff, 0x07,
109         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
110         0xff, 0xff, 0x7f, 0xff, 0xff, 0xff, 0x7f, 0xff
111     };
112
113     if (!name)
114         return;
115
116     while (*name) {
117         if (!(legal[*name / 8] & (1 << (*name % 8))))
118             *name = '_';
119         name++;
120     }
121 }