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