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 * The xkb_types section
35 * =====================
36 * This section is the second to be processesed, after xkb_keycodes.
37 * However, it is completely independent and could have been the first
38 * to be processed (it does not refer to specific keys as specified in
39 * the xkb_keycodes section).
41 * This section defines key types, which, given a key and a keyboard
42 * state (i.e. modifier state and group), determine the shift level to
43 * be used in translating the key to keysyms. These types are assigned
44 * to each group in each key, in the xkb_symbols section.
46 * Key types are called this way because, in a way, they really describe
47 * the "type" of the key (or more correctly, a specific group of the
48 * key). For example, an ordinary keymap will provide a type called
49 * "KEYPAD", which consists of two levels, with the second level being
50 * chosen according to the state of the Num Lock (or Shift) modifiers.
51 * Another example is a type called "ONE_LEVEL", which is usually
52 * assigned to keys such as Escape; these have just one level and are
53 * not affected by the modifier state. Yet more common examples are
54 * "TWO_LEVEL" (with Shift choosing the second level), "ALPHABETIC"
55 * (where Caps Lock may also choose the second level), etc.
59 * Statements of the form:
60 * type "FOUR_LEVEL" { ... }
62 * The above would create a new type named "FOUR_LEVEL".
63 * The body of the definition may include statements of the following
66 * - level_name statements (mandatory for each level in the type):
67 * level_name[Level1] = "Base";
69 * Gives each level in this type a descriptive name. It isn't used
71 * Note: A level may be specified as Level[1-8] or just a number (can
74 * - modifiers statement (mandatory, should be specified only once):
75 * modifiers = Shift+Lock+LevelThree;
77 * A mask of real and virtual modifiers. These are the only modifiers
78 * being considered when matching the modifier state against the type.
79 * The other modifiers, whether active or not, are masked out in the
82 * - map entry statements (should have at least as many mappings as there
83 * are levels in the type):
84 * map[Shift+LevelThree] = Level4;
86 * If the active modifiers, masked with the type's modifiers (as stated
87 * above), match (i.e. equal) the modifiers inside the map[] statement,
88 * then the level in the right hand side is chosen. For example, in the
89 * above, if in the current keyboard state the Shift and LevelThree
90 * modifiers are active, while the Lock modifier is not, then the
91 * keysym(s) in the 4th level of the group will be returned to the
94 * - preserve statements:
95 * map[Shift+Lock+LevelThree] = Level5;
96 * preserve[Shift+Lock+LevelThree] = Lock;
98 * When a map entry matches the active modifiers and the level it
99 * specified is chosen, then these modifiers are said to be "consumed";
100 * for example, in a simple US keymap where the "g" key is assigned an
101 * ordinary ALPHABETIC key type, if the Lock (Caps Lock) modifier is
102 * active and the key is pressed, then a "G" keysym is produced (as
103 * opposed to lower-case "g"). This is because the type definition has
104 * a map entry like the following:
105 * map[Lock] = Level2;
106 * And as such the Lock modifier is consumed. This information is
107 * relevant for applications which further process the modifiers,
108 * since by then the consumed modifiers have already "done their part"
109 * and should be masked out.
111 * However, sometimes even if a modifier is actually used to choose
112 * the shift level (as Lock above), it should *not* be reported as
113 * consumed, for various reasons. In this case, a preserve[] statement
114 * can be used to augment the map entry. The modifiers inside the square
115 * brackets should match one of the map[] statements in the type. The
116 * right hand side should consists of modifiers from the left hand
117 * side; these modifiers are then "preserved" and not reported as
120 * Virtual modifier statements
121 * ---------------------------
122 * Statements of the form:
123 * virtual_modifiers LControl;
125 * Can appear in the xkb_types, xkb_compat, xkb_symbols sections.
130 TYPE_FIELD_MASK = (1 << 0),
131 TYPE_FIELD_MAP = (1 << 1),
132 TYPE_FIELD_PRESERVE = (1 << 2),
133 TYPE_FIELD_LEVEL_NAME = (1 << 3),
136 typedef struct _KeyTypeInfo {
137 enum type_field defined;
139 enum merge_mode merge;
144 xkb_level_index_t num_levels;
145 darray(struct xkb_kt_map_entry) entries;
146 darray(xkb_atom_t) level_names;
149 typedef struct _KeyTypesInfo {
156 struct xkb_keymap *keymap;
159 /***====================================================================***/
161 static inline const char *
162 MapEntryTxt(KeyTypesInfo *info, struct xkb_kt_map_entry *entry)
164 return VModMaskText(info->keymap, entry->mods.mods);
167 static inline const char *
168 TypeTxt(KeyTypesInfo *info, KeyTypeInfo *type)
170 return xkb_atom_text(info->keymap->ctx, type->name);
173 static inline const char *
174 TypeMaskTxt(KeyTypesInfo *info, KeyTypeInfo *type)
176 return VModMaskText(info->keymap, type->mods);
180 ReportTypeShouldBeArray(KeyTypesInfo *info, KeyTypeInfo *type,
183 return ReportShouldBeArray(info->keymap, "key type", field,
184 TypeTxt(info, type));
188 ReportTypeBadType(KeyTypesInfo *info, KeyTypeInfo *type,
189 const char *field, const char *wanted)
191 return ReportBadType(info->keymap->ctx, "key type", field,
192 TypeTxt(info, type), wanted);
196 ReportTypeBadWidth(KeyTypesInfo *info, const char *type, int has, int needs)
198 log_err(info->keymap->ctx,
199 "Key type \"%s\" has %d levels, must have %d; "
200 "Illegal type definition ignored\n",
205 /***====================================================================***/
208 InitKeyTypesInfo(KeyTypesInfo *info, struct xkb_keymap *keymap,
211 info->name = strdup("default");
212 info->errorCount = 0;
214 list_init(&info->types);
215 info->file_id = file_id;
216 InitVModInfo(&info->vmods, keymap);
217 info->keymap = keymap;
221 FreeKeyTypeInfo(KeyTypeInfo * type)
223 darray_free(type->entries);
224 darray_free(type->level_names);
228 FreeKeyTypesInfo(KeyTypesInfo * info)
230 KeyTypeInfo *type, *next_type;
233 list_foreach_safe(type, next_type, &info->types, entry) {
234 FreeKeyTypeInfo(type);
240 NextKeyType(KeyTypesInfo * info)
244 type = calloc(1, sizeof(*type));
248 type->file_id = info->file_id;
250 list_append(&type->entry, &info->types);
256 FindMatchingKeyType(KeyTypesInfo *info, xkb_atom_t name)
260 list_foreach(old, &info->types, entry)
261 if (old->name == name)
268 AddKeyType(KeyTypesInfo *info, KeyTypeInfo *new)
272 int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
274 old = FindMatchingKeyType(info, new->name);
276 if (new->merge == MERGE_REPLACE || new->merge == MERGE_OVERRIDE) {
277 if ((old->file_id == new->file_id && verbosity > 0) ||
279 log_warn(info->keymap->ctx,
280 "Multiple definitions of the %s key type; "
281 "Earlier definition ignored\n",
282 xkb_atom_text(info->keymap->ctx, new->name));
286 FreeKeyTypeInfo(old);
289 darray_init(new->entries);
290 darray_init(new->level_names);
294 if (old->file_id == new->file_id)
295 log_lvl(info->keymap->ctx, 4,
296 "Multiple definitions of the %s key type; "
297 "Later definition ignored\n",
298 xkb_atom_text(info->keymap->ctx, new->name));
300 FreeKeyTypeInfo(new);
304 old = NextKeyType(info);
311 darray_init(new->entries);
312 darray_init(new->level_names);
316 /***====================================================================***/
319 MergeIncludedKeyTypes(KeyTypesInfo *into, KeyTypesInfo *from,
320 enum merge_mode merge)
322 KeyTypeInfo *type, *next_type;
324 if (from->errorCount > 0) {
325 into->errorCount += from->errorCount;
329 if (into->name == NULL) {
330 into->name = from->name;
334 list_foreach_safe(type, next_type, &from->types, entry) {
335 type->merge = (merge == MERGE_DEFAULT ? type->merge : merge);
336 if (!AddKeyType(into, type))
342 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge);
345 HandleIncludeKeyTypes(KeyTypesInfo *info, IncludeStmt *stmt)
347 enum merge_mode merge = MERGE_DEFAULT;
349 KeyTypesInfo included, next_incl;
351 InitKeyTypesInfo(&included, info->keymap, info->file_id);
354 included.name = stmt->stmt;
358 for (; stmt; stmt = stmt->next_incl) {
359 if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_TYPES,
361 info->errorCount += 10;
362 FreeKeyTypesInfo(&included);
366 InitKeyTypesInfo(&next_incl, info->keymap, rtrn->id);
368 HandleKeyTypesFile(&next_incl, rtrn, merge);
370 MergeIncludedKeyTypes(&included, &next_incl, merge);
372 FreeKeyTypesInfo(&next_incl);
376 MergeIncludedKeyTypes(info, &included, merge);
377 FreeKeyTypesInfo(&included);
379 return (info->errorCount == 0);
382 /***====================================================================***/
385 SetModifiers(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
391 log_warn(info->keymap->ctx,
392 "The modifiers field of a key type is not an array; "
393 "Illegal array subscript ignored\n");
395 /* get modifier mask for current type */
396 if (!ExprResolveVModMask(info->keymap, value, &mods)) {
397 log_err(info->keymap->ctx,
398 "Key type mask field must be a modifier mask; "
399 "Key type definition ignored\n");
403 if (type->defined & TYPE_FIELD_MASK) {
404 log_warn(info->keymap->ctx,
405 "Multiple modifier mask definitions for key type %s; "
406 "Using %s, ignoring %s\n",
407 xkb_atom_text(info->keymap->ctx, type->name),
408 TypeMaskTxt(info, type),
409 VModMaskText(info->keymap, mods));
417 /***====================================================================***/
419 static struct xkb_kt_map_entry *
420 FindMatchingMapEntry(KeyTypeInfo *type, xkb_mod_mask_t mods)
422 struct xkb_kt_map_entry *entry;
424 darray_foreach(entry, type->entries)
425 if (entry->mods.mods == mods)
432 * Add a new KTMapEntry to the given key type. If an entry with the same mods
433 * already exists, the level is updated (if clobber is TRUE). Otherwise, a new
436 * @param clobber Overwrite existing entry.
437 * @param report true if a warning is to be printed on.
440 AddMapEntry(KeyTypesInfo *info, KeyTypeInfo *type,
441 struct xkb_kt_map_entry *new, bool clobber, bool report)
443 struct xkb_kt_map_entry * old;
445 old = FindMatchingMapEntry(type, new->mods.mods);
447 if (report && old->level != new->level) {
448 log_warn(info->keymap->ctx,
449 "Multiple map entries for %s in %s; "
450 "Using %d, ignoring %d\n",
451 MapEntryTxt(info, new), TypeTxt(info, type),
452 (clobber ? new->level : old->level) + 1,
453 (clobber ? old->level : new->level) + 1);
456 log_lvl(info->keymap->ctx, 10,
457 "Multiple occurences of map[%s]= %d in %s; Ignored\n",
458 MapEntryTxt(info, new), new->level + 1,
459 TypeTxt(info, type));
464 if (new->level >= type->num_levels)
465 type->num_levels = new->level + 1;
466 old->level = new->level;
472 if (new->level >= type->num_levels)
473 type->num_levels = new->level + 1;
475 darray_append(type->entries, *new);
480 SetMapEntry(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
483 struct xkb_kt_map_entry entry;
485 if (arrayNdx == NULL)
486 return ReportTypeShouldBeArray(info, type, "map entry");
488 if (!ExprResolveVModMask(info->keymap, arrayNdx, &entry.mods.mods))
489 return ReportTypeBadType(info, type, "map entry", "modifier mask");
491 if (entry.mods.mods & (~type->mods)) {
492 log_lvl(info->keymap->ctx, 1,
493 "Map entry for unused modifiers in %s; "
494 "Using %s instead of %s\n",
496 VModMaskText(info->keymap, entry.mods.mods & type->mods),
497 MapEntryTxt(info, &entry));
498 entry.mods.mods &= type->mods;
501 if (!ExprResolveLevel(info->keymap->ctx, value, &entry.level)) {
502 log_err(info->keymap->ctx,
503 "Level specifications in a key type must be integer; "
504 "Ignoring malformed level specification\n");
508 entry.preserve.mods = 0;
510 return AddMapEntry(info, type, &entry, true, true);
513 /***====================================================================***/
516 AddPreserve(KeyTypesInfo *info, KeyTypeInfo *type,
517 xkb_mod_mask_t mods, xkb_mod_mask_t preserve_mods)
519 struct xkb_kt_map_entry *entry;
520 struct xkb_kt_map_entry new;
522 darray_foreach(entry, type->entries) {
523 if (entry->mods.mods != mods)
526 /* Map exists without previous preserve (or "None"); override. */
527 if (entry->preserve.mods == 0) {
528 entry->preserve.mods = preserve_mods;
532 /* Map exists with same preserve; do nothing. */
533 if (entry->preserve.mods == preserve_mods) {
534 log_lvl(info->keymap->ctx, 10,
535 "Identical definitions for preserve[%s] in %s; "
537 VModMaskText(info->keymap, mods),
538 TypeTxt(info, type));
542 /* Map exists with different preserve; latter wins. */
543 log_lvl(info->keymap->ctx, 1,
544 "Multiple definitions for preserve[%s] in %s; "
545 "Using %s, ignoring %s\n",
546 VModMaskText(info->keymap, mods),
548 VModMaskText(info->keymap, preserve_mods),
549 VModMaskText(info->keymap, entry->preserve.mods));
551 entry->preserve.mods = preserve_mods;
556 * Map does not exist, i.e. preserve[] came before map[].
557 * Create a map with the specified mask mapping to Level1. The level
558 * may be overriden later with an explicit map[] statement.
561 new.mods.mods = mods;
562 new.preserve.mods = preserve_mods;
563 darray_append(type->entries, new);
568 SetPreserve(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
571 xkb_mod_mask_t mods, preserve_mods;
573 if (arrayNdx == NULL)
574 return ReportTypeShouldBeArray(info, type, "preserve entry");
576 if (!ExprResolveVModMask(info->keymap, arrayNdx, &mods))
577 return ReportTypeBadType(info, type, "preserve entry",
580 if (mods & ~type->mods) {
581 const char *before, *after;
583 before = VModMaskText(info->keymap, mods);
585 after = VModMaskText(info->keymap, mods);
587 log_lvl(info->keymap->ctx, 1,
588 "Preserve for modifiers not used by the %s type; "
589 "Index %s converted to %s\n",
590 TypeTxt(info, type), before, after);
593 if (!ExprResolveVModMask(info->keymap, value, &preserve_mods)) {
594 log_err(info->keymap->ctx,
595 "Preserve value in a key type is not a modifier mask; "
596 "Ignoring preserve[%s] in type %s\n",
597 VModMaskText(info->keymap, mods),
598 TypeTxt(info, type));
602 if (preserve_mods & ~mods) {
603 const char *before, *after;
605 before = VModMaskText(info->keymap, preserve_mods);
606 preserve_mods &= mods;
607 after = VModMaskText(info->keymap, preserve_mods);
609 log_lvl(info->keymap->ctx, 1,
610 "Illegal value for preserve[%s] in type %s; "
611 "Converted %s to %s\n",
612 VModMaskText(info->keymap, mods),
613 TypeTxt(info, type), before, after);
616 return AddPreserve(info, type, mods, preserve_mods);
619 /***====================================================================***/
622 AddLevelName(KeyTypesInfo *info, KeyTypeInfo *type,
623 xkb_level_index_t level, xkb_atom_t name, bool clobber)
626 if (level >= darray_size(type->level_names)) {
627 darray_resize0(type->level_names, level + 1);
631 /* Same level, same name. */
632 if (darray_item(type->level_names, level) == name) {
633 log_lvl(info->keymap->ctx, 10,
634 "Duplicate names for level %d of key type %s; Ignored\n",
635 level + 1, TypeTxt(info, type));
639 /* Same level, different name. */
640 if (darray_item(type->level_names, level) != XKB_ATOM_NONE) {
641 const char *old, *new;
642 old = xkb_atom_text(info->keymap->ctx,
643 darray_item(type->level_names, level));
644 new = xkb_atom_text(info->keymap->ctx, name);
645 log_lvl(info->keymap->ctx, 1,
646 "Multiple names for level %d of key type %s; "
647 "Using %s, ignoring %s\n",
648 level + 1, TypeTxt(info, type),
649 (clobber ? new : old), (clobber ? old : new));
655 /* XXX: What about different level, same name? */
658 darray_item(type->level_names, level) = name;
663 SetLevelName(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
666 xkb_level_index_t level;
667 xkb_atom_t level_name;
668 struct xkb_context *ctx = info->keymap->ctx;
671 if (arrayNdx == NULL)
672 return ReportTypeShouldBeArray(info, type, "level name");
674 if (!ExprResolveLevel(ctx, arrayNdx, &level))
675 return ReportTypeBadType(info, type, "level name", "integer");
677 if (!ExprResolveString(ctx, value, &str)) {
678 log_err(info->keymap->ctx,
679 "Non-string name for level %d in key type %s; "
680 "Ignoring illegal level name definition\n",
681 level + 1, xkb_atom_text(ctx, type->name));
685 level_name = xkb_atom_intern(ctx, str);
687 return AddLevelName(info, type, level, level_name, true);
690 /***====================================================================***/
693 * Parses the fields in a type "..." { } description.
695 * @param field The field to parse (e.g. modifiers, map, level_name)
698 SetKeyTypeField(KeyTypesInfo *info, KeyTypeInfo *type,
699 const char *field, ExprDef *arrayNdx, ExprDef *value)
702 enum type_field type_field = 0;
704 if (istreq(field, "modifiers")) {
705 type_field = TYPE_FIELD_MASK;
706 ok = SetModifiers(info, type, arrayNdx, value);
708 else if (istreq(field, "map")) {
709 type_field = TYPE_FIELD_MAP;
710 ok = SetMapEntry(info, type, arrayNdx, value);
712 else if (istreq(field, "preserve")) {
713 type_field = TYPE_FIELD_PRESERVE;
714 ok = SetPreserve(info, type, arrayNdx, value);
716 else if (istreq(field, "levelname") || istreq(field, "level_name")) {
717 type_field = TYPE_FIELD_LEVEL_NAME;
718 ok = SetLevelName(info, type, arrayNdx, value);
720 log_err(info->keymap->ctx,
721 "Unknown field %s in key type %s; Definition ignored\n",
722 field, TypeTxt(info, type));
725 type->defined |= type_field;
730 HandleKeyTypeBody(KeyTypesInfo *info, VarDef *def, KeyTypeInfo *type)
733 const char *elem, *field;
736 for (; def; def = (VarDef *) def->common.next) {
737 ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
742 if (elem && istreq(elem, "type")) {
743 log_err(info->keymap->ctx,
744 "Support for changing the default type has been removed; "
745 "Statement ignored\n");
749 ok = SetKeyTypeField(info, type, field, arrayNdx, def->value);
756 * Process a type "XYZ" { } specification in the xkb_types section.
760 HandleKeyTypeDef(KeyTypesInfo *info, KeyTypeDef *def, enum merge_mode merge)
764 .file_id = info->file_id,
765 .merge = (def->merge == MERGE_DEFAULT ? merge : def->merge),
769 .entries = darray_new(),
770 .level_names = darray_new(),
773 /* Parse the actual content. */
774 if (!HandleKeyTypeBody(info, def->body, &type)) {
779 /* Now add the new keytype to the info struct */
780 if (!AddKeyType(info, &type)) {
789 * Process an xkb_types section.
791 * @param file The parsed xkb_types section.
792 * @param merge Merge Strategy (e.g. MERGE_OVERRIDE)
793 * @param info Pointer to memory where the outcome will be stored.
796 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge)
802 info->name = strdup_safe(file->name);
804 for (stmt = file->defs; stmt; stmt = stmt->next) {
805 switch (stmt->type) {
807 ok = HandleIncludeKeyTypes(info, (IncludeStmt *) stmt);
809 case STMT_TYPE: /* e.g. type "ONE_LEVEL" */
810 ok = HandleKeyTypeDef(info, (KeyTypeDef *) stmt, merge);
813 log_err(info->keymap->ctx,
814 "Support for changing the default type has been removed; "
815 "Statement ignored\n");
818 case STMT_VMOD: /* virtual_modifiers NumLock, ... */
819 ok = HandleVModDef((VModDef *) stmt, info->keymap, merge,
823 log_err(info->keymap->ctx,
824 "Key type files may not include other declarations; "
825 "Ignoring %s\n", StmtTypeToString(stmt->type));
833 if (info->errorCount > 10) {
834 log_err(info->keymap->ctx,
835 "Abandoning keytypes file \"%s\"\n", file->topName);
842 CopyDefToKeyType(KeyTypesInfo *info, KeyTypeInfo *def,
843 struct xkb_key_type *type)
845 type->mods.mods = def->mods;
846 type->num_levels = def->num_levels;
847 type->map = darray_mem(def->entries, 0);
848 type->num_entries = darray_size(def->entries);
849 darray_init(def->entries);
850 type->name = def->name;
851 type->level_names = darray_mem(def->level_names, 0);
852 darray_init(def->level_names);
858 CompileKeyTypes(XkbFile *file, struct xkb_keymap *keymap,
859 enum merge_mode merge)
862 unsigned int num_types;
866 InitKeyTypesInfo(&info, keymap, file->id);
868 HandleKeyTypesFile(&info, file, merge);
870 if (info.errorCount != 0)
874 keymap->types_section_name = strdup(info.name);
876 num_types = info.num_types ? info.num_types : 1;
877 keymap->types = calloc(num_types, sizeof(*keymap->types));
880 keymap->num_types = num_types;
883 * If no types were specified, a default unnamed one-level type is
886 if (info.num_types == 0) {
888 .name = xkb_atom_intern(keymap->ctx, "default"),
891 .entries = darray_new(),
892 .level_names = darray_new(),
895 if (!CopyDefToKeyType(&info, &dflt, &keymap->types[0]))
899 list_foreach(def, &info.types, entry)
900 if (!CopyDefToKeyType(&info, def, &keymap->types[i++]))
904 FreeKeyTypesInfo(&info);
908 FreeKeyTypesInfo(&info);