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) {
231 else if (into->type == XKB_ATOM_NONE) {
232 into->type = from->type;
235 xkb_atom_t use = (clobber ? from->type : into->type);
236 xkb_atom_t ignore = (clobber ? into->type : from->type);
240 "Multiple definitions for group %d type of key %s; "
241 "Using %s, ignoring %s\n",
242 group + 1, KeyNameText(info->ctx, key_name),
243 xkb_atom_text(info->ctx, use),
244 xkb_atom_text(info->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(info->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;
302 else if (!XkbLevelsSameSyms(fromLevel, intoLevel)) {
305 "Multiple symbols for level %d/group %u on key %s; "
306 "Using %s, ignoring %s\n",
307 i + 1, group + 1, KeyNameText(info->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 darray_foreach_from(level, from->levels, levels_in_both) {
324 darray_append(into->levels, *level);
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, bool same_file)
354 xkb_layout_index_t i;
355 xkb_layout_index_t groups_in_both;
356 enum key_field collide = 0;
357 const int verbosity = xkb_context_get_log_verbosity(info->ctx);
358 const bool clobber = (from->merge != MERGE_AUGMENT);
359 const bool report = (same_file && verbosity > 0) || verbosity > 9;
361 if (from->merge == MERGE_REPLACE) {
364 InitKeyInfo(info->ctx, from);
368 groups_in_both = MIN(darray_size(into->groups), darray_size(from->groups));
369 for (i = 0; i < groups_in_both; i++)
371 &darray_item(into->groups, i),
372 &darray_item(from->groups, i),
373 clobber, report, i, into->name);
374 /* If @from has extra groups, just move them to @into. */
375 for (i = groups_in_both; i < darray_size(from->groups); i++) {
376 darray_append(into->groups, darray_item(from->groups, i));
377 InitGroupInfo(&darray_item(from->groups, i));
380 if (UseNewKeyField(KEY_FIELD_VMODMAP, into->defined, from->defined,
381 clobber, report, &collide)) {
382 into->vmodmap = from->vmodmap;
383 into->defined |= KEY_FIELD_VMODMAP;
385 if (UseNewKeyField(KEY_FIELD_REPEAT, into->defined, from->defined,
386 clobber, report, &collide)) {
387 into->repeat = from->repeat;
388 into->defined |= KEY_FIELD_REPEAT;
390 if (UseNewKeyField(KEY_FIELD_DEFAULT_TYPE, into->defined, from->defined,
391 clobber, report, &collide)) {
392 into->default_type = from->default_type;
393 into->defined |= KEY_FIELD_DEFAULT_TYPE;
395 if (UseNewKeyField(KEY_FIELD_GROUPINFO, into->defined, from->defined,
396 clobber, report, &collide)) {
397 into->out_of_range_group_action = from->out_of_range_group_action;
398 into->out_of_range_group_number = from->out_of_range_group_number;
399 into->defined |= KEY_FIELD_GROUPINFO;
404 "Symbol map for key %s redefined; "
405 "Using %s definition for conflicting fields\n",
406 KeyNameText(info->ctx, into->name),
407 (clobber ? "first" : "last"));
410 InitKeyInfo(info->ctx, from);
414 /* TODO: Make it so this function doesn't need the entire keymap. */
416 AddKeySymbols(SymbolsInfo *info, KeyInfo *keyi, bool same_file)
418 xkb_atom_t real_name;
422 * Don't keep aliases in the keys array; this guarantees that
423 * searching for keys to merge with by straight comparison (see the
424 * following loop) is enough, and we won't get multiple KeyInfo's
425 * for the same key because of aliases.
427 real_name = XkbResolveKeyAlias(info->keymap, keyi->name);
428 if (real_name != XKB_ATOM_NONE)
429 keyi->name = real_name;
431 darray_foreach(iter, info->keys)
432 if (iter->name == keyi->name)
433 return MergeKeys(info, iter, keyi, same_file);
435 darray_append(info->keys, *keyi);
436 InitKeyInfo(info->ctx, keyi);
441 AddModMapEntry(SymbolsInfo *info, ModMapEntry *new)
444 bool clobber = (new->merge != MERGE_AUGMENT);
446 darray_foreach(old, info->modmaps) {
447 xkb_mod_index_t use, ignore;
449 if ((new->haveSymbol != old->haveSymbol) ||
450 (new->haveSymbol && new->u.keySym != old->u.keySym) ||
451 (!new->haveSymbol && new->u.keyName != old->u.keyName))
454 if (new->modifier == old->modifier)
457 use = (clobber ? new->modifier : old->modifier);
458 ignore = (clobber ? old->modifier : new->modifier);
462 "Symbol \"%s\" added to modifier map for multiple modifiers; "
463 "Using %s, ignoring %s\n",
464 KeysymText(info->ctx, new->u.keySym),
465 ModIndexText(info->ctx, &info->mods, use),
466 ModIndexText(info->ctx, &info->mods, ignore));
469 "Key \"%s\" added to modifier map for multiple modifiers; "
470 "Using %s, ignoring %s\n",
471 KeyNameText(info->ctx, new->u.keyName),
472 ModIndexText(info->ctx, &info->mods, use),
473 ModIndexText(info->ctx, &info->mods, ignore));
479 darray_append(info->modmaps, *new);
483 /***====================================================================***/
486 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
487 enum merge_mode merge)
489 xkb_atom_t *group_name;
490 xkb_layout_index_t group_names_in_both;
492 if (from->errorCount > 0) {
493 into->errorCount += from->errorCount;
497 into->mods = from->mods;
499 if (into->name == NULL) {
500 into->name = from->name;
504 group_names_in_both = MIN(darray_size(into->group_names),
505 darray_size(from->group_names));
506 for (xkb_layout_index_t i = 0; i < group_names_in_both; i++) {
507 if (!darray_item(from->group_names, i))
510 if (merge == MERGE_AUGMENT && darray_item(into->group_names, i))
513 darray_item(into->group_names, i) = darray_item(from->group_names, i);
515 /* If @from has more, get them as well. */
516 darray_foreach_from(group_name, from->group_names, group_names_in_both)
517 darray_append(into->group_names, *group_name);
519 if (darray_empty(into->keys)) {
520 into->keys = from->keys;
521 darray_init(from->keys);
525 darray_foreach(keyi, from->keys) {
526 keyi->merge = (merge == MERGE_DEFAULT ? keyi->merge : merge);
527 if (!AddKeySymbols(into, keyi, false))
532 if (darray_empty(into->modmaps)) {
533 into->modmaps = from->modmaps;
534 darray_init(from->modmaps);
538 darray_foreach(mm, from->modmaps) {
539 mm->merge = (merge == MERGE_DEFAULT ? mm->merge : merge);
540 if (!AddModMapEntry(into, mm))
547 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge);
550 HandleIncludeSymbols(SymbolsInfo *info, IncludeStmt *include)
552 SymbolsInfo included;
554 InitSymbolsInfo(&included, info->keymap, info->actions, &info->mods);
555 included.name = include->stmt;
556 include->stmt = NULL;
558 for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
559 SymbolsInfo next_incl;
562 file = ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_SYMBOLS);
564 info->errorCount += 10;
565 ClearSymbolsInfo(&included);
569 InitSymbolsInfo(&next_incl, info->keymap, info->actions,
571 if (stmt->modifier) {
572 next_incl.explicit_group = atoi(stmt->modifier) - 1;
573 if (next_incl.explicit_group >= XKB_MAX_GROUPS) {
575 "Cannot set explicit group to %d - must be between 1..%d; "
576 "Ignoring group number\n",
577 next_incl.explicit_group + 1, XKB_MAX_GROUPS);
578 next_incl.explicit_group = info->explicit_group;
582 next_incl.explicit_group = info->explicit_group;
585 HandleSymbolsFile(&next_incl, file, MERGE_OVERRIDE);
587 MergeIncludedSymbols(&included, &next_incl, stmt->merge);
589 ClearSymbolsInfo(&next_incl);
593 MergeIncludedSymbols(info, &included, include->merge);
594 ClearSymbolsInfo(&included);
596 return (info->errorCount == 0);
603 GetGroupIndex(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
604 unsigned what, xkb_layout_index_t *ndx_rtrn)
606 const char *name = (what == SYMBOLS ? "symbols" : "actions");
608 if (arrayNdx == NULL) {
609 xkb_layout_index_t i;
611 enum group_field field = (what == SYMBOLS ?
612 GROUP_FIELD_SYMS : GROUP_FIELD_ACTS);
614 darray_enumerate(i, groupi, keyi->groups) {
615 if (!(groupi->defined & field)) {
621 if (i >= XKB_MAX_GROUPS) {
623 "Too many groups of %s for key %s (max %u); "
624 "Ignoring %s defined for extra groups\n",
625 name, KeyInfoText(info, keyi), XKB_MAX_GROUPS, name);
629 darray_resize0(keyi->groups, darray_size(keyi->groups) + 1);
630 *ndx_rtrn = darray_size(keyi->groups) - 1;
634 if (!ExprResolveGroup(info->ctx, arrayNdx, ndx_rtrn)) {
636 "Illegal group index for %s of key %s\n"
637 "Definition with non-integer array index ignored\n",
638 name, KeyInfoText(info, keyi));
643 if (*ndx_rtrn >= darray_size(keyi->groups))
644 darray_resize0(keyi->groups, *ndx_rtrn + 1);
650 AddSymbolsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
653 xkb_layout_index_t ndx;
655 xkb_level_index_t nLevels;
657 if (!GetGroupIndex(info, keyi, arrayNdx, SYMBOLS, &ndx))
660 groupi = &darray_item(keyi->groups, ndx);
663 groupi->defined |= GROUP_FIELD_SYMS;
667 if (value->expr.op != EXPR_KEYSYM_LIST) {
669 "Expected a list of symbols, found %s; "
670 "Ignoring symbols for group %u of %s\n",
671 expr_op_type_to_string(value->expr.op), ndx + 1,
672 KeyInfoText(info, keyi));
676 if (groupi->defined & GROUP_FIELD_SYMS) {
678 "Symbols for key %s, group %u already defined; "
679 "Ignoring duplicate definition\n",
680 KeyInfoText(info, keyi), ndx + 1);
684 nLevels = darray_size(value->keysym_list.symsMapIndex);
685 if (darray_size(groupi->levels) < nLevels)
686 darray_resize0(groupi->levels, nLevels);
688 groupi->defined |= GROUP_FIELD_SYMS;
690 for (xkb_level_index_t i = 0; i < nLevels; i++) {
691 unsigned int sym_index;
692 struct xkb_level *leveli = &darray_item(groupi->levels, i);
694 sym_index = darray_item(value->keysym_list.symsMapIndex, i);
695 leveli->num_syms = darray_item(value->keysym_list.symsNumEntries, i);
696 if (leveli->num_syms > 1)
697 leveli->u.syms = calloc(leveli->num_syms, sizeof(*leveli->u.syms));
699 for (unsigned j = 0; j < leveli->num_syms; j++) {
700 xkb_keysym_t keysym = darray_item(value->keysym_list.syms,
703 if (leveli->num_syms == 1) {
704 if (keysym == XKB_KEY_NoSymbol)
705 leveli->num_syms = 0;
707 leveli->u.sym = keysym;
709 else if (leveli->num_syms > 1) {
710 leveli->u.syms[j] = keysym;
719 AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
722 xkb_layout_index_t ndx;
727 if (!GetGroupIndex(info, keyi, arrayNdx, ACTIONS, &ndx))
730 groupi = &darray_item(keyi->groups, ndx);
733 groupi->defined |= GROUP_FIELD_ACTS;
737 if (value->expr.op != EXPR_ACTION_LIST) {
739 "Bad expression type (%d) for action list value; "
740 "Ignoring actions for group %u of %s\n",
741 value->expr.op, ndx, KeyInfoText(info, keyi));
745 if (groupi->defined & GROUP_FIELD_ACTS) {
747 "Actions for key %s, group %u already defined\n",
748 KeyInfoText(info, keyi), ndx);
753 for (act = value->unary.child; act; act = (ExprDef *) act->common.next)
756 if (darray_size(groupi->levels) < nActs)
757 darray_resize0(groupi->levels, nActs);
759 groupi->defined |= GROUP_FIELD_ACTS;
761 act = value->unary.child;
762 for (unsigned i = 0; i < nActs; i++) {
763 union xkb_action *toAct = &darray_item(groupi->levels, i).action;
765 if (!HandleActionDef(info->ctx, info->actions, &info->mods, act, toAct))
767 "Illegal action definition for %s; "
768 "Action for group %u/level %u ignored\n",
769 KeyInfoText(info, keyi), ndx + 1, i + 1);
771 act = (ExprDef *) act->common.next;
777 static const LookupEntry repeatEntries[] = {
778 { "true", KEY_REPEAT_YES },
779 { "yes", KEY_REPEAT_YES },
780 { "on", KEY_REPEAT_YES },
781 { "false", KEY_REPEAT_NO },
782 { "no", KEY_REPEAT_NO },
783 { "off", KEY_REPEAT_NO },
784 { "default", KEY_REPEAT_UNDEFINED },
789 SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
790 ExprDef *arrayNdx, ExprDef *value)
792 if (istreq(field, "type")) {
793 xkb_layout_index_t ndx;
796 if (!ExprResolveString(info->ctx, value, &val)) {
798 "The type field of a key symbol map must be a string; "
799 "Ignoring illegal type definition\n");
804 keyi->default_type = val;
805 keyi->defined |= KEY_FIELD_DEFAULT_TYPE;
807 else if (!ExprResolveGroup(info->ctx, arrayNdx, &ndx)) {
809 "Illegal group index for type of key %s; "
810 "Definition with non-integer array index ignored\n",
811 KeyInfoText(info, keyi));
816 if (ndx >= darray_size(keyi->groups))
817 darray_resize0(keyi->groups, ndx + 1);
818 darray_item(keyi->groups, ndx).type = val;
819 darray_item(keyi->groups, ndx).defined |= GROUP_FIELD_TYPE;
822 else if (istreq(field, "symbols")) {
823 return AddSymbolsToKey(info, keyi, arrayNdx, value);
825 else if (istreq(field, "actions")) {
826 return AddActionsToKey(info, keyi, arrayNdx, value);
828 else if (istreq(field, "vmods") ||
829 istreq(field, "virtualmods") ||
830 istreq(field, "virtualmodifiers")) {
833 if (!ExprResolveModMask(info->ctx, value, MOD_VIRT, &info->mods,
836 "Expected a virtual modifier mask, found %s; "
837 "Ignoring virtual modifiers definition for key %s\n",
838 expr_op_type_to_string(value->expr.op),
839 KeyInfoText(info, keyi));
843 keyi->vmodmap = mask;
844 keyi->defined |= KEY_FIELD_VMODMAP;
846 else if (istreq(field, "locking") ||
847 istreq(field, "lock") ||
848 istreq(field, "locks")) {
849 log_vrb(info->ctx, 1,
850 "Key behaviors not supported; "
851 "Ignoring locking specification for key %s\n",
852 KeyInfoText(info, keyi));
854 else if (istreq(field, "radiogroup") ||
855 istreq(field, "permanentradiogroup") ||
856 istreq(field, "allownone")) {
857 log_vrb(info->ctx, 1,
858 "Radio groups not supported; "
859 "Ignoring radio group specification for key %s\n",
860 KeyInfoText(info, keyi));
862 else if (istreq_prefix("overlay", field) ||
863 istreq_prefix("permanentoverlay", field)) {
864 log_vrb(info->ctx, 1,
865 "Overlays not supported; "
866 "Ignoring overlay specification for key %s\n",
867 KeyInfoText(info, keyi));
869 else if (istreq(field, "repeating") ||
870 istreq(field, "repeats") ||
871 istreq(field, "repeat")) {
874 if (!ExprResolveEnum(info->ctx, value, &val, repeatEntries)) {
876 "Illegal repeat setting for %s; "
877 "Non-boolean repeat setting ignored\n",
878 KeyInfoText(info, keyi));
883 keyi->defined |= KEY_FIELD_REPEAT;
885 else if (istreq(field, "groupswrap") ||
886 istreq(field, "wrapgroups")) {
889 if (!ExprResolveBoolean(info->ctx, value, &set)) {
891 "Illegal groupsWrap setting for %s; "
892 "Non-boolean value ignored\n",
893 KeyInfoText(info, keyi));
897 keyi->out_of_range_group_action = (set ? RANGE_WRAP : RANGE_SATURATE);
898 keyi->defined |= KEY_FIELD_GROUPINFO;
900 else if (istreq(field, "groupsclamp") ||
901 istreq(field, "clampgroups")) {
904 if (!ExprResolveBoolean(info->ctx, value, &set)) {
906 "Illegal groupsClamp setting for %s; "
907 "Non-boolean value ignored\n",
908 KeyInfoText(info, keyi));
912 keyi->out_of_range_group_action = (set ? RANGE_SATURATE : RANGE_WRAP);
913 keyi->defined |= KEY_FIELD_GROUPINFO;
915 else if (istreq(field, "groupsredirect") ||
916 istreq(field, "redirectgroups")) {
917 xkb_layout_index_t grp;
919 if (!ExprResolveGroup(info->ctx, value, &grp)) {
921 "Illegal group index for redirect of key %s; "
922 "Definition with non-integer group ignored\n",
923 KeyInfoText(info, keyi));
927 keyi->out_of_range_group_action = RANGE_REDIRECT;
928 keyi->out_of_range_group_number = grp - 1;
929 keyi->defined |= KEY_FIELD_GROUPINFO;
933 "Unknown field %s in a symbol interpretation; "
934 "Definition ignored\n",
943 SetGroupName(SymbolsInfo *info, ExprDef *arrayNdx, ExprDef *value)
945 xkb_layout_index_t group, group_to_use;
949 log_vrb(info->ctx, 1,
950 "You must specify an index when specifying a group name; "
951 "Group name definition without array subscript ignored\n");
955 if (!ExprResolveGroup(info->ctx, arrayNdx, &group)) {
957 "Illegal index in group name definition; "
958 "Definition with non-integer array index ignored\n");
962 if (!ExprResolveString(info->ctx, value, &name)) {
964 "Group name must be a string; "
965 "Illegal name for group %d ignored\n", group);
969 if (info->explicit_group == XKB_LAYOUT_INVALID) {
970 group_to_use = group - 1;
972 else if (group - 1 == 0) {
973 group_to_use = info->explicit_group;
977 "An explicit group was specified for the '%s' map, "
978 "but it provides a name for a group other than Group1 (%d); "
979 "Ignoring group name '%s'\n",
981 xkb_atom_text(info->ctx, name));
985 if (group_to_use >= darray_size(info->group_names))
986 darray_resize0(info->group_names, group_to_use + 1);
987 darray_item(info->group_names, group_to_use) = name;
993 HandleGlobalVar(SymbolsInfo *info, VarDef *stmt)
995 const char *elem, *field;
999 if (!ExprResolveLhs(info->ctx, stmt->name, &elem, &field, &arrayNdx))
1002 if (elem && istreq(elem, "key")) {
1003 ret = SetSymbolsField(info, &info->default_key, field, arrayNdx,
1006 else if (!elem && (istreq(field, "name") ||
1007 istreq(field, "groupname"))) {
1008 ret = SetGroupName(info, arrayNdx, stmt->value);
1010 else if (!elem && (istreq(field, "groupswrap") ||
1011 istreq(field, "wrapgroups"))) {
1013 "Global \"groupswrap\" not supported; Ignored\n");
1016 else if (!elem && (istreq(field, "groupsclamp") ||
1017 istreq(field, "clampgroups"))) {
1019 "Global \"groupsclamp\" not supported; Ignored\n");
1022 else if (!elem && (istreq(field, "groupsredirect") ||
1023 istreq(field, "redirectgroups"))) {
1025 "Global \"groupsredirect\" not supported; Ignored\n");
1028 else if (!elem && istreq(field, "allownone")) {
1030 "Radio groups not supported; "
1031 "Ignoring \"allownone\" specification\n");
1035 ret = SetActionField(info->ctx, info->actions, &info->mods,
1036 elem, field, arrayNdx, stmt->value);
1043 HandleSymbolsBody(SymbolsInfo *info, VarDef *def, KeyInfo *keyi)
1046 const char *elem, *field;
1049 for (; def; def = (VarDef *) def->common.next) {
1050 if (def->name && def->name->expr.op == EXPR_FIELD_REF) {
1052 "Cannot set a global default value from within a key statement; "
1053 "Move statements to the global file scope\n");
1058 if (!def->value || def->value->expr.op == EXPR_KEYSYM_LIST)
1065 ok = ExprResolveLhs(info->ctx, def->name, &elem, &field,
1070 ok = SetSymbolsField(info, keyi, field, arrayNdx, def->value);
1077 SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi)
1079 xkb_layout_index_t i;
1083 if (info->explicit_group == XKB_LAYOUT_INVALID)
1086 darray_enumerate_from(i, groupi, keyi->groups, 1) {
1087 if (groupi->defined) {
1089 ClearGroupInfo(groupi);
1090 InitGroupInfo(groupi);
1096 "For the map %s an explicit group specified, "
1097 "but key %s has more than one group defined; "
1098 "All groups except first one will be ignored\n",
1099 info->name, KeyInfoText(info, keyi));
1101 darray_resize0(keyi->groups, info->explicit_group + 1);
1102 if (info->explicit_group > 0) {
1103 darray_item(keyi->groups, info->explicit_group) =
1104 darray_item(keyi->groups, 0);
1105 InitGroupInfo(&darray_item(keyi->groups, 0));
1112 HandleSymbolsDef(SymbolsInfo *info, SymbolsDef *stmt)
1116 keyi = info->default_key;
1117 darray_init(keyi.groups);
1118 darray_copy(keyi.groups, info->default_key.groups);
1119 for (xkb_layout_index_t i = 0; i < darray_size(keyi.groups); i++)
1120 CopyGroupInfo(&darray_item(keyi.groups, i),
1121 &darray_item(info->default_key.groups, i));
1122 keyi.merge = stmt->merge;
1123 keyi.name = stmt->keyName;
1125 if (!HandleSymbolsBody(info, stmt->symbols, &keyi)) {
1130 if (!SetExplicitGroup(info, &keyi)) {
1135 if (!AddKeySymbols(info, &keyi, true)) {
1144 HandleModMapDef(SymbolsInfo *info, ModMapDef *def)
1147 xkb_mod_index_t ndx;
1149 struct xkb_context *ctx = info->ctx;
1151 ndx = XkbModNameToIndex(&info->mods, def->modifier, MOD_REAL);
1152 if (ndx == XKB_MOD_INVALID) {
1154 "Illegal modifier map definition; "
1155 "Ignoring map for non-modifier \"%s\"\n",
1156 xkb_atom_text(ctx, def->modifier));
1162 tmp.merge = def->merge;
1164 for (ExprDef *key = def->keys; key; key = (ExprDef *) key->common.next) {
1167 if (key->expr.op == EXPR_VALUE &&
1168 key->expr.value_type == EXPR_TYPE_KEYNAME) {
1169 tmp.haveSymbol = false;
1170 tmp.u.keyName = key->key_name.key_name;
1172 else if (ExprResolveKeySym(ctx, key, &sym)) {
1173 tmp.haveSymbol = true;
1178 "Modmap entries may contain only key names or keysyms; "
1179 "Illegal definition for %s modifier ignored\n",
1180 ModIndexText(info->ctx, &info->mods, tmp.modifier));
1184 ok = AddModMapEntry(info, &tmp) && ok;
1190 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge)
1195 info->name = strdup_safe(file->name);
1197 for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
1198 switch (stmt->type) {
1200 ok = HandleIncludeSymbols(info, (IncludeStmt *) stmt);
1203 ok = HandleSymbolsDef(info, (SymbolsDef *) stmt);
1206 ok = HandleGlobalVar(info, (VarDef *) stmt);
1209 ok = HandleVModDef(info->ctx, &info->mods, (VModDef *) stmt, merge);
1212 ok = HandleModMapDef(info, (ModMapDef *) stmt);
1216 "Symbols files may not include other types; "
1217 "Ignoring %s\n", stmt_type_to_string(stmt->type));
1225 if (info->errorCount > 10) {
1226 log_err(info->ctx, "Abandoning symbols file \"%s\"\n",
1234 * Given a keysym @sym, return a key which generates it, or NULL.
1235 * This is used for example in a modifier map definition, such as:
1236 * modifier_map Lock { Caps_Lock };
1237 * where we want to add the Lock modifier to the modmap of the key
1238 * which matches the keysym Caps_Lock.
1239 * Since there can be many keys which generates the keysym, the key
1240 * is chosen first by lowest group in which the keysym appears, than
1241 * by lowest level and than by lowest key code.
1243 static struct xkb_key *
1244 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
1246 struct xkb_key *key;
1247 xkb_layout_index_t group;
1248 bool got_one_group, got_one_level;
1252 xkb_level_index_t level = 0;
1253 got_one_group = false;
1255 got_one_level = false;
1256 xkb_keys_foreach(key, keymap) {
1257 if (group < key->num_groups &&
1258 level < XkbKeyNumLevels(key, group)) {
1259 got_one_group = got_one_level = true;
1260 if (key->groups[group].levels[level].num_syms == 1 &&
1261 key->groups[group].levels[level].u.sym == sym)
1266 } while (got_one_level);
1268 } while (got_one_group);
1274 * Find an appropriate type for a group and return its name.
1277 * - ONE_LEVEL for width 0/1
1278 * - ALPHABETIC for 2 shift levels, with lower/upercase keysyms
1279 * - KEYPAD for keypad keys.
1280 * - TWO_LEVEL for other 2 shift level keys.
1281 * and the same for four level keys.
1283 * FIXME: Decide how to handle multiple-syms-per-level, and do it.
1286 FindAutomaticType(struct xkb_context *ctx, GroupInfo *groupi)
1288 xkb_keysym_t sym0, sym1;
1289 const xkb_level_index_t width = darray_size(groupi->levels);
1291 #define GET_SYM(level) \
1292 (darray_item(groupi->levels, level).num_syms == 0 ? \
1293 XKB_KEY_NoSymbol : \
1294 darray_item(groupi->levels, level).num_syms == 1 ? \
1295 darray_item(groupi->levels, level).u.sym : \
1296 /* num_syms > 1 */ \
1297 darray_item(groupi->levels, level).u.syms[0])
1299 if (width == 1 || width <= 0)
1300 return xkb_atom_intern_literal(ctx, "ONE_LEVEL");
1306 if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1))
1307 return xkb_atom_intern_literal(ctx, "ALPHABETIC");
1309 if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1310 return xkb_atom_intern_literal(ctx, "KEYPAD");
1312 return xkb_atom_intern_literal(ctx, "TWO_LEVEL");
1316 if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1)) {
1317 xkb_keysym_t sym2, sym3;
1319 sym3 = (width == 4 ? GET_SYM(3) : XKB_KEY_NoSymbol);
1321 if (xkb_keysym_is_lower(sym2) && xkb_keysym_is_upper(sym3))
1322 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_ALPHABETIC");
1324 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_SEMIALPHABETIC");
1327 if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1328 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_KEYPAD");
1330 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL");
1333 return XKB_ATOM_NONE;
1338 static const struct xkb_key_type *
1339 FindTypeForGroup(struct xkb_keymap *keymap, KeyInfo *keyi,
1340 xkb_layout_index_t group, bool *explicit_type)
1343 GroupInfo *groupi = &darray_item(keyi->groups, group);
1344 xkb_atom_t type_name = groupi->type;
1346 *explicit_type = true;
1348 if (type_name == XKB_ATOM_NONE) {
1349 if (keyi->default_type != XKB_ATOM_NONE) {
1350 type_name = keyi->default_type;
1353 type_name = FindAutomaticType(keymap->ctx, groupi);
1354 if (type_name != XKB_ATOM_NONE)
1355 *explicit_type = false;
1359 if (type_name == XKB_ATOM_NONE) {
1360 log_warn(keymap->ctx,
1361 "Couldn't find an automatic type for key '%s' group %d with %lu levels; "
1362 "Using the default type\n",
1363 KeyNameText(keymap->ctx, keyi->name), group + 1,
1364 (unsigned long) darray_size(groupi->levels));
1368 for (i = 0; i < keymap->num_types; i++)
1369 if (keymap->types[i].name == type_name)
1372 if (i >= keymap->num_types) {
1373 log_warn(keymap->ctx,
1374 "The type \"%s\" for key '%s' group %d was not previously defined; "
1375 "Using the default type\n",
1376 xkb_atom_text(keymap->ctx, type_name),
1377 KeyNameText(keymap->ctx, keyi->name), group + 1);
1381 return &keymap->types[i];
1385 * Index 0 is guaranteed to contain something, usually
1386 * ONE_LEVEL or at least some default one-level type.
1388 return &keymap->types[0];
1392 CopySymbolsDefToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info,
1395 struct xkb_key *key;
1397 const GroupInfo *group0;
1398 xkb_layout_index_t i;
1401 * The name is guaranteed to be real and not an alias (see
1402 * AddKeySymbols), so 'false' is safe here.
1404 key = XkbKeyByName(keymap, keyi->name, false);
1406 log_vrb(info->ctx, 5,
1407 "Key %s not found in keycodes; Symbols ignored\n",
1408 KeyInfoText(info, keyi));
1412 /* Find the range of groups we need. */
1413 key->num_groups = 0;
1414 darray_enumerate(i, groupi, keyi->groups)
1415 if (groupi->defined)
1416 key->num_groups = i + 1;
1418 if (key->num_groups <= 0)
1419 return false; /* WSGO */
1421 darray_resize(keyi->groups, key->num_groups);
1424 * If there are empty groups between non-empty ones, fill them with data
1425 * from the first group.
1426 * We can make a wrong assumption here. But leaving gaps is worse.
1428 group0 = &darray_item(keyi->groups, 0);
1429 darray_foreach_from(groupi, keyi->groups, 1) {
1430 if (groupi->defined)
1433 CopyGroupInfo(groupi, group0);
1436 key->groups = calloc(key->num_groups, sizeof(*key->groups));
1438 /* Find and assign the groups' types in the keymap. */
1439 darray_enumerate(i, groupi, keyi->groups) {
1440 const struct xkb_key_type *type;
1443 type = FindTypeForGroup(keymap, keyi, i, &explicit_type);
1445 /* Always have as many levels as the type specifies. */
1446 if (type->num_levels < darray_size(groupi->levels)) {
1447 struct xkb_level *leveli;
1449 log_vrb(info->ctx, 1,
1450 "Type \"%s\" has %d levels, but %s has %d levels; "
1451 "Ignoring extra symbols\n",
1452 xkb_atom_text(keymap->ctx, type->name), type->num_levels,
1453 KeyInfoText(info, keyi),
1454 (int) darray_size(groupi->levels));
1456 darray_foreach_from(leveli, groupi->levels, type->num_levels)
1457 ClearLevelInfo(leveli);
1459 darray_resize0(groupi->levels, type->num_levels);
1461 key->groups[i].explicit_type = explicit_type;
1462 key->groups[i].type = type;
1466 darray_enumerate(i, groupi, keyi->groups)
1467 darray_steal(groupi->levels, &key->groups[i].levels, NULL);
1469 key->out_of_range_group_number = keyi->out_of_range_group_number;
1470 key->out_of_range_group_action = keyi->out_of_range_group_action;
1472 if (keyi->defined & KEY_FIELD_VMODMAP) {
1473 key->vmodmap = keyi->vmodmap;
1474 key->explicit |= EXPLICIT_VMODMAP;
1477 if (keyi->repeat != KEY_REPEAT_UNDEFINED) {
1478 key->repeats = (keyi->repeat == KEY_REPEAT_YES);
1479 key->explicit |= EXPLICIT_REPEAT;
1482 darray_foreach(groupi, keyi->groups) {
1483 if (groupi->defined & GROUP_FIELD_ACTS) {
1484 key->explicit |= EXPLICIT_INTERP;
1493 CopyModMapDefToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info,
1496 struct xkb_key *key;
1498 if (!entry->haveSymbol) {
1499 key = XkbKeyByName(keymap, entry->u.keyName, true);
1501 log_vrb(info->ctx, 5,
1502 "Key %s not found in keycodes; "
1503 "Modifier map entry for %s not updated\n",
1504 KeyNameText(info->ctx, entry->u.keyName),
1505 ModIndexText(info->ctx, &info->mods, entry->modifier));
1510 key = FindKeyForSymbol(keymap, entry->u.keySym);
1512 log_vrb(info->ctx, 5,
1513 "Key \"%s\" not found in symbol map; "
1514 "Modifier map entry for %s not updated\n",
1515 KeysymText(info->ctx, entry->u.keySym),
1516 ModIndexText(info->ctx, &info->mods, entry->modifier));
1521 key->modmap |= (1u << entry->modifier);
1526 CopySymbolsToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info)
1531 keymap->symbols_section_name = strdup_safe(info->name);
1532 XkbEscapeMapName(keymap->symbols_section_name);
1534 keymap->mods = info->mods;
1536 darray_steal(info->group_names,
1537 &keymap->group_names, &keymap->num_group_names);
1539 darray_foreach(keyi, info->keys)
1540 if (!CopySymbolsDefToKeymap(keymap, info, keyi))
1543 if (xkb_context_get_log_verbosity(keymap->ctx) > 3) {
1544 struct xkb_key *key;
1546 xkb_keys_foreach(key, keymap) {
1547 if (key->name == XKB_ATOM_NONE)
1550 if (key->num_groups < 1)
1552 "No symbols defined for %s\n",
1553 KeyNameText(info->ctx, key->name));
1557 darray_foreach(mm, info->modmaps)
1558 if (!CopyModMapDefToKeymap(keymap, info, mm))
1561 /* XXX: If we don't ignore errorCount, things break. */
1566 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1567 enum merge_mode merge)
1570 ActionsInfo *actions;
1572 actions = NewActionsInfo();
1576 InitSymbolsInfo(&info, keymap, actions, &keymap->mods);
1577 info.default_key.merge = merge;
1579 HandleSymbolsFile(&info, file, merge);
1581 if (info.errorCount != 0)
1584 if (!CopySymbolsToKeymap(keymap, &info))
1587 ClearSymbolsInfo(&info);
1588 FreeActionsInfo(actions);
1592 FreeActionsInfo(actions);
1593 ClearSymbolsInfo(&info);