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 * After all of the xkb_types sections have been compiled, the following
131 * members of struct xkb_keymap are finalized:
132 * struct xkb_key_type *types;
133 * unsigned int num_types;
134 * char *types_section_name;
135 * TODO: virtual modifiers.
139 TYPE_FIELD_MASK = (1 << 0),
140 TYPE_FIELD_MAP = (1 << 1),
141 TYPE_FIELD_PRESERVE = (1 << 2),
142 TYPE_FIELD_LEVEL_NAME = (1 << 3),
145 typedef struct _KeyTypeInfo {
146 enum type_field defined;
148 enum merge_mode merge;
152 xkb_level_index_t num_levels;
153 darray(struct xkb_kt_map_entry) entries;
154 darray(xkb_atom_t) level_names;
157 typedef struct _KeyTypesInfo {
162 darray(KeyTypeInfo) types;
164 struct xkb_keymap *keymap;
167 /***====================================================================***/
169 static inline const char *
170 MapEntryTxt(KeyTypesInfo *info, struct xkb_kt_map_entry *entry)
172 return VModMaskText(info->keymap, entry->mods.mods);
175 static inline const char *
176 TypeTxt(KeyTypesInfo *info, KeyTypeInfo *type)
178 return xkb_atom_text(info->keymap->ctx, type->name);
181 static inline const char *
182 TypeMaskTxt(KeyTypesInfo *info, KeyTypeInfo *type)
184 return VModMaskText(info->keymap, type->mods);
188 ReportTypeShouldBeArray(KeyTypesInfo *info, KeyTypeInfo *type,
191 return ReportShouldBeArray(info->keymap, "key type", field,
192 TypeTxt(info, type));
196 ReportTypeBadType(KeyTypesInfo *info, KeyTypeInfo *type,
197 const char *field, const char *wanted)
199 return ReportBadType(info->keymap->ctx, "key type", field,
200 TypeTxt(info, type), wanted);
204 ReportTypeBadWidth(KeyTypesInfo *info, const char *type, int has, int needs)
206 log_err(info->keymap->ctx,
207 "Key type \"%s\" has %d levels, must have %d; "
208 "Illegal type definition ignored\n",
213 /***====================================================================***/
216 InitKeyTypesInfo(KeyTypesInfo *info, struct xkb_keymap *keymap,
220 info->errorCount = 0;
221 darray_init(info->types);
222 info->file_id = file_id;
223 InitVModInfo(&info->vmods, keymap);
224 info->keymap = keymap;
228 ClearKeyTypeInfo(KeyTypeInfo *type)
230 darray_free(type->entries);
231 darray_free(type->level_names);
235 FreeKeyTypesInfo(KeyTypesInfo * info)
239 darray_free(info->types);
243 FindMatchingKeyType(KeyTypesInfo *info, xkb_atom_t name)
247 darray_foreach(old, info->types)
248 if (old->name == name)
255 AddKeyType(KeyTypesInfo *info, KeyTypeInfo *new)
258 int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
260 old = FindMatchingKeyType(info, new->name);
262 if (new->merge == MERGE_REPLACE || new->merge == MERGE_OVERRIDE) {
263 if ((old->file_id == new->file_id && verbosity > 0) ||
265 log_warn(info->keymap->ctx,
266 "Multiple definitions of the %s key type; "
267 "Earlier definition ignored\n",
268 xkb_atom_text(info->keymap->ctx, new->name));
271 ClearKeyTypeInfo(old);
273 darray_init(new->entries);
274 darray_init(new->level_names);
278 if (old->file_id == new->file_id)
279 log_vrb(info->keymap->ctx, 4,
280 "Multiple definitions of the %s key type; "
281 "Later definition ignored\n",
282 xkb_atom_text(info->keymap->ctx, new->name));
284 ClearKeyTypeInfo(new);
288 new->file_id = info->file_id;
289 darray_append(info->types, *new);
293 /***====================================================================***/
296 MergeIncludedKeyTypes(KeyTypesInfo *into, KeyTypesInfo *from,
297 enum merge_mode merge)
301 if (from->errorCount > 0) {
302 into->errorCount += from->errorCount;
306 if (into->name == NULL) {
307 into->name = from->name;
311 darray_foreach(type, from->types) {
312 type->merge = (merge == MERGE_DEFAULT ? type->merge : merge);
313 if (!AddKeyType(into, type))
319 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge);
322 HandleIncludeKeyTypes(KeyTypesInfo *info, IncludeStmt *stmt)
324 enum merge_mode merge = MERGE_DEFAULT;
326 KeyTypesInfo included, next_incl;
328 InitKeyTypesInfo(&included, info->keymap, info->file_id);
331 included.name = stmt->stmt;
335 for (; stmt; stmt = stmt->next_incl) {
336 if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_TYPES,
338 info->errorCount += 10;
339 FreeKeyTypesInfo(&included);
343 InitKeyTypesInfo(&next_incl, info->keymap, rtrn->id);
345 HandleKeyTypesFile(&next_incl, rtrn, merge);
347 MergeIncludedKeyTypes(&included, &next_incl, merge);
349 FreeKeyTypesInfo(&next_incl);
353 MergeIncludedKeyTypes(info, &included, merge);
354 FreeKeyTypesInfo(&included);
356 return (info->errorCount == 0);
359 /***====================================================================***/
362 SetModifiers(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
368 log_warn(info->keymap->ctx,
369 "The modifiers field of a key type is not an array; "
370 "Illegal array subscript ignored\n");
372 /* get modifier mask for current type */
373 if (!ExprResolveVModMask(info->keymap, value, &mods)) {
374 log_err(info->keymap->ctx,
375 "Key type mask field must be a modifier mask; "
376 "Key type definition ignored\n");
380 if (type->defined & TYPE_FIELD_MASK) {
381 log_warn(info->keymap->ctx,
382 "Multiple modifier mask definitions for key type %s; "
383 "Using %s, ignoring %s\n",
384 xkb_atom_text(info->keymap->ctx, type->name),
385 TypeMaskTxt(info, type),
386 VModMaskText(info->keymap, mods));
394 /***====================================================================***/
396 static struct xkb_kt_map_entry *
397 FindMatchingMapEntry(KeyTypeInfo *type, xkb_mod_mask_t mods)
399 struct xkb_kt_map_entry *entry;
401 darray_foreach(entry, type->entries)
402 if (entry->mods.mods == mods)
409 AddMapEntry(KeyTypesInfo *info, KeyTypeInfo *type,
410 struct xkb_kt_map_entry *new, bool clobber, bool report)
412 struct xkb_kt_map_entry * old;
414 old = FindMatchingMapEntry(type, new->mods.mods);
416 if (report && old->level != new->level) {
417 log_warn(info->keymap->ctx,
418 "Multiple map entries for %s in %s; "
419 "Using %d, ignoring %d\n",
420 MapEntryTxt(info, new), TypeTxt(info, type),
421 (clobber ? new->level : old->level) + 1,
422 (clobber ? old->level : new->level) + 1);
425 log_vrb(info->keymap->ctx, 10,
426 "Multiple occurences of map[%s]= %d in %s; Ignored\n",
427 MapEntryTxt(info, new), new->level + 1,
428 TypeTxt(info, type));
433 if (new->level >= type->num_levels)
434 type->num_levels = new->level + 1;
435 old->level = new->level;
441 if (new->level >= type->num_levels)
442 type->num_levels = new->level + 1;
444 darray_append(type->entries, *new);
449 SetMapEntry(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
452 struct xkb_kt_map_entry entry;
454 if (arrayNdx == NULL)
455 return ReportTypeShouldBeArray(info, type, "map entry");
457 if (!ExprResolveVModMask(info->keymap, arrayNdx, &entry.mods.mods))
458 return ReportTypeBadType(info, type, "map entry", "modifier mask");
460 if (entry.mods.mods & (~type->mods)) {
461 log_vrb(info->keymap->ctx, 1,
462 "Map entry for unused modifiers in %s; "
463 "Using %s instead of %s\n",
465 VModMaskText(info->keymap, entry.mods.mods & type->mods),
466 MapEntryTxt(info, &entry));
467 entry.mods.mods &= type->mods;
470 if (!ExprResolveLevel(info->keymap->ctx, value, &entry.level)) {
471 log_err(info->keymap->ctx,
472 "Level specifications in a key type must be integer; "
473 "Ignoring malformed level specification\n");
477 entry.preserve.mods = 0;
479 return AddMapEntry(info, type, &entry, true, true);
482 /***====================================================================***/
485 AddPreserve(KeyTypesInfo *info, KeyTypeInfo *type,
486 xkb_mod_mask_t mods, xkb_mod_mask_t preserve_mods)
488 struct xkb_kt_map_entry *entry;
489 struct xkb_kt_map_entry new;
491 darray_foreach(entry, type->entries) {
492 if (entry->mods.mods != mods)
495 /* Map exists without previous preserve (or "None"); override. */
496 if (entry->preserve.mods == 0) {
497 entry->preserve.mods = preserve_mods;
501 /* Map exists with same preserve; do nothing. */
502 if (entry->preserve.mods == preserve_mods) {
503 log_vrb(info->keymap->ctx, 10,
504 "Identical definitions for preserve[%s] in %s; "
506 VModMaskText(info->keymap, mods),
507 TypeTxt(info, type));
511 /* Map exists with different preserve; latter wins. */
512 log_vrb(info->keymap->ctx, 1,
513 "Multiple definitions for preserve[%s] in %s; "
514 "Using %s, ignoring %s\n",
515 VModMaskText(info->keymap, mods),
517 VModMaskText(info->keymap, preserve_mods),
518 VModMaskText(info->keymap, entry->preserve.mods));
520 entry->preserve.mods = preserve_mods;
525 * Map does not exist, i.e. preserve[] came before map[].
526 * Create a map with the specified mask mapping to Level1. The level
527 * may be overriden later with an explicit map[] statement.
530 new.mods.mods = mods;
531 new.preserve.mods = preserve_mods;
532 darray_append(type->entries, new);
537 SetPreserve(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
540 xkb_mod_mask_t mods, preserve_mods;
542 if (arrayNdx == NULL)
543 return ReportTypeShouldBeArray(info, type, "preserve entry");
545 if (!ExprResolveVModMask(info->keymap, arrayNdx, &mods))
546 return ReportTypeBadType(info, type, "preserve entry",
549 if (mods & ~type->mods) {
550 const char *before, *after;
552 before = VModMaskText(info->keymap, mods);
554 after = VModMaskText(info->keymap, mods);
556 log_vrb(info->keymap->ctx, 1,
557 "Preserve for modifiers not used by the %s type; "
558 "Index %s converted to %s\n",
559 TypeTxt(info, type), before, after);
562 if (!ExprResolveVModMask(info->keymap, value, &preserve_mods)) {
563 log_err(info->keymap->ctx,
564 "Preserve value in a key type is not a modifier mask; "
565 "Ignoring preserve[%s] in type %s\n",
566 VModMaskText(info->keymap, mods),
567 TypeTxt(info, type));
571 if (preserve_mods & ~mods) {
572 const char *before, *after;
574 before = VModMaskText(info->keymap, preserve_mods);
575 preserve_mods &= mods;
576 after = VModMaskText(info->keymap, preserve_mods);
578 log_vrb(info->keymap->ctx, 1,
579 "Illegal value for preserve[%s] in type %s; "
580 "Converted %s to %s\n",
581 VModMaskText(info->keymap, mods),
582 TypeTxt(info, type), before, after);
585 return AddPreserve(info, type, mods, preserve_mods);
588 /***====================================================================***/
591 AddLevelName(KeyTypesInfo *info, KeyTypeInfo *type,
592 xkb_level_index_t level, xkb_atom_t name, bool clobber)
595 if (level >= darray_size(type->level_names)) {
596 darray_resize0(type->level_names, level + 1);
600 /* Same level, same name. */
601 if (darray_item(type->level_names, level) == name) {
602 log_vrb(info->keymap->ctx, 10,
603 "Duplicate names for level %d of key type %s; Ignored\n",
604 level + 1, TypeTxt(info, type));
608 /* Same level, different name. */
609 if (darray_item(type->level_names, level) != XKB_ATOM_NONE) {
610 const char *old, *new;
611 old = xkb_atom_text(info->keymap->ctx,
612 darray_item(type->level_names, level));
613 new = xkb_atom_text(info->keymap->ctx, name);
614 log_vrb(info->keymap->ctx, 1,
615 "Multiple names for level %d of key type %s; "
616 "Using %s, ignoring %s\n",
617 level + 1, TypeTxt(info, type),
618 (clobber ? new : old), (clobber ? old : new));
624 /* XXX: What about different level, same name? */
627 darray_item(type->level_names, level) = name;
632 SetLevelName(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
635 xkb_level_index_t level;
636 xkb_atom_t level_name;
637 struct xkb_context *ctx = info->keymap->ctx;
640 if (arrayNdx == NULL)
641 return ReportTypeShouldBeArray(info, type, "level name");
643 if (!ExprResolveLevel(ctx, arrayNdx, &level))
644 return ReportTypeBadType(info, type, "level name", "integer");
646 if (!ExprResolveString(ctx, value, &str)) {
647 log_err(info->keymap->ctx,
648 "Non-string name for level %d in key type %s; "
649 "Ignoring illegal level name definition\n",
650 level + 1, xkb_atom_text(ctx, type->name));
654 level_name = xkb_atom_intern(ctx, str);
656 return AddLevelName(info, type, level, level_name, true);
659 /***====================================================================***/
662 SetKeyTypeField(KeyTypesInfo *info, KeyTypeInfo *type,
663 const char *field, ExprDef *arrayNdx, ExprDef *value)
666 enum type_field type_field = 0;
668 if (istreq(field, "modifiers")) {
669 type_field = TYPE_FIELD_MASK;
670 ok = SetModifiers(info, type, arrayNdx, value);
672 else if (istreq(field, "map")) {
673 type_field = TYPE_FIELD_MAP;
674 ok = SetMapEntry(info, type, arrayNdx, value);
676 else if (istreq(field, "preserve")) {
677 type_field = TYPE_FIELD_PRESERVE;
678 ok = SetPreserve(info, type, arrayNdx, value);
680 else if (istreq(field, "levelname") || istreq(field, "level_name")) {
681 type_field = TYPE_FIELD_LEVEL_NAME;
682 ok = SetLevelName(info, type, arrayNdx, value);
684 log_err(info->keymap->ctx,
685 "Unknown field %s in key type %s; Definition ignored\n",
686 field, TypeTxt(info, type));
689 type->defined |= type_field;
694 HandleKeyTypeBody(KeyTypesInfo *info, VarDef *def, KeyTypeInfo *type)
697 const char *elem, *field;
700 for (; def; def = (VarDef *) def->common.next) {
701 ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
706 if (elem && istreq(elem, "type")) {
707 log_err(info->keymap->ctx,
708 "Support for changing the default type has been removed; "
709 "Statement ignored\n");
713 ok = SetKeyTypeField(info, type, field, arrayNdx, def->value);
720 HandleKeyTypeDef(KeyTypesInfo *info, KeyTypeDef *def, enum merge_mode merge)
724 .file_id = info->file_id,
725 .merge = (def->merge == MERGE_DEFAULT ? merge : def->merge),
729 .entries = darray_new(),
730 .level_names = darray_new(),
733 if (!HandleKeyTypeBody(info, def->body, &type)) {
738 if (!AddKeyType(info, &type)) {
747 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge)
753 info->name = strdup_safe(file->name);
755 for (stmt = file->defs; stmt; stmt = stmt->next) {
756 switch (stmt->type) {
758 ok = HandleIncludeKeyTypes(info, (IncludeStmt *) stmt);
760 case STMT_TYPE: /* e.g. type "ONE_LEVEL" */
761 ok = HandleKeyTypeDef(info, (KeyTypeDef *) stmt, merge);
764 log_err(info->keymap->ctx,
765 "Support for changing the default type has been removed; "
766 "Statement ignored\n");
769 case STMT_VMOD: /* virtual_modifiers NumLock, ... */
770 ok = HandleVModDef((VModDef *) stmt, info->keymap, merge,
774 log_err(info->keymap->ctx,
775 "Key type files may not include other declarations; "
776 "Ignoring %s\n", stmt_type_to_string(stmt->type));
784 if (info->errorCount > 10) {
785 log_err(info->keymap->ctx,
786 "Abandoning keytypes file \"%s\"\n", file->topName);
793 CopyDefToKeyType(KeyTypeInfo *def, struct xkb_key_type *type)
795 type->mods.mods = def->mods;
796 type->num_levels = def->num_levels;
797 type->map = darray_mem(def->entries, 0);
798 type->num_entries = darray_size(def->entries);
799 darray_init(def->entries);
800 type->name = def->name;
801 type->level_names = darray_mem(def->level_names, 0);
802 darray_init(def->level_names);
806 CopyKeyTypesToKeymap(struct xkb_keymap *keymap, KeyTypesInfo *info)
809 unsigned int num_types;
811 num_types = darray_size(info->types) ? darray_size(info->types) : 1;
812 keymap->types = calloc(num_types, sizeof(*keymap->types));
816 keymap->num_types = num_types;
819 * If no types were specified, a default unnamed one-level type is
822 if (darray_empty(info->types)) {
824 .name = xkb_atom_intern(keymap->ctx, "default"),
827 .entries = darray_new(),
828 .level_names = darray_new(),
831 CopyDefToKeyType(&dflt, &keymap->types[0]);
833 for (i = 0; i < num_types; i++)
834 CopyDefToKeyType(&darray_item(info->types, i), &keymap->types[i]);
837 keymap->types_section_name = strdup_safe(info->name);
843 CompileKeyTypes(XkbFile *file, struct xkb_keymap *keymap,
844 enum merge_mode merge)
848 InitKeyTypesInfo(&info, keymap, file->id);
850 HandleKeyTypesFile(&info, file, merge);
851 if (info.errorCount != 0)
854 if (!CopyKeyTypesToKeymap(keymap, &info))
857 FreeKeyTypesInfo(&info);
861 FreeKeyTypesInfo(&info);