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>
56 #include "xkbcomp-priv.h"
65 KEY_REPEAT_UNDEFINED = 0,
71 GROUP_FIELD_SYMS = (1 << 0),
72 GROUP_FIELD_ACTS = (1 << 1),
73 GROUP_FIELD_TYPE = (1 << 2),
77 KEY_FIELD_REPEAT = (1 << 0),
78 KEY_FIELD_DEFAULT_TYPE = (1 << 1),
79 KEY_FIELD_GROUPINFO = (1 << 2),
80 KEY_FIELD_VMODMAP = (1 << 3),
84 enum group_field defined;
85 darray(struct xkb_level) levels;
90 enum key_field defined;
91 enum merge_mode merge;
95 darray(GroupInfo) groups;
97 enum key_repeat repeat;
98 xkb_mod_mask_t vmodmap;
99 xkb_atom_t default_type;
101 enum xkb_range_exceed_type out_of_range_group_action;
102 xkb_layout_index_t out_of_range_group_number;
106 ClearLevelInfo(struct xkb_level *leveli)
108 if (leveli->num_syms > 1)
109 free(leveli->u.syms);
113 InitGroupInfo(GroupInfo *groupi)
115 memset(groupi, 0, sizeof(*groupi));
119 ClearGroupInfo(GroupInfo *groupi)
121 struct xkb_level *leveli;
122 darray_foreach(leveli, groupi->levels)
123 ClearLevelInfo(leveli);
124 darray_free(groupi->levels);
128 CopyGroupInfo(GroupInfo *to, const GroupInfo *from)
130 to->defined = from->defined;
131 to->type = from->type;
132 darray_init(to->levels);
133 darray_copy(to->levels, from->levels);
134 for (xkb_level_index_t j = 0; j < darray_size(to->levels); j++)
135 if (darray_item(from->levels, j).num_syms > 1)
136 darray_item(to->levels, j).u.syms =
137 memdup(darray_item(from->levels, j).u.syms,
138 darray_item(from->levels, j).num_syms,
139 sizeof(xkb_keysym_t));
143 InitKeyInfo(struct xkb_context *ctx, KeyInfo *keyi)
145 memset(keyi, 0, sizeof(*keyi));
146 keyi->merge = MERGE_OVERRIDE;
147 keyi->name = xkb_atom_intern_literal(ctx, "*");
148 keyi->out_of_range_group_action = RANGE_WRAP;
152 ClearKeyInfo(KeyInfo *keyi)
155 darray_foreach(groupi, keyi->groups)
156 ClearGroupInfo(groupi);
157 darray_free(keyi->groups);
160 /***====================================================================***/
163 enum merge_mode merge;
165 xkb_mod_index_t modifier;
173 char *name; /* e.g. pc+us+inet(evdev) */
175 enum merge_mode merge;
176 xkb_layout_index_t explicit_group;
177 darray(KeyInfo) keys;
179 ActionsInfo *actions;
180 darray(xkb_atom_t) group_names;
181 darray(ModMapEntry) modmaps;
182 struct xkb_mod_set mods;
184 struct xkb_context *ctx;
185 /* Needed for AddKeySymbols. */
186 const struct xkb_keymap *keymap;
190 InitSymbolsInfo(SymbolsInfo *info, const struct xkb_keymap *keymap,
191 ActionsInfo *actions, const struct xkb_mod_set *mods)
193 memset(info, 0, sizeof(*info));
194 info->ctx = keymap->ctx;
195 info->keymap = keymap;
196 info->merge = MERGE_OVERRIDE;
197 InitKeyInfo(keymap->ctx, &info->default_key);
198 info->actions = actions;
200 info->explicit_group = XKB_LAYOUT_INVALID;
204 ClearSymbolsInfo(SymbolsInfo *info)
208 darray_foreach(keyi, info->keys)
210 darray_free(info->keys);
211 darray_free(info->group_names);
212 darray_free(info->modmaps);
213 ClearKeyInfo(&info->default_key);
217 KeyInfoText(SymbolsInfo *info, KeyInfo *keyi)
219 return KeyNameText(info->ctx, keyi->name);
223 MergeGroups(SymbolsInfo *info, GroupInfo *into, GroupInfo *from, bool clobber,
224 bool report, xkb_layout_index_t group, xkb_atom_t key_name)
226 xkb_level_index_t i, levels_in_both;
227 struct xkb_level *level;
229 /* First find the type of the merged group. */
230 if (into->type != from->type) {
231 if (from->type == XKB_ATOM_NONE) {
232 /* it's empty for consistency with other comparisons */
234 else if (into->type == XKB_ATOM_NONE) {
235 into->type = from->type;
238 xkb_atom_t use = (clobber ? from->type : into->type);
239 xkb_atom_t ignore = (clobber ? into->type : from->type);
243 "Multiple definitions for group %d type of key %s; "
244 "Using %s, ignoring %s\n",
245 group + 1, KeyNameText(info->ctx, key_name),
246 xkb_atom_text(info->ctx, use),
247 xkb_atom_text(info->ctx, ignore));
252 into->defined |= (from->defined & GROUP_FIELD_TYPE);
254 /* Now look at the levels. */
256 if (darray_empty(from->levels)) {
261 if (darray_empty(into->levels)) {
262 from->type = into->type;
268 /* Merge the actions and syms. */
269 levels_in_both = MIN(darray_size(into->levels), darray_size(from->levels));
270 for (i = 0; i < levels_in_both; i++) {
271 struct xkb_level *intoLevel = &darray_item(into->levels, i);
272 struct xkb_level *fromLevel = &darray_item(from->levels, i);
274 if (fromLevel->action.type == ACTION_TYPE_NONE) {
275 /* it's empty for consistency with other comparisons */
277 else if (intoLevel->action.type == ACTION_TYPE_NONE) {
278 intoLevel->action = fromLevel->action;
281 union xkb_action *use, *ignore;
282 use = (clobber ? &fromLevel->action : &intoLevel->action);
283 ignore = (clobber ? &intoLevel->action : &fromLevel->action);
287 "Multiple actions for level %d/group %u on key %s; "
288 "Using %s, ignoring %s\n",
289 i + 1, group + 1, KeyNameText(info->ctx, key_name),
290 ActionTypeText(use->type),
291 ActionTypeText(ignore->type));
293 intoLevel->action = *use;
296 if (fromLevel->num_syms == 0) {
297 /* it's empty for consistency with other comparisons */
299 else if (intoLevel->num_syms == 0) {
300 intoLevel->num_syms = fromLevel->num_syms;
301 if (fromLevel->num_syms > 1)
302 intoLevel->u.syms = fromLevel->u.syms;
304 intoLevel->u.sym = fromLevel->u.sym;
305 fromLevel->num_syms = 0;
307 else if (!XkbLevelsSameSyms(fromLevel, intoLevel)) {
310 "Multiple symbols for level %d/group %u on key %s; "
311 "Using %s, ignoring %s\n",
312 i + 1, group + 1, KeyNameText(info->ctx, key_name),
313 (clobber ? "from" : "to"),
314 (clobber ? "to" : "from"));
317 ClearLevelInfo(intoLevel);
318 intoLevel->num_syms = fromLevel->num_syms;
319 if (fromLevel->num_syms > 1)
320 intoLevel->u.syms = fromLevel->u.syms;
322 intoLevel->u.sym = fromLevel->u.sym;
323 fromLevel->num_syms = 0;
327 /* If @from has extra levels, get them as well. */
328 darray_foreach_from(level, from->levels, levels_in_both) {
329 darray_append(into->levels, *level);
332 into->defined |= (from->defined & GROUP_FIELD_ACTS);
333 into->defined |= (from->defined & GROUP_FIELD_SYMS);
339 UseNewKeyField(enum key_field field, enum key_field old, enum key_field new,
340 bool clobber, bool report, enum key_field *collide)
343 return (new & field);
357 MergeKeys(SymbolsInfo *info, KeyInfo *into, KeyInfo *from, bool same_file)
359 xkb_layout_index_t i;
360 xkb_layout_index_t groups_in_both;
361 enum key_field collide = 0;
362 const int verbosity = xkb_context_get_log_verbosity(info->ctx);
363 const bool clobber = (from->merge != MERGE_AUGMENT);
364 const bool report = (same_file && verbosity > 0) || verbosity > 9;
366 if (from->merge == MERGE_REPLACE) {
369 InitKeyInfo(info->ctx, from);
373 groups_in_both = MIN(darray_size(into->groups), darray_size(from->groups));
374 for (i = 0; i < groups_in_both; i++)
376 &darray_item(into->groups, i),
377 &darray_item(from->groups, i),
378 clobber, report, i, into->name);
379 /* If @from has extra groups, just move them to @into. */
380 for (i = groups_in_both; i < darray_size(from->groups); i++) {
381 darray_append(into->groups, darray_item(from->groups, i));
382 InitGroupInfo(&darray_item(from->groups, i));
385 if (UseNewKeyField(KEY_FIELD_VMODMAP, into->defined, from->defined,
386 clobber, report, &collide)) {
387 into->vmodmap = from->vmodmap;
388 into->defined |= KEY_FIELD_VMODMAP;
390 if (UseNewKeyField(KEY_FIELD_REPEAT, into->defined, from->defined,
391 clobber, report, &collide)) {
392 into->repeat = from->repeat;
393 into->defined |= KEY_FIELD_REPEAT;
395 if (UseNewKeyField(KEY_FIELD_DEFAULT_TYPE, into->defined, from->defined,
396 clobber, report, &collide)) {
397 into->default_type = from->default_type;
398 into->defined |= KEY_FIELD_DEFAULT_TYPE;
400 if (UseNewKeyField(KEY_FIELD_GROUPINFO, into->defined, from->defined,
401 clobber, report, &collide)) {
402 into->out_of_range_group_action = from->out_of_range_group_action;
403 into->out_of_range_group_number = from->out_of_range_group_number;
404 into->defined |= KEY_FIELD_GROUPINFO;
409 "Symbol map for key %s redefined; "
410 "Using %s definition for conflicting fields\n",
411 KeyNameText(info->ctx, into->name),
412 (clobber ? "first" : "last"));
415 InitKeyInfo(info->ctx, from);
419 /* TODO: Make it so this function doesn't need the entire keymap. */
421 AddKeySymbols(SymbolsInfo *info, KeyInfo *keyi, bool same_file)
423 xkb_atom_t real_name;
427 * Don't keep aliases in the keys array; this guarantees that
428 * searching for keys to merge with by straight comparison (see the
429 * following loop) is enough, and we won't get multiple KeyInfo's
430 * for the same key because of aliases.
432 real_name = XkbResolveKeyAlias(info->keymap, keyi->name);
433 if (real_name != XKB_ATOM_NONE)
434 keyi->name = real_name;
436 darray_foreach(iter, info->keys)
437 if (iter->name == keyi->name)
438 return MergeKeys(info, iter, keyi, same_file);
440 darray_append(info->keys, *keyi);
441 InitKeyInfo(info->ctx, keyi);
446 AddModMapEntry(SymbolsInfo *info, ModMapEntry *new)
449 bool clobber = (new->merge != MERGE_AUGMENT);
451 darray_foreach(old, info->modmaps) {
452 xkb_mod_index_t use, ignore;
454 if ((new->haveSymbol != old->haveSymbol) ||
455 (new->haveSymbol && new->u.keySym != old->u.keySym) ||
456 (!new->haveSymbol && new->u.keyName != old->u.keyName))
459 if (new->modifier == old->modifier)
462 use = (clobber ? new->modifier : old->modifier);
463 ignore = (clobber ? old->modifier : new->modifier);
467 "Symbol \"%s\" added to modifier map for multiple modifiers; "
468 "Using %s, ignoring %s\n",
469 KeysymText(info->ctx, new->u.keySym),
470 ModIndexText(info->ctx, &info->mods, use),
471 ModIndexText(info->ctx, &info->mods, ignore));
474 "Key \"%s\" added to modifier map for multiple modifiers; "
475 "Using %s, ignoring %s\n",
476 KeyNameText(info->ctx, new->u.keyName),
477 ModIndexText(info->ctx, &info->mods, use),
478 ModIndexText(info->ctx, &info->mods, ignore));
484 darray_append(info->modmaps, *new);
488 /***====================================================================***/
491 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
492 enum merge_mode merge)
494 xkb_atom_t *group_name;
495 xkb_layout_index_t group_names_in_both;
497 if (from->errorCount > 0) {
498 into->errorCount += from->errorCount;
502 into->mods = from->mods;
504 if (into->name == NULL) {
505 into->name = from->name;
509 group_names_in_both = MIN(darray_size(into->group_names),
510 darray_size(from->group_names));
511 for (xkb_layout_index_t i = 0; i < group_names_in_both; i++) {
512 if (!darray_item(from->group_names, i))
515 if (merge == MERGE_AUGMENT && darray_item(into->group_names, i))
518 darray_item(into->group_names, i) = darray_item(from->group_names, i);
520 /* If @from has more, get them as well. */
521 darray_foreach_from(group_name, from->group_names, group_names_in_both)
522 darray_append(into->group_names, *group_name);
524 if (darray_empty(into->keys)) {
525 into->keys = from->keys;
526 darray_init(from->keys);
530 darray_foreach(keyi, from->keys) {
531 keyi->merge = (merge == MERGE_DEFAULT ? keyi->merge : merge);
532 if (!AddKeySymbols(into, keyi, false))
537 if (darray_empty(into->modmaps)) {
538 into->modmaps = from->modmaps;
539 darray_init(from->modmaps);
543 darray_foreach(mm, from->modmaps) {
544 mm->merge = (merge == MERGE_DEFAULT ? mm->merge : merge);
545 if (!AddModMapEntry(into, mm))
552 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge);
555 HandleIncludeSymbols(SymbolsInfo *info, IncludeStmt *include)
557 SymbolsInfo included;
559 InitSymbolsInfo(&included, info->keymap, info->actions, &info->mods);
560 included.name = include->stmt;
561 include->stmt = NULL;
563 for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
564 SymbolsInfo next_incl;
567 file = ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_SYMBOLS);
569 info->errorCount += 10;
570 ClearSymbolsInfo(&included);
574 InitSymbolsInfo(&next_incl, info->keymap, info->actions,
576 if (stmt->modifier) {
577 next_incl.explicit_group = atoi(stmt->modifier) - 1;
578 if (next_incl.explicit_group >= XKB_MAX_GROUPS) {
580 "Cannot set explicit group to %d - must be between 1..%d; "
581 "Ignoring group number\n",
582 next_incl.explicit_group + 1, XKB_MAX_GROUPS);
583 next_incl.explicit_group = info->explicit_group;
587 next_incl.explicit_group = info->explicit_group;
590 HandleSymbolsFile(&next_incl, file, MERGE_OVERRIDE);
592 MergeIncludedSymbols(&included, &next_incl, stmt->merge);
594 ClearSymbolsInfo(&next_incl);
598 MergeIncludedSymbols(info, &included, include->merge);
599 ClearSymbolsInfo(&included);
601 return (info->errorCount == 0);
608 GetGroupIndex(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
609 unsigned what, xkb_layout_index_t *ndx_rtrn)
611 const char *name = (what == SYMBOLS ? "symbols" : "actions");
613 if (arrayNdx == NULL) {
614 xkb_layout_index_t i;
616 enum group_field field = (what == SYMBOLS ?
617 GROUP_FIELD_SYMS : GROUP_FIELD_ACTS);
619 darray_enumerate(i, groupi, keyi->groups) {
620 if (!(groupi->defined & field)) {
626 if (i >= XKB_MAX_GROUPS) {
628 "Too many groups of %s for key %s (max %u); "
629 "Ignoring %s defined for extra groups\n",
630 name, KeyInfoText(info, keyi), XKB_MAX_GROUPS, name);
634 darray_resize0(keyi->groups, darray_size(keyi->groups) + 1);
635 *ndx_rtrn = darray_size(keyi->groups) - 1;
639 if (!ExprResolveGroup(info->ctx, arrayNdx, ndx_rtrn)) {
641 "Illegal group index for %s of key %s\n"
642 "Definition with non-integer array index ignored\n",
643 name, KeyInfoText(info, keyi));
648 if (*ndx_rtrn >= darray_size(keyi->groups))
649 darray_resize0(keyi->groups, *ndx_rtrn + 1);
655 AddSymbolsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
658 xkb_layout_index_t ndx;
660 xkb_level_index_t nLevels;
662 if (!GetGroupIndex(info, keyi, arrayNdx, SYMBOLS, &ndx))
665 groupi = &darray_item(keyi->groups, ndx);
668 groupi->defined |= GROUP_FIELD_SYMS;
672 if (value->expr.op != EXPR_KEYSYM_LIST) {
674 "Expected a list of symbols, found %s; "
675 "Ignoring symbols for group %u of %s\n",
676 expr_op_type_to_string(value->expr.op), ndx + 1,
677 KeyInfoText(info, keyi));
681 if (groupi->defined & GROUP_FIELD_SYMS) {
683 "Symbols for key %s, group %u already defined; "
684 "Ignoring duplicate definition\n",
685 KeyInfoText(info, keyi), ndx + 1);
689 nLevels = darray_size(value->keysym_list.symsMapIndex);
690 if (darray_size(groupi->levels) < nLevels)
691 darray_resize0(groupi->levels, nLevels);
693 groupi->defined |= GROUP_FIELD_SYMS;
695 for (xkb_level_index_t i = 0; i < nLevels; i++) {
696 unsigned int sym_index;
697 struct xkb_level *leveli = &darray_item(groupi->levels, i);
699 sym_index = darray_item(value->keysym_list.symsMapIndex, i);
700 leveli->num_syms = darray_item(value->keysym_list.symsNumEntries, i);
701 if (leveli->num_syms > 1)
702 leveli->u.syms = calloc(leveli->num_syms, sizeof(*leveli->u.syms));
704 for (unsigned j = 0; j < leveli->num_syms; j++) {
705 xkb_keysym_t keysym = darray_item(value->keysym_list.syms,
708 if (leveli->num_syms == 1) {
709 if (keysym == XKB_KEY_NoSymbol)
710 leveli->num_syms = 0;
712 leveli->u.sym = keysym;
714 else if (leveli->num_syms > 1) {
715 leveli->u.syms[j] = keysym;
724 AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
727 xkb_layout_index_t ndx;
732 if (!GetGroupIndex(info, keyi, arrayNdx, ACTIONS, &ndx))
735 groupi = &darray_item(keyi->groups, ndx);
738 groupi->defined |= GROUP_FIELD_ACTS;
742 if (value->expr.op != EXPR_ACTION_LIST) {
744 "Bad expression type (%d) for action list value; "
745 "Ignoring actions for group %u of %s\n",
746 value->expr.op, ndx, KeyInfoText(info, keyi));
750 if (groupi->defined & GROUP_FIELD_ACTS) {
752 "Actions for key %s, group %u already defined\n",
753 KeyInfoText(info, keyi), ndx);
758 for (act = value->actions.actions; act; act = (ExprDef *) act->common.next)
761 if (darray_size(groupi->levels) < nActs)
762 darray_resize0(groupi->levels, nActs);
764 groupi->defined |= GROUP_FIELD_ACTS;
766 act = value->actions.actions;
767 for (unsigned i = 0; i < nActs; i++) {
768 union xkb_action *toAct = &darray_item(groupi->levels, i).action;
770 if (!HandleActionDef(info->ctx, info->actions, &info->mods, act, toAct))
772 "Illegal action definition for %s; "
773 "Action for group %u/level %u ignored\n",
774 KeyInfoText(info, keyi), ndx + 1, i + 1);
776 act = (ExprDef *) act->common.next;
782 static const LookupEntry repeatEntries[] = {
783 { "true", KEY_REPEAT_YES },
784 { "yes", KEY_REPEAT_YES },
785 { "on", KEY_REPEAT_YES },
786 { "false", KEY_REPEAT_NO },
787 { "no", KEY_REPEAT_NO },
788 { "off", KEY_REPEAT_NO },
789 { "default", KEY_REPEAT_UNDEFINED },
794 SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
795 ExprDef *arrayNdx, ExprDef *value)
797 if (istreq(field, "type")) {
798 xkb_layout_index_t ndx;
801 if (!ExprResolveString(info->ctx, value, &val)) {
803 "The type field of a key symbol map must be a string; "
804 "Ignoring illegal type definition\n");
809 keyi->default_type = val;
810 keyi->defined |= KEY_FIELD_DEFAULT_TYPE;
812 else if (!ExprResolveGroup(info->ctx, arrayNdx, &ndx)) {
814 "Illegal group index for type of key %s; "
815 "Definition with non-integer array index ignored\n",
816 KeyInfoText(info, keyi));
821 if (ndx >= darray_size(keyi->groups))
822 darray_resize0(keyi->groups, ndx + 1);
823 darray_item(keyi->groups, ndx).type = val;
824 darray_item(keyi->groups, ndx).defined |= GROUP_FIELD_TYPE;
827 else if (istreq(field, "symbols")) {
828 return AddSymbolsToKey(info, keyi, arrayNdx, value);
830 else if (istreq(field, "actions")) {
831 return AddActionsToKey(info, keyi, arrayNdx, value);
833 else if (istreq(field, "vmods") ||
834 istreq(field, "virtualmods") ||
835 istreq(field, "virtualmodifiers")) {
838 if (!ExprResolveModMask(info->ctx, value, MOD_VIRT, &info->mods,
841 "Expected a virtual modifier mask, found %s; "
842 "Ignoring virtual modifiers definition for key %s\n",
843 expr_op_type_to_string(value->expr.op),
844 KeyInfoText(info, keyi));
848 keyi->vmodmap = mask;
849 keyi->defined |= KEY_FIELD_VMODMAP;
851 else if (istreq(field, "locking") ||
852 istreq(field, "lock") ||
853 istreq(field, "locks")) {
854 log_vrb(info->ctx, 1,
855 "Key behaviors not supported; "
856 "Ignoring locking specification for key %s\n",
857 KeyInfoText(info, keyi));
859 else if (istreq(field, "radiogroup") ||
860 istreq(field, "permanentradiogroup") ||
861 istreq(field, "allownone")) {
862 log_vrb(info->ctx, 1,
863 "Radio groups not supported; "
864 "Ignoring radio group specification for key %s\n",
865 KeyInfoText(info, keyi));
867 else if (istreq_prefix("overlay", field) ||
868 istreq_prefix("permanentoverlay", field)) {
869 log_vrb(info->ctx, 1,
870 "Overlays not supported; "
871 "Ignoring overlay specification for key %s\n",
872 KeyInfoText(info, keyi));
874 else if (istreq(field, "repeating") ||
875 istreq(field, "repeats") ||
876 istreq(field, "repeat")) {
879 if (!ExprResolveEnum(info->ctx, value, &val, repeatEntries)) {
881 "Illegal repeat setting for %s; "
882 "Non-boolean repeat setting ignored\n",
883 KeyInfoText(info, keyi));
888 keyi->defined |= KEY_FIELD_REPEAT;
890 else if (istreq(field, "groupswrap") ||
891 istreq(field, "wrapgroups")) {
894 if (!ExprResolveBoolean(info->ctx, value, &set)) {
896 "Illegal groupsWrap setting for %s; "
897 "Non-boolean value ignored\n",
898 KeyInfoText(info, keyi));
902 keyi->out_of_range_group_action = (set ? RANGE_WRAP : RANGE_SATURATE);
903 keyi->defined |= KEY_FIELD_GROUPINFO;
905 else if (istreq(field, "groupsclamp") ||
906 istreq(field, "clampgroups")) {
909 if (!ExprResolveBoolean(info->ctx, value, &set)) {
911 "Illegal groupsClamp setting for %s; "
912 "Non-boolean value ignored\n",
913 KeyInfoText(info, keyi));
917 keyi->out_of_range_group_action = (set ? RANGE_SATURATE : RANGE_WRAP);
918 keyi->defined |= KEY_FIELD_GROUPINFO;
920 else if (istreq(field, "groupsredirect") ||
921 istreq(field, "redirectgroups")) {
922 xkb_layout_index_t grp;
924 if (!ExprResolveGroup(info->ctx, value, &grp)) {
926 "Illegal group index for redirect of key %s; "
927 "Definition with non-integer group ignored\n",
928 KeyInfoText(info, keyi));
932 keyi->out_of_range_group_action = RANGE_REDIRECT;
933 keyi->out_of_range_group_number = grp - 1;
934 keyi->defined |= KEY_FIELD_GROUPINFO;
938 "Unknown field %s in a symbol interpretation; "
939 "Definition ignored\n",
948 SetGroupName(SymbolsInfo *info, ExprDef *arrayNdx, ExprDef *value)
950 xkb_layout_index_t group, group_to_use;
954 log_vrb(info->ctx, 1,
955 "You must specify an index when specifying a group name; "
956 "Group name definition without array subscript ignored\n");
960 if (!ExprResolveGroup(info->ctx, arrayNdx, &group)) {
962 "Illegal index in group name definition; "
963 "Definition with non-integer array index ignored\n");
967 if (!ExprResolveString(info->ctx, value, &name)) {
969 "Group name must be a string; "
970 "Illegal name for group %d ignored\n", group);
974 if (info->explicit_group == XKB_LAYOUT_INVALID) {
975 group_to_use = group - 1;
977 else if (group - 1 == 0) {
978 group_to_use = info->explicit_group;
982 "An explicit group was specified for the '%s' map, "
983 "but it provides a name for a group other than Group1 (%d); "
984 "Ignoring group name '%s'\n",
986 xkb_atom_text(info->ctx, name));
990 if (group_to_use >= darray_size(info->group_names))
991 darray_resize0(info->group_names, group_to_use + 1);
992 darray_item(info->group_names, group_to_use) = name;
998 HandleGlobalVar(SymbolsInfo *info, VarDef *stmt)
1000 const char *elem, *field;
1004 if (!ExprResolveLhs(info->ctx, stmt->name, &elem, &field, &arrayNdx))
1007 if (elem && istreq(elem, "key")) {
1008 ret = SetSymbolsField(info, &info->default_key, field, arrayNdx,
1011 else if (!elem && (istreq(field, "name") ||
1012 istreq(field, "groupname"))) {
1013 ret = SetGroupName(info, arrayNdx, stmt->value);
1015 else if (!elem && (istreq(field, "groupswrap") ||
1016 istreq(field, "wrapgroups"))) {
1018 "Global \"groupswrap\" not supported; Ignored\n");
1021 else if (!elem && (istreq(field, "groupsclamp") ||
1022 istreq(field, "clampgroups"))) {
1024 "Global \"groupsclamp\" not supported; Ignored\n");
1027 else if (!elem && (istreq(field, "groupsredirect") ||
1028 istreq(field, "redirectgroups"))) {
1030 "Global \"groupsredirect\" not supported; Ignored\n");
1033 else if (!elem && istreq(field, "allownone")) {
1035 "Radio groups not supported; "
1036 "Ignoring \"allownone\" specification\n");
1040 ret = SetActionField(info->ctx, info->actions, &info->mods,
1041 elem, field, arrayNdx, stmt->value);
1048 HandleSymbolsBody(SymbolsInfo *info, VarDef *def, KeyInfo *keyi)
1051 const char *elem, *field;
1054 for (; def; def = (VarDef *) def->common.next) {
1055 if (def->name && def->name->expr.op == EXPR_FIELD_REF) {
1057 "Cannot set a global default value from within a key statement; "
1058 "Move statements to the global file scope\n");
1063 if (!def->value || def->value->expr.op == EXPR_KEYSYM_LIST)
1070 ok = ExprResolveLhs(info->ctx, def->name, &elem, &field,
1075 ok = SetSymbolsField(info, keyi, field, arrayNdx, def->value);
1082 SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi)
1084 xkb_layout_index_t i;
1088 if (info->explicit_group == XKB_LAYOUT_INVALID)
1091 darray_enumerate_from(i, groupi, keyi->groups, 1) {
1092 if (groupi->defined) {
1094 ClearGroupInfo(groupi);
1095 InitGroupInfo(groupi);
1101 "For the map %s an explicit group specified, "
1102 "but key %s has more than one group defined; "
1103 "All groups except first one will be ignored\n",
1104 info->name, KeyInfoText(info, keyi));
1106 darray_resize0(keyi->groups, info->explicit_group + 1);
1107 if (info->explicit_group > 0) {
1108 darray_item(keyi->groups, info->explicit_group) =
1109 darray_item(keyi->groups, 0);
1110 InitGroupInfo(&darray_item(keyi->groups, 0));
1117 HandleSymbolsDef(SymbolsInfo *info, SymbolsDef *stmt)
1121 keyi = info->default_key;
1122 darray_init(keyi.groups);
1123 darray_copy(keyi.groups, info->default_key.groups);
1124 for (xkb_layout_index_t i = 0; i < darray_size(keyi.groups); i++)
1125 CopyGroupInfo(&darray_item(keyi.groups, i),
1126 &darray_item(info->default_key.groups, i));
1127 keyi.merge = stmt->merge;
1128 keyi.name = stmt->keyName;
1130 if (!HandleSymbolsBody(info, stmt->symbols, &keyi)) {
1135 if (!SetExplicitGroup(info, &keyi)) {
1140 if (!AddKeySymbols(info, &keyi, true)) {
1149 HandleModMapDef(SymbolsInfo *info, ModMapDef *def)
1152 xkb_mod_index_t ndx;
1154 struct xkb_context *ctx = info->ctx;
1156 ndx = XkbModNameToIndex(&info->mods, def->modifier, MOD_REAL);
1157 if (ndx == XKB_MOD_INVALID) {
1159 "Illegal modifier map definition; "
1160 "Ignoring map for non-modifier \"%s\"\n",
1161 xkb_atom_text(ctx, def->modifier));
1167 tmp.merge = def->merge;
1169 for (ExprDef *key = def->keys; key; key = (ExprDef *) key->common.next) {
1172 if (key->expr.op == EXPR_VALUE &&
1173 key->expr.value_type == EXPR_TYPE_KEYNAME) {
1174 tmp.haveSymbol = false;
1175 tmp.u.keyName = key->key_name.key_name;
1177 else if (ExprResolveKeySym(ctx, key, &sym)) {
1178 tmp.haveSymbol = true;
1183 "Modmap entries may contain only key names or keysyms; "
1184 "Illegal definition for %s modifier ignored\n",
1185 ModIndexText(info->ctx, &info->mods, tmp.modifier));
1189 ok = AddModMapEntry(info, &tmp) && ok;
1195 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge)
1200 info->name = strdup_safe(file->name);
1202 for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
1203 switch (stmt->type) {
1205 ok = HandleIncludeSymbols(info, (IncludeStmt *) stmt);
1208 ok = HandleSymbolsDef(info, (SymbolsDef *) stmt);
1211 ok = HandleGlobalVar(info, (VarDef *) stmt);
1214 ok = HandleVModDef(info->ctx, &info->mods, (VModDef *) stmt, merge);
1217 ok = HandleModMapDef(info, (ModMapDef *) stmt);
1221 "Symbols files may not include other types; "
1222 "Ignoring %s\n", stmt_type_to_string(stmt->type));
1230 if (info->errorCount > 10) {
1231 log_err(info->ctx, "Abandoning symbols file \"%s\"\n",
1239 * Given a keysym @sym, return a key which generates it, or NULL.
1240 * This is used for example in a modifier map definition, such as:
1241 * modifier_map Lock { Caps_Lock };
1242 * where we want to add the Lock modifier to the modmap of the key
1243 * which matches the keysym Caps_Lock.
1244 * Since there can be many keys which generates the keysym, the key
1245 * is chosen first by lowest group in which the keysym appears, than
1246 * by lowest level and than by lowest key code.
1248 static struct xkb_key *
1249 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
1251 struct xkb_key *key;
1252 xkb_layout_index_t group;
1253 bool got_one_group, got_one_level;
1257 xkb_level_index_t level = 0;
1258 got_one_group = false;
1260 got_one_level = false;
1261 xkb_keys_foreach(key, keymap) {
1262 if (group < key->num_groups &&
1263 level < XkbKeyNumLevels(key, group)) {
1264 got_one_group = got_one_level = true;
1265 if (key->groups[group].levels[level].num_syms == 1 &&
1266 key->groups[group].levels[level].u.sym == sym)
1271 } while (got_one_level);
1273 } while (got_one_group);
1279 * Find an appropriate type for a group and return its name.
1282 * - ONE_LEVEL for width 0/1
1283 * - ALPHABETIC for 2 shift levels, with lower/upercase keysyms
1284 * - KEYPAD for keypad keys.
1285 * - TWO_LEVEL for other 2 shift level keys.
1286 * and the same for four level keys.
1288 * FIXME: Decide how to handle multiple-syms-per-level, and do it.
1291 FindAutomaticType(struct xkb_context *ctx, GroupInfo *groupi)
1293 xkb_keysym_t sym0, sym1;
1294 const xkb_level_index_t width = darray_size(groupi->levels);
1296 #define GET_SYM(level) \
1297 (darray_item(groupi->levels, level).num_syms == 0 ? \
1298 XKB_KEY_NoSymbol : \
1299 darray_item(groupi->levels, level).num_syms == 1 ? \
1300 darray_item(groupi->levels, level).u.sym : \
1301 /* num_syms > 1 */ \
1302 darray_item(groupi->levels, level).u.syms[0])
1304 if (width == 1 || width <= 0)
1305 return xkb_atom_intern_literal(ctx, "ONE_LEVEL");
1311 if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1))
1312 return xkb_atom_intern_literal(ctx, "ALPHABETIC");
1314 if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1315 return xkb_atom_intern_literal(ctx, "KEYPAD");
1317 return xkb_atom_intern_literal(ctx, "TWO_LEVEL");
1321 if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1)) {
1322 xkb_keysym_t sym2, sym3;
1324 sym3 = (width == 4 ? GET_SYM(3) : XKB_KEY_NoSymbol);
1326 if (xkb_keysym_is_lower(sym2) && xkb_keysym_is_upper(sym3))
1327 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_ALPHABETIC");
1329 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_SEMIALPHABETIC");
1332 if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1333 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_KEYPAD");
1335 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL");
1338 return XKB_ATOM_NONE;
1343 static const struct xkb_key_type *
1344 FindTypeForGroup(struct xkb_keymap *keymap, KeyInfo *keyi,
1345 xkb_layout_index_t group, bool *explicit_type)
1348 GroupInfo *groupi = &darray_item(keyi->groups, group);
1349 xkb_atom_t type_name = groupi->type;
1351 *explicit_type = true;
1353 if (type_name == XKB_ATOM_NONE) {
1354 if (keyi->default_type != XKB_ATOM_NONE) {
1355 type_name = keyi->default_type;
1358 type_name = FindAutomaticType(keymap->ctx, groupi);
1359 if (type_name != XKB_ATOM_NONE)
1360 *explicit_type = false;
1364 if (type_name == XKB_ATOM_NONE) {
1365 log_warn(keymap->ctx,
1366 "Couldn't find an automatic type for key '%s' group %d with %lu levels; "
1367 "Using the default type\n",
1368 KeyNameText(keymap->ctx, keyi->name), group + 1,
1369 (unsigned long) darray_size(groupi->levels));
1373 for (i = 0; i < keymap->num_types; i++)
1374 if (keymap->types[i].name == type_name)
1377 if (i >= keymap->num_types) {
1378 log_warn(keymap->ctx,
1379 "The type \"%s\" for key '%s' group %d was not previously defined; "
1380 "Using the default type\n",
1381 xkb_atom_text(keymap->ctx, type_name),
1382 KeyNameText(keymap->ctx, keyi->name), group + 1);
1386 return &keymap->types[i];
1390 * Index 0 is guaranteed to contain something, usually
1391 * ONE_LEVEL or at least some default one-level type.
1393 return &keymap->types[0];
1397 CopySymbolsDefToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info,
1400 struct xkb_key *key;
1402 const GroupInfo *group0;
1403 xkb_layout_index_t i;
1406 * The name is guaranteed to be real and not an alias (see
1407 * AddKeySymbols), so 'false' is safe here.
1409 key = XkbKeyByName(keymap, keyi->name, false);
1411 log_vrb(info->ctx, 5,
1412 "Key %s not found in keycodes; Symbols ignored\n",
1413 KeyInfoText(info, keyi));
1417 /* Find the range of groups we need. */
1418 key->num_groups = 0;
1419 darray_enumerate(i, groupi, keyi->groups)
1420 if (groupi->defined)
1421 key->num_groups = i + 1;
1423 if (key->num_groups <= 0)
1424 return false; /* WSGO */
1426 darray_resize(keyi->groups, key->num_groups);
1429 * If there are empty groups between non-empty ones, fill them with data
1430 * from the first group.
1431 * We can make a wrong assumption here. But leaving gaps is worse.
1433 group0 = &darray_item(keyi->groups, 0);
1434 darray_foreach_from(groupi, keyi->groups, 1) {
1435 if (groupi->defined)
1438 CopyGroupInfo(groupi, group0);
1441 key->groups = calloc(key->num_groups, sizeof(*key->groups));
1443 /* Find and assign the groups' types in the keymap. */
1444 darray_enumerate(i, groupi, keyi->groups) {
1445 const struct xkb_key_type *type;
1448 type = FindTypeForGroup(keymap, keyi, i, &explicit_type);
1450 /* Always have as many levels as the type specifies. */
1451 if (type->num_levels < darray_size(groupi->levels)) {
1452 struct xkb_level *leveli;
1454 log_vrb(info->ctx, 1,
1455 "Type \"%s\" has %d levels, but %s has %d levels; "
1456 "Ignoring extra symbols\n",
1457 xkb_atom_text(keymap->ctx, type->name), type->num_levels,
1458 KeyInfoText(info, keyi),
1459 (int) darray_size(groupi->levels));
1461 darray_foreach_from(leveli, groupi->levels, type->num_levels)
1462 ClearLevelInfo(leveli);
1464 darray_resize0(groupi->levels, type->num_levels);
1466 key->groups[i].explicit_type = explicit_type;
1467 key->groups[i].type = type;
1471 darray_enumerate(i, groupi, keyi->groups)
1472 darray_steal(groupi->levels, &key->groups[i].levels, NULL);
1474 key->out_of_range_group_number = keyi->out_of_range_group_number;
1475 key->out_of_range_group_action = keyi->out_of_range_group_action;
1477 if (keyi->defined & KEY_FIELD_VMODMAP) {
1478 key->vmodmap = keyi->vmodmap;
1479 key->explicit |= EXPLICIT_VMODMAP;
1482 if (keyi->repeat != KEY_REPEAT_UNDEFINED) {
1483 key->repeats = (keyi->repeat == KEY_REPEAT_YES);
1484 key->explicit |= EXPLICIT_REPEAT;
1487 darray_foreach(groupi, keyi->groups) {
1488 if (groupi->defined & GROUP_FIELD_ACTS) {
1489 key->explicit |= EXPLICIT_INTERP;
1498 CopyModMapDefToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info,
1501 struct xkb_key *key;
1503 if (!entry->haveSymbol) {
1504 key = XkbKeyByName(keymap, entry->u.keyName, true);
1506 log_vrb(info->ctx, 5,
1507 "Key %s not found in keycodes; "
1508 "Modifier map entry for %s not updated\n",
1509 KeyNameText(info->ctx, entry->u.keyName),
1510 ModIndexText(info->ctx, &info->mods, entry->modifier));
1515 key = FindKeyForSymbol(keymap, entry->u.keySym);
1517 log_vrb(info->ctx, 5,
1518 "Key \"%s\" not found in symbol map; "
1519 "Modifier map entry for %s not updated\n",
1520 KeysymText(info->ctx, entry->u.keySym),
1521 ModIndexText(info->ctx, &info->mods, entry->modifier));
1526 key->modmap |= (1u << entry->modifier);
1531 CopySymbolsToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info)
1536 keymap->symbols_section_name = strdup_safe(info->name);
1537 XkbEscapeMapName(keymap->symbols_section_name);
1539 keymap->mods = info->mods;
1541 darray_steal(info->group_names,
1542 &keymap->group_names, &keymap->num_group_names);
1544 darray_foreach(keyi, info->keys)
1545 if (!CopySymbolsDefToKeymap(keymap, info, keyi))
1548 if (xkb_context_get_log_verbosity(keymap->ctx) > 3) {
1549 struct xkb_key *key;
1551 xkb_keys_foreach(key, keymap) {
1552 if (key->name == XKB_ATOM_NONE)
1555 if (key->num_groups < 1)
1557 "No symbols defined for %s\n",
1558 KeyNameText(info->ctx, key->name));
1562 darray_foreach(mm, info->modmaps)
1563 if (!CopyModMapDefToKeymap(keymap, info, mm))
1566 /* XXX: If we don't ignore errorCount, things break. */
1571 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1572 enum merge_mode merge)
1575 ActionsInfo *actions;
1577 actions = NewActionsInfo();
1581 InitSymbolsInfo(&info, keymap, actions, &keymap->mods);
1582 info.default_key.merge = merge;
1584 HandleSymbolsFile(&info, file, merge);
1586 if (info.errorCount != 0)
1589 if (!CopySymbolsToKeymap(keymap, &info))
1592 ClearSymbolsInfo(&info);
1593 FreeActionsInfo(actions);
1597 FreeActionsInfo(actions);
1598 ClearSymbolsInfo(&info);