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