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