xkbcomp/keymap: correct ACTION_MODS_LOOKUP_MODS handling
[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_group_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_key_get_syms_by_level(keymap, key, group, level, &syms);
79     if (num_syms == 0)
80         return NULL;
81
82     /*
83      * There may be multiple matchings interprets; we should always return
84      * the most specific. Here we rely on compat.c to set up the
85      * sym_interpret array from the most specific to the least specific,
86      * such that when we find a match we return immediately.
87      */
88     darray_foreach(interp, keymap->sym_interpret) {
89         uint32_t mods;
90         bool found;
91
92         if ((num_syms > 1 || interp->sym != syms[0]) &&
93             interp->sym != XKB_KEY_NoSymbol)
94             continue;
95
96         if (level == 0 || !(interp->match & MATCH_LEVEL_ONE_ONLY))
97             mods = key->modmap;
98         else
99             mods = 0;
100
101         switch (interp->match & MATCH_OP_MASK) {
102         case MATCH_NONE:
103             found = !(interp->mods & mods);
104             break;
105         case MATCH_ANY_OR_NONE:
106             found = (!mods || (interp->mods & mods));
107             break;
108         case MATCH_ANY:
109             found = !!(interp->mods & mods);
110             break;
111         case MATCH_ALL:
112             found = ((interp->mods & mods) == interp->mods);
113             break;
114         case MATCH_EXACTLY:
115             found = (interp->mods == mods);
116             break;
117         default:
118             found = false;
119             break;
120         }
121
122         if (found)
123             return interp;
124     }
125
126     return NULL;
127 }
128
129 static bool
130 ApplyInterpsToKey(struct xkb_keymap *keymap, struct xkb_key *key)
131 {
132     xkb_mod_mask_t vmodmask = 0;
133     xkb_group_index_t group;
134     xkb_level_index_t width, level;
135
136     /* If we've been told not to bind interps to this key, then don't. */
137     if (key->explicit & EXPLICIT_INTERP)
138         return true;
139
140     for (group = 0; group < key->num_groups; group++) {
141         width = XkbKeyGroupWidth(keymap, key, group);
142         for (level = 0; level < width; level++) {
143             struct xkb_sym_interpret *interp;
144
145             interp = FindInterpForKey(keymap, key, group, level);
146
147             /* Infer default key behaviours from the base level. */
148             if (group == 0 && level == 0) {
149                 if (!(key->explicit & EXPLICIT_REPEAT) &&
150                     (!interp || interp->repeat))
151                     key->repeats = true;
152             }
153
154             if (!interp)
155                 continue;
156
157             if ((group == 0 && level == 0) ||
158                 !(interp->match & MATCH_LEVEL_ONE_ONLY)) {
159                 if (interp->virtual_mod != XKB_MOD_INVALID)
160                     vmodmask |= (1 << interp->virtual_mod);
161             }
162
163             if (interp->act.type != ACTION_TYPE_NONE) {
164                 if (!key->actions) {
165                     key->actions = calloc(key->num_groups * key->width,
166                                           sizeof(*key->actions));
167                     if (!key->actions)
168                         return false;
169                 }
170
171                 key->actions[group * key->width + level] = interp->act;
172             }
173         }
174     }
175
176     if (!(key->explicit & EXPLICIT_VMODMAP))
177         key->vmodmap = vmodmask;
178
179     return true;
180 }
181
182 /**
183  * This collects a bunch of disparate functions which was done in the server
184  * at various points that really should've been done within xkbcomp.  Turns out
185  * your actions and types are a lot more useful when any of your modifiers
186  * other than Shift actually do something ...
187  */
188 static bool
189 UpdateDerivedKeymapFields(struct xkb_keymap *keymap)
190 {
191     xkb_mod_index_t vmod;
192     xkb_led_index_t led;
193     unsigned int i, j;
194     struct xkb_key *key;
195
196     /* Find all the interprets for the key and bind them to actions,
197      * which will also update the vmodmap. */
198     xkb_foreach_key(key, keymap)
199         if (!ApplyInterpsToKey(keymap, key))
200             return false;
201
202     /* Update keymap->vmods, the virtual -> real mod mapping. */
203     for (vmod = 0; vmod < XKB_NUM_VIRTUAL_MODS; vmod++)
204         keymap->vmods[vmod] = 0;
205
206     xkb_foreach_key(key, keymap) {
207         if (!key->vmodmap)
208             continue;
209
210         for (vmod = 0; vmod < XKB_NUM_VIRTUAL_MODS; vmod++) {
211             if (!(key->vmodmap & (1 << vmod)))
212                 continue;
213             keymap->vmods[vmod] |= key->modmap;
214         }
215     }
216
217     /* Now update the level masks for all the types to reflect the vmods. */
218     for (i = 0; i < keymap->num_types; i++) {
219         ComputeEffectiveMask(keymap, &keymap->types[i].mods);
220
221         for (j = 0; j < keymap->types[i].num_entries; j++) {
222             ComputeEffectiveMask(keymap, &keymap->types[i].map[j].mods);
223             ComputeEffectiveMask(keymap, &keymap->types[i].map[j].preserve);
224         }
225     }
226
227     /* Update action modifiers. */
228     xkb_foreach_key(key, keymap) {
229         if (!key->actions)
230             continue;
231
232         for (i = 0; i < key->num_groups * key->width; i++)
233             UpdateActionMods(keymap, &key->actions[i], key->modmap);
234     }
235
236     /* Update vmod -> indicator maps. */
237     for (led = 0; led < XKB_NUM_INDICATORS; led++)
238         ComputeEffectiveMask(keymap, &keymap->indicators[led].mods);
239
240     /* Find maximum number of groups out of all keys in the keymap. */
241     xkb_foreach_key(key, keymap)
242         keymap->num_groups = MAX(keymap->num_groups, key->num_groups);
243
244     return true;
245 }
246
247 typedef bool (*compile_file_fn)(XkbFile *file,
248                                 struct xkb_keymap *keymap,
249                                 enum merge_mode merge);
250
251 static const compile_file_fn compile_file_fns[LAST_KEYMAP_FILE_TYPE + 1] = {
252     [FILE_TYPE_KEYCODES] = CompileKeycodes,
253     [FILE_TYPE_TYPES] = CompileKeyTypes,
254     [FILE_TYPE_COMPAT] = CompileCompatMap,
255     [FILE_TYPE_SYMBOLS] = CompileSymbols,
256 };
257
258 bool
259 CompileKeymap(XkbFile *file, struct xkb_keymap *keymap, enum merge_mode merge)
260 {
261     bool ok;
262     const char *main_name;
263     XkbFile *files[LAST_KEYMAP_FILE_TYPE + 1] = { NULL };
264     enum xkb_file_type type;
265     struct xkb_context *ctx = keymap->ctx;
266
267     main_name = file->name ? file->name : "(unnamed)";
268
269     /* Collect section files and check for duplicates. */
270     for (file = (XkbFile *) file->defs; file;
271          file = (XkbFile *) file->common.next) {
272         if (file->file_type < FIRST_KEYMAP_FILE_TYPE ||
273             file->file_type > LAST_KEYMAP_FILE_TYPE) {
274             log_err(ctx, "Cannot define %s in a keymap file\n",
275                     xkb_file_type_to_string(file->file_type));
276             continue;
277         }
278
279         if (files[file->file_type]) {
280             log_err(ctx,
281                     "More than one %s section in keymap file; "
282                     "All sections after the first ignored\n",
283                     xkb_file_type_to_string(file->file_type));
284             continue;
285         }
286
287         if (!file->topName) {
288             free(file->topName);
289             file->topName = strdup(main_name);
290         }
291
292         files[file->file_type] = file;
293     }
294
295     /*
296      * Check that all required section were provided.
297      * Report everything before failing.
298      */
299     ok = true;
300     for (type = FIRST_KEYMAP_FILE_TYPE;
301          type <= LAST_KEYMAP_FILE_TYPE;
302          type++) {
303         if (files[type] == NULL) {
304             log_err(ctx, "Required section %s missing from keymap\n",
305                     xkb_file_type_to_string(type));
306             ok = false;
307         }
308     }
309     if (!ok)
310         return false;
311
312     /* Compile sections. */
313     for (type = FIRST_KEYMAP_FILE_TYPE;
314          type <= LAST_KEYMAP_FILE_TYPE;
315          type++) {
316         log_dbg(ctx, "Compiling %s \"%s\"\n",
317                 xkb_file_type_to_string(type), files[type]->topName);
318
319         ok = compile_file_fns[type](files[type], keymap, merge);
320         if (!ok) {
321             log_err(ctx, "Failed to compile %s\n",
322                     xkb_file_type_to_string(type));
323             return false;
324         }
325     }
326
327     return UpdateDerivedKeymapFields(keymap);
328 }