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_mod_set mods;
58 struct xkb_context *ctx;
61 /***====================================================================***/
63 static inline const char *
64 MapEntryTxt(KeyTypesInfo *info, struct xkb_key_type_entry *entry)
66 return ModMaskText(info->ctx, &info->mods, entry->mods.mods);
69 static inline const char *
70 TypeTxt(KeyTypesInfo *info, KeyTypeInfo *type)
72 return xkb_atom_text(info->ctx, type->name);
75 static inline const char *
76 TypeMaskTxt(KeyTypesInfo *info, KeyTypeInfo *type)
78 return ModMaskText(info->ctx, &info->mods, type->mods);
82 ReportTypeShouldBeArray(KeyTypesInfo *info, KeyTypeInfo *type,
85 return ReportShouldBeArray(info->ctx, "key type", field,
90 ReportTypeBadType(KeyTypesInfo *info, KeyTypeInfo *type,
91 const char *field, const char *wanted)
93 return ReportBadType(info->ctx, "key type", field,
94 TypeTxt(info, type), wanted);
97 /***====================================================================***/
100 InitKeyTypesInfo(KeyTypesInfo *info, struct xkb_context *ctx,
101 const struct xkb_mod_set *mods)
103 memset(info, 0, sizeof(*info));
109 ClearKeyTypeInfo(KeyTypeInfo *type)
111 darray_free(type->entries);
112 darray_free(type->level_names);
116 ClearKeyTypesInfo(KeyTypesInfo *info)
119 darray_free(info->types);
123 FindMatchingKeyType(KeyTypesInfo *info, xkb_atom_t name)
127 darray_foreach(old, info->types)
128 if (old->name == name)
135 AddKeyType(KeyTypesInfo *info, KeyTypeInfo *new, bool same_file)
138 const int verbosity = xkb_context_get_log_verbosity(info->ctx);
140 old = FindMatchingKeyType(info, new->name);
142 if (new->merge == MERGE_REPLACE || new->merge == MERGE_OVERRIDE) {
143 if ((same_file && verbosity > 0) || verbosity > 9) {
145 "Multiple definitions of the %s key type; "
146 "Earlier definition ignored\n",
147 xkb_atom_text(info->ctx, new->name));
150 ClearKeyTypeInfo(old);
152 darray_init(new->entries);
153 darray_init(new->level_names);
158 log_vrb(info->ctx, 4,
159 "Multiple definitions of the %s key type; "
160 "Later definition ignored\n",
161 xkb_atom_text(info->ctx, new->name));
163 ClearKeyTypeInfo(new);
167 darray_append(info->types, *new);
171 /***====================================================================***/
174 MergeIncludedKeyTypes(KeyTypesInfo *into, KeyTypesInfo *from,
175 enum merge_mode merge)
179 if (from->errorCount > 0) {
180 into->errorCount += from->errorCount;
184 into->mods = from->mods;
186 if (into->name == NULL) {
187 into->name = from->name;
191 if (darray_empty(into->types)) {
192 into->types = from->types;
193 darray_init(from->types);
196 darray_foreach(type, from->types) {
197 type->merge = (merge == MERGE_DEFAULT ? type->merge : merge);
198 if (!AddKeyType(into, type, false))
205 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge);
208 HandleIncludeKeyTypes(KeyTypesInfo *info, IncludeStmt *include)
210 KeyTypesInfo included;
212 InitKeyTypesInfo(&included, info->ctx, &info->mods);
213 included.name = include->stmt;
214 include->stmt = NULL;
216 for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
217 KeyTypesInfo next_incl;
220 file = ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_TYPES);
222 info->errorCount += 10;
223 ClearKeyTypesInfo(&included);
227 InitKeyTypesInfo(&next_incl, info->ctx, &included.mods);
229 HandleKeyTypesFile(&next_incl, file, stmt->merge);
231 MergeIncludedKeyTypes(&included, &next_incl, stmt->merge);
233 ClearKeyTypesInfo(&next_incl);
237 MergeIncludedKeyTypes(info, &included, include->merge);
238 ClearKeyTypesInfo(&included);
240 return (info->errorCount == 0);
243 /***====================================================================***/
246 SetModifiers(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
253 "The modifiers field of a key type is not an array; "
254 "Illegal array subscript ignored\n");
256 if (!ExprResolveModMask(info->ctx, value, MOD_BOTH, &info->mods, &mods)) {
258 "Key type mask field must be a modifier mask; "
259 "Key type definition ignored\n");
263 if (type->defined & TYPE_FIELD_MASK) {
265 "Multiple modifier mask definitions for key type %s; "
266 "Using %s, ignoring %s\n",
267 xkb_atom_text(info->ctx, type->name),
268 TypeMaskTxt(info, type),
269 ModMaskText(info->ctx, &info->mods, mods));
277 /***====================================================================***/
279 static struct xkb_key_type_entry *
280 FindMatchingMapEntry(KeyTypeInfo *type, xkb_mod_mask_t mods)
282 struct xkb_key_type_entry *entry;
284 darray_foreach(entry, type->entries)
285 if (entry->mods.mods == mods)
292 AddMapEntry(KeyTypesInfo *info, KeyTypeInfo *type,
293 struct xkb_key_type_entry *new, bool clobber, bool report)
295 struct xkb_key_type_entry *old;
297 old = FindMatchingMapEntry(type, new->mods.mods);
299 if (report && old->level != new->level) {
301 "Multiple map entries for %s in %s; "
302 "Using %d, ignoring %d\n",
303 MapEntryTxt(info, new), TypeTxt(info, type),
304 (clobber ? new->level : old->level) + 1,
305 (clobber ? old->level : new->level) + 1);
308 log_vrb(info->ctx, 10,
309 "Multiple occurrences of map[%s]= %d in %s; Ignored\n",
310 MapEntryTxt(info, new), new->level + 1,
311 TypeTxt(info, type));
316 if (new->level >= type->num_levels)
317 type->num_levels = new->level + 1;
318 old->level = new->level;
324 if (new->level >= type->num_levels)
325 type->num_levels = new->level + 1;
327 darray_append(type->entries, *new);
332 SetMapEntry(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
335 struct xkb_key_type_entry entry;
337 if (arrayNdx == NULL)
338 return ReportTypeShouldBeArray(info, type, "map entry");
340 if (!ExprResolveModMask(info->ctx, arrayNdx, MOD_BOTH, &info->mods,
342 return ReportTypeBadType(info, type, "map entry", "modifier mask");
344 if (entry.mods.mods & (~type->mods)) {
345 log_vrb(info->ctx, 1,
346 "Map entry for unused modifiers in %s; "
347 "Using %s instead of %s\n",
349 ModMaskText(info->ctx, &info->mods,
350 entry.mods.mods & type->mods),
351 MapEntryTxt(info, &entry));
352 entry.mods.mods &= type->mods;
355 if (!ExprResolveLevel(info->ctx, value, &entry.level)) {
357 "Level specifications in a key type must be integer; "
358 "Ignoring malformed level specification\n");
362 entry.preserve.mods = 0;
364 return AddMapEntry(info, type, &entry, true, true);
367 /***====================================================================***/
370 AddPreserve(KeyTypesInfo *info, KeyTypeInfo *type,
371 xkb_mod_mask_t mods, xkb_mod_mask_t preserve_mods)
373 struct xkb_key_type_entry *entry;
374 struct xkb_key_type_entry new;
376 darray_foreach(entry, type->entries) {
377 if (entry->mods.mods != mods)
380 /* Map exists without previous preserve (or "None"); override. */
381 if (entry->preserve.mods == 0) {
382 entry->preserve.mods = preserve_mods;
386 /* Map exists with same preserve; do nothing. */
387 if (entry->preserve.mods == preserve_mods) {
388 log_vrb(info->ctx, 10,
389 "Identical definitions for preserve[%s] in %s; "
391 ModMaskText(info->ctx, &info->mods, mods),
392 TypeTxt(info, type));
396 /* Map exists with different preserve; latter wins. */
397 log_vrb(info->ctx, 1,
398 "Multiple definitions for preserve[%s] in %s; "
399 "Using %s, ignoring %s\n",
400 ModMaskText(info->ctx, &info->mods, mods),
402 ModMaskText(info->ctx, &info->mods, preserve_mods),
403 ModMaskText(info->ctx, &info->mods, entry->preserve.mods));
405 entry->preserve.mods = preserve_mods;
410 * Map does not exist, i.e. preserve[] came before map[].
411 * Create a map with the specified mask mapping to Level1. The level
412 * may be overridden later with an explicit map[] statement.
415 new.mods.mods = mods;
416 new.preserve.mods = preserve_mods;
417 darray_append(type->entries, new);
422 SetPreserve(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
425 xkb_mod_mask_t mods, preserve_mods;
427 if (arrayNdx == NULL)
428 return ReportTypeShouldBeArray(info, type, "preserve entry");
430 if (!ExprResolveModMask(info->ctx, arrayNdx, MOD_BOTH, &info->mods, &mods))
431 return ReportTypeBadType(info, type, "preserve entry",
434 if (mods & ~type->mods) {
435 const char *before, *after;
437 before = ModMaskText(info->ctx, &info->mods, mods);
439 after = ModMaskText(info->ctx, &info->mods, mods);
441 log_vrb(info->ctx, 1,
442 "Preserve for modifiers not used by the %s type; "
443 "Index %s converted to %s\n",
444 TypeTxt(info, type), before, after);
447 if (!ExprResolveModMask(info->ctx, value, MOD_BOTH, &info->mods,
450 "Preserve value in a key type is not a modifier mask; "
451 "Ignoring preserve[%s] in type %s\n",
452 ModMaskText(info->ctx, &info->mods, mods),
453 TypeTxt(info, type));
457 if (preserve_mods & ~mods) {
458 const char *before, *after;
460 before = ModMaskText(info->ctx, &info->mods, preserve_mods);
461 preserve_mods &= mods;
462 after = ModMaskText(info->ctx, &info->mods, preserve_mods);
464 log_vrb(info->ctx, 1,
465 "Illegal value for preserve[%s] in type %s; "
466 "Converted %s to %s\n",
467 ModMaskText(info->ctx, &info->mods, mods),
468 TypeTxt(info, type), before, after);
471 return AddPreserve(info, type, mods, preserve_mods);
474 /***====================================================================***/
477 AddLevelName(KeyTypesInfo *info, KeyTypeInfo *type,
478 xkb_level_index_t level, xkb_atom_t name, bool clobber)
481 if (level >= darray_size(type->level_names)) {
482 darray_resize0(type->level_names, level + 1);
486 /* Same level, same name. */
487 if (darray_item(type->level_names, level) == name) {
488 log_vrb(info->ctx, 10,
489 "Duplicate names for level %d of key type %s; Ignored\n",
490 level + 1, TypeTxt(info, type));
494 /* Same level, different name. */
495 if (darray_item(type->level_names, level) != XKB_ATOM_NONE) {
496 const char *old, *new;
497 old = xkb_atom_text(info->ctx,
498 darray_item(type->level_names, level));
499 new = xkb_atom_text(info->ctx, name);
500 log_vrb(info->ctx, 1,
501 "Multiple names for level %d of key type %s; "
502 "Using %s, ignoring %s\n",
503 level + 1, TypeTxt(info, type),
504 (clobber ? new : old), (clobber ? old : new));
510 /* XXX: What about different level, same name? */
513 darray_item(type->level_names, level) = name;
518 SetLevelName(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
521 xkb_level_index_t level;
522 xkb_atom_t level_name;
524 if (arrayNdx == NULL)
525 return ReportTypeShouldBeArray(info, type, "level name");
527 if (!ExprResolveLevel(info->ctx, arrayNdx, &level))
528 return ReportTypeBadType(info, type, "level name", "integer");
530 if (!ExprResolveString(info->ctx, value, &level_name)) {
532 "Non-string name for level %d in key type %s; "
533 "Ignoring illegal level name definition\n",
534 level + 1, xkb_atom_text(info->ctx, type->name));
538 return AddLevelName(info, type, level, level_name, true);
541 /***====================================================================***/
544 SetKeyTypeField(KeyTypesInfo *info, KeyTypeInfo *type,
545 const char *field, ExprDef *arrayNdx, ExprDef *value)
548 enum type_field type_field = 0;
550 if (istreq(field, "modifiers")) {
551 type_field = TYPE_FIELD_MASK;
552 ok = SetModifiers(info, type, arrayNdx, value);
554 else if (istreq(field, "map")) {
555 type_field = TYPE_FIELD_MAP;
556 ok = SetMapEntry(info, type, arrayNdx, value);
558 else if (istreq(field, "preserve")) {
559 type_field = TYPE_FIELD_PRESERVE;
560 ok = SetPreserve(info, type, arrayNdx, value);
562 else if (istreq(field, "levelname") || istreq(field, "level_name")) {
563 type_field = TYPE_FIELD_LEVEL_NAME;
564 ok = SetLevelName(info, type, arrayNdx, value);
567 "Unknown field %s in key type %s; Definition ignored\n",
568 field, TypeTxt(info, type));
571 type->defined |= type_field;
576 HandleKeyTypeBody(KeyTypesInfo *info, VarDef *def, KeyTypeInfo *type)
579 const char *elem, *field;
582 for (; def; def = (VarDef *) def->common.next) {
583 ok = ExprResolveLhs(info->ctx, def->name, &elem, &field,
588 if (elem && istreq(elem, "type")) {
590 "Support for changing the default type has been removed; "
591 "Statement ignored\n");
595 ok = SetKeyTypeField(info, type, field, arrayNdx, def->value);
602 HandleKeyTypeDef(KeyTypesInfo *info, KeyTypeDef *def, enum merge_mode merge)
606 .merge = (def->merge == MERGE_DEFAULT ? merge : def->merge),
610 .entries = darray_new(),
611 .level_names = darray_new(),
614 if (!HandleKeyTypeBody(info, def->body, &type)) {
619 if (!AddKeyType(info, &type, true)) {
628 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge)
633 info->name = strdup_safe(file->name);
635 for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
636 switch (stmt->type) {
638 ok = HandleIncludeKeyTypes(info, (IncludeStmt *) stmt);
641 ok = HandleKeyTypeDef(info, (KeyTypeDef *) stmt, merge);
645 "Support for changing the default type has been removed; "
646 "Statement ignored\n");
650 ok = HandleVModDef(info->ctx, &info->mods, (VModDef *) stmt, merge);
654 "Key type files may not include other declarations; "
655 "Ignoring %s\n", stmt_type_to_string(stmt->type));
663 if (info->errorCount > 10) {
665 "Abandoning keytypes file \"%s\"\n", file->name);
671 /***====================================================================***/
674 CopyKeyTypesToKeymap(struct xkb_keymap *keymap, KeyTypesInfo *info)
677 struct xkb_key_type *types;
679 num_types = darray_empty(info->types) ? 1 : darray_size(info->types);
680 types = calloc(num_types, sizeof(*types));
685 * If no types were specified, a default unnamed one-level type is
688 if (darray_empty(info->types)) {
689 struct xkb_key_type *type = &types[0];
692 type->num_levels = 1;
693 type->entries = NULL;
694 type->num_entries = 0;
695 type->name = xkb_atom_intern_literal(keymap->ctx, "default");
696 type->level_names = NULL;
699 for (unsigned i = 0; i < num_types; i++) {
700 KeyTypeInfo *def = &darray_item(info->types, i);
701 struct xkb_key_type *type = &types[i];
703 type->name = def->name;
704 type->mods.mods = def->mods;
705 darray_steal(def->level_names,
706 &type->level_names, &type->num_levels);
707 darray_steal(def->entries,
708 &type->entries, &type->num_entries);
712 keymap->types_section_name = strdup_safe(info->name);
713 XkbEscapeMapName(keymap->types_section_name);
714 keymap->num_types = num_types;
715 keymap->types = types;
716 keymap->mods = info->mods;
720 /***====================================================================***/
723 CompileKeyTypes(XkbFile *file, struct xkb_keymap *keymap,
724 enum merge_mode merge)
728 InitKeyTypesInfo(&info, keymap->ctx, &keymap->mods);
730 HandleKeyTypesFile(&info, file, merge);
731 if (info.errorCount != 0)
734 if (!CopyKeyTypesToKeymap(keymap, &info))
737 ClearKeyTypesInfo(&info);
741 ClearKeyTypesInfo(&info);