Convert macros to inline functions
[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 use_new_field(unsigned field, short old_defined, unsigned old_file_id,
487               short new_defined, unsigned new_file_id, enum merge_mode new_merge,
488               unsigned *collide)
489 {
490     if (!(old_defined & field))
491         return true;
492
493     if (new_defined & field) {
494         if ((old_file_id == new_file_id && warningLevel > 0) || warningLevel > 9)
495             *collide |= field;
496
497         if (new_merge != MERGE_AUGMENT)
498             return true;
499     }
500
501     return false;
502 }
503
504 static bool
505 MergeKeys(SymbolsInfo *info, struct xkb_keymap *keymap,
506           KeyInfo *into, KeyInfo *from)
507 {
508     int i;
509     unsigned collide = 0;
510     bool report;
511
512     if (from->merge == MERGE_REPLACE) {
513         for (i = 0; i < XkbNumKbdGroups; i++) {
514             if (into->numLevels[i] != 0) {
515                 darray_free(into->syms[i]);
516                 darray_free(into->acts[i]);
517             }
518         }
519         *into = *from;
520         memset(from, 0, sizeof(KeyInfo));
521         return true;
522     }
523     report = ((warningLevel > 9) ||
524               ((into->file_id == from->file_id)
525                && (warningLevel > 0)));
526     for (i = 0; i < XkbNumKbdGroups; i++) {
527         if (from->numLevels[i] > 0) {
528             if (into->numLevels[i] == 0) {
529                 into->numLevels[i] = from->numLevels[i];
530                 into->syms[i] = from->syms[i];
531                 into->symsMapIndex[i] = from->symsMapIndex[i];
532                 into->symsMapNumEntries[i] = from->symsMapNumEntries[i];
533                 into->acts[i] = from->acts[i];
534                 into->symsDefined |= (1 << i);
535                 darray_init(from->syms[i]);
536                 darray_init(from->symsMapIndex[i]);
537                 darray_init(from->symsMapNumEntries[i]);
538                 darray_init(from->acts[i]);
539                 from->numLevels[i] = 0;
540                 from->symsDefined &= ~(1 << i);
541                 if (!darray_empty(into->syms[i]))
542                     into->defined |= _Key_Syms;
543                 if (!darray_empty(into->acts[i]))
544                     into->defined |= _Key_Acts;
545             }
546             else {
547                 if (report) {
548                     if (!darray_empty(into->syms[i]))
549                         collide |= _Key_Syms;
550                     if (!darray_empty(into->acts[i]))
551                         collide |= _Key_Acts;
552                 }
553                 MergeKeyGroups(info, into, from, (unsigned) i);
554             }
555         }
556         if (from->types[i] != XKB_ATOM_NONE) {
557             if ((into->types[i] != XKB_ATOM_NONE) && report &&
558                 (into->types[i] != from->types[i])) {
559                 xkb_atom_t use, ignore;
560                 collide |= _Key_Types;
561                 if (from->merge != MERGE_AUGMENT) {
562                     use = from->types[i];
563                     ignore = into->types[i];
564                 }
565                 else {
566                     use = into->types[i];
567                     ignore = from->types[i];
568                 }
569                 WARN
570                     ("Multiple definitions for group %d type of key %s\n",
571                     i, longText(into->name));
572                 ACTION("Using %s, ignoring %s\n",
573                        xkb_atom_text(keymap->ctx, use),
574                        xkb_atom_text(keymap->ctx, ignore));
575             }
576             if (from->merge != MERGE_AUGMENT ||
577                 into->types[i] == XKB_ATOM_NONE) {
578                 into->types[i] = from->types[i];
579             }
580         }
581     }
582     if (use_new_field(_Key_Behavior, into->defined, into->file_id,
583                       from->defined, from->file_id, from->merge, &collide)) {
584         into->behavior = from->behavior;
585         into->defined |= _Key_Behavior;
586     }
587     if (use_new_field(_Key_VModMap, into->defined, into->file_id,
588                       from->defined, from->file_id, from->merge, &collide)) {
589         into->vmodmap = from->vmodmap;
590         into->defined |= _Key_VModMap;
591     }
592     if (use_new_field(_Key_Repeat, into->defined, into->file_id,
593                       from->defined, from->file_id, from->merge, &collide)) {
594         into->repeat = from->repeat;
595         into->defined |= _Key_Repeat;
596     }
597     if (use_new_field(_Key_Type_Dflt, into->defined, into->file_id,
598                       from->defined, from->file_id, from->merge, &collide)) {
599         into->dfltType = from->dfltType;
600         into->defined |= _Key_Type_Dflt;
601     }
602     if (use_new_field(_Key_GroupInfo, into->defined, into->file_id,
603                       from->defined, from->file_id, from->merge, &collide)) {
604         into->out_of_range_group_action = from->out_of_range_group_action;
605         into->out_of_range_group_number = from->out_of_range_group_number;
606         into->defined |= _Key_GroupInfo;
607     }
608     if (collide) {
609         WARN("Symbol map for key %s redefined\n",
610              longText(into->name));
611         ACTION("Using %s definition for conflicting fields\n",
612                (from->merge == MERGE_AUGMENT ? "first" : "last"));
613     }
614     return true;
615 }
616
617 static bool
618 AddKeySymbols(SymbolsInfo *info, KeyInfo *keyi, struct xkb_keymap *keymap)
619 {
620     unsigned long real_name;
621     KeyInfo *iter, *new;
622
623     darray_foreach(iter, info->keys)
624         if (iter->name == keyi->name)
625             return MergeKeys(info, keymap, iter, keyi);
626
627     if (FindKeyNameForAlias(keymap, keyi->name, &real_name))
628         darray_foreach(iter, info->keys)
629             if (iter->name == real_name)
630                 return MergeKeys(info, keymap, iter, keyi);
631
632     darray_resize0(info->keys, darray_size(info->keys) + 1);
633     new = &darray_item(info->keys, darray_size(info->keys) - 1);
634     return CopyKeyInfo(keyi, new, true);
635 }
636
637 static bool
638 AddModMapEntry(SymbolsInfo * info, ModMapEntry * new)
639 {
640     ModMapEntry *mm;
641     bool clobber;
642
643     clobber = (new->merge != MERGE_AUGMENT);
644     list_foreach(mm, &info->modMaps, entry) {
645         if (new->haveSymbol && mm->haveSymbol
646             && (new->u.keySym == mm->u.keySym)) {
647             unsigned use, ignore;
648             if (mm->modifier != new->modifier) {
649                 if (clobber) {
650                     use = new->modifier;
651                     ignore = mm->modifier;
652                 }
653                 else {
654                     use = mm->modifier;
655                     ignore = new->modifier;
656                 }
657                 ERROR
658                     ("%s added to symbol map for multiple modifiers\n",
659                     XkbcKeysymText(new->u.keySym));
660                 ACTION("Using %s, ignoring %s.\n",
661                        XkbcModIndexText(use),
662                        XkbcModIndexText(ignore));
663                 mm->modifier = use;
664             }
665             return true;
666         }
667         if ((!new->haveSymbol) && (!mm->haveSymbol) &&
668             (new->u.keyName == mm->u.keyName)) {
669             unsigned use, ignore;
670             if (mm->modifier != new->modifier) {
671                 if (clobber) {
672                     use = new->modifier;
673                     ignore = mm->modifier;
674                 }
675                 else {
676                     use = mm->modifier;
677                     ignore = new->modifier;
678                 }
679                 ERROR("Key %s added to map for multiple modifiers\n",
680                       longText(new->u.keyName));
681                 ACTION("Using %s, ignoring %s.\n",
682                        XkbcModIndexText(use),
683                        XkbcModIndexText(ignore));
684                 mm->modifier = use;
685             }
686             return true;
687         }
688     }
689
690     mm = malloc(sizeof(*mm));
691     if (!mm) {
692         WSGO("Could not allocate modifier map entry\n");
693         ACTION("Modifier map for %s will be incomplete\n",
694                XkbcModIndexText(new->modifier));
695         return false;
696     }
697
698     *mm = *new;
699     list_add(&mm->entry, &info->modMaps);
700     return true;
701 }
702
703 /***====================================================================***/
704
705 static void
706 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
707                      enum merge_mode merge, struct xkb_keymap *keymap)
708 {
709     unsigned int i;
710     KeyInfo *keyi;
711     ModMapEntry *mm, *next;
712
713     if (from->errorCount > 0) {
714         into->errorCount += from->errorCount;
715         return;
716     }
717     if (into->name == NULL) {
718         into->name = from->name;
719         from->name = NULL;
720     }
721     for (i = 0; i < XkbNumKbdGroups; i++) {
722         if (from->groupNames[i] != XKB_ATOM_NONE) {
723             if ((merge != MERGE_AUGMENT) ||
724                 (into->groupNames[i] == XKB_ATOM_NONE))
725                 into->groupNames[i] = from->groupNames[i];
726         }
727     }
728
729     darray_foreach(keyi, from->keys) {
730         if (merge != MERGE_DEFAULT)
731             keyi->merge = merge;
732
733         if (!AddKeySymbols(into, keyi, keymap))
734             into->errorCount++;
735     }
736
737     list_foreach_safe(mm, next, &from->modMaps, entry) {
738         if (merge != MERGE_DEFAULT)
739             mm->merge = merge;
740         if (!AddModMapEntry(into, mm))
741             into->errorCount++;
742         free(mm);
743     }
744     list_init(&from->modMaps);
745 }
746
747 static void
748 HandleSymbolsFile(XkbFile *file, struct xkb_keymap *keymap,
749                   enum merge_mode merge,
750                   SymbolsInfo *info);
751
752 static bool
753 HandleIncludeSymbols(IncludeStmt *stmt, struct xkb_keymap *keymap,
754                      SymbolsInfo *info)
755 {
756     enum merge_mode newMerge;
757     XkbFile *rtrn;
758     SymbolsInfo included;
759     bool haveSelf;
760
761     haveSelf = false;
762     if ((stmt->file == NULL) && (stmt->map == NULL)) {
763         haveSelf = true;
764         included = *info;
765         memset(info, 0, sizeof(SymbolsInfo));
766     }
767     else if (ProcessIncludeFile(keymap->ctx, stmt, FILE_TYPE_SYMBOLS, &rtrn,
768                                 &newMerge)) {
769         InitSymbolsInfo(&included, keymap, rtrn->id);
770         included.merge = included.dflt.merge = MERGE_OVERRIDE;
771         if (stmt->modifier)
772             included.explicit_group = atoi(stmt->modifier) - 1;
773         else
774             included.explicit_group = info->explicit_group;
775         HandleSymbolsFile(rtrn, keymap, MERGE_OVERRIDE, &included);
776         if (stmt->stmt != NULL) {
777             free(included.name);
778             included.name = stmt->stmt;
779             stmt->stmt = NULL;
780         }
781         FreeXKBFile(rtrn);
782     }
783     else {
784         info->errorCount += 10;
785         return false;
786     }
787     if ((stmt->next != NULL) && (included.errorCount < 1)) {
788         IncludeStmt *next;
789         unsigned op;
790         SymbolsInfo next_incl;
791
792         for (next = stmt->next; next != NULL; next = next->next) {
793             if ((next->file == NULL) && (next->map == NULL)) {
794                 haveSelf = true;
795                 MergeIncludedSymbols(&included, info, next->merge, keymap);
796                 FreeSymbolsInfo(info);
797             }
798             else if (ProcessIncludeFile(keymap->ctx, next, FILE_TYPE_SYMBOLS,
799                                         &rtrn, &op)) {
800                 InitSymbolsInfo(&next_incl, keymap, rtrn->id);
801                 next_incl.merge = next_incl.dflt.merge = MERGE_OVERRIDE;
802                 if (next->modifier)
803                     next_incl.explicit_group = atoi(next->modifier) - 1;
804                 else
805                     next_incl.explicit_group = info->explicit_group;
806                 HandleSymbolsFile(rtrn, keymap, MERGE_OVERRIDE, &next_incl);
807                 MergeIncludedSymbols(&included, &next_incl, op, keymap);
808                 FreeSymbolsInfo(&next_incl);
809                 FreeXKBFile(rtrn);
810             }
811             else {
812                 info->errorCount += 10;
813                 FreeSymbolsInfo(&included);
814                 return false;
815             }
816         }
817     }
818     else if (stmt->next) {
819         info->errorCount += included.errorCount;
820     }
821     if (haveSelf)
822         *info = included;
823     else {
824         MergeIncludedSymbols(info, &included, newMerge, keymap);
825         FreeSymbolsInfo(&included);
826     }
827     return (info->errorCount == 0);
828 }
829
830 #define SYMBOLS 1
831 #define ACTIONS 2
832
833 static bool
834 GetGroupIndex(KeyInfo *keyi, struct xkb_keymap *keymap,
835               ExprDef * arrayNdx, unsigned what, unsigned *ndx_rtrn)
836 {
837     const char *name;
838     ExprResult tmp;
839
840     if (what == SYMBOLS)
841         name = "symbols";
842     else
843         name = "actions";
844
845     if (arrayNdx == NULL) {
846         int i;
847         unsigned defined;
848         if (what == SYMBOLS)
849             defined = keyi->symsDefined;
850         else
851             defined = keyi->actsDefined;
852
853         for (i = 0; i < XkbNumKbdGroups; i++) {
854             if ((defined & (1 << i)) == 0) {
855                 *ndx_rtrn = i;
856                 return true;
857             }
858         }
859         ERROR("Too many groups of %s for key %s (max %d)\n", name,
860               longText(keyi->name), XkbNumKbdGroups + 1);
861         ACTION("Ignoring %s defined for extra groups\n", name);
862         return false;
863     }
864     if (!ExprResolveGroup(keymap->ctx, arrayNdx, &tmp)) {
865         ERROR("Illegal group index for %s of key %s\n", name,
866               longText(keyi->name));
867         ACTION("Definition with non-integer array index ignored\n");
868         return false;
869     }
870     *ndx_rtrn = tmp.uval - 1;
871     return true;
872 }
873
874 static bool
875 AddSymbolsToKey(KeyInfo *keyi, struct xkb_keymap *keymap,
876                 ExprDef *arrayNdx, ExprDef *value, SymbolsInfo *info)
877 {
878     unsigned ndx, nSyms, nLevels;
879     unsigned int i;
880     long j;
881
882     if (!GetGroupIndex(keyi, keymap, arrayNdx, SYMBOLS, &ndx))
883         return false;
884     if (value == NULL) {
885         keyi->symsDefined |= (1 << ndx);
886         return true;
887     }
888     if (value->op != ExprKeysymList) {
889         ERROR("Expected a list of symbols, found %s\n", exprOpText(value->op));
890         ACTION("Ignoring symbols for group %d of %s\n", ndx + 1,
891                longText(keyi->name));
892         return false;
893     }
894     if (!darray_empty(keyi->syms[ndx])) {
895         ERROR("Symbols for key %s, group %d already defined\n",
896                longText(keyi->name), ndx + 1);
897         ACTION("Ignoring duplicate definition\n");
898         return false;
899     }
900     nSyms = darray_size(value->value.list.syms);
901     nLevels = darray_size(value->value.list.symsMapIndex);
902     if ((keyi->numLevels[ndx] < nSyms || darray_empty(keyi->syms[ndx])) &&
903         (!ResizeKeyGroup(keyi, ndx, nLevels, nSyms, false))) {
904         WSGO("Could not resize group %d of key %s to contain %d levels\n",
905              ndx + 1, longText(keyi->name), nSyms);
906         ACTION("Symbols lost\n");
907         return false;
908     }
909     keyi->symsDefined |= (1 << ndx);
910     for (i = 0; i < nLevels; i++) {
911         darray_item(keyi->symsMapIndex[ndx], i) =
912             darray_item(value->value.list.symsMapIndex, i);
913         darray_item(keyi->symsMapNumEntries[ndx], i) =
914             darray_item(value->value.list.symsNumEntries, i);
915
916         for (j = 0; j < darray_item(keyi->symsMapNumEntries[ndx], i); j++) {
917             /* FIXME: What's abort() doing here? */
918             if (darray_item(keyi->symsMapIndex[ndx], i) + j >= nSyms)
919                 abort();
920             if (!LookupKeysym(darray_item(value->value.list.syms,
921                                           darray_item(value->value.list.symsMapIndex,
922                                                       i) + j),
923                               &darray_item(keyi->syms[ndx],
924                                            darray_item(keyi->symsMapIndex[ndx],
925                                                        i) + j))) {
926                 WARN(
927                     "Could not resolve keysym %s for key %s, group %d (%s), level %d\n",
928                     darray_item(value->value.list.syms, i),
929                     longText(keyi->name),
930                     ndx + 1,
931                     xkb_atom_text(keymap->ctx, info->groupNames[ndx]), nSyms);
932                 while (--j >= 0)
933                     darray_item(keyi->syms[ndx],
934                                 darray_item(keyi->symsMapIndex[ndx],
935                                             i) + j) = XKB_KEY_NoSymbol;
936                 darray_item(keyi->symsMapIndex[ndx], i) = -1;
937                 darray_item(keyi->symsMapNumEntries[ndx], i) = 0;
938                 break;
939             }
940             if (darray_item(keyi->symsMapNumEntries[ndx], i) == 1 &&
941                 darray_item(keyi->syms[ndx],
942                             darray_item(keyi->symsMapIndex[ndx],
943                                         i) + j) == XKB_KEY_NoSymbol) {
944                 darray_item(keyi->symsMapIndex[ndx], i) = -1;
945                 darray_item(keyi->symsMapNumEntries[ndx], i) = 0;
946             }
947         }
948     }
949     for (j = keyi->numLevels[ndx] - 1;
950          j >= 0 && darray_item(keyi->symsMapNumEntries[ndx], j) == 0; j--)
951         keyi->numLevels[ndx]--;
952     return true;
953 }
954
955 static bool
956 AddActionsToKey(KeyInfo *keyi, struct xkb_keymap *keymap, ExprDef *arrayNdx,
957                 ExprDef *value, SymbolsInfo *info)
958 {
959     unsigned int i;
960     unsigned ndx, nActs;
961     ExprDef *act;
962     struct xkb_any_action *toAct;
963
964     if (!GetGroupIndex(keyi, keymap, arrayNdx, ACTIONS, &ndx))
965         return false;
966
967     if (value == NULL) {
968         keyi->actsDefined |= (1 << ndx);
969         return true;
970     }
971     if (value->op != ExprActionList) {
972         WSGO("Bad expression type (%d) for action list value\n", value->op);
973         ACTION("Ignoring actions for group %d of %s\n", ndx,
974                longText(keyi->name));
975         return false;
976     }
977     if (!darray_empty(keyi->acts[ndx])) {
978         WSGO("Actions for key %s, group %d already defined\n",
979              longText(keyi->name), ndx);
980         return false;
981     }
982     for (nActs = 0, act = value->value.child; act != NULL; nActs++) {
983         act = (ExprDef *) act->common.next;
984     }
985     if (nActs < 1) {
986         WSGO("Action list but not actions in AddActionsToKey\n");
987         return false;
988     }
989     if ((keyi->numLevels[ndx] < nActs || darray_empty(keyi->acts[ndx])) &&
990         !ResizeKeyGroup(keyi, ndx, nActs, nActs, true)) {
991         WSGO("Could not resize group %d of key %s\n", ndx,
992               longText(keyi->name));
993         ACTION("Actions lost\n");
994         return false;
995     }
996     keyi->actsDefined |= (1 << ndx);
997
998     toAct = (struct xkb_any_action *) darray_mem(keyi->acts[ndx], 0);
999     act = value->value.child;
1000     for (i = 0; i < nActs; i++, toAct++) {
1001         if (!HandleActionDef(act, keymap, toAct, info->action)) {
1002             ERROR("Illegal action definition for %s\n",
1003                   longText(keyi->name));
1004             ACTION("Action for group %d/level %d ignored\n", ndx + 1, i + 1);
1005         }
1006         act = (ExprDef *) act->common.next;
1007     }
1008     return true;
1009 }
1010
1011 static const LookupEntry lockingEntries[] = {
1012     { "true", XkbKB_Lock },
1013     { "yes", XkbKB_Lock },
1014     { "on", XkbKB_Lock },
1015     { "false", XkbKB_Default },
1016     { "no", XkbKB_Default },
1017     { "off", XkbKB_Default },
1018     { "permanent", XkbKB_Lock | XkbKB_Permanent },
1019     { NULL, 0 }
1020 };
1021
1022 static const LookupEntry repeatEntries[] = {
1023     { "true", RepeatYes },
1024     { "yes", RepeatYes },
1025     { "on", RepeatYes },
1026     { "false", RepeatNo },
1027     { "no", RepeatNo },
1028     { "off", RepeatNo },
1029     { "default", RepeatUndefined },
1030     { NULL, 0 }
1031 };
1032
1033 static bool
1034 SetSymbolsField(KeyInfo *keyi, struct xkb_keymap *keymap, char *field,
1035                 ExprDef *arrayNdx, ExprDef *value, SymbolsInfo *info)
1036 {
1037     bool ok = true;
1038     ExprResult tmp;
1039
1040     if (strcasecmp(field, "type") == 0) {
1041         ExprResult ndx;
1042         if ((!ExprResolveString(keymap->ctx, value, &tmp))
1043             && (warningLevel > 0)) {
1044             WARN("The type field of a key symbol map must be a string\n");
1045             ACTION("Ignoring illegal type definition\n");
1046         }
1047         if (arrayNdx == NULL) {
1048             keyi->dfltType = xkb_atom_intern(keymap->ctx, tmp.str);
1049             keyi->defined |= _Key_Type_Dflt;
1050         }
1051         else if (!ExprResolveGroup(keymap->ctx, arrayNdx, &ndx)) {
1052             ERROR("Illegal group index for type of key %s\n",
1053                   longText(keyi->name));
1054             ACTION("Definition with non-integer array index ignored\n");
1055             free(tmp.str);
1056             return false;
1057         }
1058         else {
1059             keyi->types[ndx.uval - 1] = xkb_atom_intern(keymap->ctx, tmp.str);
1060             keyi->typesDefined |= (1 << (ndx.uval - 1));
1061         }
1062         free(tmp.str);
1063     }
1064     else if (strcasecmp(field, "symbols") == 0)
1065         return AddSymbolsToKey(keyi, keymap, arrayNdx, value, info);
1066     else if (strcasecmp(field, "actions") == 0)
1067         return AddActionsToKey(keyi, keymap, arrayNdx, value, info);
1068     else if ((strcasecmp(field, "vmods") == 0) ||
1069              (strcasecmp(field, "virtualmods") == 0) ||
1070              (strcasecmp(field, "virtualmodifiers") == 0)) {
1071         ok = ExprResolveVModMask(value, &tmp, keymap);
1072         if (ok) {
1073             keyi->vmodmap = (tmp.uval >> 8);
1074             keyi->defined |= _Key_VModMap;
1075         }
1076         else {
1077             ERROR("Expected a virtual modifier mask, found %s\n",
1078                   exprOpText(value->op));
1079             ACTION("Ignoring virtual modifiers definition for key %s\n",
1080                    longText(keyi->name));
1081         }
1082     }
1083     else if ((strcasecmp(field, "locking") == 0) ||
1084              (strcasecmp(field, "lock") == 0) ||
1085              (strcasecmp(field, "locks") == 0)) {
1086         ok = ExprResolveEnum(keymap->ctx, value, &tmp, lockingEntries);
1087         if (ok)
1088             keyi->behavior.type = tmp.uval;
1089         keyi->defined |= _Key_Behavior;
1090     }
1091     else if ((strcasecmp(field, "radiogroup") == 0) ||
1092              (strcasecmp(field, "permanentradiogroup") == 0) ||
1093              (strcasecmp(field, "allownone") == 0)) {
1094         ERROR("Radio groups not supported\n");
1095         ACTION("Ignoring radio group specification for key %s\n",
1096                longText(keyi->name));
1097         return false;
1098     }
1099     else if (uStrCasePrefix("overlay", field) ||
1100              uStrCasePrefix("permanentoverlay", field)) {
1101         ERROR("Overlays not supported\n");
1102         ACTION("Ignoring overlay specification for key %s\n",
1103                longText(keyi->name));
1104     }
1105     else if ((strcasecmp(field, "repeating") == 0) ||
1106              (strcasecmp(field, "repeats") == 0) ||
1107              (strcasecmp(field, "repeat") == 0)) {
1108         ok = ExprResolveEnum(keymap->ctx, value, &tmp, repeatEntries);
1109         if (!ok) {
1110             ERROR("Illegal repeat setting for %s\n",
1111                   longText(keyi->name));
1112             ACTION("Non-boolean repeat setting ignored\n");
1113             return false;
1114         }
1115         keyi->repeat = tmp.uval;
1116         keyi->defined |= _Key_Repeat;
1117     }
1118     else if ((strcasecmp(field, "groupswrap") == 0) ||
1119              (strcasecmp(field, "wrapgroups") == 0)) {
1120         ok = ExprResolveBoolean(keymap->ctx, value, &tmp);
1121         if (!ok) {
1122             ERROR("Illegal groupsWrap setting for %s\n",
1123                   longText(keyi->name));
1124             ACTION("Non-boolean value ignored\n");
1125             return false;
1126         }
1127         if (tmp.uval)
1128             keyi->out_of_range_group_action = XkbWrapIntoRange;
1129         else
1130             keyi->out_of_range_group_action = XkbClampIntoRange;
1131         keyi->defined |= _Key_GroupInfo;
1132     }
1133     else if ((strcasecmp(field, "groupsclamp") == 0) ||
1134              (strcasecmp(field, "clampgroups") == 0)) {
1135         ok = ExprResolveBoolean(keymap->ctx, value, &tmp);
1136         if (!ok) {
1137             ERROR("Illegal groupsClamp setting for %s\n",
1138                   longText(keyi->name));
1139             ACTION("Non-boolean value ignored\n");
1140             return false;
1141         }
1142         if (tmp.uval)
1143             keyi->out_of_range_group_action = XkbClampIntoRange;
1144         else
1145             keyi->out_of_range_group_action = XkbWrapIntoRange;
1146         keyi->defined |= _Key_GroupInfo;
1147     }
1148     else if ((strcasecmp(field, "groupsredirect") == 0) ||
1149              (strcasecmp(field, "redirectgroups") == 0)) {
1150         if (!ExprResolveGroup(keymap->ctx, value, &tmp)) {
1151             ERROR("Illegal group index for redirect of key %s\n",
1152                   longText(keyi->name));
1153             ACTION("Definition with non-integer group ignored\n");
1154             return false;
1155         }
1156         keyi->out_of_range_group_action = XkbRedirectIntoRange;
1157         keyi->out_of_range_group_number = tmp.uval - 1;
1158         keyi->defined |= _Key_GroupInfo;
1159     }
1160     else {
1161         ERROR("Unknown field %s in a symbol interpretation\n", field);
1162         ACTION("Definition ignored\n");
1163         ok = false;
1164     }
1165     return ok;
1166 }
1167
1168 static int
1169 SetGroupName(SymbolsInfo *info, struct xkb_keymap *keymap, ExprDef *arrayNdx,
1170              ExprDef *value)
1171 {
1172     ExprResult tmp, name;
1173
1174     if ((arrayNdx == NULL) && (warningLevel > 0)) {
1175         WARN("You must specify an index when specifying a group name\n");
1176         ACTION("Group name definition without array subscript ignored\n");
1177         return false;
1178     }
1179     if (!ExprResolveGroup(keymap->ctx, arrayNdx, &tmp)) {
1180         ERROR("Illegal index in group name definition\n");
1181         ACTION("Definition with non-integer array index ignored\n");
1182         return false;
1183     }
1184     if (!ExprResolveString(keymap->ctx, value, &name)) {
1185         ERROR("Group name must be a string\n");
1186         ACTION("Illegal name for group %d ignored\n", tmp.uval);
1187         return false;
1188     }
1189     info->groupNames[tmp.uval - 1 + info->explicit_group] =
1190         xkb_atom_intern(keymap->ctx, name.str);
1191     free(name.str);
1192
1193     return true;
1194 }
1195
1196 static int
1197 HandleSymbolsVar(VarDef *stmt, struct xkb_keymap *keymap, SymbolsInfo *info)
1198 {
1199     ExprResult elem, field;
1200     ExprDef *arrayNdx;
1201     bool ret;
1202
1203     if (ExprResolveLhs(keymap, stmt->name, &elem, &field, &arrayNdx) == 0)
1204         return 0;               /* internal error, already reported */
1205     if (elem.str && (strcasecmp(elem.str, "key") == 0)) {
1206         ret = SetSymbolsField(&info->dflt, keymap, field.str, arrayNdx,
1207                               stmt->value, info);
1208     }
1209     else if ((elem.str == NULL) && ((strcasecmp(field.str, "name") == 0) ||
1210                                     (strcasecmp(field.str, "groupname") ==
1211                                      0))) {
1212         ret = SetGroupName(info, keymap, arrayNdx, stmt->value);
1213     }
1214     else if ((elem.str == NULL)
1215              && ((strcasecmp(field.str, "groupswrap") == 0) ||
1216                  (strcasecmp(field.str, "wrapgroups") == 0))) {
1217         ERROR("Global \"groupswrap\" not supported\n");
1218         ACTION("Ignored\n");
1219         ret = true;
1220     }
1221     else if ((elem.str == NULL)
1222              && ((strcasecmp(field.str, "groupsclamp") == 0) ||
1223                  (strcasecmp(field.str, "clampgroups") == 0))) {
1224         ERROR("Global \"groupsclamp\" not supported\n");
1225         ACTION("Ignored\n");
1226         ret = true;
1227     }
1228     else if ((elem.str == NULL)
1229              && ((strcasecmp(field.str, "groupsredirect") == 0) ||
1230                  (strcasecmp(field.str, "redirectgroups") == 0))) {
1231         ERROR("Global \"groupsredirect\" not supported\n");
1232         ACTION("Ignored\n");
1233         ret = true;
1234     }
1235     else if ((elem.str == NULL) &&
1236              (strcasecmp(field.str, "allownone") == 0)) {
1237         ERROR("Radio groups not supported\n");
1238         ACTION("Ignoring \"allownone\" specification\n");
1239         ret = true;
1240     }
1241     else {
1242         ret = SetActionField(keymap, elem.str, field.str, arrayNdx,
1243                              stmt->value, &info->action);
1244     }
1245
1246     free(elem.str);
1247     free(field.str);
1248     return ret;
1249 }
1250
1251 static bool
1252 HandleSymbolsBody(VarDef *def, struct xkb_keymap *keymap, KeyInfo *keyi,
1253                   SymbolsInfo *info)
1254 {
1255     bool ok = true;
1256     ExprResult tmp, field;
1257     ExprDef *arrayNdx;
1258
1259     for (; def != NULL; def = (VarDef *) def->common.next) {
1260         if ((def->name) && (def->name->type == ExprFieldRef)) {
1261             ok = HandleSymbolsVar(def, keymap, info);
1262             continue;
1263         }
1264         else {
1265             if (def->name == NULL) {
1266                 if ((def->value == NULL)
1267                     || (def->value->op == ExprKeysymList))
1268                     field.str = strdup("symbols");
1269                 else
1270                     field.str = strdup("actions");
1271                 arrayNdx = NULL;
1272             }
1273             else {
1274                 ok = ExprResolveLhs(keymap, def->name, &tmp, &field,
1275                                     &arrayNdx);
1276             }
1277             if (ok)
1278                 ok = SetSymbolsField(keyi, keymap, field.str, arrayNdx,
1279                                      def->value, info);
1280             free(field.str);
1281         }
1282     }
1283     return ok;
1284 }
1285
1286 static bool
1287 SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi)
1288 {
1289     unsigned group = info->explicit_group;
1290
1291     if (group == 0)
1292         return true;
1293
1294     if ((keyi->typesDefined | keyi->symsDefined | keyi->actsDefined) & ~1) {
1295         int i;
1296         WARN("For the map %s an explicit group specified\n", info->name);
1297         WARN("but key %s has more than one group defined\n",
1298              longText(keyi->name));
1299         ACTION("All groups except first one will be ignored\n");
1300         for (i = 1; i < XkbNumKbdGroups; i++) {
1301             keyi->numLevels[i] = 0;
1302             darray_free(keyi->syms[i]);
1303             darray_free(keyi->acts[i]);
1304             keyi->types[i] = 0;
1305         }
1306     }
1307     keyi->typesDefined = keyi->symsDefined = keyi->actsDefined = 1 << group;
1308
1309     keyi->numLevels[group] = keyi->numLevels[0];
1310     keyi->numLevels[0] = 0;
1311     keyi->syms[group] = keyi->syms[0];
1312     darray_init(keyi->syms[0]);
1313     keyi->symsMapIndex[group] = keyi->symsMapIndex[0];
1314     darray_init(keyi->symsMapIndex[0]);
1315     keyi->symsMapNumEntries[group] = keyi->symsMapNumEntries[0];
1316     darray_init(keyi->symsMapNumEntries[0]);
1317     keyi->acts[group] = keyi->acts[0];
1318     darray_init(keyi->acts[0]);
1319     keyi->types[group] = keyi->types[0];
1320     keyi->types[0] = 0;
1321     return true;
1322 }
1323
1324 static int
1325 HandleSymbolsDef(SymbolsDef *stmt, struct xkb_keymap *keymap,
1326                  SymbolsInfo *info)
1327 {
1328     KeyInfo keyi;
1329
1330     InitKeyInfo(&keyi, info->file_id);
1331     CopyKeyInfo(&info->dflt, &keyi, false);
1332     keyi.merge = stmt->merge;
1333     keyi.name = KeyNameToLong(stmt->keyName);
1334     if (!HandleSymbolsBody((VarDef *) stmt->symbols, keymap, &keyi, info)) {
1335         info->errorCount++;
1336         return false;
1337     }
1338
1339     if (!SetExplicitGroup(info, &keyi)) {
1340         info->errorCount++;
1341         return false;
1342     }
1343
1344     if (!AddKeySymbols(info, &keyi, keymap)) {
1345         info->errorCount++;
1346         return false;
1347     }
1348     return true;
1349 }
1350
1351 static bool
1352 HandleModMapDef(ModMapDef *def, struct xkb_keymap *keymap, SymbolsInfo *info)
1353 {
1354     ExprDef *key;
1355     ModMapEntry tmp;
1356     ExprResult rtrn;
1357     bool ok;
1358
1359     if (!LookupModIndex(keymap->ctx, NULL, def->modifier, TypeInt, &rtrn)) {
1360         ERROR("Illegal modifier map definition\n");
1361         ACTION("Ignoring map for non-modifier \"%s\"\n",
1362                xkb_atom_text(keymap->ctx, def->modifier));
1363         return false;
1364     }
1365     ok = true;
1366     tmp.modifier = rtrn.uval;
1367     for (key = def->keys; key != NULL; key = (ExprDef *) key->common.next) {
1368         if ((key->op == ExprValue) && (key->type == TypeKeyName)) {
1369             tmp.haveSymbol = false;
1370             tmp.u.keyName = KeyNameToLong(key->value.keyName);
1371         }
1372         else if (ExprResolveKeySym(keymap->ctx, key, &rtrn)) {
1373             tmp.haveSymbol = true;
1374             tmp.u.keySym = rtrn.uval;
1375         }
1376         else {
1377             ERROR("Modmap entries may contain only key names or keysyms\n");
1378             ACTION("Illegal definition for %s modifier ignored\n",
1379                    XkbcModIndexText(tmp.modifier));
1380             continue;
1381         }
1382
1383         ok = AddModMapEntry(info, &tmp) && ok;
1384     }
1385     return ok;
1386 }
1387
1388 static void
1389 HandleSymbolsFile(XkbFile *file, struct xkb_keymap *keymap,
1390                   enum merge_mode merge, SymbolsInfo *info)
1391 {
1392     ParseCommon *stmt;
1393
1394     free(info->name);
1395     info->name = uDupString(file->name);
1396     stmt = file->defs;
1397     while (stmt)
1398     {
1399         switch (stmt->stmtType) {
1400         case StmtInclude:
1401             if (!HandleIncludeSymbols((IncludeStmt *) stmt, keymap, info))
1402                 info->errorCount++;
1403             break;
1404         case StmtSymbolsDef:
1405             if (!HandleSymbolsDef((SymbolsDef *) stmt, keymap, info))
1406                 info->errorCount++;
1407             break;
1408         case StmtVarDef:
1409             if (!HandleSymbolsVar((VarDef *) stmt, keymap, info))
1410                 info->errorCount++;
1411             break;
1412         case StmtVModDef:
1413             if (!HandleVModDef((VModDef *) stmt, keymap, merge, &info->vmods))
1414                 info->errorCount++;
1415             break;
1416         case StmtInterpDef:
1417             ERROR("Interpretation files may not include other types\n");
1418             ACTION("Ignoring definition of symbol interpretation\n");
1419             info->errorCount++;
1420             break;
1421         case StmtKeycodeDef:
1422             ERROR("Interpretation files may not include other types\n");
1423             ACTION("Ignoring definition of key name\n");
1424             info->errorCount++;
1425             break;
1426         case StmtModMapDef:
1427             if (!HandleModMapDef((ModMapDef *) stmt, keymap, info))
1428                 info->errorCount++;
1429             break;
1430         default:
1431             WSGO("Unexpected statement type %d in HandleSymbolsFile\n",
1432                  stmt->stmtType);
1433             break;
1434         }
1435         stmt = stmt->next;
1436         if (info->errorCount > 10) {
1437 #ifdef NOISY
1438             ERROR("Too many errors\n");
1439 #endif
1440             ACTION("Abandoning symbols file \"%s\"\n", file->topName);
1441             break;
1442         }
1443     }
1444 }
1445
1446 /**
1447  * Given a keysym @sym, return a key which generates it, or NULL.
1448  * This is used for example in a modifier map definition, such as:
1449  *      modifier_map Lock           { Caps_Lock };
1450  * where we want to add the Lock modifier to the modmap of the key
1451  * which matches the keysym Caps_Lock.
1452  * Since there can be many keys which generates the keysym, the key
1453  * is chosen first by lowest group in which the keysym appears, than
1454  * by lowest level and than by lowest key code.
1455  */
1456 static struct xkb_key *
1457 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
1458 {
1459     struct xkb_key *key, *ret = NULL;
1460     unsigned int group, level, min_group = UINT_MAX, min_level = UINT_MAX;
1461
1462     xkb_foreach_key(key, keymap) {
1463         for (group = 0; group < key->num_groups; group++) {
1464             for (level = 0; level < XkbKeyGroupWidth(keymap, key, group);
1465                  level++) {
1466                 if (XkbKeyNumSyms(key, group, level) != 1 ||
1467                     (XkbKeySymEntry(key, group, level))[0] != sym)
1468                     continue;
1469
1470                 /*
1471                  * If the keysym was found in a group or level > 0, we must
1472                  * keep looking since we might find a key in which the keysym
1473                  * is in a lower group or level.
1474                  */
1475                 if (group < min_group ||
1476                     (group == min_group && level < min_level)) {
1477                     ret = key;
1478                     if (group == 0 && level == 0) {
1479                         return ret;
1480                     }
1481                     else {
1482                         min_group = group;
1483                         min_level = level;
1484                     }
1485                 }
1486             }
1487         }
1488     }
1489
1490     return ret;
1491 }
1492
1493 /**
1494  * Find the given name in the keymap->map->types and return its index.
1495  *
1496  * @param atom The atom to search for.
1497  * @param type_rtrn Set to the index of the name if found.
1498  *
1499  * @return true if found, false otherwise.
1500  */
1501 static bool
1502 FindNamedType(struct xkb_keymap *keymap, xkb_atom_t atom, unsigned *type_rtrn)
1503 {
1504     unsigned n = 0;
1505     const char *name = xkb_atom_text(keymap->ctx, atom);
1506     struct xkb_key_type *type;
1507
1508     if (keymap) {
1509         darray_foreach(type, keymap->types) {
1510             if (strcmp(type->name, name) == 0) {
1511                 *type_rtrn = n;
1512                 return true;
1513             }
1514             n++;
1515         }
1516     }
1517     return false;
1518 }
1519
1520 /**
1521  * Assign a type to the given sym and return the Atom for the type assigned.
1522  *
1523  * Simple recipe:
1524  * - ONE_LEVEL for width 0/1
1525  * - ALPHABETIC for 2 shift levels, with lower/upercase
1526  * - KEYPAD for keypad keys.
1527  * - TWO_LEVEL for other 2 shift level keys.
1528  * and the same for four level keys.
1529  *
1530  * @param width Number of sysms in syms.
1531  * @param syms The keysyms for the given key (must be size width).
1532  * @param typeNameRtrn Set to the Atom of the type name.
1533  *
1534  * @returns true if a type could be found, false otherwise.
1535  *
1536  * FIXME: I need to take the KeyInfo so I can look at symsMapIndex and
1537  *        all that fun stuff rather than just assuming there's always one
1538  *        symbol per level.
1539  */
1540 static bool
1541 FindAutomaticType(struct xkb_keymap *keymap, int width,
1542                   const xkb_keysym_t *syms, xkb_atom_t *typeNameRtrn,
1543                   bool *autoType)
1544 {
1545     *autoType = false;
1546     if ((width == 1) || (width == 0)) {
1547         *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ONE_LEVEL");
1548         *autoType = true;
1549     }
1550     else if (width == 2) {
1551         if (syms && xkb_keysym_is_lower(syms[0]) &&
1552             xkb_keysym_is_upper(syms[1])) {
1553             *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ALPHABETIC");
1554         }
1555         else if (syms && (xkb_keysym_is_keypad(syms[0]) ||
1556                           xkb_keysym_is_keypad(syms[1]))) {
1557             *typeNameRtrn = xkb_atom_intern(keymap->ctx, "KEYPAD");
1558             *autoType = true;
1559         }
1560         else {
1561             *typeNameRtrn = xkb_atom_intern(keymap->ctx, "TWO_LEVEL");
1562             *autoType = true;
1563         }
1564     }
1565     else if (width <= 4) {
1566         if (syms && xkb_keysym_is_lower(syms[0]) &&
1567             xkb_keysym_is_upper(syms[1]))
1568             if (xkb_keysym_is_lower(syms[2]) && xkb_keysym_is_upper(syms[3]))
1569                 *typeNameRtrn =
1570                     xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_ALPHABETIC");
1571             else
1572                 *typeNameRtrn = xkb_atom_intern(keymap->ctx,
1573                                                 "FOUR_LEVEL_SEMIALPHABETIC");
1574
1575         else if (syms && (xkb_keysym_is_keypad(syms[0]) ||
1576                           xkb_keysym_is_keypad(syms[1])))
1577             *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_KEYPAD");
1578         else
1579             *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL");
1580         /* XXX: why not set autoType here? */
1581     }
1582     return ((width >= 0) && (width <= 4));
1583 }
1584
1585 /**
1586  * Ensure the given KeyInfo is in a coherent state, i.e. no gaps between the
1587  * groups, and reduce to one group if all groups are identical anyway.
1588  */
1589 static void
1590 PrepareKeyDef(KeyInfo *keyi)
1591 {
1592     int i, j, width, defined, lastGroup;
1593     bool identical;
1594
1595     defined = keyi->symsDefined | keyi->actsDefined | keyi->typesDefined;
1596     /* get highest group number */
1597     for (i = XkbNumKbdGroups - 1; i >= 0; i--) {
1598         if (defined & (1 << i))
1599             break;
1600     }
1601     lastGroup = i;
1602
1603     if (lastGroup == 0)
1604         return;
1605
1606     /* If there are empty groups between non-empty ones fill them with data */
1607     /* from the first group. */
1608     /* We can make a wrong assumption here. But leaving gaps is worse. */
1609     for (i = lastGroup; i > 0; i--) {
1610         if (defined & (1 << i))
1611             continue;
1612         width = keyi->numLevels[0];
1613         if (keyi->typesDefined & 1) {
1614             for (j = 0; j < width; j++) {
1615                 keyi->types[i] = keyi->types[0];
1616             }
1617             keyi->typesDefined |= 1 << i;
1618         }
1619         if ((keyi->actsDefined & 1) && !darray_empty(keyi->acts[0])) {
1620             darray_copy(keyi->acts[i], keyi->acts[0]);
1621             keyi->actsDefined |= 1 << i;
1622         }
1623         if ((keyi->symsDefined & 1) && !darray_empty(keyi->syms[0])) {
1624             darray_copy(keyi->syms[i], keyi->syms[0]);
1625             darray_copy(keyi->symsMapIndex[i], keyi->symsMapIndex[0]);
1626             darray_copy(keyi->symsMapNumEntries[i],
1627                         keyi->symsMapNumEntries[0]);
1628             keyi->symsDefined |= 1 << i;
1629         }
1630         if (defined & 1) {
1631             keyi->numLevels[i] = keyi->numLevels[0];
1632         }
1633     }
1634     /* If all groups are completely identical remove them all */
1635     /* exept the first one. */
1636     identical = true;
1637     for (i = lastGroup; i > 0; i--) {
1638         if ((keyi->numLevels[i] != keyi->numLevels[0]) ||
1639             (keyi->types[i] != keyi->types[0])) {
1640             identical = false;
1641             break;
1642         }
1643         if (!darray_same(keyi->syms[i], keyi->syms[0]) &&
1644             (darray_empty(keyi->syms[i]) || darray_empty(keyi->syms[0]) ||
1645              darray_size(keyi->syms[i]) != darray_size(keyi->syms[0]) ||
1646              memcmp(darray_mem(keyi->syms[i], 0),
1647                     darray_mem(keyi->syms[0], 0),
1648                    sizeof(xkb_keysym_t) * darray_size(keyi->syms[0])))) {
1649             identical = false;
1650             break;
1651         }
1652         if (!darray_same(keyi->symsMapIndex[i], keyi->symsMapIndex[0]) &&
1653             (darray_empty(keyi->symsMapIndex[i]) ||
1654              darray_empty(keyi->symsMapIndex[0]) ||
1655              memcmp(darray_mem(keyi->symsMapIndex[i], 0),
1656                     darray_mem(keyi->symsMapIndex[0], 0),
1657                     keyi->numLevels[0] * sizeof(int)))) {
1658             identical = false;
1659             continue;
1660         }
1661         if (!darray_same(keyi->symsMapNumEntries[i],
1662                          keyi->symsMapNumEntries[0]) &&
1663             (darray_empty(keyi->symsMapNumEntries[i]) ||
1664              darray_empty(keyi->symsMapNumEntries[0]) ||
1665              memcmp(darray_mem(keyi->symsMapNumEntries[i], 0),
1666                     darray_mem(keyi->symsMapNumEntries[0], 0),
1667                     keyi->numLevels[0] * sizeof(size_t)))) {
1668             identical = false;
1669             continue;
1670         }
1671         if (!darray_same(keyi->acts[i], keyi->acts[0]) &&
1672             (darray_empty(keyi->acts[i]) || darray_empty(keyi->acts[0]) ||
1673              memcmp(darray_mem(keyi->acts[i], 0),
1674                     darray_mem(keyi->acts[0], 0),
1675                     keyi->numLevels[0] * sizeof(union xkb_action)))) {
1676             identical = false;
1677             break;
1678         }
1679     }
1680     if (identical) {
1681         for (i = lastGroup; i > 0; i--) {
1682             keyi->numLevels[i] = 0;
1683             darray_free(keyi->syms[i]);
1684             darray_free(keyi->symsMapIndex[i]);
1685             darray_free(keyi->symsMapNumEntries[i]);
1686             darray_free(keyi->acts[i]);
1687             keyi->types[i] = 0;
1688         }
1689         keyi->symsDefined &= 1;
1690         keyi->actsDefined &= 1;
1691         keyi->typesDefined &= 1;
1692     }
1693 }
1694
1695 /**
1696  * Copy the KeyInfo into the keyboard description.
1697  *
1698  * This function recurses.
1699  */
1700 static bool
1701 CopySymbolsDef(struct xkb_keymap *keymap, KeyInfo *keyi, int start_from)
1702 {
1703     unsigned int i;
1704     xkb_keycode_t kc;
1705     struct xkb_key *key;
1706     unsigned int sizeSyms = 0;
1707     unsigned width, tmp, nGroups;
1708     struct xkb_key_type * type;
1709     bool haveActions, autoType, useAlias;
1710     unsigned types[XkbNumKbdGroups];
1711     union xkb_action *outActs;
1712     unsigned int symIndex = 0;
1713
1714     useAlias = (start_from == 0);
1715
1716     key = FindNamedKey(keymap, keyi->name, useAlias,
1717                        CreateKeyNames(keymap), start_from);
1718     if (!key) {
1719         if (start_from == 0 && warningLevel >= 5) {
1720             WARN("Key %s not found in keycodes\n", longText(keyi->name));
1721             ACTION("Symbols ignored\n");
1722         }
1723         return false;
1724     }
1725     kc = XkbKeyGetKeycode(keymap, key);
1726
1727     haveActions = false;
1728     for (i = width = nGroups = 0; i < XkbNumKbdGroups; i++) {
1729         if (((i + 1) > nGroups)
1730             && (((keyi->symsDefined | keyi->actsDefined) & (1 << i))
1731                 || (keyi->typesDefined) & (1 << i)))
1732             nGroups = i + 1;
1733         if (!darray_empty(keyi->acts[i]))
1734             haveActions = true;
1735         autoType = false;
1736         /* Assign the type to the key, if it is missing. */
1737         if (keyi->types[i] == XKB_ATOM_NONE) {
1738             if (keyi->dfltType != XKB_ATOM_NONE)
1739                 keyi->types[i] = keyi->dfltType;
1740             else if (FindAutomaticType(keymap, keyi->numLevels[i],
1741                                        darray_mem(keyi->syms[i], 0),
1742                                        &keyi->types[i], &autoType)) { }
1743             else {
1744                 if (warningLevel >= 5) {
1745                     WARN("No automatic type for %d symbols\n",
1746                           (unsigned int) keyi->numLevels[i]);
1747                     ACTION("Using %s for the %s key (keycode %d)\n",
1748                             xkb_atom_text(keymap->ctx, keyi->types[i]),
1749                             longText(keyi->name), kc);
1750                 }
1751             }
1752         }
1753         if (FindNamedType(keymap, keyi->types[i], &types[i])) {
1754             if (!autoType || keyi->numLevels[i] > 2)
1755                 key->explicit |= (1 << i);
1756         }
1757         else {
1758             if (warningLevel >= 3) {
1759                 WARN("Type \"%s\" is not defined\n",
1760                      xkb_atom_text(keymap->ctx, keyi->types[i]));
1761                 ACTION("Using TWO_LEVEL for the %s key (keycode %d)\n",
1762                        longText(keyi->name), kc);
1763             }
1764             types[i] = XkbTwoLevelIndex;
1765         }
1766         /* if the type specifies fewer levels than the key has, shrink the key */
1767         type = &darray_item(keymap->types, types[i]);
1768         if (type->num_levels < keyi->numLevels[i]) {
1769             if (warningLevel > 0) {
1770                 WARN("Type \"%s\" has %d levels, but %s has %d symbols\n",
1771                      type->name, type->num_levels,
1772                      xkb_atom_text(keymap->ctx, keyi->name), keyi->numLevels[i]);
1773                 ACTION("Ignoring extra symbols\n");
1774             }
1775             keyi->numLevels[i] = type->num_levels;
1776         }
1777         if (keyi->numLevels[i] > width)
1778             width = keyi->numLevels[i];
1779         if (type->num_levels > width)
1780             width = type->num_levels;
1781         sizeSyms += darray_size(keyi->syms[i]);
1782     }
1783
1784     darray_resize0(key->syms, sizeSyms);
1785
1786     if (haveActions) {
1787         outActs = XkbcResizeKeyActions(keymap, key, width * nGroups);
1788         if (outActs == NULL) {
1789             WSGO("Could not enlarge actions for %s (key %d)\n",
1790                  longText(keyi->name), kc);
1791             return false;
1792         }
1793         key->explicit |= XkbExplicitInterpretMask;
1794     }
1795     else
1796         outActs = NULL;
1797
1798     key->num_groups = nGroups;
1799     if (keyi->defined & _Key_GroupInfo) {
1800         key->out_of_range_group_number = keyi->out_of_range_group_number;
1801         key->out_of_range_group_action = keyi->out_of_range_group_action;
1802     }
1803     key->width = width;
1804     key->sym_index = calloc(nGroups * width, sizeof(*key->sym_index));
1805     key->num_syms = calloc(nGroups * width, sizeof(*key->num_syms));
1806
1807     for (i = 0; i < nGroups; i++) {
1808         /* assign kt_index[i] to the index of the type in map->types.
1809          * kt_index[i] may have been set by a previous run (if we have two
1810          * layouts specified). Let's not overwrite it with the ONE_LEVEL
1811          * default group if we dont even have keys for this group anyway.
1812          *
1813          * FIXME: There should be a better fix for this.
1814          */
1815         if (keyi->numLevels[i])
1816             key->kt_index[i] = types[i];
1817         if (!darray_empty(keyi->syms[i])) {
1818             /* fill key to "width" symbols*/
1819             for (tmp = 0; tmp < width; tmp++) {
1820                 if (tmp < keyi->numLevels[i] &&
1821                     darray_item(keyi->symsMapNumEntries[i], tmp) != 0) {
1822                     memcpy(darray_mem(key->syms, symIndex),
1823                            darray_mem(keyi->syms[i],
1824                                       darray_item(keyi->symsMapIndex[i], tmp)),
1825                            darray_item(keyi->symsMapNumEntries[i],
1826                                        tmp) * sizeof(xkb_keysym_t));
1827                     key->sym_index[(i * width) + tmp] = symIndex;
1828                     key->num_syms[(i * width) + tmp] =
1829                         darray_item(keyi->symsMapNumEntries[i], tmp);
1830                     symIndex += key->num_syms[(i * width) + tmp];
1831                 }
1832                 else {
1833                     key->sym_index[(i * width) + tmp] = -1;
1834                     key->num_syms[(i * width) + tmp] = 0;
1835                 }
1836                 if (outActs != NULL && !darray_empty(keyi->acts[i])) {
1837                     if (tmp < keyi->numLevels[i])
1838                         outActs[tmp] = darray_item(keyi->acts[i], tmp);
1839                     else
1840                         outActs[tmp].type = XkbSA_NoAction;
1841                 }
1842             }
1843         }
1844     }
1845     switch (keyi->behavior.type & XkbKB_OpMask) {
1846     case XkbKB_Default:
1847         break;
1848
1849     default:
1850         key->behavior = keyi->behavior;
1851         key->explicit |= XkbExplicitBehaviorMask;
1852         break;
1853     }
1854     if (keyi->defined & _Key_VModMap) {
1855         key->vmodmap = keyi->vmodmap;
1856         key->explicit |= XkbExplicitVModMapMask;
1857     }
1858     if (keyi->repeat != RepeatUndefined) {
1859         key->repeats = keyi->repeat == RepeatYes;
1860         key->explicit |= XkbExplicitAutoRepeatMask;
1861     }
1862
1863     /* do the same thing for the next key */
1864     CopySymbolsDef(keymap, keyi, kc + 1);
1865     return true;
1866 }
1867
1868 static bool
1869 CopyModMapDef(struct xkb_keymap *keymap, ModMapEntry *entry)
1870 {
1871     struct xkb_key *key;
1872
1873     if (!entry->haveSymbol) {
1874         key = FindNamedKey(keymap, entry->u.keyName, true,
1875                            CreateKeyNames(keymap), 0);
1876         if (!key) {
1877             if (warningLevel >= 5) {
1878                 WARN("Key %s not found in keycodes\n",
1879                      longText(entry->u.keyName));
1880                 ACTION("Modifier map entry for %s not updated\n",
1881                        XkbcModIndexText(entry->modifier));
1882             }
1883             return false;
1884         }
1885     }
1886     else {
1887         key = FindKeyForSymbol(keymap, entry->u.keySym);
1888         if (!key) {
1889             if (warningLevel > 5) {
1890                 WARN("Key \"%s\" not found in symbol map\n",
1891                      XkbcKeysymText(entry->u.keySym));
1892                 ACTION("Modifier map entry for %s not updated\n",
1893                        XkbcModIndexText(entry->modifier));
1894             }
1895             return false;
1896         }
1897     }
1898
1899     key->modmap |= (1 << entry->modifier);
1900     return true;
1901 }
1902
1903 /**
1904  * Handle the xkb_symbols section of an xkb file.
1905  *
1906  * @param file The parsed xkb_symbols section of the xkb file.
1907  * @param keymap Handle to the keyboard description to store the symbols in.
1908  * @param merge Merge strategy (e.g. MERGE_OVERRIDE).
1909  */
1910 bool
1911 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1912                enum merge_mode merge)
1913 {
1914     int i;
1915     struct xkb_key *key;
1916     SymbolsInfo info;
1917     KeyInfo *keyi;
1918     ModMapEntry *mm;
1919
1920     InitSymbolsInfo(&info, keymap, file->id);
1921     info.dflt.merge = merge;
1922
1923     HandleSymbolsFile(file, keymap, merge, &info);
1924
1925     if (darray_empty(info.keys))
1926         goto err_info;
1927
1928     if (info.errorCount != 0)
1929         goto err_info;
1930
1931     darray_resize0(keymap->acts, darray_size(keymap->acts) + 32 + 1);
1932
1933     if (info.name)
1934         keymap->symbols_section_name = strdup(info.name);
1935
1936     for (i = 0; i < XkbNumKbdGroups; i++) {
1937         if (info.groupNames[i] != XKB_ATOM_NONE) {
1938             free(keymap->group_names[i]);
1939             keymap->group_names[i] = xkb_atom_strdup(keymap->ctx,
1940                                                      info.groupNames[i]);
1941         }
1942     }
1943
1944     /* sanitize keys */
1945     darray_foreach(keyi, info.keys)
1946         PrepareKeyDef(keyi);
1947
1948     /* copy! */
1949     darray_foreach(keyi, info.keys)
1950         if (!CopySymbolsDef(keymap, keyi, 0))
1951             info.errorCount++;
1952
1953     if (warningLevel > 3) {
1954         xkb_foreach_key(key, keymap) {
1955             if (key->name[0] == '\0')
1956                 continue;
1957
1958             if (key->num_groups < 1)
1959                 WARN("No symbols defined for <%.4s> (keycode %d)\n",
1960                      key->name, XkbKeyGetKeycode(keymap, key));
1961         }
1962     }
1963
1964     list_foreach(mm, &info.modMaps, entry)
1965         if (!CopyModMapDef(keymap, mm))
1966             info.errorCount++;
1967
1968     FreeSymbolsInfo(&info);
1969     return true;
1970
1971 err_info:
1972     FreeSymbolsInfo(&info);
1973     return false;
1974 }