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)
177 if (from->errorCount > 0) {
178 into->errorCount += from->errorCount;
182 into->mods = from->mods;
184 if (into->name == NULL) {
185 into->name = from->name;
189 if (darray_empty(into->types)) {
190 into->types = from->types;
191 darray_init(from->types);
195 darray_foreach(type, from->types) {
196 type->merge = (merge == MERGE_DEFAULT ? type->merge : merge);
197 if (!AddKeyType(into, type, false))
204 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge);
207 HandleIncludeKeyTypes(KeyTypesInfo *info, IncludeStmt *include)
209 KeyTypesInfo included;
211 InitKeyTypesInfo(&included, info->ctx, &info->mods);
212 included.name = include->stmt;
213 include->stmt = NULL;
215 for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
216 KeyTypesInfo next_incl;
219 file = ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_TYPES);
221 info->errorCount += 10;
222 ClearKeyTypesInfo(&included);
226 InitKeyTypesInfo(&next_incl, info->ctx, &included.mods);
228 HandleKeyTypesFile(&next_incl, file, stmt->merge);
230 MergeIncludedKeyTypes(&included, &next_incl, stmt->merge);
232 ClearKeyTypesInfo(&next_incl);
236 MergeIncludedKeyTypes(info, &included, include->merge);
237 ClearKeyTypesInfo(&included);
239 return (info->errorCount == 0);
242 /***====================================================================***/
245 SetModifiers(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
252 "The modifiers field of a key type is not an array; "
253 "Illegal array subscript ignored\n");
255 if (!ExprResolveModMask(info->ctx, value, MOD_BOTH, &info->mods, &mods)) {
257 "Key type mask field must be a modifier mask; "
258 "Key type definition ignored\n");
262 if (type->defined & TYPE_FIELD_MASK) {
264 "Multiple modifier mask definitions for key type %s; "
265 "Using %s, ignoring %s\n",
266 xkb_atom_text(info->ctx, type->name),
267 TypeMaskTxt(info, type),
268 ModMaskText(info->ctx, &info->mods, mods));
276 /***====================================================================***/
278 static struct xkb_key_type_entry *
279 FindMatchingMapEntry(KeyTypeInfo *type, xkb_mod_mask_t mods)
281 struct xkb_key_type_entry *entry;
283 darray_foreach(entry, type->entries)
284 if (entry->mods.mods == mods)
291 AddMapEntry(KeyTypesInfo *info, KeyTypeInfo *type,
292 struct xkb_key_type_entry *new, bool clobber, bool report)
294 struct xkb_key_type_entry *old;
296 old = FindMatchingMapEntry(type, new->mods.mods);
298 if (report && old->level != new->level) {
300 "Multiple map entries for %s in %s; "
301 "Using %d, ignoring %d\n",
302 MapEntryTxt(info, new), TypeTxt(info, type),
303 (clobber ? new->level : old->level) + 1,
304 (clobber ? old->level : new->level) + 1);
307 log_vrb(info->ctx, 10,
308 "Multiple occurrences of map[%s]= %d in %s; Ignored\n",
309 MapEntryTxt(info, new), new->level + 1,
310 TypeTxt(info, type));
315 if (new->level >= type->num_levels)
316 type->num_levels = new->level + 1;
317 old->level = new->level;
323 if (new->level >= type->num_levels)
324 type->num_levels = new->level + 1;
326 darray_append(type->entries, *new);
331 SetMapEntry(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
334 struct xkb_key_type_entry entry;
336 if (arrayNdx == NULL)
337 return ReportTypeShouldBeArray(info, type, "map entry");
339 if (!ExprResolveModMask(info->ctx, arrayNdx, MOD_BOTH, &info->mods,
341 return ReportTypeBadType(info, type, "map entry", "modifier mask");
343 if (entry.mods.mods & (~type->mods)) {
344 log_vrb(info->ctx, 1,
345 "Map entry for unused modifiers in %s; "
346 "Using %s instead of %s\n",
348 ModMaskText(info->ctx, &info->mods,
349 entry.mods.mods & type->mods),
350 MapEntryTxt(info, &entry));
351 entry.mods.mods &= type->mods;
354 if (!ExprResolveLevel(info->ctx, value, &entry.level)) {
356 "Level specifications in a key type must be integer; "
357 "Ignoring malformed level specification\n");
361 entry.preserve.mods = 0;
363 return AddMapEntry(info, type, &entry, true, true);
366 /***====================================================================***/
369 AddPreserve(KeyTypesInfo *info, KeyTypeInfo *type,
370 xkb_mod_mask_t mods, xkb_mod_mask_t preserve_mods)
372 struct xkb_key_type_entry *entry;
373 struct xkb_key_type_entry new;
375 darray_foreach(entry, type->entries) {
376 if (entry->mods.mods != mods)
379 /* Map exists without previous preserve (or "None"); override. */
380 if (entry->preserve.mods == 0) {
381 entry->preserve.mods = preserve_mods;
385 /* Map exists with same preserve; do nothing. */
386 if (entry->preserve.mods == preserve_mods) {
387 log_vrb(info->ctx, 10,
388 "Identical definitions for preserve[%s] in %s; "
390 ModMaskText(info->ctx, &info->mods, mods),
391 TypeTxt(info, type));
395 /* Map exists with different preserve; latter wins. */
396 log_vrb(info->ctx, 1,
397 "Multiple definitions for preserve[%s] in %s; "
398 "Using %s, ignoring %s\n",
399 ModMaskText(info->ctx, &info->mods, mods),
401 ModMaskText(info->ctx, &info->mods, preserve_mods),
402 ModMaskText(info->ctx, &info->mods, entry->preserve.mods));
404 entry->preserve.mods = preserve_mods;
409 * Map does not exist, i.e. preserve[] came before map[].
410 * Create a map with the specified mask mapping to Level1. The level
411 * may be overridden later with an explicit map[] statement.
414 new.mods.mods = mods;
415 new.preserve.mods = preserve_mods;
416 darray_append(type->entries, new);
421 SetPreserve(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
424 xkb_mod_mask_t mods, preserve_mods;
426 if (arrayNdx == NULL)
427 return ReportTypeShouldBeArray(info, type, "preserve entry");
429 if (!ExprResolveModMask(info->ctx, arrayNdx, MOD_BOTH, &info->mods, &mods))
430 return ReportTypeBadType(info, type, "preserve entry",
433 if (mods & ~type->mods) {
434 const char *before, *after;
436 before = ModMaskText(info->ctx, &info->mods, mods);
438 after = ModMaskText(info->ctx, &info->mods, mods);
440 log_vrb(info->ctx, 1,
441 "Preserve for modifiers not used by the %s type; "
442 "Index %s converted to %s\n",
443 TypeTxt(info, type), before, after);
446 if (!ExprResolveModMask(info->ctx, value, MOD_BOTH, &info->mods,
449 "Preserve value in a key type is not a modifier mask; "
450 "Ignoring preserve[%s] in type %s\n",
451 ModMaskText(info->ctx, &info->mods, mods),
452 TypeTxt(info, type));
456 if (preserve_mods & ~mods) {
457 const char *before, *after;
459 before = ModMaskText(info->ctx, &info->mods, preserve_mods);
460 preserve_mods &= mods;
461 after = ModMaskText(info->ctx, &info->mods, preserve_mods);
463 log_vrb(info->ctx, 1,
464 "Illegal value for preserve[%s] in type %s; "
465 "Converted %s to %s\n",
466 ModMaskText(info->ctx, &info->mods, mods),
467 TypeTxt(info, type), before, after);
470 return AddPreserve(info, type, mods, preserve_mods);
473 /***====================================================================***/
476 AddLevelName(KeyTypesInfo *info, KeyTypeInfo *type,
477 xkb_level_index_t level, xkb_atom_t name, bool clobber)
480 if (level >= darray_size(type->level_names)) {
481 darray_resize0(type->level_names, level + 1);
485 /* Same level, same name. */
486 if (darray_item(type->level_names, level) == name) {
487 log_vrb(info->ctx, 10,
488 "Duplicate names for level %d of key type %s; Ignored\n",
489 level + 1, TypeTxt(info, type));
493 /* Same level, different name. */
494 if (darray_item(type->level_names, level) != XKB_ATOM_NONE) {
495 const char *old, *new;
496 old = xkb_atom_text(info->ctx,
497 darray_item(type->level_names, level));
498 new = xkb_atom_text(info->ctx, name);
499 log_vrb(info->ctx, 1,
500 "Multiple names for level %d of key type %s; "
501 "Using %s, ignoring %s\n",
502 level + 1, TypeTxt(info, type),
503 (clobber ? new : old), (clobber ? old : new));
509 /* XXX: What about different level, same name? */
512 darray_item(type->level_names, level) = name;
517 SetLevelName(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
520 xkb_level_index_t level;
521 xkb_atom_t level_name;
523 if (arrayNdx == NULL)
524 return ReportTypeShouldBeArray(info, type, "level name");
526 if (!ExprResolveLevel(info->ctx, arrayNdx, &level))
527 return ReportTypeBadType(info, type, "level name", "integer");
529 if (!ExprResolveString(info->ctx, value, &level_name)) {
531 "Non-string name for level %d in key type %s; "
532 "Ignoring illegal level name definition\n",
533 level + 1, xkb_atom_text(info->ctx, type->name));
537 return AddLevelName(info, type, level, level_name, true);
540 /***====================================================================***/
543 SetKeyTypeField(KeyTypesInfo *info, KeyTypeInfo *type,
544 const char *field, ExprDef *arrayNdx, ExprDef *value)
547 enum type_field type_field = 0;
549 if (istreq(field, "modifiers")) {
550 type_field = TYPE_FIELD_MASK;
551 ok = SetModifiers(info, type, arrayNdx, value);
553 else if (istreq(field, "map")) {
554 type_field = TYPE_FIELD_MAP;
555 ok = SetMapEntry(info, type, arrayNdx, value);
557 else if (istreq(field, "preserve")) {
558 type_field = TYPE_FIELD_PRESERVE;
559 ok = SetPreserve(info, type, arrayNdx, value);
561 else if (istreq(field, "levelname") || istreq(field, "level_name")) {
562 type_field = TYPE_FIELD_LEVEL_NAME;
563 ok = SetLevelName(info, type, arrayNdx, value);
566 "Unknown field %s in key type %s; Definition ignored\n",
567 field, TypeTxt(info, type));
570 type->defined |= type_field;
575 HandleKeyTypeBody(KeyTypesInfo *info, VarDef *def, KeyTypeInfo *type)
578 const char *elem, *field;
581 for (; def; def = (VarDef *) def->common.next) {
582 ok = ExprResolveLhs(info->ctx, def->name, &elem, &field,
587 if (elem && istreq(elem, "type")) {
589 "Support for changing the default type has been removed; "
590 "Statement ignored\n");
594 ok = SetKeyTypeField(info, type, field, arrayNdx, def->value);
601 HandleKeyTypeDef(KeyTypesInfo *info, KeyTypeDef *def, enum merge_mode merge)
605 .merge = (def->merge == MERGE_DEFAULT ? merge : def->merge),
609 .entries = darray_new(),
610 .level_names = darray_new(),
613 if (!HandleKeyTypeBody(info, def->body, &type)) {
618 if (!AddKeyType(info, &type, true)) {
627 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge)
632 info->name = strdup_safe(file->name);
634 for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
635 switch (stmt->type) {
637 ok = HandleIncludeKeyTypes(info, (IncludeStmt *) stmt);
640 ok = HandleKeyTypeDef(info, (KeyTypeDef *) stmt, merge);
644 "Support for changing the default type has been removed; "
645 "Statement ignored\n");
649 ok = HandleVModDef(info->ctx, &info->mods, (VModDef *) stmt, merge);
653 "Key type files may not include other declarations; "
654 "Ignoring %s\n", stmt_type_to_string(stmt->type));
662 if (info->errorCount > 10) {
664 "Abandoning keytypes file \"%s\"\n", file->name);
670 /***====================================================================***/
673 CopyKeyTypesToKeymap(struct xkb_keymap *keymap, KeyTypesInfo *info)
676 struct xkb_key_type *types;
678 num_types = darray_empty(info->types) ? 1 : darray_size(info->types);
679 types = calloc(num_types, sizeof(*types));
684 * If no types were specified, a default unnamed one-level type is
687 if (darray_empty(info->types)) {
688 struct xkb_key_type *type = &types[0];
691 type->num_levels = 1;
692 type->entries = NULL;
693 type->num_entries = 0;
694 type->name = xkb_atom_intern_literal(keymap->ctx, "default");
695 type->level_names = NULL;
698 for (unsigned i = 0; i < num_types; i++) {
699 KeyTypeInfo *def = &darray_item(info->types, i);
700 struct xkb_key_type *type = &types[i];
702 type->name = def->name;
703 type->mods.mods = def->mods;
704 darray_steal(def->level_names,
705 &type->level_names, &type->num_levels);
706 darray_steal(def->entries,
707 &type->entries, &type->num_entries);
711 keymap->types_section_name = strdup_safe(info->name);
712 XkbEscapeMapName(keymap->types_section_name);
713 keymap->num_types = num_types;
714 keymap->types = types;
715 keymap->mods = info->mods;
719 /***====================================================================***/
722 CompileKeyTypes(XkbFile *file, struct xkb_keymap *keymap,
723 enum merge_mode merge)
727 InitKeyTypesInfo(&info, keymap->ctx, &keymap->mods);
729 HandleKeyTypesFile(&info, file, merge);
730 if (info.errorCount != 0)
733 if (!CopyKeyTypesToKeymap(keymap, &info))
736 ClearKeyTypesInfo(&info);
740 ClearKeyTypesInfo(&info);