1 /************************************************************
2 * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
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.
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.
25 ********************************************************/
27 #include "xkbcomp-priv.h"
35 * The xkb_compat section
36 * =====================
37 * This section is the third to be processesed, after xkb_keycodes and
40 * Interpret statements
41 * --------------------
42 * Statements of the form:
43 * interpret Num_Lock+Any { ... }
45 * The body of the statment may include statements of the following
49 * action = LockMods(modifiers=NumLock);
51 * - virtual modifier statement:
52 * virtualModifier = NumLock;
57 * - useModMapMods statement:
58 * useModMapMods = level1;
60 * Indicator map statements
61 * ------------------------
62 * Statements of the form:
63 * indicator "Shift Lock" { ... }
65 * This statement specifies the behavior and binding of the indicator
66 * with the given name ("Shift Lock" above). The name should have been
67 * declared previously in the xkb_keycodes section (see Indicator name
68 * statement), and given an index there. If it wasn't, it is created
69 * with the next free index.
70 * The body of the statement describes the conditions of the keyboard
71 * state which will cause the indicator to be lit. It may include the
72 * following statements:
74 * - modifiers statment:
75 * modifiers = ScrollLock;
77 * If the given modifiers are in the required state (see below), the
80 * - whichModifierState statment:
81 * whichModState = Latched + Locked;
83 * Can be any combination of:
84 * base, latched, locked, effective
85 * any (i.e. all of the above)
86 * none (i.e. none of the above)
87 * compat (this is legal, but unused)
88 * This will cause the respective portion of the modifer state (see
89 * struct xkb_state) to be matched against the modifiers given in the
90 * "modifiers" statement.
92 * Here's a simple example:
93 * indicator "Num Lock" {
94 * modifiers = NumLock;
95 * whichModState = Locked;
97 * Whenever the NumLock modifier is locked, the Num Lock indicator
101 * groups = All - group1;
103 * If the given groups are in the required state (see below), the led
106 * - whichGroupState statment:
107 * whichGroupState = Effective;
109 * Can be any combination of:
110 * base, latched, locked, effective
111 * any (i.e. all of the above)
112 * none (i.e. none of the above)
113 * This will cause the respective portion of the group state (see
114 * struct xkb_state) to be matched against the groups given in the
115 * "groups" statement.
117 * Note: the above conditions are disjunctive, i.e. if any of them are
118 * satisfied the led is lit.
120 * Virtual modifier statements
121 * ---------------------------
122 * Statements of the form:
123 * virtual_modifiers LControl;
125 * Can appear in the xkb_types, xkb_compat, xkb_symbols sections.
130 * After all of the xkb_compat sections have been compiled, the following
131 * members of struct xkb_keymap are finalized:
132 * darray(struct xkb_sym_interpret) sym_interpret;
133 * struct xkb_indicator_map indicators[XkbNumIndicators];
134 * char *compat_section_name;
135 * TODO: virtual modifiers.
139 SI_FIELD_VIRTUAL_MOD = (1 << 0),
140 SI_FIELD_ACTION = (1 << 1),
141 SI_FIELD_AUTO_REPEAT = (1 << 2),
142 SI_FIELD_LEVEL_ONE_ONLY = (1 << 3),
145 typedef struct _SymInterpInfo {
146 enum si_field defined;
148 enum merge_mode merge;
151 struct xkb_sym_interpret interp;
155 LED_FIELD_MODS = (1 << 0),
156 LED_FIELD_GROUPS = (1 << 1),
157 LED_FIELD_CTRLS = (1 << 2),
158 LED_FIELD_EXPLICIT = (1 << 3),
159 LED_FIELD_DRIVES_KBD = (1 << 4),
162 typedef struct _LEDInfo {
163 enum led_field defined;
165 enum merge_mode merge;
169 unsigned char which_mods;
171 unsigned char which_groups;
176 typedef struct _GroupCompatInfo {
178 enum merge_mode merge;
183 typedef struct _CompatInfo {
191 GroupCompatInfo groupCompat[XkbNumKbdGroups];
192 darray(LEDInfo) leds;
195 struct xkb_keymap *keymap;
199 siText(SymInterpInfo *si, CompatInfo *info)
201 static char buf[128];
203 if (si == &info->dflt)
206 snprintf(buf, sizeof(buf), "%s+%s(%s)",
207 KeysymText(si->interp.sym),
208 SIMatchText(si->interp.match),
209 ModMaskText(si->interp.mods));
214 ReportSINotArray(CompatInfo *info, SymInterpInfo *si, const char *field)
216 return ReportNotArray(info->keymap, "symbol interpretation", field,
221 ReportSIBadType(CompatInfo *info, SymInterpInfo *si, const char *field,
224 return ReportBadType(info->keymap->ctx, "symbol interpretation", field,
225 siText(si, info), wanted);
229 ReportIndicatorBadType(CompatInfo *info, LEDInfo *led,
230 const char *field, const char *wanted)
232 return ReportBadType(info->keymap->ctx, "indicator map", field,
233 xkb_atom_text(info->keymap->ctx, led->name),
238 ReportIndicatorNotArray(CompatInfo *info, LEDInfo *led,
241 return ReportNotArray(info->keymap, "indicator map", field,
242 xkb_atom_text(info->keymap->ctx, led->name));
246 ClearIndicatorMapInfo(struct xkb_context *ctx, LEDInfo *info)
248 info->name = xkb_atom_intern(ctx, "default");
250 info->which_mods = 0;
252 info->which_groups = info->groups = 0;
257 InitCompatInfo(CompatInfo *info, struct xkb_keymap *keymap, unsigned file_id)
259 info->keymap = keymap;
261 info->file_id = file_id;
262 info->errorCount = 0;
264 list_init(&info->interps);
266 info->dflt.file_id = file_id;
267 info->dflt.defined = 0;
268 info->dflt.merge = MERGE_OVERRIDE;
269 info->dflt.interp.flags = 0;
270 info->dflt.interp.virtual_mod = XkbNoModifier;
271 memset(&info->dflt.interp.act, 0, sizeof(info->dflt.interp.act));
272 info->dflt.interp.act.type = XkbSA_NoAction;
273 ClearIndicatorMapInfo(keymap->ctx, &info->ledDflt);
274 info->ledDflt.file_id = file_id;
275 info->ledDflt.defined = 0;
276 info->ledDflt.merge = MERGE_OVERRIDE;
277 memset(&info->groupCompat[0], 0,
278 XkbNumKbdGroups * sizeof(GroupCompatInfo));
279 darray_init(info->leds);
280 InitVModInfo(&info->vmods, keymap);
284 ClearCompatInfo(CompatInfo *info)
286 SymInterpInfo *si, *next_si;
287 struct xkb_keymap *keymap = info->keymap;
291 info->dflt.defined = 0;
292 info->dflt.merge = MERGE_AUGMENT;
293 info->dflt.interp.flags = 0;
294 info->dflt.interp.virtual_mod = XkbNoModifier;
295 memset(&info->dflt.interp.act, 0, sizeof(info->dflt.interp.act));
296 info->dflt.interp.act.type = XkbSA_NoAction;
297 ClearIndicatorMapInfo(keymap->ctx, &info->ledDflt);
299 list_foreach_safe(si, next_si, &info->interps, entry)
301 memset(&info->groupCompat[0], 0,
302 XkbNumKbdGroups * sizeof(GroupCompatInfo));
303 darray_free(info->leds);
304 FreeActionInfo(info->act);
307 ClearVModInfo(&info->vmods, keymap);
310 static SymInterpInfo *
311 NextInterp(CompatInfo *info)
315 si = calloc(1, sizeof(*si));
319 list_append(&si->entry, &info->interps);
325 static SymInterpInfo *
326 FindMatchingInterp(CompatInfo *info, SymInterpInfo *new)
330 list_foreach(old, &info->interps, entry)
331 if (old->interp.sym == new->interp.sym &&
332 old->interp.mods == new->interp.mods &&
333 old->interp.match == new->interp.match)
340 UseNewInterpField(enum si_field field, SymInterpInfo *old, SymInterpInfo *new,
341 int verbosity, enum si_field *collide)
343 if (!(old->defined & field))
346 if (new->defined & field) {
347 if ((old->file_id == new->file_id && verbosity > 0) || verbosity > 9)
350 if (new->merge != MERGE_AUGMENT)
358 AddInterp(CompatInfo *info, SymInterpInfo *new)
360 enum si_field collide;
363 int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
366 old = FindMatchingInterp(info, new);
368 if (new->merge == MERGE_REPLACE) {
370 if ((old->file_id == new->file_id && verbosity > 0) ||
372 log_warn(info->keymap->ctx,
373 "Multiple definitions for \"%s\"; "
374 "Earlier interpretation ignored\n",
381 if (UseNewInterpField(SI_FIELD_VIRTUAL_MOD, old, new, verbosity,
383 old->interp.virtual_mod = new->interp.virtual_mod;
384 old->defined |= SI_FIELD_VIRTUAL_MOD;
386 if (UseNewInterpField(SI_FIELD_ACTION, old, new, verbosity,
388 old->interp.act = new->interp.act;
389 old->defined |= SI_FIELD_ACTION;
391 if (UseNewInterpField(SI_FIELD_AUTO_REPEAT, old, new, verbosity,
393 old->interp.flags &= ~XkbSI_AutoRepeat;
394 old->interp.flags |= (new->interp.flags & XkbSI_AutoRepeat);
395 old->defined |= SI_FIELD_AUTO_REPEAT;
397 if (UseNewInterpField(SI_FIELD_LEVEL_ONE_ONLY, old, new, verbosity,
399 old->interp.match &= ~XkbSI_LevelOneOnly;
400 old->interp.match |= (new->interp.match & XkbSI_LevelOneOnly);
401 old->defined |= SI_FIELD_LEVEL_ONE_ONLY;
405 log_warn(info->keymap->ctx,
406 "Multiple interpretations of \"%s\"; "
407 "Using %s definition for duplicate fields\n",
409 (new->merge != MERGE_AUGMENT ? "last" : "first"));
416 new = NextInterp(info);
426 AddGroupCompat(CompatInfo *info, xkb_group_index_t group, GroupCompatInfo *new)
429 int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
431 gc = &info->groupCompat[group];
432 if (gc->mods == new->mods)
435 if ((gc->file_id == new->file_id && verbosity > 0) || verbosity > 9)
436 log_warn(info->keymap->ctx,
437 "Compat map for group %u redefined; "
438 "Using %s definition\n",
439 group + 1, (new->merge == MERGE_AUGMENT ? "old" : "new"));
441 if (new->defined && (new->merge != MERGE_AUGMENT || !gc->defined))
447 /***====================================================================***/
450 ResolveStateAndPredicate(ExprDef *expr, unsigned *pred_rtrn,
451 xkb_mod_mask_t *mods_rtrn, CompatInfo *info)
454 *pred_rtrn = XkbSI_AnyOfOrNone;
459 *pred_rtrn = XkbSI_Exactly;
460 if (expr->op == EXPR_ACTION_DECL) {
461 const char *pred_txt = xkb_atom_text(info->keymap->ctx,
462 expr->value.action.name);
463 if (istreq(pred_txt, "noneof"))
464 *pred_rtrn = XkbSI_NoneOf;
465 else if (istreq(pred_txt, "anyofornone"))
466 *pred_rtrn = XkbSI_AnyOfOrNone;
467 else if (istreq(pred_txt, "anyof"))
468 *pred_rtrn = XkbSI_AnyOf;
469 else if (istreq(pred_txt, "allof"))
470 *pred_rtrn = XkbSI_AllOf;
471 else if (istreq(pred_txt, "exactly"))
472 *pred_rtrn = XkbSI_Exactly;
474 log_err(info->keymap->ctx,
475 "Illegal modifier predicate \"%s\"; Ignored\n", pred_txt);
478 expr = expr->value.action.args;
480 else if (expr->op == EXPR_IDENT) {
481 const char *pred_txt = xkb_atom_text(info->keymap->ctx,
483 if (pred_txt && istreq(pred_txt, "any")) {
484 *pred_rtrn = XkbSI_AnyOf;
490 return ExprResolveModMask(info->keymap->ctx, expr, mods_rtrn);
493 /***====================================================================***/
496 UseNewLEDField(enum led_field field, LEDInfo *old, LEDInfo *new,
497 int verbosity, enum led_field *collide)
499 if (!(old->defined & field))
502 if (new->defined & field) {
503 if ((old->file_id == new->file_id && verbosity > 0) || verbosity > 9)
506 if (new->merge != MERGE_AUGMENT)
514 AddIndicatorMap(CompatInfo *info, LEDInfo *new)
517 enum led_field collide;
518 struct xkb_context *ctx = info->keymap->ctx;
519 int verbosity = xkb_get_log_verbosity(ctx);
521 darray_foreach(old, info->leds) {
522 if (old->name != new->name)
525 if (old->mods == new->mods &&
526 old->groups == new->groups &&
527 old->ctrls == new->ctrls &&
528 old->which_mods == new->which_mods &&
529 old->which_groups == new->which_groups) {
530 old->defined |= new->defined;
534 if (new->merge == MERGE_REPLACE) {
535 if ((old->file_id == new->file_id && verbosity > 0) ||
537 log_warn(info->keymap->ctx,
538 "Map for indicator %s redefined; "
539 "Earlier definition ignored\n",
540 xkb_atom_text(ctx, old->name));
546 if (UseNewLEDField(LED_FIELD_MODS, old, new, verbosity,
548 old->which_mods = new->which_mods;
549 old->mods = new->mods;
550 old->defined |= LED_FIELD_MODS;
552 if (UseNewLEDField(LED_FIELD_GROUPS, old, new, verbosity,
554 old->which_groups = new->which_groups;
555 old->groups = new->groups;
556 old->defined |= LED_FIELD_GROUPS;
558 if (UseNewLEDField(LED_FIELD_CTRLS, old, new, verbosity,
560 old->ctrls = new->ctrls;
561 old->defined |= LED_FIELD_CTRLS;
563 if (UseNewLEDField(LED_FIELD_EXPLICIT, old, new, verbosity,
565 old->flags &= ~XkbIM_NoExplicit;
566 old->flags |= (new->flags & XkbIM_NoExplicit);
567 old->defined |= LED_FIELD_EXPLICIT;
569 if (UseNewLEDField(LED_FIELD_DRIVES_KBD, old, new, verbosity,
571 old->flags &= ~XkbIM_LEDDrivesKB;
572 old->flags |= (new->flags & XkbIM_LEDDrivesKB);
573 old->defined |= LED_FIELD_DRIVES_KBD;
577 log_warn(info->keymap->ctx,
578 "Map for indicator %s redefined; "
579 "Using %s definition for duplicate fields\n",
580 xkb_atom_text(ctx, old->name),
581 (new->merge == MERGE_AUGMENT ? "first" : "last"));
587 darray_append(info->leds, *new);
592 MergeIncludedCompatMaps(CompatInfo *into, CompatInfo *from,
593 enum merge_mode merge)
595 SymInterpInfo *si, *next_si;
597 GroupCompatInfo *gcm;
600 if (from->errorCount > 0) {
601 into->errorCount += from->errorCount;
605 if (into->name == NULL) {
606 into->name = from->name;
610 list_foreach_safe(si, next_si, &from->interps, entry) {
611 si->merge = (merge == MERGE_DEFAULT ? si->merge : merge);
612 if (!AddInterp(into, si))
616 for (i = 0; i < XkbNumKbdGroups; i++) {
617 gcm = &from->groupCompat[i];
618 gcm->merge = (merge == MERGE_DEFAULT ? gcm->merge : merge);
619 if (!AddGroupCompat(into, i, gcm))
623 darray_foreach(led, from->leds) {
624 led->merge = (merge == MERGE_DEFAULT ? led->merge : merge);
625 if (!AddIndicatorMap(into, led))
631 HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge);
634 HandleIncludeCompatMap(CompatInfo *info, IncludeStmt *stmt)
636 enum merge_mode merge = MERGE_DEFAULT;
638 CompatInfo included, next_incl;
640 InitCompatInfo(&included, info->keymap, info->file_id);
643 included.name = stmt->stmt;
647 for (; stmt; stmt = stmt->next_incl) {
648 if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_COMPAT,
650 info->errorCount += 10;
651 ClearCompatInfo(&included);
655 InitCompatInfo(&next_incl, info->keymap, rtrn->id);
656 next_incl.file_id = rtrn->id;
657 next_incl.dflt = info->dflt;
658 next_incl.dflt.file_id = rtrn->id;
659 next_incl.dflt.merge = merge;
660 next_incl.ledDflt.file_id = rtrn->id;
661 next_incl.ledDflt.merge = merge;
662 next_incl.act = info->act;
664 HandleCompatMapFile(&next_incl, rtrn, MERGE_OVERRIDE);
666 MergeIncludedCompatMaps(&included, &next_incl, merge);
668 next_incl.act = NULL;
670 ClearCompatInfo(&next_incl);
674 MergeIncludedCompatMaps(info, &included, merge);
675 ClearCompatInfo(&included);
677 return (info->errorCount == 0);
680 static const LookupEntry useModMapValues[] = {
689 SetInterpField(CompatInfo *info, SymInterpInfo *si, const char *field,
690 ExprDef *arrayNdx, ExprDef *value)
692 struct xkb_keymap *keymap = info->keymap;
695 if (istreq(field, "action")) {
697 return ReportSINotArray(info, si, field);
699 if (!HandleActionDef(value, keymap, &si->interp.act, info->act))
702 si->defined |= SI_FIELD_ACTION;
704 else if (istreq(field, "virtualmodifier") ||
705 istreq(field, "virtualmod")) {
707 return ReportSINotArray(info, si, field);
709 if (!ResolveVirtualModifier(value, keymap, &ndx, &info->vmods))
710 return ReportSIBadType(info, si, field, "virtual modifier");
712 si->interp.virtual_mod = ndx;
713 si->defined |= SI_FIELD_VIRTUAL_MOD;
715 else if (istreq(field, "repeat")) {
719 return ReportSINotArray(info, si, field);
721 if (!ExprResolveBoolean(keymap->ctx, value, &set))
722 return ReportSIBadType(info, si, field, "boolean");
725 si->interp.flags |= XkbSI_AutoRepeat;
727 si->interp.flags &= ~XkbSI_AutoRepeat;
729 si->defined |= SI_FIELD_AUTO_REPEAT;
731 else if (istreq(field, "locking")) {
732 log_dbg(info->keymap->ctx,
733 "The \"locking\" field in symbol interpretation is unsupported; "
736 else if (istreq(field, "usemodmap") ||
737 istreq(field, "usemodmapmods")) {
741 return ReportSINotArray(info, si, field);
743 if (!ExprResolveEnum(keymap->ctx, value, &val, useModMapValues))
744 return ReportSIBadType(info, si, field, "level specification");
747 si->interp.match |= XkbSI_LevelOneOnly;
749 si->interp.match &= ~XkbSI_LevelOneOnly;
751 si->defined |= SI_FIELD_LEVEL_ONE_ONLY;
754 return ReportBadField(keymap, "symbol interpretation", field,
761 static const LookupEntry modComponentNames[] = {
762 {"base", XkbIM_UseBase},
763 {"latched", XkbIM_UseLatched},
764 {"locked", XkbIM_UseLocked},
765 {"effective", XkbIM_UseEffective},
766 {"compat", XkbIM_UseCompat},
767 {"any", XkbIM_UseAnyMods},
772 static const LookupEntry groupComponentNames[] = {
773 {"base", XkbIM_UseBase},
774 {"latched", XkbIM_UseLatched},
775 {"locked", XkbIM_UseLocked},
776 {"effective", XkbIM_UseEffective},
777 {"any", XkbIM_UseAnyGroup},
782 static const LookupEntry groupNames[] = {
797 SetIndicatorMapField(CompatInfo *info, LEDInfo *led,
798 const char *field, ExprDef *arrayNdx, ExprDef *value)
801 struct xkb_keymap *keymap = info->keymap;
803 if (istreq(field, "modifiers") || istreq(field, "mods")) {
805 return ReportIndicatorNotArray(info, led, field);
807 if (!ExprResolveVModMask(keymap, value, &led->mods))
808 return ReportIndicatorBadType(info, led, field, "modifier mask");
810 led->defined |= LED_FIELD_MODS;
812 else if (istreq(field, "groups")) {
816 return ReportIndicatorNotArray(info, led, field);
818 if (!ExprResolveMask(keymap->ctx, value, &mask, groupNames))
819 return ReportIndicatorBadType(info, led, field, "group mask");
822 led->defined |= LED_FIELD_GROUPS;
824 else if (istreq(field, "controls") || istreq(field, "ctrls")) {
828 return ReportIndicatorNotArray(info, led, field);
830 if (!ExprResolveMask(keymap->ctx, value, &mask, ctrlNames))
831 return ReportIndicatorBadType(info, led, field,
835 led->defined |= LED_FIELD_CTRLS;
837 else if (istreq(field, "allowexplicit")) {
841 return ReportIndicatorNotArray(info, led, field);
843 if (!ExprResolveBoolean(keymap->ctx, value, &set))
844 return ReportIndicatorBadType(info, led, field, "boolean");
847 led->flags &= ~XkbIM_NoExplicit;
849 led->flags |= XkbIM_NoExplicit;
851 led->defined |= LED_FIELD_EXPLICIT;
853 else if (istreq(field, "whichmodstate") ||
854 istreq(field, "whichmodifierstate")) {
858 return ReportIndicatorNotArray(info, led, field);
860 if (!ExprResolveMask(keymap->ctx, value, &mask, modComponentNames))
861 return ReportIndicatorBadType(info, led, field,
862 "mask of modifier state components");
864 led->which_mods = mask;
866 else if (istreq(field, "whichgroupstate")) {
870 return ReportIndicatorNotArray(info, led, field);
872 if (!ExprResolveMask(keymap->ctx, value, &mask, groupComponentNames))
873 return ReportIndicatorBadType(info, led, field,
874 "mask of group state components");
876 led->which_groups = mask;
878 else if (istreq(field, "driveskbd") ||
879 istreq(field, "driveskeyboard") ||
880 istreq(field, "leddriveskbd") ||
881 istreq(field, "leddriveskeyboard") ||
882 istreq(field, "indicatordriveskbd") ||
883 istreq(field, "indicatordriveskeyboard")) {
887 return ReportIndicatorNotArray(info, led, field);
889 if (!ExprResolveBoolean(keymap->ctx, value, &set))
890 return ReportIndicatorBadType(info, led, field, "boolean");
893 led->flags |= XkbIM_LEDDrivesKB;
895 led->flags &= ~XkbIM_LEDDrivesKB;
897 led->defined |= LED_FIELD_DRIVES_KBD;
899 else if (istreq(field, "index")) {
900 /* Users should see this, it might cause unexpected behavior. */
901 log_err(info->keymap->ctx,
902 "The \"index\" field in indicator statements is unsupported; "
906 log_err(info->keymap->ctx,
907 "Unknown field %s in map for %s indicator; "
908 "Definition ignored\n",
909 field, xkb_atom_text(keymap->ctx, led->name));
917 HandleInterpVar(CompatInfo *info, VarDef *stmt)
919 const char *elem, *field;
923 if (!ExprResolveLhs(info->keymap->ctx, stmt->name, &elem, &field, &ndx))
925 else if (elem && istreq(elem, "interpret"))
926 ret = SetInterpField(info, &info->dflt, field, ndx, stmt->value);
927 else if (elem && istreq(elem, "indicator"))
928 ret = SetIndicatorMapField(info, &info->ledDflt, field, ndx,
931 ret = SetActionField(info->keymap, elem, field, ndx, stmt->value,
937 HandleInterpBody(CompatInfo *info, VarDef *def, SymInterpInfo *si)
940 const char *elem, *field;
943 for (; def; def = (VarDef *) def->common.next) {
944 if (def->name && def->name->op == EXPR_FIELD_REF) {
945 ok = HandleInterpVar(info, def);
949 ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
954 ok = SetInterpField(info, si, field, arrayNdx, def->value);
961 HandleInterpDef(CompatInfo *info, InterpDef *def, enum merge_mode merge)
966 if (!ResolveStateAndPredicate(def->match, &pred, &mods, info)) {
967 log_err(info->keymap->ctx,
968 "Couldn't determine matching modifiers; "
969 "Symbol interpretation ignored\n");
975 si.merge = merge = (def->merge == MERGE_DEFAULT ? merge : def->merge);
977 if (!LookupKeysym(def->sym, &si.interp.sym)) {
978 log_err(info->keymap->ctx,
979 "Could not resolve keysym %s; "
980 "Symbol interpretation ignored\n",
985 si.interp.match = pred & XkbSI_OpMask;
987 si.interp.mods = mods;
989 if (!HandleInterpBody(info, def->def, &si)) {
994 if (!AddInterp(info, &si)) {
1003 HandleGroupCompatDef(CompatInfo *info, GroupCompatDef *def,
1004 enum merge_mode merge)
1006 GroupCompatInfo tmp;
1008 merge = (def->merge == MERGE_DEFAULT ? merge : def->merge);
1010 if (def->group < 1 || def->group > XkbNumKbdGroups) {
1011 log_err(info->keymap->ctx,
1012 "Keyboard group must be in the range 1..%u; "
1013 "Compatibility map for illegal group %u ignored\n",
1014 XkbNumKbdGroups, def->group);
1018 tmp.file_id = info->file_id;
1021 if (!ExprResolveVModMask(info->keymap, def->def, &tmp.mods)) {
1022 log_err(info->keymap->ctx,
1023 "Expected a modifier mask in group compatibility definition; "
1024 "Ignoring illegal compatibility map for group %u\n",
1030 return AddGroupCompat(info, def->group - 1, &tmp);
1034 HandleIndicatorMapDef(CompatInfo *info, IndicatorMapDef *def,
1035 enum merge_mode merge)
1041 if (def->merge != MERGE_DEFAULT)
1044 led = info->ledDflt;
1046 led.name = def->name;
1049 for (var = def->body; var != NULL; var = (VarDef *) var->common.next) {
1050 const char *elem, *field;
1052 if (!ExprResolveLhs(info->keymap->ctx, var->name, &elem, &field,
1059 log_err(info->keymap->ctx,
1060 "Cannot set defaults for \"%s\" element in indicator map; "
1061 "Assignment to %s.%s ignored\n", elem, elem, field);
1065 ok = SetIndicatorMapField(info, &led, field, arrayNdx,
1071 return AddIndicatorMap(info, &led);
1077 HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge)
1082 merge = (merge == MERGE_DEFAULT ? MERGE_AUGMENT : merge);
1085 info->name = strdup_safe(file->name);
1087 for (stmt = file->defs; stmt; stmt = stmt->next) {
1088 switch (stmt->type) {
1090 ok = HandleIncludeCompatMap(info, (IncludeStmt *) stmt);
1093 ok = HandleInterpDef(info, (InterpDef *) stmt, merge);
1095 case STMT_GROUP_COMPAT:
1096 ok = HandleGroupCompatDef(info, (GroupCompatDef *) stmt, merge);
1098 case STMT_INDICATOR_MAP:
1099 ok = HandleIndicatorMapDef(info, (IndicatorMapDef *) stmt, merge);
1102 ok = HandleInterpVar(info, (VarDef *) stmt);
1105 ok = HandleVModDef((VModDef *) stmt, info->keymap, merge,
1109 log_err(info->keymap->ctx,
1110 "Interpretation files may not include other types; "
1111 "Ignoring %s\n", StmtTypeToString(stmt->type));
1119 if (info->errorCount > 10) {
1120 log_err(info->keymap->ctx,
1121 "Abandoning compatibility map \"%s\"\n", file->topName);
1128 CopyInterps(CompatInfo *info, bool needSymbol, unsigned pred)
1132 list_foreach(si, &info->interps, entry) {
1133 if (((si->interp.match & XkbSI_OpMask) != pred) ||
1134 (needSymbol && si->interp.sym == XKB_KEY_NoSymbol) ||
1135 (!needSymbol && si->interp.sym != XKB_KEY_NoSymbol))
1138 darray_append(info->keymap->sym_interpret, si->interp);
1143 CopyIndicatorMapDefs(CompatInfo *info)
1146 struct xkb_indicator_map *im;
1148 struct xkb_keymap *keymap = info->keymap;
1150 darray_foreach(led, info->leds) {
1151 const char *name = xkb_atom_text(keymap->ctx, led->name);
1154 * Find the indicator with the given name, if it was already
1155 * declared in keycodes.
1158 for (i = 0; i < XkbNumIndicators; i++) {
1159 if (streq_not_null(keymap->indicator_names[i], name)) {
1160 im = &keymap->indicators[i];
1165 /* Not previously declared; create it with next free index. */
1167 log_dbg(keymap->ctx,
1168 "Indicator name \"%s\" was not declared in the keycodes section; "
1169 "Adding new indicator\n", name);
1171 for (i = 0; i < XkbNumIndicators; i++) {
1172 if (keymap->indicator_names[i])
1175 keymap->indicator_names[i] = name;
1176 im = &keymap->indicators[i];
1180 /* Not place to put the it; ignore. */
1182 log_err(keymap->ctx,
1183 "Too many indicators (maximum is %d); "
1184 "Indicator name \"%s\" ignored\n",
1185 XkbNumIndicators, name);
1190 im->flags = led->flags;
1191 if (led->groups != 0 && led->which_groups == 0)
1192 im->which_groups = XkbIM_UseEffective;
1194 im->which_groups = led->which_groups;
1195 im->groups = led->groups;
1196 if (led->mods != 0 && led->which_mods == 0)
1197 im->which_mods = XkbIM_UseEffective;
1199 im->which_mods = led->which_mods;
1200 im->mods.mods = led->mods;
1201 im->ctrls = led->ctrls;
1208 CompileCompatMap(XkbFile *file, struct xkb_keymap *keymap,
1209 enum merge_mode merge)
1211 xkb_group_index_t i;
1213 GroupCompatInfo *gcm;
1215 InitCompatInfo(&info, keymap, file->id);
1216 info.dflt.merge = merge;
1217 info.ledDflt.merge = merge;
1219 HandleCompatMapFile(&info, file, merge);
1221 if (info.errorCount != 0)
1225 keymap->compat_section_name = strdup(info.name);
1227 darray_init(keymap->sym_interpret);
1228 if (info.nInterps > 0) {
1229 darray_growalloc(keymap->sym_interpret, info.nInterps);
1230 CopyInterps(&info, true, XkbSI_Exactly);
1231 CopyInterps(&info, true, XkbSI_AllOf | XkbSI_NoneOf);
1232 CopyInterps(&info, true, XkbSI_AnyOf);
1233 CopyInterps(&info, true, XkbSI_AnyOfOrNone);
1234 CopyInterps(&info, false, XkbSI_Exactly);
1235 CopyInterps(&info, false, XkbSI_AllOf | XkbSI_NoneOf);
1236 CopyInterps(&info, false, XkbSI_AnyOf);
1237 CopyInterps(&info, false, XkbSI_AnyOfOrNone);
1240 for (i = 0; i < XkbNumKbdGroups; i++) {
1241 gcm = &info.groupCompat[i];
1242 if (gcm->file_id != 0 || gcm->mods != 0)
1243 keymap->groups[i].mods = gcm->mods;
1246 if (!CopyIndicatorMapDefs(&info))
1249 ClearCompatInfo(&info);
1253 ClearCompatInfo(&info);
1258 ComputeEffectiveMask(struct xkb_keymap *keymap, struct xkb_mods *mods)
1261 xkb_mod_mask_t vmask = mods->mods >> XkbNumModifiers;
1263 /* The effective mask is only real mods for now. */
1264 mods->mask = mods->mods & 0xff;
1266 for (i = 0; i < XkbNumVirtualMods; i++) {
1267 if (!(vmask & (1 << i)))
1269 mods->mask |= keymap->vmods[i];
1274 UpdateActionMods(struct xkb_keymap *keymap, union xkb_action *act,
1275 xkb_mod_mask_t rmodmask)
1278 struct xkb_mods *mods;
1280 switch (act->type) {
1282 case XkbSA_LatchMods:
1283 case XkbSA_LockMods:
1284 flags = act->mods.flags;
1285 mods = &act->mods.mods;
1289 flags = act->iso.flags;
1290 mods = &act->iso.mods;
1297 if (flags & XkbSA_UseModMapMods) {
1298 /* XXX: what's that. */
1300 mods->mods |= rmodmask;
1302 ComputeEffectiveMask(keymap, mods);
1306 * Find an interpretation which applies to this particular level, either by
1307 * finding an exact match for the symbol and modifier combination, or a
1308 * generic XKB_KEY_NoSymbol match.
1310 static struct xkb_sym_interpret *
1311 FindInterpForKey(struct xkb_keymap *keymap, struct xkb_key *key,
1312 xkb_group_index_t group, xkb_level_index_t level)
1314 struct xkb_sym_interpret *ret = NULL;
1315 struct xkb_sym_interpret *interp;
1316 const xkb_keysym_t *syms;
1319 num_syms = xkb_key_get_syms_by_level(keymap, key, group, level, &syms);
1323 darray_foreach(interp, keymap->sym_interpret) {
1327 if ((num_syms > 1 || interp->sym != syms[0]) &&
1328 interp->sym != XKB_KEY_NoSymbol)
1331 if (level == 0 || !(interp->match & XkbSI_LevelOneOnly))
1336 switch (interp->match & XkbSI_OpMask) {
1338 found = !(interp->mods & mods);
1340 case XkbSI_AnyOfOrNone:
1341 found = (!mods || (interp->mods & mods));
1344 found = !!(interp->mods & mods);
1347 found = ((interp->mods & mods) == interp->mods);
1350 found = (interp->mods == mods);
1357 if (found && interp->sym != XKB_KEY_NoSymbol)
1359 else if (found && !ret)
1367 ApplyInterpsToKey(struct xkb_keymap *keymap, struct xkb_key *key)
1369 #define INTERP_SIZE (8 * 4)
1370 struct xkb_sym_interpret *interps[INTERP_SIZE];
1371 xkb_mod_mask_t vmodmask = 0;
1373 xkb_group_index_t group;
1374 xkb_level_index_t level;
1377 /* If we've been told not to bind interps to this key, then don't. */
1378 if (key->explicit & XkbExplicitInterpretMask)
1381 for (i = 0; i < INTERP_SIZE; i++)
1384 for (group = 0; group < key->num_groups; group++) {
1385 for (level = 0; level < XkbKeyGroupWidth(keymap, key, group);
1387 i = (group * key->width) + level;
1388 if (i >= INTERP_SIZE) /* XXX FIXME */
1390 interps[i] = FindInterpForKey(keymap, key, group, level);
1396 if (num_acts && !key->actions) {
1397 key->actions = calloc(key->num_groups * key->width,
1398 sizeof(*key->actions));
1403 for (group = 0; group < key->num_groups; group++) {
1404 for (level = 0; level < XkbKeyGroupWidth(keymap, key, group);
1406 struct xkb_sym_interpret *interp;
1408 i = (group * key->width) + level;
1409 interp = interps[i];
1411 /* Infer default key behaviours from the base level. */
1412 if (group == 0 && level == 0) {
1413 if (!(key->explicit & XkbExplicitAutoRepeatMask) &&
1414 (!interp || (interp->flags & XkbSI_AutoRepeat)))
1415 key->repeats = true;
1421 if ((group == 0 && level == 0) ||
1422 !(interp->match & XkbSI_LevelOneOnly)) {
1423 if (interp->virtual_mod != XkbNoModifier)
1424 vmodmask |= (1 << interp->virtual_mod);
1427 key->actions[i] = interp->act;
1431 if (!(key->explicit & XkbExplicitVModMapMask))
1432 key->vmodmap = vmodmask;
1439 * This collects a bunch of disparate functions which was done in the server
1440 * at various points that really should've been done within xkbcomp. Turns out
1441 * your actions and types are a lot more useful when any of your modifiers
1442 * other than Shift actually do something ...
1445 UpdateModifiersFromCompat(struct xkb_keymap *keymap)
1447 xkb_mod_index_t vmod;
1448 xkb_group_index_t grp;
1449 xkb_led_index_t led;
1451 struct xkb_key *key;
1453 /* Find all the interprets for the key and bind them to actions,
1454 * which will also update the vmodmap. */
1455 xkb_foreach_key(key, keymap)
1456 if (!ApplyInterpsToKey(keymap, key))
1459 /* Update keymap->vmods, the virtual -> real mod mapping. */
1460 for (vmod = 0; vmod < XkbNumVirtualMods; vmod++)
1461 keymap->vmods[vmod] = 0;
1463 xkb_foreach_key(key, keymap) {
1467 for (vmod = 0; vmod < XkbNumVirtualMods; vmod++) {
1468 if (!(key->vmodmap & (1 << vmod)))
1470 keymap->vmods[vmod] |= key->modmap;
1474 /* Now update the level masks for all the types to reflect the vmods. */
1475 for (i = 0; i < keymap->num_types; i++) {
1476 ComputeEffectiveMask(keymap, &keymap->types[i].mods);
1478 for (j = 0; j < keymap->types[i].num_entries; j++) {
1479 ComputeEffectiveMask(keymap, &keymap->types[i].map[j].mods);
1480 ComputeEffectiveMask(keymap, &keymap->types[i].map[j].preserve);
1484 /* Update action modifiers. */
1485 xkb_foreach_key(key, keymap) {
1489 for (i = 0; i < key->num_groups * key->width; i++)
1490 UpdateActionMods(keymap, &key->actions[i], key->modmap);
1493 /* Update group modifiers. */
1494 for (grp = 0; grp < XkbNumKbdGroups; grp++)
1495 ComputeEffectiveMask(keymap, &keymap->groups[grp]);
1497 /* Update vmod -> indicator maps. */
1498 for (led = 0; led < XkbNumIndicators; led++)
1499 ComputeEffectiveMask(keymap, &keymap->indicators[led].mods);