1 /************************************************************
2 * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
4 * Permission to use, copy, modify, and distribute this
5 * software and its documentation for any purpose and without
6 * fee is hereby granted, provided that the above copyright
7 * notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting
9 * documentation, and that the name of Silicon Graphics not be
10 * used in advertising or publicity pertaining to distribution
11 * of the software without specific prior written permission.
12 * Silicon Graphics makes no representation about the suitability
13 * of this software for any purpose. It is provided "as is"
14 * without any express or implied warranty.
16 * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
23 * THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 ********************************************************/
27 #include "xkbcomp-priv.h"
34 TYPE_FIELD_MASK = (1 << 0),
35 TYPE_FIELD_MAP = (1 << 1),
36 TYPE_FIELD_PRESERVE = (1 << 2),
37 TYPE_FIELD_LEVEL_NAME = (1 << 3),
41 enum type_field defined;
42 enum merge_mode merge;
46 xkb_level_index_t num_levels;
47 darray(struct xkb_key_type_entry) entries;
48 darray(xkb_atom_t) level_names;
55 darray(KeyTypeInfo) types;
56 struct xkb_keymap *keymap;
59 /***====================================================================***/
61 static inline const char *
62 MapEntryTxt(KeyTypesInfo *info, struct xkb_key_type_entry *entry)
64 return ModMaskText(info->keymap, entry->mods.mods);
67 static inline const char *
68 TypeTxt(KeyTypesInfo *info, KeyTypeInfo *type)
70 return xkb_atom_text(info->keymap->ctx, type->name);
73 static inline const char *
74 TypeMaskTxt(KeyTypesInfo *info, KeyTypeInfo *type)
76 return ModMaskText(info->keymap, type->mods);
80 ReportTypeShouldBeArray(KeyTypesInfo *info, KeyTypeInfo *type,
83 return ReportShouldBeArray(info->keymap->ctx, "key type", field,
88 ReportTypeBadType(KeyTypesInfo *info, KeyTypeInfo *type,
89 const char *field, const char *wanted)
91 return ReportBadType(info->keymap->ctx, "key type", field,
92 TypeTxt(info, type), wanted);
95 /***====================================================================***/
98 InitKeyTypesInfo(KeyTypesInfo *info, struct xkb_keymap *keymap)
100 memset(info, 0, sizeof(*info));
101 info->keymap = keymap;
105 ClearKeyTypeInfo(KeyTypeInfo *type)
107 darray_free(type->entries);
108 darray_free(type->level_names);
112 ClearKeyTypesInfo(KeyTypesInfo *info)
115 darray_free(info->types);
119 FindMatchingKeyType(KeyTypesInfo *info, xkb_atom_t name)
123 darray_foreach(old, info->types)
124 if (old->name == name)
131 AddKeyType(KeyTypesInfo *info, KeyTypeInfo *new, bool same_file)
134 const int verbosity = xkb_context_get_log_verbosity(info->keymap->ctx);
136 old = FindMatchingKeyType(info, new->name);
138 if (new->merge == MERGE_REPLACE || new->merge == MERGE_OVERRIDE) {
139 if ((same_file && verbosity > 0) || verbosity > 9) {
140 log_warn(info->keymap->ctx,
141 "Multiple definitions of the %s key type; "
142 "Earlier definition ignored\n",
143 xkb_atom_text(info->keymap->ctx, new->name));
146 ClearKeyTypeInfo(old);
148 darray_init(new->entries);
149 darray_init(new->level_names);
154 log_vrb(info->keymap->ctx, 4,
155 "Multiple definitions of the %s key type; "
156 "Later definition ignored\n",
157 xkb_atom_text(info->keymap->ctx, new->name));
159 ClearKeyTypeInfo(new);
163 darray_append(info->types, *new);
167 /***====================================================================***/
170 MergeIncludedKeyTypes(KeyTypesInfo *into, KeyTypesInfo *from,
171 enum merge_mode merge)
175 if (from->errorCount > 0) {
176 into->errorCount += from->errorCount;
180 if (into->name == NULL) {
181 into->name = from->name;
185 if (darray_empty(into->types)) {
186 into->types = from->types;
187 darray_init(from->types);
190 darray_foreach(type, from->types) {
191 type->merge = (merge == MERGE_DEFAULT ? type->merge : merge);
192 if (!AddKeyType(into, type, false))
199 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge);
202 HandleIncludeKeyTypes(KeyTypesInfo *info, IncludeStmt *include)
204 KeyTypesInfo included;
206 InitKeyTypesInfo(&included, info->keymap);
207 included.name = include->stmt;
208 include->stmt = NULL;
210 for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
211 KeyTypesInfo next_incl;
214 file = ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_TYPES);
216 info->errorCount += 10;
217 ClearKeyTypesInfo(&included);
221 InitKeyTypesInfo(&next_incl, info->keymap);
223 HandleKeyTypesFile(&next_incl, file, stmt->merge);
225 MergeIncludedKeyTypes(&included, &next_incl, stmt->merge);
227 ClearKeyTypesInfo(&next_incl);
231 MergeIncludedKeyTypes(info, &included, include->merge);
232 ClearKeyTypesInfo(&included);
234 return (info->errorCount == 0);
237 /***====================================================================***/
240 SetModifiers(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
246 log_warn(info->keymap->ctx,
247 "The modifiers field of a key type is not an array; "
248 "Illegal array subscript ignored\n");
250 if (!ExprResolveModMask(info->keymap, value, MOD_BOTH, &mods)) {
251 log_err(info->keymap->ctx,
252 "Key type mask field must be a modifier mask; "
253 "Key type definition ignored\n");
257 if (type->defined & TYPE_FIELD_MASK) {
258 log_warn(info->keymap->ctx,
259 "Multiple modifier mask definitions for key type %s; "
260 "Using %s, ignoring %s\n",
261 xkb_atom_text(info->keymap->ctx, type->name),
262 TypeMaskTxt(info, type),
263 ModMaskText(info->keymap, mods));
271 /***====================================================================***/
273 static struct xkb_key_type_entry *
274 FindMatchingMapEntry(KeyTypeInfo *type, xkb_mod_mask_t mods)
276 struct xkb_key_type_entry *entry;
278 darray_foreach(entry, type->entries)
279 if (entry->mods.mods == mods)
286 AddMapEntry(KeyTypesInfo *info, KeyTypeInfo *type,
287 struct xkb_key_type_entry *new, bool clobber, bool report)
289 struct xkb_key_type_entry *old;
291 old = FindMatchingMapEntry(type, new->mods.mods);
293 if (report && old->level != new->level) {
294 log_warn(info->keymap->ctx,
295 "Multiple map entries for %s in %s; "
296 "Using %d, ignoring %d\n",
297 MapEntryTxt(info, new), TypeTxt(info, type),
298 (clobber ? new->level : old->level) + 1,
299 (clobber ? old->level : new->level) + 1);
302 log_vrb(info->keymap->ctx, 10,
303 "Multiple occurrences of map[%s]= %d in %s; Ignored\n",
304 MapEntryTxt(info, new), new->level + 1,
305 TypeTxt(info, type));
310 if (new->level >= type->num_levels)
311 type->num_levels = new->level + 1;
312 old->level = new->level;
318 if (new->level >= type->num_levels)
319 type->num_levels = new->level + 1;
321 darray_append(type->entries, *new);
326 SetMapEntry(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
329 struct xkb_key_type_entry entry;
331 if (arrayNdx == NULL)
332 return ReportTypeShouldBeArray(info, type, "map entry");
334 if (!ExprResolveModMask(info->keymap, arrayNdx, MOD_BOTH, &entry.mods.mods))
335 return ReportTypeBadType(info, type, "map entry", "modifier mask");
337 if (entry.mods.mods & (~type->mods)) {
338 log_vrb(info->keymap->ctx, 1,
339 "Map entry for unused modifiers in %s; "
340 "Using %s instead of %s\n",
342 ModMaskText(info->keymap, entry.mods.mods & type->mods),
343 MapEntryTxt(info, &entry));
344 entry.mods.mods &= type->mods;
347 if (!ExprResolveLevel(info->keymap->ctx, value, &entry.level)) {
348 log_err(info->keymap->ctx,
349 "Level specifications in a key type must be integer; "
350 "Ignoring malformed level specification\n");
354 entry.preserve.mods = 0;
356 return AddMapEntry(info, type, &entry, true, true);
359 /***====================================================================***/
362 AddPreserve(KeyTypesInfo *info, KeyTypeInfo *type,
363 xkb_mod_mask_t mods, xkb_mod_mask_t preserve_mods)
365 struct xkb_key_type_entry *entry;
366 struct xkb_key_type_entry new;
368 darray_foreach(entry, type->entries) {
369 if (entry->mods.mods != mods)
372 /* Map exists without previous preserve (or "None"); override. */
373 if (entry->preserve.mods == 0) {
374 entry->preserve.mods = preserve_mods;
378 /* Map exists with same preserve; do nothing. */
379 if (entry->preserve.mods == preserve_mods) {
380 log_vrb(info->keymap->ctx, 10,
381 "Identical definitions for preserve[%s] in %s; "
383 ModMaskText(info->keymap, mods),
384 TypeTxt(info, type));
388 /* Map exists with different preserve; latter wins. */
389 log_vrb(info->keymap->ctx, 1,
390 "Multiple definitions for preserve[%s] in %s; "
391 "Using %s, ignoring %s\n",
392 ModMaskText(info->keymap, mods),
394 ModMaskText(info->keymap, preserve_mods),
395 ModMaskText(info->keymap, entry->preserve.mods));
397 entry->preserve.mods = preserve_mods;
402 * Map does not exist, i.e. preserve[] came before map[].
403 * Create a map with the specified mask mapping to Level1. The level
404 * may be overridden later with an explicit map[] statement.
407 new.mods.mods = mods;
408 new.preserve.mods = preserve_mods;
409 darray_append(type->entries, new);
414 SetPreserve(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
417 xkb_mod_mask_t mods, preserve_mods;
419 if (arrayNdx == NULL)
420 return ReportTypeShouldBeArray(info, type, "preserve entry");
422 if (!ExprResolveModMask(info->keymap, arrayNdx, MOD_BOTH, &mods))
423 return ReportTypeBadType(info, type, "preserve entry",
426 if (mods & ~type->mods) {
427 const char *before, *after;
429 before = ModMaskText(info->keymap, mods);
431 after = ModMaskText(info->keymap, mods);
433 log_vrb(info->keymap->ctx, 1,
434 "Preserve for modifiers not used by the %s type; "
435 "Index %s converted to %s\n",
436 TypeTxt(info, type), before, after);
439 if (!ExprResolveModMask(info->keymap, value, MOD_BOTH, &preserve_mods)) {
440 log_err(info->keymap->ctx,
441 "Preserve value in a key type is not a modifier mask; "
442 "Ignoring preserve[%s] in type %s\n",
443 ModMaskText(info->keymap, mods),
444 TypeTxt(info, type));
448 if (preserve_mods & ~mods) {
449 const char *before, *after;
451 before = ModMaskText(info->keymap, preserve_mods);
452 preserve_mods &= mods;
453 after = ModMaskText(info->keymap, preserve_mods);
455 log_vrb(info->keymap->ctx, 1,
456 "Illegal value for preserve[%s] in type %s; "
457 "Converted %s to %s\n",
458 ModMaskText(info->keymap, mods),
459 TypeTxt(info, type), before, after);
462 return AddPreserve(info, type, mods, preserve_mods);
465 /***====================================================================***/
468 AddLevelName(KeyTypesInfo *info, KeyTypeInfo *type,
469 xkb_level_index_t level, xkb_atom_t name, bool clobber)
472 if (level >= darray_size(type->level_names)) {
473 darray_resize0(type->level_names, level + 1);
477 /* Same level, same name. */
478 if (darray_item(type->level_names, level) == name) {
479 log_vrb(info->keymap->ctx, 10,
480 "Duplicate names for level %d of key type %s; Ignored\n",
481 level + 1, TypeTxt(info, type));
485 /* Same level, different name. */
486 if (darray_item(type->level_names, level) != XKB_ATOM_NONE) {
487 const char *old, *new;
488 old = xkb_atom_text(info->keymap->ctx,
489 darray_item(type->level_names, level));
490 new = xkb_atom_text(info->keymap->ctx, name);
491 log_vrb(info->keymap->ctx, 1,
492 "Multiple names for level %d of key type %s; "
493 "Using %s, ignoring %s\n",
494 level + 1, TypeTxt(info, type),
495 (clobber ? new : old), (clobber ? old : new));
501 /* XXX: What about different level, same name? */
504 darray_item(type->level_names, level) = name;
509 SetLevelName(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
512 xkb_level_index_t level;
513 xkb_atom_t level_name;
514 struct xkb_context *ctx = info->keymap->ctx;
516 if (arrayNdx == NULL)
517 return ReportTypeShouldBeArray(info, type, "level name");
519 if (!ExprResolveLevel(ctx, arrayNdx, &level))
520 return ReportTypeBadType(info, type, "level name", "integer");
522 if (!ExprResolveString(ctx, value, &level_name)) {
523 log_err(info->keymap->ctx,
524 "Non-string name for level %d in key type %s; "
525 "Ignoring illegal level name definition\n",
526 level + 1, xkb_atom_text(ctx, type->name));
530 return AddLevelName(info, type, level, level_name, true);
533 /***====================================================================***/
536 SetKeyTypeField(KeyTypesInfo *info, KeyTypeInfo *type,
537 const char *field, ExprDef *arrayNdx, ExprDef *value)
540 enum type_field type_field = 0;
542 if (istreq(field, "modifiers")) {
543 type_field = TYPE_FIELD_MASK;
544 ok = SetModifiers(info, type, arrayNdx, value);
546 else if (istreq(field, "map")) {
547 type_field = TYPE_FIELD_MAP;
548 ok = SetMapEntry(info, type, arrayNdx, value);
550 else if (istreq(field, "preserve")) {
551 type_field = TYPE_FIELD_PRESERVE;
552 ok = SetPreserve(info, type, arrayNdx, value);
554 else if (istreq(field, "levelname") || istreq(field, "level_name")) {
555 type_field = TYPE_FIELD_LEVEL_NAME;
556 ok = SetLevelName(info, type, arrayNdx, value);
558 log_err(info->keymap->ctx,
559 "Unknown field %s in key type %s; Definition ignored\n",
560 field, TypeTxt(info, type));
563 type->defined |= type_field;
568 HandleKeyTypeBody(KeyTypesInfo *info, VarDef *def, KeyTypeInfo *type)
571 const char *elem, *field;
574 for (; def; def = (VarDef *) def->common.next) {
575 ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
580 if (elem && istreq(elem, "type")) {
581 log_err(info->keymap->ctx,
582 "Support for changing the default type has been removed; "
583 "Statement ignored\n");
587 ok = SetKeyTypeField(info, type, field, arrayNdx, def->value);
594 HandleKeyTypeDef(KeyTypesInfo *info, KeyTypeDef *def, enum merge_mode merge)
598 .merge = (def->merge == MERGE_DEFAULT ? merge : def->merge),
602 .entries = darray_new(),
603 .level_names = darray_new(),
606 if (!HandleKeyTypeBody(info, def->body, &type)) {
611 if (!AddKeyType(info, &type, true)) {
620 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge)
625 info->name = strdup_safe(file->name);
627 for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
628 switch (stmt->type) {
630 ok = HandleIncludeKeyTypes(info, (IncludeStmt *) stmt);
633 ok = HandleKeyTypeDef(info, (KeyTypeDef *) stmt, merge);
636 log_err(info->keymap->ctx,
637 "Support for changing the default type has been removed; "
638 "Statement ignored\n");
642 ok = HandleVModDef(info->keymap, (VModDef *) stmt);
645 log_err(info->keymap->ctx,
646 "Key type files may not include other declarations; "
647 "Ignoring %s\n", stmt_type_to_string(stmt->type));
655 if (info->errorCount > 10) {
656 log_err(info->keymap->ctx,
657 "Abandoning keytypes file \"%s\"\n", file->topName);
663 /***====================================================================***/
666 CopyKeyTypesToKeymap(struct xkb_keymap *keymap, KeyTypesInfo *info)
668 keymap->types_section_name = strdup_safe(info->name);
669 XkbEscapeMapName(keymap->types_section_name);
671 keymap->num_types = darray_size(info->types);
672 if (keymap->num_types == 0)
673 keymap->num_types = 1;
675 keymap->types = calloc(keymap->num_types, sizeof(*keymap->types));
678 * If no types were specified, a default unnamed one-level type is
681 if (darray_empty(info->types)) {
682 struct xkb_key_type *type = &keymap->types[0];
685 type->num_levels = 1;
686 type->entries = NULL;
687 type->num_entries = 0;
688 type->name = xkb_atom_intern_literal(keymap->ctx, "default");
689 type->level_names = NULL;
694 for (unsigned i = 0; i < keymap->num_types; i++) {
695 KeyTypeInfo *def = &darray_item(info->types, i);
696 struct xkb_key_type *type = &keymap->types[i];
698 type->mods.mods = def->mods;
699 type->num_levels = def->num_levels;
700 type->entries = darray_mem(def->entries, 0);
701 type->num_entries = darray_size(def->entries);
702 darray_init(def->entries);
703 type->name = def->name;
704 type->level_names = darray_mem(def->level_names, 0);
705 darray_init(def->level_names);
711 /***====================================================================***/
714 CompileKeyTypes(XkbFile *file, struct xkb_keymap *keymap,
715 enum merge_mode merge)
719 InitKeyTypesInfo(&info, keymap);
721 HandleKeyTypesFile(&info, file, merge);
722 if (info.errorCount != 0)
725 if (!CopyKeyTypesToKeymap(keymap, &info))
728 ClearKeyTypesInfo(&info);
732 ClearKeyTypesInfo(&info);