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 ********************************************************/
29 #include "xkbcomp-priv.h"
36 TYPE_FIELD_MASK = (1 << 0),
37 TYPE_FIELD_MAP = (1 << 1),
38 TYPE_FIELD_PRESERVE = (1 << 2),
39 TYPE_FIELD_LEVEL_NAME = (1 << 3),
43 enum type_field defined;
44 enum merge_mode merge;
48 xkb_level_index_t num_levels;
49 darray(struct xkb_key_type_entry) entries;
50 darray(xkb_atom_t) level_names;
57 darray(KeyTypeInfo) types;
58 struct xkb_mod_set mods;
60 struct xkb_context *ctx;
63 /***====================================================================***/
65 static inline const char *
66 MapEntryTxt(KeyTypesInfo *info, struct xkb_key_type_entry *entry)
68 return ModMaskText(info->ctx, &info->mods, entry->mods.mods);
71 static inline const char *
72 TypeTxt(KeyTypesInfo *info, KeyTypeInfo *type)
74 return xkb_atom_text(info->ctx, type->name);
77 static inline const char *
78 TypeMaskTxt(KeyTypesInfo *info, KeyTypeInfo *type)
80 return ModMaskText(info->ctx, &info->mods, type->mods);
84 ReportTypeShouldBeArray(KeyTypesInfo *info, KeyTypeInfo *type,
87 return ReportShouldBeArray(info->ctx, "key type", field,
92 ReportTypeBadType(KeyTypesInfo *info, xkb_message_code_t code,
93 KeyTypeInfo *type, const char *field, const char *wanted)
95 return ReportBadType(info->ctx, code, "key type", field,
96 TypeTxt(info, type), wanted);
99 /***====================================================================***/
102 InitKeyTypesInfo(KeyTypesInfo *info, struct xkb_context *ctx,
103 const struct xkb_mod_set *mods)
105 memset(info, 0, sizeof(*info));
111 ClearKeyTypeInfo(KeyTypeInfo *type)
113 darray_free(type->entries);
114 darray_free(type->level_names);
118 ClearKeyTypesInfo(KeyTypesInfo *info)
121 darray_free(info->types);
125 FindMatchingKeyType(KeyTypesInfo *info, xkb_atom_t name)
129 darray_foreach(old, info->types)
130 if (old->name == name)
137 AddKeyType(KeyTypesInfo *info, KeyTypeInfo *new, bool same_file)
140 const int verbosity = xkb_context_get_log_verbosity(info->ctx);
142 old = FindMatchingKeyType(info, new->name);
144 if (new->merge == MERGE_REPLACE || new->merge == MERGE_OVERRIDE) {
145 if ((same_file && verbosity > 0) || verbosity > 9) {
146 log_warn_with_code(info->ctx,
147 XKB_WARNING_CONFLICTING_KEY_TYPE_DEFINITIONS,
148 "Multiple definitions of the %s key type; "
149 "Earlier definition ignored\n",
150 xkb_atom_text(info->ctx, new->name));
153 ClearKeyTypeInfo(old);
155 darray_init(new->entries);
156 darray_init(new->level_names);
161 log_vrb(info->ctx, 4,
162 XKB_WARNING_CONFLICTING_KEY_TYPE_DEFINITIONS,
163 "Multiple definitions of the %s key type; "
164 "Later definition ignored\n",
165 xkb_atom_text(info->ctx, new->name));
167 ClearKeyTypeInfo(new);
171 darray_append(info->types, *new);
175 /***====================================================================***/
178 MergeIncludedKeyTypes(KeyTypesInfo *into, KeyTypesInfo *from,
179 enum merge_mode merge)
181 if (from->errorCount > 0) {
182 into->errorCount += from->errorCount;
186 into->mods = from->mods;
188 if (into->name == NULL) {
189 into->name = from->name;
193 if (darray_empty(into->types)) {
194 into->types = from->types;
195 darray_init(from->types);
199 darray_foreach(type, from->types) {
200 type->merge = (merge == MERGE_DEFAULT ? type->merge : merge);
201 if (!AddKeyType(into, type, false))
208 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge);
211 HandleIncludeKeyTypes(KeyTypesInfo *info, IncludeStmt *include)
213 KeyTypesInfo included;
215 InitKeyTypesInfo(&included, info->ctx, &info->mods);
216 included.name = include->stmt;
217 include->stmt = NULL;
219 for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
220 KeyTypesInfo next_incl;
223 file = ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_TYPES);
225 info->errorCount += 10;
226 ClearKeyTypesInfo(&included);
230 InitKeyTypesInfo(&next_incl, info->ctx, &included.mods);
232 HandleKeyTypesFile(&next_incl, file, stmt->merge);
234 MergeIncludedKeyTypes(&included, &next_incl, stmt->merge);
236 ClearKeyTypesInfo(&next_incl);
240 MergeIncludedKeyTypes(info, &included, include->merge);
241 ClearKeyTypesInfo(&included);
243 return (info->errorCount == 0);
246 /***====================================================================***/
249 SetModifiers(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
256 "The modifiers field of a key type is not an array; "
257 "Illegal array subscript ignored\n");
259 if (!ExprResolveModMask(info->ctx, value, MOD_BOTH, &info->mods, &mods)) {
260 log_err_with_code(info->ctx,
261 XKB_ERROR_UNSUPPORTED_MODIFIER_MASK,
262 "Key type mask field must be a modifier mask; "
263 "Key type definition ignored\n");
267 if (type->defined & TYPE_FIELD_MASK) {
269 "Multiple modifier mask definitions for key type %s; "
270 "Using %s, ignoring %s\n",
271 xkb_atom_text(info->ctx, type->name),
272 TypeMaskTxt(info, type),
273 ModMaskText(info->ctx, &info->mods, mods));
281 /***====================================================================***/
283 static struct xkb_key_type_entry *
284 FindMatchingMapEntry(KeyTypeInfo *type, xkb_mod_mask_t mods)
286 struct xkb_key_type_entry *entry;
288 darray_foreach(entry, type->entries)
289 if (entry->mods.mods == mods)
296 AddMapEntry(KeyTypesInfo *info, KeyTypeInfo *type,
297 struct xkb_key_type_entry *new, bool clobber, bool report)
299 struct xkb_key_type_entry *old;
301 old = FindMatchingMapEntry(type, new->mods.mods);
303 if (report && old->level != new->level) {
304 log_warn_with_code(info->ctx,
305 XKB_WARNING_CONFLICTING_KEY_TYPE_MAP_ENTRY,
306 "Multiple map entries for %s in %s; "
307 "Using %d, ignoring %d\n",
308 MapEntryTxt(info, new), TypeTxt(info, type),
309 (clobber ? new->level : old->level) + 1,
310 (clobber ? old->level : new->level) + 1);
313 log_vrb(info->ctx, 10,
314 XKB_WARNING_CONFLICTING_KEY_TYPE_MAP_ENTRY,
315 "Multiple occurrences of map[%s]= %d in %s; Ignored\n",
316 MapEntryTxt(info, new), new->level + 1,
317 TypeTxt(info, type));
322 if (new->level >= type->num_levels)
323 type->num_levels = new->level + 1;
324 old->level = new->level;
330 if (new->level >= type->num_levels)
331 type->num_levels = new->level + 1;
333 darray_append(type->entries, *new);
338 SetMapEntry(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
341 struct xkb_key_type_entry entry;
343 if (arrayNdx == NULL)
344 return ReportTypeShouldBeArray(info, type, "map entry");
346 if (!ExprResolveModMask(info->ctx, arrayNdx, MOD_BOTH, &info->mods,
348 return ReportTypeBadType(info, XKB_ERROR_UNSUPPORTED_MODIFIER_MASK,
349 type, "map entry", "modifier mask");
351 if (entry.mods.mods & (~type->mods)) {
352 log_vrb(info->ctx, 1,
353 XKB_WARNING_UNDECLARED_MODIFIERS_IN_KEY_TYPE,
354 "Map entry for modifiers not used by type %s; "
355 "Using %s instead of %s\n",
357 ModMaskText(info->ctx, &info->mods,
358 entry.mods.mods & type->mods),
359 MapEntryTxt(info, &entry));
360 entry.mods.mods &= type->mods;
363 if (!ExprResolveLevel(info->ctx, value, &entry.level)) {
364 log_err_with_code(info->ctx, XKB_ERROR_UNSUPPORTED_SHIFT_LEVEL,
365 "Level specifications in a key type must be integer; "
366 "Ignoring malformed level specification\n");
370 entry.preserve.mods = 0;
372 return AddMapEntry(info, type, &entry, true, true);
375 /***====================================================================***/
378 AddPreserve(KeyTypesInfo *info, KeyTypeInfo *type,
379 xkb_mod_mask_t mods, xkb_mod_mask_t preserve_mods)
381 struct xkb_key_type_entry *entry;
382 struct xkb_key_type_entry new;
384 darray_foreach(entry, type->entries) {
385 if (entry->mods.mods != mods)
388 /* Map exists without previous preserve (or "None"); override. */
389 if (entry->preserve.mods == 0) {
390 entry->preserve.mods = preserve_mods;
394 /* Map exists with same preserve; do nothing. */
395 if (entry->preserve.mods == preserve_mods) {
396 log_vrb(info->ctx, 10,
397 XKB_WARNING_DUPLICATE_ENTRY,
398 "Identical definitions for preserve[%s] in %s; "
400 ModMaskText(info->ctx, &info->mods, mods),
401 TypeTxt(info, type));
405 /* Map exists with different preserve; latter wins. */
406 log_vrb(info->ctx, 1,
407 XKB_WARNING_CONFLICTING_KEY_TYPE_PRESERVE_ENTRIES,
408 "Multiple definitions for preserve[%s] in %s; "
409 "Using %s, ignoring %s\n",
410 ModMaskText(info->ctx, &info->mods, mods),
412 ModMaskText(info->ctx, &info->mods, preserve_mods),
413 ModMaskText(info->ctx, &info->mods, entry->preserve.mods));
415 entry->preserve.mods = preserve_mods;
420 * Map does not exist, i.e. preserve[] came before map[].
421 * Create a map with the specified mask mapping to Level1. The level
422 * may be overridden later with an explicit map[] statement.
425 new.mods.mods = mods;
426 new.preserve.mods = preserve_mods;
427 darray_append(type->entries, new);
432 SetPreserve(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
435 xkb_mod_mask_t mods, preserve_mods;
437 if (arrayNdx == NULL)
438 return ReportTypeShouldBeArray(info, type, "preserve entry");
440 if (!ExprResolveModMask(info->ctx, arrayNdx, MOD_BOTH, &info->mods, &mods))
441 return ReportTypeBadType(info, XKB_ERROR_UNSUPPORTED_MODIFIER_MASK,
442 type, "preserve entry", "modifier mask");
444 if (mods & ~type->mods) {
445 const char *before, *after;
447 before = ModMaskText(info->ctx, &info->mods, mods);
449 after = ModMaskText(info->ctx, &info->mods, mods);
451 log_vrb(info->ctx, 1,
452 XKB_WARNING_UNDECLARED_MODIFIERS_IN_KEY_TYPE,
453 "Preserve entry for modifiers not used by the %s type; "
454 "Index %s converted to %s\n",
455 TypeTxt(info, type), before, after);
458 if (!ExprResolveModMask(info->ctx, value, MOD_BOTH, &info->mods,
460 log_err_with_code(info->ctx,
461 XKB_ERROR_UNSUPPORTED_MODIFIER_MASK,
462 "Preserve value in a key type is not a modifier mask; "
463 "Ignoring preserve[%s] in type %s\n",
464 ModMaskText(info->ctx, &info->mods, mods),
465 TypeTxt(info, type));
469 if (preserve_mods & ~mods) {
470 const char *before, *after;
472 before = ModMaskText(info->ctx, &info->mods, preserve_mods);
473 preserve_mods &= mods;
474 after = ModMaskText(info->ctx, &info->mods, preserve_mods);
476 log_vrb(info->ctx, 1,
477 XKB_WARNING_ILLEGAL_KEY_TYPE_PRESERVE_RESULT,
478 "Illegal value for preserve[%s] in type %s; "
479 "Converted %s to %s\n",
480 ModMaskText(info->ctx, &info->mods, mods),
481 TypeTxt(info, type), before, after);
484 return AddPreserve(info, type, mods, preserve_mods);
487 /***====================================================================***/
490 AddLevelName(KeyTypesInfo *info, KeyTypeInfo *type,
491 xkb_level_index_t level, xkb_atom_t name, bool clobber)
494 if (level >= darray_size(type->level_names)) {
495 darray_resize0(type->level_names, level + 1);
499 /* Same level, same name. */
500 if (darray_item(type->level_names, level) == name) {
501 log_vrb(info->ctx, 10,
502 XKB_WARNING_DUPLICATE_ENTRY,
503 "Duplicate names for level %d of key type %s; Ignored\n",
504 level + 1, TypeTxt(info, type));
508 /* Same level, different name. */
509 if (darray_item(type->level_names, level) != XKB_ATOM_NONE) {
510 const char *old, *new;
511 old = xkb_atom_text(info->ctx,
512 darray_item(type->level_names, level));
513 new = xkb_atom_text(info->ctx, name);
514 log_vrb(info->ctx, 1,
515 XKB_WARNING_CONFLICTING_KEY_TYPE_LEVEL_NAMES,
516 "Multiple names for level %d of key type %s; "
517 "Using %s, ignoring %s\n",
518 level + 1, TypeTxt(info, type),
519 (clobber ? new : old), (clobber ? old : new));
525 /* XXX: What about different level, same name? */
528 darray_item(type->level_names, level) = name;
533 SetLevelName(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
536 xkb_level_index_t level;
537 xkb_atom_t level_name;
539 if (arrayNdx == NULL)
540 return ReportTypeShouldBeArray(info, type, "level name");
542 if (!ExprResolveLevel(info->ctx, arrayNdx, &level))
543 return ReportTypeBadType(info, XKB_ERROR_UNSUPPORTED_SHIFT_LEVEL,
544 type, "level name", "integer");
546 if (!ExprResolveString(info->ctx, value, &level_name)) {
547 log_err_with_code(info->ctx,
548 XKB_ERROR_WRONG_FIELD_TYPE,
549 "Non-string name for level %d in key type %s; "
550 "Ignoring illegal level name definition\n",
551 level + 1, xkb_atom_text(info->ctx, type->name));
555 return AddLevelName(info, type, level, level_name, true);
558 /***====================================================================***/
561 SetKeyTypeField(KeyTypesInfo *info, KeyTypeInfo *type,
562 const char *field, ExprDef *arrayNdx, ExprDef *value)
565 enum type_field type_field = 0;
567 if (istreq(field, "modifiers")) {
568 type_field = TYPE_FIELD_MASK;
569 ok = SetModifiers(info, type, arrayNdx, value);
571 else if (istreq(field, "map")) {
572 type_field = TYPE_FIELD_MAP;
573 ok = SetMapEntry(info, type, arrayNdx, value);
575 else if (istreq(field, "preserve")) {
576 type_field = TYPE_FIELD_PRESERVE;
577 ok = SetPreserve(info, type, arrayNdx, value);
579 else if (istreq(field, "levelname") || istreq(field, "level_name")) {
580 type_field = TYPE_FIELD_LEVEL_NAME;
581 ok = SetLevelName(info, type, arrayNdx, value);
583 log_err_with_code(info->ctx,
584 XKB_ERROR_UNKNOWN_FIELD,
585 "Unknown field %s in key type %s; Definition ignored\n",
586 field, TypeTxt(info, type));
589 type->defined |= type_field;
594 HandleKeyTypeBody(KeyTypesInfo *info, VarDef *def, KeyTypeInfo *type)
597 const char *elem, *field;
600 for (; def; def = (VarDef *) def->common.next) {
601 ok = ExprResolveLhs(info->ctx, def->name, &elem, &field,
606 if (elem && istreq(elem, "type")) {
607 log_err_with_code(info->ctx,
608 XKB_ERROR_INVALID_SET_DEFAULT_STATEMENT,
609 "Support for changing the default type has been removed; "
610 "Statement ignored\n");
614 ok = SetKeyTypeField(info, type, field, arrayNdx, def->value);
621 HandleKeyTypeDef(KeyTypesInfo *info, KeyTypeDef *def, enum merge_mode merge)
625 .merge = (def->merge == MERGE_DEFAULT ? merge : def->merge),
629 .entries = darray_new(),
630 .level_names = darray_new(),
633 if (!HandleKeyTypeBody(info, def->body, &type)) {
638 if (!AddKeyType(info, &type, true)) {
647 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge)
652 info->name = strdup_safe(file->name);
654 for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
655 switch (stmt->type) {
657 ok = HandleIncludeKeyTypes(info, (IncludeStmt *) stmt);
660 ok = HandleKeyTypeDef(info, (KeyTypeDef *) stmt, merge);
663 log_err_with_code(info->ctx,
664 XKB_ERROR_WRONG_STATEMENT_TYPE,
665 "Support for changing the default type has been removed; "
666 "Statement ignored\n");
670 ok = HandleVModDef(info->ctx, &info->mods, (VModDef *) stmt, merge);
673 log_err_with_code(info->ctx,
674 XKB_ERROR_WRONG_STATEMENT_TYPE,
675 "Key type files may not include other declarations; "
676 "Ignoring %s\n", stmt_type_to_string(stmt->type));
684 if (info->errorCount > 10) {
685 log_err_with_code(info->ctx,
686 XKB_ERROR_INVALID_SYNTAX,
687 "Abandoning keytypes file \"%s\"\n", file->name);
693 /***====================================================================***/
696 CopyKeyTypesToKeymap(struct xkb_keymap *keymap, KeyTypesInfo *info)
699 struct xkb_key_type *types;
701 num_types = darray_empty(info->types) ? 1 : darray_size(info->types);
702 types = calloc(num_types, sizeof(*types));
707 * If no types were specified, a default unnamed one-level type is
710 if (darray_empty(info->types)) {
711 struct xkb_key_type *type = &types[0];
714 type->num_levels = 1;
715 type->entries = NULL;
716 type->num_entries = 0;
717 type->name = xkb_atom_intern_literal(keymap->ctx, "default");
718 type->level_names = NULL;
719 type->num_level_names = 0;
722 for (unsigned i = 0; i < num_types; i++) {
723 KeyTypeInfo *def = &darray_item(info->types, i);
724 struct xkb_key_type *type = &types[i];
726 type->name = def->name;
727 type->mods.mods = def->mods;
728 type->num_levels = def->num_levels;
729 darray_steal(def->level_names, &type->level_names, &type->num_level_names);
730 darray_steal(def->entries, &type->entries, &type->num_entries);
734 keymap->types_section_name = strdup_safe(info->name);
735 XkbEscapeMapName(keymap->types_section_name);
736 keymap->num_types = num_types;
737 keymap->types = types;
738 keymap->mods = info->mods;
742 /***====================================================================***/
745 CompileKeyTypes(XkbFile *file, struct xkb_keymap *keymap,
746 enum merge_mode merge)
750 InitKeyTypesInfo(&info, keymap->ctx, &keymap->mods);
752 HandleKeyTypesFile(&info, file, merge);
753 if (info.errorCount != 0)
756 if (!CopyKeyTypesToKeymap(keymap, &info))
759 ClearKeyTypesInfo(&info);
763 ClearKeyTypesInfo(&info);