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