kbproto unentanglement: XkbNumIndicators
[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 #include "xkbcomp-priv.h"
28 #include "text.h"
29 #include "expr.h"
30 #include "action.h"
31 #include "vmod.h"
32 #include "include.h"
33
34 /*
35  * The xkb_compat section
36  * =====================
37  * This section is the third to be processesed, after xkb_keycodes and
38  * xkb_types.
39  *
40  * Interpret statements
41  * --------------------
42  * Statements of the form:
43  *      interpret Num_Lock+Any { ... }
44  *
45  * The body of the statment may include statements of the following
46  * forms:
47  *
48  * - action statement:
49  *      action = LockMods(modifiers=NumLock);
50  *
51  * - virtual modifier statement:
52  *      virtualModifier = NumLock;
53  *
54  * - repeat statement:
55  *      repeat = True;
56  *
57  * - useModMapMods statement:
58  *      useModMapMods = level1;
59  *
60  * Indicator map statements
61  * ------------------------
62  * Statements of the form:
63  *      indicator "Shift Lock" { ... }
64  *
65  *   This statement specifies the behavior and binding of the indicator
66  *   with the given name ("Shift Lock" above). The name should have been
67  *   declared previously in the xkb_keycodes section (see Indicator name
68  *   statement), and given an index there. If it wasn't, it is created
69  *   with the next free index.
70  *   The body of the statement describes the conditions of the keyboard
71  *   state which will cause the indicator to be lit. It may include the
72  *   following statements:
73  *
74  * - modifiers statment:
75  *      modifiers = ScrollLock;
76  *
77  *   If the given modifiers are in the required state (see below), the
78  *   led is lit.
79  *
80  * - whichModifierState statment:
81  *      whichModState = Latched + Locked;
82  *
83  *   Can be any combination of:
84  *      base, latched, locked, effective
85  *      any (i.e. all of the above)
86  *      none (i.e. none of the above)
87  *      compat (this is legal, but unused)
88  *   This will cause the respective portion of the modifer state (see
89  *   struct xkb_state) to be matched against the modifiers given in the
90  *   "modifiers" statement.
91  *
92  *   Here's a simple example:
93  *      indicator "Num Lock" {
94  *          modifiers = NumLock;
95  *          whichModState = Locked;
96  *      };
97  *   Whenever the NumLock modifier is locked, the Num Lock indicator
98  *   will light up.
99  *
100  * - groups statment:
101  *      groups = All - group1;
102  *
103  *   If the given groups are in the required state (see below), the led
104  *   is lit.
105  *
106  * - whichGroupState statment:
107  *      whichGroupState = Effective;
108  *
109  *   Can be any combination of:
110  *      base, latched, locked, effective
111  *      any (i.e. all of the above)
112  *      none (i.e. none of the above)
113  *   This will cause the respective portion of the group state (see
114  *   struct xkb_state) to be matched against the groups given in the
115  *   "groups" statement.
116  *
117  *   Note: the above conditions are disjunctive, i.e. if any of them are
118  *   satisfied the led is lit.
119  *
120  * Virtual modifier statements
121  * ---------------------------
122  * Statements of the form:
123  *     virtual_modifiers LControl;
124  *
125  * Can appear in the xkb_types, xkb_compat, xkb_symbols sections.
126  * TODO
127  *
128  * Effect on keymap
129  * ----------------
130  * After all of the xkb_compat sections have been compiled, the following
131  * members of struct xkb_keymap are finalized:
132  *      darray(struct xkb_sym_interpret) sym_interpret;
133  *      struct xkb_indicator_map indicators[XKB_NUM_INDICATORS];
134  *      char *compat_section_name;
135  * TODO: virtual modifiers.
136  */
137
138 enum si_field {
139     SI_FIELD_VIRTUAL_MOD    = (1 << 0),
140     SI_FIELD_ACTION         = (1 << 1),
141     SI_FIELD_AUTO_REPEAT    = (1 << 2),
142     SI_FIELD_LEVEL_ONE_ONLY = (1 << 3),
143 };
144
145 typedef struct _SymInterpInfo {
146     enum si_field defined;
147     unsigned file_id;
148     enum merge_mode merge;
149
150     struct xkb_sym_interpret interp;
151 } SymInterpInfo;
152
153 enum led_field {
154     LED_FIELD_MODS       = (1 << 0),
155     LED_FIELD_GROUPS     = (1 << 1),
156     LED_FIELD_CTRLS      = (1 << 2),
157 };
158
159 typedef struct _LEDInfo {
160     enum led_field defined;
161     unsigned file_id;
162     enum merge_mode merge;
163
164     struct xkb_indicator_map im;
165 } LEDInfo;
166
167 typedef struct _CompatInfo {
168     char *name;
169     unsigned file_id;
170     int errorCount;
171     SymInterpInfo dflt;
172     darray(SymInterpInfo) interps;
173     LEDInfo ledDflt;
174     darray(LEDInfo) leds;
175     VModInfo vmods;
176     ActionsInfo *actions;
177     struct xkb_keymap *keymap;
178 } CompatInfo;
179
180 static const char *
181 siText(SymInterpInfo *si, CompatInfo *info)
182 {
183     static char buf[128];
184
185     if (si == &info->dflt)
186         return "default";
187
188     snprintf(buf, sizeof(buf), "%s+%s(%s)",
189              KeysymText(si->interp.sym),
190              SIMatchText(si->interp.match),
191              ModMaskText(si->interp.mods));
192     return buf;
193 }
194
195 static inline bool
196 ReportSINotArray(CompatInfo *info, SymInterpInfo *si, const char *field)
197 {
198     return ReportNotArray(info->keymap, "symbol interpretation", field,
199                           siText(si, info));
200 }
201
202 static inline bool
203 ReportSIBadType(CompatInfo *info, SymInterpInfo *si, const char *field,
204                 const char *wanted)
205 {
206     return ReportBadType(info->keymap->ctx, "symbol interpretation", field,
207                          siText(si, info), wanted);
208 }
209
210 static inline bool
211 ReportIndicatorBadType(CompatInfo *info, LEDInfo *led,
212                        const char *field, const char *wanted)
213 {
214     return ReportBadType(info->keymap->ctx, "indicator map", field,
215                          xkb_atom_text(info->keymap->ctx, led->im.name),
216                          wanted);
217 }
218
219 static inline bool
220 ReportIndicatorNotArray(CompatInfo *info, LEDInfo *led,
221                         const char *field)
222 {
223     return ReportNotArray(info->keymap, "indicator map", field,
224                           xkb_atom_text(info->keymap->ctx, led->im.name));
225 }
226
227 static void
228 InitCompatInfo(CompatInfo *info, struct xkb_keymap *keymap, unsigned file_id,
229                ActionsInfo *actions)
230 {
231     info->keymap = keymap;
232     info->name = NULL;
233     info->file_id = file_id;
234     info->errorCount = 0;
235     darray_init(info->interps);
236     info->actions = actions;
237     info->dflt.file_id = file_id;
238     info->dflt.defined = 0;
239     info->dflt.merge = MERGE_OVERRIDE;
240     info->dflt.interp.flags = 0;
241     info->dflt.interp.virtual_mod = XKB_MOD_INVALID;
242     memset(&info->dflt.interp.act, 0, sizeof(info->dflt.interp.act));
243     info->dflt.interp.act.type = XkbSA_NoAction;
244     memset(&info->ledDflt, 0, sizeof(info->ledDflt));
245     info->ledDflt.file_id = file_id;
246     info->ledDflt.merge = MERGE_OVERRIDE;
247     darray_init(info->leds);
248     InitVModInfo(&info->vmods, keymap);
249 }
250
251 static void
252 ClearCompatInfo(CompatInfo *info)
253 {
254     free(info->name);
255     info->name = NULL;
256     info->dflt.defined = 0;
257     info->dflt.merge = MERGE_AUGMENT;
258     info->dflt.interp.flags = 0;
259     info->dflt.interp.virtual_mod = XKB_MOD_INVALID;
260     memset(&info->dflt.interp.act, 0, sizeof(info->dflt.interp.act));
261     info->dflt.interp.act.type = XkbSA_NoAction;
262     memset(&info->ledDflt, 0, sizeof(info->ledDflt));
263     darray_free(info->interps);
264     darray_free(info->leds);
265     info->actions = NULL;
266     info->keymap = NULL;
267     ClearVModInfo(&info->vmods);
268 }
269
270 static SymInterpInfo *
271 FindMatchingInterp(CompatInfo *info, SymInterpInfo *new)
272 {
273     SymInterpInfo *old;
274
275     darray_foreach(old, info->interps)
276         if (old->interp.sym == new->interp.sym &&
277             old->interp.mods == new->interp.mods &&
278             old->interp.match == new->interp.match)
279             return old;
280
281     return NULL;
282 }
283
284 static bool
285 UseNewInterpField(enum si_field field, SymInterpInfo *old, SymInterpInfo *new,
286                   bool report, enum si_field *collide)
287 {
288     if (!(old->defined & field))
289         return true;
290
291     if (new->defined & field) {
292         if (report)
293             *collide |= field;
294
295         if (new->merge != MERGE_AUGMENT)
296             return true;
297     }
298
299     return false;
300 }
301
302 static bool
303 AddInterp(CompatInfo *info, SymInterpInfo *new)
304 {
305     enum si_field collide = 0;
306     SymInterpInfo *old;
307
308     old = FindMatchingInterp(info, new);
309     if (old) {
310         int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
311         bool report = ((old->file_id == new->file_id && verbosity > 0) ||
312                        verbosity > 9);
313
314         if (new->merge == MERGE_REPLACE) {
315             if (report)
316                 log_warn(info->keymap->ctx,
317                          "Multiple definitions for \"%s\"; "
318                          "Earlier interpretation ignored\n",
319                          siText(new, info));
320             *old = *new;
321             return true;
322         }
323
324         if (UseNewInterpField(SI_FIELD_VIRTUAL_MOD, old, new, report,
325                               &collide)) {
326             old->interp.virtual_mod = new->interp.virtual_mod;
327             old->defined |= SI_FIELD_VIRTUAL_MOD;
328         }
329         if (UseNewInterpField(SI_FIELD_ACTION, old, new, report,
330                               &collide)) {
331             old->interp.act = new->interp.act;
332             old->defined |= SI_FIELD_ACTION;
333         }
334         if (UseNewInterpField(SI_FIELD_AUTO_REPEAT, old, new, report,
335                               &collide)) {
336             old->interp.flags &= ~XkbSI_AutoRepeat;
337             old->interp.flags |= (new->interp.flags & XkbSI_AutoRepeat);
338             old->defined |= SI_FIELD_AUTO_REPEAT;
339         }
340         if (UseNewInterpField(SI_FIELD_LEVEL_ONE_ONLY, old, new, report,
341                               &collide)) {
342             old->interp.match &= ~XkbSI_LevelOneOnly;
343             old->interp.match |= (new->interp.match & XkbSI_LevelOneOnly);
344             old->defined |= SI_FIELD_LEVEL_ONE_ONLY;
345         }
346
347         if (collide) {
348             log_warn(info->keymap->ctx,
349                      "Multiple interpretations of \"%s\"; "
350                      "Using %s definition for duplicate fields\n",
351                      siText(new, info),
352                      (new->merge != MERGE_AUGMENT ? "last" : "first"));
353         }
354
355         return true;
356     }
357
358     darray_append(info->interps, *new);
359     return true;
360 }
361
362
363 /***====================================================================***/
364
365 static bool
366 ResolveStateAndPredicate(ExprDef *expr, unsigned *pred_rtrn,
367                          xkb_mod_mask_t *mods_rtrn, CompatInfo *info)
368 {
369     if (expr == NULL) {
370         *pred_rtrn = XkbSI_AnyOfOrNone;
371         *mods_rtrn = ~0;
372         return true;
373     }
374
375     *pred_rtrn = XkbSI_Exactly;
376     if (expr->op == EXPR_ACTION_DECL) {
377         const char *pred_txt = xkb_atom_text(info->keymap->ctx,
378                                              expr->value.action.name);
379         if (!LookupString(symInterpretMatchMaskNames, pred_txt, pred_rtrn)) {
380             log_err(info->keymap->ctx,
381                     "Illegal modifier predicate \"%s\"; Ignored\n", pred_txt);
382             return false;
383         }
384         expr = expr->value.action.args;
385     }
386     else if (expr->op == EXPR_IDENT) {
387         const char *pred_txt = xkb_atom_text(info->keymap->ctx,
388                                              expr->value.str);
389         if (pred_txt && istreq(pred_txt, "any")) {
390             *pred_rtrn = XkbSI_AnyOf;
391             *mods_rtrn = 0xff;
392             return true;
393         }
394     }
395
396     return ExprResolveModMask(info->keymap->ctx, expr, mods_rtrn);
397 }
398
399 /***====================================================================***/
400
401 static bool
402 UseNewLEDField(enum led_field field, LEDInfo *old, LEDInfo *new,
403                bool report, enum led_field *collide)
404 {
405     if (!(old->defined & field))
406         return true;
407
408     if (new->defined & field) {
409         if (report)
410             *collide |= field;
411
412         if (new->merge != MERGE_AUGMENT)
413             return true;
414     }
415
416     return false;
417 }
418
419 static bool
420 AddIndicatorMap(CompatInfo *info, LEDInfo *new)
421 {
422     LEDInfo *old;
423     enum led_field collide;
424     struct xkb_context *ctx = info->keymap->ctx;
425     int verbosity = xkb_get_log_verbosity(ctx);
426
427     darray_foreach(old, info->leds) {
428         bool report;
429
430         if (old->im.name != new->im.name)
431             continue;
432
433         if (old->im.mods.mods == new->im.mods.mods &&
434             old->im.groups == new->im.groups &&
435             old->im.ctrls == new->im.ctrls &&
436             old->im.which_mods == new->im.which_mods &&
437             old->im.which_groups == new->im.which_groups) {
438             old->defined |= new->defined;
439             return true;
440         }
441
442         report = ((old->file_id == new->file_id && verbosity > 0) ||
443                   verbosity > 9);
444
445         if (new->merge == MERGE_REPLACE) {
446             if (report)
447                 log_warn(info->keymap->ctx,
448                          "Map for indicator %s redefined; "
449                          "Earlier definition ignored\n",
450                          xkb_atom_text(ctx, old->im.name));
451             *old = *new;
452             return true;
453         }
454
455         collide = 0;
456         if (UseNewLEDField(LED_FIELD_MODS, old, new, report, &collide)) {
457             old->im.which_mods = new->im.which_mods;
458             old->im.mods = new->im.mods;
459             old->defined |= LED_FIELD_MODS;
460         }
461         if (UseNewLEDField(LED_FIELD_GROUPS, old, new, report, &collide)) {
462             old->im.which_groups = new->im.which_groups;
463             old->im.groups = new->im.groups;
464             old->defined |= LED_FIELD_GROUPS;
465         }
466         if (UseNewLEDField(LED_FIELD_CTRLS, old, new, report, &collide)) {
467             old->im.ctrls = new->im.ctrls;
468             old->defined |= LED_FIELD_CTRLS;
469         }
470
471         if (collide) {
472             log_warn(info->keymap->ctx,
473                      "Map for indicator %s redefined; "
474                      "Using %s definition for duplicate fields\n",
475                      xkb_atom_text(ctx, old->im.name),
476                      (new->merge == MERGE_AUGMENT ? "first" : "last"));
477         }
478
479         return true;
480     }
481
482     darray_append(info->leds, *new);
483     return true;
484 }
485
486 static void
487 MergeIncludedCompatMaps(CompatInfo *into, CompatInfo *from,
488                         enum merge_mode merge)
489 {
490     SymInterpInfo *si;
491     LEDInfo *led;
492
493     if (from->errorCount > 0) {
494         into->errorCount += from->errorCount;
495         return;
496     }
497
498     if (into->name == NULL) {
499         into->name = from->name;
500         from->name = NULL;
501     }
502
503     darray_foreach(si, from->interps) {
504         si->merge = (merge == MERGE_DEFAULT ? si->merge : merge);
505         if (!AddInterp(into, si))
506             into->errorCount++;
507     }
508
509     darray_foreach(led, from->leds) {
510         led->merge = (merge == MERGE_DEFAULT ? led->merge : merge);
511         if (!AddIndicatorMap(into, led))
512             into->errorCount++;
513     }
514 }
515
516 static void
517 HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge);
518
519 static bool
520 HandleIncludeCompatMap(CompatInfo *info, IncludeStmt *stmt)
521 {
522     enum merge_mode merge = MERGE_DEFAULT;
523     XkbFile *rtrn;
524     CompatInfo included, next_incl;
525
526     InitCompatInfo(&included, info->keymap, info->file_id, info->actions);
527     if (stmt->stmt) {
528         free(included.name);
529         included.name = stmt->stmt;
530         stmt->stmt = NULL;
531     }
532
533     for (; stmt; stmt = stmt->next_incl) {
534         if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_COMPAT,
535                                 &rtrn, &merge)) {
536             info->errorCount += 10;
537             ClearCompatInfo(&included);
538             return false;
539         }
540
541         InitCompatInfo(&next_incl, info->keymap, rtrn->id, info->actions);
542         next_incl.file_id = rtrn->id;
543         next_incl.dflt = info->dflt;
544         next_incl.dflt.file_id = rtrn->id;
545         next_incl.dflt.merge = merge;
546         next_incl.ledDflt.file_id = rtrn->id;
547         next_incl.ledDflt.merge = merge;
548
549         HandleCompatMapFile(&next_incl, rtrn, MERGE_OVERRIDE);
550
551         MergeIncludedCompatMaps(&included, &next_incl, merge);
552
553         ClearCompatInfo(&next_incl);
554         FreeXkbFile(rtrn);
555     }
556
557     MergeIncludedCompatMaps(info, &included, merge);
558     ClearCompatInfo(&included);
559
560     return (info->errorCount == 0);
561 }
562
563 static bool
564 SetInterpField(CompatInfo *info, SymInterpInfo *si, const char *field,
565                ExprDef *arrayNdx, ExprDef *value)
566 {
567     struct xkb_keymap *keymap = info->keymap;
568     xkb_mod_index_t ndx;
569
570     if (istreq(field, "action")) {
571         if (arrayNdx)
572             return ReportSINotArray(info, si, field);
573
574         if (!HandleActionDef(value, keymap, &si->interp.act, info->actions))
575             return false;
576
577         si->defined |= SI_FIELD_ACTION;
578     }
579     else if (istreq(field, "virtualmodifier") ||
580              istreq(field, "virtualmod")) {
581         if (arrayNdx)
582             return ReportSINotArray(info, si, field);
583
584         if (!ResolveVirtualModifier(value, keymap, &ndx, &info->vmods))
585             return ReportSIBadType(info, si, field, "virtual modifier");
586
587         si->interp.virtual_mod = ndx;
588         si->defined |= SI_FIELD_VIRTUAL_MOD;
589     }
590     else if (istreq(field, "repeat")) {
591         bool set;
592
593         if (arrayNdx)
594             return ReportSINotArray(info, si, field);
595
596         if (!ExprResolveBoolean(keymap->ctx, value, &set))
597             return ReportSIBadType(info, si, field, "boolean");
598
599         if (set)
600             si->interp.flags |= XkbSI_AutoRepeat;
601         else
602             si->interp.flags &= ~XkbSI_AutoRepeat;
603
604         si->defined |= SI_FIELD_AUTO_REPEAT;
605     }
606     else if (istreq(field, "locking")) {
607         log_dbg(info->keymap->ctx,
608                 "The \"locking\" field in symbol interpretation is unsupported; "
609                 "Ignored\n");
610     }
611     else if (istreq(field, "usemodmap") ||
612              istreq(field, "usemodmapmods")) {
613         unsigned int val;
614
615         if (arrayNdx)
616             return ReportSINotArray(info, si, field);
617
618         if (!ExprResolveEnum(keymap->ctx, value, &val, useModMapValueNames))
619             return ReportSIBadType(info, si, field, "level specification");
620
621         if (val)
622             si->interp.match |= XkbSI_LevelOneOnly;
623         else
624             si->interp.match &= ~XkbSI_LevelOneOnly;
625
626         si->defined |= SI_FIELD_LEVEL_ONE_ONLY;
627     }
628     else {
629         return ReportBadField(keymap, "symbol interpretation", field,
630                               siText(si, info));
631     }
632
633     return true;
634 }
635
636 static bool
637 SetIndicatorMapField(CompatInfo *info, LEDInfo *led,
638                      const char *field, ExprDef *arrayNdx, ExprDef *value)
639 {
640     bool ok = true;
641     struct xkb_keymap *keymap = info->keymap;
642
643     if (istreq(field, "modifiers") || istreq(field, "mods")) {
644         if (arrayNdx)
645             return ReportIndicatorNotArray(info, led, field);
646
647         if (!ExprResolveVModMask(keymap, value, &led->im.mods.mods))
648             return ReportIndicatorBadType(info, led, field, "modifier mask");
649
650         led->defined |= LED_FIELD_MODS;
651     }
652     else if (istreq(field, "groups")) {
653         unsigned int mask;
654
655         if (arrayNdx)
656             return ReportIndicatorNotArray(info, led, field);
657
658         if (!ExprResolveMask(keymap->ctx, value, &mask, groupMaskNames))
659             return ReportIndicatorBadType(info, led, field, "group mask");
660
661         led->im.groups = mask;
662         led->defined |= LED_FIELD_GROUPS;
663     }
664     else if (istreq(field, "controls") || istreq(field, "ctrls")) {
665         unsigned int mask;
666
667         if (arrayNdx)
668             return ReportIndicatorNotArray(info, led, field);
669
670         if (!ExprResolveMask(keymap->ctx, value, &mask, ctrlMaskNames))
671             return ReportIndicatorBadType(info, led, field,
672                                           "controls mask");
673
674         led->im.ctrls = mask;
675         led->defined |= LED_FIELD_CTRLS;
676     }
677     else if (istreq(field, "allowexplicit")) {
678         log_dbg(info->keymap->ctx,
679                 "The \"allowExplicit\" field in indicator statements is unsupported; "
680                 "Ignored\n");
681     }
682     else if (istreq(field, "whichmodstate") ||
683              istreq(field, "whichmodifierstate")) {
684         unsigned int mask;
685
686         if (arrayNdx)
687             return ReportIndicatorNotArray(info, led, field);
688
689         if (!ExprResolveMask(keymap->ctx, value, &mask,
690                              modComponentMaskNames))
691             return ReportIndicatorBadType(info, led, field,
692                                           "mask of modifier state components");
693
694         led->im.which_mods = mask;
695     }
696     else if (istreq(field, "whichgroupstate")) {
697         unsigned mask;
698
699         if (arrayNdx)
700             return ReportIndicatorNotArray(info, led, field);
701
702         if (!ExprResolveMask(keymap->ctx, value, &mask,
703                              groupComponentMaskNames))
704             return ReportIndicatorBadType(info, led, field,
705                                           "mask of group state components");
706
707         led->im.which_groups = mask;
708     }
709     else if (istreq(field, "driveskbd") ||
710              istreq(field, "driveskeyboard") ||
711              istreq(field, "leddriveskbd") ||
712              istreq(field, "leddriveskeyboard") ||
713              istreq(field, "indicatordriveskbd") ||
714              istreq(field, "indicatordriveskeyboard")) {
715         log_dbg(info->keymap->ctx,
716                 "The \"%s\" field in indicator statements is unsupported; "
717                 "Ignored\n", field);
718     }
719     else if (istreq(field, "index")) {
720         /* Users should see this, it might cause unexpected behavior. */
721         log_err(info->keymap->ctx,
722                 "The \"index\" field in indicator statements is unsupported; "
723                 "Ignored\n");
724     }
725     else {
726         log_err(info->keymap->ctx,
727                 "Unknown field %s in map for %s indicator; "
728                 "Definition ignored\n",
729                 field, xkb_atom_text(keymap->ctx, led->im.name));
730         ok = false;
731     }
732
733     return ok;
734 }
735
736 static bool
737 HandleGlobalVar(CompatInfo *info, VarDef *stmt)
738 {
739     const char *elem, *field;
740     ExprDef *ndx;
741     bool ret;
742
743     if (!ExprResolveLhs(info->keymap->ctx, stmt->name, &elem, &field, &ndx))
744         ret = false;
745     else if (elem && istreq(elem, "interpret"))
746         ret = SetInterpField(info, &info->dflt, field, ndx, stmt->value);
747     else if (elem && istreq(elem, "indicator"))
748         ret = SetIndicatorMapField(info, &info->ledDflt, field, ndx,
749                                    stmt->value);
750     else
751         ret = SetActionField(info->keymap, elem, field, ndx, stmt->value,
752                              info->actions);
753     return ret;
754 }
755
756 static bool
757 HandleInterpBody(CompatInfo *info, VarDef *def, SymInterpInfo *si)
758 {
759     bool ok = true;
760     const char *elem, *field;
761     ExprDef *arrayNdx;
762
763     for (; def; def = (VarDef *) def->common.next) {
764         if (def->name && def->name->op == EXPR_FIELD_REF) {
765             log_err(info->keymap->ctx,
766                     "Cannot set a global default value from within an interpret statement; "
767                     "Move statements to the global file scope\n");
768             ok = false;
769             continue;
770         }
771
772         ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
773                             &arrayNdx);
774         if (!ok)
775             continue;
776
777         ok = SetInterpField(info, si, field, arrayNdx, def->value);
778     }
779
780     return ok;
781 }
782
783 static bool
784 HandleInterpDef(CompatInfo *info, InterpDef *def, enum merge_mode merge)
785 {
786     unsigned pred, mods;
787     SymInterpInfo si;
788
789     if (!ResolveStateAndPredicate(def->match, &pred, &mods, info)) {
790         log_err(info->keymap->ctx,
791                 "Couldn't determine matching modifiers; "
792                 "Symbol interpretation ignored\n");
793         return false;
794     }
795
796     si = info->dflt;
797
798     si.merge = merge = (def->merge == MERGE_DEFAULT ? merge : def->merge);
799
800     if (!LookupKeysym(def->sym, &si.interp.sym)) {
801         log_err(info->keymap->ctx,
802                 "Could not resolve keysym %s; "
803                 "Symbol interpretation ignored\n",
804                 def->sym);
805         return false;
806     }
807
808     si.interp.match = pred & XkbSI_OpMask;
809
810     si.interp.mods = mods;
811
812     if (!HandleInterpBody(info, def->def, &si)) {
813         info->errorCount++;
814         return false;
815     }
816
817     if (!AddInterp(info, &si)) {
818         info->errorCount++;
819         return false;
820     }
821
822     return true;
823 }
824
825 static bool
826 HandleIndicatorMapDef(CompatInfo *info, IndicatorMapDef *def,
827                       enum merge_mode merge)
828 {
829     LEDInfo led;
830     VarDef *var;
831     bool ok;
832
833     if (def->merge != MERGE_DEFAULT)
834         merge = def->merge;
835
836     led = info->ledDflt;
837     led.merge = merge;
838     led.im.name = def->name;
839
840     ok = true;
841     for (var = def->body; var != NULL; var = (VarDef *) var->common.next) {
842         const char *elem, *field;
843         ExprDef *arrayNdx;
844         if (!ExprResolveLhs(info->keymap->ctx, var->name, &elem, &field,
845                             &arrayNdx)) {
846             ok = false;
847             continue;
848         }
849
850         if (elem) {
851             log_err(info->keymap->ctx,
852                     "Cannot set defaults for \"%s\" element in indicator map; "
853                     "Assignment to %s.%s ignored\n", elem, elem, field);
854             ok = false;
855         }
856         else {
857             ok = SetIndicatorMapField(info, &led, field, arrayNdx,
858                                       var->value) && ok;
859         }
860     }
861
862     if (ok)
863         return AddIndicatorMap(info, &led);
864
865     return false;
866 }
867
868 static void
869 HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge)
870 {
871     bool ok;
872     ParseCommon *stmt;
873
874     merge = (merge == MERGE_DEFAULT ? MERGE_AUGMENT : merge);
875
876     free(info->name);
877     info->name = strdup_safe(file->name);
878
879     for (stmt = file->defs; stmt; stmt = stmt->next) {
880         switch (stmt->type) {
881         case STMT_INCLUDE:
882             ok = HandleIncludeCompatMap(info, (IncludeStmt *) stmt);
883             break;
884         case STMT_INTERP:
885             ok = HandleInterpDef(info, (InterpDef *) stmt, merge);
886             break;
887         case STMT_GROUP_COMPAT:
888             log_dbg(info->keymap->ctx,
889                     "The \"group\" statement in compat is unsupported; "
890                     "Ignored\n");
891             ok = true;
892             break;
893         case STMT_INDICATOR_MAP:
894             ok = HandleIndicatorMapDef(info, (IndicatorMapDef *) stmt, merge);
895             break;
896         case STMT_VAR:
897             ok = HandleGlobalVar(info, (VarDef *) stmt);
898             break;
899         case STMT_VMOD:
900             ok = HandleVModDef((VModDef *) stmt, info->keymap, merge,
901                                &info->vmods);
902             break;
903         default:
904             log_err(info->keymap->ctx,
905                     "Interpretation files may not include other types; "
906                     "Ignoring %s\n", stmt_type_to_string(stmt->type));
907             ok = false;
908             break;
909         }
910
911         if (!ok)
912             info->errorCount++;
913
914         if (info->errorCount > 10) {
915             log_err(info->keymap->ctx,
916                     "Abandoning compatibility map \"%s\"\n", file->topName);
917             break;
918         }
919     }
920 }
921
922 static void
923 CopyInterps(CompatInfo *info, bool needSymbol, unsigned pred)
924 {
925     SymInterpInfo *si;
926
927     darray_foreach(si, info->interps) {
928         if (((si->interp.match & XkbSI_OpMask) != pred) ||
929             (needSymbol && si->interp.sym == XKB_KEY_NoSymbol) ||
930             (!needSymbol && si->interp.sym != XKB_KEY_NoSymbol))
931             continue;
932
933         darray_append(info->keymap->sym_interpret, si->interp);
934     }
935 }
936
937 static void
938 CopyIndicatorMapDefs(CompatInfo *info)
939 {
940     LEDInfo *led;
941     xkb_led_index_t i;
942     struct xkb_indicator_map *im;
943     struct xkb_keymap *keymap = info->keymap;
944
945     darray_foreach(led, info->leds) {
946         /*
947          * Find the indicator with the given name, if it was already
948          * declared in keycodes.
949          */
950         for (i = 0; i < XKB_NUM_INDICATORS; i++)
951             if (keymap->indicators[i].name == led->im.name)
952                 break;
953
954         /* Not previously declared; create it with next free index. */
955         if (i >= XKB_NUM_INDICATORS) {
956             log_dbg(keymap->ctx,
957                     "Indicator name \"%s\" was not declared in the keycodes section; "
958                     "Adding new indicator\n",
959                     xkb_atom_text(keymap->ctx, led->im.name));
960
961             for (i = 0; i < XKB_NUM_INDICATORS; i++)
962                 if (keymap->indicators[i].name == XKB_ATOM_NONE)
963                     break;
964
965             /* Not place to put it; ignore. */
966             if (i >= XKB_NUM_INDICATORS) {
967                 log_err(keymap->ctx,
968                         "Too many indicators (maximum is %d); "
969                         "Indicator name \"%s\" ignored\n",
970                         XKB_NUM_INDICATORS,
971                         xkb_atom_text(keymap->ctx, led->im.name));
972                 continue;
973             }
974         }
975
976         im = &keymap->indicators[i];
977         *im  = led->im;
978         if (im->groups != 0 && im->which_groups == 0)
979             im->which_groups = XkbIM_UseEffective;
980         if (im->mods.mods != 0 && im->which_mods == 0)
981             im->which_mods = XkbIM_UseEffective;
982     }
983 }
984
985 static bool
986 CopyCompatToKeymap(struct xkb_keymap *keymap, CompatInfo *info)
987 {
988     keymap->compat_section_name = strdup_safe(info->name);
989
990     if (!darray_empty(info->interps)) {
991         /* Most specific to least specific. */
992         CopyInterps(info, true, XkbSI_Exactly);
993         CopyInterps(info, true, XkbSI_AllOf | XkbSI_NoneOf);
994         CopyInterps(info, true, XkbSI_AnyOf);
995         CopyInterps(info, true, XkbSI_AnyOfOrNone);
996         CopyInterps(info, false, XkbSI_Exactly);
997         CopyInterps(info, false, XkbSI_AllOf | XkbSI_NoneOf);
998         CopyInterps(info, false, XkbSI_AnyOf);
999         CopyInterps(info, false, XkbSI_AnyOfOrNone);
1000     }
1001
1002     CopyIndicatorMapDefs(info);
1003
1004     return true;
1005 }
1006
1007 bool
1008 CompileCompatMap(XkbFile *file, struct xkb_keymap *keymap,
1009                  enum merge_mode merge)
1010 {
1011     CompatInfo info;
1012     ActionsInfo *actions;
1013
1014     actions = NewActionsInfo();
1015     if (!actions)
1016         return false;
1017
1018     InitCompatInfo(&info, keymap, file->id, actions);
1019     info.dflt.merge = merge;
1020     info.ledDflt.merge = merge;
1021
1022     HandleCompatMapFile(&info, file, merge);
1023     if (info.errorCount != 0)
1024         goto err_info;
1025
1026     if (!CopyCompatToKeymap(keymap, &info))
1027         goto err_info;
1028
1029     ClearCompatInfo(&info);
1030     FreeActionsInfo(actions);
1031     return true;
1032
1033 err_info:
1034     ClearCompatInfo(&info);
1035     FreeActionsInfo(actions);
1036     return false;
1037 }