Store actions inside struct xkb_key
[platform/upstream/libxkbcommon.git] / src / xkbcomp / compat.c
1 /************************************************************
2  * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
3  *
4  * Permission to use, copy, modify, and distribute this
5  * software and its documentation for any purpose and without
6  * fee is hereby granted, provided that the above copyright
7  * notice appear in all copies and that both that copyright
8  * notice and this permission notice appear in supporting
9  * documentation, and that the name of Silicon Graphics not be
10  * used in advertising or publicity pertaining to distribution
11  * of the software without specific prior written permission.
12  * Silicon Graphics makes no representation about the suitability
13  * of this software for any purpose. It is provided "as is"
14  * without any express or implied warranty.
15  *
16  * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18  * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19  * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
23  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
24  *
25  ********************************************************/
26
27 #include "xkbcomp-priv.h"
28 #include "parseutils.h"
29 #include "action.h"
30 #include "vmod.h"
31
32 enum si_field {
33     SI_FIELD_VIRTUAL_MOD    = (1 << 0),
34     SI_FIELD_ACTION         = (1 << 1),
35     SI_FIELD_AUTO_REPEAT    = (1 << 2),
36     SI_FIELD_LOCKING_KEY    = (1 << 3),
37     SI_FIELD_LEVEL_ONE_ONLY = (1 << 4),
38 };
39
40 typedef struct _SymInterpInfo {
41     enum si_field defined;
42     unsigned file_id;
43     enum merge_mode merge;
44     struct list entry;
45
46     struct xkb_sym_interpret interp;
47 } SymInterpInfo;
48
49 enum led_field {
50     LED_FIELD_INDEX      = (1 << 0),
51     LED_FIELD_MODS       = (1 << 1),
52     LED_FIELD_GROUPS     = (1 << 2),
53     LED_FIELD_CTRLS      = (1 << 3),
54     LED_FIELD_EXPLICIT   = (1 << 4),
55     LED_FIELD_AUTOMATIC  = (1 << 5),
56     LED_FIELD_DRIVES_KBD = (1 << 6),
57 };
58
59 typedef struct _LEDInfo {
60     enum led_field defined;
61     unsigned file_id;
62     enum merge_mode merge;
63     struct list entry;
64
65     xkb_atom_t name;
66     xkb_led_index_t indicator;
67     unsigned char flags;
68     unsigned char which_mods;
69     xkb_mod_mask_t mods;
70     unsigned char which_groups;
71     uint32_t groups;
72     unsigned int ctrls;
73 } LEDInfo;
74
75 typedef struct _GroupCompatInfo {
76     unsigned file_id;
77     enum merge_mode merge;
78     bool defined;
79     xkb_mod_mask_t mods;
80 } GroupCompatInfo;
81
82 typedef struct _CompatInfo {
83     char *name;
84     unsigned file_id;
85     int errorCount;
86     int nInterps;
87     struct list interps;
88     SymInterpInfo dflt;
89     LEDInfo ledDflt;
90     GroupCompatInfo groupCompat[XkbNumKbdGroups];
91     struct list leds;
92     VModInfo vmods;
93     ActionInfo *act;
94     struct xkb_keymap *keymap;
95 } CompatInfo;
96
97 static const char *
98 siText(SymInterpInfo * si, CompatInfo * info)
99 {
100     static char buf[128];
101
102     if (si == &info->dflt) {
103         snprintf(buf, sizeof(buf), "default");
104     }
105     else {
106         snprintf(buf, sizeof(buf), "%s+%s(%s)",
107                  KeysymText(si->interp.sym),
108                  SIMatchText(si->interp.match),
109                  ModMaskText(si->interp.mods));
110     }
111     return buf;
112 }
113
114 static inline bool
115 ReportSINotArray(CompatInfo *info, SymInterpInfo *si, const char *field)
116 {
117     return ReportNotArray(info->keymap, "symbol interpretation", field,
118                           siText(si, info));
119 }
120
121 static inline bool
122 ReportSIBadType(CompatInfo *info, SymInterpInfo *si, const char *field,
123                 const char *wanted)
124 {
125     return ReportBadType(info->keymap->ctx, "symbol interpretation", field,
126                          siText(si, info), wanted);
127 }
128
129 static inline bool
130 ReportIndicatorBadType(CompatInfo *info, LEDInfo *led,
131                        const char *field, const char *wanted)
132 {
133     return ReportBadType(info->keymap->ctx, "indicator map", field,
134                          xkb_atom_text(info->keymap->ctx, led->name),
135                          wanted);
136 }
137
138 static inline bool
139 ReportIndicatorNotArray(CompatInfo *info, LEDInfo *led,
140                         const char *field)
141 {
142     return ReportNotArray(info->keymap, "indicator map", field,
143                           xkb_atom_text(info->keymap->ctx, led->name));
144 }
145
146 static void
147 ClearIndicatorMapInfo(struct xkb_context *ctx, LEDInfo * info)
148 {
149     info->name = xkb_atom_intern(ctx, "default");
150     info->indicator = XKB_LED_INVALID;
151     info->flags = 0;
152     info->which_mods = 0;
153     info->mods = 0;
154     info->which_groups = info->groups = 0;
155     info->ctrls = 0;
156 }
157
158 static void
159 InitCompatInfo(CompatInfo *info, struct xkb_keymap *keymap, unsigned file_id)
160 {
161     unsigned int i;
162
163     info->keymap = keymap;
164     info->name = NULL;
165     info->file_id = file_id;
166     info->errorCount = 0;
167     info->nInterps = 0;
168     list_init(&info->interps);
169     info->act = NULL;
170     info->dflt.file_id = file_id;
171     info->dflt.defined = 0;
172     info->dflt.merge = MERGE_OVERRIDE;
173     info->dflt.interp.flags = 0;
174     info->dflt.interp.virtual_mod = XkbNoModifier;
175     info->dflt.interp.act.type = XkbSA_NoAction;
176     for (i = 0; i < sizeof(info->dflt.interp.act.any.data); i++)
177         info->dflt.interp.act.any.data[i] = 0;
178     ClearIndicatorMapInfo(keymap->ctx, &info->ledDflt);
179     info->ledDflt.file_id = file_id;
180     info->ledDflt.defined = 0;
181     info->ledDflt.merge = MERGE_OVERRIDE;
182     memset(&info->groupCompat[0], 0,
183            XkbNumKbdGroups * sizeof(GroupCompatInfo));
184     list_init(&info->leds);
185     InitVModInfo(&info->vmods, keymap);
186 }
187
188 static void
189 ClearCompatInfo(CompatInfo *info)
190 {
191     unsigned int i;
192     ActionInfo *next_act;
193     SymInterpInfo *si, *next_si;
194     LEDInfo *led, *next_led;
195     struct xkb_keymap *keymap = info->keymap;
196
197     free(info->name);
198     info->name = NULL;
199     info->dflt.defined = 0;
200     info->dflt.merge = MERGE_AUGMENT;
201     info->dflt.interp.flags = 0;
202     info->dflt.interp.virtual_mod = XkbNoModifier;
203     info->dflt.interp.act.type = XkbSA_NoAction;
204     for (i = 0; i < sizeof(info->dflt.interp.act.any.data); i++)
205         info->dflt.interp.act.any.data[i] = 0;
206     ClearIndicatorMapInfo(keymap->ctx, &info->ledDflt);
207     info->nInterps = 0;
208     list_foreach_safe(si, next_si, &info->interps, entry)
209         free(si);
210     memset(&info->groupCompat[0], 0,
211            XkbNumKbdGroups * sizeof(GroupCompatInfo));
212     list_foreach_safe(led, next_led, &info->leds, entry)
213         free(led);
214     while (info->act) {
215         next_act = info->act->next;
216         free(info->act);
217         info->act = next_act;
218     }
219     info->keymap = NULL;
220     ClearVModInfo(&info->vmods, keymap);
221 }
222
223 static SymInterpInfo *
224 NextInterp(CompatInfo * info)
225 {
226     SymInterpInfo *si;
227
228     si = calloc(1, sizeof(*si));
229     if (!si)
230         return NULL;
231
232     list_append(&si->entry, &info->interps);
233     info->nInterps++;
234
235     return si;
236 }
237
238 static SymInterpInfo *
239 FindMatchingInterp(CompatInfo * info, SymInterpInfo * new)
240 {
241     SymInterpInfo *old;
242
243     list_foreach(old, &info->interps, entry)
244         if (old->interp.sym == new->interp.sym &&
245             old->interp.mods == new->interp.mods &&
246             old->interp.match == new->interp.match)
247             return old;
248
249     return NULL;
250 }
251
252 static bool
253 UseNewInterpField(enum si_field field, SymInterpInfo *old, SymInterpInfo *new,
254                   int verbosity, enum si_field *collide)
255 {
256     if (!(old->defined & field))
257         return true;
258
259     if (new->defined & field) {
260         if ((old->file_id == new->file_id && verbosity > 0) || verbosity > 9)
261             *collide |= field;
262
263         if (new->merge != MERGE_AUGMENT)
264             return true;
265     }
266
267     return false;
268 }
269
270 static bool
271 AddInterp(CompatInfo * info, SymInterpInfo * new)
272 {
273     enum si_field collide;
274     SymInterpInfo *old;
275     struct list entry;
276     int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
277
278     collide = 0;
279     old = FindMatchingInterp(info, new);
280     if (old != NULL) {
281         if (new->merge == MERGE_REPLACE) {
282             entry = old->entry;
283             if ((old->file_id == new->file_id && verbosity > 0) ||
284                 verbosity > 9)
285                 log_warn(info->keymap->ctx,
286                          "Multiple definitions for \"%s\"; "
287                          "Earlier interpretation ignored\n",
288                          siText(new, info));
289             *old = *new;
290             old->entry = entry;
291             return true;
292         }
293
294         if (UseNewInterpField(SI_FIELD_VIRTUAL_MOD, old, new, verbosity,
295                               &collide)) {
296             old->interp.virtual_mod = new->interp.virtual_mod;
297             old->defined |= SI_FIELD_VIRTUAL_MOD;
298         }
299         if (UseNewInterpField(SI_FIELD_ACTION, old, new, verbosity,
300                               &collide)) {
301             old->interp.act = new->interp.act;
302             old->defined |= SI_FIELD_ACTION;
303         }
304         if (UseNewInterpField(SI_FIELD_AUTO_REPEAT, old, new, verbosity,
305                               &collide)) {
306             old->interp.flags &= ~XkbSI_AutoRepeat;
307             old->interp.flags |= (new->interp.flags & XkbSI_AutoRepeat);
308             old->defined |= SI_FIELD_AUTO_REPEAT;
309         }
310         if (UseNewInterpField(SI_FIELD_LOCKING_KEY, old, new, verbosity,
311                               &collide)) {
312             old->interp.flags &= ~XkbSI_LockingKey;
313             old->interp.flags |= (new->interp.flags & XkbSI_LockingKey);
314             old->defined |= SI_FIELD_LOCKING_KEY;
315         }
316         if (UseNewInterpField(SI_FIELD_LEVEL_ONE_ONLY, old, new, verbosity,
317                               &collide)) {
318             old->interp.match &= ~XkbSI_LevelOneOnly;
319             old->interp.match |= (new->interp.match & XkbSI_LevelOneOnly);
320             old->defined |= SI_FIELD_LEVEL_ONE_ONLY;
321         }
322
323         if (collide) {
324             log_warn(info->keymap->ctx,
325                      "Multiple interpretations of \"%s\"; "
326                      "Using %s definition for duplicate fields\n",
327                      siText(new, info),
328                      (new->merge != MERGE_AUGMENT ? "last" : "first"));
329         }
330
331         return true;
332     }
333
334     old = new;
335     if ((new = NextInterp(info)) == NULL)
336         return false;
337     entry = new->entry;
338     *new = *old;
339     new->entry = entry;
340     return true;
341 }
342
343 static bool
344 AddGroupCompat(CompatInfo *info, xkb_group_index_t group, GroupCompatInfo *new)
345 {
346     GroupCompatInfo *gc;
347     int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
348
349     gc = &info->groupCompat[group];
350     if (gc->mods == new->mods)
351         return true;
352
353     if ((gc->file_id == new->file_id && verbosity > 0) || verbosity > 9)
354         log_warn(info->keymap->ctx,
355                  "Compat map for group %u redefined; "
356                  "Using %s definition\n",
357                  group + 1, (new->merge == MERGE_AUGMENT ? "old" : "new"));
358
359     if (new->defined && (new->merge != MERGE_AUGMENT || !gc->defined))
360         *gc = *new;
361
362     return true;
363 }
364
365 /***====================================================================***/
366
367 static bool
368 ResolveStateAndPredicate(ExprDef * expr,
369                          unsigned *pred_rtrn,
370                          xkb_mod_mask_t *mods_rtrn, CompatInfo * info)
371 {
372     if (expr == NULL) {
373         *pred_rtrn = XkbSI_AnyOfOrNone;
374         *mods_rtrn = ~0;
375         return true;
376     }
377
378     *pred_rtrn = XkbSI_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 (istreq(pred_txt, "noneof"))
383             *pred_rtrn = XkbSI_NoneOf;
384         else if (istreq(pred_txt, "anyofornone"))
385             *pred_rtrn = XkbSI_AnyOfOrNone;
386         else if (istreq(pred_txt, "anyof"))
387             *pred_rtrn = XkbSI_AnyOf;
388         else if (istreq(pred_txt, "allof"))
389             *pred_rtrn = XkbSI_AllOf;
390         else if (istreq(pred_txt, "exactly"))
391             *pred_rtrn = XkbSI_Exactly;
392         else {
393             log_err(info->keymap->ctx,
394                     "Illegal modifier predicate \"%s\"; Ignored\n", pred_txt);
395             return false;
396         }
397         expr = expr->value.action.args;
398     }
399     else if (expr->op == EXPR_IDENT) {
400         const char *pred_txt = xkb_atom_text(info->keymap->ctx,
401                                              expr->value.str);
402         if (pred_txt && istreq(pred_txt, "any")) {
403             *pred_rtrn = XkbSI_AnyOf;
404             *mods_rtrn = 0xff;
405             return true;
406         }
407     }
408
409     return ExprResolveModMask(info->keymap->ctx, expr, mods_rtrn);
410 }
411
412 /***====================================================================***/
413
414 static bool
415 UseNewLEDField(enum led_field field, LEDInfo *old, LEDInfo *new,
416                int verbosity, enum led_field *collide)
417 {
418     if (!(old->defined & field))
419         return true;
420
421     if (new->defined & field) {
422         if ((old->file_id == new->file_id && verbosity > 0) || verbosity > 9)
423             *collide |= field;
424
425         if (new->merge != MERGE_AUGMENT)
426             return true;
427     }
428
429     return false;
430 }
431
432 static bool
433 AddIndicatorMap(CompatInfo *info, LEDInfo *new)
434 {
435     LEDInfo *old;
436     enum led_field collide;
437     struct xkb_context *ctx = info->keymap->ctx;
438     int verbosity = xkb_get_log_verbosity(ctx);
439
440     list_foreach(old, &info->leds, entry) {
441         if (old->name == new->name) {
442             if ((old->mods == new->mods) &&
443                 (old->groups == new->groups) &&
444                 (old->ctrls == new->ctrls) &&
445                 (old->which_mods == new->which_mods) &&
446                 (old->which_groups == new->which_groups)) {
447                 old->defined |= new->defined;
448                 return true;
449             }
450
451             if (new->merge == MERGE_REPLACE) {
452                 struct list entry = old->entry;
453                 if ((old->file_id == new->file_id && verbosity > 0) ||
454                     verbosity > 9)
455                     log_warn(info->keymap->ctx,
456                              "Map for indicator %s redefined; "
457                              "Earlier definition ignored\n",
458                              xkb_atom_text(ctx, old->name));
459                 *old = *new;
460                 old->entry = entry;
461                 return true;
462             }
463
464             collide = 0;
465             if (UseNewLEDField(LED_FIELD_INDEX, old, new, verbosity,
466                                &collide)) {
467                 old->indicator = new->indicator;
468                 old->defined |= LED_FIELD_INDEX;
469             }
470             if (UseNewLEDField(LED_FIELD_MODS, old, new, verbosity,
471                                &collide)) {
472                 old->which_mods = new->which_mods;
473                 old->mods = new->mods;
474                 old->defined |= LED_FIELD_MODS;
475             }
476             if (UseNewLEDField(LED_FIELD_GROUPS, old, new, verbosity,
477                                &collide)) {
478                 old->which_groups = new->which_groups;
479                 old->groups = new->groups;
480                 old->defined |= LED_FIELD_GROUPS;
481             }
482             if (UseNewLEDField(LED_FIELD_CTRLS, old, new, verbosity,
483                                &collide)) {
484                 old->ctrls = new->ctrls;
485                 old->defined |= LED_FIELD_CTRLS;
486             }
487             if (UseNewLEDField(LED_FIELD_EXPLICIT, old, new, verbosity,
488                                &collide)) {
489                 old->flags &= ~XkbIM_NoExplicit;
490                 old->flags |= (new->flags & XkbIM_NoExplicit);
491                 old->defined |= LED_FIELD_EXPLICIT;
492             }
493             if (UseNewLEDField(LED_FIELD_AUTOMATIC, old, new, verbosity,
494                                &collide)) {
495                 old->flags &= ~XkbIM_NoAutomatic;
496                 old->flags |= (new->flags & XkbIM_NoAutomatic);
497                 old->defined |= LED_FIELD_AUTOMATIC;
498             }
499             if (UseNewLEDField(LED_FIELD_DRIVES_KBD, old, new, verbosity,
500                                &collide)) {
501                 old->flags &= ~XkbIM_LEDDrivesKB;
502                 old->flags |= (new->flags & XkbIM_LEDDrivesKB);
503                 old->defined |= LED_FIELD_DRIVES_KBD;
504             }
505
506             if (collide) {
507                 log_warn(info->keymap->ctx,
508                          "Map for indicator %s redefined; "
509                          "Using %s definition for duplicate fields\n",
510                          xkb_atom_text(ctx, old->name),
511                          (new->merge == MERGE_AUGMENT ? "first" : "last"));
512             }
513
514             return true;
515         }
516     }
517
518     /* new definition */
519     old = malloc(sizeof(*old));
520     if (!old) {
521         log_wsgo(info->keymap->ctx,
522                  "Couldn't allocate indicator map; "
523                  "Map for indicator %s not compiled\n",
524                  xkb_atom_text(ctx, new->name));
525         return false;
526     }
527
528     *old = *new;
529     list_append(&old->entry, &info->leds);
530
531     return true;
532 }
533
534 static void
535 MergeIncludedCompatMaps(CompatInfo * into, CompatInfo * from,
536                         enum merge_mode merge)
537 {
538     SymInterpInfo *si;
539     LEDInfo *led, *next_led;
540     GroupCompatInfo *gcm;
541     xkb_group_index_t i;
542
543     if (from->errorCount > 0) {
544         into->errorCount += from->errorCount;
545         return;
546     }
547     if (into->name == NULL) {
548         into->name = from->name;
549         from->name = NULL;
550     }
551
552     list_foreach(si, &from->interps, entry) {
553         if (merge != MERGE_DEFAULT)
554             si->merge = merge;
555         if (!AddInterp(into, si))
556             into->errorCount++;
557     }
558
559     for (i = 0, gcm = &from->groupCompat[0]; i < XkbNumKbdGroups;
560          i++, gcm++) {
561         if (merge != MERGE_DEFAULT)
562             gcm->merge = merge;
563         if (!AddGroupCompat(into, i, gcm))
564             into->errorCount++;
565     }
566
567     list_foreach_safe(led, next_led, &from->leds, entry) {
568         led->merge = (merge == MERGE_DEFAULT ? led->merge : merge);
569         if (!AddIndicatorMap(into, led))
570             into->errorCount++;
571     }
572 }
573
574 static void
575 HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge);
576
577 static bool
578 HandleIncludeCompatMap(CompatInfo *info, IncludeStmt *stmt)
579 {
580     enum merge_mode merge = MERGE_DEFAULT;
581     XkbFile *rtrn;
582     CompatInfo included, next_incl;
583
584     InitCompatInfo(&included, info->keymap, info->file_id);
585     if (stmt->stmt) {
586         free(included.name);
587         included.name = stmt->stmt;
588         stmt->stmt = NULL;
589     }
590
591     for (; stmt; stmt = stmt->next_incl) {
592         if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_COMPAT,
593                                 &rtrn, &merge)) {
594             info->errorCount += 10;
595             ClearCompatInfo(&included);
596             return false;
597         }
598
599         InitCompatInfo(&next_incl, info->keymap, rtrn->id);
600         next_incl.file_id = rtrn->id;
601         next_incl.dflt = info->dflt;
602         next_incl.dflt.file_id = rtrn->id;
603         next_incl.dflt.merge = merge;
604         next_incl.ledDflt.file_id = rtrn->id;
605         next_incl.ledDflt.merge = merge;
606         next_incl.act = info->act;
607
608         HandleCompatMapFile(&next_incl, rtrn, MERGE_OVERRIDE);
609
610         MergeIncludedCompatMaps(&included, &next_incl, merge);
611         if (info->act)
612             next_incl.act = NULL;
613
614         ClearCompatInfo(&next_incl);
615         FreeXKBFile(rtrn);
616     }
617
618     MergeIncludedCompatMaps(info, &included, merge);
619     ClearCompatInfo(&included);
620
621     return (info->errorCount == 0);
622 }
623
624 static const LookupEntry useModMapValues[] = {
625     { "levelone", 1 },
626     { "level1", 1 },
627     { "anylevel", 0 },
628     { "any", 0 },
629     { NULL, 0 }
630 };
631
632 static int
633 SetInterpField(CompatInfo *info, SymInterpInfo *si, const char *field,
634                ExprDef *arrayNdx, ExprDef *value)
635 {
636     struct xkb_keymap *keymap = info->keymap;
637     xkb_mod_index_t ndx;
638
639     if (istreq(field, "action")) {
640         if (arrayNdx)
641             return ReportSINotArray(info, si, field);
642
643         if (!HandleActionDef(value, keymap, &si->interp.act.any, info->act))
644             return false;
645
646         si->defined |= SI_FIELD_ACTION;
647     }
648     else if (istreq(field, "virtualmodifier") ||
649              istreq(field, "virtualmod")) {
650         if (arrayNdx)
651             return ReportSINotArray(info, si, field);
652
653         if (!ResolveVirtualModifier(value, keymap, &ndx, &info->vmods))
654             return ReportSIBadType(info, si, field, "virtual modifier");
655
656         si->interp.virtual_mod = ndx;
657         si->defined |= SI_FIELD_VIRTUAL_MOD;
658     }
659     else if (istreq(field, "repeat")) {
660         bool set;
661
662         if (arrayNdx)
663             return ReportSINotArray(info, si, field);
664
665         if (!ExprResolveBoolean(keymap->ctx, value, &set))
666             return ReportSIBadType(info, si, field, "boolean");
667
668         if (set)
669             si->interp.flags |= XkbSI_AutoRepeat;
670         else
671             si->interp.flags &= ~XkbSI_AutoRepeat;
672
673         si->defined |= SI_FIELD_AUTO_REPEAT;
674     }
675     else if (istreq(field, "locking")) {
676         bool set;
677
678         if (arrayNdx)
679             return ReportSINotArray(info, si, field);
680
681         if (!ExprResolveBoolean(keymap->ctx, value, &set))
682             return ReportSIBadType(info, si, field, "boolean");
683
684         if (set)
685             si->interp.flags |= XkbSI_LockingKey;
686         else
687             si->interp.flags &= ~XkbSI_LockingKey;
688
689         si->defined |= SI_FIELD_LOCKING_KEY;
690     }
691     else if (istreq(field, "usemodmap") ||
692              istreq(field, "usemodmapmods")) {
693         unsigned int val;
694
695         if (arrayNdx)
696             return ReportSINotArray(info, si, field);
697
698         if (!ExprResolveEnum(keymap->ctx, value, &val, useModMapValues))
699             return ReportSIBadType(info, si, field, "level specification");
700
701         if (val)
702             si->interp.match |= XkbSI_LevelOneOnly;
703         else
704             si->interp.match &= ~XkbSI_LevelOneOnly;
705
706         si->defined |= SI_FIELD_LEVEL_ONE_ONLY;
707     }
708     else {
709         return ReportBadField(keymap, "symbol interpretation", field,
710                               siText(si, info));
711     }
712
713     return true;
714 }
715
716 static const LookupEntry modComponentNames[] = {
717     {"base", XkbIM_UseBase},
718     {"latched", XkbIM_UseLatched},
719     {"locked", XkbIM_UseLocked},
720     {"effective", XkbIM_UseEffective},
721     {"compat", XkbIM_UseCompat},
722     {"any", XkbIM_UseAnyMods},
723     {"none", 0},
724     {NULL, 0}
725 };
726 static const LookupEntry groupComponentNames[] = {
727     {"base", XkbIM_UseBase},
728     {"latched", XkbIM_UseLatched},
729     {"locked", XkbIM_UseLocked},
730     {"effective", XkbIM_UseEffective},
731     {"any", XkbIM_UseAnyGroup},
732     {"none", 0},
733     {NULL, 0}
734 };
735
736 static const LookupEntry groupNames[] = {
737     {"group1", 0x01},
738     {"group2", 0x02},
739     {"group3", 0x04},
740     {"group4", 0x08},
741     {"group5", 0x10},
742     {"group6", 0x20},
743     {"group7", 0x40},
744     {"group8", 0x80},
745     {"none", 0x00},
746     {"all", 0xff},
747     {NULL, 0}
748 };
749
750 static int
751 SetIndicatorMapField(CompatInfo *info, LEDInfo *led,
752                      const char *field, ExprDef *arrayNdx, ExprDef *value)
753 {
754     bool ok = true;
755     struct xkb_keymap *keymap = info->keymap;
756
757     if (istreq(field, "modifiers") || istreq(field, "mods")) {
758         if (arrayNdx)
759             return ReportIndicatorNotArray(info, led, field);
760
761         if (!ExprResolveVModMask(keymap, value, &led->mods))
762             return ReportIndicatorBadType(info, led, field, "modifier mask");
763
764         led->defined |= LED_FIELD_MODS;
765     }
766     else if (istreq(field, "groups")) {
767         unsigned int mask;
768
769         if (arrayNdx)
770             return ReportIndicatorNotArray(info, led, field);
771
772         if (!ExprResolveMask(keymap->ctx, value, &mask, groupNames))
773             return ReportIndicatorBadType(info, led, field, "group mask");
774
775         led->groups = mask;
776         led->defined |= LED_FIELD_GROUPS;
777     }
778     else if (istreq(field, "controls") || istreq(field, "ctrls")) {
779         unsigned int mask;
780
781         if (arrayNdx)
782             return ReportIndicatorNotArray(info, led, field);
783
784         if (!ExprResolveMask(keymap->ctx, value, &mask, ctrlNames))
785             return ReportIndicatorBadType(info, led, field,
786                                           "controls mask");
787
788         led->ctrls = mask;
789         led->defined |= LED_FIELD_CTRLS;
790     }
791     else if (istreq(field, "allowexplicit")) {
792         bool set;
793
794         if (arrayNdx)
795             return ReportIndicatorNotArray(info, led, field);
796
797         if (!ExprResolveBoolean(keymap->ctx, value, &set))
798             return ReportIndicatorBadType(info, led, field, "boolean");
799
800         if (set)
801             led->flags &= ~XkbIM_NoExplicit;
802         else
803             led->flags |= XkbIM_NoExplicit;
804
805         led->defined |= LED_FIELD_EXPLICIT;
806     }
807     else if (istreq(field, "whichmodstate") ||
808              istreq(field, "whichmodifierstate")) {
809         unsigned int mask;
810
811         if (arrayNdx)
812             return ReportIndicatorNotArray(info, led, field);
813
814         if (!ExprResolveMask(keymap->ctx, value, &mask, modComponentNames))
815             return ReportIndicatorBadType(info, led, field,
816                                           "mask of modifier state components");
817
818         led->which_mods = mask;
819     }
820     else if (istreq(field, "whichgroupstate")) {
821         unsigned mask;
822
823         if (arrayNdx)
824             return ReportIndicatorNotArray(info, led, field);
825
826         if (!ExprResolveMask(keymap->ctx, value, &mask, groupComponentNames))
827             return ReportIndicatorBadType(info, led, field,
828                                           "mask of group state components");
829
830         led->which_groups = mask;
831     }
832     else if (istreq(field, "driveskbd") ||
833              istreq(field, "driveskeyboard") ||
834              istreq(field, "leddriveskbd") ||
835              istreq(field, "leddriveskeyboard") ||
836              istreq(field, "indicatordriveskbd") ||
837              istreq(field, "indicatordriveskeyboard")) {
838         bool set;
839
840         if (arrayNdx)
841             return ReportIndicatorNotArray(info, led, field);
842
843         if (!ExprResolveBoolean(keymap->ctx, value, &set))
844             return ReportIndicatorBadType(info, led, field, "boolean");
845
846         if (set)
847             led->flags |= XkbIM_LEDDrivesKB;
848         else
849             led->flags &= ~XkbIM_LEDDrivesKB;
850
851         led->defined |= LED_FIELD_DRIVES_KBD;
852     }
853     else if (istreq(field, "index")) {
854         int ndx;
855
856         if (arrayNdx)
857             return ReportIndicatorNotArray(info, led, field);
858
859         if (!ExprResolveInteger(keymap->ctx, value, &ndx))
860             return ReportIndicatorBadType(info, led, field,
861                                           "indicator index");
862
863         if (ndx < 1 || ndx > 32) {
864             log_err(info->keymap->ctx,
865                     "Illegal indicator index %d (range 1..%d); "
866                     "Index definition for %s indicator ignored\n",
867                     ndx, XkbNumIndicators,
868                     xkb_atom_text(keymap->ctx, led->name));
869             return false;
870         }
871
872         led->indicator = (xkb_led_index_t) ndx;
873         led->defined |= LED_FIELD_INDEX;
874     }
875     else {
876         log_err(info->keymap->ctx,
877                 "Unknown field %s in map for %s indicator; "
878                 "Definition ignored\n",
879                 field, xkb_atom_text(keymap->ctx, led->name));
880         ok = false;
881     }
882
883     return ok;
884 }
885
886 static int
887 HandleInterpVar(CompatInfo *info, VarDef *stmt)
888 {
889     const char *elem, *field;
890     ExprDef *ndx;
891     int ret;
892
893     if (!ExprResolveLhs(info->keymap->ctx, stmt->name, &elem, &field, &ndx))
894         ret = 0;               /* internal error, already reported */
895     else if (elem && istreq(elem, "interpret"))
896         ret = SetInterpField(info, &info->dflt, field, ndx, stmt->value);
897     else if (elem && istreq(elem, "indicator"))
898         ret = SetIndicatorMapField(info, &info->ledDflt, field, ndx,
899                                    stmt->value);
900     else
901         ret = SetActionField(info->keymap, elem, field, ndx, stmt->value,
902                              &info->act);
903     return ret;
904 }
905
906 static int
907 HandleInterpBody(CompatInfo *info, VarDef *def, SymInterpInfo *si)
908 {
909     int ok = 1;
910     const char *elem, *field;
911     ExprDef *arrayNdx;
912
913     for (; def != NULL; def = (VarDef *) def->common.next) {
914         if (def->name && def->name->op == EXPR_FIELD_REF) {
915             ok = HandleInterpVar(info, def);
916             continue;
917         }
918         ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
919                             &arrayNdx);
920         if (ok) {
921             ok = SetInterpField(info, si, field, arrayNdx, def->value);
922         }
923     }
924     return ok;
925 }
926
927 static int
928 HandleInterpDef(CompatInfo *info, InterpDef *def, enum merge_mode merge)
929 {
930     unsigned pred, mods;
931     SymInterpInfo si;
932
933     if (!ResolveStateAndPredicate(def->match, &pred, &mods, info)) {
934         log_err(info->keymap->ctx,
935                 "Couldn't determine matching modifiers; "
936                 "Symbol interpretation ignored\n");
937         return false;
938     }
939     if (def->merge != MERGE_DEFAULT)
940         merge = def->merge;
941
942     si = info->dflt;
943     si.merge = merge;
944     if (!LookupKeysym(def->sym, &si.interp.sym)) {
945         log_err(info->keymap->ctx,
946                 "Could not resolve keysym %s; "
947                 "Symbol interpretation ignored\n",
948                 def->sym);
949         return false;
950     }
951     si.interp.match = pred & XkbSI_OpMask;
952     si.interp.mods = mods;
953     if (!HandleInterpBody(info, def->def, &si)) {
954         info->errorCount++;
955         return false;
956     }
957
958     if (!AddInterp(info, &si)) {
959         info->errorCount++;
960         return false;
961     }
962     return true;
963 }
964
965 static int
966 HandleGroupCompatDef(CompatInfo *info, GroupCompatDef *def,
967                      enum merge_mode merge)
968 {
969     GroupCompatInfo tmp;
970
971     merge = (def->merge == MERGE_DEFAULT ? merge : def->merge);
972
973     if (def->group < 1 || def->group > XkbNumKbdGroups) {
974         log_err(info->keymap->ctx,
975                 "Keyboard group must be in the range 1..%u; "
976                 "Compatibility map for illegal group %u ignored\n",
977                 XkbNumKbdGroups, def->group);
978         return false;
979     }
980
981     tmp.file_id = info->file_id;
982     tmp.merge = merge;
983
984     if (!ExprResolveVModMask(info->keymap, def->def, &tmp.mods)) {
985         log_err(info->keymap->ctx,
986                 "Expected a modifier mask in group compatibility definition; "
987                 "Ignoring illegal compatibility map for group %u\n",
988                 def->group);
989         return false;
990     }
991
992     tmp.defined = true;
993     return AddGroupCompat(info, def->group - 1, &tmp);
994 }
995
996 static bool
997 HandleIndicatorMapDef(CompatInfo *info, IndicatorMapDef *def,
998                       enum merge_mode merge)
999 {
1000     LEDInfo led;
1001     VarDef *var;
1002     bool ok;
1003
1004     if (def->merge != MERGE_DEFAULT)
1005         merge = def->merge;
1006
1007     led = info->ledDflt;
1008     led.merge = merge;
1009     led.name = def->name;
1010
1011     ok = true;
1012     for (var = def->body; var != NULL; var = (VarDef *) var->common.next) {
1013         const char *elem, *field;
1014         ExprDef *arrayNdx;
1015         if (!ExprResolveLhs(info->keymap->ctx, var->name, &elem, &field,
1016                             &arrayNdx)) {
1017             ok = false;
1018             continue;
1019         }
1020
1021         if (elem) {
1022             log_err(info->keymap->ctx,
1023                     "Cannot set defaults for \"%s\" element in indicator map; "
1024                     "Assignment to %s.%s ignored\n", elem, elem, field);
1025             ok = false;
1026         }
1027         else {
1028             ok = SetIndicatorMapField(info, &led, field, arrayNdx,
1029                                       var->value) && ok;
1030         }
1031     }
1032
1033     if (ok)
1034         return AddIndicatorMap(info, &led);
1035
1036     return false;
1037 }
1038
1039 static void
1040 HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge)
1041 {
1042     bool ok;
1043     ParseCommon *stmt;
1044
1045     merge = (merge == MERGE_DEFAULT ? MERGE_AUGMENT : merge);
1046
1047     free(info->name);
1048     info->name = strdup_safe(file->name);
1049
1050     for (stmt = file->defs; stmt; stmt = stmt->next) {
1051         switch (stmt->type) {
1052         case STMT_INCLUDE:
1053             ok = HandleIncludeCompatMap(info, (IncludeStmt *) stmt);
1054             break;
1055         case STMT_INTERP:
1056             ok = HandleInterpDef(info, (InterpDef *) stmt, merge);
1057             break;
1058         case STMT_GROUP_COMPAT:
1059             ok = HandleGroupCompatDef(info, (GroupCompatDef *) stmt, merge);
1060             break;
1061         case STMT_INDICATOR_MAP:
1062             ok = HandleIndicatorMapDef(info, (IndicatorMapDef *) stmt, merge);
1063             break;
1064         case STMT_VAR:
1065             ok = HandleInterpVar(info, (VarDef *) stmt);
1066             break;
1067         case STMT_VMOD:
1068             ok = HandleVModDef((VModDef *) stmt, info->keymap, merge,
1069                                &info->vmods);
1070             break;
1071         default:
1072             log_err(info->keymap->ctx,
1073                     "Interpretation files may not include other types; "
1074                     "Ignoring %s\n", StmtTypeToString(stmt->type));
1075             ok = false;
1076             break;
1077         }
1078
1079         if (!ok)
1080             info->errorCount++;
1081
1082         if (info->errorCount > 10) {
1083             log_err(info->keymap->ctx,
1084                     "Abandoning compatibility map \"%s\"\n", file->topName);
1085             break;
1086         }
1087     }
1088 }
1089
1090 static void
1091 CopyInterps(CompatInfo *info, bool needSymbol, unsigned pred)
1092 {
1093     SymInterpInfo *si;
1094
1095     list_foreach(si, &info->interps, entry) {
1096         if (((si->interp.match & XkbSI_OpMask) != pred) ||
1097             (needSymbol && si->interp.sym == XKB_KEY_NoSymbol) ||
1098             (!needSymbol && si->interp.sym != XKB_KEY_NoSymbol))
1099             continue;
1100
1101         darray_append(info->keymap->sym_interpret, si->interp);
1102     }
1103 }
1104
1105 static void
1106 BindIndicators(CompatInfo *info, struct list *unbound_leds)
1107 {
1108     xkb_led_index_t i;
1109     LEDInfo *led, *next_led;
1110     struct xkb_indicator_map *map;
1111     struct xkb_keymap *keymap = info->keymap;
1112
1113     list_foreach(led, unbound_leds, entry) {
1114         if (led->indicator == XKB_LED_INVALID) {
1115             for (i = 0; i < XkbNumIndicators; i++) {
1116                 if (keymap->indicator_names[i] &&
1117                     streq(keymap->indicator_names[i],
1118                           xkb_atom_text(keymap->ctx, led->name))) {
1119                     led->indicator = i + 1;
1120                     break;
1121                 }
1122             }
1123         }
1124     }
1125
1126     list_foreach(led, unbound_leds, entry) {
1127         if (led->indicator == XKB_LED_INVALID) {
1128             for (i = 0; i < XkbNumIndicators; i++) {
1129                 if (keymap->indicator_names[i] == NULL) {
1130                     keymap->indicator_names[i] =
1131                         xkb_atom_text(keymap->ctx, led->name);
1132                     led->indicator = i + 1;
1133                     break;
1134                 }
1135             }
1136
1137             if (led->indicator == XKB_LED_INVALID) {
1138                 log_err(info->keymap->ctx,
1139                         "No unnamed indicators found; "
1140                         "Virtual indicator map \"%s\" not bound\n",
1141                         xkb_atom_text(keymap->ctx, led->name));
1142                 continue;
1143             }
1144         }
1145     }
1146
1147     list_foreach_safe(led, next_led, unbound_leds, entry) {
1148         if (led->indicator == XKB_LED_INVALID) {
1149             free(led);
1150             continue;
1151         }
1152
1153         if (!streq(keymap->indicator_names[led->indicator - 1],
1154                    xkb_atom_text(keymap->ctx, led->name))) {
1155             const char *old = keymap->indicator_names[led->indicator - 1];
1156             log_err(info->keymap->ctx,
1157                     "Multiple names bound to indicator %d; "
1158                     "Using %s, ignoring %s\n",
1159                     led->indicator, old,
1160                     xkb_atom_text(keymap->ctx, led->name));
1161             free(led);
1162             continue;
1163         }
1164
1165         map = &keymap->indicators[led->indicator - 1];
1166         map->flags = led->flags;
1167         map->which_groups = led->which_groups;
1168         map->groups = led->groups;
1169         map->which_mods = led->which_mods;
1170         map->mods.mods = led->mods;
1171         map->ctrls = led->ctrls;
1172         free(led);
1173     }
1174
1175     list_init(unbound_leds);
1176 }
1177
1178 static bool
1179 CopyIndicatorMapDefs(CompatInfo *info)
1180 {
1181     LEDInfo *led, *next_led;
1182     struct list unbound_leds;
1183     struct xkb_indicator_map *im;
1184     struct xkb_keymap *keymap = info->keymap;
1185
1186     list_init(&unbound_leds);
1187
1188     list_foreach_safe(led, next_led, &info->leds, entry) {
1189         if (led->groups != 0 && led->which_groups == 0)
1190             led->which_groups = XkbIM_UseEffective;
1191
1192         if (led->which_mods == 0 && led->mods)
1193             led->which_mods = XkbIM_UseEffective;
1194
1195         if (led->indicator == XKB_LED_INVALID) {
1196             list_append(&led->entry, &unbound_leds);
1197             continue;
1198         }
1199
1200         im = &keymap->indicators[led->indicator - 1];
1201         im->flags = led->flags;
1202         im->which_groups = led->which_groups;
1203         im->groups = led->groups;
1204         im->which_mods = led->which_mods;
1205         im->mods.mods = led->mods;
1206         im->ctrls = led->ctrls;
1207         keymap->indicator_names[led->indicator - 1] =
1208             xkb_atom_text(keymap->ctx, led->name);
1209         free(led);
1210     }
1211     list_init(&info->leds);
1212
1213     BindIndicators(info, &unbound_leds);
1214
1215     return true;
1216 }
1217
1218 bool
1219 CompileCompatMap(XkbFile *file, struct xkb_keymap *keymap,
1220                  enum merge_mode merge)
1221 {
1222     xkb_group_index_t i;
1223     CompatInfo info;
1224     GroupCompatInfo *gcm;
1225
1226     InitCompatInfo(&info, keymap, file->id);
1227     info.dflt.merge = merge;
1228     info.ledDflt.merge = merge;
1229
1230     HandleCompatMapFile(&info, file, merge);
1231
1232     if (info.errorCount != 0)
1233         goto err_info;
1234
1235     darray_init(keymap->sym_interpret);
1236     darray_growalloc(keymap->sym_interpret, info.nInterps);
1237
1238     if (info.name)
1239         keymap->compat_section_name = strdup(info.name);
1240
1241     if (info.nInterps > 0) {
1242         CopyInterps(&info, true, XkbSI_Exactly);
1243         CopyInterps(&info, true, XkbSI_AllOf | XkbSI_NoneOf);
1244         CopyInterps(&info, true, XkbSI_AnyOf);
1245         CopyInterps(&info, true, XkbSI_AnyOfOrNone);
1246         CopyInterps(&info, false, XkbSI_Exactly);
1247         CopyInterps(&info, false, XkbSI_AllOf | XkbSI_NoneOf);
1248         CopyInterps(&info, false, XkbSI_AnyOf);
1249         CopyInterps(&info, false, XkbSI_AnyOfOrNone);
1250     }
1251
1252     for (i = 0, gcm = &info.groupCompat[0]; i < XkbNumKbdGroups;
1253          i++, gcm++) {
1254         if (gcm->file_id != 0 || gcm->mods != 0)
1255             keymap->groups[i].mods = gcm->mods;
1256     }
1257
1258     if (!CopyIndicatorMapDefs(&info))
1259         info.errorCount++;
1260
1261     ClearCompatInfo(&info);
1262     return true;
1263
1264 err_info:
1265     ClearCompatInfo(&info);
1266     return false;
1267 }
1268
1269 static void
1270 ComputeEffectiveMask(struct xkb_keymap *keymap, struct xkb_mods *mods)
1271 {
1272     xkb_mod_index_t i;
1273     xkb_mod_mask_t vmask = mods->mods >> XkbNumModifiers;
1274
1275     /* The effective mask is only real mods for now. */
1276     mods->mask = mods->mods & 0xff;
1277
1278     for (i = 0; i < XkbNumVirtualMods; i++) {
1279         if (!(vmask & (1 << i)))
1280             continue;
1281         mods->mask |= keymap->vmods[i];
1282     }
1283 }
1284
1285 static void
1286 UpdateActionMods(struct xkb_keymap *keymap, union xkb_action *act,
1287                  xkb_mod_mask_t rmodmask)
1288 {
1289     unsigned int flags;
1290     struct xkb_mods *mods;
1291
1292     switch (act->type) {
1293     case XkbSA_SetMods:
1294     case XkbSA_LatchMods:
1295     case XkbSA_LockMods:
1296         flags = act->mods.flags;
1297         mods = &act->mods.mods;
1298         break;
1299
1300     case XkbSA_ISOLock:
1301         flags = act->iso.flags;
1302         mods = &act->iso.mods;
1303         break;
1304
1305     default:
1306         return;
1307     }
1308
1309     if (flags & XkbSA_UseModMapMods) {
1310         /* XXX: what's that. */
1311         mods->mods &= 0xff;
1312         mods->mods |= rmodmask;
1313     }
1314     ComputeEffectiveMask(keymap, mods);
1315 }
1316
1317 /**
1318  * Find an interpretation which applies to this particular level, either by
1319  * finding an exact match for the symbol and modifier combination, or a
1320  * generic XKB_KEY_NoSymbol match.
1321  */
1322 static struct xkb_sym_interpret *
1323 FindInterpForKey(struct xkb_keymap *keymap, struct xkb_key *key,
1324                  xkb_group_index_t group, xkb_level_index_t level)
1325 {
1326     struct xkb_sym_interpret *ret = NULL;
1327     struct xkb_sym_interpret *interp;
1328     const xkb_keysym_t *syms;
1329     int num_syms;
1330
1331     num_syms = xkb_key_get_syms_by_level(keymap, key, group, level, &syms);
1332     if (num_syms == 0)
1333         return NULL;
1334
1335     darray_foreach(interp, keymap->sym_interpret) {
1336         uint32_t mods;
1337         bool found;
1338
1339         if ((num_syms > 1 || interp->sym != syms[0]) &&
1340             interp->sym != XKB_KEY_NoSymbol)
1341             continue;
1342
1343         if (level == 0 || !(interp->match & XkbSI_LevelOneOnly))
1344             mods = key->modmap;
1345         else
1346             mods = 0;
1347
1348         switch (interp->match & XkbSI_OpMask) {
1349         case XkbSI_NoneOf:
1350             found = !(interp->mods & mods);
1351             break;
1352         case XkbSI_AnyOfOrNone:
1353             found = (!mods || (interp->mods & mods));
1354             break;
1355         case XkbSI_AnyOf:
1356             found = !!(interp->mods & mods);
1357             break;
1358         case XkbSI_AllOf:
1359             found = ((interp->mods & mods) == interp->mods);
1360             break;
1361         case XkbSI_Exactly:
1362             found = (interp->mods == mods);
1363             break;
1364         default:
1365             found = false;
1366             break;
1367         }
1368
1369         if (found && interp->sym != XKB_KEY_NoSymbol)
1370             return interp;
1371         else if (found && !ret)
1372             ret = interp;
1373     }
1374
1375     return ret;
1376 }
1377
1378 /**
1379  */
1380 static bool
1381 ApplyInterpsToKey(struct xkb_keymap *keymap, struct xkb_key *key)
1382 {
1383 #define INTERP_SIZE (8 * 4)
1384     struct xkb_sym_interpret *interps[INTERP_SIZE];
1385     xkb_mod_mask_t vmodmask = 0;
1386     int num_acts = 0;
1387     xkb_group_index_t group;
1388     xkb_level_index_t level;
1389     unsigned int i;
1390
1391     /* If we've been told not to bind interps to this key, then don't. */
1392     if (key->explicit & XkbExplicitInterpretMask)
1393         return true;
1394
1395     for (i = 0; i < INTERP_SIZE; i++)
1396         interps[i] = NULL;
1397
1398     for (group = 0; group < key->num_groups; group++) {
1399         for (level = 0; level < XkbKeyGroupWidth(keymap, key, group);
1400              level++) {
1401             i = (group * key->width) + level;
1402             if (i >= INTERP_SIZE) /* XXX FIXME */
1403                 return false;
1404             interps[i] = FindInterpForKey(keymap, key, group, level);
1405             if (interps[i])
1406                 num_acts++;
1407         }
1408     }
1409
1410     if (num_acts && !key->actions) {
1411         key->actions = calloc(key->num_groups * key->width,
1412                               sizeof(*key->actions));
1413         if (!key->actions)
1414             return false;
1415     }
1416
1417     for (group = 0; group < key->num_groups; group++) {
1418         for (level = 0; level < XkbKeyGroupWidth(keymap, key, group);
1419              level++) {
1420             struct xkb_sym_interpret *interp;
1421
1422             i = (group * key->width) + level;
1423             interp = interps[i];
1424
1425             /* Infer default key behaviours from the base level. */
1426             if (group == 0 && level == 0) {
1427                 if (!(key->explicit & XkbExplicitAutoRepeatMask) &&
1428                     (!interp || (interp->flags & XkbSI_AutoRepeat)))
1429                     key->repeats = true;
1430             }
1431
1432             if (!interp)
1433                 continue;
1434
1435             if ((group == 0 && level == 0) ||
1436                 !(interp->match & XkbSI_LevelOneOnly)) {
1437                 if (interp->virtual_mod != XkbNoModifier)
1438                     vmodmask |= (1 << interp->virtual_mod);
1439             }
1440
1441             key->actions[i] = interp->act;
1442         }
1443     }
1444
1445     if (!(key->explicit & XkbExplicitVModMapMask))
1446         key->vmodmap = vmodmask;
1447
1448     return true;
1449 #undef INTERP_SIZE
1450 }
1451
1452 /**
1453  * This collects a bunch of disparate functions which was done in the server
1454  * at various points that really should've been done within xkbcomp.  Turns out
1455  * your actions and types are a lot more useful when any of your modifiers
1456  * other than Shift actually do something ...
1457  */
1458 bool
1459 UpdateModifiersFromCompat(struct xkb_keymap *keymap)
1460 {
1461     xkb_mod_index_t vmod;
1462     xkb_group_index_t grp;
1463     xkb_led_index_t led;
1464     unsigned int i, j;
1465     struct xkb_key *key;
1466
1467     /* Find all the interprets for the key and bind them to actions,
1468      * which will also update the vmodmap. */
1469     xkb_foreach_key(key, keymap)
1470         if (!ApplyInterpsToKey(keymap, key))
1471             return false;
1472
1473     /* Update keymap->vmods, the virtual -> real mod mapping. */
1474     for (vmod = 0; vmod < XkbNumVirtualMods; vmod++)
1475         keymap->vmods[vmod] = 0;
1476
1477     xkb_foreach_key(key, keymap) {
1478         if (!key->vmodmap)
1479             continue;
1480
1481         for (vmod = 0; vmod < XkbNumVirtualMods; vmod++) {
1482             if (!(key->vmodmap & (1 << vmod)))
1483                 continue;
1484             keymap->vmods[vmod] |= key->modmap;
1485         }
1486     }
1487
1488     /* Now update the level masks for all the types to reflect the vmods. */
1489     for (i = 0; i < keymap->num_types; i++) {
1490         ComputeEffectiveMask(keymap, &keymap->types[i].mods);
1491
1492         for (j = 0; j < keymap->types[i].num_entries; j++) {
1493             ComputeEffectiveMask(keymap, &keymap->types[i].map[j].mods);
1494             ComputeEffectiveMask(keymap, &keymap->types[i].map[j].preserve);
1495         }
1496     }
1497
1498     /* Update action modifiers. */
1499     xkb_foreach_key(key, keymap) {
1500         if (!key->actions)
1501             continue;
1502
1503         for (i = 0; i < key->num_groups * key->width; i++)
1504             UpdateActionMods(keymap, &key->actions[i], key->modmap);
1505     }
1506
1507     /* Update group modifiers. */
1508     for (grp = 0; grp < XkbNumKbdGroups; grp++)
1509         ComputeEffectiveMask(keymap, &keymap->groups[grp]);
1510
1511     /* Update vmod -> indicator maps. */
1512     for (led = 0; led < XkbNumIndicators; led++)
1513         ComputeEffectiveMask(keymap, &keymap->indicators[led].mods);
1514
1515     return true;
1516 }