text: take xkb_mod_set instead of the entire keymap
[platform/upstream/libxkbcommon.git] / src / xkbcomp / symbols.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 /*
28  * Copyright © 2012 Intel Corporation
29  * Copyright © 2012 Ran Benita <ran234@gmail.com>
30  *
31  * Permission is hereby granted, free of charge, to any person obtaining a
32  * copy of this software and associated documentation files (the "Software"),
33  * to deal in the Software without restriction, including without limitation
34  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
35  * and/or sell copies of the Software, and to permit persons to whom the
36  * Software is furnished to do so, subject to the following conditions:
37  *
38  * The above copyright notice and this permission notice (including the next
39  * paragraph) shall be included in all copies or substantial portions of the
40  * Software.
41  *
42  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
45  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48  * DEALINGS IN THE SOFTWARE.
49  *
50  * Author: Daniel Stone <daniel@fooishbar.org>
51  *         Ran Benita <ran234@gmail.com>
52  */
53
54 #include "xkbcomp-priv.h"
55 #include "text.h"
56 #include "expr.h"
57 #include "action.h"
58 #include "vmod.h"
59 #include "include.h"
60 #include "keysym.h"
61
62 enum key_repeat {
63     KEY_REPEAT_UNDEFINED = 0,
64     KEY_REPEAT_YES = 1,
65     KEY_REPEAT_NO = 2,
66 };
67
68 enum group_field {
69     GROUP_FIELD_SYMS = (1 << 0),
70     GROUP_FIELD_ACTS = (1 << 1),
71     GROUP_FIELD_TYPE = (1 << 2),
72 };
73
74 enum key_field {
75     KEY_FIELD_REPEAT    = (1 << 0),
76     KEY_FIELD_DEFAULT_TYPE = (1 << 1),
77     KEY_FIELD_GROUPINFO = (1 << 2),
78     KEY_FIELD_VMODMAP   = (1 << 3),
79 };
80
81 typedef struct {
82     enum group_field defined;
83     darray(struct xkb_level) levels;
84     xkb_atom_t type;
85 } GroupInfo;
86
87 typedef struct {
88     enum key_field defined;
89     enum merge_mode merge;
90
91     xkb_atom_t name;
92
93     darray(GroupInfo) groups;
94
95     enum key_repeat repeat;
96     xkb_mod_mask_t vmodmap;
97     xkb_atom_t default_type;
98
99     enum xkb_range_exceed_type out_of_range_group_action;
100     xkb_layout_index_t out_of_range_group_number;
101 } KeyInfo;
102
103 static void
104 ClearLevelInfo(struct xkb_level *leveli)
105 {
106     if (leveli->num_syms > 1)
107         free(leveli->u.syms);
108 }
109
110 static void
111 InitGroupInfo(GroupInfo *groupi)
112 {
113     memset(groupi, 0, sizeof(*groupi));
114 }
115
116 static void
117 ClearGroupInfo(GroupInfo *groupi)
118 {
119     struct xkb_level *leveli;
120     darray_foreach(leveli, groupi->levels)
121         ClearLevelInfo(leveli);
122     darray_free(groupi->levels);
123 }
124
125 static void
126 CopyGroupInfo(GroupInfo *to, const GroupInfo *from)
127 {
128     to->defined = from->defined;
129     to->type = from->type;
130     darray_init(to->levels);
131     darray_copy(to->levels, from->levels);
132     for (xkb_level_index_t j = 0; j < darray_size(to->levels); j++)
133         if (darray_item(from->levels, j).num_syms > 1)
134             darray_item(to->levels, j).u.syms =
135                 memdup(darray_item(from->levels, j).u.syms,
136                        darray_item(from->levels, j).num_syms,
137                        sizeof(xkb_keysym_t));
138 }
139
140 static void
141 InitKeyInfo(struct xkb_context *ctx, KeyInfo *keyi)
142 {
143     memset(keyi, 0, sizeof(*keyi));
144     keyi->merge = MERGE_OVERRIDE;
145     keyi->name = xkb_atom_intern_literal(ctx, "*");
146     keyi->out_of_range_group_action = RANGE_WRAP;
147 }
148
149 static void
150 ClearKeyInfo(KeyInfo *keyi)
151 {
152     GroupInfo *groupi;
153     darray_foreach(groupi, keyi->groups)
154         ClearGroupInfo(groupi);
155     darray_free(keyi->groups);
156 }
157
158 /***====================================================================***/
159
160 typedef struct {
161     enum merge_mode merge;
162     bool haveSymbol;
163     xkb_mod_index_t modifier;
164     union {
165         xkb_atom_t keyName;
166         xkb_keysym_t keySym;
167     } u;
168 } ModMapEntry;
169
170 typedef struct {
171     char *name;         /* e.g. pc+us+inet(evdev) */
172     int errorCount;
173     enum merge_mode merge;
174     xkb_layout_index_t explicit_group;
175     darray(KeyInfo) keys;
176     KeyInfo default_key;
177     ActionsInfo *actions;
178     darray(xkb_atom_t) group_names;
179     darray(ModMapEntry) modmaps;
180
181     struct xkb_context *ctx;
182     struct xkb_keymap *keymap;
183 } SymbolsInfo;
184
185 static void
186 InitSymbolsInfo(SymbolsInfo *info, struct xkb_keymap *keymap,
187                 ActionsInfo *actions)
188 {
189     memset(info, 0, sizeof(*info));
190     info->ctx = keymap->ctx;
191     info->keymap = keymap;
192     info->merge = MERGE_OVERRIDE;
193     InitKeyInfo(keymap->ctx, &info->default_key);
194     info->actions = actions;
195     info->explicit_group = XKB_LAYOUT_INVALID;
196 }
197
198 static void
199 ClearSymbolsInfo(SymbolsInfo *info)
200 {
201     KeyInfo *keyi;
202     free(info->name);
203     darray_foreach(keyi, info->keys)
204         ClearKeyInfo(keyi);
205     darray_free(info->keys);
206     darray_free(info->group_names);
207     darray_free(info->modmaps);
208     ClearKeyInfo(&info->default_key);
209 }
210
211 static const char *
212 KeyInfoText(SymbolsInfo *info, KeyInfo *keyi)
213 {
214     return KeyNameText(info->ctx, keyi->name);
215 }
216
217 static bool
218 MergeGroups(SymbolsInfo *info, GroupInfo *into, GroupInfo *from, bool clobber,
219             bool report, xkb_layout_index_t group, xkb_atom_t key_name)
220 {
221     xkb_level_index_t i, levels_in_both;
222
223     /* First find the type of the merged group. */
224     if (into->type != from->type) {
225         if (from->type == XKB_ATOM_NONE) {
226         }
227         else if (into->type == XKB_ATOM_NONE) {
228             into->type = from->type;
229         }
230         else {
231             xkb_atom_t use = (clobber ? from->type : into->type);
232             xkb_atom_t ignore = (clobber ? into->type : from->type);
233
234             if (report)
235                 log_warn(info->ctx,
236                          "Multiple definitions for group %d type of key %s; "
237                          "Using %s, ignoring %s\n",
238                          group + 1, KeyNameText(info->ctx, key_name),
239                          xkb_atom_text(info->ctx, use),
240                          xkb_atom_text(info->ctx, ignore));
241
242             into->type = use;
243         }
244     }
245     into->defined |= (from->defined & GROUP_FIELD_TYPE);
246
247     /* Now look at the levels. */
248
249     if (darray_empty(from->levels)) {
250         InitGroupInfo(from);
251         return true;
252     }
253
254     if (darray_empty(into->levels)) {
255         from->type = into->type;
256         *into = *from;
257         InitGroupInfo(from);
258         return true;
259     }
260
261     /* Merge the actions and syms. */
262     levels_in_both = MIN(darray_size(into->levels), darray_size(from->levels));
263     for (i = 0; i < levels_in_both; i++) {
264         struct xkb_level *intoLevel = &darray_item(into->levels, i);
265         struct xkb_level *fromLevel = &darray_item(from->levels, i);
266
267         if (fromLevel->action.type == ACTION_TYPE_NONE) {
268         }
269         else if (intoLevel->action.type == ACTION_TYPE_NONE) {
270             intoLevel->action = fromLevel->action;
271         }
272         else {
273             union xkb_action *use, *ignore;
274             use = (clobber ? &fromLevel->action : &intoLevel->action);
275             ignore = (clobber ? &intoLevel->action : &fromLevel->action);
276
277             if (report)
278                 log_warn(info->ctx,
279                          "Multiple actions for level %d/group %u on key %s; "
280                          "Using %s, ignoring %s\n",
281                          i + 1, group + 1, KeyNameText(info->ctx, key_name),
282                          ActionTypeText(use->type),
283                          ActionTypeText(ignore->type));
284
285             intoLevel->action = *use;
286         }
287
288         if (fromLevel->num_syms == 0) {
289         }
290         else if (intoLevel->num_syms == 0) {
291             intoLevel->num_syms = fromLevel->num_syms;
292             if (fromLevel->num_syms > 1)
293                 intoLevel->u.syms = fromLevel->u.syms;
294             else
295                 intoLevel->u.sym = fromLevel->u.sym;
296             fromLevel->num_syms = 0;
297         }
298         else {
299             if (report)
300                 log_warn(info->ctx,
301                          "Multiple symbols for level %d/group %u on key %s; "
302                          "Using %s, ignoring %s\n",
303                          i + 1, group + 1, KeyNameText(info->ctx, key_name),
304                          (clobber ? "from" : "to"),
305                          (clobber ? "to" : "from"));
306
307             if (clobber) {
308                 ClearLevelInfo(intoLevel);
309                 intoLevel->num_syms = fromLevel->num_syms;
310                 if (fromLevel->num_syms > 1)
311                     intoLevel->u.syms = fromLevel->u.syms;
312                 else
313                     intoLevel->u.sym = fromLevel->u.sym;
314                 fromLevel->num_syms = 0;
315             }
316         }
317     }
318     /* If @from has extra levels, get them as well. */
319     for (i = levels_in_both; i < darray_size(from->levels); i++) {
320         darray_append(into->levels, darray_item(from->levels, i));
321         darray_item(from->levels, i).num_syms = 0;
322     }
323     into->defined |= (from->defined & GROUP_FIELD_ACTS);
324     into->defined |= (from->defined & GROUP_FIELD_SYMS);
325
326     return true;
327 }
328
329 static bool
330 UseNewKeyField(enum key_field field, enum key_field old, enum key_field new,
331                bool clobber, bool report, enum key_field *collide)
332 {
333     if (!(old & field))
334         return (new & field);
335
336     if (new & field) {
337         if (report)
338             *collide |= field;
339
340         if (clobber)
341             return true;
342     }
343
344     return false;
345 }
346
347 static bool
348 MergeKeys(SymbolsInfo *info, KeyInfo *into, KeyInfo *from, bool same_file)
349 {
350     xkb_layout_index_t i;
351     xkb_layout_index_t groups_in_both;
352     enum key_field collide = 0;
353     const int verbosity = xkb_context_get_log_verbosity(info->ctx);
354     const bool clobber = (from->merge != MERGE_AUGMENT);
355     const bool report = (same_file && verbosity > 0) || verbosity > 9;
356
357     if (from->merge == MERGE_REPLACE) {
358         ClearKeyInfo(into);
359         *into = *from;
360         InitKeyInfo(info->ctx, from);
361         return true;
362     }
363
364     groups_in_both = MIN(darray_size(into->groups), darray_size(from->groups));
365     for (i = 0; i < groups_in_both; i++)
366         MergeGroups(info,
367                     &darray_item(into->groups, i),
368                     &darray_item(from->groups, i),
369                     clobber, report, i, into->name);
370     /* If @from has extra groups, just move them to @into. */
371     for (i = groups_in_both; i < darray_size(from->groups); i++) {
372         darray_append(into->groups, darray_item(from->groups, i));
373         InitGroupInfo(&darray_item(from->groups, i));
374     }
375
376     if (UseNewKeyField(KEY_FIELD_VMODMAP, into->defined, from->defined,
377                        clobber, report, &collide)) {
378         into->vmodmap = from->vmodmap;
379         into->defined |= KEY_FIELD_VMODMAP;
380     }
381     if (UseNewKeyField(KEY_FIELD_REPEAT, into->defined, from->defined,
382                        clobber, report, &collide)) {
383         into->repeat = from->repeat;
384         into->defined |= KEY_FIELD_REPEAT;
385     }
386     if (UseNewKeyField(KEY_FIELD_DEFAULT_TYPE, into->defined, from->defined,
387                        clobber, report, &collide)) {
388         into->default_type = from->default_type;
389         into->defined |= KEY_FIELD_DEFAULT_TYPE;
390     }
391     if (UseNewKeyField(KEY_FIELD_GROUPINFO, into->defined, from->defined,
392                        clobber, report, &collide)) {
393         into->out_of_range_group_action = from->out_of_range_group_action;
394         into->out_of_range_group_number = from->out_of_range_group_number;
395         into->defined |= KEY_FIELD_GROUPINFO;
396     }
397
398     if (collide)
399         log_warn(info->ctx,
400                  "Symbol map for key %s redefined; "
401                  "Using %s definition for conflicting fields\n",
402                  KeyNameText(info->ctx, into->name),
403                  (clobber ? "first" : "last"));
404
405     ClearKeyInfo(from);
406     InitKeyInfo(info->ctx, from);
407     return true;
408 }
409
410 static bool
411 AddKeySymbols(SymbolsInfo *info, KeyInfo *keyi, bool same_file)
412 {
413     xkb_atom_t real_name;
414     KeyInfo *iter;
415
416     /*
417      * Don't keep aliases in the keys array; this guarantees that
418      * searching for keys to merge with by straight comparison (see the
419      * following loop) is enough, and we won't get multiple KeyInfo's
420      * for the same key because of aliases.
421      */
422     real_name = XkbResolveKeyAlias(info->keymap, keyi->name);
423     if (real_name != XKB_ATOM_NONE)
424         keyi->name = real_name;
425
426     darray_foreach(iter, info->keys)
427         if (iter->name == keyi->name)
428             return MergeKeys(info, iter, keyi, same_file);
429
430     darray_append(info->keys, *keyi);
431     InitKeyInfo(info->ctx, keyi);
432     return true;
433 }
434
435 static bool
436 AddModMapEntry(SymbolsInfo *info, ModMapEntry *new)
437 {
438     ModMapEntry *old;
439     bool clobber = (new->merge != MERGE_AUGMENT);
440
441     darray_foreach(old, info->modmaps) {
442         xkb_mod_index_t use, ignore;
443
444         if ((new->haveSymbol != old->haveSymbol) ||
445             (new->haveSymbol && new->u.keySym != old->u.keySym) ||
446             (!new->haveSymbol && new->u.keyName != old->u.keyName))
447             continue;
448
449         if (new->modifier == old->modifier)
450             return true;
451
452         use = (clobber ? new->modifier : old->modifier);
453         ignore = (clobber ? old->modifier : new->modifier);
454
455         if (new->haveSymbol)
456             log_err(info->ctx,
457                     "Symbol \"%s\" added to modifier map for multiple modifiers; "
458                     "Using %s, ignoring %s\n",
459                     KeysymText(info->ctx, new->u.keySym),
460                     ModIndexText(info->ctx, &info->keymap->mods, use),
461                     ModIndexText(info->ctx, &info->keymap->mods, ignore));
462         else
463             log_err(info->ctx,
464                     "Key \"%s\" added to modifier map for multiple modifiers; "
465                     "Using %s, ignoring %s\n",
466                     KeyNameText(info->ctx, new->u.keyName),
467                     ModIndexText(info->ctx, &info->keymap->mods, use),
468                     ModIndexText(info->ctx, &info->keymap->mods, ignore));
469
470         old->modifier = use;
471         return true;
472     }
473
474     darray_append(info->modmaps, *new);
475     return true;
476 }
477
478 /***====================================================================***/
479
480 static void
481 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
482                      enum merge_mode merge)
483 {
484     KeyInfo *keyi;
485     ModMapEntry *mm;
486     xkb_atom_t *group_name;
487     xkb_layout_index_t group_names_in_both;
488
489     if (from->errorCount > 0) {
490         into->errorCount += from->errorCount;
491         return;
492     }
493
494     if (into->name == NULL) {
495         into->name = from->name;
496         from->name = NULL;
497     }
498
499     group_names_in_both = MIN(darray_size(into->group_names),
500                               darray_size(from->group_names));
501     for (xkb_layout_index_t i = 0; i < group_names_in_both; i++) {
502         if (!darray_item(from->group_names, i))
503             continue;
504
505         if (merge == MERGE_AUGMENT && darray_item(into->group_names, i))
506             continue;
507
508         darray_item(into->group_names, i) = darray_item(from->group_names, i);
509     }
510     /* If @from has more, get them as well. */
511     darray_foreach_from(group_name, from->group_names, group_names_in_both)
512         darray_append(into->group_names, *group_name);
513
514     if (darray_empty(into->keys)) {
515         into->keys = from->keys;
516         darray_init(from->keys);
517     }
518     else {
519         darray_foreach(keyi, from->keys) {
520             keyi->merge = (merge == MERGE_DEFAULT ? keyi->merge : merge);
521             if (!AddKeySymbols(into, keyi, false))
522                 into->errorCount++;
523         }
524     }
525
526     if (darray_empty(into->modmaps)) {
527         into->modmaps = from->modmaps;
528         darray_init(from->modmaps);
529     }
530     else {
531         darray_foreach(mm, from->modmaps) {
532             mm->merge = (merge == MERGE_DEFAULT ? mm->merge : merge);
533             if (!AddModMapEntry(into, mm))
534                 into->errorCount++;
535         }
536     }
537 }
538
539 static void
540 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge);
541
542 static bool
543 HandleIncludeSymbols(SymbolsInfo *info, IncludeStmt *include)
544 {
545     SymbolsInfo included;
546
547     InitSymbolsInfo(&included, info->keymap, info->actions);
548     included.name = include->stmt;
549     include->stmt = NULL;
550
551     for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
552         SymbolsInfo next_incl;
553         XkbFile *file;
554
555         file = ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_SYMBOLS);
556         if (!file) {
557             info->errorCount += 10;
558             ClearSymbolsInfo(&included);
559             return false;
560         }
561
562         InitSymbolsInfo(&next_incl, info->keymap, info->actions);
563         if (stmt->modifier) {
564             next_incl.explicit_group = atoi(stmt->modifier) - 1;
565             if (next_incl.explicit_group >= XKB_MAX_GROUPS) {
566                 log_err(info->ctx,
567                         "Cannot set explicit group to %d - must be between 1..%d; "
568                         "Ignoring group number\n",
569                         next_incl.explicit_group + 1, XKB_MAX_GROUPS);
570                 next_incl.explicit_group = info->explicit_group;
571             }
572         }
573         else {
574             next_incl.explicit_group = info->explicit_group;
575         }
576
577         HandleSymbolsFile(&next_incl, file, MERGE_OVERRIDE);
578
579         MergeIncludedSymbols(&included, &next_incl, stmt->merge);
580
581         ClearSymbolsInfo(&next_incl);
582         FreeXkbFile(file);
583     }
584
585     MergeIncludedSymbols(info, &included, include->merge);
586     ClearSymbolsInfo(&included);
587
588     return (info->errorCount == 0);
589 }
590
591 #define SYMBOLS 1
592 #define ACTIONS 2
593
594 static bool
595 GetGroupIndex(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
596               unsigned what, xkb_layout_index_t *ndx_rtrn)
597 {
598     const char *name = (what == SYMBOLS ? "symbols" : "actions");
599
600     if (arrayNdx == NULL) {
601         xkb_layout_index_t i;
602         GroupInfo *groupi;
603         enum group_field field = (what == SYMBOLS ?
604                                   GROUP_FIELD_SYMS : GROUP_FIELD_ACTS);
605
606         darray_enumerate(i, groupi, keyi->groups) {
607             if (!(groupi->defined & field)) {
608                 *ndx_rtrn = i;
609                 return true;
610             }
611         }
612
613         if (i >= XKB_MAX_GROUPS) {
614             log_err(info->ctx,
615                     "Too many groups of %s for key %s (max %u); "
616                     "Ignoring %s defined for extra groups\n",
617                     name, KeyInfoText(info, keyi), XKB_MAX_GROUPS, name);
618             return false;
619         }
620
621         darray_resize0(keyi->groups, darray_size(keyi->groups) + 1);
622         *ndx_rtrn = darray_size(keyi->groups) - 1;
623         return true;
624     }
625
626     if (!ExprResolveGroup(info->ctx, arrayNdx, ndx_rtrn)) {
627         log_err(info->ctx,
628                 "Illegal group index for %s of key %s\n"
629                 "Definition with non-integer array index ignored\n",
630                 name, KeyInfoText(info, keyi));
631         return false;
632     }
633
634     (*ndx_rtrn)--;
635     if (*ndx_rtrn >= darray_size(keyi->groups))
636         darray_resize0(keyi->groups, *ndx_rtrn + 1);
637
638     return true;
639 }
640
641 static bool
642 AddSymbolsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
643                 ExprDef *value)
644 {
645     xkb_layout_index_t ndx;
646     GroupInfo *groupi;
647     xkb_level_index_t nLevels;
648
649     if (!GetGroupIndex(info, keyi, arrayNdx, SYMBOLS, &ndx))
650         return false;
651
652     groupi = &darray_item(keyi->groups, ndx);
653
654     if (value == NULL) {
655         groupi->defined |= GROUP_FIELD_SYMS;
656         return true;
657     }
658
659     if (value->expr.op != EXPR_KEYSYM_LIST) {
660         log_err(info->ctx,
661                 "Expected a list of symbols, found %s; "
662                 "Ignoring symbols for group %u of %s\n",
663                 expr_op_type_to_string(value->expr.op), ndx + 1,
664                 KeyInfoText(info, keyi));
665         return false;
666     }
667
668     if (groupi->defined & GROUP_FIELD_SYMS) {
669         log_err(info->ctx,
670                 "Symbols for key %s, group %u already defined; "
671                 "Ignoring duplicate definition\n",
672                 KeyInfoText(info, keyi), ndx + 1);
673         return false;
674     }
675
676     nLevels = darray_size(value->keysym_list.symsMapIndex);
677     if (darray_size(groupi->levels) < nLevels)
678         darray_resize0(groupi->levels, nLevels);
679
680     groupi->defined |= GROUP_FIELD_SYMS;
681
682     for (xkb_level_index_t i = 0; i < nLevels; i++) {
683         unsigned int sym_index;
684         struct xkb_level *leveli = &darray_item(groupi->levels, i);
685
686         sym_index = darray_item(value->keysym_list.symsMapIndex, i);
687         leveli->num_syms = darray_item(value->keysym_list.symsNumEntries, i);
688         if (leveli->num_syms > 1)
689             leveli->u.syms = calloc(leveli->num_syms, sizeof(*leveli->u.syms));
690
691         for (unsigned j = 0; j < leveli->num_syms; j++) {
692             xkb_keysym_t keysym = darray_item(value->keysym_list.syms,
693                                               sym_index + j);
694
695             if (leveli->num_syms == 1) {
696                 if (keysym == XKB_KEY_NoSymbol)
697                     leveli->num_syms = 0;
698                 else
699                     leveli->u.sym = keysym;
700             }
701             else if (leveli->num_syms > 1) {
702                 leveli->u.syms[j] = keysym;
703             }
704         }
705     }
706
707     return true;
708 }
709
710 static bool
711 AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
712                 ExprDef *value)
713 {
714     xkb_layout_index_t ndx;
715     GroupInfo *groupi;
716     unsigned int nActs;
717     ExprDef *act;
718
719     if (!GetGroupIndex(info, keyi, arrayNdx, ACTIONS, &ndx))
720         return false;
721
722     groupi = &darray_item(keyi->groups, ndx);
723
724     if (value == NULL) {
725         groupi->defined |= GROUP_FIELD_ACTS;
726         return true;
727     }
728
729     if (value->expr.op != EXPR_ACTION_LIST) {
730         log_wsgo(info->ctx,
731                  "Bad expression type (%d) for action list value; "
732                  "Ignoring actions for group %u of %s\n",
733                  value->expr.op, ndx, KeyInfoText(info, keyi));
734         return false;
735     }
736
737     if (groupi->defined & GROUP_FIELD_ACTS) {
738         log_wsgo(info->ctx,
739                  "Actions for key %s, group %u already defined\n",
740                  KeyInfoText(info, keyi), ndx);
741         return false;
742     }
743
744     nActs = 0;
745     for (act = value->unary.child; act; act = (ExprDef *) act->common.next)
746         nActs++;
747
748     if (darray_size(groupi->levels) < nActs)
749         darray_resize0(groupi->levels, nActs);
750
751     groupi->defined |= GROUP_FIELD_ACTS;
752
753     act = value->unary.child;
754     for (unsigned i = 0; i < nActs; i++) {
755         union xkb_action *toAct = &darray_item(groupi->levels, i).action;
756
757         if (!HandleActionDef(act, info->keymap, toAct, info->actions))
758             log_err(info->ctx,
759                     "Illegal action definition for %s; "
760                     "Action for group %u/level %u ignored\n",
761                     KeyInfoText(info, keyi), ndx + 1, i + 1);
762
763         act = (ExprDef *) act->common.next;
764     }
765
766     return true;
767 }
768
769 static const LookupEntry repeatEntries[] = {
770     { "true", KEY_REPEAT_YES },
771     { "yes", KEY_REPEAT_YES },
772     { "on", KEY_REPEAT_YES },
773     { "false", KEY_REPEAT_NO },
774     { "no", KEY_REPEAT_NO },
775     { "off", KEY_REPEAT_NO },
776     { "default", KEY_REPEAT_UNDEFINED },
777     { NULL, 0 }
778 };
779
780 static bool
781 SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
782                 ExprDef *arrayNdx, ExprDef *value)
783 {
784     if (istreq(field, "type")) {
785         xkb_layout_index_t ndx;
786         xkb_atom_t val;
787
788         if (!ExprResolveString(info->ctx, value, &val)) {
789             log_err(info->ctx,
790                     "The type field of a key symbol map must be a string; "
791                     "Ignoring illegal type definition\n");
792             return false;
793         }
794
795         if (!arrayNdx) {
796             keyi->default_type = val;
797             keyi->defined |= KEY_FIELD_DEFAULT_TYPE;
798         }
799         else if (!ExprResolveGroup(info->ctx, arrayNdx, &ndx)) {
800             log_err(info->ctx,
801                     "Illegal group index for type of key %s; "
802                     "Definition with non-integer array index ignored\n",
803                     KeyInfoText(info, keyi));
804             return false;
805         }
806         else {
807             ndx--;
808             if (ndx >= darray_size(keyi->groups))
809                 darray_resize0(keyi->groups, ndx + 1);
810             darray_item(keyi->groups, ndx).type = val;
811             darray_item(keyi->groups, ndx).defined |= GROUP_FIELD_TYPE;
812         }
813     }
814     else if (istreq(field, "symbols")) {
815         return AddSymbolsToKey(info, keyi, arrayNdx, value);
816     }
817     else if (istreq(field, "actions")) {
818         return AddActionsToKey(info, keyi, arrayNdx, value);
819     }
820     else if (istreq(field, "vmods") ||
821              istreq(field, "virtualmods") ||
822              istreq(field, "virtualmodifiers")) {
823         xkb_mod_mask_t mask;
824
825         if (!ExprResolveModMask(info->keymap, value, MOD_VIRT, &mask)) {
826             log_err(info->ctx,
827                     "Expected a virtual modifier mask, found %s; "
828                     "Ignoring virtual modifiers definition for key %s\n",
829                     expr_op_type_to_string(value->expr.op),
830                     KeyInfoText(info, keyi));
831             return false;
832         }
833
834         keyi->vmodmap = mask;
835         keyi->defined |= KEY_FIELD_VMODMAP;
836     }
837     else if (istreq(field, "locking") ||
838              istreq(field, "lock") ||
839              istreq(field, "locks")) {
840         log_vrb(info->ctx, 1,
841                 "Key behaviors not supported; "
842                 "Ignoring locking specification for key %s\n",
843                 KeyInfoText(info, keyi));
844     }
845     else if (istreq(field, "radiogroup") ||
846              istreq(field, "permanentradiogroup") ||
847              istreq(field, "allownone")) {
848         log_vrb(info->ctx, 1,
849                 "Radio groups not supported; "
850                 "Ignoring radio group specification for key %s\n",
851                 KeyInfoText(info, keyi));
852     }
853     else if (istreq_prefix("overlay", field) ||
854              istreq_prefix("permanentoverlay", field)) {
855         log_vrb(info->ctx, 1,
856                 "Overlays not supported; "
857                 "Ignoring overlay specification for key %s\n",
858                 KeyInfoText(info, keyi));
859     }
860     else if (istreq(field, "repeating") ||
861              istreq(field, "repeats") ||
862              istreq(field, "repeat")) {
863         unsigned int val;
864
865         if (!ExprResolveEnum(info->ctx, value, &val, repeatEntries)) {
866             log_err(info->ctx,
867                     "Illegal repeat setting for %s; "
868                     "Non-boolean repeat setting ignored\n",
869                     KeyInfoText(info, keyi));
870             return false;
871         }
872
873         keyi->repeat = val;
874         keyi->defined |= KEY_FIELD_REPEAT;
875     }
876     else if (istreq(field, "groupswrap") ||
877              istreq(field, "wrapgroups")) {
878         bool set;
879
880         if (!ExprResolveBoolean(info->ctx, value, &set)) {
881             log_err(info->ctx,
882                     "Illegal groupsWrap setting for %s; "
883                     "Non-boolean value ignored\n",
884                     KeyInfoText(info, keyi));
885             return false;
886         }
887
888         keyi->out_of_range_group_action = (set ? RANGE_WRAP : RANGE_SATURATE);
889         keyi->defined |= KEY_FIELD_GROUPINFO;
890     }
891     else if (istreq(field, "groupsclamp") ||
892              istreq(field, "clampgroups")) {
893         bool set;
894
895         if (!ExprResolveBoolean(info->ctx, value, &set)) {
896             log_err(info->ctx,
897                     "Illegal groupsClamp setting for %s; "
898                     "Non-boolean value ignored\n",
899                     KeyInfoText(info, keyi));
900             return false;
901         }
902
903         keyi->out_of_range_group_action = (set ? RANGE_SATURATE : RANGE_WRAP);
904         keyi->defined |= KEY_FIELD_GROUPINFO;
905     }
906     else if (istreq(field, "groupsredirect") ||
907              istreq(field, "redirectgroups")) {
908         xkb_layout_index_t grp;
909
910         if (!ExprResolveGroup(info->ctx, value, &grp)) {
911             log_err(info->ctx,
912                     "Illegal group index for redirect of key %s; "
913                     "Definition with non-integer group ignored\n",
914                     KeyInfoText(info, keyi));
915             return false;
916         }
917
918         keyi->out_of_range_group_action = RANGE_REDIRECT;
919         keyi->out_of_range_group_number = grp - 1;
920         keyi->defined |= KEY_FIELD_GROUPINFO;
921     }
922     else {
923         log_err(info->ctx,
924                 "Unknown field %s in a symbol interpretation; "
925                 "Definition ignored\n",
926                 field);
927         return false;
928     }
929
930     return true;
931 }
932
933 static bool
934 SetGroupName(SymbolsInfo *info, ExprDef *arrayNdx, ExprDef *value)
935 {
936     xkb_layout_index_t group, group_to_use;
937     xkb_atom_t name;
938
939     if (!arrayNdx) {
940         log_vrb(info->ctx, 1,
941                 "You must specify an index when specifying a group name; "
942                 "Group name definition without array subscript ignored\n");
943         return false;
944     }
945
946     if (!ExprResolveGroup(info->ctx, arrayNdx, &group)) {
947         log_err(info->ctx,
948                 "Illegal index in group name definition; "
949                 "Definition with non-integer array index ignored\n");
950         return false;
951     }
952
953     if (!ExprResolveString(info->ctx, value, &name)) {
954         log_err(info->ctx,
955                 "Group name must be a string; "
956                 "Illegal name for group %d ignored\n", group);
957         return false;
958     }
959
960     if (info->explicit_group == XKB_LAYOUT_INVALID) {
961         group_to_use = group - 1;
962     }
963     else if (group - 1 == 0) {
964         group_to_use = info->explicit_group;
965     }
966     else {
967         log_warn(info->ctx,
968                  "An explicit group was specified for the '%s' map, "
969                  "but it provides a name for a group other than Group1 (%d); "
970                  "Ignoring group name '%s'\n",
971                  info->name, group,
972                  xkb_atom_text(info->ctx, name));
973         return false;
974     }
975
976     if (group_to_use >= darray_size(info->group_names))
977         darray_resize0(info->group_names, group_to_use + 1);
978     darray_item(info->group_names, group_to_use) = name;
979
980     return true;
981 }
982
983 static bool
984 HandleGlobalVar(SymbolsInfo *info, VarDef *stmt)
985 {
986     const char *elem, *field;
987     ExprDef *arrayNdx;
988     bool ret;
989
990     if (!ExprResolveLhs(info->ctx, stmt->name, &elem, &field, &arrayNdx))
991         return false;
992
993     if (elem && istreq(elem, "key")) {
994         ret = SetSymbolsField(info, &info->default_key, field, arrayNdx,
995                               stmt->value);
996     }
997     else if (!elem && (istreq(field, "name") ||
998                        istreq(field, "groupname"))) {
999         ret = SetGroupName(info, arrayNdx, stmt->value);
1000     }
1001     else if (!elem && (istreq(field, "groupswrap") ||
1002                        istreq(field, "wrapgroups"))) {
1003         log_err(info->ctx,
1004                 "Global \"groupswrap\" not supported; Ignored\n");
1005         ret = true;
1006     }
1007     else if (!elem && (istreq(field, "groupsclamp") ||
1008                        istreq(field, "clampgroups"))) {
1009         log_err(info->ctx,
1010                 "Global \"groupsclamp\" not supported; Ignored\n");
1011         ret = true;
1012     }
1013     else if (!elem && (istreq(field, "groupsredirect") ||
1014                        istreq(field, "redirectgroups"))) {
1015         log_err(info->ctx,
1016                 "Global \"groupsredirect\" not supported; Ignored\n");
1017         ret = true;
1018     }
1019     else if (!elem && istreq(field, "allownone")) {
1020         log_err(info->ctx,
1021                 "Radio groups not supported; "
1022                 "Ignoring \"allownone\" specification\n");
1023         ret = true;
1024     }
1025     else {
1026         ret = SetActionField(info->keymap, elem, field, arrayNdx, stmt->value,
1027                              info->actions);
1028     }
1029
1030     return ret;
1031 }
1032
1033 static bool
1034 HandleSymbolsBody(SymbolsInfo *info, VarDef *def, KeyInfo *keyi)
1035 {
1036     bool ok = true;
1037     const char *elem, *field;
1038     ExprDef *arrayNdx;
1039
1040     for (; def; def = (VarDef *) def->common.next) {
1041         if (def->name && def->name->expr.op == EXPR_FIELD_REF) {
1042             log_err(info->ctx,
1043                     "Cannot set a global default value from within a key statement; "
1044                     "Move statements to the global file scope\n");
1045             continue;
1046         }
1047
1048         if (!def->name) {
1049             if (!def->value || def->value->expr.op == EXPR_KEYSYM_LIST)
1050                 field = "symbols";
1051             else
1052                 field = "actions";
1053             arrayNdx = NULL;
1054         }
1055         else {
1056             ok = ExprResolveLhs(info->ctx, def->name, &elem, &field,
1057                                 &arrayNdx);
1058         }
1059
1060         if (ok)
1061             ok = SetSymbolsField(info, keyi, field, arrayNdx, def->value);
1062     }
1063
1064     return ok;
1065 }
1066
1067 static bool
1068 SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi)
1069 {
1070     xkb_layout_index_t i;
1071     GroupInfo *groupi;
1072     bool warn = false;
1073
1074     if (info->explicit_group == XKB_LAYOUT_INVALID)
1075         return true;
1076
1077     darray_enumerate_from(i, groupi, keyi->groups, 1) {
1078         if (groupi->defined) {
1079             warn = true;
1080             ClearGroupInfo(groupi);
1081             InitGroupInfo(groupi);
1082         }
1083     }
1084
1085     if (warn)
1086         log_warn(info->ctx,
1087                  "For the map %s an explicit group specified, "
1088                  "but key %s has more than one group defined; "
1089                  "All groups except first one will be ignored\n",
1090                  info->name, KeyInfoText(info, keyi));
1091
1092     darray_resize0(keyi->groups, info->explicit_group + 1);
1093     if (info->explicit_group > 0) {
1094         darray_item(keyi->groups, info->explicit_group) =
1095             darray_item(keyi->groups, 0);
1096         InitGroupInfo(&darray_item(keyi->groups, 0));
1097     }
1098
1099     return true;
1100 }
1101
1102 static bool
1103 HandleSymbolsDef(SymbolsInfo *info, SymbolsDef *stmt)
1104 {
1105     KeyInfo keyi;
1106
1107     keyi = info->default_key;
1108     darray_init(keyi.groups);
1109     darray_copy(keyi.groups, info->default_key.groups);
1110     for (xkb_layout_index_t i = 0; i < darray_size(keyi.groups); i++)
1111         CopyGroupInfo(&darray_item(keyi.groups, i),
1112                       &darray_item(info->default_key.groups, i));
1113     keyi.merge = stmt->merge;
1114     keyi.name = stmt->keyName;
1115
1116     if (!HandleSymbolsBody(info, stmt->symbols, &keyi)) {
1117         info->errorCount++;
1118         return false;
1119     }
1120
1121     if (!SetExplicitGroup(info, &keyi)) {
1122         info->errorCount++;
1123         return false;
1124     }
1125
1126     if (!AddKeySymbols(info, &keyi, true)) {
1127         info->errorCount++;
1128         return false;
1129     }
1130
1131     return true;
1132 }
1133
1134 static bool
1135 HandleModMapDef(SymbolsInfo *info, ModMapDef *def)
1136 {
1137     ModMapEntry tmp;
1138     xkb_mod_index_t ndx;
1139     bool ok;
1140     struct xkb_context *ctx = info->ctx;
1141
1142     ndx = XkbModNameToIndex(&info->keymap->mods, def->modifier, MOD_REAL);
1143     if (ndx == XKB_MOD_INVALID) {
1144         log_err(info->ctx,
1145                 "Illegal modifier map definition; "
1146                 "Ignoring map for non-modifier \"%s\"\n",
1147                 xkb_atom_text(ctx, def->modifier));
1148         return false;
1149     }
1150
1151     ok = true;
1152     tmp.modifier = ndx;
1153     tmp.merge = def->merge;
1154
1155     for (ExprDef *key = def->keys; key; key = (ExprDef *) key->common.next) {
1156         xkb_keysym_t sym;
1157
1158         if (key->expr.op == EXPR_VALUE &&
1159             key->expr.value_type == EXPR_TYPE_KEYNAME) {
1160             tmp.haveSymbol = false;
1161             tmp.u.keyName = key->key_name.key_name;
1162         }
1163         else if (ExprResolveKeySym(ctx, key, &sym)) {
1164             tmp.haveSymbol = true;
1165             tmp.u.keySym = sym;
1166         }
1167         else {
1168             log_err(info->ctx,
1169                     "Modmap entries may contain only key names or keysyms; "
1170                     "Illegal definition for %s modifier ignored\n",
1171                     ModIndexText(info->ctx, &info->keymap->mods, tmp.modifier));
1172             continue;
1173         }
1174
1175         ok = AddModMapEntry(info, &tmp) && ok;
1176     }
1177     return ok;
1178 }
1179
1180 static void
1181 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge)
1182 {
1183     bool ok;
1184
1185     free(info->name);
1186     info->name = strdup_safe(file->name);
1187
1188     for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
1189         switch (stmt->type) {
1190         case STMT_INCLUDE:
1191             ok = HandleIncludeSymbols(info, (IncludeStmt *) stmt);
1192             break;
1193         case STMT_SYMBOLS:
1194             ok = HandleSymbolsDef(info, (SymbolsDef *) stmt);
1195             break;
1196         case STMT_VAR:
1197             ok = HandleGlobalVar(info, (VarDef *) stmt);
1198             break;
1199         case STMT_VMOD:
1200             ok = HandleVModDef(info->keymap, (VModDef *) stmt, merge);
1201             break;
1202         case STMT_MODMAP:
1203             ok = HandleModMapDef(info, (ModMapDef *) stmt);
1204             break;
1205         default:
1206             log_err(info->ctx,
1207                     "Symbols files may not include other types; "
1208                     "Ignoring %s\n", stmt_type_to_string(stmt->type));
1209             ok = false;
1210             break;
1211         }
1212
1213         if (!ok)
1214             info->errorCount++;
1215
1216         if (info->errorCount > 10) {
1217             log_err(info->ctx, "Abandoning symbols file \"%s\"\n",
1218                     file->topName);
1219             break;
1220         }
1221     }
1222 }
1223
1224 /**
1225  * Given a keysym @sym, return a key which generates it, or NULL.
1226  * This is used for example in a modifier map definition, such as:
1227  *      modifier_map Lock           { Caps_Lock };
1228  * where we want to add the Lock modifier to the modmap of the key
1229  * which matches the keysym Caps_Lock.
1230  * Since there can be many keys which generates the keysym, the key
1231  * is chosen first by lowest group in which the keysym appears, than
1232  * by lowest level and than by lowest key code.
1233  */
1234 static struct xkb_key *
1235 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
1236 {
1237     struct xkb_key *key, *ret = NULL;
1238     xkb_layout_index_t group, min_group = UINT32_MAX;
1239     xkb_level_index_t level, min_level = UINT16_MAX;
1240
1241     xkb_foreach_key(key, keymap) {
1242         for (group = 0; group < key->num_groups; group++) {
1243             for (level = 0; level < XkbKeyGroupWidth(key, group); level++) {
1244                 if (key->groups[group].levels[level].num_syms != 1 ||
1245                     key->groups[group].levels[level].u.sym != sym)
1246                     continue;
1247
1248                 /*
1249                  * If the keysym was found in a group or level > 0, we must
1250                  * keep looking since we might find a key in which the keysym
1251                  * is in a lower group or level.
1252                  */
1253                 if (group < min_group ||
1254                     (group == min_group && level < min_level)) {
1255                     ret = key;
1256                     if (group == 0 && level == 0) {
1257                         return ret;
1258                     }
1259                     else {
1260                         min_group = group;
1261                         min_level = level;
1262                     }
1263                 }
1264             }
1265         }
1266     }
1267
1268     return ret;
1269 }
1270
1271 /*
1272  * Find an appropriate type for a group and return its name.
1273  *
1274  * Simple recipe:
1275  * - ONE_LEVEL for width 0/1
1276  * - ALPHABETIC for 2 shift levels, with lower/upercase keysyms
1277  * - KEYPAD for keypad keys.
1278  * - TWO_LEVEL for other 2 shift level keys.
1279  * and the same for four level keys.
1280  *
1281  * FIXME: Decide how to handle multiple-syms-per-level, and do it.
1282  */
1283 static xkb_atom_t
1284 FindAutomaticType(struct xkb_context *ctx, GroupInfo *groupi)
1285 {
1286     xkb_keysym_t sym0, sym1, sym2, sym3;
1287     xkb_level_index_t width = darray_size(groupi->levels);
1288
1289 #define GET_SYM(level) \
1290     (darray_item(groupi->levels, level).num_syms == 0 ? \
1291         XKB_KEY_NoSymbol : \
1292      darray_item(groupi->levels, level).num_syms == 1 ? \
1293         darray_item(groupi->levels, level).u.sym : \
1294      /* num_syms > 1 */ \
1295         darray_item(groupi->levels, level).u.syms[0])
1296
1297     if (width == 1 || width <= 0)
1298         return xkb_atom_intern_literal(ctx, "ONE_LEVEL");
1299
1300     sym0 = GET_SYM(0);
1301     sym1 = GET_SYM(1);
1302
1303     if (width == 2) {
1304         if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1))
1305             return xkb_atom_intern_literal(ctx, "ALPHABETIC");
1306
1307         if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1308             return xkb_atom_intern_literal(ctx, "KEYPAD");
1309
1310         return xkb_atom_intern_literal(ctx, "TWO_LEVEL");
1311     }
1312
1313     if (width <= 4) {
1314         if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1)) {
1315             sym2 = GET_SYM(2);
1316             sym3 = (width == 4 ? GET_SYM(3) : XKB_KEY_NoSymbol);
1317
1318             if (xkb_keysym_is_lower(sym2) && xkb_keysym_is_upper(sym3))
1319                 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_ALPHABETIC");
1320
1321             return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_SEMIALPHABETIC");
1322         }
1323
1324         if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1325             return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_KEYPAD");
1326
1327         return xkb_atom_intern_literal(ctx, "FOUR_LEVEL");
1328     }
1329
1330     return XKB_ATOM_NONE;
1331
1332 #undef GET_SYM
1333 }
1334
1335 static const struct xkb_key_type *
1336 FindTypeForGroup(struct xkb_keymap *keymap, KeyInfo *keyi,
1337                  xkb_layout_index_t group, bool *explicit_type)
1338 {
1339     unsigned int i;
1340     GroupInfo *groupi = &darray_item(keyi->groups, group);
1341     xkb_atom_t type_name = groupi->type;
1342
1343     *explicit_type = true;
1344
1345     if (type_name == XKB_ATOM_NONE) {
1346         if (keyi->default_type != XKB_ATOM_NONE) {
1347             type_name  = keyi->default_type;
1348         }
1349         else {
1350             type_name = FindAutomaticType(keymap->ctx, groupi);
1351             if (type_name != XKB_ATOM_NONE)
1352                 *explicit_type = false;
1353         }
1354     }
1355
1356     if (type_name == XKB_ATOM_NONE) {
1357         log_warn(keymap->ctx,
1358                  "Couldn't find an automatic type for key '%s' group %d with %lu levels; "
1359                  "Using the default type\n",
1360                  KeyNameText(keymap->ctx, keyi->name), group + 1,
1361                  (unsigned long) darray_size(groupi->levels));
1362         goto use_default;
1363     }
1364
1365     for (i = 0; i < keymap->num_types; i++)
1366         if (keymap->types[i].name == type_name)
1367             break;
1368
1369     if (i >= keymap->num_types) {
1370         log_warn(keymap->ctx,
1371                  "The type \"%s\" for key '%s' group %d was not previously defined; "
1372                  "Using the default type\n",
1373                  xkb_atom_text(keymap->ctx, type_name),
1374                  KeyNameText(keymap->ctx, keyi->name), group + 1);
1375         goto use_default;
1376     }
1377
1378     return &keymap->types[i];
1379
1380 use_default:
1381     /*
1382      * Index 0 is guaranteed to contain something, usually
1383      * ONE_LEVEL or at least some default one-level type.
1384      */
1385     return &keymap->types[0];
1386 }
1387
1388 static bool
1389 CopySymbolsDef(SymbolsInfo *info, KeyInfo *keyi)
1390 {
1391     struct xkb_keymap *keymap = info->keymap;
1392     struct xkb_key *key;
1393     GroupInfo *groupi;
1394     const GroupInfo *group0;
1395     xkb_layout_index_t i;
1396
1397     /*
1398      * The name is guaranteed to be real and not an alias (see
1399      * AddKeySymbols), so 'false' is safe here.
1400      */
1401     key = XkbKeyByName(keymap, keyi->name, false);
1402     if (!key) {
1403         log_vrb(info->ctx, 5,
1404                 "Key %s not found in keycodes; Symbols ignored\n",
1405                 KeyInfoText(info, keyi));
1406         return false;
1407     }
1408
1409     /* Find the range of groups we need. */
1410     key->num_groups = 0;
1411     darray_enumerate(i, groupi, keyi->groups)
1412         if (groupi->defined)
1413             key->num_groups = i + 1;
1414
1415     if (key->num_groups <= 0)
1416         return false; /* WSGO */
1417
1418     darray_resize(keyi->groups, key->num_groups);
1419
1420     /*
1421      * If there are empty groups between non-empty ones, fill them with data
1422      * from the first group.
1423      * We can make a wrong assumption here. But leaving gaps is worse.
1424      */
1425     group0 = &darray_item(keyi->groups, 0);
1426     darray_foreach_from(groupi, keyi->groups, 1) {
1427         if (groupi->defined)
1428             continue;
1429
1430         CopyGroupInfo(groupi, group0);
1431     }
1432
1433     key->groups = calloc(key->num_groups, sizeof(*key->groups));
1434
1435     /* Find and assign the groups' types in the keymap. */
1436     darray_enumerate(i, groupi, keyi->groups) {
1437         const struct xkb_key_type *type;
1438         bool explicit_type;
1439
1440         type = FindTypeForGroup(keymap, keyi, i, &explicit_type);
1441
1442         /* Always have as many levels as the type specifies. */
1443         if (type->num_levels < darray_size(groupi->levels)) {
1444             struct xkb_level *leveli;
1445
1446             log_vrb(info->ctx, 1,
1447                     "Type \"%s\" has %d levels, but %s has %d levels; "
1448                     "Ignoring extra symbols\n",
1449                     xkb_atom_text(keymap->ctx, type->name), type->num_levels,
1450                     KeyInfoText(info, keyi),
1451                     (int) darray_size(groupi->levels));
1452
1453             darray_foreach_from(leveli, groupi->levels, type->num_levels)
1454                 ClearLevelInfo(leveli);
1455         }
1456         darray_resize0(groupi->levels, type->num_levels);
1457
1458         key->groups[i].explicit_type = explicit_type;
1459         key->groups[i].type = type;
1460     }
1461
1462     /* Copy levels. */
1463     darray_enumerate(i, groupi, keyi->groups) {
1464         key->groups[i].levels = darray_mem(groupi->levels, 0);
1465         darray_init(groupi->levels);
1466     }
1467
1468     key->out_of_range_group_number = keyi->out_of_range_group_number;
1469     key->out_of_range_group_action = keyi->out_of_range_group_action;
1470
1471     if (keyi->defined & KEY_FIELD_VMODMAP) {
1472         key->vmodmap = keyi->vmodmap;
1473         key->explicit |= EXPLICIT_VMODMAP;
1474     }
1475
1476     if (keyi->repeat != KEY_REPEAT_UNDEFINED) {
1477         key->repeats = (keyi->repeat == KEY_REPEAT_YES);
1478         key->explicit |= EXPLICIT_REPEAT;
1479     }
1480
1481     darray_foreach(groupi, keyi->groups) {
1482         if (groupi->defined & GROUP_FIELD_ACTS) {
1483             key->explicit |= EXPLICIT_INTERP;
1484             break;
1485         }
1486     }
1487
1488     return true;
1489 }
1490
1491 static bool
1492 CopyModMapDef(SymbolsInfo *info, ModMapEntry *entry)
1493 {
1494     struct xkb_key *key;
1495     struct xkb_keymap *keymap = info->keymap;
1496
1497     if (!entry->haveSymbol) {
1498         key = XkbKeyByName(keymap, entry->u.keyName, true);
1499         if (!key) {
1500             log_vrb(info->ctx, 5,
1501                     "Key %s not found in keycodes; "
1502                     "Modifier map entry for %s not updated\n",
1503                     KeyNameText(keymap->ctx, entry->u.keyName),
1504                     ModIndexText(info->ctx, &info->keymap->mods, entry->modifier));
1505             return false;
1506         }
1507     }
1508     else {
1509         key = FindKeyForSymbol(keymap, entry->u.keySym);
1510         if (!key) {
1511             log_vrb(info->ctx, 5,
1512                     "Key \"%s\" not found in symbol map; "
1513                     "Modifier map entry for %s not updated\n",
1514                     KeysymText(info->ctx, entry->u.keySym),
1515                     ModIndexText(info->ctx, &info->keymap->mods, entry->modifier));
1516             return false;
1517         }
1518     }
1519
1520     key->modmap |= (1u << entry->modifier);
1521     return true;
1522 }
1523
1524 static bool
1525 CopySymbolsToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info)
1526 {
1527     KeyInfo *keyi;
1528     ModMapEntry *mm;
1529
1530     keymap->symbols_section_name = strdup_safe(info->name);
1531     XkbEscapeMapName(keymap->symbols_section_name);
1532
1533     keymap->num_group_names = darray_size(info->group_names);
1534     keymap->group_names = darray_mem(info->group_names, 0);
1535     darray_init(info->group_names);
1536
1537     darray_foreach(keyi, info->keys)
1538         if (!CopySymbolsDef(info, keyi))
1539             info->errorCount++;
1540
1541     if (xkb_context_get_log_verbosity(keymap->ctx) > 3) {
1542         struct xkb_key *key;
1543
1544         xkb_foreach_key(key, keymap) {
1545             if (key->name == XKB_ATOM_NONE)
1546                 continue;
1547
1548             if (key->num_groups < 1)
1549                 log_info(keymap->ctx,
1550                          "No symbols defined for %s\n",
1551                          KeyNameText(keymap->ctx, key->name));
1552         }
1553     }
1554
1555     darray_foreach(mm, info->modmaps)
1556         if (!CopyModMapDef(info, mm))
1557             info->errorCount++;
1558
1559     /* XXX: If we don't ignore errorCount, things break. */
1560     return true;
1561 }
1562
1563 bool
1564 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1565                enum merge_mode merge)
1566 {
1567     SymbolsInfo info;
1568     ActionsInfo *actions;
1569
1570     actions = NewActionsInfo();
1571     if (!actions)
1572         return false;
1573
1574     InitSymbolsInfo(&info, keymap, actions);
1575     info.default_key.merge = merge;
1576
1577     HandleSymbolsFile(&info, file, merge);
1578
1579     if (info.errorCount != 0)
1580         goto err_info;
1581
1582     if (!CopySymbolsToKeymap(keymap, &info))
1583         goto err_info;
1584
1585     ClearSymbolsInfo(&info);
1586     FreeActionsInfo(actions);
1587     return true;
1588
1589 err_info:
1590     FreeActionsInfo(actions);
1591     ClearSymbolsInfo(&info);
1592     return false;
1593 }