Add struct xkb_mod_set
[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     const struct xkb_mod *mod;
36     xkb_mod_index_t i;
37
38     /* The effective mask is only real mods for now. */
39     mods->mask = mods->mods & MOD_REAL_MASK_ALL;
40
41     darray_enumerate(i, mod, keymap->mods.mods)
42         if (mods->mods & (1u << i))
43             mods->mask |= mod->mapping;
44 }
45
46 static void
47 UpdateActionMods(struct xkb_keymap *keymap, union xkb_action *act,
48                  xkb_mod_mask_t modmap)
49 {
50     switch (act->type) {
51     case ACTION_TYPE_MOD_SET:
52     case ACTION_TYPE_MOD_LATCH:
53     case ACTION_TYPE_MOD_LOCK:
54         if (act->mods.flags & ACTION_MODS_LOOKUP_MODMAP)
55             act->mods.mods.mods = modmap;
56         ComputeEffectiveMask(keymap, &act->mods.mods);
57         break;
58     default:
59         break;
60     }
61 }
62
63 static const struct xkb_sym_interpret default_interpret = {
64     .sym = XKB_KEY_NoSymbol,
65     .repeat = true,
66     .match = MATCH_ANY_OR_NONE,
67     .mods = 0,
68     .virtual_mod = XKB_MOD_INVALID,
69     .action = { .type = ACTION_TYPE_NONE },
70 };
71
72 /**
73  * Find an interpretation which applies to this particular level, either by
74  * finding an exact match for the symbol and modifier combination, or a
75  * generic XKB_KEY_NoSymbol match.
76  */
77 static const struct xkb_sym_interpret *
78 FindInterpForKey(struct xkb_keymap *keymap, const struct xkb_key *key,
79                  xkb_layout_index_t group, xkb_level_index_t level)
80 {
81     const xkb_keysym_t *syms;
82     int num_syms;
83
84     num_syms = xkb_keymap_key_get_syms_by_level(keymap, key->keycode, group,
85                                                 level, &syms);
86     if (num_syms == 0)
87         return NULL;
88
89     /*
90      * There may be multiple matchings interprets; we should always return
91      * the most specific. Here we rely on compat.c to set up the
92      * sym_interprets array from the most specific to the least specific,
93      * such that when we find a match we return immediately.
94      */
95     for (unsigned i = 0; i < keymap->num_sym_interprets; i++) {
96         const struct xkb_sym_interpret *interp = &keymap->sym_interprets[i];
97
98         xkb_mod_mask_t mods;
99         bool found = false;
100
101         if ((num_syms > 1 || interp->sym != syms[0]) &&
102             interp->sym != XKB_KEY_NoSymbol)
103             continue;
104
105         if (interp->level_one_only && level != 0)
106             mods = 0;
107         else
108             mods = key->modmap;
109
110         switch (interp->match) {
111         case MATCH_NONE:
112             found = !(interp->mods & mods);
113             break;
114         case MATCH_ANY_OR_NONE:
115             found = (!mods || (interp->mods & mods));
116             break;
117         case MATCH_ANY:
118             found = !!(interp->mods & mods);
119             break;
120         case MATCH_ALL:
121             found = ((interp->mods & mods) == interp->mods);
122             break;
123         case MATCH_EXACTLY:
124             found = (interp->mods == mods);
125             break;
126         }
127
128         if (found)
129             return interp;
130     }
131
132     return &default_interpret;
133 }
134
135 static bool
136 ApplyInterpsToKey(struct xkb_keymap *keymap, struct xkb_key *key)
137 {
138     xkb_mod_mask_t vmodmap = 0;
139     xkb_layout_index_t group;
140     xkb_level_index_t level;
141
142     /* If we've been told not to bind interps to this key, then don't. */
143     if (key->explicit & EXPLICIT_INTERP)
144         return true;
145
146     for (group = 0; group < key->num_groups; group++) {
147         for (level = 0; level < XkbKeyGroupWidth(key, group); level++) {
148             const struct xkb_sym_interpret *interp;
149
150             interp = FindInterpForKey(keymap, key, group, level);
151             if (!interp)
152                 continue;
153
154             /* Infer default key behaviours from the base level. */
155             if (group == 0 && level == 0)
156                 if (!(key->explicit & EXPLICIT_REPEAT) && interp->repeat)
157                     key->repeats = true;
158
159             if ((group == 0 && level == 0) || !interp->level_one_only)
160                 if (interp->virtual_mod != XKB_MOD_INVALID)
161                     vmodmap |= (1u << interp->virtual_mod);
162
163             if (interp->action.type != ACTION_TYPE_NONE)
164                 key->groups[group].levels[level].action = interp->action;
165         }
166     }
167
168     if (!(key->explicit & EXPLICIT_VMODMAP))
169         key->vmodmap = vmodmap;
170
171     return true;
172 }
173
174 /**
175  * This collects a bunch of disparate functions which was done in the server
176  * at various points that really should've been done within xkbcomp.  Turns out
177  * your actions and types are a lot more useful when any of your modifiers
178  * other than Shift actually do something ...
179  */
180 static bool
181 UpdateDerivedKeymapFields(struct xkb_keymap *keymap)
182 {
183     struct xkb_mod *mod;
184     struct xkb_led *led;
185     unsigned int i, j;
186     struct xkb_key *key;
187
188     /* Find all the interprets for the key and bind them to actions,
189      * which will also update the vmodmap. */
190     xkb_foreach_key(key, keymap)
191         if (!ApplyInterpsToKey(keymap, key))
192             return false;
193
194     /* Update keymap->mods, the virtual -> real mod mapping. */
195     xkb_foreach_key(key, keymap)
196         darray_enumerate(i, mod, keymap->mods.mods)
197             if (key->vmodmap & (1u << i))
198                 mod->mapping |= key->modmap;
199
200     /* Now update the level masks for all the types to reflect the vmods. */
201     for (i = 0; i < keymap->num_types; i++) {
202         ComputeEffectiveMask(keymap, &keymap->types[i].mods);
203
204         for (j = 0; j < keymap->types[i].num_entries; j++) {
205             ComputeEffectiveMask(keymap, &keymap->types[i].entries[j].mods);
206             ComputeEffectiveMask(keymap, &keymap->types[i].entries[j].preserve);
207         }
208     }
209
210     /* Update action modifiers. */
211     xkb_foreach_key(key, keymap)
212         for (i = 0; i < key->num_groups; i++)
213             for (j = 0; j < XkbKeyGroupWidth(key, i); j++)
214                 UpdateActionMods(keymap, &key->groups[i].levels[j].action,
215                                  key->modmap);
216
217     /* Update vmod -> led maps. */
218     darray_foreach(led, keymap->leds)
219         ComputeEffectiveMask(keymap, &led->mods);
220
221     /* Find maximum number of groups out of all keys in the keymap. */
222     xkb_foreach_key(key, keymap)
223         keymap->num_groups = MAX(keymap->num_groups, key->num_groups);
224
225     return true;
226 }
227
228 typedef bool (*compile_file_fn)(XkbFile *file,
229                                 struct xkb_keymap *keymap,
230                                 enum merge_mode merge);
231
232 static const compile_file_fn compile_file_fns[LAST_KEYMAP_FILE_TYPE + 1] = {
233     [FILE_TYPE_KEYCODES] = CompileKeycodes,
234     [FILE_TYPE_TYPES] = CompileKeyTypes,
235     [FILE_TYPE_COMPAT] = CompileCompatMap,
236     [FILE_TYPE_SYMBOLS] = CompileSymbols,
237 };
238
239 bool
240 CompileKeymap(XkbFile *file, struct xkb_keymap *keymap, enum merge_mode merge)
241 {
242     bool ok;
243     const char *main_name;
244     XkbFile *files[LAST_KEYMAP_FILE_TYPE + 1] = { NULL };
245     enum xkb_file_type type;
246     struct xkb_context *ctx = keymap->ctx;
247
248     main_name = file->name ? file->name : "(unnamed)";
249
250     /* Collect section files and check for duplicates. */
251     for (file = (XkbFile *) file->defs; file;
252          file = (XkbFile *) file->common.next) {
253         if (file->file_type < FIRST_KEYMAP_FILE_TYPE ||
254             file->file_type > LAST_KEYMAP_FILE_TYPE) {
255             log_err(ctx, "Cannot define %s in a keymap file\n",
256                     xkb_file_type_to_string(file->file_type));
257             continue;
258         }
259
260         if (files[file->file_type]) {
261             log_err(ctx,
262                     "More than one %s section in keymap file; "
263                     "All sections after the first ignored\n",
264                     xkb_file_type_to_string(file->file_type));
265             continue;
266         }
267
268         if (!file->topName) {
269             free(file->topName);
270             file->topName = strdup(main_name);
271         }
272
273         files[file->file_type] = file;
274     }
275
276     /*
277      * Check that all required section were provided.
278      * Report everything before failing.
279      */
280     ok = true;
281     for (type = FIRST_KEYMAP_FILE_TYPE;
282          type <= LAST_KEYMAP_FILE_TYPE;
283          type++) {
284         if (files[type] == NULL) {
285             log_err(ctx, "Required section %s missing from keymap\n",
286                     xkb_file_type_to_string(type));
287             ok = false;
288         }
289     }
290     if (!ok)
291         return false;
292
293     /* Compile sections. */
294     for (type = FIRST_KEYMAP_FILE_TYPE;
295          type <= LAST_KEYMAP_FILE_TYPE;
296          type++) {
297         log_dbg(ctx, "Compiling %s \"%s\"\n",
298                 xkb_file_type_to_string(type), files[type]->topName);
299
300         ok = compile_file_fns[type](files[type], keymap, merge);
301         if (!ok) {
302             log_err(ctx, "Failed to compile %s\n",
303                     xkb_file_type_to_string(type));
304             return false;
305         }
306     }
307
308     return UpdateDerivedKeymapFields(keymap);
309 }