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