keymap-dump: use consistent order set/latch/lock (style)
[platform/upstream/libxkbcommon.git] / src / xkbcomp / compat.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 Ran Benita <ran234@gmail.com>
29  *
30  * Permission is hereby granted, free of charge, to any person obtaining a
31  * copy of this software and associated documentation files (the "Software"),
32  * to deal in the Software without restriction, including without limitation
33  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
34  * and/or sell copies of the Software, and to permit persons to whom the
35  * Software is furnished to do so, subject to the following conditions:
36  *
37  * The above copyright notice and this permission notice (including the next
38  * paragraph) shall be included in all copies or substantial portions of the
39  * Software.
40  *
41  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
42  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
43  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
44  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
45  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
46  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
47  * DEALINGS IN THE SOFTWARE.
48  */
49
50 #include "xkbcomp-priv.h"
51 #include "text.h"
52 #include "expr.h"
53 #include "action.h"
54 #include "vmod.h"
55 #include "include.h"
56
57 enum si_field {
58     SI_FIELD_VIRTUAL_MOD = (1 << 0),
59     SI_FIELD_ACTION = (1 << 1),
60     SI_FIELD_AUTO_REPEAT = (1 << 2),
61     SI_FIELD_LEVEL_ONE_ONLY = (1 << 3),
62 };
63
64 typedef struct {
65     enum si_field defined;
66     enum merge_mode merge;
67
68     struct xkb_sym_interpret interp;
69 } SymInterpInfo;
70
71 enum led_field {
72     LED_FIELD_MODS = (1 << 0),
73     LED_FIELD_GROUPS = (1 << 1),
74     LED_FIELD_CTRLS = (1 << 2),
75 };
76
77 typedef struct {
78     enum led_field defined;
79     enum merge_mode merge;
80
81     struct xkb_led led;
82 } LedInfo;
83
84 typedef struct {
85     char *name;
86     int errorCount;
87     SymInterpInfo default_interp;
88     darray(SymInterpInfo) interps;
89     LedInfo default_led;
90     LedInfo leds[XKB_MAX_LEDS];
91     unsigned int num_leds;
92     ActionsInfo *actions;
93     struct xkb_mod_set mods;
94
95     struct xkb_context *ctx;
96 } CompatInfo;
97
98 static const char *
99 siText(SymInterpInfo *si, CompatInfo *info)
100 {
101     char *buf = xkb_context_get_buffer(info->ctx, 128);
102
103     if (si == &info->default_interp)
104         return "default";
105
106     snprintf(buf, 128, "%s+%s(%s)",
107              KeysymText(info->ctx, si->interp.sym),
108              SIMatchText(si->interp.match),
109              ModMaskText(info->ctx, &info->mods, si->interp.mods));
110
111     return buf;
112 }
113
114 static inline bool
115 ReportSINotArray(CompatInfo *info, SymInterpInfo *si, const char *field)
116 {
117     return ReportNotArray(info->ctx, "symbol interpretation", field,
118                           siText(si, info));
119 }
120
121 static inline bool
122 ReportSIBadType(CompatInfo *info, SymInterpInfo *si, const char *field,
123                 const char *wanted)
124 {
125     return ReportBadType(info->ctx, "symbol interpretation", field,
126                          siText(si, info), wanted);
127 }
128
129 static inline bool
130 ReportLedBadType(CompatInfo *info, LedInfo *ledi, const char *field,
131                  const char *wanted)
132 {
133     return ReportBadType(info->ctx, "indicator map", field,
134                          xkb_atom_text(info->ctx, ledi->led.name),
135                          wanted);
136 }
137
138 static inline bool
139 ReportLedNotArray(CompatInfo *info, LedInfo *ledi, const char *field)
140 {
141     return ReportNotArray(info->ctx, "indicator map", field,
142                           xkb_atom_text(info->ctx, ledi->led.name));
143 }
144
145 static void
146 InitCompatInfo(CompatInfo *info, struct xkb_context *ctx,
147                ActionsInfo *actions, const struct xkb_mod_set *mods)
148 {
149     memset(info, 0, sizeof(*info));
150     info->ctx = ctx;
151     info->actions = actions;
152     info->mods = *mods;
153     info->default_interp.merge = MERGE_OVERRIDE;
154     info->default_interp.interp.virtual_mod = XKB_MOD_INVALID;
155     info->default_led.merge = MERGE_OVERRIDE;
156 }
157
158 static void
159 ClearCompatInfo(CompatInfo *info)
160 {
161     free(info->name);
162     darray_free(info->interps);
163 }
164
165 static SymInterpInfo *
166 FindMatchingInterp(CompatInfo *info, SymInterpInfo *new)
167 {
168     SymInterpInfo *old;
169
170     darray_foreach(old, info->interps)
171         if (old->interp.sym == new->interp.sym &&
172             old->interp.mods == new->interp.mods &&
173             old->interp.match == new->interp.match)
174             return old;
175
176     return NULL;
177 }
178
179 static bool
180 UseNewInterpField(enum si_field field, SymInterpInfo *old, SymInterpInfo *new,
181                   bool report, enum si_field *collide)
182 {
183     if (!(old->defined & field))
184         return true;
185
186     if (new->defined & field) {
187         if (report)
188             *collide |= field;
189
190         if (new->merge != MERGE_AUGMENT)
191             return true;
192     }
193
194     return false;
195 }
196
197 static bool
198 AddInterp(CompatInfo *info, SymInterpInfo *new, bool same_file)
199 {
200     SymInterpInfo *old = FindMatchingInterp(info, new);
201     if (old) {
202         const int verbosity = xkb_context_get_log_verbosity(info->ctx);
203         const bool report = (same_file && verbosity > 0) || verbosity > 9;
204         enum si_field collide = 0;
205
206         if (new->merge == MERGE_REPLACE) {
207             if (report)
208                 log_warn(info->ctx,
209                          "Multiple definitions for \"%s\"; "
210                          "Earlier interpretation ignored\n",
211                          siText(new, info));
212             *old = *new;
213             return true;
214         }
215
216         if (UseNewInterpField(SI_FIELD_VIRTUAL_MOD, old, new, report,
217                               &collide)) {
218             old->interp.virtual_mod = new->interp.virtual_mod;
219             old->defined |= SI_FIELD_VIRTUAL_MOD;
220         }
221         if (UseNewInterpField(SI_FIELD_ACTION, old, new, report,
222                               &collide)) {
223             old->interp.action = new->interp.action;
224             old->defined |= SI_FIELD_ACTION;
225         }
226         if (UseNewInterpField(SI_FIELD_AUTO_REPEAT, old, new, report,
227                               &collide)) {
228             old->interp.repeat = new->interp.repeat;
229             old->defined |= SI_FIELD_AUTO_REPEAT;
230         }
231         if (UseNewInterpField(SI_FIELD_LEVEL_ONE_ONLY, old, new, report,
232                               &collide)) {
233             old->interp.level_one_only = new->interp.level_one_only;
234             old->defined |= SI_FIELD_LEVEL_ONE_ONLY;
235         }
236
237         if (collide) {
238             log_warn(info->ctx,
239                      "Multiple interpretations of \"%s\"; "
240                      "Using %s definition for duplicate fields\n",
241                      siText(new, info),
242                      (new->merge != MERGE_AUGMENT ? "last" : "first"));
243         }
244
245         return true;
246     }
247
248     darray_append(info->interps, *new);
249     return true;
250 }
251
252 /***====================================================================***/
253
254 static bool
255 ResolveStateAndPredicate(ExprDef *expr, enum xkb_match_operation *pred_rtrn,
256                          xkb_mod_mask_t *mods_rtrn, CompatInfo *info)
257 {
258     if (expr == NULL) {
259         *pred_rtrn = MATCH_ANY_OR_NONE;
260         *mods_rtrn = MOD_REAL_MASK_ALL;
261         return true;
262     }
263
264     *pred_rtrn = MATCH_EXACTLY;
265     if (expr->expr.op == EXPR_ACTION_DECL) {
266         const char *pred_txt = xkb_atom_text(info->ctx, expr->action.name);
267         if (!LookupString(symInterpretMatchMaskNames, pred_txt, pred_rtrn)) {
268             log_err(info->ctx,
269                     "Illegal modifier predicate \"%s\"; Ignored\n", pred_txt);
270             return false;
271         }
272         expr = expr->action.args;
273     }
274     else if (expr->expr.op == EXPR_IDENT) {
275         const char *pred_txt = xkb_atom_text(info->ctx, expr->ident.ident);
276         if (pred_txt && istreq(pred_txt, "any")) {
277             *pred_rtrn = MATCH_ANY;
278             *mods_rtrn = MOD_REAL_MASK_ALL;
279             return true;
280         }
281     }
282
283     return ExprResolveModMask(info->ctx, expr, MOD_REAL, &info->mods,
284                               mods_rtrn);
285 }
286
287 /***====================================================================***/
288
289 static bool
290 UseNewLEDField(enum led_field field, LedInfo *old, LedInfo *new,
291                bool report, enum led_field *collide)
292 {
293     if (!(old->defined & field))
294         return true;
295
296     if (new->defined & field) {
297         if (report)
298             *collide |= field;
299
300         if (new->merge != MERGE_AUGMENT)
301             return true;
302     }
303
304     return false;
305 }
306
307 static bool
308 AddLedMap(CompatInfo *info, LedInfo *new, bool same_file)
309 {
310     enum led_field collide;
311     const int verbosity = xkb_context_get_log_verbosity(info->ctx);
312     const bool report = (same_file && verbosity > 0) || verbosity > 9;
313
314     for (xkb_led_index_t i = 0; i < info->num_leds; i++) {
315         LedInfo *old = &info->leds[i];
316
317         if (old->led.name != new->led.name)
318             continue;
319
320         if (old->led.mods.mods == new->led.mods.mods &&
321             old->led.groups == new->led.groups &&
322             old->led.ctrls == new->led.ctrls &&
323             old->led.which_mods == new->led.which_mods &&
324             old->led.which_groups == new->led.which_groups) {
325             old->defined |= new->defined;
326             return true;
327         }
328
329         if (new->merge == MERGE_REPLACE) {
330             if (report)
331                 log_warn(info->ctx,
332                          "Map for indicator %s redefined; "
333                          "Earlier definition ignored\n",
334                          xkb_atom_text(info->ctx, old->led.name));
335             *old = *new;
336             return true;
337         }
338
339         collide = 0;
340         if (UseNewLEDField(LED_FIELD_MODS, old, new, report, &collide)) {
341             old->led.which_mods = new->led.which_mods;
342             old->led.mods = new->led.mods;
343             old->defined |= LED_FIELD_MODS;
344         }
345         if (UseNewLEDField(LED_FIELD_GROUPS, old, new, report, &collide)) {
346             old->led.which_groups = new->led.which_groups;
347             old->led.groups = new->led.groups;
348             old->defined |= LED_FIELD_GROUPS;
349         }
350         if (UseNewLEDField(LED_FIELD_CTRLS, old, new, report, &collide)) {
351             old->led.ctrls = new->led.ctrls;
352             old->defined |= LED_FIELD_CTRLS;
353         }
354
355         if (collide) {
356             log_warn(info->ctx,
357                      "Map for indicator %s redefined; "
358                      "Using %s definition for duplicate fields\n",
359                      xkb_atom_text(info->ctx, old->led.name),
360                      (new->merge == MERGE_AUGMENT ? "first" : "last"));
361         }
362
363         return true;
364     }
365
366     if (info->num_leds >= XKB_MAX_LEDS) {
367         log_err(info->ctx,
368                 "Too many LEDs defined (maximum %d)\n",
369                 XKB_MAX_LEDS);
370         return false;
371     }
372     info->leds[info->num_leds++] = *new;
373     return true;
374 }
375
376 static void
377 MergeIncludedCompatMaps(CompatInfo *into, CompatInfo *from,
378                         enum merge_mode merge)
379 {
380     if (from->errorCount > 0) {
381         into->errorCount += from->errorCount;
382         return;
383     }
384
385     into->mods = from->mods;
386
387     if (into->name == NULL) {
388         into->name = from->name;
389         from->name = NULL;
390     }
391
392     if (darray_empty(into->interps)) {
393         into->interps = from->interps;
394         darray_init(from->interps);
395     }
396     else {
397         SymInterpInfo *si;
398         darray_foreach(si, from->interps) {
399             si->merge = (merge == MERGE_DEFAULT ? si->merge : merge);
400             if (!AddInterp(into, si, false))
401                 into->errorCount++;
402         }
403     }
404
405     if (into->num_leds == 0) {
406         memcpy(into->leds, from->leds, sizeof(*from->leds) * from->num_leds);
407         into->num_leds = from->num_leds;
408         from->num_leds = 0;
409     }
410     else {
411         for (xkb_led_index_t i = 0; i < from->num_leds; i++) {
412             LedInfo *ledi = &from->leds[i];
413             ledi->merge = (merge == MERGE_DEFAULT ? ledi->merge : merge);
414             if (!AddLedMap(into, ledi, false))
415                 into->errorCount++;
416         }
417     }
418 }
419
420 static void
421 HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge);
422
423 static bool
424 HandleIncludeCompatMap(CompatInfo *info, IncludeStmt *include)
425 {
426     CompatInfo included;
427
428     InitCompatInfo(&included, info->ctx, info->actions, &info->mods);
429     included.name = include->stmt;
430     include->stmt = NULL;
431
432     for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
433         CompatInfo next_incl;
434         XkbFile *file;
435
436         file = ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_COMPAT);
437         if (!file) {
438             info->errorCount += 10;
439             ClearCompatInfo(&included);
440             return false;
441         }
442
443         InitCompatInfo(&next_incl, info->ctx, info->actions, &included.mods);
444         next_incl.default_interp = info->default_interp;
445         next_incl.default_interp.merge = stmt->merge;
446         next_incl.default_led = info->default_led;
447         next_incl.default_led.merge = stmt->merge;
448
449         HandleCompatMapFile(&next_incl, file, MERGE_OVERRIDE);
450
451         MergeIncludedCompatMaps(&included, &next_incl, stmt->merge);
452
453         ClearCompatInfo(&next_incl);
454         FreeXkbFile(file);
455     }
456
457     MergeIncludedCompatMaps(info, &included, include->merge);
458     ClearCompatInfo(&included);
459
460     return (info->errorCount == 0);
461 }
462
463 static bool
464 SetInterpField(CompatInfo *info, SymInterpInfo *si, const char *field,
465                ExprDef *arrayNdx, ExprDef *value)
466 {
467     xkb_mod_index_t ndx;
468
469     if (istreq(field, "action")) {
470         if (arrayNdx)
471             return ReportSINotArray(info, si, field);
472
473         if (!HandleActionDef(info->ctx, info->actions, &info->mods,
474                              value, &si->interp.action))
475             return false;
476
477         si->defined |= SI_FIELD_ACTION;
478     }
479     else if (istreq(field, "virtualmodifier") ||
480              istreq(field, "virtualmod")) {
481         if (arrayNdx)
482             return ReportSINotArray(info, si, field);
483
484         if (!ExprResolveMod(info->ctx, value, MOD_VIRT, &info->mods, &ndx))
485             return ReportSIBadType(info, si, field, "virtual modifier");
486
487         si->interp.virtual_mod = ndx;
488         si->defined |= SI_FIELD_VIRTUAL_MOD;
489     }
490     else if (istreq(field, "repeat")) {
491         bool set;
492
493         if (arrayNdx)
494             return ReportSINotArray(info, si, field);
495
496         if (!ExprResolveBoolean(info->ctx, value, &set))
497             return ReportSIBadType(info, si, field, "boolean");
498
499         si->interp.repeat = set;
500
501         si->defined |= SI_FIELD_AUTO_REPEAT;
502     }
503     else if (istreq(field, "locking")) {
504         log_dbg(info->ctx,
505                 "The \"locking\" field in symbol interpretation is unsupported; "
506                 "Ignored\n");
507     }
508     else if (istreq(field, "usemodmap") ||
509              istreq(field, "usemodmapmods")) {
510         unsigned int val;
511
512         if (arrayNdx)
513             return ReportSINotArray(info, si, field);
514
515         if (!ExprResolveEnum(info->ctx, value, &val, useModMapValueNames))
516             return ReportSIBadType(info, si, field, "level specification");
517
518         si->interp.level_one_only = val;
519         si->defined |= SI_FIELD_LEVEL_ONE_ONLY;
520     }
521     else {
522         return ReportBadField(info->ctx, "symbol interpretation", field,
523                               siText(si, info));
524     }
525
526     return true;
527 }
528
529 static bool
530 SetLedMapField(CompatInfo *info, LedInfo *ledi, const char *field,
531                ExprDef *arrayNdx, ExprDef *value)
532 {
533     bool ok = true;
534
535     if (istreq(field, "modifiers") || istreq(field, "mods")) {
536         if (arrayNdx)
537             return ReportLedNotArray(info, ledi, field);
538
539         if (!ExprResolveModMask(info->ctx, value, MOD_BOTH,
540                                 &info->mods, &ledi->led.mods.mods))
541             return ReportLedBadType(info, ledi, field, "modifier mask");
542
543         ledi->defined |= LED_FIELD_MODS;
544     }
545     else if (istreq(field, "groups")) {
546         unsigned int mask;
547
548         if (arrayNdx)
549             return ReportLedNotArray(info, ledi, field);
550
551         if (!ExprResolveMask(info->ctx, value, &mask, groupMaskNames))
552             return ReportLedBadType(info, ledi, field, "group mask");
553
554         ledi->led.groups = mask;
555         ledi->defined |= LED_FIELD_GROUPS;
556     }
557     else if (istreq(field, "controls") || istreq(field, "ctrls")) {
558         unsigned int mask;
559
560         if (arrayNdx)
561             return ReportLedNotArray(info, ledi, field);
562
563         if (!ExprResolveMask(info->ctx, value, &mask, ctrlMaskNames))
564             return ReportLedBadType(info, ledi, field, "controls mask");
565
566         ledi->led.ctrls = mask;
567         ledi->defined |= LED_FIELD_CTRLS;
568     }
569     else if (istreq(field, "allowexplicit")) {
570         log_dbg(info->ctx,
571                 "The \"allowExplicit\" field in indicator statements is unsupported; "
572                 "Ignored\n");
573     }
574     else if (istreq(field, "whichmodstate") ||
575              istreq(field, "whichmodifierstate")) {
576         unsigned int mask;
577
578         if (arrayNdx)
579             return ReportLedNotArray(info, ledi, field);
580
581         if (!ExprResolveMask(info->ctx, value, &mask,
582                              modComponentMaskNames))
583             return ReportLedBadType(info, ledi, field,
584                                     "mask of modifier state components");
585
586         ledi->led.which_mods = mask;
587     }
588     else if (istreq(field, "whichgroupstate")) {
589         unsigned mask;
590
591         if (arrayNdx)
592             return ReportLedNotArray(info, ledi, field);
593
594         if (!ExprResolveMask(info->ctx, value, &mask,
595                              groupComponentMaskNames))
596             return ReportLedBadType(info, ledi, field,
597                                     "mask of group state components");
598
599         ledi->led.which_groups = mask;
600     }
601     else if (istreq(field, "driveskbd") ||
602              istreq(field, "driveskeyboard") ||
603              istreq(field, "leddriveskbd") ||
604              istreq(field, "leddriveskeyboard") ||
605              istreq(field, "indicatordriveskbd") ||
606              istreq(field, "indicatordriveskeyboard")) {
607         log_dbg(info->ctx,
608                 "The \"%s\" field in indicator statements is unsupported; "
609                 "Ignored\n", field);
610     }
611     else if (istreq(field, "index")) {
612         /* Users should see this, it might cause unexpected behavior. */
613         log_err(info->ctx,
614                 "The \"index\" field in indicator statements is unsupported; "
615                 "Ignored\n");
616     }
617     else {
618         log_err(info->ctx,
619                 "Unknown field %s in map for %s indicator; "
620                 "Definition ignored\n",
621                 field, xkb_atom_text(info->ctx, ledi->led.name));
622         ok = false;
623     }
624
625     return ok;
626 }
627
628 static bool
629 HandleGlobalVar(CompatInfo *info, VarDef *stmt)
630 {
631     const char *elem, *field;
632     ExprDef *ndx;
633     bool ret;
634
635     if (!ExprResolveLhs(info->ctx, stmt->name, &elem, &field, &ndx))
636         ret = false;
637     else if (elem && istreq(elem, "interpret"))
638         ret = SetInterpField(info, &info->default_interp, field, ndx,
639                              stmt->value);
640     else if (elem && istreq(elem, "indicator"))
641         ret = SetLedMapField(info, &info->default_led, field, ndx,
642                              stmt->value);
643     else
644         ret = SetActionField(info->ctx, info->actions, &info->mods,
645                              elem, field, ndx, stmt->value);
646     return ret;
647 }
648
649 static bool
650 HandleInterpBody(CompatInfo *info, VarDef *def, SymInterpInfo *si)
651 {
652     bool ok = true;
653     const char *elem, *field;
654     ExprDef *arrayNdx;
655
656     for (; def; def = (VarDef *) def->common.next) {
657         if (def->name && def->name->expr.op == EXPR_FIELD_REF) {
658             log_err(info->ctx,
659                     "Cannot set a global default value from within an interpret statement; "
660                     "Move statements to the global file scope\n");
661             ok = false;
662             continue;
663         }
664
665         ok = ExprResolveLhs(info->ctx, def->name, &elem, &field, &arrayNdx);
666         if (!ok)
667             continue;
668
669         ok = SetInterpField(info, si, field, arrayNdx, def->value);
670     }
671
672     return ok;
673 }
674
675 static bool
676 HandleInterpDef(CompatInfo *info, InterpDef *def, enum merge_mode merge)
677 {
678     enum xkb_match_operation pred;
679     xkb_mod_mask_t mods;
680     SymInterpInfo si;
681
682     if (!ResolveStateAndPredicate(def->match, &pred, &mods, info)) {
683         log_err(info->ctx,
684                 "Couldn't determine matching modifiers; "
685                 "Symbol interpretation ignored\n");
686         return false;
687     }
688
689     si = info->default_interp;
690     si.merge = merge = (def->merge == MERGE_DEFAULT ? merge : def->merge);
691     si.interp.sym = def->sym;
692     si.interp.match = pred;
693     si.interp.mods = mods;
694
695     if (!HandleInterpBody(info, def->def, &si)) {
696         info->errorCount++;
697         return false;
698     }
699
700     if (!AddInterp(info, &si, true)) {
701         info->errorCount++;
702         return false;
703     }
704
705     return true;
706 }
707
708 static bool
709 HandleLedMapDef(CompatInfo *info, LedMapDef *def, enum merge_mode merge)
710 {
711     LedInfo ledi;
712     VarDef *var;
713     bool ok;
714
715     if (def->merge != MERGE_DEFAULT)
716         merge = def->merge;
717
718     ledi = info->default_led;
719     ledi.merge = merge;
720     ledi.led.name = def->name;
721
722     ok = true;
723     for (var = def->body; var != NULL; var = (VarDef *) var->common.next) {
724         const char *elem, *field;
725         ExprDef *arrayNdx;
726         if (!ExprResolveLhs(info->ctx, var->name, &elem, &field, &arrayNdx)) {
727             ok = false;
728             continue;
729         }
730
731         if (elem) {
732             log_err(info->ctx,
733                     "Cannot set defaults for \"%s\" element in indicator map; "
734                     "Assignment to %s.%s ignored\n", elem, elem, field);
735             ok = false;
736         }
737         else {
738             ok = SetLedMapField(info, &ledi, field, arrayNdx, var->value) && ok;
739         }
740     }
741
742     if (ok)
743         return AddLedMap(info, &ledi, true);
744
745     return false;
746 }
747
748 static void
749 HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge)
750 {
751     bool ok;
752
753     merge = (merge == MERGE_DEFAULT ? MERGE_AUGMENT : merge);
754
755     free(info->name);
756     info->name = strdup_safe(file->name);
757
758     for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
759         switch (stmt->type) {
760         case STMT_INCLUDE:
761             ok = HandleIncludeCompatMap(info, (IncludeStmt *) stmt);
762             break;
763         case STMT_INTERP:
764             ok = HandleInterpDef(info, (InterpDef *) stmt, merge);
765             break;
766         case STMT_GROUP_COMPAT:
767             log_dbg(info->ctx,
768                     "The \"group\" statement in compat is unsupported; "
769                     "Ignored\n");
770             ok = true;
771             break;
772         case STMT_LED_MAP:
773             ok = HandleLedMapDef(info, (LedMapDef *) stmt, merge);
774             break;
775         case STMT_VAR:
776             ok = HandleGlobalVar(info, (VarDef *) stmt);
777             break;
778         case STMT_VMOD:
779             ok = HandleVModDef(info->ctx, &info->mods, (VModDef *) stmt, merge);
780             break;
781         default:
782             log_err(info->ctx,
783                     "Compat files may not include other types; "
784                     "Ignoring %s\n", stmt_type_to_string(stmt->type));
785             ok = false;
786             break;
787         }
788
789         if (!ok)
790             info->errorCount++;
791
792         if (info->errorCount > 10) {
793             log_err(info->ctx,
794                     "Abandoning compatibility map \"%s\"\n", file->name);
795             break;
796         }
797     }
798 }
799
800 /* Temporary struct for CopyInterps. */
801 struct collect {
802     darray(struct xkb_sym_interpret) sym_interprets;
803 };
804
805 static void
806 CopyInterps(CompatInfo *info, bool needSymbol, enum xkb_match_operation pred,
807             struct collect *collect)
808 {
809     SymInterpInfo *si;
810
811     darray_foreach(si, info->interps)
812         if (si->interp.match == pred &&
813             (si->interp.sym != XKB_KEY_NoSymbol) == needSymbol)
814             darray_append(collect->sym_interprets, si->interp);
815 }
816
817 static void
818 CopyLedMapDefsToKeymap(struct xkb_keymap *keymap, CompatInfo *info)
819 {
820     for (xkb_led_index_t idx = 0; idx < info->num_leds; idx++) {
821         LedInfo *ledi = &info->leds[idx];
822         xkb_led_index_t i;
823         struct xkb_led *led;
824
825         /*
826          * Find the LED with the given name, if it was already declared
827          * in keycodes.
828          */
829         xkb_leds_enumerate(i, led, keymap)
830             if (led->name == ledi->led.name)
831                 break;
832
833         /* Not previously declared; create it with next free index. */
834         if (i >= keymap->num_leds) {
835             log_dbg(keymap->ctx,
836                     "Indicator name \"%s\" was not declared in the keycodes section; "
837                     "Adding new indicator\n",
838                     xkb_atom_text(keymap->ctx, ledi->led.name));
839
840             xkb_leds_enumerate(i, led, keymap)
841                 if (led->name == XKB_ATOM_NONE)
842                     break;
843
844             if (i >= keymap->num_leds) {
845                 /* Not place to put it; ignore. */
846                 if (i >= XKB_MAX_LEDS) {
847                     log_err(keymap->ctx,
848                             "Too many indicators (maximum is %d); "
849                             "Indicator name \"%s\" ignored\n",
850                             XKB_MAX_LEDS,
851                             xkb_atom_text(keymap->ctx, ledi->led.name));
852                     continue;
853                 }
854
855                 /* Add a new LED. */
856                 led = &keymap->leds[keymap->num_leds++];
857             }
858         }
859
860         *led = ledi->led;
861         if (led->groups != 0 && led->which_groups == 0)
862             led->which_groups = XKB_STATE_LAYOUT_EFFECTIVE;
863         if (led->mods.mods != 0 && led->which_mods == 0)
864             led->which_mods = XKB_STATE_MODS_EFFECTIVE;
865     }
866 }
867
868 static bool
869 CopyCompatToKeymap(struct xkb_keymap *keymap, CompatInfo *info)
870 {
871     keymap->compat_section_name = strdup_safe(info->name);
872     XkbEscapeMapName(keymap->compat_section_name);
873
874     keymap->mods = info->mods;
875
876     if (!darray_empty(info->interps)) {
877         struct collect collect;
878         darray_init(collect.sym_interprets);
879
880         /* Most specific to least specific. */
881         CopyInterps(info, true, MATCH_EXACTLY, &collect);
882         CopyInterps(info, true, MATCH_ALL, &collect);
883         CopyInterps(info, true, MATCH_NONE, &collect);
884         CopyInterps(info, true, MATCH_ANY, &collect);
885         CopyInterps(info, true, MATCH_ANY_OR_NONE, &collect);
886         CopyInterps(info, false, MATCH_EXACTLY, &collect);
887         CopyInterps(info, false, MATCH_ALL, &collect);
888         CopyInterps(info, false, MATCH_NONE, &collect);
889         CopyInterps(info, false, MATCH_ANY, &collect);
890         CopyInterps(info, false, MATCH_ANY_OR_NONE, &collect);
891
892         darray_steal(collect.sym_interprets,
893                      &keymap->sym_interprets, &keymap->num_sym_interprets);
894     }
895
896     CopyLedMapDefsToKeymap(keymap, info);
897
898     return true;
899 }
900
901 bool
902 CompileCompatMap(XkbFile *file, struct xkb_keymap *keymap,
903                  enum merge_mode merge)
904 {
905     CompatInfo info;
906     ActionsInfo *actions;
907
908     actions = NewActionsInfo();
909     if (!actions)
910         return false;
911
912     InitCompatInfo(&info, keymap->ctx, actions, &keymap->mods);
913     info.default_interp.merge = merge;
914     info.default_led.merge = merge;
915
916     HandleCompatMapFile(&info, file, merge);
917     if (info.errorCount != 0)
918         goto err_info;
919
920     if (!CopyCompatToKeymap(keymap, &info))
921         goto err_info;
922
923     ClearCompatInfo(&info);
924     FreeActionsInfo(actions);
925     return true;
926
927 err_info:
928     ClearCompatInfo(&info);
929     FreeActionsInfo(actions);
930     return false;
931 }