keymap: don't use darray in xkb_mod_set
[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     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->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     CopyModSet(&into->mods, &from->mods);
382
383     if (into->name == NULL) {
384         into->name = from->name;
385         from->name = NULL;
386     }
387
388     if (darray_empty(into->interps)) {
389         into->interps = from->interps;
390         darray_init(from->interps);
391     }
392     else {
393         darray_foreach(si, from->interps) {
394             si->merge = (merge == MERGE_DEFAULT ? si->merge : merge);
395             if (!AddInterp(into, si, false))
396                 into->errorCount++;
397         }
398     }
399
400     if (darray_empty(into->leds)) {
401         into->leds = from->leds;
402         darray_init(from->leds);
403     }
404     else {
405         darray_foreach(ledi, from->leds) {
406             ledi->merge = (merge == MERGE_DEFAULT ? ledi->merge : merge);
407             if (!AddLedMap(into, ledi, false))
408                 into->errorCount++;
409         }
410     }
411 }
412
413 static void
414 HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge);
415
416 static bool
417 HandleIncludeCompatMap(CompatInfo *info, IncludeStmt *include)
418 {
419     CompatInfo included;
420
421     InitCompatInfo(&included, info->ctx, info->actions, &info->mods);
422     included.name = include->stmt;
423     include->stmt = NULL;
424
425     for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
426         CompatInfo next_incl;
427         XkbFile *file;
428
429         file = ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_COMPAT);
430         if (!file) {
431             info->errorCount += 10;
432             ClearCompatInfo(&included);
433             return false;
434         }
435
436         InitCompatInfo(&next_incl, info->ctx, info->actions, &included.mods);
437         next_incl.default_interp = info->default_interp;
438         next_incl.default_interp.merge = stmt->merge;
439         next_incl.default_led = info->default_led;
440         next_incl.default_led.merge = stmt->merge;
441
442         HandleCompatMapFile(&next_incl, file, MERGE_OVERRIDE);
443
444         MergeIncludedCompatMaps(&included, &next_incl, stmt->merge);
445
446         ClearCompatInfo(&next_incl);
447         FreeXkbFile(file);
448     }
449
450     MergeIncludedCompatMaps(info, &included, include->merge);
451     ClearCompatInfo(&included);
452
453     return (info->errorCount == 0);
454 }
455
456 static bool
457 SetInterpField(CompatInfo *info, SymInterpInfo *si, const char *field,
458                ExprDef *arrayNdx, ExprDef *value)
459 {
460     xkb_mod_index_t ndx;
461
462     if (istreq(field, "action")) {
463         if (arrayNdx)
464             return ReportSINotArray(info, si, field);
465
466         if (!HandleActionDef(info->ctx, info->actions, &info->mods,
467                              value, &si->interp.action))
468             return false;
469
470         si->defined |= SI_FIELD_ACTION;
471     }
472     else if (istreq(field, "virtualmodifier") ||
473              istreq(field, "virtualmod")) {
474         if (arrayNdx)
475             return ReportSINotArray(info, si, field);
476
477         if (!ExprResolveMod(info->ctx, value, MOD_VIRT, &info->mods, &ndx))
478             return ReportSIBadType(info, si, field, "virtual modifier");
479
480         si->interp.virtual_mod = ndx;
481         si->defined |= SI_FIELD_VIRTUAL_MOD;
482     }
483     else if (istreq(field, "repeat")) {
484         bool set;
485
486         if (arrayNdx)
487             return ReportSINotArray(info, si, field);
488
489         if (!ExprResolveBoolean(info->ctx, value, &set))
490             return ReportSIBadType(info, si, field, "boolean");
491
492         si->interp.repeat = set;
493
494         si->defined |= SI_FIELD_AUTO_REPEAT;
495     }
496     else if (istreq(field, "locking")) {
497         log_dbg(info->ctx,
498                 "The \"locking\" field in symbol interpretation is unsupported; "
499                 "Ignored\n");
500     }
501     else if (istreq(field, "usemodmap") ||
502              istreq(field, "usemodmapmods")) {
503         unsigned int val;
504
505         if (arrayNdx)
506             return ReportSINotArray(info, si, field);
507
508         if (!ExprResolveEnum(info->ctx, value, &val, useModMapValueNames))
509             return ReportSIBadType(info, si, field, "level specification");
510
511         si->interp.level_one_only = !!val;
512         si->defined |= SI_FIELD_LEVEL_ONE_ONLY;
513     }
514     else {
515         return ReportBadField(info->ctx, "symbol interpretation", field,
516                               siText(si, info));
517     }
518
519     return true;
520 }
521
522 static bool
523 SetLedMapField(CompatInfo *info, LedInfo *ledi, const char *field,
524                ExprDef *arrayNdx, ExprDef *value)
525 {
526     bool ok = true;
527
528     if (istreq(field, "modifiers") || istreq(field, "mods")) {
529         if (arrayNdx)
530             return ReportLedNotArray(info, ledi, field);
531
532         if (!ExprResolveModMask(info->ctx, value, MOD_BOTH,
533                                 &info->mods, &ledi->led.mods.mods))
534             return ReportLedBadType(info, ledi, field, "modifier mask");
535
536         ledi->defined |= LED_FIELD_MODS;
537     }
538     else if (istreq(field, "groups")) {
539         unsigned int mask;
540
541         if (arrayNdx)
542             return ReportLedNotArray(info, ledi, field);
543
544         if (!ExprResolveMask(info->ctx, value, &mask, groupMaskNames))
545             return ReportLedBadType(info, ledi, field, "group mask");
546
547         ledi->led.groups = mask;
548         ledi->defined |= LED_FIELD_GROUPS;
549     }
550     else if (istreq(field, "controls") || istreq(field, "ctrls")) {
551         unsigned int mask;
552
553         if (arrayNdx)
554             return ReportLedNotArray(info, ledi, field);
555
556         if (!ExprResolveMask(info->ctx, value, &mask, ctrlMaskNames))
557             return ReportLedBadType(info, ledi, field, "controls mask");
558
559         ledi->led.ctrls = mask;
560         ledi->defined |= LED_FIELD_CTRLS;
561     }
562     else if (istreq(field, "allowexplicit")) {
563         log_dbg(info->ctx,
564                 "The \"allowExplicit\" field in indicator statements is unsupported; "
565                 "Ignored\n");
566     }
567     else if (istreq(field, "whichmodstate") ||
568              istreq(field, "whichmodifierstate")) {
569         unsigned int mask;
570
571         if (arrayNdx)
572             return ReportLedNotArray(info, ledi, field);
573
574         if (!ExprResolveMask(info->ctx, value, &mask,
575                              modComponentMaskNames))
576             return ReportLedBadType(info, ledi, field,
577                                     "mask of modifier state components");
578
579         ledi->led.which_mods = mask;
580     }
581     else if (istreq(field, "whichgroupstate")) {
582         unsigned mask;
583
584         if (arrayNdx)
585             return ReportLedNotArray(info, ledi, field);
586
587         if (!ExprResolveMask(info->ctx, value, &mask,
588                              groupComponentMaskNames))
589             return ReportLedBadType(info, ledi, field,
590                                     "mask of group state components");
591
592         ledi->led.which_groups = mask;
593     }
594     else if (istreq(field, "driveskbd") ||
595              istreq(field, "driveskeyboard") ||
596              istreq(field, "leddriveskbd") ||
597              istreq(field, "leddriveskeyboard") ||
598              istreq(field, "indicatordriveskbd") ||
599              istreq(field, "indicatordriveskeyboard")) {
600         log_dbg(info->ctx,
601                 "The \"%s\" field in indicator statements is unsupported; "
602                 "Ignored\n", field);
603     }
604     else if (istreq(field, "index")) {
605         /* Users should see this, it might cause unexpected behavior. */
606         log_err(info->ctx,
607                 "The \"index\" field in indicator statements is unsupported; "
608                 "Ignored\n");
609     }
610     else {
611         log_err(info->ctx,
612                 "Unknown field %s in map for %s indicator; "
613                 "Definition ignored\n",
614                 field, xkb_atom_text(info->ctx, ledi->led.name));
615         ok = false;
616     }
617
618     return ok;
619 }
620
621 static bool
622 HandleGlobalVar(CompatInfo *info, VarDef *stmt)
623 {
624     const char *elem, *field;
625     ExprDef *ndx;
626     bool ret;
627
628     if (!ExprResolveLhs(info->ctx, stmt->name, &elem, &field, &ndx))
629         ret = false;
630     else if (elem && istreq(elem, "interpret"))
631         ret = SetInterpField(info, &info->default_interp, field, ndx,
632                              stmt->value);
633     else if (elem && istreq(elem, "indicator"))
634         ret = SetLedMapField(info, &info->default_led, field, ndx,
635                              stmt->value);
636     else
637         ret = SetActionField(info->ctx, info->actions, &info->mods,
638                              elem, field, ndx, stmt->value);
639     return ret;
640 }
641
642 static bool
643 HandleInterpBody(CompatInfo *info, VarDef *def, SymInterpInfo *si)
644 {
645     bool ok = true;
646     const char *elem, *field;
647     ExprDef *arrayNdx;
648
649     for (; def; def = (VarDef *) def->common.next) {
650         if (def->name && def->name->expr.op == EXPR_FIELD_REF) {
651             log_err(info->ctx,
652                     "Cannot set a global default value from within an interpret statement; "
653                     "Move statements to the global file scope\n");
654             ok = false;
655             continue;
656         }
657
658         ok = ExprResolveLhs(info->ctx, def->name, &elem, &field, &arrayNdx);
659         if (!ok)
660             continue;
661
662         ok = SetInterpField(info, si, field, arrayNdx, def->value);
663     }
664
665     return ok;
666 }
667
668 static bool
669 HandleInterpDef(CompatInfo *info, InterpDef *def, enum merge_mode merge)
670 {
671     enum xkb_match_operation pred;
672     xkb_mod_mask_t mods;
673     SymInterpInfo si;
674
675     if (!ResolveStateAndPredicate(def->match, &pred, &mods, info)) {
676         log_err(info->ctx,
677                 "Couldn't determine matching modifiers; "
678                 "Symbol interpretation ignored\n");
679         return false;
680     }
681
682     si = info->default_interp;
683     si.merge = merge = (def->merge == MERGE_DEFAULT ? merge : def->merge);
684     si.interp.sym = def->sym;
685     si.interp.match = pred;
686     si.interp.mods = mods;
687
688     if (!HandleInterpBody(info, def->def, &si)) {
689         info->errorCount++;
690         return false;
691     }
692
693     if (!AddInterp(info, &si, true)) {
694         info->errorCount++;
695         return false;
696     }
697
698     return true;
699 }
700
701 static bool
702 HandleLedMapDef(CompatInfo *info, LedMapDef *def, enum merge_mode merge)
703 {
704     LedInfo ledi;
705     VarDef *var;
706     bool ok;
707
708     if (def->merge != MERGE_DEFAULT)
709         merge = def->merge;
710
711     ledi = info->default_led;
712     ledi.merge = merge;
713     ledi.led.name = def->name;
714
715     ok = true;
716     for (var = def->body; var != NULL; var = (VarDef *) var->common.next) {
717         const char *elem, *field;
718         ExprDef *arrayNdx;
719         if (!ExprResolveLhs(info->ctx, var->name, &elem, &field, &arrayNdx)) {
720             ok = false;
721             continue;
722         }
723
724         if (elem) {
725             log_err(info->ctx,
726                     "Cannot set defaults for \"%s\" element in indicator map; "
727                     "Assignment to %s.%s ignored\n", elem, elem, field);
728             ok = false;
729         }
730         else {
731             ok = SetLedMapField(info, &ledi, field, arrayNdx, var->value) && ok;
732         }
733     }
734
735     if (ok)
736         return AddLedMap(info, &ledi, true);
737
738     return false;
739 }
740
741 static void
742 HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge)
743 {
744     bool ok;
745
746     merge = (merge == MERGE_DEFAULT ? MERGE_AUGMENT : merge);
747
748     free(info->name);
749     info->name = strdup_safe(file->name);
750
751     for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
752         switch (stmt->type) {
753         case STMT_INCLUDE:
754             ok = HandleIncludeCompatMap(info, (IncludeStmt *) stmt);
755             break;
756         case STMT_INTERP:
757             ok = HandleInterpDef(info, (InterpDef *) stmt, merge);
758             break;
759         case STMT_GROUP_COMPAT:
760             log_dbg(info->ctx,
761                     "The \"group\" statement in compat is unsupported; "
762                     "Ignored\n");
763             ok = true;
764             break;
765         case STMT_LED_MAP:
766             ok = HandleLedMapDef(info, (LedMapDef *) stmt, merge);
767             break;
768         case STMT_VAR:
769             ok = HandleGlobalVar(info, (VarDef *) stmt);
770             break;
771         case STMT_VMOD:
772             ok = HandleVModDef(info->ctx, &info->mods, (VModDef *) stmt, merge);
773             break;
774         default:
775             log_err(info->ctx,
776                     "Compat files may not include other types; "
777                     "Ignoring %s\n", stmt_type_to_string(stmt->type));
778             ok = false;
779             break;
780         }
781
782         if (!ok)
783             info->errorCount++;
784
785         if (info->errorCount > 10) {
786             log_err(info->ctx,
787                     "Abandoning compatibility map \"%s\"\n", file->topName);
788             break;
789         }
790     }
791 }
792
793 /* Temporary struct for CopyInterps. */
794 struct collect {
795     darray(struct xkb_sym_interpret) sym_interprets;
796 };
797
798 static void
799 CopyInterps(CompatInfo *info, bool needSymbol, enum xkb_match_operation pred,
800             struct collect *collect)
801 {
802     SymInterpInfo *si;
803
804     darray_foreach(si, info->interps)
805         if (si->interp.match == pred &&
806             (si->interp.sym != XKB_KEY_NoSymbol) == needSymbol)
807             darray_append(collect->sym_interprets, si->interp);
808 }
809
810 static void
811 CopyLedMapDefsToKeymap(struct xkb_keymap *keymap, CompatInfo *info)
812 {
813     LedInfo *ledi;
814     xkb_led_index_t i;
815     struct xkb_led *led;
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     CopyModSet(&keymap->mods, &info->mods);
868
869     if (!darray_empty(info->interps)) {
870         struct collect collect;
871         darray_init(collect.sym_interprets);
872
873         /* Most specific to least specific. */
874         CopyInterps(info, true, MATCH_EXACTLY, &collect);
875         CopyInterps(info, true, MATCH_ALL, &collect);
876         CopyInterps(info, true, MATCH_NONE, &collect);
877         CopyInterps(info, true, MATCH_ANY, &collect);
878         CopyInterps(info, true, MATCH_ANY_OR_NONE, &collect);
879         CopyInterps(info, false, MATCH_EXACTLY, &collect);
880         CopyInterps(info, false, MATCH_ALL, &collect);
881         CopyInterps(info, false, MATCH_NONE, &collect);
882         CopyInterps(info, false, MATCH_ANY, &collect);
883         CopyInterps(info, false, MATCH_ANY_OR_NONE, &collect);
884
885         keymap->num_sym_interprets = darray_size(collect.sym_interprets);
886         keymap->sym_interprets = darray_mem(collect.sym_interprets, 0);
887     }
888
889     CopyLedMapDefsToKeymap(keymap, info);
890
891     return true;
892 }
893
894 bool
895 CompileCompatMap(XkbFile *file, struct xkb_keymap *keymap,
896                  enum merge_mode merge)
897 {
898     CompatInfo info;
899     ActionsInfo *actions;
900
901     actions = NewActionsInfo();
902     if (!actions)
903         return false;
904
905     InitCompatInfo(&info, keymap->ctx, actions, &keymap->mods);
906     info.default_interp.merge = merge;
907     info.default_led.merge = merge;
908
909     HandleCompatMapFile(&info, file, merge);
910     if (info.errorCount != 0)
911         goto err_info;
912
913     if (!CopyCompatToKeymap(keymap, &info))
914         goto err_info;
915
916     ClearCompatInfo(&info);
917     FreeActionsInfo(actions);
918     return true;
919
920 err_info:
921     ClearCompatInfo(&info);
922     FreeActionsInfo(actions);
923     return false;
924 }