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"
31 typedef struct _PreserveInfo {
33 short matchingMapIndex;
34 unsigned char indexMods;
35 unsigned char preMods;
36 unsigned short indexVMods;
37 unsigned short preVMods;
40 #define _KT_Name (1 << 0)
41 #define _KT_Mask (1 << 1)
42 #define _KT_Map (1 << 2)
43 #define _KT_Preserve (1 << 3)
44 #define _KT_LevelNames (1 << 4)
46 typedef struct _KeyTypeInfo {
47 unsigned short defined;
49 enum merge_mode merge;
56 darray(struct xkb_kt_map_entry) entries;
57 struct list preserves;
58 darray(xkb_atom_t) lvlNames;
61 typedef struct _KeyTypesInfo {
70 struct xkb_keymap *keymap;
72 xkb_atom_t tok_ONE_LEVEL;
73 xkb_atom_t tok_TWO_LEVEL;
74 xkb_atom_t tok_ALPHABETIC;
75 xkb_atom_t tok_KEYPAD;
78 /***====================================================================***/
80 static inline const char *
81 MapEntryTxt(KeyTypesInfo *info, struct xkb_kt_map_entry *entry)
83 return XkbcVModMaskText(info->keymap, entry->mods.real_mods,
87 static inline const char *
88 PreserveIndexTxt(KeyTypesInfo *info, PreserveInfo *pi)
90 return XkbcVModMaskText(info->keymap, pi->indexMods, pi->indexVMods);
93 static inline const char *
94 PreserveTxt(KeyTypesInfo *info, PreserveInfo *pi)
96 return XkbcVModMaskText(info->keymap, pi->preMods, pi->preVMods);
99 static inline const char *
100 TypeTxt(KeyTypesInfo *info, KeyTypeInfo *type)
102 return xkb_atom_text(info->keymap->ctx, type->name);
105 static inline const char *
106 TypeMaskTxt(KeyTypesInfo *info, KeyTypeInfo *type)
108 return XkbcVModMaskText(info->keymap, type->mask, type->vmask);
112 ReportTypeShouldBeArray(KeyTypesInfo *info, KeyTypeInfo *type,
115 return ReportShouldBeArray(info->keymap, "key type", field,
116 TypeTxt(info, type));
120 ReportTypeBadType(KeyTypesInfo *info, KeyTypeInfo *type,
121 const char *field, const char *wanted)
123 return ReportBadType(info->keymap, "key type", field, TypeTxt(info, type),
128 ReportTypeBadWidth(KeyTypesInfo *info, const char *type, int has, int needs)
130 log_err(info->keymap->ctx,
131 "Key type \"%s\" has %d levels, must have %d; "
132 "Illegal type definition ignored\n",
137 /***====================================================================***/
140 InitKeyTypesInfo(KeyTypesInfo *info, struct xkb_keymap *keymap,
141 KeyTypesInfo *from, unsigned file_id)
143 PreserveInfo *old, *new;
145 info->tok_ONE_LEVEL = xkb_atom_intern(keymap->ctx, "ONE_LEVEL");
146 info->tok_TWO_LEVEL = xkb_atom_intern(keymap->ctx, "TWO_LEVEL");
147 info->tok_ALPHABETIC = xkb_atom_intern(keymap->ctx, "ALPHABETIC");
148 info->tok_KEYPAD = xkb_atom_intern(keymap->ctx, "KEYPAD");
149 info->name = strdup("default");
150 info->errorCount = 0;
151 info->stdPresent = 0;
153 list_init(&info->types);
154 info->file_id = file_id;
155 info->dflt.defined = 0;
156 info->dflt.file_id = file_id;
157 info->dflt.merge = MERGE_OVERRIDE;
158 info->dflt.name = XKB_ATOM_NONE;
160 info->dflt.vmask = 0;
161 info->dflt.numLevels = 1;
162 darray_init(info->dflt.entries);
163 darray_init(info->dflt.lvlNames);
164 list_init(&info->dflt.preserves);
165 InitVModInfo(&info->vmods, keymap);
166 info->keymap = keymap;
171 info->dflt = from->dflt;
173 darray_copy(info->dflt.entries, from->dflt.entries);
174 darray_copy(info->dflt.lvlNames, from->dflt.lvlNames);
176 list_init(&info->dflt.preserves);
177 list_foreach(old, &from->dflt.preserves, entry) {
178 new = malloc(sizeof(*new));
183 list_append(&new->entry, &info->dflt.preserves);
188 FreeKeyTypeInfo(KeyTypeInfo * type)
190 PreserveInfo *pi, *next_pi;
191 darray_free(type->entries);
192 darray_free(type->lvlNames);
193 list_foreach_safe(pi, next_pi, &type->preserves, entry)
195 list_init(&type->preserves);
199 FreeKeyTypesInfo(KeyTypesInfo * info)
201 KeyTypeInfo *type, *next_type;
204 list_foreach_safe(type, next_type, &info->types, entry) {
205 FreeKeyTypeInfo(type);
208 FreeKeyTypeInfo(&info->dflt);
212 NextKeyType(KeyTypesInfo * info)
216 type = calloc(1, sizeof(*type));
220 list_init(&type->preserves);
221 type->file_id = info->file_id;
223 list_append(&type->entry, &info->types);
229 FindMatchingKeyType(KeyTypesInfo * info, KeyTypeInfo * new)
233 list_foreach(old, &info->types, entry)
234 if (old->name == new->name)
241 AddKeyType(KeyTypesInfo *info, KeyTypeInfo *new)
244 struct list type_entry, preserves_entry;
245 int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
247 if (new->name == info->tok_ONE_LEVEL) {
248 if (new->numLevels > 1)
249 return ReportTypeBadWidth(info, "ONE_LEVEL", new->numLevels, 1);
250 info->stdPresent |= XkbOneLevelMask;
252 else if (new->name == info->tok_TWO_LEVEL) {
253 if (new->numLevels > 2)
254 return ReportTypeBadWidth(info, "TWO_LEVEL", new->numLevels, 2);
255 else if (new->numLevels < 2)
257 info->stdPresent |= XkbTwoLevelMask;
259 else if (new->name == info->tok_ALPHABETIC) {
260 if (new->numLevels > 2)
261 return ReportTypeBadWidth(info, "ALPHABETIC", new->numLevels, 2);
262 else if (new->numLevels < 2)
264 info->stdPresent |= XkbAlphabeticMask;
266 else if (new->name == info->tok_KEYPAD) {
267 if (new->numLevels > 2)
268 return ReportTypeBadWidth(info, "KEYPAD", new->numLevels, 2);
269 else if (new->numLevels < 2)
271 info->stdPresent |= XkbKeypadMask;
274 old = FindMatchingKeyType(info, new);
276 if (new->merge == MERGE_REPLACE || new->merge == MERGE_OVERRIDE) {
277 if ((old->file_id == new->file_id && verbosity > 0) ||
279 log_warn(info->keymap->ctx,
280 "Multiple definitions of the %s key type; "
281 "Earlier definition ignored\n",
282 xkb_atom_text(info->keymap->ctx, new->name));
285 type_entry = old->entry;
286 FreeKeyTypeInfo(old);
288 old->entry = type_entry;
289 darray_init(new->entries);
290 darray_init(new->lvlNames);
291 list_init(&new->preserves);
295 if (old->file_id == new->file_id)
296 log_lvl(info->keymap->ctx, 4,
297 "Multiple definitions of the %s key type; "
298 "Later definition ignored\n",
299 xkb_atom_text(info->keymap->ctx, new->name));
301 FreeKeyTypeInfo(new);
305 old = NextKeyType(info);
308 list_replace(&new->preserves, &old->preserves);
309 type_entry = old->entry;
310 preserves_entry = old->preserves;
312 old->preserves = preserves_entry;
313 old->entry = type_entry;
314 darray_init(new->entries);
315 darray_init(new->lvlNames);
316 list_init(&new->preserves);
320 /***====================================================================***/
323 MergeIncludedKeyTypes(KeyTypesInfo *into, KeyTypesInfo *from,
324 enum merge_mode merge)
326 KeyTypeInfo *type, *next_type;
328 if (from->errorCount > 0) {
329 into->errorCount += from->errorCount;
333 if (into->name == NULL) {
334 into->name = from->name;
338 list_foreach_safe(type, next_type, &from->types, entry) {
339 type->merge = (merge == MERGE_DEFAULT ? type->merge : merge);
340 if (!AddKeyType(into, type))
344 into->stdPresent |= from->stdPresent;
348 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge);
351 HandleIncludeKeyTypes(KeyTypesInfo *info, IncludeStmt *stmt)
353 enum merge_mode merge = MERGE_DEFAULT;
355 KeyTypesInfo included, next_incl;
357 InitKeyTypesInfo(&included, info->keymap, info, info->file_id);
360 included.name = stmt->stmt;
364 for (; stmt; stmt = stmt->next) {
365 if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_TYPES,
367 info->errorCount += 10;
368 FreeKeyTypesInfo(&included);
372 InitKeyTypesInfo(&next_incl, info->keymap, &included, rtrn->id);
373 next_incl.dflt.merge = merge;
375 HandleKeyTypesFile(&next_incl, rtrn, merge);
377 MergeIncludedKeyTypes(&included, &next_incl, merge);
379 FreeKeyTypesInfo(&next_incl);
383 MergeIncludedKeyTypes(info, &included, merge);
384 FreeKeyTypesInfo(&included);
386 return (info->errorCount == 0);
389 /***====================================================================***/
391 static struct xkb_kt_map_entry *
392 FindMatchingMapEntry(KeyTypeInfo * type, unsigned mask, unsigned vmask)
394 struct xkb_kt_map_entry *entry;
396 darray_foreach(entry, type->entries)
397 if (entry->mods.real_mods == mask && entry->mods.vmods == vmask)
404 DeleteLevel1MapEntries(KeyTypeInfo * type)
408 /* TODO: Be just a bit more clever here. */
409 for (i = 0; i < darray_size(type->entries); i++) {
410 if (darray_item(type->entries, i).level == 0) {
411 for (n = i; n < darray_size(type->entries) - 1; n++)
412 darray_item(type->entries, n) =
413 darray_item(type->entries, n + 1);
414 (void) darray_pop(type->entries);
419 static struct xkb_kt_map_entry *
420 NextMapEntry(KeyTypesInfo *info, KeyTypeInfo * type)
422 darray_resize0(type->entries, darray_size(type->entries) + 1);
423 return &darray_item(type->entries, darray_size(type->entries) - 1);
427 AddPreserve(KeyTypesInfo *info, KeyTypeInfo *type,
428 PreserveInfo *new, bool clobber, bool report)
432 list_foreach(old, &type->preserves, entry) {
433 if (old->indexMods != new->indexMods ||
434 old->indexVMods != new->indexVMods)
437 if (old->preMods == new->preMods && old->preVMods == new->preVMods) {
438 log_lvl(info->keymap->ctx, 10,
439 "Identical definitions for preserve[%s] in %s; "
441 PreserveIndexTxt(info, old), TypeTxt(info, type));
446 log_lvl(info->keymap->ctx, 1,
447 "Multiple definitions for preserve[%s] in %s; "
448 "Using %s, ignoring %s\n",
449 PreserveIndexTxt(info, old), TypeTxt(info, type),
450 PreserveTxt(info, clobber ? new : old),
451 PreserveTxt(info, clobber ? old : new));
454 old->preMods = new->preMods;
455 old->preVMods = new->preVMods;
461 old = malloc(sizeof(*old));
463 log_wsgo(info->keymap->ctx,
464 "Couldn't allocate preserve in %s; Preserve[%s] lost\n",
465 TypeTxt(info, type), PreserveIndexTxt(info, new));
470 old->matchingMapIndex = -1;
471 list_append(&old->entry, &type->preserves);
477 * Add a new KTMapEntry to the given key type. If an entry with the same mods
478 * already exists, the level is updated (if clobber is TRUE). Otherwise, a new
481 * @param clobber Overwrite existing entry.
482 * @param report true if a warning is to be printed on.
485 AddMapEntry(KeyTypesInfo *info, KeyTypeInfo *type,
486 struct xkb_kt_map_entry *new, bool clobber, bool report)
488 struct xkb_kt_map_entry * old;
490 if ((old = FindMatchingMapEntry(type, new->mods.real_mods,
492 if (report && (old->level != new->level)) {
493 unsigned use, ignore;
495 use = new->level + 1;
496 ignore = old->level + 1;
499 use = old->level + 1;
500 ignore = new->level + 1;
502 log_warn(info->keymap->ctx,
503 "Multiple map entries for %s in %s; "
504 "Using %d, ignoring %d\n",
505 MapEntryTxt(info, new), TypeTxt(info, type), use,
509 log_lvl(info->keymap->ctx, 10,
510 "Multiple occurences of map[%s]= %d in %s; Ignored\n",
511 MapEntryTxt(info, new), new->level + 1,
512 TypeTxt(info, type));
516 old->level = new->level;
519 if ((old = NextMapEntry(info, type)) == NULL)
520 return false; /* allocation failure, already reported */
521 if (new->level >= type->numLevels)
522 type->numLevels = new->level + 1;
523 old->mods.mask = new->mods.real_mods;
524 old->mods.real_mods = new->mods.real_mods;
525 old->mods.vmods = new->mods.vmods;
526 old->level = new->level;
531 SetMapEntry(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
535 struct xkb_kt_map_entry entry;
537 if (arrayNdx == NULL)
538 return ReportTypeShouldBeArray(info, type, "map entry");
540 if (!ExprResolveVModMask(info->keymap, arrayNdx, &rtrn))
541 return ReportTypeBadType(info, type, "map entry", "modifier mask");
543 entry.mods.real_mods = rtrn.uval & 0xff; /* modifiers < 512 */
544 entry.mods.vmods = (rtrn.uval >> 8) & 0xffff; /* modifiers > 512 */
546 if ((entry.mods.real_mods & (~type->mask)) ||
547 ((entry.mods.vmods & (~type->vmask)) != 0)) {
548 log_lvl(info->keymap->ctx, 1,
549 "Map entry for unused modifiers in %s; "
550 "Using %s instead of %s\n",
552 XkbcVModMaskText(info->keymap,
553 entry.mods.real_mods & type->mask,
554 entry.mods.vmods & type->vmask),
555 MapEntryTxt(info, &entry));
556 entry.mods.real_mods &= type->mask;
557 entry.mods.vmods &= type->vmask;
560 if (!ExprResolveLevel(info->keymap->ctx, value, &rtrn)) {
561 log_err(info->keymap->ctx,
562 "Level specifications in a key type must be integer; "
563 "Ignoring malformed level specification\n");
567 entry.level = rtrn.ival - 1;
568 return AddMapEntry(info, type, &entry, true, true);
572 SetPreserve(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
578 if (arrayNdx == NULL)
579 return ReportTypeShouldBeArray(info, type, "preserve entry");
581 if (!ExprResolveVModMask(info->keymap, arrayNdx, &rtrn))
582 return ReportTypeBadType(info, type, "preserve entry",
585 new.indexMods = rtrn.uval & 0xff;
586 new.indexVMods = (rtrn.uval >> 8) & 0xffff;
588 if ((new.indexMods & (~type->mask)) ||
589 (new.indexVMods & (~type->vmask))) {
590 const char *before = PreserveIndexTxt(info, &new);
592 new.indexMods &= type->mask;
593 new.indexVMods &= type->vmask;
595 log_lvl(info->keymap->ctx, 1,
596 "Preserve for modifiers not used by the %s type; "
597 "Index %s converted to %s\n",
598 TypeTxt(info, type), before,
599 PreserveIndexTxt(info, &new));
602 if (!ExprResolveVModMask(info->keymap, value, &rtrn)) {
603 log_err(info->keymap->ctx,
604 "Preserve value in a key type is not a modifier mask; "
605 "Ignoring preserve[%s] in type %s\n",
606 PreserveIndexTxt(info, &new), TypeTxt(info, type));
610 new.preMods = rtrn.uval & 0xff;
611 new.preVMods = (rtrn.uval >> 16) & 0xffff;
613 if ((new.preMods & (~new.indexMods)) ||
614 (new.preVMods & (~new.indexVMods))) {
615 const char *before = PreserveIndexTxt(info, &new);
617 new.preMods &= new.indexMods;
618 new.preVMods &= new.indexVMods;
620 log_lvl(info->keymap->ctx, 1,
621 "Illegal value for preserve[%s] in type %s; "
622 "Converted %s to %s\n",
623 PreserveTxt(info, &new), TypeTxt(info, type),
624 before, PreserveIndexTxt(info, &new));
627 return AddPreserve(info, type, &new, true, true);
630 /***====================================================================***/
633 AddLevelName(KeyTypesInfo *info, KeyTypeInfo *type,
634 unsigned level, xkb_atom_t name, bool clobber)
636 if (level >= darray_size(type->lvlNames))
637 darray_resize0(type->lvlNames, level + 1);
639 if (darray_item(type->lvlNames, level) == name) {
640 log_lvl(info->keymap->ctx, 10,
641 "Duplicate names for level %d of key type %s; Ignored\n",
642 level + 1, TypeTxt(info, type));
645 else if (darray_item(type->lvlNames, level) != XKB_ATOM_NONE) {
646 if (xkb_get_log_verbosity(info->keymap->ctx) > 0) {
647 const char *old, *new;
648 old = xkb_atom_text(info->keymap->ctx,
649 darray_item(type->lvlNames, level));
650 new = xkb_atom_text(info->keymap->ctx, name);
651 log_lvl(info->keymap->ctx, 1,
652 "Multiple names for level %d of key type %s; "
653 "Using %s, ignoring %s\n",
654 level + 1, TypeTxt(info, type),
655 (clobber ? new : old), (clobber ? old : new));
662 darray_item(type->lvlNames, level) = name;
667 SetLevelName(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
672 xkb_atom_t level_name;
673 struct xkb_context *ctx = info->keymap->ctx;
675 if (arrayNdx == NULL)
676 return ReportTypeShouldBeArray(info, type, "level name");
677 if (!ExprResolveLevel(ctx, arrayNdx, &rtrn))
678 return ReportTypeBadType(info, type, "level name", "integer");
679 level = rtrn.ival - 1;
680 if (!ExprResolveString(ctx, value, &rtrn)) {
681 log_err(info->keymap->ctx,
682 "Non-string name for level %d in key type %s; "
683 "Ignoring illegal level name definition\n",
684 level + 1, xkb_atom_text(ctx, type->name));
687 level_name = xkb_atom_intern(ctx, rtrn.str);
688 return AddLevelName(info, type, level, level_name, true);
691 /***====================================================================***/
694 * Parses the fields in a type "..." { } description.
696 * @param field The field to parse (e.g. modifiers, map, level_name)
699 SetKeyTypeField(KeyTypesInfo *info, KeyTypeInfo *type,
700 const char *field, ExprDef *arrayNdx, ExprDef *value)
704 if (strcasecmp(field, "modifiers") == 0) {
705 unsigned mods, vmods;
706 if (arrayNdx != NULL)
707 log_warn(info->keymap->ctx,
708 "The modifiers field of a key type is not an array; "
709 "Illegal array subscript ignored\n");
710 /* get modifier mask for current type */
711 if (!ExprResolveVModMask(info->keymap, value, &tmp)) {
712 log_err(info->keymap->ctx,
713 "Key type mask field must be a modifier mask; "
714 "Key type definition ignored\n");
717 mods = tmp.uval & 0xff; /* core mods */
718 vmods = (tmp.uval >> 8) & 0xffff; /* xkb virtual mods */
719 if (type->defined & _KT_Mask) {
720 log_warn(info->keymap->ctx,
721 "Multiple modifier mask definitions for key type %s; "
722 "Using %s, ignoring %s\n",
723 xkb_atom_text(info->keymap->ctx, type->name),
724 TypeMaskTxt(info, type),
725 XkbcVModMaskText(info->keymap, mods, vmods));
730 type->defined |= _KT_Mask;
733 else if (strcasecmp(field, "map") == 0) {
734 type->defined |= _KT_Map;
735 return SetMapEntry(info, type, arrayNdx, value);
737 else if (strcasecmp(field, "preserve") == 0) {
738 type->defined |= _KT_Preserve;
739 return SetPreserve(info, type, arrayNdx, value);
741 else if ((strcasecmp(field, "levelname") == 0) ||
742 (strcasecmp(field, "level_name") == 0)) {
743 type->defined |= _KT_LevelNames;
744 return SetLevelName(info, type, arrayNdx, value);
747 log_err(info->keymap->ctx,
748 "Unknown field %s in key type %s; Definition ignored\n",
749 field, TypeTxt(info, type));
755 HandleKeyTypeVar(KeyTypesInfo *info, VarDef *stmt)
757 ExprResult elem, field;
760 if (!ExprResolveLhs(info->keymap, stmt->name, &elem, &field, &arrayNdx))
761 return false; /* internal error, already reported */
762 if (elem.str && (strcasecmp(elem.str, "type") == 0))
763 return SetKeyTypeField(info, &info->dflt, field.str, arrayNdx,
765 if (elem.str != NULL) {
766 log_err(info->keymap->ctx,
767 "Default for unknown element %s; "
768 "Value for field %s ignored\n",
769 field.str, elem.str);
771 else if (field.str != NULL) {
772 log_err(info->keymap->ctx,
773 "Default defined for unknown field %s; Ignored\n", field.str);
779 HandleKeyTypeBody(KeyTypesInfo *info, VarDef *def, KeyTypeInfo *type)
782 ExprResult tmp, field;
785 for (; def != NULL; def = (VarDef *) def->common.next) {
786 if ((def->name) && (def->name->type == ExprFieldRef)) {
787 ok = HandleKeyTypeVar(info, def);
790 ok = ExprResolveLhs(info->keymap, def->name, &tmp, &field, &arrayNdx);
792 ok = SetKeyTypeField(info, type, field.str, arrayNdx,
800 * Process a type "XYZ" { } specification in the xkb_types section.
804 HandleKeyTypeDef(KeyTypesInfo *info, KeyTypeDef *def, enum merge_mode merge)
808 struct xkb_kt_map_entry *entry;
809 PreserveInfo *pi, *pi_next;
811 if (def->merge != MERGE_DEFAULT)
815 type.file_id = info->file_id;
817 type.name = def->name;
818 type.mask = info->dflt.mask;
819 type.vmask = info->dflt.vmask;
821 darray_init(type.entries);
822 darray_init(type.lvlNames);
823 list_init(&type.preserves);
825 /* Parse the actual content. */
826 if (!HandleKeyTypeBody(info, def->body, &type)) {
831 /* now copy any appropriate map, preserve or level names from the */
833 darray_foreach(entry, info->dflt.entries) {
834 if ((entry->mods.real_mods & type.mask) == entry->mods.real_mods &&
835 (entry->mods.vmods & type.vmask) == entry->mods.vmods)
836 AddMapEntry(info, &type, entry, false, false);
839 list_foreach_safe(pi, pi_next, &info->dflt.preserves, entry) {
840 if ((pi->indexMods & type.mask) == pi->indexMods &&
841 (pi->indexVMods & type.vmask) == pi->indexVMods)
842 AddPreserve(info, &type, pi, false, false);
845 for (i = 0; i < darray_size(info->dflt.lvlNames); i++) {
846 if (i < type.numLevels &&
847 darray_item(info->dflt.lvlNames, i) != XKB_ATOM_NONE) {
848 AddLevelName(info, &type, i,
849 darray_item(info->dflt.lvlNames, i), false);
853 /* Now add the new keytype to the info struct */
854 if (!AddKeyType(info, &type)) {
862 * Process an xkb_types section.
864 * @param file The parsed xkb_types section.
865 * @param merge Merge Strategy (e.g. MERGE_OVERRIDE)
866 * @param info Pointer to memory where the outcome will be stored.
869 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge)
874 info->name = uDupString(file->name);
878 switch (stmt->stmtType) {
880 if (!HandleIncludeKeyTypes(info, (IncludeStmt *) stmt))
883 case StmtKeyTypeDef: /* e.g. type "ONE_LEVEL" */
884 if (!HandleKeyTypeDef(info, (KeyTypeDef *) stmt, merge))
888 if (!HandleKeyTypeVar(info, (VarDef *) stmt))
891 case StmtVModDef: /* virtual_modifiers NumLock, ... */
892 if (!HandleVModDef((VModDef *) stmt, info->keymap, merge,
896 case StmtKeyAliasDef:
897 log_err(info->keymap->ctx,
898 "Key type files may not include other declarations; "
899 "Ignoring definition of key alias\n");
903 log_err(info->keymap->ctx,
904 "Key type files may not include other declarations; "
905 "Ignoring definition of key name\n");
909 log_err(info->keymap->ctx,
910 "Key type files may not include other declarations; "
911 "Ignoring definition of symbol interpretation\n");
915 log_wsgo(info->keymap->ctx,
916 "Unexpected statement type %d in HandleKeyTypesFile\n",
921 if (info->errorCount > 10) {
922 log_err(info->keymap->ctx,
923 "Abandoning keytypes file \"%s\"\n", file->topName);
930 ComputeEffectiveMap(struct xkb_keymap *keymap, struct xkb_key_type *type)
933 struct xkb_kt_map_entry *entry = NULL;
935 if (type->mods.vmods != 0) {
936 tmp = VModsToReal(keymap, type->mods.vmods);
937 type->mods.mask = tmp | type->mods.real_mods;
938 darray_foreach(entry, type->map) {
940 if (entry->mods.vmods != 0) {
941 tmp = VModsToReal(keymap, entry->mods.vmods);
946 (entry->mods.real_mods | tmp) & type->mods.mask;
950 type->mods.mask = type->mods.real_mods;
956 CopyDefToKeyType(KeyTypesInfo *info, KeyTypeInfo *def,
957 struct xkb_key_type *type)
961 struct xkb_keymap *keymap = info->keymap;
963 list_foreach(pre, &def->preserves, entry) {
964 struct xkb_kt_map_entry * match;
965 struct xkb_kt_map_entry tmp;
966 tmp.mods.real_mods = pre->indexMods;
967 tmp.mods.vmods = pre->indexVMods;
969 AddMapEntry(info, def, &tmp, false, false);
970 match = FindMatchingMapEntry(def, pre->indexMods, pre->indexVMods);
972 log_wsgo(info->keymap->ctx,
973 "Couldn't find matching entry for preserve; Aborting\n");
976 pre->matchingMapIndex = match - &darray_item(def->entries, 0);
978 type->mods.real_mods = def->mask;
979 type->mods.vmods = def->vmask;
980 type->num_levels = def->numLevels;
981 memcpy(&type->map, &def->entries, sizeof(def->entries));
982 if (!list_empty(&def->preserves)) {
983 type->preserve = calloc(darray_size(type->map),
984 sizeof(*type->preserve));
985 if (!type->preserve) {
986 log_warn(info->keymap->ctx,
987 "Couldn't allocate preserve array in CopyDefToKeyType; "
988 "Preserve setting for type %s lost\n",
989 xkb_atom_text(keymap->ctx, def->name));
992 list_foreach(pre, &def->preserves, entry) {
993 int ndx = pre->matchingMapIndex;
994 type->preserve[ndx].mask = pre->preMods;
995 type->preserve[ndx].real_mods = pre->preMods;
996 type->preserve[ndx].vmods = pre->preVMods;
1001 type->preserve = NULL;
1002 type->name = xkb_atom_text(keymap->ctx, def->name);
1004 if (!darray_empty(def->lvlNames)) {
1005 type->level_names = calloc(darray_size(def->lvlNames),
1006 sizeof(*type->level_names));
1008 /* assert def->szNames<=def->numLevels */
1009 for (i = 0; i < darray_size(def->lvlNames); i++)
1010 type->level_names[i] =
1011 xkb_atom_text(keymap->ctx, darray_item(def->lvlNames, i));
1014 type->level_names = NULL;
1017 darray_init(def->entries);
1018 return ComputeEffectiveMap(keymap, type);
1021 static struct xkb_kt_map_entry map2Level[] = {
1024 .mods = { .mask = 1, .vmods = ShiftMask, .real_mods = 0 }
1028 static struct xkb_kt_map_entry mapAlpha[] = {
1031 .mods = { .mask = 1, .vmods = ShiftMask, .real_mods = 0 }
1035 .mods = { .mask = 0, .vmods = LockMask, .real_mods = 0 }
1039 static struct xkb_mods preAlpha[] = {
1040 { .mask = 0, .vmods = 0, .real_mods = 0 },
1041 { .mask = LockMask, .vmods = LockMask, .real_mods = 0 }
1044 static struct xkb_kt_map_entry mapKeypad[] = {
1047 .mods = { .mask = 1, .vmods = ShiftMask, .real_mods = 0 }
1051 .mods = { .mask = 1, .vmods = 0, .real_mods = 0 }
1055 static const struct xkb_key_type canonicalTypes[XkbNumRequiredTypes] = {
1056 [XkbOneLevelIndex] = {
1057 .mods = { .mask = 0, .vmods = 0, .real_mods = 0 },
1063 [XkbTwoLevelIndex] = {
1064 .mods = { .mask = ShiftMask, .vmods = ShiftMask, .real_mods = 0 },
1066 .map = darray_lit(map2Level),
1071 [XkbAlphabeticIndex] = {
1073 .mask = ShiftMask | LockMask,
1074 .vmods = ShiftMask | LockMask,
1078 .map = darray_lit(mapAlpha),
1079 .preserve = preAlpha,
1083 [XkbKeypadIndex] = {
1084 .mods = { .mask = ShiftMask, .vmods = ShiftMask, .real_mods = 0 },
1086 .map = darray_lit(mapKeypad),
1094 CopyKeyType(const struct xkb_key_type *from, struct xkb_key_type *into)
1098 darray_free(into->map);
1099 free(into->preserve);
1100 free(into->level_names);
1103 darray_init(into->map);
1105 darray_copy(into->map, from->map);
1107 if (from->preserve && !darray_empty(into->map)) {
1108 into->preserve = calloc(darray_size(into->map),
1109 sizeof(*into->preserve));
1110 if (!into->preserve)
1112 memcpy(into->preserve, from->preserve,
1113 darray_size(into->map) * sizeof(*into->preserve));
1116 if (from->level_names && into->num_levels > 0) {
1117 into->level_names = calloc(into->num_levels,
1118 sizeof(*into->level_names));
1119 if (!into->level_names)
1122 for (i = 0; i < into->num_levels; i++)
1123 into->level_names[i] = strdup(from->level_names[i]);
1130 InitCanonicalKeyTypes(struct xkb_keymap *keymap, unsigned which,
1133 const struct xkb_key_type *from;
1136 darray_growalloc(keymap->types, XkbNumRequiredTypes);
1138 if ((which & XkbAllRequiredTypes) == 0)
1142 from = canonicalTypes;
1144 if (which & XkbOneLevelMask)
1145 rtrn = CopyKeyType(&from[XkbOneLevelIndex],
1146 &darray_item(keymap->types, XkbOneLevelIndex));
1148 if ((which & XkbTwoLevelMask) && rtrn == Success)
1149 rtrn = CopyKeyType(&from[XkbTwoLevelIndex],
1150 &darray_item(keymap->types, XkbTwoLevelIndex));
1152 if ((which & XkbAlphabeticMask) && rtrn == Success)
1153 rtrn = CopyKeyType(&from[XkbAlphabeticIndex],
1154 &darray_item(keymap->types, XkbAlphabeticIndex));
1156 if ((which & XkbKeypadMask) && rtrn == Success) {
1157 struct xkb_key_type *type;
1159 rtrn = CopyKeyType(&from[XkbKeypadIndex],
1160 &darray_item(keymap->types, XkbKeypadIndex));
1161 type = &darray_item(keymap->types, XkbKeypadIndex);
1163 if (keypadVMod >= 0 && keypadVMod < XkbNumVirtualMods &&
1165 struct xkb_kt_map_entry *entry;
1166 type->mods.vmods = (1 << keypadVMod);
1168 entry = &darray_item(type->map, 0);
1169 entry->mods.mask = ShiftMask;
1170 entry->mods.real_mods = ShiftMask;
1171 entry->mods.vmods = 0;
1174 entry = &darray_item(type->map, 1);
1175 entry->mods.mask = 0;
1176 entry->mods.real_mods = 0;
1177 entry->mods.vmods = (1 << keypadVMod);
1186 CompileKeyTypes(XkbFile *file, struct xkb_keymap *keymap,
1187 enum merge_mode merge)
1190 struct xkb_key_type *type, *next;
1194 InitKeyTypesInfo(&info, keymap, NULL, file->id);
1196 HandleKeyTypesFile(&info, file, merge);
1198 if (info.errorCount != 0)
1202 keymap->types_section_name = strdup(info.name);
1205 if ((info.stdPresent & XkbOneLevelMask) == 0)
1207 if ((info.stdPresent & XkbTwoLevelMask) == 0)
1209 if ((info.stdPresent & XkbKeypadMask) == 0)
1211 if ((info.stdPresent & XkbAlphabeticMask) == 0)
1214 darray_resize0(keymap->types, i);
1216 if (XkbAllRequiredTypes & (~info.stdPresent)) {
1217 unsigned missing, keypadVMod;
1219 missing = XkbAllRequiredTypes & (~info.stdPresent);
1220 keypadVMod = FindKeypadVMod(keymap);
1222 if (InitCanonicalKeyTypes(keymap, missing, keypadVMod) != Success) {
1223 log_wsgo(info.keymap->ctx,
1224 "Couldn't initialize canonical key types\n");
1228 if (missing & XkbOneLevelMask)
1229 darray_item(keymap->types, XkbOneLevelIndex).name =
1230 xkb_atom_text(keymap->ctx, info.tok_ONE_LEVEL);
1231 if (missing & XkbTwoLevelMask)
1232 darray_item(keymap->types, XkbTwoLevelIndex).name =
1233 xkb_atom_text(keymap->ctx, info.tok_TWO_LEVEL);
1234 if (missing & XkbAlphabeticMask)
1235 darray_item(keymap->types, XkbAlphabeticIndex).name =
1236 xkb_atom_text(keymap->ctx, info.tok_ALPHABETIC);
1237 if (missing & XkbKeypadMask)
1238 darray_item(keymap->types, XkbKeypadIndex).name =
1239 xkb_atom_text(keymap->ctx, info.tok_KEYPAD);
1242 next = &darray_item(keymap->types, XkbLastRequiredType + 1);
1243 list_foreach(def, &info.types, entry) {
1244 if (def->name == info.tok_ONE_LEVEL)
1245 type = &darray_item(keymap->types, XkbOneLevelIndex);
1246 else if (def->name == info.tok_TWO_LEVEL)
1247 type = &darray_item(keymap->types, XkbTwoLevelIndex);
1248 else if (def->name == info.tok_ALPHABETIC)
1249 type = &darray_item(keymap->types, XkbAlphabeticIndex);
1250 else if (def->name == info.tok_KEYPAD)
1251 type = &darray_item(keymap->types, XkbKeypadIndex);
1255 DeleteLevel1MapEntries(def);
1257 if (!CopyDefToKeyType(&info, def, type))
1261 FreeKeyTypesInfo(&info);
1265 FreeKeyTypesInfo(&info);