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