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