1 /************************************************************
2 * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
4 * Permission to use, copy, modify, and distribute this
5 * software and its documentation for any purpose and without
6 * fee is hereby granted, provided that the above copyright
7 * notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting
9 * documentation, and that the name of Silicon Graphics not be
10 * used in advertising or publicity pertaining to distribution
11 * of the software without specific prior written permission.
12 * Silicon Graphics makes no representation about the suitability
13 * of this software for any purpose. It is provided "as is"
14 * without any express or implied warranty.
16 * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
23 * THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 ********************************************************/
28 * Copyright © 2012 Intel Corporation
29 * Copyright © 2012 Ran Benita <ran234@gmail.com>
31 * Permission is hereby granted, free of charge, to any person obtaining a
32 * copy of this software and associated documentation files (the "Software"),
33 * to deal in the Software without restriction, including without limitation
34 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
35 * and/or sell copies of the Software, and to permit persons to whom the
36 * Software is furnished to do so, subject to the following conditions:
38 * The above copyright notice and this permission notice (including the next
39 * paragraph) shall be included in all copies or substantial portions of the
42 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
45 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48 * DEALINGS IN THE SOFTWARE.
50 * Author: Daniel Stone <daniel@fooishbar.org>
51 * Ran Benita <ran234@gmail.com>
54 #include "xkbcomp-priv.h"
63 KEY_REPEAT_UNDEFINED = 0,
69 GROUP_FIELD_SYMS = (1 << 0),
70 GROUP_FIELD_ACTS = (1 << 1),
71 GROUP_FIELD_TYPE = (1 << 2),
75 KEY_FIELD_REPEAT = (1 << 0),
76 KEY_FIELD_DEFAULT_TYPE = (1 << 1),
77 KEY_FIELD_GROUPINFO = (1 << 2),
78 KEY_FIELD_VMODMAP = (1 << 3),
82 enum group_field defined;
83 darray(struct xkb_level) levels;
88 enum key_field defined;
89 enum merge_mode merge;
93 darray(GroupInfo) groups;
95 enum key_repeat repeat;
96 xkb_mod_mask_t vmodmap;
97 xkb_atom_t default_type;
99 enum xkb_range_exceed_type out_of_range_group_action;
100 xkb_layout_index_t out_of_range_group_number;
104 ClearLevelInfo(struct xkb_level *leveli)
106 if (leveli->num_syms > 1)
107 free(leveli->u.syms);
111 InitGroupInfo(GroupInfo *groupi)
113 memset(groupi, 0, sizeof(*groupi));
117 ClearGroupInfo(GroupInfo *groupi)
119 struct xkb_level *leveli;
120 darray_foreach(leveli, groupi->levels)
121 ClearLevelInfo(leveli);
122 darray_free(groupi->levels);
126 CopyGroupInfo(GroupInfo *to, const GroupInfo *from)
128 to->defined = from->defined;
129 to->type = from->type;
130 darray_init(to->levels);
131 darray_copy(to->levels, from->levels);
132 for (xkb_level_index_t j = 0; j < darray_size(to->levels); j++)
133 if (darray_item(from->levels, j).num_syms > 1)
134 darray_item(to->levels, j).u.syms =
135 memdup(darray_item(from->levels, j).u.syms,
136 darray_item(from->levels, j).num_syms,
137 sizeof(xkb_keysym_t));
141 InitKeyInfo(struct xkb_context *ctx, KeyInfo *keyi)
143 memset(keyi, 0, sizeof(*keyi));
144 keyi->merge = MERGE_OVERRIDE;
145 keyi->name = xkb_atom_intern_literal(ctx, "*");
146 keyi->out_of_range_group_action = RANGE_WRAP;
150 ClearKeyInfo(KeyInfo *keyi)
153 darray_foreach(groupi, keyi->groups)
154 ClearGroupInfo(groupi);
155 darray_free(keyi->groups);
158 /***====================================================================***/
161 enum merge_mode merge;
163 xkb_mod_index_t modifier;
171 char *name; /* e.g. pc+us+inet(evdev) */
173 enum merge_mode merge;
174 xkb_layout_index_t explicit_group;
175 darray(KeyInfo) keys;
177 ActionsInfo *actions;
178 darray(xkb_atom_t) group_names;
179 darray(ModMapEntry) modmaps;
181 struct xkb_keymap *keymap;
185 InitSymbolsInfo(SymbolsInfo *info, struct xkb_keymap *keymap,
186 ActionsInfo *actions)
188 memset(info, 0, sizeof(*info));
189 info->keymap = keymap;
190 info->merge = MERGE_OVERRIDE;
191 InitKeyInfo(keymap->ctx, &info->default_key);
192 info->actions = actions;
193 info->explicit_group = XKB_LAYOUT_INVALID;
197 ClearSymbolsInfo(SymbolsInfo *info)
201 darray_foreach(keyi, info->keys)
203 darray_free(info->keys);
204 darray_free(info->group_names);
205 darray_free(info->modmaps);
206 ClearKeyInfo(&info->default_key);
210 KeyInfoText(SymbolsInfo *info, KeyInfo *keyi)
212 return KeyNameText(info->keymap->ctx, keyi->name);
216 MergeGroups(SymbolsInfo *info, GroupInfo *into, GroupInfo *from, bool clobber,
217 bool report, xkb_layout_index_t group, xkb_atom_t key_name)
219 xkb_level_index_t i, levels_in_both;
220 struct xkb_context *ctx = info->keymap->ctx;
222 /* First find the type of the merged group. */
223 if (into->type != from->type) {
224 if (from->type == XKB_ATOM_NONE) {
226 else if (into->type == XKB_ATOM_NONE) {
227 into->type = from->type;
230 xkb_atom_t use = (clobber ? from->type : into->type);
231 xkb_atom_t ignore = (clobber ? into->type : from->type);
234 log_warn(info->keymap->ctx,
235 "Multiple definitions for group %d type of key %s; "
236 "Using %s, ignoring %s\n",
237 group + 1, KeyNameText(ctx, key_name),
238 xkb_atom_text(ctx, use), xkb_atom_text(ctx, ignore));
243 into->defined |= (from->defined & GROUP_FIELD_TYPE);
245 /* Now look at the levels. */
247 if (darray_empty(from->levels)) {
252 if (darray_empty(into->levels)) {
253 from->type = into->type;
259 /* Merge the actions and syms. */
260 levels_in_both = MIN(darray_size(into->levels), darray_size(from->levels));
261 for (i = 0; i < levels_in_both; i++) {
262 struct xkb_level *intoLevel = &darray_item(into->levels, i);
263 struct xkb_level *fromLevel = &darray_item(from->levels, i);
265 if (fromLevel->action.type == ACTION_TYPE_NONE) {
267 else if (intoLevel->action.type == ACTION_TYPE_NONE) {
268 intoLevel->action = fromLevel->action;
271 union xkb_action *use, *ignore;
272 use = (clobber ? &fromLevel->action : &intoLevel->action);
273 ignore = (clobber ? &intoLevel->action : &fromLevel->action);
277 "Multiple actions for level %d/group %u on key %s; "
278 "Using %s, ignoring %s\n",
279 i + 1, group + 1, KeyNameText(ctx, key_name),
280 ActionTypeText(use->type),
281 ActionTypeText(ignore->type));
283 intoLevel->action = *use;
286 if (fromLevel->num_syms == 0) {
288 else if (intoLevel->num_syms == 0) {
289 intoLevel->num_syms = fromLevel->num_syms;
290 if (fromLevel->num_syms > 1)
291 intoLevel->u.syms = fromLevel->u.syms;
293 intoLevel->u.sym = fromLevel->u.sym;
294 fromLevel->num_syms = 0;
299 "Multiple symbols for level %d/group %u on key %s; "
300 "Using %s, ignoring %s\n",
301 i + 1, group + 1, KeyNameText(ctx, key_name),
302 (clobber ? "from" : "to"),
303 (clobber ? "to" : "from"));
306 ClearLevelInfo(intoLevel);
307 intoLevel->num_syms = fromLevel->num_syms;
308 if (fromLevel->num_syms > 1)
309 intoLevel->u.syms = fromLevel->u.syms;
311 intoLevel->u.sym = fromLevel->u.sym;
312 fromLevel->num_syms = 0;
316 /* If @from has extra levels, get them as well. */
317 for (i = levels_in_both; i < darray_size(from->levels); i++) {
318 darray_append(into->levels, darray_item(from->levels, i));
319 darray_item(from->levels, i).num_syms = 0;
321 into->defined |= (from->defined & GROUP_FIELD_ACTS);
322 into->defined |= (from->defined & GROUP_FIELD_SYMS);
328 UseNewKeyField(enum key_field field, enum key_field old, enum key_field new,
329 bool clobber, bool report, enum key_field *collide)
332 return (new & field);
346 MergeKeys(SymbolsInfo *info, KeyInfo *into, KeyInfo *from, bool same_file)
348 xkb_layout_index_t i;
349 xkb_layout_index_t groups_in_both;
350 enum key_field collide = 0;
351 const int verbosity = xkb_context_get_log_verbosity(info->keymap->ctx);
352 const bool clobber = (from->merge != MERGE_AUGMENT);
353 const bool report = (same_file && verbosity > 0) || verbosity > 9;
355 if (from->merge == MERGE_REPLACE) {
358 InitKeyInfo(info->keymap->ctx, from);
362 groups_in_both = MIN(darray_size(into->groups), darray_size(from->groups));
363 for (i = 0; i < groups_in_both; i++)
365 &darray_item(into->groups, i),
366 &darray_item(from->groups, i),
367 clobber, report, i, into->name);
368 /* If @from has extra groups, just move them to @into. */
369 for (i = groups_in_both; i < darray_size(from->groups); i++) {
370 darray_append(into->groups, darray_item(from->groups, i));
371 InitGroupInfo(&darray_item(from->groups, i));
374 if (UseNewKeyField(KEY_FIELD_VMODMAP, into->defined, from->defined,
375 clobber, report, &collide)) {
376 into->vmodmap = from->vmodmap;
377 into->defined |= KEY_FIELD_VMODMAP;
379 if (UseNewKeyField(KEY_FIELD_REPEAT, into->defined, from->defined,
380 clobber, report, &collide)) {
381 into->repeat = from->repeat;
382 into->defined |= KEY_FIELD_REPEAT;
384 if (UseNewKeyField(KEY_FIELD_DEFAULT_TYPE, into->defined, from->defined,
385 clobber, report, &collide)) {
386 into->default_type = from->default_type;
387 into->defined |= KEY_FIELD_DEFAULT_TYPE;
389 if (UseNewKeyField(KEY_FIELD_GROUPINFO, into->defined, from->defined,
390 clobber, report, &collide)) {
391 into->out_of_range_group_action = from->out_of_range_group_action;
392 into->out_of_range_group_number = from->out_of_range_group_number;
393 into->defined |= KEY_FIELD_GROUPINFO;
397 log_warn(info->keymap->ctx,
398 "Symbol map for key %s redefined; "
399 "Using %s definition for conflicting fields\n",
400 KeyNameText(info->keymap->ctx, into->name),
401 (clobber ? "first" : "last"));
404 InitKeyInfo(info->keymap->ctx, from);
409 AddKeySymbols(SymbolsInfo *info, KeyInfo *keyi, bool same_file)
411 xkb_atom_t real_name;
415 * Don't keep aliases in the keys array; this guarantees that
416 * searching for keys to merge with by straight comparison (see the
417 * following loop) is enough, and we won't get multiple KeyInfo's
418 * for the same key because of aliases.
420 real_name = XkbResolveKeyAlias(info->keymap, keyi->name);
421 if (real_name != XKB_ATOM_NONE)
422 keyi->name = real_name;
424 darray_foreach(iter, info->keys)
425 if (iter->name == keyi->name)
426 return MergeKeys(info, iter, keyi, same_file);
428 darray_append(info->keys, *keyi);
429 InitKeyInfo(info->keymap->ctx, keyi);
434 AddModMapEntry(SymbolsInfo *info, ModMapEntry *new)
437 bool clobber = (new->merge != MERGE_AUGMENT);
439 darray_foreach(old, info->modmaps) {
440 xkb_mod_index_t use, ignore;
442 if ((new->haveSymbol != old->haveSymbol) ||
443 (new->haveSymbol && new->u.keySym != old->u.keySym) ||
444 (!new->haveSymbol && new->u.keyName != old->u.keyName))
447 if (new->modifier == old->modifier)
450 use = (clobber ? new->modifier : old->modifier);
451 ignore = (clobber ? old->modifier : new->modifier);
454 log_err(info->keymap->ctx,
455 "Symbol \"%s\" added to modifier map for multiple modifiers; "
456 "Using %s, ignoring %s\n",
457 KeysymText(info->keymap->ctx, new->u.keySym),
458 ModIndexText(info->keymap, use),
459 ModIndexText(info->keymap, ignore));
461 log_err(info->keymap->ctx,
462 "Key \"%s\" added to modifier map for multiple modifiers; "
463 "Using %s, ignoring %s\n",
464 KeyNameText(info->keymap->ctx, new->u.keyName),
465 ModIndexText(info->keymap, use),
466 ModIndexText(info->keymap, ignore));
472 darray_append(info->modmaps, *new);
476 /***====================================================================***/
479 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
480 enum merge_mode merge)
484 xkb_atom_t *group_name;
485 xkb_layout_index_t group_names_in_both;
487 if (from->errorCount > 0) {
488 into->errorCount += from->errorCount;
492 if (into->name == NULL) {
493 into->name = from->name;
497 group_names_in_both = MIN(darray_size(into->group_names),
498 darray_size(from->group_names));
499 for (xkb_layout_index_t i = 0; i < group_names_in_both; i++) {
500 if (!darray_item(from->group_names, i))
503 if (merge == MERGE_AUGMENT && darray_item(into->group_names, i))
506 darray_item(into->group_names, i) = darray_item(from->group_names, i);
508 /* If @from has more, get them as well. */
509 darray_foreach_from(group_name, from->group_names, group_names_in_both)
510 darray_append(into->group_names, *group_name);
512 if (darray_empty(into->keys)) {
513 into->keys = from->keys;
514 darray_init(from->keys);
517 darray_foreach(keyi, from->keys) {
518 keyi->merge = (merge == MERGE_DEFAULT ? keyi->merge : merge);
519 if (!AddKeySymbols(into, keyi, false))
524 if (darray_empty(into->modmaps)) {
525 into->modmaps = from->modmaps;
526 darray_init(from->modmaps);
529 darray_foreach(mm, from->modmaps) {
530 mm->merge = (merge == MERGE_DEFAULT ? mm->merge : merge);
531 if (!AddModMapEntry(into, mm))
538 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge);
541 HandleIncludeSymbols(SymbolsInfo *info, IncludeStmt *include)
543 SymbolsInfo included;
545 InitSymbolsInfo(&included, info->keymap, info->actions);
546 included.name = include->stmt;
547 include->stmt = NULL;
549 for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
550 SymbolsInfo next_incl;
553 file = ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_SYMBOLS);
555 info->errorCount += 10;
556 ClearSymbolsInfo(&included);
560 InitSymbolsInfo(&next_incl, info->keymap, info->actions);
561 if (stmt->modifier) {
562 next_incl.explicit_group = atoi(stmt->modifier) - 1;
563 if (next_incl.explicit_group >= XKB_MAX_GROUPS) {
564 log_err(info->keymap->ctx,
565 "Cannot set explicit group to %d - must be between 1..%d; "
566 "Ignoring group number\n",
567 next_incl.explicit_group + 1, XKB_MAX_GROUPS);
568 next_incl.explicit_group = info->explicit_group;
572 next_incl.explicit_group = info->explicit_group;
575 HandleSymbolsFile(&next_incl, file, MERGE_OVERRIDE);
577 MergeIncludedSymbols(&included, &next_incl, stmt->merge);
579 ClearSymbolsInfo(&next_incl);
583 MergeIncludedSymbols(info, &included, include->merge);
584 ClearSymbolsInfo(&included);
586 return (info->errorCount == 0);
593 GetGroupIndex(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
594 unsigned what, xkb_layout_index_t *ndx_rtrn)
596 const char *name = (what == SYMBOLS ? "symbols" : "actions");
598 if (arrayNdx == NULL) {
599 xkb_layout_index_t i;
601 enum group_field field = (what == SYMBOLS ?
602 GROUP_FIELD_SYMS : GROUP_FIELD_ACTS);
604 darray_enumerate(i, groupi, keyi->groups) {
605 if (!(groupi->defined & field)) {
611 if (i >= XKB_MAX_GROUPS) {
612 log_err(info->keymap->ctx,
613 "Too many groups of %s for key %s (max %u); "
614 "Ignoring %s defined for extra groups\n",
615 name, KeyInfoText(info, keyi), XKB_MAX_GROUPS, name);
619 darray_resize0(keyi->groups, darray_size(keyi->groups) + 1);
620 *ndx_rtrn = darray_size(keyi->groups) - 1;
624 if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, ndx_rtrn)) {
625 log_err(info->keymap->ctx,
626 "Illegal group index for %s of key %s\n"
627 "Definition with non-integer array index ignored\n",
628 name, KeyInfoText(info, keyi));
633 if (*ndx_rtrn >= darray_size(keyi->groups))
634 darray_resize0(keyi->groups, *ndx_rtrn + 1);
640 AddSymbolsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
643 xkb_layout_index_t ndx;
645 xkb_level_index_t nLevels;
647 if (!GetGroupIndex(info, keyi, arrayNdx, SYMBOLS, &ndx))
650 groupi = &darray_item(keyi->groups, ndx);
653 groupi->defined |= GROUP_FIELD_SYMS;
657 if (value->expr.op != EXPR_KEYSYM_LIST) {
658 log_err(info->keymap->ctx,
659 "Expected a list of symbols, found %s; "
660 "Ignoring symbols for group %u of %s\n",
661 expr_op_type_to_string(value->expr.op), ndx + 1,
662 KeyInfoText(info, keyi));
666 if (groupi->defined & GROUP_FIELD_SYMS) {
667 log_err(info->keymap->ctx,
668 "Symbols for key %s, group %u already defined; "
669 "Ignoring duplicate definition\n",
670 KeyInfoText(info, keyi), ndx + 1);
674 nLevels = darray_size(value->keysym_list.symsMapIndex);
675 if (darray_size(groupi->levels) < nLevels)
676 darray_resize0(groupi->levels, nLevels);
678 groupi->defined |= GROUP_FIELD_SYMS;
680 for (xkb_level_index_t i = 0; i < nLevels; i++) {
681 unsigned int sym_index;
682 struct xkb_level *leveli = &darray_item(groupi->levels, i);
684 sym_index = darray_item(value->keysym_list.symsMapIndex, i);
685 leveli->num_syms = darray_item(value->keysym_list.symsNumEntries, i);
686 if (leveli->num_syms > 1)
687 leveli->u.syms = calloc(leveli->num_syms, sizeof(*leveli->u.syms));
689 for (unsigned j = 0; j < leveli->num_syms; j++) {
690 xkb_keysym_t keysym = darray_item(value->keysym_list.syms,
693 if (leveli->num_syms == 1) {
694 if (keysym == XKB_KEY_NoSymbol)
695 leveli->num_syms = 0;
697 leveli->u.sym = keysym;
699 else if (leveli->num_syms > 1) {
700 leveli->u.syms[j] = keysym;
709 AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
712 xkb_layout_index_t ndx;
717 if (!GetGroupIndex(info, keyi, arrayNdx, ACTIONS, &ndx))
720 groupi = &darray_item(keyi->groups, ndx);
723 groupi->defined |= GROUP_FIELD_ACTS;
727 if (value->expr.op != EXPR_ACTION_LIST) {
728 log_wsgo(info->keymap->ctx,
729 "Bad expression type (%d) for action list value; "
730 "Ignoring actions for group %u of %s\n",
731 value->expr.op, ndx, KeyInfoText(info, keyi));
735 if (groupi->defined & GROUP_FIELD_ACTS) {
736 log_wsgo(info->keymap->ctx,
737 "Actions for key %s, group %u already defined\n",
738 KeyInfoText(info, keyi), ndx);
743 for (act = value->unary.child; act; act = (ExprDef *) act->common.next)
746 if (darray_size(groupi->levels) < nActs)
747 darray_resize0(groupi->levels, nActs);
749 groupi->defined |= GROUP_FIELD_ACTS;
751 act = value->unary.child;
752 for (unsigned i = 0; i < nActs; i++) {
753 union xkb_action *toAct = &darray_item(groupi->levels, i).action;
755 if (!HandleActionDef(act, info->keymap, toAct, info->actions))
756 log_err(info->keymap->ctx,
757 "Illegal action definition for %s; "
758 "Action for group %u/level %u ignored\n",
759 KeyInfoText(info, keyi), ndx + 1, i + 1);
761 act = (ExprDef *) act->common.next;
767 static const LookupEntry repeatEntries[] = {
768 { "true", KEY_REPEAT_YES },
769 { "yes", KEY_REPEAT_YES },
770 { "on", KEY_REPEAT_YES },
771 { "false", KEY_REPEAT_NO },
772 { "no", KEY_REPEAT_NO },
773 { "off", KEY_REPEAT_NO },
774 { "default", KEY_REPEAT_UNDEFINED },
779 SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
780 ExprDef *arrayNdx, ExprDef *value)
783 struct xkb_context *ctx = info->keymap->ctx;
785 if (istreq(field, "type")) {
786 xkb_layout_index_t ndx;
789 if (!ExprResolveString(ctx, value, &val))
791 "The type field of a key symbol map must be a string; "
792 "Ignoring illegal type definition\n");
794 if (arrayNdx == NULL) {
795 keyi->default_type = val;
796 keyi->defined |= KEY_FIELD_DEFAULT_TYPE;
798 else if (!ExprResolveGroup(ctx, arrayNdx, &ndx)) {
800 "Illegal group index for type of key %s; "
801 "Definition with non-integer array index ignored\n",
802 KeyInfoText(info, keyi));
807 if (ndx >= darray_size(keyi->groups))
808 darray_resize0(keyi->groups, ndx + 1);
809 darray_item(keyi->groups, ndx).type = val;
810 darray_item(keyi->groups, ndx).defined |= GROUP_FIELD_TYPE;
813 else if (istreq(field, "symbols"))
814 return AddSymbolsToKey(info, keyi, arrayNdx, value);
815 else if (istreq(field, "actions"))
816 return AddActionsToKey(info, keyi, arrayNdx, value);
817 else if (istreq(field, "vmods") ||
818 istreq(field, "virtualmods") ||
819 istreq(field, "virtualmodifiers")) {
822 ok = ExprResolveModMask(info->keymap, value, MOD_VIRT, &mask);
824 keyi->vmodmap = mask;
825 keyi->defined |= KEY_FIELD_VMODMAP;
828 log_err(info->keymap->ctx,
829 "Expected a virtual modifier mask, found %s; "
830 "Ignoring virtual modifiers definition for key %s\n",
831 expr_op_type_to_string(value->expr.op),
832 KeyInfoText(info, keyi));
835 else if (istreq(field, "locking") ||
836 istreq(field, "lock") ||
837 istreq(field, "locks")) {
838 log_err(info->keymap->ctx,
839 "Key behaviors not supported; "
840 "Ignoring locking specification for key %s\n",
841 KeyInfoText(info, keyi));
843 else if (istreq(field, "radiogroup") ||
844 istreq(field, "permanentradiogroup") ||
845 istreq(field, "allownone")) {
846 log_err(info->keymap->ctx,
847 "Radio groups not supported; "
848 "Ignoring radio group specification for key %s\n",
849 KeyInfoText(info, keyi));
851 else if (istreq_prefix("overlay", field) ||
852 istreq_prefix("permanentoverlay", field)) {
853 log_err(info->keymap->ctx,
854 "Overlays not supported; "
855 "Ignoring overlay specification for key %s\n",
856 KeyInfoText(info, keyi));
858 else if (istreq(field, "repeating") ||
859 istreq(field, "repeats") ||
860 istreq(field, "repeat")) {
863 ok = ExprResolveEnum(ctx, value, &val, repeatEntries);
865 log_err(info->keymap->ctx,
866 "Illegal repeat setting for %s; "
867 "Non-boolean repeat setting ignored\n",
868 KeyInfoText(info, keyi));
872 keyi->defined |= KEY_FIELD_REPEAT;
874 else if (istreq(field, "groupswrap") ||
875 istreq(field, "wrapgroups")) {
878 if (!ExprResolveBoolean(ctx, value, &set)) {
879 log_err(info->keymap->ctx,
880 "Illegal groupsWrap setting for %s; "
881 "Non-boolean value ignored\n",
882 KeyInfoText(info, keyi));
887 keyi->out_of_range_group_action = RANGE_WRAP;
889 keyi->out_of_range_group_action = RANGE_SATURATE;
891 keyi->defined |= KEY_FIELD_GROUPINFO;
893 else if (istreq(field, "groupsclamp") ||
894 istreq(field, "clampgroups")) {
897 if (!ExprResolveBoolean(ctx, value, &set)) {
898 log_err(info->keymap->ctx,
899 "Illegal groupsClamp setting for %s; "
900 "Non-boolean value ignored\n",
901 KeyInfoText(info, keyi));
906 keyi->out_of_range_group_action = RANGE_SATURATE;
908 keyi->out_of_range_group_action = RANGE_WRAP;
910 keyi->defined |= KEY_FIELD_GROUPINFO;
912 else if (istreq(field, "groupsredirect") ||
913 istreq(field, "redirectgroups")) {
914 xkb_layout_index_t grp;
916 if (!ExprResolveGroup(ctx, value, &grp)) {
917 log_err(info->keymap->ctx,
918 "Illegal group index for redirect of key %s; "
919 "Definition with non-integer group ignored\n",
920 KeyInfoText(info, keyi));
924 keyi->out_of_range_group_action = RANGE_REDIRECT;
925 keyi->out_of_range_group_number = grp - 1;
926 keyi->defined |= KEY_FIELD_GROUPINFO;
929 log_err(info->keymap->ctx,
930 "Unknown field %s in a symbol interpretation; "
931 "Definition ignored\n",
940 SetGroupName(SymbolsInfo *info, ExprDef *arrayNdx, ExprDef *value)
942 xkb_layout_index_t group, group_to_use;
946 log_vrb(info->keymap->ctx, 1,
947 "You must specify an index when specifying a group name; "
948 "Group name definition without array subscript ignored\n");
952 if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, &group)) {
953 log_err(info->keymap->ctx,
954 "Illegal index in group name definition; "
955 "Definition with non-integer array index ignored\n");
959 if (!ExprResolveString(info->keymap->ctx, value, &name)) {
960 log_err(info->keymap->ctx,
961 "Group name must be a string; "
962 "Illegal name for group %d ignored\n", group);
966 if (info->explicit_group == XKB_LAYOUT_INVALID) {
967 group_to_use = group - 1;
969 else if (group - 1 == 0) {
970 group_to_use = info->explicit_group;
973 log_warn(info->keymap->ctx,
974 "An explicit group was specified for the '%s' map, "
975 "but it provides a name for a group other than Group1 (%d); "
976 "Ignoring group name '%s'\n",
978 xkb_atom_text(info->keymap->ctx, name));
982 if (group_to_use >= darray_size(info->group_names))
983 darray_resize0(info->group_names, group_to_use + 1);
984 darray_item(info->group_names, group_to_use) = name;
990 HandleGlobalVar(SymbolsInfo *info, VarDef *stmt)
992 const char *elem, *field;
996 if (ExprResolveLhs(info->keymap->ctx, stmt->name, &elem, &field,
998 return 0; /* internal error, already reported */
999 if (elem && istreq(elem, "key")) {
1000 ret = SetSymbolsField(info, &info->default_key, field, arrayNdx,
1003 else if (!elem && (istreq(field, "name") ||
1004 istreq(field, "groupname"))) {
1005 ret = SetGroupName(info, arrayNdx, stmt->value);
1007 else if (!elem && (istreq(field, "groupswrap") ||
1008 istreq(field, "wrapgroups"))) {
1009 log_err(info->keymap->ctx,
1010 "Global \"groupswrap\" not supported; Ignored\n");
1013 else if (!elem && (istreq(field, "groupsclamp") ||
1014 istreq(field, "clampgroups"))) {
1015 log_err(info->keymap->ctx,
1016 "Global \"groupsclamp\" not supported; Ignored\n");
1019 else if (!elem && (istreq(field, "groupsredirect") ||
1020 istreq(field, "redirectgroups"))) {
1021 log_err(info->keymap->ctx,
1022 "Global \"groupsredirect\" not supported; Ignored\n");
1025 else if (!elem && istreq(field, "allownone")) {
1026 log_err(info->keymap->ctx,
1027 "Radio groups not supported; "
1028 "Ignoring \"allownone\" specification\n");
1032 ret = SetActionField(info->keymap, elem, field, arrayNdx, stmt->value,
1040 HandleSymbolsBody(SymbolsInfo *info, VarDef *def, KeyInfo *keyi)
1043 const char *elem, *field;
1046 for (; def; def = (VarDef *) def->common.next) {
1047 if (def->name && def->name->expr.op == EXPR_FIELD_REF) {
1048 log_err(info->keymap->ctx,
1049 "Cannot set a global default value from within a key statement; "
1050 "Move statements to the global file scope\n");
1055 if (!def->value || def->value->expr.op == EXPR_KEYSYM_LIST)
1062 ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
1067 ok = SetSymbolsField(info, keyi, field, arrayNdx, def->value);
1074 SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi)
1076 xkb_layout_index_t i;
1080 if (info->explicit_group == XKB_LAYOUT_INVALID)
1083 darray_enumerate_from(i, groupi, keyi->groups, 1) {
1084 if (groupi->defined) {
1086 ClearGroupInfo(groupi);
1087 InitGroupInfo(groupi);
1092 log_warn(info->keymap->ctx,
1093 "For the map %s an explicit group specified, "
1094 "but key %s has more than one group defined; "
1095 "All groups except first one will be ignored\n",
1096 info->name, KeyInfoText(info, keyi));
1098 darray_resize0(keyi->groups, info->explicit_group + 1);
1099 if (info->explicit_group > 0) {
1100 darray_item(keyi->groups, info->explicit_group) =
1101 darray_item(keyi->groups, 0);
1102 InitGroupInfo(&darray_item(keyi->groups, 0));
1109 HandleSymbolsDef(SymbolsInfo *info, SymbolsDef *stmt)
1113 keyi = info->default_key;
1114 darray_init(keyi.groups);
1115 darray_copy(keyi.groups, info->default_key.groups);
1116 for (xkb_layout_index_t i = 0; i < darray_size(keyi.groups); i++)
1117 CopyGroupInfo(&darray_item(keyi.groups, i),
1118 &darray_item(info->default_key.groups, i));
1119 keyi.merge = stmt->merge;
1120 keyi.name = stmt->keyName;
1122 if (!HandleSymbolsBody(info, stmt->symbols, &keyi)) {
1127 if (!SetExplicitGroup(info, &keyi)) {
1132 if (!AddKeySymbols(info, &keyi, true)) {
1141 HandleModMapDef(SymbolsInfo *info, ModMapDef *def)
1144 xkb_mod_index_t ndx;
1146 struct xkb_context *ctx = info->keymap->ctx;
1148 ndx = ModNameToIndex(info->keymap, def->modifier, MOD_REAL);
1149 if (ndx == XKB_MOD_INVALID) {
1150 log_err(info->keymap->ctx,
1151 "Illegal modifier map definition; "
1152 "Ignoring map for non-modifier \"%s\"\n",
1153 xkb_atom_text(ctx, def->modifier));
1159 tmp.merge = def->merge;
1161 for (ExprDef *key = def->keys; key; key = (ExprDef *) key->common.next) {
1164 if (key->expr.op == EXPR_VALUE &&
1165 key->expr.value_type == EXPR_TYPE_KEYNAME) {
1166 tmp.haveSymbol = false;
1167 tmp.u.keyName = key->key_name.key_name;
1169 else if (ExprResolveKeySym(ctx, key, &sym)) {
1170 tmp.haveSymbol = true;
1174 log_err(info->keymap->ctx,
1175 "Modmap entries may contain only key names or keysyms; "
1176 "Illegal definition for %s modifier ignored\n",
1177 ModIndexText(info->keymap, tmp.modifier));
1181 ok = AddModMapEntry(info, &tmp) && ok;
1187 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge)
1192 info->name = strdup_safe(file->name);
1194 for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
1195 switch (stmt->type) {
1197 ok = HandleIncludeSymbols(info, (IncludeStmt *) stmt);
1200 ok = HandleSymbolsDef(info, (SymbolsDef *) stmt);
1203 ok = HandleGlobalVar(info, (VarDef *) stmt);
1206 ok = HandleVModDef(info->keymap, (VModDef *) stmt, merge);
1209 ok = HandleModMapDef(info, (ModMapDef *) stmt);
1212 log_err(info->keymap->ctx,
1213 "Symbols files may not include other types; "
1214 "Ignoring %s\n", stmt_type_to_string(stmt->type));
1222 if (info->errorCount > 10) {
1223 log_err(info->keymap->ctx, "Abandoning symbols file \"%s\"\n",
1231 * Given a keysym @sym, return a key which generates it, or NULL.
1232 * This is used for example in a modifier map definition, such as:
1233 * modifier_map Lock { Caps_Lock };
1234 * where we want to add the Lock modifier to the modmap of the key
1235 * which matches the keysym Caps_Lock.
1236 * Since there can be many keys which generates the keysym, the key
1237 * is chosen first by lowest group in which the keysym appears, than
1238 * by lowest level and than by lowest key code.
1240 static struct xkb_key *
1241 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
1243 struct xkb_key *key, *ret = NULL;
1244 xkb_layout_index_t group, min_group = UINT32_MAX;
1245 xkb_level_index_t level, min_level = UINT16_MAX;
1247 xkb_foreach_key(key, keymap) {
1248 for (group = 0; group < key->num_groups; group++) {
1249 for (level = 0; level < XkbKeyGroupWidth(key, group); level++) {
1250 if (key->groups[group].levels[level].num_syms != 1 ||
1251 key->groups[group].levels[level].u.sym != sym)
1255 * If the keysym was found in a group or level > 0, we must
1256 * keep looking since we might find a key in which the keysym
1257 * is in a lower group or level.
1259 if (group < min_group ||
1260 (group == min_group && level < min_level)) {
1262 if (group == 0 && level == 0) {
1278 * Find an appropriate type for a group and return its name.
1281 * - ONE_LEVEL for width 0/1
1282 * - ALPHABETIC for 2 shift levels, with lower/upercase keysyms
1283 * - KEYPAD for keypad keys.
1284 * - TWO_LEVEL for other 2 shift level keys.
1285 * and the same for four level keys.
1287 * FIXME: Decide how to handle multiple-syms-per-level, and do it.
1290 FindAutomaticType(struct xkb_context *ctx, GroupInfo *groupi)
1292 xkb_keysym_t sym0, sym1, sym2, sym3;
1293 xkb_level_index_t width = darray_size(groupi->levels);
1295 #define GET_SYM(level) \
1296 (darray_item(groupi->levels, level).num_syms == 0 ? \
1297 XKB_KEY_NoSymbol : \
1298 darray_item(groupi->levels, level).num_syms == 1 ? \
1299 darray_item(groupi->levels, level).u.sym : \
1300 /* num_syms > 1 */ \
1301 darray_item(groupi->levels, level).u.syms[0])
1303 if (width == 1 || width <= 0)
1304 return xkb_atom_intern_literal(ctx, "ONE_LEVEL");
1310 if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1))
1311 return xkb_atom_intern_literal(ctx, "ALPHABETIC");
1313 if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1314 return xkb_atom_intern_literal(ctx, "KEYPAD");
1316 return xkb_atom_intern_literal(ctx, "TWO_LEVEL");
1320 if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1)) {
1322 sym3 = (width == 4 ? GET_SYM(3) : XKB_KEY_NoSymbol);
1324 if (xkb_keysym_is_lower(sym2) && xkb_keysym_is_upper(sym3))
1325 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_ALPHABETIC");
1327 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_SEMIALPHABETIC");
1330 if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1331 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_KEYPAD");
1333 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL");
1336 return XKB_ATOM_NONE;
1341 static const struct xkb_key_type *
1342 FindTypeForGroup(struct xkb_keymap *keymap, KeyInfo *keyi,
1343 xkb_layout_index_t group, bool *explicit_type)
1346 GroupInfo *groupi = &darray_item(keyi->groups, group);
1347 xkb_atom_t type_name = groupi->type;
1349 *explicit_type = true;
1351 if (type_name == XKB_ATOM_NONE) {
1352 if (keyi->default_type != XKB_ATOM_NONE) {
1353 type_name = keyi->default_type;
1356 type_name = FindAutomaticType(keymap->ctx, groupi);
1357 if (type_name != XKB_ATOM_NONE)
1358 *explicit_type = false;
1362 if (type_name == XKB_ATOM_NONE) {
1363 log_warn(keymap->ctx,
1364 "Couldn't find an automatic type for key '%s' group %d with %lu levels; "
1365 "Using the default type\n",
1366 KeyNameText(keymap->ctx, keyi->name), group + 1,
1367 (unsigned long) darray_size(groupi->levels));
1371 for (i = 0; i < keymap->num_types; i++)
1372 if (keymap->types[i].name == type_name)
1375 if (i >= keymap->num_types) {
1376 log_warn(keymap->ctx,
1377 "The type \"%s\" for key '%s' group %d was not previously defined; "
1378 "Using the default type\n",
1379 xkb_atom_text(keymap->ctx, type_name),
1380 KeyNameText(keymap->ctx, keyi->name), group + 1);
1384 return &keymap->types[i];
1388 * Index 0 is guaranteed to contain something, usually
1389 * ONE_LEVEL or at least some default one-level type.
1391 return &keymap->types[0];
1395 CopySymbolsDef(SymbolsInfo *info, KeyInfo *keyi)
1397 struct xkb_keymap *keymap = info->keymap;
1398 struct xkb_key *key;
1400 const GroupInfo *group0;
1401 xkb_layout_index_t i;
1404 * The name is guaranteed to be real and not an alias (see
1405 * AddKeySymbols), so 'false' is safe here.
1407 key = XkbKeyByName(keymap, keyi->name, false);
1409 log_vrb(info->keymap->ctx, 5,
1410 "Key %s not found in keycodes; Symbols ignored\n",
1411 KeyInfoText(info, keyi));
1415 /* Find the range of groups we need. */
1416 key->num_groups = 0;
1417 darray_enumerate(i, groupi, keyi->groups)
1418 if (groupi->defined)
1419 key->num_groups = i + 1;
1421 if (key->num_groups <= 0)
1422 return false; /* WSGO */
1424 darray_resize(keyi->groups, key->num_groups);
1427 * If there are empty groups between non-empty ones, fill them with data
1428 * from the first group.
1429 * We can make a wrong assumption here. But leaving gaps is worse.
1431 group0 = &darray_item(keyi->groups, 0);
1432 darray_foreach_from(groupi, keyi->groups, 1) {
1433 if (groupi->defined)
1436 CopyGroupInfo(groupi, group0);
1439 key->groups = calloc(key->num_groups, sizeof(*key->groups));
1441 /* Find and assign the groups' types in the keymap. */
1442 darray_enumerate(i, groupi, keyi->groups) {
1443 const struct xkb_key_type *type;
1446 type = FindTypeForGroup(keymap, keyi, i, &explicit_type);
1448 /* Always have as many levels as the type specifies. */
1449 if (type->num_levels < darray_size(groupi->levels)) {
1450 struct xkb_level *leveli;
1452 log_vrb(info->keymap->ctx, 1,
1453 "Type \"%s\" has %d levels, but %s has %d levels; "
1454 "Ignoring extra symbols\n",
1455 xkb_atom_text(keymap->ctx, type->name), type->num_levels,
1456 KeyInfoText(info, keyi),
1457 (int) darray_size(groupi->levels));
1459 darray_foreach_from(leveli, groupi->levels, type->num_levels)
1460 ClearLevelInfo(leveli);
1462 darray_resize0(groupi->levels, type->num_levels);
1464 key->groups[i].explicit_type = explicit_type;
1465 key->groups[i].type = type;
1469 darray_enumerate(i, groupi, keyi->groups) {
1470 key->groups[i].levels = darray_mem(groupi->levels, 0);
1471 darray_init(groupi->levels);
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 CopyModMapDef(SymbolsInfo *info, ModMapEntry *entry)
1500 struct xkb_key *key;
1501 struct xkb_keymap *keymap = info->keymap;
1503 if (!entry->haveSymbol) {
1504 key = XkbKeyByName(keymap, entry->u.keyName, true);
1506 log_vrb(info->keymap->ctx, 5,
1507 "Key %s not found in keycodes; "
1508 "Modifier map entry for %s not updated\n",
1509 KeyNameText(keymap->ctx, entry->u.keyName),
1510 ModIndexText(info->keymap, entry->modifier));
1515 key = FindKeyForSymbol(keymap, entry->u.keySym);
1517 log_vrb(info->keymap->ctx, 5,
1518 "Key \"%s\" not found in symbol map; "
1519 "Modifier map entry for %s not updated\n",
1520 KeysymText(info->keymap->ctx, entry->u.keySym),
1521 ModIndexText(info->keymap, 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->num_group_names = darray_size(info->group_names);
1540 keymap->group_names = darray_mem(info->group_names, 0);
1541 darray_init(info->group_names);
1543 darray_foreach(keyi, info->keys)
1544 if (!CopySymbolsDef(info, keyi))
1547 if (xkb_context_get_log_verbosity(keymap->ctx) > 3) {
1548 struct xkb_key *key;
1550 xkb_foreach_key(key, keymap) {
1551 if (key->name == XKB_ATOM_NONE)
1554 if (key->num_groups < 1)
1555 log_info(keymap->ctx,
1556 "No symbols defined for %s\n",
1557 KeyNameText(keymap->ctx, key->name));
1561 darray_foreach(mm, info->modmaps)
1562 if (!CopyModMapDef(info, mm))
1565 /* XXX: If we don't ignore errorCount, things break. */
1570 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1571 enum merge_mode merge)
1574 ActionsInfo *actions;
1576 actions = NewActionsInfo();
1580 InitSymbolsInfo(&info, keymap, actions);
1581 info.default_key.merge = merge;
1583 HandleSymbolsFile(&info, file, merge);
1585 if (info.errorCount != 0)
1588 if (!CopySymbolsToKeymap(keymap, &info))
1591 ClearSymbolsInfo(&info);
1592 FreeActionsInfo(actions);
1596 FreeActionsInfo(actions);
1597 ClearSymbolsInfo(&info);