vmod: 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(info->ctx, info->actions, &info->keymap->mods,
758                              act, toAct))
759             log_err(info->ctx,
760                     "Illegal action definition for %s; "
761                     "Action for group %u/level %u ignored\n",
762                     KeyInfoText(info, keyi), ndx + 1, i + 1);
763
764         act = (ExprDef *) act->common.next;
765     }
766
767     return true;
768 }
769
770 static const LookupEntry repeatEntries[] = {
771     { "true", KEY_REPEAT_YES },
772     { "yes", KEY_REPEAT_YES },
773     { "on", KEY_REPEAT_YES },
774     { "false", KEY_REPEAT_NO },
775     { "no", KEY_REPEAT_NO },
776     { "off", KEY_REPEAT_NO },
777     { "default", KEY_REPEAT_UNDEFINED },
778     { NULL, 0 }
779 };
780
781 static bool
782 SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
783                 ExprDef *arrayNdx, ExprDef *value)
784 {
785     if (istreq(field, "type")) {
786         xkb_layout_index_t ndx;
787         xkb_atom_t val;
788
789         if (!ExprResolveString(info->ctx, value, &val)) {
790             log_err(info->ctx,
791                     "The type field of a key symbol map must be a string; "
792                     "Ignoring illegal type definition\n");
793             return false;
794         }
795
796         if (!arrayNdx) {
797             keyi->default_type = val;
798             keyi->defined |= KEY_FIELD_DEFAULT_TYPE;
799         }
800         else if (!ExprResolveGroup(info->ctx, arrayNdx, &ndx)) {
801             log_err(info->ctx,
802                     "Illegal group index for type of key %s; "
803                     "Definition with non-integer array index ignored\n",
804                     KeyInfoText(info, keyi));
805             return false;
806         }
807         else {
808             ndx--;
809             if (ndx >= darray_size(keyi->groups))
810                 darray_resize0(keyi->groups, ndx + 1);
811             darray_item(keyi->groups, ndx).type = val;
812             darray_item(keyi->groups, ndx).defined |= GROUP_FIELD_TYPE;
813         }
814     }
815     else if (istreq(field, "symbols")) {
816         return AddSymbolsToKey(info, keyi, arrayNdx, value);
817     }
818     else if (istreq(field, "actions")) {
819         return AddActionsToKey(info, keyi, arrayNdx, value);
820     }
821     else if (istreq(field, "vmods") ||
822              istreq(field, "virtualmods") ||
823              istreq(field, "virtualmodifiers")) {
824         xkb_mod_mask_t mask;
825
826         if (!ExprResolveModMask(info->ctx, value, MOD_VIRT,
827                                 &info->keymap->mods, &mask)) {
828             log_err(info->ctx,
829                     "Expected a virtual modifier mask, found %s; "
830                     "Ignoring virtual modifiers definition for key %s\n",
831                     expr_op_type_to_string(value->expr.op),
832                     KeyInfoText(info, keyi));
833             return false;
834         }
835
836         keyi->vmodmap = mask;
837         keyi->defined |= KEY_FIELD_VMODMAP;
838     }
839     else if (istreq(field, "locking") ||
840              istreq(field, "lock") ||
841              istreq(field, "locks")) {
842         log_vrb(info->ctx, 1,
843                 "Key behaviors not supported; "
844                 "Ignoring locking specification for key %s\n",
845                 KeyInfoText(info, keyi));
846     }
847     else if (istreq(field, "radiogroup") ||
848              istreq(field, "permanentradiogroup") ||
849              istreq(field, "allownone")) {
850         log_vrb(info->ctx, 1,
851                 "Radio groups not supported; "
852                 "Ignoring radio group specification for key %s\n",
853                 KeyInfoText(info, keyi));
854     }
855     else if (istreq_prefix("overlay", field) ||
856              istreq_prefix("permanentoverlay", field)) {
857         log_vrb(info->ctx, 1,
858                 "Overlays not supported; "
859                 "Ignoring overlay specification for key %s\n",
860                 KeyInfoText(info, keyi));
861     }
862     else if (istreq(field, "repeating") ||
863              istreq(field, "repeats") ||
864              istreq(field, "repeat")) {
865         unsigned int val;
866
867         if (!ExprResolveEnum(info->ctx, value, &val, repeatEntries)) {
868             log_err(info->ctx,
869                     "Illegal repeat setting for %s; "
870                     "Non-boolean repeat setting ignored\n",
871                     KeyInfoText(info, keyi));
872             return false;
873         }
874
875         keyi->repeat = val;
876         keyi->defined |= KEY_FIELD_REPEAT;
877     }
878     else if (istreq(field, "groupswrap") ||
879              istreq(field, "wrapgroups")) {
880         bool set;
881
882         if (!ExprResolveBoolean(info->ctx, value, &set)) {
883             log_err(info->ctx,
884                     "Illegal groupsWrap setting for %s; "
885                     "Non-boolean value ignored\n",
886                     KeyInfoText(info, keyi));
887             return false;
888         }
889
890         keyi->out_of_range_group_action = (set ? RANGE_WRAP : RANGE_SATURATE);
891         keyi->defined |= KEY_FIELD_GROUPINFO;
892     }
893     else if (istreq(field, "groupsclamp") ||
894              istreq(field, "clampgroups")) {
895         bool set;
896
897         if (!ExprResolveBoolean(info->ctx, value, &set)) {
898             log_err(info->ctx,
899                     "Illegal groupsClamp setting for %s; "
900                     "Non-boolean value ignored\n",
901                     KeyInfoText(info, keyi));
902             return false;
903         }
904
905         keyi->out_of_range_group_action = (set ? RANGE_SATURATE : RANGE_WRAP);
906         keyi->defined |= KEY_FIELD_GROUPINFO;
907     }
908     else if (istreq(field, "groupsredirect") ||
909              istreq(field, "redirectgroups")) {
910         xkb_layout_index_t grp;
911
912         if (!ExprResolveGroup(info->ctx, value, &grp)) {
913             log_err(info->ctx,
914                     "Illegal group index for redirect of key %s; "
915                     "Definition with non-integer group ignored\n",
916                     KeyInfoText(info, keyi));
917             return false;
918         }
919
920         keyi->out_of_range_group_action = RANGE_REDIRECT;
921         keyi->out_of_range_group_number = grp - 1;
922         keyi->defined |= KEY_FIELD_GROUPINFO;
923     }
924     else {
925         log_err(info->ctx,
926                 "Unknown field %s in a symbol interpretation; "
927                 "Definition ignored\n",
928                 field);
929         return false;
930     }
931
932     return true;
933 }
934
935 static bool
936 SetGroupName(SymbolsInfo *info, ExprDef *arrayNdx, ExprDef *value)
937 {
938     xkb_layout_index_t group, group_to_use;
939     xkb_atom_t name;
940
941     if (!arrayNdx) {
942         log_vrb(info->ctx, 1,
943                 "You must specify an index when specifying a group name; "
944                 "Group name definition without array subscript ignored\n");
945         return false;
946     }
947
948     if (!ExprResolveGroup(info->ctx, arrayNdx, &group)) {
949         log_err(info->ctx,
950                 "Illegal index in group name definition; "
951                 "Definition with non-integer array index ignored\n");
952         return false;
953     }
954
955     if (!ExprResolveString(info->ctx, value, &name)) {
956         log_err(info->ctx,
957                 "Group name must be a string; "
958                 "Illegal name for group %d ignored\n", group);
959         return false;
960     }
961
962     if (info->explicit_group == XKB_LAYOUT_INVALID) {
963         group_to_use = group - 1;
964     }
965     else if (group - 1 == 0) {
966         group_to_use = info->explicit_group;
967     }
968     else {
969         log_warn(info->ctx,
970                  "An explicit group was specified for the '%s' map, "
971                  "but it provides a name for a group other than Group1 (%d); "
972                  "Ignoring group name '%s'\n",
973                  info->name, group,
974                  xkb_atom_text(info->ctx, name));
975         return false;
976     }
977
978     if (group_to_use >= darray_size(info->group_names))
979         darray_resize0(info->group_names, group_to_use + 1);
980     darray_item(info->group_names, group_to_use) = name;
981
982     return true;
983 }
984
985 static bool
986 HandleGlobalVar(SymbolsInfo *info, VarDef *stmt)
987 {
988     const char *elem, *field;
989     ExprDef *arrayNdx;
990     bool ret;
991
992     if (!ExprResolveLhs(info->ctx, stmt->name, &elem, &field, &arrayNdx))
993         return false;
994
995     if (elem && istreq(elem, "key")) {
996         ret = SetSymbolsField(info, &info->default_key, field, arrayNdx,
997                               stmt->value);
998     }
999     else if (!elem && (istreq(field, "name") ||
1000                        istreq(field, "groupname"))) {
1001         ret = SetGroupName(info, arrayNdx, stmt->value);
1002     }
1003     else if (!elem && (istreq(field, "groupswrap") ||
1004                        istreq(field, "wrapgroups"))) {
1005         log_err(info->ctx,
1006                 "Global \"groupswrap\" not supported; Ignored\n");
1007         ret = true;
1008     }
1009     else if (!elem && (istreq(field, "groupsclamp") ||
1010                        istreq(field, "clampgroups"))) {
1011         log_err(info->ctx,
1012                 "Global \"groupsclamp\" not supported; Ignored\n");
1013         ret = true;
1014     }
1015     else if (!elem && (istreq(field, "groupsredirect") ||
1016                        istreq(field, "redirectgroups"))) {
1017         log_err(info->ctx,
1018                 "Global \"groupsredirect\" not supported; Ignored\n");
1019         ret = true;
1020     }
1021     else if (!elem && istreq(field, "allownone")) {
1022         log_err(info->ctx,
1023                 "Radio groups not supported; "
1024                 "Ignoring \"allownone\" specification\n");
1025         ret = true;
1026     }
1027     else {
1028         ret = SetActionField(info->ctx, info->actions, &info->keymap->mods,
1029                              elem, field, arrayNdx, stmt->value);
1030     }
1031
1032     return ret;
1033 }
1034
1035 static bool
1036 HandleSymbolsBody(SymbolsInfo *info, VarDef *def, KeyInfo *keyi)
1037 {
1038     bool ok = true;
1039     const char *elem, *field;
1040     ExprDef *arrayNdx;
1041
1042     for (; def; def = (VarDef *) def->common.next) {
1043         if (def->name && def->name->expr.op == EXPR_FIELD_REF) {
1044             log_err(info->ctx,
1045                     "Cannot set a global default value from within a key statement; "
1046                     "Move statements to the global file scope\n");
1047             continue;
1048         }
1049
1050         if (!def->name) {
1051             if (!def->value || def->value->expr.op == EXPR_KEYSYM_LIST)
1052                 field = "symbols";
1053             else
1054                 field = "actions";
1055             arrayNdx = NULL;
1056         }
1057         else {
1058             ok = ExprResolveLhs(info->ctx, def->name, &elem, &field,
1059                                 &arrayNdx);
1060         }
1061
1062         if (ok)
1063             ok = SetSymbolsField(info, keyi, field, arrayNdx, def->value);
1064     }
1065
1066     return ok;
1067 }
1068
1069 static bool
1070 SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi)
1071 {
1072     xkb_layout_index_t i;
1073     GroupInfo *groupi;
1074     bool warn = false;
1075
1076     if (info->explicit_group == XKB_LAYOUT_INVALID)
1077         return true;
1078
1079     darray_enumerate_from(i, groupi, keyi->groups, 1) {
1080         if (groupi->defined) {
1081             warn = true;
1082             ClearGroupInfo(groupi);
1083             InitGroupInfo(groupi);
1084         }
1085     }
1086
1087     if (warn)
1088         log_warn(info->ctx,
1089                  "For the map %s an explicit group specified, "
1090                  "but key %s has more than one group defined; "
1091                  "All groups except first one will be ignored\n",
1092                  info->name, KeyInfoText(info, keyi));
1093
1094     darray_resize0(keyi->groups, info->explicit_group + 1);
1095     if (info->explicit_group > 0) {
1096         darray_item(keyi->groups, info->explicit_group) =
1097             darray_item(keyi->groups, 0);
1098         InitGroupInfo(&darray_item(keyi->groups, 0));
1099     }
1100
1101     return true;
1102 }
1103
1104 static bool
1105 HandleSymbolsDef(SymbolsInfo *info, SymbolsDef *stmt)
1106 {
1107     KeyInfo keyi;
1108
1109     keyi = info->default_key;
1110     darray_init(keyi.groups);
1111     darray_copy(keyi.groups, info->default_key.groups);
1112     for (xkb_layout_index_t i = 0; i < darray_size(keyi.groups); i++)
1113         CopyGroupInfo(&darray_item(keyi.groups, i),
1114                       &darray_item(info->default_key.groups, i));
1115     keyi.merge = stmt->merge;
1116     keyi.name = stmt->keyName;
1117
1118     if (!HandleSymbolsBody(info, stmt->symbols, &keyi)) {
1119         info->errorCount++;
1120         return false;
1121     }
1122
1123     if (!SetExplicitGroup(info, &keyi)) {
1124         info->errorCount++;
1125         return false;
1126     }
1127
1128     if (!AddKeySymbols(info, &keyi, true)) {
1129         info->errorCount++;
1130         return false;
1131     }
1132
1133     return true;
1134 }
1135
1136 static bool
1137 HandleModMapDef(SymbolsInfo *info, ModMapDef *def)
1138 {
1139     ModMapEntry tmp;
1140     xkb_mod_index_t ndx;
1141     bool ok;
1142     struct xkb_context *ctx = info->ctx;
1143
1144     ndx = XkbModNameToIndex(&info->keymap->mods, def->modifier, MOD_REAL);
1145     if (ndx == XKB_MOD_INVALID) {
1146         log_err(info->ctx,
1147                 "Illegal modifier map definition; "
1148                 "Ignoring map for non-modifier \"%s\"\n",
1149                 xkb_atom_text(ctx, def->modifier));
1150         return false;
1151     }
1152
1153     ok = true;
1154     tmp.modifier = ndx;
1155     tmp.merge = def->merge;
1156
1157     for (ExprDef *key = def->keys; key; key = (ExprDef *) key->common.next) {
1158         xkb_keysym_t sym;
1159
1160         if (key->expr.op == EXPR_VALUE &&
1161             key->expr.value_type == EXPR_TYPE_KEYNAME) {
1162             tmp.haveSymbol = false;
1163             tmp.u.keyName = key->key_name.key_name;
1164         }
1165         else if (ExprResolveKeySym(ctx, key, &sym)) {
1166             tmp.haveSymbol = true;
1167             tmp.u.keySym = sym;
1168         }
1169         else {
1170             log_err(info->ctx,
1171                     "Modmap entries may contain only key names or keysyms; "
1172                     "Illegal definition for %s modifier ignored\n",
1173                     ModIndexText(info->ctx, &info->keymap->mods, tmp.modifier));
1174             continue;
1175         }
1176
1177         ok = AddModMapEntry(info, &tmp) && ok;
1178     }
1179     return ok;
1180 }
1181
1182 static void
1183 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge)
1184 {
1185     bool ok;
1186
1187     free(info->name);
1188     info->name = strdup_safe(file->name);
1189
1190     for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
1191         switch (stmt->type) {
1192         case STMT_INCLUDE:
1193             ok = HandleIncludeSymbols(info, (IncludeStmt *) stmt);
1194             break;
1195         case STMT_SYMBOLS:
1196             ok = HandleSymbolsDef(info, (SymbolsDef *) stmt);
1197             break;
1198         case STMT_VAR:
1199             ok = HandleGlobalVar(info, (VarDef *) stmt);
1200             break;
1201         case STMT_VMOD:
1202             ok = HandleVModDef(info->ctx, &info->keymap->mods,
1203                                (VModDef *) stmt, merge);
1204             break;
1205         case STMT_MODMAP:
1206             ok = HandleModMapDef(info, (ModMapDef *) stmt);
1207             break;
1208         default:
1209             log_err(info->ctx,
1210                     "Symbols files may not include other types; "
1211                     "Ignoring %s\n", stmt_type_to_string(stmt->type));
1212             ok = false;
1213             break;
1214         }
1215
1216         if (!ok)
1217             info->errorCount++;
1218
1219         if (info->errorCount > 10) {
1220             log_err(info->ctx, "Abandoning symbols file \"%s\"\n",
1221                     file->topName);
1222             break;
1223         }
1224     }
1225 }
1226
1227 /**
1228  * Given a keysym @sym, return a key which generates it, or NULL.
1229  * This is used for example in a modifier map definition, such as:
1230  *      modifier_map Lock           { Caps_Lock };
1231  * where we want to add the Lock modifier to the modmap of the key
1232  * which matches the keysym Caps_Lock.
1233  * Since there can be many keys which generates the keysym, the key
1234  * is chosen first by lowest group in which the keysym appears, than
1235  * by lowest level and than by lowest key code.
1236  */
1237 static struct xkb_key *
1238 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
1239 {
1240     struct xkb_key *key, *ret = NULL;
1241     xkb_layout_index_t group, min_group = UINT32_MAX;
1242     xkb_level_index_t level, min_level = UINT16_MAX;
1243
1244     xkb_foreach_key(key, keymap) {
1245         for (group = 0; group < key->num_groups; group++) {
1246             for (level = 0; level < XkbKeyGroupWidth(key, group); level++) {
1247                 if (key->groups[group].levels[level].num_syms != 1 ||
1248                     key->groups[group].levels[level].u.sym != sym)
1249                     continue;
1250
1251                 /*
1252                  * If the keysym was found in a group or level > 0, we must
1253                  * keep looking since we might find a key in which the keysym
1254                  * is in a lower group or level.
1255                  */
1256                 if (group < min_group ||
1257                     (group == min_group && level < min_level)) {
1258                     ret = key;
1259                     if (group == 0 && level == 0) {
1260                         return ret;
1261                     }
1262                     else {
1263                         min_group = group;
1264                         min_level = level;
1265                     }
1266                 }
1267             }
1268         }
1269     }
1270
1271     return ret;
1272 }
1273
1274 /*
1275  * Find an appropriate type for a group and return its name.
1276  *
1277  * Simple recipe:
1278  * - ONE_LEVEL for width 0/1
1279  * - ALPHABETIC for 2 shift levels, with lower/upercase keysyms
1280  * - KEYPAD for keypad keys.
1281  * - TWO_LEVEL for other 2 shift level keys.
1282  * and the same for four level keys.
1283  *
1284  * FIXME: Decide how to handle multiple-syms-per-level, and do it.
1285  */
1286 static xkb_atom_t
1287 FindAutomaticType(struct xkb_context *ctx, GroupInfo *groupi)
1288 {
1289     xkb_keysym_t sym0, sym1, sym2, sym3;
1290     xkb_level_index_t width = darray_size(groupi->levels);
1291
1292 #define GET_SYM(level) \
1293     (darray_item(groupi->levels, level).num_syms == 0 ? \
1294         XKB_KEY_NoSymbol : \
1295      darray_item(groupi->levels, level).num_syms == 1 ? \
1296         darray_item(groupi->levels, level).u.sym : \
1297      /* num_syms > 1 */ \
1298         darray_item(groupi->levels, level).u.syms[0])
1299
1300     if (width == 1 || width <= 0)
1301         return xkb_atom_intern_literal(ctx, "ONE_LEVEL");
1302
1303     sym0 = GET_SYM(0);
1304     sym1 = GET_SYM(1);
1305
1306     if (width == 2) {
1307         if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1))
1308             return xkb_atom_intern_literal(ctx, "ALPHABETIC");
1309
1310         if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1311             return xkb_atom_intern_literal(ctx, "KEYPAD");
1312
1313         return xkb_atom_intern_literal(ctx, "TWO_LEVEL");
1314     }
1315
1316     if (width <= 4) {
1317         if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1)) {
1318             sym2 = GET_SYM(2);
1319             sym3 = (width == 4 ? GET_SYM(3) : XKB_KEY_NoSymbol);
1320
1321             if (xkb_keysym_is_lower(sym2) && xkb_keysym_is_upper(sym3))
1322                 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_ALPHABETIC");
1323
1324             return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_SEMIALPHABETIC");
1325         }
1326
1327         if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1328             return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_KEYPAD");
1329
1330         return xkb_atom_intern_literal(ctx, "FOUR_LEVEL");
1331     }
1332
1333     return XKB_ATOM_NONE;
1334
1335 #undef GET_SYM
1336 }
1337
1338 static const struct xkb_key_type *
1339 FindTypeForGroup(struct xkb_keymap *keymap, KeyInfo *keyi,
1340                  xkb_layout_index_t group, bool *explicit_type)
1341 {
1342     unsigned int i;
1343     GroupInfo *groupi = &darray_item(keyi->groups, group);
1344     xkb_atom_t type_name = groupi->type;
1345
1346     *explicit_type = true;
1347
1348     if (type_name == XKB_ATOM_NONE) {
1349         if (keyi->default_type != XKB_ATOM_NONE) {
1350             type_name  = keyi->default_type;
1351         }
1352         else {
1353             type_name = FindAutomaticType(keymap->ctx, groupi);
1354             if (type_name != XKB_ATOM_NONE)
1355                 *explicit_type = false;
1356         }
1357     }
1358
1359     if (type_name == XKB_ATOM_NONE) {
1360         log_warn(keymap->ctx,
1361                  "Couldn't find an automatic type for key '%s' group %d with %lu levels; "
1362                  "Using the default type\n",
1363                  KeyNameText(keymap->ctx, keyi->name), group + 1,
1364                  (unsigned long) darray_size(groupi->levels));
1365         goto use_default;
1366     }
1367
1368     for (i = 0; i < keymap->num_types; i++)
1369         if (keymap->types[i].name == type_name)
1370             break;
1371
1372     if (i >= keymap->num_types) {
1373         log_warn(keymap->ctx,
1374                  "The type \"%s\" for key '%s' group %d was not previously defined; "
1375                  "Using the default type\n",
1376                  xkb_atom_text(keymap->ctx, type_name),
1377                  KeyNameText(keymap->ctx, keyi->name), group + 1);
1378         goto use_default;
1379     }
1380
1381     return &keymap->types[i];
1382
1383 use_default:
1384     /*
1385      * Index 0 is guaranteed to contain something, usually
1386      * ONE_LEVEL or at least some default one-level type.
1387      */
1388     return &keymap->types[0];
1389 }
1390
1391 static bool
1392 CopySymbolsDef(SymbolsInfo *info, KeyInfo *keyi)
1393 {
1394     struct xkb_keymap *keymap = info->keymap;
1395     struct xkb_key *key;
1396     GroupInfo *groupi;
1397     const GroupInfo *group0;
1398     xkb_layout_index_t i;
1399
1400     /*
1401      * The name is guaranteed to be real and not an alias (see
1402      * AddKeySymbols), so 'false' is safe here.
1403      */
1404     key = XkbKeyByName(keymap, keyi->name, false);
1405     if (!key) {
1406         log_vrb(info->ctx, 5,
1407                 "Key %s not found in keycodes; Symbols ignored\n",
1408                 KeyInfoText(info, keyi));
1409         return false;
1410     }
1411
1412     /* Find the range of groups we need. */
1413     key->num_groups = 0;
1414     darray_enumerate(i, groupi, keyi->groups)
1415         if (groupi->defined)
1416             key->num_groups = i + 1;
1417
1418     if (key->num_groups <= 0)
1419         return false; /* WSGO */
1420
1421     darray_resize(keyi->groups, key->num_groups);
1422
1423     /*
1424      * If there are empty groups between non-empty ones, fill them with data
1425      * from the first group.
1426      * We can make a wrong assumption here. But leaving gaps is worse.
1427      */
1428     group0 = &darray_item(keyi->groups, 0);
1429     darray_foreach_from(groupi, keyi->groups, 1) {
1430         if (groupi->defined)
1431             continue;
1432
1433         CopyGroupInfo(groupi, group0);
1434     }
1435
1436     key->groups = calloc(key->num_groups, sizeof(*key->groups));
1437
1438     /* Find and assign the groups' types in the keymap. */
1439     darray_enumerate(i, groupi, keyi->groups) {
1440         const struct xkb_key_type *type;
1441         bool explicit_type;
1442
1443         type = FindTypeForGroup(keymap, keyi, i, &explicit_type);
1444
1445         /* Always have as many levels as the type specifies. */
1446         if (type->num_levels < darray_size(groupi->levels)) {
1447             struct xkb_level *leveli;
1448
1449             log_vrb(info->ctx, 1,
1450                     "Type \"%s\" has %d levels, but %s has %d levels; "
1451                     "Ignoring extra symbols\n",
1452                     xkb_atom_text(keymap->ctx, type->name), type->num_levels,
1453                     KeyInfoText(info, keyi),
1454                     (int) darray_size(groupi->levels));
1455
1456             darray_foreach_from(leveli, groupi->levels, type->num_levels)
1457                 ClearLevelInfo(leveli);
1458         }
1459         darray_resize0(groupi->levels, type->num_levels);
1460
1461         key->groups[i].explicit_type = explicit_type;
1462         key->groups[i].type = type;
1463     }
1464
1465     /* Copy levels. */
1466     darray_enumerate(i, groupi, keyi->groups) {
1467         key->groups[i].levels = darray_mem(groupi->levels, 0);
1468         darray_init(groupi->levels);
1469     }
1470
1471     key->out_of_range_group_number = keyi->out_of_range_group_number;
1472     key->out_of_range_group_action = keyi->out_of_range_group_action;
1473
1474     if (keyi->defined & KEY_FIELD_VMODMAP) {
1475         key->vmodmap = keyi->vmodmap;
1476         key->explicit |= EXPLICIT_VMODMAP;
1477     }
1478
1479     if (keyi->repeat != KEY_REPEAT_UNDEFINED) {
1480         key->repeats = (keyi->repeat == KEY_REPEAT_YES);
1481         key->explicit |= EXPLICIT_REPEAT;
1482     }
1483
1484     darray_foreach(groupi, keyi->groups) {
1485         if (groupi->defined & GROUP_FIELD_ACTS) {
1486             key->explicit |= EXPLICIT_INTERP;
1487             break;
1488         }
1489     }
1490
1491     return true;
1492 }
1493
1494 static bool
1495 CopyModMapDef(SymbolsInfo *info, ModMapEntry *entry)
1496 {
1497     struct xkb_key *key;
1498     struct xkb_keymap *keymap = info->keymap;
1499
1500     if (!entry->haveSymbol) {
1501         key = XkbKeyByName(keymap, entry->u.keyName, true);
1502         if (!key) {
1503             log_vrb(info->ctx, 5,
1504                     "Key %s not found in keycodes; "
1505                     "Modifier map entry for %s not updated\n",
1506                     KeyNameText(keymap->ctx, entry->u.keyName),
1507                     ModIndexText(info->ctx, &info->keymap->mods, entry->modifier));
1508             return false;
1509         }
1510     }
1511     else {
1512         key = FindKeyForSymbol(keymap, entry->u.keySym);
1513         if (!key) {
1514             log_vrb(info->ctx, 5,
1515                     "Key \"%s\" not found in symbol map; "
1516                     "Modifier map entry for %s not updated\n",
1517                     KeysymText(info->ctx, entry->u.keySym),
1518                     ModIndexText(info->ctx, &info->keymap->mods, entry->modifier));
1519             return false;
1520         }
1521     }
1522
1523     key->modmap |= (1u << entry->modifier);
1524     return true;
1525 }
1526
1527 static bool
1528 CopySymbolsToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info)
1529 {
1530     KeyInfo *keyi;
1531     ModMapEntry *mm;
1532
1533     keymap->symbols_section_name = strdup_safe(info->name);
1534     XkbEscapeMapName(keymap->symbols_section_name);
1535
1536     keymap->num_group_names = darray_size(info->group_names);
1537     keymap->group_names = darray_mem(info->group_names, 0);
1538     darray_init(info->group_names);
1539
1540     darray_foreach(keyi, info->keys)
1541         if (!CopySymbolsDef(info, keyi))
1542             info->errorCount++;
1543
1544     if (xkb_context_get_log_verbosity(keymap->ctx) > 3) {
1545         struct xkb_key *key;
1546
1547         xkb_foreach_key(key, keymap) {
1548             if (key->name == XKB_ATOM_NONE)
1549                 continue;
1550
1551             if (key->num_groups < 1)
1552                 log_info(keymap->ctx,
1553                          "No symbols defined for %s\n",
1554                          KeyNameText(keymap->ctx, key->name));
1555         }
1556     }
1557
1558     darray_foreach(mm, info->modmaps)
1559         if (!CopyModMapDef(info, mm))
1560             info->errorCount++;
1561
1562     /* XXX: If we don't ignore errorCount, things break. */
1563     return true;
1564 }
1565
1566 bool
1567 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1568                enum merge_mode merge)
1569 {
1570     SymbolsInfo info;
1571     ActionsInfo *actions;
1572
1573     actions = NewActionsInfo();
1574     if (!actions)
1575         return false;
1576
1577     InitSymbolsInfo(&info, keymap, actions);
1578     info.default_key.merge = merge;
1579
1580     HandleSymbolsFile(&info, file, merge);
1581
1582     if (info.errorCount != 0)
1583         goto err_info;
1584
1585     if (!CopySymbolsToKeymap(keymap, &info))
1586         goto err_info;
1587
1588     ClearSymbolsInfo(&info);
1589     FreeActionsInfo(actions);
1590     return true;
1591
1592 err_info:
1593     FreeActionsInfo(actions);
1594     ClearSymbolsInfo(&info);
1595     return false;
1596 }