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