xkbcomp: handle empty keymaps
[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 /*
28  * Copyright © 2012 Intel Corporation
29  * Copyright © 2012 Ran Benita <ran234@gmail.com>
30  *
31  * Permission is hereby granted, free of charge, to any person obtaining a
32  * copy of this software and associated documentation files (the "Software"),
33  * to deal in the Software without restriction, including without limitation
34  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
35  * and/or sell copies of the Software, and to permit persons to whom the
36  * Software is furnished to do so, subject to the following conditions:
37  *
38  * The above copyright notice and this permission notice (including the next
39  * paragraph) shall be included in all copies or substantial portions of the
40  * Software.
41  *
42  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
45  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48  * DEALINGS IN THE SOFTWARE.
49  *
50  * Author: Daniel Stone <daniel@fooishbar.org>
51  *         Ran Benita <ran234@gmail.com>
52  */
53
54 #include "xkbcomp-priv.h"
55 #include "text.h"
56 #include "expr.h"
57 #include "action.h"
58 #include "vmod.h"
59 #include "include.h"
60 #include "keysym.h"
61
62 enum key_repeat {
63     KEY_REPEAT_UNDEFINED = 0,
64     KEY_REPEAT_YES = 1,
65     KEY_REPEAT_NO = 2,
66 };
67
68 enum group_field {
69     GROUP_FIELD_SYMS = (1 << 0),
70     GROUP_FIELD_ACTS = (1 << 1),
71     GROUP_FIELD_TYPE = (1 << 2),
72 };
73
74 enum key_field {
75     KEY_FIELD_REPEAT    = (1 << 0),
76     KEY_FIELD_DEFAULT_TYPE = (1 << 1),
77     KEY_FIELD_GROUPINFO = (1 << 2),
78     KEY_FIELD_VMODMAP   = (1 << 3),
79 };
80
81 typedef struct {
82     enum group_field defined;
83     darray(struct xkb_level) levels;
84     xkb_atom_t type;
85 } GroupInfo;
86
87 typedef struct {
88     enum key_field defined;
89     enum merge_mode merge;
90
91     xkb_atom_t name;
92
93     darray(GroupInfo) groups;
94
95     enum key_repeat repeat;
96     xkb_mod_mask_t vmodmap;
97     xkb_atom_t default_type;
98
99     enum xkb_range_exceed_type out_of_range_group_action;
100     xkb_layout_index_t out_of_range_group_number;
101 } KeyInfo;
102
103 static void
104 ClearLevelInfo(struct xkb_level *leveli)
105 {
106     if (leveli->num_syms > 1)
107         free(leveli->u.syms);
108 }
109
110 static void
111 InitGroupInfo(GroupInfo *groupi)
112 {
113     memset(groupi, 0, sizeof(*groupi));
114 }
115
116 static void
117 ClearGroupInfo(GroupInfo *groupi)
118 {
119     struct xkb_level *leveli;
120     darray_foreach(leveli, groupi->levels)
121         ClearLevelInfo(leveli);
122     darray_free(groupi->levels);
123 }
124
125 static void
126 CopyGroupInfo(GroupInfo *to, const GroupInfo *from)
127 {
128     xkb_level_index_t j;
129     to->defined = from->defined;
130     to->type = from->type;
131     darray_init(to->levels);
132     darray_copy(to->levels, from->levels);
133     for (j = 0; j < darray_size(to->levels); j++)
134         if (darray_item(from->levels, j).num_syms > 1)
135             darray_item(to->levels, j).u.syms =
136                 memdup(darray_item(from->levels, j).u.syms,
137                        darray_item(from->levels, j).num_syms,
138                        sizeof(xkb_keysym_t));
139 }
140
141 static void
142 InitKeyInfo(struct xkb_context *ctx, KeyInfo *keyi)
143 {
144     memset(keyi, 0, sizeof(*keyi));
145     keyi->merge = MERGE_OVERRIDE;
146     keyi->name = xkb_atom_intern_literal(ctx, "*");
147     keyi->out_of_range_group_action = RANGE_WRAP;
148 }
149
150 static void
151 ClearKeyInfo(KeyInfo *keyi)
152 {
153     GroupInfo *groupi;
154     darray_foreach(groupi, keyi->groups)
155         ClearGroupInfo(groupi);
156     darray_free(keyi->groups);
157 }
158
159 /***====================================================================***/
160
161 typedef struct {
162     enum merge_mode merge;
163     bool haveSymbol;
164     xkb_mod_index_t modifier;
165     union {
166         xkb_atom_t keyName;
167         xkb_keysym_t keySym;
168     } u;
169 } ModMapEntry;
170
171 typedef struct {
172     char *name;         /* e.g. pc+us+inet(evdev) */
173     int errorCount;
174     enum merge_mode merge;
175     xkb_layout_index_t explicit_group;
176     darray(KeyInfo) keys;
177     KeyInfo default_key;
178     ActionsInfo *actions;
179     darray(xkb_atom_t) group_names;
180     darray(ModMapEntry) modMaps;
181
182     struct xkb_keymap *keymap;
183 } SymbolsInfo;
184
185 static void
186 InitSymbolsInfo(SymbolsInfo *info, struct xkb_keymap *keymap,
187                 ActionsInfo *actions)
188 {
189     memset(info, 0, sizeof(*info));
190     info->keymap = keymap;
191     info->merge = MERGE_OVERRIDE;
192     InitKeyInfo(keymap->ctx, &info->default_key);
193     info->actions = actions;
194     info->explicit_group = XKB_LAYOUT_INVALID;
195 }
196
197 static void
198 ClearSymbolsInfo(SymbolsInfo *info)
199 {
200     KeyInfo *keyi;
201     free(info->name);
202     darray_foreach(keyi, info->keys)
203         ClearKeyInfo(keyi);
204     darray_free(info->keys);
205     darray_free(info->group_names);
206     darray_free(info->modMaps);
207     ClearKeyInfo(&info->default_key);
208 }
209
210 static const char *
211 KeyInfoText(SymbolsInfo *info, KeyInfo *keyi)
212 {
213     return KeyNameText(info->keymap->ctx, keyi->name);
214 }
215
216 static bool
217 MergeGroups(SymbolsInfo *info, GroupInfo *into, GroupInfo *from, bool clobber,
218             bool report, xkb_layout_index_t group, xkb_atom_t key_name)
219 {
220     xkb_level_index_t i, levels_in_both;
221     struct xkb_context *ctx = info->keymap->ctx;
222
223     /* First find the type of the merged group. */
224     if (into->type != from->type) {
225         if (from->type == XKB_ATOM_NONE) {
226         }
227         else if (into->type == XKB_ATOM_NONE) {
228             into->type = from->type;
229         }
230         else {
231             xkb_atom_t use = (clobber ? from->type : into->type);
232             xkb_atom_t ignore = (clobber ? into->type : from->type);
233
234             if (report)
235                 log_warn(info->keymap->ctx,
236                          "Multiple definitions for group %d type of key %s; "
237                          "Using %s, ignoring %s\n",
238                          group + 1, KeyNameText(ctx, key_name),
239                          xkb_atom_text(ctx, use), xkb_atom_text(ctx, ignore));
240
241             into->type = use;
242         }
243     }
244     into->defined |= (from->defined & GROUP_FIELD_TYPE);
245
246     /* Now look at the levels. */
247
248     if (darray_empty(from->levels)) {
249         InitGroupInfo(from);
250         return true;
251     }
252
253     if (darray_empty(into->levels)) {
254         from->type = into->type;
255         *into = *from;
256         InitGroupInfo(from);
257         return true;
258     }
259
260     /* Merge the actions and syms. */
261     levels_in_both = MIN(darray_size(into->levels), darray_size(from->levels));
262     for (i = 0; i < levels_in_both; i++) {
263         struct xkb_level *intoLevel = &darray_item(into->levels, i);
264         struct xkb_level *fromLevel = &darray_item(from->levels, i);
265
266         if (fromLevel->action.type == ACTION_TYPE_NONE) {
267         }
268         else if (intoLevel->action.type == ACTION_TYPE_NONE) {
269             intoLevel->action = fromLevel->action;
270         }
271         else {
272             union xkb_action *use, *ignore;
273             use = (clobber ? &fromLevel->action : &intoLevel->action);
274             ignore = (clobber ? &intoLevel->action : &fromLevel->action);
275
276             if (report)
277                 log_warn(ctx,
278                          "Multiple actions for level %d/group %u on key %s; "
279                          "Using %s, ignoring %s\n",
280                          i + 1, group + 1, KeyNameText(ctx, key_name),
281                          ActionTypeText(use->type),
282                          ActionTypeText(ignore->type));
283
284             intoLevel->action = *use;
285         }
286
287         if (fromLevel->num_syms == 0) {
288         }
289         else if (intoLevel->num_syms == 0) {
290             intoLevel->num_syms = fromLevel->num_syms;
291             if (fromLevel->num_syms > 1)
292                 intoLevel->u.syms = fromLevel->u.syms;
293             else
294                 intoLevel->u.sym = fromLevel->u.sym;
295             fromLevel->num_syms = 0;
296         }
297         else {
298             if (report)
299                 log_warn(ctx,
300                          "Multiple symbols for level %d/group %u on key %s; "
301                          "Using %s, ignoring %s\n",
302                          i + 1, group + 1, KeyNameText(ctx, key_name),
303                          (clobber ? "from" : "to"),
304                          (clobber ? "to" : "from"));
305
306             if (clobber) {
307                 ClearLevelInfo(intoLevel);
308                 intoLevel->num_syms = fromLevel->num_syms;
309                 if (fromLevel->num_syms > 1)
310                     intoLevel->u.syms = fromLevel->u.syms;
311                 else
312                     intoLevel->u.sym = fromLevel->u.sym;
313                 fromLevel->num_syms = 0;
314             }
315         }
316     }
317     /* If @from has extra levels, get them as well. */
318     for (i = levels_in_both; i < darray_size(from->levels); i++) {
319         darray_append(into->levels, darray_item(from->levels, i));
320         darray_item(from->levels, i).num_syms = 0;
321     }
322     into->defined |= (from->defined & GROUP_FIELD_ACTS);
323     into->defined |= (from->defined & GROUP_FIELD_SYMS);
324
325     return true;
326 }
327
328 static bool
329 UseNewKeyField(enum key_field field, enum key_field old, enum key_field new,
330                bool clobber, bool report, enum key_field *collide)
331 {
332     if (!(old & field))
333         return (new & field);
334
335     if (new & field) {
336         if (report)
337             *collide |= field;
338
339         if (clobber)
340             return true;
341     }
342
343     return false;
344 }
345
346 static bool
347 MergeKeys(SymbolsInfo *info, KeyInfo *into, KeyInfo *from, bool same_file)
348 {
349     xkb_layout_index_t i;
350     xkb_layout_index_t groups_in_both;
351     enum key_field collide = 0;
352     const int verbosity = xkb_context_get_log_verbosity(info->keymap->ctx);
353     const bool clobber = (from->merge != MERGE_AUGMENT);
354     const bool report = (same_file && verbosity > 0) || verbosity > 9;
355
356     if (from->merge == MERGE_REPLACE) {
357         ClearKeyInfo(into);
358         *into = *from;
359         InitKeyInfo(info->keymap->ctx, from);
360         return true;
361     }
362
363     groups_in_both = MIN(darray_size(into->groups), darray_size(from->groups));
364     for (i = 0; i < groups_in_both; i++)
365         MergeGroups(info,
366                     &darray_item(into->groups, i),
367                     &darray_item(from->groups, i),
368                     clobber, report, i, into->name);
369     /* If @from has extra groups, just move them to @into. */
370     for (i = groups_in_both; i < darray_size(from->groups); i++) {
371         darray_append(into->groups, darray_item(from->groups, i));
372         InitGroupInfo(&darray_item(from->groups, i));
373     }
374
375     if (UseNewKeyField(KEY_FIELD_VMODMAP, into->defined, from->defined,
376                        clobber, report, &collide)) {
377         into->vmodmap = from->vmodmap;
378         into->defined |= KEY_FIELD_VMODMAP;
379     }
380     if (UseNewKeyField(KEY_FIELD_REPEAT, into->defined, from->defined,
381                        clobber, report, &collide)) {
382         into->repeat = from->repeat;
383         into->defined |= KEY_FIELD_REPEAT;
384     }
385     if (UseNewKeyField(KEY_FIELD_DEFAULT_TYPE, into->defined, from->defined,
386                        clobber, report, &collide)) {
387         into->default_type = from->default_type;
388         into->defined |= KEY_FIELD_DEFAULT_TYPE;
389     }
390     if (UseNewKeyField(KEY_FIELD_GROUPINFO, into->defined, from->defined,
391                        clobber, report, &collide)) {
392         into->out_of_range_group_action = from->out_of_range_group_action;
393         into->out_of_range_group_number = from->out_of_range_group_number;
394         into->defined |= KEY_FIELD_GROUPINFO;
395     }
396
397     if (collide)
398         log_warn(info->keymap->ctx,
399                  "Symbol map for key %s redefined; "
400                  "Using %s definition for conflicting fields\n",
401                  KeyNameText(info->keymap->ctx, into->name),
402                  (clobber ? "first" : "last"));
403
404     ClearKeyInfo(from);
405     InitKeyInfo(info->keymap->ctx, from);
406     return true;
407 }
408
409 static bool
410 AddKeySymbols(SymbolsInfo *info, KeyInfo *keyi, bool same_file)
411 {
412     xkb_atom_t real_name;
413     KeyInfo *iter;
414
415     /*
416      * Don't keep aliases in the keys array; this guarantees that
417      * searching for keys to merge with by straight comparison (see the
418      * following loop) is enough, and we won't get multiple KeyInfo's
419      * for the same key because of aliases.
420      */
421     real_name = XkbResolveKeyAlias(info->keymap, keyi->name);
422     if (real_name != XKB_ATOM_NONE)
423         keyi->name = real_name;
424
425     darray_foreach(iter, info->keys)
426         if (iter->name == keyi->name)
427             return MergeKeys(info, iter, keyi, same_file);
428
429     darray_append(info->keys, *keyi);
430     InitKeyInfo(info->keymap->ctx, keyi);
431     return true;
432 }
433
434 static bool
435 AddModMapEntry(SymbolsInfo *info, ModMapEntry *new)
436 {
437     ModMapEntry *old;
438     bool clobber = (new->merge != MERGE_AUGMENT);
439
440     darray_foreach(old, info->modMaps) {
441         xkb_mod_index_t use, ignore;
442
443         if ((new->haveSymbol != old->haveSymbol) ||
444             (new->haveSymbol && new->u.keySym != old->u.keySym) ||
445             (!new->haveSymbol && new->u.keyName != old->u.keyName))
446             continue;
447
448         if (new->modifier == old->modifier)
449             return true;
450
451         use = (clobber ? new->modifier : old->modifier);
452         ignore = (clobber ? old->modifier : new->modifier);
453
454         if (new->haveSymbol)
455             log_err(info->keymap->ctx,
456                     "Symbol \"%s\" added to modifier map for multiple modifiers; "
457                     "Using %s, ignoring %s\n",
458                     KeysymText(info->keymap->ctx, new->u.keySym),
459                     ModIndexText(info->keymap, use),
460                     ModIndexText(info->keymap, ignore));
461         else
462             log_err(info->keymap->ctx,
463                     "Key \"%s\" added to modifier map for multiple modifiers; "
464                     "Using %s, ignoring %s\n",
465                     KeyNameText(info->keymap->ctx, new->u.keyName),
466                     ModIndexText(info->keymap, use),
467                     ModIndexText(info->keymap, ignore));
468
469         old->modifier = use;
470         return true;
471     }
472
473     darray_append(info->modMaps, *new);
474     return true;
475 }
476
477 /***====================================================================***/
478
479 static void
480 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
481                      enum merge_mode merge)
482 {
483     unsigned int i;
484     KeyInfo *keyi;
485     ModMapEntry *mm;
486     xkb_atom_t *group_name;
487     xkb_layout_index_t group_names_in_both;
488
489     if (from->errorCount > 0) {
490         into->errorCount += from->errorCount;
491         return;
492     }
493
494     if (into->name == NULL) {
495         into->name = from->name;
496         from->name = NULL;
497     }
498
499     group_names_in_both = MIN(darray_size(into->group_names),
500                               darray_size(from->group_names));
501     for (i = 0; i < group_names_in_both; i++) {
502         if (!darray_item(from->group_names, i))
503             continue;
504
505         if (merge == MERGE_AUGMENT && darray_item(into->group_names, i))
506             continue;
507
508         darray_item(into->group_names, i) = darray_item(from->group_names, i);
509     }
510     /* If @from has more, get them as well. */
511     darray_foreach_from(group_name, from->group_names, group_names_in_both)
512         darray_append(into->group_names, *group_name);
513
514     darray_foreach(keyi, from->keys) {
515         keyi->merge = (merge == MERGE_DEFAULT ? keyi->merge : merge);
516         if (!AddKeySymbols(into, keyi, false))
517             into->errorCount++;
518     }
519
520     darray_foreach(mm, from->modMaps) {
521         mm->merge = (merge == MERGE_DEFAULT ? mm->merge : merge);
522         if (!AddModMapEntry(into, mm))
523             into->errorCount++;
524     }
525 }
526
527 static void
528 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge);
529
530 static bool
531 HandleIncludeSymbols(SymbolsInfo *info, IncludeStmt *include)
532 {
533     SymbolsInfo included;
534
535     InitSymbolsInfo(&included, info->keymap, info->actions);
536     included.name = include->stmt;
537     include->stmt = NULL;
538
539     for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
540         SymbolsInfo next_incl;
541         XkbFile *file;
542
543         file = ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_SYMBOLS);
544         if (!file) {
545             info->errorCount += 10;
546             ClearSymbolsInfo(&included);
547             return false;
548         }
549
550         InitSymbolsInfo(&next_incl, info->keymap, info->actions);
551         if (stmt->modifier) {
552             next_incl.explicit_group = atoi(stmt->modifier) - 1;
553             if (next_incl.explicit_group >= XKB_MAX_GROUPS) {
554                 log_err(info->keymap->ctx,
555                         "Cannot set explicit group to %d - must be between 1..%d; "
556                         "Ignoring group number\n",
557                         next_incl.explicit_group + 1, XKB_MAX_GROUPS);
558                 next_incl.explicit_group = info->explicit_group;
559             }
560         }
561         else {
562             next_incl.explicit_group = info->explicit_group;
563         }
564
565         HandleSymbolsFile(&next_incl, file, MERGE_OVERRIDE);
566
567         MergeIncludedSymbols(&included, &next_incl, stmt->merge);
568
569         ClearSymbolsInfo(&next_incl);
570         FreeXkbFile(file);
571     }
572
573     MergeIncludedSymbols(info, &included, include->merge);
574     ClearSymbolsInfo(&included);
575
576     return (info->errorCount == 0);
577 }
578
579 #define SYMBOLS 1
580 #define ACTIONS 2
581
582 static bool
583 GetGroupIndex(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
584               unsigned what, xkb_layout_index_t *ndx_rtrn)
585 {
586     const char *name = (what == SYMBOLS ? "symbols" : "actions");
587
588     if (arrayNdx == NULL) {
589         xkb_layout_index_t i;
590         GroupInfo *groupi;
591         enum group_field field = (what == SYMBOLS ?
592                                   GROUP_FIELD_SYMS : GROUP_FIELD_ACTS);
593
594         darray_enumerate(i, groupi, keyi->groups) {
595             if (!(groupi->defined & field)) {
596                 *ndx_rtrn = i;
597                 return true;
598             }
599         }
600
601         if (i >= XKB_MAX_GROUPS) {
602             log_err(info->keymap->ctx,
603                     "Too many groups of %s for key %s (max %u); "
604                     "Ignoring %s defined for extra groups\n",
605                     name, KeyInfoText(info, keyi), XKB_MAX_GROUPS, name);
606             return false;
607         }
608
609         darray_resize0(keyi->groups, darray_size(keyi->groups) + 1);
610         *ndx_rtrn = darray_size(keyi->groups) - 1;
611         return true;
612     }
613
614     if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, ndx_rtrn)) {
615         log_err(info->keymap->ctx,
616                 "Illegal group index for %s of key %s\n"
617                 "Definition with non-integer array index ignored\n",
618                 name, KeyInfoText(info, keyi));
619         return false;
620     }
621
622     (*ndx_rtrn)--;
623     if (*ndx_rtrn >= darray_size(keyi->groups))
624         darray_resize0(keyi->groups, *ndx_rtrn + 1);
625
626     return true;
627 }
628
629 bool
630 LookupKeysym(const char *str, xkb_keysym_t *sym_rtrn)
631 {
632     xkb_keysym_t sym;
633
634     if (!str || istreq(str, "any") || istreq(str, "nosymbol")) {
635         *sym_rtrn = XKB_KEY_NoSymbol;
636         return 1;
637     }
638
639     if (istreq(str, "none") || istreq(str, "voidsymbol")) {
640         *sym_rtrn = XKB_KEY_VoidSymbol;
641         return 1;
642     }
643
644     sym = xkb_keysym_from_name(str, 0);
645     if (sym != XKB_KEY_NoSymbol) {
646         *sym_rtrn = sym;
647         return 1;
648     }
649
650     return 0;
651 }
652
653 static bool
654 AddSymbolsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
655                 ExprDef *value)
656 {
657     xkb_layout_index_t ndx;
658     GroupInfo *groupi;
659     xkb_level_index_t nLevels;
660     xkb_level_index_t i;
661     int j;
662
663     if (!GetGroupIndex(info, keyi, arrayNdx, SYMBOLS, &ndx))
664         return false;
665
666     groupi = &darray_item(keyi->groups, ndx);
667
668     if (value == NULL) {
669         groupi->defined |= GROUP_FIELD_SYMS;
670         return true;
671     }
672
673     if (value->op != EXPR_KEYSYM_LIST) {
674         log_err(info->keymap->ctx,
675                 "Expected a list of symbols, found %s; "
676                 "Ignoring symbols for group %u of %s\n",
677                 expr_op_type_to_string(value->op), ndx + 1,
678                 KeyInfoText(info, keyi));
679         return false;
680     }
681
682     if (groupi->defined & GROUP_FIELD_SYMS) {
683         log_err(info->keymap->ctx,
684                 "Symbols for key %s, group %u already defined; "
685                 "Ignoring duplicate definition\n",
686                 KeyInfoText(info, keyi), ndx + 1);
687         return false;
688     }
689
690     nLevels = darray_size(value->value.list.symsMapIndex);
691     if (darray_size(groupi->levels) < nLevels)
692         darray_resize0(groupi->levels, nLevels);
693
694     groupi->defined |= GROUP_FIELD_SYMS;
695
696     for (i = 0; i < nLevels; i++) {
697         unsigned int sym_index;
698         struct xkb_level *leveli = &darray_item(groupi->levels, i);
699
700         sym_index = darray_item(value->value.list.symsMapIndex, i);
701         leveli->num_syms = darray_item(value->value.list.symsNumEntries, i);
702         if (leveli->num_syms > 1)
703             leveli->u.syms = calloc(leveli->num_syms, sizeof(*leveli->u.syms));
704
705         for (j = 0; j < leveli->num_syms; j++) {
706             char *sym_name = darray_item(value->value.list.syms,
707                                          sym_index + j);
708             xkb_keysym_t keysym;
709
710             if (!LookupKeysym(sym_name, &keysym)) {
711                 const char *group_name = "unnamed";
712
713                 if (ndx < darray_size(info->group_names) &&
714                     darray_item(info->group_names, ndx))
715                     group_name = xkb_atom_text(info->keymap->ctx,
716                                                darray_item(info->group_names,
717                                                            ndx));
718
719                 log_warn(info->keymap->ctx,
720                          "Could not resolve keysym %s for key %s, group %u (%s), level %u\n",
721                          sym_name, KeyInfoText(info, keyi), ndx + 1,
722                          group_name, i);
723
724                 ClearLevelInfo(leveli);
725                 leveli->num_syms = 0;
726                 break;
727             }
728
729             if (leveli->num_syms == 1) {
730                 if (keysym == XKB_KEY_NoSymbol)
731                     leveli->num_syms = 0;
732                 else
733                     leveli->u.sym = keysym;
734             }
735             else if (leveli->num_syms > 1) {
736                 leveli->u.syms[j] = keysym;
737             }
738         }
739     }
740
741     return true;
742 }
743
744 static bool
745 AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
746                 ExprDef *value)
747 {
748     unsigned int i;
749     xkb_layout_index_t ndx;
750     GroupInfo *groupi;
751     unsigned int nActs;
752     ExprDef *act;
753     union xkb_action *toAct;
754
755     if (!GetGroupIndex(info, keyi, arrayNdx, ACTIONS, &ndx))
756         return false;
757
758     groupi = &darray_item(keyi->groups, ndx);
759
760     if (value == NULL) {
761         groupi->defined |= GROUP_FIELD_ACTS;
762         return true;
763     }
764
765     if (value->op != EXPR_ACTION_LIST) {
766         log_wsgo(info->keymap->ctx,
767                  "Bad expression type (%d) for action list value; "
768                  "Ignoring actions for group %u of %s\n",
769                  value->op, ndx, KeyInfoText(info, keyi));
770         return false;
771     }
772
773     if (groupi->defined & GROUP_FIELD_ACTS) {
774         log_wsgo(info->keymap->ctx,
775                  "Actions for key %s, group %u already defined\n",
776                  KeyInfoText(info, keyi), ndx);
777         return false;
778     }
779
780     nActs = 0;
781     for (act = value->value.child; act; act = (ExprDef *) act->common.next)
782         nActs++;
783
784     if (darray_size(groupi->levels) < nActs)
785         darray_resize0(groupi->levels, nActs);
786
787     groupi->defined |= GROUP_FIELD_ACTS;
788
789     act = value->value.child;
790     for (i = 0; i < nActs; i++) {
791         toAct = &darray_item(groupi->levels, i).action;
792
793         if (!HandleActionDef(act, info->keymap, toAct, info->actions))
794             log_err(info->keymap->ctx,
795                     "Illegal action definition for %s; "
796                     "Action for group %u/level %u ignored\n",
797                     KeyInfoText(info, keyi), ndx + 1, i + 1);
798
799         act = (ExprDef *) act->common.next;
800     }
801
802     return true;
803 }
804
805 static const LookupEntry repeatEntries[] = {
806     { "true", KEY_REPEAT_YES },
807     { "yes", KEY_REPEAT_YES },
808     { "on", KEY_REPEAT_YES },
809     { "false", KEY_REPEAT_NO },
810     { "no", KEY_REPEAT_NO },
811     { "off", KEY_REPEAT_NO },
812     { "default", KEY_REPEAT_UNDEFINED },
813     { NULL, 0 }
814 };
815
816 static bool
817 SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
818                 ExprDef *arrayNdx, ExprDef *value)
819 {
820     bool ok = true;
821     struct xkb_context *ctx = info->keymap->ctx;
822
823     if (istreq(field, "type")) {
824         xkb_layout_index_t ndx;
825         xkb_atom_t val;
826
827         if (!ExprResolveString(ctx, value, &val))
828             log_vrb(ctx, 1,
829                     "The type field of a key symbol map must be a string; "
830                     "Ignoring illegal type definition\n");
831
832         if (arrayNdx == NULL) {
833             keyi->default_type = val;
834             keyi->defined |= KEY_FIELD_DEFAULT_TYPE;
835         }
836         else if (!ExprResolveGroup(ctx, arrayNdx, &ndx)) {
837             log_err(ctx,
838                     "Illegal group index for type of key %s; "
839                     "Definition with non-integer array index ignored\n",
840                     KeyInfoText(info, keyi));
841             return false;
842         }
843         else {
844             ndx--;
845             if (ndx >= darray_size(keyi->groups))
846                 darray_resize0(keyi->groups, ndx + 1);
847             darray_item(keyi->groups, ndx).type = val;
848             darray_item(keyi->groups, ndx).defined |= GROUP_FIELD_TYPE;
849         }
850     }
851     else if (istreq(field, "symbols"))
852         return AddSymbolsToKey(info, keyi, arrayNdx, value);
853     else if (istreq(field, "actions"))
854         return AddActionsToKey(info, keyi, arrayNdx, value);
855     else if (istreq(field, "vmods") ||
856              istreq(field, "virtualmods") ||
857              istreq(field, "virtualmodifiers")) {
858         xkb_mod_mask_t mask;
859
860         ok = ExprResolveModMask(info->keymap, value, MOD_VIRT, &mask);
861         if (ok) {
862             keyi->vmodmap = mask;
863             keyi->defined |= KEY_FIELD_VMODMAP;
864         }
865         else {
866             log_err(info->keymap->ctx,
867                     "Expected a virtual modifier mask, found %s; "
868                     "Ignoring virtual modifiers definition for key %s\n",
869                     expr_op_type_to_string(value->op),
870                     KeyInfoText(info, keyi));
871         }
872     }
873     else if (istreq(field, "locking") ||
874              istreq(field, "lock") ||
875              istreq(field, "locks")) {
876         log_err(info->keymap->ctx,
877                 "Key behaviors not supported; "
878                 "Ignoring locking specification for key %s\n",
879                 KeyInfoText(info, keyi));
880     }
881     else if (istreq(field, "radiogroup") ||
882              istreq(field, "permanentradiogroup") ||
883              istreq(field, "allownone")) {
884         log_err(info->keymap->ctx,
885                 "Radio groups not supported; "
886                 "Ignoring radio group specification for key %s\n",
887                 KeyInfoText(info, keyi));
888     }
889     else if (istreq_prefix("overlay", field) ||
890              istreq_prefix("permanentoverlay", field)) {
891         log_err(info->keymap->ctx,
892                 "Overlays not supported; "
893                 "Ignoring overlay specification for key %s\n",
894                 KeyInfoText(info, keyi));
895     }
896     else if (istreq(field, "repeating") ||
897              istreq(field, "repeats") ||
898              istreq(field, "repeat")) {
899         unsigned int val;
900
901         ok = ExprResolveEnum(ctx, value, &val, repeatEntries);
902         if (!ok) {
903             log_err(info->keymap->ctx,
904                     "Illegal repeat setting for %s; "
905                     "Non-boolean repeat setting ignored\n",
906                     KeyInfoText(info, keyi));
907             return false;
908         }
909         keyi->repeat = val;
910         keyi->defined |= KEY_FIELD_REPEAT;
911     }
912     else if (istreq(field, "groupswrap") ||
913              istreq(field, "wrapgroups")) {
914         bool set;
915
916         if (!ExprResolveBoolean(ctx, value, &set)) {
917             log_err(info->keymap->ctx,
918                     "Illegal groupsWrap setting for %s; "
919                     "Non-boolean value ignored\n",
920                     KeyInfoText(info, keyi));
921             return false;
922         }
923
924         if (set)
925             keyi->out_of_range_group_action = RANGE_WRAP;
926         else
927             keyi->out_of_range_group_action = RANGE_SATURATE;
928
929         keyi->defined |= KEY_FIELD_GROUPINFO;
930     }
931     else if (istreq(field, "groupsclamp") ||
932              istreq(field, "clampgroups")) {
933         bool set;
934
935         if (!ExprResolveBoolean(ctx, value, &set)) {
936             log_err(info->keymap->ctx,
937                     "Illegal groupsClamp setting for %s; "
938                     "Non-boolean value ignored\n",
939                     KeyInfoText(info, keyi));
940             return false;
941         }
942
943         if (set)
944             keyi->out_of_range_group_action = RANGE_SATURATE;
945         else
946             keyi->out_of_range_group_action = RANGE_WRAP;
947
948         keyi->defined |= KEY_FIELD_GROUPINFO;
949     }
950     else if (istreq(field, "groupsredirect") ||
951              istreq(field, "redirectgroups")) {
952         xkb_layout_index_t grp;
953
954         if (!ExprResolveGroup(ctx, value, &grp)) {
955             log_err(info->keymap->ctx,
956                     "Illegal group index for redirect of key %s; "
957                     "Definition with non-integer group ignored\n",
958                     KeyInfoText(info, keyi));
959             return false;
960         }
961
962         keyi->out_of_range_group_action = RANGE_REDIRECT;
963         keyi->out_of_range_group_number = grp - 1;
964         keyi->defined |= KEY_FIELD_GROUPINFO;
965     }
966     else {
967         log_err(info->keymap->ctx,
968                 "Unknown field %s in a symbol interpretation; "
969                 "Definition ignored\n",
970                 field);
971         ok = false;
972     }
973
974     return ok;
975 }
976
977 static int
978 SetGroupName(SymbolsInfo *info, ExprDef *arrayNdx, ExprDef *value)
979 {
980     xkb_layout_index_t group, group_to_use;
981     xkb_atom_t name;
982
983     if (!arrayNdx) {
984         log_vrb(info->keymap->ctx, 1,
985                 "You must specify an index when specifying a group name; "
986                 "Group name definition without array subscript ignored\n");
987         return false;
988     }
989
990     if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, &group)) {
991         log_err(info->keymap->ctx,
992                 "Illegal index in group name definition; "
993                 "Definition with non-integer array index ignored\n");
994         return false;
995     }
996
997     if (!ExprResolveString(info->keymap->ctx, value, &name)) {
998         log_err(info->keymap->ctx,
999                 "Group name must be a string; "
1000                 "Illegal name for group %d ignored\n", group);
1001         return false;
1002     }
1003
1004     if (info->explicit_group == XKB_LAYOUT_INVALID) {
1005         group_to_use = group - 1;
1006     }
1007     else if (group - 1 == 0) {
1008         group_to_use = info->explicit_group;
1009     }
1010     else {
1011         log_warn(info->keymap->ctx,
1012                  "An explicit group was specified for the '%s' map, "
1013                  "but it provides a name for a group other than Group1 (%d); "
1014                  "Ignoring group name '%s'\n",
1015                  info->name, group,
1016                  xkb_atom_text(info->keymap->ctx, name));
1017         return false;
1018     }
1019
1020     if (group_to_use >= darray_size(info->group_names))
1021         darray_resize0(info->group_names, group_to_use + 1);
1022     darray_item(info->group_names, group_to_use) = name;
1023
1024     return true;
1025 }
1026
1027 static int
1028 HandleGlobalVar(SymbolsInfo *info, VarDef *stmt)
1029 {
1030     const char *elem, *field;
1031     ExprDef *arrayNdx;
1032     bool ret;
1033
1034     if (ExprResolveLhs(info->keymap->ctx, stmt->name, &elem, &field,
1035                        &arrayNdx) == 0)
1036         return 0;               /* internal error, already reported */
1037     if (elem && istreq(elem, "key")) {
1038         ret = SetSymbolsField(info, &info->default_key, field, arrayNdx,
1039                               stmt->value);
1040     }
1041     else if (!elem && (istreq(field, "name") ||
1042                        istreq(field, "groupname"))) {
1043         ret = SetGroupName(info, arrayNdx, stmt->value);
1044     }
1045     else if (!elem && (istreq(field, "groupswrap") ||
1046                        istreq(field, "wrapgroups"))) {
1047         log_err(info->keymap->ctx,
1048                 "Global \"groupswrap\" not supported; Ignored\n");
1049         ret = true;
1050     }
1051     else if (!elem && (istreq(field, "groupsclamp") ||
1052                        istreq(field, "clampgroups"))) {
1053         log_err(info->keymap->ctx,
1054                 "Global \"groupsclamp\" not supported; Ignored\n");
1055         ret = true;
1056     }
1057     else if (!elem && (istreq(field, "groupsredirect") ||
1058                        istreq(field, "redirectgroups"))) {
1059         log_err(info->keymap->ctx,
1060                 "Global \"groupsredirect\" not supported; Ignored\n");
1061         ret = true;
1062     }
1063     else if (!elem && istreq(field, "allownone")) {
1064         log_err(info->keymap->ctx,
1065                 "Radio groups not supported; "
1066                 "Ignoring \"allownone\" specification\n");
1067         ret = true;
1068     }
1069     else {
1070         ret = SetActionField(info->keymap, elem, field, arrayNdx, stmt->value,
1071                              info->actions);
1072     }
1073
1074     return ret;
1075 }
1076
1077 static bool
1078 HandleSymbolsBody(SymbolsInfo *info, VarDef *def, KeyInfo *keyi)
1079 {
1080     bool ok = true;
1081     const char *elem, *field;
1082     ExprDef *arrayNdx;
1083
1084     for (; def; def = (VarDef *) def->common.next) {
1085         if (def->name && def->name->op == EXPR_FIELD_REF) {
1086             log_err(info->keymap->ctx,
1087                     "Cannot set a global default value from within a key statement; "
1088                     "Move statements to the global file scope\n");
1089             continue;
1090         }
1091
1092         if (!def->name) {
1093             if (!def->value || def->value->op == EXPR_KEYSYM_LIST)
1094                 field = "symbols";
1095             else
1096                 field = "actions";
1097             arrayNdx = NULL;
1098         }
1099         else {
1100             ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
1101                                 &arrayNdx);
1102         }
1103
1104         if (ok)
1105             ok = SetSymbolsField(info, keyi, field, arrayNdx, def->value);
1106     }
1107
1108     return ok;
1109 }
1110
1111 static bool
1112 SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi)
1113 {
1114     xkb_layout_index_t i;
1115     GroupInfo *groupi;
1116     bool warn = false;
1117
1118     if (info->explicit_group == XKB_LAYOUT_INVALID)
1119         return true;
1120
1121     darray_enumerate_from(i, groupi, keyi->groups, 1) {
1122         if (groupi->defined) {
1123             warn = true;
1124             ClearGroupInfo(groupi);
1125             InitGroupInfo(groupi);
1126         }
1127     }
1128
1129     if (warn)
1130         log_warn(info->keymap->ctx,
1131                  "For the map %s an explicit group specified, "
1132                  "but key %s has more than one group defined; "
1133                  "All groups except first one will be ignored\n",
1134                  info->name, KeyInfoText(info, keyi));
1135
1136     darray_resize0(keyi->groups, info->explicit_group + 1);
1137     if (info->explicit_group > 0) {
1138         darray_item(keyi->groups, info->explicit_group) =
1139             darray_item(keyi->groups, 0);
1140         InitGroupInfo(&darray_item(keyi->groups, 0));
1141     }
1142
1143     return true;
1144 }
1145
1146 static int
1147 HandleSymbolsDef(SymbolsInfo *info, SymbolsDef *stmt)
1148 {
1149     KeyInfo keyi;
1150     xkb_layout_index_t i;
1151
1152     keyi = info->default_key;
1153     darray_init(keyi.groups);
1154     darray_copy(keyi.groups, info->default_key.groups);
1155     for (i = 0; i < darray_size(keyi.groups); i++)
1156         CopyGroupInfo(&darray_item(keyi.groups, i),
1157                       &darray_item(info->default_key.groups, i));
1158     keyi.merge = stmt->merge;
1159     keyi.name = stmt->keyName;
1160
1161     if (!HandleSymbolsBody(info, (VarDef *) stmt->symbols, &keyi)) {
1162         info->errorCount++;
1163         return false;
1164     }
1165
1166     if (!SetExplicitGroup(info, &keyi)) {
1167         info->errorCount++;
1168         return false;
1169     }
1170
1171     if (!AddKeySymbols(info, &keyi, true)) {
1172         info->errorCount++;
1173         return false;
1174     }
1175
1176     return true;
1177 }
1178
1179 static bool
1180 HandleModMapDef(SymbolsInfo *info, ModMapDef *def)
1181 {
1182     ExprDef *key;
1183     ModMapEntry tmp;
1184     xkb_mod_index_t ndx;
1185     bool ok;
1186     struct xkb_context *ctx = info->keymap->ctx;
1187
1188     ndx = ModNameToIndex(info->keymap, def->modifier, MOD_REAL);
1189     if (ndx == XKB_MOD_INVALID) {
1190         log_err(info->keymap->ctx,
1191                 "Illegal modifier map definition; "
1192                 "Ignoring map for non-modifier \"%s\"\n",
1193                 xkb_atom_text(ctx, def->modifier));
1194         return false;
1195     }
1196
1197     ok = true;
1198     tmp.modifier = ndx;
1199
1200     for (key = def->keys; key != NULL; key = (ExprDef *) key->common.next) {
1201         xkb_keysym_t sym;
1202
1203         if (key->op == EXPR_VALUE && key->value_type == EXPR_TYPE_KEYNAME) {
1204             tmp.haveSymbol = false;
1205             tmp.u.keyName = key->value.keyName;
1206         }
1207         else if (ExprResolveKeySym(ctx, key, &sym)) {
1208             tmp.haveSymbol = true;
1209             tmp.u.keySym = sym;
1210         }
1211         else {
1212             log_err(info->keymap->ctx,
1213                     "Modmap entries may contain only key names or keysyms; "
1214                     "Illegal definition for %s modifier ignored\n",
1215                     ModIndexText(info->keymap, tmp.modifier));
1216             continue;
1217         }
1218
1219         ok = AddModMapEntry(info, &tmp) && ok;
1220     }
1221     return ok;
1222 }
1223
1224 static void
1225 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge)
1226 {
1227     bool ok;
1228
1229     free(info->name);
1230     info->name = strdup_safe(file->name);
1231
1232     for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
1233         switch (stmt->type) {
1234         case STMT_INCLUDE:
1235             ok = HandleIncludeSymbols(info, (IncludeStmt *) stmt);
1236             break;
1237         case STMT_SYMBOLS:
1238             ok = HandleSymbolsDef(info, (SymbolsDef *) stmt);
1239             break;
1240         case STMT_VAR:
1241             ok = HandleGlobalVar(info, (VarDef *) stmt);
1242             break;
1243         case STMT_VMOD:
1244             ok = HandleVModDef(info->keymap, (VModDef *) stmt);
1245             break;
1246         case STMT_MODMAP:
1247             ok = HandleModMapDef(info, (ModMapDef *) stmt);
1248             break;
1249         default:
1250             log_err(info->keymap->ctx,
1251                     "Interpretation files may not include other types; "
1252                     "Ignoring %s\n", stmt_type_to_string(stmt->type));
1253             ok = false;
1254             break;
1255         }
1256
1257         if (!ok)
1258             info->errorCount++;
1259
1260         if (info->errorCount > 10) {
1261             log_err(info->keymap->ctx, "Abandoning symbols file \"%s\"\n",
1262                     file->topName);
1263             break;
1264         }
1265     }
1266 }
1267
1268 /**
1269  * Given a keysym @sym, return a key which generates it, or NULL.
1270  * This is used for example in a modifier map definition, such as:
1271  *      modifier_map Lock           { Caps_Lock };
1272  * where we want to add the Lock modifier to the modmap of the key
1273  * which matches the keysym Caps_Lock.
1274  * Since there can be many keys which generates the keysym, the key
1275  * is chosen first by lowest group in which the keysym appears, than
1276  * by lowest level and than by lowest key code.
1277  */
1278 static struct xkb_key *
1279 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
1280 {
1281     struct xkb_key *key, *ret = NULL;
1282     xkb_layout_index_t group, min_group = UINT32_MAX;
1283     xkb_level_index_t level, min_level = UINT16_MAX;
1284
1285     xkb_foreach_key(key, keymap) {
1286         for (group = 0; group < key->num_groups; group++) {
1287             for (level = 0; level < XkbKeyGroupWidth(key, group); level++) {
1288                 if (key->groups[group].levels[level].num_syms != 1 ||
1289                     key->groups[group].levels[level].u.sym != sym)
1290                     continue;
1291
1292                 /*
1293                  * If the keysym was found in a group or level > 0, we must
1294                  * keep looking since we might find a key in which the keysym
1295                  * is in a lower group or level.
1296                  */
1297                 if (group < min_group ||
1298                     (group == min_group && level < min_level)) {
1299                     ret = key;
1300                     if (group == 0 && level == 0) {
1301                         return ret;
1302                     }
1303                     else {
1304                         min_group = group;
1305                         min_level = level;
1306                     }
1307                 }
1308             }
1309         }
1310     }
1311
1312     return ret;
1313 }
1314
1315 /*
1316  * Find an appropriate type for a group and return its name.
1317  *
1318  * Simple recipe:
1319  * - ONE_LEVEL for width 0/1
1320  * - ALPHABETIC for 2 shift levels, with lower/upercase keysyms
1321  * - KEYPAD for keypad keys.
1322  * - TWO_LEVEL for other 2 shift level keys.
1323  * and the same for four level keys.
1324  *
1325  * FIXME: Decide how to handle multiple-syms-per-level, and do it.
1326  */
1327 static xkb_atom_t
1328 FindAutomaticType(struct xkb_context *ctx, GroupInfo *groupi)
1329 {
1330     xkb_keysym_t sym0, sym1, sym2, sym3;
1331     xkb_level_index_t width = darray_size(groupi->levels);
1332
1333 #define GET_SYM(level) \
1334     (darray_item(groupi->levels, level).num_syms == 0 ? \
1335         XKB_KEY_NoSymbol : \
1336      darray_item(groupi->levels, level).num_syms == 1 ? \
1337         darray_item(groupi->levels, level).u.sym : \
1338      /* num_syms > 1 */ \
1339         darray_item(groupi->levels, level).u.syms[0])
1340
1341     if (width == 1 || width <= 0)
1342         return xkb_atom_intern_literal(ctx, "ONE_LEVEL");
1343
1344     sym0 = GET_SYM(0);
1345     sym1 = GET_SYM(1);
1346
1347     if (width == 2) {
1348         if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1))
1349             return xkb_atom_intern_literal(ctx, "ALPHABETIC");
1350
1351         if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1352             return xkb_atom_intern_literal(ctx, "KEYPAD");
1353
1354         return xkb_atom_intern_literal(ctx, "TWO_LEVEL");
1355     }
1356
1357     if (width <= 4) {
1358         if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1)) {
1359             sym2 = GET_SYM(2);
1360             sym3 = (width == 4 ? GET_SYM(3) : XKB_KEY_NoSymbol);
1361
1362             if (xkb_keysym_is_lower(sym2) && xkb_keysym_is_upper(sym3))
1363                 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_ALPHABETIC");
1364
1365             return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_SEMIALPHABETIC");
1366         }
1367
1368         if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1369             return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_KEYPAD");
1370
1371         return xkb_atom_intern_literal(ctx, "FOUR_LEVEL");
1372     }
1373
1374     return XKB_ATOM_NONE;
1375
1376 #undef GET_SYM
1377 }
1378
1379 static const struct xkb_key_type *
1380 FindTypeForGroup(struct xkb_keymap *keymap, KeyInfo *keyi,
1381                  xkb_layout_index_t group, bool *explicit_type)
1382 {
1383     unsigned int i;
1384     GroupInfo *groupi = &darray_item(keyi->groups, group);
1385     xkb_atom_t type_name = groupi->type;
1386
1387     *explicit_type = true;
1388
1389     if (type_name == XKB_ATOM_NONE) {
1390         if (keyi->default_type != XKB_ATOM_NONE) {
1391             type_name  = keyi->default_type;
1392         }
1393         else {
1394             type_name = FindAutomaticType(keymap->ctx, groupi);
1395             if (type_name != XKB_ATOM_NONE)
1396                 *explicit_type = false;
1397         }
1398     }
1399
1400     if (type_name == XKB_ATOM_NONE) {
1401         log_warn(keymap->ctx,
1402                  "Couldn't find an automatic type for key '%s' group %d with %lu levels; "
1403                  "Using the default type\n",
1404                  KeyNameText(keymap->ctx, keyi->name), group + 1,
1405                  (unsigned long) darray_size(groupi->levels));
1406         goto use_default;
1407     }
1408
1409     for (i = 0; i < keymap->num_types; i++)
1410         if (keymap->types[i].name == type_name)
1411             break;
1412
1413     if (i >= keymap->num_types) {
1414         log_warn(keymap->ctx,
1415                  "The type \"%s\" for key '%s' group %d was not previously defined; "
1416                  "Using the default type\n",
1417                  xkb_atom_text(keymap->ctx, type_name),
1418                  KeyNameText(keymap->ctx, keyi->name), group + 1);
1419         goto use_default;
1420     }
1421
1422     return &keymap->types[i];
1423
1424 use_default:
1425     /*
1426      * Index 0 is guaranteed to contain something, usually
1427      * ONE_LEVEL or at least some default one-level type.
1428      */
1429     return &keymap->types[0];
1430 }
1431
1432 static bool
1433 CopySymbolsDef(SymbolsInfo *info, KeyInfo *keyi)
1434 {
1435     struct xkb_keymap *keymap = info->keymap;
1436     struct xkb_key *key;
1437     GroupInfo *groupi;
1438     const GroupInfo *group0;
1439     xkb_layout_index_t i;
1440
1441     /*
1442      * The name is guaranteed to be real and not an alias (see
1443      * AddKeySymbols), so 'false' is safe here.
1444      */
1445     key = XkbKeyByName(keymap, keyi->name, false);
1446     if (!key) {
1447         log_vrb(info->keymap->ctx, 5,
1448                 "Key %s not found in keycodes; Symbols ignored\n",
1449                 KeyInfoText(info, keyi));
1450         return false;
1451     }
1452
1453     /* Find the range of groups we need. */
1454     key->num_groups = 0;
1455     darray_enumerate(i, groupi, keyi->groups)
1456         if (groupi->defined)
1457             key->num_groups = i + 1;
1458
1459     if (key->num_groups <= 0)
1460         return false; /* WSGO */
1461
1462     darray_resize(keyi->groups, key->num_groups);
1463
1464     /*
1465      * If there are empty groups between non-empty ones, fill them with data
1466      * from the first group.
1467      * We can make a wrong assumption here. But leaving gaps is worse.
1468      */
1469     group0 = &darray_item(keyi->groups, 0);
1470     darray_foreach_from(groupi, keyi->groups, 1) {
1471         if (groupi->defined)
1472             continue;
1473
1474         CopyGroupInfo(groupi, group0);
1475     }
1476
1477     key->groups = calloc(key->num_groups, sizeof(*key->groups));
1478
1479     /* Find and assign the groups' types in the keymap. */
1480     darray_enumerate(i, groupi, keyi->groups) {
1481         const struct xkb_key_type *type;
1482         bool explicit_type;
1483
1484         type = FindTypeForGroup(keymap, keyi, i, &explicit_type);
1485
1486         /* Always have as many levels as the type specifies. */
1487         if (type->num_levels < darray_size(groupi->levels)) {
1488             struct xkb_level *leveli;
1489
1490             log_vrb(info->keymap->ctx, 1,
1491                     "Type \"%s\" has %d levels, but %s has %d levels; "
1492                     "Ignoring extra symbols\n",
1493                     xkb_atom_text(keymap->ctx, type->name), type->num_levels,
1494                     KeyInfoText(info, keyi),
1495                     (int) darray_size(groupi->levels));
1496
1497             darray_foreach_from(leveli, groupi->levels, type->num_levels)
1498                 ClearLevelInfo(leveli);
1499         }
1500         darray_resize0(groupi->levels, type->num_levels);
1501
1502         key->groups[i].explicit_type = explicit_type;
1503         key->groups[i].type = type;
1504     }
1505
1506     /* Copy levels. */
1507     darray_enumerate(i, groupi, keyi->groups) {
1508         key->groups[i].levels = darray_mem(groupi->levels, 0);
1509         darray_init(groupi->levels);
1510     }
1511
1512     key->out_of_range_group_number = keyi->out_of_range_group_number;
1513     key->out_of_range_group_action = keyi->out_of_range_group_action;
1514
1515     if (keyi->defined & KEY_FIELD_VMODMAP) {
1516         key->vmodmap = keyi->vmodmap;
1517         key->explicit |= EXPLICIT_VMODMAP;
1518     }
1519
1520     if (keyi->repeat != KEY_REPEAT_UNDEFINED) {
1521         key->repeats = (keyi->repeat == KEY_REPEAT_YES);
1522         key->explicit |= EXPLICIT_REPEAT;
1523     }
1524
1525     darray_foreach(groupi, keyi->groups) {
1526         if (groupi->defined & GROUP_FIELD_ACTS) {
1527             key->explicit |= EXPLICIT_INTERP;
1528             break;
1529         }
1530     }
1531
1532     return true;
1533 }
1534
1535 static bool
1536 CopyModMapDef(SymbolsInfo *info, ModMapEntry *entry)
1537 {
1538     struct xkb_key *key;
1539     struct xkb_keymap *keymap = info->keymap;
1540
1541     if (!entry->haveSymbol) {
1542         key = XkbKeyByName(keymap, entry->u.keyName, true);
1543         if (!key) {
1544             log_vrb(info->keymap->ctx, 5,
1545                     "Key %s not found in keycodes; "
1546                     "Modifier map entry for %s not updated\n",
1547                     KeyNameText(keymap->ctx, entry->u.keyName),
1548                     ModIndexText(info->keymap, entry->modifier));
1549             return false;
1550         }
1551     }
1552     else {
1553         key = FindKeyForSymbol(keymap, entry->u.keySym);
1554         if (!key) {
1555             log_vrb(info->keymap->ctx, 5,
1556                     "Key \"%s\" not found in symbol map; "
1557                     "Modifier map entry for %s not updated\n",
1558                     KeysymText(info->keymap->ctx, entry->u.keySym),
1559                     ModIndexText(info->keymap, entry->modifier));
1560             return false;
1561         }
1562     }
1563
1564     key->modmap |= (1 << entry->modifier);
1565     return true;
1566 }
1567
1568 static bool
1569 CopySymbolsToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info)
1570 {
1571     KeyInfo *keyi;
1572     ModMapEntry *mm;
1573     struct xkb_key *key;
1574
1575     keymap->symbols_section_name = strdup_safe(info->name);
1576     XkbEscapeMapName(keymap->symbols_section_name);
1577
1578     keymap->num_group_names = darray_size(info->group_names);
1579     keymap->group_names = darray_mem(info->group_names, 0);
1580     darray_init(info->group_names);
1581
1582     darray_foreach(keyi, info->keys)
1583         if (!CopySymbolsDef(info, keyi))
1584             info->errorCount++;
1585
1586     if (xkb_context_get_log_verbosity(keymap->ctx) > 3) {
1587         xkb_foreach_key(key, keymap) {
1588             if (key->name == XKB_ATOM_NONE)
1589                 continue;
1590
1591             if (key->num_groups < 1)
1592                 log_info(keymap->ctx,
1593                          "No symbols defined for %s\n",
1594                          KeyNameText(keymap->ctx, key->name));
1595         }
1596     }
1597
1598     darray_foreach(mm, info->modMaps)
1599         if (!CopyModMapDef(info, mm))
1600             info->errorCount++;
1601
1602     /* XXX: If we don't ignore errorCount, things break. */
1603     return true;
1604 }
1605
1606 bool
1607 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1608                enum merge_mode merge)
1609 {
1610     SymbolsInfo info;
1611     ActionsInfo *actions;
1612
1613     actions = NewActionsInfo();
1614     if (!actions)
1615         return false;
1616
1617     InitSymbolsInfo(&info, keymap, actions);
1618     info.default_key.merge = merge;
1619
1620     HandleSymbolsFile(&info, file, merge);
1621
1622     if (info.errorCount != 0)
1623         goto err_info;
1624
1625     if (!CopySymbolsToKeymap(keymap, &info))
1626         goto err_info;
1627
1628     ClearSymbolsInfo(&info);
1629     FreeActionsInfo(actions);
1630     return true;
1631
1632 err_info:
1633     FreeActionsInfo(actions);
1634     ClearSymbolsInfo(&info);
1635     return false;
1636 }