Handle key names consistently
[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 #include "xkbcomp-priv.h"
28 #include "parseutils.h"
29 #include "action.h"
30 #include "vmod.h"
31
32 /***====================================================================***/
33
34 /* Needed to work with the typechecker. */
35 typedef darray(xkb_keysym_t) darray_xkb_keysym_t;
36 typedef darray(union xkb_action) darray_xkb_action;
37
38 enum key_repeat {
39     KEY_REPEAT_YES = 1,
40     KEY_REPEAT_NO = 0,
41     KEY_REPEAT_UNDEFINED = -1
42 };
43
44 enum key_field {
45     KEY_FIELD_SYMS      = (1 << 0),
46     KEY_FIELD_ACTS      = (1 << 1),
47     KEY_FIELD_REPEAT    = (1 << 2),
48     KEY_FIELD_BEHAVIOR  = (1 << 3),
49     KEY_FIELD_TYPE_DFLT = (1 << 4),
50     KEY_FIELD_TYPES     = (1 << 5),
51     KEY_FIELD_GROUPINFO = (1 << 6),
52     KEY_FIELD_VMODMAP   = (1 << 7),
53 };
54
55 typedef struct _KeyInfo {
56     enum key_field defined;
57     unsigned file_id;
58     enum merge_mode merge;
59
60     unsigned long name; /* the 4 chars of the key name, as long */
61     unsigned char typesDefined;
62     unsigned char symsDefined;
63     unsigned char actsDefined;
64     xkb_level_index_t numLevels[XkbNumKbdGroups];
65
66     /* syms[group] -> Single array for all the keysyms in the group. */
67     darray_xkb_keysym_t syms[XkbNumKbdGroups];
68     /*
69      * symsMapIndex[group][level] -> The index from which the syms for
70      * the level begin in the syms[group] array. Remember each keycode
71      * can have multiple keysyms in each level (that is, each key press
72      * can result in multiple keysyms).
73      */
74     darray(int) symsMapIndex[XkbNumKbdGroups];
75     /*
76      * symsMapNumEntries[group][level] -> How many syms are in
77      * syms[group][symsMapIndex[group][level]].
78      */
79     darray(size_t) symsMapNumEntries[XkbNumKbdGroups];
80
81     darray_xkb_action acts[XkbNumKbdGroups];
82
83     xkb_atom_t types[XkbNumKbdGroups];
84     enum key_repeat repeat;
85     struct xkb_behavior behavior;
86     unsigned short vmodmap;
87     xkb_atom_t dfltType;
88
89     uint8_t out_of_range_group_action;
90     xkb_group_index_t out_of_range_group_number;
91 } KeyInfo;
92
93 /**
94  * Init the given key info to sane values.
95  */
96 static void
97 InitKeyInfo(KeyInfo *keyi, unsigned file_id)
98 {
99     xkb_group_index_t i;
100     static const char dflt[4] = "*";
101
102     keyi->defined = 0;
103     keyi->file_id = file_id;
104     keyi->merge = MERGE_OVERRIDE;
105     keyi->name = KeyNameToLong(dflt);
106     keyi->typesDefined = keyi->symsDefined = keyi->actsDefined = 0;
107
108     for (i = 0; i < XkbNumKbdGroups; i++) {
109         keyi->numLevels[i] = 0;
110         keyi->types[i] = XKB_ATOM_NONE;
111         darray_init(keyi->syms[i]);
112         darray_init(keyi->symsMapIndex[i]);
113         darray_init(keyi->symsMapNumEntries[i]);
114         darray_init(keyi->acts[i]);
115     }
116
117     keyi->dfltType = XKB_ATOM_NONE;
118     keyi->behavior.type = XkbKB_Default;
119     keyi->behavior.data = 0;
120     keyi->vmodmap = 0;
121     keyi->repeat = KEY_REPEAT_UNDEFINED;
122     keyi->out_of_range_group_action = 0;
123     keyi->out_of_range_group_number = 0;
124 }
125
126 static void
127 FreeKeyInfo(KeyInfo *keyi)
128 {
129     xkb_group_index_t i;
130
131     for (i = 0; i < XkbNumKbdGroups; i++) {
132         darray_free(keyi->syms[i]);
133         darray_free(keyi->symsMapIndex[i]);
134         darray_free(keyi->symsMapNumEntries[i]);
135         darray_free(keyi->acts[i]);
136     }
137 }
138
139 /**
140  * Copy old into new, optionally reset old to 0.
141  * If old is reset, new simply re-uses old's memory. Otherwise, the memory is
142  * newly allocated and new points to the new memory areas.
143  */
144 static bool
145 CopyKeyInfo(KeyInfo * old, KeyInfo * new, bool clearOld)
146 {
147     xkb_group_index_t i;
148
149     *new = *old;
150
151     if (clearOld) {
152         for (i = 0; i < XkbNumKbdGroups; i++) {
153             old->numLevels[i] = 0;
154             darray_init(old->symsMapIndex[i]);
155             darray_init(old->symsMapNumEntries[i]);
156             darray_init(old->syms[i]);
157             darray_init(old->acts[i]);
158         }
159     }
160     else {
161         for (i = 0; i < XkbNumKbdGroups; i++) {
162             darray_copy(new->syms[i], old->syms[i]);
163             darray_copy(new->symsMapIndex[i], old->symsMapIndex[i]);
164             darray_copy(new->symsMapNumEntries[i], old->symsMapNumEntries[i]);
165             darray_copy(new->acts[i], old->acts[i]);
166         }
167     }
168
169     return true;
170 }
171
172 /***====================================================================***/
173
174 typedef struct _ModMapEntry {
175     struct list entry;
176     enum merge_mode merge;
177     bool haveSymbol;
178     int modifier;
179     union {
180         unsigned long keyName;
181         xkb_keysym_t keySym;
182     } u;
183 } ModMapEntry;
184
185 typedef struct _SymbolsInfo {
186     char *name;         /* e.g. pc+us+inet(evdev) */
187     int errorCount;
188     unsigned file_id;
189     enum merge_mode merge;
190     xkb_group_index_t explicit_group;
191     darray(KeyInfo) keys;
192     KeyInfo dflt;
193     VModInfo vmods;
194     ActionInfo *action;
195     xkb_atom_t groupNames[XkbNumKbdGroups];
196
197     struct list modMaps;
198
199     struct xkb_keymap *keymap;
200 } SymbolsInfo;
201
202 static void
203 InitSymbolsInfo(SymbolsInfo * info, struct xkb_keymap *keymap,
204                 unsigned file_id)
205 {
206     xkb_group_index_t i;
207
208     info->name = NULL;
209     info->explicit_group = 0;
210     info->errorCount = 0;
211     info->file_id = file_id;
212     info->merge = MERGE_OVERRIDE;
213     darray_init(info->keys);
214     darray_growalloc(info->keys, 110);
215     list_init(&info->modMaps);
216     for (i = 0; i < XkbNumKbdGroups; i++)
217         info->groupNames[i] = XKB_ATOM_NONE;
218     InitKeyInfo(&info->dflt, file_id);
219     InitVModInfo(&info->vmods, keymap);
220     info->action = NULL;
221     info->keymap = keymap;
222 }
223
224 static void
225 FreeSymbolsInfo(SymbolsInfo * info)
226 {
227     KeyInfo *keyi;
228     ModMapEntry *mm, *next;
229
230     free(info->name);
231     darray_foreach(keyi, info->keys) {
232         FreeKeyInfo(keyi);
233     }
234     darray_free(info->keys);
235     list_foreach_safe(mm, next, &info->modMaps, entry)
236         free(mm);
237     memset(info, 0, sizeof(SymbolsInfo));
238 }
239
240 static bool
241 ResizeKeyGroup(KeyInfo *keyi, xkb_group_index_t group,
242                xkb_level_index_t numLevels, unsigned sizeSyms,
243                bool forceActions)
244 {
245     xkb_level_index_t i;
246
247     if (darray_size(keyi->syms[group]) < sizeSyms)
248         darray_resize0(keyi->syms[group], sizeSyms);
249
250     if (darray_empty(keyi->symsMapIndex[group]) ||
251         keyi->numLevels[group] < numLevels) {
252         darray_resize(keyi->symsMapIndex[group], numLevels);
253         for (i = keyi->numLevels[group]; i < numLevels; i++)
254             darray_item(keyi->symsMapIndex[group], i) = -1;
255     }
256
257     if (darray_empty(keyi->symsMapNumEntries[group]) ||
258         keyi->numLevels[group] < numLevels)
259         darray_resize0(keyi->symsMapNumEntries[group], numLevels);
260
261     if ((forceActions && (keyi->numLevels[group] < numLevels ||
262                           darray_empty(keyi->acts[group]))) ||
263         (keyi->numLevels[group] < numLevels && !darray_empty(keyi->acts[group])))
264         darray_resize0(keyi->acts[group], numLevels);
265
266     if (keyi->numLevels[group] < numLevels)
267         keyi->numLevels[group] = numLevels;
268
269     return true;
270 }
271
272 enum key_group_selector {
273     NONE = 0,
274     FROM = (1 << 0),
275     TO = (1 << 1),
276 };
277
278 static bool
279 MergeKeyGroups(SymbolsInfo * info,
280                KeyInfo * into, KeyInfo * from, xkb_group_index_t group)
281 {
282     darray_xkb_keysym_t resultSyms;
283     enum key_group_selector using = NONE;
284     darray_xkb_action resultActs;
285     xkb_level_index_t resultWidth;
286     unsigned int resultSize = 0;
287     int cur_idx = 0;
288     xkb_level_index_t i;
289     bool report, clobber;
290     int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
291
292     clobber = (from->merge != MERGE_AUGMENT);
293
294     report = (verbosity > 9) ||
295              (into->file_id == from->file_id && verbosity > 0);
296
297     darray_init(resultSyms);
298
299     if (into->numLevels[group] >= from->numLevels[group]) {
300         resultActs = into->acts[group];
301         resultWidth = into->numLevels[group];
302     }
303     else {
304         resultActs = from->acts[group];
305         resultWidth = from->numLevels[group];
306         darray_resize(into->symsMapIndex[group],
307                       from->numLevels[group]);
308         darray_resize0(into->symsMapNumEntries[group],
309                        from->numLevels[group]);
310
311         for (i = into->numLevels[group]; i < from->numLevels[group]; i++)
312             darray_item(into->symsMapIndex[group], i) = -1;
313     }
314
315     if (darray_empty(resultActs) && (!darray_empty(into->acts[group]) ||
316                                      !darray_empty(from->acts[group]))) {
317         darray_resize0(resultActs, resultWidth);
318         for (i = 0; i < resultWidth; i++) {
319             union xkb_action *fromAct = NULL, *toAct = NULL;
320
321             if (!darray_empty(from->acts[group]))
322                 fromAct = &darray_item(from->acts[group], i);
323
324             if (!darray_empty(into->acts[group]))
325                 toAct = &darray_item(into->acts[group], i);
326
327             if (((fromAct == NULL) || (fromAct->type == XkbSA_NoAction))
328                 && (toAct != NULL)) {
329                 darray_item(resultActs, i) = *toAct;
330             }
331             else if (((toAct == NULL) || (toAct->type == XkbSA_NoAction))
332                      && (fromAct != NULL)) {
333                 darray_item(resultActs, i) = *fromAct;
334             }
335             else {
336                 union xkb_action *use, *ignore;
337                 if (clobber) {
338                     use = fromAct;
339                     ignore = toAct;
340                 }
341                 else {
342                     use = toAct;
343                     ignore = fromAct;
344                 }
345                 if (report)
346                     log_warn(info->keymap->ctx,
347                              "Multiple actions for level %d/group %u on key %s; "
348                              "Using %s, ignoring %s\n",
349                              i + 1, group + 1, LongKeyNameText(into->name),
350                              ActionTypeText(use->type),
351                              ActionTypeText(ignore->type));
352                 if (use)
353                     darray_item(resultActs, i) = *use;
354             }
355         }
356     }
357
358     for (i = 0; i < resultWidth; i++) {
359         unsigned int fromSize = 0;
360         unsigned toSize = 0;
361
362         if (!darray_empty(from->symsMapNumEntries[group]) &&
363             i < from->numLevels[group])
364             fromSize = darray_item(from->symsMapNumEntries[group], i);
365
366         if (!darray_empty(into->symsMapNumEntries[group]) &&
367             i < into->numLevels[group])
368             toSize = darray_item(into->symsMapNumEntries[group], i);
369
370         if (fromSize == 0) {
371             resultSize += toSize;
372             using |= TO;
373         }
374         else if (toSize == 0 || clobber) {
375             resultSize += fromSize;
376             using |= FROM;
377         }
378         else {
379             resultSize += toSize;
380             using |= TO;
381         }
382     }
383
384     if (resultSize == 0)
385         goto out;
386
387     if (using == FROM) {
388         resultSyms = from->syms[group];
389         darray_free(into->symsMapNumEntries[group]);
390         darray_free(into->symsMapIndex[group]);
391         into->symsMapNumEntries[group] = from->symsMapNumEntries[group];
392         into->symsMapIndex[group] = from->symsMapIndex[group];
393         darray_init(from->symsMapNumEntries[group]);
394         darray_init(from->symsMapIndex[group]);
395         goto out;
396     }
397     else if (using == TO) {
398         resultSyms = into->syms[group];
399         goto out;
400     }
401
402     darray_resize0(resultSyms, resultSize);
403
404     for (i = 0; i < resultWidth; i++) {
405         enum key_group_selector use = NONE;
406         unsigned int fromSize = 0;
407         unsigned int toSize = 0;
408
409         if (i < from->numLevels[group])
410             fromSize = darray_item(from->symsMapNumEntries[group], i);
411
412         if (i < into->numLevels[group])
413             toSize = darray_item(into->symsMapNumEntries[group], i);
414
415         if (fromSize == 0 && toSize == 0) {
416             darray_item(into->symsMapIndex[group], i) = -1;
417             darray_item(into->symsMapNumEntries[group], i) = 0;
418             continue;
419         }
420
421         if (fromSize == 0)
422             use = TO;
423         else if (toSize == 0 || clobber)
424             use = FROM;
425         else
426             use = TO;
427
428         if (toSize && fromSize && report) {
429             log_info(info->keymap->ctx,
430                      "Multiple symbols for group %u, level %d on key %s; "
431                      "Using %s, ignoring %s\n",
432                      group + 1, i + 1, LongKeyNameText(into->name),
433                      (use == FROM ? "from" : "to"),
434                      (use == FROM ? "to" : "from"));
435         }
436
437         if (use == FROM) {
438             memcpy(darray_mem(resultSyms, cur_idx),
439                    darray_mem(from->syms[group],
440                               darray_item(from->symsMapIndex[group], i)),
441                    darray_item(from->symsMapNumEntries[group],
442                                i) * sizeof(xkb_keysym_t));
443             darray_item(into->symsMapIndex[group], i) = cur_idx;
444             darray_item(into->symsMapNumEntries[group], i) =
445                 darray_item(from->symsMapNumEntries[group], i);
446         }
447         else {
448             memcpy(darray_mem(resultSyms, cur_idx),
449                    darray_mem(into->syms[group],
450                               darray_item(into->symsMapIndex[group], i)),
451                    darray_item(into->symsMapNumEntries[group],
452                                i) * sizeof(xkb_keysym_t));
453             darray_item(into->symsMapIndex[group], i) = cur_idx;
454         }
455         cur_idx += darray_item(into->symsMapNumEntries[group], i);
456     }
457
458 out:
459     if (!darray_same(resultActs, into->acts[group]))
460         darray_free(into->acts[group]);
461     if (!darray_same(resultActs, from->acts[group]))
462         darray_free(from->acts[group]);
463     into->numLevels[group] = resultWidth;
464     if (!darray_same(resultSyms, into->syms[group]))
465         darray_free(into->syms[group]);
466     into->syms[group] = resultSyms;
467     if (!darray_same(resultSyms, from->syms[group]))
468         darray_free(from->syms[group]);
469     darray_init(from->syms[group]);
470     darray_free(from->symsMapIndex[group]);
471     darray_free(from->symsMapNumEntries[group]);
472     into->acts[group] = resultActs;
473     darray_init(from->acts[group]);
474     if (!darray_empty(into->syms[group]))
475         into->symsDefined |= (1 << group);
476     from->symsDefined &= ~(1 << group);
477     into->actsDefined |= (1 << group);
478     from->actsDefined &= ~(1 << group);
479
480     return true;
481 }
482
483 static bool
484 UseNewKeyField(enum key_field field, KeyInfo *old, KeyInfo *new,
485                int verbosity, enum key_field *collide)
486 {
487     if (!(old->defined & field))
488         return true;
489
490     if (new->defined & field) {
491         if ((old->file_id == new->file_id && verbosity > 0) || verbosity > 9)
492             *collide |= field;
493
494         if (new->merge != MERGE_AUGMENT)
495             return true;
496     }
497
498     return false;
499 }
500
501
502 static bool
503 MergeKeys(SymbolsInfo *info, KeyInfo *into, KeyInfo *from)
504 {
505     xkb_group_index_t i;
506     enum key_field collide = 0;
507     bool report;
508     int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
509
510     if (from->merge == MERGE_REPLACE) {
511         for (i = 0; i < XkbNumKbdGroups; i++) {
512             if (into->numLevels[i] != 0) {
513                 darray_free(into->syms[i]);
514                 darray_free(into->acts[i]);
515             }
516         }
517         *into = *from;
518         memset(from, 0, sizeof(KeyInfo));
519         return true;
520     }
521
522     report = (verbosity > 9 ||
523               (into->file_id == from->file_id && verbosity > 0));
524
525     for (i = 0; i < XkbNumKbdGroups; i++) {
526         if (from->numLevels[i] > 0) {
527             if (into->numLevels[i] == 0) {
528                 into->numLevels[i] = from->numLevels[i];
529                 into->syms[i] = from->syms[i];
530                 into->symsMapIndex[i] = from->symsMapIndex[i];
531                 into->symsMapNumEntries[i] = from->symsMapNumEntries[i];
532                 into->acts[i] = from->acts[i];
533                 into->symsDefined |= (1 << i);
534                 darray_init(from->syms[i]);
535                 darray_init(from->symsMapIndex[i]);
536                 darray_init(from->symsMapNumEntries[i]);
537                 darray_init(from->acts[i]);
538                 from->numLevels[i] = 0;
539                 from->symsDefined &= ~(1 << i);
540                 if (!darray_empty(into->syms[i]))
541                     into->defined |= KEY_FIELD_SYMS;
542                 if (!darray_empty(into->acts[i]))
543                     into->defined |= KEY_FIELD_ACTS;
544             }
545             else {
546                 if (report) {
547                     if (!darray_empty(into->syms[i]))
548                         collide |= KEY_FIELD_SYMS;
549                     if (!darray_empty(into->acts[i]))
550                         collide |= KEY_FIELD_ACTS;
551                 }
552                 MergeKeyGroups(info, into, from, (unsigned) i);
553             }
554         }
555         if (from->types[i] != XKB_ATOM_NONE) {
556             if ((into->types[i] != XKB_ATOM_NONE) && report &&
557                 (into->types[i] != from->types[i])) {
558                 xkb_atom_t use, ignore;
559                 collide |= KEY_FIELD_TYPES;
560                 if (from->merge != MERGE_AUGMENT) {
561                     use = from->types[i];
562                     ignore = into->types[i];
563                 }
564                 else {
565                     use = into->types[i];
566                     ignore = from->types[i];
567                 }
568
569                 log_warn(info->keymap->ctx,
570                          "Multiple definitions for group %d type of key %s; "
571                          "Using %s, ignoring %s\n",
572                          i, LongKeyNameText(into->name),
573                          xkb_atom_text(info->keymap->ctx, use),
574                          xkb_atom_text(info->keymap->ctx, ignore));
575             }
576
577             if (from->merge != MERGE_AUGMENT ||
578                 into->types[i] == XKB_ATOM_NONE) {
579                 into->types[i] = from->types[i];
580             }
581         }
582     }
583
584     if (UseNewKeyField(KEY_FIELD_BEHAVIOR, into, from, verbosity, &collide)) {
585         into->behavior = from->behavior;
586         into->defined |= KEY_FIELD_BEHAVIOR;
587     }
588     if (UseNewKeyField(KEY_FIELD_VMODMAP, into, from, verbosity, &collide)) {
589         into->vmodmap = from->vmodmap;
590         into->defined |= KEY_FIELD_VMODMAP;
591     }
592     if (UseNewKeyField(KEY_FIELD_REPEAT, into, from, verbosity, &collide)) {
593         into->repeat = from->repeat;
594         into->defined |= KEY_FIELD_REPEAT;
595     }
596     if (UseNewKeyField(KEY_FIELD_TYPE_DFLT, into, from, verbosity, &collide)) {
597         into->dfltType = from->dfltType;
598         into->defined |= KEY_FIELD_TYPE_DFLT;
599     }
600     if (UseNewKeyField(KEY_FIELD_GROUPINFO, into, from, verbosity, &collide)) {
601         into->out_of_range_group_action = from->out_of_range_group_action;
602         into->out_of_range_group_number = from->out_of_range_group_number;
603         into->defined |= KEY_FIELD_GROUPINFO;
604     }
605
606     if (collide)
607         log_warn(info->keymap->ctx,
608                  "Symbol map for key %s redefined; "
609                  "Using %s definition for conflicting fields\n",
610                  LongKeyNameText(into->name),
611                  (from->merge == MERGE_AUGMENT ? "first" : "last"));
612
613     return true;
614 }
615
616 static bool
617 AddKeySymbols(SymbolsInfo *info, KeyInfo *keyi)
618 {
619     unsigned long real_name;
620     KeyInfo *iter, *new;
621
622     darray_foreach(iter, info->keys)
623         if (iter->name == keyi->name)
624             return MergeKeys(info, iter, keyi);
625
626     if (FindKeyNameForAlias(info->keymap, keyi->name, &real_name))
627         darray_foreach(iter, info->keys)
628             if (iter->name == real_name)
629                 return MergeKeys(info, iter, keyi);
630
631     darray_resize0(info->keys, darray_size(info->keys) + 1);
632     new = &darray_item(info->keys, darray_size(info->keys) - 1);
633     return CopyKeyInfo(keyi, new, true);
634 }
635
636 static bool
637 AddModMapEntry(SymbolsInfo * info, ModMapEntry * new)
638 {
639     ModMapEntry *mm;
640     bool clobber;
641
642     clobber = (new->merge != MERGE_AUGMENT);
643     list_foreach(mm, &info->modMaps, entry) {
644         if (new->haveSymbol && mm->haveSymbol
645             && (new->u.keySym == mm->u.keySym)) {
646             unsigned use, ignore;
647             if (mm->modifier != new->modifier) {
648                 if (clobber) {
649                     use = new->modifier;
650                     ignore = mm->modifier;
651                 }
652                 else {
653                     use = mm->modifier;
654                     ignore = new->modifier;
655                 }
656                 log_err(info->keymap->ctx,
657                         "%s added to symbol map for multiple modifiers; "
658                         "Using %s, ignoring %s.\n",
659                         KeysymText(new->u.keySym), ModIndexText(use),
660                         ModIndexText(ignore));
661                 mm->modifier = use;
662             }
663             return true;
664         }
665         if ((!new->haveSymbol) && (!mm->haveSymbol) &&
666             (new->u.keyName == mm->u.keyName)) {
667             unsigned use, ignore;
668             if (mm->modifier != new->modifier) {
669                 if (clobber) {
670                     use = new->modifier;
671                     ignore = mm->modifier;
672                 }
673                 else {
674                     use = mm->modifier;
675                     ignore = new->modifier;
676                 }
677                 log_err(info->keymap->ctx,
678                         "Key %s added to map for multiple modifiers; "
679                         "Using %s, ignoring %s.\n",
680                         LongKeyNameText(new->u.keyName), ModIndexText(use),
681                         ModIndexText(ignore));
682                 mm->modifier = use;
683             }
684             return true;
685         }
686     }
687
688     mm = malloc(sizeof(*mm));
689     if (!mm) {
690         log_wsgo(info->keymap->ctx,
691                  "Could not allocate modifier map entry; "
692                  "Modifier map for %s will be incomplete\n",
693                  ModIndexText(new->modifier));
694         return false;
695     }
696
697     *mm = *new;
698     list_add(&mm->entry, &info->modMaps);
699     return true;
700 }
701
702 /***====================================================================***/
703
704 static void
705 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
706                      enum merge_mode merge)
707 {
708     unsigned int i;
709     KeyInfo *keyi;
710     ModMapEntry *mm, *next;
711
712     if (from->errorCount > 0) {
713         into->errorCount += from->errorCount;
714         return;
715     }
716     if (into->name == NULL) {
717         into->name = from->name;
718         from->name = NULL;
719     }
720     for (i = 0; i < XkbNumKbdGroups; i++) {
721         if (from->groupNames[i] != XKB_ATOM_NONE) {
722             if ((merge != MERGE_AUGMENT) ||
723                 (into->groupNames[i] == XKB_ATOM_NONE))
724                 into->groupNames[i] = from->groupNames[i];
725         }
726     }
727
728     darray_foreach(keyi, from->keys) {
729         if (merge != MERGE_DEFAULT)
730             keyi->merge = merge;
731
732         if (!AddKeySymbols(into, keyi))
733             into->errorCount++;
734     }
735
736     list_foreach_safe(mm, next, &from->modMaps, entry) {
737         if (merge != MERGE_DEFAULT)
738             mm->merge = merge;
739         if (!AddModMapEntry(into, mm))
740             into->errorCount++;
741         free(mm);
742     }
743     list_init(&from->modMaps);
744 }
745
746 static void
747 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge);
748
749 static bool
750 HandleIncludeSymbols(SymbolsInfo *info, IncludeStmt *stmt)
751 {
752     enum merge_mode merge = MERGE_DEFAULT;
753     XkbFile *rtrn;
754     SymbolsInfo included, next_incl;
755
756     InitSymbolsInfo(&included, info->keymap, info->file_id);
757     if (stmt->stmt) {
758         free(included.name);
759         included.name = stmt->stmt;
760         stmt->stmt = NULL;
761     }
762
763     for (; stmt; stmt = stmt->next_incl) {
764         if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_SYMBOLS,
765                                 &rtrn, &merge)) {
766             info->errorCount += 10;
767             FreeSymbolsInfo(&included);
768             return false;
769         }
770
771         InitSymbolsInfo(&next_incl, info->keymap, rtrn->id);
772         next_incl.merge = next_incl.dflt.merge = MERGE_OVERRIDE;
773         if (stmt->modifier)
774             next_incl.explicit_group = atoi(stmt->modifier) - 1;
775         else
776             next_incl.explicit_group = info->explicit_group;
777
778         HandleSymbolsFile(&next_incl, rtrn, MERGE_OVERRIDE);
779
780         MergeIncludedSymbols(&included, &next_incl, merge);
781
782         FreeSymbolsInfo(&next_incl);
783         FreeXKBFile(rtrn);
784     }
785
786     MergeIncludedSymbols(info, &included, merge);
787     FreeSymbolsInfo(&included);
788
789     return (info->errorCount == 0);
790 }
791
792 #define SYMBOLS 1
793 #define ACTIONS 2
794
795 static bool
796 GetGroupIndex(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
797               unsigned what, xkb_group_index_t *ndx_rtrn)
798 {
799     const char *name;
800
801     if (what == SYMBOLS)
802         name = "symbols";
803     else
804         name = "actions";
805
806     if (arrayNdx == NULL) {
807         xkb_group_index_t i;
808         unsigned defined;
809         if (what == SYMBOLS)
810             defined = keyi->symsDefined;
811         else
812             defined = keyi->actsDefined;
813
814         for (i = 0; i < XkbNumKbdGroups; i++) {
815             if ((defined & (1 << i)) == 0) {
816                 *ndx_rtrn = i;
817                 return true;
818             }
819         }
820
821         log_err(info->keymap->ctx,
822                 "Too many groups of %s for key %s (max %u); "
823                 "Ignoring %s defined for extra groups\n",
824                 name, LongKeyNameText(keyi->name), XkbNumKbdGroups + 1, name);
825         return false;
826     }
827
828     if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, ndx_rtrn)) {
829         log_err(info->keymap->ctx,
830                 "Illegal group index for %s of key %s\n"
831                 "Definition with non-integer array index ignored\n",
832                 name, LongKeyNameText(keyi->name));
833         return false;
834     }
835
836     (*ndx_rtrn)--;
837     return true;
838 }
839
840 static bool
841 AddSymbolsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
842                 ExprDef *value)
843 {
844     xkb_group_index_t ndx;
845     size_t nSyms;
846     xkb_level_index_t nLevels;
847     xkb_level_index_t i;
848     int j;
849
850     if (!GetGroupIndex(info, keyi, arrayNdx, SYMBOLS, &ndx))
851         return false;
852     if (value == NULL) {
853         keyi->symsDefined |= (1 << ndx);
854         return true;
855     }
856     if (value->op != EXPR_KEYSYM_LIST) {
857         log_err(info->keymap->ctx,
858                 "Expected a list of symbols, found %s; "
859                 "Ignoring symbols for group %u of %s\n",
860                 exprOpText(value->op), ndx + 1, LongKeyNameText(keyi->name));
861         return false;
862     }
863     if (!darray_empty(keyi->syms[ndx])) {
864         log_err(info->keymap->ctx,
865                 "Symbols for key %s, group %u already defined; "
866                 "Ignoring duplicate definition\n",
867                 LongKeyNameText(keyi->name), ndx + 1);
868         return false;
869     }
870     nSyms = darray_size(value->value.list.syms);
871     nLevels = darray_size(value->value.list.symsMapIndex);
872     if ((keyi->numLevels[ndx] < nSyms || darray_empty(keyi->syms[ndx])) &&
873         (!ResizeKeyGroup(keyi, ndx, nLevels, nSyms, false))) {
874         log_wsgo(info->keymap->ctx,
875                  "Could not resize group %u of key %s to contain %zu levels; "
876                  "Symbols lost\n",
877                  ndx + 1, LongKeyNameText(keyi->name), nSyms);
878         return false;
879     }
880     keyi->symsDefined |= (1 << ndx);
881     for (i = 0; i < nLevels; i++) {
882         darray_item(keyi->symsMapIndex[ndx], i) =
883             darray_item(value->value.list.symsMapIndex, i);
884         darray_item(keyi->symsMapNumEntries[ndx], i) =
885             darray_item(value->value.list.symsNumEntries, i);
886
887         for (j = 0; j < darray_item(keyi->symsMapNumEntries[ndx], i); j++) {
888             /* FIXME: What's abort() doing here? */
889             if (darray_item(keyi->symsMapIndex[ndx], i) + j >= nSyms)
890                 abort();
891             if (!LookupKeysym(darray_item(value->value.list.syms,
892                                           darray_item(value->value.list.symsMapIndex,
893                                                       i) + j),
894                               &darray_item(keyi->syms[ndx],
895                                            darray_item(keyi->symsMapIndex[ndx],
896                                                        i) + j))) {
897                 log_warn(info->keymap->ctx,
898                          "Could not resolve keysym %s for key %s, group %u (%s), level %zu\n",
899                          darray_item(value->value.list.syms, i),
900                          LongKeyNameText(keyi->name),
901                          ndx + 1,
902                          xkb_atom_text(info->keymap->ctx,
903                                        info->groupNames[ndx]),
904                          nSyms);
905                 while (--j >= 0)
906                     darray_item(keyi->syms[ndx],
907                                 darray_item(keyi->symsMapIndex[ndx],
908                                             i) + j) = XKB_KEY_NoSymbol;
909                 darray_item(keyi->symsMapIndex[ndx], i) = -1;
910                 darray_item(keyi->symsMapNumEntries[ndx], i) = 0;
911                 break;
912             }
913             if (darray_item(keyi->symsMapNumEntries[ndx], i) == 1 &&
914                 darray_item(keyi->syms[ndx],
915                             darray_item(keyi->symsMapIndex[ndx],
916                                         i) + j) == XKB_KEY_NoSymbol) {
917                 darray_item(keyi->symsMapIndex[ndx], i) = -1;
918                 darray_item(keyi->symsMapNumEntries[ndx], i) = 0;
919             }
920         }
921     }
922     for (j = keyi->numLevels[ndx] - 1;
923          j >= 0 && darray_item(keyi->symsMapNumEntries[ndx], j) == 0; j--)
924         keyi->numLevels[ndx]--;
925     return true;
926 }
927
928 static bool
929 AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
930                 ExprDef *value)
931 {
932     size_t i;
933     xkb_group_index_t ndx;
934     size_t nActs;
935     ExprDef *act;
936     struct xkb_any_action *toAct;
937
938     if (!GetGroupIndex(info, keyi, arrayNdx, ACTIONS, &ndx))
939         return false;
940
941     if (value == NULL) {
942         keyi->actsDefined |= (1 << ndx);
943         return true;
944     }
945
946     if (value->op != EXPR_ACTION_LIST) {
947         log_wsgo(info->keymap->ctx,
948                  "Bad expression type (%d) for action list value; "
949                  "Ignoring actions for group %u of %s\n",
950                  value->op, ndx, LongKeyNameText(keyi->name));
951         return false;
952     }
953
954     if (!darray_empty(keyi->acts[ndx])) {
955         log_wsgo(info->keymap->ctx,
956                  "Actions for key %s, group %u already defined\n",
957                  LongKeyNameText(keyi->name), ndx);
958         return false;
959     }
960
961     for (nActs = 0, act = value->value.child; act != NULL; nActs++) {
962         act = (ExprDef *) act->common.next;
963     }
964
965     if (nActs < 1) {
966         log_wsgo(info->keymap->ctx,
967                  "Action list but not actions in AddActionsToKey\n");
968         return false;
969     }
970
971     if ((keyi->numLevels[ndx] < nActs || darray_empty(keyi->acts[ndx])) &&
972         !ResizeKeyGroup(keyi, ndx, nActs, nActs, true)) {
973         log_wsgo(info->keymap->ctx,
974                  "Could not resize group %u of key %s; "
975                  "Actions lost\n",
976                  ndx, LongKeyNameText(keyi->name));
977         return false;
978     }
979     keyi->actsDefined |= (1 << ndx);
980
981     toAct = (struct xkb_any_action *) darray_mem(keyi->acts[ndx], 0);
982     act = value->value.child;
983     for (i = 0; i < nActs; i++, toAct++) {
984         if (!HandleActionDef(act, info->keymap, toAct, info->action)) {
985             log_err(info->keymap->ctx,
986                     "Illegal action definition for %s; "
987                     "Action for group %u/level %zu ignored\n",
988                     LongKeyNameText(keyi->name), ndx + 1, i + 1);
989         }
990         act = (ExprDef *) act->common.next;
991     }
992
993     return true;
994 }
995
996 static const LookupEntry lockingEntries[] = {
997     { "true", XkbKB_Lock },
998     { "yes", XkbKB_Lock },
999     { "on", XkbKB_Lock },
1000     { "false", XkbKB_Default },
1001     { "no", XkbKB_Default },
1002     { "off", XkbKB_Default },
1003     { "permanent", XkbKB_Lock | XkbKB_Permanent },
1004     { NULL, 0 }
1005 };
1006
1007 static const LookupEntry repeatEntries[] = {
1008     { "true", KEY_REPEAT_YES },
1009     { "yes", KEY_REPEAT_YES },
1010     { "on", KEY_REPEAT_YES },
1011     { "false", KEY_REPEAT_NO },
1012     { "no", KEY_REPEAT_NO },
1013     { "off", KEY_REPEAT_NO },
1014     { "default", KEY_REPEAT_UNDEFINED },
1015     { NULL, 0 }
1016 };
1017
1018 static bool
1019 SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
1020                 ExprDef *arrayNdx, ExprDef *value)
1021 {
1022     bool ok = true;
1023     struct xkb_context *ctx = info->keymap->ctx;
1024
1025     if (istreq(field, "type")) {
1026         xkb_group_index_t ndx;
1027         const char *str;
1028
1029         if (!ExprResolveString(ctx, value, &str))
1030             log_lvl(info->keymap->ctx, 1,
1031                     "The type field of a key symbol map must be a string; "
1032                     "Ignoring illegal type definition\n");
1033
1034         if (arrayNdx == NULL) {
1035             keyi->dfltType = xkb_atom_intern(ctx, str);
1036             keyi->defined |= KEY_FIELD_TYPE_DFLT;
1037         }
1038         else if (!ExprResolveGroup(ctx, arrayNdx, &ndx)) {
1039             log_err(info->keymap->ctx,
1040                     "Illegal group index for type of key %s; "
1041                     "Definition with non-integer array index ignored\n",
1042                     LongKeyNameText(keyi->name));
1043             return false;
1044         }
1045         else {
1046             ndx--;
1047             keyi->types[ndx] = xkb_atom_intern(ctx, str);
1048             keyi->typesDefined |= (1 << ndx);
1049         }
1050     }
1051     else if (istreq(field, "symbols"))
1052         return AddSymbolsToKey(info, keyi, arrayNdx, value);
1053     else if (istreq(field, "actions"))
1054         return AddActionsToKey(info, keyi, arrayNdx, value);
1055     else if (istreq(field, "vmods") ||
1056              istreq(field, "virtualmods") ||
1057              istreq(field, "virtualmodifiers")) {
1058         xkb_mod_mask_t mask;
1059
1060         ok = ExprResolveVModMask(info->keymap, value, &mask);
1061         if (ok) {
1062             keyi->vmodmap = (mask >> 8);
1063             keyi->defined |= KEY_FIELD_VMODMAP;
1064         }
1065         else {
1066             log_err(info->keymap->ctx,
1067                     "Expected a virtual modifier mask, found %s; "
1068                     "Ignoring virtual modifiers definition for key %s\n",
1069                     exprOpText(value->op), LongKeyNameText(keyi->name));
1070         }
1071     }
1072     else if (istreq(field, "locking") ||
1073              istreq(field, "lock") ||
1074              istreq(field, "locks")) {
1075         unsigned int val;
1076
1077         ok = ExprResolveEnum(ctx, value, &val, lockingEntries);
1078         if (ok)
1079             keyi->behavior.type = val;
1080         keyi->defined |= KEY_FIELD_BEHAVIOR;
1081     }
1082     else if (istreq(field, "radiogroup") ||
1083              istreq(field, "permanentradiogroup") ||
1084              istreq(field, "allownone")) {
1085         log_err(info->keymap->ctx,
1086                 "Radio groups not supported; "
1087                 "Ignoring radio group specification for key %s\n",
1088                 LongKeyNameText(keyi->name));
1089         return false;
1090     }
1091     else if (istreq_prefix("overlay", field) ||
1092              istreq_prefix("permanentoverlay", field)) {
1093         log_err(info->keymap->ctx,
1094                 "Overlays not supported; "
1095                 "Ignoring overlay specification for key %s\n",
1096                 LongKeyNameText(keyi->name));
1097     }
1098     else if (istreq(field, "repeating") ||
1099              istreq(field, "repeats") ||
1100              istreq(field, "repeat")) {
1101         unsigned int val;
1102
1103         ok = ExprResolveEnum(ctx, value, &val, repeatEntries);
1104         if (!ok) {
1105             log_err(info->keymap->ctx,
1106                     "Illegal repeat setting for %s; "
1107                     "Non-boolean repeat setting ignored\n",
1108                     LongKeyNameText(keyi->name));
1109             return false;
1110         }
1111         keyi->repeat = val;
1112         keyi->defined |= KEY_FIELD_REPEAT;
1113     }
1114     else if (istreq(field, "groupswrap") ||
1115              istreq(field, "wrapgroups")) {
1116         bool set;
1117
1118         if (!ExprResolveBoolean(ctx, value, &set)) {
1119             log_err(info->keymap->ctx,
1120                     "Illegal groupsWrap setting for %s; "
1121                     "Non-boolean value ignored\n",
1122                     LongKeyNameText(keyi->name));
1123             return false;
1124         }
1125
1126         if (set)
1127             keyi->out_of_range_group_action = XkbWrapIntoRange;
1128         else
1129             keyi->out_of_range_group_action = XkbClampIntoRange;
1130
1131         keyi->defined |= KEY_FIELD_GROUPINFO;
1132     }
1133     else if (istreq(field, "groupsclamp") ||
1134              istreq(field, "clampgroups")) {
1135         bool set;
1136
1137         if (!ExprResolveBoolean(ctx, value, &set)) {
1138             log_err(info->keymap->ctx,
1139                     "Illegal groupsClamp setting for %s; "
1140                     "Non-boolean value ignored\n",
1141                     LongKeyNameText(keyi->name));
1142             return false;
1143         }
1144
1145         if (set)
1146             keyi->out_of_range_group_action = XkbClampIntoRange;
1147         else
1148             keyi->out_of_range_group_action = XkbWrapIntoRange;
1149
1150         keyi->defined |= KEY_FIELD_GROUPINFO;
1151     }
1152     else if (istreq(field, "groupsredirect") ||
1153              istreq(field, "redirectgroups")) {
1154         xkb_group_index_t grp;
1155
1156         if (!ExprResolveGroup(ctx, value, &grp)) {
1157             log_err(info->keymap->ctx,
1158                     "Illegal group index for redirect of key %s; "
1159                     "Definition with non-integer group ignored\n",
1160                     LongKeyNameText(keyi->name));
1161             return false;
1162         }
1163
1164         keyi->out_of_range_group_action = XkbRedirectIntoRange;
1165         keyi->out_of_range_group_number = grp - 1;
1166         keyi->defined |= KEY_FIELD_GROUPINFO;
1167     }
1168     else {
1169         log_err(info->keymap->ctx,
1170                 "Unknown field %s in a symbol interpretation; "
1171                 "Definition ignored\n",
1172                 field);
1173         ok = false;
1174     }
1175     return ok;
1176 }
1177
1178 static int
1179 SetGroupName(SymbolsInfo *info, ExprDef *arrayNdx, ExprDef *value)
1180 {
1181     xkb_group_index_t grp;
1182     const char *name;
1183
1184     if (!arrayNdx) {
1185         log_lvl(info->keymap->ctx, 1,
1186                 "You must specify an index when specifying a group name; "
1187                 "Group name definition without array subscript ignored\n");
1188         return false;
1189     }
1190
1191     if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, &grp)) {
1192         log_err(info->keymap->ctx,
1193                 "Illegal index in group name definition; "
1194                 "Definition with non-integer array index ignored\n");
1195         return false;
1196     }
1197
1198     if (!ExprResolveString(info->keymap->ctx, value, &name)) {
1199         log_err(info->keymap->ctx,
1200                 "Group name must be a string; "
1201                 "Illegal name for group %d ignored\n", grp);
1202         return false;
1203     }
1204
1205     info->groupNames[grp - 1 + info->explicit_group] =
1206         xkb_atom_intern(info->keymap->ctx, name);
1207
1208     return true;
1209 }
1210
1211 static int
1212 HandleSymbolsVar(SymbolsInfo *info, VarDef *stmt)
1213 {
1214     const char *elem, *field;
1215     ExprDef *arrayNdx;
1216     bool ret;
1217
1218     if (ExprResolveLhs(info->keymap->ctx, stmt->name, &elem, &field,
1219                        &arrayNdx) == 0)
1220         return 0;               /* internal error, already reported */
1221     if (elem && istreq(elem, "key")) {
1222         ret = SetSymbolsField(info, &info->dflt, field, arrayNdx,
1223                               stmt->value);
1224     }
1225     else if (!elem && (istreq(field, "name") ||
1226                        istreq(field, "groupname"))) {
1227         ret = SetGroupName(info, arrayNdx, stmt->value);
1228     }
1229     else if (!elem && (istreq(field, "groupswrap") ||
1230                        istreq(field, "wrapgroups"))) {
1231         log_err(info->keymap->ctx,
1232                 "Global \"groupswrap\" not supported; Ignored\n");
1233         ret = true;
1234     }
1235     else if (!elem && (istreq(field, "groupsclamp") ||
1236                        istreq(field, "clampgroups"))) {
1237         log_err(info->keymap->ctx,
1238                 "Global \"groupsclamp\" not supported; Ignored\n");
1239         ret = true;
1240     }
1241     else if (!elem && (istreq(field, "groupsredirect") ||
1242                        istreq(field, "redirectgroups"))) {
1243         log_err(info->keymap->ctx,
1244                 "Global \"groupsredirect\" not supported; Ignored\n");
1245         ret = true;
1246     }
1247     else if (!elem && istreq(field, "allownone")) {
1248         log_err(info->keymap->ctx,
1249                 "Radio groups not supported; "
1250                 "Ignoring \"allownone\" specification\n");
1251         ret = true;
1252     }
1253     else {
1254         ret = SetActionField(info->keymap, elem, field, arrayNdx,
1255                              stmt->value, &info->action);
1256     }
1257
1258     return ret;
1259 }
1260
1261 static bool
1262 HandleSymbolsBody(SymbolsInfo *info, VarDef *def, KeyInfo *keyi)
1263 {
1264     bool ok = true;
1265     const char *elem, *field;
1266     ExprDef *arrayNdx;
1267
1268     for (; def; def = (VarDef *) def->common.next) {
1269         if (def->name && def->name->op == EXPR_FIELD_REF) {
1270             ok = HandleSymbolsVar(info, def);
1271             continue;
1272         }
1273
1274         if (!def->name) {
1275             if (!def->value || def->value->op == EXPR_KEYSYM_LIST)
1276                 field = "symbols";
1277             else
1278                 field = "actions";
1279             arrayNdx = NULL;
1280         }
1281         else {
1282             ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
1283                                 &arrayNdx);
1284         }
1285
1286         if (ok)
1287             ok = SetSymbolsField(info, keyi, field, arrayNdx, def->value);
1288     }
1289
1290     return ok;
1291 }
1292
1293 static bool
1294 SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi)
1295 {
1296     xkb_group_index_t group = info->explicit_group;
1297
1298     if (group == 0)
1299         return true;
1300
1301     if ((keyi->typesDefined | keyi->symsDefined | keyi->actsDefined) & ~1) {
1302         xkb_group_index_t i;
1303         log_warn(info->keymap->ctx,
1304                  "For the map %s an explicit group specified, "
1305                  "but key %s has more than one group defined; "
1306                  "All groups except first one will be ignored\n",
1307                  info->name, LongKeyNameText(keyi->name));
1308         for (i = 1; i < XkbNumKbdGroups; i++) {
1309             keyi->numLevels[i] = 0;
1310             darray_free(keyi->syms[i]);
1311             darray_free(keyi->acts[i]);
1312             keyi->types[i] = 0;
1313         }
1314     }
1315     keyi->typesDefined = keyi->symsDefined = keyi->actsDefined = 1 << group;
1316
1317     keyi->numLevels[group] = keyi->numLevels[0];
1318     keyi->numLevels[0] = 0;
1319     keyi->syms[group] = keyi->syms[0];
1320     darray_init(keyi->syms[0]);
1321     keyi->symsMapIndex[group] = keyi->symsMapIndex[0];
1322     darray_init(keyi->symsMapIndex[0]);
1323     keyi->symsMapNumEntries[group] = keyi->symsMapNumEntries[0];
1324     darray_init(keyi->symsMapNumEntries[0]);
1325     keyi->acts[group] = keyi->acts[0];
1326     darray_init(keyi->acts[0]);
1327     keyi->types[group] = keyi->types[0];
1328     keyi->types[0] = 0;
1329     return true;
1330 }
1331
1332 static int
1333 HandleSymbolsDef(SymbolsInfo *info, SymbolsDef *stmt)
1334 {
1335     KeyInfo keyi;
1336
1337     InitKeyInfo(&keyi, info->file_id);
1338     CopyKeyInfo(&info->dflt, &keyi, false);
1339     keyi.merge = stmt->merge;
1340     keyi.name = KeyNameToLong(stmt->keyName);
1341     if (!HandleSymbolsBody(info, (VarDef *) stmt->symbols, &keyi)) {
1342         info->errorCount++;
1343         return false;
1344     }
1345
1346     if (!SetExplicitGroup(info, &keyi)) {
1347         info->errorCount++;
1348         return false;
1349     }
1350
1351     if (!AddKeySymbols(info, &keyi)) {
1352         info->errorCount++;
1353         return false;
1354     }
1355     return true;
1356 }
1357
1358 static bool
1359 HandleModMapDef(SymbolsInfo *info, ModMapDef *def)
1360 {
1361     ExprDef *key;
1362     ModMapEntry tmp;
1363     xkb_mod_index_t ndx;
1364     bool ok;
1365     struct xkb_context *ctx = info->keymap->ctx;
1366
1367     if (!LookupModIndex(ctx, NULL, def->modifier, EXPR_TYPE_INT, &ndx)) {
1368         log_err(info->keymap->ctx,
1369                 "Illegal modifier map definition; "
1370                 "Ignoring map for non-modifier \"%s\"\n",
1371                 xkb_atom_text(ctx, def->modifier));
1372         return false;
1373     }
1374
1375     ok = true;
1376     tmp.modifier = ndx;
1377
1378     for (key = def->keys; key != NULL; key = (ExprDef *) key->common.next) {
1379         xkb_keysym_t sym;
1380
1381         if (key->op == EXPR_VALUE && key->value_type == EXPR_TYPE_KEYNAME) {
1382             tmp.haveSymbol = false;
1383             tmp.u.keyName = KeyNameToLong(key->value.keyName);
1384         }
1385         else if (ExprResolveKeySym(ctx, key, &sym)) {
1386             tmp.haveSymbol = true;
1387             tmp.u.keySym = sym;
1388         }
1389         else {
1390             log_err(info->keymap->ctx,
1391                     "Modmap entries may contain only key names or keysyms; "
1392                     "Illegal definition for %s modifier ignored\n",
1393                     ModIndexText(tmp.modifier));
1394             continue;
1395         }
1396
1397         ok = AddModMapEntry(info, &tmp) && ok;
1398     }
1399     return ok;
1400 }
1401
1402 static void
1403 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge)
1404 {
1405     bool ok;
1406     ParseCommon *stmt;
1407
1408     free(info->name);
1409     info->name = strdup_safe(file->name);
1410
1411     stmt = file->defs;
1412     for (stmt = file->defs; stmt; stmt = stmt->next) {
1413         switch (stmt->type) {
1414         case STMT_INCLUDE:
1415             ok = HandleIncludeSymbols(info, (IncludeStmt *) stmt);
1416             break;
1417         case STMT_SYMBOLS:
1418             ok = HandleSymbolsDef(info, (SymbolsDef *) stmt);
1419             break;
1420         case STMT_VAR:
1421             ok = HandleSymbolsVar(info, (VarDef *) stmt);
1422             break;
1423         case STMT_VMOD:
1424             ok = HandleVModDef((VModDef *) stmt, info->keymap, merge,
1425                                &info->vmods);
1426             break;
1427         case STMT_MODMAP:
1428             ok = HandleModMapDef(info, (ModMapDef *) stmt);
1429             break;
1430         default:
1431             log_err(info->keymap->ctx,
1432                     "Interpretation files may not include other types; "
1433                     "Ignoring %s\n", StmtTypeToString(stmt->type));
1434             ok = false;
1435             break;
1436         }
1437
1438         if (!ok)
1439             info->errorCount++;
1440
1441         if (info->errorCount > 10) {
1442             log_err(info->keymap->ctx, "Abandoning symbols file \"%s\"\n",
1443                     file->topName);
1444             break;
1445         }
1446     }
1447 }
1448
1449 /**
1450  * Given a keysym @sym, return a key which generates it, or NULL.
1451  * This is used for example in a modifier map definition, such as:
1452  *      modifier_map Lock           { Caps_Lock };
1453  * where we want to add the Lock modifier to the modmap of the key
1454  * which matches the keysym Caps_Lock.
1455  * Since there can be many keys which generates the keysym, the key
1456  * is chosen first by lowest group in which the keysym appears, than
1457  * by lowest level and than by lowest key code.
1458  */
1459 static struct xkb_key *
1460 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
1461 {
1462     struct xkb_key *key, *ret = NULL;
1463     xkb_group_index_t group, min_group = UINT32_MAX;
1464     xkb_level_index_t level, min_level = UINT16_MAX;
1465
1466     xkb_foreach_key(key, keymap) {
1467         for (group = 0; group < key->num_groups; group++) {
1468             for (level = 0; level < XkbKeyGroupWidth(keymap, key, group);
1469                  level++) {
1470                 if (XkbKeyNumSyms(key, group, level) != 1 ||
1471                     (XkbKeySymEntry(key, group, level))[0] != sym)
1472                     continue;
1473
1474                 /*
1475                  * If the keysym was found in a group or level > 0, we must
1476                  * keep looking since we might find a key in which the keysym
1477                  * is in a lower group or level.
1478                  */
1479                 if (group < min_group ||
1480                     (group == min_group && level < min_level)) {
1481                     ret = key;
1482                     if (group == 0 && level == 0) {
1483                         return ret;
1484                     }
1485                     else {
1486                         min_group = group;
1487                         min_level = level;
1488                     }
1489                 }
1490             }
1491         }
1492     }
1493
1494     return ret;
1495 }
1496
1497 /**
1498  * Find the given name in the keymap->map->types and return its index.
1499  *
1500  * @param atom The atom to search for.
1501  * @param type_rtrn Set to the index of the name if found.
1502  *
1503  * @return true if found, false otherwise.
1504  */
1505 static bool
1506 FindNamedType(struct xkb_keymap *keymap, xkb_atom_t atom, unsigned *type_rtrn)
1507 {
1508     unsigned n = 0;
1509     const char *name = xkb_atom_text(keymap->ctx, atom);
1510     struct xkb_key_type *type;
1511
1512     if (keymap) {
1513         darray_foreach(type, keymap->types) {
1514             if (streq(type->name, name)) {
1515                 *type_rtrn = n;
1516                 return true;
1517             }
1518             n++;
1519         }
1520     }
1521     return false;
1522 }
1523
1524 /**
1525  * Assign a type to the given sym and return the Atom for the type assigned.
1526  *
1527  * Simple recipe:
1528  * - ONE_LEVEL for width 0/1
1529  * - ALPHABETIC for 2 shift levels, with lower/upercase
1530  * - KEYPAD for keypad keys.
1531  * - TWO_LEVEL for other 2 shift level keys.
1532  * and the same for four level keys.
1533  *
1534  * @param width Number of sysms in syms.
1535  * @param syms The keysyms for the given key (must be size width).
1536  * @param typeNameRtrn Set to the Atom of the type name.
1537  *
1538  * @returns true if a type could be found, false otherwise.
1539  *
1540  * FIXME: I need to take the KeyInfo so I can look at symsMapIndex and
1541  *        all that fun stuff rather than just assuming there's always one
1542  *        symbol per level.
1543  */
1544 static bool
1545 FindAutomaticType(struct xkb_keymap *keymap, xkb_level_index_t width,
1546                   const xkb_keysym_t *syms, xkb_atom_t *typeNameRtrn,
1547                   bool *autoType)
1548 {
1549     *autoType = false;
1550     if ((width == 1) || (width == 0)) {
1551         *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ONE_LEVEL");
1552         *autoType = true;
1553     }
1554     else if (width == 2) {
1555         if (syms && xkb_keysym_is_lower(syms[0]) &&
1556             xkb_keysym_is_upper(syms[1])) {
1557             *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ALPHABETIC");
1558         }
1559         else if (syms && (xkb_keysym_is_keypad(syms[0]) ||
1560                           xkb_keysym_is_keypad(syms[1]))) {
1561             *typeNameRtrn = xkb_atom_intern(keymap->ctx, "KEYPAD");
1562             *autoType = true;
1563         }
1564         else {
1565             *typeNameRtrn = xkb_atom_intern(keymap->ctx, "TWO_LEVEL");
1566             *autoType = true;
1567         }
1568     }
1569     else if (width <= 4) {
1570         if (syms && xkb_keysym_is_lower(syms[0]) &&
1571             xkb_keysym_is_upper(syms[1]))
1572             if (xkb_keysym_is_lower(syms[2]) && xkb_keysym_is_upper(syms[3]))
1573                 *typeNameRtrn =
1574                     xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_ALPHABETIC");
1575             else
1576                 *typeNameRtrn = xkb_atom_intern(keymap->ctx,
1577                                                 "FOUR_LEVEL_SEMIALPHABETIC");
1578
1579         else if (syms && (xkb_keysym_is_keypad(syms[0]) ||
1580                           xkb_keysym_is_keypad(syms[1])))
1581             *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_KEYPAD");
1582         else
1583             *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL");
1584         /* XXX: why not set autoType here? */
1585     }
1586     return ((width >= 0) && (width <= 4));
1587 }
1588
1589 /**
1590  * Ensure the given KeyInfo is in a coherent state, i.e. no gaps between the
1591  * groups, and reduce to one group if all groups are identical anyway.
1592  */
1593 static void
1594 PrepareKeyDef(KeyInfo *keyi)
1595 {
1596     xkb_group_index_t i, lastGroup;
1597     unsigned int defined;
1598     xkb_level_index_t j, width;
1599     bool identical;
1600
1601     defined = keyi->symsDefined | keyi->actsDefined | keyi->typesDefined;
1602     /* get highest group number */
1603     for (i = XkbNumKbdGroups - 1; i > 0; i--) {
1604         if (defined & (1 << i))
1605             break;
1606     }
1607     lastGroup = i;
1608
1609     if (lastGroup == 0)
1610         return;
1611
1612     /* If there are empty groups between non-empty ones fill them with data */
1613     /* from the first group. */
1614     /* We can make a wrong assumption here. But leaving gaps is worse. */
1615     for (i = lastGroup; i > 0; i--) {
1616         if (defined & (1 << i))
1617             continue;
1618         width = keyi->numLevels[0];
1619         if (keyi->typesDefined & 1) {
1620             for (j = 0; j < width; j++) {
1621                 keyi->types[i] = keyi->types[0];
1622             }
1623             keyi->typesDefined |= 1 << i;
1624         }
1625         if ((keyi->actsDefined & 1) && !darray_empty(keyi->acts[0])) {
1626             darray_copy(keyi->acts[i], keyi->acts[0]);
1627             keyi->actsDefined |= 1 << i;
1628         }
1629         if ((keyi->symsDefined & 1) && !darray_empty(keyi->syms[0])) {
1630             darray_copy(keyi->syms[i], keyi->syms[0]);
1631             darray_copy(keyi->symsMapIndex[i], keyi->symsMapIndex[0]);
1632             darray_copy(keyi->symsMapNumEntries[i],
1633                         keyi->symsMapNumEntries[0]);
1634             keyi->symsDefined |= 1 << i;
1635         }
1636         if (defined & 1) {
1637             keyi->numLevels[i] = keyi->numLevels[0];
1638         }
1639     }
1640     /* If all groups are completely identical remove them all */
1641     /* exept the first one. */
1642     identical = true;
1643     for (i = lastGroup; i > 0; i--) {
1644         if ((keyi->numLevels[i] != keyi->numLevels[0]) ||
1645             (keyi->types[i] != keyi->types[0])) {
1646             identical = false;
1647             break;
1648         }
1649         if (!darray_same(keyi->syms[i], keyi->syms[0]) &&
1650             (darray_empty(keyi->syms[i]) || darray_empty(keyi->syms[0]) ||
1651              darray_size(keyi->syms[i]) != darray_size(keyi->syms[0]) ||
1652              memcmp(darray_mem(keyi->syms[i], 0),
1653                     darray_mem(keyi->syms[0], 0),
1654                     sizeof(xkb_keysym_t) * darray_size(keyi->syms[0])))) {
1655             identical = false;
1656             break;
1657         }
1658         if (!darray_same(keyi->symsMapIndex[i], keyi->symsMapIndex[0]) &&
1659             (darray_empty(keyi->symsMapIndex[i]) ||
1660              darray_empty(keyi->symsMapIndex[0]) ||
1661              memcmp(darray_mem(keyi->symsMapIndex[i], 0),
1662                     darray_mem(keyi->symsMapIndex[0], 0),
1663                     keyi->numLevels[0] * sizeof(int)))) {
1664             identical = false;
1665             continue;
1666         }
1667         if (!darray_same(keyi->symsMapNumEntries[i],
1668                          keyi->symsMapNumEntries[0]) &&
1669             (darray_empty(keyi->symsMapNumEntries[i]) ||
1670              darray_empty(keyi->symsMapNumEntries[0]) ||
1671              memcmp(darray_mem(keyi->symsMapNumEntries[i], 0),
1672                     darray_mem(keyi->symsMapNumEntries[0], 0),
1673                     keyi->numLevels[0] * sizeof(size_t)))) {
1674             identical = false;
1675             continue;
1676         }
1677         if (!darray_same(keyi->acts[i], keyi->acts[0]) &&
1678             (darray_empty(keyi->acts[i]) || darray_empty(keyi->acts[0]) ||
1679              memcmp(darray_mem(keyi->acts[i], 0),
1680                     darray_mem(keyi->acts[0], 0),
1681                     keyi->numLevels[0] * sizeof(union xkb_action)))) {
1682             identical = false;
1683             break;
1684         }
1685     }
1686     if (identical) {
1687         for (i = lastGroup; i > 0; i--) {
1688             keyi->numLevels[i] = 0;
1689             darray_free(keyi->syms[i]);
1690             darray_free(keyi->symsMapIndex[i]);
1691             darray_free(keyi->symsMapNumEntries[i]);
1692             darray_free(keyi->acts[i]);
1693             keyi->types[i] = 0;
1694         }
1695         keyi->symsDefined &= 1;
1696         keyi->actsDefined &= 1;
1697         keyi->typesDefined &= 1;
1698     }
1699 }
1700
1701 /**
1702  * Copy the KeyInfo into the keyboard description.
1703  *
1704  * This function recurses.
1705  */
1706 static bool
1707 CopySymbolsDef(SymbolsInfo *info, KeyInfo *keyi,
1708                xkb_keycode_t start_from)
1709 {
1710     struct xkb_keymap *keymap = info->keymap;
1711     xkb_keycode_t kc;
1712     struct xkb_key *key;
1713     size_t sizeSyms = 0;
1714     xkb_group_index_t i, nGroups;
1715     xkb_level_index_t width, tmp;
1716     struct xkb_key_type * type;
1717     bool haveActions, autoType, useAlias;
1718     unsigned types[XkbNumKbdGroups];
1719     union xkb_action *outActs;
1720     unsigned int symIndex = 0;
1721
1722     useAlias = (start_from == 0);
1723
1724     key = FindNamedKey(keymap, keyi->name, useAlias,
1725                        CreateKeyNames(keymap), start_from);
1726     if (!key) {
1727         if (start_from == 0)
1728             log_lvl(info->keymap->ctx, 5,
1729                     "Key %s not found in keycodes; Symbols ignored\n",
1730                     LongKeyNameText(keyi->name));
1731         return false;
1732     }
1733     kc = XkbKeyGetKeycode(keymap, key);
1734
1735     haveActions = false;
1736     width = 0;
1737     for (i = nGroups = 0; i < XkbNumKbdGroups; i++) {
1738         if (((i + 1) > nGroups)
1739             && (((keyi->symsDefined | keyi->actsDefined) & (1 << i))
1740                 || (keyi->typesDefined) & (1 << i)))
1741             nGroups = i + 1;
1742         if (!darray_empty(keyi->acts[i]))
1743             haveActions = true;
1744         autoType = false;
1745         /* Assign the type to the key, if it is missing. */
1746         if (keyi->types[i] == XKB_ATOM_NONE) {
1747             if (keyi->dfltType != XKB_ATOM_NONE)
1748                 keyi->types[i] = keyi->dfltType;
1749             else if (FindAutomaticType(keymap, keyi->numLevels[i],
1750                                        darray_mem(keyi->syms[i], 0),
1751                                        &keyi->types[i], &autoType)) { }
1752             else
1753                 log_lvl(info->keymap->ctx, 5,
1754                         "No automatic type for %d symbols; "
1755                         "Using %s for the %s key (keycode %d)\n",
1756                         keyi->numLevels[i],
1757                         xkb_atom_text(keymap->ctx, keyi->types[i]),
1758                         LongKeyNameText(keyi->name), kc);
1759         }
1760         if (FindNamedType(keymap, keyi->types[i], &types[i])) {
1761             if (!autoType || keyi->numLevels[i] > 2)
1762                 key->explicit |= (1 << i);
1763         }
1764         else {
1765             log_lvl(info->keymap->ctx, 3,
1766                     "Type \"%s\" is not defined; "
1767                     "Using TWO_LEVEL for the %s key (keycode %d)\n",
1768                     xkb_atom_text(keymap->ctx, keyi->types[i]),
1769                     LongKeyNameText(keyi->name), kc);
1770             types[i] = XkbTwoLevelIndex;
1771         }
1772
1773         /* if the type specifies fewer levels than the key has, shrink the key */
1774         type = &darray_item(keymap->types, types[i]);
1775         if (type->num_levels < keyi->numLevels[i]) {
1776             log_lvl(info->keymap->ctx, 1,
1777                     "Type \"%s\" has %d levels, but %s has %d symbols; "
1778                     "Ignoring extra symbols\n",
1779                     type->name, type->num_levels,
1780                     LongKeyNameText(keyi->name),
1781                     keyi->numLevels[i]);
1782             keyi->numLevels[i] = type->num_levels;
1783         }
1784         if (keyi->numLevels[i] > width)
1785             width = keyi->numLevels[i];
1786         if (type->num_levels > width)
1787             width = type->num_levels;
1788         sizeSyms += darray_size(keyi->syms[i]);
1789     }
1790
1791     darray_resize0(key->syms, sizeSyms);
1792
1793     if (haveActions) {
1794         outActs = ResizeKeyActions(keymap, key, width * nGroups);
1795         if (outActs == NULL) {
1796             log_wsgo(info->keymap->ctx,
1797                      "Could not enlarge actions for %s (key %d)\n",
1798                      LongKeyNameText(keyi->name), kc);
1799             return false;
1800         }
1801         key->explicit |= XkbExplicitInterpretMask;
1802     }
1803     else
1804         outActs = NULL;
1805
1806     key->num_groups = nGroups;
1807     if (keyi->defined & KEY_FIELD_GROUPINFO) {
1808         key->out_of_range_group_number = keyi->out_of_range_group_number;
1809         key->out_of_range_group_action = keyi->out_of_range_group_action;
1810     }
1811     key->width = width;
1812     key->sym_index = calloc(nGroups * width, sizeof(*key->sym_index));
1813     key->num_syms = calloc(nGroups * width, sizeof(*key->num_syms));
1814
1815     for (i = 0; i < nGroups; i++) {
1816         /* assign kt_index[i] to the index of the type in map->types.
1817          * kt_index[i] may have been set by a previous run (if we have two
1818          * layouts specified). Let's not overwrite it with the ONE_LEVEL
1819          * default group if we dont even have keys for this group anyway.
1820          *
1821          * FIXME: There should be a better fix for this.
1822          */
1823         if (keyi->numLevels[i])
1824             key->kt_index[i] = types[i];
1825         if (!darray_empty(keyi->syms[i])) {
1826             /* fill key to "width" symbols*/
1827             for (tmp = 0; tmp < width; tmp++) {
1828                 if (tmp < keyi->numLevels[i] &&
1829                     darray_item(keyi->symsMapNumEntries[i], tmp) != 0) {
1830                     memcpy(darray_mem(key->syms, symIndex),
1831                            darray_mem(keyi->syms[i],
1832                                       darray_item(keyi->symsMapIndex[i], tmp)),
1833                            darray_item(keyi->symsMapNumEntries[i],
1834                                        tmp) * sizeof(xkb_keysym_t));
1835                     key->sym_index[(i * width) + tmp] = symIndex;
1836                     key->num_syms[(i * width) + tmp] =
1837                         darray_item(keyi->symsMapNumEntries[i], tmp);
1838                     symIndex += key->num_syms[(i * width) + tmp];
1839                 }
1840                 else {
1841                     key->sym_index[(i * width) + tmp] = -1;
1842                     key->num_syms[(i * width) + tmp] = 0;
1843                 }
1844                 if (outActs != NULL && !darray_empty(keyi->acts[i])) {
1845                     if (tmp < keyi->numLevels[i])
1846                         outActs[tmp] = darray_item(keyi->acts[i], tmp);
1847                     else
1848                         outActs[tmp].type = XkbSA_NoAction;
1849                 }
1850             }
1851         }
1852     }
1853     switch (keyi->behavior.type & XkbKB_OpMask) {
1854     case XkbKB_Default:
1855         break;
1856
1857     default:
1858         key->behavior = keyi->behavior;
1859         key->explicit |= XkbExplicitBehaviorMask;
1860         break;
1861     }
1862     if (keyi->defined & KEY_FIELD_VMODMAP) {
1863         key->vmodmap = keyi->vmodmap;
1864         key->explicit |= XkbExplicitVModMapMask;
1865     }
1866     if (keyi->repeat != KEY_REPEAT_UNDEFINED) {
1867         key->repeats = (keyi->repeat == KEY_REPEAT_YES);
1868         key->explicit |= XkbExplicitAutoRepeatMask;
1869     }
1870
1871     /* do the same thing for the next key */
1872     CopySymbolsDef(info, keyi, kc + 1);
1873     return true;
1874 }
1875
1876 static bool
1877 CopyModMapDef(SymbolsInfo *info, ModMapEntry *entry)
1878 {
1879     struct xkb_key *key;
1880     struct xkb_keymap *keymap = info->keymap;
1881
1882     if (!entry->haveSymbol) {
1883         key = FindNamedKey(keymap, entry->u.keyName, true,
1884                            CreateKeyNames(keymap), 0);
1885         if (!key) {
1886             log_lvl(info->keymap->ctx, 5,
1887                     "Key %s not found in keycodes; "
1888                     "Modifier map entry for %s not updated\n",
1889                     LongKeyNameText(entry->u.keyName),
1890                     ModIndexText(entry->modifier));
1891             return false;
1892         }
1893     }
1894     else {
1895         key = FindKeyForSymbol(keymap, entry->u.keySym);
1896         if (!key) {
1897             log_lvl(info->keymap->ctx, 5,
1898                     "Key \"%s\" not found in symbol map; "
1899                     "Modifier map entry for %s not updated\n",
1900                     KeysymText(entry->u.keySym),
1901                     ModIndexText(entry->modifier));
1902             return false;
1903         }
1904     }
1905
1906     key->modmap |= (1 << entry->modifier);
1907     return true;
1908 }
1909
1910 /**
1911  * Handle the xkb_symbols section of an xkb file.
1912  *
1913  * @param file The parsed xkb_symbols section of the xkb file.
1914  * @param keymap Handle to the keyboard description to store the symbols in.
1915  * @param merge Merge strategy (e.g. MERGE_OVERRIDE).
1916  */
1917 bool
1918 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1919                enum merge_mode merge)
1920 {
1921     xkb_group_index_t i;
1922     struct xkb_key *key;
1923     SymbolsInfo info;
1924     KeyInfo *keyi;
1925     ModMapEntry *mm;
1926
1927     InitSymbolsInfo(&info, keymap, file->id);
1928     info.dflt.merge = merge;
1929
1930     HandleSymbolsFile(&info, file, merge);
1931
1932     if (darray_empty(info.keys))
1933         goto err_info;
1934
1935     if (info.errorCount != 0)
1936         goto err_info;
1937
1938     darray_resize0(keymap->acts, darray_size(keymap->acts) + 32 + 1);
1939
1940     if (info.name)
1941         keymap->symbols_section_name = strdup(info.name);
1942
1943     for (i = 0; i < XkbNumKbdGroups; i++) {
1944         if (info.groupNames[i] != XKB_ATOM_NONE) {
1945             keymap->group_names[i] = xkb_atom_text(keymap->ctx,
1946                                                    info.groupNames[i]);
1947         }
1948     }
1949
1950     /* sanitize keys */
1951     darray_foreach(keyi, info.keys)
1952         PrepareKeyDef(keyi);
1953
1954     /* copy! */
1955     darray_foreach(keyi, info.keys)
1956         if (!CopySymbolsDef(&info, keyi, 0))
1957             info.errorCount++;
1958
1959     if (xkb_get_log_verbosity(keymap->ctx) > 3) {
1960         xkb_foreach_key(key, keymap) {
1961             if (key->name[0] == '\0')
1962                 continue;
1963
1964             if (key->num_groups < 1)
1965                 log_warn(info.keymap->ctx,
1966                          "No symbols defined for %s (keycode %d)\n",
1967                          KeyNameText(key->name),
1968                          XkbKeyGetKeycode(keymap, key));
1969         }
1970     }
1971
1972     list_foreach(mm, &info.modMaps, entry)
1973         if (!CopyModMapDef(&info, mm))
1974             info.errorCount++;
1975
1976     FreeSymbolsInfo(&info);
1977     return true;
1978
1979 err_info:
1980     FreeSymbolsInfo(&info);
1981     return false;
1982 }