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