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 ********************************************************/
27 #include "xkbcomp-priv.h"
28 #include "parseutils.h"
34 /***====================================================================***/
38 #define RepeatUndefined ~((unsigned)0)
40 #define _Key_Syms (1<<0)
41 #define _Key_Acts (1<<1)
42 #define _Key_Repeat (1<<2)
43 #define _Key_Behavior (1<<3)
44 #define _Key_Type_Dflt (1<<4)
45 #define _Key_Types (1<<5)
46 #define _Key_GroupInfo (1<<6)
47 #define _Key_VModMap (1<<7)
49 typedef struct _KeyInfo
52 unsigned long name; /* the 4 chars of the key name, as long */
53 unsigned char groupInfo;
54 unsigned char typesDefined;
55 unsigned char symsDefined;
56 unsigned char actsDefined;
57 unsigned int numLevels[XkbNumKbdGroups];
59 /* syms[group] -> Single array for all the keysyms in the group. */
60 xkb_keysym_t *syms[XkbNumKbdGroups];
61 /* sizeSyms[group] -> The size of the syms[group] array. */
62 int sizeSyms[XkbNumKbdGroups];
64 * symsMapIndex[group][level] -> The index from which the syms for
65 * the level begin in the syms[group] array. Remember each keycode
66 * can have multiple keysyms in each level (that is, each key press
67 * can result in multiple keysyms).
69 int *symsMapIndex[XkbNumKbdGroups];
71 * symsMapNumEntries[group][level] -> How many syms are in
72 * syms[group][symsMapIndex[group][level]].
74 unsigned int *symsMapNumEntries[XkbNumKbdGroups];
76 union xkb_action *acts[XkbNumKbdGroups];
77 xkb_atom_t types[XkbNumKbdGroups];
79 struct xkb_behavior behavior;
80 unsigned short vmodmap;
85 * Init the given key info to sane values.
88 InitKeyInfo(KeyInfo * info)
91 static const char dflt[4] = "*";
93 info->defs.defined = 0;
94 info->defs.fileID = 0;
95 info->defs.merge = MergeOverride;
96 info->defs.next = NULL;
97 info->name = KeyNameToLong(dflt);
99 info->typesDefined = info->symsDefined = info->actsDefined = 0;
100 for (i = 0; i < XkbNumKbdGroups; i++)
102 info->numLevels[i] = 0;
103 info->types[i] = XKB_ATOM_NONE;
104 info->syms[i] = NULL;
105 info->sizeSyms[i] = 0;
106 info->symsMapIndex[i] = NULL;
107 info->symsMapNumEntries[i] = NULL;
108 info->acts[i] = NULL;
110 info->dfltType = XKB_ATOM_NONE;
111 info->behavior.type = XkbKB_Default;
112 info->behavior.data = 0;
114 info->repeat = RepeatUndefined;
118 * Free memory associated with this key info and reset to sane values.
121 FreeKeyInfo(KeyInfo * info)
125 info->defs.defined = 0;
126 info->defs.fileID = 0;
127 info->defs.merge = MergeOverride;
128 info->defs.next = NULL;
130 info->typesDefined = info->symsDefined = info->actsDefined = 0;
131 for (i = 0; i < XkbNumKbdGroups; i++)
133 info->numLevels[i] = 0;
134 info->types[i] = XKB_ATOM_NONE;
136 info->syms[i] = NULL;
137 info->sizeSyms[i] = 0;
138 free(info->symsMapIndex[i]);
139 info->symsMapIndex[i] = NULL;
140 free(info->symsMapNumEntries[i]);
141 info->symsMapNumEntries[i] = NULL;
143 info->acts[i] = NULL;
145 info->dfltType = XKB_ATOM_NONE;
146 info->behavior.type = XkbKB_Default;
147 info->behavior.data = 0;
149 info->repeat = RepeatUndefined;
153 * Copy old into new, optionally reset old to 0.
154 * If old is reset, new simply re-uses old's memory. Otherwise, the memory is
155 * newly allocated and new points to the new memory areas.
158 CopyKeyInfo(KeyInfo * old, KeyInfo * new, bool clearOld)
163 new->defs.next = NULL;
166 for (i = 0; i < XkbNumKbdGroups; i++)
168 old->numLevels[i] = 0;
169 old->symsMapIndex[i] = NULL;
170 old->symsMapNumEntries[i] = NULL;
172 old->sizeSyms[i] = 0;
179 for (i = 0; i < XkbNumKbdGroups; i++)
181 width = new->numLevels[i];
182 if (old->syms[i] != NULL)
184 new->syms[i] = uTypedCalloc(new->sizeSyms[i], xkb_keysym_t);
188 new->sizeSyms[i] = 0;
189 new->numLevels[i] = 0;
193 memcpy(new->syms[i], old->syms[i],
194 new->sizeSyms[i] * sizeof(xkb_keysym_t));
195 new->symsMapIndex[i] = uTypedCalloc(width, int);
196 if (!new->symsMapIndex[i])
200 new->sizeSyms[i] = 0;
201 new->numLevels[i] = 0;
205 memcpy(new->symsMapIndex[i], old->symsMapIndex[i],
206 width * sizeof(int));
207 new->symsMapNumEntries[i] = uTypedCalloc(width, unsigned int);
208 if (!new->symsMapNumEntries[i])
212 new->sizeSyms[i] = 0;
213 free(new->symsMapIndex[i]);
214 new->symsMapIndex[i] = NULL;
215 new->numLevels[i] = 0;
219 memcpy(new->symsMapNumEntries[i], old->symsMapNumEntries[i],
220 width * sizeof(unsigned int));
222 if (old->acts[i] != NULL)
224 new->acts[i] = uTypedCalloc(width, union xkb_action);
229 new->sizeSyms[i] = 0;
230 free(new->symsMapIndex[i]);
231 new->symsMapIndex[i] = NULL;
232 free(new->symsMapNumEntries[i]);
233 new->symsMapNumEntries[i] = NULL;
234 new->numLevels[i] = 0;
237 memcpy(new->acts[i], old->acts[i],
238 width * sizeof(union xkb_action));
245 /***====================================================================***/
247 typedef struct _ModMapEntry
254 unsigned long keyName;
259 #define SYMBOLS_INIT_SIZE 110
261 typedef struct _SymbolsInfo
263 char *name; /* e.g. pc+us+inet(evdev) */
267 unsigned explicit_group;
269 darray(KeyInfo) keys;
273 xkb_atom_t groupNames[XkbNumKbdGroups];
280 InitSymbolsInfo(SymbolsInfo * info, struct xkb_keymap *keymap)
285 info->explicit_group = 0;
286 info->errorCount = 0;
288 info->merge = MergeOverride;
290 darray_init(info->keys);
291 darray_growalloc(info->keys, SYMBOLS_INIT_SIZE);
293 for (i = 0; i < XkbNumKbdGroups; i++)
294 info->groupNames[i] = XKB_ATOM_NONE;
295 InitKeyInfo(&info->dflt);
296 InitVModInfo(&info->vmods, keymap);
298 info->aliases = NULL;
302 FreeSymbolsInfo(SymbolsInfo * info)
307 darray_foreach(key, info->keys)
309 darray_free(info->keys);
311 ClearCommonInfo(&info->modMap->defs);
313 ClearAliases(&info->aliases);
314 memset(info, 0, sizeof(SymbolsInfo));
318 ResizeKeyGroup(KeyInfo * key, unsigned int group, unsigned int numLevels,
319 unsigned sizeSyms, bool forceActions)
323 if (key->syms[group] == NULL || key->sizeSyms[group] < sizeSyms)
325 key->syms[group] = uTypedRecalloc(key->syms[group],
326 key->sizeSyms[group],
329 if (!key->syms[group]) {
330 key->sizeSyms[group] = 0;
333 key->sizeSyms[group] = sizeSyms;
335 if (!key->symsMapIndex[group] || key->numLevels[group] < numLevels)
337 key->symsMapIndex[group] = uTypedRealloc(key->symsMapIndex[group],
340 if (!key->symsMapIndex[group])
342 for (i = key->numLevels[group]; i < numLevels; i++)
343 key->symsMapIndex[group][i] = -1;
345 if (!key->symsMapNumEntries[group] || key->numLevels[group] < numLevels)
347 key->symsMapNumEntries[group] =
348 uTypedRecalloc(key->symsMapNumEntries[group],
349 key->numLevels[group],
352 if (!key->symsMapNumEntries[group])
356 (key->numLevels[group] < numLevels || (key->acts[group] == NULL))) ||
357 (key->numLevels[group] < numLevels && (key->acts[group] != NULL)))
359 key->acts[group] = uTypedRecalloc(key->acts[group],
360 key->numLevels[group],
363 if (!key->acts[group])
366 if (key->numLevels[group] < numLevels)
367 key->numLevels[group] = numLevels;
372 MergeKeyGroups(SymbolsInfo * info,
373 KeyInfo * into, KeyInfo * from, unsigned group)
375 xkb_keysym_t *resultSyms = NULL;
376 union xkb_action *resultActs;
377 unsigned int resultWidth;
378 unsigned int resultSize = 0;
381 bool report, clobber;
383 clobber = (from->defs.merge != MergeAugment);
384 report = (warningLevel > 9) ||
385 ((into->defs.fileID == from->defs.fileID) && (warningLevel > 0));
386 if (into->numLevels[group] >= from->numLevels[group])
388 resultActs = into->acts[group];
389 resultWidth = into->numLevels[group];
393 resultActs = from->acts[group];
394 resultWidth = from->numLevels[group];
395 into->symsMapIndex[group] = uTypedRealloc(into->symsMapIndex[group],
396 from->numLevels[group],
398 into->symsMapNumEntries[group] =
399 uTypedRecalloc(into->symsMapNumEntries[group],
400 into->numLevels[group],
401 from->numLevels[group],
403 if (!into->symsMapIndex[group] || !into->symsMapNumEntries[group])
405 WSGO("Could not allocate level indices for key info merge\n");
406 ACTION("Group %d of key %s not merged\n", group,
407 longText(into->name));
411 for (i = into->numLevels[group]; i < from->numLevels[group]; i++)
412 into->symsMapIndex[group][i] = -1;
415 if ((resultActs == NULL) && (into->acts[group] || from->acts[group]))
417 resultActs = uTypedCalloc(resultWidth, union xkb_action);
420 WSGO("Could not allocate actions for group merge\n");
421 ACTION("Group %d of key %s not merged\n", group,
422 longText(into->name));
425 for (i = 0; i < resultWidth; i++)
427 union xkb_action *fromAct, *toAct;
428 fromAct = (from->acts[group] ? &from->acts[group][i] : NULL);
429 toAct = (into->acts[group] ? &into->acts[group][i] : NULL);
430 if (((fromAct == NULL) || (fromAct->type == XkbSA_NoAction))
433 resultActs[i] = *toAct;
435 else if (((toAct == NULL) || (toAct->type == XkbSA_NoAction))
436 && (fromAct != NULL))
438 resultActs[i] = *fromAct;
442 union xkb_action *use, *ignore;
456 ("Multiple actions for level %d/group %d on key %s\n",
457 i + 1, group + 1, longText(into->name));
458 ACTION("Using %s, ignoring %s\n",
459 XkbcActionTypeText(use->type),
460 XkbcActionTypeText(ignore->type));
463 resultActs[i] = *use;
468 for (i = 0; i < resultWidth; i++)
470 unsigned int fromSize = 0;
473 if (from->symsMapNumEntries[group] && (i < from->numLevels[group]))
474 fromSize = from->symsMapNumEntries[group][i];
475 if (into->symsMapNumEntries[group] && (i < into->numLevels[group]))
476 toSize = into->symsMapNumEntries[group][i];
478 if (fromSize == 0 || fromSize == toSize || clobber)
482 else if (toSize == 0)
484 resultSize += fromSize;
491 resultSyms = uTypedCalloc(resultSize, xkb_keysym_t);
494 WSGO("Could not allocate symbols for group merge\n");
495 ACTION("Group %d of key %s not merged\n", group, longText(into->name));
499 for (i = 0; i < resultWidth; i++)
501 enum { NONE, FROM, TO } use;
502 unsigned int fromSize = 0;
503 unsigned int toSize = 0;
505 if (from->symsMapNumEntries[group] && (i < from->numLevels[group]))
506 fromSize = from->symsMapNumEntries[group][i];
507 if (into->symsMapNumEntries[group] && (i < into->numLevels[group]))
508 toSize = into->symsMapNumEntries[group][i];
510 if (!fromSize && !toSize)
512 into->symsMapIndex[group][i] = -1;
513 into->symsMapNumEntries[group][i] = 0;
517 if ((fromSize && !toSize) || clobber)
522 if (toSize && fromSize && report)
524 INFO("Multiple symbols for group %d, level %d on key %s\n",
525 group + 1, i + 1, longText(into->name));
526 ACTION("Using %s, ignoring %s\n",
527 (use == FROM ? "from" : "to"),
528 (use == FROM ? "to" : "from"));
533 memcpy(&resultSyms[cur_idx],
534 &from->syms[group][from->symsMapIndex[group][i]],
535 from->symsMapNumEntries[group][i] * sizeof(xkb_keysym_t));
536 into->symsMapIndex[group][i] = cur_idx;
537 into->symsMapNumEntries[group][i] =
538 from->symsMapNumEntries[group][i];
542 memcpy(&resultSyms[cur_idx],
543 &into->syms[group][from->symsMapIndex[group][i]],
544 into->symsMapNumEntries[group][i] * sizeof(xkb_keysym_t));
545 into->symsMapIndex[group][i] = cur_idx;
547 cur_idx += into->symsMapNumEntries[group][i];
551 if (resultActs != into->acts[group])
552 free(into->acts[group]);
553 if (resultActs != from->acts[group])
554 free(from->acts[group]);
555 into->numLevels[group] = resultWidth;
556 free(into->syms[group]);
557 into->syms[group] = resultSyms;
558 free(from->syms[group]);
559 from->syms[group] = NULL;
560 from->sizeSyms[group] = 0;
561 into->sizeSyms[group] = resultSize;
562 free(from->symsMapIndex[group]);
563 from->symsMapIndex[group] = NULL;
564 free(from->symsMapNumEntries[group]);
565 from->symsMapNumEntries[group] = NULL;
566 into->acts[group] = resultActs;
567 from->acts[group] = NULL;
568 into->symsDefined |= (1 << group);
569 from->symsDefined &= ~(1 << group);
570 into->actsDefined |= (1 << group);
571 from->actsDefined &= ~(1 << group);
577 MergeKeys(SymbolsInfo *info, struct xkb_keymap *keymap,
578 KeyInfo *into, KeyInfo *from)
581 unsigned collide = 0;
584 if (from->defs.merge == MergeReplace)
586 for (i = 0; i < XkbNumKbdGroups; i++)
588 if (into->numLevels[i] != 0)
595 memset(from, 0, sizeof(KeyInfo));
598 report = ((warningLevel > 9) ||
599 ((into->defs.fileID == from->defs.fileID)
600 && (warningLevel > 0)));
601 for (i = 0; i < XkbNumKbdGroups; i++)
603 if (from->numLevels[i] > 0)
605 if (into->numLevels[i] == 0)
607 into->numLevels[i] = from->numLevels[i];
608 into->syms[i] = from->syms[i];
609 into->sizeSyms[i] = from->sizeSyms[i];
610 into->symsMapIndex[i] = from->symsMapIndex[i];
611 into->symsMapNumEntries[i] = from->symsMapNumEntries[i];
612 into->acts[i] = from->acts[i];
613 into->symsDefined |= (1 << i);
614 from->syms[i] = NULL;
615 from->sizeSyms[i] = 0;
616 from->symsMapIndex[i] = NULL;
617 from->symsMapNumEntries[i] = NULL;
618 from->acts[i] = NULL;
619 from->numLevels[i] = 0;
620 from->symsDefined &= ~(1 << i);
622 into->defs.defined |= _Key_Syms;
624 into->defs.defined |= _Key_Acts;
631 collide |= _Key_Syms;
633 collide |= _Key_Acts;
635 MergeKeyGroups(info, into, from, (unsigned) i);
638 if (from->types[i] != XKB_ATOM_NONE)
640 if ((into->types[i] != XKB_ATOM_NONE) && report &&
641 (into->types[i] != from->types[i]))
643 xkb_atom_t use, ignore;
644 collide |= _Key_Types;
645 if (from->defs.merge != MergeAugment)
647 use = from->types[i];
648 ignore = into->types[i];
652 use = into->types[i];
653 ignore = from->types[i];
656 ("Multiple definitions for group %d type of key %s\n",
657 i, longText(into->name));
658 ACTION("Using %s, ignoring %s\n",
659 xkb_atom_text(keymap->ctx, use),
660 xkb_atom_text(keymap->ctx, ignore));
662 if ((from->defs.merge != MergeAugment)
663 || (into->types[i] == XKB_ATOM_NONE))
665 into->types[i] = from->types[i];
669 if (UseNewField(_Key_Behavior, &into->defs, &from->defs, &collide))
671 into->behavior = from->behavior;
672 into->defs.defined |= _Key_Behavior;
674 if (UseNewField(_Key_VModMap, &into->defs, &from->defs, &collide))
676 into->vmodmap = from->vmodmap;
677 into->defs.defined |= _Key_VModMap;
679 if (UseNewField(_Key_Repeat, &into->defs, &from->defs, &collide))
681 into->repeat = from->repeat;
682 into->defs.defined |= _Key_Repeat;
684 if (UseNewField(_Key_Type_Dflt, &into->defs, &from->defs, &collide))
686 into->dfltType = from->dfltType;
687 into->defs.defined |= _Key_Type_Dflt;
689 if (UseNewField(_Key_GroupInfo, &into->defs, &from->defs, &collide))
691 into->groupInfo = from->groupInfo;
692 into->defs.defined |= _Key_GroupInfo;
696 WARN("Symbol map for key %s redefined\n",
697 longText(into->name));
698 ACTION("Using %s definition for conflicting fields\n",
699 (from->defs.merge == MergeAugment ? "first" : "last"));
705 AddKeySymbols(SymbolsInfo *info, KeyInfo *key, struct xkb_keymap *keymap)
707 unsigned long real_name;
710 darray_foreach(iter, info->keys)
711 if (iter->name == key->name)
712 return MergeKeys(info, keymap, iter, key);
714 if (FindKeyNameForAlias(keymap, key->name, &real_name))
715 darray_foreach(iter, info->keys)
716 if (iter->name == real_name)
717 return MergeKeys(info, keymap, iter, key);
719 darray_resize0(info->keys, darray_size(info->keys) + 1);
720 new = &darray_item(info->keys, darray_size(info->keys) - 1);
721 return CopyKeyInfo(key, new, true);
725 AddModMapEntry(SymbolsInfo * info, ModMapEntry * new)
730 clobber = (new->defs.merge != MergeAugment);
731 for (mm = info->modMap; mm != NULL; mm = (ModMapEntry *) mm->defs.next)
733 if (new->haveSymbol && mm->haveSymbol
734 && (new->u.keySym == mm->u.keySym))
736 unsigned use, ignore;
737 if (mm->modifier != new->modifier)
742 ignore = mm->modifier;
747 ignore = new->modifier;
750 ("%s added to symbol map for multiple modifiers\n",
751 XkbcKeysymText(new->u.keySym));
752 ACTION("Using %s, ignoring %s.\n",
753 XkbcModIndexText(use),
754 XkbcModIndexText(ignore));
759 if ((!new->haveSymbol) && (!mm->haveSymbol) &&
760 (new->u.keyName == mm->u.keyName))
762 unsigned use, ignore;
763 if (mm->modifier != new->modifier)
768 ignore = mm->modifier;
773 ignore = new->modifier;
775 ERROR("Key %s added to map for multiple modifiers\n",
776 longText(new->u.keyName));
777 ACTION("Using %s, ignoring %s.\n",
778 XkbcModIndexText(use),
779 XkbcModIndexText(ignore));
785 mm = uTypedAlloc(ModMapEntry);
788 WSGO("Could not allocate modifier map entry\n");
789 ACTION("Modifier map for %s will be incomplete\n",
790 XkbcModIndexText(new->modifier));
794 mm->defs.next = &info->modMap->defs;
799 /***====================================================================***/
802 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
803 unsigned merge, struct xkb_keymap *keymap)
808 if (from->errorCount > 0)
810 into->errorCount += from->errorCount;
813 if (into->name == NULL)
815 into->name = from->name;
818 for (i = 0; i < XkbNumKbdGroups; i++)
820 if (from->groupNames[i] != XKB_ATOM_NONE)
822 if ((merge != MergeAugment) ||
823 (into->groupNames[i] == XKB_ATOM_NONE))
824 into->groupNames[i] = from->groupNames[i];
828 darray_foreach(key, from->keys) {
829 if (merge != MergeDefault)
830 key->defs.merge = merge;
832 if (!AddKeySymbols(into, key, keymap))
836 if (from->modMap != NULL)
838 ModMapEntry *mm, *next;
839 for (mm = from->modMap; mm != NULL; mm = next)
841 if (merge != MergeDefault)
842 mm->defs.merge = merge;
843 if (!AddModMapEntry(into, mm))
845 next = (ModMapEntry *) mm->defs.next;
850 if (!MergeAliases(&into->aliases, &from->aliases, merge))
855 HandleSymbolsFile(XkbFile *file, struct xkb_keymap *keymap,
856 unsigned merge, SymbolsInfo *info);
859 HandleIncludeSymbols(IncludeStmt *stmt, struct xkb_keymap *keymap,
864 SymbolsInfo included;
868 if ((stmt->file == NULL) && (stmt->map == NULL))
872 memset(info, 0, sizeof(SymbolsInfo));
874 else if (ProcessIncludeFile(keymap->ctx, stmt, XkmSymbolsIndex, &rtrn,
877 InitSymbolsInfo(&included, keymap);
878 included.fileID = included.dflt.defs.fileID = rtrn->id;
879 included.merge = included.dflt.defs.merge = MergeOverride;
882 included.explicit_group = atoi(stmt->modifier) - 1;
886 included.explicit_group = info->explicit_group;
888 HandleSymbolsFile(rtrn, keymap, MergeOverride, &included);
889 if (stmt->stmt != NULL)
892 included.name = stmt->stmt;
899 info->errorCount += 10;
902 if ((stmt->next != NULL) && (included.errorCount < 1))
906 SymbolsInfo next_incl;
908 for (next = stmt->next; next != NULL; next = next->next)
910 if ((next->file == NULL) && (next->map == NULL))
913 MergeIncludedSymbols(&included, info, next->merge, keymap);
914 FreeSymbolsInfo(info);
916 else if (ProcessIncludeFile(keymap->ctx, next, XkmSymbolsIndex,
919 InitSymbolsInfo(&next_incl, keymap);
920 next_incl.fileID = next_incl.dflt.defs.fileID = rtrn->id;
921 next_incl.merge = next_incl.dflt.defs.merge = MergeOverride;
924 next_incl.explicit_group = atoi(next->modifier) - 1;
928 next_incl.explicit_group = info->explicit_group;
930 HandleSymbolsFile(rtrn, keymap, MergeOverride, &next_incl);
931 MergeIncludedSymbols(&included, &next_incl, op, keymap);
932 FreeSymbolsInfo(&next_incl);
937 info->errorCount += 10;
938 FreeSymbolsInfo(&included);
945 info->errorCount += included.errorCount;
951 MergeIncludedSymbols(info, &included, newMerge, keymap);
952 FreeSymbolsInfo(&included);
954 return (info->errorCount == 0);
961 GetGroupIndex(KeyInfo *key, struct xkb_keymap *keymap,
962 ExprDef * arrayNdx, unsigned what, unsigned *ndx_rtrn)
972 if (arrayNdx == NULL)
977 defined = key->symsDefined;
979 defined = key->actsDefined;
981 for (i = 0; i < XkbNumKbdGroups; i++)
983 if ((defined & (1 << i)) == 0)
989 ERROR("Too many groups of %s for key %s (max %d)\n", name,
990 longText(key->name), XkbNumKbdGroups + 1);
991 ACTION("Ignoring %s defined for extra groups\n", name);
994 if (!ExprResolveGroup(keymap->ctx, arrayNdx, &tmp))
996 ERROR("Illegal group index for %s of key %s\n", name,
997 longText(key->name));
998 ACTION("Definition with non-integer array index ignored\n");
1001 *ndx_rtrn = tmp.uval - 1;
1006 AddSymbolsToKey(KeyInfo *key, struct xkb_keymap *keymap,
1007 ExprDef *arrayNdx, ExprDef *value, SymbolsInfo *info)
1009 unsigned ndx, nSyms, nLevels;
1013 if (!GetGroupIndex(key, keymap, arrayNdx, SYMBOLS, &ndx))
1017 key->symsDefined |= (1 << ndx);
1020 if (value->op != ExprKeysymList)
1022 ERROR("Expected a list of symbols, found %s\n", exprOpText(value->op));
1023 ACTION("Ignoring symbols for group %d of %s\n", ndx + 1,
1024 longText(key->name));
1027 if (key->sizeSyms[ndx] != 0)
1029 ERROR("Symbols for key %s, group %d already defined\n",
1030 longText(key->name), ndx + 1);
1031 ACTION("Ignoring duplicate definition\n");
1034 nSyms = darray_size(value->value.list.syms);
1035 nLevels = darray_size(value->value.list.symsMapIndex);
1036 if (((key->numLevels[ndx] < nSyms) || (key->syms[ndx] == NULL)) &&
1037 (!ResizeKeyGroup(key, ndx, nLevels, nSyms, false)))
1039 WSGO("Could not resize group %d of key %s to contain %d levels\n",
1040 ndx + 1, longText(key->name), nSyms);
1041 ACTION("Symbols lost\n");
1044 key->symsDefined |= (1 << ndx);
1045 for (i = 0; i < nLevels; i++) {
1046 key->symsMapIndex[ndx][i] =
1047 darray_item(value->value.list.symsMapIndex, i);
1048 key->symsMapNumEntries[ndx][i] =
1049 darray_item(value->value.list.symsNumEntries, i);
1051 for (j = 0; j < key->symsMapNumEntries[ndx][i]; j++) {
1052 if (key->symsMapIndex[ndx][i] + j >= nSyms)
1054 if (!LookupKeysym(darray_item(value->value.list.syms,
1055 darray_item(value->value.list.symsMapIndex, i) + j),
1056 &key->syms[ndx][key->symsMapIndex[ndx][i] + j])) {
1057 WARN("Could not resolve keysym %s for key %s, group %d (%s), level %d\n",
1058 darray_item(value->value.list.syms, i),
1059 longText(key->name),
1061 xkb_atom_text(keymap->ctx, info->groupNames[ndx]), nSyms);
1063 key->syms[ndx][key->symsMapIndex[ndx][i] + j] = XKB_KEY_NoSymbol;
1064 key->symsMapIndex[ndx][i] = -1;
1065 key->symsMapNumEntries[ndx][i] = 0;
1070 for (j = key->numLevels[ndx] - 1;
1071 j >= 0 && key->symsMapNumEntries[ndx][j] == 0; j--)
1072 key->numLevels[ndx]--;
1077 AddActionsToKey(KeyInfo *key, struct xkb_keymap *keymap, ExprDef *arrayNdx,
1078 ExprDef *value, SymbolsInfo *info)
1081 unsigned ndx, nActs;
1083 struct xkb_any_action *toAct;
1085 if (!GetGroupIndex(key, keymap, arrayNdx, ACTIONS, &ndx))
1090 key->actsDefined |= (1 << ndx);
1093 if (value->op != ExprActionList)
1095 WSGO("Bad expression type (%d) for action list value\n", value->op);
1096 ACTION("Ignoring actions for group %d of %s\n", ndx,
1097 longText(key->name));
1100 if (key->acts[ndx] != NULL)
1102 WSGO("Actions for key %s, group %d already defined\n",
1103 longText(key->name), ndx);
1106 for (nActs = 0, act = value->value.child; act != NULL; nActs++)
1108 act = (ExprDef *) act->common.next;
1112 WSGO("Action list but not actions in AddActionsToKey\n");
1115 if (((key->numLevels[ndx] < nActs) || (key->acts[ndx] == NULL)) &&
1116 (!ResizeKeyGroup(key, ndx, nActs, nActs, true)))
1118 WSGO("Could not resize group %d of key %s\n", ndx,
1119 longText(key->name));
1120 ACTION("Actions lost\n");
1123 key->actsDefined |= (1 << ndx);
1125 toAct = (struct xkb_any_action *) key->acts[ndx];
1126 act = value->value.child;
1127 for (i = 0; i < nActs; i++, toAct++)
1129 if (!HandleActionDef(act, keymap, toAct, info->action))
1131 ERROR("Illegal action definition for %s\n",
1132 longText(key->name));
1133 ACTION("Action for group %d/level %d ignored\n", ndx + 1, i + 1);
1135 act = (ExprDef *) act->common.next;
1140 static const LookupEntry lockingEntries[] = {
1141 {"true", XkbKB_Lock},
1142 {"yes", XkbKB_Lock},
1144 {"false", XkbKB_Default},
1145 {"no", XkbKB_Default},
1146 {"off", XkbKB_Default},
1147 {"permanent", XkbKB_Lock | XkbKB_Permanent},
1151 static const LookupEntry repeatEntries[] = {
1152 {"true", RepeatYes},
1155 {"false", RepeatNo},
1158 {"default", RepeatUndefined},
1163 SetSymbolsField(KeyInfo *key, struct xkb_keymap *keymap, char *field,
1164 ExprDef *arrayNdx, ExprDef *value, SymbolsInfo *info)
1169 if (strcasecmp(field, "type") == 0)
1172 if ((!ExprResolveString(keymap->ctx, value, &tmp))
1173 && (warningLevel > 0))
1175 WARN("The type field of a key symbol map must be a string\n");
1176 ACTION("Ignoring illegal type definition\n");
1178 if (arrayNdx == NULL)
1180 key->dfltType = xkb_atom_intern(keymap->ctx, tmp.str);
1181 key->defs.defined |= _Key_Type_Dflt;
1183 else if (!ExprResolveGroup(keymap->ctx, arrayNdx, &ndx))
1185 ERROR("Illegal group index for type of key %s\n",
1186 longText(key->name));
1187 ACTION("Definition with non-integer array index ignored\n");
1193 key->types[ndx.uval - 1] = xkb_atom_intern(keymap->ctx, tmp.str);
1194 key->typesDefined |= (1 << (ndx.uval - 1));
1198 else if (strcasecmp(field, "symbols") == 0)
1199 return AddSymbolsToKey(key, keymap, arrayNdx, value, info);
1200 else if (strcasecmp(field, "actions") == 0)
1201 return AddActionsToKey(key, keymap, arrayNdx, value, info);
1202 else if ((strcasecmp(field, "vmods") == 0) ||
1203 (strcasecmp(field, "virtualmods") == 0) ||
1204 (strcasecmp(field, "virtualmodifiers") == 0))
1206 ok = ExprResolveVModMask(value, &tmp, keymap);
1209 key->vmodmap = (tmp.uval >> 8);
1210 key->defs.defined |= _Key_VModMap;
1214 ERROR("Expected a virtual modifier mask, found %s\n",
1215 exprOpText(value->op));
1216 ACTION("Ignoring virtual modifiers definition for key %s\n",
1217 longText(key->name));
1220 else if ((strcasecmp(field, "locking") == 0) ||
1221 (strcasecmp(field, "lock") == 0) ||
1222 (strcasecmp(field, "locks") == 0))
1224 ok = ExprResolveEnum(keymap->ctx, value, &tmp, lockingEntries);
1226 key->behavior.type = tmp.uval;
1227 key->defs.defined |= _Key_Behavior;
1229 else if ((strcasecmp(field, "radiogroup") == 0) ||
1230 (strcasecmp(field, "permanentradiogroup") == 0) ||
1231 (strcasecmp(field, "allownone") == 0))
1233 ERROR("Radio groups not supported\n");
1234 ACTION("Ignoring radio group specification for key %s\n", longText(key->name));
1237 else if (uStrCasePrefix("overlay", field) ||
1238 uStrCasePrefix("permanentoverlay", field))
1240 ERROR("Overlays not supported\n");
1241 ACTION("Ignoring overlay specification for key %s\n", longText(key->name));
1243 else if ((strcasecmp(field, "repeating") == 0) ||
1244 (strcasecmp(field, "repeats") == 0) ||
1245 (strcasecmp(field, "repeat") == 0))
1247 ok = ExprResolveEnum(keymap->ctx, value, &tmp, repeatEntries);
1250 ERROR("Illegal repeat setting for %s\n",
1251 longText(key->name));
1252 ACTION("Non-boolean repeat setting ignored\n");
1255 key->repeat = tmp.uval;
1256 key->defs.defined |= _Key_Repeat;
1258 else if ((strcasecmp(field, "groupswrap") == 0) ||
1259 (strcasecmp(field, "wrapgroups") == 0))
1261 ok = ExprResolveBoolean(keymap->ctx, value, &tmp);
1264 ERROR("Illegal groupsWrap setting for %s\n",
1265 longText(key->name));
1266 ACTION("Non-boolean value ignored\n");
1270 key->groupInfo = XkbWrapIntoRange;
1272 key->groupInfo = XkbClampIntoRange;
1273 key->defs.defined |= _Key_GroupInfo;
1275 else if ((strcasecmp(field, "groupsclamp") == 0) ||
1276 (strcasecmp(field, "clampgroups") == 0))
1278 ok = ExprResolveBoolean(keymap->ctx, value, &tmp);
1281 ERROR("Illegal groupsClamp setting for %s\n",
1282 longText(key->name));
1283 ACTION("Non-boolean value ignored\n");
1287 key->groupInfo = XkbClampIntoRange;
1289 key->groupInfo = XkbWrapIntoRange;
1290 key->defs.defined |= _Key_GroupInfo;
1292 else if ((strcasecmp(field, "groupsredirect") == 0) ||
1293 (strcasecmp(field, "redirectgroups") == 0))
1295 if (!ExprResolveGroup(keymap->ctx, value, &tmp))
1297 ERROR("Illegal group index for redirect of key %s\n",
1298 longText(key->name));
1299 ACTION("Definition with non-integer group ignored\n");
1303 XkbSetGroupInfo(0, XkbRedirectIntoRange, tmp.uval - 1);
1304 key->defs.defined |= _Key_GroupInfo;
1308 ERROR("Unknown field %s in a symbol interpretation\n", field);
1309 ACTION("Definition ignored\n");
1316 SetGroupName(SymbolsInfo *info, struct xkb_keymap *keymap, ExprDef *arrayNdx,
1319 ExprResult tmp, name;
1321 if ((arrayNdx == NULL) && (warningLevel > 0))
1323 WARN("You must specify an index when specifying a group name\n");
1324 ACTION("Group name definition without array subscript ignored\n");
1327 if (!ExprResolveGroup(keymap->ctx, arrayNdx, &tmp))
1329 ERROR("Illegal index in group name definition\n");
1330 ACTION("Definition with non-integer array index ignored\n");
1333 if (!ExprResolveString(keymap->ctx, value, &name))
1335 ERROR("Group name must be a string\n");
1336 ACTION("Illegal name for group %d ignored\n", tmp.uval);
1339 info->groupNames[tmp.uval - 1 + info->explicit_group] =
1340 xkb_atom_intern(keymap->ctx, name.str);
1347 HandleSymbolsVar(VarDef *stmt, struct xkb_keymap *keymap, SymbolsInfo *info)
1349 ExprResult elem, field, tmp;
1353 if (ExprResolveLhs(keymap, stmt->name, &elem, &field, &arrayNdx) == 0)
1354 return 0; /* internal error, already reported */
1355 if (elem.str && (strcasecmp(elem.str, "key") == 0))
1357 ret = SetSymbolsField(&info->dflt, keymap, field.str, arrayNdx,
1360 else if ((elem.str == NULL) && ((strcasecmp(field.str, "name") == 0) ||
1361 (strcasecmp(field.str, "groupname") ==
1364 ret = SetGroupName(info, keymap, arrayNdx, stmt->value);
1366 else if ((elem.str == NULL)
1367 && ((strcasecmp(field.str, "groupswrap") == 0) ||
1368 (strcasecmp(field.str, "wrapgroups") == 0)))
1370 if (!ExprResolveBoolean(keymap->ctx, stmt->value, &tmp))
1372 ERROR("Illegal setting for global groupsWrap\n");
1373 ACTION("Non-boolean value ignored\n");
1378 info->groupInfo = XkbWrapIntoRange;
1380 info->groupInfo = XkbClampIntoRange;
1384 else if ((elem.str == NULL)
1385 && ((strcasecmp(field.str, "groupsclamp") == 0) ||
1386 (strcasecmp(field.str, "clampgroups") == 0)))
1388 if (!ExprResolveBoolean(keymap->ctx, stmt->value, &tmp))
1390 ERROR("Illegal setting for global groupsClamp\n");
1391 ACTION("Non-boolean value ignored\n");
1396 info->groupInfo = XkbClampIntoRange;
1398 info->groupInfo = XkbWrapIntoRange;
1402 else if ((elem.str == NULL)
1403 && ((strcasecmp(field.str, "groupsredirect") == 0) ||
1404 (strcasecmp(field.str, "redirectgroups") == 0)))
1406 if (!ExprResolveGroup(keymap->ctx, stmt->value, &tmp))
1408 ERROR("Illegal group index for global groupsRedirect\n");
1409 ACTION("Definition with non-integer group ignored\n");
1413 info->groupInfo = XkbSetGroupInfo(0, XkbRedirectIntoRange,
1418 else if ((elem.str == NULL) && (strcasecmp(field.str, "allownone") == 0))
1420 ERROR("Radio groups not supported\n");
1421 ACTION("Ignoring \"allow none\" specification\n");
1425 ret = SetActionField(keymap, elem.str, field.str, arrayNdx,
1426 stmt->value, &info->action);
1435 HandleSymbolsBody(VarDef *def, struct xkb_keymap *keymap, KeyInfo *key,
1439 ExprResult tmp, field;
1442 for (; def != NULL; def = (VarDef *) def->common.next)
1444 if ((def->name) && (def->name->type == ExprFieldRef))
1446 ok = HandleSymbolsVar(def, keymap, info);
1451 if (def->name == NULL)
1453 if ((def->value == NULL)
1454 || (def->value->op == ExprKeysymList))
1455 field.str = strdup("symbols");
1457 field.str = strdup("actions");
1462 ok = ExprResolveLhs(keymap, def->name, &tmp, &field,
1466 ok = SetSymbolsField(key, keymap, field.str, arrayNdx,
1475 SetExplicitGroup(SymbolsInfo *info, KeyInfo *key)
1477 unsigned group = info->explicit_group;
1482 if ((key->typesDefined | key->symsDefined | key->actsDefined) & ~1)
1485 WARN("For the map %s an explicit group specified\n", info->name);
1486 WARN("but key %s has more than one group defined\n",
1487 longText(key->name));
1488 ACTION("All groups except first one will be ignored\n");
1489 for (i = 1; i < XkbNumKbdGroups; i++)
1491 key->numLevels[i] = 0;
1493 key->syms[i] = NULL;
1495 key->acts[i] = NULL;
1499 key->typesDefined = key->symsDefined = key->actsDefined = 1 << group;
1501 key->numLevels[group] = key->numLevels[0];
1502 key->numLevels[0] = 0;
1503 key->syms[group] = key->syms[0];
1504 key->syms[0] = NULL;
1505 key->sizeSyms[group] = key->sizeSyms[0];
1506 key->sizeSyms[0] = 0;
1507 key->symsMapIndex[group] = key->symsMapIndex[0];
1508 key->symsMapIndex[0] = NULL;
1509 key->symsMapNumEntries[group] = key->symsMapNumEntries[0];
1510 key->symsMapNumEntries[0] = NULL;
1511 key->acts[group] = key->acts[0];
1512 key->acts[0] = NULL;
1513 key->types[group] = key->types[0];
1519 HandleSymbolsDef(SymbolsDef *stmt, struct xkb_keymap *keymap,
1525 CopyKeyInfo(&info->dflt, &key, false);
1526 key.defs.merge = stmt->merge;
1527 key.name = KeyNameToLong(stmt->keyName);
1528 if (!HandleSymbolsBody((VarDef *) stmt->symbols, keymap, &key, info))
1534 if (!SetExplicitGroup(info, &key))
1540 if (!AddKeySymbols(info, &key, keymap))
1549 HandleModMapDef(ModMapDef *def, struct xkb_keymap *keymap, SymbolsInfo *info)
1556 if (!LookupModIndex(keymap->ctx, NULL, def->modifier, TypeInt, &rtrn))
1558 ERROR("Illegal modifier map definition\n");
1559 ACTION("Ignoring map for non-modifier \"%s\"\n",
1560 xkb_atom_text(keymap->ctx, def->modifier));
1564 tmp.modifier = rtrn.uval;
1565 for (key = def->keys; key != NULL; key = (ExprDef *) key->common.next)
1567 if ((key->op == ExprValue) && (key->type == TypeKeyName))
1569 tmp.haveSymbol = false;
1570 tmp.u.keyName = KeyNameToLong(key->value.keyName);
1572 else if (ExprResolveKeySym(keymap->ctx, key, &rtrn))
1574 tmp.haveSymbol = true;
1575 tmp.u.keySym = rtrn.uval;
1579 ERROR("Modmap entries may contain only key names or keysyms\n");
1580 ACTION("Illegal definition for %s modifier ignored\n",
1581 XkbcModIndexText(tmp.modifier));
1585 ok = AddModMapEntry(info, &tmp) && ok;
1591 HandleSymbolsFile(XkbFile *file, struct xkb_keymap *keymap,
1592 unsigned merge, SymbolsInfo *info)
1597 info->name = uDupString(file->name);
1601 switch (stmt->stmtType)
1604 if (!HandleIncludeSymbols((IncludeStmt *) stmt, keymap, info))
1607 case StmtSymbolsDef:
1608 if (!HandleSymbolsDef((SymbolsDef *) stmt, keymap, info))
1612 if (!HandleSymbolsVar((VarDef *) stmt, keymap, info))
1616 if (!HandleVModDef((VModDef *) stmt, keymap, merge, &info->vmods))
1620 ERROR("Interpretation files may not include other types\n");
1621 ACTION("Ignoring definition of symbol interpretation\n");
1624 case StmtKeycodeDef:
1625 ERROR("Interpretation files may not include other types\n");
1626 ACTION("Ignoring definition of key name\n");
1630 if (!HandleModMapDef((ModMapDef *) stmt, keymap, info))
1634 WSGO("Unexpected statement type %d in HandleSymbolsFile\n",
1639 if (info->errorCount > 10)
1642 ERROR("Too many errors\n");
1644 ACTION("Abandoning symbols file \"%s\"\n", file->topName);
1651 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym,
1652 xkb_keycode_t *kc_rtrn)
1655 unsigned int group, level;
1657 for (key = keymap->min_key_code; key <= keymap->max_key_code; key++)
1659 for (group = 0; group < XkbKeyNumGroups(keymap, key); group++)
1661 for (level = 0; level < XkbKeyGroupWidth(keymap, key, group);
1664 if (XkbKeyNumSyms(keymap, key, group, level) != 1 ||
1665 (XkbKeySymEntry(keymap, key, group, level))[0] != sym)
1677 * Find the given name in the keymap->map->types and return its index.
1679 * @param atom The atom to search for.
1680 * @param type_rtrn Set to the index of the name if found.
1682 * @return true if found, false otherwise.
1685 FindNamedType(struct xkb_keymap *keymap, xkb_atom_t atom, unsigned *type_rtrn)
1688 const char *name = xkb_atom_text(keymap->ctx, atom);
1689 struct xkb_key_type *type;
1691 if (keymap && keymap->map) {
1692 darray_foreach(type, keymap->map->types) {
1693 if (strcmp(type->name, name) == 0) {
1704 * Assign a type to the given sym and return the Atom for the type assigned.
1707 * - ONE_LEVEL for width 0/1
1708 * - ALPHABETIC for 2 shift levels, with lower/upercase
1709 * - KEYPAD for keypad keys.
1710 * - TWO_LEVEL for other 2 shift level keys.
1711 * and the same for four level keys.
1713 * @param width Number of sysms in syms.
1714 * @param syms The keysyms for the given key (must be size width).
1715 * @param typeNameRtrn Set to the Atom of the type name.
1717 * @returns true if a type could be found, false otherwise.
1720 FindAutomaticType(struct xkb_keymap *keymap, int width, xkb_keysym_t *syms,
1721 xkb_atom_t *typeNameRtrn, bool *autoType)
1724 if ((width == 1) || (width == 0))
1726 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ONE_LEVEL");
1729 else if (width == 2)
1731 if (syms && XkbcKSIsLower(syms[0]) && XkbcKSIsUpper(syms[1]))
1733 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ALPHABETIC");
1735 else if (syms && (XkbKSIsKeypad(syms[0]) || XkbKSIsKeypad(syms[1])))
1737 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "KEYPAD");
1742 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "TWO_LEVEL");
1746 else if (width <= 4)
1748 if (syms && XkbcKSIsLower(syms[0]) && XkbcKSIsUpper(syms[1]))
1749 if (XkbcKSIsLower(syms[2]) && XkbcKSIsUpper(syms[3]))
1751 xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_ALPHABETIC");
1753 *typeNameRtrn = xkb_atom_intern(keymap->ctx,
1754 "FOUR_LEVEL_SEMIALPHABETIC");
1756 else if (syms && (XkbKSIsKeypad(syms[0]) || XkbKSIsKeypad(syms[1])))
1757 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_KEYPAD");
1759 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL");
1760 /* XXX: why not set autoType here? */
1762 return ((width >= 0) && (width <= 4));
1766 * Ensure the given KeyInfo is in a coherent state, i.e. no gaps between the
1767 * groups, and reduce to one group if all groups are identical anyway.
1770 PrepareKeyDef(KeyInfo * key)
1772 int i, j, width, defined, lastGroup;
1775 defined = key->symsDefined | key->actsDefined | key->typesDefined;
1776 /* get highest group number */
1777 for (i = XkbNumKbdGroups - 1; i >= 0; i--)
1779 if (defined & (1 << i))
1787 /* If there are empty groups between non-empty ones fill them with data */
1788 /* from the first group. */
1789 /* We can make a wrong assumption here. But leaving gaps is worse. */
1790 for (i = lastGroup; i > 0; i--)
1792 if (defined & (1 << i))
1794 width = key->numLevels[0];
1795 if (key->typesDefined & 1)
1797 for (j = 0; j < width; j++)
1799 key->types[i] = key->types[0];
1801 key->typesDefined |= 1 << i;
1803 if ((key->actsDefined & 1) && key->acts[0])
1805 key->acts[i] = uTypedCalloc(width, union xkb_action);
1806 if (key->acts[i] == NULL)
1808 memcpy(key->acts[i], key->acts[0],
1809 width * sizeof(union xkb_action));
1810 key->actsDefined |= 1 << i;
1812 if ((key->symsDefined & 1) && key->sizeSyms[0])
1814 key->syms[i] = uTypedCalloc(key->sizeSyms[0], xkb_keysym_t);
1815 if (key->syms[i] == NULL)
1817 memcpy(key->syms[i], key->syms[0],
1818 key->sizeSyms[0] * sizeof(xkb_keysym_t));
1819 key->symsMapIndex[i] = uTypedCalloc(width, int);
1820 if (!key->symsMapIndex[i])
1823 key->syms[i] = NULL;
1826 memcpy(key->symsMapIndex[i], key->symsMapIndex[0],
1827 width * sizeof(int));
1828 key->symsMapNumEntries[i] = uTypedCalloc(width, unsigned int);
1829 if (!key->symsMapNumEntries[i])
1832 key->syms[i] = NULL;
1833 free(key->symsMapIndex[i]);
1834 key->symsMapIndex[i] = NULL;
1837 memcpy(key->symsMapNumEntries[i], key->symsMapNumEntries[0],
1838 width * sizeof(int));
1839 key->sizeSyms[i] = key->sizeSyms[0];
1840 key->symsDefined |= 1 << i;
1844 key->numLevels[i] = key->numLevels[0];
1847 /* If all groups are completely identical remove them all */
1848 /* exept the first one. */
1850 for (i = lastGroup; i > 0; i--)
1852 if ((key->numLevels[i] != key->numLevels[0]) ||
1853 (key->types[i] != key->types[0]))
1858 if ((key->syms[i] != key->syms[0]) &&
1859 (key->syms[i] == NULL || key->syms[0] == NULL ||
1860 key->sizeSyms[i] != key->sizeSyms[0] ||
1861 memcmp(key->syms[i], key->syms[0],
1862 sizeof(xkb_keysym_t) * key->sizeSyms[0])))
1867 if ((key->symsMapIndex[i] != key->symsMapIndex[i]) &&
1868 (key->symsMapIndex[i] == NULL || key->symsMapIndex[0] == NULL ||
1869 memcmp(key->symsMapIndex[i], key->symsMapIndex[0],
1870 key->numLevels[0] * sizeof(int))))
1875 if ((key->symsMapNumEntries[i] != key->symsMapNumEntries[i]) &&
1876 (key->symsMapNumEntries[i] == NULL ||
1877 key->symsMapNumEntries[0] == NULL ||
1878 memcmp(key->symsMapNumEntries[i], key->symsMapNumEntries[0],
1879 key->numLevels[0] * sizeof(int))))
1884 if ((key->acts[i] != key->acts[0]) &&
1885 (key->acts[i] == NULL || key->acts[0] == NULL ||
1886 memcmp(key->acts[i], key->acts[0],
1887 sizeof(union xkb_action) * key->numLevels[0])))
1895 for (i = lastGroup; i > 0; i--)
1897 key->numLevels[i] = 0;
1899 key->syms[i] = NULL;
1900 key->sizeSyms[i] = 0;
1901 free(key->symsMapIndex[i]);
1902 key->symsMapIndex[i] = NULL;
1903 free(key->symsMapNumEntries[i]);
1904 key->symsMapNumEntries[i] = NULL;
1906 key->acts[i] = NULL;
1909 key->symsDefined &= 1;
1910 key->actsDefined &= 1;
1911 key->typesDefined &= 1;
1916 * Copy the KeyInfo into the keyboard description.
1918 * This function recurses.
1921 CopySymbolsDef(struct xkb_keymap *keymap, KeyInfo *key, int start_from)
1925 unsigned int sizeSyms = 0;
1926 unsigned width, tmp, nGroups;
1927 struct xkb_key_type * type;
1928 bool haveActions, autoType, useAlias;
1929 unsigned types[XkbNumKbdGroups];
1930 union xkb_action *outActs;
1931 unsigned int symIndex = 0;
1933 useAlias = (start_from == 0);
1935 /* get the keycode for the key. */
1936 if (!FindNamedKey(keymap, key->name, &kc, useAlias,
1937 CreateKeyNames(keymap), start_from))
1939 if ((start_from == 0) && (warningLevel >= 5))
1941 WARN("Key %s not found in keycodes\n", longText(key->name));
1942 ACTION("Symbols ignored\n");
1947 haveActions = false;
1948 for (i = width = nGroups = 0; i < XkbNumKbdGroups; i++)
1950 if (((i + 1) > nGroups)
1951 && (((key->symsDefined | key->actsDefined) & (1 << i))
1952 || (key->typesDefined) & (1 << i)))
1957 /* Assign the type to the key, if it is missing. */
1958 if (key->types[i] == XKB_ATOM_NONE)
1960 if (key->dfltType != XKB_ATOM_NONE)
1961 key->types[i] = key->dfltType;
1962 else if (FindAutomaticType(keymap, key->numLevels[i], key->syms[i],
1963 &key->types[i], &autoType))
1968 if (warningLevel >= 5)
1970 WARN("No automatic type for %d symbols\n",
1971 (unsigned int) key->numLevels[i]);
1972 ACTION("Using %s for the %s key (keycode %d)\n",
1973 xkb_atom_text(keymap->ctx, key->types[i]),
1974 longText(key->name), kc);
1978 if (FindNamedType(keymap, key->types[i], &types[i]))
1980 if (!autoType || key->numLevels[i] > 2)
1981 keymap->server->explicit[kc] |= (1 << i);
1985 if (warningLevel >= 3)
1987 WARN("Type \"%s\" is not defined\n",
1988 xkb_atom_text(keymap->ctx, key->types[i]));
1989 ACTION("Using TWO_LEVEL for the %s key (keycode %d)\n",
1990 longText(key->name), kc);
1992 types[i] = XkbTwoLevelIndex;
1994 /* if the type specifies fewer levels than the key has, shrink the key */
1995 type = &darray_item(keymap->map->types, types[i]);
1996 if (type->num_levels < key->numLevels[i])
1998 if (warningLevel > 0)
2000 WARN("Type \"%s\" has %d levels, but %s has %d symbols\n",
2001 type->name, type->num_levels,
2002 xkb_atom_text(keymap->ctx, key->name), key->numLevels[i]);
2003 ACTION("Ignoring extra symbols\n");
2005 key->numLevels[i] = type->num_levels;
2007 if (key->numLevels[i] > width)
2008 width = key->numLevels[i];
2009 if (type->num_levels > width)
2010 width = type->num_levels;
2011 sizeSyms += key->sizeSyms[i];
2014 if (!XkbcResizeKeySyms(keymap, kc, sizeSyms))
2016 WSGO("Could not enlarge symbols for %s (keycode %d)\n",
2017 longText(key->name), kc);
2022 outActs = XkbcResizeKeyActions(keymap, kc, width * nGroups);
2023 if (outActs == NULL)
2025 WSGO("Could not enlarge actions for %s (key %d)\n",
2026 longText(key->name), kc);
2029 keymap->server->explicit[kc] |= XkbExplicitInterpretMask;
2033 if (key->defs.defined & _Key_GroupInfo)
2036 i = keymap->map->key_sym_map[kc].group_info;
2038 keymap->map->key_sym_map[kc].group_info = XkbSetNumGroups(i, nGroups);
2039 keymap->map->key_sym_map[kc].width = width;
2040 keymap->map->key_sym_map[kc].sym_index = uTypedCalloc(nGroups * width,
2042 keymap->map->key_sym_map[kc].num_syms = uTypedCalloc(nGroups * width,
2044 for (i = 0; i < nGroups; i++)
2046 /* assign kt_index[i] to the index of the type in map->types.
2047 * kt_index[i] may have been set by a previous run (if we have two
2048 * layouts specified). Let's not overwrite it with the ONE_LEVEL
2049 * default group if we dont even have keys for this group anyway.
2051 * FIXME: There should be a better fix for this.
2053 if (key->numLevels[i])
2054 keymap->map->key_sym_map[kc].kt_index[i] = types[i];
2055 if (key->sizeSyms[i] != 0)
2057 /* fill key to "width" symbols*/
2058 for (tmp = 0; tmp < width; tmp++)
2060 if (tmp < key->numLevels[i] && key->symsMapNumEntries[i][tmp])
2062 memcpy(&keymap->map->key_sym_map[kc].syms[symIndex],
2063 &key->syms[i][key->symsMapIndex[i][tmp]],
2064 key->symsMapNumEntries[i][tmp] *
2065 sizeof(xkb_keysym_t));
2066 keymap->map->key_sym_map[kc].sym_index[(i * width) + tmp] =
2068 keymap->map->key_sym_map[kc].num_syms[(i * width) + tmp] =
2069 key->symsMapNumEntries[i][tmp];
2071 keymap->map->key_sym_map[kc].num_syms[(i * width) + tmp];
2075 keymap->map->key_sym_map[kc].sym_index[(i * width) + tmp] = -1;
2076 keymap->map->key_sym_map[kc].num_syms[(i * width) + tmp] = 0;
2078 if ((outActs != NULL) && (key->acts[i] != NULL))
2080 if (tmp < key->numLevels[i])
2081 outActs[tmp] = key->acts[i][tmp];
2083 outActs[tmp].type = XkbSA_NoAction;
2088 switch (key->behavior.type & XkbKB_OpMask)
2093 keymap->server->behaviors[kc] = key->behavior;
2094 keymap->server->explicit[kc] |= XkbExplicitBehaviorMask;
2097 if (key->defs.defined & _Key_VModMap)
2099 keymap->server->vmodmap[kc] = key->vmodmap;
2100 keymap->server->explicit[kc] |= XkbExplicitVModMapMask;
2102 if (key->repeat != RepeatUndefined)
2104 if (key->repeat == RepeatYes)
2105 keymap->ctrls->per_key_repeat[kc / 8] |= (1 << (kc % 8));
2107 keymap->ctrls->per_key_repeat[kc / 8] &= ~(1 << (kc % 8));
2108 keymap->server->explicit[kc] |= XkbExplicitAutoRepeatMask;
2111 if (nGroups > keymap->ctrls->num_groups)
2112 keymap->ctrls->num_groups = nGroups;
2114 /* do the same thing for the next key */
2115 CopySymbolsDef(keymap, key, kc + 1);
2120 CopyModMapDef(struct xkb_keymap *keymap, ModMapEntry *entry)
2124 if (!entry->haveSymbol &&
2125 !FindNamedKey(keymap, entry->u.keyName, &kc, true,
2126 CreateKeyNames(keymap), 0))
2128 if (warningLevel >= 5)
2130 WARN("Key %s not found in keycodes\n",
2131 longText(entry->u.keyName));
2132 ACTION("Modifier map entry for %s not updated\n",
2133 XkbcModIndexText(entry->modifier));
2137 else if (entry->haveSymbol &&
2138 !FindKeyForSymbol(keymap, entry->u.keySym, &kc))
2140 if (warningLevel > 5)
2142 WARN("Key \"%s\" not found in symbol map\n",
2143 XkbcKeysymText(entry->u.keySym));
2144 ACTION("Modifier map entry for %s not updated\n",
2145 XkbcModIndexText(entry->modifier));
2149 keymap->map->modmap[kc] |= (1 << entry->modifier);
2154 * Handle the xkb_symbols section of an xkb file.
2156 * @param file The parsed xkb_symbols section of the xkb file.
2157 * @param keymap Handle to the keyboard description to store the symbols in.
2158 * @param merge Merge strategy (e.g. MergeOverride).
2161 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap, unsigned merge)
2167 InitSymbolsInfo(&info, keymap);
2168 info.dflt.defs.fileID = file->id;
2169 info.dflt.defs.merge = merge;
2171 HandleSymbolsFile(file, keymap, merge, &info);
2173 if (darray_empty(info.keys))
2176 if (info.errorCount != 0)
2179 /* alloc memory in the xkb struct */
2180 if (XkbcAllocNames(keymap, XkbGroupNamesMask, 0) != Success) {
2181 WSGO("Can not allocate names in CompileSymbols\n");
2182 ACTION("Symbols not added\n");
2186 if (XkbcAllocClientMap(keymap, XkbKeySymsMask | XkbModifierMapMask, 0)
2188 WSGO("Could not allocate client map in CompileSymbols\n");
2189 ACTION("Symbols not added\n");
2193 if (XkbcAllocServerMap(keymap, XkbAllServerInfoMask, 32) != Success) {
2194 WSGO("Could not allocate server map in CompileSymbols\n");
2195 ACTION("Symbols not added\n");
2199 if (XkbcAllocControls(keymap) != Success) {
2200 WSGO("Could not allocate controls in CompileSymbols\n");
2201 ACTION("Symbols not added\n");
2205 /* now copy info into xkb. */
2206 ApplyAliases(keymap, &info.aliases);
2208 for (i = 0; i < XkbNumKbdGroups; i++) {
2209 if (info.groupNames[i] != XKB_ATOM_NONE) {
2210 free(UNCONSTIFY(keymap->names->groups[i]));
2211 keymap->names->groups[i] = xkb_atom_strdup(keymap->ctx,
2212 info.groupNames[i]);
2217 darray_foreach(key, info.keys)
2221 darray_foreach(key, info.keys)
2222 if (!CopySymbolsDef(keymap, key, 0))
2225 if (warningLevel > 3) {
2226 for (i = keymap->min_key_code; i <= keymap->max_key_code; i++) {
2227 if (darray_item(keymap->names->keys, i).name[0] == '\0')
2230 if (XkbKeyNumGroups(keymap, i) < 1) {
2232 memcpy(buf, darray_item(keymap->names->keys, i).name, 4);
2234 WARN("No symbols defined for <%s> (keycode %d)\n", buf, i);
2240 ModMapEntry *mm, *next;
2241 for (mm = info.modMap; mm != NULL; mm = next) {
2242 if (!CopyModMapDef(keymap, mm))
2244 next = (ModMapEntry *) mm->defs.next;
2248 FreeSymbolsInfo(&info);
2252 FreeSymbolsInfo(&info);