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),
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;
162 darray(KeyTypeInfo) types;
163 struct xkb_keymap *keymap;
166 /***====================================================================***/
168 static inline const char *
169 MapEntryTxt(KeyTypesInfo *info, struct xkb_kt_map_entry *entry)
171 return ModMaskText(info->keymap, entry->mods.mods);
174 static inline const char *
175 TypeTxt(KeyTypesInfo *info, KeyTypeInfo *type)
177 return xkb_atom_text(info->keymap->ctx, type->name);
180 static inline const char *
181 TypeMaskTxt(KeyTypesInfo *info, KeyTypeInfo *type)
183 return ModMaskText(info->keymap, type->mods);
187 ReportTypeShouldBeArray(KeyTypesInfo *info, KeyTypeInfo *type,
190 return ReportShouldBeArray(info->keymap->ctx, "key type", field,
191 TypeTxt(info, type));
195 ReportTypeBadType(KeyTypesInfo *info, KeyTypeInfo *type,
196 const char *field, const char *wanted)
198 return ReportBadType(info->keymap->ctx, "key type", field,
199 TypeTxt(info, type), wanted);
203 ReportTypeBadWidth(KeyTypesInfo *info, const char *type, int has, int needs)
205 log_err(info->keymap->ctx,
206 "Key type \"%s\" has %d levels, must have %d; "
207 "Illegal type definition ignored\n",
212 /***====================================================================***/
215 InitKeyTypesInfo(KeyTypesInfo *info, struct xkb_keymap *keymap,
218 memset(info, 0, sizeof(*info));
219 info->keymap = keymap;
220 info->file_id = file_id;
224 ClearKeyTypeInfo(KeyTypeInfo *type)
226 darray_free(type->entries);
227 darray_free(type->level_names);
231 ClearKeyTypesInfo(KeyTypesInfo *info)
234 darray_free(info->types);
238 FindMatchingKeyType(KeyTypesInfo *info, xkb_atom_t name)
242 darray_foreach(old, info->types)
243 if (old->name == name)
250 AddKeyType(KeyTypesInfo *info, KeyTypeInfo *new)
253 int verbosity = xkb_context_get_log_verbosity(info->keymap->ctx);
255 old = FindMatchingKeyType(info, new->name);
257 if (new->merge == MERGE_REPLACE || new->merge == MERGE_OVERRIDE) {
258 if ((old->file_id == new->file_id && verbosity > 0) ||
260 log_warn(info->keymap->ctx,
261 "Multiple definitions of the %s key type; "
262 "Earlier definition ignored\n",
263 xkb_atom_text(info->keymap->ctx, new->name));
266 ClearKeyTypeInfo(old);
268 darray_init(new->entries);
269 darray_init(new->level_names);
273 if (old->file_id == new->file_id)
274 log_vrb(info->keymap->ctx, 4,
275 "Multiple definitions of the %s key type; "
276 "Later definition ignored\n",
277 xkb_atom_text(info->keymap->ctx, new->name));
279 ClearKeyTypeInfo(new);
283 new->file_id = info->file_id;
284 darray_append(info->types, *new);
288 /***====================================================================***/
291 MergeIncludedKeyTypes(KeyTypesInfo *into, KeyTypesInfo *from,
292 enum merge_mode merge)
296 if (from->errorCount > 0) {
297 into->errorCount += from->errorCount;
301 if (into->name == NULL) {
302 into->name = from->name;
306 darray_foreach(type, from->types) {
307 type->merge = (merge == MERGE_DEFAULT ? type->merge : merge);
308 if (!AddKeyType(into, type))
314 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge);
317 HandleIncludeKeyTypes(KeyTypesInfo *info, IncludeStmt *stmt)
319 enum merge_mode merge = MERGE_DEFAULT;
321 KeyTypesInfo included, next_incl;
323 InitKeyTypesInfo(&included, info->keymap, info->file_id);
326 included.name = stmt->stmt;
330 for (; stmt; stmt = stmt->next_incl) {
331 if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_TYPES,
333 info->errorCount += 10;
334 ClearKeyTypesInfo(&included);
338 InitKeyTypesInfo(&next_incl, info->keymap, rtrn->id);
340 HandleKeyTypesFile(&next_incl, rtrn, merge);
342 MergeIncludedKeyTypes(&included, &next_incl, merge);
344 ClearKeyTypesInfo(&next_incl);
348 MergeIncludedKeyTypes(info, &included, merge);
349 ClearKeyTypesInfo(&included);
351 return (info->errorCount == 0);
354 /***====================================================================***/
357 SetModifiers(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
363 log_warn(info->keymap->ctx,
364 "The modifiers field of a key type is not an array; "
365 "Illegal array subscript ignored\n");
367 if (!ExprResolveModMask(info->keymap, value, MOD_BOTH, &mods)) {
368 log_err(info->keymap->ctx,
369 "Key type mask field must be a modifier mask; "
370 "Key type definition ignored\n");
374 if (type->defined & TYPE_FIELD_MASK) {
375 log_warn(info->keymap->ctx,
376 "Multiple modifier mask definitions for key type %s; "
377 "Using %s, ignoring %s\n",
378 xkb_atom_text(info->keymap->ctx, type->name),
379 TypeMaskTxt(info, type),
380 ModMaskText(info->keymap, mods));
388 /***====================================================================***/
390 static struct xkb_kt_map_entry *
391 FindMatchingMapEntry(KeyTypeInfo *type, xkb_mod_mask_t mods)
393 struct xkb_kt_map_entry *entry;
395 darray_foreach(entry, type->entries)
396 if (entry->mods.mods == mods)
403 AddMapEntry(KeyTypesInfo *info, KeyTypeInfo *type,
404 struct xkb_kt_map_entry *new, bool clobber, bool report)
406 struct xkb_kt_map_entry * old;
408 old = FindMatchingMapEntry(type, new->mods.mods);
410 if (report && old->level != new->level) {
411 log_warn(info->keymap->ctx,
412 "Multiple map entries for %s in %s; "
413 "Using %d, ignoring %d\n",
414 MapEntryTxt(info, new), TypeTxt(info, type),
415 (clobber ? new->level : old->level) + 1,
416 (clobber ? old->level : new->level) + 1);
419 log_vrb(info->keymap->ctx, 10,
420 "Multiple occurences of map[%s]= %d in %s; Ignored\n",
421 MapEntryTxt(info, new), new->level + 1,
422 TypeTxt(info, type));
427 if (new->level >= type->num_levels)
428 type->num_levels = new->level + 1;
429 old->level = new->level;
435 if (new->level >= type->num_levels)
436 type->num_levels = new->level + 1;
438 darray_append(type->entries, *new);
443 SetMapEntry(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
446 struct xkb_kt_map_entry entry;
448 if (arrayNdx == NULL)
449 return ReportTypeShouldBeArray(info, type, "map entry");
451 if (!ExprResolveModMask(info->keymap, arrayNdx, MOD_BOTH, &entry.mods.mods))
452 return ReportTypeBadType(info, type, "map entry", "modifier mask");
454 if (entry.mods.mods & (~type->mods)) {
455 log_vrb(info->keymap->ctx, 1,
456 "Map entry for unused modifiers in %s; "
457 "Using %s instead of %s\n",
459 ModMaskText(info->keymap, entry.mods.mods & type->mods),
460 MapEntryTxt(info, &entry));
461 entry.mods.mods &= type->mods;
464 if (!ExprResolveLevel(info->keymap->ctx, value, &entry.level)) {
465 log_err(info->keymap->ctx,
466 "Level specifications in a key type must be integer; "
467 "Ignoring malformed level specification\n");
471 entry.preserve.mods = 0;
473 return AddMapEntry(info, type, &entry, true, true);
476 /***====================================================================***/
479 AddPreserve(KeyTypesInfo *info, KeyTypeInfo *type,
480 xkb_mod_mask_t mods, xkb_mod_mask_t preserve_mods)
482 struct xkb_kt_map_entry *entry;
483 struct xkb_kt_map_entry new;
485 darray_foreach(entry, type->entries) {
486 if (entry->mods.mods != mods)
489 /* Map exists without previous preserve (or "None"); override. */
490 if (entry->preserve.mods == 0) {
491 entry->preserve.mods = preserve_mods;
495 /* Map exists with same preserve; do nothing. */
496 if (entry->preserve.mods == preserve_mods) {
497 log_vrb(info->keymap->ctx, 10,
498 "Identical definitions for preserve[%s] in %s; "
500 ModMaskText(info->keymap, mods),
501 TypeTxt(info, type));
505 /* Map exists with different preserve; latter wins. */
506 log_vrb(info->keymap->ctx, 1,
507 "Multiple definitions for preserve[%s] in %s; "
508 "Using %s, ignoring %s\n",
509 ModMaskText(info->keymap, mods),
511 ModMaskText(info->keymap, preserve_mods),
512 ModMaskText(info->keymap, entry->preserve.mods));
514 entry->preserve.mods = preserve_mods;
519 * Map does not exist, i.e. preserve[] came before map[].
520 * Create a map with the specified mask mapping to Level1. The level
521 * may be overriden later with an explicit map[] statement.
524 new.mods.mods = mods;
525 new.preserve.mods = preserve_mods;
526 darray_append(type->entries, new);
531 SetPreserve(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
534 xkb_mod_mask_t mods, preserve_mods;
536 if (arrayNdx == NULL)
537 return ReportTypeShouldBeArray(info, type, "preserve entry");
539 if (!ExprResolveModMask(info->keymap, arrayNdx, MOD_BOTH, &mods))
540 return ReportTypeBadType(info, type, "preserve entry",
543 if (mods & ~type->mods) {
544 const char *before, *after;
546 before = ModMaskText(info->keymap, mods);
548 after = ModMaskText(info->keymap, mods);
550 log_vrb(info->keymap->ctx, 1,
551 "Preserve for modifiers not used by the %s type; "
552 "Index %s converted to %s\n",
553 TypeTxt(info, type), before, after);
556 if (!ExprResolveModMask(info->keymap, value, MOD_BOTH, &preserve_mods)) {
557 log_err(info->keymap->ctx,
558 "Preserve value in a key type is not a modifier mask; "
559 "Ignoring preserve[%s] in type %s\n",
560 ModMaskText(info->keymap, mods),
561 TypeTxt(info, type));
565 if (preserve_mods & ~mods) {
566 const char *before, *after;
568 before = ModMaskText(info->keymap, preserve_mods);
569 preserve_mods &= mods;
570 after = ModMaskText(info->keymap, preserve_mods);
572 log_vrb(info->keymap->ctx, 1,
573 "Illegal value for preserve[%s] in type %s; "
574 "Converted %s to %s\n",
575 ModMaskText(info->keymap, mods),
576 TypeTxt(info, type), before, after);
579 return AddPreserve(info, type, mods, preserve_mods);
582 /***====================================================================***/
585 AddLevelName(KeyTypesInfo *info, KeyTypeInfo *type,
586 xkb_level_index_t level, xkb_atom_t name, bool clobber)
589 if (level >= darray_size(type->level_names)) {
590 darray_resize0(type->level_names, level + 1);
594 /* Same level, same name. */
595 if (darray_item(type->level_names, level) == name) {
596 log_vrb(info->keymap->ctx, 10,
597 "Duplicate names for level %d of key type %s; Ignored\n",
598 level + 1, TypeTxt(info, type));
602 /* Same level, different name. */
603 if (darray_item(type->level_names, level) != XKB_ATOM_NONE) {
604 const char *old, *new;
605 old = xkb_atom_text(info->keymap->ctx,
606 darray_item(type->level_names, level));
607 new = xkb_atom_text(info->keymap->ctx, name);
608 log_vrb(info->keymap->ctx, 1,
609 "Multiple names for level %d of key type %s; "
610 "Using %s, ignoring %s\n",
611 level + 1, TypeTxt(info, type),
612 (clobber ? new : old), (clobber ? old : new));
618 /* XXX: What about different level, same name? */
621 darray_item(type->level_names, level) = name;
626 SetLevelName(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
629 xkb_level_index_t level;
630 xkb_atom_t level_name;
631 struct xkb_context *ctx = info->keymap->ctx;
633 if (arrayNdx == NULL)
634 return ReportTypeShouldBeArray(info, type, "level name");
636 if (!ExprResolveLevel(ctx, arrayNdx, &level))
637 return ReportTypeBadType(info, type, "level name", "integer");
639 if (!ExprResolveString(ctx, value, &level_name)) {
640 log_err(info->keymap->ctx,
641 "Non-string name for level %d in key type %s; "
642 "Ignoring illegal level name definition\n",
643 level + 1, xkb_atom_text(ctx, type->name));
647 return AddLevelName(info, type, level, level_name, true);
650 /***====================================================================***/
653 SetKeyTypeField(KeyTypesInfo *info, KeyTypeInfo *type,
654 const char *field, ExprDef *arrayNdx, ExprDef *value)
657 enum type_field type_field = 0;
659 if (istreq(field, "modifiers")) {
660 type_field = TYPE_FIELD_MASK;
661 ok = SetModifiers(info, type, arrayNdx, value);
663 else if (istreq(field, "map")) {
664 type_field = TYPE_FIELD_MAP;
665 ok = SetMapEntry(info, type, arrayNdx, value);
667 else if (istreq(field, "preserve")) {
668 type_field = TYPE_FIELD_PRESERVE;
669 ok = SetPreserve(info, type, arrayNdx, value);
671 else if (istreq(field, "levelname") || istreq(field, "level_name")) {
672 type_field = TYPE_FIELD_LEVEL_NAME;
673 ok = SetLevelName(info, type, arrayNdx, value);
675 log_err(info->keymap->ctx,
676 "Unknown field %s in key type %s; Definition ignored\n",
677 field, TypeTxt(info, type));
680 type->defined |= type_field;
685 HandleKeyTypeBody(KeyTypesInfo *info, VarDef *def, KeyTypeInfo *type)
688 const char *elem, *field;
691 for (; def; def = (VarDef *) def->common.next) {
692 ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
697 if (elem && istreq(elem, "type")) {
698 log_err(info->keymap->ctx,
699 "Support for changing the default type has been removed; "
700 "Statement ignored\n");
704 ok = SetKeyTypeField(info, type, field, arrayNdx, def->value);
711 HandleKeyTypeDef(KeyTypesInfo *info, KeyTypeDef *def, enum merge_mode merge)
715 .file_id = info->file_id,
716 .merge = (def->merge == MERGE_DEFAULT ? merge : def->merge),
720 .entries = darray_new(),
721 .level_names = darray_new(),
724 if (!HandleKeyTypeBody(info, def->body, &type)) {
729 if (!AddKeyType(info, &type)) {
738 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge)
743 info->name = strdup_safe(file->name);
745 for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
746 switch (stmt->type) {
748 ok = HandleIncludeKeyTypes(info, (IncludeStmt *) stmt);
751 ok = HandleKeyTypeDef(info, (KeyTypeDef *) stmt, merge);
754 log_err(info->keymap->ctx,
755 "Support for changing the default type has been removed; "
756 "Statement ignored\n");
760 ok = HandleVModDef(info->keymap, (VModDef *) stmt);
763 log_err(info->keymap->ctx,
764 "Key type files may not include other declarations; "
765 "Ignoring %s\n", stmt_type_to_string(stmt->type));
773 if (info->errorCount > 10) {
774 log_err(info->keymap->ctx,
775 "Abandoning keytypes file \"%s\"\n", file->topName);
782 CopyDefToKeyType(KeyTypeInfo *def, struct xkb_key_type *type)
784 type->mods.mods = def->mods;
785 type->num_levels = def->num_levels;
786 type->map = darray_mem(def->entries, 0);
787 type->num_entries = darray_size(def->entries);
788 darray_init(def->entries);
789 type->name = def->name;
790 type->level_names = darray_mem(def->level_names, 0);
791 darray_init(def->level_names);
795 CopyKeyTypesToKeymap(struct xkb_keymap *keymap, KeyTypesInfo *info)
798 unsigned int num_types;
800 num_types = darray_size(info->types) ? darray_size(info->types) : 1;
801 keymap->types = calloc(num_types, sizeof(*keymap->types));
805 keymap->num_types = num_types;
808 * If no types were specified, a default unnamed one-level type is
811 if (darray_empty(info->types)) {
813 .name = xkb_atom_intern(keymap->ctx, "default"),
816 .entries = darray_new(),
817 .level_names = darray_new(),
820 CopyDefToKeyType(&dflt, &keymap->types[0]);
822 for (i = 0; i < num_types; i++)
823 CopyDefToKeyType(&darray_item(info->types, i), &keymap->types[i]);
826 keymap->types_section_name = strdup_safe(info->name);
832 CompileKeyTypes(XkbFile *file, struct xkb_keymap *keymap,
833 enum merge_mode merge)
837 InitKeyTypesInfo(&info, keymap, file->id);
839 HandleKeyTypesFile(&info, file, merge);
840 if (info.errorCount != 0)
843 if (!CopyKeyTypesToKeymap(keymap, &info))
846 ClearKeyTypesInfo(&info);
850 ClearKeyTypesInfo(&info);