1 /************************************************************
2 * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
4 * Permission to use, copy, modify, and distribute this
5 * software and its documentation for any purpose and without
6 * fee is hereby granted, provided that the above copyright
7 * notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting
9 * documentation, and that the name of Silicon Graphics not be
10 * used in advertising or publicity pertaining to distribution
11 * of the software without specific prior written permission.
12 * Silicon Graphics makes no representation about the suitability
13 * of this software for any purpose. It is provided "as is"
14 * without any express or implied warranty.
16 * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
23 * THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 ********************************************************/
28 * Copyright © 2012 Intel Corporation
29 * Copyright © 2012 Ran Benita <ran234@gmail.com>
31 * Permission is hereby granted, free of charge, to any person obtaining a
32 * copy of this software and associated documentation files (the "Software"),
33 * to deal in the Software without restriction, including without limitation
34 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
35 * and/or sell copies of the Software, and to permit persons to whom the
36 * Software is furnished to do so, subject to the following conditions:
38 * The above copyright notice and this permission notice (including the next
39 * paragraph) shall be included in all copies or substantial portions of the
42 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
45 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48 * DEALINGS IN THE SOFTWARE.
50 * Author: Daniel Stone <daniel@fooishbar.org>
51 * Ran Benita <ran234@gmail.com>
54 #include "xkbcomp-priv.h"
63 KEY_REPEAT_UNDEFINED = 0,
69 GROUP_FIELD_SYMS = (1 << 0),
70 GROUP_FIELD_ACTS = (1 << 1),
71 GROUP_FIELD_TYPE = (1 << 2),
75 KEY_FIELD_REPEAT = (1 << 0),
76 KEY_FIELD_DEFAULT_TYPE = (1 << 1),
77 KEY_FIELD_GROUPINFO = (1 << 2),
78 KEY_FIELD_VMODMAP = (1 << 3),
82 enum group_field defined;
83 darray(struct xkb_level) levels;
88 enum key_field defined;
89 enum merge_mode merge;
93 darray(GroupInfo) groups;
95 enum key_repeat repeat;
96 xkb_mod_mask_t vmodmap;
97 xkb_atom_t default_type;
99 enum xkb_range_exceed_type out_of_range_group_action;
100 xkb_layout_index_t out_of_range_group_number;
104 ClearLevelInfo(struct xkb_level *leveli)
106 if (leveli->num_syms > 1)
107 free(leveli->u.syms);
111 InitGroupInfo(GroupInfo *groupi)
113 memset(groupi, 0, sizeof(*groupi));
117 ClearGroupInfo(GroupInfo *groupi)
119 struct xkb_level *leveli;
120 darray_foreach(leveli, groupi->levels)
121 ClearLevelInfo(leveli);
122 darray_free(groupi->levels);
126 CopyGroupInfo(GroupInfo *to, const GroupInfo *from)
129 to->defined = from->defined;
130 to->type = from->type;
131 darray_init(to->levels);
132 darray_copy(to->levels, from->levels);
133 for (j = 0; j < darray_size(to->levels); j++)
134 if (darray_item(from->levels, j).num_syms > 1)
135 darray_item(to->levels, j).u.syms =
136 memdup(darray_item(from->levels, j).u.syms,
137 darray_item(from->levels, j).num_syms,
138 sizeof(xkb_keysym_t));
142 InitKeyInfo(struct xkb_context *ctx, KeyInfo *keyi)
144 memset(keyi, 0, sizeof(*keyi));
145 keyi->merge = MERGE_OVERRIDE;
146 keyi->name = xkb_atom_intern_literal(ctx, "*");
147 keyi->out_of_range_group_action = RANGE_WRAP;
151 ClearKeyInfo(KeyInfo *keyi)
154 darray_foreach(groupi, keyi->groups)
155 ClearGroupInfo(groupi);
156 darray_free(keyi->groups);
159 /***====================================================================***/
162 enum merge_mode merge;
164 xkb_mod_index_t modifier;
172 char *name; /* e.g. pc+us+inet(evdev) */
174 enum merge_mode merge;
175 xkb_layout_index_t explicit_group;
176 darray(KeyInfo) keys;
178 ActionsInfo *actions;
179 darray(xkb_atom_t) group_names;
180 darray(ModMapEntry) modMaps;
182 struct xkb_keymap *keymap;
186 InitSymbolsInfo(SymbolsInfo *info, struct xkb_keymap *keymap,
187 ActionsInfo *actions)
189 memset(info, 0, sizeof(*info));
190 info->keymap = keymap;
191 info->merge = MERGE_OVERRIDE;
192 InitKeyInfo(keymap->ctx, &info->default_key);
193 info->actions = actions;
194 info->explicit_group = XKB_LAYOUT_INVALID;
198 ClearSymbolsInfo(SymbolsInfo *info)
202 darray_foreach(keyi, info->keys)
204 darray_free(info->keys);
205 darray_free(info->group_names);
206 darray_free(info->modMaps);
207 ClearKeyInfo(&info->default_key);
211 KeyInfoText(SymbolsInfo *info, KeyInfo *keyi)
213 return KeyNameText(info->keymap->ctx, keyi->name);
217 MergeGroups(SymbolsInfo *info, GroupInfo *into, GroupInfo *from, bool clobber,
218 bool report, xkb_layout_index_t group, xkb_atom_t key_name)
220 xkb_level_index_t i, levels_in_both;
221 struct xkb_context *ctx = info->keymap->ctx;
223 /* First find the type of the merged group. */
224 if (into->type != from->type) {
225 if (from->type == XKB_ATOM_NONE) {
227 else if (into->type == XKB_ATOM_NONE) {
228 into->type = from->type;
231 xkb_atom_t use = (clobber ? from->type : into->type);
232 xkb_atom_t ignore = (clobber ? into->type : from->type);
235 log_warn(info->keymap->ctx,
236 "Multiple definitions for group %d type of key %s; "
237 "Using %s, ignoring %s\n",
238 group + 1, KeyNameText(ctx, key_name),
239 xkb_atom_text(ctx, use), xkb_atom_text(ctx, ignore));
244 into->defined |= (from->defined & GROUP_FIELD_TYPE);
246 /* Now look at the levels. */
248 if (darray_empty(from->levels)) {
253 if (darray_empty(into->levels)) {
254 from->type = into->type;
260 /* Merge the actions and syms. */
261 levels_in_both = MIN(darray_size(into->levels), darray_size(from->levels));
262 for (i = 0; i < levels_in_both; i++) {
263 struct xkb_level *intoLevel = &darray_item(into->levels, i);
264 struct xkb_level *fromLevel = &darray_item(from->levels, i);
266 if (fromLevel->action.type == ACTION_TYPE_NONE) {
268 else if (intoLevel->action.type == ACTION_TYPE_NONE) {
269 intoLevel->action = fromLevel->action;
272 union xkb_action *use, *ignore;
273 use = (clobber ? &fromLevel->action : &intoLevel->action);
274 ignore = (clobber ? &intoLevel->action : &fromLevel->action);
278 "Multiple actions for level %d/group %u on key %s; "
279 "Using %s, ignoring %s\n",
280 i + 1, group + 1, KeyNameText(ctx, key_name),
281 ActionTypeText(use->type),
282 ActionTypeText(ignore->type));
284 intoLevel->action = *use;
287 if (fromLevel->num_syms == 0) {
289 else if (intoLevel->num_syms == 0) {
290 intoLevel->num_syms = fromLevel->num_syms;
291 if (fromLevel->num_syms > 1)
292 intoLevel->u.syms = fromLevel->u.syms;
294 intoLevel->u.sym = fromLevel->u.sym;
295 fromLevel->num_syms = 0;
300 "Multiple symbols for level %d/group %u on key %s; "
301 "Using %s, ignoring %s\n",
302 i + 1, group + 1, KeyNameText(ctx, key_name),
303 (clobber ? "from" : "to"),
304 (clobber ? "to" : "from"));
307 ClearLevelInfo(intoLevel);
308 intoLevel->num_syms = fromLevel->num_syms;
309 if (fromLevel->num_syms > 1)
310 intoLevel->u.syms = fromLevel->u.syms;
312 intoLevel->u.sym = fromLevel->u.sym;
313 fromLevel->num_syms = 0;
317 /* If @from has extra levels, get them as well. */
318 for (i = levels_in_both; i < darray_size(from->levels); i++) {
319 darray_append(into->levels, darray_item(from->levels, i));
320 darray_item(from->levels, i).num_syms = 0;
322 into->defined |= (from->defined & GROUP_FIELD_ACTS);
323 into->defined |= (from->defined & GROUP_FIELD_SYMS);
329 UseNewKeyField(enum key_field field, enum key_field old, enum key_field new,
330 bool clobber, bool report, enum key_field *collide)
333 return (new & field);
347 MergeKeys(SymbolsInfo *info, KeyInfo *into, KeyInfo *from, bool same_file)
349 xkb_layout_index_t i;
350 xkb_layout_index_t groups_in_both;
351 enum key_field collide = 0;
352 const int verbosity = xkb_context_get_log_verbosity(info->keymap->ctx);
353 const bool clobber = (from->merge != MERGE_AUGMENT);
354 const bool report = (same_file && verbosity > 0) || verbosity > 9;
356 if (from->merge == MERGE_REPLACE) {
359 InitKeyInfo(info->keymap->ctx, from);
363 groups_in_both = MIN(darray_size(into->groups), darray_size(from->groups));
364 for (i = 0; i < groups_in_both; i++)
366 &darray_item(into->groups, i),
367 &darray_item(from->groups, i),
368 clobber, report, i, into->name);
369 /* If @from has extra groups, just move them to @into. */
370 for (i = groups_in_both; i < darray_size(from->groups); i++) {
371 darray_append(into->groups, darray_item(from->groups, i));
372 InitGroupInfo(&darray_item(from->groups, i));
375 if (UseNewKeyField(KEY_FIELD_VMODMAP, into->defined, from->defined,
376 clobber, report, &collide)) {
377 into->vmodmap = from->vmodmap;
378 into->defined |= KEY_FIELD_VMODMAP;
380 if (UseNewKeyField(KEY_FIELD_REPEAT, into->defined, from->defined,
381 clobber, report, &collide)) {
382 into->repeat = from->repeat;
383 into->defined |= KEY_FIELD_REPEAT;
385 if (UseNewKeyField(KEY_FIELD_DEFAULT_TYPE, into->defined, from->defined,
386 clobber, report, &collide)) {
387 into->default_type = from->default_type;
388 into->defined |= KEY_FIELD_DEFAULT_TYPE;
390 if (UseNewKeyField(KEY_FIELD_GROUPINFO, into->defined, from->defined,
391 clobber, report, &collide)) {
392 into->out_of_range_group_action = from->out_of_range_group_action;
393 into->out_of_range_group_number = from->out_of_range_group_number;
394 into->defined |= KEY_FIELD_GROUPINFO;
398 log_warn(info->keymap->ctx,
399 "Symbol map for key %s redefined; "
400 "Using %s definition for conflicting fields\n",
401 KeyNameText(info->keymap->ctx, into->name),
402 (clobber ? "first" : "last"));
405 InitKeyInfo(info->keymap->ctx, from);
410 AddKeySymbols(SymbolsInfo *info, KeyInfo *keyi, bool same_file)
412 xkb_atom_t real_name;
416 * Don't keep aliases in the keys array; this guarantees that
417 * searching for keys to merge with by straight comparison (see the
418 * following loop) is enough, and we won't get multiple KeyInfo's
419 * for the same key because of aliases.
421 real_name = XkbResolveKeyAlias(info->keymap, keyi->name);
422 if (real_name != XKB_ATOM_NONE)
423 keyi->name = real_name;
425 darray_foreach(iter, info->keys)
426 if (iter->name == keyi->name)
427 return MergeKeys(info, iter, keyi, same_file);
429 darray_append(info->keys, *keyi);
430 InitKeyInfo(info->keymap->ctx, keyi);
435 AddModMapEntry(SymbolsInfo *info, ModMapEntry *new)
438 bool clobber = (new->merge != MERGE_AUGMENT);
440 darray_foreach(old, info->modMaps) {
441 xkb_mod_index_t use, ignore;
443 if ((new->haveSymbol != old->haveSymbol) ||
444 (new->haveSymbol && new->u.keySym != old->u.keySym) ||
445 (!new->haveSymbol && new->u.keyName != old->u.keyName))
448 if (new->modifier == old->modifier)
451 use = (clobber ? new->modifier : old->modifier);
452 ignore = (clobber ? old->modifier : new->modifier);
455 log_err(info->keymap->ctx,
456 "Symbol \"%s\" added to modifier map for multiple modifiers; "
457 "Using %s, ignoring %s\n",
458 KeysymText(info->keymap->ctx, new->u.keySym),
459 ModIndexText(info->keymap, use),
460 ModIndexText(info->keymap, ignore));
462 log_err(info->keymap->ctx,
463 "Key \"%s\" added to modifier map for multiple modifiers; "
464 "Using %s, ignoring %s\n",
465 KeyNameText(info->keymap->ctx, new->u.keyName),
466 ModIndexText(info->keymap, use),
467 ModIndexText(info->keymap, ignore));
473 darray_append(info->modMaps, *new);
477 /***====================================================================***/
480 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
481 enum merge_mode merge)
486 xkb_atom_t *group_name;
487 xkb_layout_index_t group_names_in_both;
489 if (from->errorCount > 0) {
490 into->errorCount += from->errorCount;
494 if (into->name == NULL) {
495 into->name = from->name;
499 group_names_in_both = MIN(darray_size(into->group_names),
500 darray_size(from->group_names));
501 for (i = 0; i < group_names_in_both; i++) {
502 if (!darray_item(from->group_names, i))
505 if (merge == MERGE_AUGMENT && darray_item(into->group_names, i))
508 darray_item(into->group_names, i) = darray_item(from->group_names, i);
510 /* If @from has more, get them as well. */
511 darray_foreach_from(group_name, from->group_names, group_names_in_both)
512 darray_append(into->group_names, *group_name);
514 darray_foreach(keyi, from->keys) {
515 keyi->merge = (merge == MERGE_DEFAULT ? keyi->merge : merge);
516 if (!AddKeySymbols(into, keyi, false))
520 darray_foreach(mm, from->modMaps) {
521 mm->merge = (merge == MERGE_DEFAULT ? mm->merge : merge);
522 if (!AddModMapEntry(into, mm))
528 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge);
531 HandleIncludeSymbols(SymbolsInfo *info, IncludeStmt *include)
533 SymbolsInfo included;
535 InitSymbolsInfo(&included, info->keymap, info->actions);
536 included.name = include->stmt;
537 include->stmt = NULL;
539 for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
540 SymbolsInfo next_incl;
543 file = ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_SYMBOLS);
545 info->errorCount += 10;
546 ClearSymbolsInfo(&included);
550 InitSymbolsInfo(&next_incl, info->keymap, info->actions);
551 if (stmt->modifier) {
552 next_incl.explicit_group = atoi(stmt->modifier) - 1;
553 if (next_incl.explicit_group >= XKB_MAX_GROUPS) {
554 log_err(info->keymap->ctx,
555 "Cannot set explicit group to %d - must be between 1..%d; "
556 "Ignoring group number\n",
557 next_incl.explicit_group + 1, XKB_MAX_GROUPS);
558 next_incl.explicit_group = info->explicit_group;
562 next_incl.explicit_group = info->explicit_group;
565 HandleSymbolsFile(&next_incl, file, MERGE_OVERRIDE);
567 MergeIncludedSymbols(&included, &next_incl, stmt->merge);
569 ClearSymbolsInfo(&next_incl);
573 MergeIncludedSymbols(info, &included, include->merge);
574 ClearSymbolsInfo(&included);
576 return (info->errorCount == 0);
583 GetGroupIndex(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
584 unsigned what, xkb_layout_index_t *ndx_rtrn)
586 const char *name = (what == SYMBOLS ? "symbols" : "actions");
588 if (arrayNdx == NULL) {
589 xkb_layout_index_t i;
591 enum group_field field = (what == SYMBOLS ?
592 GROUP_FIELD_SYMS : GROUP_FIELD_ACTS);
594 darray_enumerate(i, groupi, keyi->groups) {
595 if (!(groupi->defined & field)) {
601 if (i >= XKB_MAX_GROUPS) {
602 log_err(info->keymap->ctx,
603 "Too many groups of %s for key %s (max %u); "
604 "Ignoring %s defined for extra groups\n",
605 name, KeyInfoText(info, keyi), XKB_MAX_GROUPS, name);
609 darray_resize0(keyi->groups, darray_size(keyi->groups) + 1);
610 *ndx_rtrn = darray_size(keyi->groups) - 1;
614 if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, ndx_rtrn)) {
615 log_err(info->keymap->ctx,
616 "Illegal group index for %s of key %s\n"
617 "Definition with non-integer array index ignored\n",
618 name, KeyInfoText(info, keyi));
623 if (*ndx_rtrn >= darray_size(keyi->groups))
624 darray_resize0(keyi->groups, *ndx_rtrn + 1);
630 LookupKeysym(const char *str, xkb_keysym_t *sym_rtrn)
634 if (!str || istreq(str, "any") || istreq(str, "nosymbol")) {
635 *sym_rtrn = XKB_KEY_NoSymbol;
639 if (istreq(str, "none") || istreq(str, "voidsymbol")) {
640 *sym_rtrn = XKB_KEY_VoidSymbol;
644 sym = xkb_keysym_from_name(str, 0);
645 if (sym != XKB_KEY_NoSymbol) {
654 AddSymbolsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
657 xkb_layout_index_t ndx;
659 xkb_level_index_t nLevels;
663 if (!GetGroupIndex(info, keyi, arrayNdx, SYMBOLS, &ndx))
666 groupi = &darray_item(keyi->groups, ndx);
669 groupi->defined |= GROUP_FIELD_SYMS;
673 if (value->op != EXPR_KEYSYM_LIST) {
674 log_err(info->keymap->ctx,
675 "Expected a list of symbols, found %s; "
676 "Ignoring symbols for group %u of %s\n",
677 expr_op_type_to_string(value->op), ndx + 1,
678 KeyInfoText(info, keyi));
682 if (groupi->defined & GROUP_FIELD_SYMS) {
683 log_err(info->keymap->ctx,
684 "Symbols for key %s, group %u already defined; "
685 "Ignoring duplicate definition\n",
686 KeyInfoText(info, keyi), ndx + 1);
690 nLevels = darray_size(value->value.list.symsMapIndex);
691 if (darray_size(groupi->levels) < nLevels)
692 darray_resize0(groupi->levels, nLevels);
694 groupi->defined |= GROUP_FIELD_SYMS;
696 for (i = 0; i < nLevels; i++) {
697 unsigned int sym_index;
698 struct xkb_level *leveli = &darray_item(groupi->levels, i);
700 sym_index = darray_item(value->value.list.symsMapIndex, i);
701 leveli->num_syms = darray_item(value->value.list.symsNumEntries, i);
702 if (leveli->num_syms > 1)
703 leveli->u.syms = calloc(leveli->num_syms, sizeof(*leveli->u.syms));
705 for (j = 0; j < leveli->num_syms; j++) {
706 char *sym_name = darray_item(value->value.list.syms,
710 if (!LookupKeysym(sym_name, &keysym)) {
711 const char *group_name = "unnamed";
713 if (ndx < darray_size(info->group_names) &&
714 darray_item(info->group_names, ndx))
715 group_name = xkb_atom_text(info->keymap->ctx,
716 darray_item(info->group_names,
719 log_warn(info->keymap->ctx,
720 "Could not resolve keysym %s for key %s, group %u (%s), level %u\n",
721 sym_name, KeyInfoText(info, keyi), ndx + 1,
724 ClearLevelInfo(leveli);
725 leveli->num_syms = 0;
729 if (leveli->num_syms == 1) {
730 if (keysym == XKB_KEY_NoSymbol)
731 leveli->num_syms = 0;
733 leveli->u.sym = keysym;
735 else if (leveli->num_syms > 1) {
736 leveli->u.syms[j] = keysym;
745 AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
749 xkb_layout_index_t ndx;
753 union xkb_action *toAct;
755 if (!GetGroupIndex(info, keyi, arrayNdx, ACTIONS, &ndx))
758 groupi = &darray_item(keyi->groups, ndx);
761 groupi->defined |= GROUP_FIELD_ACTS;
765 if (value->op != EXPR_ACTION_LIST) {
766 log_wsgo(info->keymap->ctx,
767 "Bad expression type (%d) for action list value; "
768 "Ignoring actions for group %u of %s\n",
769 value->op, ndx, KeyInfoText(info, keyi));
773 if (groupi->defined & GROUP_FIELD_ACTS) {
774 log_wsgo(info->keymap->ctx,
775 "Actions for key %s, group %u already defined\n",
776 KeyInfoText(info, keyi), ndx);
781 for (act = value->value.child; act; act = (ExprDef *) act->common.next)
784 if (darray_size(groupi->levels) < nActs)
785 darray_resize0(groupi->levels, nActs);
787 groupi->defined |= GROUP_FIELD_ACTS;
789 act = value->value.child;
790 for (i = 0; i < nActs; i++) {
791 toAct = &darray_item(groupi->levels, i).action;
793 if (!HandleActionDef(act, info->keymap, toAct, info->actions))
794 log_err(info->keymap->ctx,
795 "Illegal action definition for %s; "
796 "Action for group %u/level %u ignored\n",
797 KeyInfoText(info, keyi), ndx + 1, i + 1);
799 act = (ExprDef *) act->common.next;
805 static const LookupEntry repeatEntries[] = {
806 { "true", KEY_REPEAT_YES },
807 { "yes", KEY_REPEAT_YES },
808 { "on", KEY_REPEAT_YES },
809 { "false", KEY_REPEAT_NO },
810 { "no", KEY_REPEAT_NO },
811 { "off", KEY_REPEAT_NO },
812 { "default", KEY_REPEAT_UNDEFINED },
817 SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
818 ExprDef *arrayNdx, ExprDef *value)
821 struct xkb_context *ctx = info->keymap->ctx;
823 if (istreq(field, "type")) {
824 xkb_layout_index_t ndx;
827 if (!ExprResolveString(ctx, value, &val))
829 "The type field of a key symbol map must be a string; "
830 "Ignoring illegal type definition\n");
832 if (arrayNdx == NULL) {
833 keyi->default_type = val;
834 keyi->defined |= KEY_FIELD_DEFAULT_TYPE;
836 else if (!ExprResolveGroup(ctx, arrayNdx, &ndx)) {
838 "Illegal group index for type of key %s; "
839 "Definition with non-integer array index ignored\n",
840 KeyInfoText(info, keyi));
845 if (ndx >= darray_size(keyi->groups))
846 darray_resize0(keyi->groups, ndx + 1);
847 darray_item(keyi->groups, ndx).type = val;
848 darray_item(keyi->groups, ndx).defined |= GROUP_FIELD_TYPE;
851 else if (istreq(field, "symbols"))
852 return AddSymbolsToKey(info, keyi, arrayNdx, value);
853 else if (istreq(field, "actions"))
854 return AddActionsToKey(info, keyi, arrayNdx, value);
855 else if (istreq(field, "vmods") ||
856 istreq(field, "virtualmods") ||
857 istreq(field, "virtualmodifiers")) {
860 ok = ExprResolveModMask(info->keymap, value, MOD_VIRT, &mask);
862 keyi->vmodmap = mask;
863 keyi->defined |= KEY_FIELD_VMODMAP;
866 log_err(info->keymap->ctx,
867 "Expected a virtual modifier mask, found %s; "
868 "Ignoring virtual modifiers definition for key %s\n",
869 expr_op_type_to_string(value->op),
870 KeyInfoText(info, keyi));
873 else if (istreq(field, "locking") ||
874 istreq(field, "lock") ||
875 istreq(field, "locks")) {
876 log_err(info->keymap->ctx,
877 "Key behaviors not supported; "
878 "Ignoring locking specification for key %s\n",
879 KeyInfoText(info, keyi));
881 else if (istreq(field, "radiogroup") ||
882 istreq(field, "permanentradiogroup") ||
883 istreq(field, "allownone")) {
884 log_err(info->keymap->ctx,
885 "Radio groups not supported; "
886 "Ignoring radio group specification for key %s\n",
887 KeyInfoText(info, keyi));
889 else if (istreq_prefix("overlay", field) ||
890 istreq_prefix("permanentoverlay", field)) {
891 log_err(info->keymap->ctx,
892 "Overlays not supported; "
893 "Ignoring overlay specification for key %s\n",
894 KeyInfoText(info, keyi));
896 else if (istreq(field, "repeating") ||
897 istreq(field, "repeats") ||
898 istreq(field, "repeat")) {
901 ok = ExprResolveEnum(ctx, value, &val, repeatEntries);
903 log_err(info->keymap->ctx,
904 "Illegal repeat setting for %s; "
905 "Non-boolean repeat setting ignored\n",
906 KeyInfoText(info, keyi));
910 keyi->defined |= KEY_FIELD_REPEAT;
912 else if (istreq(field, "groupswrap") ||
913 istreq(field, "wrapgroups")) {
916 if (!ExprResolveBoolean(ctx, value, &set)) {
917 log_err(info->keymap->ctx,
918 "Illegal groupsWrap setting for %s; "
919 "Non-boolean value ignored\n",
920 KeyInfoText(info, keyi));
925 keyi->out_of_range_group_action = RANGE_WRAP;
927 keyi->out_of_range_group_action = RANGE_SATURATE;
929 keyi->defined |= KEY_FIELD_GROUPINFO;
931 else if (istreq(field, "groupsclamp") ||
932 istreq(field, "clampgroups")) {
935 if (!ExprResolveBoolean(ctx, value, &set)) {
936 log_err(info->keymap->ctx,
937 "Illegal groupsClamp setting for %s; "
938 "Non-boolean value ignored\n",
939 KeyInfoText(info, keyi));
944 keyi->out_of_range_group_action = RANGE_SATURATE;
946 keyi->out_of_range_group_action = RANGE_WRAP;
948 keyi->defined |= KEY_FIELD_GROUPINFO;
950 else if (istreq(field, "groupsredirect") ||
951 istreq(field, "redirectgroups")) {
952 xkb_layout_index_t grp;
954 if (!ExprResolveGroup(ctx, value, &grp)) {
955 log_err(info->keymap->ctx,
956 "Illegal group index for redirect of key %s; "
957 "Definition with non-integer group ignored\n",
958 KeyInfoText(info, keyi));
962 keyi->out_of_range_group_action = RANGE_REDIRECT;
963 keyi->out_of_range_group_number = grp - 1;
964 keyi->defined |= KEY_FIELD_GROUPINFO;
967 log_err(info->keymap->ctx,
968 "Unknown field %s in a symbol interpretation; "
969 "Definition ignored\n",
978 SetGroupName(SymbolsInfo *info, ExprDef *arrayNdx, ExprDef *value)
980 xkb_layout_index_t group, group_to_use;
984 log_vrb(info->keymap->ctx, 1,
985 "You must specify an index when specifying a group name; "
986 "Group name definition without array subscript ignored\n");
990 if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, &group)) {
991 log_err(info->keymap->ctx,
992 "Illegal index in group name definition; "
993 "Definition with non-integer array index ignored\n");
997 if (!ExprResolveString(info->keymap->ctx, value, &name)) {
998 log_err(info->keymap->ctx,
999 "Group name must be a string; "
1000 "Illegal name for group %d ignored\n", group);
1004 if (info->explicit_group == XKB_LAYOUT_INVALID) {
1005 group_to_use = group - 1;
1007 else if (group - 1 == 0) {
1008 group_to_use = info->explicit_group;
1011 log_warn(info->keymap->ctx,
1012 "An explicit group was specified for the '%s' map, "
1013 "but it provides a name for a group other than Group1 (%d); "
1014 "Ignoring group name '%s'\n",
1016 xkb_atom_text(info->keymap->ctx, name));
1020 if (group_to_use >= darray_size(info->group_names))
1021 darray_resize0(info->group_names, group_to_use + 1);
1022 darray_item(info->group_names, group_to_use) = name;
1028 HandleGlobalVar(SymbolsInfo *info, VarDef *stmt)
1030 const char *elem, *field;
1034 if (ExprResolveLhs(info->keymap->ctx, stmt->name, &elem, &field,
1036 return 0; /* internal error, already reported */
1037 if (elem && istreq(elem, "key")) {
1038 ret = SetSymbolsField(info, &info->default_key, field, arrayNdx,
1041 else if (!elem && (istreq(field, "name") ||
1042 istreq(field, "groupname"))) {
1043 ret = SetGroupName(info, arrayNdx, stmt->value);
1045 else if (!elem && (istreq(field, "groupswrap") ||
1046 istreq(field, "wrapgroups"))) {
1047 log_err(info->keymap->ctx,
1048 "Global \"groupswrap\" not supported; Ignored\n");
1051 else if (!elem && (istreq(field, "groupsclamp") ||
1052 istreq(field, "clampgroups"))) {
1053 log_err(info->keymap->ctx,
1054 "Global \"groupsclamp\" not supported; Ignored\n");
1057 else if (!elem && (istreq(field, "groupsredirect") ||
1058 istreq(field, "redirectgroups"))) {
1059 log_err(info->keymap->ctx,
1060 "Global \"groupsredirect\" not supported; Ignored\n");
1063 else if (!elem && istreq(field, "allownone")) {
1064 log_err(info->keymap->ctx,
1065 "Radio groups not supported; "
1066 "Ignoring \"allownone\" specification\n");
1070 ret = SetActionField(info->keymap, elem, field, arrayNdx, stmt->value,
1078 HandleSymbolsBody(SymbolsInfo *info, VarDef *def, KeyInfo *keyi)
1081 const char *elem, *field;
1084 for (; def; def = (VarDef *) def->common.next) {
1085 if (def->name && def->name->op == EXPR_FIELD_REF) {
1086 log_err(info->keymap->ctx,
1087 "Cannot set a global default value from within a key statement; "
1088 "Move statements to the global file scope\n");
1093 if (!def->value || def->value->op == EXPR_KEYSYM_LIST)
1100 ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
1105 ok = SetSymbolsField(info, keyi, field, arrayNdx, def->value);
1112 SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi)
1114 xkb_layout_index_t i;
1118 if (info->explicit_group == XKB_LAYOUT_INVALID)
1121 darray_enumerate_from(i, groupi, keyi->groups, 1) {
1122 if (groupi->defined) {
1124 ClearGroupInfo(groupi);
1125 InitGroupInfo(groupi);
1130 log_warn(info->keymap->ctx,
1131 "For the map %s an explicit group specified, "
1132 "but key %s has more than one group defined; "
1133 "All groups except first one will be ignored\n",
1134 info->name, KeyInfoText(info, keyi));
1136 darray_resize0(keyi->groups, info->explicit_group + 1);
1137 if (info->explicit_group > 0) {
1138 darray_item(keyi->groups, info->explicit_group) =
1139 darray_item(keyi->groups, 0);
1140 InitGroupInfo(&darray_item(keyi->groups, 0));
1147 HandleSymbolsDef(SymbolsInfo *info, SymbolsDef *stmt)
1150 xkb_layout_index_t i;
1152 keyi = info->default_key;
1153 darray_init(keyi.groups);
1154 darray_copy(keyi.groups, info->default_key.groups);
1155 for (i = 0; i < darray_size(keyi.groups); i++)
1156 CopyGroupInfo(&darray_item(keyi.groups, i),
1157 &darray_item(info->default_key.groups, i));
1158 keyi.merge = stmt->merge;
1159 keyi.name = stmt->keyName;
1161 if (!HandleSymbolsBody(info, (VarDef *) stmt->symbols, &keyi)) {
1166 if (!SetExplicitGroup(info, &keyi)) {
1171 if (!AddKeySymbols(info, &keyi, true)) {
1180 HandleModMapDef(SymbolsInfo *info, ModMapDef *def)
1184 xkb_mod_index_t ndx;
1186 struct xkb_context *ctx = info->keymap->ctx;
1188 ndx = ModNameToIndex(info->keymap, def->modifier, MOD_REAL);
1189 if (ndx == XKB_MOD_INVALID) {
1190 log_err(info->keymap->ctx,
1191 "Illegal modifier map definition; "
1192 "Ignoring map for non-modifier \"%s\"\n",
1193 xkb_atom_text(ctx, def->modifier));
1200 for (key = def->keys; key != NULL; key = (ExprDef *) key->common.next) {
1203 if (key->op == EXPR_VALUE && key->value_type == EXPR_TYPE_KEYNAME) {
1204 tmp.haveSymbol = false;
1205 tmp.u.keyName = key->value.keyName;
1207 else if (ExprResolveKeySym(ctx, key, &sym)) {
1208 tmp.haveSymbol = true;
1212 log_err(info->keymap->ctx,
1213 "Modmap entries may contain only key names or keysyms; "
1214 "Illegal definition for %s modifier ignored\n",
1215 ModIndexText(info->keymap, tmp.modifier));
1219 ok = AddModMapEntry(info, &tmp) && ok;
1225 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge)
1230 info->name = strdup_safe(file->name);
1232 for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
1233 switch (stmt->type) {
1235 ok = HandleIncludeSymbols(info, (IncludeStmt *) stmt);
1238 ok = HandleSymbolsDef(info, (SymbolsDef *) stmt);
1241 ok = HandleGlobalVar(info, (VarDef *) stmt);
1244 ok = HandleVModDef(info->keymap, (VModDef *) stmt);
1247 ok = HandleModMapDef(info, (ModMapDef *) stmt);
1250 log_err(info->keymap->ctx,
1251 "Symbols files may not include other types; "
1252 "Ignoring %s\n", stmt_type_to_string(stmt->type));
1260 if (info->errorCount > 10) {
1261 log_err(info->keymap->ctx, "Abandoning symbols file \"%s\"\n",
1269 * Given a keysym @sym, return a key which generates it, or NULL.
1270 * This is used for example in a modifier map definition, such as:
1271 * modifier_map Lock { Caps_Lock };
1272 * where we want to add the Lock modifier to the modmap of the key
1273 * which matches the keysym Caps_Lock.
1274 * Since there can be many keys which generates the keysym, the key
1275 * is chosen first by lowest group in which the keysym appears, than
1276 * by lowest level and than by lowest key code.
1278 static struct xkb_key *
1279 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
1281 struct xkb_key *key, *ret = NULL;
1282 xkb_layout_index_t group, min_group = UINT32_MAX;
1283 xkb_level_index_t level, min_level = UINT16_MAX;
1285 xkb_foreach_key(key, keymap) {
1286 for (group = 0; group < key->num_groups; group++) {
1287 for (level = 0; level < XkbKeyGroupWidth(key, group); level++) {
1288 if (key->groups[group].levels[level].num_syms != 1 ||
1289 key->groups[group].levels[level].u.sym != sym)
1293 * If the keysym was found in a group or level > 0, we must
1294 * keep looking since we might find a key in which the keysym
1295 * is in a lower group or level.
1297 if (group < min_group ||
1298 (group == min_group && level < min_level)) {
1300 if (group == 0 && level == 0) {
1316 * Find an appropriate type for a group and return its name.
1319 * - ONE_LEVEL for width 0/1
1320 * - ALPHABETIC for 2 shift levels, with lower/upercase keysyms
1321 * - KEYPAD for keypad keys.
1322 * - TWO_LEVEL for other 2 shift level keys.
1323 * and the same for four level keys.
1325 * FIXME: Decide how to handle multiple-syms-per-level, and do it.
1328 FindAutomaticType(struct xkb_context *ctx, GroupInfo *groupi)
1330 xkb_keysym_t sym0, sym1, sym2, sym3;
1331 xkb_level_index_t width = darray_size(groupi->levels);
1333 #define GET_SYM(level) \
1334 (darray_item(groupi->levels, level).num_syms == 0 ? \
1335 XKB_KEY_NoSymbol : \
1336 darray_item(groupi->levels, level).num_syms == 1 ? \
1337 darray_item(groupi->levels, level).u.sym : \
1338 /* num_syms > 1 */ \
1339 darray_item(groupi->levels, level).u.syms[0])
1341 if (width == 1 || width <= 0)
1342 return xkb_atom_intern_literal(ctx, "ONE_LEVEL");
1348 if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1))
1349 return xkb_atom_intern_literal(ctx, "ALPHABETIC");
1351 if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1352 return xkb_atom_intern_literal(ctx, "KEYPAD");
1354 return xkb_atom_intern_literal(ctx, "TWO_LEVEL");
1358 if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1)) {
1360 sym3 = (width == 4 ? GET_SYM(3) : XKB_KEY_NoSymbol);
1362 if (xkb_keysym_is_lower(sym2) && xkb_keysym_is_upper(sym3))
1363 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_ALPHABETIC");
1365 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_SEMIALPHABETIC");
1368 if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1369 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_KEYPAD");
1371 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL");
1374 return XKB_ATOM_NONE;
1379 static const struct xkb_key_type *
1380 FindTypeForGroup(struct xkb_keymap *keymap, KeyInfo *keyi,
1381 xkb_layout_index_t group, bool *explicit_type)
1384 GroupInfo *groupi = &darray_item(keyi->groups, group);
1385 xkb_atom_t type_name = groupi->type;
1387 *explicit_type = true;
1389 if (type_name == XKB_ATOM_NONE) {
1390 if (keyi->default_type != XKB_ATOM_NONE) {
1391 type_name = keyi->default_type;
1394 type_name = FindAutomaticType(keymap->ctx, groupi);
1395 if (type_name != XKB_ATOM_NONE)
1396 *explicit_type = false;
1400 if (type_name == XKB_ATOM_NONE) {
1401 log_warn(keymap->ctx,
1402 "Couldn't find an automatic type for key '%s' group %d with %lu levels; "
1403 "Using the default type\n",
1404 KeyNameText(keymap->ctx, keyi->name), group + 1,
1405 (unsigned long) darray_size(groupi->levels));
1409 for (i = 0; i < keymap->num_types; i++)
1410 if (keymap->types[i].name == type_name)
1413 if (i >= keymap->num_types) {
1414 log_warn(keymap->ctx,
1415 "The type \"%s\" for key '%s' group %d was not previously defined; "
1416 "Using the default type\n",
1417 xkb_atom_text(keymap->ctx, type_name),
1418 KeyNameText(keymap->ctx, keyi->name), group + 1);
1422 return &keymap->types[i];
1426 * Index 0 is guaranteed to contain something, usually
1427 * ONE_LEVEL or at least some default one-level type.
1429 return &keymap->types[0];
1433 CopySymbolsDef(SymbolsInfo *info, KeyInfo *keyi)
1435 struct xkb_keymap *keymap = info->keymap;
1436 struct xkb_key *key;
1438 const GroupInfo *group0;
1439 xkb_layout_index_t i;
1442 * The name is guaranteed to be real and not an alias (see
1443 * AddKeySymbols), so 'false' is safe here.
1445 key = XkbKeyByName(keymap, keyi->name, false);
1447 log_vrb(info->keymap->ctx, 5,
1448 "Key %s not found in keycodes; Symbols ignored\n",
1449 KeyInfoText(info, keyi));
1453 /* Find the range of groups we need. */
1454 key->num_groups = 0;
1455 darray_enumerate(i, groupi, keyi->groups)
1456 if (groupi->defined)
1457 key->num_groups = i + 1;
1459 if (key->num_groups <= 0)
1460 return false; /* WSGO */
1462 darray_resize(keyi->groups, key->num_groups);
1465 * If there are empty groups between non-empty ones, fill them with data
1466 * from the first group.
1467 * We can make a wrong assumption here. But leaving gaps is worse.
1469 group0 = &darray_item(keyi->groups, 0);
1470 darray_foreach_from(groupi, keyi->groups, 1) {
1471 if (groupi->defined)
1474 CopyGroupInfo(groupi, group0);
1477 key->groups = calloc(key->num_groups, sizeof(*key->groups));
1479 /* Find and assign the groups' types in the keymap. */
1480 darray_enumerate(i, groupi, keyi->groups) {
1481 const struct xkb_key_type *type;
1484 type = FindTypeForGroup(keymap, keyi, i, &explicit_type);
1486 /* Always have as many levels as the type specifies. */
1487 if (type->num_levels < darray_size(groupi->levels)) {
1488 struct xkb_level *leveli;
1490 log_vrb(info->keymap->ctx, 1,
1491 "Type \"%s\" has %d levels, but %s has %d levels; "
1492 "Ignoring extra symbols\n",
1493 xkb_atom_text(keymap->ctx, type->name), type->num_levels,
1494 KeyInfoText(info, keyi),
1495 (int) darray_size(groupi->levels));
1497 darray_foreach_from(leveli, groupi->levels, type->num_levels)
1498 ClearLevelInfo(leveli);
1500 darray_resize0(groupi->levels, type->num_levels);
1502 key->groups[i].explicit_type = explicit_type;
1503 key->groups[i].type = type;
1507 darray_enumerate(i, groupi, keyi->groups) {
1508 key->groups[i].levels = darray_mem(groupi->levels, 0);
1509 darray_init(groupi->levels);
1512 key->out_of_range_group_number = keyi->out_of_range_group_number;
1513 key->out_of_range_group_action = keyi->out_of_range_group_action;
1515 if (keyi->defined & KEY_FIELD_VMODMAP) {
1516 key->vmodmap = keyi->vmodmap;
1517 key->explicit |= EXPLICIT_VMODMAP;
1520 if (keyi->repeat != KEY_REPEAT_UNDEFINED) {
1521 key->repeats = (keyi->repeat == KEY_REPEAT_YES);
1522 key->explicit |= EXPLICIT_REPEAT;
1525 darray_foreach(groupi, keyi->groups) {
1526 if (groupi->defined & GROUP_FIELD_ACTS) {
1527 key->explicit |= EXPLICIT_INTERP;
1536 CopyModMapDef(SymbolsInfo *info, ModMapEntry *entry)
1538 struct xkb_key *key;
1539 struct xkb_keymap *keymap = info->keymap;
1541 if (!entry->haveSymbol) {
1542 key = XkbKeyByName(keymap, entry->u.keyName, true);
1544 log_vrb(info->keymap->ctx, 5,
1545 "Key %s not found in keycodes; "
1546 "Modifier map entry for %s not updated\n",
1547 KeyNameText(keymap->ctx, entry->u.keyName),
1548 ModIndexText(info->keymap, entry->modifier));
1553 key = FindKeyForSymbol(keymap, entry->u.keySym);
1555 log_vrb(info->keymap->ctx, 5,
1556 "Key \"%s\" not found in symbol map; "
1557 "Modifier map entry for %s not updated\n",
1558 KeysymText(info->keymap->ctx, entry->u.keySym),
1559 ModIndexText(info->keymap, entry->modifier));
1564 key->modmap |= (1 << entry->modifier);
1569 CopySymbolsToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info)
1573 struct xkb_key *key;
1575 keymap->symbols_section_name = strdup_safe(info->name);
1576 XkbEscapeMapName(keymap->symbols_section_name);
1578 keymap->num_group_names = darray_size(info->group_names);
1579 keymap->group_names = darray_mem(info->group_names, 0);
1580 darray_init(info->group_names);
1582 darray_foreach(keyi, info->keys)
1583 if (!CopySymbolsDef(info, keyi))
1586 if (xkb_context_get_log_verbosity(keymap->ctx) > 3) {
1587 xkb_foreach_key(key, keymap) {
1588 if (key->name == XKB_ATOM_NONE)
1591 if (key->num_groups < 1)
1592 log_info(keymap->ctx,
1593 "No symbols defined for %s\n",
1594 KeyNameText(keymap->ctx, key->name));
1598 darray_foreach(mm, info->modMaps)
1599 if (!CopyModMapDef(info, mm))
1602 /* XXX: If we don't ignore errorCount, things break. */
1607 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1608 enum merge_mode merge)
1611 ActionsInfo *actions;
1613 actions = NewActionsInfo();
1617 InitSymbolsInfo(&info, keymap, actions);
1618 info.default_key.merge = merge;
1620 HandleSymbolsFile(&info, file, merge);
1622 if (info.errorCount != 0)
1625 if (!CopySymbolsToKeymap(keymap, &info))
1628 ClearSymbolsInfo(&info);
1629 FreeActionsInfo(actions);
1633 FreeActionsInfo(actions);
1634 ClearSymbolsInfo(&info);