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->expr.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->expr.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->keysym_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->keysym_list.symsMapIndex, i);
677 leveli->num_syms = darray_item(value->keysym_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->keysym_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->expr.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->expr.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->unary.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->unary.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->expr.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->expr.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->expr.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, 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->expr.op == EXPR_VALUE &&
1161 key->expr.value_type == EXPR_TYPE_KEYNAME) {
1162 tmp.haveSymbol = false;
1163 tmp.u.keyName = key->key_name.key_name;
1165 else if (ExprResolveKeySym(ctx, key, &sym)) {
1166 tmp.haveSymbol = true;
1170 log_err(info->keymap->ctx,
1171 "Modmap entries may contain only key names or keysyms; "
1172 "Illegal definition for %s modifier ignored\n",
1173 ModIndexText(info->keymap, tmp.modifier));
1177 ok = AddModMapEntry(info, &tmp) && ok;
1183 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge)
1188 info->name = strdup_safe(file->name);
1190 for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
1191 switch (stmt->type) {
1193 ok = HandleIncludeSymbols(info, (IncludeStmt *) stmt);
1196 ok = HandleSymbolsDef(info, (SymbolsDef *) stmt);
1199 ok = HandleGlobalVar(info, (VarDef *) stmt);
1202 ok = HandleVModDef(info->keymap, (VModDef *) stmt);
1205 ok = HandleModMapDef(info, (ModMapDef *) stmt);
1208 log_err(info->keymap->ctx,
1209 "Symbols files may not include other types; "
1210 "Ignoring %s\n", stmt_type_to_string(stmt->type));
1218 if (info->errorCount > 10) {
1219 log_err(info->keymap->ctx, "Abandoning symbols file \"%s\"\n",
1227 * Given a keysym @sym, return a key which generates it, or NULL.
1228 * This is used for example in a modifier map definition, such as:
1229 * modifier_map Lock { Caps_Lock };
1230 * where we want to add the Lock modifier to the modmap of the key
1231 * which matches the keysym Caps_Lock.
1232 * Since there can be many keys which generates the keysym, the key
1233 * is chosen first by lowest group in which the keysym appears, than
1234 * by lowest level and than by lowest key code.
1236 static struct xkb_key *
1237 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
1239 struct xkb_key *key, *ret = NULL;
1240 xkb_layout_index_t group, min_group = UINT32_MAX;
1241 xkb_level_index_t level, min_level = UINT16_MAX;
1243 xkb_foreach_key(key, keymap) {
1244 for (group = 0; group < key->num_groups; group++) {
1245 for (level = 0; level < XkbKeyGroupWidth(key, group); level++) {
1246 if (key->groups[group].levels[level].num_syms != 1 ||
1247 key->groups[group].levels[level].u.sym != sym)
1251 * If the keysym was found in a group or level > 0, we must
1252 * keep looking since we might find a key in which the keysym
1253 * is in a lower group or level.
1255 if (group < min_group ||
1256 (group == min_group && level < min_level)) {
1258 if (group == 0 && level == 0) {
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, sym2, sym3;
1289 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)) {
1318 sym3 = (width == 4 ? GET_SYM(3) : XKB_KEY_NoSymbol);
1320 if (xkb_keysym_is_lower(sym2) && xkb_keysym_is_upper(sym3))
1321 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_ALPHABETIC");
1323 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_SEMIALPHABETIC");
1326 if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1327 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_KEYPAD");
1329 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL");
1332 return XKB_ATOM_NONE;
1337 static const struct xkb_key_type *
1338 FindTypeForGroup(struct xkb_keymap *keymap, KeyInfo *keyi,
1339 xkb_layout_index_t group, bool *explicit_type)
1342 GroupInfo *groupi = &darray_item(keyi->groups, group);
1343 xkb_atom_t type_name = groupi->type;
1345 *explicit_type = true;
1347 if (type_name == XKB_ATOM_NONE) {
1348 if (keyi->default_type != XKB_ATOM_NONE) {
1349 type_name = keyi->default_type;
1352 type_name = FindAutomaticType(keymap->ctx, groupi);
1353 if (type_name != XKB_ATOM_NONE)
1354 *explicit_type = false;
1358 if (type_name == XKB_ATOM_NONE) {
1359 log_warn(keymap->ctx,
1360 "Couldn't find an automatic type for key '%s' group %d with %lu levels; "
1361 "Using the default type\n",
1362 KeyNameText(keymap->ctx, keyi->name), group + 1,
1363 (unsigned long) darray_size(groupi->levels));
1367 for (i = 0; i < keymap->num_types; i++)
1368 if (keymap->types[i].name == type_name)
1371 if (i >= keymap->num_types) {
1372 log_warn(keymap->ctx,
1373 "The type \"%s\" for key '%s' group %d was not previously defined; "
1374 "Using the default type\n",
1375 xkb_atom_text(keymap->ctx, type_name),
1376 KeyNameText(keymap->ctx, keyi->name), group + 1);
1380 return &keymap->types[i];
1384 * Index 0 is guaranteed to contain something, usually
1385 * ONE_LEVEL or at least some default one-level type.
1387 return &keymap->types[0];
1391 CopySymbolsDef(SymbolsInfo *info, KeyInfo *keyi)
1393 struct xkb_keymap *keymap = info->keymap;
1394 struct xkb_key *key;
1396 const GroupInfo *group0;
1397 xkb_layout_index_t i;
1400 * The name is guaranteed to be real and not an alias (see
1401 * AddKeySymbols), so 'false' is safe here.
1403 key = XkbKeyByName(keymap, keyi->name, false);
1405 log_vrb(info->keymap->ctx, 5,
1406 "Key %s not found in keycodes; Symbols ignored\n",
1407 KeyInfoText(info, keyi));
1411 /* Find the range of groups we need. */
1412 key->num_groups = 0;
1413 darray_enumerate(i, groupi, keyi->groups)
1414 if (groupi->defined)
1415 key->num_groups = i + 1;
1417 if (key->num_groups <= 0)
1418 return false; /* WSGO */
1420 darray_resize(keyi->groups, key->num_groups);
1423 * If there are empty groups between non-empty ones, fill them with data
1424 * from the first group.
1425 * We can make a wrong assumption here. But leaving gaps is worse.
1427 group0 = &darray_item(keyi->groups, 0);
1428 darray_foreach_from(groupi, keyi->groups, 1) {
1429 if (groupi->defined)
1432 CopyGroupInfo(groupi, group0);
1435 key->groups = calloc(key->num_groups, sizeof(*key->groups));
1437 /* Find and assign the groups' types in the keymap. */
1438 darray_enumerate(i, groupi, keyi->groups) {
1439 const struct xkb_key_type *type;
1442 type = FindTypeForGroup(keymap, keyi, i, &explicit_type);
1444 /* Always have as many levels as the type specifies. */
1445 if (type->num_levels < darray_size(groupi->levels)) {
1446 struct xkb_level *leveli;
1448 log_vrb(info->keymap->ctx, 1,
1449 "Type \"%s\" has %d levels, but %s has %d levels; "
1450 "Ignoring extra symbols\n",
1451 xkb_atom_text(keymap->ctx, type->name), type->num_levels,
1452 KeyInfoText(info, keyi),
1453 (int) darray_size(groupi->levels));
1455 darray_foreach_from(leveli, groupi->levels, type->num_levels)
1456 ClearLevelInfo(leveli);
1458 darray_resize0(groupi->levels, type->num_levels);
1460 key->groups[i].explicit_type = explicit_type;
1461 key->groups[i].type = type;
1465 darray_enumerate(i, groupi, keyi->groups) {
1466 key->groups[i].levels = darray_mem(groupi->levels, 0);
1467 darray_init(groupi->levels);
1470 key->out_of_range_group_number = keyi->out_of_range_group_number;
1471 key->out_of_range_group_action = keyi->out_of_range_group_action;
1473 if (keyi->defined & KEY_FIELD_VMODMAP) {
1474 key->vmodmap = keyi->vmodmap;
1475 key->explicit |= EXPLICIT_VMODMAP;
1478 if (keyi->repeat != KEY_REPEAT_UNDEFINED) {
1479 key->repeats = (keyi->repeat == KEY_REPEAT_YES);
1480 key->explicit |= EXPLICIT_REPEAT;
1483 darray_foreach(groupi, keyi->groups) {
1484 if (groupi->defined & GROUP_FIELD_ACTS) {
1485 key->explicit |= EXPLICIT_INTERP;
1494 CopyModMapDef(SymbolsInfo *info, ModMapEntry *entry)
1496 struct xkb_key *key;
1497 struct xkb_keymap *keymap = info->keymap;
1499 if (!entry->haveSymbol) {
1500 key = XkbKeyByName(keymap, entry->u.keyName, true);
1502 log_vrb(info->keymap->ctx, 5,
1503 "Key %s not found in keycodes; "
1504 "Modifier map entry for %s not updated\n",
1505 KeyNameText(keymap->ctx, entry->u.keyName),
1506 ModIndexText(info->keymap, entry->modifier));
1511 key = FindKeyForSymbol(keymap, entry->u.keySym);
1513 log_vrb(info->keymap->ctx, 5,
1514 "Key \"%s\" not found in symbol map; "
1515 "Modifier map entry for %s not updated\n",
1516 KeysymText(info->keymap->ctx, entry->u.keySym),
1517 ModIndexText(info->keymap, entry->modifier));
1522 key->modmap |= (1 << entry->modifier);
1527 CopySymbolsToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info)
1531 struct xkb_key *key;
1533 keymap->symbols_section_name = strdup_safe(info->name);
1534 XkbEscapeMapName(keymap->symbols_section_name);
1536 keymap->num_group_names = darray_size(info->group_names);
1537 keymap->group_names = darray_mem(info->group_names, 0);
1538 darray_init(info->group_names);
1540 darray_foreach(keyi, info->keys)
1541 if (!CopySymbolsDef(info, keyi))
1544 if (xkb_context_get_log_verbosity(keymap->ctx) > 3) {
1545 xkb_foreach_key(key, keymap) {
1546 if (key->name == XKB_ATOM_NONE)
1549 if (key->num_groups < 1)
1550 log_info(keymap->ctx,
1551 "No symbols defined for %s\n",
1552 KeyNameText(keymap->ctx, key->name));
1556 darray_foreach(mm, info->modmaps)
1557 if (!CopyModMapDef(info, mm))
1560 /* XXX: If we don't ignore errorCount, things break. */
1565 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1566 enum merge_mode merge)
1569 ActionsInfo *actions;
1571 actions = NewActionsInfo();
1575 InitSymbolsInfo(&info, keymap, actions);
1576 info.default_key.merge = merge;
1578 HandleSymbolsFile(&info, file, merge);
1580 if (info.errorCount != 0)
1583 if (!CopySymbolsToKeymap(keymap, &info))
1586 ClearSymbolsInfo(&info);
1587 FreeActionsInfo(actions);
1591 FreeActionsInfo(actions);
1592 ClearSymbolsInfo(&info);