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