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)
129 to->defined = from->defined;
130 to->type = from->type;
131 darray_init(to->levels);
132 darray_copy(to->levels, from->levels);
133 for (j = 0; j < darray_size(to->levels); j++)
134 if (darray_item(from->levels, j).num_syms > 1)
135 darray_item(to->levels, j).u.syms =
136 memdup(darray_item(from->levels, j).u.syms,
137 darray_item(from->levels, j).num_syms,
138 sizeof(xkb_keysym_t));
142 InitKeyInfo(struct xkb_context *ctx, KeyInfo *keyi)
144 memset(keyi, 0, sizeof(*keyi));
145 keyi->merge = MERGE_OVERRIDE;
146 keyi->name = xkb_atom_intern_literal(ctx, "*");
147 keyi->out_of_range_group_action = RANGE_WRAP;
151 ClearKeyInfo(KeyInfo *keyi)
154 darray_foreach(groupi, keyi->groups)
155 ClearGroupInfo(groupi);
156 darray_free(keyi->groups);
159 /***====================================================================***/
162 enum merge_mode merge;
164 xkb_mod_index_t modifier;
172 char *name; /* e.g. pc+us+inet(evdev) */
174 enum merge_mode merge;
175 xkb_layout_index_t explicit_group;
176 darray(KeyInfo) keys;
178 ActionsInfo *actions;
179 darray(xkb_atom_t) group_names;
180 darray(ModMapEntry) modmaps;
182 struct xkb_keymap *keymap;
186 InitSymbolsInfo(SymbolsInfo *info, struct xkb_keymap *keymap,
187 ActionsInfo *actions)
189 memset(info, 0, sizeof(*info));
190 info->keymap = keymap;
191 info->merge = MERGE_OVERRIDE;
192 InitKeyInfo(keymap->ctx, &info->default_key);
193 info->actions = actions;
194 info->explicit_group = XKB_LAYOUT_INVALID;
198 ClearSymbolsInfo(SymbolsInfo *info)
202 darray_foreach(keyi, info->keys)
204 darray_free(info->keys);
205 darray_free(info->group_names);
206 darray_free(info->modmaps);
207 ClearKeyInfo(&info->default_key);
211 KeyInfoText(SymbolsInfo *info, KeyInfo *keyi)
213 return KeyNameText(info->keymap->ctx, keyi->name);
217 MergeGroups(SymbolsInfo *info, GroupInfo *into, GroupInfo *from, bool clobber,
218 bool report, xkb_layout_index_t group, xkb_atom_t key_name)
220 xkb_level_index_t i, levels_in_both;
221 struct xkb_context *ctx = info->keymap->ctx;
223 /* First find the type of the merged group. */
224 if (into->type != from->type) {
225 if (from->type == XKB_ATOM_NONE) {
227 else if (into->type == XKB_ATOM_NONE) {
228 into->type = from->type;
231 xkb_atom_t use = (clobber ? from->type : into->type);
232 xkb_atom_t ignore = (clobber ? into->type : from->type);
235 log_warn(info->keymap->ctx,
236 "Multiple definitions for group %d type of key %s; "
237 "Using %s, ignoring %s\n",
238 group + 1, KeyNameText(ctx, key_name),
239 xkb_atom_text(ctx, use), xkb_atom_text(ctx, ignore));
244 into->defined |= (from->defined & GROUP_FIELD_TYPE);
246 /* Now look at the levels. */
248 if (darray_empty(from->levels)) {
253 if (darray_empty(into->levels)) {
254 from->type = into->type;
260 /* Merge the actions and syms. */
261 levels_in_both = MIN(darray_size(into->levels), darray_size(from->levels));
262 for (i = 0; i < levels_in_both; i++) {
263 struct xkb_level *intoLevel = &darray_item(into->levels, i);
264 struct xkb_level *fromLevel = &darray_item(from->levels, i);
266 if (fromLevel->action.type == ACTION_TYPE_NONE) {
268 else if (intoLevel->action.type == ACTION_TYPE_NONE) {
269 intoLevel->action = fromLevel->action;
272 union xkb_action *use, *ignore;
273 use = (clobber ? &fromLevel->action : &intoLevel->action);
274 ignore = (clobber ? &intoLevel->action : &fromLevel->action);
278 "Multiple actions for level %d/group %u on key %s; "
279 "Using %s, ignoring %s\n",
280 i + 1, group + 1, KeyNameText(ctx, key_name),
281 ActionTypeText(use->type),
282 ActionTypeText(ignore->type));
284 intoLevel->action = *use;
287 if (fromLevel->num_syms == 0) {
289 else if (intoLevel->num_syms == 0) {
290 intoLevel->num_syms = fromLevel->num_syms;
291 if (fromLevel->num_syms > 1)
292 intoLevel->u.syms = fromLevel->u.syms;
294 intoLevel->u.sym = fromLevel->u.sym;
295 fromLevel->num_syms = 0;
300 "Multiple symbols for level %d/group %u on key %s; "
301 "Using %s, ignoring %s\n",
302 i + 1, group + 1, KeyNameText(ctx, key_name),
303 (clobber ? "from" : "to"),
304 (clobber ? "to" : "from"));
307 ClearLevelInfo(intoLevel);
308 intoLevel->num_syms = fromLevel->num_syms;
309 if (fromLevel->num_syms > 1)
310 intoLevel->u.syms = fromLevel->u.syms;
312 intoLevel->u.sym = fromLevel->u.sym;
313 fromLevel->num_syms = 0;
317 /* If @from has extra levels, get them as well. */
318 for (i = levels_in_both; i < darray_size(from->levels); i++) {
319 darray_append(into->levels, darray_item(from->levels, i));
320 darray_item(from->levels, i).num_syms = 0;
322 into->defined |= (from->defined & GROUP_FIELD_ACTS);
323 into->defined |= (from->defined & GROUP_FIELD_SYMS);
329 UseNewKeyField(enum key_field field, enum key_field old, enum key_field new,
330 bool clobber, bool report, enum key_field *collide)
333 return (new & field);
347 MergeKeys(SymbolsInfo *info, KeyInfo *into, KeyInfo *from, bool same_file)
349 xkb_layout_index_t i;
350 xkb_layout_index_t groups_in_both;
351 enum key_field collide = 0;
352 const int verbosity = xkb_context_get_log_verbosity(info->keymap->ctx);
353 const bool clobber = (from->merge != MERGE_AUGMENT);
354 const bool report = (same_file && verbosity > 0) || verbosity > 9;
356 if (from->merge == MERGE_REPLACE) {
359 InitKeyInfo(info->keymap->ctx, from);
363 groups_in_both = MIN(darray_size(into->groups), darray_size(from->groups));
364 for (i = 0; i < groups_in_both; i++)
366 &darray_item(into->groups, i),
367 &darray_item(from->groups, i),
368 clobber, report, i, into->name);
369 /* If @from has extra groups, just move them to @into. */
370 for (i = groups_in_both; i < darray_size(from->groups); i++) {
371 darray_append(into->groups, darray_item(from->groups, i));
372 InitGroupInfo(&darray_item(from->groups, i));
375 if (UseNewKeyField(KEY_FIELD_VMODMAP, into->defined, from->defined,
376 clobber, report, &collide)) {
377 into->vmodmap = from->vmodmap;
378 into->defined |= KEY_FIELD_VMODMAP;
380 if (UseNewKeyField(KEY_FIELD_REPEAT, into->defined, from->defined,
381 clobber, report, &collide)) {
382 into->repeat = from->repeat;
383 into->defined |= KEY_FIELD_REPEAT;
385 if (UseNewKeyField(KEY_FIELD_DEFAULT_TYPE, into->defined, from->defined,
386 clobber, report, &collide)) {
387 into->default_type = from->default_type;
388 into->defined |= KEY_FIELD_DEFAULT_TYPE;
390 if (UseNewKeyField(KEY_FIELD_GROUPINFO, into->defined, from->defined,
391 clobber, report, &collide)) {
392 into->out_of_range_group_action = from->out_of_range_group_action;
393 into->out_of_range_group_number = from->out_of_range_group_number;
394 into->defined |= KEY_FIELD_GROUPINFO;
398 log_warn(info->keymap->ctx,
399 "Symbol map for key %s redefined; "
400 "Using %s definition for conflicting fields\n",
401 KeyNameText(info->keymap->ctx, into->name),
402 (clobber ? "first" : "last"));
405 InitKeyInfo(info->keymap->ctx, from);
410 AddKeySymbols(SymbolsInfo *info, KeyInfo *keyi, bool same_file)
412 xkb_atom_t real_name;
416 * Don't keep aliases in the keys array; this guarantees that
417 * searching for keys to merge with by straight comparison (see the
418 * following loop) is enough, and we won't get multiple KeyInfo's
419 * for the same key because of aliases.
421 real_name = XkbResolveKeyAlias(info->keymap, keyi->name);
422 if (real_name != XKB_ATOM_NONE)
423 keyi->name = real_name;
425 darray_foreach(iter, info->keys)
426 if (iter->name == keyi->name)
427 return MergeKeys(info, iter, keyi, same_file);
429 darray_append(info->keys, *keyi);
430 InitKeyInfo(info->keymap->ctx, keyi);
435 AddModMapEntry(SymbolsInfo *info, ModMapEntry *new)
438 bool clobber = (new->merge != MERGE_AUGMENT);
440 darray_foreach(old, info->modmaps) {
441 xkb_mod_index_t use, ignore;
443 if ((new->haveSymbol != old->haveSymbol) ||
444 (new->haveSymbol && new->u.keySym != old->u.keySym) ||
445 (!new->haveSymbol && new->u.keyName != old->u.keyName))
448 if (new->modifier == old->modifier)
451 use = (clobber ? new->modifier : old->modifier);
452 ignore = (clobber ? old->modifier : new->modifier);
455 log_err(info->keymap->ctx,
456 "Symbol \"%s\" added to modifier map for multiple modifiers; "
457 "Using %s, ignoring %s\n",
458 KeysymText(info->keymap->ctx, new->u.keySym),
459 ModIndexText(info->keymap, use),
460 ModIndexText(info->keymap, ignore));
462 log_err(info->keymap->ctx,
463 "Key \"%s\" added to modifier map for multiple modifiers; "
464 "Using %s, ignoring %s\n",
465 KeyNameText(info->keymap->ctx, new->u.keyName),
466 ModIndexText(info->keymap, use),
467 ModIndexText(info->keymap, ignore));
473 darray_append(info->modmaps, *new);
477 /***====================================================================***/
480 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
481 enum merge_mode merge)
486 xkb_atom_t *group_name;
487 xkb_layout_index_t group_names_in_both;
489 if (from->errorCount > 0) {
490 into->errorCount += from->errorCount;
494 if (into->name == NULL) {
495 into->name = from->name;
499 group_names_in_both = MIN(darray_size(into->group_names),
500 darray_size(from->group_names));
501 for (i = 0; i < group_names_in_both; i++) {
502 if (!darray_item(from->group_names, i))
505 if (merge == MERGE_AUGMENT && darray_item(into->group_names, i))
508 darray_item(into->group_names, i) = darray_item(from->group_names, i);
510 /* If @from has more, get them as well. */
511 darray_foreach_from(group_name, from->group_names, group_names_in_both)
512 darray_append(into->group_names, *group_name);
514 darray_foreach(keyi, from->keys) {
515 keyi->merge = (merge == MERGE_DEFAULT ? keyi->merge : merge);
516 if (!AddKeySymbols(into, keyi, false))
520 darray_foreach(mm, from->modmaps) {
521 mm->merge = (merge == MERGE_DEFAULT ? mm->merge : merge);
522 if (!AddModMapEntry(into, mm))
528 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge);
531 HandleIncludeSymbols(SymbolsInfo *info, IncludeStmt *include)
533 SymbolsInfo included;
535 InitSymbolsInfo(&included, info->keymap, info->actions);
536 included.name = include->stmt;
537 include->stmt = NULL;
539 for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
540 SymbolsInfo next_incl;
543 file = ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_SYMBOLS);
545 info->errorCount += 10;
546 ClearSymbolsInfo(&included);
550 InitSymbolsInfo(&next_incl, info->keymap, info->actions);
551 if (stmt->modifier) {
552 next_incl.explicit_group = atoi(stmt->modifier) - 1;
553 if (next_incl.explicit_group >= XKB_MAX_GROUPS) {
554 log_err(info->keymap->ctx,
555 "Cannot set explicit group to %d - must be between 1..%d; "
556 "Ignoring group number\n",
557 next_incl.explicit_group + 1, XKB_MAX_GROUPS);
558 next_incl.explicit_group = info->explicit_group;
562 next_incl.explicit_group = info->explicit_group;
565 HandleSymbolsFile(&next_incl, file, MERGE_OVERRIDE);
567 MergeIncludedSymbols(&included, &next_incl, stmt->merge);
569 ClearSymbolsInfo(&next_incl);
573 MergeIncludedSymbols(info, &included, include->merge);
574 ClearSymbolsInfo(&included);
576 return (info->errorCount == 0);
583 GetGroupIndex(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
584 unsigned what, xkb_layout_index_t *ndx_rtrn)
586 const char *name = (what == SYMBOLS ? "symbols" : "actions");
588 if (arrayNdx == NULL) {
589 xkb_layout_index_t i;
591 enum group_field field = (what == SYMBOLS ?
592 GROUP_FIELD_SYMS : GROUP_FIELD_ACTS);
594 darray_enumerate(i, groupi, keyi->groups) {
595 if (!(groupi->defined & field)) {
601 if (i >= XKB_MAX_GROUPS) {
602 log_err(info->keymap->ctx,
603 "Too many groups of %s for key %s (max %u); "
604 "Ignoring %s defined for extra groups\n",
605 name, KeyInfoText(info, keyi), XKB_MAX_GROUPS, name);
609 darray_resize0(keyi->groups, darray_size(keyi->groups) + 1);
610 *ndx_rtrn = darray_size(keyi->groups) - 1;
614 if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, ndx_rtrn)) {
615 log_err(info->keymap->ctx,
616 "Illegal group index for %s of key %s\n"
617 "Definition with non-integer array index ignored\n",
618 name, KeyInfoText(info, keyi));
623 if (*ndx_rtrn >= darray_size(keyi->groups))
624 darray_resize0(keyi->groups, *ndx_rtrn + 1);
630 AddSymbolsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
633 xkb_layout_index_t ndx;
635 xkb_level_index_t nLevels;
639 if (!GetGroupIndex(info, keyi, arrayNdx, SYMBOLS, &ndx))
642 groupi = &darray_item(keyi->groups, ndx);
645 groupi->defined |= GROUP_FIELD_SYMS;
649 if (value->op != EXPR_KEYSYM_LIST) {
650 log_err(info->keymap->ctx,
651 "Expected a list of symbols, found %s; "
652 "Ignoring symbols for group %u of %s\n",
653 expr_op_type_to_string(value->op), ndx + 1,
654 KeyInfoText(info, keyi));
658 if (groupi->defined & GROUP_FIELD_SYMS) {
659 log_err(info->keymap->ctx,
660 "Symbols for key %s, group %u already defined; "
661 "Ignoring duplicate definition\n",
662 KeyInfoText(info, keyi), ndx + 1);
666 nLevels = darray_size(value->value.list.symsMapIndex);
667 if (darray_size(groupi->levels) < nLevels)
668 darray_resize0(groupi->levels, nLevels);
670 groupi->defined |= GROUP_FIELD_SYMS;
672 for (i = 0; i < nLevels; i++) {
673 unsigned int sym_index;
674 struct xkb_level *leveli = &darray_item(groupi->levels, i);
676 sym_index = darray_item(value->value.list.symsMapIndex, i);
677 leveli->num_syms = darray_item(value->value.list.symsNumEntries, i);
678 if (leveli->num_syms > 1)
679 leveli->u.syms = calloc(leveli->num_syms, sizeof(*leveli->u.syms));
681 for (j = 0; j < leveli->num_syms; j++) {
682 xkb_keysym_t keysym = darray_item(value->value.list.syms,
685 if (leveli->num_syms == 1) {
686 if (keysym == XKB_KEY_NoSymbol)
687 leveli->num_syms = 0;
689 leveli->u.sym = keysym;
691 else if (leveli->num_syms > 1) {
692 leveli->u.syms[j] = keysym;
701 AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
705 xkb_layout_index_t ndx;
709 union xkb_action *toAct;
711 if (!GetGroupIndex(info, keyi, arrayNdx, ACTIONS, &ndx))
714 groupi = &darray_item(keyi->groups, ndx);
717 groupi->defined |= GROUP_FIELD_ACTS;
721 if (value->op != EXPR_ACTION_LIST) {
722 log_wsgo(info->keymap->ctx,
723 "Bad expression type (%d) for action list value; "
724 "Ignoring actions for group %u of %s\n",
725 value->op, ndx, KeyInfoText(info, keyi));
729 if (groupi->defined & GROUP_FIELD_ACTS) {
730 log_wsgo(info->keymap->ctx,
731 "Actions for key %s, group %u already defined\n",
732 KeyInfoText(info, keyi), ndx);
737 for (act = value->value.child; act; act = (ExprDef *) act->common.next)
740 if (darray_size(groupi->levels) < nActs)
741 darray_resize0(groupi->levels, nActs);
743 groupi->defined |= GROUP_FIELD_ACTS;
745 act = value->value.child;
746 for (i = 0; i < nActs; i++) {
747 toAct = &darray_item(groupi->levels, i).action;
749 if (!HandleActionDef(act, info->keymap, toAct, info->actions))
750 log_err(info->keymap->ctx,
751 "Illegal action definition for %s; "
752 "Action for group %u/level %u ignored\n",
753 KeyInfoText(info, keyi), ndx + 1, i + 1);
755 act = (ExprDef *) act->common.next;
761 static const LookupEntry repeatEntries[] = {
762 { "true", KEY_REPEAT_YES },
763 { "yes", KEY_REPEAT_YES },
764 { "on", KEY_REPEAT_YES },
765 { "false", KEY_REPEAT_NO },
766 { "no", KEY_REPEAT_NO },
767 { "off", KEY_REPEAT_NO },
768 { "default", KEY_REPEAT_UNDEFINED },
773 SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
774 ExprDef *arrayNdx, ExprDef *value)
777 struct xkb_context *ctx = info->keymap->ctx;
779 if (istreq(field, "type")) {
780 xkb_layout_index_t ndx;
783 if (!ExprResolveString(ctx, value, &val))
785 "The type field of a key symbol map must be a string; "
786 "Ignoring illegal type definition\n");
788 if (arrayNdx == NULL) {
789 keyi->default_type = val;
790 keyi->defined |= KEY_FIELD_DEFAULT_TYPE;
792 else if (!ExprResolveGroup(ctx, arrayNdx, &ndx)) {
794 "Illegal group index for type of key %s; "
795 "Definition with non-integer array index ignored\n",
796 KeyInfoText(info, keyi));
801 if (ndx >= darray_size(keyi->groups))
802 darray_resize0(keyi->groups, ndx + 1);
803 darray_item(keyi->groups, ndx).type = val;
804 darray_item(keyi->groups, ndx).defined |= GROUP_FIELD_TYPE;
807 else if (istreq(field, "symbols"))
808 return AddSymbolsToKey(info, keyi, arrayNdx, value);
809 else if (istreq(field, "actions"))
810 return AddActionsToKey(info, keyi, arrayNdx, value);
811 else if (istreq(field, "vmods") ||
812 istreq(field, "virtualmods") ||
813 istreq(field, "virtualmodifiers")) {
816 ok = ExprResolveModMask(info->keymap, value, MOD_VIRT, &mask);
818 keyi->vmodmap = mask;
819 keyi->defined |= KEY_FIELD_VMODMAP;
822 log_err(info->keymap->ctx,
823 "Expected a virtual modifier mask, found %s; "
824 "Ignoring virtual modifiers definition for key %s\n",
825 expr_op_type_to_string(value->op),
826 KeyInfoText(info, keyi));
829 else if (istreq(field, "locking") ||
830 istreq(field, "lock") ||
831 istreq(field, "locks")) {
832 log_err(info->keymap->ctx,
833 "Key behaviors not supported; "
834 "Ignoring locking specification for key %s\n",
835 KeyInfoText(info, keyi));
837 else if (istreq(field, "radiogroup") ||
838 istreq(field, "permanentradiogroup") ||
839 istreq(field, "allownone")) {
840 log_err(info->keymap->ctx,
841 "Radio groups not supported; "
842 "Ignoring radio group specification for key %s\n",
843 KeyInfoText(info, keyi));
845 else if (istreq_prefix("overlay", field) ||
846 istreq_prefix("permanentoverlay", field)) {
847 log_err(info->keymap->ctx,
848 "Overlays not supported; "
849 "Ignoring overlay specification for key %s\n",
850 KeyInfoText(info, keyi));
852 else if (istreq(field, "repeating") ||
853 istreq(field, "repeats") ||
854 istreq(field, "repeat")) {
857 ok = ExprResolveEnum(ctx, value, &val, repeatEntries);
859 log_err(info->keymap->ctx,
860 "Illegal repeat setting for %s; "
861 "Non-boolean repeat setting ignored\n",
862 KeyInfoText(info, keyi));
866 keyi->defined |= KEY_FIELD_REPEAT;
868 else if (istreq(field, "groupswrap") ||
869 istreq(field, "wrapgroups")) {
872 if (!ExprResolveBoolean(ctx, value, &set)) {
873 log_err(info->keymap->ctx,
874 "Illegal groupsWrap setting for %s; "
875 "Non-boolean value ignored\n",
876 KeyInfoText(info, keyi));
881 keyi->out_of_range_group_action = RANGE_WRAP;
883 keyi->out_of_range_group_action = RANGE_SATURATE;
885 keyi->defined |= KEY_FIELD_GROUPINFO;
887 else if (istreq(field, "groupsclamp") ||
888 istreq(field, "clampgroups")) {
891 if (!ExprResolveBoolean(ctx, value, &set)) {
892 log_err(info->keymap->ctx,
893 "Illegal groupsClamp setting for %s; "
894 "Non-boolean value ignored\n",
895 KeyInfoText(info, keyi));
900 keyi->out_of_range_group_action = RANGE_SATURATE;
902 keyi->out_of_range_group_action = RANGE_WRAP;
904 keyi->defined |= KEY_FIELD_GROUPINFO;
906 else if (istreq(field, "groupsredirect") ||
907 istreq(field, "redirectgroups")) {
908 xkb_layout_index_t grp;
910 if (!ExprResolveGroup(ctx, value, &grp)) {
911 log_err(info->keymap->ctx,
912 "Illegal group index for redirect of key %s; "
913 "Definition with non-integer group ignored\n",
914 KeyInfoText(info, keyi));
918 keyi->out_of_range_group_action = RANGE_REDIRECT;
919 keyi->out_of_range_group_number = grp - 1;
920 keyi->defined |= KEY_FIELD_GROUPINFO;
923 log_err(info->keymap->ctx,
924 "Unknown field %s in a symbol interpretation; "
925 "Definition ignored\n",
934 SetGroupName(SymbolsInfo *info, ExprDef *arrayNdx, ExprDef *value)
936 xkb_layout_index_t group, group_to_use;
940 log_vrb(info->keymap->ctx, 1,
941 "You must specify an index when specifying a group name; "
942 "Group name definition without array subscript ignored\n");
946 if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, &group)) {
947 log_err(info->keymap->ctx,
948 "Illegal index in group name definition; "
949 "Definition with non-integer array index ignored\n");
953 if (!ExprResolveString(info->keymap->ctx, value, &name)) {
954 log_err(info->keymap->ctx,
955 "Group name must be a string; "
956 "Illegal name for group %d ignored\n", group);
960 if (info->explicit_group == XKB_LAYOUT_INVALID) {
961 group_to_use = group - 1;
963 else if (group - 1 == 0) {
964 group_to_use = info->explicit_group;
967 log_warn(info->keymap->ctx,
968 "An explicit group was specified for the '%s' map, "
969 "but it provides a name for a group other than Group1 (%d); "
970 "Ignoring group name '%s'\n",
972 xkb_atom_text(info->keymap->ctx, name));
976 if (group_to_use >= darray_size(info->group_names))
977 darray_resize0(info->group_names, group_to_use + 1);
978 darray_item(info->group_names, group_to_use) = name;
984 HandleGlobalVar(SymbolsInfo *info, VarDef *stmt)
986 const char *elem, *field;
990 if (ExprResolveLhs(info->keymap->ctx, stmt->name, &elem, &field,
992 return 0; /* internal error, already reported */
993 if (elem && istreq(elem, "key")) {
994 ret = SetSymbolsField(info, &info->default_key, field, arrayNdx,
997 else if (!elem && (istreq(field, "name") ||
998 istreq(field, "groupname"))) {
999 ret = SetGroupName(info, arrayNdx, stmt->value);
1001 else if (!elem && (istreq(field, "groupswrap") ||
1002 istreq(field, "wrapgroups"))) {
1003 log_err(info->keymap->ctx,
1004 "Global \"groupswrap\" not supported; Ignored\n");
1007 else if (!elem && (istreq(field, "groupsclamp") ||
1008 istreq(field, "clampgroups"))) {
1009 log_err(info->keymap->ctx,
1010 "Global \"groupsclamp\" not supported; Ignored\n");
1013 else if (!elem && (istreq(field, "groupsredirect") ||
1014 istreq(field, "redirectgroups"))) {
1015 log_err(info->keymap->ctx,
1016 "Global \"groupsredirect\" not supported; Ignored\n");
1019 else if (!elem && istreq(field, "allownone")) {
1020 log_err(info->keymap->ctx,
1021 "Radio groups not supported; "
1022 "Ignoring \"allownone\" specification\n");
1026 ret = SetActionField(info->keymap, elem, field, arrayNdx, stmt->value,
1034 HandleSymbolsBody(SymbolsInfo *info, VarDef *def, KeyInfo *keyi)
1037 const char *elem, *field;
1040 for (; def; def = (VarDef *) def->common.next) {
1041 if (def->name && def->name->op == EXPR_FIELD_REF) {
1042 log_err(info->keymap->ctx,
1043 "Cannot set a global default value from within a key statement; "
1044 "Move statements to the global file scope\n");
1049 if (!def->value || def->value->op == EXPR_KEYSYM_LIST)
1056 ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
1061 ok = SetSymbolsField(info, keyi, field, arrayNdx, def->value);
1068 SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi)
1070 xkb_layout_index_t i;
1074 if (info->explicit_group == XKB_LAYOUT_INVALID)
1077 darray_enumerate_from(i, groupi, keyi->groups, 1) {
1078 if (groupi->defined) {
1080 ClearGroupInfo(groupi);
1081 InitGroupInfo(groupi);
1086 log_warn(info->keymap->ctx,
1087 "For the map %s an explicit group specified, "
1088 "but key %s has more than one group defined; "
1089 "All groups except first one will be ignored\n",
1090 info->name, KeyInfoText(info, keyi));
1092 darray_resize0(keyi->groups, info->explicit_group + 1);
1093 if (info->explicit_group > 0) {
1094 darray_item(keyi->groups, info->explicit_group) =
1095 darray_item(keyi->groups, 0);
1096 InitGroupInfo(&darray_item(keyi->groups, 0));
1103 HandleSymbolsDef(SymbolsInfo *info, SymbolsDef *stmt)
1106 xkb_layout_index_t i;
1108 keyi = info->default_key;
1109 darray_init(keyi.groups);
1110 darray_copy(keyi.groups, info->default_key.groups);
1111 for (i = 0; i < darray_size(keyi.groups); i++)
1112 CopyGroupInfo(&darray_item(keyi.groups, i),
1113 &darray_item(info->default_key.groups, i));
1114 keyi.merge = stmt->merge;
1115 keyi.name = stmt->keyName;
1117 if (!HandleSymbolsBody(info, (VarDef *) stmt->symbols, &keyi)) {
1122 if (!SetExplicitGroup(info, &keyi)) {
1127 if (!AddKeySymbols(info, &keyi, true)) {
1136 HandleModMapDef(SymbolsInfo *info, ModMapDef *def)
1140 xkb_mod_index_t ndx;
1142 struct xkb_context *ctx = info->keymap->ctx;
1144 ndx = ModNameToIndex(info->keymap, def->modifier, MOD_REAL);
1145 if (ndx == XKB_MOD_INVALID) {
1146 log_err(info->keymap->ctx,
1147 "Illegal modifier map definition; "
1148 "Ignoring map for non-modifier \"%s\"\n",
1149 xkb_atom_text(ctx, def->modifier));
1155 tmp.merge = def->merge;
1157 for (key = def->keys; key != NULL; key = (ExprDef *) key->common.next) {
1160 if (key->op == EXPR_VALUE && key->value_type == EXPR_TYPE_KEYNAME) {
1161 tmp.haveSymbol = false;
1162 tmp.u.keyName = key->value.keyName;
1164 else if (ExprResolveKeySym(ctx, key, &sym)) {
1165 tmp.haveSymbol = true;
1169 log_err(info->keymap->ctx,
1170 "Modmap entries may contain only key names or keysyms; "
1171 "Illegal definition for %s modifier ignored\n",
1172 ModIndexText(info->keymap, tmp.modifier));
1176 ok = AddModMapEntry(info, &tmp) && ok;
1182 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge)
1187 info->name = strdup_safe(file->name);
1189 for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
1190 switch (stmt->type) {
1192 ok = HandleIncludeSymbols(info, (IncludeStmt *) stmt);
1195 ok = HandleSymbolsDef(info, (SymbolsDef *) stmt);
1198 ok = HandleGlobalVar(info, (VarDef *) stmt);
1201 ok = HandleVModDef(info->keymap, (VModDef *) stmt);
1204 ok = HandleModMapDef(info, (ModMapDef *) stmt);
1207 log_err(info->keymap->ctx,
1208 "Symbols files may not include other types; "
1209 "Ignoring %s\n", stmt_type_to_string(stmt->type));
1217 if (info->errorCount > 10) {
1218 log_err(info->keymap->ctx, "Abandoning symbols file \"%s\"\n",
1226 * Given a keysym @sym, return a key which generates it, or NULL.
1227 * This is used for example in a modifier map definition, such as:
1228 * modifier_map Lock { Caps_Lock };
1229 * where we want to add the Lock modifier to the modmap of the key
1230 * which matches the keysym Caps_Lock.
1231 * Since there can be many keys which generates the keysym, the key
1232 * is chosen first by lowest group in which the keysym appears, than
1233 * by lowest level and than by lowest key code.
1235 static struct xkb_key *
1236 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
1238 struct xkb_key *key, *ret = NULL;
1239 xkb_layout_index_t group, min_group = UINT32_MAX;
1240 xkb_level_index_t level, min_level = UINT16_MAX;
1242 xkb_foreach_key(key, keymap) {
1243 for (group = 0; group < key->num_groups; group++) {
1244 for (level = 0; level < XkbKeyGroupWidth(key, group); level++) {
1245 if (key->groups[group].levels[level].num_syms != 1 ||
1246 key->groups[group].levels[level].u.sym != sym)
1250 * If the keysym was found in a group or level > 0, we must
1251 * keep looking since we might find a key in which the keysym
1252 * is in a lower group or level.
1254 if (group < min_group ||
1255 (group == min_group && level < min_level)) {
1257 if (group == 0 && level == 0) {
1273 * Find an appropriate type for a group and return its name.
1276 * - ONE_LEVEL for width 0/1
1277 * - ALPHABETIC for 2 shift levels, with lower/upercase keysyms
1278 * - KEYPAD for keypad keys.
1279 * - TWO_LEVEL for other 2 shift level keys.
1280 * and the same for four level keys.
1282 * FIXME: Decide how to handle multiple-syms-per-level, and do it.
1285 FindAutomaticType(struct xkb_context *ctx, GroupInfo *groupi)
1287 xkb_keysym_t sym0, sym1, sym2, sym3;
1288 xkb_level_index_t width = darray_size(groupi->levels);
1290 #define GET_SYM(level) \
1291 (darray_item(groupi->levels, level).num_syms == 0 ? \
1292 XKB_KEY_NoSymbol : \
1293 darray_item(groupi->levels, level).num_syms == 1 ? \
1294 darray_item(groupi->levels, level).u.sym : \
1295 /* num_syms > 1 */ \
1296 darray_item(groupi->levels, level).u.syms[0])
1298 if (width == 1 || width <= 0)
1299 return xkb_atom_intern_literal(ctx, "ONE_LEVEL");
1305 if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1))
1306 return xkb_atom_intern_literal(ctx, "ALPHABETIC");
1308 if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1309 return xkb_atom_intern_literal(ctx, "KEYPAD");
1311 return xkb_atom_intern_literal(ctx, "TWO_LEVEL");
1315 if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1)) {
1317 sym3 = (width == 4 ? GET_SYM(3) : XKB_KEY_NoSymbol);
1319 if (xkb_keysym_is_lower(sym2) && xkb_keysym_is_upper(sym3))
1320 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_ALPHABETIC");
1322 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_SEMIALPHABETIC");
1325 if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1326 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_KEYPAD");
1328 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL");
1331 return XKB_ATOM_NONE;
1336 static const struct xkb_key_type *
1337 FindTypeForGroup(struct xkb_keymap *keymap, KeyInfo *keyi,
1338 xkb_layout_index_t group, bool *explicit_type)
1341 GroupInfo *groupi = &darray_item(keyi->groups, group);
1342 xkb_atom_t type_name = groupi->type;
1344 *explicit_type = true;
1346 if (type_name == XKB_ATOM_NONE) {
1347 if (keyi->default_type != XKB_ATOM_NONE) {
1348 type_name = keyi->default_type;
1351 type_name = FindAutomaticType(keymap->ctx, groupi);
1352 if (type_name != XKB_ATOM_NONE)
1353 *explicit_type = false;
1357 if (type_name == XKB_ATOM_NONE) {
1358 log_warn(keymap->ctx,
1359 "Couldn't find an automatic type for key '%s' group %d with %lu levels; "
1360 "Using the default type\n",
1361 KeyNameText(keymap->ctx, keyi->name), group + 1,
1362 (unsigned long) darray_size(groupi->levels));
1366 for (i = 0; i < keymap->num_types; i++)
1367 if (keymap->types[i].name == type_name)
1370 if (i >= keymap->num_types) {
1371 log_warn(keymap->ctx,
1372 "The type \"%s\" for key '%s' group %d was not previously defined; "
1373 "Using the default type\n",
1374 xkb_atom_text(keymap->ctx, type_name),
1375 KeyNameText(keymap->ctx, keyi->name), group + 1);
1379 return &keymap->types[i];
1383 * Index 0 is guaranteed to contain something, usually
1384 * ONE_LEVEL or at least some default one-level type.
1386 return &keymap->types[0];
1390 CopySymbolsDef(SymbolsInfo *info, KeyInfo *keyi)
1392 struct xkb_keymap *keymap = info->keymap;
1393 struct xkb_key *key;
1395 const GroupInfo *group0;
1396 xkb_layout_index_t i;
1399 * The name is guaranteed to be real and not an alias (see
1400 * AddKeySymbols), so 'false' is safe here.
1402 key = XkbKeyByName(keymap, keyi->name, false);
1404 log_vrb(info->keymap->ctx, 5,
1405 "Key %s not found in keycodes; Symbols ignored\n",
1406 KeyInfoText(info, keyi));
1410 /* Find the range of groups we need. */
1411 key->num_groups = 0;
1412 darray_enumerate(i, groupi, keyi->groups)
1413 if (groupi->defined)
1414 key->num_groups = i + 1;
1416 if (key->num_groups <= 0)
1417 return false; /* WSGO */
1419 darray_resize(keyi->groups, key->num_groups);
1422 * If there are empty groups between non-empty ones, fill them with data
1423 * from the first group.
1424 * We can make a wrong assumption here. But leaving gaps is worse.
1426 group0 = &darray_item(keyi->groups, 0);
1427 darray_foreach_from(groupi, keyi->groups, 1) {
1428 if (groupi->defined)
1431 CopyGroupInfo(groupi, group0);
1434 key->groups = calloc(key->num_groups, sizeof(*key->groups));
1436 /* Find and assign the groups' types in the keymap. */
1437 darray_enumerate(i, groupi, keyi->groups) {
1438 const struct xkb_key_type *type;
1441 type = FindTypeForGroup(keymap, keyi, i, &explicit_type);
1443 /* Always have as many levels as the type specifies. */
1444 if (type->num_levels < darray_size(groupi->levels)) {
1445 struct xkb_level *leveli;
1447 log_vrb(info->keymap->ctx, 1,
1448 "Type \"%s\" has %d levels, but %s has %d levels; "
1449 "Ignoring extra symbols\n",
1450 xkb_atom_text(keymap->ctx, type->name), type->num_levels,
1451 KeyInfoText(info, keyi),
1452 (int) darray_size(groupi->levels));
1454 darray_foreach_from(leveli, groupi->levels, type->num_levels)
1455 ClearLevelInfo(leveli);
1457 darray_resize0(groupi->levels, type->num_levels);
1459 key->groups[i].explicit_type = explicit_type;
1460 key->groups[i].type = type;
1464 darray_enumerate(i, groupi, keyi->groups) {
1465 key->groups[i].levels = darray_mem(groupi->levels, 0);
1466 darray_init(groupi->levels);
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 CopyModMapDef(SymbolsInfo *info, ModMapEntry *entry)
1495 struct xkb_key *key;
1496 struct xkb_keymap *keymap = info->keymap;
1498 if (!entry->haveSymbol) {
1499 key = XkbKeyByName(keymap, entry->u.keyName, true);
1501 log_vrb(info->keymap->ctx, 5,
1502 "Key %s not found in keycodes; "
1503 "Modifier map entry for %s not updated\n",
1504 KeyNameText(keymap->ctx, entry->u.keyName),
1505 ModIndexText(info->keymap, entry->modifier));
1510 key = FindKeyForSymbol(keymap, entry->u.keySym);
1512 log_vrb(info->keymap->ctx, 5,
1513 "Key \"%s\" not found in symbol map; "
1514 "Modifier map entry for %s not updated\n",
1515 KeysymText(info->keymap->ctx, entry->u.keySym),
1516 ModIndexText(info->keymap, entry->modifier));
1521 key->modmap |= (1 << entry->modifier);
1526 CopySymbolsToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info)
1530 struct xkb_key *key;
1532 keymap->symbols_section_name = strdup_safe(info->name);
1533 XkbEscapeMapName(keymap->symbols_section_name);
1535 keymap->num_group_names = darray_size(info->group_names);
1536 keymap->group_names = darray_mem(info->group_names, 0);
1537 darray_init(info->group_names);
1539 darray_foreach(keyi, info->keys)
1540 if (!CopySymbolsDef(info, keyi))
1543 if (xkb_context_get_log_verbosity(keymap->ctx) > 3) {
1544 xkb_foreach_key(key, keymap) {
1545 if (key->name == XKB_ATOM_NONE)
1548 if (key->num_groups < 1)
1549 log_info(keymap->ctx,
1550 "No symbols defined for %s\n",
1551 KeyNameText(keymap->ctx, key->name));
1555 darray_foreach(mm, info->modmaps)
1556 if (!CopyModMapDef(info, mm))
1559 /* XXX: If we don't ignore errorCount, things break. */
1564 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1565 enum merge_mode merge)
1568 ActionsInfo *actions;
1570 actions = NewActionsInfo();
1574 InitSymbolsInfo(&info, keymap, actions);
1575 info.default_key.merge = merge;
1577 HandleSymbolsFile(&info, file, merge);
1579 if (info.errorCount != 0)
1582 if (!CopySymbolsToKeymap(keymap, &info))
1585 ClearSymbolsInfo(&info);
1586 FreeActionsInfo(actions);
1590 FreeActionsInfo(actions);
1591 ClearSymbolsInfo(&info);