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) {
147 "Multiple definitions of the %s key type; "
148 "Earlier definition ignored\n",
149 xkb_atom_text(info->ctx, new->name));
152 ClearKeyTypeInfo(old);
154 darray_init(new->entries);
155 darray_init(new->level_names);
160 log_vrb(info->ctx, 4,
161 "Multiple definitions of the %s key type; "
162 "Later definition ignored\n",
163 xkb_atom_text(info->ctx, new->name));
165 ClearKeyTypeInfo(new);
169 darray_append(info->types, *new);
173 /***====================================================================***/
176 MergeIncludedKeyTypes(KeyTypesInfo *into, KeyTypesInfo *from,
177 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);
197 darray_foreach(type, from->types) {
198 type->merge = (merge == MERGE_DEFAULT ? type->merge : merge);
199 if (!AddKeyType(into, type, false))
206 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge);
209 HandleIncludeKeyTypes(KeyTypesInfo *info, IncludeStmt *include)
211 KeyTypesInfo included;
213 InitKeyTypesInfo(&included, info->ctx, &info->mods);
214 included.name = include->stmt;
215 include->stmt = NULL;
217 for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
218 KeyTypesInfo next_incl;
221 file = ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_TYPES);
223 info->errorCount += 10;
224 ClearKeyTypesInfo(&included);
228 InitKeyTypesInfo(&next_incl, info->ctx, &included.mods);
230 HandleKeyTypesFile(&next_incl, file, stmt->merge);
232 MergeIncludedKeyTypes(&included, &next_incl, stmt->merge);
234 ClearKeyTypesInfo(&next_incl);
238 MergeIncludedKeyTypes(info, &included, include->merge);
239 ClearKeyTypesInfo(&included);
241 return (info->errorCount == 0);
244 /***====================================================================***/
247 SetModifiers(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
254 "The modifiers field of a key type is not an array; "
255 "Illegal array subscript ignored\n");
257 if (!ExprResolveModMask(info->ctx, value, MOD_BOTH, &info->mods, &mods)) {
259 "Key type mask field must be a modifier mask; "
260 "Key type definition ignored\n");
264 if (type->defined & TYPE_FIELD_MASK) {
266 "Multiple modifier mask definitions for key type %s; "
267 "Using %s, ignoring %s\n",
268 xkb_atom_text(info->ctx, type->name),
269 TypeMaskTxt(info, type),
270 ModMaskText(info->ctx, &info->mods, mods));
278 /***====================================================================***/
280 static struct xkb_key_type_entry *
281 FindMatchingMapEntry(KeyTypeInfo *type, xkb_mod_mask_t mods)
283 struct xkb_key_type_entry *entry;
285 darray_foreach(entry, type->entries)
286 if (entry->mods.mods == mods)
293 AddMapEntry(KeyTypesInfo *info, KeyTypeInfo *type,
294 struct xkb_key_type_entry *new, bool clobber, bool report)
296 struct xkb_key_type_entry *old;
298 old = FindMatchingMapEntry(type, new->mods.mods);
300 if (report && old->level != new->level) {
302 "Multiple map entries for %s in %s; "
303 "Using %d, ignoring %d\n",
304 MapEntryTxt(info, new), TypeTxt(info, type),
305 (clobber ? new->level : old->level) + 1,
306 (clobber ? old->level : new->level) + 1);
309 log_vrb(info->ctx, 10,
310 "Multiple occurrences of map[%s]= %d in %s; Ignored\n",
311 MapEntryTxt(info, new), new->level + 1,
312 TypeTxt(info, type));
317 if (new->level >= type->num_levels)
318 type->num_levels = new->level + 1;
319 old->level = new->level;
325 if (new->level >= type->num_levels)
326 type->num_levels = new->level + 1;
328 darray_append(type->entries, *new);
333 SetMapEntry(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
336 struct xkb_key_type_entry entry;
338 if (arrayNdx == NULL)
339 return ReportTypeShouldBeArray(info, type, "map entry");
341 if (!ExprResolveModMask(info->ctx, arrayNdx, MOD_BOTH, &info->mods,
343 return ReportTypeBadType(info, XKB_ERROR_UNSUPPORTED_MODIFIER_MASK,
344 type, "map entry", "modifier mask");
346 if (entry.mods.mods & (~type->mods)) {
347 log_vrb(info->ctx, 1,
348 "Map entry for unused modifiers in %s; "
349 "Using %s instead of %s\n",
351 ModMaskText(info->ctx, &info->mods,
352 entry.mods.mods & type->mods),
353 MapEntryTxt(info, &entry));
354 entry.mods.mods &= type->mods;
357 if (!ExprResolveLevel(info->ctx, value, &entry.level)) {
358 log_err_with_code(info->ctx, XKB_ERROR_UNSUPPORTED_SHIFT_LEVEL,
359 "Level specifications in a key type must be integer; "
360 "Ignoring malformed level specification\n");
364 entry.preserve.mods = 0;
366 return AddMapEntry(info, type, &entry, true, true);
369 /***====================================================================***/
372 AddPreserve(KeyTypesInfo *info, KeyTypeInfo *type,
373 xkb_mod_mask_t mods, xkb_mod_mask_t preserve_mods)
375 struct xkb_key_type_entry *entry;
376 struct xkb_key_type_entry new;
378 darray_foreach(entry, type->entries) {
379 if (entry->mods.mods != mods)
382 /* Map exists without previous preserve (or "None"); override. */
383 if (entry->preserve.mods == 0) {
384 entry->preserve.mods = preserve_mods;
388 /* Map exists with same preserve; do nothing. */
389 if (entry->preserve.mods == preserve_mods) {
390 log_vrb(info->ctx, 10,
391 "Identical definitions for preserve[%s] in %s; "
393 ModMaskText(info->ctx, &info->mods, mods),
394 TypeTxt(info, type));
398 /* Map exists with different preserve; latter wins. */
399 log_vrb(info->ctx, 1,
400 "Multiple definitions for preserve[%s] in %s; "
401 "Using %s, ignoring %s\n",
402 ModMaskText(info->ctx, &info->mods, mods),
404 ModMaskText(info->ctx, &info->mods, preserve_mods),
405 ModMaskText(info->ctx, &info->mods, entry->preserve.mods));
407 entry->preserve.mods = preserve_mods;
412 * Map does not exist, i.e. preserve[] came before map[].
413 * Create a map with the specified mask mapping to Level1. The level
414 * may be overridden later with an explicit map[] statement.
417 new.mods.mods = mods;
418 new.preserve.mods = preserve_mods;
419 darray_append(type->entries, new);
424 SetPreserve(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
427 xkb_mod_mask_t mods, preserve_mods;
429 if (arrayNdx == NULL)
430 return ReportTypeShouldBeArray(info, type, "preserve entry");
432 if (!ExprResolveModMask(info->ctx, arrayNdx, MOD_BOTH, &info->mods, &mods))
433 return ReportTypeBadType(info, XKB_ERROR_UNSUPPORTED_MODIFIER_MASK,
434 type, "preserve entry", "modifier mask");
436 if (mods & ~type->mods) {
437 const char *before, *after;
439 before = ModMaskText(info->ctx, &info->mods, mods);
441 after = ModMaskText(info->ctx, &info->mods, mods);
443 log_vrb(info->ctx, 1,
444 "Preserve for modifiers not used by the %s type; "
445 "Index %s converted to %s\n",
446 TypeTxt(info, type), before, after);
449 if (!ExprResolveModMask(info->ctx, value, MOD_BOTH, &info->mods,
452 "Preserve value in a key type is not a modifier mask; "
453 "Ignoring preserve[%s] in type %s\n",
454 ModMaskText(info->ctx, &info->mods, mods),
455 TypeTxt(info, type));
459 if (preserve_mods & ~mods) {
460 const char *before, *after;
462 before = ModMaskText(info->ctx, &info->mods, preserve_mods);
463 preserve_mods &= mods;
464 after = ModMaskText(info->ctx, &info->mods, preserve_mods);
466 log_vrb(info->ctx, 1,
467 "Illegal value for preserve[%s] in type %s; "
468 "Converted %s to %s\n",
469 ModMaskText(info->ctx, &info->mods, mods),
470 TypeTxt(info, type), before, after);
473 return AddPreserve(info, type, mods, preserve_mods);
476 /***====================================================================***/
479 AddLevelName(KeyTypesInfo *info, KeyTypeInfo *type,
480 xkb_level_index_t level, xkb_atom_t name, bool clobber)
483 if (level >= darray_size(type->level_names)) {
484 darray_resize0(type->level_names, level + 1);
488 /* Same level, same name. */
489 if (darray_item(type->level_names, level) == name) {
490 log_vrb(info->ctx, 10,
491 "Duplicate names for level %d of key type %s; Ignored\n",
492 level + 1, TypeTxt(info, type));
496 /* Same level, different name. */
497 if (darray_item(type->level_names, level) != XKB_ATOM_NONE) {
498 const char *old, *new;
499 old = xkb_atom_text(info->ctx,
500 darray_item(type->level_names, level));
501 new = xkb_atom_text(info->ctx, name);
502 log_vrb(info->ctx, 1,
503 "Multiple names for level %d of key type %s; "
504 "Using %s, ignoring %s\n",
505 level + 1, TypeTxt(info, type),
506 (clobber ? new : old), (clobber ? old : new));
512 /* XXX: What about different level, same name? */
515 darray_item(type->level_names, level) = name;
520 SetLevelName(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
523 xkb_level_index_t level;
524 xkb_atom_t level_name;
526 if (arrayNdx == NULL)
527 return ReportTypeShouldBeArray(info, type, "level name");
529 if (!ExprResolveLevel(info->ctx, arrayNdx, &level))
530 return ReportTypeBadType(info, XKB_ERROR_UNSUPPORTED_SHIFT_LEVEL,
531 type, "level name", "integer");
533 if (!ExprResolveString(info->ctx, value, &level_name)) {
535 "Non-string name for level %d in key type %s; "
536 "Ignoring illegal level name definition\n",
537 level + 1, xkb_atom_text(info->ctx, type->name));
541 return AddLevelName(info, type, level, level_name, true);
544 /***====================================================================***/
547 SetKeyTypeField(KeyTypesInfo *info, KeyTypeInfo *type,
548 const char *field, ExprDef *arrayNdx, ExprDef *value)
551 enum type_field type_field = 0;
553 if (istreq(field, "modifiers")) {
554 type_field = TYPE_FIELD_MASK;
555 ok = SetModifiers(info, type, arrayNdx, value);
557 else if (istreq(field, "map")) {
558 type_field = TYPE_FIELD_MAP;
559 ok = SetMapEntry(info, type, arrayNdx, value);
561 else if (istreq(field, "preserve")) {
562 type_field = TYPE_FIELD_PRESERVE;
563 ok = SetPreserve(info, type, arrayNdx, value);
565 else if (istreq(field, "levelname") || istreq(field, "level_name")) {
566 type_field = TYPE_FIELD_LEVEL_NAME;
567 ok = SetLevelName(info, type, arrayNdx, value);
570 "Unknown field %s in key type %s; Definition ignored\n",
571 field, TypeTxt(info, type));
574 type->defined |= type_field;
579 HandleKeyTypeBody(KeyTypesInfo *info, VarDef *def, KeyTypeInfo *type)
582 const char *elem, *field;
585 for (; def; def = (VarDef *) def->common.next) {
586 ok = ExprResolveLhs(info->ctx, def->name, &elem, &field,
591 if (elem && istreq(elem, "type")) {
593 "Support for changing the default type has been removed; "
594 "Statement ignored\n");
598 ok = SetKeyTypeField(info, type, field, arrayNdx, def->value);
605 HandleKeyTypeDef(KeyTypesInfo *info, KeyTypeDef *def, enum merge_mode merge)
609 .merge = (def->merge == MERGE_DEFAULT ? merge : def->merge),
613 .entries = darray_new(),
614 .level_names = darray_new(),
617 if (!HandleKeyTypeBody(info, def->body, &type)) {
622 if (!AddKeyType(info, &type, true)) {
631 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge)
636 info->name = strdup_safe(file->name);
638 for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
639 switch (stmt->type) {
641 ok = HandleIncludeKeyTypes(info, (IncludeStmt *) stmt);
644 ok = HandleKeyTypeDef(info, (KeyTypeDef *) stmt, merge);
648 "Support for changing the default type has been removed; "
649 "Statement ignored\n");
653 ok = HandleVModDef(info->ctx, &info->mods, (VModDef *) stmt, merge);
657 "Key type files may not include other declarations; "
658 "Ignoring %s\n", stmt_type_to_string(stmt->type));
666 if (info->errorCount > 10) {
668 "Abandoning keytypes file \"%s\"\n", file->name);
674 /***====================================================================***/
677 CopyKeyTypesToKeymap(struct xkb_keymap *keymap, KeyTypesInfo *info)
680 struct xkb_key_type *types;
682 num_types = darray_empty(info->types) ? 1 : darray_size(info->types);
683 types = calloc(num_types, sizeof(*types));
688 * If no types were specified, a default unnamed one-level type is
691 if (darray_empty(info->types)) {
692 struct xkb_key_type *type = &types[0];
695 type->num_levels = 1;
696 type->entries = NULL;
697 type->num_entries = 0;
698 type->name = xkb_atom_intern_literal(keymap->ctx, "default");
699 type->level_names = NULL;
700 type->num_level_names = 0;
703 for (unsigned i = 0; i < num_types; i++) {
704 KeyTypeInfo *def = &darray_item(info->types, i);
705 struct xkb_key_type *type = &types[i];
707 type->name = def->name;
708 type->mods.mods = def->mods;
709 type->num_levels = def->num_levels;
710 darray_steal(def->level_names, &type->level_names, &type->num_level_names);
711 darray_steal(def->entries, &type->entries, &type->num_entries);
715 keymap->types_section_name = strdup_safe(info->name);
716 XkbEscapeMapName(keymap->types_section_name);
717 keymap->num_types = num_types;
718 keymap->types = types;
719 keymap->mods = info->mods;
723 /***====================================================================***/
726 CompileKeyTypes(XkbFile *file, struct xkb_keymap *keymap,
727 enum merge_mode merge)
731 InitKeyTypesInfo(&info, keymap->ctx, &keymap->mods);
733 HandleKeyTypesFile(&info, file, merge);
734 if (info.errorCount != 0)
737 if (!CopyKeyTypesToKeymap(keymap, &info))
740 ClearKeyTypesInfo(&info);
744 ClearKeyTypesInfo(&info);