symbols: steal keys and modmaps when merging if possible
[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_keymap *keymap;
93 } CompatInfo;
94
95 static const char *
96 siText(SymInterpInfo *si, CompatInfo *info)
97 {
98     char *buf = xkb_context_get_buffer(info->keymap->ctx, 128);
99
100     if (si == &info->default_interp)
101         return "default";
102
103     snprintf(buf, 128, "%s+%s(%s)",
104              KeysymText(info->keymap->ctx, si->interp.sym),
105              SIMatchText(si->interp.match),
106              ModMaskText(info->keymap, si->interp.mods));
107
108     return buf;
109 }
110
111 static inline bool
112 ReportSINotArray(CompatInfo *info, SymInterpInfo *si, const char *field)
113 {
114     return ReportNotArray(info->keymap->ctx, "symbol interpretation", field,
115                           siText(si, info));
116 }
117
118 static inline bool
119 ReportSIBadType(CompatInfo *info, SymInterpInfo *si, const char *field,
120                 const char *wanted)
121 {
122     return ReportBadType(info->keymap->ctx, "symbol interpretation", field,
123                          siText(si, info), wanted);
124 }
125
126 static inline bool
127 ReportLedBadType(CompatInfo *info, LedInfo *ledi, const char *field,
128                  const char *wanted)
129 {
130     return ReportBadType(info->keymap->ctx, "indicator map", field,
131                          xkb_atom_text(info->keymap->ctx, ledi->led.name),
132                          wanted);
133 }
134
135 static inline bool
136 ReportLedNotArray(CompatInfo *info, LedInfo *ledi, const char *field)
137 {
138     return ReportNotArray(info->keymap->ctx, "indicator map", field,
139                           xkb_atom_text(info->keymap->ctx, ledi->led.name));
140 }
141
142 static void
143 InitCompatInfo(CompatInfo *info, struct xkb_keymap *keymap,
144                ActionsInfo *actions)
145 {
146     memset(info, 0, sizeof(*info));
147     info->keymap = keymap;
148     info->actions = actions;
149     info->default_interp.merge = MERGE_OVERRIDE;
150     info->default_interp.interp.virtual_mod = XKB_MOD_INVALID;
151     info->default_led.merge = MERGE_OVERRIDE;
152 }
153
154 static void
155 ClearCompatInfo(CompatInfo *info)
156 {
157     free(info->name);
158     darray_free(info->interps);
159     darray_free(info->leds);
160 }
161
162 static SymInterpInfo *
163 FindMatchingInterp(CompatInfo *info, SymInterpInfo *new)
164 {
165     SymInterpInfo *old;
166
167     darray_foreach(old, info->interps)
168         if (old->interp.sym == new->interp.sym &&
169             old->interp.mods == new->interp.mods &&
170             old->interp.match == new->interp.match)
171             return old;
172
173     return NULL;
174 }
175
176 static bool
177 UseNewInterpField(enum si_field field, SymInterpInfo *old, SymInterpInfo *new,
178                   bool report, enum si_field *collide)
179 {
180     if (!(old->defined & field))
181         return true;
182
183     if (new->defined & field) {
184         if (report)
185             *collide |= field;
186
187         if (new->merge != MERGE_AUGMENT)
188             return true;
189     }
190
191     return false;
192 }
193
194 static bool
195 AddInterp(CompatInfo *info, SymInterpInfo *new, bool same_file)
196 {
197     SymInterpInfo *old = FindMatchingInterp(info, new);
198     if (old) {
199         const int verbosity = xkb_context_get_log_verbosity(info->keymap->ctx);
200         const bool report = (same_file && verbosity > 0) || verbosity > 9;
201         enum si_field collide = 0;
202
203         if (new->merge == MERGE_REPLACE) {
204             if (report)
205                 log_warn(info->keymap->ctx,
206                          "Multiple definitions for \"%s\"; "
207                          "Earlier interpretation ignored\n",
208                          siText(new, info));
209             *old = *new;
210             return true;
211         }
212
213         if (UseNewInterpField(SI_FIELD_VIRTUAL_MOD, old, new, report,
214                               &collide)) {
215             old->interp.virtual_mod = new->interp.virtual_mod;
216             old->defined |= SI_FIELD_VIRTUAL_MOD;
217         }
218         if (UseNewInterpField(SI_FIELD_ACTION, old, new, report,
219                               &collide)) {
220             old->interp.action = new->interp.action;
221             old->defined |= SI_FIELD_ACTION;
222         }
223         if (UseNewInterpField(SI_FIELD_AUTO_REPEAT, old, new, report,
224                               &collide)) {
225             old->interp.repeat = new->interp.repeat;
226             old->defined |= SI_FIELD_AUTO_REPEAT;
227         }
228         if (UseNewInterpField(SI_FIELD_LEVEL_ONE_ONLY, old, new, report,
229                               &collide)) {
230             old->interp.level_one_only = new->interp.level_one_only;
231             old->defined |= SI_FIELD_LEVEL_ONE_ONLY;
232         }
233
234         if (collide) {
235             log_warn(info->keymap->ctx,
236                      "Multiple interpretations of \"%s\"; "
237                      "Using %s definition for duplicate fields\n",
238                      siText(new, info),
239                      (new->merge != MERGE_AUGMENT ? "last" : "first"));
240         }
241
242         return true;
243     }
244
245     darray_append(info->interps, *new);
246     return true;
247 }
248
249 /***====================================================================***/
250
251 static bool
252 ResolveStateAndPredicate(ExprDef *expr, enum xkb_match_operation *pred_rtrn,
253                          xkb_mod_mask_t *mods_rtrn, CompatInfo *info)
254 {
255     if (expr == NULL) {
256         *pred_rtrn = MATCH_ANY_OR_NONE;
257         *mods_rtrn = MOD_REAL_MASK_ALL;
258         return true;
259     }
260
261     *pred_rtrn = MATCH_EXACTLY;
262     if (expr->expr.op == EXPR_ACTION_DECL) {
263         const char *pred_txt = xkb_atom_text(info->keymap->ctx,
264                                              expr->action.name);
265         if (!LookupString(symInterpretMatchMaskNames, pred_txt, pred_rtrn)) {
266             log_err(info->keymap->ctx,
267                     "Illegal modifier predicate \"%s\"; Ignored\n", pred_txt);
268             return false;
269         }
270         expr = expr->action.args;
271     }
272     else if (expr->expr.op == EXPR_IDENT) {
273         const char *pred_txt = xkb_atom_text(info->keymap->ctx,
274                                              expr->ident.ident);
275         if (pred_txt && istreq(pred_txt, "any")) {
276             *pred_rtrn = MATCH_ANY;
277             *mods_rtrn = MOD_REAL_MASK_ALL;
278             return true;
279         }
280     }
281
282     return ExprResolveModMask(info->keymap, expr, MOD_REAL, mods_rtrn);
283 }
284
285 /***====================================================================***/
286
287 static bool
288 UseNewLEDField(enum led_field field, LedInfo *old, LedInfo *new,
289                bool report, enum led_field *collide)
290 {
291     if (!(old->defined & field))
292         return true;
293
294     if (new->defined & field) {
295         if (report)
296             *collide |= field;
297
298         if (new->merge != MERGE_AUGMENT)
299             return true;
300     }
301
302     return false;
303 }
304
305 static bool
306 AddLedMap(CompatInfo *info, LedInfo *new, bool same_file)
307 {
308     LedInfo *old;
309     enum led_field collide;
310     struct xkb_context *ctx = info->keymap->ctx;
311     const int verbosity = xkb_context_get_log_verbosity(ctx);
312     const bool report = (same_file && verbosity > 0) || verbosity > 9;
313
314     darray_foreach(old, info->leds) {
315         if (old->led.name != new->led.name)
316             continue;
317
318         if (old->led.mods.mods == new->led.mods.mods &&
319             old->led.groups == new->led.groups &&
320             old->led.ctrls == new->led.ctrls &&
321             old->led.which_mods == new->led.which_mods &&
322             old->led.which_groups == new->led.which_groups) {
323             old->defined |= new->defined;
324             return true;
325         }
326
327         if (new->merge == MERGE_REPLACE) {
328             if (report)
329                 log_warn(info->keymap->ctx,
330                          "Map for indicator %s redefined; "
331                          "Earlier definition ignored\n",
332                          xkb_atom_text(ctx, old->led.name));
333             *old = *new;
334             return true;
335         }
336
337         collide = 0;
338         if (UseNewLEDField(LED_FIELD_MODS, old, new, report, &collide)) {
339             old->led.which_mods = new->led.which_mods;
340             old->led.mods = new->led.mods;
341             old->defined |= LED_FIELD_MODS;
342         }
343         if (UseNewLEDField(LED_FIELD_GROUPS, old, new, report, &collide)) {
344             old->led.which_groups = new->led.which_groups;
345             old->led.groups = new->led.groups;
346             old->defined |= LED_FIELD_GROUPS;
347         }
348         if (UseNewLEDField(LED_FIELD_CTRLS, old, new, report, &collide)) {
349             old->led.ctrls = new->led.ctrls;
350             old->defined |= LED_FIELD_CTRLS;
351         }
352
353         if (collide) {
354             log_warn(info->keymap->ctx,
355                      "Map for indicator %s redefined; "
356                      "Using %s definition for duplicate fields\n",
357                      xkb_atom_text(ctx, old->led.name),
358                      (new->merge == MERGE_AUGMENT ? "first" : "last"));
359         }
360
361         return true;
362     }
363
364     darray_append(info->leds, *new);
365     return true;
366 }
367
368 static void
369 MergeIncludedCompatMaps(CompatInfo *into, CompatInfo *from,
370                         enum merge_mode merge)
371 {
372     SymInterpInfo *si;
373     LedInfo *ledi;
374
375     if (from->errorCount > 0) {
376         into->errorCount += from->errorCount;
377         return;
378     }
379
380     if (into->name == NULL) {
381         into->name = from->name;
382         from->name = NULL;
383     }
384
385     if (darray_empty(into->interps)) {
386         into->interps = from->interps;
387         darray_init(from->interps);
388     }
389     else {
390         darray_foreach(si, from->interps) {
391             si->merge = (merge == MERGE_DEFAULT ? si->merge : merge);
392             if (!AddInterp(into, si, false))
393                 into->errorCount++;
394         }
395     }
396
397     if (darray_empty(into->leds)) {
398         into->leds = from->leds;
399         darray_init(from->leds);
400     }
401     else {
402         darray_foreach(ledi, from->leds) {
403             ledi->merge = (merge == MERGE_DEFAULT ? ledi->merge : merge);
404             if (!AddLedMap(into, ledi, false))
405                 into->errorCount++;
406         }
407     }
408 }
409
410 static void
411 HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge);
412
413 static bool
414 HandleIncludeCompatMap(CompatInfo *info, IncludeStmt *include)
415 {
416     CompatInfo included;
417
418     InitCompatInfo(&included, info->keymap, info->actions);
419     included.name = include->stmt;
420     include->stmt = NULL;
421
422     for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
423         CompatInfo next_incl;
424         XkbFile *file;
425
426         file = ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_COMPAT);
427         if (!file) {
428             info->errorCount += 10;
429             ClearCompatInfo(&included);
430             return false;
431         }
432
433         InitCompatInfo(&next_incl, info->keymap, info->actions);
434         next_incl.default_interp = info->default_interp;
435         next_incl.default_interp.merge = stmt->merge;
436         next_incl.default_led = info->default_led;
437         next_incl.default_led.merge = stmt->merge;
438
439         HandleCompatMapFile(&next_incl, file, MERGE_OVERRIDE);
440
441         MergeIncludedCompatMaps(&included, &next_incl, stmt->merge);
442
443         ClearCompatInfo(&next_incl);
444         FreeXkbFile(file);
445     }
446
447     MergeIncludedCompatMaps(info, &included, include->merge);
448     ClearCompatInfo(&included);
449
450     return (info->errorCount == 0);
451 }
452
453 static bool
454 SetInterpField(CompatInfo *info, SymInterpInfo *si, const char *field,
455                ExprDef *arrayNdx, ExprDef *value)
456 {
457     struct xkb_keymap *keymap = info->keymap;
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(value, keymap, &si->interp.action, info->actions))
465             return false;
466
467         si->defined |= SI_FIELD_ACTION;
468     }
469     else if (istreq(field, "virtualmodifier") ||
470              istreq(field, "virtualmod")) {
471         if (arrayNdx)
472             return ReportSINotArray(info, si, field);
473
474         if (!ExprResolveMod(keymap, value, MOD_VIRT, &ndx))
475             return ReportSIBadType(info, si, field, "virtual modifier");
476
477         si->interp.virtual_mod = ndx;
478         si->defined |= SI_FIELD_VIRTUAL_MOD;
479     }
480     else if (istreq(field, "repeat")) {
481         bool set;
482
483         if (arrayNdx)
484             return ReportSINotArray(info, si, field);
485
486         if (!ExprResolveBoolean(keymap->ctx, value, &set))
487             return ReportSIBadType(info, si, field, "boolean");
488
489         si->interp.repeat = set;
490
491         si->defined |= SI_FIELD_AUTO_REPEAT;
492     }
493     else if (istreq(field, "locking")) {
494         log_dbg(info->keymap->ctx,
495                 "The \"locking\" field in symbol interpretation is unsupported; "
496                 "Ignored\n");
497     }
498     else if (istreq(field, "usemodmap") ||
499              istreq(field, "usemodmapmods")) {
500         unsigned int val;
501
502         if (arrayNdx)
503             return ReportSINotArray(info, si, field);
504
505         if (!ExprResolveEnum(keymap->ctx, value, &val, useModMapValueNames))
506             return ReportSIBadType(info, si, field, "level specification");
507
508         si->interp.level_one_only = !!val;
509         si->defined |= SI_FIELD_LEVEL_ONE_ONLY;
510     }
511     else {
512         return ReportBadField(keymap->ctx, "symbol interpretation", field,
513                               siText(si, info));
514     }
515
516     return true;
517 }
518
519 static bool
520 SetLedMapField(CompatInfo *info, LedInfo *ledi, const char *field,
521                ExprDef *arrayNdx, ExprDef *value)
522 {
523     bool ok = true;
524     struct xkb_keymap *keymap = info->keymap;
525
526     if (istreq(field, "modifiers") || istreq(field, "mods")) {
527         if (arrayNdx)
528             return ReportLedNotArray(info, ledi, field);
529
530         if (!ExprResolveModMask(keymap, value, MOD_BOTH, &ledi->led.mods.mods))
531             return ReportLedBadType(info, ledi, field, "modifier mask");
532
533         ledi->defined |= LED_FIELD_MODS;
534     }
535     else if (istreq(field, "groups")) {
536         unsigned int mask;
537
538         if (arrayNdx)
539             return ReportLedNotArray(info, ledi, field);
540
541         if (!ExprResolveMask(keymap->ctx, value, &mask, groupMaskNames))
542             return ReportLedBadType(info, ledi, field, "group mask");
543
544         ledi->led.groups = mask;
545         ledi->defined |= LED_FIELD_GROUPS;
546     }
547     else if (istreq(field, "controls") || istreq(field, "ctrls")) {
548         unsigned int mask;
549
550         if (arrayNdx)
551             return ReportLedNotArray(info, ledi, field);
552
553         if (!ExprResolveMask(keymap->ctx, value, &mask, ctrlMaskNames))
554             return ReportLedBadType(info, ledi, field, "controls mask");
555
556         ledi->led.ctrls = mask;
557         ledi->defined |= LED_FIELD_CTRLS;
558     }
559     else if (istreq(field, "allowexplicit")) {
560         log_dbg(info->keymap->ctx,
561                 "The \"allowExplicit\" field in indicator statements is unsupported; "
562                 "Ignored\n");
563     }
564     else if (istreq(field, "whichmodstate") ||
565              istreq(field, "whichmodifierstate")) {
566         unsigned int mask;
567
568         if (arrayNdx)
569             return ReportLedNotArray(info, ledi, field);
570
571         if (!ExprResolveMask(keymap->ctx, value, &mask,
572                              modComponentMaskNames))
573             return ReportLedBadType(info, ledi, field,
574                                     "mask of modifier state components");
575
576         ledi->led.which_mods = mask;
577     }
578     else if (istreq(field, "whichgroupstate")) {
579         unsigned mask;
580
581         if (arrayNdx)
582             return ReportLedNotArray(info, ledi, field);
583
584         if (!ExprResolveMask(keymap->ctx, value, &mask,
585                              groupComponentMaskNames))
586             return ReportLedBadType(info, ledi, field,
587                                     "mask of group state components");
588
589         ledi->led.which_groups = mask;
590     }
591     else if (istreq(field, "driveskbd") ||
592              istreq(field, "driveskeyboard") ||
593              istreq(field, "leddriveskbd") ||
594              istreq(field, "leddriveskeyboard") ||
595              istreq(field, "indicatordriveskbd") ||
596              istreq(field, "indicatordriveskeyboard")) {
597         log_dbg(info->keymap->ctx,
598                 "The \"%s\" field in indicator statements is unsupported; "
599                 "Ignored\n", field);
600     }
601     else if (istreq(field, "index")) {
602         /* Users should see this, it might cause unexpected behavior. */
603         log_err(info->keymap->ctx,
604                 "The \"index\" field in indicator statements is unsupported; "
605                 "Ignored\n");
606     }
607     else {
608         log_err(info->keymap->ctx,
609                 "Unknown field %s in map for %s indicator; "
610                 "Definition ignored\n",
611                 field, xkb_atom_text(keymap->ctx, ledi->led.name));
612         ok = false;
613     }
614
615     return ok;
616 }
617
618 static bool
619 HandleGlobalVar(CompatInfo *info, VarDef *stmt)
620 {
621     const char *elem, *field;
622     ExprDef *ndx;
623     bool ret;
624
625     if (!ExprResolveLhs(info->keymap->ctx, stmt->name, &elem, &field, &ndx))
626         ret = false;
627     else if (elem && istreq(elem, "interpret"))
628         ret = SetInterpField(info, &info->default_interp, field, ndx,
629                              stmt->value);
630     else if (elem && istreq(elem, "indicator"))
631         ret = SetLedMapField(info, &info->default_led, field, ndx,
632                              stmt->value);
633     else
634         ret = SetActionField(info->keymap, elem, field, ndx, stmt->value,
635                              info->actions);
636     return ret;
637 }
638
639 static bool
640 HandleInterpBody(CompatInfo *info, VarDef *def, SymInterpInfo *si)
641 {
642     bool ok = true;
643     const char *elem, *field;
644     ExprDef *arrayNdx;
645
646     for (; def; def = (VarDef *) def->common.next) {
647         if (def->name && def->name->expr.op == EXPR_FIELD_REF) {
648             log_err(info->keymap->ctx,
649                     "Cannot set a global default value from within an interpret statement; "
650                     "Move statements to the global file scope\n");
651             ok = false;
652             continue;
653         }
654
655         ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
656                             &arrayNdx);
657         if (!ok)
658             continue;
659
660         ok = SetInterpField(info, si, field, arrayNdx, def->value);
661     }
662
663     return ok;
664 }
665
666 static bool
667 HandleInterpDef(CompatInfo *info, InterpDef *def, enum merge_mode merge)
668 {
669     enum xkb_match_operation pred;
670     xkb_mod_mask_t mods;
671     SymInterpInfo si;
672
673     if (!ResolveStateAndPredicate(def->match, &pred, &mods, info)) {
674         log_err(info->keymap->ctx,
675                 "Couldn't determine matching modifiers; "
676                 "Symbol interpretation ignored\n");
677         return false;
678     }
679
680     si = info->default_interp;
681     si.merge = merge = (def->merge == MERGE_DEFAULT ? merge : def->merge);
682     si.interp.sym = def->sym;
683     si.interp.match = pred;
684     si.interp.mods = mods;
685
686     if (!HandleInterpBody(info, def->def, &si)) {
687         info->errorCount++;
688         return false;
689     }
690
691     if (!AddInterp(info, &si, true)) {
692         info->errorCount++;
693         return false;
694     }
695
696     return true;
697 }
698
699 static bool
700 HandleLedMapDef(CompatInfo *info, LedMapDef *def, enum merge_mode merge)
701 {
702     LedInfo ledi;
703     VarDef *var;
704     bool ok;
705
706     if (def->merge != MERGE_DEFAULT)
707         merge = def->merge;
708
709     ledi = info->default_led;
710     ledi.merge = merge;
711     ledi.led.name = def->name;
712
713     ok = true;
714     for (var = def->body; var != NULL; var = (VarDef *) var->common.next) {
715         const char *elem, *field;
716         ExprDef *arrayNdx;
717         if (!ExprResolveLhs(info->keymap->ctx, var->name, &elem, &field,
718                             &arrayNdx)) {
719             ok = false;
720             continue;
721         }
722
723         if (elem) {
724             log_err(info->keymap->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->keymap->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);
772             break;
773         default:
774             log_err(info->keymap->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->keymap->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 }