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 "config.h"
28 
29 #include "keymap.h"
30 
31 static void
update_builtin_keymap_fields(struct xkb_keymap * keymap)32 update_builtin_keymap_fields(struct xkb_keymap *keymap)
33 {
34     /* Predefined (AKA real, core, X11) modifiers. The order is important! */
35     static const char *const builtin_mods[] = {
36         [0] = "Shift",
37         [1] = "Lock",
38         [2] = "Control",
39         [3] = "Mod1",
40         [4] = "Mod2",
41         [5] = "Mod3",
42         [6] = "Mod4",
43         [7] = "Mod5"
44     };
45 
46     for (unsigned i = 0; i < ARRAY_SIZE(builtin_mods); i++) {
47         keymap->mods.mods[i].name = xkb_atom_intern(keymap->ctx,
48                                                     builtin_mods[i],
49                                                     strlen(builtin_mods[i]));
50         keymap->mods.mods[i].type = MOD_REAL;
51     }
52     keymap->mods.num_mods = ARRAY_SIZE(builtin_mods);
53 }
54 
55 struct xkb_keymap *
xkb_keymap_new(struct xkb_context * ctx,enum xkb_keymap_format format,enum xkb_keymap_compile_flags flags)56 xkb_keymap_new(struct xkb_context *ctx,
57                enum xkb_keymap_format format,
58                enum xkb_keymap_compile_flags flags)
59 {
60     struct xkb_keymap *keymap;
61 
62     keymap = calloc(1, sizeof(*keymap));
63     if (!keymap)
64         return NULL;
65 
66     keymap->refcnt = 1;
67     keymap->ctx = xkb_context_ref(ctx);
68 
69     keymap->format = format;
70     keymap->flags = flags;
71 
72     update_builtin_keymap_fields(keymap);
73 
74     return keymap;
75 }
76 
77 struct xkb_key *
XkbKeyByName(struct xkb_keymap * keymap,xkb_atom_t name,bool use_aliases)78 XkbKeyByName(struct xkb_keymap *keymap, xkb_atom_t name, bool use_aliases)
79 {
80     struct xkb_key *key;
81 
82     xkb_keys_foreach(key, keymap)
83         if (key->name == name)
84             return key;
85 
86     if (use_aliases) {
87         xkb_atom_t new_name = XkbResolveKeyAlias(keymap, name);
88         if (new_name != XKB_ATOM_NONE)
89             return XkbKeyByName(keymap, new_name, false);
90     }
91 
92     return NULL;
93 }
94 
95 xkb_atom_t
XkbResolveKeyAlias(const struct xkb_keymap * keymap,xkb_atom_t name)96 XkbResolveKeyAlias(const struct xkb_keymap *keymap, xkb_atom_t name)
97 {
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;
101 
102     return XKB_ATOM_NONE;
103 }
104 
105 void
XkbEscapeMapName(char * name)106 XkbEscapeMapName(char *name)
107 {
108     /*
109      * All latin-1 alphanumerics, plus parens, slash, minus, underscore and
110      * wildcards.
111      */
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
117     };
118 
119     if (!name)
120         return;
121 
122     while (*name) {
123         unsigned char c = *name;
124         if (!(legal[c / 8] & (1 << (c % 8))))
125             *name = '_';
126         name++;
127     }
128 }
129 
130 xkb_mod_index_t
XkbModNameToIndex(const struct xkb_mod_set * mods,xkb_atom_t name,enum mod_type type)131 XkbModNameToIndex(const struct xkb_mod_set *mods, xkb_atom_t name,
132                   enum mod_type type)
133 {
134     xkb_mod_index_t i;
135     const struct xkb_mod *mod;
136 
137     xkb_mods_enumerate(i, mod, mods)
138         if ((mod->type & type) && name == mod->name)
139             return i;
140 
141     return XKB_MOD_INVALID;
142 }
143 
144 bool
XkbLevelsSameSyms(const struct xkb_level * a,const struct xkb_level * b)145 XkbLevelsSameSyms(const struct xkb_level *a, const struct xkb_level *b)
146 {
147     if (a->num_syms != b->num_syms)
148         return false;
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;
152 }
153