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