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