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