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"
28 #include "parseutils.h"
32 * The xkb_types section
33 * =====================
34 * This section is the second to be processesed, after xkb_keycodes.
35 * However, it is completely independent and could have been the first
36 * to be processed (it does not refer to specific keys as specified in
37 * the xkb_keycodes section).
39 * This section defines key types, which, given a key and a keyboard
40 * state (i.e. modifier state and group), determine the shift level to
41 * be used in translating the key to keysyms. These types are assigned
42 * to each group in each key, in the xkb_symbols section.
44 * Key types are called this way because, in a way, they really describe
45 * the "type" of the key (or more correctly, a specific group of the
46 * key). For example, an ordinary keymap will provide a type called
47 * "KEYPAD", which consists of two levels, with the second level being
48 * chosen according to the state of the Num Lock (or Shift) modifiers.
49 * Another example is a type called "ONE_LEVEL", which is usually
50 * assigned to keys such as Escape; these have just one level and are
51 * not affected by the modifier state. Yet more common examples are
52 * "TWO_LEVEL" (with Shift choosing the second level), "ALPHABETIC"
53 * (where Caps Lock may also choose the second level), etc.
57 * Statements of the form:
58 * type "FOUR_LEVEL" { ... }
60 * The above would create a new type named "FOUR_LEVEL".
61 * The body of the definition may include statements of the following
64 * - level_name statements (mandatory for each level in the type):
65 * level_name[Level1] = "Base";
67 * Gives each level in this type a descriptive name. It isn't used
69 * Note: A level may be specified as Level[1-8] or just a number (can
72 * - modifiers statement (mandatory, should be specified only once):
73 * modifiers = Shift+Lock+LevelThree;
75 * A mask of real and virtual modifiers. These are the only modifiers
76 * being considered when matching the modifier state against the type.
77 * The other modifiers, whether active or not, are masked out in the
80 * - map entry statements (should have at least as many mappings as there
81 * are levels in the type):
82 * map[Shift+LevelThree] = Level4;
84 * If the active modifiers, masked with the type's modifiers (as stated
85 * above), match (i.e. equal) the modifiers inside the map[] statement,
86 * then the level in the right hand side is chosen. For example, in the
87 * above, if in the current keyboard state the Shift and LevelThree
88 * modifiers are active, while the Lock modifier is not, then the
89 * keysym(s) in the 4th level of the group will be returned to the
92 * - preserve statements:
93 * map[Shift+Lock+LevelThree] = Level5;
94 * preserve[Shift+Lock+LevelThree] = Lock;
96 * When a map entry matches the active modifiers and the level it
97 * specified is chosen, then these modifiers are said to be "consumed";
98 * for example, in a simple US keymap where the "g" key is assigned an
99 * ordinary ALPHABETIC key type, if the Lock (Caps Lock) modifier is
100 * active and the key is pressed, then a "G" keysym is produced (as
101 * opposed to lower-case "g"). This is because the type definition has
102 * a map entry like the following:
103 * map[Lock] = Level2;
104 * And as such the Lock modifier is consumed. This information is
105 * relevant for applications which further process the modifiers,
106 * since by then the consumed modifiers have already "done their part"
107 * and should be masked out.
109 * However, sometimes even if a modifier is actually used to choose
110 * the shift level (as Lock above), it should *not* be reported as
111 * consumed, for various reasons. In this case, a preserve[] statement
112 * can be used to augment the map entry. The modifiers inside the square
113 * brackets should match one of the map[] statements in the type. The
114 * right hand side should consists of modifiers from the left hand
115 * side; these modifiers are then "preserved" and not reported as
118 * Virtual modifier statements
119 * ---------------------------
120 * Statements of the form:
121 * virtual_modifiers LControl;
123 * Can appear in the xkb_types, xkb_compat, xkb_symbols sections.
128 TYPE_FIELD_MASK = (1 << 0),
129 TYPE_FIELD_MAP = (1 << 1),
130 TYPE_FIELD_PRESERVE = (1 << 2),
131 TYPE_FIELD_LEVEL_NAME = (1 << 3),
134 typedef struct _KeyTypeInfo {
135 enum type_field defined;
137 enum merge_mode merge;
142 xkb_level_index_t num_levels;
143 darray(struct xkb_kt_map_entry) entries;
144 darray(xkb_atom_t) level_names;
147 typedef struct _KeyTypesInfo {
154 struct xkb_keymap *keymap;
157 /***====================================================================***/
159 static inline const char *
160 MapEntryTxt(KeyTypesInfo *info, struct xkb_kt_map_entry *entry)
162 return VModMaskText(info->keymap, entry->mods.mods);
165 static inline const char *
166 TypeTxt(KeyTypesInfo *info, KeyTypeInfo *type)
168 return xkb_atom_text(info->keymap->ctx, type->name);
171 static inline const char *
172 TypeMaskTxt(KeyTypesInfo *info, KeyTypeInfo *type)
174 return VModMaskText(info->keymap, type->mods);
178 ReportTypeShouldBeArray(KeyTypesInfo *info, KeyTypeInfo *type,
181 return ReportShouldBeArray(info->keymap, "key type", field,
182 TypeTxt(info, type));
186 ReportTypeBadType(KeyTypesInfo *info, KeyTypeInfo *type,
187 const char *field, const char *wanted)
189 return ReportBadType(info->keymap->ctx, "key type", field,
190 TypeTxt(info, type), wanted);
194 ReportTypeBadWidth(KeyTypesInfo *info, const char *type, int has, int needs)
196 log_err(info->keymap->ctx,
197 "Key type \"%s\" has %d levels, must have %d; "
198 "Illegal type definition ignored\n",
203 /***====================================================================***/
206 InitKeyTypesInfo(KeyTypesInfo *info, struct xkb_keymap *keymap,
209 info->name = strdup("default");
210 info->errorCount = 0;
212 list_init(&info->types);
213 info->file_id = file_id;
214 InitVModInfo(&info->vmods, keymap);
215 info->keymap = keymap;
219 FreeKeyTypeInfo(KeyTypeInfo * type)
221 darray_free(type->entries);
222 darray_free(type->level_names);
226 FreeKeyTypesInfo(KeyTypesInfo * info)
228 KeyTypeInfo *type, *next_type;
231 list_foreach_safe(type, next_type, &info->types, entry) {
232 FreeKeyTypeInfo(type);
238 NextKeyType(KeyTypesInfo * info)
242 type = calloc(1, sizeof(*type));
246 type->file_id = info->file_id;
248 list_append(&type->entry, &info->types);
254 FindMatchingKeyType(KeyTypesInfo *info, xkb_atom_t name)
258 list_foreach(old, &info->types, entry)
259 if (old->name == name)
266 AddKeyType(KeyTypesInfo *info, KeyTypeInfo *new)
270 int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
272 old = FindMatchingKeyType(info, new->name);
274 if (new->merge == MERGE_REPLACE || new->merge == MERGE_OVERRIDE) {
275 if ((old->file_id == new->file_id && verbosity > 0) ||
277 log_warn(info->keymap->ctx,
278 "Multiple definitions of the %s key type; "
279 "Earlier definition ignored\n",
280 xkb_atom_text(info->keymap->ctx, new->name));
284 FreeKeyTypeInfo(old);
287 darray_init(new->entries);
288 darray_init(new->level_names);
292 if (old->file_id == new->file_id)
293 log_lvl(info->keymap->ctx, 4,
294 "Multiple definitions of the %s key type; "
295 "Later definition ignored\n",
296 xkb_atom_text(info->keymap->ctx, new->name));
298 FreeKeyTypeInfo(new);
302 old = NextKeyType(info);
309 darray_init(new->entries);
310 darray_init(new->level_names);
314 /***====================================================================***/
317 MergeIncludedKeyTypes(KeyTypesInfo *into, KeyTypesInfo *from,
318 enum merge_mode merge)
320 KeyTypeInfo *type, *next_type;
322 if (from->errorCount > 0) {
323 into->errorCount += from->errorCount;
327 if (into->name == NULL) {
328 into->name = from->name;
332 list_foreach_safe(type, next_type, &from->types, entry) {
333 type->merge = (merge == MERGE_DEFAULT ? type->merge : merge);
334 if (!AddKeyType(into, type))
340 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge);
343 HandleIncludeKeyTypes(KeyTypesInfo *info, IncludeStmt *stmt)
345 enum merge_mode merge = MERGE_DEFAULT;
347 KeyTypesInfo included, next_incl;
349 InitKeyTypesInfo(&included, info->keymap, info->file_id);
352 included.name = stmt->stmt;
356 for (; stmt; stmt = stmt->next_incl) {
357 if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_TYPES,
359 info->errorCount += 10;
360 FreeKeyTypesInfo(&included);
364 InitKeyTypesInfo(&next_incl, info->keymap, rtrn->id);
366 HandleKeyTypesFile(&next_incl, rtrn, merge);
368 MergeIncludedKeyTypes(&included, &next_incl, merge);
370 FreeKeyTypesInfo(&next_incl);
374 MergeIncludedKeyTypes(info, &included, merge);
375 FreeKeyTypesInfo(&included);
377 return (info->errorCount == 0);
380 /***====================================================================***/
383 SetModifiers(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
389 log_warn(info->keymap->ctx,
390 "The modifiers field of a key type is not an array; "
391 "Illegal array subscript ignored\n");
393 /* get modifier mask for current type */
394 if (!ExprResolveVModMask(info->keymap, value, &mods)) {
395 log_err(info->keymap->ctx,
396 "Key type mask field must be a modifier mask; "
397 "Key type definition ignored\n");
401 if (type->defined & TYPE_FIELD_MASK) {
402 log_warn(info->keymap->ctx,
403 "Multiple modifier mask definitions for key type %s; "
404 "Using %s, ignoring %s\n",
405 xkb_atom_text(info->keymap->ctx, type->name),
406 TypeMaskTxt(info, type),
407 VModMaskText(info->keymap, mods));
415 /***====================================================================***/
417 static struct xkb_kt_map_entry *
418 FindMatchingMapEntry(KeyTypeInfo *type, xkb_mod_mask_t mods)
420 struct xkb_kt_map_entry *entry;
422 darray_foreach(entry, type->entries)
423 if (entry->mods.mods == mods)
430 * Add a new KTMapEntry to the given key type. If an entry with the same mods
431 * already exists, the level is updated (if clobber is TRUE). Otherwise, a new
434 * @param clobber Overwrite existing entry.
435 * @param report true if a warning is to be printed on.
438 AddMapEntry(KeyTypesInfo *info, KeyTypeInfo *type,
439 struct xkb_kt_map_entry *new, bool clobber, bool report)
441 struct xkb_kt_map_entry * old;
443 old = FindMatchingMapEntry(type, new->mods.mods);
445 if (report && old->level != new->level) {
446 log_warn(info->keymap->ctx,
447 "Multiple map entries for %s in %s; "
448 "Using %d, ignoring %d\n",
449 MapEntryTxt(info, new), TypeTxt(info, type),
450 (clobber ? new->level : old->level) + 1,
451 (clobber ? old->level : new->level) + 1);
454 log_lvl(info->keymap->ctx, 10,
455 "Multiple occurences of map[%s]= %d in %s; Ignored\n",
456 MapEntryTxt(info, new), new->level + 1,
457 TypeTxt(info, type));
462 if (new->level >= type->num_levels)
463 type->num_levels = new->level + 1;
464 old->level = new->level;
470 if (new->level >= type->num_levels)
471 type->num_levels = new->level + 1;
473 darray_append(type->entries, *new);
478 SetMapEntry(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
481 struct xkb_kt_map_entry entry;
483 if (arrayNdx == NULL)
484 return ReportTypeShouldBeArray(info, type, "map entry");
486 if (!ExprResolveVModMask(info->keymap, arrayNdx, &entry.mods.mods))
487 return ReportTypeBadType(info, type, "map entry", "modifier mask");
489 if (entry.mods.mods & (~type->mods)) {
490 log_lvl(info->keymap->ctx, 1,
491 "Map entry for unused modifiers in %s; "
492 "Using %s instead of %s\n",
494 VModMaskText(info->keymap, entry.mods.mods & type->mods),
495 MapEntryTxt(info, &entry));
496 entry.mods.mods &= type->mods;
499 if (!ExprResolveLevel(info->keymap->ctx, value, &entry.level)) {
500 log_err(info->keymap->ctx,
501 "Level specifications in a key type must be integer; "
502 "Ignoring malformed level specification\n");
506 entry.preserve.mods = 0;
508 return AddMapEntry(info, type, &entry, true, true);
511 /***====================================================================***/
514 AddPreserve(KeyTypesInfo *info, KeyTypeInfo *type,
515 xkb_mod_mask_t mods, xkb_mod_mask_t preserve_mods)
517 struct xkb_kt_map_entry *entry;
518 struct xkb_kt_map_entry new;
520 darray_foreach(entry, type->entries) {
521 if (entry->mods.mods != mods)
524 /* Map exists without previous preserve (or "None"); override. */
525 if (entry->preserve.mods == 0) {
526 entry->preserve.mods = preserve_mods;
530 /* Map exists with same preserve; do nothing. */
531 if (entry->preserve.mods == preserve_mods) {
532 log_lvl(info->keymap->ctx, 10,
533 "Identical definitions for preserve[%s] in %s; "
535 VModMaskText(info->keymap, mods),
536 TypeTxt(info, type));
540 /* Map exists with different preserve; latter wins. */
541 log_lvl(info->keymap->ctx, 1,
542 "Multiple definitions for preserve[%s] in %s; "
543 "Using %s, ignoring %s\n",
544 VModMaskText(info->keymap, mods),
546 VModMaskText(info->keymap, preserve_mods),
547 VModMaskText(info->keymap, entry->preserve.mods));
549 entry->preserve.mods = preserve_mods;
554 * Map does not exist, i.e. preserve[] came before map[].
555 * Create a map with the specified mask mapping to Level1. The level
556 * may be overriden later with an explicit map[] statement.
559 new.mods.mods = mods;
560 new.preserve.mods = preserve_mods;
561 darray_append(type->entries, new);
566 SetPreserve(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
569 xkb_mod_mask_t mods, preserve_mods;
571 if (arrayNdx == NULL)
572 return ReportTypeShouldBeArray(info, type, "preserve entry");
574 if (!ExprResolveVModMask(info->keymap, arrayNdx, &mods))
575 return ReportTypeBadType(info, type, "preserve entry",
578 if (mods & ~type->mods) {
579 const char *before, *after;
581 before = VModMaskText(info->keymap, mods);
583 after = VModMaskText(info->keymap, mods);
585 log_lvl(info->keymap->ctx, 1,
586 "Preserve for modifiers not used by the %s type; "
587 "Index %s converted to %s\n",
588 TypeTxt(info, type), before, after);
591 if (!ExprResolveVModMask(info->keymap, value, &preserve_mods)) {
592 log_err(info->keymap->ctx,
593 "Preserve value in a key type is not a modifier mask; "
594 "Ignoring preserve[%s] in type %s\n",
595 VModMaskText(info->keymap, mods),
596 TypeTxt(info, type));
600 if (preserve_mods & ~mods) {
601 const char *before, *after;
603 before = VModMaskText(info->keymap, preserve_mods);
604 preserve_mods &= mods;
605 after = VModMaskText(info->keymap, preserve_mods);
607 log_lvl(info->keymap->ctx, 1,
608 "Illegal value for preserve[%s] in type %s; "
609 "Converted %s to %s\n",
610 VModMaskText(info->keymap, mods),
611 TypeTxt(info, type), before, after);
614 return AddPreserve(info, type, mods, preserve_mods);
617 /***====================================================================***/
620 AddLevelName(KeyTypesInfo *info, KeyTypeInfo *type,
621 xkb_level_index_t level, xkb_atom_t name, bool clobber)
624 if (level >= darray_size(type->level_names)) {
625 darray_resize0(type->level_names, level + 1);
629 /* Same level, same name. */
630 if (darray_item(type->level_names, level) == name) {
631 log_lvl(info->keymap->ctx, 10,
632 "Duplicate names for level %d of key type %s; Ignored\n",
633 level + 1, TypeTxt(info, type));
637 /* Same level, different name. */
638 if (darray_item(type->level_names, level) != XKB_ATOM_NONE) {
639 const char *old, *new;
640 old = xkb_atom_text(info->keymap->ctx,
641 darray_item(type->level_names, level));
642 new = xkb_atom_text(info->keymap->ctx, name);
643 log_lvl(info->keymap->ctx, 1,
644 "Multiple names for level %d of key type %s; "
645 "Using %s, ignoring %s\n",
646 level + 1, TypeTxt(info, type),
647 (clobber ? new : old), (clobber ? old : new));
653 /* XXX: What about different level, same name? */
656 darray_item(type->level_names, level) = name;
661 SetLevelName(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
664 xkb_level_index_t level;
665 xkb_atom_t level_name;
666 struct xkb_context *ctx = info->keymap->ctx;
669 if (arrayNdx == NULL)
670 return ReportTypeShouldBeArray(info, type, "level name");
672 if (!ExprResolveLevel(ctx, arrayNdx, &level))
673 return ReportTypeBadType(info, type, "level name", "integer");
675 if (!ExprResolveString(ctx, value, &str)) {
676 log_err(info->keymap->ctx,
677 "Non-string name for level %d in key type %s; "
678 "Ignoring illegal level name definition\n",
679 level + 1, xkb_atom_text(ctx, type->name));
683 level_name = xkb_atom_intern(ctx, str);
685 return AddLevelName(info, type, level, level_name, true);
688 /***====================================================================***/
691 * Parses the fields in a type "..." { } description.
693 * @param field The field to parse (e.g. modifiers, map, level_name)
696 SetKeyTypeField(KeyTypesInfo *info, KeyTypeInfo *type,
697 const char *field, ExprDef *arrayNdx, ExprDef *value)
700 enum type_field type_field = 0;
702 if (istreq(field, "modifiers")) {
703 type_field = TYPE_FIELD_MASK;
704 ok = SetModifiers(info, type, arrayNdx, value);
706 else if (istreq(field, "map")) {
707 type_field = TYPE_FIELD_MAP;
708 ok = SetMapEntry(info, type, arrayNdx, value);
710 else if (istreq(field, "preserve")) {
711 type_field = TYPE_FIELD_PRESERVE;
712 ok = SetPreserve(info, type, arrayNdx, value);
714 else if (istreq(field, "levelname") || istreq(field, "level_name")) {
715 type_field = TYPE_FIELD_LEVEL_NAME;
716 ok = SetLevelName(info, type, arrayNdx, value);
718 log_err(info->keymap->ctx,
719 "Unknown field %s in key type %s; Definition ignored\n",
720 field, TypeTxt(info, type));
723 type->defined |= type_field;
728 HandleKeyTypeBody(KeyTypesInfo *info, VarDef *def, KeyTypeInfo *type)
731 const char *elem, *field;
734 for (; def; def = (VarDef *) def->common.next) {
735 ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
740 if (elem && istreq(elem, "type")) {
741 log_err(info->keymap->ctx,
742 "Support for changing the default type has been removed; "
743 "Statement ignored\n");
747 ok = SetKeyTypeField(info, type, field, arrayNdx, def->value);
754 * Process a type "XYZ" { } specification in the xkb_types section.
758 HandleKeyTypeDef(KeyTypesInfo *info, KeyTypeDef *def, enum merge_mode merge)
762 .file_id = info->file_id,
763 .merge = (def->merge == MERGE_DEFAULT ? merge : def->merge),
767 .entries = darray_new(),
768 .level_names = darray_new(),
771 /* Parse the actual content. */
772 if (!HandleKeyTypeBody(info, def->body, &type)) {
777 /* Now add the new keytype to the info struct */
778 if (!AddKeyType(info, &type)) {
787 * Process an xkb_types section.
789 * @param file The parsed xkb_types section.
790 * @param merge Merge Strategy (e.g. MERGE_OVERRIDE)
791 * @param info Pointer to memory where the outcome will be stored.
794 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge)
800 info->name = strdup_safe(file->name);
802 for (stmt = file->defs; stmt; stmt = stmt->next) {
803 switch (stmt->type) {
805 ok = HandleIncludeKeyTypes(info, (IncludeStmt *) stmt);
807 case STMT_TYPE: /* e.g. type "ONE_LEVEL" */
808 ok = HandleKeyTypeDef(info, (KeyTypeDef *) stmt, merge);
811 log_err(info->keymap->ctx,
812 "Support for changing the default type has been removed; "
813 "Statement ignored\n");
816 case STMT_VMOD: /* virtual_modifiers NumLock, ... */
817 ok = HandleVModDef((VModDef *) stmt, info->keymap, merge,
821 log_err(info->keymap->ctx,
822 "Key type files may not include other declarations; "
823 "Ignoring %s\n", StmtTypeToString(stmt->type));
831 if (info->errorCount > 10) {
832 log_err(info->keymap->ctx,
833 "Abandoning keytypes file \"%s\"\n", file->topName);
840 CopyDefToKeyType(KeyTypesInfo *info, KeyTypeInfo *def,
841 struct xkb_key_type *type)
843 type->mods.mods = def->mods;
844 type->num_levels = def->num_levels;
845 type->map = darray_mem(def->entries, 0);
846 type->num_entries = darray_size(def->entries);
847 darray_init(def->entries);
848 type->name = def->name;
849 type->level_names = darray_mem(def->level_names, 0);
850 darray_init(def->level_names);
856 CompileKeyTypes(XkbFile *file, struct xkb_keymap *keymap,
857 enum merge_mode merge)
860 unsigned int num_types;
864 InitKeyTypesInfo(&info, keymap, file->id);
866 HandleKeyTypesFile(&info, file, merge);
868 if (info.errorCount != 0)
872 keymap->types_section_name = strdup(info.name);
874 num_types = info.num_types ? info.num_types : 1;
875 keymap->types = calloc(num_types, sizeof(*keymap->types));
878 keymap->num_types = num_types;
881 * If no types were specified, a default unnamed one-level type is
884 if (info.num_types == 0) {
886 .name = xkb_atom_intern(keymap->ctx, "default"),
889 .entries = darray_new(),
890 .level_names = darray_new(),
893 if (!CopyDefToKeyType(&info, &dflt, &keymap->types[0]))
897 list_foreach(def, &info.types, entry)
898 if (!CopyDefToKeyType(&info, def, &keymap->types[i++]))
902 FreeKeyTypesInfo(&info);
906 FreeKeyTypesInfo(&info);