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>
30 #include "xkbcomp-priv.h"
33 ComputeEffectiveMask(struct xkb_keymap *keymap, struct xkb_mods *mods)
36 xkb_mod_mask_t vmask = mods->mods >> XKB_NUM_CORE_MODS;
38 /* The effective mask is only real mods for now. */
39 mods->mask = mods->mods & 0xff;
41 for (i = 0; i < XKB_NUM_VIRTUAL_MODS; i++) {
42 if (!(vmask & (1 << i)))
44 mods->mask |= keymap->vmods[i];
49 UpdateActionMods(struct xkb_keymap *keymap, union xkb_action *act,
50 xkb_mod_mask_t modmap)
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);
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.
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)
74 struct xkb_sym_interpret *interp;
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_interpret array from the most specific to the least specific,
87 * such that when we find a match we return immediately.
89 darray_foreach(interp, keymap->sym_interpret) {
93 if ((num_syms > 1 || interp->sym != syms[0]) &&
94 interp->sym != XKB_KEY_NoSymbol)
97 if (level == 0 || !(interp->match & MATCH_LEVEL_ONE_ONLY))
102 switch (interp->match & MATCH_OP_MASK) {
104 found = !(interp->mods & mods);
106 case MATCH_ANY_OR_NONE:
107 found = (!mods || (interp->mods & mods));
110 found = !!(interp->mods & mods);
113 found = ((interp->mods & mods) == interp->mods);
116 found = (interp->mods == mods);
131 ApplyInterpsToKey(struct xkb_keymap *keymap, struct xkb_key *key)
133 xkb_mod_mask_t vmodmask = 0;
134 xkb_group_index_t group;
135 xkb_level_index_t width, level;
137 /* If we've been told not to bind interps to this key, then don't. */
138 if (key->explicit & EXPLICIT_INTERP)
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;
146 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) &&
151 (!interp || interp->repeat))
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);
164 if (interp->act.type != ACTION_TYPE_NONE) {
166 key->actions = calloc(key->num_groups * key->width,
167 sizeof(*key->actions));
172 key->actions[group * key->width + level] = interp->act;
177 if (!(key->explicit & EXPLICIT_VMODMAP))
178 key->vmodmap = vmodmask;
184 * This collects a bunch of disparate functions which was done in the server
185 * at various points that really should've been done within xkbcomp. Turns out
186 * your actions and types are a lot more useful when any of your modifiers
187 * other than Shift actually do something ...
190 UpdateDerivedKeymapFields(struct xkb_keymap *keymap)
192 xkb_mod_index_t vmod;
197 /* Find all the interprets for the key and bind them to actions,
198 * which will also update the vmodmap. */
199 xkb_foreach_key(key, keymap)
200 if (!ApplyInterpsToKey(keymap, key))
203 /* Update keymap->vmods, the virtual -> real mod mapping. */
204 for (vmod = 0; vmod < XKB_NUM_VIRTUAL_MODS; vmod++)
205 keymap->vmods[vmod] = 0;
207 xkb_foreach_key(key, keymap) {
211 for (vmod = 0; vmod < XKB_NUM_VIRTUAL_MODS; vmod++) {
212 if (!(key->vmodmap & (1 << vmod)))
214 keymap->vmods[vmod] |= key->modmap;
218 /* Now update the level masks for all the types to reflect the vmods. */
219 for (i = 0; i < keymap->num_types; i++) {
220 ComputeEffectiveMask(keymap, &keymap->types[i].mods);
222 for (j = 0; j < keymap->types[i].num_entries; j++) {
223 ComputeEffectiveMask(keymap, &keymap->types[i].map[j].mods);
224 ComputeEffectiveMask(keymap, &keymap->types[i].map[j].preserve);
228 /* Update action modifiers. */
229 xkb_foreach_key(key, keymap) {
233 for (i = 0; i < key->num_groups * key->width; i++)
234 UpdateActionMods(keymap, &key->actions[i], key->modmap);
237 /* Update vmod -> indicator maps. */
238 for (led = 0; led < XKB_NUM_INDICATORS; led++)
239 ComputeEffectiveMask(keymap, &keymap->indicators[led].mods);
241 /* Find maximum number of groups out of all keys in the keymap. */
242 xkb_foreach_key(key, keymap)
243 keymap->num_groups = MAX(keymap->num_groups, key->num_groups);
248 typedef bool (*compile_file_fn)(XkbFile *file,
249 struct xkb_keymap *keymap,
250 enum merge_mode merge);
252 static const compile_file_fn compile_file_fns[LAST_KEYMAP_FILE_TYPE + 1] = {
253 [FILE_TYPE_KEYCODES] = CompileKeycodes,
254 [FILE_TYPE_TYPES] = CompileKeyTypes,
255 [FILE_TYPE_COMPAT] = CompileCompatMap,
256 [FILE_TYPE_SYMBOLS] = CompileSymbols,
260 CompileKeymap(XkbFile *file, struct xkb_keymap *keymap, enum merge_mode merge)
263 const char *main_name;
264 XkbFile *files[LAST_KEYMAP_FILE_TYPE + 1] = { NULL };
265 enum xkb_file_type type;
266 struct xkb_context *ctx = keymap->ctx;
268 main_name = file->name ? file->name : "(unnamed)";
270 /* Collect section files and check for duplicates. */
271 for (file = (XkbFile *) file->defs; file;
272 file = (XkbFile *) file->common.next) {
273 if (file->file_type < FIRST_KEYMAP_FILE_TYPE ||
274 file->file_type > LAST_KEYMAP_FILE_TYPE) {
275 log_err(ctx, "Cannot define %s in a keymap file\n",
276 xkb_file_type_to_string(file->file_type));
280 if (files[file->file_type]) {
282 "More than one %s section in keymap file; "
283 "All sections after the first ignored\n",
284 xkb_file_type_to_string(file->file_type));
288 if (!file->topName) {
290 file->topName = strdup(main_name);
293 files[file->file_type] = file;
297 * Check that all required section were provided.
298 * Report everything before failing.
301 for (type = FIRST_KEYMAP_FILE_TYPE;
302 type <= LAST_KEYMAP_FILE_TYPE;
304 if (files[type] == NULL) {
305 log_err(ctx, "Required section %s missing from keymap\n",
306 xkb_file_type_to_string(type));
313 /* Compile sections. */
314 for (type = FIRST_KEYMAP_FILE_TYPE;
315 type <= LAST_KEYMAP_FILE_TYPE;
317 log_dbg(ctx, "Compiling %s \"%s\"\n",
318 xkb_file_type_to_string(type), files[type]->topName);
320 ok = compile_file_fns[type](files[type], keymap, merge);
322 log_err(ctx, "Failed to compile %s\n",
323 xkb_file_type_to_string(type));
328 return UpdateDerivedKeymapFields(keymap);