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 ********************************************************/
28 * Copyright © 2012 Intel Corporation
29 * Copyright © 2012 Ran Benita <ran234@gmail.com>
31 * Permission is hereby granted, free of charge, to any person obtaining a
32 * copy of this software and associated documentation files (the "Software"),
33 * to deal in the Software without restriction, including without limitation
34 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
35 * and/or sell copies of the Software, and to permit persons to whom the
36 * Software is furnished to do so, subject to the following conditions:
38 * The above copyright notice and this permission notice (including the next
39 * paragraph) shall be included in all copies or substantial portions of the
42 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
45 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48 * DEALINGS IN THE SOFTWARE.
50 * Author: Daniel Stone <daniel@fooishbar.org>
51 * Ran Benita <ran234@gmail.com>
54 #include "xkbcomp-priv.h"
63 KEY_REPEAT_UNDEFINED = 0,
69 GROUP_FIELD_SYMS = (1 << 0),
70 GROUP_FIELD_ACTS = (1 << 1),
71 GROUP_FIELD_TYPE = (1 << 2),
75 KEY_FIELD_REPEAT = (1 << 0),
76 KEY_FIELD_DEFAULT_TYPE = (1 << 1),
77 KEY_FIELD_GROUPINFO = (1 << 2),
78 KEY_FIELD_VMODMAP = (1 << 3),
82 enum group_field defined;
83 darray(struct xkb_level) levels;
88 enum key_field defined;
89 enum merge_mode merge;
93 darray(GroupInfo) groups;
95 enum key_repeat repeat;
96 xkb_mod_mask_t vmodmap;
97 xkb_atom_t default_type;
99 enum xkb_range_exceed_type out_of_range_group_action;
100 xkb_layout_index_t out_of_range_group_number;
104 ClearLevelInfo(struct xkb_level *leveli)
106 if (leveli->num_syms > 1)
107 free(leveli->u.syms);
111 InitGroupInfo(GroupInfo *groupi)
113 memset(groupi, 0, sizeof(*groupi));
117 ClearGroupInfo(GroupInfo *groupi)
119 struct xkb_level *leveli;
120 darray_foreach(leveli, groupi->levels)
121 ClearLevelInfo(leveli);
122 darray_free(groupi->levels);
126 CopyGroupInfo(GroupInfo *to, const GroupInfo *from)
128 to->defined = from->defined;
129 to->type = from->type;
130 darray_init(to->levels);
131 darray_copy(to->levels, from->levels);
132 for (xkb_level_index_t j = 0; j < darray_size(to->levels); j++)
133 if (darray_item(from->levels, j).num_syms > 1)
134 darray_item(to->levels, j).u.syms =
135 memdup(darray_item(from->levels, j).u.syms,
136 darray_item(from->levels, j).num_syms,
137 sizeof(xkb_keysym_t));
141 InitKeyInfo(struct xkb_context *ctx, KeyInfo *keyi)
143 memset(keyi, 0, sizeof(*keyi));
144 keyi->merge = MERGE_OVERRIDE;
145 keyi->name = xkb_atom_intern_literal(ctx, "*");
146 keyi->out_of_range_group_action = RANGE_WRAP;
150 ClearKeyInfo(KeyInfo *keyi)
153 darray_foreach(groupi, keyi->groups)
154 ClearGroupInfo(groupi);
155 darray_free(keyi->groups);
158 /***====================================================================***/
161 enum merge_mode merge;
163 xkb_mod_index_t modifier;
171 char *name; /* e.g. pc+us+inet(evdev) */
173 enum merge_mode merge;
174 xkb_layout_index_t explicit_group;
175 darray(KeyInfo) keys;
177 ActionsInfo *actions;
178 darray(xkb_atom_t) group_names;
179 darray(ModMapEntry) modmaps;
180 struct xkb_mod_set mods;
182 struct xkb_context *ctx;
183 /* Needed for AddKeySymbols. */
184 const struct xkb_keymap *keymap;
188 InitSymbolsInfo(SymbolsInfo *info, const struct xkb_keymap *keymap,
189 ActionsInfo *actions, const struct xkb_mod_set *mods)
191 memset(info, 0, sizeof(*info));
192 info->ctx = keymap->ctx;
193 info->keymap = keymap;
194 info->merge = MERGE_OVERRIDE;
195 InitKeyInfo(keymap->ctx, &info->default_key);
196 info->actions = actions;
198 info->explicit_group = XKB_LAYOUT_INVALID;
202 ClearSymbolsInfo(SymbolsInfo *info)
206 darray_foreach(keyi, info->keys)
208 darray_free(info->keys);
209 darray_free(info->group_names);
210 darray_free(info->modmaps);
211 ClearKeyInfo(&info->default_key);
215 KeyInfoText(SymbolsInfo *info, KeyInfo *keyi)
217 return KeyNameText(info->ctx, keyi->name);
221 MergeGroups(SymbolsInfo *info, GroupInfo *into, GroupInfo *from, bool clobber,
222 bool report, xkb_layout_index_t group, xkb_atom_t key_name)
224 xkb_level_index_t i, levels_in_both;
225 struct xkb_level *level;
227 /* First find the type of the merged group. */
228 if (into->type != from->type) {
229 if (from->type == XKB_ATOM_NONE) {
230 /* it's empty for consistency with other comparisons */
232 else if (into->type == XKB_ATOM_NONE) {
233 into->type = from->type;
236 xkb_atom_t use = (clobber ? from->type : into->type);
237 xkb_atom_t ignore = (clobber ? into->type : from->type);
241 "Multiple definitions for group %d type of key %s; "
242 "Using %s, ignoring %s\n",
243 group + 1, KeyNameText(info->ctx, key_name),
244 xkb_atom_text(info->ctx, use),
245 xkb_atom_text(info->ctx, ignore));
250 into->defined |= (from->defined & GROUP_FIELD_TYPE);
252 /* Now look at the levels. */
254 if (darray_empty(from->levels)) {
259 if (darray_empty(into->levels)) {
260 from->type = into->type;
266 /* Merge the actions and syms. */
267 levels_in_both = MIN(darray_size(into->levels), darray_size(from->levels));
268 for (i = 0; i < levels_in_both; i++) {
269 struct xkb_level *intoLevel = &darray_item(into->levels, i);
270 struct xkb_level *fromLevel = &darray_item(from->levels, i);
272 if (fromLevel->action.type == ACTION_TYPE_NONE) {
273 /* it's empty for consistency with other comparisons */
275 else if (intoLevel->action.type == ACTION_TYPE_NONE) {
276 intoLevel->action = fromLevel->action;
279 union xkb_action *use, *ignore;
280 use = (clobber ? &fromLevel->action : &intoLevel->action);
281 ignore = (clobber ? &intoLevel->action : &fromLevel->action);
285 "Multiple actions for level %d/group %u on key %s; "
286 "Using %s, ignoring %s\n",
287 i + 1, group + 1, KeyNameText(info->ctx, key_name),
288 ActionTypeText(use->type),
289 ActionTypeText(ignore->type));
291 intoLevel->action = *use;
294 if (fromLevel->num_syms == 0) {
295 /* it's empty for consistency with other comparisons */
297 else if (intoLevel->num_syms == 0) {
298 intoLevel->num_syms = fromLevel->num_syms;
299 if (fromLevel->num_syms > 1)
300 intoLevel->u.syms = fromLevel->u.syms;
302 intoLevel->u.sym = fromLevel->u.sym;
303 fromLevel->num_syms = 0;
305 else if (!XkbLevelsSameSyms(fromLevel, intoLevel)) {
308 "Multiple symbols for level %d/group %u on key %s; "
309 "Using %s, ignoring %s\n",
310 i + 1, group + 1, KeyNameText(info->ctx, key_name),
311 (clobber ? "from" : "to"),
312 (clobber ? "to" : "from"));
315 ClearLevelInfo(intoLevel);
316 intoLevel->num_syms = fromLevel->num_syms;
317 if (fromLevel->num_syms > 1)
318 intoLevel->u.syms = fromLevel->u.syms;
320 intoLevel->u.sym = fromLevel->u.sym;
321 fromLevel->num_syms = 0;
325 /* If @from has extra levels, get them as well. */
326 darray_foreach_from(level, from->levels, levels_in_both) {
327 darray_append(into->levels, *level);
330 into->defined |= (from->defined & GROUP_FIELD_ACTS);
331 into->defined |= (from->defined & GROUP_FIELD_SYMS);
337 UseNewKeyField(enum key_field field, enum key_field old, enum key_field new,
338 bool clobber, bool report, enum key_field *collide)
341 return (new & field);
355 MergeKeys(SymbolsInfo *info, KeyInfo *into, KeyInfo *from, bool same_file)
357 xkb_layout_index_t i;
358 xkb_layout_index_t groups_in_both;
359 enum key_field collide = 0;
360 const int verbosity = xkb_context_get_log_verbosity(info->ctx);
361 const bool clobber = (from->merge != MERGE_AUGMENT);
362 const bool report = (same_file && verbosity > 0) || verbosity > 9;
364 if (from->merge == MERGE_REPLACE) {
367 InitKeyInfo(info->ctx, from);
371 groups_in_both = MIN(darray_size(into->groups), darray_size(from->groups));
372 for (i = 0; i < groups_in_both; i++)
374 &darray_item(into->groups, i),
375 &darray_item(from->groups, i),
376 clobber, report, i, into->name);
377 /* If @from has extra groups, just move them to @into. */
378 for (i = groups_in_both; i < darray_size(from->groups); i++) {
379 darray_append(into->groups, darray_item(from->groups, i));
380 InitGroupInfo(&darray_item(from->groups, i));
383 if (UseNewKeyField(KEY_FIELD_VMODMAP, into->defined, from->defined,
384 clobber, report, &collide)) {
385 into->vmodmap = from->vmodmap;
386 into->defined |= KEY_FIELD_VMODMAP;
388 if (UseNewKeyField(KEY_FIELD_REPEAT, into->defined, from->defined,
389 clobber, report, &collide)) {
390 into->repeat = from->repeat;
391 into->defined |= KEY_FIELD_REPEAT;
393 if (UseNewKeyField(KEY_FIELD_DEFAULT_TYPE, into->defined, from->defined,
394 clobber, report, &collide)) {
395 into->default_type = from->default_type;
396 into->defined |= KEY_FIELD_DEFAULT_TYPE;
398 if (UseNewKeyField(KEY_FIELD_GROUPINFO, into->defined, from->defined,
399 clobber, report, &collide)) {
400 into->out_of_range_group_action = from->out_of_range_group_action;
401 into->out_of_range_group_number = from->out_of_range_group_number;
402 into->defined |= KEY_FIELD_GROUPINFO;
407 "Symbol map for key %s redefined; "
408 "Using %s definition for conflicting fields\n",
409 KeyNameText(info->ctx, into->name),
410 (clobber ? "first" : "last"));
413 InitKeyInfo(info->ctx, from);
417 /* TODO: Make it so this function doesn't need the entire keymap. */
419 AddKeySymbols(SymbolsInfo *info, KeyInfo *keyi, bool same_file)
421 xkb_atom_t real_name;
425 * Don't keep aliases in the keys array; this guarantees that
426 * searching for keys to merge with by straight comparison (see the
427 * following loop) is enough, and we won't get multiple KeyInfo's
428 * for the same key because of aliases.
430 real_name = XkbResolveKeyAlias(info->keymap, keyi->name);
431 if (real_name != XKB_ATOM_NONE)
432 keyi->name = real_name;
434 darray_foreach(iter, info->keys)
435 if (iter->name == keyi->name)
436 return MergeKeys(info, iter, keyi, same_file);
438 darray_append(info->keys, *keyi);
439 InitKeyInfo(info->ctx, keyi);
444 AddModMapEntry(SymbolsInfo *info, ModMapEntry *new)
447 bool clobber = (new->merge != MERGE_AUGMENT);
449 darray_foreach(old, info->modmaps) {
450 xkb_mod_index_t use, ignore;
452 if ((new->haveSymbol != old->haveSymbol) ||
453 (new->haveSymbol && new->u.keySym != old->u.keySym) ||
454 (!new->haveSymbol && new->u.keyName != old->u.keyName))
457 if (new->modifier == old->modifier)
460 use = (clobber ? new->modifier : old->modifier);
461 ignore = (clobber ? old->modifier : new->modifier);
465 "Symbol \"%s\" added to modifier map for multiple modifiers; "
466 "Using %s, ignoring %s\n",
467 KeysymText(info->ctx, new->u.keySym),
468 ModIndexText(info->ctx, &info->mods, use),
469 ModIndexText(info->ctx, &info->mods, ignore));
472 "Key \"%s\" added to modifier map for multiple modifiers; "
473 "Using %s, ignoring %s\n",
474 KeyNameText(info->ctx, new->u.keyName),
475 ModIndexText(info->ctx, &info->mods, use),
476 ModIndexText(info->ctx, &info->mods, ignore));
482 darray_append(info->modmaps, *new);
486 /***====================================================================***/
489 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
490 enum merge_mode merge)
492 xkb_atom_t *group_name;
493 xkb_layout_index_t group_names_in_both;
495 if (from->errorCount > 0) {
496 into->errorCount += from->errorCount;
500 into->mods = from->mods;
502 if (into->name == NULL) {
503 into->name = from->name;
507 group_names_in_both = MIN(darray_size(into->group_names),
508 darray_size(from->group_names));
509 for (xkb_layout_index_t i = 0; i < group_names_in_both; i++) {
510 if (!darray_item(from->group_names, i))
513 if (merge == MERGE_AUGMENT && darray_item(into->group_names, i))
516 darray_item(into->group_names, i) = darray_item(from->group_names, i);
518 /* If @from has more, get them as well. */
519 darray_foreach_from(group_name, from->group_names, group_names_in_both)
520 darray_append(into->group_names, *group_name);
522 if (darray_empty(into->keys)) {
523 into->keys = from->keys;
524 darray_init(from->keys);
528 darray_foreach(keyi, from->keys) {
529 keyi->merge = (merge == MERGE_DEFAULT ? keyi->merge : merge);
530 if (!AddKeySymbols(into, keyi, false))
535 if (darray_empty(into->modmaps)) {
536 into->modmaps = from->modmaps;
537 darray_init(from->modmaps);
541 darray_foreach(mm, from->modmaps) {
542 mm->merge = (merge == MERGE_DEFAULT ? mm->merge : merge);
543 if (!AddModMapEntry(into, mm))
550 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge);
553 HandleIncludeSymbols(SymbolsInfo *info, IncludeStmt *include)
555 SymbolsInfo included;
557 InitSymbolsInfo(&included, info->keymap, info->actions, &info->mods);
558 included.name = include->stmt;
559 include->stmt = NULL;
561 for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
562 SymbolsInfo next_incl;
565 file = ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_SYMBOLS);
567 info->errorCount += 10;
568 ClearSymbolsInfo(&included);
572 InitSymbolsInfo(&next_incl, info->keymap, info->actions,
574 if (stmt->modifier) {
575 next_incl.explicit_group = atoi(stmt->modifier) - 1;
576 if (next_incl.explicit_group >= XKB_MAX_GROUPS) {
578 "Cannot set explicit group to %d - must be between 1..%d; "
579 "Ignoring group number\n",
580 next_incl.explicit_group + 1, XKB_MAX_GROUPS);
581 next_incl.explicit_group = info->explicit_group;
585 next_incl.explicit_group = info->explicit_group;
588 HandleSymbolsFile(&next_incl, file, MERGE_OVERRIDE);
590 MergeIncludedSymbols(&included, &next_incl, stmt->merge);
592 ClearSymbolsInfo(&next_incl);
596 MergeIncludedSymbols(info, &included, include->merge);
597 ClearSymbolsInfo(&included);
599 return (info->errorCount == 0);
606 GetGroupIndex(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
607 unsigned what, xkb_layout_index_t *ndx_rtrn)
609 const char *name = (what == SYMBOLS ? "symbols" : "actions");
611 if (arrayNdx == NULL) {
612 xkb_layout_index_t i;
614 enum group_field field = (what == SYMBOLS ?
615 GROUP_FIELD_SYMS : GROUP_FIELD_ACTS);
617 darray_enumerate(i, groupi, keyi->groups) {
618 if (!(groupi->defined & field)) {
624 if (i >= XKB_MAX_GROUPS) {
626 "Too many groups of %s for key %s (max %u); "
627 "Ignoring %s defined for extra groups\n",
628 name, KeyInfoText(info, keyi), XKB_MAX_GROUPS, name);
632 darray_resize0(keyi->groups, darray_size(keyi->groups) + 1);
633 *ndx_rtrn = darray_size(keyi->groups) - 1;
637 if (!ExprResolveGroup(info->ctx, arrayNdx, ndx_rtrn)) {
639 "Illegal group index for %s of key %s\n"
640 "Definition with non-integer array index ignored\n",
641 name, KeyInfoText(info, keyi));
646 if (*ndx_rtrn >= darray_size(keyi->groups))
647 darray_resize0(keyi->groups, *ndx_rtrn + 1);
653 AddSymbolsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
656 xkb_layout_index_t ndx;
658 xkb_level_index_t nLevels;
660 if (!GetGroupIndex(info, keyi, arrayNdx, SYMBOLS, &ndx))
663 groupi = &darray_item(keyi->groups, ndx);
666 groupi->defined |= GROUP_FIELD_SYMS;
670 if (value->expr.op != EXPR_KEYSYM_LIST) {
672 "Expected a list of symbols, found %s; "
673 "Ignoring symbols for group %u of %s\n",
674 expr_op_type_to_string(value->expr.op), ndx + 1,
675 KeyInfoText(info, keyi));
679 if (groupi->defined & GROUP_FIELD_SYMS) {
681 "Symbols for key %s, group %u already defined; "
682 "Ignoring duplicate definition\n",
683 KeyInfoText(info, keyi), ndx + 1);
687 nLevels = darray_size(value->keysym_list.symsMapIndex);
688 if (darray_size(groupi->levels) < nLevels)
689 darray_resize0(groupi->levels, nLevels);
691 groupi->defined |= GROUP_FIELD_SYMS;
693 for (xkb_level_index_t i = 0; i < nLevels; i++) {
694 unsigned int sym_index;
695 struct xkb_level *leveli = &darray_item(groupi->levels, i);
697 sym_index = darray_item(value->keysym_list.symsMapIndex, i);
698 leveli->num_syms = darray_item(value->keysym_list.symsNumEntries, i);
699 if (leveli->num_syms > 1)
700 leveli->u.syms = calloc(leveli->num_syms, sizeof(*leveli->u.syms));
702 for (unsigned j = 0; j < leveli->num_syms; j++) {
703 xkb_keysym_t keysym = darray_item(value->keysym_list.syms,
706 if (leveli->num_syms == 1) {
707 if (keysym == XKB_KEY_NoSymbol)
708 leveli->num_syms = 0;
710 leveli->u.sym = keysym;
712 else if (leveli->num_syms > 1) {
713 leveli->u.syms[j] = keysym;
722 AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
725 xkb_layout_index_t ndx;
730 if (!GetGroupIndex(info, keyi, arrayNdx, ACTIONS, &ndx))
733 groupi = &darray_item(keyi->groups, ndx);
736 groupi->defined |= GROUP_FIELD_ACTS;
740 if (value->expr.op != EXPR_ACTION_LIST) {
742 "Bad expression type (%d) for action list value; "
743 "Ignoring actions for group %u of %s\n",
744 value->expr.op, ndx, KeyInfoText(info, keyi));
748 if (groupi->defined & GROUP_FIELD_ACTS) {
750 "Actions for key %s, group %u already defined\n",
751 KeyInfoText(info, keyi), ndx);
756 for (act = value->actions.actions; act; act = (ExprDef *) act->common.next)
759 if (darray_size(groupi->levels) < nActs)
760 darray_resize0(groupi->levels, nActs);
762 groupi->defined |= GROUP_FIELD_ACTS;
764 act = value->actions.actions;
765 for (unsigned i = 0; i < nActs; i++) {
766 union xkb_action *toAct = &darray_item(groupi->levels, i).action;
768 if (!HandleActionDef(info->ctx, info->actions, &info->mods, act, toAct))
770 "Illegal action definition for %s; "
771 "Action for group %u/level %u ignored\n",
772 KeyInfoText(info, keyi), ndx + 1, i + 1);
774 act = (ExprDef *) act->common.next;
780 static const LookupEntry repeatEntries[] = {
781 { "true", KEY_REPEAT_YES },
782 { "yes", KEY_REPEAT_YES },
783 { "on", KEY_REPEAT_YES },
784 { "false", KEY_REPEAT_NO },
785 { "no", KEY_REPEAT_NO },
786 { "off", KEY_REPEAT_NO },
787 { "default", KEY_REPEAT_UNDEFINED },
792 SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
793 ExprDef *arrayNdx, ExprDef *value)
795 if (istreq(field, "type")) {
796 xkb_layout_index_t ndx;
799 if (!ExprResolveString(info->ctx, value, &val)) {
801 "The type field of a key symbol map must be a string; "
802 "Ignoring illegal type definition\n");
807 keyi->default_type = val;
808 keyi->defined |= KEY_FIELD_DEFAULT_TYPE;
810 else if (!ExprResolveGroup(info->ctx, arrayNdx, &ndx)) {
812 "Illegal group index for type of key %s; "
813 "Definition with non-integer array index ignored\n",
814 KeyInfoText(info, keyi));
819 if (ndx >= darray_size(keyi->groups))
820 darray_resize0(keyi->groups, ndx + 1);
821 darray_item(keyi->groups, ndx).type = val;
822 darray_item(keyi->groups, ndx).defined |= GROUP_FIELD_TYPE;
825 else if (istreq(field, "symbols")) {
826 return AddSymbolsToKey(info, keyi, arrayNdx, value);
828 else if (istreq(field, "actions")) {
829 return AddActionsToKey(info, keyi, arrayNdx, value);
831 else if (istreq(field, "vmods") ||
832 istreq(field, "virtualmods") ||
833 istreq(field, "virtualmodifiers")) {
836 if (!ExprResolveModMask(info->ctx, value, MOD_VIRT, &info->mods,
839 "Expected a virtual modifier mask, found %s; "
840 "Ignoring virtual modifiers definition for key %s\n",
841 expr_op_type_to_string(value->expr.op),
842 KeyInfoText(info, keyi));
846 keyi->vmodmap = mask;
847 keyi->defined |= KEY_FIELD_VMODMAP;
849 else if (istreq(field, "locking") ||
850 istreq(field, "lock") ||
851 istreq(field, "locks")) {
852 log_vrb(info->ctx, 1,
853 "Key behaviors not supported; "
854 "Ignoring locking specification for key %s\n",
855 KeyInfoText(info, keyi));
857 else if (istreq(field, "radiogroup") ||
858 istreq(field, "permanentradiogroup") ||
859 istreq(field, "allownone")) {
860 log_vrb(info->ctx, 1,
861 "Radio groups not supported; "
862 "Ignoring radio group specification for key %s\n",
863 KeyInfoText(info, keyi));
865 else if (istreq_prefix("overlay", field) ||
866 istreq_prefix("permanentoverlay", field)) {
867 log_vrb(info->ctx, 1,
868 "Overlays not supported; "
869 "Ignoring overlay specification for key %s\n",
870 KeyInfoText(info, keyi));
872 else if (istreq(field, "repeating") ||
873 istreq(field, "repeats") ||
874 istreq(field, "repeat")) {
877 if (!ExprResolveEnum(info->ctx, value, &val, repeatEntries)) {
879 "Illegal repeat setting for %s; "
880 "Non-boolean repeat setting ignored\n",
881 KeyInfoText(info, keyi));
886 keyi->defined |= KEY_FIELD_REPEAT;
888 else if (istreq(field, "groupswrap") ||
889 istreq(field, "wrapgroups")) {
892 if (!ExprResolveBoolean(info->ctx, value, &set)) {
894 "Illegal groupsWrap setting for %s; "
895 "Non-boolean value ignored\n",
896 KeyInfoText(info, keyi));
900 keyi->out_of_range_group_action = (set ? RANGE_WRAP : RANGE_SATURATE);
901 keyi->defined |= KEY_FIELD_GROUPINFO;
903 else if (istreq(field, "groupsclamp") ||
904 istreq(field, "clampgroups")) {
907 if (!ExprResolveBoolean(info->ctx, value, &set)) {
909 "Illegal groupsClamp setting for %s; "
910 "Non-boolean value ignored\n",
911 KeyInfoText(info, keyi));
915 keyi->out_of_range_group_action = (set ? RANGE_SATURATE : RANGE_WRAP);
916 keyi->defined |= KEY_FIELD_GROUPINFO;
918 else if (istreq(field, "groupsredirect") ||
919 istreq(field, "redirectgroups")) {
920 xkb_layout_index_t grp;
922 if (!ExprResolveGroup(info->ctx, value, &grp)) {
924 "Illegal group index for redirect of key %s; "
925 "Definition with non-integer group ignored\n",
926 KeyInfoText(info, keyi));
930 keyi->out_of_range_group_action = RANGE_REDIRECT;
931 keyi->out_of_range_group_number = grp - 1;
932 keyi->defined |= KEY_FIELD_GROUPINFO;
936 "Unknown field %s in a symbol interpretation; "
937 "Definition ignored\n",
946 SetGroupName(SymbolsInfo *info, ExprDef *arrayNdx, ExprDef *value)
948 xkb_layout_index_t group, group_to_use;
952 log_vrb(info->ctx, 1,
953 "You must specify an index when specifying a group name; "
954 "Group name definition without array subscript ignored\n");
958 if (!ExprResolveGroup(info->ctx, arrayNdx, &group)) {
960 "Illegal index in group name definition; "
961 "Definition with non-integer array index ignored\n");
965 if (!ExprResolveString(info->ctx, value, &name)) {
967 "Group name must be a string; "
968 "Illegal name for group %d ignored\n", group);
972 if (info->explicit_group == XKB_LAYOUT_INVALID) {
973 group_to_use = group - 1;
975 else if (group - 1 == 0) {
976 group_to_use = info->explicit_group;
980 "An explicit group was specified for the '%s' map, "
981 "but it provides a name for a group other than Group1 (%d); "
982 "Ignoring group name '%s'\n",
984 xkb_atom_text(info->ctx, name));
988 if (group_to_use >= darray_size(info->group_names))
989 darray_resize0(info->group_names, group_to_use + 1);
990 darray_item(info->group_names, group_to_use) = name;
996 HandleGlobalVar(SymbolsInfo *info, VarDef *stmt)
998 const char *elem, *field;
1002 if (!ExprResolveLhs(info->ctx, stmt->name, &elem, &field, &arrayNdx))
1005 if (elem && istreq(elem, "key")) {
1006 ret = SetSymbolsField(info, &info->default_key, field, arrayNdx,
1009 else if (!elem && (istreq(field, "name") ||
1010 istreq(field, "groupname"))) {
1011 ret = SetGroupName(info, arrayNdx, stmt->value);
1013 else if (!elem && (istreq(field, "groupswrap") ||
1014 istreq(field, "wrapgroups"))) {
1016 "Global \"groupswrap\" not supported; Ignored\n");
1019 else if (!elem && (istreq(field, "groupsclamp") ||
1020 istreq(field, "clampgroups"))) {
1022 "Global \"groupsclamp\" not supported; Ignored\n");
1025 else if (!elem && (istreq(field, "groupsredirect") ||
1026 istreq(field, "redirectgroups"))) {
1028 "Global \"groupsredirect\" not supported; Ignored\n");
1031 else if (!elem && istreq(field, "allownone")) {
1033 "Radio groups not supported; "
1034 "Ignoring \"allownone\" specification\n");
1038 ret = SetActionField(info->ctx, info->actions, &info->mods,
1039 elem, field, arrayNdx, stmt->value);
1046 HandleSymbolsBody(SymbolsInfo *info, VarDef *def, KeyInfo *keyi)
1049 const char *elem, *field;
1052 for (; def; def = (VarDef *) def->common.next) {
1053 if (def->name && def->name->expr.op == EXPR_FIELD_REF) {
1055 "Cannot set a global default value from within a key statement; "
1056 "Move statements to the global file scope\n");
1061 if (!def->value || def->value->expr.op == EXPR_KEYSYM_LIST)
1068 ok = ExprResolveLhs(info->ctx, def->name, &elem, &field,
1073 ok = SetSymbolsField(info, keyi, field, arrayNdx, def->value);
1080 SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi)
1082 xkb_layout_index_t i;
1086 if (info->explicit_group == XKB_LAYOUT_INVALID)
1089 darray_enumerate_from(i, groupi, keyi->groups, 1) {
1090 if (groupi->defined) {
1092 ClearGroupInfo(groupi);
1093 InitGroupInfo(groupi);
1099 "For the map %s an explicit group specified, "
1100 "but key %s has more than one group defined; "
1101 "All groups except first one will be ignored\n",
1102 info->name, KeyInfoText(info, keyi));
1104 darray_resize0(keyi->groups, info->explicit_group + 1);
1105 if (info->explicit_group > 0) {
1106 darray_item(keyi->groups, info->explicit_group) =
1107 darray_item(keyi->groups, 0);
1108 InitGroupInfo(&darray_item(keyi->groups, 0));
1115 HandleSymbolsDef(SymbolsInfo *info, SymbolsDef *stmt)
1119 keyi = info->default_key;
1120 darray_init(keyi.groups);
1121 darray_copy(keyi.groups, info->default_key.groups);
1122 for (xkb_layout_index_t i = 0; i < darray_size(keyi.groups); i++)
1123 CopyGroupInfo(&darray_item(keyi.groups, i),
1124 &darray_item(info->default_key.groups, i));
1125 keyi.merge = stmt->merge;
1126 keyi.name = stmt->keyName;
1128 if (!HandleSymbolsBody(info, stmt->symbols, &keyi)) {
1133 if (!SetExplicitGroup(info, &keyi)) {
1138 if (!AddKeySymbols(info, &keyi, true)) {
1147 HandleModMapDef(SymbolsInfo *info, ModMapDef *def)
1150 xkb_mod_index_t ndx;
1152 struct xkb_context *ctx = info->ctx;
1154 ndx = XkbModNameToIndex(&info->mods, def->modifier, MOD_REAL);
1155 if (ndx == XKB_MOD_INVALID) {
1157 "Illegal modifier map definition; "
1158 "Ignoring map for non-modifier \"%s\"\n",
1159 xkb_atom_text(ctx, def->modifier));
1165 tmp.merge = def->merge;
1167 for (ExprDef *key = def->keys; key; key = (ExprDef *) key->common.next) {
1170 if (key->expr.op == EXPR_VALUE &&
1171 key->expr.value_type == EXPR_TYPE_KEYNAME) {
1172 tmp.haveSymbol = false;
1173 tmp.u.keyName = key->key_name.key_name;
1175 else if (ExprResolveKeySym(ctx, key, &sym)) {
1176 tmp.haveSymbol = true;
1181 "Modmap entries may contain only key names or keysyms; "
1182 "Illegal definition for %s modifier ignored\n",
1183 ModIndexText(info->ctx, &info->mods, tmp.modifier));
1187 ok = AddModMapEntry(info, &tmp) && ok;
1193 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge)
1198 info->name = strdup_safe(file->name);
1200 for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
1201 switch (stmt->type) {
1203 ok = HandleIncludeSymbols(info, (IncludeStmt *) stmt);
1206 ok = HandleSymbolsDef(info, (SymbolsDef *) stmt);
1209 ok = HandleGlobalVar(info, (VarDef *) stmt);
1212 ok = HandleVModDef(info->ctx, &info->mods, (VModDef *) stmt, merge);
1215 ok = HandleModMapDef(info, (ModMapDef *) stmt);
1219 "Symbols files may not include other types; "
1220 "Ignoring %s\n", stmt_type_to_string(stmt->type));
1228 if (info->errorCount > 10) {
1229 log_err(info->ctx, "Abandoning symbols file \"%s\"\n",
1237 * Given a keysym @sym, return a key which generates it, or NULL.
1238 * This is used for example in a modifier map definition, such as:
1239 * modifier_map Lock { Caps_Lock };
1240 * where we want to add the Lock modifier to the modmap of the key
1241 * which matches the keysym Caps_Lock.
1242 * Since there can be many keys which generates the keysym, the key
1243 * is chosen first by lowest group in which the keysym appears, than
1244 * by lowest level and than by lowest key code.
1246 static struct xkb_key *
1247 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
1249 struct xkb_key *key;
1250 xkb_layout_index_t group;
1251 bool got_one_group, got_one_level;
1255 xkb_level_index_t level = 0;
1256 got_one_group = false;
1258 got_one_level = false;
1259 xkb_keys_foreach(key, keymap) {
1260 if (group < key->num_groups &&
1261 level < XkbKeyNumLevels(key, group)) {
1262 got_one_group = got_one_level = true;
1263 if (key->groups[group].levels[level].num_syms == 1 &&
1264 key->groups[group].levels[level].u.sym == sym)
1269 } while (got_one_level);
1271 } while (got_one_group);
1277 * Find an appropriate type for a group and return its name.
1280 * - ONE_LEVEL for width 0/1
1281 * - ALPHABETIC for 2 shift levels, with lower/upercase keysyms
1282 * - KEYPAD for keypad keys.
1283 * - TWO_LEVEL for other 2 shift level keys.
1284 * and the same for four level keys.
1286 * FIXME: Decide how to handle multiple-syms-per-level, and do it.
1289 FindAutomaticType(struct xkb_context *ctx, GroupInfo *groupi)
1291 xkb_keysym_t sym0, sym1;
1292 const xkb_level_index_t width = darray_size(groupi->levels);
1294 #define GET_SYM(level) \
1295 (darray_item(groupi->levels, level).num_syms == 0 ? \
1296 XKB_KEY_NoSymbol : \
1297 darray_item(groupi->levels, level).num_syms == 1 ? \
1298 darray_item(groupi->levels, level).u.sym : \
1299 /* num_syms > 1 */ \
1300 darray_item(groupi->levels, level).u.syms[0])
1302 if (width == 1 || width <= 0)
1303 return xkb_atom_intern_literal(ctx, "ONE_LEVEL");
1309 if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1))
1310 return xkb_atom_intern_literal(ctx, "ALPHABETIC");
1312 if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1313 return xkb_atom_intern_literal(ctx, "KEYPAD");
1315 return xkb_atom_intern_literal(ctx, "TWO_LEVEL");
1319 if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1)) {
1320 xkb_keysym_t sym2, sym3;
1322 sym3 = (width == 4 ? GET_SYM(3) : XKB_KEY_NoSymbol);
1324 if (xkb_keysym_is_lower(sym2) && xkb_keysym_is_upper(sym3))
1325 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_ALPHABETIC");
1327 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_SEMIALPHABETIC");
1330 if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1331 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_KEYPAD");
1333 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL");
1336 return XKB_ATOM_NONE;
1341 static const struct xkb_key_type *
1342 FindTypeForGroup(struct xkb_keymap *keymap, KeyInfo *keyi,
1343 xkb_layout_index_t group, bool *explicit_type)
1346 GroupInfo *groupi = &darray_item(keyi->groups, group);
1347 xkb_atom_t type_name = groupi->type;
1349 *explicit_type = true;
1351 if (type_name == XKB_ATOM_NONE) {
1352 if (keyi->default_type != XKB_ATOM_NONE) {
1353 type_name = keyi->default_type;
1356 type_name = FindAutomaticType(keymap->ctx, groupi);
1357 if (type_name != XKB_ATOM_NONE)
1358 *explicit_type = false;
1362 if (type_name == XKB_ATOM_NONE) {
1363 log_warn(keymap->ctx,
1364 "Couldn't find an automatic type for key '%s' group %d with %lu levels; "
1365 "Using the default type\n",
1366 KeyNameText(keymap->ctx, keyi->name), group + 1,
1367 (unsigned long) darray_size(groupi->levels));
1371 for (i = 0; i < keymap->num_types; i++)
1372 if (keymap->types[i].name == type_name)
1375 if (i >= keymap->num_types) {
1376 log_warn(keymap->ctx,
1377 "The type \"%s\" for key '%s' group %d was not previously defined; "
1378 "Using the default type\n",
1379 xkb_atom_text(keymap->ctx, type_name),
1380 KeyNameText(keymap->ctx, keyi->name), group + 1);
1384 return &keymap->types[i];
1388 * Index 0 is guaranteed to contain something, usually
1389 * ONE_LEVEL or at least some default one-level type.
1391 return &keymap->types[0];
1395 CopySymbolsDefToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info,
1398 struct xkb_key *key;
1400 const GroupInfo *group0;
1401 xkb_layout_index_t i;
1404 * The name is guaranteed to be real and not an alias (see
1405 * AddKeySymbols), so 'false' is safe here.
1407 key = XkbKeyByName(keymap, keyi->name, false);
1409 log_vrb(info->ctx, 5,
1410 "Key %s not found in keycodes; Symbols ignored\n",
1411 KeyInfoText(info, keyi));
1415 /* Find the range of groups we need. */
1416 key->num_groups = 0;
1417 darray_enumerate(i, groupi, keyi->groups)
1418 if (groupi->defined)
1419 key->num_groups = i + 1;
1421 if (key->num_groups <= 0)
1422 return false; /* WSGO */
1424 darray_resize(keyi->groups, key->num_groups);
1427 * If there are empty groups between non-empty ones, fill them with data
1428 * from the first group.
1429 * We can make a wrong assumption here. But leaving gaps is worse.
1431 group0 = &darray_item(keyi->groups, 0);
1432 darray_foreach_from(groupi, keyi->groups, 1) {
1433 if (groupi->defined)
1436 CopyGroupInfo(groupi, group0);
1439 key->groups = calloc(key->num_groups, sizeof(*key->groups));
1441 /* Find and assign the groups' types in the keymap. */
1442 darray_enumerate(i, groupi, keyi->groups) {
1443 const struct xkb_key_type *type;
1446 type = FindTypeForGroup(keymap, keyi, i, &explicit_type);
1448 /* Always have as many levels as the type specifies. */
1449 if (type->num_levels < darray_size(groupi->levels)) {
1450 struct xkb_level *leveli;
1452 log_vrb(info->ctx, 1,
1453 "Type \"%s\" has %d levels, but %s has %d levels; "
1454 "Ignoring extra symbols\n",
1455 xkb_atom_text(keymap->ctx, type->name), type->num_levels,
1456 KeyInfoText(info, keyi),
1457 (int) darray_size(groupi->levels));
1459 darray_foreach_from(leveli, groupi->levels, type->num_levels)
1460 ClearLevelInfo(leveli);
1462 darray_resize0(groupi->levels, type->num_levels);
1464 key->groups[i].explicit_type = explicit_type;
1465 key->groups[i].type = type;
1469 darray_enumerate(i, groupi, keyi->groups)
1470 darray_steal(groupi->levels, &key->groups[i].levels, NULL);
1472 key->out_of_range_group_number = keyi->out_of_range_group_number;
1473 key->out_of_range_group_action = keyi->out_of_range_group_action;
1475 if (keyi->defined & KEY_FIELD_VMODMAP) {
1476 key->vmodmap = keyi->vmodmap;
1477 key->explicit |= EXPLICIT_VMODMAP;
1480 if (keyi->repeat != KEY_REPEAT_UNDEFINED) {
1481 key->repeats = (keyi->repeat == KEY_REPEAT_YES);
1482 key->explicit |= EXPLICIT_REPEAT;
1485 darray_foreach(groupi, keyi->groups) {
1486 if (groupi->defined & GROUP_FIELD_ACTS) {
1487 key->explicit |= EXPLICIT_INTERP;
1496 CopyModMapDefToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info,
1499 struct xkb_key *key;
1501 if (!entry->haveSymbol) {
1502 key = XkbKeyByName(keymap, entry->u.keyName, true);
1504 log_vrb(info->ctx, 5,
1505 "Key %s not found in keycodes; "
1506 "Modifier map entry for %s not updated\n",
1507 KeyNameText(info->ctx, entry->u.keyName),
1508 ModIndexText(info->ctx, &info->mods, entry->modifier));
1513 key = FindKeyForSymbol(keymap, entry->u.keySym);
1515 log_vrb(info->ctx, 5,
1516 "Key \"%s\" not found in symbol map; "
1517 "Modifier map entry for %s not updated\n",
1518 KeysymText(info->ctx, entry->u.keySym),
1519 ModIndexText(info->ctx, &info->mods, entry->modifier));
1524 key->modmap |= (1u << entry->modifier);
1529 CopySymbolsToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info)
1534 keymap->symbols_section_name = strdup_safe(info->name);
1535 XkbEscapeMapName(keymap->symbols_section_name);
1537 keymap->mods = info->mods;
1539 darray_steal(info->group_names,
1540 &keymap->group_names, &keymap->num_group_names);
1542 darray_foreach(keyi, info->keys)
1543 if (!CopySymbolsDefToKeymap(keymap, info, keyi))
1546 if (xkb_context_get_log_verbosity(keymap->ctx) > 3) {
1547 struct xkb_key *key;
1549 xkb_keys_foreach(key, keymap) {
1550 if (key->name == XKB_ATOM_NONE)
1553 if (key->num_groups < 1)
1555 "No symbols defined for %s\n",
1556 KeyNameText(info->ctx, key->name));
1560 darray_foreach(mm, info->modmaps)
1561 if (!CopyModMapDefToKeymap(keymap, info, mm))
1564 /* XXX: If we don't ignore errorCount, things break. */
1569 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1570 enum merge_mode merge)
1573 ActionsInfo *actions;
1575 actions = NewActionsInfo();
1579 InitSymbolsInfo(&info, keymap, actions, &keymap->mods);
1580 info.default_key.merge = merge;
1582 HandleSymbolsFile(&info, file, merge);
1584 if (info.errorCount != 0)
1587 if (!CopySymbolsToKeymap(keymap, &info))
1590 ClearSymbolsInfo(&info);
1591 FreeActionsInfo(actions);
1595 FreeActionsInfo(actions);
1596 ClearSymbolsInfo(&info);