Clean up Init/Clear functions
[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 /*
58  * The xkb_compat section
59  * =====================
60  * This section is the third to be processesed, after xkb_keycodes and
61  * xkb_types.
62  *
63  * Interpret statements
64  * --------------------
65  * Statements of the form:
66  *      interpret Num_Lock+Any { ... }
67  *
68  * The body of the statment may include statements of the following
69  * forms:
70  *
71  * - action statement:
72  *      action = LockMods(modifiers=NumLock);
73  *
74  * - virtual modifier statement:
75  *      virtualModifier = NumLock;
76  *
77  * - repeat statement:
78  *      repeat = True;
79  *
80  * - useModMapMods statement:
81  *      useModMapMods = level1;
82  *
83  * Indicator map statements
84  * ------------------------
85  * Statements of the form:
86  *      indicator "Shift Lock" { ... }
87  *
88  *   This statement specifies the behavior and binding of the indicator
89  *   with the given name ("Shift Lock" above). The name should have been
90  *   declared previously in the xkb_keycodes section (see Indicator name
91  *   statement), and given an index there. If it wasn't, it is created
92  *   with the next free index.
93  *   The body of the statement describes the conditions of the keyboard
94  *   state which will cause the indicator to be lit. It may include the
95  *   following statements:
96  *
97  * - modifiers statment:
98  *      modifiers = ScrollLock;
99  *
100  *   If the given modifiers are in the required state (see below), the
101  *   led is lit.
102  *
103  * - whichModifierState statment:
104  *      whichModState = Latched + Locked;
105  *
106  *   Can be any combination of:
107  *      base, latched, locked, effective
108  *      any (i.e. all of the above)
109  *      none (i.e. none of the above)
110  *      compat (this is legal, but unused)
111  *   This will cause the respective portion of the modifer state (see
112  *   struct xkb_state) to be matched against the modifiers given in the
113  *   "modifiers" statement.
114  *
115  *   Here's a simple example:
116  *      indicator "Num Lock" {
117  *          modifiers = NumLock;
118  *          whichModState = Locked;
119  *      };
120  *   Whenever the NumLock modifier is locked, the Num Lock indicator
121  *   will light up.
122  *
123  * - groups statment:
124  *      groups = All - group1;
125  *
126  *   If the given groups are in the required state (see below), the led
127  *   is lit.
128  *
129  * - whichGroupState statment:
130  *      whichGroupState = Effective;
131  *
132  *   Can be any combination of:
133  *      base, latched, locked, effective
134  *      any (i.e. all of the above)
135  *      none (i.e. none of the above)
136  *   This will cause the respective portion of the group state (see
137  *   struct xkb_state) to be matched against the groups given in the
138  *   "groups" statement.
139  *
140  *   Note: the above conditions are disjunctive, i.e. if any of them are
141  *   satisfied the led is lit.
142  *
143  * Virtual modifier statements
144  * ---------------------------
145  * Statements of the form:
146  *     virtual_modifiers LControl;
147  *
148  * Can appear in the xkb_types, xkb_compat, xkb_symbols sections.
149  * TODO
150  *
151  * Effect on keymap
152  * ----------------
153  * After all of the xkb_compat sections have been compiled, the following
154  * members of struct xkb_keymap are finalized:
155  *      darray(struct xkb_sym_interpret) sym_interpret;
156  *      struct xkb_indicator_map indicators[XKB_NUM_INDICATORS];
157  *      char *compat_section_name;
158  * TODO: virtual modifiers.
159  */
160
161 enum si_field {
162     SI_FIELD_VIRTUAL_MOD    = (1 << 0),
163     SI_FIELD_ACTION         = (1 << 1),
164     SI_FIELD_AUTO_REPEAT    = (1 << 2),
165     SI_FIELD_LEVEL_ONE_ONLY = (1 << 3),
166 };
167
168 typedef struct _SymInterpInfo {
169     enum si_field defined;
170     unsigned file_id;
171     enum merge_mode merge;
172
173     struct xkb_sym_interpret interp;
174 } SymInterpInfo;
175
176 enum led_field {
177     LED_FIELD_MODS       = (1 << 0),
178     LED_FIELD_GROUPS     = (1 << 1),
179     LED_FIELD_CTRLS      = (1 << 2),
180 };
181
182 typedef struct _LEDInfo {
183     enum led_field defined;
184     unsigned file_id;
185     enum merge_mode merge;
186
187     struct xkb_indicator_map im;
188 } LEDInfo;
189
190 typedef struct _CompatInfo {
191     char *name;
192     unsigned file_id;
193     int errorCount;
194     SymInterpInfo dflt;
195     darray(SymInterpInfo) interps;
196     LEDInfo ledDflt;
197     darray(LEDInfo) leds;
198     VModInfo vmods;
199     ActionsInfo *actions;
200     struct xkb_keymap *keymap;
201 } CompatInfo;
202
203 static const char *
204 siText(SymInterpInfo *si, CompatInfo *info)
205 {
206     static char buf[128];
207
208     if (si == &info->dflt)
209         return "default";
210
211     snprintf(buf, sizeof(buf), "%s+%s(%s)",
212              KeysymText(si->interp.sym),
213              SIMatchText(si->interp.match),
214              ModMaskText(si->interp.mods));
215     return buf;
216 }
217
218 static inline bool
219 ReportSINotArray(CompatInfo *info, SymInterpInfo *si, const char *field)
220 {
221     return ReportNotArray(info->keymap, "symbol interpretation", field,
222                           siText(si, info));
223 }
224
225 static inline bool
226 ReportSIBadType(CompatInfo *info, SymInterpInfo *si, const char *field,
227                 const char *wanted)
228 {
229     return ReportBadType(info->keymap->ctx, "symbol interpretation", field,
230                          siText(si, info), wanted);
231 }
232
233 static inline bool
234 ReportIndicatorBadType(CompatInfo *info, LEDInfo *led,
235                        const char *field, const char *wanted)
236 {
237     return ReportBadType(info->keymap->ctx, "indicator map", field,
238                          xkb_atom_text(info->keymap->ctx, led->im.name),
239                          wanted);
240 }
241
242 static inline bool
243 ReportIndicatorNotArray(CompatInfo *info, LEDInfo *led,
244                         const char *field)
245 {
246     return ReportNotArray(info->keymap, "indicator map", field,
247                           xkb_atom_text(info->keymap->ctx, led->im.name));
248 }
249
250 static void
251 InitCompatInfo(CompatInfo *info, struct xkb_keymap *keymap, unsigned file_id,
252                ActionsInfo *actions)
253 {
254     memset(info, 0, sizeof(*info));
255     info->keymap = keymap;
256     info->file_id = file_id;
257     info->actions = actions;
258     info->dflt.file_id = file_id;
259     info->dflt.merge = MERGE_OVERRIDE;
260     info->dflt.interp.virtual_mod = XKB_MOD_INVALID;
261     info->ledDflt.file_id = file_id;
262     info->ledDflt.merge = MERGE_OVERRIDE;
263     InitVModInfo(&info->vmods, keymap);
264 }
265
266 static void
267 ClearCompatInfo(CompatInfo *info)
268 {
269     free(info->name);
270     darray_free(info->interps);
271     darray_free(info->leds);
272 }
273
274 static SymInterpInfo *
275 FindMatchingInterp(CompatInfo *info, SymInterpInfo *new)
276 {
277     SymInterpInfo *old;
278
279     darray_foreach(old, info->interps)
280         if (old->interp.sym == new->interp.sym &&
281             old->interp.mods == new->interp.mods &&
282             old->interp.match == new->interp.match)
283             return old;
284
285     return NULL;
286 }
287
288 static bool
289 UseNewInterpField(enum si_field field, SymInterpInfo *old, SymInterpInfo *new,
290                   bool report, enum si_field *collide)
291 {
292     if (!(old->defined & field))
293         return true;
294
295     if (new->defined & field) {
296         if (report)
297             *collide |= field;
298
299         if (new->merge != MERGE_AUGMENT)
300             return true;
301     }
302
303     return false;
304 }
305
306 static bool
307 AddInterp(CompatInfo *info, SymInterpInfo *new)
308 {
309     enum si_field collide = 0;
310     SymInterpInfo *old;
311
312     old = FindMatchingInterp(info, new);
313     if (old) {
314         int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
315         bool report = ((old->file_id == new->file_id && verbosity > 0) ||
316                        verbosity > 9);
317
318         if (new->merge == MERGE_REPLACE) {
319             if (report)
320                 log_warn(info->keymap->ctx,
321                          "Multiple definitions for \"%s\"; "
322                          "Earlier interpretation ignored\n",
323                          siText(new, info));
324             *old = *new;
325             return true;
326         }
327
328         if (UseNewInterpField(SI_FIELD_VIRTUAL_MOD, old, new, report,
329                               &collide)) {
330             old->interp.virtual_mod = new->interp.virtual_mod;
331             old->defined |= SI_FIELD_VIRTUAL_MOD;
332         }
333         if (UseNewInterpField(SI_FIELD_ACTION, old, new, report,
334                               &collide)) {
335             old->interp.act = new->interp.act;
336             old->defined |= SI_FIELD_ACTION;
337         }
338         if (UseNewInterpField(SI_FIELD_AUTO_REPEAT, old, new, report,
339                               &collide)) {
340             old->interp.repeat = new->interp.repeat;
341             old->defined |= SI_FIELD_AUTO_REPEAT;
342         }
343         if (UseNewInterpField(SI_FIELD_LEVEL_ONE_ONLY, old, new, report,
344                               &collide)) {
345             old->interp.match &= ~MATCH_LEVEL_ONE_ONLY;
346             old->interp.match |= (new->interp.match & MATCH_LEVEL_ONE_ONLY);
347             old->defined |= SI_FIELD_LEVEL_ONE_ONLY;
348         }
349
350         if (collide) {
351             log_warn(info->keymap->ctx,
352                      "Multiple interpretations of \"%s\"; "
353                      "Using %s definition for duplicate fields\n",
354                      siText(new, info),
355                      (new->merge != MERGE_AUGMENT ? "last" : "first"));
356         }
357
358         return true;
359     }
360
361     darray_append(info->interps, *new);
362     return true;
363 }
364
365
366 /***====================================================================***/
367
368 static bool
369 ResolveStateAndPredicate(ExprDef *expr, enum xkb_match_operation *pred_rtrn,
370                          xkb_mod_mask_t *mods_rtrn, CompatInfo *info)
371 {
372     if (expr == NULL) {
373         *pred_rtrn = MATCH_ANY_OR_NONE;
374         *mods_rtrn = ~0;
375         return true;
376     }
377
378     *pred_rtrn = MATCH_EXACTLY;
379     if (expr->op == EXPR_ACTION_DECL) {
380         const char *pred_txt = xkb_atom_text(info->keymap->ctx,
381                                              expr->value.action.name);
382         if (!LookupString(symInterpretMatchMaskNames, pred_txt, pred_rtrn)) {
383             log_err(info->keymap->ctx,
384                     "Illegal modifier predicate \"%s\"; Ignored\n", pred_txt);
385             return false;
386         }
387         expr = expr->value.action.args;
388     }
389     else if (expr->op == EXPR_IDENT) {
390         const char *pred_txt = xkb_atom_text(info->keymap->ctx,
391                                              expr->value.str);
392         if (pred_txt && istreq(pred_txt, "any")) {
393             *pred_rtrn = MATCH_ANY;
394             *mods_rtrn = 0xff;
395             return true;
396         }
397     }
398
399     return ExprResolveModMask(info->keymap->ctx, expr, mods_rtrn);
400 }
401
402 /***====================================================================***/
403
404 static bool
405 UseNewLEDField(enum led_field field, LEDInfo *old, LEDInfo *new,
406                bool report, enum led_field *collide)
407 {
408     if (!(old->defined & field))
409         return true;
410
411     if (new->defined & field) {
412         if (report)
413             *collide |= field;
414
415         if (new->merge != MERGE_AUGMENT)
416             return true;
417     }
418
419     return false;
420 }
421
422 static bool
423 AddIndicatorMap(CompatInfo *info, LEDInfo *new)
424 {
425     LEDInfo *old;
426     enum led_field collide;
427     struct xkb_context *ctx = info->keymap->ctx;
428     int verbosity = xkb_get_log_verbosity(ctx);
429
430     darray_foreach(old, info->leds) {
431         bool report;
432
433         if (old->im.name != new->im.name)
434             continue;
435
436         if (old->im.mods.mods == new->im.mods.mods &&
437             old->im.groups == new->im.groups &&
438             old->im.ctrls == new->im.ctrls &&
439             old->im.which_mods == new->im.which_mods &&
440             old->im.which_groups == new->im.which_groups) {
441             old->defined |= new->defined;
442             return true;
443         }
444
445         report = ((old->file_id == new->file_id && verbosity > 0) ||
446                   verbosity > 9);
447
448         if (new->merge == MERGE_REPLACE) {
449             if (report)
450                 log_warn(info->keymap->ctx,
451                          "Map for indicator %s redefined; "
452                          "Earlier definition ignored\n",
453                          xkb_atom_text(ctx, old->im.name));
454             *old = *new;
455             return true;
456         }
457
458         collide = 0;
459         if (UseNewLEDField(LED_FIELD_MODS, old, new, report, &collide)) {
460             old->im.which_mods = new->im.which_mods;
461             old->im.mods = new->im.mods;
462             old->defined |= LED_FIELD_MODS;
463         }
464         if (UseNewLEDField(LED_FIELD_GROUPS, old, new, report, &collide)) {
465             old->im.which_groups = new->im.which_groups;
466             old->im.groups = new->im.groups;
467             old->defined |= LED_FIELD_GROUPS;
468         }
469         if (UseNewLEDField(LED_FIELD_CTRLS, old, new, report, &collide)) {
470             old->im.ctrls = new->im.ctrls;
471             old->defined |= LED_FIELD_CTRLS;
472         }
473
474         if (collide) {
475             log_warn(info->keymap->ctx,
476                      "Map for indicator %s redefined; "
477                      "Using %s definition for duplicate fields\n",
478                      xkb_atom_text(ctx, old->im.name),
479                      (new->merge == MERGE_AUGMENT ? "first" : "last"));
480         }
481
482         return true;
483     }
484
485     darray_append(info->leds, *new);
486     return true;
487 }
488
489 static void
490 MergeIncludedCompatMaps(CompatInfo *into, CompatInfo *from,
491                         enum merge_mode merge)
492 {
493     SymInterpInfo *si;
494     LEDInfo *led;
495
496     if (from->errorCount > 0) {
497         into->errorCount += from->errorCount;
498         return;
499     }
500
501     if (into->name == NULL) {
502         into->name = from->name;
503         from->name = NULL;
504     }
505
506     darray_foreach(si, from->interps) {
507         si->merge = (merge == MERGE_DEFAULT ? si->merge : merge);
508         if (!AddInterp(into, si))
509             into->errorCount++;
510     }
511
512     darray_foreach(led, from->leds) {
513         led->merge = (merge == MERGE_DEFAULT ? led->merge : merge);
514         if (!AddIndicatorMap(into, led))
515             into->errorCount++;
516     }
517 }
518
519 static void
520 HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge);
521
522 static bool
523 HandleIncludeCompatMap(CompatInfo *info, IncludeStmt *stmt)
524 {
525     enum merge_mode merge = MERGE_DEFAULT;
526     XkbFile *rtrn;
527     CompatInfo included, next_incl;
528
529     InitCompatInfo(&included, info->keymap, info->file_id, info->actions);
530     if (stmt->stmt) {
531         free(included.name);
532         included.name = stmt->stmt;
533         stmt->stmt = NULL;
534     }
535
536     for (; stmt; stmt = stmt->next_incl) {
537         if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_COMPAT,
538                                 &rtrn, &merge)) {
539             info->errorCount += 10;
540             ClearCompatInfo(&included);
541             return false;
542         }
543
544         InitCompatInfo(&next_incl, info->keymap, rtrn->id, info->actions);
545         next_incl.file_id = rtrn->id;
546         next_incl.dflt = info->dflt;
547         next_incl.dflt.file_id = rtrn->id;
548         next_incl.dflt.merge = merge;
549         next_incl.ledDflt.file_id = rtrn->id;
550         next_incl.ledDflt.merge = merge;
551
552         HandleCompatMapFile(&next_incl, rtrn, MERGE_OVERRIDE);
553
554         MergeIncludedCompatMaps(&included, &next_incl, merge);
555
556         ClearCompatInfo(&next_incl);
557         FreeXkbFile(rtrn);
558     }
559
560     MergeIncludedCompatMaps(info, &included, merge);
561     ClearCompatInfo(&included);
562
563     return (info->errorCount == 0);
564 }
565
566 static bool
567 SetInterpField(CompatInfo *info, SymInterpInfo *si, const char *field,
568                ExprDef *arrayNdx, ExprDef *value)
569 {
570     struct xkb_keymap *keymap = info->keymap;
571     xkb_mod_index_t ndx;
572
573     if (istreq(field, "action")) {
574         if (arrayNdx)
575             return ReportSINotArray(info, si, field);
576
577         if (!HandleActionDef(value, keymap, &si->interp.act, info->actions))
578             return false;
579
580         si->defined |= SI_FIELD_ACTION;
581     }
582     else if (istreq(field, "virtualmodifier") ||
583              istreq(field, "virtualmod")) {
584         if (arrayNdx)
585             return ReportSINotArray(info, si, field);
586
587         if (!ResolveVirtualModifier(value, keymap, &ndx, &info->vmods))
588             return ReportSIBadType(info, si, field, "virtual modifier");
589
590         si->interp.virtual_mod = ndx;
591         si->defined |= SI_FIELD_VIRTUAL_MOD;
592     }
593     else if (istreq(field, "repeat")) {
594         bool set;
595
596         if (arrayNdx)
597             return ReportSINotArray(info, si, field);
598
599         if (!ExprResolveBoolean(keymap->ctx, value, &set))
600             return ReportSIBadType(info, si, field, "boolean");
601
602         si->interp.repeat = set;
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 |= MATCH_LEVEL_ONE_ONLY;
623         else
624             si->interp.match &= ~MATCH_LEVEL_ONE_ONLY;
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 & MATCH_OP_MASK;
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, enum xkb_match_operation pred)
924 {
925     SymInterpInfo *si;
926
927     darray_foreach(si, info->interps) {
928         if (((si->interp.match & MATCH_OP_MASK) != 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 = XKB_STATE_EFFECTIVE;
980         if (im->mods.mods != 0 && im->which_mods == 0)
981             im->which_mods = XKB_STATE_EFFECTIVE;
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, MATCH_EXACTLY);
993         CopyInterps(info, true, MATCH_ALL);
994         CopyInterps(info, true, MATCH_ANY);
995         CopyInterps(info, true, MATCH_ANY_OR_NONE);
996         CopyInterps(info, false, MATCH_EXACTLY);
997         CopyInterps(info, false, MATCH_ALL);
998         CopyInterps(info, false, MATCH_ANY);
999         CopyInterps(info, false, MATCH_ANY_OR_NONE);
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 }