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