44a444cccc9ba260ae4945b1c87e6a5cc0e8bca7
[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 "config.h"
51
52 #include "xkbcomp-priv.h"
53 #include "text.h"
54 #include "expr.h"
55 #include "action.h"
56 #include "vmod.h"
57 #include "include.h"
58
59 enum si_field {
60     SI_FIELD_VIRTUAL_MOD = (1 << 0),
61     SI_FIELD_ACTION = (1 << 1),
62     SI_FIELD_AUTO_REPEAT = (1 << 2),
63     SI_FIELD_LEVEL_ONE_ONLY = (1 << 3),
64 };
65
66 typedef struct {
67     enum si_field defined;
68     enum merge_mode merge;
69
70     struct xkb_sym_interpret interp;
71 } SymInterpInfo;
72
73 enum led_field {
74     LED_FIELD_MODS = (1 << 0),
75     LED_FIELD_GROUPS = (1 << 1),
76     LED_FIELD_CTRLS = (1 << 2),
77 };
78
79 typedef struct {
80     enum led_field defined;
81     enum merge_mode merge;
82
83     struct xkb_led led;
84 } LedInfo;
85
86 typedef struct {
87     char *name;
88     int errorCount;
89     SymInterpInfo default_interp;
90     darray(SymInterpInfo) interps;
91     LedInfo default_led;
92     LedInfo leds[XKB_MAX_LEDS];
93     unsigned int num_leds;
94     ActionsInfo *actions;
95     struct xkb_mod_set mods;
96
97     struct xkb_context *ctx;
98 } CompatInfo;
99
100 static const char *
101 siText(SymInterpInfo *si, CompatInfo *info)
102 {
103     char *buf = xkb_context_get_buffer(info->ctx, 128);
104
105     if (si == &info->default_interp)
106         return "default";
107
108     snprintf(buf, 128, "%s+%s(%s)",
109              KeysymText(info->ctx, si->interp.sym),
110              SIMatchText(si->interp.match),
111              ModMaskText(info->ctx, &info->mods, si->interp.mods));
112
113     return buf;
114 }
115
116 static inline bool
117 ReportSINotArray(CompatInfo *info, SymInterpInfo *si, const char *field)
118 {
119     return ReportNotArray(info->ctx, "symbol interpretation", field,
120                           siText(si, info));
121 }
122
123 static inline bool
124 ReportSIBadType(CompatInfo *info, SymInterpInfo *si, const char *field,
125                 const char *wanted)
126 {
127     return ReportBadType(info->ctx, XKB_ERROR_WRONG_FIELD_TYPE,
128                          "symbol interpretation", field,
129                          siText(si, info), wanted);
130 }
131
132 static inline bool
133 ReportLedBadType(CompatInfo *info, LedInfo *ledi, const char *field,
134                  const char *wanted)
135 {
136     return ReportBadType(info->ctx, XKB_ERROR_WRONG_FIELD_TYPE,
137                          "indicator map", field,
138                          xkb_atom_text(info->ctx, ledi->led.name),
139                          wanted);
140 }
141
142 static inline bool
143 ReportLedNotArray(CompatInfo *info, LedInfo *ledi, const char *field)
144 {
145     return ReportNotArray(info->ctx, "indicator map", field,
146                           xkb_atom_text(info->ctx, ledi->led.name));
147 }
148
149 static void
150 InitCompatInfo(CompatInfo *info, struct xkb_context *ctx,
151                ActionsInfo *actions, const struct xkb_mod_set *mods)
152 {
153     memset(info, 0, sizeof(*info));
154     info->ctx = ctx;
155     info->actions = actions;
156     info->mods = *mods;
157     info->default_interp.merge = MERGE_OVERRIDE;
158     info->default_interp.interp.virtual_mod = XKB_MOD_INVALID;
159     info->default_led.merge = MERGE_OVERRIDE;
160 }
161
162 static void
163 ClearCompatInfo(CompatInfo *info)
164 {
165     free(info->name);
166     darray_free(info->interps);
167 }
168
169 static SymInterpInfo *
170 FindMatchingInterp(CompatInfo *info, SymInterpInfo *new)
171 {
172     SymInterpInfo *old;
173
174     darray_foreach(old, info->interps)
175         if (old->interp.sym == new->interp.sym &&
176             old->interp.mods == new->interp.mods &&
177             old->interp.match == new->interp.match)
178             return old;
179
180     return NULL;
181 }
182
183 static bool
184 UseNewInterpField(enum si_field field, SymInterpInfo *old, SymInterpInfo *new,
185                   bool report, enum si_field *collide)
186 {
187     if (!(old->defined & field))
188         return true;
189
190     if (new->defined & field) {
191         if (report)
192             *collide |= field;
193
194         if (new->merge != MERGE_AUGMENT)
195             return true;
196     }
197
198     return false;
199 }
200
201 static bool
202 AddInterp(CompatInfo *info, SymInterpInfo *new, bool same_file)
203 {
204     SymInterpInfo *old = FindMatchingInterp(info, new);
205     if (old) {
206         const int verbosity = xkb_context_get_log_verbosity(info->ctx);
207         const bool report = (same_file && verbosity > 0) || verbosity > 9;
208         enum si_field collide = 0;
209
210         if (new->merge == MERGE_REPLACE) {
211             if (report)
212                 log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID,
213                          "Multiple definitions for \"%s\"; "
214                          "Earlier interpretation ignored\n",
215                          siText(new, info));
216             *old = *new;
217             return true;
218         }
219
220         if (UseNewInterpField(SI_FIELD_VIRTUAL_MOD, old, new, report,
221                               &collide)) {
222             old->interp.virtual_mod = new->interp.virtual_mod;
223             old->defined |= SI_FIELD_VIRTUAL_MOD;
224         }
225         if (UseNewInterpField(SI_FIELD_ACTION, old, new, report,
226                               &collide)) {
227             old->interp.action = new->interp.action;
228             old->defined |= SI_FIELD_ACTION;
229         }
230         if (UseNewInterpField(SI_FIELD_AUTO_REPEAT, old, new, report,
231                               &collide)) {
232             old->interp.repeat = new->interp.repeat;
233             old->defined |= SI_FIELD_AUTO_REPEAT;
234         }
235         if (UseNewInterpField(SI_FIELD_LEVEL_ONE_ONLY, old, new, report,
236                               &collide)) {
237             old->interp.level_one_only = new->interp.level_one_only;
238             old->defined |= SI_FIELD_LEVEL_ONE_ONLY;
239         }
240
241         if (collide) {
242             log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID,
243                      "Multiple interpretations of \"%s\"; "
244                      "Using %s definition for duplicate fields\n",
245                      siText(new, info),
246                      (new->merge != MERGE_AUGMENT ? "last" : "first"));
247         }
248
249         return true;
250     }
251
252     darray_append(info->interps, *new);
253     return true;
254 }
255
256 /***====================================================================***/
257
258 static bool
259 ResolveStateAndPredicate(ExprDef *expr, enum xkb_match_operation *pred_rtrn,
260                          xkb_mod_mask_t *mods_rtrn, CompatInfo *info)
261 {
262     if (expr == NULL) {
263         *pred_rtrn = MATCH_ANY_OR_NONE;
264         *mods_rtrn = MOD_REAL_MASK_ALL;
265         return true;
266     }
267
268     *pred_rtrn = MATCH_EXACTLY;
269     if (expr->expr.op == EXPR_ACTION_DECL) {
270         const char *pred_txt = xkb_atom_text(info->ctx, expr->action.name);
271         if (!LookupString(symInterpretMatchMaskNames, pred_txt, pred_rtrn) ||
272             !expr->action.args || expr->action.args->common.next) {
273             log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
274                     "Illegal modifier predicate \"%s\"; Ignored\n", pred_txt);
275             return false;
276         }
277         expr = expr->action.args;
278     }
279     else if (expr->expr.op == EXPR_IDENT) {
280         const char *pred_txt = xkb_atom_text(info->ctx, expr->ident.ident);
281         if (pred_txt && istreq(pred_txt, "any")) {
282             *pred_rtrn = MATCH_ANY;
283             *mods_rtrn = MOD_REAL_MASK_ALL;
284             return true;
285         }
286     }
287
288     return ExprResolveModMask(info->ctx, expr, MOD_REAL, &info->mods,
289                               mods_rtrn);
290 }
291
292 /***====================================================================***/
293
294 static bool
295 UseNewLEDField(enum led_field field, LedInfo *old, LedInfo *new,
296                bool report, enum led_field *collide)
297 {
298     if (!(old->defined & field))
299         return true;
300
301     if (new->defined & field) {
302         if (report)
303             *collide |= field;
304
305         if (new->merge != MERGE_AUGMENT)
306             return true;
307     }
308
309     return false;
310 }
311
312 static bool
313 AddLedMap(CompatInfo *info, LedInfo *new, bool same_file)
314 {
315     enum led_field collide;
316     const int verbosity = xkb_context_get_log_verbosity(info->ctx);
317     const bool report = (same_file && verbosity > 0) || verbosity > 9;
318
319     for (xkb_led_index_t i = 0; i < info->num_leds; i++) {
320         LedInfo *old = &info->leds[i];
321
322         if (old->led.name != new->led.name)
323             continue;
324
325         if (old->led.mods.mods == new->led.mods.mods &&
326             old->led.groups == new->led.groups &&
327             old->led.ctrls == new->led.ctrls &&
328             old->led.which_mods == new->led.which_mods &&
329             old->led.which_groups == new->led.which_groups) {
330             old->defined |= new->defined;
331             return true;
332         }
333
334         if (new->merge == MERGE_REPLACE) {
335             if (report)
336                 log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID,
337                          "Map for indicator %s redefined; "
338                          "Earlier definition ignored\n",
339                          xkb_atom_text(info->ctx, old->led.name));
340             *old = *new;
341             return true;
342         }
343
344         collide = 0;
345         if (UseNewLEDField(LED_FIELD_MODS, old, new, report, &collide)) {
346             old->led.which_mods = new->led.which_mods;
347             old->led.mods = new->led.mods;
348             old->defined |= LED_FIELD_MODS;
349         }
350         if (UseNewLEDField(LED_FIELD_GROUPS, old, new, report, &collide)) {
351             old->led.which_groups = new->led.which_groups;
352             old->led.groups = new->led.groups;
353             old->defined |= LED_FIELD_GROUPS;
354         }
355         if (UseNewLEDField(LED_FIELD_CTRLS, old, new, report, &collide)) {
356             old->led.ctrls = new->led.ctrls;
357             old->defined |= LED_FIELD_CTRLS;
358         }
359
360         if (collide) {
361             log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID,
362                      "Map for indicator %s redefined; "
363                      "Using %s definition for duplicate fields\n",
364                      xkb_atom_text(info->ctx, old->led.name),
365                      (new->merge == MERGE_AUGMENT ? "first" : "last"));
366         }
367
368         return true;
369     }
370
371     if (info->num_leds >= XKB_MAX_LEDS) {
372         log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
373                 "Too many LEDs defined (maximum %d)\n",
374                 XKB_MAX_LEDS);
375         return false;
376     }
377     info->leds[info->num_leds++] = *new;
378     return true;
379 }
380
381 static void
382 MergeIncludedCompatMaps(CompatInfo *into, CompatInfo *from,
383                         enum merge_mode merge)
384 {
385     if (from->errorCount > 0) {
386         into->errorCount += from->errorCount;
387         return;
388     }
389
390     into->mods = from->mods;
391
392     if (into->name == NULL) {
393         into->name = from->name;
394         from->name = NULL;
395     }
396
397     if (darray_empty(into->interps)) {
398         into->interps = from->interps;
399         darray_init(from->interps);
400     }
401     else {
402         SymInterpInfo *si;
403         darray_foreach(si, from->interps) {
404             si->merge = (merge == MERGE_DEFAULT ? si->merge : merge);
405             if (!AddInterp(into, si, false))
406                 into->errorCount++;
407         }
408     }
409
410     if (into->num_leds == 0) {
411         memcpy(into->leds, from->leds, sizeof(*from->leds) * from->num_leds);
412         into->num_leds = from->num_leds;
413         from->num_leds = 0;
414     }
415     else {
416         for (xkb_led_index_t i = 0; i < from->num_leds; i++) {
417             LedInfo *ledi = &from->leds[i];
418             ledi->merge = (merge == MERGE_DEFAULT ? ledi->merge : merge);
419             if (!AddLedMap(into, ledi, false))
420                 into->errorCount++;
421         }
422     }
423 }
424
425 static void
426 HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge);
427
428 static bool
429 HandleIncludeCompatMap(CompatInfo *info, IncludeStmt *include)
430 {
431     CompatInfo included;
432
433     if (!include)
434         return false;
435
436     InitCompatInfo(&included, info->ctx, info->actions, &info->mods);
437     included.name = include->stmt;
438     include->stmt = NULL;
439
440     for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
441         CompatInfo next_incl;
442         XkbFile *file;
443
444         file = ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_COMPAT);
445         if (!file) {
446             info->errorCount += 10;
447             ClearCompatInfo(&included);
448             return false;
449         }
450
451         InitCompatInfo(&next_incl, info->ctx, info->actions, &included.mods);
452         next_incl.default_interp = info->default_interp;
453         next_incl.default_interp.merge = stmt->merge;
454         next_incl.default_led = info->default_led;
455         next_incl.default_led.merge = stmt->merge;
456
457         HandleCompatMapFile(&next_incl, file, MERGE_OVERRIDE);
458
459         MergeIncludedCompatMaps(&included, &next_incl, stmt->merge);
460
461         ClearCompatInfo(&next_incl);
462         FreeXkbFile(file);
463     }
464
465     MergeIncludedCompatMaps(info, &included, include->merge);
466     ClearCompatInfo(&included);
467
468     return (info->errorCount == 0);
469 }
470
471 static bool
472 SetInterpField(CompatInfo *info, SymInterpInfo *si, const char *field,
473                ExprDef *arrayNdx, ExprDef *value)
474 {
475     xkb_mod_index_t ndx;
476
477     if (istreq(field, "action")) {
478         if (arrayNdx)
479             return ReportSINotArray(info, si, field);
480
481         if (!HandleActionDef(info->ctx, info->actions, &info->mods,
482                              value, &si->interp.action))
483             return false;
484
485         si->defined |= SI_FIELD_ACTION;
486     }
487     else if (istreq(field, "virtualmodifier") ||
488              istreq(field, "virtualmod")) {
489         if (arrayNdx)
490             return ReportSINotArray(info, si, field);
491
492         if (!ExprResolveMod(info->ctx, value, MOD_VIRT, &info->mods, &ndx))
493             return ReportSIBadType(info, si, field, "virtual modifier");
494
495         si->interp.virtual_mod = ndx;
496         si->defined |= SI_FIELD_VIRTUAL_MOD;
497     }
498     else if (istreq(field, "repeat")) {
499         bool set;
500
501         if (arrayNdx)
502             return ReportSINotArray(info, si, field);
503
504         if (!ExprResolveBoolean(info->ctx, value, &set))
505             return ReportSIBadType(info, si, field, "boolean");
506
507         si->interp.repeat = set;
508
509         si->defined |= SI_FIELD_AUTO_REPEAT;
510     }
511     else if (istreq(field, "locking")) {
512         log_dbg(info->ctx, XKB_LOG_MESSAGE_NO_ID,
513                 "The \"locking\" field in symbol interpretation is unsupported; "
514                 "Ignored\n");
515     }
516     else if (istreq(field, "usemodmap") ||
517              istreq(field, "usemodmapmods")) {
518         unsigned int val;
519
520         if (arrayNdx)
521             return ReportSINotArray(info, si, field);
522
523         if (!ExprResolveEnum(info->ctx, value, &val, useModMapValueNames))
524             return ReportSIBadType(info, si, field, "level specification");
525
526         si->interp.level_one_only = val;
527         si->defined |= SI_FIELD_LEVEL_ONE_ONLY;
528     }
529     else {
530         return ReportBadField(info->ctx, "symbol interpretation", field,
531                               siText(si, info));
532     }
533
534     return true;
535 }
536
537 static bool
538 SetLedMapField(CompatInfo *info, LedInfo *ledi, const char *field,
539                ExprDef *arrayNdx, ExprDef *value)
540 {
541     bool ok = true;
542
543     if (istreq(field, "modifiers") || istreq(field, "mods")) {
544         if (arrayNdx)
545             return ReportLedNotArray(info, ledi, field);
546
547         if (!ExprResolveModMask(info->ctx, value, MOD_BOTH,
548                                 &info->mods, &ledi->led.mods.mods))
549             return ReportLedBadType(info, ledi, field, "modifier mask");
550
551         ledi->defined |= LED_FIELD_MODS;
552     }
553     else if (istreq(field, "groups")) {
554         unsigned int mask;
555
556         if (arrayNdx)
557             return ReportLedNotArray(info, ledi, field);
558
559         if (!ExprResolveMask(info->ctx, value, &mask, groupMaskNames))
560             return ReportLedBadType(info, ledi, field, "group mask");
561
562         ledi->led.groups = mask;
563         ledi->defined |= LED_FIELD_GROUPS;
564     }
565     else if (istreq(field, "controls") || istreq(field, "ctrls")) {
566         unsigned int mask;
567
568         if (arrayNdx)
569             return ReportLedNotArray(info, ledi, field);
570
571         if (!ExprResolveMask(info->ctx, value, &mask, ctrlMaskNames))
572             return ReportLedBadType(info, ledi, field, "controls mask");
573
574         ledi->led.ctrls = mask;
575         ledi->defined |= LED_FIELD_CTRLS;
576     }
577     else if (istreq(field, "allowexplicit")) {
578         log_dbg(info->ctx, XKB_LOG_MESSAGE_NO_ID,
579                 "The \"allowExplicit\" field in indicator statements is unsupported; "
580                 "Ignored\n");
581     }
582     else if (istreq(field, "whichmodstate") ||
583              istreq(field, "whichmodifierstate")) {
584         unsigned int mask;
585
586         if (arrayNdx)
587             return ReportLedNotArray(info, ledi, field);
588
589         if (!ExprResolveMask(info->ctx, value, &mask,
590                              modComponentMaskNames))
591             return ReportLedBadType(info, ledi, field,
592                                     "mask of modifier state components");
593
594         ledi->led.which_mods = mask;
595     }
596     else if (istreq(field, "whichgroupstate")) {
597         unsigned mask;
598
599         if (arrayNdx)
600             return ReportLedNotArray(info, ledi, field);
601
602         if (!ExprResolveMask(info->ctx, value, &mask,
603                              groupComponentMaskNames))
604             return ReportLedBadType(info, ledi, field,
605                                     "mask of group state components");
606
607         ledi->led.which_groups = mask;
608     }
609     else if (istreq(field, "driveskbd") ||
610              istreq(field, "driveskeyboard") ||
611              istreq(field, "leddriveskbd") ||
612              istreq(field, "leddriveskeyboard") ||
613              istreq(field, "indicatordriveskbd") ||
614              istreq(field, "indicatordriveskeyboard")) {
615         log_dbg(info->ctx, XKB_LOG_MESSAGE_NO_ID,
616                 "The \"%s\" field in indicator statements is unsupported; "
617                 "Ignored\n", field);
618     }
619     else if (istreq(field, "index")) {
620         /* Users should see this, it might cause unexpected behavior. */
621         log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
622                 "The \"index\" field in indicator statements is unsupported; "
623                 "Ignored\n");
624     }
625     else {
626         log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
627                 "Unknown field %s in map for %s indicator; "
628                 "Definition ignored\n",
629                 field, xkb_atom_text(info->ctx, ledi->led.name));
630         ok = false;
631     }
632
633     return ok;
634 }
635
636 static bool
637 HandleGlobalVar(CompatInfo *info, VarDef *stmt)
638 {
639     const char *elem, *field;
640     ExprDef *ndx;
641     bool ret;
642
643     if (!ExprResolveLhs(info->ctx, stmt->name, &elem, &field, &ndx))
644         ret = false;
645     else if (elem && istreq(elem, "interpret"))
646         ret = SetInterpField(info, &info->default_interp, field, ndx,
647                              stmt->value);
648     else if (elem && istreq(elem, "indicator"))
649         ret = SetLedMapField(info, &info->default_led, field, ndx,
650                              stmt->value);
651     else
652         ret = SetActionField(info->ctx, info->actions, &info->mods,
653                              elem, field, ndx, stmt->value);
654     return ret;
655 }
656
657 static bool
658 HandleInterpBody(CompatInfo *info, VarDef *def, SymInterpInfo *si)
659 {
660     bool ok = true;
661     const char *elem, *field;
662     ExprDef *arrayNdx;
663
664     for (; def; def = (VarDef *) def->common.next) {
665         if (def->name && def->name->expr.op == EXPR_FIELD_REF) {
666             log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
667                     "Cannot set a global default value from within an interpret statement; "
668                     "Move statements to the global file scope\n");
669             ok = false;
670             continue;
671         }
672
673         ok = ExprResolveLhs(info->ctx, def->name, &elem, &field, &arrayNdx);
674         if (!ok)
675             continue;
676
677         ok = SetInterpField(info, si, field, arrayNdx, def->value);
678     }
679
680     return ok;
681 }
682
683 static bool
684 HandleInterpDef(CompatInfo *info, InterpDef *def, enum merge_mode merge)
685 {
686     enum xkb_match_operation pred;
687     xkb_mod_mask_t mods;
688     SymInterpInfo si;
689
690     if (!ResolveStateAndPredicate(def->match, &pred, &mods, info)) {
691         log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
692                 "Couldn't determine matching modifiers; "
693                 "Symbol interpretation ignored\n");
694         return false;
695     }
696
697     si = info->default_interp;
698     si.merge = (def->merge == MERGE_DEFAULT ? merge : def->merge);
699     si.interp.sym = def->sym;
700     si.interp.match = pred;
701     si.interp.mods = mods;
702
703     if (!HandleInterpBody(info, def->def, &si)) {
704         info->errorCount++;
705         return false;
706     }
707
708     if (!AddInterp(info, &si, true)) {
709         info->errorCount++;
710         return false;
711     }
712
713     return true;
714 }
715
716 static bool
717 HandleLedMapDef(CompatInfo *info, LedMapDef *def, enum merge_mode merge)
718 {
719     LedInfo ledi;
720     VarDef *var;
721     bool ok;
722
723     if (def->merge != MERGE_DEFAULT)
724         merge = def->merge;
725
726     ledi = info->default_led;
727     ledi.merge = merge;
728     ledi.led.name = def->name;
729
730     ok = true;
731     for (var = def->body; var != NULL; var = (VarDef *) var->common.next) {
732         const char *elem, *field;
733         ExprDef *arrayNdx;
734         if (!ExprResolveLhs(info->ctx, var->name, &elem, &field, &arrayNdx)) {
735             ok = false;
736             continue;
737         }
738
739         if (elem) {
740             log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
741                     "Cannot set defaults for \"%s\" element in indicator map; "
742                     "Assignment to %s.%s ignored\n", elem, elem, field);
743             ok = false;
744         }
745         else {
746             ok = SetLedMapField(info, &ledi, field, arrayNdx, var->value) && ok;
747         }
748     }
749
750     if (ok)
751         return AddLedMap(info, &ledi, true);
752
753     return false;
754 }
755
756 static void
757 HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge)
758 {
759     bool ok;
760
761     merge = (merge == MERGE_DEFAULT ? MERGE_AUGMENT : merge);
762
763     free(info->name);
764     info->name = strdup_safe(file->name);
765
766     for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
767         switch (stmt->type) {
768         case STMT_INCLUDE:
769             ok = HandleIncludeCompatMap(info, (IncludeStmt *) stmt);
770             break;
771         case STMT_INTERP:
772             ok = HandleInterpDef(info, (InterpDef *) stmt, merge);
773             break;
774         case STMT_GROUP_COMPAT:
775             log_dbg(info->ctx, XKB_LOG_MESSAGE_NO_ID,
776                     "The \"group\" statement in compat is unsupported; "
777                     "Ignored\n");
778             ok = true;
779             break;
780         case STMT_LED_MAP:
781             ok = HandleLedMapDef(info, (LedMapDef *) stmt, merge);
782             break;
783         case STMT_VAR:
784             ok = HandleGlobalVar(info, (VarDef *) stmt);
785             break;
786         case STMT_VMOD:
787             ok = HandleVModDef(info->ctx, &info->mods, (VModDef *) stmt, merge);
788             break;
789         default:
790             log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
791                     "Compat files may not include other types; "
792                     "Ignoring %s\n", stmt_type_to_string(stmt->type));
793             ok = false;
794             break;
795         }
796
797         if (!ok)
798             info->errorCount++;
799
800         if (info->errorCount > 10) {
801             log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
802                     "Abandoning compatibility map \"%s\"\n", file->name);
803             break;
804         }
805     }
806 }
807
808 /* Temporary struct for CopyInterps. */
809 struct collect {
810     darray(struct xkb_sym_interpret) sym_interprets;
811 };
812
813 static void
814 CopyInterps(CompatInfo *info, bool needSymbol, enum xkb_match_operation pred,
815             struct collect *collect)
816 {
817     SymInterpInfo *si;
818
819     darray_foreach(si, info->interps)
820         if (si->interp.match == pred &&
821             (si->interp.sym != XKB_KEY_NoSymbol) == needSymbol)
822             darray_append(collect->sym_interprets, si->interp);
823 }
824
825 static void
826 CopyLedMapDefsToKeymap(struct xkb_keymap *keymap, CompatInfo *info)
827 {
828     for (xkb_led_index_t idx = 0; idx < info->num_leds; idx++) {
829         LedInfo *ledi = &info->leds[idx];
830         xkb_led_index_t i;
831         struct xkb_led *led;
832
833         /*
834          * Find the LED with the given name, if it was already declared
835          * in keycodes.
836          */
837         xkb_leds_enumerate(i, led, keymap)
838             if (led->name == ledi->led.name)
839                 break;
840
841         /* Not previously declared; create it with next free index. */
842         if (i >= keymap->num_leds) {
843             log_dbg(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
844                     "Indicator name \"%s\" was not declared in the keycodes section; "
845                     "Adding new indicator\n",
846                     xkb_atom_text(keymap->ctx, ledi->led.name));
847
848             xkb_leds_enumerate(i, led, keymap)
849                 if (led->name == XKB_ATOM_NONE)
850                     break;
851
852             if (i >= keymap->num_leds) {
853                 /* Not place to put it; ignore. */
854                 if (i >= XKB_MAX_LEDS) {
855                     log_err(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
856                             "Too many indicators (maximum is %d); "
857                             "Indicator name \"%s\" ignored\n",
858                             XKB_MAX_LEDS,
859                             xkb_atom_text(keymap->ctx, ledi->led.name));
860                     continue;
861                 }
862
863                 /* Add a new LED. */
864                 led = &keymap->leds[keymap->num_leds++];
865             }
866         }
867
868         *led = ledi->led;
869         if (led->groups != 0 && led->which_groups == 0)
870             led->which_groups = XKB_STATE_LAYOUT_EFFECTIVE;
871         if (led->mods.mods != 0 && led->which_mods == 0)
872             led->which_mods = XKB_STATE_MODS_EFFECTIVE;
873     }
874 }
875
876 static bool
877 CopyCompatToKeymap(struct xkb_keymap *keymap, CompatInfo *info)
878 {
879     keymap->compat_section_name = strdup_safe(info->name);
880     XkbEscapeMapName(keymap->compat_section_name);
881
882     keymap->mods = info->mods;
883
884     if (!darray_empty(info->interps)) {
885         struct collect collect;
886         darray_init(collect.sym_interprets);
887
888         /* Most specific to least specific. */
889         CopyInterps(info, true, MATCH_EXACTLY, &collect);
890         CopyInterps(info, true, MATCH_ALL, &collect);
891         CopyInterps(info, true, MATCH_NONE, &collect);
892         CopyInterps(info, true, MATCH_ANY, &collect);
893         CopyInterps(info, true, MATCH_ANY_OR_NONE, &collect);
894         CopyInterps(info, false, MATCH_EXACTLY, &collect);
895         CopyInterps(info, false, MATCH_ALL, &collect);
896         CopyInterps(info, false, MATCH_NONE, &collect);
897         CopyInterps(info, false, MATCH_ANY, &collect);
898         CopyInterps(info, false, MATCH_ANY_OR_NONE, &collect);
899
900         darray_steal(collect.sym_interprets,
901                      &keymap->sym_interprets, &keymap->num_sym_interprets);
902     }
903
904     CopyLedMapDefsToKeymap(keymap, info);
905
906     return true;
907 }
908
909 bool
910 CompileCompatMap(XkbFile *file, struct xkb_keymap *keymap,
911                  enum merge_mode merge)
912 {
913     CompatInfo info;
914     ActionsInfo *actions;
915
916     actions = NewActionsInfo();
917     if (!actions)
918         return false;
919
920     InitCompatInfo(&info, keymap->ctx, actions, &keymap->mods);
921     info.default_interp.merge = merge;
922     info.default_led.merge = merge;
923
924     HandleCompatMapFile(&info, file, merge);
925     if (info.errorCount != 0)
926         goto err_info;
927
928     if (!CopyCompatToKeymap(keymap, &info))
929         goto err_info;
930
931     ClearCompatInfo(&info);
932     FreeActionsInfo(actions);
933     return true;
934
935 err_info:
936     ClearCompatInfo(&info);
937     FreeActionsInfo(actions);
938     return false;
939 }