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"
64 KEY_REPEAT_UNDEFINED = 0,
70 GROUP_FIELD_SYMS = (1 << 0),
71 GROUP_FIELD_ACTS = (1 << 1),
72 GROUP_FIELD_TYPE = (1 << 2),
76 KEY_FIELD_REPEAT = (1 << 0),
77 KEY_FIELD_TYPE_DFLT = (1 << 1),
78 KEY_FIELD_GROUPINFO = (1 << 2),
79 KEY_FIELD_VMODMAP = (1 << 3),
83 enum group_field defined;
84 darray(struct xkb_level) levels;
89 enum key_field defined;
91 enum merge_mode merge;
95 darray(GroupInfo) groups;
97 enum key_repeat repeat;
98 xkb_mod_mask_t vmodmap;
101 enum xkb_range_exceed_type out_of_range_group_action;
102 xkb_layout_index_t out_of_range_group_number;
106 ClearLevelInfo(struct xkb_level *leveli)
108 if (leveli->num_syms > 1)
109 free(leveli->u.syms);
113 InitGroupInfo(GroupInfo *groupi)
115 memset(groupi, 0, sizeof(*groupi));
119 ClearGroupInfo(GroupInfo *groupi)
121 struct xkb_level *leveli;
122 darray_foreach(leveli, groupi->levels)
123 ClearLevelInfo(leveli);
124 darray_free(groupi->levels);
128 CopyGroupInfo(GroupInfo *to, const GroupInfo *from)
131 to->defined = from->defined;
132 to->type = from->type;
133 darray_init(to->levels);
134 darray_copy(to->levels, from->levels);
135 for (j = 0; j < darray_size(to->levels); j++)
136 if (darray_item(from->levels, j).num_syms > 1)
137 darray_item(to->levels, j).u.syms =
138 memdup(darray_item(from->levels, j).u.syms,
139 darray_item(from->levels, j).num_syms,
140 sizeof(xkb_keysym_t));
144 InitKeyInfo(struct xkb_context *ctx, KeyInfo *keyi, unsigned file_id)
146 memset(keyi, 0, sizeof(*keyi));
147 keyi->file_id = file_id;
148 keyi->merge = MERGE_OVERRIDE;
149 keyi->name = xkb_atom_intern(ctx, "*");
150 keyi->out_of_range_group_action = RANGE_WRAP;
154 ClearKeyInfo(KeyInfo *keyi)
157 darray_foreach(groupi, keyi->groups)
158 ClearGroupInfo(groupi);
159 darray_free(keyi->groups);
162 /***====================================================================***/
165 enum merge_mode merge;
167 xkb_mod_index_t modifier;
175 char *name; /* e.g. pc+us+inet(evdev) */
178 enum merge_mode merge;
179 xkb_layout_index_t explicit_group;
180 darray(KeyInfo) keys;
182 ActionsInfo *actions;
183 darray_xkb_atom_t group_names;
184 darray(ModMapEntry) modMaps;
186 struct xkb_keymap *keymap;
190 InitSymbolsInfo(SymbolsInfo *info, struct xkb_keymap *keymap,
191 unsigned file_id, ActionsInfo *actions)
193 memset(info, 0, sizeof(*info));
194 info->keymap = keymap;
195 info->file_id = file_id;
196 info->merge = MERGE_OVERRIDE;
197 InitKeyInfo(keymap->ctx, &info->dflt, file_id);
198 info->actions = actions;
199 info->explicit_group = XKB_LAYOUT_INVALID;
203 ClearSymbolsInfo(SymbolsInfo * info)
207 darray_foreach(keyi, info->keys)
209 darray_free(info->keys);
210 darray_free(info->group_names);
211 darray_free(info->modMaps);
212 ClearKeyInfo(&info->dflt);
216 KeyInfoText(SymbolsInfo *info, KeyInfo *keyi)
218 return KeyNameText(info->keymap->ctx, keyi->name);
222 MergeGroups(SymbolsInfo *info, GroupInfo *into, GroupInfo *from, bool clobber,
223 bool report, xkb_layout_index_t group, xkb_atom_t key_name)
225 xkb_level_index_t i, levels_in_both;
226 struct xkb_context *ctx = info->keymap->ctx;
228 /* First find the type of the merged group. */
229 if (into->type != from->type) {
230 if (from->type == XKB_ATOM_NONE) {
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);
240 log_warn(info->keymap->ctx,
241 "Multiple definitions for group %d type of key %s; "
242 "Using %s, ignoring %s\n",
243 group + 1, KeyNameText(ctx, key_name),
244 xkb_atom_text(ctx, use), xkb_atom_text(ctx, ignore));
249 into->defined |= (from->defined & GROUP_FIELD_TYPE);
251 /* Now look at the levels. */
253 if (darray_empty(from->levels)) {
258 if (darray_empty(into->levels)) {
259 from->type = into->type;
265 /* Merge the actions and syms. */
266 levels_in_both = MIN(darray_size(into->levels), darray_size(from->levels));
267 for (i = 0; i < levels_in_both; i++) {
268 struct xkb_level *intoLevel = &darray_item(into->levels, i);
269 struct xkb_level *fromLevel = &darray_item(from->levels, i);
271 if (fromLevel->action.type == ACTION_TYPE_NONE) {
273 else if (intoLevel->action.type == ACTION_TYPE_NONE) {
274 intoLevel->action = fromLevel->action;
277 union xkb_action *use, *ignore;
278 use = (clobber ? &fromLevel->action : &intoLevel->action);
279 ignore = (clobber ? &intoLevel->action : &fromLevel->action);
283 "Multiple actions for level %d/group %u on key %s; "
284 "Using %s, ignoring %s\n",
285 i + 1, group + 1, KeyNameText(ctx, key_name),
286 ActionTypeText(use->type),
287 ActionTypeText(ignore->type));
289 intoLevel->action = *use;
292 if (fromLevel->num_syms == 0) {
294 else if (intoLevel->num_syms == 0) {
295 intoLevel->num_syms = fromLevel->num_syms;
296 if (fromLevel->num_syms > 1)
297 intoLevel->u.syms = fromLevel->u.syms;
299 intoLevel->u.sym = fromLevel->u.sym;
300 fromLevel->num_syms = 0;
305 "Multiple symbols for level %d/group %u on key %s; "
306 "Using %s, ignoring %s\n",
307 i + 1, group + 1, KeyNameText(ctx, key_name),
308 (clobber ? "from" : "to"),
309 (clobber ? "to" : "from"));
312 ClearLevelInfo(intoLevel);
313 intoLevel->num_syms = fromLevel->num_syms;
314 if (fromLevel->num_syms > 1)
315 intoLevel->u.syms = fromLevel->u.syms;
317 intoLevel->u.sym = fromLevel->u.sym;
318 fromLevel->num_syms = 0;
322 /* If @from has extra levels, get them as well. */
323 for (i = levels_in_both; i < darray_size(from->levels); i++) {
324 darray_append(into->levels, darray_item(from->levels, i));
325 darray_item(from->levels, i).num_syms = 0;
327 into->defined |= (from->defined & GROUP_FIELD_ACTS);
328 into->defined |= (from->defined & GROUP_FIELD_SYMS);
334 UseNewKeyField(enum key_field field, enum key_field old, enum key_field new,
335 bool clobber, bool report, enum key_field *collide)
338 return (new & field);
352 MergeKeys(SymbolsInfo *info, KeyInfo *into, KeyInfo *from)
354 xkb_layout_index_t i;
355 xkb_layout_index_t groups_in_both;
356 enum key_field collide = 0;
357 bool clobber, report;
358 int verbosity = xkb_context_get_log_verbosity(info->keymap->ctx);
360 if (from->merge == MERGE_REPLACE) {
363 InitKeyInfo(info->keymap->ctx, from, info->file_id);
367 clobber = (from->merge != MERGE_AUGMENT);
368 report = (verbosity > 9 ||
369 (into->file_id == from->file_id && verbosity > 0));
371 groups_in_both = MIN(darray_size(into->groups),
372 darray_size(from->groups));
373 for (i = 0; i < groups_in_both; i++)
375 &darray_item(into->groups, i),
376 &darray_item(from->groups, i),
377 clobber, report, i, into->name);
378 /* If @from has extra groups, just move them to @into. */
379 for (i = groups_in_both; i < darray_size(from->groups); i++) {
380 darray_append(into->groups, darray_item(from->groups, i));
381 InitGroupInfo(&darray_item(from->groups, i));
384 if (UseNewKeyField(KEY_FIELD_VMODMAP, into->defined, from->defined,
385 clobber, report, &collide)) {
386 into->vmodmap = from->vmodmap;
387 into->defined |= KEY_FIELD_VMODMAP;
389 if (UseNewKeyField(KEY_FIELD_REPEAT, into->defined, from->defined,
390 clobber, report, &collide)) {
391 into->repeat = from->repeat;
392 into->defined |= KEY_FIELD_REPEAT;
394 if (UseNewKeyField(KEY_FIELD_TYPE_DFLT, into->defined, from->defined,
395 clobber, report, &collide)) {
396 into->dfltType = from->dfltType;
397 into->defined |= KEY_FIELD_TYPE_DFLT;
399 if (UseNewKeyField(KEY_FIELD_GROUPINFO, into->defined, from->defined,
400 clobber, report, &collide)) {
401 into->out_of_range_group_action = from->out_of_range_group_action;
402 into->out_of_range_group_number = from->out_of_range_group_number;
403 into->defined |= KEY_FIELD_GROUPINFO;
407 log_warn(info->keymap->ctx,
408 "Symbol map for key %s redefined; "
409 "Using %s definition for conflicting fields\n",
410 KeyNameText(info->keymap->ctx, into->name),
411 (clobber ? "first" : "last"));
414 InitKeyInfo(info->keymap->ctx, from, info->file_id);
419 AddKeySymbols(SymbolsInfo *info, KeyInfo *keyi)
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 if (FindKeyNameForAlias(info->keymap, keyi->name, &real_name))
431 keyi->name = real_name;
433 darray_foreach(iter, info->keys)
434 if (iter->name == keyi->name)
435 return MergeKeys(info, iter, keyi);
437 darray_append(info->keys, *keyi);
438 InitKeyInfo(info->keymap->ctx, keyi, info->file_id);
443 AddModMapEntry(SymbolsInfo * info, ModMapEntry * new)
448 clobber = (new->merge != MERGE_AUGMENT);
449 darray_foreach(mm, info->modMaps) {
450 if (new->haveSymbol && mm->haveSymbol
451 && (new->u.keySym == mm->u.keySym)) {
452 xkb_mod_index_t use, ignore;
453 if (mm->modifier != new->modifier) {
456 ignore = mm->modifier;
460 ignore = new->modifier;
462 log_err(info->keymap->ctx,
463 "%s added to symbol map for multiple modifiers; "
464 "Using %s, ignoring %s.\n",
465 KeysymText(new->u.keySym), ModIndexText(use),
466 ModIndexText(ignore));
471 if ((!new->haveSymbol) && (!mm->haveSymbol) &&
472 (new->u.keyName == mm->u.keyName)) {
473 xkb_mod_index_t use, ignore;
474 if (mm->modifier != new->modifier) {
477 ignore = mm->modifier;
481 ignore = new->modifier;
483 log_err(info->keymap->ctx,
484 "Key %s added to map for multiple modifiers; "
485 "Using %s, ignoring %s.\n",
486 KeyNameText(info->keymap->ctx, new->u.keyName),
487 ModIndexText(use), ModIndexText(ignore));
494 darray_append(info->modMaps, *new);
498 /***====================================================================***/
501 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
502 enum merge_mode merge)
507 xkb_atom_t *group_name;
508 xkb_layout_index_t group_names_in_both;
510 if (from->errorCount > 0) {
511 into->errorCount += from->errorCount;
515 if (into->name == NULL) {
516 into->name = from->name;
520 group_names_in_both = MIN(darray_size(into->group_names),
521 darray_size(from->group_names));
522 for (i = 0; i < group_names_in_both; i++) {
523 if (!darray_item(from->group_names, i))
526 if (merge == MERGE_AUGMENT && darray_item(into->group_names, i))
529 darray_item(into->group_names, i) = darray_item(from->group_names, i);
531 /* If @from has more, get them as well. */
532 darray_foreach_from(group_name, from->group_names, group_names_in_both)
533 darray_append(into->group_names, *group_name);
535 darray_foreach(keyi, from->keys) {
536 merge = (merge == MERGE_DEFAULT ? keyi->merge : merge);
537 if (!AddKeySymbols(into, keyi))
541 darray_foreach(mm, from->modMaps) {
542 mm->merge = (merge == MERGE_DEFAULT ? mm->merge : merge);
543 if (!AddModMapEntry(into, mm))
549 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge);
552 HandleIncludeSymbols(SymbolsInfo *info, IncludeStmt *stmt)
554 enum merge_mode merge = MERGE_DEFAULT;
556 SymbolsInfo included, next_incl;
558 InitSymbolsInfo(&included, info->keymap, info->file_id, info->actions);
561 included.name = stmt->stmt;
565 for (; stmt; stmt = stmt->next_incl) {
566 if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_SYMBOLS,
568 info->errorCount += 10;
569 ClearSymbolsInfo(&included);
573 InitSymbolsInfo(&next_incl, info->keymap, rtrn->id, info->actions);
574 next_incl.merge = next_incl.dflt.merge = MERGE_OVERRIDE;
575 if (stmt->modifier) {
576 next_incl.explicit_group = atoi(stmt->modifier) - 1;
577 if (next_incl.explicit_group >= XKB_NUM_GROUPS) {
578 log_err(info->keymap->ctx,
579 "Cannot set explicit group to %d - must be between 1..%d; "
580 "Ignoring group number\n",
581 next_incl.explicit_group + 1, XKB_NUM_GROUPS);
582 next_incl.explicit_group = info->explicit_group;
586 next_incl.explicit_group = info->explicit_group;
589 HandleSymbolsFile(&next_incl, rtrn, MERGE_OVERRIDE);
591 MergeIncludedSymbols(&included, &next_incl, merge);
593 ClearSymbolsInfo(&next_incl);
597 MergeIncludedSymbols(info, &included, merge);
598 ClearSymbolsInfo(&included);
600 return (info->errorCount == 0);
607 GetGroupIndex(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
608 unsigned what, xkb_layout_index_t *ndx_rtrn)
610 const char *name = (what == SYMBOLS ? "symbols" : "actions");
612 if (arrayNdx == NULL) {
613 xkb_layout_index_t i;
615 enum group_field field = (what == SYMBOLS ?
616 GROUP_FIELD_SYMS : GROUP_FIELD_ACTS);
618 darray_enumerate(i, groupi, keyi->groups) {
619 if (!(groupi->defined & field)) {
625 if (i >= XKB_NUM_GROUPS) {
626 log_err(info->keymap->ctx,
627 "Too many groups of %s for key %s (max %u); "
628 "Ignoring %s defined for extra groups\n",
629 name, KeyInfoText(info, keyi), XKB_NUM_GROUPS + 1, name);
633 darray_resize0(keyi->groups, darray_size(keyi->groups) + 1);
634 *ndx_rtrn = darray_size(keyi->groups) - 1;
638 if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, ndx_rtrn)) {
639 log_err(info->keymap->ctx,
640 "Illegal group index for %s of key %s\n"
641 "Definition with non-integer array index ignored\n",
642 name, KeyInfoText(info, keyi));
647 if (*ndx_rtrn >= darray_size(keyi->groups))
648 darray_resize0(keyi->groups, *ndx_rtrn + 1);
654 LookupKeysym(const char *str, xkb_keysym_t *sym_rtrn)
658 if (!str || istreq(str, "any") || istreq(str, "nosymbol")) {
659 *sym_rtrn = XKB_KEY_NoSymbol;
663 if (istreq(str, "none") || istreq(str, "voidsymbol")) {
664 *sym_rtrn = XKB_KEY_VoidSymbol;
668 sym = xkb_keysym_from_name(str);
669 if (sym != XKB_KEY_NoSymbol) {
678 AddSymbolsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
681 xkb_layout_index_t ndx;
683 xkb_level_index_t nLevels;
687 if (!GetGroupIndex(info, keyi, arrayNdx, SYMBOLS, &ndx))
690 groupi = &darray_item(keyi->groups, ndx);
693 groupi->defined |= GROUP_FIELD_SYMS;
697 if (value->op != EXPR_KEYSYM_LIST) {
698 log_err(info->keymap->ctx,
699 "Expected a list of symbols, found %s; "
700 "Ignoring symbols for group %u of %s\n",
701 expr_op_type_to_string(value->op), ndx + 1,
702 KeyInfoText(info, keyi));
706 if (groupi->defined & GROUP_FIELD_SYMS) {
707 log_err(info->keymap->ctx,
708 "Symbols for key %s, group %u already defined; "
709 "Ignoring duplicate definition\n",
710 KeyInfoText(info, keyi), ndx + 1);
714 nLevels = darray_size(value->value.list.symsMapIndex);
715 if (darray_size(groupi->levels) < nLevels)
716 darray_resize0(groupi->levels, nLevels);
718 groupi->defined |= GROUP_FIELD_SYMS;
720 for (i = 0; i < nLevels; i++) {
721 unsigned int sym_index;
722 struct xkb_level *leveli = &darray_item(groupi->levels, i);
724 sym_index = darray_item(value->value.list.symsMapIndex, i);
725 leveli->num_syms = darray_item(value->value.list.symsNumEntries, i);
726 if (leveli->num_syms > 1)
727 leveli->u.syms = calloc(leveli->num_syms, sizeof(*leveli->u.syms));
729 for (j = 0; j < leveli->num_syms; j++) {
730 char *sym_name = darray_item(value->value.list.syms,
734 if (!LookupKeysym(sym_name, &keysym)) {
735 const char *group_name = "unnamed";
737 if (ndx < darray_size(info->group_names) &&
738 darray_item(info->group_names, ndx))
739 group_name = xkb_atom_text(info->keymap->ctx,
740 darray_item(info->group_names,
743 log_warn(info->keymap->ctx,
744 "Could not resolve keysym %s for key %s, group %u (%s), level %u\n",
745 sym_name, KeyInfoText(info, keyi), ndx + 1,
748 ClearLevelInfo(leveli);
749 leveli->num_syms = 0;
753 if (leveli->num_syms == 1) {
754 if (keysym == XKB_KEY_NoSymbol)
755 leveli->num_syms = 0;
757 leveli->u.sym = keysym;
759 else if (leveli->num_syms > 1) {
760 leveli->u.syms[j] = keysym;
769 AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
773 xkb_layout_index_t ndx;
777 union xkb_action *toAct;
779 if (!GetGroupIndex(info, keyi, arrayNdx, ACTIONS, &ndx))
782 groupi = &darray_item(keyi->groups, ndx);
785 groupi->defined |= GROUP_FIELD_ACTS;
789 if (value->op != EXPR_ACTION_LIST) {
790 log_wsgo(info->keymap->ctx,
791 "Bad expression type (%d) for action list value; "
792 "Ignoring actions for group %u of %s\n",
793 value->op, ndx, KeyInfoText(info, keyi));
797 if (groupi->defined & GROUP_FIELD_ACTS) {
798 log_wsgo(info->keymap->ctx,
799 "Actions for key %s, group %u already defined\n",
800 KeyInfoText(info, keyi), ndx);
805 for (act = value->value.child; act; act = (ExprDef *) act->common.next)
808 if (darray_size(groupi->levels) < nActs)
809 darray_resize0(groupi->levels, nActs);
811 groupi->defined |= GROUP_FIELD_ACTS;
813 act = value->value.child;
814 for (i = 0; i < nActs; i++) {
815 toAct = &darray_item(groupi->levels, i).action;
817 if (!HandleActionDef(act, info->keymap, toAct, info->actions))
818 log_err(info->keymap->ctx,
819 "Illegal action definition for %s; "
820 "Action for group %u/level %u ignored\n",
821 KeyInfoText(info, keyi), ndx + 1, i + 1);
823 act = (ExprDef *) act->common.next;
829 static const LookupEntry repeatEntries[] = {
830 { "true", KEY_REPEAT_YES },
831 { "yes", KEY_REPEAT_YES },
832 { "on", KEY_REPEAT_YES },
833 { "false", KEY_REPEAT_NO },
834 { "no", KEY_REPEAT_NO },
835 { "off", KEY_REPEAT_NO },
836 { "default", KEY_REPEAT_UNDEFINED },
841 SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
842 ExprDef *arrayNdx, ExprDef *value)
845 struct xkb_context *ctx = info->keymap->ctx;
847 if (istreq(field, "type")) {
848 xkb_layout_index_t ndx;
851 if (!ExprResolveString(ctx, value, &val))
853 "The type field of a key symbol map must be a string; "
854 "Ignoring illegal type definition\n");
856 if (arrayNdx == NULL) {
857 keyi->dfltType = val;
858 keyi->defined |= KEY_FIELD_TYPE_DFLT;
860 else if (!ExprResolveGroup(ctx, arrayNdx, &ndx)) {
862 "Illegal group index for type of key %s; "
863 "Definition with non-integer array index ignored\n",
864 KeyInfoText(info, keyi));
869 if (ndx >= darray_size(keyi->groups))
870 darray_resize0(keyi->groups, ndx + 1);
871 darray_item(keyi->groups, ndx).type = val;
872 darray_item(keyi->groups, ndx).defined |= GROUP_FIELD_TYPE;
875 else if (istreq(field, "symbols"))
876 return AddSymbolsToKey(info, keyi, arrayNdx, value);
877 else if (istreq(field, "actions"))
878 return AddActionsToKey(info, keyi, arrayNdx, value);
879 else if (istreq(field, "vmods") ||
880 istreq(field, "virtualmods") ||
881 istreq(field, "virtualmodifiers")) {
884 ok = ExprResolveVModMask(info->keymap, value, &mask);
886 keyi->vmodmap = (mask >> XKB_NUM_CORE_MODS) & 0xffff;
887 keyi->defined |= KEY_FIELD_VMODMAP;
890 log_err(info->keymap->ctx,
891 "Expected a virtual modifier mask, found %s; "
892 "Ignoring virtual modifiers definition for key %s\n",
893 expr_op_type_to_string(value->op),
894 KeyInfoText(info, keyi));
897 else if (istreq(field, "locking") ||
898 istreq(field, "lock") ||
899 istreq(field, "locks")) {
900 log_err(info->keymap->ctx,
901 "Key behaviors not supported; "
902 "Ignoring locking specification for key %s\n",
903 KeyInfoText(info, keyi));
905 else if (istreq(field, "radiogroup") ||
906 istreq(field, "permanentradiogroup") ||
907 istreq(field, "allownone")) {
908 log_err(info->keymap->ctx,
909 "Radio groups not supported; "
910 "Ignoring radio group specification for key %s\n",
911 KeyInfoText(info, keyi));
913 else if (istreq_prefix("overlay", field) ||
914 istreq_prefix("permanentoverlay", field)) {
915 log_err(info->keymap->ctx,
916 "Overlays not supported; "
917 "Ignoring overlay specification for key %s\n",
918 KeyInfoText(info, keyi));
920 else if (istreq(field, "repeating") ||
921 istreq(field, "repeats") ||
922 istreq(field, "repeat")) {
925 ok = ExprResolveEnum(ctx, value, &val, repeatEntries);
927 log_err(info->keymap->ctx,
928 "Illegal repeat setting for %s; "
929 "Non-boolean repeat setting ignored\n",
930 KeyInfoText(info, keyi));
934 keyi->defined |= KEY_FIELD_REPEAT;
936 else if (istreq(field, "groupswrap") ||
937 istreq(field, "wrapgroups")) {
940 if (!ExprResolveBoolean(ctx, value, &set)) {
941 log_err(info->keymap->ctx,
942 "Illegal groupsWrap setting for %s; "
943 "Non-boolean value ignored\n",
944 KeyInfoText(info, keyi));
949 keyi->out_of_range_group_action = RANGE_WRAP;
951 keyi->out_of_range_group_action = RANGE_SATURATE;
953 keyi->defined |= KEY_FIELD_GROUPINFO;
955 else if (istreq(field, "groupsclamp") ||
956 istreq(field, "clampgroups")) {
959 if (!ExprResolveBoolean(ctx, value, &set)) {
960 log_err(info->keymap->ctx,
961 "Illegal groupsClamp setting for %s; "
962 "Non-boolean value ignored\n",
963 KeyInfoText(info, keyi));
968 keyi->out_of_range_group_action = RANGE_SATURATE;
970 keyi->out_of_range_group_action = RANGE_WRAP;
972 keyi->defined |= KEY_FIELD_GROUPINFO;
974 else if (istreq(field, "groupsredirect") ||
975 istreq(field, "redirectgroups")) {
976 xkb_layout_index_t grp;
978 if (!ExprResolveGroup(ctx, value, &grp)) {
979 log_err(info->keymap->ctx,
980 "Illegal group index for redirect of key %s; "
981 "Definition with non-integer group ignored\n",
982 KeyInfoText(info, keyi));
986 keyi->out_of_range_group_action = RANGE_REDIRECT;
987 keyi->out_of_range_group_number = grp - 1;
988 keyi->defined |= KEY_FIELD_GROUPINFO;
991 log_err(info->keymap->ctx,
992 "Unknown field %s in a symbol interpretation; "
993 "Definition ignored\n",
1002 SetGroupName(SymbolsInfo *info, ExprDef *arrayNdx, ExprDef *value)
1004 xkb_layout_index_t grp, grp_to_use;
1008 log_vrb(info->keymap->ctx, 1,
1009 "You must specify an index when specifying a group name; "
1010 "Group name definition without array subscript ignored\n");
1014 if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, &grp)) {
1015 log_err(info->keymap->ctx,
1016 "Illegal index in group name definition; "
1017 "Definition with non-integer array index ignored\n");
1021 if (!ExprResolveString(info->keymap->ctx, value, &name)) {
1022 log_err(info->keymap->ctx,
1023 "Group name must be a string; "
1024 "Illegal name for group %d ignored\n", grp);
1028 grp_to_use = XKB_LAYOUT_INVALID;
1029 if (info->explicit_group == XKB_LAYOUT_INVALID) {
1030 grp_to_use = grp - 1;
1032 else if (grp - 1 == 0) {
1033 grp_to_use = info->explicit_group;
1036 log_warn(info->keymap->ctx,
1037 "An explicit group was specified for the '%s' map, "
1038 "but it provides a name for a group other than Group1 (%d); "
1039 "Ignoring group name '%s'\n",
1041 xkb_atom_text(info->keymap->ctx, name));
1045 if (grp_to_use >= darray_size(info->group_names))
1046 darray_resize0(info->group_names, grp_to_use + 1);
1047 darray_item(info->group_names, grp_to_use) = name;
1052 HandleGlobalVar(SymbolsInfo *info, VarDef *stmt)
1054 const char *elem, *field;
1058 if (ExprResolveLhs(info->keymap->ctx, stmt->name, &elem, &field,
1060 return 0; /* internal error, already reported */
1061 if (elem && istreq(elem, "key")) {
1062 ret = SetSymbolsField(info, &info->dflt, field, arrayNdx,
1065 else if (!elem && (istreq(field, "name") ||
1066 istreq(field, "groupname"))) {
1067 ret = SetGroupName(info, arrayNdx, stmt->value);
1069 else if (!elem && (istreq(field, "groupswrap") ||
1070 istreq(field, "wrapgroups"))) {
1071 log_err(info->keymap->ctx,
1072 "Global \"groupswrap\" not supported; Ignored\n");
1075 else if (!elem && (istreq(field, "groupsclamp") ||
1076 istreq(field, "clampgroups"))) {
1077 log_err(info->keymap->ctx,
1078 "Global \"groupsclamp\" not supported; Ignored\n");
1081 else if (!elem && (istreq(field, "groupsredirect") ||
1082 istreq(field, "redirectgroups"))) {
1083 log_err(info->keymap->ctx,
1084 "Global \"groupsredirect\" not supported; Ignored\n");
1087 else if (!elem && istreq(field, "allownone")) {
1088 log_err(info->keymap->ctx,
1089 "Radio groups not supported; "
1090 "Ignoring \"allownone\" specification\n");
1094 ret = SetActionField(info->keymap, elem, field, arrayNdx, stmt->value,
1102 HandleSymbolsBody(SymbolsInfo *info, VarDef *def, KeyInfo *keyi)
1105 const char *elem, *field;
1108 for (; def; def = (VarDef *) def->common.next) {
1109 if (def->name && def->name->op == EXPR_FIELD_REF) {
1110 log_err(info->keymap->ctx,
1111 "Cannot set a global default value from within a key statement; "
1112 "Move statements to the global file scope\n");
1117 if (!def->value || def->value->op == EXPR_KEYSYM_LIST)
1124 ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
1129 ok = SetSymbolsField(info, keyi, field, arrayNdx, def->value);
1136 SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi)
1138 xkb_layout_index_t i;
1142 if (info->explicit_group == XKB_LAYOUT_INVALID)
1145 darray_enumerate_from(i, groupi, keyi->groups, 1) {
1146 if (groupi->defined) {
1148 ClearGroupInfo(groupi);
1149 InitGroupInfo(groupi);
1154 log_warn(info->keymap->ctx,
1155 "For the map %s an explicit group specified, "
1156 "but key %s has more than one group defined; "
1157 "All groups except first one will be ignored\n",
1158 info->name, KeyInfoText(info, keyi));
1160 darray_resize0(keyi->groups, info->explicit_group + 1);
1161 if (info->explicit_group > 0) {
1162 darray_item(keyi->groups, info->explicit_group) =
1163 darray_item(keyi->groups, 0);
1164 InitGroupInfo(&darray_item(keyi->groups, 0));
1171 HandleSymbolsDef(SymbolsInfo *info, SymbolsDef *stmt)
1174 xkb_layout_index_t i;
1177 darray_init(keyi.groups);
1178 darray_copy(keyi.groups, info->dflt.groups);
1179 for (i = 0; i < darray_size(keyi.groups); i++)
1180 CopyGroupInfo(&darray_item(keyi.groups, i),
1181 &darray_item(info->dflt.groups, i));
1182 keyi.merge = stmt->merge;
1183 keyi.name = stmt->keyName;
1185 if (!HandleSymbolsBody(info, (VarDef *) stmt->symbols, &keyi)) {
1190 if (!SetExplicitGroup(info, &keyi)) {
1195 if (!AddKeySymbols(info, &keyi)) {
1204 HandleModMapDef(SymbolsInfo *info, ModMapDef *def)
1208 xkb_mod_index_t ndx;
1210 struct xkb_context *ctx = info->keymap->ctx;
1212 ndx = ModNameToIndex(xkb_atom_text(ctx, def->modifier));
1213 if (ndx == XKB_MOD_INVALID) {
1214 log_err(info->keymap->ctx,
1215 "Illegal modifier map definition; "
1216 "Ignoring map for non-modifier \"%s\"\n",
1217 xkb_atom_text(ctx, def->modifier));
1224 for (key = def->keys; key != NULL; key = (ExprDef *) key->common.next) {
1227 if (key->op == EXPR_VALUE && key->value_type == EXPR_TYPE_KEYNAME) {
1228 tmp.haveSymbol = false;
1229 tmp.u.keyName = key->value.keyName;
1231 else if (ExprResolveKeySym(ctx, key, &sym)) {
1232 tmp.haveSymbol = true;
1236 log_err(info->keymap->ctx,
1237 "Modmap entries may contain only key names or keysyms; "
1238 "Illegal definition for %s modifier ignored\n",
1239 ModIndexText(tmp.modifier));
1243 ok = AddModMapEntry(info, &tmp) && ok;
1249 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge)
1255 info->name = strdup_safe(file->name);
1258 for (stmt = file->defs; stmt; stmt = stmt->next) {
1259 switch (stmt->type) {
1261 ok = HandleIncludeSymbols(info, (IncludeStmt *) stmt);
1264 ok = HandleSymbolsDef(info, (SymbolsDef *) stmt);
1267 ok = HandleGlobalVar(info, (VarDef *) stmt);
1270 ok = HandleVModDef(info->keymap, (VModDef *) stmt);
1273 ok = HandleModMapDef(info, (ModMapDef *) stmt);
1276 log_err(info->keymap->ctx,
1277 "Interpretation files may not include other types; "
1278 "Ignoring %s\n", stmt_type_to_string(stmt->type));
1286 if (info->errorCount > 10) {
1287 log_err(info->keymap->ctx, "Abandoning symbols file \"%s\"\n",
1295 * Given a keysym @sym, return a key which generates it, or NULL.
1296 * This is used for example in a modifier map definition, such as:
1297 * modifier_map Lock { Caps_Lock };
1298 * where we want to add the Lock modifier to the modmap of the key
1299 * which matches the keysym Caps_Lock.
1300 * Since there can be many keys which generates the keysym, the key
1301 * is chosen first by lowest group in which the keysym appears, than
1302 * by lowest level and than by lowest key code.
1304 static struct xkb_key *
1305 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
1307 struct xkb_key *key, *ret = NULL;
1308 xkb_layout_index_t group, min_group = UINT32_MAX;
1309 xkb_level_index_t level, min_level = UINT16_MAX;
1311 xkb_foreach_key(key, keymap) {
1312 for (group = 0; group < key->num_groups; group++) {
1313 for (level = 0; level < XkbKeyGroupWidth(key, group); level++) {
1314 if (key->groups[group].levels[level].num_syms != 1 ||
1315 key->groups[group].levels[level].u.sym != sym)
1319 * If the keysym was found in a group or level > 0, we must
1320 * keep looking since we might find a key in which the keysym
1321 * is in a lower group or level.
1323 if (group < min_group ||
1324 (group == min_group && level < min_level)) {
1326 if (group == 0 && level == 0) {
1342 * Find an appropriate type for a group and return its name.
1345 * - ONE_LEVEL for width 0/1
1346 * - ALPHABETIC for 2 shift levels, with lower/upercase keysyms
1347 * - KEYPAD for keypad keys.
1348 * - TWO_LEVEL for other 2 shift level keys.
1349 * and the same for four level keys.
1351 * FIXME: Decide how to handle multiple-syms-per-level, and do it.
1354 FindAutomaticType(struct xkb_context *ctx, GroupInfo *groupi)
1356 xkb_keysym_t sym0, sym1, sym2, sym3;
1357 xkb_level_index_t width = darray_size(groupi->levels);
1359 #define GET_SYM(level) \
1360 (darray_item(groupi->levels, level).num_syms == 0 ? \
1361 XKB_KEY_NoSymbol : \
1362 darray_item(groupi->levels, level).num_syms == 1 ? \
1363 darray_item(groupi->levels, level).u.sym : \
1364 /* num_syms > 1 */ \
1365 darray_item(groupi->levels, level).u.syms[0])
1367 if (width == 1 || width <= 0)
1368 return xkb_atom_intern(ctx, "ONE_LEVEL");
1374 if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1))
1375 return xkb_atom_intern(ctx, "ALPHABETIC");
1377 if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1378 return xkb_atom_intern(ctx, "KEYPAD");
1380 return xkb_atom_intern(ctx, "TWO_LEVEL");
1384 if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1)) {
1386 sym3 = (width == 4 ? GET_SYM(3) : XKB_KEY_NoSymbol);
1388 if (xkb_keysym_is_lower(sym2) && xkb_keysym_is_upper(sym3))
1389 return xkb_atom_intern(ctx, "FOUR_LEVEL_ALPHABETIC");
1391 return xkb_atom_intern(ctx, "FOUR_LEVEL_SEMIALPHABETIC");
1394 if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1395 return xkb_atom_intern(ctx, "FOUR_LEVEL_KEYPAD");
1397 return xkb_atom_intern(ctx, "FOUR_LEVEL");
1400 return XKB_ATOM_NONE;
1405 static const struct xkb_key_type *
1406 FindTypeForGroup(struct xkb_keymap *keymap, KeyInfo *keyi,
1407 xkb_layout_index_t group, bool *explicit_type)
1410 GroupInfo *groupi = &darray_item(keyi->groups, group);
1411 xkb_atom_t type_name = groupi->type;
1413 *explicit_type = true;
1415 if (type_name == XKB_ATOM_NONE) {
1416 if (keyi->dfltType != XKB_ATOM_NONE) {
1417 type_name = keyi->dfltType;
1420 type_name = FindAutomaticType(keymap->ctx, groupi);
1421 if (type_name != XKB_ATOM_NONE)
1422 *explicit_type = false;
1426 if (type_name == XKB_ATOM_NONE) {
1427 log_warn(keymap->ctx,
1428 "Couldn't find an automatic type for key '%s' group %d with %lu levels; "
1429 "Using the default type\n",
1430 KeyNameText(keymap->ctx, keyi->name), group + 1,
1431 (unsigned long) darray_size(groupi->levels));
1435 for (i = 0; i < keymap->num_types; i++)
1436 if (keymap->types[i].name == type_name)
1439 if (i >= keymap->num_types) {
1440 log_warn(keymap->ctx,
1441 "The type \"%s\" for key '%s' group %d was not previously defined; "
1442 "Using the default type\n",
1443 xkb_atom_text(keymap->ctx, type_name),
1444 KeyNameText(keymap->ctx, keyi->name), group + 1);
1448 return &keymap->types[i];
1452 * Index 0 is guaranteed to contain something, usually
1453 * ONE_LEVEL or at least some default one-level type.
1455 return &keymap->types[0];
1459 CopySymbolsDef(SymbolsInfo *info, KeyInfo *keyi)
1461 struct xkb_keymap *keymap = info->keymap;
1462 struct xkb_key *key;
1464 const GroupInfo *group0;
1465 xkb_layout_index_t i;
1468 * The name is guaranteed to be real and not an alias (see
1469 * AddKeySymbols), so 'false' is safe here.
1471 key = FindNamedKey(keymap, keyi->name, false);
1473 log_vrb(info->keymap->ctx, 5,
1474 "Key %s not found in keycodes; Symbols ignored\n",
1475 KeyInfoText(info, keyi));
1479 /* Find the range of groups we need. */
1480 key->num_groups = 0;
1481 darray_enumerate(i, groupi, keyi->groups)
1482 if (groupi->defined)
1483 key->num_groups = i + 1;
1485 if (key->num_groups <= 0)
1486 return false; /* WSGO */
1488 darray_resize(keyi->groups, key->num_groups);
1491 * If there are empty groups between non-empty ones, fill them with data
1492 * from the first group.
1493 * We can make a wrong assumption here. But leaving gaps is worse.
1495 group0 = &darray_item(keyi->groups, 0);
1496 darray_foreach_from(groupi, keyi->groups, 1) {
1497 if (groupi->defined)
1500 CopyGroupInfo(groupi, group0);
1503 key->groups = calloc(key->num_groups, sizeof(*key->groups));
1505 /* Find and assign the groups' types in the keymap. */
1506 darray_enumerate(i, groupi, keyi->groups) {
1507 const struct xkb_key_type *type;
1510 type = FindTypeForGroup(keymap, keyi, i, &explicit_type);
1512 /* Always have as many levels as the type specifies. */
1513 if (type->num_levels < darray_size(groupi->levels)) {
1514 struct xkb_level *leveli;
1516 log_vrb(info->keymap->ctx, 1,
1517 "Type \"%s\" has %d levels, but %s has %d levels; "
1518 "Ignoring extra symbols\n",
1519 xkb_atom_text(keymap->ctx, type->name), type->num_levels,
1520 KeyInfoText(info, keyi),
1521 (int) darray_size(groupi->levels));
1523 darray_foreach_from(leveli, groupi->levels, type->num_levels)
1524 ClearLevelInfo(leveli);
1526 darray_resize0(groupi->levels, type->num_levels);
1528 key->groups[i].explicit_type = explicit_type;
1529 key->groups[i].type = type;
1533 darray_enumerate(i, groupi, keyi->groups) {
1534 key->groups[i].levels = darray_mem(groupi->levels, 0);
1535 darray_init(groupi->levels);
1538 key->out_of_range_group_number = keyi->out_of_range_group_number;
1539 key->out_of_range_group_action = keyi->out_of_range_group_action;
1541 if (keyi->defined & KEY_FIELD_VMODMAP) {
1542 key->vmodmap = keyi->vmodmap;
1543 key->explicit |= EXPLICIT_VMODMAP;
1546 if (keyi->repeat != KEY_REPEAT_UNDEFINED) {
1547 key->repeats = (keyi->repeat == KEY_REPEAT_YES);
1548 key->explicit |= EXPLICIT_REPEAT;
1551 darray_foreach(groupi, keyi->groups) {
1552 if (groupi->defined & GROUP_FIELD_ACTS) {
1553 key->explicit |= EXPLICIT_INTERP;
1562 CopyModMapDef(SymbolsInfo *info, ModMapEntry *entry)
1564 struct xkb_key *key;
1565 struct xkb_keymap *keymap = info->keymap;
1567 if (!entry->haveSymbol) {
1568 key = FindNamedKey(keymap, entry->u.keyName, true);
1570 log_vrb(info->keymap->ctx, 5,
1571 "Key %s not found in keycodes; "
1572 "Modifier map entry for %s not updated\n",
1573 KeyNameText(keymap->ctx, entry->u.keyName),
1574 ModIndexText(entry->modifier));
1579 key = FindKeyForSymbol(keymap, entry->u.keySym);
1581 log_vrb(info->keymap->ctx, 5,
1582 "Key \"%s\" not found in symbol map; "
1583 "Modifier map entry for %s not updated\n",
1584 KeysymText(entry->u.keySym),
1585 ModIndexText(entry->modifier));
1590 key->modmap |= (1 << entry->modifier);
1595 CopySymbolsToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info)
1599 struct xkb_key *key;
1601 keymap->symbols_section_name = strdup_safe(info->name);
1603 keymap->group_names = info->group_names;
1604 darray_init(info->group_names);
1606 darray_foreach(keyi, info->keys)
1607 if (!CopySymbolsDef(info, keyi))
1610 if (xkb_context_get_log_verbosity(keymap->ctx) > 3) {
1611 xkb_foreach_key(key, keymap) {
1612 if (key->name == XKB_ATOM_NONE)
1615 if (key->num_groups < 1)
1616 log_info(keymap->ctx,
1617 "No symbols defined for %s\n",
1618 KeyNameText(keymap->ctx, key->name));
1622 darray_foreach(mm, info->modMaps)
1623 if (!CopyModMapDef(info, mm))
1626 /* XXX: If we don't ignore errorCount, things break. */
1631 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1632 enum merge_mode merge)
1635 ActionsInfo *actions;
1637 actions = NewActionsInfo();
1641 InitSymbolsInfo(&info, keymap, file->id, actions);
1642 info.dflt.merge = merge;
1644 HandleSymbolsFile(&info, file, merge);
1646 if (darray_empty(info.keys))
1649 if (info.errorCount != 0)
1652 if (!CopySymbolsToKeymap(keymap, &info))
1655 ClearSymbolsInfo(&info);
1656 FreeActionsInfo(actions);
1660 FreeActionsInfo(actions);
1661 ClearSymbolsInfo(&info);