types: remove outdated comments
[platform/upstream/libxkbcommon.git] / src / xkbcomp / types.c
1 /************************************************************
2  * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
3  *
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.
15  *
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.
24  *
25  ********************************************************/
26
27 #include "xkbcomp-priv.h"
28 #include "text.h"
29 #include "vmod.h"
30 #include "expr.h"
31 #include "include.h"
32
33 /*
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).
40  *
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.
45  *
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.
56  *
57  * Type definitions
58  * ----------------
59  *  Statements of the form:
60  *      type "FOUR_LEVEL" { ... }
61  *
62  * The above would create a new type named "FOUR_LEVEL".
63  * The body of the definition may include statements of the following
64  * forms:
65  *
66  * - level_name statements (mandatory for each level in the type):
67  *      level_name[Level1] = "Base";
68  *
69  *   Gives each level in this type a descriptive name. It isn't used
70  *   for any thing.
71  *   Note: A level may be specified as Level[1-8] or just a number (can
72  *   be more than 8).
73  *
74  * - modifiers statement (mandatory, should be specified only once):
75  *      modifiers = Shift+Lock+LevelThree;
76  *
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
80  *   calculation.
81  *
82  * - map entry statements (should have at least as many mappings as there
83  *   are levels in the type):
84  *      map[Shift+LevelThree] = Level4;
85  *
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
92  *   user.
93  *
94  * - preserve statements:
95  *      map[Shift+Lock+LevelThree] = Level5;
96  *      preserve[Shift+Lock+LevelThree] = Lock;
97  *
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.
110  *
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
118  *   consumed.
119  *
120  * Virtual modifier statements
121  * ---------------------------
122  * Statements of the form:
123  *     virtual_modifiers LControl;
124  *
125  * Can appear in the xkb_types, xkb_compat, xkb_symbols sections.
126  * TODO
127  *
128  * Effect on keymap
129  * ----------------
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.
136  */
137
138 enum type_field {
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),
143 };
144
145 typedef struct _KeyTypeInfo {
146     enum type_field defined;
147     unsigned file_id;
148     enum merge_mode merge;
149     struct list entry;
150
151     xkb_atom_t name;
152     xkb_mod_mask_t mods;
153     xkb_level_index_t num_levels;
154     darray(struct xkb_kt_map_entry) entries;
155     darray(xkb_atom_t) level_names;
156 } KeyTypeInfo;
157
158 typedef struct _KeyTypesInfo {
159     char *name;
160     int errorCount;
161     unsigned file_id;
162     unsigned num_types;
163     struct list types;
164     VModInfo vmods;
165     struct xkb_keymap *keymap;
166 } KeyTypesInfo;
167
168 /***====================================================================***/
169
170 static inline const char *
171 MapEntryTxt(KeyTypesInfo *info, struct xkb_kt_map_entry *entry)
172 {
173     return VModMaskText(info->keymap, entry->mods.mods);
174 }
175
176 static inline const char *
177 TypeTxt(KeyTypesInfo *info, KeyTypeInfo *type)
178 {
179     return xkb_atom_text(info->keymap->ctx, type->name);
180 }
181
182 static inline const char *
183 TypeMaskTxt(KeyTypesInfo *info, KeyTypeInfo *type)
184 {
185     return VModMaskText(info->keymap, type->mods);
186 }
187
188 static inline bool
189 ReportTypeShouldBeArray(KeyTypesInfo *info, KeyTypeInfo *type,
190                         const char *field)
191 {
192     return ReportShouldBeArray(info->keymap, "key type", field,
193                                TypeTxt(info, type));
194 }
195
196 static inline bool
197 ReportTypeBadType(KeyTypesInfo *info, KeyTypeInfo *type,
198                   const char *field, const char *wanted)
199 {
200     return ReportBadType(info->keymap->ctx, "key type", field,
201                          TypeTxt(info, type), wanted);
202 }
203
204 static inline bool
205 ReportTypeBadWidth(KeyTypesInfo *info, const char *type, int has, int needs)
206 {
207     log_err(info->keymap->ctx,
208             "Key type \"%s\" has %d levels, must have %d; "
209             "Illegal type definition ignored\n",
210             type, has, needs);
211     return false;
212 }
213
214 /***====================================================================***/
215
216 static void
217 InitKeyTypesInfo(KeyTypesInfo *info, struct xkb_keymap *keymap,
218                  unsigned file_id)
219 {
220     info->name = strdup("default");
221     info->errorCount = 0;
222     info->num_types = 0;
223     list_init(&info->types);
224     info->file_id = file_id;
225     InitVModInfo(&info->vmods, keymap);
226     info->keymap = keymap;
227 }
228
229 static void
230 FreeKeyTypeInfo(KeyTypeInfo * type)
231 {
232     darray_free(type->entries);
233     darray_free(type->level_names);
234 }
235
236 static void
237 FreeKeyTypesInfo(KeyTypesInfo * info)
238 {
239     KeyTypeInfo *type, *next_type;
240     free(info->name);
241     info->name = NULL;
242     list_foreach_safe(type, next_type, &info->types, entry) {
243         FreeKeyTypeInfo(type);
244         free(type);
245     }
246 }
247
248 static KeyTypeInfo *
249 NextKeyType(KeyTypesInfo * info)
250 {
251     KeyTypeInfo *type;
252
253     type = calloc(1, sizeof(*type));
254     if (!type)
255         return NULL;
256
257     type->file_id = info->file_id;
258
259     list_append(&type->entry, &info->types);
260     info->num_types++;
261     return type;
262 }
263
264 static KeyTypeInfo *
265 FindMatchingKeyType(KeyTypesInfo *info, xkb_atom_t name)
266 {
267     KeyTypeInfo *old;
268
269     list_foreach(old, &info->types, entry)
270         if (old->name == name)
271             return old;
272
273     return NULL;
274 }
275
276 static bool
277 AddKeyType(KeyTypesInfo *info, KeyTypeInfo *new)
278 {
279     KeyTypeInfo *old;
280     struct list entry;
281     int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
282
283     old = FindMatchingKeyType(info, new->name);
284     if (old) {
285         if (new->merge == MERGE_REPLACE || new->merge == MERGE_OVERRIDE) {
286             if ((old->file_id == new->file_id && verbosity > 0) ||
287                 verbosity > 9) {
288                 log_warn(info->keymap->ctx,
289                          "Multiple definitions of the %s key type; "
290                          "Earlier definition ignored\n",
291                          xkb_atom_text(info->keymap->ctx, new->name));
292             }
293
294             entry = old->entry;
295             FreeKeyTypeInfo(old);
296             *old = *new;
297             old->entry = entry;
298             darray_init(new->entries);
299             darray_init(new->level_names);
300             return true;
301         }
302
303         if (old->file_id == new->file_id)
304             log_lvl(info->keymap->ctx, 4,
305                     "Multiple definitions of the %s key type; "
306                     "Later definition ignored\n",
307                     xkb_atom_text(info->keymap->ctx, new->name));
308
309         FreeKeyTypeInfo(new);
310         return true;
311     }
312
313     old = NextKeyType(info);
314     if (!old)
315         return false;
316
317     entry = old->entry;
318     *old = *new;
319     old->entry = entry;
320     darray_init(new->entries);
321     darray_init(new->level_names);
322     return true;
323 }
324
325 /***====================================================================***/
326
327 static void
328 MergeIncludedKeyTypes(KeyTypesInfo *into, KeyTypesInfo *from,
329                       enum merge_mode merge)
330 {
331     KeyTypeInfo *type, *next_type;
332
333     if (from->errorCount > 0) {
334         into->errorCount += from->errorCount;
335         return;
336     }
337
338     if (into->name == NULL) {
339         into->name = from->name;
340         from->name = NULL;
341     }
342
343     list_foreach_safe(type, next_type, &from->types, entry) {
344         type->merge = (merge == MERGE_DEFAULT ? type->merge : merge);
345         if (!AddKeyType(into, type))
346             into->errorCount++;
347     }
348 }
349
350 static void
351 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge);
352
353 static bool
354 HandleIncludeKeyTypes(KeyTypesInfo *info, IncludeStmt *stmt)
355 {
356     enum merge_mode merge = MERGE_DEFAULT;
357     XkbFile *rtrn;
358     KeyTypesInfo included, next_incl;
359
360     InitKeyTypesInfo(&included, info->keymap, info->file_id);
361     if (stmt->stmt) {
362         free(included.name);
363         included.name = stmt->stmt;
364         stmt->stmt = NULL;
365     }
366
367     for (; stmt; stmt = stmt->next_incl) {
368         if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_TYPES,
369                                 &rtrn, &merge)) {
370             info->errorCount += 10;
371             FreeKeyTypesInfo(&included);
372             return false;
373         }
374
375         InitKeyTypesInfo(&next_incl, info->keymap, rtrn->id);
376
377         HandleKeyTypesFile(&next_incl, rtrn, merge);
378
379         MergeIncludedKeyTypes(&included, &next_incl, merge);
380
381         FreeKeyTypesInfo(&next_incl);
382         FreeXkbFile(rtrn);
383     }
384
385     MergeIncludedKeyTypes(info, &included, merge);
386     FreeKeyTypesInfo(&included);
387
388     return (info->errorCount == 0);
389 }
390
391 /***====================================================================***/
392
393 static bool
394 SetModifiers(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
395              ExprDef *value)
396 {
397     xkb_mod_mask_t mods;
398
399     if (arrayNdx)
400         log_warn(info->keymap->ctx,
401                  "The modifiers field of a key type is not an array; "
402                  "Illegal array subscript ignored\n");
403
404     /* get modifier mask for current type */
405     if (!ExprResolveVModMask(info->keymap, value, &mods)) {
406         log_err(info->keymap->ctx,
407                 "Key type mask field must be a modifier mask; "
408                 "Key type definition ignored\n");
409         return false;
410     }
411
412     if (type->defined & TYPE_FIELD_MASK) {
413         log_warn(info->keymap->ctx,
414                  "Multiple modifier mask definitions for key type %s; "
415                  "Using %s, ignoring %s\n",
416                  xkb_atom_text(info->keymap->ctx, type->name),
417                  TypeMaskTxt(info, type),
418                  VModMaskText(info->keymap, mods));
419         return false;
420     }
421
422     type->mods = mods;
423     return true;
424 }
425
426 /***====================================================================***/
427
428 static struct xkb_kt_map_entry *
429 FindMatchingMapEntry(KeyTypeInfo *type, xkb_mod_mask_t mods)
430 {
431     struct xkb_kt_map_entry *entry;
432
433     darray_foreach(entry, type->entries)
434         if (entry->mods.mods == mods)
435             return entry;
436
437     return NULL;
438 }
439
440 static bool
441 AddMapEntry(KeyTypesInfo *info, KeyTypeInfo *type,
442             struct xkb_kt_map_entry *new, bool clobber, bool report)
443 {
444     struct xkb_kt_map_entry * old;
445
446     old = FindMatchingMapEntry(type, new->mods.mods);
447     if (old) {
448         if (report && old->level != new->level) {
449             log_warn(info->keymap->ctx,
450                      "Multiple map entries for %s in %s; "
451                      "Using %d, ignoring %d\n",
452                      MapEntryTxt(info, new), TypeTxt(info, type),
453                      (clobber ? new->level : old->level) + 1,
454                      (clobber ? old->level : new->level) + 1);
455         }
456         else {
457             log_lvl(info->keymap->ctx, 10,
458                     "Multiple occurences of map[%s]= %d in %s; Ignored\n",
459                     MapEntryTxt(info, new), new->level + 1,
460                     TypeTxt(info, type));
461             return true;
462         }
463
464         if (clobber) {
465             if (new->level >= type->num_levels)
466                 type->num_levels = new->level + 1;
467             old->level = new->level;
468         }
469
470         return true;
471     }
472
473     if (new->level >= type->num_levels)
474         type->num_levels = new->level + 1;
475
476     darray_append(type->entries, *new);
477     return true;
478 }
479
480 static bool
481 SetMapEntry(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
482             ExprDef *value)
483 {
484     struct xkb_kt_map_entry entry;
485
486     if (arrayNdx == NULL)
487         return ReportTypeShouldBeArray(info, type, "map entry");
488
489     if (!ExprResolveVModMask(info->keymap, arrayNdx, &entry.mods.mods))
490         return ReportTypeBadType(info, type, "map entry", "modifier mask");
491
492     if (entry.mods.mods & (~type->mods)) {
493         log_lvl(info->keymap->ctx, 1,
494                 "Map entry for unused modifiers in %s; "
495                 "Using %s instead of %s\n",
496                 TypeTxt(info, type),
497                 VModMaskText(info->keymap, entry.mods.mods & type->mods),
498                 MapEntryTxt(info, &entry));
499         entry.mods.mods &= type->mods;
500     }
501
502     if (!ExprResolveLevel(info->keymap->ctx, value, &entry.level)) {
503         log_err(info->keymap->ctx,
504                 "Level specifications in a key type must be integer; "
505                 "Ignoring malformed level specification\n");
506         return false;
507     }
508
509     entry.preserve.mods = 0;
510
511     return AddMapEntry(info, type, &entry, true, true);
512 }
513
514 /***====================================================================***/
515
516 static bool
517 AddPreserve(KeyTypesInfo *info, KeyTypeInfo *type,
518             xkb_mod_mask_t mods, xkb_mod_mask_t preserve_mods)
519 {
520     struct xkb_kt_map_entry *entry;
521     struct xkb_kt_map_entry new;
522
523     darray_foreach(entry, type->entries) {
524         if (entry->mods.mods != mods)
525             continue;
526
527         /* Map exists without previous preserve (or "None"); override. */
528         if (entry->preserve.mods == 0) {
529             entry->preserve.mods = preserve_mods;
530             return true;
531         }
532
533         /* Map exists with same preserve; do nothing. */
534         if (entry->preserve.mods == preserve_mods) {
535             log_lvl(info->keymap->ctx, 10,
536                     "Identical definitions for preserve[%s] in %s; "
537                     "Ignored\n",
538                     VModMaskText(info->keymap, mods),
539                     TypeTxt(info, type));
540             return true;
541         }
542
543         /* Map exists with different preserve; latter wins. */
544         log_lvl(info->keymap->ctx, 1,
545                 "Multiple definitions for preserve[%s] in %s; "
546                 "Using %s, ignoring %s\n",
547                 VModMaskText(info->keymap, mods),
548                 TypeTxt(info, type),
549                 VModMaskText(info->keymap, preserve_mods),
550                 VModMaskText(info->keymap, entry->preserve.mods));
551
552         entry->preserve.mods = preserve_mods;
553         return true;
554     }
555
556     /*
557      * Map does not exist, i.e. preserve[] came before map[].
558      * Create a map with the specified mask mapping to Level1. The level
559      * may be overriden later with an explicit map[] statement.
560      */
561     new.level = 0;
562     new.mods.mods = mods;
563     new.preserve.mods = preserve_mods;
564     darray_append(type->entries, new);
565     return true;
566 }
567
568 static bool
569 SetPreserve(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
570             ExprDef *value)
571 {
572     xkb_mod_mask_t mods, preserve_mods;
573
574     if (arrayNdx == NULL)
575         return ReportTypeShouldBeArray(info, type, "preserve entry");
576
577     if (!ExprResolveVModMask(info->keymap, arrayNdx, &mods))
578         return ReportTypeBadType(info, type, "preserve entry",
579                                  "modifier mask");
580
581     if (mods & ~type->mods) {
582         const char *before, *after;
583
584         before = VModMaskText(info->keymap, mods);
585         mods &= type->mods;
586         after = VModMaskText(info->keymap, mods);
587
588         log_lvl(info->keymap->ctx, 1,
589                 "Preserve for modifiers not used by the %s type; "
590                 "Index %s converted to %s\n",
591                 TypeTxt(info, type), before, after);
592     }
593
594     if (!ExprResolveVModMask(info->keymap, value, &preserve_mods)) {
595         log_err(info->keymap->ctx,
596                 "Preserve value in a key type is not a modifier mask; "
597                 "Ignoring preserve[%s] in type %s\n",
598                 VModMaskText(info->keymap, mods),
599                 TypeTxt(info, type));
600         return false;
601     }
602
603     if (preserve_mods & ~mods) {
604         const char *before, *after;
605
606         before = VModMaskText(info->keymap, preserve_mods);
607         preserve_mods &= mods;
608         after = VModMaskText(info->keymap, preserve_mods);
609
610         log_lvl(info->keymap->ctx, 1,
611                 "Illegal value for preserve[%s] in type %s; "
612                 "Converted %s to %s\n",
613                 VModMaskText(info->keymap, mods),
614                 TypeTxt(info, type), before, after);
615     }
616
617     return AddPreserve(info, type, mods, preserve_mods);
618 }
619
620 /***====================================================================***/
621
622 static bool
623 AddLevelName(KeyTypesInfo *info, KeyTypeInfo *type,
624              xkb_level_index_t level, xkb_atom_t name, bool clobber)
625 {
626     /* New name. */
627     if (level >= darray_size(type->level_names)) {
628         darray_resize0(type->level_names, level + 1);
629         goto finish;
630     }
631
632     /* Same level, same name. */
633     if (darray_item(type->level_names, level) == name) {
634         log_lvl(info->keymap->ctx, 10,
635                 "Duplicate names for level %d of key type %s; Ignored\n",
636                 level + 1, TypeTxt(info, type));
637         return true;
638     }
639
640     /* Same level, different name. */
641     if (darray_item(type->level_names, level) != XKB_ATOM_NONE) {
642         const char *old, *new;
643         old = xkb_atom_text(info->keymap->ctx,
644                             darray_item(type->level_names, level));
645         new = xkb_atom_text(info->keymap->ctx, name);
646         log_lvl(info->keymap->ctx, 1,
647                 "Multiple names for level %d of key type %s; "
648                 "Using %s, ignoring %s\n",
649                 level + 1, TypeTxt(info, type),
650                 (clobber ? new : old), (clobber ? old : new));
651
652         if (!clobber)
653             return true;
654     }
655
656     /* XXX: What about different level, same name? */
657
658 finish:
659     darray_item(type->level_names, level) = name;
660     return true;
661 }
662
663 static bool
664 SetLevelName(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
665              ExprDef *value)
666 {
667     xkb_level_index_t level;
668     xkb_atom_t level_name;
669     struct xkb_context *ctx = info->keymap->ctx;
670     const char *str;
671
672     if (arrayNdx == NULL)
673         return ReportTypeShouldBeArray(info, type, "level name");
674
675     if (!ExprResolveLevel(ctx, arrayNdx, &level))
676         return ReportTypeBadType(info, type, "level name", "integer");
677
678     if (!ExprResolveString(ctx, value, &str)) {
679         log_err(info->keymap->ctx,
680                 "Non-string name for level %d in key type %s; "
681                 "Ignoring illegal level name definition\n",
682                 level + 1, xkb_atom_text(ctx, type->name));
683         return false;
684     }
685
686     level_name = xkb_atom_intern(ctx, str);
687
688     return AddLevelName(info, type, level, level_name, true);
689 }
690
691 /***====================================================================***/
692
693 static bool
694 SetKeyTypeField(KeyTypesInfo *info, KeyTypeInfo *type,
695                 const char *field, ExprDef *arrayNdx, ExprDef *value)
696 {
697     bool ok = false;
698     enum type_field type_field = 0;
699
700     if (istreq(field, "modifiers")) {
701         type_field = TYPE_FIELD_MASK;
702         ok = SetModifiers(info, type, arrayNdx, value);
703     }
704     else if (istreq(field, "map")) {
705         type_field = TYPE_FIELD_MAP;
706         ok = SetMapEntry(info, type, arrayNdx, value);
707     }
708     else if (istreq(field, "preserve")) {
709         type_field = TYPE_FIELD_PRESERVE;
710         ok = SetPreserve(info, type, arrayNdx, value);
711     }
712     else if (istreq(field, "levelname") || istreq(field, "level_name")) {
713         type_field = TYPE_FIELD_LEVEL_NAME;
714         ok = SetLevelName(info, type, arrayNdx, value);
715     } else {
716         log_err(info->keymap->ctx,
717                 "Unknown field %s in key type %s; Definition ignored\n",
718                 field, TypeTxt(info, type));
719     }
720
721     type->defined |= type_field;
722     return ok;
723 }
724
725 static bool
726 HandleKeyTypeBody(KeyTypesInfo *info, VarDef *def, KeyTypeInfo *type)
727 {
728     bool ok = true;
729     const char *elem, *field;
730     ExprDef *arrayNdx;
731
732     for (; def; def = (VarDef *) def->common.next) {
733         ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
734                             &arrayNdx);
735         if (!ok)
736             continue;
737
738         if (elem && istreq(elem, "type")) {
739             log_err(info->keymap->ctx,
740                     "Support for changing the default type has been removed; "
741                     "Statement ignored\n");
742             continue;
743         }
744
745         ok = SetKeyTypeField(info, type, field, arrayNdx, def->value);
746     }
747
748     return ok;
749 }
750
751 static bool
752 HandleKeyTypeDef(KeyTypesInfo *info, KeyTypeDef *def, enum merge_mode merge)
753 {
754     KeyTypeInfo type = {
755         .defined = 0,
756         .file_id = info->file_id,
757         .merge = (def->merge == MERGE_DEFAULT ? merge : def->merge),
758         .name = def->name,
759         .mods = 0,
760         .num_levels = 1,
761         .entries = darray_new(),
762         .level_names = darray_new(),
763     };
764
765     if (!HandleKeyTypeBody(info, def->body, &type)) {
766         info->errorCount++;
767         return false;
768     }
769
770     if (!AddKeyType(info, &type)) {
771         info->errorCount++;
772         return false;
773     }
774
775     return true;
776 }
777
778 static void
779 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge)
780 {
781     bool ok;
782     ParseCommon *stmt;
783
784     free(info->name);
785     info->name = strdup_safe(file->name);
786
787     for (stmt = file->defs; stmt; stmt = stmt->next) {
788         switch (stmt->type) {
789         case STMT_INCLUDE:
790             ok = HandleIncludeKeyTypes(info, (IncludeStmt *) stmt);
791             break;
792         case STMT_TYPE: /* e.g. type "ONE_LEVEL" */
793             ok = HandleKeyTypeDef(info, (KeyTypeDef *) stmt, merge);
794             break;
795         case STMT_VAR:
796             log_err(info->keymap->ctx,
797                     "Support for changing the default type has been removed; "
798                     "Statement ignored\n");
799             ok = true;
800             break;
801         case STMT_VMOD: /* virtual_modifiers NumLock, ... */
802             ok = HandleVModDef((VModDef *) stmt, info->keymap, merge,
803                                &info->vmods);
804             break;
805         default:
806             log_err(info->keymap->ctx,
807                     "Key type files may not include other declarations; "
808                     "Ignoring %s\n", stmt_type_to_string(stmt->type));
809             ok = false;
810             break;
811         }
812
813         if (!ok)
814             info->errorCount++;
815
816         if (info->errorCount > 10) {
817             log_err(info->keymap->ctx,
818                     "Abandoning keytypes file \"%s\"\n", file->topName);
819             break;
820         }
821     }
822 }
823
824 static void
825 CopyDefToKeyType(KeyTypesInfo *info, KeyTypeInfo *def,
826                  struct xkb_key_type *type)
827 {
828     type->mods.mods = def->mods;
829     type->num_levels = def->num_levels;
830     type->map = darray_mem(def->entries, 0);
831     type->num_entries = darray_size(def->entries);
832     darray_init(def->entries);
833     type->name = def->name;
834     type->level_names = darray_mem(def->level_names, 0);
835     darray_init(def->level_names);
836 }
837
838 static bool
839 CopyKeyTypesToKeymap(struct xkb_keymap *keymap, KeyTypesInfo *info)
840 {
841     unsigned int i;
842     unsigned int num_types;
843     KeyTypeInfo *def;
844
845     num_types = info->num_types ? info->num_types : 1;
846     keymap->types = calloc(num_types, sizeof(*keymap->types));
847     if (!keymap->types)
848         return false;
849
850     keymap->num_types = num_types;
851
852     /*
853      * If no types were specified, a default unnamed one-level type is
854      * used for all keys.
855      */
856     if (info->num_types == 0) {
857         KeyTypeInfo dflt = {
858             .name = xkb_atom_intern(keymap->ctx, "default"),
859             .mods = 0,
860             .num_levels = 1,
861             .entries = darray_new(),
862             .level_names = darray_new(),
863         };
864
865         CopyDefToKeyType(info, &dflt, &keymap->types[0]);
866     } else {
867         i = 0;
868         list_foreach(def, &info->types, entry)
869             CopyDefToKeyType(info, def, &keymap->types[i++]);
870     }
871
872     keymap->types_section_name = strdup_safe(info->name);
873
874     return true;
875 }
876
877 bool
878 CompileKeyTypes(XkbFile *file, struct xkb_keymap *keymap,
879                 enum merge_mode merge)
880 {
881     KeyTypesInfo info;
882
883     InitKeyTypesInfo(&info, keymap, file->id);
884
885     HandleKeyTypesFile(&info, file, merge);
886     if (info.errorCount != 0)
887         goto err_info;
888
889     if (!CopyKeyTypesToKeymap(keymap, &info))
890         goto err_info;
891
892     FreeKeyTypesInfo(&info);
893     return true;
894
895 err_info:
896     FreeKeyTypesInfo(&info);
897     return false;
898 }