keymap, symbols: improve xkb_key memory layout
[platform/upstream/libxkbcommon.git] / src / xkbcomp / keymap.c
1 /*
2  * Copyright © 2009 Dan Nicholson
3  * Copyright © 2012 Intel Corporation
4  * Copyright © 2012 Ran Benita <ran234@gmail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  * Author: Dan Nicholson <dbn.lists@gmail.com>
26  *         Daniel Stone <daniel@fooishbar.org>
27  *         Ran Benita <ran234@gmail.com>
28  */
29
30 #include "xkbcomp-priv.h"
31
32 static void
33 ComputeEffectiveMask(struct xkb_keymap *keymap, struct xkb_mods *mods)
34 {
35     xkb_mod_index_t i;
36     xkb_mod_mask_t vmask = mods->mods >> XKB_NUM_CORE_MODS;
37
38     /* The effective mask is only real mods for now. */
39     mods->mask = mods->mods & 0xff;
40
41     for (i = 0; i < XKB_NUM_VIRTUAL_MODS; i++) {
42         if (!(vmask & (1 << i)))
43             continue;
44         mods->mask |= keymap->vmods[i];
45     }
46 }
47
48 static void
49 UpdateActionMods(struct xkb_keymap *keymap, union xkb_action *act,
50                  xkb_mod_mask_t modmap)
51 {
52     switch (act->type) {
53     case ACTION_TYPE_MOD_SET:
54     case ACTION_TYPE_MOD_LATCH:
55     case ACTION_TYPE_MOD_LOCK:
56         if (act->mods.flags & ACTION_MODS_LOOKUP_MODMAP)
57             act->mods.mods.mods = modmap;
58         ComputeEffectiveMask(keymap, &act->mods.mods);
59         break;
60     default:
61         break;
62     }
63 }
64
65 /**
66  * Find an interpretation which applies to this particular level, either by
67  * finding an exact match for the symbol and modifier combination, or a
68  * generic XKB_KEY_NoSymbol match.
69  */
70 static struct xkb_sym_interpret *
71 FindInterpForKey(struct xkb_keymap *keymap, struct xkb_key *key,
72                  xkb_layout_index_t group, xkb_level_index_t level)
73 {
74     struct xkb_sym_interpret *interp;
75     const xkb_keysym_t *syms;
76     int num_syms;
77
78     num_syms = xkb_keymap_key_get_syms_by_level(keymap, key->keycode, group,
79                                                 level, &syms);
80     if (num_syms == 0)
81         return NULL;
82
83     /*
84      * There may be multiple matchings interprets; we should always return
85      * the most specific. Here we rely on compat.c to set up the
86      * sym_interpret array from the most specific to the least specific,
87      * such that when we find a match we return immediately.
88      */
89     darray_foreach(interp, keymap->sym_interpret) {
90         uint32_t mods;
91         bool found;
92
93         if ((num_syms > 1 || interp->sym != syms[0]) &&
94             interp->sym != XKB_KEY_NoSymbol)
95             continue;
96
97         if (level == 0 || !(interp->match & MATCH_LEVEL_ONE_ONLY))
98             mods = key->modmap;
99         else
100             mods = 0;
101
102         switch (interp->match & MATCH_OP_MASK) {
103         case MATCH_NONE:
104             found = !(interp->mods & mods);
105             break;
106         case MATCH_ANY_OR_NONE:
107             found = (!mods || (interp->mods & mods));
108             break;
109         case MATCH_ANY:
110             found = !!(interp->mods & mods);
111             break;
112         case MATCH_ALL:
113             found = ((interp->mods & mods) == interp->mods);
114             break;
115         case MATCH_EXACTLY:
116             found = (interp->mods == mods);
117             break;
118         default:
119             found = false;
120             break;
121         }
122
123         if (found)
124             return interp;
125     }
126
127     return NULL;
128 }
129
130 static bool
131 ApplyInterpsToKey(struct xkb_keymap *keymap, struct xkb_key *key)
132 {
133     xkb_mod_mask_t vmodmask = 0;
134     xkb_layout_index_t group;
135     xkb_level_index_t width, level;
136
137     /* If we've been told not to bind interps to this key, then don't. */
138     if (key->explicit & EXPLICIT_INTERP)
139         return true;
140
141     for (group = 0; group < key->num_groups; group++) {
142         width = XkbKeyGroupWidth(keymap, key, group);
143         for (level = 0; level < width; level++) {
144             struct xkb_sym_interpret *interp;
145
146             interp = FindInterpForKey(keymap, key, group, level);
147
148             /* Infer default key behaviours from the base level. */
149             if (group == 0 && level == 0) {
150                 if (!(key->explicit & EXPLICIT_REPEAT) &&
151                     (!interp || interp->repeat))
152                     key->repeats = true;
153             }
154
155             if (!interp)
156                 continue;
157
158             if ((group == 0 && level == 0) ||
159                 !(interp->match & MATCH_LEVEL_ONE_ONLY)) {
160                 if (interp->virtual_mod != XKB_MOD_INVALID)
161                     vmodmask |= (1 << interp->virtual_mod);
162             }
163
164             if (interp->act.type != ACTION_TYPE_NONE)
165                 key->groups[group].levels[level].action = interp->act;
166         }
167     }
168
169     if (!(key->explicit & EXPLICIT_VMODMAP))
170         key->vmodmap = vmodmask;
171
172     return true;
173 }
174
175 /**
176  * This collects a bunch of disparate functions which was done in the server
177  * at various points that really should've been done within xkbcomp.  Turns out
178  * your actions and types are a lot more useful when any of your modifiers
179  * other than Shift actually do something ...
180  */
181 static bool
182 UpdateDerivedKeymapFields(struct xkb_keymap *keymap)
183 {
184     xkb_mod_index_t vmod;
185     xkb_led_index_t led;
186     unsigned int i, j;
187     struct xkb_key *key;
188
189     /* Find all the interprets for the key and bind them to actions,
190      * which will also update the vmodmap. */
191     xkb_foreach_key(key, keymap)
192         if (!ApplyInterpsToKey(keymap, key))
193             return false;
194
195     /* Update keymap->vmods, the virtual -> real mod mapping. */
196     for (vmod = 0; vmod < XKB_NUM_VIRTUAL_MODS; vmod++)
197         keymap->vmods[vmod] = 0;
198
199     xkb_foreach_key(key, keymap) {
200         if (!key->vmodmap)
201             continue;
202
203         for (vmod = 0; vmod < XKB_NUM_VIRTUAL_MODS; vmod++) {
204             if (!(key->vmodmap & (1 << vmod)))
205                 continue;
206             keymap->vmods[vmod] |= key->modmap;
207         }
208     }
209
210     /* Now update the level masks for all the types to reflect the vmods. */
211     for (i = 0; i < keymap->num_types; i++) {
212         ComputeEffectiveMask(keymap, &keymap->types[i].mods);
213
214         for (j = 0; j < keymap->types[i].num_entries; j++) {
215             ComputeEffectiveMask(keymap, &keymap->types[i].map[j].mods);
216             ComputeEffectiveMask(keymap, &keymap->types[i].map[j].preserve);
217         }
218     }
219
220     /* Update action modifiers. */
221     xkb_foreach_key(key, keymap)
222         for (i = 0; i < key->num_groups; i++)
223             for (j = 0; j < XkbKeyGroupWidth(keymap, key, i); j++)
224                 UpdateActionMods(keymap, &key->groups[i].levels[j].action,
225                                  key->modmap);
226
227     /* Update vmod -> indicator maps. */
228     for (led = 0; led < XKB_NUM_INDICATORS; led++)
229         ComputeEffectiveMask(keymap, &keymap->indicators[led].mods);
230
231     /* Find maximum number of groups out of all keys in the keymap. */
232     xkb_foreach_key(key, keymap)
233         keymap->num_groups = MAX(keymap->num_groups, key->num_groups);
234
235     return true;
236 }
237
238 typedef bool (*compile_file_fn)(XkbFile *file,
239                                 struct xkb_keymap *keymap,
240                                 enum merge_mode merge);
241
242 static const compile_file_fn compile_file_fns[LAST_KEYMAP_FILE_TYPE + 1] = {
243     [FILE_TYPE_KEYCODES] = CompileKeycodes,
244     [FILE_TYPE_TYPES] = CompileKeyTypes,
245     [FILE_TYPE_COMPAT] = CompileCompatMap,
246     [FILE_TYPE_SYMBOLS] = CompileSymbols,
247 };
248
249 bool
250 CompileKeymap(XkbFile *file, struct xkb_keymap *keymap, enum merge_mode merge)
251 {
252     bool ok;
253     const char *main_name;
254     XkbFile *files[LAST_KEYMAP_FILE_TYPE + 1] = { NULL };
255     enum xkb_file_type type;
256     struct xkb_context *ctx = keymap->ctx;
257
258     main_name = file->name ? file->name : "(unnamed)";
259
260     /* Collect section files and check for duplicates. */
261     for (file = (XkbFile *) file->defs; file;
262          file = (XkbFile *) file->common.next) {
263         if (file->file_type < FIRST_KEYMAP_FILE_TYPE ||
264             file->file_type > LAST_KEYMAP_FILE_TYPE) {
265             log_err(ctx, "Cannot define %s in a keymap file\n",
266                     xkb_file_type_to_string(file->file_type));
267             continue;
268         }
269
270         if (files[file->file_type]) {
271             log_err(ctx,
272                     "More than one %s section in keymap file; "
273                     "All sections after the first ignored\n",
274                     xkb_file_type_to_string(file->file_type));
275             continue;
276         }
277
278         if (!file->topName) {
279             free(file->topName);
280             file->topName = strdup(main_name);
281         }
282
283         files[file->file_type] = file;
284     }
285
286     /*
287      * Check that all required section were provided.
288      * Report everything before failing.
289      */
290     ok = true;
291     for (type = FIRST_KEYMAP_FILE_TYPE;
292          type <= LAST_KEYMAP_FILE_TYPE;
293          type++) {
294         if (files[type] == NULL) {
295             log_err(ctx, "Required section %s missing from keymap\n",
296                     xkb_file_type_to_string(type));
297             ok = false;
298         }
299     }
300     if (!ok)
301         return false;
302
303     /* Compile sections. */
304     for (type = FIRST_KEYMAP_FILE_TYPE;
305          type <= LAST_KEYMAP_FILE_TYPE;
306          type++) {
307         log_dbg(ctx, "Compiling %s \"%s\"\n",
308                 xkb_file_type_to_string(type), files[type]->topName);
309
310         ok = compile_file_fns[type](files[type], keymap, merge);
311         if (!ok) {
312             log_err(ctx, "Failed to compile %s\n",
313                     xkb_file_type_to_string(type));
314             return false;
315         }
316     }
317
318     return UpdateDerivedKeymapFields(keymap);
319 }