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 <X11/keysym.h>
29 #include "xkbcomp-priv.h"
30 #include "parseutils.h"
36 /***====================================================================***/
40 #define RepeatUndefined ~((unsigned)0)
42 #define _Key_Syms (1<<0)
43 #define _Key_Acts (1<<1)
44 #define _Key_Repeat (1<<2)
45 #define _Key_Behavior (1<<3)
46 #define _Key_Type_Dflt (1<<4)
47 #define _Key_Types (1<<5)
48 #define _Key_GroupInfo (1<<6)
49 #define _Key_VModMap (1<<7)
51 typedef struct _KeyInfo
54 unsigned long name; /* the 4 chars of the key name, as long */
55 unsigned char groupInfo;
56 unsigned char typesDefined;
57 unsigned char symsDefined;
58 unsigned char actsDefined;
59 unsigned int numLevels[XkbNumKbdGroups];
61 /* syms[group] -> Single array for all the keysyms in the group. */
62 xkb_keysym_t *syms[XkbNumKbdGroups];
63 /* sizeSyms[group] -> The size of the syms[group] array. */
64 int sizeSyms[XkbNumKbdGroups];
66 * symsMapIndex[group][level] -> The index from which the syms for
67 * the level begin in the syms[group] array. Remember each keycode
68 * can have multiple keysyms in each level (that is, each key press
69 * can result in multiple keysyms).
71 int *symsMapIndex[XkbNumKbdGroups];
73 * symsMapNumEntries[group][level] -> How many syms are in
74 * syms[group][symsMapIndex[group][level]].
76 unsigned int *symsMapNumEntries[XkbNumKbdGroups];
78 union xkb_action *acts[XkbNumKbdGroups];
79 xkb_atom_t types[XkbNumKbdGroups];
81 struct xkb_behavior behavior;
82 unsigned short vmodmap;
83 unsigned long allowNone;
88 * Init the given key info to sane values.
91 InitKeyInfo(KeyInfo * info)
94 static char dflt[4] = "*";
96 info->defs.defined = 0;
97 info->defs.fileID = 0;
98 info->defs.merge = MergeOverride;
99 info->defs.next = NULL;
100 info->name = KeyNameToLong(dflt);
102 info->typesDefined = info->symsDefined = info->actsDefined = 0;
103 for (i = 0; i < XkbNumKbdGroups; i++)
105 info->numLevels[i] = 0;
106 info->types[i] = XKB_ATOM_NONE;
107 info->syms[i] = NULL;
108 info->sizeSyms[i] = 0;
109 info->symsMapIndex[i] = NULL;
110 info->symsMapNumEntries[i] = NULL;
111 info->acts[i] = NULL;
113 info->dfltType = XKB_ATOM_NONE;
114 info->behavior.type = XkbKB_Default;
115 info->behavior.data = 0;
117 info->repeat = RepeatUndefined;
122 * Free memory associated with this key info and reset to sane values.
125 FreeKeyInfo(KeyInfo * info)
129 info->defs.defined = 0;
130 info->defs.fileID = 0;
131 info->defs.merge = MergeOverride;
132 info->defs.next = NULL;
134 info->typesDefined = info->symsDefined = info->actsDefined = 0;
135 for (i = 0; i < XkbNumKbdGroups; i++)
137 info->numLevels[i] = 0;
138 info->types[i] = XKB_ATOM_NONE;
140 info->syms[i] = NULL;
141 info->sizeSyms[i] = 0;
142 free(info->symsMapIndex[i]);
143 info->symsMapIndex[i] = NULL;
144 free(info->symsMapNumEntries[i]);
145 info->symsMapNumEntries[i] = NULL;
147 info->acts[i] = NULL;
149 info->dfltType = XKB_ATOM_NONE;
150 info->behavior.type = XkbKB_Default;
151 info->behavior.data = 0;
153 info->repeat = RepeatUndefined;
158 * Copy old into new, optionally reset old to 0.
159 * If old is reset, new simply re-uses old's memory. Otherwise, the memory is
160 * newly allocated and new points to the new memory areas.
163 CopyKeyInfo(KeyInfo * old, KeyInfo * new, bool clearOld)
168 new->defs.next = NULL;
171 for (i = 0; i < XkbNumKbdGroups; i++)
173 old->numLevels[i] = 0;
174 old->symsMapIndex[i] = NULL;
175 old->symsMapNumEntries[i] = NULL;
177 old->sizeSyms[i] = 0;
184 for (i = 0; i < XkbNumKbdGroups; i++)
186 width = new->numLevels[i];
187 if (old->syms[i] != NULL)
189 new->syms[i] = uTypedCalloc(new->sizeSyms[i], xkb_keysym_t);
193 new->sizeSyms[i] = 0;
194 new->numLevels[i] = 0;
198 memcpy(new->syms[i], old->syms[i],
199 new->sizeSyms[i] * sizeof(xkb_keysym_t));
200 new->symsMapIndex[i] = uTypedCalloc(width, int);
201 if (!new->symsMapIndex[i])
205 new->sizeSyms[i] = 0;
206 new->numLevels[i] = 0;
210 memcpy(new->symsMapIndex[i], old->symsMapIndex[i],
211 width * sizeof(int));
212 new->symsMapNumEntries[i] = uTypedCalloc(width, unsigned int);
213 if (!new->symsMapNumEntries[i])
217 new->sizeSyms[i] = 0;
218 free(new->symsMapIndex[i]);
219 new->symsMapIndex[i] = NULL;
220 new->numLevels[i] = 0;
224 memcpy(new->symsMapNumEntries[i], old->symsMapNumEntries[i],
225 width * sizeof(unsigned int));
227 if (old->acts[i] != NULL)
229 new->acts[i] = uTypedCalloc(width, union xkb_action);
234 new->sizeSyms[i] = 0;
235 free(new->symsMapIndex[i]);
236 new->symsMapIndex[i] = NULL;
237 free(new->symsMapNumEntries[i]);
238 new->symsMapNumEntries[i] = NULL;
239 new->numLevels[i] = 0;
242 memcpy(new->acts[i], old->acts[i],
243 width * sizeof(union xkb_action));
250 /***====================================================================***/
252 typedef struct _ModMapEntry
259 unsigned long keyName;
264 #define SYMBOLS_INIT_SIZE 110
265 #define SYMBOLS_CHUNK 20
266 typedef struct _SymbolsInfo
268 char *name; /* e.g. pc+us+inet(evdev) */
272 unsigned explicit_group;
280 xkb_atom_t groupNames[XkbNumKbdGroups];
287 InitSymbolsInfo(SymbolsInfo * info, struct xkb_keymap *keymap)
292 info->explicit_group = 0;
293 info->errorCount = 0;
295 info->merge = MergeOverride;
297 info->szKeys = SYMBOLS_INIT_SIZE;
299 info->keys = uTypedCalloc(SYMBOLS_INIT_SIZE, KeyInfo);
301 for (i = 0; i < XkbNumKbdGroups; i++)
302 info->groupNames[i] = XKB_ATOM_NONE;
303 InitKeyInfo(&info->dflt);
304 InitVModInfo(&info->vmods, keymap);
306 info->aliases = NULL;
310 FreeSymbolsInfo(SymbolsInfo * info)
317 for (i = 0; i < info->nKeys; i++)
318 FreeKeyInfo(&info->keys[i]);
322 ClearCommonInfo(&info->modMap->defs);
324 ClearAliases(&info->aliases);
325 memset(info, 0, sizeof(SymbolsInfo));
329 ResizeKeyGroup(KeyInfo * key, unsigned int group, unsigned int numLevels,
330 unsigned sizeSyms, bool forceActions)
334 if (key->syms[group] == NULL || key->sizeSyms[group] < sizeSyms)
336 key->syms[group] = uTypedRecalloc(key->syms[group],
337 key->sizeSyms[group],
340 if (!key->syms[group]) {
341 key->sizeSyms[group] = 0;
344 key->sizeSyms[group] = sizeSyms;
346 if (!key->symsMapIndex[group] || key->numLevels[group] < numLevels)
348 key->symsMapIndex[group] = uTypedRealloc(key->symsMapIndex[group],
351 if (!key->symsMapIndex[group])
353 for (i = key->numLevels[group]; i < numLevels; i++)
354 key->symsMapIndex[group][i] = -1;
356 if (!key->symsMapNumEntries[group] || key->numLevels[group] < numLevels)
358 key->symsMapNumEntries[group] =
359 uTypedRecalloc(key->symsMapNumEntries[group],
360 key->numLevels[group],
363 if (!key->symsMapNumEntries[group])
367 (key->numLevels[group] < numLevels || (key->acts[group] == NULL))) ||
368 (key->numLevels[group] < numLevels && (key->acts[group] != NULL)))
370 key->acts[group] = uTypedRecalloc(key->acts[group],
371 key->numLevels[group],
374 if (!key->acts[group])
377 if (key->numLevels[group] < numLevels)
378 key->numLevels[group] = numLevels;
383 MergeKeyGroups(SymbolsInfo * info,
384 KeyInfo * into, KeyInfo * from, unsigned group)
386 xkb_keysym_t *resultSyms = NULL;
387 union xkb_action *resultActs;
388 unsigned int resultWidth;
389 unsigned int resultSize = 0;
392 bool report, clobber;
394 clobber = (from->defs.merge != MergeAugment);
395 report = (warningLevel > 9) ||
396 ((into->defs.fileID == from->defs.fileID) && (warningLevel > 0));
397 if (into->numLevels[group] >= from->numLevels[group])
399 resultActs = into->acts[group];
400 resultWidth = into->numLevels[group];
404 resultActs = from->acts[group];
405 resultWidth = from->numLevels[group];
406 into->symsMapIndex[group] = uTypedRealloc(into->symsMapIndex[group],
407 from->numLevels[group],
409 into->symsMapNumEntries[group] =
410 uTypedRecalloc(into->symsMapNumEntries[group],
411 into->numLevels[group],
412 from->numLevels[group],
414 if (!into->symsMapIndex[group] || !into->symsMapNumEntries[group])
416 WSGO("Could not allocate level indices for key info merge\n");
417 ACTION("Group %d of key %s not merged\n", group,
418 longText(into->name));
422 for (i = into->numLevels[group]; i < from->numLevels[group]; i++)
423 into->symsMapIndex[group][i] = -1;
426 if ((resultActs == NULL) && (into->acts[group] || from->acts[group]))
428 resultActs = uTypedCalloc(resultWidth, union xkb_action);
431 WSGO("Could not allocate actions for group merge\n");
432 ACTION("Group %d of key %s not merged\n", group,
433 longText(into->name));
436 for (i = 0; i < resultWidth; i++)
438 union xkb_action *fromAct, *toAct;
439 fromAct = (from->acts[group] ? &from->acts[group][i] : NULL);
440 toAct = (into->acts[group] ? &into->acts[group][i] : NULL);
441 if (((fromAct == NULL) || (fromAct->type == XkbSA_NoAction))
444 resultActs[i] = *toAct;
446 else if (((toAct == NULL) || (toAct->type == XkbSA_NoAction))
447 && (fromAct != NULL))
449 resultActs[i] = *fromAct;
453 union xkb_action *use, *ignore;
467 ("Multiple actions for level %d/group %d on key %s\n",
468 i + 1, group + 1, longText(into->name));
469 ACTION("Using %s, ignoring %s\n",
470 XkbcActionTypeText(use->type),
471 XkbcActionTypeText(ignore->type));
474 resultActs[i] = *use;
479 for (i = 0; i < resultWidth; i++)
481 unsigned int fromSize = 0;
484 if (from->symsMapNumEntries[group] && (i < from->numLevels[group]))
485 fromSize = from->symsMapNumEntries[group][i];
486 if (into->symsMapNumEntries[group] && (i < into->numLevels[group]))
487 toSize = into->symsMapNumEntries[group][i];
489 if ((fromSize != 0 && toSize == 0) || clobber)
490 resultSize += fromSize;
492 resultSize += toSize;
498 resultSyms = uTypedCalloc(resultSize, xkb_keysym_t);
501 WSGO("Could not allocate symbols for group merge\n");
502 ACTION("Group %d of key %s not merged\n", group, longText(into->name));
506 for (i = 0; i < resultWidth; i++)
508 enum { NONE, FROM, TO } use;
509 unsigned int fromSize = 0;
510 unsigned int toSize = 0;
512 if (from->symsMapNumEntries[group] && (i < from->numLevels[group]))
513 fromSize = from->symsMapNumEntries[group][i];
514 if (into->symsMapNumEntries[group] && (i < into->numLevels[group]))
515 toSize = into->symsMapNumEntries[group][i];
517 if (!fromSize && !toSize)
519 into->symsMapIndex[group][i] = -1;
520 into->symsMapNumEntries[group][i] = 0;
524 if ((fromSize && !toSize) || clobber)
529 if (toSize && fromSize && report)
531 INFO("Multiple symbols for group %d, level %d on key %s\n",
532 group + 1, i + 1, longText(into->name));
533 ACTION("Using %s, ignoring %s\n",
534 (use == FROM ? "from" : "to"),
535 (use == FROM ? "to" : "from"));
540 memcpy(&resultSyms[cur_idx],
541 &from->syms[group][from->symsMapIndex[group][i]],
542 from->symsMapNumEntries[group][i] * sizeof(xkb_keysym_t));
543 into->symsMapIndex[group][i] = cur_idx;
544 into->symsMapNumEntries[group][i] =
545 from->symsMapNumEntries[group][i];
549 memcpy(&resultSyms[cur_idx],
550 &into->syms[group][from->symsMapIndex[group][i]],
551 into->symsMapNumEntries[group][i] * sizeof(xkb_keysym_t));
552 into->symsMapIndex[group][i] = cur_idx;
554 cur_idx += into->symsMapNumEntries[group][i];
558 if (resultActs != into->acts[group])
559 free(into->acts[group]);
560 if (resultActs != from->acts[group])
561 free(from->acts[group]);
562 into->numLevels[group] = resultWidth;
563 free(into->syms[group]);
564 into->syms[group] = resultSyms;
565 free(from->syms[group]);
566 from->syms[group] = NULL;
567 from->sizeSyms[group] = 0;
568 into->sizeSyms[group] = resultSize;
569 free(from->symsMapIndex[group]);
570 from->symsMapIndex[group] = NULL;
571 free(from->symsMapNumEntries[group]);
572 from->symsMapNumEntries[group] = NULL;
573 into->acts[group] = resultActs;
574 from->acts[group] = NULL;
575 into->symsDefined |= (1 << group);
576 from->symsDefined &= ~(1 << group);
577 into->actsDefined |= (1 << group);
578 from->actsDefined &= ~(1 << group);
584 MergeKeys(SymbolsInfo * info, KeyInfo * into, KeyInfo * from)
587 unsigned collide = 0;
590 if (from->defs.merge == MergeReplace)
592 for (i = 0; i < XkbNumKbdGroups; i++)
594 if (into->numLevels[i] != 0)
601 memset(from, 0, sizeof(KeyInfo));
604 report = ((warningLevel > 9) ||
605 ((into->defs.fileID == from->defs.fileID)
606 && (warningLevel > 0)));
607 for (i = 0; i < XkbNumKbdGroups; i++)
609 if (from->numLevels[i] > 0)
611 if (into->numLevels[i] == 0)
613 into->numLevels[i] = from->numLevels[i];
614 into->syms[i] = from->syms[i];
615 into->sizeSyms[i] = from->sizeSyms[i];
616 into->symsMapIndex[i] = from->symsMapIndex[i];
617 into->symsMapNumEntries[i] = from->symsMapNumEntries[i];
618 into->acts[i] = from->acts[i];
619 into->symsDefined |= (1 << i);
620 from->syms[i] = NULL;
621 from->sizeSyms[i] = 0;
622 from->symsMapIndex[i] = NULL;
623 from->symsMapNumEntries[i] = NULL;
624 from->acts[i] = NULL;
625 from->numLevels[i] = 0;
626 from->symsDefined &= ~(1 << i);
628 into->defs.defined |= _Key_Syms;
630 into->defs.defined |= _Key_Acts;
637 collide |= _Key_Syms;
639 collide |= _Key_Acts;
641 MergeKeyGroups(info, into, from, (unsigned) i);
644 if (from->types[i] != XKB_ATOM_NONE)
646 if ((into->types[i] != XKB_ATOM_NONE) && report &&
647 (into->types[i] != from->types[i]))
649 xkb_atom_t use, ignore;
650 collide |= _Key_Types;
651 if (from->defs.merge != MergeAugment)
653 use = from->types[i];
654 ignore = into->types[i];
658 use = into->types[i];
659 ignore = from->types[i];
662 ("Multiple definitions for group %d type of key %s\n",
663 i, longText(into->name));
664 ACTION("Using %s, ignoring %s\n",
666 XkbcAtomText(ignore));
668 if ((from->defs.merge != MergeAugment)
669 || (into->types[i] == XKB_ATOM_NONE))
671 into->types[i] = from->types[i];
675 if (UseNewField(_Key_Behavior, &into->defs, &from->defs, &collide))
677 into->behavior = from->behavior;
678 into->defs.defined |= _Key_Behavior;
680 if (UseNewField(_Key_VModMap, &into->defs, &from->defs, &collide))
682 into->vmodmap = from->vmodmap;
683 into->defs.defined |= _Key_VModMap;
685 if (UseNewField(_Key_Repeat, &into->defs, &from->defs, &collide))
687 into->repeat = from->repeat;
688 into->defs.defined |= _Key_Repeat;
690 if (UseNewField(_Key_Type_Dflt, &into->defs, &from->defs, &collide))
692 into->dfltType = from->dfltType;
693 into->defs.defined |= _Key_Type_Dflt;
695 if (UseNewField(_Key_GroupInfo, &into->defs, &from->defs, &collide))
697 into->groupInfo = from->groupInfo;
698 into->defs.defined |= _Key_GroupInfo;
702 WARN("Symbol map for key %s redefined\n",
703 longText(into->name));
704 ACTION("Using %s definition for conflicting fields\n",
705 (from->defs.merge == MergeAugment ? "first" : "last"));
711 AddKeySymbols(SymbolsInfo *info, KeyInfo *key, struct xkb_keymap *keymap)
714 unsigned long real_name;
716 for (i = 0; i < info->nKeys; i++)
718 if (info->keys[i].name == key->name)
719 return MergeKeys(info, &info->keys[i], key);
721 if (FindKeyNameForAlias(keymap, key->name, &real_name))
723 for (i = 0; i < info->nKeys; i++)
725 if (info->keys[i].name == real_name)
726 return MergeKeys(info, &info->keys[i], key);
729 if (info->nKeys >= info->szKeys)
731 info->szKeys += SYMBOLS_CHUNK;
733 uTypedRecalloc(info->keys, info->nKeys, info->szKeys, KeyInfo);
736 WSGO("Could not allocate key symbols descriptions\n");
737 ACTION("Some key symbols definitions may be lost\n");
741 return CopyKeyInfo(key, &info->keys[info->nKeys++], true);
745 AddModMapEntry(SymbolsInfo * info, ModMapEntry * new)
750 clobber = (new->defs.merge != MergeAugment);
751 for (mm = info->modMap; mm != NULL; mm = (ModMapEntry *) mm->defs.next)
753 if (new->haveSymbol && mm->haveSymbol
754 && (new->u.keySym == mm->u.keySym))
756 unsigned use, ignore;
757 if (mm->modifier != new->modifier)
762 ignore = mm->modifier;
767 ignore = new->modifier;
770 ("%s added to symbol map for multiple modifiers\n",
771 XkbcKeysymText(new->u.keySym));
772 ACTION("Using %s, ignoring %s.\n",
773 XkbcModIndexText(use),
774 XkbcModIndexText(ignore));
779 if ((!new->haveSymbol) && (!mm->haveSymbol) &&
780 (new->u.keyName == mm->u.keyName))
782 unsigned use, ignore;
783 if (mm->modifier != new->modifier)
788 ignore = mm->modifier;
793 ignore = new->modifier;
795 ERROR("Key %s added to map for multiple modifiers\n",
796 longText(new->u.keyName));
797 ACTION("Using %s, ignoring %s.\n",
798 XkbcModIndexText(use),
799 XkbcModIndexText(ignore));
805 mm = uTypedAlloc(ModMapEntry);
808 WSGO("Could not allocate modifier map entry\n");
809 ACTION("Modifier map for %s will be incomplete\n",
810 XkbcModIndexText(new->modifier));
814 mm->defs.next = &info->modMap->defs;
819 /***====================================================================***/
822 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
823 unsigned merge, struct xkb_keymap *keymap)
828 if (from->errorCount > 0)
830 into->errorCount += from->errorCount;
833 if (into->name == NULL)
835 into->name = from->name;
838 for (i = 0; i < XkbNumKbdGroups; i++)
840 if (from->groupNames[i] != XKB_ATOM_NONE)
842 if ((merge != MergeAugment) ||
843 (into->groupNames[i] == XKB_ATOM_NONE))
844 into->groupNames[i] = from->groupNames[i];
847 for (i = 0, key = from->keys; i < from->nKeys; i++, key++)
849 if (merge != MergeDefault)
850 key->defs.merge = merge;
851 if (!AddKeySymbols(into, key, keymap))
854 if (from->modMap != NULL)
856 ModMapEntry *mm, *next;
857 for (mm = from->modMap; mm != NULL; mm = next)
859 if (merge != MergeDefault)
860 mm->defs.merge = merge;
861 if (!AddModMapEntry(into, mm))
863 next = (ModMapEntry *) mm->defs.next;
868 if (!MergeAliases(&into->aliases, &from->aliases, merge))
872 typedef void (*FileHandler) (XkbFile *rtrn, struct xkb_keymap *keymap,
873 unsigned merge, SymbolsInfo *included);
876 HandleIncludeSymbols(IncludeStmt *stmt, struct xkb_keymap *keymap,
877 SymbolsInfo *info, FileHandler hndlr)
881 SymbolsInfo included;
885 if ((stmt->file == NULL) && (stmt->map == NULL))
889 memset(info, 0, sizeof(SymbolsInfo));
891 else if (ProcessIncludeFile(keymap->context, stmt, XkmSymbolsIndex,
894 InitSymbolsInfo(&included, keymap);
895 included.fileID = included.dflt.defs.fileID = rtrn->id;
896 included.merge = included.dflt.defs.merge = MergeOverride;
899 included.explicit_group = atoi(stmt->modifier) - 1;
903 included.explicit_group = info->explicit_group;
905 (*hndlr) (rtrn, keymap, MergeOverride, &included);
906 if (stmt->stmt != NULL)
909 included.name = stmt->stmt;
916 info->errorCount += 10;
919 if ((stmt->next != NULL) && (included.errorCount < 1))
923 SymbolsInfo next_incl;
925 for (next = stmt->next; next != NULL; next = next->next)
927 if ((next->file == NULL) && (next->map == NULL))
930 MergeIncludedSymbols(&included, info, next->merge, keymap);
931 FreeSymbolsInfo(info);
933 else if (ProcessIncludeFile(keymap->context, next,
934 XkmSymbolsIndex, &rtrn, &op))
936 InitSymbolsInfo(&next_incl, keymap);
937 next_incl.fileID = next_incl.dflt.defs.fileID = rtrn->id;
938 next_incl.merge = next_incl.dflt.defs.merge = MergeOverride;
941 next_incl.explicit_group = atoi(next->modifier) - 1;
945 next_incl.explicit_group = info->explicit_group;
947 (*hndlr) (rtrn, keymap, MergeOverride, &next_incl);
948 MergeIncludedSymbols(&included, &next_incl, op, keymap);
949 FreeSymbolsInfo(&next_incl);
954 info->errorCount += 10;
955 FreeSymbolsInfo(&included);
962 info->errorCount += included.errorCount;
968 MergeIncludedSymbols(info, &included, newMerge, keymap);
969 FreeSymbolsInfo(&included);
971 return (info->errorCount == 0);
978 GetGroupIndex(KeyInfo * key,
979 ExprDef * arrayNdx, unsigned what, unsigned *ndx_rtrn)
989 if (arrayNdx == NULL)
994 defined = key->symsDefined;
996 defined = key->actsDefined;
998 for (i = 0; i < XkbNumKbdGroups; i++)
1000 if ((defined & (1 << i)) == 0)
1006 ERROR("Too many groups of %s for key %s (max %d)\n", name,
1007 longText(key->name), XkbNumKbdGroups + 1);
1008 ACTION("Ignoring %s defined for extra groups\n", name);
1011 if (!ExprResolveGroup(arrayNdx, &tmp))
1013 ERROR("Illegal group index for %s of key %s\n", name,
1014 longText(key->name));
1015 ACTION("Definition with non-integer array index ignored\n");
1018 *ndx_rtrn = tmp.uval - 1;
1023 AddSymbolsToKey(KeyInfo *key, struct xkb_keymap *keymap,
1024 ExprDef *arrayNdx, ExprDef *value, SymbolsInfo *info)
1026 unsigned ndx, nSyms, nLevels;
1030 if (!GetGroupIndex(key, arrayNdx, SYMBOLS, &ndx))
1034 key->symsDefined |= (1 << ndx);
1037 if (value->op != ExprKeysymList)
1039 ERROR("Expected a list of symbols, found %s\n", exprOpText(value->op));
1040 ACTION("Ignoring symbols for group %d of %s\n", ndx + 1,
1041 longText(key->name));
1044 if (key->sizeSyms[ndx] != 0)
1046 ERROR("Symbols for key %s, group %d already defined\n",
1047 longText(key->name), ndx + 1);
1048 ACTION("Ignoring duplicate definition\n");
1051 nSyms = value->value.list.nSyms;
1052 nLevels = value->value.list.nLevels;
1053 if (((key->numLevels[ndx] < nSyms) || (key->syms[ndx] == NULL)) &&
1054 (!ResizeKeyGroup(key, ndx, nLevels, nSyms, false)))
1056 WSGO("Could not resize group %d of key %s to contain %d levels\n",
1057 ndx + 1, longText(key->name), nSyms);
1058 ACTION("Symbols lost\n");
1061 key->symsDefined |= (1 << ndx);
1062 for (i = 0; i < nLevels; i++) {
1063 key->symsMapIndex[ndx][i] = value->value.list.symsMapIndex[i];
1064 key->symsMapNumEntries[ndx][i] = value->value.list.symsNumEntries[i];
1065 for (j = 0; j < key->symsMapNumEntries[ndx][i]; j++) {
1066 if (key->symsMapIndex[ndx][i] + j >= nSyms)
1068 if (!LookupKeysym(value->value.list.syms[value->value.list.symsMapIndex[i] + j],
1069 &key->syms[ndx][key->symsMapIndex[ndx][i] + j])) {
1070 WARN("Could not resolve keysym %s for key %s, group %d (%s), level %d\n",
1071 value->value.list.syms[i], longText(key->name), ndx + 1,
1072 XkbcAtomText(info->groupNames[ndx]), nSyms);
1074 key->syms[ndx][key->symsMapIndex[ndx][i] + j] = NoSymbol;
1075 key->symsMapIndex[ndx][i] = -1;
1076 key->symsMapNumEntries[ndx][i] = 0;
1081 for (j = key->numLevels[ndx] - 1;
1082 j >= 0 && key->symsMapNumEntries[ndx][j] == 0; j--)
1083 key->numLevels[ndx]--;
1088 AddActionsToKey(KeyInfo *key, struct xkb_keymap *keymap, ExprDef *arrayNdx,
1089 ExprDef *value, SymbolsInfo *info)
1092 unsigned ndx, nActs;
1094 struct xkb_any_action *toAct;
1096 if (!GetGroupIndex(key, arrayNdx, ACTIONS, &ndx))
1101 key->actsDefined |= (1 << ndx);
1104 if (value->op != ExprActionList)
1106 WSGO("Bad expression type (%d) for action list value\n", value->op);
1107 ACTION("Ignoring actions for group %d of %s\n", ndx,
1108 longText(key->name));
1111 if (key->acts[ndx] != NULL)
1113 WSGO("Actions for key %s, group %d already defined\n",
1114 longText(key->name), ndx);
1117 for (nActs = 0, act = value->value.child; act != NULL; nActs++)
1119 act = (ExprDef *) act->common.next;
1123 WSGO("Action list but not actions in AddActionsToKey\n");
1126 if (((key->numLevels[ndx] < nActs) || (key->acts[ndx] == NULL)) &&
1127 (!ResizeKeyGroup(key, ndx, nActs, nActs, true)))
1129 WSGO("Could not resize group %d of key %s\n", ndx,
1130 longText(key->name));
1131 ACTION("Actions lost\n");
1134 key->actsDefined |= (1 << ndx);
1136 toAct = (struct xkb_any_action *) key->acts[ndx];
1137 act = value->value.child;
1138 for (i = 0; i < nActs; i++, toAct++)
1140 if (!HandleActionDef(act, keymap, toAct, info->action))
1142 ERROR("Illegal action definition for %s\n",
1143 longText(key->name));
1144 ACTION("Action for group %d/level %d ignored\n", ndx + 1, i + 1);
1146 act = (ExprDef *) act->common.next;
1151 static const LookupEntry lockingEntries[] = {
1152 {"true", XkbKB_Lock},
1153 {"yes", XkbKB_Lock},
1155 {"false", XkbKB_Default},
1156 {"no", XkbKB_Default},
1157 {"off", XkbKB_Default},
1158 {"permanent", XkbKB_Lock | XkbKB_Permanent},
1162 static const LookupEntry repeatEntries[] = {
1163 {"true", RepeatYes},
1166 {"false", RepeatNo},
1169 {"default", RepeatUndefined},
1174 SetSymbolsField(KeyInfo *key, struct xkb_keymap *keymap, char *field,
1175 ExprDef *arrayNdx, ExprDef *value, SymbolsInfo *info)
1180 if (strcasecmp(field, "type") == 0)
1183 if ((!ExprResolveString(keymap, value, &tmp))
1184 && (warningLevel > 0))
1186 WARN("The type field of a key symbol map must be a string\n");
1187 ACTION("Ignoring illegal type definition\n");
1189 if (arrayNdx == NULL)
1191 key->dfltType = xkb_atom_intern(keymap->context, tmp.str);
1192 key->defs.defined |= _Key_Type_Dflt;
1194 else if (!ExprResolveGroup(arrayNdx, &ndx))
1196 ERROR("Illegal group index for type of key %s\n",
1197 longText(key->name));
1198 ACTION("Definition with non-integer array index ignored\n");
1204 key->types[ndx.uval - 1] = xkb_atom_intern(keymap->context,
1206 key->typesDefined |= (1 << (ndx.uval - 1));
1210 else if (strcasecmp(field, "symbols") == 0)
1211 return AddSymbolsToKey(key, keymap, arrayNdx, value, info);
1212 else if (strcasecmp(field, "actions") == 0)
1213 return AddActionsToKey(key, keymap, arrayNdx, value, info);
1214 else if ((strcasecmp(field, "vmods") == 0) ||
1215 (strcasecmp(field, "virtualmods") == 0) ||
1216 (strcasecmp(field, "virtualmodifiers") == 0))
1218 ok = ExprResolveVModMask(value, &tmp, keymap);
1221 key->vmodmap = (tmp.uval >> 8);
1222 key->defs.defined |= _Key_VModMap;
1226 ERROR("Expected a virtual modifier mask, found %s\n",
1227 exprOpText(value->op));
1228 ACTION("Ignoring virtual modifiers definition for key %s\n",
1229 longText(key->name));
1232 else if ((strcasecmp(field, "locking") == 0) ||
1233 (strcasecmp(field, "lock") == 0) ||
1234 (strcasecmp(field, "locks") == 0))
1236 ok = ExprResolveEnum(value, &tmp, lockingEntries);
1238 key->behavior.type = tmp.uval;
1239 key->defs.defined |= _Key_Behavior;
1241 else if ((strcasecmp(field, "radiogroup") == 0) ||
1242 (strcasecmp(field, "permanentradiogroup") == 0) ||
1243 (strcasecmp(field, "allownone") == 0))
1245 ERROR("Radio groups not supported\n");
1246 ACTION("Ignoring radio group specification for key %s\n", longText(key->name));
1249 else if (uStrCasePrefix("overlay", field) ||
1250 uStrCasePrefix("permanentoverlay", field))
1252 ERROR("Overlays not supported\n");
1253 ACTION("Ignoring overlay specification for key %s\n", longText(key->name));
1255 else if ((strcasecmp(field, "repeating") == 0) ||
1256 (strcasecmp(field, "repeats") == 0) ||
1257 (strcasecmp(field, "repeat") == 0))
1259 ok = ExprResolveEnum(value, &tmp, repeatEntries);
1262 ERROR("Illegal repeat setting for %s\n",
1263 longText(key->name));
1264 ACTION("Non-boolean repeat setting ignored\n");
1267 key->repeat = tmp.uval;
1268 key->defs.defined |= _Key_Repeat;
1270 else if ((strcasecmp(field, "groupswrap") == 0) ||
1271 (strcasecmp(field, "wrapgroups") == 0))
1273 ok = ExprResolveBoolean(value, &tmp);
1276 ERROR("Illegal groupsWrap setting for %s\n",
1277 longText(key->name));
1278 ACTION("Non-boolean value ignored\n");
1282 key->groupInfo = XkbWrapIntoRange;
1284 key->groupInfo = XkbClampIntoRange;
1285 key->defs.defined |= _Key_GroupInfo;
1287 else if ((strcasecmp(field, "groupsclamp") == 0) ||
1288 (strcasecmp(field, "clampgroups") == 0))
1290 ok = ExprResolveBoolean(value, &tmp);
1293 ERROR("Illegal groupsClamp setting for %s\n",
1294 longText(key->name));
1295 ACTION("Non-boolean value ignored\n");
1299 key->groupInfo = XkbClampIntoRange;
1301 key->groupInfo = XkbWrapIntoRange;
1302 key->defs.defined |= _Key_GroupInfo;
1304 else if ((strcasecmp(field, "groupsredirect") == 0) ||
1305 (strcasecmp(field, "redirectgroups") == 0))
1307 if (!ExprResolveGroup(value, &tmp))
1309 ERROR("Illegal group index for redirect of key %s\n",
1310 longText(key->name));
1311 ACTION("Definition with non-integer group ignored\n");
1315 XkbSetGroupInfo(0, XkbRedirectIntoRange, tmp.uval - 1);
1316 key->defs.defined |= _Key_GroupInfo;
1320 ERROR("Unknown field %s in a symbol interpretation\n", field);
1321 ACTION("Definition ignored\n");
1328 SetGroupName(SymbolsInfo *info, struct xkb_keymap *keymap, ExprDef *arrayNdx,
1331 ExprResult tmp, name;
1333 if ((arrayNdx == NULL) && (warningLevel > 0))
1335 WARN("You must specify an index when specifying a group name\n");
1336 ACTION("Group name definition without array subscript ignored\n");
1339 if (!ExprResolveGroup(arrayNdx, &tmp))
1341 ERROR("Illegal index in group name definition\n");
1342 ACTION("Definition with non-integer array index ignored\n");
1345 if (!ExprResolveString(keymap, value, &name))
1347 ERROR("Group name must be a string\n");
1348 ACTION("Illegal name for group %d ignored\n", tmp.uval);
1351 info->groupNames[tmp.uval - 1 + info->explicit_group] =
1352 xkb_atom_intern(keymap->context, name.str);
1359 HandleSymbolsVar(VarDef *stmt, struct xkb_keymap *keymap, SymbolsInfo *info)
1361 ExprResult elem, field, tmp;
1365 if (ExprResolveLhs(keymap, stmt->name, &elem, &field, &arrayNdx) == 0)
1366 return 0; /* internal error, already reported */
1367 if (elem.str && (strcasecmp(elem.str, "key") == 0))
1369 ret = SetSymbolsField(&info->dflt, keymap, field.str, arrayNdx,
1372 else if ((elem.str == NULL) && ((strcasecmp(field.str, "name") == 0) ||
1373 (strcasecmp(field.str, "groupname") ==
1376 ret = SetGroupName(info, keymap, arrayNdx, stmt->value);
1378 else if ((elem.str == NULL)
1379 && ((strcasecmp(field.str, "groupswrap") == 0) ||
1380 (strcasecmp(field.str, "wrapgroups") == 0)))
1382 if (!ExprResolveBoolean(stmt->value, &tmp))
1384 ERROR("Illegal setting for global groupsWrap\n");
1385 ACTION("Non-boolean value ignored\n");
1390 info->groupInfo = XkbWrapIntoRange;
1392 info->groupInfo = XkbClampIntoRange;
1396 else if ((elem.str == NULL)
1397 && ((strcasecmp(field.str, "groupsclamp") == 0) ||
1398 (strcasecmp(field.str, "clampgroups") == 0)))
1400 if (!ExprResolveBoolean(stmt->value, &tmp))
1402 ERROR("Illegal setting for global groupsClamp\n");
1403 ACTION("Non-boolean value ignored\n");
1408 info->groupInfo = XkbClampIntoRange;
1410 info->groupInfo = XkbWrapIntoRange;
1414 else if ((elem.str == NULL)
1415 && ((strcasecmp(field.str, "groupsredirect") == 0) ||
1416 (strcasecmp(field.str, "redirectgroups") == 0)))
1418 if (!ExprResolveGroup(stmt->value, &tmp))
1420 ERROR("Illegal group index for global groupsRedirect\n");
1421 ACTION("Definition with non-integer group ignored\n");
1425 info->groupInfo = XkbSetGroupInfo(0, XkbRedirectIntoRange,
1430 else if ((elem.str == NULL) && (strcasecmp(field.str, "allownone") == 0))
1432 ERROR("Radio groups not supported\n");
1433 ACTION("Ignoring \"allow none\" specification\n");
1437 ret = SetActionField(keymap, elem.str, field.str, arrayNdx,
1438 stmt->value, &info->action);
1447 HandleSymbolsBody(VarDef *def, struct xkb_keymap *keymap, KeyInfo *key,
1451 ExprResult tmp, field;
1454 for (; def != NULL; def = (VarDef *) def->common.next)
1456 if ((def->name) && (def->name->type == ExprFieldRef))
1458 ok = HandleSymbolsVar(def, keymap, info);
1463 if (def->name == NULL)
1465 if ((def->value == NULL)
1466 || (def->value->op == ExprKeysymList))
1467 field.str = strdup("symbols");
1469 field.str = strdup("actions");
1474 ok = ExprResolveLhs(keymap, def->name, &tmp, &field,
1478 ok = SetSymbolsField(key, keymap, field.str, arrayNdx,
1487 SetExplicitGroup(SymbolsInfo *info, KeyInfo *key)
1489 unsigned group = info->explicit_group;
1494 if ((key->typesDefined | key->symsDefined | key->actsDefined) & ~1)
1497 WARN("For the map %s an explicit group specified\n", info->name);
1498 WARN("but key %s has more than one group defined\n",
1499 longText(key->name));
1500 ACTION("All groups except first one will be ignored\n");
1501 for (i = 1; i < XkbNumKbdGroups; i++)
1503 key->numLevels[i] = 0;
1505 key->syms[i] = NULL;
1507 key->acts[i] = NULL;
1511 key->typesDefined = key->symsDefined = key->actsDefined = 1 << group;
1513 key->numLevels[group] = key->numLevels[0];
1514 key->numLevels[0] = 0;
1515 key->syms[group] = key->syms[0];
1516 key->syms[0] = NULL;
1517 key->sizeSyms[group] = key->sizeSyms[0];
1518 key->sizeSyms[0] = 0;
1519 key->symsMapIndex[group] = key->symsMapIndex[0];
1520 key->symsMapIndex[0] = NULL;
1521 key->symsMapNumEntries[group] = key->symsMapNumEntries[0];
1522 key->symsMapNumEntries[0] = NULL;
1523 key->acts[group] = key->acts[0];
1524 key->acts[0] = NULL;
1525 key->types[group] = key->types[0];
1531 HandleSymbolsDef(SymbolsDef *stmt, struct xkb_keymap *keymap,
1537 CopyKeyInfo(&info->dflt, &key, false);
1538 key.defs.merge = stmt->merge;
1539 key.name = KeyNameToLong(stmt->keyName);
1540 if (!HandleSymbolsBody((VarDef *) stmt->symbols, keymap, &key, info))
1546 if (!SetExplicitGroup(info, &key))
1552 if (!AddKeySymbols(info, &key, keymap))
1561 HandleModMapDef(ModMapDef *def, struct xkb_keymap *keymap, SymbolsInfo *info)
1568 if (!LookupModIndex(NULL, def->modifier, TypeInt, &rtrn))
1570 ERROR("Illegal modifier map definition\n");
1571 ACTION("Ignoring map for non-modifier \"%s\"\n",
1572 XkbcAtomText(def->modifier));
1576 tmp.modifier = rtrn.uval;
1577 for (key = def->keys; key != NULL; key = (ExprDef *) key->common.next)
1579 if ((key->op == ExprValue) && (key->type == TypeKeyName))
1581 tmp.haveSymbol = false;
1582 tmp.u.keyName = KeyNameToLong(key->value.keyName);
1584 else if (ExprResolveKeySym(key, &rtrn))
1586 tmp.haveSymbol = true;
1587 tmp.u.keySym = rtrn.uval;
1591 ERROR("Modmap entries may contain only key names or keysyms\n");
1592 ACTION("Illegal definition for %s modifier ignored\n",
1593 XkbcModIndexText(tmp.modifier));
1597 ok = AddModMapEntry(info, &tmp) && ok;
1603 HandleSymbolsFile(XkbFile *file, struct xkb_keymap *keymap,
1604 unsigned merge, SymbolsInfo *info)
1609 info->name = uDupString(file->name);
1613 switch (stmt->stmtType)
1616 if (!HandleIncludeSymbols((IncludeStmt *) stmt, keymap, info,
1620 case StmtSymbolsDef:
1621 if (!HandleSymbolsDef((SymbolsDef *) stmt, keymap, info))
1625 if (!HandleSymbolsVar((VarDef *) stmt, keymap, info))
1629 if (!HandleVModDef((VModDef *) stmt, keymap, merge, &info->vmods))
1633 ERROR("Interpretation files may not include other types\n");
1634 ACTION("Ignoring definition of symbol interpretation\n");
1637 case StmtKeycodeDef:
1638 ERROR("Interpretation files may not include other types\n");
1639 ACTION("Ignoring definition of key name\n");
1643 if (!HandleModMapDef((ModMapDef *) stmt, keymap, info))
1647 WSGO("Unexpected statement type %d in HandleSymbolsFile\n",
1652 if (info->errorCount > 10)
1655 ERROR("Too many errors\n");
1657 ACTION("Abandoning symbols file \"%s\"\n", file->topName);
1664 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym,
1665 xkb_keycode_t *kc_rtrn)
1668 unsigned int group, level;
1670 for (key = keymap->min_key_code; key <= keymap->max_key_code; key++)
1672 for (group = 0; group < XkbKeyNumGroups(keymap, key); group++)
1674 for (level = 0; level < XkbKeyGroupWidth(keymap, key, group);
1677 if (XkbKeyNumSyms(keymap, key, group, level) != 1 ||
1678 (XkbKeySymEntry(keymap, key, group, level))[0] != sym)
1690 * Find the given name in the xkb->map->types and return its index.
1692 * @param atom The atom to search for.
1693 * @param type_rtrn Set to the index of the name if found.
1695 * @return true if found, false otherwise.
1698 FindNamedType(struct xkb_keymap *keymap, xkb_atom_t atom, unsigned *type_rtrn)
1701 const char *name = XkbcAtomText(atom);
1703 if (keymap && keymap->map && keymap->map->types)
1705 for (n = 0; n < keymap->map->num_types; n++)
1707 if (strcmp(keymap->map->types[n].name, name) == 0)
1718 * Assign a type to the given sym and return the Atom for the type assigned.
1721 * - ONE_LEVEL for width 0/1
1722 * - ALPHABETIC for 2 shift levels, with lower/upercase
1723 * - KEYPAD for keypad keys.
1724 * - TWO_LEVEL for other 2 shift level keys.
1725 * and the same for four level keys.
1727 * @param width Number of sysms in syms.
1728 * @param syms The keysyms for the given key (must be size width).
1729 * @param typeNameRtrn Set to the Atom of the type name.
1731 * @returns true if a type could be found, false otherwise.
1734 FindAutomaticType(struct xkb_keymap *keymap, int width, xkb_keysym_t *syms,
1735 xkb_atom_t *typeNameRtrn, bool *autoType)
1738 if ((width == 1) || (width == 0))
1740 *typeNameRtrn = xkb_atom_intern(keymap->context, "ONE_LEVEL");
1743 else if (width == 2)
1745 if (syms && XkbcKSIsLower(syms[0]) && XkbcKSIsUpper(syms[1]))
1747 *typeNameRtrn = xkb_atom_intern(keymap->context, "ALPHABETIC");
1749 else if (syms && (XkbKSIsKeypad(syms[0]) || XkbKSIsKeypad(syms[1])))
1751 *typeNameRtrn = xkb_atom_intern(keymap->context, "KEYPAD");
1756 *typeNameRtrn = xkb_atom_intern(keymap->context, "TWO_LEVEL");
1760 else if (width <= 4)
1762 if (syms && XkbcKSIsLower(syms[0]) && XkbcKSIsUpper(syms[1]))
1763 if (XkbcKSIsLower(syms[2]) && XkbcKSIsUpper(syms[3]))
1765 xkb_atom_intern(keymap->context, "FOUR_LEVEL_ALPHABETIC");
1767 *typeNameRtrn = xkb_atom_intern(keymap->context,
1768 "FOUR_LEVEL_SEMIALPHABETIC");
1770 else if (syms && (XkbKSIsKeypad(syms[0]) || XkbKSIsKeypad(syms[1])))
1771 *typeNameRtrn = xkb_atom_intern(keymap->context, "FOUR_LEVEL_KEYPAD");
1773 *typeNameRtrn = xkb_atom_intern(keymap->context, "FOUR_LEVEL");
1774 /* XXX: why not set autoType here? */
1776 return ((width >= 0) && (width <= 4));
1780 * Ensure the given KeyInfo is in a coherent state, i.e. no gaps between the
1781 * groups, and reduce to one group if all groups are identical anyway.
1784 PrepareKeyDef(KeyInfo * key)
1786 int i, j, width, defined, lastGroup;
1789 defined = key->symsDefined | key->actsDefined | key->typesDefined;
1790 /* get highest group number */
1791 for (i = XkbNumKbdGroups - 1; i >= 0; i--)
1793 if (defined & (1 << i))
1801 /* If there are empty groups between non-empty ones fill them with data */
1802 /* from the first group. */
1803 /* We can make a wrong assumption here. But leaving gaps is worse. */
1804 for (i = lastGroup; i > 0; i--)
1806 if (defined & (1 << i))
1808 width = key->numLevels[0];
1809 if (key->typesDefined & 1)
1811 for (j = 0; j < width; j++)
1813 key->types[i] = key->types[0];
1815 key->typesDefined |= 1 << i;
1817 if ((key->actsDefined & 1) && key->acts[0])
1819 key->acts[i] = uTypedCalloc(width, union xkb_action);
1820 if (key->acts[i] == NULL)
1822 memcpy(key->acts[i], key->acts[0],
1823 width * sizeof(union xkb_action));
1824 key->actsDefined |= 1 << i;
1826 if ((key->symsDefined & 1) && key->sizeSyms[0])
1828 key->syms[i] = uTypedCalloc(key->sizeSyms[0], xkb_keysym_t);
1829 if (key->syms[i] == NULL)
1831 memcpy(key->syms[i], key->syms[0],
1832 key->sizeSyms[0] * sizeof(xkb_keysym_t));
1833 key->symsMapIndex[i] = uTypedCalloc(width, int);
1834 if (!key->symsMapIndex[i])
1837 key->syms[i] = NULL;
1840 memcpy(key->symsMapIndex[i], key->symsMapIndex[0],
1841 width * sizeof(int));
1842 key->symsMapNumEntries[i] = uTypedCalloc(width, unsigned int);
1843 if (!key->symsMapNumEntries[i])
1846 key->syms[i] = NULL;
1847 free(key->symsMapIndex[i]);
1848 key->symsMapIndex[i] = NULL;
1851 memcpy(key->symsMapNumEntries[i], key->symsMapNumEntries[0],
1852 width * sizeof(int));
1853 key->sizeSyms[i] = key->sizeSyms[0];
1854 key->symsDefined |= 1 << i;
1858 key->numLevels[i] = key->numLevels[0];
1861 /* If all groups are completely identical remove them all */
1862 /* exept the first one. */
1864 for (i = lastGroup; i > 0; i--)
1866 if ((key->numLevels[i] != key->numLevels[0]) ||
1867 (key->types[i] != key->types[0]))
1872 if ((key->syms[i] != key->syms[0]) &&
1873 (key->syms[i] == NULL || key->syms[0] == NULL ||
1874 key->sizeSyms[i] != key->sizeSyms[0] ||
1875 memcmp(key->syms[i], key->syms[0],
1876 sizeof(xkb_keysym_t) * key->sizeSyms[0])))
1881 if ((key->symsMapIndex[i] != key->symsMapIndex[i]) &&
1882 (key->symsMapIndex[i] == NULL || key->symsMapIndex[0] == NULL ||
1883 memcmp(key->symsMapIndex[i], key->symsMapIndex[0],
1884 key->numLevels[0] * sizeof(int))))
1889 if ((key->symsMapNumEntries[i] != key->symsMapNumEntries[i]) &&
1890 (key->symsMapNumEntries[i] == NULL ||
1891 key->symsMapNumEntries[0] == NULL ||
1892 memcmp(key->symsMapNumEntries[i], key->symsMapNumEntries[0],
1893 key->numLevels[0] * sizeof(int))))
1898 if ((key->acts[i] != key->acts[0]) &&
1899 (key->acts[i] == NULL || key->acts[0] == NULL ||
1900 memcmp(key->acts[i], key->acts[0],
1901 sizeof(union xkb_action) * key->numLevels[0])))
1909 for (i = lastGroup; i > 0; i--)
1911 key->numLevels[i] = 0;
1913 key->syms[i] = NULL;
1914 key->sizeSyms[i] = 0;
1915 free(key->symsMapIndex[i]);
1916 key->symsMapIndex[i] = NULL;
1917 free(key->symsMapNumEntries[i]);
1918 key->symsMapNumEntries[i] = NULL;
1920 key->acts[i] = NULL;
1923 key->symsDefined &= 1;
1924 key->actsDefined &= 1;
1925 key->typesDefined &= 1;
1930 * Copy the KeyInfo into the keyboard description.
1932 * This function recurses.
1935 CopySymbolsDef(struct xkb_keymap *keymap, KeyInfo *key, int start_from)
1939 unsigned int sizeSyms = 0;
1940 unsigned width, tmp, nGroups;
1941 struct xkb_key_type * type;
1942 bool haveActions, autoType, useAlias;
1943 unsigned types[XkbNumKbdGroups];
1944 union xkb_action *outActs;
1945 unsigned int symIndex = 0;
1947 useAlias = (start_from == 0);
1949 /* get the keycode for the key. */
1950 if (!FindNamedKey(keymap, key->name, &kc, useAlias,
1951 CreateKeyNames(keymap), start_from))
1953 if ((start_from == 0) && (warningLevel >= 5))
1955 WARN("Key %s not found in keycodes\n", longText(key->name));
1956 ACTION("Symbols ignored\n");
1961 haveActions = false;
1962 for (i = width = nGroups = 0; i < XkbNumKbdGroups; i++)
1964 if (((i + 1) > nGroups)
1965 && (((key->symsDefined | key->actsDefined) & (1 << i))
1966 || (key->typesDefined) & (1 << i)))
1971 /* Assign the type to the key, if it is missing. */
1972 if (key->types[i] == XKB_ATOM_NONE)
1974 if (key->dfltType != XKB_ATOM_NONE)
1975 key->types[i] = key->dfltType;
1976 else if (FindAutomaticType(keymap, key->numLevels[i], key->syms[i],
1977 &key->types[i], &autoType))
1982 if (warningLevel >= 5)
1984 WARN("No automatic type for %d symbols\n",
1985 (unsigned int) key->numLevels[i]);
1986 ACTION("Using %s for the %s key (keycode %d)\n",
1987 XkbcAtomText(key->types[i]),
1988 longText(key->name), kc);
1992 if (FindNamedType(keymap, key->types[i], &types[i]))
1994 if (!autoType || key->numLevels[i] > 2)
1995 keymap->server->explicit[kc] |= (1 << i);
1999 if (warningLevel >= 3)
2001 WARN("Type \"%s\" is not defined\n",
2002 XkbcAtomText(key->types[i]));
2003 ACTION("Using TWO_LEVEL for the %s key (keycode %d)\n",
2004 longText(key->name), kc);
2006 types[i] = XkbTwoLevelIndex;
2008 /* if the type specifies fewer levels than the key has, shrink the key */
2009 type = &keymap->map->types[types[i]];
2010 if (type->num_levels < key->numLevels[i])
2012 if (warningLevel > 0)
2014 WARN("Type \"%s\" has %d levels, but %s has %d symbols\n",
2015 type->name, type->num_levels,
2016 XkbcAtomText(key->name), key->numLevels[i]);
2017 ACTION("Ignoring extra symbols\n");
2019 key->numLevels[i] = type->num_levels;
2021 if (key->numLevels[i] > width)
2022 width = key->numLevels[i];
2023 if (type->num_levels > width)
2024 width = type->num_levels;
2025 sizeSyms += key->sizeSyms[i];
2028 if (!XkbcResizeKeySyms(keymap, kc, sizeSyms))
2030 WSGO("Could not enlarge symbols for %s (keycode %d)\n",
2031 longText(key->name), kc);
2036 outActs = XkbcResizeKeyActions(keymap, kc, width * nGroups);
2037 if (outActs == NULL)
2039 WSGO("Could not enlarge actions for %s (key %d)\n",
2040 longText(key->name), kc);
2043 keymap->server->explicit[kc] |= XkbExplicitInterpretMask;
2047 if (key->defs.defined & _Key_GroupInfo)
2050 i = keymap->map->key_sym_map[kc].group_info;
2052 keymap->map->key_sym_map[kc].group_info = XkbSetNumGroups(i, nGroups);
2053 keymap->map->key_sym_map[kc].width = width;
2054 keymap->map->key_sym_map[kc].sym_index = uTypedCalloc(nGroups * width,
2056 keymap->map->key_sym_map[kc].num_syms = uTypedCalloc(nGroups * width,
2058 for (i = 0; i < nGroups; i++)
2060 /* assign kt_index[i] to the index of the type in map->types.
2061 * kt_index[i] may have been set by a previous run (if we have two
2062 * layouts specified). Let's not overwrite it with the ONE_LEVEL
2063 * default group if we dont even have keys for this group anyway.
2065 * FIXME: There should be a better fix for this.
2067 if (key->numLevels[i])
2068 keymap->map->key_sym_map[kc].kt_index[i] = types[i];
2069 if (key->sizeSyms[i] != 0)
2071 /* fill key to "width" symbols*/
2072 for (tmp = 0; tmp < width; tmp++)
2074 if (tmp < key->numLevels[i] && key->symsMapNumEntries[i][tmp])
2076 memcpy(&keymap->map->key_sym_map[kc].syms[symIndex],
2077 &key->syms[i][key->symsMapIndex[i][tmp]],
2078 key->symsMapNumEntries[i][tmp] *
2079 sizeof(xkb_keysym_t));
2080 keymap->map->key_sym_map[kc].sym_index[(i * width) + tmp] =
2082 keymap->map->key_sym_map[kc].num_syms[(i * width) + tmp] =
2083 key->symsMapNumEntries[i][tmp];
2085 keymap->map->key_sym_map[kc].num_syms[(i * width) + tmp];
2089 keymap->map->key_sym_map[kc].sym_index[(i * width) + tmp] = -1;
2090 keymap->map->key_sym_map[kc].num_syms[(i * width) + tmp] = 0;
2092 if ((outActs != NULL) && (key->acts[i] != NULL))
2094 if (tmp < key->numLevels[i])
2095 outActs[tmp] = key->acts[i][tmp];
2097 outActs[tmp].type = XkbSA_NoAction;
2102 switch (key->behavior.type & XkbKB_OpMask)
2107 keymap->server->behaviors[kc] = key->behavior;
2108 keymap->server->explicit[kc] |= XkbExplicitBehaviorMask;
2111 if (key->defs.defined & _Key_VModMap)
2113 keymap->server->vmodmap[kc] = key->vmodmap;
2114 keymap->server->explicit[kc] |= XkbExplicitVModMapMask;
2116 if (key->repeat != RepeatUndefined)
2118 if (key->repeat == RepeatYes)
2119 keymap->ctrls->per_key_repeat[kc / 8] |= (1 << (kc % 8));
2121 keymap->ctrls->per_key_repeat[kc / 8] &= ~(1 << (kc % 8));
2122 keymap->server->explicit[kc] |= XkbExplicitAutoRepeatMask;
2125 if (nGroups > keymap->ctrls->num_groups)
2126 keymap->ctrls->num_groups = nGroups;
2128 /* do the same thing for the next key */
2129 CopySymbolsDef(keymap, key, kc + 1);
2134 CopyModMapDef(struct xkb_keymap *keymap, ModMapEntry *entry)
2138 if (!entry->haveSymbol &&
2139 !FindNamedKey(keymap, entry->u.keyName, &kc, true,
2140 CreateKeyNames(keymap), 0))
2142 if (warningLevel >= 5)
2144 WARN("Key %s not found in keycodes\n",
2145 longText(entry->u.keyName));
2146 ACTION("Modifier map entry for %s not updated\n",
2147 XkbcModIndexText(entry->modifier));
2151 else if (entry->haveSymbol &&
2152 !FindKeyForSymbol(keymap, entry->u.keySym, &kc))
2154 if (warningLevel > 5)
2156 WARN("Key \"%s\" not found in symbol map\n",
2157 XkbcKeysymText(entry->u.keySym));
2158 ACTION("Modifier map entry for %s not updated\n",
2159 XkbcModIndexText(entry->modifier));
2163 keymap->map->modmap[kc] |= (1 << entry->modifier);
2168 * Handle the xkb_symbols section of an xkb file.
2170 * @param file The parsed xkb_symbols section of the xkb file.
2171 * @param keymap Handle to the keyboard description to store the symbols in.
2172 * @param merge Merge strategy (e.g. MergeOverride).
2175 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap, unsigned merge)
2181 InitSymbolsInfo(&info, keymap);
2182 info.dflt.defs.fileID = file->id;
2183 info.dflt.defs.merge = merge;
2185 HandleSymbolsFile(file, keymap, merge, &info);
2187 if (info.nKeys == 0)
2190 if (info.errorCount != 0)
2193 /* alloc memory in the xkb struct */
2194 if (XkbcAllocNames(keymap, XkbGroupNamesMask, 0) != Success) {
2195 WSGO("Can not allocate names in CompileSymbols\n");
2196 ACTION("Symbols not added\n");
2200 if (XkbcAllocClientMap(keymap, XkbKeySymsMask | XkbModifierMapMask, 0)
2202 WSGO("Could not allocate client map in CompileSymbols\n");
2203 ACTION("Symbols not added\n");
2207 if (XkbcAllocServerMap(keymap, XkbAllServerInfoMask, 32) != Success) {
2208 WSGO("Could not allocate server map in CompileSymbols\n");
2209 ACTION("Symbols not added\n");
2213 if (XkbcAllocControls(keymap) != Success) {
2214 WSGO("Could not allocate controls in CompileSymbols\n");
2215 ACTION("Symbols not added\n");
2219 /* now copy info into xkb. */
2220 ApplyAliases(keymap, &info.aliases);
2222 for (i = 0; i < XkbNumKbdGroups; i++) {
2223 if (info.groupNames[i] != XKB_ATOM_NONE) {
2224 free(UNCONSTIFY(keymap->names->groups[i]));
2225 keymap->names->groups[i] = xkb_atom_strdup(keymap->context,
2226 info.groupNames[i]);
2231 for (key = info.keys, i = 0; i < info.nKeys; i++, key++)
2235 for (key = info.keys, i = 0; i < info.nKeys; i++, key++)
2236 if (!CopySymbolsDef(keymap, key, 0))
2239 if (warningLevel > 3) {
2240 for (i = keymap->min_key_code; i <= keymap->max_key_code; i++) {
2241 if (keymap->names->keys[i].name[0] == '\0')
2244 if (XkbKeyNumGroups(keymap, i) < 1) {
2246 memcpy(buf, keymap->names->keys[i].name, 4);
2248 WARN("No symbols defined for <%s> (keycode %d)\n", buf, i);
2254 ModMapEntry *mm, *next;
2255 for (mm = info.modMap; mm != NULL; mm = next) {
2256 if (!CopyModMapDef(keymap, mm))
2258 next = (ModMapEntry *) mm->defs.next;
2262 FreeSymbolsInfo(&info);
2266 FreeSymbolsInfo(&info);