2 * Copyright 2009 Dan Nicholson
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
18 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 * Except as contained in this notice, the names of the authors or their
22 * institutions shall not be used in advertising or otherwise to promote the
23 * sale, use or other dealings in this Software without prior written
24 * authorization from the authors.
27 #include "xkbcomp-priv.h"
30 ComputeEffectiveMask(struct xkb_keymap *keymap, struct xkb_mods *mods)
33 xkb_mod_mask_t vmask = mods->mods >> XKB_NUM_CORE_MODS;
35 /* The effective mask is only real mods for now. */
36 mods->mask = mods->mods & 0xff;
38 for (i = 0; i < XKB_NUM_VIRTUAL_MODS; i++) {
39 if (!(vmask & (1 << i)))
41 mods->mask |= keymap->vmods[i];
46 UpdateActionMods(struct xkb_keymap *keymap, union xkb_action *act,
47 xkb_mod_mask_t rmodmask)
50 struct xkb_mods *mods;
56 flags = act->mods.flags;
57 mods = &act->mods.mods;
61 flags = act->iso.flags;
62 mods = &act->iso.mods;
69 if (flags & XkbSA_UseModMapMods) {
70 /* XXX: what's that. */
72 mods->mods |= rmodmask;
74 ComputeEffectiveMask(keymap, mods);
78 * Find an interpretation which applies to this particular level, either by
79 * finding an exact match for the symbol and modifier combination, or a
80 * generic XKB_KEY_NoSymbol match.
82 static struct xkb_sym_interpret *
83 FindInterpForKey(struct xkb_keymap *keymap, struct xkb_key *key,
84 xkb_group_index_t group, xkb_level_index_t level)
86 struct xkb_sym_interpret *interp;
87 const xkb_keysym_t *syms;
90 num_syms = xkb_key_get_syms_by_level(keymap, key, group, level, &syms);
95 * There may be multiple matchings interprets; we should always return
96 * the most specific. Here we rely on compat.c to set up the
97 * sym_interpret array from the most specific to the least specific,
98 * such that when we find a match we return immediately.
100 darray_foreach(interp, keymap->sym_interpret) {
104 if ((num_syms > 1 || interp->sym != syms[0]) &&
105 interp->sym != XKB_KEY_NoSymbol)
108 if (level == 0 || !(interp->match & XkbSI_LevelOneOnly))
113 switch (interp->match & XkbSI_OpMask) {
115 found = !(interp->mods & mods);
117 case XkbSI_AnyOfOrNone:
118 found = (!mods || (interp->mods & mods));
121 found = !!(interp->mods & mods);
124 found = ((interp->mods & mods) == interp->mods);
127 found = (interp->mods == mods);
142 ApplyInterpsToKey(struct xkb_keymap *keymap, struct xkb_key *key)
144 xkb_mod_mask_t vmodmask = 0;
145 xkb_group_index_t group;
146 xkb_level_index_t width, level;
148 /* If we've been told not to bind interps to this key, then don't. */
149 if (key->explicit & XkbExplicitInterpretMask)
152 for (group = 0; group < key->num_groups; group++) {
153 width = XkbKeyGroupWidth(keymap, key, group);
154 for (level = 0; level < width; level++) {
155 struct xkb_sym_interpret *interp;
157 interp = FindInterpForKey(keymap, key, group, level);
159 /* Infer default key behaviours from the base level. */
160 if (group == 0 && level == 0) {
161 if (!(key->explicit & XkbExplicitAutoRepeatMask) &&
162 (!interp || (interp->flags & XkbSI_AutoRepeat)))
169 if ((group == 0 && level == 0) ||
170 !(interp->match & XkbSI_LevelOneOnly)) {
171 if (interp->virtual_mod != XKB_MOD_INVALID)
172 vmodmask |= (1 << interp->virtual_mod);
175 if (interp->act.type != XkbSA_NoAction) {
177 key->actions = calloc(key->num_groups * key->width,
178 sizeof(*key->actions));
183 *XkbKeyActionEntry(key, group, level) = interp->act;
188 if (!(key->explicit & XkbExplicitVModMapMask))
189 key->vmodmap = vmodmask;
195 * This collects a bunch of disparate functions which was done in the server
196 * at various points that really should've been done within xkbcomp. Turns out
197 * your actions and types are a lot more useful when any of your modifiers
198 * other than Shift actually do something ...
201 UpdateDerivedKeymapFields(struct xkb_keymap *keymap)
203 xkb_mod_index_t vmod;
208 /* Find all the interprets for the key and bind them to actions,
209 * which will also update the vmodmap. */
210 xkb_foreach_key(key, keymap)
211 if (!ApplyInterpsToKey(keymap, key))
214 /* Update keymap->vmods, the virtual -> real mod mapping. */
215 for (vmod = 0; vmod < XKB_NUM_VIRTUAL_MODS; vmod++)
216 keymap->vmods[vmod] = 0;
218 xkb_foreach_key(key, keymap) {
222 for (vmod = 0; vmod < XKB_NUM_VIRTUAL_MODS; vmod++) {
223 if (!(key->vmodmap & (1 << vmod)))
225 keymap->vmods[vmod] |= key->modmap;
229 /* Now update the level masks for all the types to reflect the vmods. */
230 for (i = 0; i < keymap->num_types; i++) {
231 ComputeEffectiveMask(keymap, &keymap->types[i].mods);
233 for (j = 0; j < keymap->types[i].num_entries; j++) {
234 ComputeEffectiveMask(keymap, &keymap->types[i].map[j].mods);
235 ComputeEffectiveMask(keymap, &keymap->types[i].map[j].preserve);
239 /* Update action modifiers. */
240 xkb_foreach_key(key, keymap) {
244 for (i = 0; i < key->num_groups * key->width; i++)
245 UpdateActionMods(keymap, &key->actions[i], key->modmap);
248 /* Update vmod -> indicator maps. */
249 for (led = 0; led < XKB_NUM_INDICATORS; led++)
250 ComputeEffectiveMask(keymap, &keymap->indicators[led].mods);
252 /* Find maximum number of groups out of all keys in the keymap. */
253 xkb_foreach_key(key, keymap)
254 keymap->num_groups = MAX(keymap->num_groups, key->num_groups);
259 typedef bool (*compile_file_fn)(XkbFile *file,
260 struct xkb_keymap *keymap,
261 enum merge_mode merge);
263 static const compile_file_fn compile_file_fns[LAST_KEYMAP_FILE_TYPE + 1] = {
264 [FILE_TYPE_KEYCODES] = CompileKeycodes,
265 [FILE_TYPE_TYPES] = CompileKeyTypes,
266 [FILE_TYPE_COMPAT] = CompileCompatMap,
267 [FILE_TYPE_SYMBOLS] = CompileSymbols,
271 CompileKeymap(XkbFile *file, struct xkb_keymap *keymap, enum merge_mode merge)
274 const char *main_name;
275 XkbFile *files[LAST_KEYMAP_FILE_TYPE + 1] = { NULL };
276 enum xkb_file_type type;
277 struct xkb_context *ctx = keymap->ctx;
279 main_name = file->name ? file->name : "(unnamed)";
281 /* Collect section files and check for duplicates. */
282 for (file = (XkbFile *) file->defs; file;
283 file = (XkbFile *) file->common.next) {
284 if (file->file_type < FIRST_KEYMAP_FILE_TYPE ||
285 file->file_type > LAST_KEYMAP_FILE_TYPE) {
286 log_err(ctx, "Cannot define %s in a keymap file\n",
287 xkb_file_type_to_string(file->file_type));
291 if (files[file->file_type]) {
293 "More than one %s section in keymap file; "
294 "All sections after the first ignored\n",
295 xkb_file_type_to_string(file->file_type));
299 if (!file->topName) {
301 file->topName = strdup(main_name);
304 files[file->file_type] = file;
308 * Check that all required section were provided.
309 * Report everything before failing.
312 for (type = FIRST_KEYMAP_FILE_TYPE;
313 type <= LAST_KEYMAP_FILE_TYPE;
315 if (files[type] == NULL) {
316 log_err(ctx, "Required section %s missing from keymap\n",
317 xkb_file_type_to_string(type));
324 /* Compile sections. */
325 for (type = FIRST_KEYMAP_FILE_TYPE;
326 type <= LAST_KEYMAP_FILE_TYPE;
328 log_dbg(ctx, "Compiling %s \"%s\"\n",
329 xkb_file_type_to_string(type), files[type]->topName);
331 ok = compile_file_fns[type](files[type], keymap, merge);
333 log_err(ctx, "Failed to compile %s\n",
334 xkb_file_type_to_string(type));
339 return UpdateDerivedKeymapFields(keymap);