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;
147 enum merge_mode merge;
151 xkb_level_index_t num_levels;
152 darray(struct xkb_key_type_entry) entries;
153 darray(xkb_atom_t) level_names;
160 darray(KeyTypeInfo) types;
161 struct xkb_keymap *keymap;
164 /***====================================================================***/
166 static inline const char *
167 MapEntryTxt(KeyTypesInfo *info, struct xkb_key_type_entry *entry)
169 return ModMaskText(info->keymap, entry->mods.mods);
172 static inline const char *
173 TypeTxt(KeyTypesInfo *info, KeyTypeInfo *type)
175 return xkb_atom_text(info->keymap->ctx, type->name);
178 static inline const char *
179 TypeMaskTxt(KeyTypesInfo *info, KeyTypeInfo *type)
181 return ModMaskText(info->keymap, type->mods);
185 ReportTypeShouldBeArray(KeyTypesInfo *info, KeyTypeInfo *type,
188 return ReportShouldBeArray(info->keymap->ctx, "key type", field,
189 TypeTxt(info, type));
193 ReportTypeBadType(KeyTypesInfo *info, KeyTypeInfo *type,
194 const char *field, const char *wanted)
196 return ReportBadType(info->keymap->ctx, "key type", field,
197 TypeTxt(info, type), wanted);
200 /***====================================================================***/
203 InitKeyTypesInfo(KeyTypesInfo *info, struct xkb_keymap *keymap)
205 memset(info, 0, sizeof(*info));
206 info->keymap = keymap;
210 ClearKeyTypeInfo(KeyTypeInfo *type)
212 darray_free(type->entries);
213 darray_free(type->level_names);
217 ClearKeyTypesInfo(KeyTypesInfo *info)
220 darray_free(info->types);
224 FindMatchingKeyType(KeyTypesInfo *info, xkb_atom_t name)
228 darray_foreach(old, info->types)
229 if (old->name == name)
236 AddKeyType(KeyTypesInfo *info, KeyTypeInfo *new, bool same_file)
239 const int verbosity = xkb_context_get_log_verbosity(info->keymap->ctx);
241 old = FindMatchingKeyType(info, new->name);
243 if (new->merge == MERGE_REPLACE || new->merge == MERGE_OVERRIDE) {
244 if ((same_file && verbosity > 0) || verbosity > 9) {
245 log_warn(info->keymap->ctx,
246 "Multiple definitions of the %s key type; "
247 "Earlier definition ignored\n",
248 xkb_atom_text(info->keymap->ctx, new->name));
251 ClearKeyTypeInfo(old);
253 darray_init(new->entries);
254 darray_init(new->level_names);
259 log_vrb(info->keymap->ctx, 4,
260 "Multiple definitions of the %s key type; "
261 "Later definition ignored\n",
262 xkb_atom_text(info->keymap->ctx, new->name));
264 ClearKeyTypeInfo(new);
268 darray_append(info->types, *new);
272 /***====================================================================***/
275 MergeIncludedKeyTypes(KeyTypesInfo *into, KeyTypesInfo *from,
276 enum merge_mode merge)
280 if (from->errorCount > 0) {
281 into->errorCount += from->errorCount;
285 if (into->name == NULL) {
286 into->name = from->name;
290 darray_foreach(type, from->types) {
291 type->merge = (merge == MERGE_DEFAULT ? type->merge : merge);
292 if (!AddKeyType(into, type, false))
298 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge);
301 HandleIncludeKeyTypes(KeyTypesInfo *info, IncludeStmt *include)
303 KeyTypesInfo included;
305 InitKeyTypesInfo(&included, info->keymap);
306 included.name = include->stmt;
307 include->stmt = NULL;
309 for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
310 KeyTypesInfo next_incl;
313 file = ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_TYPES);
315 info->errorCount += 10;
316 ClearKeyTypesInfo(&included);
320 InitKeyTypesInfo(&next_incl, info->keymap);
322 HandleKeyTypesFile(&next_incl, file, stmt->merge);
324 MergeIncludedKeyTypes(&included, &next_incl, stmt->merge);
326 ClearKeyTypesInfo(&next_incl);
330 MergeIncludedKeyTypes(info, &included, include->merge);
331 ClearKeyTypesInfo(&included);
333 return (info->errorCount == 0);
336 /***====================================================================***/
339 SetModifiers(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
345 log_warn(info->keymap->ctx,
346 "The modifiers field of a key type is not an array; "
347 "Illegal array subscript ignored\n");
349 if (!ExprResolveModMask(info->keymap, value, MOD_BOTH, &mods)) {
350 log_err(info->keymap->ctx,
351 "Key type mask field must be a modifier mask; "
352 "Key type definition ignored\n");
356 if (type->defined & TYPE_FIELD_MASK) {
357 log_warn(info->keymap->ctx,
358 "Multiple modifier mask definitions for key type %s; "
359 "Using %s, ignoring %s\n",
360 xkb_atom_text(info->keymap->ctx, type->name),
361 TypeMaskTxt(info, type),
362 ModMaskText(info->keymap, mods));
370 /***====================================================================***/
372 static struct xkb_key_type_entry *
373 FindMatchingMapEntry(KeyTypeInfo *type, xkb_mod_mask_t mods)
375 struct xkb_key_type_entry *entry;
377 darray_foreach(entry, type->entries)
378 if (entry->mods.mods == mods)
385 AddMapEntry(KeyTypesInfo *info, KeyTypeInfo *type,
386 struct xkb_key_type_entry *new, bool clobber, bool report)
388 struct xkb_key_type_entry *old;
390 old = FindMatchingMapEntry(type, new->mods.mods);
392 if (report && old->level != new->level) {
393 log_warn(info->keymap->ctx,
394 "Multiple map entries for %s in %s; "
395 "Using %d, ignoring %d\n",
396 MapEntryTxt(info, new), TypeTxt(info, type),
397 (clobber ? new->level : old->level) + 1,
398 (clobber ? old->level : new->level) + 1);
401 log_vrb(info->keymap->ctx, 10,
402 "Multiple occurrences of map[%s]= %d in %s; Ignored\n",
403 MapEntryTxt(info, new), new->level + 1,
404 TypeTxt(info, type));
409 if (new->level >= type->num_levels)
410 type->num_levels = new->level + 1;
411 old->level = new->level;
417 if (new->level >= type->num_levels)
418 type->num_levels = new->level + 1;
420 darray_append(type->entries, *new);
425 SetMapEntry(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
428 struct xkb_key_type_entry entry;
430 if (arrayNdx == NULL)
431 return ReportTypeShouldBeArray(info, type, "map entry");
433 if (!ExprResolveModMask(info->keymap, arrayNdx, MOD_BOTH, &entry.mods.mods))
434 return ReportTypeBadType(info, type, "map entry", "modifier mask");
436 if (entry.mods.mods & (~type->mods)) {
437 log_vrb(info->keymap->ctx, 1,
438 "Map entry for unused modifiers in %s; "
439 "Using %s instead of %s\n",
441 ModMaskText(info->keymap, entry.mods.mods & type->mods),
442 MapEntryTxt(info, &entry));
443 entry.mods.mods &= type->mods;
446 if (!ExprResolveLevel(info->keymap->ctx, value, &entry.level)) {
447 log_err(info->keymap->ctx,
448 "Level specifications in a key type must be integer; "
449 "Ignoring malformed level specification\n");
453 entry.preserve.mods = 0;
455 return AddMapEntry(info, type, &entry, true, true);
458 /***====================================================================***/
461 AddPreserve(KeyTypesInfo *info, KeyTypeInfo *type,
462 xkb_mod_mask_t mods, xkb_mod_mask_t preserve_mods)
464 struct xkb_key_type_entry *entry;
465 struct xkb_key_type_entry new;
467 darray_foreach(entry, type->entries) {
468 if (entry->mods.mods != mods)
471 /* Map exists without previous preserve (or "None"); override. */
472 if (entry->preserve.mods == 0) {
473 entry->preserve.mods = preserve_mods;
477 /* Map exists with same preserve; do nothing. */
478 if (entry->preserve.mods == preserve_mods) {
479 log_vrb(info->keymap->ctx, 10,
480 "Identical definitions for preserve[%s] in %s; "
482 ModMaskText(info->keymap, mods),
483 TypeTxt(info, type));
487 /* Map exists with different preserve; latter wins. */
488 log_vrb(info->keymap->ctx, 1,
489 "Multiple definitions for preserve[%s] in %s; "
490 "Using %s, ignoring %s\n",
491 ModMaskText(info->keymap, mods),
493 ModMaskText(info->keymap, preserve_mods),
494 ModMaskText(info->keymap, entry->preserve.mods));
496 entry->preserve.mods = preserve_mods;
501 * Map does not exist, i.e. preserve[] came before map[].
502 * Create a map with the specified mask mapping to Level1. The level
503 * may be overridden later with an explicit map[] statement.
506 new.mods.mods = mods;
507 new.preserve.mods = preserve_mods;
508 darray_append(type->entries, new);
513 SetPreserve(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
516 xkb_mod_mask_t mods, preserve_mods;
518 if (arrayNdx == NULL)
519 return ReportTypeShouldBeArray(info, type, "preserve entry");
521 if (!ExprResolveModMask(info->keymap, arrayNdx, MOD_BOTH, &mods))
522 return ReportTypeBadType(info, type, "preserve entry",
525 if (mods & ~type->mods) {
526 const char *before, *after;
528 before = ModMaskText(info->keymap, mods);
530 after = ModMaskText(info->keymap, mods);
532 log_vrb(info->keymap->ctx, 1,
533 "Preserve for modifiers not used by the %s type; "
534 "Index %s converted to %s\n",
535 TypeTxt(info, type), before, after);
538 if (!ExprResolveModMask(info->keymap, value, MOD_BOTH, &preserve_mods)) {
539 log_err(info->keymap->ctx,
540 "Preserve value in a key type is not a modifier mask; "
541 "Ignoring preserve[%s] in type %s\n",
542 ModMaskText(info->keymap, mods),
543 TypeTxt(info, type));
547 if (preserve_mods & ~mods) {
548 const char *before, *after;
550 before = ModMaskText(info->keymap, preserve_mods);
551 preserve_mods &= mods;
552 after = ModMaskText(info->keymap, preserve_mods);
554 log_vrb(info->keymap->ctx, 1,
555 "Illegal value for preserve[%s] in type %s; "
556 "Converted %s to %s\n",
557 ModMaskText(info->keymap, mods),
558 TypeTxt(info, type), before, after);
561 return AddPreserve(info, type, mods, preserve_mods);
564 /***====================================================================***/
567 AddLevelName(KeyTypesInfo *info, KeyTypeInfo *type,
568 xkb_level_index_t level, xkb_atom_t name, bool clobber)
571 if (level >= darray_size(type->level_names)) {
572 darray_resize0(type->level_names, level + 1);
576 /* Same level, same name. */
577 if (darray_item(type->level_names, level) == name) {
578 log_vrb(info->keymap->ctx, 10,
579 "Duplicate names for level %d of key type %s; Ignored\n",
580 level + 1, TypeTxt(info, type));
584 /* Same level, different name. */
585 if (darray_item(type->level_names, level) != XKB_ATOM_NONE) {
586 const char *old, *new;
587 old = xkb_atom_text(info->keymap->ctx,
588 darray_item(type->level_names, level));
589 new = xkb_atom_text(info->keymap->ctx, name);
590 log_vrb(info->keymap->ctx, 1,
591 "Multiple names for level %d of key type %s; "
592 "Using %s, ignoring %s\n",
593 level + 1, TypeTxt(info, type),
594 (clobber ? new : old), (clobber ? old : new));
600 /* XXX: What about different level, same name? */
603 darray_item(type->level_names, level) = name;
608 SetLevelName(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
611 xkb_level_index_t level;
612 xkb_atom_t level_name;
613 struct xkb_context *ctx = info->keymap->ctx;
615 if (arrayNdx == NULL)
616 return ReportTypeShouldBeArray(info, type, "level name");
618 if (!ExprResolveLevel(ctx, arrayNdx, &level))
619 return ReportTypeBadType(info, type, "level name", "integer");
621 if (!ExprResolveString(ctx, value, &level_name)) {
622 log_err(info->keymap->ctx,
623 "Non-string name for level %d in key type %s; "
624 "Ignoring illegal level name definition\n",
625 level + 1, xkb_atom_text(ctx, type->name));
629 return AddLevelName(info, type, level, level_name, true);
632 /***====================================================================***/
635 SetKeyTypeField(KeyTypesInfo *info, KeyTypeInfo *type,
636 const char *field, ExprDef *arrayNdx, ExprDef *value)
639 enum type_field type_field = 0;
641 if (istreq(field, "modifiers")) {
642 type_field = TYPE_FIELD_MASK;
643 ok = SetModifiers(info, type, arrayNdx, value);
645 else if (istreq(field, "map")) {
646 type_field = TYPE_FIELD_MAP;
647 ok = SetMapEntry(info, type, arrayNdx, value);
649 else if (istreq(field, "preserve")) {
650 type_field = TYPE_FIELD_PRESERVE;
651 ok = SetPreserve(info, type, arrayNdx, value);
653 else if (istreq(field, "levelname") || istreq(field, "level_name")) {
654 type_field = TYPE_FIELD_LEVEL_NAME;
655 ok = SetLevelName(info, type, arrayNdx, value);
657 log_err(info->keymap->ctx,
658 "Unknown field %s in key type %s; Definition ignored\n",
659 field, TypeTxt(info, type));
662 type->defined |= type_field;
667 HandleKeyTypeBody(KeyTypesInfo *info, VarDef *def, KeyTypeInfo *type)
670 const char *elem, *field;
673 for (; def; def = (VarDef *) def->common.next) {
674 ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
679 if (elem && istreq(elem, "type")) {
680 log_err(info->keymap->ctx,
681 "Support for changing the default type has been removed; "
682 "Statement ignored\n");
686 ok = SetKeyTypeField(info, type, field, arrayNdx, def->value);
693 HandleKeyTypeDef(KeyTypesInfo *info, KeyTypeDef *def, enum merge_mode merge)
697 .merge = (def->merge == MERGE_DEFAULT ? merge : def->merge),
701 .entries = darray_new(),
702 .level_names = darray_new(),
705 if (!HandleKeyTypeBody(info, def->body, &type)) {
710 if (!AddKeyType(info, &type, true)) {
719 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge)
724 info->name = strdup_safe(file->name);
726 for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
727 switch (stmt->type) {
729 ok = HandleIncludeKeyTypes(info, (IncludeStmt *) stmt);
732 ok = HandleKeyTypeDef(info, (KeyTypeDef *) stmt, merge);
735 log_err(info->keymap->ctx,
736 "Support for changing the default type has been removed; "
737 "Statement ignored\n");
741 ok = HandleVModDef(info->keymap, (VModDef *) stmt);
744 log_err(info->keymap->ctx,
745 "Key type files may not include other declarations; "
746 "Ignoring %s\n", stmt_type_to_string(stmt->type));
754 if (info->errorCount > 10) {
755 log_err(info->keymap->ctx,
756 "Abandoning keytypes file \"%s\"\n", file->topName);
762 /***====================================================================***/
765 CopyKeyTypesToKeymap(struct xkb_keymap *keymap, KeyTypesInfo *info)
767 keymap->types_section_name = strdup_safe(info->name);
768 XkbEscapeMapName(keymap->types_section_name);
770 keymap->num_types = darray_size(info->types);
771 if (keymap->num_types == 0)
772 keymap->num_types = 1;
774 keymap->types = calloc(keymap->num_types, sizeof(*keymap->types));
777 * If no types were specified, a default unnamed one-level type is
780 if (darray_empty(info->types)) {
781 struct xkb_key_type *type = &keymap->types[0];
784 type->num_levels = 1;
785 type->entries = NULL;
786 type->num_entries = 0;
787 type->name = xkb_atom_intern_literal(keymap->ctx, "default");
788 type->level_names = NULL;
793 for (unsigned i = 0; i < keymap->num_types; i++) {
794 KeyTypeInfo *def = &darray_item(info->types, i);
795 struct xkb_key_type *type = &keymap->types[i];
797 type->mods.mods = def->mods;
798 type->num_levels = def->num_levels;
799 type->entries = darray_mem(def->entries, 0);
800 type->num_entries = darray_size(def->entries);
801 darray_init(def->entries);
802 type->name = def->name;
803 type->level_names = darray_mem(def->level_names, 0);
804 darray_init(def->level_names);
810 /***====================================================================***/
813 CompileKeyTypes(XkbFile *file, struct xkb_keymap *keymap,
814 enum merge_mode merge)
818 InitKeyTypesInfo(&info, keymap);
820 HandleKeyTypesFile(&info, file, merge);
821 if (info.errorCount != 0)
824 if (!CopyKeyTypesToKeymap(keymap, &info))
827 ClearKeyTypesInfo(&info);
831 ClearKeyTypesInfo(&info);