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 ********************************************************/
29 #include "xkbcomp-priv.h"
30 #include "parseutils.h"
34 /***====================================================================***/
36 /* Needed to work with the typechecker. */
37 typedef darray(xkb_keysym_t) darray_xkb_keysym_t;
38 typedef darray(union xkb_action) darray_xkb_action;
43 KEY_REPEAT_UNDEFINED = -1
47 KEY_FIELD_SYMS = (1 << 0),
48 KEY_FIELD_ACTS = (1 << 1),
49 KEY_FIELD_REPEAT = (1 << 2),
50 KEY_FIELD_BEHAVIOR = (1 << 3),
51 KEY_FIELD_TYPE_DFLT = (1 << 4),
52 KEY_FIELD_TYPES = (1 << 5),
53 KEY_FIELD_GROUPINFO = (1 << 6),
54 KEY_FIELD_VMODMAP = (1 << 7),
57 static inline const char *
58 longText(unsigned long val)
62 LongToKeyName(val, buf);
63 return KeyNameText(buf);
66 typedef struct _KeyInfo {
67 enum key_field defined;
69 enum merge_mode merge;
71 unsigned long name; /* the 4 chars of the key name, as long */
72 unsigned char typesDefined;
73 unsigned char symsDefined;
74 unsigned char actsDefined;
75 unsigned int numLevels[XkbNumKbdGroups];
77 /* syms[group] -> Single array for all the keysyms in the group. */
78 darray_xkb_keysym_t syms[XkbNumKbdGroups];
80 * symsMapIndex[group][level] -> The index from which the syms for
81 * the level begin in the syms[group] array. Remember each keycode
82 * can have multiple keysyms in each level (that is, each key press
83 * can result in multiple keysyms).
85 darray(int) symsMapIndex[XkbNumKbdGroups];
87 * symsMapNumEntries[group][level] -> How many syms are in
88 * syms[group][symsMapIndex[group][level]].
90 darray(size_t) symsMapNumEntries[XkbNumKbdGroups];
92 darray_xkb_action acts[XkbNumKbdGroups];
94 xkb_atom_t types[XkbNumKbdGroups];
95 enum key_repeat repeat;
96 struct xkb_behavior behavior;
97 unsigned short vmodmap;
100 uint8_t out_of_range_group_action;
101 xkb_group_index_t out_of_range_group_number;
105 * Init the given key info to sane values.
108 InitKeyInfo(KeyInfo *keyi, unsigned file_id)
111 static const char dflt[4] = "*";
114 keyi->file_id = file_id;
115 keyi->merge = MERGE_OVERRIDE;
116 keyi->name = KeyNameToLong(dflt);
117 keyi->typesDefined = keyi->symsDefined = keyi->actsDefined = 0;
119 for (i = 0; i < XkbNumKbdGroups; i++) {
120 keyi->numLevels[i] = 0;
121 keyi->types[i] = XKB_ATOM_NONE;
122 darray_init(keyi->syms[i]);
123 darray_init(keyi->symsMapIndex[i]);
124 darray_init(keyi->symsMapNumEntries[i]);
125 darray_init(keyi->acts[i]);
128 keyi->dfltType = XKB_ATOM_NONE;
129 keyi->behavior.type = XkbKB_Default;
130 keyi->behavior.data = 0;
132 keyi->repeat = KEY_REPEAT_UNDEFINED;
133 keyi->out_of_range_group_action = 0;
134 keyi->out_of_range_group_number = 0;
138 FreeKeyInfo(KeyInfo *keyi)
142 for (i = 0; i < XkbNumKbdGroups; i++) {
143 darray_free(keyi->syms[i]);
144 darray_free(keyi->symsMapIndex[i]);
145 darray_free(keyi->symsMapNumEntries[i]);
146 darray_free(keyi->acts[i]);
151 * Copy old into new, optionally reset old to 0.
152 * If old is reset, new simply re-uses old's memory. Otherwise, the memory is
153 * newly allocated and new points to the new memory areas.
156 CopyKeyInfo(KeyInfo * old, KeyInfo * new, bool clearOld)
163 for (i = 0; i < XkbNumKbdGroups; i++) {
164 old->numLevels[i] = 0;
165 darray_init(old->symsMapIndex[i]);
166 darray_init(old->symsMapNumEntries[i]);
167 darray_init(old->syms[i]);
168 darray_init(old->acts[i]);
172 for (i = 0; i < XkbNumKbdGroups; i++) {
173 darray_copy(new->syms[i], old->syms[i]);
174 darray_copy(new->symsMapIndex[i], old->symsMapIndex[i]);
175 darray_copy(new->symsMapNumEntries[i], old->symsMapNumEntries[i]);
176 darray_copy(new->acts[i], old->acts[i]);
183 /***====================================================================***/
185 typedef struct _ModMapEntry {
187 enum merge_mode merge;
191 unsigned long keyName;
196 typedef struct _SymbolsInfo {
197 char *name; /* e.g. pc+us+inet(evdev) */
200 enum merge_mode merge;
201 xkb_group_index_t explicit_group;
202 darray(KeyInfo) keys;
206 xkb_atom_t groupNames[XkbNumKbdGroups];
210 struct xkb_keymap *keymap;
214 InitSymbolsInfo(SymbolsInfo * info, struct xkb_keymap *keymap,
220 info->explicit_group = 0;
221 info->errorCount = 0;
222 info->file_id = file_id;
223 info->merge = MERGE_OVERRIDE;
224 darray_init(info->keys);
225 darray_growalloc(info->keys, 110);
226 list_init(&info->modMaps);
227 for (i = 0; i < XkbNumKbdGroups; i++)
228 info->groupNames[i] = XKB_ATOM_NONE;
229 InitKeyInfo(&info->dflt, file_id);
230 InitVModInfo(&info->vmods, keymap);
232 info->keymap = keymap;
236 FreeSymbolsInfo(SymbolsInfo * info)
239 ModMapEntry *mm, *next;
242 darray_foreach(keyi, info->keys) {
245 darray_free(info->keys);
246 list_foreach_safe(mm, next, &info->modMaps, entry)
248 memset(info, 0, sizeof(SymbolsInfo));
252 ResizeKeyGroup(KeyInfo *keyi, xkb_group_index_t group, unsigned int numLevels,
253 unsigned sizeSyms, bool forceActions)
257 if (darray_size(keyi->syms[group]) < sizeSyms)
258 darray_resize0(keyi->syms[group], sizeSyms);
260 if (darray_empty(keyi->symsMapIndex[group]) ||
261 keyi->numLevels[group] < numLevels) {
262 darray_resize(keyi->symsMapIndex[group], numLevels);
263 for (i = keyi->numLevels[group]; i < numLevels; i++)
264 darray_item(keyi->symsMapIndex[group], i) = -1;
267 if (darray_empty(keyi->symsMapNumEntries[group]) ||
268 keyi->numLevels[group] < numLevels)
269 darray_resize0(keyi->symsMapNumEntries[group], numLevels);
271 if ((forceActions && (keyi->numLevels[group] < numLevels ||
272 darray_empty(keyi->acts[group]))) ||
273 (keyi->numLevels[group] < numLevels && !darray_empty(keyi->acts[group])))
274 darray_resize0(keyi->acts[group], numLevels);
276 if (keyi->numLevels[group] < numLevels)
277 keyi->numLevels[group] = numLevels;
282 enum key_group_selector {
289 MergeKeyGroups(SymbolsInfo * info,
290 KeyInfo * into, KeyInfo * from, xkb_group_index_t group)
292 darray_xkb_keysym_t resultSyms;
293 enum key_group_selector using = NONE;
294 darray_xkb_action resultActs;
295 unsigned int resultWidth;
296 unsigned int resultSize = 0;
299 bool report, clobber;
300 int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
302 clobber = (from->merge != MERGE_AUGMENT);
304 report = (verbosity > 9) ||
305 (into->file_id == from->file_id && verbosity > 0);
307 darray_init(resultSyms);
309 if (into->numLevels[group] >= from->numLevels[group]) {
310 resultActs = into->acts[group];
311 resultWidth = into->numLevels[group];
314 resultActs = from->acts[group];
315 resultWidth = from->numLevels[group];
316 darray_resize(into->symsMapIndex[group],
317 from->numLevels[group]);
318 darray_resize0(into->symsMapNumEntries[group],
319 from->numLevels[group]);
321 for (i = into->numLevels[group]; i < from->numLevels[group]; i++)
322 darray_item(into->symsMapIndex[group], i) = -1;
325 if (darray_empty(resultActs) && (!darray_empty(into->acts[group]) ||
326 !darray_empty(from->acts[group]))) {
327 darray_resize0(resultActs, resultWidth);
328 for (i = 0; i < resultWidth; i++) {
329 union xkb_action *fromAct = NULL, *toAct = NULL;
331 if (!darray_empty(from->acts[group]))
332 fromAct = &darray_item(from->acts[group], i);
334 if (!darray_empty(into->acts[group]))
335 toAct = &darray_item(into->acts[group], i);
337 if (((fromAct == NULL) || (fromAct->type == XkbSA_NoAction))
338 && (toAct != NULL)) {
339 darray_item(resultActs, i) = *toAct;
341 else if (((toAct == NULL) || (toAct->type == XkbSA_NoAction))
342 && (fromAct != NULL)) {
343 darray_item(resultActs, i) = *fromAct;
346 union xkb_action *use, *ignore;
356 log_warn(info->keymap->ctx,
357 "Multiple actions for level %d/group %u on key %s; "
358 "Using %s, ignoring %s\n",
359 i + 1, group + 1, longText(into->name),
360 ActionTypeText(use->type),
361 ActionTypeText(ignore->type));
363 darray_item(resultActs, i) = *use;
368 for (i = 0; i < resultWidth; i++) {
369 unsigned int fromSize = 0;
372 if (!darray_empty(from->symsMapNumEntries[group]) &&
373 i < from->numLevels[group])
374 fromSize = darray_item(from->symsMapNumEntries[group], i);
376 if (!darray_empty(into->symsMapNumEntries[group]) &&
377 i < into->numLevels[group])
378 toSize = darray_item(into->symsMapNumEntries[group], i);
381 resultSize += toSize;
384 else if (toSize == 0 || clobber) {
385 resultSize += fromSize;
389 resultSize += toSize;
398 resultSyms = from->syms[group];
399 darray_free(into->symsMapNumEntries[group]);
400 darray_free(into->symsMapIndex[group]);
401 into->symsMapNumEntries[group] = from->symsMapNumEntries[group];
402 into->symsMapIndex[group] = from->symsMapIndex[group];
403 darray_init(from->symsMapNumEntries[group]);
404 darray_init(from->symsMapIndex[group]);
407 else if (using == TO) {
408 resultSyms = into->syms[group];
412 darray_resize0(resultSyms, resultSize);
414 for (i = 0; i < resultWidth; i++) {
415 enum key_group_selector use = NONE;
416 unsigned int fromSize = 0;
417 unsigned int toSize = 0;
419 if (i < from->numLevels[group])
420 fromSize = darray_item(from->symsMapNumEntries[group], i);
422 if (i < into->numLevels[group])
423 toSize = darray_item(into->symsMapNumEntries[group], i);
425 if (fromSize == 0 && toSize == 0) {
426 darray_item(into->symsMapIndex[group], i) = -1;
427 darray_item(into->symsMapNumEntries[group], i) = 0;
433 else if (toSize == 0 || clobber)
438 if (toSize && fromSize && report) {
439 log_info(info->keymap->ctx,
440 "Multiple symbols for group %u, level %d on key %s; "
441 "Using %s, ignoring %s\n",
442 group + 1, i + 1, longText(into->name),
443 (use == FROM ? "from" : "to"),
444 (use == FROM ? "to" : "from"));
448 memcpy(darray_mem(resultSyms, cur_idx),
449 darray_mem(from->syms[group],
450 darray_item(from->symsMapIndex[group], i)),
451 darray_item(from->symsMapNumEntries[group],
452 i) * sizeof(xkb_keysym_t));
453 darray_item(into->symsMapIndex[group], i) = cur_idx;
454 darray_item(into->symsMapNumEntries[group], i) =
455 darray_item(from->symsMapNumEntries[group], i);
458 memcpy(darray_mem(resultSyms, cur_idx),
459 darray_mem(into->syms[group],
460 darray_item(into->symsMapIndex[group], i)),
461 darray_item(into->symsMapNumEntries[group],
462 i) * sizeof(xkb_keysym_t));
463 darray_item(into->symsMapIndex[group], i) = cur_idx;
465 cur_idx += darray_item(into->symsMapNumEntries[group], i);
469 if (!darray_same(resultActs, into->acts[group]))
470 darray_free(into->acts[group]);
471 if (!darray_same(resultActs, from->acts[group]))
472 darray_free(from->acts[group]);
473 into->numLevels[group] = resultWidth;
474 if (!darray_same(resultSyms, into->syms[group]))
475 darray_free(into->syms[group]);
476 into->syms[group] = resultSyms;
477 if (!darray_same(resultSyms, from->syms[group]))
478 darray_free(from->syms[group]);
479 darray_init(from->syms[group]);
480 darray_free(from->symsMapIndex[group]);
481 darray_free(from->symsMapNumEntries[group]);
482 into->acts[group] = resultActs;
483 darray_init(from->acts[group]);
484 if (!darray_empty(into->syms[group]))
485 into->symsDefined |= (1 << group);
486 from->symsDefined &= ~(1 << group);
487 into->actsDefined |= (1 << group);
488 from->actsDefined &= ~(1 << group);
494 UseNewKeyField(enum key_field field, KeyInfo *old, KeyInfo *new,
495 int verbosity, enum key_field *collide)
497 if (!(old->defined & field))
500 if (new->defined & field) {
501 if ((old->file_id == new->file_id && verbosity > 0) || verbosity > 9)
504 if (new->merge != MERGE_AUGMENT)
513 MergeKeys(SymbolsInfo *info, KeyInfo *into, KeyInfo *from)
516 enum key_field collide = 0;
518 int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
520 if (from->merge == MERGE_REPLACE) {
521 for (i = 0; i < XkbNumKbdGroups; i++) {
522 if (into->numLevels[i] != 0) {
523 darray_free(into->syms[i]);
524 darray_free(into->acts[i]);
528 memset(from, 0, sizeof(KeyInfo));
532 report = (verbosity > 9 ||
533 (into->file_id == from->file_id && verbosity > 0));
535 for (i = 0; i < XkbNumKbdGroups; i++) {
536 if (from->numLevels[i] > 0) {
537 if (into->numLevels[i] == 0) {
538 into->numLevels[i] = from->numLevels[i];
539 into->syms[i] = from->syms[i];
540 into->symsMapIndex[i] = from->symsMapIndex[i];
541 into->symsMapNumEntries[i] = from->symsMapNumEntries[i];
542 into->acts[i] = from->acts[i];
543 into->symsDefined |= (1 << i);
544 darray_init(from->syms[i]);
545 darray_init(from->symsMapIndex[i]);
546 darray_init(from->symsMapNumEntries[i]);
547 darray_init(from->acts[i]);
548 from->numLevels[i] = 0;
549 from->symsDefined &= ~(1 << i);
550 if (!darray_empty(into->syms[i]))
551 into->defined |= KEY_FIELD_SYMS;
552 if (!darray_empty(into->acts[i]))
553 into->defined |= KEY_FIELD_ACTS;
557 if (!darray_empty(into->syms[i]))
558 collide |= KEY_FIELD_SYMS;
559 if (!darray_empty(into->acts[i]))
560 collide |= KEY_FIELD_ACTS;
562 MergeKeyGroups(info, into, from, (unsigned) i);
565 if (from->types[i] != XKB_ATOM_NONE) {
566 if ((into->types[i] != XKB_ATOM_NONE) && report &&
567 (into->types[i] != from->types[i])) {
568 xkb_atom_t use, ignore;
569 collide |= KEY_FIELD_TYPES;
570 if (from->merge != MERGE_AUGMENT) {
571 use = from->types[i];
572 ignore = into->types[i];
575 use = into->types[i];
576 ignore = from->types[i];
579 log_warn(info->keymap->ctx,
580 "Multiple definitions for group %d type of key %s; "
581 "Using %s, ignoring %s\n",
582 i, longText(into->name),
583 xkb_atom_text(info->keymap->ctx, use),
584 xkb_atom_text(info->keymap->ctx, ignore));
587 if (from->merge != MERGE_AUGMENT ||
588 into->types[i] == XKB_ATOM_NONE) {
589 into->types[i] = from->types[i];
594 if (UseNewKeyField(KEY_FIELD_BEHAVIOR, into, from, verbosity, &collide)) {
595 into->behavior = from->behavior;
596 into->defined |= KEY_FIELD_BEHAVIOR;
598 if (UseNewKeyField(KEY_FIELD_VMODMAP, into, from, verbosity, &collide)) {
599 into->vmodmap = from->vmodmap;
600 into->defined |= KEY_FIELD_VMODMAP;
602 if (UseNewKeyField(KEY_FIELD_REPEAT, into, from, verbosity, &collide)) {
603 into->repeat = from->repeat;
604 into->defined |= KEY_FIELD_REPEAT;
606 if (UseNewKeyField(KEY_FIELD_TYPE_DFLT, into, from, verbosity, &collide)) {
607 into->dfltType = from->dfltType;
608 into->defined |= KEY_FIELD_TYPE_DFLT;
610 if (UseNewKeyField(KEY_FIELD_GROUPINFO, into, from, verbosity, &collide)) {
611 into->out_of_range_group_action = from->out_of_range_group_action;
612 into->out_of_range_group_number = from->out_of_range_group_number;
613 into->defined |= KEY_FIELD_GROUPINFO;
617 log_warn(info->keymap->ctx,
618 "Symbol map for key %s redefined; "
619 "Using %s definition for conflicting fields\n",
620 longText(into->name),
621 (from->merge == MERGE_AUGMENT ? "first" : "last"));
627 AddKeySymbols(SymbolsInfo *info, KeyInfo *keyi)
629 unsigned long real_name;
632 darray_foreach(iter, info->keys)
633 if (iter->name == keyi->name)
634 return MergeKeys(info, iter, keyi);
636 if (FindKeyNameForAlias(info->keymap, keyi->name, &real_name))
637 darray_foreach(iter, info->keys)
638 if (iter->name == real_name)
639 return MergeKeys(info, iter, keyi);
641 darray_resize0(info->keys, darray_size(info->keys) + 1);
642 new = &darray_item(info->keys, darray_size(info->keys) - 1);
643 return CopyKeyInfo(keyi, new, true);
647 AddModMapEntry(SymbolsInfo * info, ModMapEntry * new)
652 clobber = (new->merge != MERGE_AUGMENT);
653 list_foreach(mm, &info->modMaps, entry) {
654 if (new->haveSymbol && mm->haveSymbol
655 && (new->u.keySym == mm->u.keySym)) {
656 unsigned use, ignore;
657 if (mm->modifier != new->modifier) {
660 ignore = mm->modifier;
664 ignore = new->modifier;
666 log_err(info->keymap->ctx,
667 "%s added to symbol map for multiple modifiers; "
668 "Using %s, ignoring %s.\n",
669 KeysymText(new->u.keySym), ModIndexText(use),
670 ModIndexText(ignore));
675 if ((!new->haveSymbol) && (!mm->haveSymbol) &&
676 (new->u.keyName == mm->u.keyName)) {
677 unsigned use, ignore;
678 if (mm->modifier != new->modifier) {
681 ignore = mm->modifier;
685 ignore = new->modifier;
687 log_err(info->keymap->ctx,
688 "Key %s added to map for multiple modifiers; "
689 "Using %s, ignoring %s.\n",
690 longText(new->u.keyName), ModIndexText(use),
691 ModIndexText(ignore));
698 mm = malloc(sizeof(*mm));
700 log_wsgo(info->keymap->ctx,
701 "Could not allocate modifier map entry; "
702 "Modifier map for %s will be incomplete\n",
703 ModIndexText(new->modifier));
708 list_add(&mm->entry, &info->modMaps);
712 /***====================================================================***/
715 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
716 enum merge_mode merge)
720 ModMapEntry *mm, *next;
722 if (from->errorCount > 0) {
723 into->errorCount += from->errorCount;
726 if (into->name == NULL) {
727 into->name = from->name;
730 for (i = 0; i < XkbNumKbdGroups; i++) {
731 if (from->groupNames[i] != XKB_ATOM_NONE) {
732 if ((merge != MERGE_AUGMENT) ||
733 (into->groupNames[i] == XKB_ATOM_NONE))
734 into->groupNames[i] = from->groupNames[i];
738 darray_foreach(keyi, from->keys) {
739 if (merge != MERGE_DEFAULT)
742 if (!AddKeySymbols(into, keyi))
746 list_foreach_safe(mm, next, &from->modMaps, entry) {
747 if (merge != MERGE_DEFAULT)
749 if (!AddModMapEntry(into, mm))
753 list_init(&from->modMaps);
757 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge);
760 HandleIncludeSymbols(SymbolsInfo *info, IncludeStmt *stmt)
762 enum merge_mode merge = MERGE_DEFAULT;
764 SymbolsInfo included, next_incl;
766 InitSymbolsInfo(&included, info->keymap, info->file_id);
769 included.name = stmt->stmt;
773 for (; stmt; stmt = stmt->next_incl) {
774 if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_SYMBOLS,
776 info->errorCount += 10;
777 FreeSymbolsInfo(&included);
781 InitSymbolsInfo(&next_incl, info->keymap, rtrn->id);
782 next_incl.merge = next_incl.dflt.merge = MERGE_OVERRIDE;
784 next_incl.explicit_group = atoi(stmt->modifier) - 1;
786 next_incl.explicit_group = info->explicit_group;
788 HandleSymbolsFile(&next_incl, rtrn, MERGE_OVERRIDE);
790 MergeIncludedSymbols(&included, &next_incl, merge);
792 FreeSymbolsInfo(&next_incl);
796 MergeIncludedSymbols(info, &included, merge);
797 FreeSymbolsInfo(&included);
799 return (info->errorCount == 0);
806 GetGroupIndex(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
807 unsigned what, xkb_group_index_t *ndx_rtrn)
816 if (arrayNdx == NULL) {
820 defined = keyi->symsDefined;
822 defined = keyi->actsDefined;
824 for (i = 0; i < XkbNumKbdGroups; i++) {
825 if ((defined & (1 << i)) == 0) {
831 log_err(info->keymap->ctx,
832 "Too many groups of %s for key %s (max %u); "
833 "Ignoring %s defined for extra groups\n",
834 name, longText(keyi->name), XkbNumKbdGroups + 1, name);
838 if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, ndx_rtrn)) {
839 log_err(info->keymap->ctx,
840 "Illegal group index for %s of key %s\n"
841 "Definition with non-integer array index ignored\n",
842 name, longText(keyi->name));
851 AddSymbolsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
854 xkb_group_index_t ndx;
855 size_t nSyms, nLevels;
859 if (!GetGroupIndex(info, keyi, arrayNdx, SYMBOLS, &ndx))
862 keyi->symsDefined |= (1 << ndx);
865 if (value->op != EXPR_KEYSYM_LIST) {
866 log_err(info->keymap->ctx,
867 "Expected a list of symbols, found %s; "
868 "Ignoring symbols for group %u of %s\n",
869 exprOpText(value->op), ndx + 1, longText(keyi->name));
872 if (!darray_empty(keyi->syms[ndx])) {
873 log_err(info->keymap->ctx,
874 "Symbols for key %s, group %u already defined; "
875 "Ignoring duplicate definition\n",
876 longText(keyi->name), ndx + 1);
879 nSyms = darray_size(value->value.list.syms);
880 nLevels = darray_size(value->value.list.symsMapIndex);
881 if ((keyi->numLevels[ndx] < nSyms || darray_empty(keyi->syms[ndx])) &&
882 (!ResizeKeyGroup(keyi, ndx, nLevels, nSyms, false))) {
883 log_wsgo(info->keymap->ctx,
884 "Could not resize group %u of key %s to contain %zu levels; "
886 ndx + 1, longText(keyi->name), nSyms);
889 keyi->symsDefined |= (1 << ndx);
890 for (i = 0; i < nLevels; i++) {
891 darray_item(keyi->symsMapIndex[ndx], i) =
892 darray_item(value->value.list.symsMapIndex, i);
893 darray_item(keyi->symsMapNumEntries[ndx], i) =
894 darray_item(value->value.list.symsNumEntries, i);
896 for (j = 0; j < darray_item(keyi->symsMapNumEntries[ndx], i); j++) {
897 /* FIXME: What's abort() doing here? */
898 if (darray_item(keyi->symsMapIndex[ndx], i) + j >= nSyms)
900 if (!LookupKeysym(darray_item(value->value.list.syms,
901 darray_item(value->value.list.symsMapIndex,
903 &darray_item(keyi->syms[ndx],
904 darray_item(keyi->symsMapIndex[ndx],
906 log_warn(info->keymap->ctx,
907 "Could not resolve keysym %s for key %s, group %u (%s), level %zu\n",
908 darray_item(value->value.list.syms, i),
909 longText(keyi->name),
911 xkb_atom_text(info->keymap->ctx,
912 info->groupNames[ndx]),
915 darray_item(keyi->syms[ndx],
916 darray_item(keyi->symsMapIndex[ndx],
917 i) + j) = XKB_KEY_NoSymbol;
918 darray_item(keyi->symsMapIndex[ndx], i) = -1;
919 darray_item(keyi->symsMapNumEntries[ndx], i) = 0;
922 if (darray_item(keyi->symsMapNumEntries[ndx], i) == 1 &&
923 darray_item(keyi->syms[ndx],
924 darray_item(keyi->symsMapIndex[ndx],
925 i) + j) == XKB_KEY_NoSymbol) {
926 darray_item(keyi->symsMapIndex[ndx], i) = -1;
927 darray_item(keyi->symsMapNumEntries[ndx], i) = 0;
931 for (j = keyi->numLevels[ndx] - 1;
932 j >= 0 && darray_item(keyi->symsMapNumEntries[ndx], j) == 0; j--)
933 keyi->numLevels[ndx]--;
938 AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
942 xkb_group_index_t ndx;
945 struct xkb_any_action *toAct;
947 if (!GetGroupIndex(info, keyi, arrayNdx, ACTIONS, &ndx))
951 keyi->actsDefined |= (1 << ndx);
955 if (value->op != EXPR_ACTION_LIST) {
956 log_wsgo(info->keymap->ctx,
957 "Bad expression type (%d) for action list value; "
958 "Ignoring actions for group %u of %s\n",
959 value->op, ndx, longText(keyi->name));
963 if (!darray_empty(keyi->acts[ndx])) {
964 log_wsgo(info->keymap->ctx,
965 "Actions for key %s, group %u already defined\n",
966 longText(keyi->name), ndx);
970 for (nActs = 0, act = value->value.child; act != NULL; nActs++) {
971 act = (ExprDef *) act->common.next;
975 log_wsgo(info->keymap->ctx,
976 "Action list but not actions in AddActionsToKey\n");
980 if ((keyi->numLevels[ndx] < nActs || darray_empty(keyi->acts[ndx])) &&
981 !ResizeKeyGroup(keyi, ndx, nActs, nActs, true)) {
982 log_wsgo(info->keymap->ctx,
983 "Could not resize group %u of key %s; "
985 ndx, longText(keyi->name));
988 keyi->actsDefined |= (1 << ndx);
990 toAct = (struct xkb_any_action *) darray_mem(keyi->acts[ndx], 0);
991 act = value->value.child;
992 for (i = 0; i < nActs; i++, toAct++) {
993 if (!HandleActionDef(act, info->keymap, toAct, info->action)) {
994 log_err(info->keymap->ctx,
995 "Illegal action definition for %s; "
996 "Action for group %u/level %zu ignored\n",
997 longText(keyi->name), ndx + 1, i + 1);
999 act = (ExprDef *) act->common.next;
1005 static const LookupEntry lockingEntries[] = {
1006 { "true", XkbKB_Lock },
1007 { "yes", XkbKB_Lock },
1008 { "on", XkbKB_Lock },
1009 { "false", XkbKB_Default },
1010 { "no", XkbKB_Default },
1011 { "off", XkbKB_Default },
1012 { "permanent", XkbKB_Lock | XkbKB_Permanent },
1016 static const LookupEntry repeatEntries[] = {
1017 { "true", KEY_REPEAT_YES },
1018 { "yes", KEY_REPEAT_YES },
1019 { "on", KEY_REPEAT_YES },
1020 { "false", KEY_REPEAT_NO },
1021 { "no", KEY_REPEAT_NO },
1022 { "off", KEY_REPEAT_NO },
1023 { "default", KEY_REPEAT_UNDEFINED },
1028 SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
1029 ExprDef *arrayNdx, ExprDef *value)
1032 struct xkb_context *ctx = info->keymap->ctx;
1034 if (istreq(field, "type")) {
1035 xkb_group_index_t ndx;
1038 if (!ExprResolveString(ctx, value, &str))
1039 log_lvl(info->keymap->ctx, 1,
1040 "The type field of a key symbol map must be a string; "
1041 "Ignoring illegal type definition\n");
1043 if (arrayNdx == NULL) {
1044 keyi->dfltType = xkb_atom_intern(ctx, str);
1045 keyi->defined |= KEY_FIELD_TYPE_DFLT;
1047 else if (!ExprResolveGroup(ctx, arrayNdx, &ndx)) {
1048 log_err(info->keymap->ctx,
1049 "Illegal group index for type of key %s; "
1050 "Definition with non-integer array index ignored\n",
1051 longText(keyi->name));
1056 keyi->types[ndx] = xkb_atom_intern(ctx, str);
1057 keyi->typesDefined |= (1 << ndx);
1060 else if (istreq(field, "symbols"))
1061 return AddSymbolsToKey(info, keyi, arrayNdx, value);
1062 else if (istreq(field, "actions"))
1063 return AddActionsToKey(info, keyi, arrayNdx, value);
1064 else if (istreq(field, "vmods") ||
1065 istreq(field, "virtualmods") ||
1066 istreq(field, "virtualmodifiers")) {
1067 xkb_mod_mask_t mask;
1069 ok = ExprResolveVModMask(info->keymap, value, &mask);
1071 keyi->vmodmap = (mask >> 8);
1072 keyi->defined |= KEY_FIELD_VMODMAP;
1075 log_err(info->keymap->ctx,
1076 "Expected a virtual modifier mask, found %s; "
1077 "Ignoring virtual modifiers definition for key %s\n",
1078 exprOpText(value->op), longText(keyi->name));
1081 else if (istreq(field, "locking") ||
1082 istreq(field, "lock") ||
1083 istreq(field, "locks")) {
1086 ok = ExprResolveEnum(ctx, value, &val, lockingEntries);
1088 keyi->behavior.type = val;
1089 keyi->defined |= KEY_FIELD_BEHAVIOR;
1091 else if (istreq(field, "radiogroup") ||
1092 istreq(field, "permanentradiogroup") ||
1093 istreq(field, "allownone")) {
1094 log_err(info->keymap->ctx,
1095 "Radio groups not supported; "
1096 "Ignoring radio group specification for key %s\n",
1097 longText(keyi->name));
1100 else if (istreq_prefix("overlay", field) ||
1101 istreq_prefix("permanentoverlay", field)) {
1102 log_err(info->keymap->ctx,
1103 "Overlays not supported; "
1104 "Ignoring overlay specification for key %s\n",
1105 longText(keyi->name));
1107 else if (istreq(field, "repeating") ||
1108 istreq(field, "repeats") ||
1109 istreq(field, "repeat")) {
1112 ok = ExprResolveEnum(ctx, value, &val, repeatEntries);
1114 log_err(info->keymap->ctx,
1115 "Illegal repeat setting for %s; "
1116 "Non-boolean repeat setting ignored\n",
1117 longText(keyi->name));
1121 keyi->defined |= KEY_FIELD_REPEAT;
1123 else if (istreq(field, "groupswrap") ||
1124 istreq(field, "wrapgroups")) {
1127 if (!ExprResolveBoolean(ctx, value, &set)) {
1128 log_err(info->keymap->ctx,
1129 "Illegal groupsWrap setting for %s; "
1130 "Non-boolean value ignored\n",
1131 longText(keyi->name));
1136 keyi->out_of_range_group_action = XkbWrapIntoRange;
1138 keyi->out_of_range_group_action = XkbClampIntoRange;
1140 keyi->defined |= KEY_FIELD_GROUPINFO;
1142 else if (istreq(field, "groupsclamp") ||
1143 istreq(field, "clampgroups")) {
1146 if (!ExprResolveBoolean(ctx, value, &set)) {
1147 log_err(info->keymap->ctx,
1148 "Illegal groupsClamp setting for %s; "
1149 "Non-boolean value ignored\n",
1150 longText(keyi->name));
1155 keyi->out_of_range_group_action = XkbClampIntoRange;
1157 keyi->out_of_range_group_action = XkbWrapIntoRange;
1159 keyi->defined |= KEY_FIELD_GROUPINFO;
1161 else if (istreq(field, "groupsredirect") ||
1162 istreq(field, "redirectgroups")) {
1163 xkb_group_index_t grp;
1165 if (!ExprResolveGroup(ctx, value, &grp)) {
1166 log_err(info->keymap->ctx,
1167 "Illegal group index for redirect of key %s; "
1168 "Definition with non-integer group ignored\n",
1169 longText(keyi->name));
1173 keyi->out_of_range_group_action = XkbRedirectIntoRange;
1174 keyi->out_of_range_group_number = grp - 1;
1175 keyi->defined |= KEY_FIELD_GROUPINFO;
1178 log_err(info->keymap->ctx,
1179 "Unknown field %s in a symbol interpretation; "
1180 "Definition ignored\n",
1188 SetGroupName(SymbolsInfo *info, ExprDef *arrayNdx, ExprDef *value)
1190 xkb_group_index_t grp;
1194 log_lvl(info->keymap->ctx, 1,
1195 "You must specify an index when specifying a group name; "
1196 "Group name definition without array subscript ignored\n");
1200 if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, &grp)) {
1201 log_err(info->keymap->ctx,
1202 "Illegal index in group name definition; "
1203 "Definition with non-integer array index ignored\n");
1207 if (!ExprResolveString(info->keymap->ctx, value, &name)) {
1208 log_err(info->keymap->ctx,
1209 "Group name must be a string; "
1210 "Illegal name for group %d ignored\n", grp);
1214 info->groupNames[grp - 1 + info->explicit_group] =
1215 xkb_atom_intern(info->keymap->ctx, name);
1221 HandleSymbolsVar(SymbolsInfo *info, VarDef *stmt)
1223 const char *elem, *field;
1227 if (ExprResolveLhs(info->keymap->ctx, stmt->name, &elem, &field,
1229 return 0; /* internal error, already reported */
1230 if (elem && istreq(elem, "key")) {
1231 ret = SetSymbolsField(info, &info->dflt, field, arrayNdx,
1234 else if (!elem && (istreq(field, "name") ||
1235 istreq(field, "groupname"))) {
1236 ret = SetGroupName(info, arrayNdx, stmt->value);
1238 else if (!elem && (istreq(field, "groupswrap") ||
1239 istreq(field, "wrapgroups"))) {
1240 log_err(info->keymap->ctx,
1241 "Global \"groupswrap\" not supported; Ignored\n");
1244 else if (!elem && (istreq(field, "groupsclamp") ||
1245 istreq(field, "clampgroups"))) {
1246 log_err(info->keymap->ctx,
1247 "Global \"groupsclamp\" not supported; Ignored\n");
1250 else if (!elem && (istreq(field, "groupsredirect") ||
1251 istreq(field, "redirectgroups"))) {
1252 log_err(info->keymap->ctx,
1253 "Global \"groupsredirect\" not supported; Ignored\n");
1256 else if (!elem && istreq(field, "allownone")) {
1257 log_err(info->keymap->ctx,
1258 "Radio groups not supported; "
1259 "Ignoring \"allownone\" specification\n");
1263 ret = SetActionField(info->keymap, elem, field, arrayNdx,
1264 stmt->value, &info->action);
1271 HandleSymbolsBody(SymbolsInfo *info, VarDef *def, KeyInfo *keyi)
1274 const char *elem, *field;
1277 for (; def; def = (VarDef *) def->common.next) {
1278 if (def->name && def->name->op == EXPR_FIELD_REF) {
1279 ok = HandleSymbolsVar(info, def);
1284 if (!def->value || def->value->op == EXPR_KEYSYM_LIST)
1291 ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
1296 ok = SetSymbolsField(info, keyi, field, arrayNdx, def->value);
1303 SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi)
1305 xkb_group_index_t group = info->explicit_group;
1310 if ((keyi->typesDefined | keyi->symsDefined | keyi->actsDefined) & ~1) {
1311 xkb_group_index_t i;
1312 log_warn(info->keymap->ctx,
1313 "For the map %s an explicit group specified\n",
1315 log_warn(info->keymap->ctx,
1316 "but key %s has more than one group defined; "
1317 "All groups except first one will be ignored\n",
1318 longText(keyi->name));
1319 for (i = 1; i < XkbNumKbdGroups; i++) {
1320 keyi->numLevels[i] = 0;
1321 darray_free(keyi->syms[i]);
1322 darray_free(keyi->acts[i]);
1326 keyi->typesDefined = keyi->symsDefined = keyi->actsDefined = 1 << group;
1328 keyi->numLevels[group] = keyi->numLevels[0];
1329 keyi->numLevels[0] = 0;
1330 keyi->syms[group] = keyi->syms[0];
1331 darray_init(keyi->syms[0]);
1332 keyi->symsMapIndex[group] = keyi->symsMapIndex[0];
1333 darray_init(keyi->symsMapIndex[0]);
1334 keyi->symsMapNumEntries[group] = keyi->symsMapNumEntries[0];
1335 darray_init(keyi->symsMapNumEntries[0]);
1336 keyi->acts[group] = keyi->acts[0];
1337 darray_init(keyi->acts[0]);
1338 keyi->types[group] = keyi->types[0];
1344 HandleSymbolsDef(SymbolsInfo *info, SymbolsDef *stmt)
1348 InitKeyInfo(&keyi, info->file_id);
1349 CopyKeyInfo(&info->dflt, &keyi, false);
1350 keyi.merge = stmt->merge;
1351 keyi.name = KeyNameToLong(stmt->keyName);
1352 if (!HandleSymbolsBody(info, (VarDef *) stmt->symbols, &keyi)) {
1357 if (!SetExplicitGroup(info, &keyi)) {
1362 if (!AddKeySymbols(info, &keyi)) {
1370 HandleModMapDef(SymbolsInfo *info, ModMapDef *def)
1374 xkb_mod_index_t ndx;
1376 struct xkb_context *ctx = info->keymap->ctx;
1378 if (!LookupModIndex(ctx, NULL, def->modifier, EXPR_TYPE_INT, &ndx)) {
1379 log_err(info->keymap->ctx,
1380 "Illegal modifier map definition; "
1381 "Ignoring map for non-modifier \"%s\"\n",
1382 xkb_atom_text(ctx, def->modifier));
1389 for (key = def->keys; key != NULL; key = (ExprDef *) key->common.next) {
1392 if (key->op == EXPR_VALUE && key->value_type == EXPR_TYPE_KEYNAME) {
1393 tmp.haveSymbol = false;
1394 tmp.u.keyName = KeyNameToLong(key->value.keyName);
1396 else if (ExprResolveKeySym(ctx, key, &sym)) {
1397 tmp.haveSymbol = true;
1401 log_err(info->keymap->ctx,
1402 "Modmap entries may contain only key names or keysyms; "
1403 "Illegal definition for %s modifier ignored\n",
1404 ModIndexText(tmp.modifier));
1408 ok = AddModMapEntry(info, &tmp) && ok;
1414 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge)
1420 info->name = strdup_safe(file->name);
1423 for (stmt = file->defs; stmt; stmt = stmt->next) {
1424 switch (stmt->type) {
1426 ok = HandleIncludeSymbols(info, (IncludeStmt *) stmt);
1429 ok = HandleSymbolsDef(info, (SymbolsDef *) stmt);
1432 ok = HandleSymbolsVar(info, (VarDef *) stmt);
1435 ok = HandleVModDef((VModDef *) stmt, info->keymap, merge,
1439 ok = HandleModMapDef(info, (ModMapDef *) stmt);
1442 log_err(info->keymap->ctx,
1443 "Interpretation files may not include other types; "
1444 "Ignoring %s\n", StmtTypeToString(stmt->type));
1452 if (info->errorCount > 10) {
1453 log_err(info->keymap->ctx, "Abandoning symbols file \"%s\"\n",
1461 * Given a keysym @sym, return a key which generates it, or NULL.
1462 * This is used for example in a modifier map definition, such as:
1463 * modifier_map Lock { Caps_Lock };
1464 * where we want to add the Lock modifier to the modmap of the key
1465 * which matches the keysym Caps_Lock.
1466 * Since there can be many keys which generates the keysym, the key
1467 * is chosen first by lowest group in which the keysym appears, than
1468 * by lowest level and than by lowest key code.
1470 static struct xkb_key *
1471 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
1473 struct xkb_key *key, *ret = NULL;
1474 xkb_group_index_t group, min_group = UINT_MAX;
1475 unsigned int level, min_level = UINT_MAX;
1477 xkb_foreach_key(key, keymap) {
1478 for (group = 0; group < key->num_groups; group++) {
1479 for (level = 0; level < XkbKeyGroupWidth(keymap, key, group);
1481 if (XkbKeyNumSyms(key, group, level) != 1 ||
1482 (XkbKeySymEntry(key, group, level))[0] != sym)
1486 * If the keysym was found in a group or level > 0, we must
1487 * keep looking since we might find a key in which the keysym
1488 * is in a lower group or level.
1490 if (group < min_group ||
1491 (group == min_group && level < min_level)) {
1493 if (group == 0 && level == 0) {
1509 * Find the given name in the keymap->map->types and return its index.
1511 * @param atom The atom to search for.
1512 * @param type_rtrn Set to the index of the name if found.
1514 * @return true if found, false otherwise.
1517 FindNamedType(struct xkb_keymap *keymap, xkb_atom_t atom, unsigned *type_rtrn)
1520 const char *name = xkb_atom_text(keymap->ctx, atom);
1521 struct xkb_key_type *type;
1524 darray_foreach(type, keymap->types) {
1525 if (streq(type->name, name)) {
1536 * Assign a type to the given sym and return the Atom for the type assigned.
1539 * - ONE_LEVEL for width 0/1
1540 * - ALPHABETIC for 2 shift levels, with lower/upercase
1541 * - KEYPAD for keypad keys.
1542 * - TWO_LEVEL for other 2 shift level keys.
1543 * and the same for four level keys.
1545 * @param width Number of sysms in syms.
1546 * @param syms The keysyms for the given key (must be size width).
1547 * @param typeNameRtrn Set to the Atom of the type name.
1549 * @returns true if a type could be found, false otherwise.
1551 * FIXME: I need to take the KeyInfo so I can look at symsMapIndex and
1552 * all that fun stuff rather than just assuming there's always one
1556 FindAutomaticType(struct xkb_keymap *keymap, int width,
1557 const xkb_keysym_t *syms, xkb_atom_t *typeNameRtrn,
1561 if ((width == 1) || (width == 0)) {
1562 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ONE_LEVEL");
1565 else if (width == 2) {
1566 if (syms && xkb_keysym_is_lower(syms[0]) &&
1567 xkb_keysym_is_upper(syms[1])) {
1568 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ALPHABETIC");
1570 else if (syms && (xkb_keysym_is_keypad(syms[0]) ||
1571 xkb_keysym_is_keypad(syms[1]))) {
1572 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "KEYPAD");
1576 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "TWO_LEVEL");
1580 else if (width <= 4) {
1581 if (syms && xkb_keysym_is_lower(syms[0]) &&
1582 xkb_keysym_is_upper(syms[1]))
1583 if (xkb_keysym_is_lower(syms[2]) && xkb_keysym_is_upper(syms[3]))
1585 xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_ALPHABETIC");
1587 *typeNameRtrn = xkb_atom_intern(keymap->ctx,
1588 "FOUR_LEVEL_SEMIALPHABETIC");
1590 else if (syms && (xkb_keysym_is_keypad(syms[0]) ||
1591 xkb_keysym_is_keypad(syms[1])))
1592 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_KEYPAD");
1594 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL");
1595 /* XXX: why not set autoType here? */
1597 return ((width >= 0) && (width <= 4));
1601 * Ensure the given KeyInfo is in a coherent state, i.e. no gaps between the
1602 * groups, and reduce to one group if all groups are identical anyway.
1605 PrepareKeyDef(KeyInfo *keyi)
1607 xkb_group_index_t i, lastGroup;
1608 int j, width, defined;
1611 defined = keyi->symsDefined | keyi->actsDefined | keyi->typesDefined;
1612 /* get highest group number */
1613 for (i = XkbNumKbdGroups - 1; i > 0; i--) {
1614 if (defined & (1 << i))
1622 /* If there are empty groups between non-empty ones fill them with data */
1623 /* from the first group. */
1624 /* We can make a wrong assumption here. But leaving gaps is worse. */
1625 for (i = lastGroup; i > 0; i--) {
1626 if (defined & (1 << i))
1628 width = keyi->numLevels[0];
1629 if (keyi->typesDefined & 1) {
1630 for (j = 0; j < width; j++) {
1631 keyi->types[i] = keyi->types[0];
1633 keyi->typesDefined |= 1 << i;
1635 if ((keyi->actsDefined & 1) && !darray_empty(keyi->acts[0])) {
1636 darray_copy(keyi->acts[i], keyi->acts[0]);
1637 keyi->actsDefined |= 1 << i;
1639 if ((keyi->symsDefined & 1) && !darray_empty(keyi->syms[0])) {
1640 darray_copy(keyi->syms[i], keyi->syms[0]);
1641 darray_copy(keyi->symsMapIndex[i], keyi->symsMapIndex[0]);
1642 darray_copy(keyi->symsMapNumEntries[i],
1643 keyi->symsMapNumEntries[0]);
1644 keyi->symsDefined |= 1 << i;
1647 keyi->numLevels[i] = keyi->numLevels[0];
1650 /* If all groups are completely identical remove them all */
1651 /* exept the first one. */
1653 for (i = lastGroup; i > 0; i--) {
1654 if ((keyi->numLevels[i] != keyi->numLevels[0]) ||
1655 (keyi->types[i] != keyi->types[0])) {
1659 if (!darray_same(keyi->syms[i], keyi->syms[0]) &&
1660 (darray_empty(keyi->syms[i]) || darray_empty(keyi->syms[0]) ||
1661 darray_size(keyi->syms[i]) != darray_size(keyi->syms[0]) ||
1662 memcmp(darray_mem(keyi->syms[i], 0),
1663 darray_mem(keyi->syms[0], 0),
1664 sizeof(xkb_keysym_t) * darray_size(keyi->syms[0])))) {
1668 if (!darray_same(keyi->symsMapIndex[i], keyi->symsMapIndex[0]) &&
1669 (darray_empty(keyi->symsMapIndex[i]) ||
1670 darray_empty(keyi->symsMapIndex[0]) ||
1671 memcmp(darray_mem(keyi->symsMapIndex[i], 0),
1672 darray_mem(keyi->symsMapIndex[0], 0),
1673 keyi->numLevels[0] * sizeof(int)))) {
1677 if (!darray_same(keyi->symsMapNumEntries[i],
1678 keyi->symsMapNumEntries[0]) &&
1679 (darray_empty(keyi->symsMapNumEntries[i]) ||
1680 darray_empty(keyi->symsMapNumEntries[0]) ||
1681 memcmp(darray_mem(keyi->symsMapNumEntries[i], 0),
1682 darray_mem(keyi->symsMapNumEntries[0], 0),
1683 keyi->numLevels[0] * sizeof(size_t)))) {
1687 if (!darray_same(keyi->acts[i], keyi->acts[0]) &&
1688 (darray_empty(keyi->acts[i]) || darray_empty(keyi->acts[0]) ||
1689 memcmp(darray_mem(keyi->acts[i], 0),
1690 darray_mem(keyi->acts[0], 0),
1691 keyi->numLevels[0] * sizeof(union xkb_action)))) {
1697 for (i = lastGroup; i > 0; i--) {
1698 keyi->numLevels[i] = 0;
1699 darray_free(keyi->syms[i]);
1700 darray_free(keyi->symsMapIndex[i]);
1701 darray_free(keyi->symsMapNumEntries[i]);
1702 darray_free(keyi->acts[i]);
1705 keyi->symsDefined &= 1;
1706 keyi->actsDefined &= 1;
1707 keyi->typesDefined &= 1;
1712 * Copy the KeyInfo into the keyboard description.
1714 * This function recurses.
1717 CopySymbolsDef(SymbolsInfo *info, KeyInfo *keyi,
1718 xkb_keycode_t start_from)
1720 struct xkb_keymap *keymap = info->keymap;
1722 struct xkb_key *key;
1723 size_t sizeSyms = 0;
1724 xkb_group_index_t i, nGroups;
1725 unsigned width, tmp;
1726 struct xkb_key_type * type;
1727 bool haveActions, autoType, useAlias;
1728 unsigned types[XkbNumKbdGroups];
1729 union xkb_action *outActs;
1730 unsigned int symIndex = 0;
1732 useAlias = (start_from == 0);
1734 key = FindNamedKey(keymap, keyi->name, useAlias,
1735 CreateKeyNames(keymap), start_from);
1737 if (start_from == 0)
1738 log_lvl(info->keymap->ctx, 5,
1739 "Key %s not found in keycodes; Symbols ignored\n",
1740 longText(keyi->name));
1743 kc = XkbKeyGetKeycode(keymap, key);
1745 haveActions = false;
1747 for (i = nGroups = 0; i < XkbNumKbdGroups; i++) {
1748 if (((i + 1) > nGroups)
1749 && (((keyi->symsDefined | keyi->actsDefined) & (1 << i))
1750 || (keyi->typesDefined) & (1 << i)))
1752 if (!darray_empty(keyi->acts[i]))
1755 /* Assign the type to the key, if it is missing. */
1756 if (keyi->types[i] == XKB_ATOM_NONE) {
1757 if (keyi->dfltType != XKB_ATOM_NONE)
1758 keyi->types[i] = keyi->dfltType;
1759 else if (FindAutomaticType(keymap, keyi->numLevels[i],
1760 darray_mem(keyi->syms[i], 0),
1761 &keyi->types[i], &autoType)) { }
1763 log_lvl(info->keymap->ctx, 5,
1764 "No automatic type for %d symbols; "
1765 "Using %s for the %s key (keycode %d)\n",
1767 xkb_atom_text(keymap->ctx, keyi->types[i]),
1768 longText(keyi->name), kc);
1770 if (FindNamedType(keymap, keyi->types[i], &types[i])) {
1771 if (!autoType || keyi->numLevels[i] > 2)
1772 key->explicit |= (1 << i);
1775 log_lvl(info->keymap->ctx, 3,
1776 "Type \"%s\" is not defined; "
1777 "Using TWO_LEVEL for the %s key (keycode %d)\n",
1778 xkb_atom_text(keymap->ctx, keyi->types[i]),
1779 longText(keyi->name), kc);
1780 types[i] = XkbTwoLevelIndex;
1783 /* if the type specifies fewer levels than the key has, shrink the key */
1784 type = &darray_item(keymap->types, types[i]);
1785 if (type->num_levels < keyi->numLevels[i]) {
1786 log_lvl(info->keymap->ctx, 1,
1787 "Type \"%s\" has %d levels, but %s has %d symbols; "
1788 "Ignoring extra symbols\n",
1789 type->name, type->num_levels,
1790 longText(keyi->name),
1791 keyi->numLevels[i]);
1792 keyi->numLevels[i] = type->num_levels;
1794 if (keyi->numLevels[i] > width)
1795 width = keyi->numLevels[i];
1796 if (type->num_levels > width)
1797 width = type->num_levels;
1798 sizeSyms += darray_size(keyi->syms[i]);
1801 darray_resize0(key->syms, sizeSyms);
1804 outActs = ResizeKeyActions(keymap, key, width * nGroups);
1805 if (outActs == NULL) {
1806 log_wsgo(info->keymap->ctx,
1807 "Could not enlarge actions for %s (key %d)\n",
1808 longText(keyi->name), kc);
1811 key->explicit |= XkbExplicitInterpretMask;
1816 key->num_groups = nGroups;
1817 if (keyi->defined & KEY_FIELD_GROUPINFO) {
1818 key->out_of_range_group_number = keyi->out_of_range_group_number;
1819 key->out_of_range_group_action = keyi->out_of_range_group_action;
1822 key->sym_index = calloc(nGroups * width, sizeof(*key->sym_index));
1823 key->num_syms = calloc(nGroups * width, sizeof(*key->num_syms));
1825 for (i = 0; i < nGroups; i++) {
1826 /* assign kt_index[i] to the index of the type in map->types.
1827 * kt_index[i] may have been set by a previous run (if we have two
1828 * layouts specified). Let's not overwrite it with the ONE_LEVEL
1829 * default group if we dont even have keys for this group anyway.
1831 * FIXME: There should be a better fix for this.
1833 if (keyi->numLevels[i])
1834 key->kt_index[i] = types[i];
1835 if (!darray_empty(keyi->syms[i])) {
1836 /* fill key to "width" symbols*/
1837 for (tmp = 0; tmp < width; tmp++) {
1838 if (tmp < keyi->numLevels[i] &&
1839 darray_item(keyi->symsMapNumEntries[i], tmp) != 0) {
1840 memcpy(darray_mem(key->syms, symIndex),
1841 darray_mem(keyi->syms[i],
1842 darray_item(keyi->symsMapIndex[i], tmp)),
1843 darray_item(keyi->symsMapNumEntries[i],
1844 tmp) * sizeof(xkb_keysym_t));
1845 key->sym_index[(i * width) + tmp] = symIndex;
1846 key->num_syms[(i * width) + tmp] =
1847 darray_item(keyi->symsMapNumEntries[i], tmp);
1848 symIndex += key->num_syms[(i * width) + tmp];
1851 key->sym_index[(i * width) + tmp] = -1;
1852 key->num_syms[(i * width) + tmp] = 0;
1854 if (outActs != NULL && !darray_empty(keyi->acts[i])) {
1855 if (tmp < keyi->numLevels[i])
1856 outActs[tmp] = darray_item(keyi->acts[i], tmp);
1858 outActs[tmp].type = XkbSA_NoAction;
1863 switch (keyi->behavior.type & XkbKB_OpMask) {
1868 key->behavior = keyi->behavior;
1869 key->explicit |= XkbExplicitBehaviorMask;
1872 if (keyi->defined & KEY_FIELD_VMODMAP) {
1873 key->vmodmap = keyi->vmodmap;
1874 key->explicit |= XkbExplicitVModMapMask;
1876 if (keyi->repeat != KEY_REPEAT_UNDEFINED) {
1877 key->repeats = (keyi->repeat == KEY_REPEAT_YES);
1878 key->explicit |= XkbExplicitAutoRepeatMask;
1881 /* do the same thing for the next key */
1882 CopySymbolsDef(info, keyi, kc + 1);
1887 CopyModMapDef(SymbolsInfo *info, ModMapEntry *entry)
1889 struct xkb_key *key;
1890 struct xkb_keymap *keymap = info->keymap;
1892 if (!entry->haveSymbol) {
1893 key = FindNamedKey(keymap, entry->u.keyName, true,
1894 CreateKeyNames(keymap), 0);
1896 log_lvl(info->keymap->ctx, 5,
1897 "Key %s not found in keycodes; "
1898 "Modifier map entry for %s not updated\n",
1899 longText(entry->u.keyName),
1900 ModIndexText(entry->modifier));
1905 key = FindKeyForSymbol(keymap, entry->u.keySym);
1907 log_lvl(info->keymap->ctx, 5,
1908 "Key \"%s\" not found in symbol map; "
1909 "Modifier map entry for %s not updated\n",
1910 KeysymText(entry->u.keySym),
1911 ModIndexText(entry->modifier));
1916 key->modmap |= (1 << entry->modifier);
1921 * Handle the xkb_symbols section of an xkb file.
1923 * @param file The parsed xkb_symbols section of the xkb file.
1924 * @param keymap Handle to the keyboard description to store the symbols in.
1925 * @param merge Merge strategy (e.g. MERGE_OVERRIDE).
1928 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1929 enum merge_mode merge)
1931 xkb_group_index_t i;
1932 struct xkb_key *key;
1937 InitSymbolsInfo(&info, keymap, file->id);
1938 info.dflt.merge = merge;
1940 HandleSymbolsFile(&info, file, merge);
1942 if (darray_empty(info.keys))
1945 if (info.errorCount != 0)
1948 darray_resize0(keymap->acts, darray_size(keymap->acts) + 32 + 1);
1951 keymap->symbols_section_name = strdup(info.name);
1953 for (i = 0; i < XkbNumKbdGroups; i++) {
1954 if (info.groupNames[i] != XKB_ATOM_NONE) {
1955 keymap->group_names[i] = xkb_atom_text(keymap->ctx,
1956 info.groupNames[i]);
1961 darray_foreach(keyi, info.keys)
1962 PrepareKeyDef(keyi);
1965 darray_foreach(keyi, info.keys)
1966 if (!CopySymbolsDef(&info, keyi, 0))
1969 if (xkb_get_log_verbosity(keymap->ctx) > 3) {
1970 xkb_foreach_key(key, keymap) {
1971 if (key->name[0] == '\0')
1974 if (key->num_groups < 1)
1975 log_warn(info.keymap->ctx,
1976 "No symbols defined for <%.4s> (keycode %d)\n",
1977 key->name, XkbKeyGetKeycode(keymap, key));
1981 list_foreach(mm, &info.modMaps, entry)
1982 if (!CopyModMapDef(&info, mm))
1985 FreeSymbolsInfo(&info);
1989 FreeSymbolsInfo(&info);