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"
64 KEY_REPEAT_UNDEFINED = 0,
70 GROUP_FIELD_SYMS = (1 << 0),
71 GROUP_FIELD_ACTS = (1 << 1),
72 GROUP_FIELD_TYPE = (1 << 2),
76 KEY_FIELD_REPEAT = (1 << 0),
77 KEY_FIELD_TYPE_DFLT = (1 << 1),
78 KEY_FIELD_GROUPINFO = (1 << 2),
79 KEY_FIELD_VMODMAP = (1 << 3),
83 enum group_field defined;
84 darray(struct xkb_level) levels;
88 typedef struct _KeyInfo {
89 enum key_field defined;
91 enum merge_mode merge;
93 unsigned long name; /* the 4 chars of the key name, as long */
95 darray(GroupInfo) groups;
97 enum key_repeat repeat;
98 xkb_mod_mask_t vmodmap;
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)
131 to->defined = from->defined;
132 to->type = from->type;
133 darray_init(to->levels);
134 darray_copy(to->levels, from->levels);
135 for (j = 0; j < darray_size(to->levels); j++)
136 if (darray_item(from->levels, j).num_syms > 1)
137 darray_item(to->levels, j).u.syms =
138 memdup(darray_item(from->levels, j).u.syms,
139 darray_item(from->levels, j).num_syms,
140 sizeof(xkb_keysym_t));
144 InitKeyInfo(KeyInfo *keyi, unsigned file_id)
146 static const char dflt_key_name[XKB_KEY_NAME_LENGTH] = "*";
148 memset(keyi, 0, sizeof(*keyi));
149 keyi->file_id = file_id;
150 keyi->merge = MERGE_OVERRIDE;
151 keyi->name = KeyNameToLong(dflt_key_name);
152 keyi->out_of_range_group_action = RANGE_WRAP;
156 ClearKeyInfo(KeyInfo *keyi)
159 darray_foreach(groupi, keyi->groups)
160 ClearGroupInfo(groupi);
161 darray_free(keyi->groups);
164 /***====================================================================***/
166 typedef struct _ModMapEntry {
167 enum merge_mode merge;
171 unsigned long keyName;
176 typedef struct _SymbolsInfo {
177 char *name; /* e.g. pc+us+inet(evdev) */
180 enum merge_mode merge;
181 xkb_layout_index_t explicit_group;
182 darray(KeyInfo) keys;
185 ActionsInfo *actions;
186 darray_xkb_atom_t group_names;
187 darray(ModMapEntry) modMaps;
189 struct xkb_keymap *keymap;
193 InitSymbolsInfo(SymbolsInfo *info, struct xkb_keymap *keymap,
194 unsigned file_id, ActionsInfo *actions)
196 memset(info, 0, sizeof(*info));
197 info->keymap = keymap;
198 info->file_id = file_id;
199 info->merge = MERGE_OVERRIDE;
200 InitKeyInfo(&info->dflt, file_id);
201 InitVModInfo(&info->vmods, keymap);
202 info->actions = actions;
203 info->explicit_group = XKB_LAYOUT_INVALID;
207 ClearSymbolsInfo(SymbolsInfo * info)
211 darray_foreach(keyi, info->keys)
213 darray_free(info->keys);
214 darray_free(info->group_names);
215 darray_free(info->modMaps);
216 ClearKeyInfo(&info->dflt);
220 MergeGroups(SymbolsInfo *info, GroupInfo *into, GroupInfo *from, bool clobber,
221 bool report, xkb_layout_index_t group, unsigned long key_name)
223 xkb_level_index_t i, levels_in_both;
225 /* First find the type of the merged group. */
226 if (into->type != from->type) {
227 if (from->type == XKB_ATOM_NONE) {
229 else if (into->type == XKB_ATOM_NONE) {
230 into->type = from->type;
233 xkb_atom_t use = (clobber ? from->type : into->type);
234 xkb_atom_t ignore = (clobber ? into->type : from->type);
237 log_warn(info->keymap->ctx,
238 "Multiple definitions for group %d type of key %s; "
239 "Using %s, ignoring %s\n",
240 group + 1, LongKeyNameText(key_name),
241 xkb_atom_text(info->keymap->ctx, use),
242 xkb_atom_text(info->keymap->ctx, ignore));
247 into->defined |= (from->defined & GROUP_FIELD_TYPE);
249 /* Now look at the levels. */
251 if (darray_empty(from->levels)) {
256 if (darray_empty(into->levels)) {
257 from->type = into->type;
263 /* Merge the actions and syms. */
264 levels_in_both = MIN(darray_size(into->levels), darray_size(from->levels));
265 for (i = 0; i < levels_in_both; i++) {
266 struct xkb_level *intoLevel = &darray_item(into->levels, i);
267 struct xkb_level *fromLevel = &darray_item(from->levels, i);
269 if (fromLevel->action.type == ACTION_TYPE_NONE) {
271 else if (intoLevel->action.type == ACTION_TYPE_NONE) {
272 intoLevel->action = fromLevel->action;
275 union xkb_action *use, *ignore;
276 use = (clobber ? &fromLevel->action : &intoLevel->action);
277 ignore = (clobber ? &intoLevel->action : &fromLevel->action);
280 log_warn(info->keymap->ctx,
281 "Multiple actions for level %d/group %u on key %s; "
282 "Using %s, ignoring %s\n",
283 i + 1, group + 1, LongKeyNameText(key_name),
284 ActionTypeText(use->type),
285 ActionTypeText(ignore->type));
287 intoLevel->action = *use;
290 if (fromLevel->num_syms == 0) {
292 else if (intoLevel->num_syms == 0) {
293 intoLevel->num_syms = fromLevel->num_syms;
294 if (fromLevel->num_syms > 1)
295 intoLevel->u.syms = fromLevel->u.syms;
297 intoLevel->u.sym = fromLevel->u.sym;
298 fromLevel->num_syms = 0;
302 log_warn(info->keymap->ctx,
303 "Multiple symbols for level %d/group %u on key %s; "
304 "Using %s, ignoring %s\n",
305 i + 1, group + 1, LongKeyNameText(key_name),
306 (clobber ? "from" : "to"),
307 (clobber ? "to" : "from"));
310 ClearLevelInfo(intoLevel);
311 intoLevel->num_syms = fromLevel->num_syms;
312 if (fromLevel->num_syms > 1)
313 intoLevel->u.syms = fromLevel->u.syms;
315 intoLevel->u.sym = fromLevel->u.sym;
316 fromLevel->num_syms = 0;
320 /* If @from has extra levels, get them as well. */
321 for (i = levels_in_both; i < darray_size(from->levels); i++) {
322 darray_append(into->levels, darray_item(from->levels, i));
323 darray_item(from->levels, i).num_syms = 0;
325 into->defined |= (from->defined & GROUP_FIELD_ACTS);
326 into->defined |= (from->defined & GROUP_FIELD_SYMS);
332 UseNewKeyField(enum key_field field, enum key_field old, enum key_field new,
333 bool clobber, bool report, enum key_field *collide)
336 return (new & field);
350 MergeKeys(SymbolsInfo *info, KeyInfo *into, KeyInfo *from)
352 xkb_layout_index_t i;
353 xkb_layout_index_t groups_in_both;
354 enum key_field collide = 0;
355 bool clobber, report;
356 int verbosity = xkb_context_get_log_verbosity(info->keymap->ctx);
358 if (from->merge == MERGE_REPLACE) {
361 InitKeyInfo(from, info->file_id);
365 clobber = (from->merge != MERGE_AUGMENT);
366 report = (verbosity > 9 ||
367 (into->file_id == from->file_id && verbosity > 0));
369 groups_in_both = MIN(darray_size(into->groups),
370 darray_size(from->groups));
371 for (i = 0; i < groups_in_both; i++)
373 &darray_item(into->groups, i),
374 &darray_item(from->groups, i),
375 clobber, report, i, into->name);
376 /* If @from has extra groups, just move them to @into. */
377 for (i = groups_in_both; i < darray_size(from->groups); i++) {
378 darray_append(into->groups, darray_item(from->groups, i));
379 InitGroupInfo(&darray_item(from->groups, i));
382 if (UseNewKeyField(KEY_FIELD_VMODMAP, into->defined, from->defined,
383 clobber, report, &collide)) {
384 into->vmodmap = from->vmodmap;
385 into->defined |= KEY_FIELD_VMODMAP;
387 if (UseNewKeyField(KEY_FIELD_REPEAT, into->defined, from->defined,
388 clobber, report, &collide)) {
389 into->repeat = from->repeat;
390 into->defined |= KEY_FIELD_REPEAT;
392 if (UseNewKeyField(KEY_FIELD_TYPE_DFLT, into->defined, from->defined,
393 clobber, report, &collide)) {
394 into->dfltType = from->dfltType;
395 into->defined |= KEY_FIELD_TYPE_DFLT;
397 if (UseNewKeyField(KEY_FIELD_GROUPINFO, into->defined, from->defined,
398 clobber, report, &collide)) {
399 into->out_of_range_group_action = from->out_of_range_group_action;
400 into->out_of_range_group_number = from->out_of_range_group_number;
401 into->defined |= KEY_FIELD_GROUPINFO;
405 log_warn(info->keymap->ctx,
406 "Symbol map for key %s redefined; "
407 "Using %s definition for conflicting fields\n",
408 LongKeyNameText(into->name),
409 (clobber ? "first" : "last"));
412 InitKeyInfo(from, info->file_id);
417 AddKeySymbols(SymbolsInfo *info, KeyInfo *keyi)
419 unsigned long real_name;
423 * Don't keep aliases in the keys array; this guarantees that
424 * searching for keys to merge with by straight comparison (see the
425 * following loop) is enough, and we won't get multiple KeyInfo's
426 * for the same key because of aliases.
428 if (FindKeyNameForAlias(info->keymap, keyi->name, &real_name))
429 keyi->name = real_name;
431 darray_foreach(iter, info->keys)
432 if (iter->name == keyi->name)
433 return MergeKeys(info, iter, keyi);
435 darray_append(info->keys, *keyi);
436 InitKeyInfo(keyi, info->file_id);
441 AddModMapEntry(SymbolsInfo * info, ModMapEntry * new)
446 clobber = (new->merge != MERGE_AUGMENT);
447 darray_foreach(mm, info->modMaps) {
448 if (new->haveSymbol && mm->haveSymbol
449 && (new->u.keySym == mm->u.keySym)) {
450 unsigned use, ignore;
451 if (mm->modifier != new->modifier) {
454 ignore = mm->modifier;
458 ignore = new->modifier;
460 log_err(info->keymap->ctx,
461 "%s added to symbol map for multiple modifiers; "
462 "Using %s, ignoring %s.\n",
463 KeysymText(new->u.keySym), ModIndexText(use),
464 ModIndexText(ignore));
469 if ((!new->haveSymbol) && (!mm->haveSymbol) &&
470 (new->u.keyName == mm->u.keyName)) {
471 unsigned use, ignore;
472 if (mm->modifier != new->modifier) {
475 ignore = mm->modifier;
479 ignore = new->modifier;
481 log_err(info->keymap->ctx,
482 "Key %s added to map for multiple modifiers; "
483 "Using %s, ignoring %s.\n",
484 LongKeyNameText(new->u.keyName), ModIndexText(use),
485 ModIndexText(ignore));
492 darray_append(info->modMaps, *new);
496 /***====================================================================***/
499 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
500 enum merge_mode merge)
505 xkb_atom_t *group_name;
506 xkb_layout_index_t group_names_in_both;
508 if (from->errorCount > 0) {
509 into->errorCount += from->errorCount;
513 if (into->name == NULL) {
514 into->name = from->name;
518 group_names_in_both = MIN(darray_size(into->group_names),
519 darray_size(from->group_names));
520 for (i = 0; i < group_names_in_both; i++) {
521 if (!darray_item(from->group_names, i))
524 if (merge == MERGE_AUGMENT && darray_item(into->group_names, i))
527 darray_item(into->group_names, i) = darray_item(from->group_names, i);
529 /* If @from has more, get them as well. */
530 darray_foreach_from(group_name, from->group_names, group_names_in_both)
531 darray_append(into->group_names, *group_name);
533 darray_foreach(keyi, from->keys) {
534 merge = (merge == MERGE_DEFAULT ? keyi->merge : merge);
535 if (!AddKeySymbols(into, keyi))
539 darray_foreach(mm, from->modMaps) {
540 mm->merge = (merge == MERGE_DEFAULT ? mm->merge : merge);
541 if (!AddModMapEntry(into, mm))
547 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge);
550 HandleIncludeSymbols(SymbolsInfo *info, IncludeStmt *stmt)
552 enum merge_mode merge = MERGE_DEFAULT;
554 SymbolsInfo included, next_incl;
556 InitSymbolsInfo(&included, info->keymap, info->file_id, info->actions);
559 included.name = stmt->stmt;
563 for (; stmt; stmt = stmt->next_incl) {
564 if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_SYMBOLS,
566 info->errorCount += 10;
567 ClearSymbolsInfo(&included);
571 InitSymbolsInfo(&next_incl, info->keymap, rtrn->id, info->actions);
572 next_incl.merge = next_incl.dflt.merge = MERGE_OVERRIDE;
573 if (stmt->modifier) {
574 next_incl.explicit_group = atoi(stmt->modifier) - 1;
575 if (next_incl.explicit_group >= XKB_NUM_GROUPS) {
576 log_err(info->keymap->ctx,
577 "Cannot set explicit group to %d - must be between 1..%d; "
578 "Ignoring group number\n",
579 next_incl.explicit_group + 1, XKB_NUM_GROUPS);
580 next_incl.explicit_group = info->explicit_group;
584 next_incl.explicit_group = info->explicit_group;
587 HandleSymbolsFile(&next_incl, rtrn, MERGE_OVERRIDE);
589 MergeIncludedSymbols(&included, &next_incl, merge);
591 ClearSymbolsInfo(&next_incl);
595 MergeIncludedSymbols(info, &included, merge);
596 ClearSymbolsInfo(&included);
598 return (info->errorCount == 0);
605 GetGroupIndex(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
606 unsigned what, xkb_layout_index_t *ndx_rtrn)
608 const char *name = (what == SYMBOLS ? "symbols" : "actions");
610 if (arrayNdx == NULL) {
611 xkb_layout_index_t i;
613 enum group_field field = (what == SYMBOLS ?
614 GROUP_FIELD_SYMS : GROUP_FIELD_ACTS);
616 darray_enumerate(i, groupi, keyi->groups) {
617 if (!(groupi->defined & field)) {
623 if (i >= XKB_NUM_GROUPS) {
624 log_err(info->keymap->ctx,
625 "Too many groups of %s for key %s (max %u); "
626 "Ignoring %s defined for extra groups\n",
627 name, LongKeyNameText(keyi->name), XKB_NUM_GROUPS + 1, name);
631 darray_resize0(keyi->groups, darray_size(keyi->groups) + 1);
632 *ndx_rtrn = darray_size(keyi->groups) - 1;
636 if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, ndx_rtrn)) {
637 log_err(info->keymap->ctx,
638 "Illegal group index for %s of key %s\n"
639 "Definition with non-integer array index ignored\n",
640 name, LongKeyNameText(keyi->name));
645 if (*ndx_rtrn >= darray_size(keyi->groups))
646 darray_resize0(keyi->groups, *ndx_rtrn + 1);
652 LookupKeysym(const char *str, xkb_keysym_t *sym_rtrn)
656 if (!str || istreq(str, "any") || istreq(str, "nosymbol")) {
657 *sym_rtrn = XKB_KEY_NoSymbol;
661 if (istreq(str, "none") || istreq(str, "voidsymbol")) {
662 *sym_rtrn = XKB_KEY_VoidSymbol;
666 sym = xkb_keysym_from_name(str);
667 if (sym != XKB_KEY_NoSymbol) {
676 AddSymbolsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
679 xkb_layout_index_t ndx;
681 xkb_level_index_t nLevels;
685 if (!GetGroupIndex(info, keyi, arrayNdx, SYMBOLS, &ndx))
688 groupi = &darray_item(keyi->groups, ndx);
691 groupi->defined |= GROUP_FIELD_SYMS;
695 if (value->op != EXPR_KEYSYM_LIST) {
696 log_err(info->keymap->ctx,
697 "Expected a list of symbols, found %s; "
698 "Ignoring symbols for group %u of %s\n",
699 expr_op_type_to_string(value->op), ndx + 1,
700 LongKeyNameText(keyi->name));
704 if (groupi->defined & GROUP_FIELD_SYMS) {
705 log_err(info->keymap->ctx,
706 "Symbols for key %s, group %u already defined; "
707 "Ignoring duplicate definition\n",
708 LongKeyNameText(keyi->name), ndx + 1);
712 nLevels = darray_size(value->value.list.symsMapIndex);
713 if (darray_size(groupi->levels) < nLevels)
714 darray_resize0(groupi->levels, nLevels);
716 groupi->defined |= GROUP_FIELD_SYMS;
718 for (i = 0; i < nLevels; i++) {
719 unsigned int sym_index;
720 struct xkb_level *leveli = &darray_item(groupi->levels, i);
722 sym_index = darray_item(value->value.list.symsMapIndex, i);
723 leveli->num_syms = darray_item(value->value.list.symsNumEntries, i);
724 if (leveli->num_syms > 1)
725 leveli->u.syms = calloc(leveli->num_syms, sizeof(*leveli->u.syms));
727 for (j = 0; j < leveli->num_syms; j++) {
728 char *sym_name = darray_item(value->value.list.syms,
732 if (!LookupKeysym(sym_name, &keysym)) {
733 const char *group_name = "unnamed";
735 if (ndx < darray_size(info->group_names) &&
736 darray_item(info->group_names, ndx))
737 group_name = xkb_atom_text(info->keymap->ctx,
738 darray_item(info->group_names,
741 log_warn(info->keymap->ctx,
742 "Could not resolve keysym %s for key %s, group %u (%s), level %u\n",
743 sym_name, LongKeyNameText(keyi->name), ndx + 1,
746 ClearLevelInfo(leveli);
747 leveli->num_syms = 0;
751 if (leveli->num_syms == 1) {
752 if (keysym == XKB_KEY_NoSymbol)
753 leveli->num_syms = 0;
755 leveli->u.sym = keysym;
757 else if (leveli->num_syms > 1) {
758 leveli->u.syms[j] = keysym;
767 AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
771 xkb_layout_index_t ndx;
775 union xkb_action *toAct;
777 if (!GetGroupIndex(info, keyi, arrayNdx, ACTIONS, &ndx))
780 groupi = &darray_item(keyi->groups, ndx);
783 groupi->defined |= GROUP_FIELD_ACTS;
787 if (value->op != EXPR_ACTION_LIST) {
788 log_wsgo(info->keymap->ctx,
789 "Bad expression type (%d) for action list value; "
790 "Ignoring actions for group %u of %s\n",
791 value->op, ndx, LongKeyNameText(keyi->name));
795 if (groupi->defined & GROUP_FIELD_ACTS) {
796 log_wsgo(info->keymap->ctx,
797 "Actions for key %s, group %u already defined\n",
798 LongKeyNameText(keyi->name), ndx);
803 for (act = value->value.child; act; act = (ExprDef *) act->common.next)
806 if (darray_size(groupi->levels) < nActs)
807 darray_resize0(groupi->levels, nActs);
809 groupi->defined |= GROUP_FIELD_ACTS;
811 act = value->value.child;
812 for (i = 0; i < nActs; i++) {
813 toAct = &darray_item(groupi->levels, i).action;
815 if (!HandleActionDef(act, info->keymap, toAct, info->actions))
816 log_err(info->keymap->ctx,
817 "Illegal action definition for %s; "
818 "Action for group %u/level %u ignored\n",
819 LongKeyNameText(keyi->name), ndx + 1, i + 1);
821 act = (ExprDef *) act->common.next;
827 static const LookupEntry repeatEntries[] = {
828 { "true", KEY_REPEAT_YES },
829 { "yes", KEY_REPEAT_YES },
830 { "on", KEY_REPEAT_YES },
831 { "false", KEY_REPEAT_NO },
832 { "no", KEY_REPEAT_NO },
833 { "off", KEY_REPEAT_NO },
834 { "default", KEY_REPEAT_UNDEFINED },
839 SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
840 ExprDef *arrayNdx, ExprDef *value)
843 struct xkb_context *ctx = info->keymap->ctx;
845 if (istreq(field, "type")) {
846 xkb_layout_index_t ndx;
849 if (!ExprResolveString(ctx, value, &val))
851 "The type field of a key symbol map must be a string; "
852 "Ignoring illegal type definition\n");
854 if (arrayNdx == NULL) {
855 keyi->dfltType = val;
856 keyi->defined |= KEY_FIELD_TYPE_DFLT;
858 else if (!ExprResolveGroup(ctx, arrayNdx, &ndx)) {
860 "Illegal group index for type of key %s; "
861 "Definition with non-integer array index ignored\n",
862 LongKeyNameText(keyi->name));
867 if (ndx >= darray_size(keyi->groups))
868 darray_resize0(keyi->groups, ndx + 1);
869 darray_item(keyi->groups, ndx).type = val;
870 darray_item(keyi->groups, ndx).defined |= GROUP_FIELD_TYPE;
873 else if (istreq(field, "symbols"))
874 return AddSymbolsToKey(info, keyi, arrayNdx, value);
875 else if (istreq(field, "actions"))
876 return AddActionsToKey(info, keyi, arrayNdx, value);
877 else if (istreq(field, "vmods") ||
878 istreq(field, "virtualmods") ||
879 istreq(field, "virtualmodifiers")) {
882 ok = ExprResolveVModMask(info->keymap, value, &mask);
884 keyi->vmodmap = (mask >> XKB_NUM_CORE_MODS) & 0xffff;
885 keyi->defined |= KEY_FIELD_VMODMAP;
888 log_err(info->keymap->ctx,
889 "Expected a virtual modifier mask, found %s; "
890 "Ignoring virtual modifiers definition for key %s\n",
891 expr_op_type_to_string(value->op),
892 LongKeyNameText(keyi->name));
895 else if (istreq(field, "locking") ||
896 istreq(field, "lock") ||
897 istreq(field, "locks")) {
898 log_err(info->keymap->ctx,
899 "Key behaviors not supported; "
900 "Ignoring locking specification for key %s\n",
901 LongKeyNameText(keyi->name));
903 else if (istreq(field, "radiogroup") ||
904 istreq(field, "permanentradiogroup") ||
905 istreq(field, "allownone")) {
906 log_err(info->keymap->ctx,
907 "Radio groups not supported; "
908 "Ignoring radio group specification for key %s\n",
909 LongKeyNameText(keyi->name));
911 else if (istreq_prefix("overlay", field) ||
912 istreq_prefix("permanentoverlay", field)) {
913 log_err(info->keymap->ctx,
914 "Overlays not supported; "
915 "Ignoring overlay specification for key %s\n",
916 LongKeyNameText(keyi->name));
918 else if (istreq(field, "repeating") ||
919 istreq(field, "repeats") ||
920 istreq(field, "repeat")) {
923 ok = ExprResolveEnum(ctx, value, &val, repeatEntries);
925 log_err(info->keymap->ctx,
926 "Illegal repeat setting for %s; "
927 "Non-boolean repeat setting ignored\n",
928 LongKeyNameText(keyi->name));
932 keyi->defined |= KEY_FIELD_REPEAT;
934 else if (istreq(field, "groupswrap") ||
935 istreq(field, "wrapgroups")) {
938 if (!ExprResolveBoolean(ctx, value, &set)) {
939 log_err(info->keymap->ctx,
940 "Illegal groupsWrap setting for %s; "
941 "Non-boolean value ignored\n",
942 LongKeyNameText(keyi->name));
947 keyi->out_of_range_group_action = RANGE_WRAP;
949 keyi->out_of_range_group_action = RANGE_SATURATE;
951 keyi->defined |= KEY_FIELD_GROUPINFO;
953 else if (istreq(field, "groupsclamp") ||
954 istreq(field, "clampgroups")) {
957 if (!ExprResolveBoolean(ctx, value, &set)) {
958 log_err(info->keymap->ctx,
959 "Illegal groupsClamp setting for %s; "
960 "Non-boolean value ignored\n",
961 LongKeyNameText(keyi->name));
966 keyi->out_of_range_group_action = RANGE_SATURATE;
968 keyi->out_of_range_group_action = RANGE_WRAP;
970 keyi->defined |= KEY_FIELD_GROUPINFO;
972 else if (istreq(field, "groupsredirect") ||
973 istreq(field, "redirectgroups")) {
974 xkb_layout_index_t grp;
976 if (!ExprResolveGroup(ctx, value, &grp)) {
977 log_err(info->keymap->ctx,
978 "Illegal group index for redirect of key %s; "
979 "Definition with non-integer group ignored\n",
980 LongKeyNameText(keyi->name));
984 keyi->out_of_range_group_action = RANGE_REDIRECT;
985 keyi->out_of_range_group_number = grp - 1;
986 keyi->defined |= KEY_FIELD_GROUPINFO;
989 log_err(info->keymap->ctx,
990 "Unknown field %s in a symbol interpretation; "
991 "Definition ignored\n",
1000 SetGroupName(SymbolsInfo *info, ExprDef *arrayNdx, ExprDef *value)
1002 xkb_layout_index_t grp, grp_to_use;
1006 log_vrb(info->keymap->ctx, 1,
1007 "You must specify an index when specifying a group name; "
1008 "Group name definition without array subscript ignored\n");
1012 if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, &grp)) {
1013 log_err(info->keymap->ctx,
1014 "Illegal index in group name definition; "
1015 "Definition with non-integer array index ignored\n");
1019 if (!ExprResolveString(info->keymap->ctx, value, &name)) {
1020 log_err(info->keymap->ctx,
1021 "Group name must be a string; "
1022 "Illegal name for group %d ignored\n", grp);
1026 grp_to_use = XKB_LAYOUT_INVALID;
1027 if (info->explicit_group == XKB_LAYOUT_INVALID) {
1028 grp_to_use = grp - 1;
1030 else if (grp - 1 == 0) {
1031 grp_to_use = info->explicit_group;
1034 log_warn(info->keymap->ctx,
1035 "An explicit group was specified for the '%s' map, "
1036 "but it provides a name for a group other than Group1 (%d); "
1037 "Ignoring group name '%s'\n",
1039 xkb_atom_text(info->keymap->ctx, name));
1043 if (grp_to_use >= darray_size(info->group_names))
1044 darray_resize0(info->group_names, grp_to_use + 1);
1045 darray_item(info->group_names, grp_to_use) = name;
1050 HandleGlobalVar(SymbolsInfo *info, VarDef *stmt)
1052 const char *elem, *field;
1056 if (ExprResolveLhs(info->keymap->ctx, stmt->name, &elem, &field,
1058 return 0; /* internal error, already reported */
1059 if (elem && istreq(elem, "key")) {
1060 ret = SetSymbolsField(info, &info->dflt, field, arrayNdx,
1063 else if (!elem && (istreq(field, "name") ||
1064 istreq(field, "groupname"))) {
1065 ret = SetGroupName(info, arrayNdx, stmt->value);
1067 else if (!elem && (istreq(field, "groupswrap") ||
1068 istreq(field, "wrapgroups"))) {
1069 log_err(info->keymap->ctx,
1070 "Global \"groupswrap\" not supported; Ignored\n");
1073 else if (!elem && (istreq(field, "groupsclamp") ||
1074 istreq(field, "clampgroups"))) {
1075 log_err(info->keymap->ctx,
1076 "Global \"groupsclamp\" not supported; Ignored\n");
1079 else if (!elem && (istreq(field, "groupsredirect") ||
1080 istreq(field, "redirectgroups"))) {
1081 log_err(info->keymap->ctx,
1082 "Global \"groupsredirect\" not supported; Ignored\n");
1085 else if (!elem && istreq(field, "allownone")) {
1086 log_err(info->keymap->ctx,
1087 "Radio groups not supported; "
1088 "Ignoring \"allownone\" specification\n");
1092 ret = SetActionField(info->keymap, elem, field, arrayNdx, stmt->value,
1100 HandleSymbolsBody(SymbolsInfo *info, VarDef *def, KeyInfo *keyi)
1103 const char *elem, *field;
1106 for (; def; def = (VarDef *) def->common.next) {
1107 if (def->name && def->name->op == EXPR_FIELD_REF) {
1108 log_err(info->keymap->ctx,
1109 "Cannot set a global default value from within a key statement; "
1110 "Move statements to the global file scope\n");
1115 if (!def->value || def->value->op == EXPR_KEYSYM_LIST)
1122 ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
1127 ok = SetSymbolsField(info, keyi, field, arrayNdx, def->value);
1134 SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi)
1136 xkb_layout_index_t i;
1140 if (info->explicit_group == XKB_LAYOUT_INVALID)
1143 darray_enumerate_from(i, groupi, keyi->groups, 1) {
1144 if (groupi->defined) {
1146 ClearGroupInfo(groupi);
1147 InitGroupInfo(groupi);
1152 log_warn(info->keymap->ctx,
1153 "For the map %s an explicit group specified, "
1154 "but key %s has more than one group defined; "
1155 "All groups except first one will be ignored\n",
1156 info->name, LongKeyNameText(keyi->name));
1158 darray_resize0(keyi->groups, info->explicit_group + 1);
1159 if (info->explicit_group > 0) {
1160 darray_item(keyi->groups, info->explicit_group) =
1161 darray_item(keyi->groups, 0);
1162 InitGroupInfo(&darray_item(keyi->groups, 0));
1169 HandleSymbolsDef(SymbolsInfo *info, SymbolsDef *stmt)
1172 xkb_layout_index_t i;
1175 darray_init(keyi.groups);
1176 darray_copy(keyi.groups, info->dflt.groups);
1177 for (i = 0; i < darray_size(keyi.groups); i++)
1178 CopyGroupInfo(&darray_item(keyi.groups, i),
1179 &darray_item(info->dflt.groups, i));
1180 keyi.merge = stmt->merge;
1181 keyi.name = KeyNameToLong(stmt->keyName);
1183 if (!HandleSymbolsBody(info, (VarDef *) stmt->symbols, &keyi)) {
1188 if (!SetExplicitGroup(info, &keyi)) {
1193 if (!AddKeySymbols(info, &keyi)) {
1202 HandleModMapDef(SymbolsInfo *info, ModMapDef *def)
1206 xkb_mod_index_t ndx;
1208 struct xkb_context *ctx = info->keymap->ctx;
1210 if (!LookupModIndex(ctx, NULL, def->modifier, EXPR_TYPE_INT, &ndx)) {
1211 log_err(info->keymap->ctx,
1212 "Illegal modifier map definition; "
1213 "Ignoring map for non-modifier \"%s\"\n",
1214 xkb_atom_text(ctx, def->modifier));
1221 for (key = def->keys; key != NULL; key = (ExprDef *) key->common.next) {
1224 if (key->op == EXPR_VALUE && key->value_type == EXPR_TYPE_KEYNAME) {
1225 tmp.haveSymbol = false;
1226 tmp.u.keyName = KeyNameToLong(key->value.keyName);
1228 else if (ExprResolveKeySym(ctx, key, &sym)) {
1229 tmp.haveSymbol = true;
1233 log_err(info->keymap->ctx,
1234 "Modmap entries may contain only key names or keysyms; "
1235 "Illegal definition for %s modifier ignored\n",
1236 ModIndexText(tmp.modifier));
1240 ok = AddModMapEntry(info, &tmp) && ok;
1246 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge)
1252 info->name = strdup_safe(file->name);
1255 for (stmt = file->defs; stmt; stmt = stmt->next) {
1256 switch (stmt->type) {
1258 ok = HandleIncludeSymbols(info, (IncludeStmt *) stmt);
1261 ok = HandleSymbolsDef(info, (SymbolsDef *) stmt);
1264 ok = HandleGlobalVar(info, (VarDef *) stmt);
1267 ok = HandleVModDef((VModDef *) stmt, info->keymap, merge,
1271 ok = HandleModMapDef(info, (ModMapDef *) stmt);
1274 log_err(info->keymap->ctx,
1275 "Interpretation files may not include other types; "
1276 "Ignoring %s\n", stmt_type_to_string(stmt->type));
1284 if (info->errorCount > 10) {
1285 log_err(info->keymap->ctx, "Abandoning symbols file \"%s\"\n",
1293 * Given a keysym @sym, return a key which generates it, or NULL.
1294 * This is used for example in a modifier map definition, such as:
1295 * modifier_map Lock { Caps_Lock };
1296 * where we want to add the Lock modifier to the modmap of the key
1297 * which matches the keysym Caps_Lock.
1298 * Since there can be many keys which generates the keysym, the key
1299 * is chosen first by lowest group in which the keysym appears, than
1300 * by lowest level and than by lowest key code.
1302 static struct xkb_key *
1303 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
1305 struct xkb_key *key, *ret = NULL;
1306 xkb_layout_index_t group, min_group = UINT32_MAX;
1307 xkb_level_index_t level, min_level = UINT16_MAX;
1309 xkb_foreach_key(key, keymap) {
1310 for (group = 0; group < key->num_groups; group++) {
1311 for (level = 0; level < XkbKeyGroupWidth(keymap, key, group);
1313 if (XkbKeyNumSyms(key, group, level) != 1 ||
1314 (XkbKeySymEntry(key, group, level))[0] != sym)
1318 * If the keysym was found in a group or level > 0, we must
1319 * keep looking since we might find a key in which the keysym
1320 * is in a lower group or level.
1322 if (group < min_group ||
1323 (group == min_group && level < min_level)) {
1325 if (group == 0 && level == 0) {
1341 FindNamedType(struct xkb_keymap *keymap, xkb_atom_t name, unsigned *type_rtrn)
1345 for (i = 0; i < keymap->num_types; i++) {
1346 if (keymap->types[i].name == name) {
1356 * Find an appropriate type for a group and return its name.
1359 * - ONE_LEVEL for width 0/1
1360 * - ALPHABETIC for 2 shift levels, with lower/upercase keysyms
1361 * - KEYPAD for keypad keys.
1362 * - TWO_LEVEL for other 2 shift level keys.
1363 * and the same for four level keys.
1365 * FIXME: Decide how to handle multiple-syms-per-level, and do it.
1368 FindAutomaticType(struct xkb_context *ctx, GroupInfo *groupi,
1369 xkb_atom_t *typeNameRtrn, bool *autoType)
1371 xkb_level_index_t width = darray_size(groupi->levels);
1375 #define GET_SYM(level) \
1376 (darray_item(groupi->levels, level).num_syms == 0 ? \
1377 XKB_KEY_NoSymbol : \
1378 darray_item(groupi->levels, level).num_syms == 1 ? \
1379 darray_item(groupi->levels, level).u.sym : \
1380 /* num_syms > 1 */ \
1381 darray_item(groupi->levels, level).u.syms[0])
1383 if (width == 1 || width == 0) {
1384 *typeNameRtrn = xkb_atom_intern(ctx, "ONE_LEVEL");
1387 else if (width == 2) {
1388 xkb_keysym_t sym0 = GET_SYM(0);
1389 xkb_keysym_t sym1 = GET_SYM(1);
1391 if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1)) {
1392 *typeNameRtrn = xkb_atom_intern(ctx, "ALPHABETIC");
1394 else if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1)) {
1395 *typeNameRtrn = xkb_atom_intern(ctx, "KEYPAD");
1399 *typeNameRtrn = xkb_atom_intern(ctx, "TWO_LEVEL");
1403 else if (width <= 4) {
1404 xkb_keysym_t sym0 = GET_SYM(0);
1405 xkb_keysym_t sym1 = GET_SYM(1);
1406 xkb_keysym_t sym2 = GET_SYM(2);
1407 xkb_keysym_t sym3 = (width == 4 ? GET_SYM(3) : XKB_KEY_NoSymbol);
1409 if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1))
1410 if (xkb_keysym_is_lower(sym2) && xkb_keysym_is_upper(sym3))
1412 xkb_atom_intern(ctx, "FOUR_LEVEL_ALPHABETIC");
1414 *typeNameRtrn = xkb_atom_intern(ctx,
1415 "FOUR_LEVEL_SEMIALPHABETIC");
1417 else if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1418 *typeNameRtrn = xkb_atom_intern(ctx, "FOUR_LEVEL_KEYPAD");
1420 *typeNameRtrn = xkb_atom_intern(ctx, "FOUR_LEVEL");
1421 /* XXX: why not set autoType here? */
1427 CopySymbolsDef(SymbolsInfo *info, KeyInfo *keyi)
1429 struct xkb_keymap *keymap = info->keymap;
1430 struct xkb_key *key;
1432 const GroupInfo *group0;
1433 xkb_layout_index_t i;
1436 * The name is guaranteed to be real and not an alias (see
1437 * AddKeySymbols), so 'false' is safe here.
1439 key = FindNamedKey(keymap, keyi->name, false);
1441 log_vrb(info->keymap->ctx, 5,
1442 "Key %s not found in keycodes; Symbols ignored\n",
1443 LongKeyNameText(keyi->name));
1447 /* Find the range of groups we need. */
1448 key->num_groups = 0;
1449 darray_enumerate(i, groupi, keyi->groups)
1450 if (groupi->defined)
1451 key->num_groups = i + 1;
1453 if (key->num_groups <= 0)
1454 return false; /* WSGO */
1456 darray_resize(keyi->groups, key->num_groups);
1459 * If there are empty groups between non-empty ones, fill them with data
1460 * from the first group.
1461 * We can make a wrong assumption here. But leaving gaps is worse.
1463 group0 = &darray_item(keyi->groups, 0);
1464 darray_foreach_from(groupi, keyi->groups, 1) {
1465 if (groupi->defined)
1468 CopyGroupInfo(groupi, group0);
1471 key->groups = calloc(key->num_groups, sizeof(*key->groups));
1473 /* * Find and assign the groups' types in the keymap. */
1474 darray_enumerate(i, groupi, keyi->groups) {
1475 struct xkb_key_type *type;
1476 struct xkb_group *group = &key->groups[i];
1477 bool autoType = false;
1479 /* Find the type of the group, if it is missing. */
1480 if (groupi->type == XKB_ATOM_NONE) {
1481 if (keyi->dfltType != XKB_ATOM_NONE)
1482 groupi->type = keyi->dfltType;
1483 else if (FindAutomaticType(keymap->ctx, groupi,
1484 &groupi->type, &autoType)) { }
1486 log_vrb(info->keymap->ctx, 5,
1487 "No automatic type for %d levels; "
1488 "Using %s for the %s key\n",
1489 (int) darray_size(groupi->levels),
1490 xkb_atom_text(keymap->ctx, groupi->type),
1491 LongKeyNameText(keyi->name));
1494 /* Find the type in the keymap, if it was defined in xkb_types. */
1495 if (FindNamedType(keymap, groupi->type, &group->type_index)) {
1496 if (!autoType || darray_size(groupi->levels) > 2)
1497 key->groups[i].explicit_type = true;
1500 log_vrb(info->keymap->ctx, 3,
1501 "Type \"%s\" is not defined; "
1502 "Using default type for the %s key\n",
1503 xkb_atom_text(keymap->ctx, groupi->type),
1504 LongKeyNameText(keyi->name));
1506 * Index 0 is guaranteed to contain something, usually
1507 * ONE_LEVEL or at least some default one-level type.
1509 group->type_index = 0;
1512 /* Always have as many levels as the type specifies. */
1513 type = &keymap->types[group->type_index];
1514 if (type->num_levels < darray_size(groupi->levels)) {
1515 struct xkb_level *leveli;
1517 log_vrb(info->keymap->ctx, 1,
1518 "Type \"%s\" has %d levels, but %s has %d levels; "
1519 "Ignoring extra symbols\n",
1520 xkb_atom_text(keymap->ctx, type->name),
1522 LongKeyNameText(keyi->name),
1523 (int) darray_size(groupi->levels));
1525 darray_foreach_from(leveli, groupi->levels, type->num_levels)
1526 ClearLevelInfo(leveli);
1528 darray_resize0(groupi->levels, type->num_levels);
1532 darray_enumerate(i, groupi, keyi->groups) {
1533 key->groups[i].levels = darray_mem(groupi->levels, 0);
1534 darray_init(groupi->levels);
1537 key->out_of_range_group_number = keyi->out_of_range_group_number;
1538 key->out_of_range_group_action = keyi->out_of_range_group_action;
1540 if (keyi->defined & KEY_FIELD_VMODMAP) {
1541 key->vmodmap = keyi->vmodmap;
1542 key->explicit |= EXPLICIT_VMODMAP;
1545 if (keyi->repeat != KEY_REPEAT_UNDEFINED) {
1546 key->repeats = (keyi->repeat == KEY_REPEAT_YES);
1547 key->explicit |= EXPLICIT_REPEAT;
1550 darray_foreach(groupi, keyi->groups) {
1551 if (groupi->defined & GROUP_FIELD_ACTS) {
1552 key->explicit |= EXPLICIT_INTERP;
1561 CopyModMapDef(SymbolsInfo *info, ModMapEntry *entry)
1563 struct xkb_key *key;
1564 struct xkb_keymap *keymap = info->keymap;
1566 if (!entry->haveSymbol) {
1567 key = FindNamedKey(keymap, entry->u.keyName, true);
1569 log_vrb(info->keymap->ctx, 5,
1570 "Key %s not found in keycodes; "
1571 "Modifier map entry for %s not updated\n",
1572 LongKeyNameText(entry->u.keyName),
1573 ModIndexText(entry->modifier));
1578 key = FindKeyForSymbol(keymap, entry->u.keySym);
1580 log_vrb(info->keymap->ctx, 5,
1581 "Key \"%s\" not found in symbol map; "
1582 "Modifier map entry for %s not updated\n",
1583 KeysymText(entry->u.keySym),
1584 ModIndexText(entry->modifier));
1589 key->modmap |= (1 << entry->modifier);
1594 CopySymbolsToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info)
1598 struct xkb_key *key;
1600 keymap->symbols_section_name = strdup_safe(info->name);
1602 keymap->group_names = info->group_names;
1603 darray_init(info->group_names);
1605 darray_foreach(keyi, info->keys)
1606 if (!CopySymbolsDef(info, keyi))
1609 if (xkb_context_get_log_verbosity(keymap->ctx) > 3) {
1610 xkb_foreach_key(key, keymap) {
1611 if (key->name[0] == '\0')
1614 if (key->num_groups < 1)
1615 log_info(keymap->ctx,
1616 "No symbols defined for %s\n",
1617 KeyNameText(key->name));
1621 darray_foreach(mm, info->modMaps)
1622 if (!CopyModMapDef(info, mm))
1625 /* XXX: If we don't ignore errorCount, things break. */
1630 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1631 enum merge_mode merge)
1634 ActionsInfo *actions;
1636 actions = NewActionsInfo();
1640 InitSymbolsInfo(&info, keymap, file->id, actions);
1641 info.dflt.merge = merge;
1643 HandleSymbolsFile(&info, file, merge);
1645 if (darray_empty(info.keys))
1648 if (info.errorCount != 0)
1651 if (!CopySymbolsToKeymap(keymap, &info))
1654 ClearSymbolsInfo(&info);
1655 FreeActionsInfo(actions);
1659 FreeActionsInfo(actions);
1660 ClearSymbolsInfo(&info);