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;
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,
219 memset(info, 0, sizeof(*info));
220 info->keymap = keymap;
221 info->file_id = file_id;
222 InitVModInfo(&info->vmods, keymap);
226 ClearKeyTypeInfo(KeyTypeInfo *type)
228 darray_free(type->entries);
229 darray_free(type->level_names);
233 ClearKeyTypesInfo(KeyTypesInfo *info)
236 darray_free(info->types);
240 FindMatchingKeyType(KeyTypesInfo *info, xkb_atom_t name)
244 darray_foreach(old, info->types)
245 if (old->name == name)
252 AddKeyType(KeyTypesInfo *info, KeyTypeInfo *new)
255 int verbosity = xkb_context_get_log_verbosity(info->keymap->ctx);
257 old = FindMatchingKeyType(info, new->name);
259 if (new->merge == MERGE_REPLACE || new->merge == MERGE_OVERRIDE) {
260 if ((old->file_id == new->file_id && verbosity > 0) ||
262 log_warn(info->keymap->ctx,
263 "Multiple definitions of the %s key type; "
264 "Earlier definition ignored\n",
265 xkb_atom_text(info->keymap->ctx, new->name));
268 ClearKeyTypeInfo(old);
270 darray_init(new->entries);
271 darray_init(new->level_names);
275 if (old->file_id == new->file_id)
276 log_vrb(info->keymap->ctx, 4,
277 "Multiple definitions of the %s key type; "
278 "Later definition ignored\n",
279 xkb_atom_text(info->keymap->ctx, new->name));
281 ClearKeyTypeInfo(new);
285 new->file_id = info->file_id;
286 darray_append(info->types, *new);
290 /***====================================================================***/
293 MergeIncludedKeyTypes(KeyTypesInfo *into, KeyTypesInfo *from,
294 enum merge_mode merge)
298 if (from->errorCount > 0) {
299 into->errorCount += from->errorCount;
303 if (into->name == NULL) {
304 into->name = from->name;
308 darray_foreach(type, from->types) {
309 type->merge = (merge == MERGE_DEFAULT ? type->merge : merge);
310 if (!AddKeyType(into, type))
316 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge);
319 HandleIncludeKeyTypes(KeyTypesInfo *info, IncludeStmt *stmt)
321 enum merge_mode merge = MERGE_DEFAULT;
323 KeyTypesInfo included, next_incl;
325 InitKeyTypesInfo(&included, info->keymap, info->file_id);
328 included.name = stmt->stmt;
332 for (; stmt; stmt = stmt->next_incl) {
333 if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_TYPES,
335 info->errorCount += 10;
336 ClearKeyTypesInfo(&included);
340 InitKeyTypesInfo(&next_incl, info->keymap, rtrn->id);
342 HandleKeyTypesFile(&next_incl, rtrn, merge);
344 MergeIncludedKeyTypes(&included, &next_incl, merge);
346 ClearKeyTypesInfo(&next_incl);
350 MergeIncludedKeyTypes(info, &included, merge);
351 ClearKeyTypesInfo(&included);
353 return (info->errorCount == 0);
356 /***====================================================================***/
359 SetModifiers(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
365 log_warn(info->keymap->ctx,
366 "The modifiers field of a key type is not an array; "
367 "Illegal array subscript ignored\n");
369 /* get modifier mask for current type */
370 if (!ExprResolveVModMask(info->keymap, value, &mods)) {
371 log_err(info->keymap->ctx,
372 "Key type mask field must be a modifier mask; "
373 "Key type definition ignored\n");
377 if (type->defined & TYPE_FIELD_MASK) {
378 log_warn(info->keymap->ctx,
379 "Multiple modifier mask definitions for key type %s; "
380 "Using %s, ignoring %s\n",
381 xkb_atom_text(info->keymap->ctx, type->name),
382 TypeMaskTxt(info, type),
383 VModMaskText(info->keymap, mods));
391 /***====================================================================***/
393 static struct xkb_kt_map_entry *
394 FindMatchingMapEntry(KeyTypeInfo *type, xkb_mod_mask_t mods)
396 struct xkb_kt_map_entry *entry;
398 darray_foreach(entry, type->entries)
399 if (entry->mods.mods == mods)
406 AddMapEntry(KeyTypesInfo *info, KeyTypeInfo *type,
407 struct xkb_kt_map_entry *new, bool clobber, bool report)
409 struct xkb_kt_map_entry * old;
411 old = FindMatchingMapEntry(type, new->mods.mods);
413 if (report && old->level != new->level) {
414 log_warn(info->keymap->ctx,
415 "Multiple map entries for %s in %s; "
416 "Using %d, ignoring %d\n",
417 MapEntryTxt(info, new), TypeTxt(info, type),
418 (clobber ? new->level : old->level) + 1,
419 (clobber ? old->level : new->level) + 1);
422 log_vrb(info->keymap->ctx, 10,
423 "Multiple occurences of map[%s]= %d in %s; Ignored\n",
424 MapEntryTxt(info, new), new->level + 1,
425 TypeTxt(info, type));
430 if (new->level >= type->num_levels)
431 type->num_levels = new->level + 1;
432 old->level = new->level;
438 if (new->level >= type->num_levels)
439 type->num_levels = new->level + 1;
441 darray_append(type->entries, *new);
446 SetMapEntry(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
449 struct xkb_kt_map_entry entry;
451 if (arrayNdx == NULL)
452 return ReportTypeShouldBeArray(info, type, "map entry");
454 if (!ExprResolveVModMask(info->keymap, arrayNdx, &entry.mods.mods))
455 return ReportTypeBadType(info, type, "map entry", "modifier mask");
457 if (entry.mods.mods & (~type->mods)) {
458 log_vrb(info->keymap->ctx, 1,
459 "Map entry for unused modifiers in %s; "
460 "Using %s instead of %s\n",
462 VModMaskText(info->keymap, entry.mods.mods & type->mods),
463 MapEntryTxt(info, &entry));
464 entry.mods.mods &= type->mods;
467 if (!ExprResolveLevel(info->keymap->ctx, value, &entry.level)) {
468 log_err(info->keymap->ctx,
469 "Level specifications in a key type must be integer; "
470 "Ignoring malformed level specification\n");
474 entry.preserve.mods = 0;
476 return AddMapEntry(info, type, &entry, true, true);
479 /***====================================================================***/
482 AddPreserve(KeyTypesInfo *info, KeyTypeInfo *type,
483 xkb_mod_mask_t mods, xkb_mod_mask_t preserve_mods)
485 struct xkb_kt_map_entry *entry;
486 struct xkb_kt_map_entry new;
488 darray_foreach(entry, type->entries) {
489 if (entry->mods.mods != mods)
492 /* Map exists without previous preserve (or "None"); override. */
493 if (entry->preserve.mods == 0) {
494 entry->preserve.mods = preserve_mods;
498 /* Map exists with same preserve; do nothing. */
499 if (entry->preserve.mods == preserve_mods) {
500 log_vrb(info->keymap->ctx, 10,
501 "Identical definitions for preserve[%s] in %s; "
503 VModMaskText(info->keymap, mods),
504 TypeTxt(info, type));
508 /* Map exists with different preserve; latter wins. */
509 log_vrb(info->keymap->ctx, 1,
510 "Multiple definitions for preserve[%s] in %s; "
511 "Using %s, ignoring %s\n",
512 VModMaskText(info->keymap, mods),
514 VModMaskText(info->keymap, preserve_mods),
515 VModMaskText(info->keymap, entry->preserve.mods));
517 entry->preserve.mods = preserve_mods;
522 * Map does not exist, i.e. preserve[] came before map[].
523 * Create a map with the specified mask mapping to Level1. The level
524 * may be overriden later with an explicit map[] statement.
527 new.mods.mods = mods;
528 new.preserve.mods = preserve_mods;
529 darray_append(type->entries, new);
534 SetPreserve(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
537 xkb_mod_mask_t mods, preserve_mods;
539 if (arrayNdx == NULL)
540 return ReportTypeShouldBeArray(info, type, "preserve entry");
542 if (!ExprResolveVModMask(info->keymap, arrayNdx, &mods))
543 return ReportTypeBadType(info, type, "preserve entry",
546 if (mods & ~type->mods) {
547 const char *before, *after;
549 before = VModMaskText(info->keymap, mods);
551 after = VModMaskText(info->keymap, mods);
553 log_vrb(info->keymap->ctx, 1,
554 "Preserve for modifiers not used by the %s type; "
555 "Index %s converted to %s\n",
556 TypeTxt(info, type), before, after);
559 if (!ExprResolveVModMask(info->keymap, value, &preserve_mods)) {
560 log_err(info->keymap->ctx,
561 "Preserve value in a key type is not a modifier mask; "
562 "Ignoring preserve[%s] in type %s\n",
563 VModMaskText(info->keymap, mods),
564 TypeTxt(info, type));
568 if (preserve_mods & ~mods) {
569 const char *before, *after;
571 before = VModMaskText(info->keymap, preserve_mods);
572 preserve_mods &= mods;
573 after = VModMaskText(info->keymap, preserve_mods);
575 log_vrb(info->keymap->ctx, 1,
576 "Illegal value for preserve[%s] in type %s; "
577 "Converted %s to %s\n",
578 VModMaskText(info->keymap, mods),
579 TypeTxt(info, type), before, after);
582 return AddPreserve(info, type, mods, preserve_mods);
585 /***====================================================================***/
588 AddLevelName(KeyTypesInfo *info, KeyTypeInfo *type,
589 xkb_level_index_t level, xkb_atom_t name, bool clobber)
592 if (level >= darray_size(type->level_names)) {
593 darray_resize0(type->level_names, level + 1);
597 /* Same level, same name. */
598 if (darray_item(type->level_names, level) == name) {
599 log_vrb(info->keymap->ctx, 10,
600 "Duplicate names for level %d of key type %s; Ignored\n",
601 level + 1, TypeTxt(info, type));
605 /* Same level, different name. */
606 if (darray_item(type->level_names, level) != XKB_ATOM_NONE) {
607 const char *old, *new;
608 old = xkb_atom_text(info->keymap->ctx,
609 darray_item(type->level_names, level));
610 new = xkb_atom_text(info->keymap->ctx, name);
611 log_vrb(info->keymap->ctx, 1,
612 "Multiple names for level %d of key type %s; "
613 "Using %s, ignoring %s\n",
614 level + 1, TypeTxt(info, type),
615 (clobber ? new : old), (clobber ? old : new));
621 /* XXX: What about different level, same name? */
624 darray_item(type->level_names, level) = name;
629 SetLevelName(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
632 xkb_level_index_t level;
633 xkb_atom_t level_name;
634 struct xkb_context *ctx = info->keymap->ctx;
636 if (arrayNdx == NULL)
637 return ReportTypeShouldBeArray(info, type, "level name");
639 if (!ExprResolveLevel(ctx, arrayNdx, &level))
640 return ReportTypeBadType(info, type, "level name", "integer");
642 if (!ExprResolveString(ctx, value, &level_name)) {
643 log_err(info->keymap->ctx,
644 "Non-string name for level %d in key type %s; "
645 "Ignoring illegal level name definition\n",
646 level + 1, xkb_atom_text(ctx, type->name));
650 return AddLevelName(info, type, level, level_name, true);
653 /***====================================================================***/
656 SetKeyTypeField(KeyTypesInfo *info, KeyTypeInfo *type,
657 const char *field, ExprDef *arrayNdx, ExprDef *value)
660 enum type_field type_field = 0;
662 if (istreq(field, "modifiers")) {
663 type_field = TYPE_FIELD_MASK;
664 ok = SetModifiers(info, type, arrayNdx, value);
666 else if (istreq(field, "map")) {
667 type_field = TYPE_FIELD_MAP;
668 ok = SetMapEntry(info, type, arrayNdx, value);
670 else if (istreq(field, "preserve")) {
671 type_field = TYPE_FIELD_PRESERVE;
672 ok = SetPreserve(info, type, arrayNdx, value);
674 else if (istreq(field, "levelname") || istreq(field, "level_name")) {
675 type_field = TYPE_FIELD_LEVEL_NAME;
676 ok = SetLevelName(info, type, arrayNdx, value);
678 log_err(info->keymap->ctx,
679 "Unknown field %s in key type %s; Definition ignored\n",
680 field, TypeTxt(info, type));
683 type->defined |= type_field;
688 HandleKeyTypeBody(KeyTypesInfo *info, VarDef *def, KeyTypeInfo *type)
691 const char *elem, *field;
694 for (; def; def = (VarDef *) def->common.next) {
695 ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
700 if (elem && istreq(elem, "type")) {
701 log_err(info->keymap->ctx,
702 "Support for changing the default type has been removed; "
703 "Statement ignored\n");
707 ok = SetKeyTypeField(info, type, field, arrayNdx, def->value);
714 HandleKeyTypeDef(KeyTypesInfo *info, KeyTypeDef *def, enum merge_mode merge)
718 .file_id = info->file_id,
719 .merge = (def->merge == MERGE_DEFAULT ? merge : def->merge),
723 .entries = darray_new(),
724 .level_names = darray_new(),
727 if (!HandleKeyTypeBody(info, def->body, &type)) {
732 if (!AddKeyType(info, &type)) {
741 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge)
747 info->name = strdup_safe(file->name);
749 for (stmt = file->defs; stmt; stmt = stmt->next) {
750 switch (stmt->type) {
752 ok = HandleIncludeKeyTypes(info, (IncludeStmt *) stmt);
754 case STMT_TYPE: /* e.g. type "ONE_LEVEL" */
755 ok = HandleKeyTypeDef(info, (KeyTypeDef *) stmt, merge);
758 log_err(info->keymap->ctx,
759 "Support for changing the default type has been removed; "
760 "Statement ignored\n");
763 case STMT_VMOD: /* virtual_modifiers NumLock, ... */
764 ok = HandleVModDef((VModDef *) stmt, info->keymap, merge,
768 log_err(info->keymap->ctx,
769 "Key type files may not include other declarations; "
770 "Ignoring %s\n", stmt_type_to_string(stmt->type));
778 if (info->errorCount > 10) {
779 log_err(info->keymap->ctx,
780 "Abandoning keytypes file \"%s\"\n", file->topName);
787 CopyDefToKeyType(KeyTypeInfo *def, struct xkb_key_type *type)
789 type->mods.mods = def->mods;
790 type->num_levels = def->num_levels;
791 type->map = darray_mem(def->entries, 0);
792 type->num_entries = darray_size(def->entries);
793 darray_init(def->entries);
794 type->name = def->name;
795 type->level_names = darray_mem(def->level_names, 0);
796 darray_init(def->level_names);
800 CopyKeyTypesToKeymap(struct xkb_keymap *keymap, KeyTypesInfo *info)
803 unsigned int num_types;
805 num_types = darray_size(info->types) ? darray_size(info->types) : 1;
806 keymap->types = calloc(num_types, sizeof(*keymap->types));
810 keymap->num_types = num_types;
813 * If no types were specified, a default unnamed one-level type is
816 if (darray_empty(info->types)) {
818 .name = xkb_atom_intern(keymap->ctx, "default"),
821 .entries = darray_new(),
822 .level_names = darray_new(),
825 CopyDefToKeyType(&dflt, &keymap->types[0]);
827 for (i = 0; i < num_types; i++)
828 CopyDefToKeyType(&darray_item(info->types, i), &keymap->types[i]);
831 keymap->types_section_name = strdup_safe(info->name);
837 CompileKeyTypes(XkbFile *file, struct xkb_keymap *keymap,
838 enum merge_mode merge)
842 InitKeyTypesInfo(&info, keymap, file->id);
844 HandleKeyTypesFile(&info, file, merge);
845 if (info.errorCount != 0)
848 if (!CopyKeyTypesToKeymap(keymap, &info))
851 ClearKeyTypesInfo(&info);
855 ClearKeyTypesInfo(&info);