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_LOCKING_KEY = (1 << 3),
143 SI_FIELD_LEVEL_ONE_ONLY = (1 << 4),
146 typedef struct _SymInterpInfo {
147 enum si_field defined;
149 enum merge_mode merge;
152 struct xkb_sym_interpret interp;
156 LED_FIELD_INDEX = (1 << 0),
157 LED_FIELD_MODS = (1 << 1),
158 LED_FIELD_GROUPS = (1 << 2),
159 LED_FIELD_CTRLS = (1 << 3),
160 LED_FIELD_EXPLICIT = (1 << 4),
161 LED_FIELD_AUTOMATIC = (1 << 5),
162 LED_FIELD_DRIVES_KBD = (1 << 6),
165 typedef struct _LEDInfo {
166 enum led_field defined;
168 enum merge_mode merge;
172 xkb_led_index_t indicator;
174 unsigned char which_mods;
176 unsigned char which_groups;
181 typedef struct _GroupCompatInfo {
183 enum merge_mode merge;
188 typedef struct _CompatInfo {
196 GroupCompatInfo groupCompat[XkbNumKbdGroups];
200 struct xkb_keymap *keymap;
204 siText(SymInterpInfo * si, CompatInfo * info)
206 static char buf[128];
208 if (si == &info->dflt) {
209 snprintf(buf, sizeof(buf), "default");
212 snprintf(buf, sizeof(buf), "%s+%s(%s)",
213 KeysymText(si->interp.sym),
214 SIMatchText(si->interp.match),
215 ModMaskText(si->interp.mods));
221 ReportSINotArray(CompatInfo *info, SymInterpInfo *si, const char *field)
223 return ReportNotArray(info->keymap, "symbol interpretation", field,
228 ReportSIBadType(CompatInfo *info, SymInterpInfo *si, const char *field,
231 return ReportBadType(info->keymap->ctx, "symbol interpretation", field,
232 siText(si, info), wanted);
236 ReportIndicatorBadType(CompatInfo *info, LEDInfo *led,
237 const char *field, const char *wanted)
239 return ReportBadType(info->keymap->ctx, "indicator map", field,
240 xkb_atom_text(info->keymap->ctx, led->name),
245 ReportIndicatorNotArray(CompatInfo *info, LEDInfo *led,
248 return ReportNotArray(info->keymap, "indicator map", field,
249 xkb_atom_text(info->keymap->ctx, led->name));
253 ClearIndicatorMapInfo(struct xkb_context *ctx, LEDInfo * info)
255 info->name = xkb_atom_intern(ctx, "default");
256 info->indicator = XKB_LED_INVALID;
258 info->which_mods = 0;
260 info->which_groups = info->groups = 0;
265 InitCompatInfo(CompatInfo *info, struct xkb_keymap *keymap, unsigned file_id)
267 info->keymap = keymap;
269 info->file_id = file_id;
270 info->errorCount = 0;
272 list_init(&info->interps);
274 info->dflt.file_id = file_id;
275 info->dflt.defined = 0;
276 info->dflt.merge = MERGE_OVERRIDE;
277 info->dflt.interp.flags = 0;
278 info->dflt.interp.virtual_mod = XkbNoModifier;
279 memset(&info->dflt.interp.act, 0, sizeof(info->dflt.interp.act));
280 info->dflt.interp.act.type = XkbSA_NoAction;
281 ClearIndicatorMapInfo(keymap->ctx, &info->ledDflt);
282 info->ledDflt.file_id = file_id;
283 info->ledDflt.defined = 0;
284 info->ledDflt.merge = MERGE_OVERRIDE;
285 memset(&info->groupCompat[0], 0,
286 XkbNumKbdGroups * sizeof(GroupCompatInfo));
287 list_init(&info->leds);
288 InitVModInfo(&info->vmods, keymap);
292 ClearCompatInfo(CompatInfo *info)
294 SymInterpInfo *si, *next_si;
295 LEDInfo *led, *next_led;
296 struct xkb_keymap *keymap = info->keymap;
300 info->dflt.defined = 0;
301 info->dflt.merge = MERGE_AUGMENT;
302 info->dflt.interp.flags = 0;
303 info->dflt.interp.virtual_mod = XkbNoModifier;
304 memset(&info->dflt.interp.act, 0, sizeof(info->dflt.interp.act));
305 info->dflt.interp.act.type = XkbSA_NoAction;
306 ClearIndicatorMapInfo(keymap->ctx, &info->ledDflt);
308 list_foreach_safe(si, next_si, &info->interps, entry)
310 memset(&info->groupCompat[0], 0,
311 XkbNumKbdGroups * sizeof(GroupCompatInfo));
312 list_foreach_safe(led, next_led, &info->leds, entry)
314 FreeActionInfo(info->act);
317 ClearVModInfo(&info->vmods, keymap);
320 static SymInterpInfo *
321 NextInterp(CompatInfo * info)
325 si = calloc(1, sizeof(*si));
329 list_append(&si->entry, &info->interps);
335 static SymInterpInfo *
336 FindMatchingInterp(CompatInfo * info, SymInterpInfo * new)
340 list_foreach(old, &info->interps, entry)
341 if (old->interp.sym == new->interp.sym &&
342 old->interp.mods == new->interp.mods &&
343 old->interp.match == new->interp.match)
350 UseNewInterpField(enum si_field field, SymInterpInfo *old, SymInterpInfo *new,
351 int verbosity, enum si_field *collide)
353 if (!(old->defined & field))
356 if (new->defined & field) {
357 if ((old->file_id == new->file_id && verbosity > 0) || verbosity > 9)
360 if (new->merge != MERGE_AUGMENT)
368 AddInterp(CompatInfo * info, SymInterpInfo * new)
370 enum si_field collide;
373 int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
376 old = FindMatchingInterp(info, new);
378 if (new->merge == MERGE_REPLACE) {
380 if ((old->file_id == new->file_id && verbosity > 0) ||
382 log_warn(info->keymap->ctx,
383 "Multiple definitions for \"%s\"; "
384 "Earlier interpretation ignored\n",
391 if (UseNewInterpField(SI_FIELD_VIRTUAL_MOD, old, new, verbosity,
393 old->interp.virtual_mod = new->interp.virtual_mod;
394 old->defined |= SI_FIELD_VIRTUAL_MOD;
396 if (UseNewInterpField(SI_FIELD_ACTION, old, new, verbosity,
398 old->interp.act = new->interp.act;
399 old->defined |= SI_FIELD_ACTION;
401 if (UseNewInterpField(SI_FIELD_AUTO_REPEAT, old, new, verbosity,
403 old->interp.flags &= ~XkbSI_AutoRepeat;
404 old->interp.flags |= (new->interp.flags & XkbSI_AutoRepeat);
405 old->defined |= SI_FIELD_AUTO_REPEAT;
407 if (UseNewInterpField(SI_FIELD_LOCKING_KEY, old, new, verbosity,
409 old->interp.flags &= ~XkbSI_LockingKey;
410 old->interp.flags |= (new->interp.flags & XkbSI_LockingKey);
411 old->defined |= SI_FIELD_LOCKING_KEY;
413 if (UseNewInterpField(SI_FIELD_LEVEL_ONE_ONLY, old, new, verbosity,
415 old->interp.match &= ~XkbSI_LevelOneOnly;
416 old->interp.match |= (new->interp.match & XkbSI_LevelOneOnly);
417 old->defined |= SI_FIELD_LEVEL_ONE_ONLY;
421 log_warn(info->keymap->ctx,
422 "Multiple interpretations of \"%s\"; "
423 "Using %s definition for duplicate fields\n",
425 (new->merge != MERGE_AUGMENT ? "last" : "first"));
432 if ((new = NextInterp(info)) == NULL)
441 AddGroupCompat(CompatInfo *info, xkb_group_index_t group, GroupCompatInfo *new)
444 int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
446 gc = &info->groupCompat[group];
447 if (gc->mods == new->mods)
450 if ((gc->file_id == new->file_id && verbosity > 0) || verbosity > 9)
451 log_warn(info->keymap->ctx,
452 "Compat map for group %u redefined; "
453 "Using %s definition\n",
454 group + 1, (new->merge == MERGE_AUGMENT ? "old" : "new"));
456 if (new->defined && (new->merge != MERGE_AUGMENT || !gc->defined))
462 /***====================================================================***/
465 ResolveStateAndPredicate(ExprDef * expr,
467 xkb_mod_mask_t *mods_rtrn, CompatInfo * info)
470 *pred_rtrn = XkbSI_AnyOfOrNone;
475 *pred_rtrn = XkbSI_Exactly;
476 if (expr->op == EXPR_ACTION_DECL) {
477 const char *pred_txt = xkb_atom_text(info->keymap->ctx,
478 expr->value.action.name);
479 if (istreq(pred_txt, "noneof"))
480 *pred_rtrn = XkbSI_NoneOf;
481 else if (istreq(pred_txt, "anyofornone"))
482 *pred_rtrn = XkbSI_AnyOfOrNone;
483 else if (istreq(pred_txt, "anyof"))
484 *pred_rtrn = XkbSI_AnyOf;
485 else if (istreq(pred_txt, "allof"))
486 *pred_rtrn = XkbSI_AllOf;
487 else if (istreq(pred_txt, "exactly"))
488 *pred_rtrn = XkbSI_Exactly;
490 log_err(info->keymap->ctx,
491 "Illegal modifier predicate \"%s\"; Ignored\n", pred_txt);
494 expr = expr->value.action.args;
496 else if (expr->op == EXPR_IDENT) {
497 const char *pred_txt = xkb_atom_text(info->keymap->ctx,
499 if (pred_txt && istreq(pred_txt, "any")) {
500 *pred_rtrn = XkbSI_AnyOf;
506 return ExprResolveModMask(info->keymap->ctx, expr, mods_rtrn);
509 /***====================================================================***/
512 UseNewLEDField(enum led_field field, LEDInfo *old, LEDInfo *new,
513 int verbosity, enum led_field *collide)
515 if (!(old->defined & field))
518 if (new->defined & field) {
519 if ((old->file_id == new->file_id && verbosity > 0) || verbosity > 9)
522 if (new->merge != MERGE_AUGMENT)
530 AddIndicatorMap(CompatInfo *info, LEDInfo *new)
533 enum led_field collide;
534 struct xkb_context *ctx = info->keymap->ctx;
535 int verbosity = xkb_get_log_verbosity(ctx);
537 list_foreach(old, &info->leds, entry) {
538 if (old->name == new->name) {
539 if ((old->mods == new->mods) &&
540 (old->groups == new->groups) &&
541 (old->ctrls == new->ctrls) &&
542 (old->which_mods == new->which_mods) &&
543 (old->which_groups == new->which_groups)) {
544 old->defined |= new->defined;
548 if (new->merge == MERGE_REPLACE) {
549 struct list entry = old->entry;
550 if ((old->file_id == new->file_id && verbosity > 0) ||
552 log_warn(info->keymap->ctx,
553 "Map for indicator %s redefined; "
554 "Earlier definition ignored\n",
555 xkb_atom_text(ctx, old->name));
562 if (UseNewLEDField(LED_FIELD_INDEX, old, new, verbosity,
564 old->indicator = new->indicator;
565 old->defined |= LED_FIELD_INDEX;
567 if (UseNewLEDField(LED_FIELD_MODS, old, new, verbosity,
569 old->which_mods = new->which_mods;
570 old->mods = new->mods;
571 old->defined |= LED_FIELD_MODS;
573 if (UseNewLEDField(LED_FIELD_GROUPS, old, new, verbosity,
575 old->which_groups = new->which_groups;
576 old->groups = new->groups;
577 old->defined |= LED_FIELD_GROUPS;
579 if (UseNewLEDField(LED_FIELD_CTRLS, old, new, verbosity,
581 old->ctrls = new->ctrls;
582 old->defined |= LED_FIELD_CTRLS;
584 if (UseNewLEDField(LED_FIELD_EXPLICIT, old, new, verbosity,
586 old->flags &= ~XkbIM_NoExplicit;
587 old->flags |= (new->flags & XkbIM_NoExplicit);
588 old->defined |= LED_FIELD_EXPLICIT;
590 if (UseNewLEDField(LED_FIELD_AUTOMATIC, old, new, verbosity,
592 old->flags &= ~XkbIM_NoAutomatic;
593 old->flags |= (new->flags & XkbIM_NoAutomatic);
594 old->defined |= LED_FIELD_AUTOMATIC;
596 if (UseNewLEDField(LED_FIELD_DRIVES_KBD, old, new, verbosity,
598 old->flags &= ~XkbIM_LEDDrivesKB;
599 old->flags |= (new->flags & XkbIM_LEDDrivesKB);
600 old->defined |= LED_FIELD_DRIVES_KBD;
604 log_warn(info->keymap->ctx,
605 "Map for indicator %s redefined; "
606 "Using %s definition for duplicate fields\n",
607 xkb_atom_text(ctx, old->name),
608 (new->merge == MERGE_AUGMENT ? "first" : "last"));
616 old = malloc(sizeof(*old));
618 log_wsgo(info->keymap->ctx,
619 "Couldn't allocate indicator map; "
620 "Map for indicator %s not compiled\n",
621 xkb_atom_text(ctx, new->name));
626 list_append(&old->entry, &info->leds);
632 MergeIncludedCompatMaps(CompatInfo * into, CompatInfo * from,
633 enum merge_mode merge)
636 LEDInfo *led, *next_led;
637 GroupCompatInfo *gcm;
640 if (from->errorCount > 0) {
641 into->errorCount += from->errorCount;
644 if (into->name == NULL) {
645 into->name = from->name;
649 list_foreach(si, &from->interps, entry) {
650 if (merge != MERGE_DEFAULT)
652 if (!AddInterp(into, si))
656 for (i = 0, gcm = &from->groupCompat[0]; i < XkbNumKbdGroups;
658 if (merge != MERGE_DEFAULT)
660 if (!AddGroupCompat(into, i, gcm))
664 list_foreach_safe(led, next_led, &from->leds, entry) {
665 led->merge = (merge == MERGE_DEFAULT ? led->merge : merge);
666 if (!AddIndicatorMap(into, led))
672 HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge);
675 HandleIncludeCompatMap(CompatInfo *info, IncludeStmt *stmt)
677 enum merge_mode merge = MERGE_DEFAULT;
679 CompatInfo included, next_incl;
681 InitCompatInfo(&included, info->keymap, info->file_id);
684 included.name = stmt->stmt;
688 for (; stmt; stmt = stmt->next_incl) {
689 if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_COMPAT,
691 info->errorCount += 10;
692 ClearCompatInfo(&included);
696 InitCompatInfo(&next_incl, info->keymap, rtrn->id);
697 next_incl.file_id = rtrn->id;
698 next_incl.dflt = info->dflt;
699 next_incl.dflt.file_id = rtrn->id;
700 next_incl.dflt.merge = merge;
701 next_incl.ledDflt.file_id = rtrn->id;
702 next_incl.ledDflt.merge = merge;
703 next_incl.act = info->act;
705 HandleCompatMapFile(&next_incl, rtrn, MERGE_OVERRIDE);
707 MergeIncludedCompatMaps(&included, &next_incl, merge);
709 next_incl.act = NULL;
711 ClearCompatInfo(&next_incl);
715 MergeIncludedCompatMaps(info, &included, merge);
716 ClearCompatInfo(&included);
718 return (info->errorCount == 0);
721 static const LookupEntry useModMapValues[] = {
730 SetInterpField(CompatInfo *info, SymInterpInfo *si, const char *field,
731 ExprDef *arrayNdx, ExprDef *value)
733 struct xkb_keymap *keymap = info->keymap;
736 if (istreq(field, "action")) {
738 return ReportSINotArray(info, si, field);
740 if (!HandleActionDef(value, keymap, &si->interp.act, info->act))
743 si->defined |= SI_FIELD_ACTION;
745 else if (istreq(field, "virtualmodifier") ||
746 istreq(field, "virtualmod")) {
748 return ReportSINotArray(info, si, field);
750 if (!ResolveVirtualModifier(value, keymap, &ndx, &info->vmods))
751 return ReportSIBadType(info, si, field, "virtual modifier");
753 si->interp.virtual_mod = ndx;
754 si->defined |= SI_FIELD_VIRTUAL_MOD;
756 else if (istreq(field, "repeat")) {
760 return ReportSINotArray(info, si, field);
762 if (!ExprResolveBoolean(keymap->ctx, value, &set))
763 return ReportSIBadType(info, si, field, "boolean");
766 si->interp.flags |= XkbSI_AutoRepeat;
768 si->interp.flags &= ~XkbSI_AutoRepeat;
770 si->defined |= SI_FIELD_AUTO_REPEAT;
772 else if (istreq(field, "locking")) {
776 return ReportSINotArray(info, si, field);
778 if (!ExprResolveBoolean(keymap->ctx, value, &set))
779 return ReportSIBadType(info, si, field, "boolean");
782 si->interp.flags |= XkbSI_LockingKey;
784 si->interp.flags &= ~XkbSI_LockingKey;
786 si->defined |= SI_FIELD_LOCKING_KEY;
788 else if (istreq(field, "usemodmap") ||
789 istreq(field, "usemodmapmods")) {
793 return ReportSINotArray(info, si, field);
795 if (!ExprResolveEnum(keymap->ctx, value, &val, useModMapValues))
796 return ReportSIBadType(info, si, field, "level specification");
799 si->interp.match |= XkbSI_LevelOneOnly;
801 si->interp.match &= ~XkbSI_LevelOneOnly;
803 si->defined |= SI_FIELD_LEVEL_ONE_ONLY;
806 return ReportBadField(keymap, "symbol interpretation", field,
813 static const LookupEntry modComponentNames[] = {
814 {"base", XkbIM_UseBase},
815 {"latched", XkbIM_UseLatched},
816 {"locked", XkbIM_UseLocked},
817 {"effective", XkbIM_UseEffective},
818 {"compat", XkbIM_UseCompat},
819 {"any", XkbIM_UseAnyMods},
823 static const LookupEntry groupComponentNames[] = {
824 {"base", XkbIM_UseBase},
825 {"latched", XkbIM_UseLatched},
826 {"locked", XkbIM_UseLocked},
827 {"effective", XkbIM_UseEffective},
828 {"any", XkbIM_UseAnyGroup},
833 static const LookupEntry groupNames[] = {
848 SetIndicatorMapField(CompatInfo *info, LEDInfo *led,
849 const char *field, ExprDef *arrayNdx, ExprDef *value)
852 struct xkb_keymap *keymap = info->keymap;
854 if (istreq(field, "modifiers") || istreq(field, "mods")) {
856 return ReportIndicatorNotArray(info, led, field);
858 if (!ExprResolveVModMask(keymap, value, &led->mods))
859 return ReportIndicatorBadType(info, led, field, "modifier mask");
861 led->defined |= LED_FIELD_MODS;
863 else if (istreq(field, "groups")) {
867 return ReportIndicatorNotArray(info, led, field);
869 if (!ExprResolveMask(keymap->ctx, value, &mask, groupNames))
870 return ReportIndicatorBadType(info, led, field, "group mask");
873 led->defined |= LED_FIELD_GROUPS;
875 else if (istreq(field, "controls") || istreq(field, "ctrls")) {
879 return ReportIndicatorNotArray(info, led, field);
881 if (!ExprResolveMask(keymap->ctx, value, &mask, ctrlNames))
882 return ReportIndicatorBadType(info, led, field,
886 led->defined |= LED_FIELD_CTRLS;
888 else if (istreq(field, "allowexplicit")) {
892 return ReportIndicatorNotArray(info, led, field);
894 if (!ExprResolveBoolean(keymap->ctx, value, &set))
895 return ReportIndicatorBadType(info, led, field, "boolean");
898 led->flags &= ~XkbIM_NoExplicit;
900 led->flags |= XkbIM_NoExplicit;
902 led->defined |= LED_FIELD_EXPLICIT;
904 else if (istreq(field, "whichmodstate") ||
905 istreq(field, "whichmodifierstate")) {
909 return ReportIndicatorNotArray(info, led, field);
911 if (!ExprResolveMask(keymap->ctx, value, &mask, modComponentNames))
912 return ReportIndicatorBadType(info, led, field,
913 "mask of modifier state components");
915 led->which_mods = mask;
917 else if (istreq(field, "whichgroupstate")) {
921 return ReportIndicatorNotArray(info, led, field);
923 if (!ExprResolveMask(keymap->ctx, value, &mask, groupComponentNames))
924 return ReportIndicatorBadType(info, led, field,
925 "mask of group state components");
927 led->which_groups = mask;
929 else if (istreq(field, "driveskbd") ||
930 istreq(field, "driveskeyboard") ||
931 istreq(field, "leddriveskbd") ||
932 istreq(field, "leddriveskeyboard") ||
933 istreq(field, "indicatordriveskbd") ||
934 istreq(field, "indicatordriveskeyboard")) {
938 return ReportIndicatorNotArray(info, led, field);
940 if (!ExprResolveBoolean(keymap->ctx, value, &set))
941 return ReportIndicatorBadType(info, led, field, "boolean");
944 led->flags |= XkbIM_LEDDrivesKB;
946 led->flags &= ~XkbIM_LEDDrivesKB;
948 led->defined |= LED_FIELD_DRIVES_KBD;
950 else if (istreq(field, "index")) {
954 return ReportIndicatorNotArray(info, led, field);
956 if (!ExprResolveInteger(keymap->ctx, value, &ndx))
957 return ReportIndicatorBadType(info, led, field,
960 if (ndx < 1 || ndx > 32) {
961 log_err(info->keymap->ctx,
962 "Illegal indicator index %d (range 1..%d); "
963 "Index definition for %s indicator ignored\n",
964 ndx, XkbNumIndicators,
965 xkb_atom_text(keymap->ctx, led->name));
969 led->indicator = (xkb_led_index_t) ndx;
970 led->defined |= LED_FIELD_INDEX;
973 log_err(info->keymap->ctx,
974 "Unknown field %s in map for %s indicator; "
975 "Definition ignored\n",
976 field, xkb_atom_text(keymap->ctx, led->name));
984 HandleInterpVar(CompatInfo *info, VarDef *stmt)
986 const char *elem, *field;
990 if (!ExprResolveLhs(info->keymap->ctx, stmt->name, &elem, &field, &ndx))
991 ret = 0; /* internal error, already reported */
992 else if (elem && istreq(elem, "interpret"))
993 ret = SetInterpField(info, &info->dflt, field, ndx, stmt->value);
994 else if (elem && istreq(elem, "indicator"))
995 ret = SetIndicatorMapField(info, &info->ledDflt, field, ndx,
998 ret = SetActionField(info->keymap, elem, field, ndx, stmt->value,
1004 HandleInterpBody(CompatInfo *info, VarDef *def, SymInterpInfo *si)
1007 const char *elem, *field;
1010 for (; def != NULL; def = (VarDef *) def->common.next) {
1011 if (def->name && def->name->op == EXPR_FIELD_REF) {
1012 ok = HandleInterpVar(info, def);
1015 ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
1018 ok = SetInterpField(info, si, field, arrayNdx, def->value);
1025 HandleInterpDef(CompatInfo *info, InterpDef *def, enum merge_mode merge)
1027 unsigned pred, mods;
1030 if (!ResolveStateAndPredicate(def->match, &pred, &mods, info)) {
1031 log_err(info->keymap->ctx,
1032 "Couldn't determine matching modifiers; "
1033 "Symbol interpretation ignored\n");
1036 if (def->merge != MERGE_DEFAULT)
1041 if (!LookupKeysym(def->sym, &si.interp.sym)) {
1042 log_err(info->keymap->ctx,
1043 "Could not resolve keysym %s; "
1044 "Symbol interpretation ignored\n",
1048 si.interp.match = pred & XkbSI_OpMask;
1049 si.interp.mods = mods;
1050 if (!HandleInterpBody(info, def->def, &si)) {
1055 if (!AddInterp(info, &si)) {
1063 HandleGroupCompatDef(CompatInfo *info, GroupCompatDef *def,
1064 enum merge_mode merge)
1066 GroupCompatInfo tmp;
1068 merge = (def->merge == MERGE_DEFAULT ? merge : def->merge);
1070 if (def->group < 1 || def->group > XkbNumKbdGroups) {
1071 log_err(info->keymap->ctx,
1072 "Keyboard group must be in the range 1..%u; "
1073 "Compatibility map for illegal group %u ignored\n",
1074 XkbNumKbdGroups, def->group);
1078 tmp.file_id = info->file_id;
1081 if (!ExprResolveVModMask(info->keymap, def->def, &tmp.mods)) {
1082 log_err(info->keymap->ctx,
1083 "Expected a modifier mask in group compatibility definition; "
1084 "Ignoring illegal compatibility map for group %u\n",
1090 return AddGroupCompat(info, def->group - 1, &tmp);
1094 HandleIndicatorMapDef(CompatInfo *info, IndicatorMapDef *def,
1095 enum merge_mode merge)
1101 if (def->merge != MERGE_DEFAULT)
1104 led = info->ledDflt;
1106 led.name = def->name;
1109 for (var = def->body; var != NULL; var = (VarDef *) var->common.next) {
1110 const char *elem, *field;
1112 if (!ExprResolveLhs(info->keymap->ctx, var->name, &elem, &field,
1119 log_err(info->keymap->ctx,
1120 "Cannot set defaults for \"%s\" element in indicator map; "
1121 "Assignment to %s.%s ignored\n", elem, elem, field);
1125 ok = SetIndicatorMapField(info, &led, field, arrayNdx,
1131 return AddIndicatorMap(info, &led);
1137 HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge)
1142 merge = (merge == MERGE_DEFAULT ? MERGE_AUGMENT : merge);
1145 info->name = strdup_safe(file->name);
1147 for (stmt = file->defs; stmt; stmt = stmt->next) {
1148 switch (stmt->type) {
1150 ok = HandleIncludeCompatMap(info, (IncludeStmt *) stmt);
1153 ok = HandleInterpDef(info, (InterpDef *) stmt, merge);
1155 case STMT_GROUP_COMPAT:
1156 ok = HandleGroupCompatDef(info, (GroupCompatDef *) stmt, merge);
1158 case STMT_INDICATOR_MAP:
1159 ok = HandleIndicatorMapDef(info, (IndicatorMapDef *) stmt, merge);
1162 ok = HandleInterpVar(info, (VarDef *) stmt);
1165 ok = HandleVModDef((VModDef *) stmt, info->keymap, merge,
1169 log_err(info->keymap->ctx,
1170 "Interpretation files may not include other types; "
1171 "Ignoring %s\n", StmtTypeToString(stmt->type));
1179 if (info->errorCount > 10) {
1180 log_err(info->keymap->ctx,
1181 "Abandoning compatibility map \"%s\"\n", file->topName);
1188 CopyInterps(CompatInfo *info, bool needSymbol, unsigned pred)
1192 list_foreach(si, &info->interps, entry) {
1193 if (((si->interp.match & XkbSI_OpMask) != pred) ||
1194 (needSymbol && si->interp.sym == XKB_KEY_NoSymbol) ||
1195 (!needSymbol && si->interp.sym != XKB_KEY_NoSymbol))
1198 darray_append(info->keymap->sym_interpret, si->interp);
1203 BindIndicators(CompatInfo *info, struct list *unbound_leds)
1206 LEDInfo *led, *next_led;
1207 struct xkb_indicator_map *map;
1208 struct xkb_keymap *keymap = info->keymap;
1210 list_foreach(led, unbound_leds, entry) {
1211 if (led->indicator == XKB_LED_INVALID) {
1212 for (i = 0; i < XkbNumIndicators; i++) {
1213 if (keymap->indicator_names[i] &&
1214 streq(keymap->indicator_names[i],
1215 xkb_atom_text(keymap->ctx, led->name))) {
1216 led->indicator = i + 1;
1223 list_foreach(led, unbound_leds, entry) {
1224 if (led->indicator == XKB_LED_INVALID) {
1225 for (i = 0; i < XkbNumIndicators; i++) {
1226 if (keymap->indicator_names[i] == NULL) {
1227 keymap->indicator_names[i] =
1228 xkb_atom_text(keymap->ctx, led->name);
1229 led->indicator = i + 1;
1234 if (led->indicator == XKB_LED_INVALID) {
1235 log_err(info->keymap->ctx,
1236 "No unnamed indicators found; "
1237 "Virtual indicator map \"%s\" not bound\n",
1238 xkb_atom_text(keymap->ctx, led->name));
1244 list_foreach_safe(led, next_led, unbound_leds, entry) {
1245 if (led->indicator == XKB_LED_INVALID) {
1250 if (!streq(keymap->indicator_names[led->indicator - 1],
1251 xkb_atom_text(keymap->ctx, led->name))) {
1252 const char *old = keymap->indicator_names[led->indicator - 1];
1253 log_err(info->keymap->ctx,
1254 "Multiple names bound to indicator %d; "
1255 "Using %s, ignoring %s\n",
1256 led->indicator, old,
1257 xkb_atom_text(keymap->ctx, led->name));
1262 map = &keymap->indicators[led->indicator - 1];
1263 map->flags = led->flags;
1264 map->which_groups = led->which_groups;
1265 map->groups = led->groups;
1266 map->which_mods = led->which_mods;
1267 map->mods.mods = led->mods;
1268 map->ctrls = led->ctrls;
1272 list_init(unbound_leds);
1276 CopyIndicatorMapDefs(CompatInfo *info)
1278 LEDInfo *led, *next_led;
1279 struct list unbound_leds;
1280 struct xkb_indicator_map *im;
1281 struct xkb_keymap *keymap = info->keymap;
1283 list_init(&unbound_leds);
1285 list_foreach_safe(led, next_led, &info->leds, entry) {
1286 if (led->groups != 0 && led->which_groups == 0)
1287 led->which_groups = XkbIM_UseEffective;
1289 if (led->which_mods == 0 && led->mods)
1290 led->which_mods = XkbIM_UseEffective;
1292 if (led->indicator == XKB_LED_INVALID) {
1293 list_append(&led->entry, &unbound_leds);
1297 im = &keymap->indicators[led->indicator - 1];
1298 im->flags = led->flags;
1299 im->which_groups = led->which_groups;
1300 im->groups = led->groups;
1301 im->which_mods = led->which_mods;
1302 im->mods.mods = led->mods;
1303 im->ctrls = led->ctrls;
1304 keymap->indicator_names[led->indicator - 1] =
1305 xkb_atom_text(keymap->ctx, led->name);
1308 list_init(&info->leds);
1310 BindIndicators(info, &unbound_leds);
1316 CompileCompatMap(XkbFile *file, struct xkb_keymap *keymap,
1317 enum merge_mode merge)
1319 xkb_group_index_t i;
1321 GroupCompatInfo *gcm;
1323 InitCompatInfo(&info, keymap, file->id);
1324 info.dflt.merge = merge;
1325 info.ledDflt.merge = merge;
1327 HandleCompatMapFile(&info, file, merge);
1329 if (info.errorCount != 0)
1332 darray_init(keymap->sym_interpret);
1333 darray_growalloc(keymap->sym_interpret, info.nInterps);
1336 keymap->compat_section_name = strdup(info.name);
1338 if (info.nInterps > 0) {
1339 CopyInterps(&info, true, XkbSI_Exactly);
1340 CopyInterps(&info, true, XkbSI_AllOf | XkbSI_NoneOf);
1341 CopyInterps(&info, true, XkbSI_AnyOf);
1342 CopyInterps(&info, true, XkbSI_AnyOfOrNone);
1343 CopyInterps(&info, false, XkbSI_Exactly);
1344 CopyInterps(&info, false, XkbSI_AllOf | XkbSI_NoneOf);
1345 CopyInterps(&info, false, XkbSI_AnyOf);
1346 CopyInterps(&info, false, XkbSI_AnyOfOrNone);
1349 for (i = 0, gcm = &info.groupCompat[0]; i < XkbNumKbdGroups;
1351 if (gcm->file_id != 0 || gcm->mods != 0)
1352 keymap->groups[i].mods = gcm->mods;
1355 if (!CopyIndicatorMapDefs(&info))
1358 ClearCompatInfo(&info);
1362 ClearCompatInfo(&info);
1367 ComputeEffectiveMask(struct xkb_keymap *keymap, struct xkb_mods *mods)
1370 xkb_mod_mask_t vmask = mods->mods >> XkbNumModifiers;
1372 /* The effective mask is only real mods for now. */
1373 mods->mask = mods->mods & 0xff;
1375 for (i = 0; i < XkbNumVirtualMods; i++) {
1376 if (!(vmask & (1 << i)))
1378 mods->mask |= keymap->vmods[i];
1383 UpdateActionMods(struct xkb_keymap *keymap, union xkb_action *act,
1384 xkb_mod_mask_t rmodmask)
1387 struct xkb_mods *mods;
1389 switch (act->type) {
1391 case XkbSA_LatchMods:
1392 case XkbSA_LockMods:
1393 flags = act->mods.flags;
1394 mods = &act->mods.mods;
1398 flags = act->iso.flags;
1399 mods = &act->iso.mods;
1406 if (flags & XkbSA_UseModMapMods) {
1407 /* XXX: what's that. */
1409 mods->mods |= rmodmask;
1411 ComputeEffectiveMask(keymap, mods);
1415 * Find an interpretation which applies to this particular level, either by
1416 * finding an exact match for the symbol and modifier combination, or a
1417 * generic XKB_KEY_NoSymbol match.
1419 static struct xkb_sym_interpret *
1420 FindInterpForKey(struct xkb_keymap *keymap, struct xkb_key *key,
1421 xkb_group_index_t group, xkb_level_index_t level)
1423 struct xkb_sym_interpret *ret = NULL;
1424 struct xkb_sym_interpret *interp;
1425 const xkb_keysym_t *syms;
1428 num_syms = xkb_key_get_syms_by_level(keymap, key, group, level, &syms);
1432 darray_foreach(interp, keymap->sym_interpret) {
1436 if ((num_syms > 1 || interp->sym != syms[0]) &&
1437 interp->sym != XKB_KEY_NoSymbol)
1440 if (level == 0 || !(interp->match & XkbSI_LevelOneOnly))
1445 switch (interp->match & XkbSI_OpMask) {
1447 found = !(interp->mods & mods);
1449 case XkbSI_AnyOfOrNone:
1450 found = (!mods || (interp->mods & mods));
1453 found = !!(interp->mods & mods);
1456 found = ((interp->mods & mods) == interp->mods);
1459 found = (interp->mods == mods);
1466 if (found && interp->sym != XKB_KEY_NoSymbol)
1468 else if (found && !ret)
1478 ApplyInterpsToKey(struct xkb_keymap *keymap, struct xkb_key *key)
1480 #define INTERP_SIZE (8 * 4)
1481 struct xkb_sym_interpret *interps[INTERP_SIZE];
1482 xkb_mod_mask_t vmodmask = 0;
1484 xkb_group_index_t group;
1485 xkb_level_index_t level;
1488 /* If we've been told not to bind interps to this key, then don't. */
1489 if (key->explicit & XkbExplicitInterpretMask)
1492 for (i = 0; i < INTERP_SIZE; i++)
1495 for (group = 0; group < key->num_groups; group++) {
1496 for (level = 0; level < XkbKeyGroupWidth(keymap, key, group);
1498 i = (group * key->width) + level;
1499 if (i >= INTERP_SIZE) /* XXX FIXME */
1501 interps[i] = FindInterpForKey(keymap, key, group, level);
1507 if (num_acts && !key->actions) {
1508 key->actions = calloc(key->num_groups * key->width,
1509 sizeof(*key->actions));
1514 for (group = 0; group < key->num_groups; group++) {
1515 for (level = 0; level < XkbKeyGroupWidth(keymap, key, group);
1517 struct xkb_sym_interpret *interp;
1519 i = (group * key->width) + level;
1520 interp = interps[i];
1522 /* Infer default key behaviours from the base level. */
1523 if (group == 0 && level == 0) {
1524 if (!(key->explicit & XkbExplicitAutoRepeatMask) &&
1525 (!interp || (interp->flags & XkbSI_AutoRepeat)))
1526 key->repeats = true;
1532 if ((group == 0 && level == 0) ||
1533 !(interp->match & XkbSI_LevelOneOnly)) {
1534 if (interp->virtual_mod != XkbNoModifier)
1535 vmodmask |= (1 << interp->virtual_mod);
1538 key->actions[i] = interp->act;
1542 if (!(key->explicit & XkbExplicitVModMapMask))
1543 key->vmodmap = vmodmask;
1550 * This collects a bunch of disparate functions which was done in the server
1551 * at various points that really should've been done within xkbcomp. Turns out
1552 * your actions and types are a lot more useful when any of your modifiers
1553 * other than Shift actually do something ...
1556 UpdateModifiersFromCompat(struct xkb_keymap *keymap)
1558 xkb_mod_index_t vmod;
1559 xkb_group_index_t grp;
1560 xkb_led_index_t led;
1562 struct xkb_key *key;
1564 /* Find all the interprets for the key and bind them to actions,
1565 * which will also update the vmodmap. */
1566 xkb_foreach_key(key, keymap)
1567 if (!ApplyInterpsToKey(keymap, key))
1570 /* Update keymap->vmods, the virtual -> real mod mapping. */
1571 for (vmod = 0; vmod < XkbNumVirtualMods; vmod++)
1572 keymap->vmods[vmod] = 0;
1574 xkb_foreach_key(key, keymap) {
1578 for (vmod = 0; vmod < XkbNumVirtualMods; vmod++) {
1579 if (!(key->vmodmap & (1 << vmod)))
1581 keymap->vmods[vmod] |= key->modmap;
1585 /* Now update the level masks for all the types to reflect the vmods. */
1586 for (i = 0; i < keymap->num_types; i++) {
1587 ComputeEffectiveMask(keymap, &keymap->types[i].mods);
1589 for (j = 0; j < keymap->types[i].num_entries; j++) {
1590 ComputeEffectiveMask(keymap, &keymap->types[i].map[j].mods);
1591 ComputeEffectiveMask(keymap, &keymap->types[i].map[j].preserve);
1595 /* Update action modifiers. */
1596 xkb_foreach_key(key, keymap) {
1600 for (i = 0; i < key->num_groups * key->width; i++)
1601 UpdateActionMods(keymap, &key->actions[i], key->modmap);
1604 /* Update group modifiers. */
1605 for (grp = 0; grp < XkbNumKbdGroups; grp++)
1606 ComputeEffectiveMask(keymap, &keymap->groups[grp]);
1608 /* Update vmod -> indicator maps. */
1609 for (led = 0; led < XkbNumIndicators; led++)
1610 ComputeEffectiveMask(keymap, &keymap->indicators[led].mods);