action: take xkb_mod_set instead of the 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
93     struct xkb_context *ctx;
94     struct xkb_keymap *keymap;
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->keymap->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_keymap *keymap,
146                ActionsInfo *actions)
147 {
148     memset(info, 0, sizeof(*info));
149     info->ctx = keymap->ctx;
150     info->keymap = keymap;
151     info->actions = actions;
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     darray_free(info->interps);
162     darray_free(info->leds);
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->keymap->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     LedInfo *old;
311     enum led_field collide;
312     const int verbosity = xkb_context_get_log_verbosity(info->ctx);
313     const bool report = (same_file && verbosity > 0) || verbosity > 9;
314
315     darray_foreach(old, info->leds) {
316         if (old->led.name != new->led.name)
317             continue;
318
319         if (old->led.mods.mods == new->led.mods.mods &&
320             old->led.groups == new->led.groups &&
321             old->led.ctrls == new->led.ctrls &&
322             old->led.which_mods == new->led.which_mods &&
323             old->led.which_groups == new->led.which_groups) {
324             old->defined |= new->defined;
325             return true;
326         }
327
328         if (new->merge == MERGE_REPLACE) {
329             if (report)
330                 log_warn(info->ctx,
331                          "Map for indicator %s redefined; "
332                          "Earlier definition ignored\n",
333                          xkb_atom_text(info->ctx, old->led.name));
334             *old = *new;
335             return true;
336         }
337
338         collide = 0;
339         if (UseNewLEDField(LED_FIELD_MODS, old, new, report, &collide)) {
340             old->led.which_mods = new->led.which_mods;
341             old->led.mods = new->led.mods;
342             old->defined |= LED_FIELD_MODS;
343         }
344         if (UseNewLEDField(LED_FIELD_GROUPS, old, new, report, &collide)) {
345             old->led.which_groups = new->led.which_groups;
346             old->led.groups = new->led.groups;
347             old->defined |= LED_FIELD_GROUPS;
348         }
349         if (UseNewLEDField(LED_FIELD_CTRLS, old, new, report, &collide)) {
350             old->led.ctrls = new->led.ctrls;
351             old->defined |= LED_FIELD_CTRLS;
352         }
353
354         if (collide) {
355             log_warn(info->ctx,
356                      "Map for indicator %s redefined; "
357                      "Using %s definition for duplicate fields\n",
358                      xkb_atom_text(info->ctx, old->led.name),
359                      (new->merge == MERGE_AUGMENT ? "first" : "last"));
360         }
361
362         return true;
363     }
364
365     darray_append(info->leds, *new);
366     return true;
367 }
368
369 static void
370 MergeIncludedCompatMaps(CompatInfo *into, CompatInfo *from,
371                         enum merge_mode merge)
372 {
373     SymInterpInfo *si;
374     LedInfo *ledi;
375
376     if (from->errorCount > 0) {
377         into->errorCount += from->errorCount;
378         return;
379     }
380
381     if (into->name == NULL) {
382         into->name = from->name;
383         from->name = NULL;
384     }
385
386     if (darray_empty(into->interps)) {
387         into->interps = from->interps;
388         darray_init(from->interps);
389     }
390     else {
391         darray_foreach(si, from->interps) {
392             si->merge = (merge == MERGE_DEFAULT ? si->merge : merge);
393             if (!AddInterp(into, si, false))
394                 into->errorCount++;
395         }
396     }
397
398     if (darray_empty(into->leds)) {
399         into->leds = from->leds;
400         darray_init(from->leds);
401     }
402     else {
403         darray_foreach(ledi, from->leds) {
404             ledi->merge = (merge == MERGE_DEFAULT ? ledi->merge : merge);
405             if (!AddLedMap(into, ledi, false))
406                 into->errorCount++;
407         }
408     }
409 }
410
411 static void
412 HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge);
413
414 static bool
415 HandleIncludeCompatMap(CompatInfo *info, IncludeStmt *include)
416 {
417     CompatInfo included;
418
419     InitCompatInfo(&included, info->keymap, info->actions);
420     included.name = include->stmt;
421     include->stmt = NULL;
422
423     for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
424         CompatInfo next_incl;
425         XkbFile *file;
426
427         file = ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_COMPAT);
428         if (!file) {
429             info->errorCount += 10;
430             ClearCompatInfo(&included);
431             return false;
432         }
433
434         InitCompatInfo(&next_incl, info->keymap, info->actions);
435         next_incl.default_interp = info->default_interp;
436         next_incl.default_interp.merge = stmt->merge;
437         next_incl.default_led = info->default_led;
438         next_incl.default_led.merge = stmt->merge;
439
440         HandleCompatMapFile(&next_incl, file, MERGE_OVERRIDE);
441
442         MergeIncludedCompatMaps(&included, &next_incl, stmt->merge);
443
444         ClearCompatInfo(&next_incl);
445         FreeXkbFile(file);
446     }
447
448     MergeIncludedCompatMaps(info, &included, include->merge);
449     ClearCompatInfo(&included);
450
451     return (info->errorCount == 0);
452 }
453
454 static bool
455 SetInterpField(CompatInfo *info, SymInterpInfo *si, const char *field,
456                ExprDef *arrayNdx, ExprDef *value)
457 {
458     xkb_mod_index_t ndx;
459
460     if (istreq(field, "action")) {
461         if (arrayNdx)
462             return ReportSINotArray(info, si, field);
463
464         if (!HandleActionDef(info->ctx, info->actions, &info->keymap->mods,
465                              value, &si->interp.action))
466             return false;
467
468         si->defined |= SI_FIELD_ACTION;
469     }
470     else if (istreq(field, "virtualmodifier") ||
471              istreq(field, "virtualmod")) {
472         if (arrayNdx)
473             return ReportSINotArray(info, si, field);
474
475         if (!ExprResolveMod(info->ctx, value, MOD_VIRT, &info->keymap->mods,
476                             &ndx))
477             return ReportSIBadType(info, si, field, "virtual modifier");
478
479         si->interp.virtual_mod = ndx;
480         si->defined |= SI_FIELD_VIRTUAL_MOD;
481     }
482     else if (istreq(field, "repeat")) {
483         bool set;
484
485         if (arrayNdx)
486             return ReportSINotArray(info, si, field);
487
488         if (!ExprResolveBoolean(info->ctx, value, &set))
489             return ReportSIBadType(info, si, field, "boolean");
490
491         si->interp.repeat = set;
492
493         si->defined |= SI_FIELD_AUTO_REPEAT;
494     }
495     else if (istreq(field, "locking")) {
496         log_dbg(info->ctx,
497                 "The \"locking\" field in symbol interpretation is unsupported; "
498                 "Ignored\n");
499     }
500     else if (istreq(field, "usemodmap") ||
501              istreq(field, "usemodmapmods")) {
502         unsigned int val;
503
504         if (arrayNdx)
505             return ReportSINotArray(info, si, field);
506
507         if (!ExprResolveEnum(info->ctx, value, &val, useModMapValueNames))
508             return ReportSIBadType(info, si, field, "level specification");
509
510         si->interp.level_one_only = !!val;
511         si->defined |= SI_FIELD_LEVEL_ONE_ONLY;
512     }
513     else {
514         return ReportBadField(info->ctx, "symbol interpretation", field,
515                               siText(si, info));
516     }
517
518     return true;
519 }
520
521 static bool
522 SetLedMapField(CompatInfo *info, LedInfo *ledi, const char *field,
523                ExprDef *arrayNdx, ExprDef *value)
524 {
525     bool ok = true;
526
527     if (istreq(field, "modifiers") || istreq(field, "mods")) {
528         if (arrayNdx)
529             return ReportLedNotArray(info, ledi, field);
530
531         if (!ExprResolveModMask(info->ctx, value, MOD_BOTH,
532                                 &info->keymap->mods, &ledi->led.mods.mods))
533             return ReportLedBadType(info, ledi, field, "modifier mask");
534
535         ledi->defined |= LED_FIELD_MODS;
536     }
537     else if (istreq(field, "groups")) {
538         unsigned int mask;
539
540         if (arrayNdx)
541             return ReportLedNotArray(info, ledi, field);
542
543         if (!ExprResolveMask(info->ctx, value, &mask, groupMaskNames))
544             return ReportLedBadType(info, ledi, field, "group mask");
545
546         ledi->led.groups = mask;
547         ledi->defined |= LED_FIELD_GROUPS;
548     }
549     else if (istreq(field, "controls") || istreq(field, "ctrls")) {
550         unsigned int mask;
551
552         if (arrayNdx)
553             return ReportLedNotArray(info, ledi, field);
554
555         if (!ExprResolveMask(info->ctx, value, &mask, ctrlMaskNames))
556             return ReportLedBadType(info, ledi, field, "controls mask");
557
558         ledi->led.ctrls = mask;
559         ledi->defined |= LED_FIELD_CTRLS;
560     }
561     else if (istreq(field, "allowexplicit")) {
562         log_dbg(info->ctx,
563                 "The \"allowExplicit\" field in indicator statements is unsupported; "
564                 "Ignored\n");
565     }
566     else if (istreq(field, "whichmodstate") ||
567              istreq(field, "whichmodifierstate")) {
568         unsigned int mask;
569
570         if (arrayNdx)
571             return ReportLedNotArray(info, ledi, field);
572
573         if (!ExprResolveMask(info->ctx, value, &mask,
574                              modComponentMaskNames))
575             return ReportLedBadType(info, ledi, field,
576                                     "mask of modifier state components");
577
578         ledi->led.which_mods = mask;
579     }
580     else if (istreq(field, "whichgroupstate")) {
581         unsigned mask;
582
583         if (arrayNdx)
584             return ReportLedNotArray(info, ledi, field);
585
586         if (!ExprResolveMask(info->ctx, value, &mask,
587                              groupComponentMaskNames))
588             return ReportLedBadType(info, ledi, field,
589                                     "mask of group state components");
590
591         ledi->led.which_groups = mask;
592     }
593     else if (istreq(field, "driveskbd") ||
594              istreq(field, "driveskeyboard") ||
595              istreq(field, "leddriveskbd") ||
596              istreq(field, "leddriveskeyboard") ||
597              istreq(field, "indicatordriveskbd") ||
598              istreq(field, "indicatordriveskeyboard")) {
599         log_dbg(info->ctx,
600                 "The \"%s\" field in indicator statements is unsupported; "
601                 "Ignored\n", field);
602     }
603     else if (istreq(field, "index")) {
604         /* Users should see this, it might cause unexpected behavior. */
605         log_err(info->ctx,
606                 "The \"index\" field in indicator statements is unsupported; "
607                 "Ignored\n");
608     }
609     else {
610         log_err(info->ctx,
611                 "Unknown field %s in map for %s indicator; "
612                 "Definition ignored\n",
613                 field, xkb_atom_text(info->ctx, ledi->led.name));
614         ok = false;
615     }
616
617     return ok;
618 }
619
620 static bool
621 HandleGlobalVar(CompatInfo *info, VarDef *stmt)
622 {
623     const char *elem, *field;
624     ExprDef *ndx;
625     bool ret;
626
627     if (!ExprResolveLhs(info->ctx, stmt->name, &elem, &field, &ndx))
628         ret = false;
629     else if (elem && istreq(elem, "interpret"))
630         ret = SetInterpField(info, &info->default_interp, field, ndx,
631                              stmt->value);
632     else if (elem && istreq(elem, "indicator"))
633         ret = SetLedMapField(info, &info->default_led, field, ndx,
634                              stmt->value);
635     else
636         ret = SetActionField(info->ctx, info->actions, &info->keymap->mods,
637                              elem, field, ndx, stmt->value);
638     return ret;
639 }
640
641 static bool
642 HandleInterpBody(CompatInfo *info, VarDef *def, SymInterpInfo *si)
643 {
644     bool ok = true;
645     const char *elem, *field;
646     ExprDef *arrayNdx;
647
648     for (; def; def = (VarDef *) def->common.next) {
649         if (def->name && def->name->expr.op == EXPR_FIELD_REF) {
650             log_err(info->ctx,
651                     "Cannot set a global default value from within an interpret statement; "
652                     "Move statements to the global file scope\n");
653             ok = false;
654             continue;
655         }
656
657         ok = ExprResolveLhs(info->ctx, def->name, &elem, &field, &arrayNdx);
658         if (!ok)
659             continue;
660
661         ok = SetInterpField(info, si, field, arrayNdx, def->value);
662     }
663
664     return ok;
665 }
666
667 static bool
668 HandleInterpDef(CompatInfo *info, InterpDef *def, enum merge_mode merge)
669 {
670     enum xkb_match_operation pred;
671     xkb_mod_mask_t mods;
672     SymInterpInfo si;
673
674     if (!ResolveStateAndPredicate(def->match, &pred, &mods, info)) {
675         log_err(info->ctx,
676                 "Couldn't determine matching modifiers; "
677                 "Symbol interpretation ignored\n");
678         return false;
679     }
680
681     si = info->default_interp;
682     si.merge = merge = (def->merge == MERGE_DEFAULT ? merge : def->merge);
683     si.interp.sym = def->sym;
684     si.interp.match = pred;
685     si.interp.mods = mods;
686
687     if (!HandleInterpBody(info, def->def, &si)) {
688         info->errorCount++;
689         return false;
690     }
691
692     if (!AddInterp(info, &si, true)) {
693         info->errorCount++;
694         return false;
695     }
696
697     return true;
698 }
699
700 static bool
701 HandleLedMapDef(CompatInfo *info, LedMapDef *def, enum merge_mode merge)
702 {
703     LedInfo ledi;
704     VarDef *var;
705     bool ok;
706
707     if (def->merge != MERGE_DEFAULT)
708         merge = def->merge;
709
710     ledi = info->default_led;
711     ledi.merge = merge;
712     ledi.led.name = def->name;
713
714     ok = true;
715     for (var = def->body; var != NULL; var = (VarDef *) var->common.next) {
716         const char *elem, *field;
717         ExprDef *arrayNdx;
718         if (!ExprResolveLhs(info->ctx, var->name, &elem, &field, &arrayNdx)) {
719             ok = false;
720             continue;
721         }
722
723         if (elem) {
724             log_err(info->ctx,
725                     "Cannot set defaults for \"%s\" element in indicator map; "
726                     "Assignment to %s.%s ignored\n", elem, elem, field);
727             ok = false;
728         }
729         else {
730             ok = SetLedMapField(info, &ledi, field, arrayNdx, var->value) && ok;
731         }
732     }
733
734     if (ok)
735         return AddLedMap(info, &ledi, true);
736
737     return false;
738 }
739
740 static void
741 HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge)
742 {
743     bool ok;
744
745     merge = (merge == MERGE_DEFAULT ? MERGE_AUGMENT : merge);
746
747     free(info->name);
748     info->name = strdup_safe(file->name);
749
750     for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
751         switch (stmt->type) {
752         case STMT_INCLUDE:
753             ok = HandleIncludeCompatMap(info, (IncludeStmt *) stmt);
754             break;
755         case STMT_INTERP:
756             ok = HandleInterpDef(info, (InterpDef *) stmt, merge);
757             break;
758         case STMT_GROUP_COMPAT:
759             log_dbg(info->ctx,
760                     "The \"group\" statement in compat is unsupported; "
761                     "Ignored\n");
762             ok = true;
763             break;
764         case STMT_LED_MAP:
765             ok = HandleLedMapDef(info, (LedMapDef *) stmt, merge);
766             break;
767         case STMT_VAR:
768             ok = HandleGlobalVar(info, (VarDef *) stmt);
769             break;
770         case STMT_VMOD:
771             ok = HandleVModDef(info->keymap, (VModDef *) stmt, merge);
772             break;
773         default:
774             log_err(info->ctx,
775                     "Compat files may not include other types; "
776                     "Ignoring %s\n", stmt_type_to_string(stmt->type));
777             ok = false;
778             break;
779         }
780
781         if (!ok)
782             info->errorCount++;
783
784         if (info->errorCount > 10) {
785             log_err(info->ctx,
786                     "Abandoning compatibility map \"%s\"\n", file->topName);
787             break;
788         }
789     }
790 }
791
792 /* Temporary struct for CopyInterps. */
793 struct collect {
794     darray(struct xkb_sym_interpret) sym_interprets;
795 };
796
797 static void
798 CopyInterps(CompatInfo *info, bool needSymbol, enum xkb_match_operation pred,
799             struct collect *collect)
800 {
801     SymInterpInfo *si;
802
803     darray_foreach(si, info->interps)
804         if (si->interp.match == pred &&
805             (si->interp.sym != XKB_KEY_NoSymbol) == needSymbol)
806             darray_append(collect->sym_interprets, si->interp);
807 }
808
809 static void
810 CopyLedMapDefs(CompatInfo *info)
811 {
812     LedInfo *ledi;
813     xkb_led_index_t i;
814     struct xkb_led *led;
815     struct xkb_keymap *keymap = info->keymap;
816
817     darray_foreach(ledi, info->leds) {
818         /*
819          * Find the LED with the given name, if it was already declared
820          * in keycodes.
821          */
822         darray_enumerate(i, led, keymap->leds)
823             if (led->name == ledi->led.name)
824                 break;
825
826         /* Not previously declared; create it with next free index. */
827         if (i >= darray_size(keymap->leds)) {
828             log_dbg(keymap->ctx,
829                     "Indicator name \"%s\" was not declared in the keycodes section; "
830                     "Adding new indicator\n",
831                     xkb_atom_text(keymap->ctx, ledi->led.name));
832
833             darray_enumerate(i, led, keymap->leds)
834                 if (led->name == XKB_ATOM_NONE)
835                     break;
836
837             if (i >= darray_size(keymap->leds)) {
838                 /* Not place to put it; ignore. */
839                 if (i >= XKB_MAX_LEDS) {
840                     log_err(keymap->ctx,
841                             "Too many indicators (maximum is %d); "
842                             "Indicator name \"%s\" ignored\n",
843                             XKB_MAX_LEDS,
844                             xkb_atom_text(keymap->ctx, ledi->led.name));
845                     continue;
846                 }
847                 /* Add a new LED. */
848                 darray_resize(keymap->leds, i + 1);
849                 led = &darray_item(keymap->leds, i);
850             }
851         }
852
853         *led = ledi->led;
854         if (led->groups != 0 && led->which_groups == 0)
855             led->which_groups = XKB_STATE_LAYOUT_EFFECTIVE;
856         if (led->mods.mods != 0 && led->which_mods == 0)
857             led->which_mods = XKB_STATE_MODS_EFFECTIVE;
858     }
859 }
860
861 static bool
862 CopyCompatToKeymap(struct xkb_keymap *keymap, CompatInfo *info)
863 {
864     keymap->compat_section_name = strdup_safe(info->name);
865     XkbEscapeMapName(keymap->compat_section_name);
866
867     if (!darray_empty(info->interps)) {
868         struct collect collect;
869         darray_init(collect.sym_interprets);
870
871         /* Most specific to least specific. */
872         CopyInterps(info, true, MATCH_EXACTLY, &collect);
873         CopyInterps(info, true, MATCH_ALL, &collect);
874         CopyInterps(info, true, MATCH_NONE, &collect);
875         CopyInterps(info, true, MATCH_ANY, &collect);
876         CopyInterps(info, true, MATCH_ANY_OR_NONE, &collect);
877         CopyInterps(info, false, MATCH_EXACTLY, &collect);
878         CopyInterps(info, false, MATCH_ALL, &collect);
879         CopyInterps(info, false, MATCH_NONE, &collect);
880         CopyInterps(info, false, MATCH_ANY, &collect);
881         CopyInterps(info, false, MATCH_ANY_OR_NONE, &collect);
882
883         keymap->num_sym_interprets = darray_size(collect.sym_interprets);
884         keymap->sym_interprets = darray_mem(collect.sym_interprets, 0);
885     }
886
887     CopyLedMapDefs(info);
888
889     return true;
890 }
891
892 bool
893 CompileCompatMap(XkbFile *file, struct xkb_keymap *keymap,
894                  enum merge_mode merge)
895 {
896     CompatInfo info;
897     ActionsInfo *actions;
898
899     actions = NewActionsInfo();
900     if (!actions)
901         return false;
902
903     InitCompatInfo(&info, keymap, actions);
904     info.default_interp.merge = merge;
905     info.default_led.merge = merge;
906
907     HandleCompatMapFile(&info, file, merge);
908     if (info.errorCount != 0)
909         goto err_info;
910
911     if (!CopyCompatToKeymap(keymap, &info))
912         goto err_info;
913
914     ClearCompatInfo(&info);
915     FreeActionsInfo(actions);
916     return true;
917
918 err_info:
919     ClearCompatInfo(&info);
920     FreeActionsInfo(actions);
921     return false;
922 }