2 * Copyright © 2009 Dan Nicholson
3 * Copyright © 2012 Intel Corporation
4 * Copyright © 2012 Ran Benita <ran234@gmail.com>
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:
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
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.
25 * Author: Dan Nicholson <dbn.lists@gmail.com>
26 * Daniel Stone <daniel@fooishbar.org>
27 * Ran Benita <ran234@gmail.com>
32 #include "xkbcomp-priv.h"
35 ComputeEffectiveMask(struct xkb_keymap *keymap, struct xkb_mods *mods)
37 mods->mask = mod_mask_get_effective(keymap, mods->mods);
41 UpdateActionMods(struct xkb_keymap *keymap, union xkb_action *act,
42 xkb_mod_mask_t modmap)
45 case ACTION_TYPE_MOD_SET:
46 case ACTION_TYPE_MOD_LATCH:
47 case ACTION_TYPE_MOD_LOCK:
48 if (act->mods.flags & ACTION_MODS_LOOKUP_MODMAP)
49 act->mods.mods.mods = modmap;
50 ComputeEffectiveMask(keymap, &act->mods.mods);
57 static const struct xkb_sym_interpret default_interpret = {
58 .sym = XKB_KEY_NoSymbol,
60 .match = MATCH_ANY_OR_NONE,
62 .virtual_mod = XKB_MOD_INVALID,
63 .action = { .type = ACTION_TYPE_NONE },
67 * Find an interpretation which applies to this particular level, either by
68 * finding an exact match for the symbol and modifier combination, or a
69 * generic XKB_KEY_NoSymbol match.
71 static const struct xkb_sym_interpret *
72 FindInterpForKey(struct xkb_keymap *keymap, const struct xkb_key *key,
73 xkb_layout_index_t group, xkb_level_index_t level)
75 const xkb_keysym_t *syms;
78 num_syms = xkb_keymap_key_get_syms_by_level(keymap, key->keycode, group,
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_interprets array from the most specific to the least specific,
87 * such that when we find a match we return immediately.
89 for (unsigned i = 0; i < keymap->num_sym_interprets; i++) {
90 const struct xkb_sym_interpret *interp = &keymap->sym_interprets[i];
95 if ((num_syms > 1 || interp->sym != syms[0]) &&
96 interp->sym != XKB_KEY_NoSymbol)
99 if (interp->level_one_only && level != 0)
104 switch (interp->match) {
106 found = !(interp->mods & mods);
108 case MATCH_ANY_OR_NONE:
109 found = (!mods || (interp->mods & mods));
112 found = (interp->mods & mods);
115 found = ((interp->mods & mods) == interp->mods);
118 found = (interp->mods == mods);
126 return &default_interpret;
130 ApplyInterpsToKey(struct xkb_keymap *keymap, struct xkb_key *key)
132 xkb_mod_mask_t vmodmap = 0;
133 xkb_layout_index_t group;
134 xkb_level_index_t level;
136 /* If we've been told not to bind interps to this key, then don't. */
137 if (key->explicit & EXPLICIT_INTERP)
140 for (group = 0; group < key->num_groups; group++) {
141 for (level = 0; level < XkbKeyNumLevels(key, group); level++) {
142 const struct xkb_sym_interpret *interp;
144 interp = FindInterpForKey(keymap, key, group, level);
148 /* Infer default key behaviours from the base level. */
149 if (group == 0 && level == 0)
150 if (!(key->explicit & EXPLICIT_REPEAT) && interp->repeat)
153 if ((group == 0 && level == 0) || !interp->level_one_only)
154 if (interp->virtual_mod != XKB_MOD_INVALID)
155 vmodmap |= (1u << interp->virtual_mod);
157 if (interp->action.type != ACTION_TYPE_NONE)
158 key->groups[group].levels[level].action = interp->action;
162 if (!(key->explicit & EXPLICIT_VMODMAP))
163 key->vmodmap = vmodmap;
169 * This collects a bunch of disparate functions which was done in the server
170 * at various points that really should've been done within xkbcomp. Turns out
171 * your actions and types are a lot more useful when any of your modifiers
172 * other than Shift actually do something ...
175 UpdateDerivedKeymapFields(struct xkb_keymap *keymap)
182 /* Find all the interprets for the key and bind them to actions,
183 * which will also update the vmodmap. */
184 xkb_keys_foreach(key, keymap)
185 if (!ApplyInterpsToKey(keymap, key))
188 /* Update keymap->mods, the virtual -> real mod mapping. */
189 xkb_keys_foreach(key, keymap)
190 xkb_mods_enumerate(i, mod, &keymap->mods)
191 if (key->vmodmap & (1u << i))
192 mod->mapping |= key->modmap;
194 /* Now update the level masks for all the types to reflect the vmods. */
195 for (i = 0; i < keymap->num_types; i++) {
196 ComputeEffectiveMask(keymap, &keymap->types[i].mods);
198 for (j = 0; j < keymap->types[i].num_entries; j++) {
199 ComputeEffectiveMask(keymap, &keymap->types[i].entries[j].mods);
200 ComputeEffectiveMask(keymap, &keymap->types[i].entries[j].preserve);
204 /* Update action modifiers. */
205 xkb_keys_foreach(key, keymap)
206 for (i = 0; i < key->num_groups; i++)
207 for (j = 0; j < XkbKeyNumLevels(key, i); j++)
208 UpdateActionMods(keymap, &key->groups[i].levels[j].action,
211 /* Update vmod -> led maps. */
212 xkb_leds_foreach(led, keymap)
213 ComputeEffectiveMask(keymap, &led->mods);
215 /* Find maximum number of groups out of all keys in the keymap. */
216 xkb_keys_foreach(key, keymap)
217 keymap->num_groups = MAX(keymap->num_groups, key->num_groups);
222 typedef bool (*compile_file_fn)(XkbFile *file,
223 struct xkb_keymap *keymap,
224 enum merge_mode merge);
226 static const compile_file_fn compile_file_fns[LAST_KEYMAP_FILE_TYPE + 1] = {
227 [FILE_TYPE_KEYCODES] = CompileKeycodes,
228 [FILE_TYPE_TYPES] = CompileKeyTypes,
229 [FILE_TYPE_COMPAT] = CompileCompatMap,
230 [FILE_TYPE_SYMBOLS] = CompileSymbols,
234 CompileKeymap(XkbFile *file, struct xkb_keymap *keymap, enum merge_mode merge)
237 XkbFile *files[LAST_KEYMAP_FILE_TYPE + 1] = { NULL };
238 enum xkb_file_type type;
239 struct xkb_context *ctx = keymap->ctx;
241 /* Collect section files and check for duplicates. */
242 for (file = (XkbFile *) file->defs; file;
243 file = (XkbFile *) file->common.next) {
244 if (file->file_type < FIRST_KEYMAP_FILE_TYPE ||
245 file->file_type > LAST_KEYMAP_FILE_TYPE) {
246 if (file->file_type == FILE_TYPE_GEOMETRY) {
248 "Geometry sections are not supported; ignoring\n");
250 log_err(ctx, "Cannot define %s in a keymap file\n",
251 xkb_file_type_to_string(file->file_type));
256 if (files[file->file_type]) {
258 "More than one %s section in keymap file; "
259 "All sections after the first ignored\n",
260 xkb_file_type_to_string(file->file_type));
264 files[file->file_type] = file;
268 * Check that all required section were provided.
269 * Report everything before failing.
272 for (type = FIRST_KEYMAP_FILE_TYPE;
273 type <= LAST_KEYMAP_FILE_TYPE;
275 if (files[type] == NULL) {
276 log_err(ctx, "Required section %s missing from keymap\n",
277 xkb_file_type_to_string(type));
284 /* Compile sections. */
285 for (type = FIRST_KEYMAP_FILE_TYPE;
286 type <= LAST_KEYMAP_FILE_TYPE;
288 log_dbg(ctx, "Compiling %s \"%s\"\n",
289 xkb_file_type_to_string(type), files[type]->name);
291 ok = compile_file_fns[type](files[type], keymap, merge);
293 log_err(ctx, "Failed to compile %s\n",
294 xkb_file_type_to_string(type));
299 return UpdateDerivedKeymapFields(keymap);