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 xkb_led_index_t indicator;
171 unsigned char which_mods;
173 unsigned char which_groups;
178 typedef struct _GroupCompatInfo {
180 enum merge_mode merge;
185 typedef struct _CompatInfo {
193 GroupCompatInfo groupCompat[XkbNumKbdGroups];
197 struct xkb_keymap *keymap;
201 siText(SymInterpInfo *si, CompatInfo *info)
203 static char buf[128];
205 if (si == &info->dflt)
208 snprintf(buf, sizeof(buf), "%s+%s(%s)",
209 KeysymText(si->interp.sym),
210 SIMatchText(si->interp.match),
211 ModMaskText(si->interp.mods));
216 ReportSINotArray(CompatInfo *info, SymInterpInfo *si, const char *field)
218 return ReportNotArray(info->keymap, "symbol interpretation", field,
223 ReportSIBadType(CompatInfo *info, SymInterpInfo *si, const char *field,
226 return ReportBadType(info->keymap->ctx, "symbol interpretation", field,
227 siText(si, info), wanted);
231 ReportIndicatorBadType(CompatInfo *info, LEDInfo *led,
232 const char *field, const char *wanted)
234 return ReportBadType(info->keymap->ctx, "indicator map", field,
235 xkb_atom_text(info->keymap->ctx, led->name),
240 ReportIndicatorNotArray(CompatInfo *info, LEDInfo *led,
243 return ReportNotArray(info->keymap, "indicator map", field,
244 xkb_atom_text(info->keymap->ctx, led->name));
248 ClearIndicatorMapInfo(struct xkb_context *ctx, LEDInfo *info)
250 info->name = xkb_atom_intern(ctx, "default");
251 info->indicator = XKB_LED_INVALID;
253 info->which_mods = 0;
255 info->which_groups = info->groups = 0;
260 InitCompatInfo(CompatInfo *info, struct xkb_keymap *keymap, unsigned file_id)
262 info->keymap = keymap;
264 info->file_id = file_id;
265 info->errorCount = 0;
267 list_init(&info->interps);
269 info->dflt.file_id = file_id;
270 info->dflt.defined = 0;
271 info->dflt.merge = MERGE_OVERRIDE;
272 info->dflt.interp.flags = 0;
273 info->dflt.interp.virtual_mod = XkbNoModifier;
274 memset(&info->dflt.interp.act, 0, sizeof(info->dflt.interp.act));
275 info->dflt.interp.act.type = XkbSA_NoAction;
276 ClearIndicatorMapInfo(keymap->ctx, &info->ledDflt);
277 info->ledDflt.file_id = file_id;
278 info->ledDflt.defined = 0;
279 info->ledDflt.merge = MERGE_OVERRIDE;
280 memset(&info->groupCompat[0], 0,
281 XkbNumKbdGroups * sizeof(GroupCompatInfo));
282 list_init(&info->leds);
283 InitVModInfo(&info->vmods, keymap);
287 ClearCompatInfo(CompatInfo *info)
289 SymInterpInfo *si, *next_si;
290 LEDInfo *led, *next_led;
291 struct xkb_keymap *keymap = info->keymap;
295 info->dflt.defined = 0;
296 info->dflt.merge = MERGE_AUGMENT;
297 info->dflt.interp.flags = 0;
298 info->dflt.interp.virtual_mod = XkbNoModifier;
299 memset(&info->dflt.interp.act, 0, sizeof(info->dflt.interp.act));
300 info->dflt.interp.act.type = XkbSA_NoAction;
301 ClearIndicatorMapInfo(keymap->ctx, &info->ledDflt);
303 list_foreach_safe(si, next_si, &info->interps, entry)
305 memset(&info->groupCompat[0], 0,
306 XkbNumKbdGroups * sizeof(GroupCompatInfo));
307 list_foreach_safe(led, next_led, &info->leds, entry)
309 FreeActionInfo(info->act);
312 ClearVModInfo(&info->vmods, keymap);
315 static SymInterpInfo *
316 NextInterp(CompatInfo *info)
320 si = calloc(1, sizeof(*si));
324 list_append(&si->entry, &info->interps);
330 static SymInterpInfo *
331 FindMatchingInterp(CompatInfo *info, SymInterpInfo *new)
335 list_foreach(old, &info->interps, entry)
336 if (old->interp.sym == new->interp.sym &&
337 old->interp.mods == new->interp.mods &&
338 old->interp.match == new->interp.match)
345 UseNewInterpField(enum si_field field, SymInterpInfo *old, SymInterpInfo *new,
346 int verbosity, enum si_field *collide)
348 if (!(old->defined & field))
351 if (new->defined & field) {
352 if ((old->file_id == new->file_id && verbosity > 0) || verbosity > 9)
355 if (new->merge != MERGE_AUGMENT)
363 AddInterp(CompatInfo *info, SymInterpInfo *new)
365 enum si_field collide;
368 int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
371 old = FindMatchingInterp(info, new);
373 if (new->merge == MERGE_REPLACE) {
375 if ((old->file_id == new->file_id && verbosity > 0) ||
377 log_warn(info->keymap->ctx,
378 "Multiple definitions for \"%s\"; "
379 "Earlier interpretation ignored\n",
386 if (UseNewInterpField(SI_FIELD_VIRTUAL_MOD, old, new, verbosity,
388 old->interp.virtual_mod = new->interp.virtual_mod;
389 old->defined |= SI_FIELD_VIRTUAL_MOD;
391 if (UseNewInterpField(SI_FIELD_ACTION, old, new, verbosity,
393 old->interp.act = new->interp.act;
394 old->defined |= SI_FIELD_ACTION;
396 if (UseNewInterpField(SI_FIELD_AUTO_REPEAT, old, new, verbosity,
398 old->interp.flags &= ~XkbSI_AutoRepeat;
399 old->interp.flags |= (new->interp.flags & XkbSI_AutoRepeat);
400 old->defined |= SI_FIELD_AUTO_REPEAT;
402 if (UseNewInterpField(SI_FIELD_LEVEL_ONE_ONLY, old, new, verbosity,
404 old->interp.match &= ~XkbSI_LevelOneOnly;
405 old->interp.match |= (new->interp.match & XkbSI_LevelOneOnly);
406 old->defined |= SI_FIELD_LEVEL_ONE_ONLY;
410 log_warn(info->keymap->ctx,
411 "Multiple interpretations of \"%s\"; "
412 "Using %s definition for duplicate fields\n",
414 (new->merge != MERGE_AUGMENT ? "last" : "first"));
421 new = NextInterp(info);
431 AddGroupCompat(CompatInfo *info, xkb_group_index_t group, GroupCompatInfo *new)
434 int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
436 gc = &info->groupCompat[group];
437 if (gc->mods == new->mods)
440 if ((gc->file_id == new->file_id && verbosity > 0) || verbosity > 9)
441 log_warn(info->keymap->ctx,
442 "Compat map for group %u redefined; "
443 "Using %s definition\n",
444 group + 1, (new->merge == MERGE_AUGMENT ? "old" : "new"));
446 if (new->defined && (new->merge != MERGE_AUGMENT || !gc->defined))
452 /***====================================================================***/
455 ResolveStateAndPredicate(ExprDef *expr, unsigned *pred_rtrn,
456 xkb_mod_mask_t *mods_rtrn, CompatInfo *info)
459 *pred_rtrn = XkbSI_AnyOfOrNone;
464 *pred_rtrn = XkbSI_Exactly;
465 if (expr->op == EXPR_ACTION_DECL) {
466 const char *pred_txt = xkb_atom_text(info->keymap->ctx,
467 expr->value.action.name);
468 if (istreq(pred_txt, "noneof"))
469 *pred_rtrn = XkbSI_NoneOf;
470 else if (istreq(pred_txt, "anyofornone"))
471 *pred_rtrn = XkbSI_AnyOfOrNone;
472 else if (istreq(pred_txt, "anyof"))
473 *pred_rtrn = XkbSI_AnyOf;
474 else if (istreq(pred_txt, "allof"))
475 *pred_rtrn = XkbSI_AllOf;
476 else if (istreq(pred_txt, "exactly"))
477 *pred_rtrn = XkbSI_Exactly;
479 log_err(info->keymap->ctx,
480 "Illegal modifier predicate \"%s\"; Ignored\n", pred_txt);
483 expr = expr->value.action.args;
485 else if (expr->op == EXPR_IDENT) {
486 const char *pred_txt = xkb_atom_text(info->keymap->ctx,
488 if (pred_txt && istreq(pred_txt, "any")) {
489 *pred_rtrn = XkbSI_AnyOf;
495 return ExprResolveModMask(info->keymap->ctx, expr, mods_rtrn);
498 /***====================================================================***/
501 UseNewLEDField(enum led_field field, LEDInfo *old, LEDInfo *new,
502 int verbosity, enum led_field *collide)
504 if (!(old->defined & field))
507 if (new->defined & field) {
508 if ((old->file_id == new->file_id && verbosity > 0) || verbosity > 9)
511 if (new->merge != MERGE_AUGMENT)
519 AddIndicatorMap(CompatInfo *info, LEDInfo *new)
522 enum led_field collide;
523 struct xkb_context *ctx = info->keymap->ctx;
524 int verbosity = xkb_get_log_verbosity(ctx);
526 list_foreach(old, &info->leds, entry) {
527 if (old->name != new->name)
530 if (old->mods == new->mods &&
531 old->groups == new->groups &&
532 old->ctrls == new->ctrls &&
533 old->which_mods == new->which_mods &&
534 old->which_groups == new->which_groups) {
535 old->defined |= new->defined;
539 if (new->merge == MERGE_REPLACE) {
540 struct list entry = old->entry;
541 if ((old->file_id == new->file_id && verbosity > 0) ||
543 log_warn(info->keymap->ctx,
544 "Map for indicator %s redefined; "
545 "Earlier definition ignored\n",
546 xkb_atom_text(ctx, old->name));
553 if (UseNewLEDField(LED_FIELD_MODS, old, new, verbosity,
555 old->which_mods = new->which_mods;
556 old->mods = new->mods;
557 old->defined |= LED_FIELD_MODS;
559 if (UseNewLEDField(LED_FIELD_GROUPS, old, new, verbosity,
561 old->which_groups = new->which_groups;
562 old->groups = new->groups;
563 old->defined |= LED_FIELD_GROUPS;
565 if (UseNewLEDField(LED_FIELD_CTRLS, old, new, verbosity,
567 old->ctrls = new->ctrls;
568 old->defined |= LED_FIELD_CTRLS;
570 if (UseNewLEDField(LED_FIELD_EXPLICIT, old, new, verbosity,
572 old->flags &= ~XkbIM_NoExplicit;
573 old->flags |= (new->flags & XkbIM_NoExplicit);
574 old->defined |= LED_FIELD_EXPLICIT;
576 if (UseNewLEDField(LED_FIELD_DRIVES_KBD, old, new, verbosity,
578 old->flags &= ~XkbIM_LEDDrivesKB;
579 old->flags |= (new->flags & XkbIM_LEDDrivesKB);
580 old->defined |= LED_FIELD_DRIVES_KBD;
584 log_warn(info->keymap->ctx,
585 "Map for indicator %s redefined; "
586 "Using %s definition for duplicate fields\n",
587 xkb_atom_text(ctx, old->name),
588 (new->merge == MERGE_AUGMENT ? "first" : "last"));
594 old = malloc(sizeof(*old));
596 log_wsgo(info->keymap->ctx,
597 "Couldn't allocate indicator map; "
598 "Map for indicator %s not compiled\n",
599 xkb_atom_text(ctx, new->name));
604 list_append(&old->entry, &info->leds);
610 MergeIncludedCompatMaps(CompatInfo *into, CompatInfo *from,
611 enum merge_mode merge)
613 SymInterpInfo *si, *next_si;
614 LEDInfo *led, *next_led;
615 GroupCompatInfo *gcm;
618 if (from->errorCount > 0) {
619 into->errorCount += from->errorCount;
623 if (into->name == NULL) {
624 into->name = from->name;
628 list_foreach_safe(si, next_si, &from->interps, entry) {
629 si->merge = (merge == MERGE_DEFAULT ? si->merge : merge);
630 if (!AddInterp(into, si))
634 for (i = 0; i < XkbNumKbdGroups; i++) {
635 gcm = &from->groupCompat[i];
636 gcm->merge = (merge == MERGE_DEFAULT ? gcm->merge : merge);
637 if (!AddGroupCompat(into, i, gcm))
641 list_foreach_safe(led, next_led, &from->leds, entry) {
642 led->merge = (merge == MERGE_DEFAULT ? led->merge : merge);
643 if (!AddIndicatorMap(into, led))
649 HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge);
652 HandleIncludeCompatMap(CompatInfo *info, IncludeStmt *stmt)
654 enum merge_mode merge = MERGE_DEFAULT;
656 CompatInfo included, next_incl;
658 InitCompatInfo(&included, info->keymap, info->file_id);
661 included.name = stmt->stmt;
665 for (; stmt; stmt = stmt->next_incl) {
666 if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_COMPAT,
668 info->errorCount += 10;
669 ClearCompatInfo(&included);
673 InitCompatInfo(&next_incl, info->keymap, rtrn->id);
674 next_incl.file_id = rtrn->id;
675 next_incl.dflt = info->dflt;
676 next_incl.dflt.file_id = rtrn->id;
677 next_incl.dflt.merge = merge;
678 next_incl.ledDflt.file_id = rtrn->id;
679 next_incl.ledDflt.merge = merge;
680 next_incl.act = info->act;
682 HandleCompatMapFile(&next_incl, rtrn, MERGE_OVERRIDE);
684 MergeIncludedCompatMaps(&included, &next_incl, merge);
686 next_incl.act = NULL;
688 ClearCompatInfo(&next_incl);
692 MergeIncludedCompatMaps(info, &included, merge);
693 ClearCompatInfo(&included);
695 return (info->errorCount == 0);
698 static const LookupEntry useModMapValues[] = {
707 SetInterpField(CompatInfo *info, SymInterpInfo *si, const char *field,
708 ExprDef *arrayNdx, ExprDef *value)
710 struct xkb_keymap *keymap = info->keymap;
713 if (istreq(field, "action")) {
715 return ReportSINotArray(info, si, field);
717 if (!HandleActionDef(value, keymap, &si->interp.act, info->act))
720 si->defined |= SI_FIELD_ACTION;
722 else if (istreq(field, "virtualmodifier") ||
723 istreq(field, "virtualmod")) {
725 return ReportSINotArray(info, si, field);
727 if (!ResolveVirtualModifier(value, keymap, &ndx, &info->vmods))
728 return ReportSIBadType(info, si, field, "virtual modifier");
730 si->interp.virtual_mod = ndx;
731 si->defined |= SI_FIELD_VIRTUAL_MOD;
733 else if (istreq(field, "repeat")) {
737 return ReportSINotArray(info, si, field);
739 if (!ExprResolveBoolean(keymap->ctx, value, &set))
740 return ReportSIBadType(info, si, field, "boolean");
743 si->interp.flags |= XkbSI_AutoRepeat;
745 si->interp.flags &= ~XkbSI_AutoRepeat;
747 si->defined |= SI_FIELD_AUTO_REPEAT;
749 else if (istreq(field, "locking")) {
750 log_dbg(info->keymap->ctx,
751 "The \"locking\" field in symbol interpretation is unsupported; "
754 else if (istreq(field, "usemodmap") ||
755 istreq(field, "usemodmapmods")) {
759 return ReportSINotArray(info, si, field);
761 if (!ExprResolveEnum(keymap->ctx, value, &val, useModMapValues))
762 return ReportSIBadType(info, si, field, "level specification");
765 si->interp.match |= XkbSI_LevelOneOnly;
767 si->interp.match &= ~XkbSI_LevelOneOnly;
769 si->defined |= SI_FIELD_LEVEL_ONE_ONLY;
772 return ReportBadField(keymap, "symbol interpretation", field,
779 static const LookupEntry modComponentNames[] = {
780 {"base", XkbIM_UseBase},
781 {"latched", XkbIM_UseLatched},
782 {"locked", XkbIM_UseLocked},
783 {"effective", XkbIM_UseEffective},
784 {"compat", XkbIM_UseCompat},
785 {"any", XkbIM_UseAnyMods},
790 static const LookupEntry groupComponentNames[] = {
791 {"base", XkbIM_UseBase},
792 {"latched", XkbIM_UseLatched},
793 {"locked", XkbIM_UseLocked},
794 {"effective", XkbIM_UseEffective},
795 {"any", XkbIM_UseAnyGroup},
800 static const LookupEntry groupNames[] = {
815 SetIndicatorMapField(CompatInfo *info, LEDInfo *led,
816 const char *field, ExprDef *arrayNdx, ExprDef *value)
819 struct xkb_keymap *keymap = info->keymap;
821 if (istreq(field, "modifiers") || istreq(field, "mods")) {
823 return ReportIndicatorNotArray(info, led, field);
825 if (!ExprResolveVModMask(keymap, value, &led->mods))
826 return ReportIndicatorBadType(info, led, field, "modifier mask");
828 led->defined |= LED_FIELD_MODS;
830 else if (istreq(field, "groups")) {
834 return ReportIndicatorNotArray(info, led, field);
836 if (!ExprResolveMask(keymap->ctx, value, &mask, groupNames))
837 return ReportIndicatorBadType(info, led, field, "group mask");
840 led->defined |= LED_FIELD_GROUPS;
842 else if (istreq(field, "controls") || istreq(field, "ctrls")) {
846 return ReportIndicatorNotArray(info, led, field);
848 if (!ExprResolveMask(keymap->ctx, value, &mask, ctrlNames))
849 return ReportIndicatorBadType(info, led, field,
853 led->defined |= LED_FIELD_CTRLS;
855 else if (istreq(field, "allowexplicit")) {
859 return ReportIndicatorNotArray(info, led, field);
861 if (!ExprResolveBoolean(keymap->ctx, value, &set))
862 return ReportIndicatorBadType(info, led, field, "boolean");
865 led->flags &= ~XkbIM_NoExplicit;
867 led->flags |= XkbIM_NoExplicit;
869 led->defined |= LED_FIELD_EXPLICIT;
871 else if (istreq(field, "whichmodstate") ||
872 istreq(field, "whichmodifierstate")) {
876 return ReportIndicatorNotArray(info, led, field);
878 if (!ExprResolveMask(keymap->ctx, value, &mask, modComponentNames))
879 return ReportIndicatorBadType(info, led, field,
880 "mask of modifier state components");
882 led->which_mods = mask;
884 else if (istreq(field, "whichgroupstate")) {
888 return ReportIndicatorNotArray(info, led, field);
890 if (!ExprResolveMask(keymap->ctx, value, &mask, groupComponentNames))
891 return ReportIndicatorBadType(info, led, field,
892 "mask of group state components");
894 led->which_groups = mask;
896 else if (istreq(field, "driveskbd") ||
897 istreq(field, "driveskeyboard") ||
898 istreq(field, "leddriveskbd") ||
899 istreq(field, "leddriveskeyboard") ||
900 istreq(field, "indicatordriveskbd") ||
901 istreq(field, "indicatordriveskeyboard")) {
905 return ReportIndicatorNotArray(info, led, field);
907 if (!ExprResolveBoolean(keymap->ctx, value, &set))
908 return ReportIndicatorBadType(info, led, field, "boolean");
911 led->flags |= XkbIM_LEDDrivesKB;
913 led->flags &= ~XkbIM_LEDDrivesKB;
915 led->defined |= LED_FIELD_DRIVES_KBD;
917 else if (istreq(field, "index")) {
918 /* Users should see this, it might cause unexpected behavior. */
919 log_err(info->keymap->ctx,
920 "The \"index\" field in indicator statements is unsupported; "
924 log_err(info->keymap->ctx,
925 "Unknown field %s in map for %s indicator; "
926 "Definition ignored\n",
927 field, xkb_atom_text(keymap->ctx, led->name));
935 HandleInterpVar(CompatInfo *info, VarDef *stmt)
937 const char *elem, *field;
941 if (!ExprResolveLhs(info->keymap->ctx, stmt->name, &elem, &field, &ndx))
943 else if (elem && istreq(elem, "interpret"))
944 ret = SetInterpField(info, &info->dflt, field, ndx, stmt->value);
945 else if (elem && istreq(elem, "indicator"))
946 ret = SetIndicatorMapField(info, &info->ledDflt, field, ndx,
949 ret = SetActionField(info->keymap, elem, field, ndx, stmt->value,
955 HandleInterpBody(CompatInfo *info, VarDef *def, SymInterpInfo *si)
958 const char *elem, *field;
961 for (; def; def = (VarDef *) def->common.next) {
962 if (def->name && def->name->op == EXPR_FIELD_REF) {
963 ok = HandleInterpVar(info, def);
967 ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
972 ok = SetInterpField(info, si, field, arrayNdx, def->value);
979 HandleInterpDef(CompatInfo *info, InterpDef *def, enum merge_mode merge)
984 if (!ResolveStateAndPredicate(def->match, &pred, &mods, info)) {
985 log_err(info->keymap->ctx,
986 "Couldn't determine matching modifiers; "
987 "Symbol interpretation ignored\n");
993 si.merge = merge = (def->merge == MERGE_DEFAULT ? merge : def->merge);
995 if (!LookupKeysym(def->sym, &si.interp.sym)) {
996 log_err(info->keymap->ctx,
997 "Could not resolve keysym %s; "
998 "Symbol interpretation ignored\n",
1003 si.interp.match = pred & XkbSI_OpMask;
1005 si.interp.mods = mods;
1007 if (!HandleInterpBody(info, def->def, &si)) {
1012 if (!AddInterp(info, &si)) {
1021 HandleGroupCompatDef(CompatInfo *info, GroupCompatDef *def,
1022 enum merge_mode merge)
1024 GroupCompatInfo tmp;
1026 merge = (def->merge == MERGE_DEFAULT ? merge : def->merge);
1028 if (def->group < 1 || def->group > XkbNumKbdGroups) {
1029 log_err(info->keymap->ctx,
1030 "Keyboard group must be in the range 1..%u; "
1031 "Compatibility map for illegal group %u ignored\n",
1032 XkbNumKbdGroups, def->group);
1036 tmp.file_id = info->file_id;
1039 if (!ExprResolveVModMask(info->keymap, def->def, &tmp.mods)) {
1040 log_err(info->keymap->ctx,
1041 "Expected a modifier mask in group compatibility definition; "
1042 "Ignoring illegal compatibility map for group %u\n",
1048 return AddGroupCompat(info, def->group - 1, &tmp);
1052 HandleIndicatorMapDef(CompatInfo *info, IndicatorMapDef *def,
1053 enum merge_mode merge)
1059 if (def->merge != MERGE_DEFAULT)
1062 led = info->ledDflt;
1064 led.name = def->name;
1067 for (var = def->body; var != NULL; var = (VarDef *) var->common.next) {
1068 const char *elem, *field;
1070 if (!ExprResolveLhs(info->keymap->ctx, var->name, &elem, &field,
1077 log_err(info->keymap->ctx,
1078 "Cannot set defaults for \"%s\" element in indicator map; "
1079 "Assignment to %s.%s ignored\n", elem, elem, field);
1083 ok = SetIndicatorMapField(info, &led, field, arrayNdx,
1089 return AddIndicatorMap(info, &led);
1095 HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge)
1100 merge = (merge == MERGE_DEFAULT ? MERGE_AUGMENT : merge);
1103 info->name = strdup_safe(file->name);
1105 for (stmt = file->defs; stmt; stmt = stmt->next) {
1106 switch (stmt->type) {
1108 ok = HandleIncludeCompatMap(info, (IncludeStmt *) stmt);
1111 ok = HandleInterpDef(info, (InterpDef *) stmt, merge);
1113 case STMT_GROUP_COMPAT:
1114 ok = HandleGroupCompatDef(info, (GroupCompatDef *) stmt, merge);
1116 case STMT_INDICATOR_MAP:
1117 ok = HandleIndicatorMapDef(info, (IndicatorMapDef *) stmt, merge);
1120 ok = HandleInterpVar(info, (VarDef *) stmt);
1123 ok = HandleVModDef((VModDef *) stmt, info->keymap, merge,
1127 log_err(info->keymap->ctx,
1128 "Interpretation files may not include other types; "
1129 "Ignoring %s\n", StmtTypeToString(stmt->type));
1137 if (info->errorCount > 10) {
1138 log_err(info->keymap->ctx,
1139 "Abandoning compatibility map \"%s\"\n", file->topName);
1146 CopyInterps(CompatInfo *info, bool needSymbol, unsigned pred)
1150 list_foreach(si, &info->interps, entry) {
1151 if (((si->interp.match & XkbSI_OpMask) != pred) ||
1152 (needSymbol && si->interp.sym == XKB_KEY_NoSymbol) ||
1153 (!needSymbol && si->interp.sym != XKB_KEY_NoSymbol))
1156 darray_append(info->keymap->sym_interpret, si->interp);
1161 BindIndicators(CompatInfo *info, struct list *unbound_leds)
1164 LEDInfo *led, *next_led;
1165 struct xkb_indicator_map *map;
1166 struct xkb_keymap *keymap = info->keymap;
1168 list_foreach(led, unbound_leds, entry) {
1169 if (led->indicator == XKB_LED_INVALID) {
1170 for (i = 0; i < XkbNumIndicators; i++) {
1171 if (keymap->indicator_names[i] &&
1172 streq(keymap->indicator_names[i],
1173 xkb_atom_text(keymap->ctx, led->name))) {
1174 led->indicator = i + 1;
1181 list_foreach(led, unbound_leds, entry) {
1182 if (led->indicator == XKB_LED_INVALID) {
1183 for (i = 0; i < XkbNumIndicators; i++) {
1184 if (keymap->indicator_names[i] == NULL) {
1185 keymap->indicator_names[i] =
1186 xkb_atom_text(keymap->ctx, led->name);
1187 led->indicator = i + 1;
1192 if (led->indicator == XKB_LED_INVALID) {
1193 log_err(info->keymap->ctx,
1194 "No unnamed indicators found; "
1195 "Virtual indicator map \"%s\" not bound\n",
1196 xkb_atom_text(keymap->ctx, led->name));
1202 list_foreach_safe(led, next_led, unbound_leds, entry) {
1203 if (led->indicator == XKB_LED_INVALID) {
1208 if (!streq(keymap->indicator_names[led->indicator - 1],
1209 xkb_atom_text(keymap->ctx, led->name))) {
1210 const char *old = keymap->indicator_names[led->indicator - 1];
1211 log_err(info->keymap->ctx,
1212 "Multiple names bound to indicator %d; "
1213 "Using %s, ignoring %s\n",
1214 led->indicator, old,
1215 xkb_atom_text(keymap->ctx, led->name));
1220 map = &keymap->indicators[led->indicator - 1];
1221 map->flags = led->flags;
1222 map->which_groups = led->which_groups;
1223 map->groups = led->groups;
1224 map->which_mods = led->which_mods;
1225 map->mods.mods = led->mods;
1226 map->ctrls = led->ctrls;
1230 list_init(unbound_leds);
1234 CopyIndicatorMapDefs(CompatInfo *info)
1236 LEDInfo *led, *next_led;
1237 struct list unbound_leds;
1238 struct xkb_indicator_map *im;
1239 struct xkb_keymap *keymap = info->keymap;
1241 list_init(&unbound_leds);
1243 list_foreach_safe(led, next_led, &info->leds, entry) {
1244 if (led->groups != 0 && led->which_groups == 0)
1245 led->which_groups = XkbIM_UseEffective;
1247 if (led->which_mods == 0 && led->mods)
1248 led->which_mods = XkbIM_UseEffective;
1250 if (led->indicator == XKB_LED_INVALID) {
1251 list_append(&led->entry, &unbound_leds);
1255 im = &keymap->indicators[led->indicator - 1];
1256 im->flags = led->flags;
1257 im->which_groups = led->which_groups;
1258 im->groups = led->groups;
1259 im->which_mods = led->which_mods;
1260 im->mods.mods = led->mods;
1261 im->ctrls = led->ctrls;
1262 keymap->indicator_names[led->indicator - 1] =
1263 xkb_atom_text(keymap->ctx, led->name);
1266 list_init(&info->leds);
1268 BindIndicators(info, &unbound_leds);
1274 CompileCompatMap(XkbFile *file, struct xkb_keymap *keymap,
1275 enum merge_mode merge)
1277 xkb_group_index_t i;
1279 GroupCompatInfo *gcm;
1281 InitCompatInfo(&info, keymap, file->id);
1282 info.dflt.merge = merge;
1283 info.ledDflt.merge = merge;
1285 HandleCompatMapFile(&info, file, merge);
1287 if (info.errorCount != 0)
1291 keymap->compat_section_name = strdup(info.name);
1293 darray_init(keymap->sym_interpret);
1294 if (info.nInterps > 0) {
1295 darray_growalloc(keymap->sym_interpret, info.nInterps);
1296 CopyInterps(&info, true, XkbSI_Exactly);
1297 CopyInterps(&info, true, XkbSI_AllOf | XkbSI_NoneOf);
1298 CopyInterps(&info, true, XkbSI_AnyOf);
1299 CopyInterps(&info, true, XkbSI_AnyOfOrNone);
1300 CopyInterps(&info, false, XkbSI_Exactly);
1301 CopyInterps(&info, false, XkbSI_AllOf | XkbSI_NoneOf);
1302 CopyInterps(&info, false, XkbSI_AnyOf);
1303 CopyInterps(&info, false, XkbSI_AnyOfOrNone);
1306 for (i = 0; i < XkbNumKbdGroups; i++) {
1307 gcm = &info.groupCompat[i];
1308 if (gcm->file_id != 0 || gcm->mods != 0)
1309 keymap->groups[i].mods = gcm->mods;
1312 if (!CopyIndicatorMapDefs(&info))
1315 ClearCompatInfo(&info);
1319 ClearCompatInfo(&info);
1324 ComputeEffectiveMask(struct xkb_keymap *keymap, struct xkb_mods *mods)
1327 xkb_mod_mask_t vmask = mods->mods >> XkbNumModifiers;
1329 /* The effective mask is only real mods for now. */
1330 mods->mask = mods->mods & 0xff;
1332 for (i = 0; i < XkbNumVirtualMods; i++) {
1333 if (!(vmask & (1 << i)))
1335 mods->mask |= keymap->vmods[i];
1340 UpdateActionMods(struct xkb_keymap *keymap, union xkb_action *act,
1341 xkb_mod_mask_t rmodmask)
1344 struct xkb_mods *mods;
1346 switch (act->type) {
1348 case XkbSA_LatchMods:
1349 case XkbSA_LockMods:
1350 flags = act->mods.flags;
1351 mods = &act->mods.mods;
1355 flags = act->iso.flags;
1356 mods = &act->iso.mods;
1363 if (flags & XkbSA_UseModMapMods) {
1364 /* XXX: what's that. */
1366 mods->mods |= rmodmask;
1368 ComputeEffectiveMask(keymap, mods);
1372 * Find an interpretation which applies to this particular level, either by
1373 * finding an exact match for the symbol and modifier combination, or a
1374 * generic XKB_KEY_NoSymbol match.
1376 static struct xkb_sym_interpret *
1377 FindInterpForKey(struct xkb_keymap *keymap, struct xkb_key *key,
1378 xkb_group_index_t group, xkb_level_index_t level)
1380 struct xkb_sym_interpret *ret = NULL;
1381 struct xkb_sym_interpret *interp;
1382 const xkb_keysym_t *syms;
1385 num_syms = xkb_key_get_syms_by_level(keymap, key, group, level, &syms);
1389 darray_foreach(interp, keymap->sym_interpret) {
1393 if ((num_syms > 1 || interp->sym != syms[0]) &&
1394 interp->sym != XKB_KEY_NoSymbol)
1397 if (level == 0 || !(interp->match & XkbSI_LevelOneOnly))
1402 switch (interp->match & XkbSI_OpMask) {
1404 found = !(interp->mods & mods);
1406 case XkbSI_AnyOfOrNone:
1407 found = (!mods || (interp->mods & mods));
1410 found = !!(interp->mods & mods);
1413 found = ((interp->mods & mods) == interp->mods);
1416 found = (interp->mods == mods);
1423 if (found && interp->sym != XKB_KEY_NoSymbol)
1425 else if (found && !ret)
1433 ApplyInterpsToKey(struct xkb_keymap *keymap, struct xkb_key *key)
1435 #define INTERP_SIZE (8 * 4)
1436 struct xkb_sym_interpret *interps[INTERP_SIZE];
1437 xkb_mod_mask_t vmodmask = 0;
1439 xkb_group_index_t group;
1440 xkb_level_index_t level;
1443 /* If we've been told not to bind interps to this key, then don't. */
1444 if (key->explicit & XkbExplicitInterpretMask)
1447 for (i = 0; i < INTERP_SIZE; i++)
1450 for (group = 0; group < key->num_groups; group++) {
1451 for (level = 0; level < XkbKeyGroupWidth(keymap, key, group);
1453 i = (group * key->width) + level;
1454 if (i >= INTERP_SIZE) /* XXX FIXME */
1456 interps[i] = FindInterpForKey(keymap, key, group, level);
1462 if (num_acts && !key->actions) {
1463 key->actions = calloc(key->num_groups * key->width,
1464 sizeof(*key->actions));
1469 for (group = 0; group < key->num_groups; group++) {
1470 for (level = 0; level < XkbKeyGroupWidth(keymap, key, group);
1472 struct xkb_sym_interpret *interp;
1474 i = (group * key->width) + level;
1475 interp = interps[i];
1477 /* Infer default key behaviours from the base level. */
1478 if (group == 0 && level == 0) {
1479 if (!(key->explicit & XkbExplicitAutoRepeatMask) &&
1480 (!interp || (interp->flags & XkbSI_AutoRepeat)))
1481 key->repeats = true;
1487 if ((group == 0 && level == 0) ||
1488 !(interp->match & XkbSI_LevelOneOnly)) {
1489 if (interp->virtual_mod != XkbNoModifier)
1490 vmodmask |= (1 << interp->virtual_mod);
1493 key->actions[i] = interp->act;
1497 if (!(key->explicit & XkbExplicitVModMapMask))
1498 key->vmodmap = vmodmask;
1505 * This collects a bunch of disparate functions which was done in the server
1506 * at various points that really should've been done within xkbcomp. Turns out
1507 * your actions and types are a lot more useful when any of your modifiers
1508 * other than Shift actually do something ...
1511 UpdateModifiersFromCompat(struct xkb_keymap *keymap)
1513 xkb_mod_index_t vmod;
1514 xkb_group_index_t grp;
1515 xkb_led_index_t led;
1517 struct xkb_key *key;
1519 /* Find all the interprets for the key and bind them to actions,
1520 * which will also update the vmodmap. */
1521 xkb_foreach_key(key, keymap)
1522 if (!ApplyInterpsToKey(keymap, key))
1525 /* Update keymap->vmods, the virtual -> real mod mapping. */
1526 for (vmod = 0; vmod < XkbNumVirtualMods; vmod++)
1527 keymap->vmods[vmod] = 0;
1529 xkb_foreach_key(key, keymap) {
1533 for (vmod = 0; vmod < XkbNumVirtualMods; vmod++) {
1534 if (!(key->vmodmap & (1 << vmod)))
1536 keymap->vmods[vmod] |= key->modmap;
1540 /* Now update the level masks for all the types to reflect the vmods. */
1541 for (i = 0; i < keymap->num_types; i++) {
1542 ComputeEffectiveMask(keymap, &keymap->types[i].mods);
1544 for (j = 0; j < keymap->types[i].num_entries; j++) {
1545 ComputeEffectiveMask(keymap, &keymap->types[i].map[j].mods);
1546 ComputeEffectiveMask(keymap, &keymap->types[i].map[j].preserve);
1550 /* Update action modifiers. */
1551 xkb_foreach_key(key, keymap) {
1555 for (i = 0; i < key->num_groups * key->width; i++)
1556 UpdateActionMods(keymap, &key->actions[i], key->modmap);
1559 /* Update group modifiers. */
1560 for (grp = 0; grp < XkbNumKbdGroups; grp++)
1561 ComputeEffectiveMask(keymap, &keymap->groups[grp]);
1563 /* Update vmod -> indicator maps. */
1564 for (led = 0; led < XkbNumIndicators; led++)
1565 ComputeEffectiveMask(keymap, &keymap->indicators[led].mods);