1 /************************************************************
2 * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
4 * Permission to use, copy, modify, and distribute this
5 * software and its documentation for any purpose and without
6 * fee is hereby granted, provided that the above copyright
7 * notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting
9 * documentation, and that the name of Silicon Graphics not be
10 * used in advertising or publicity pertaining to distribution
11 * of the software without specific prior written permission.
12 * Silicon Graphics makes no representation about the suitability
13 * of this software for any purpose. It is provided "as is"
14 * without any express or implied warranty.
16 * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
23 * THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 ********************************************************/
29 #include "xkbcomp-priv.h"
30 #include "parseutils.h"
35 /***====================================================================***/
37 /* Needed to work with the typechecker. */
38 typedef darray (xkb_keysym_t) darray_xkb_keysym_t;
39 typedef darray (union xkb_action) darray_xkb_action;
43 #define RepeatUndefined ~((unsigned) 0)
45 #define _Key_Syms (1 << 0)
46 #define _Key_Acts (1 << 1)
47 #define _Key_Repeat (1 << 2)
48 #define _Key_Behavior (1 << 3)
49 #define _Key_Type_Dflt (1 << 4)
50 #define _Key_Types (1 << 5)
51 #define _Key_GroupInfo (1 << 6)
52 #define _Key_VModMap (1 << 7)
54 typedef struct _KeyInfo {
56 unsigned long name; /* the 4 chars of the key name, as long */
57 unsigned char typesDefined;
58 unsigned char symsDefined;
59 unsigned char actsDefined;
60 unsigned int numLevels[XkbNumKbdGroups];
62 /* syms[group] -> Single array for all the keysyms in the group. */
63 darray_xkb_keysym_t syms[XkbNumKbdGroups];
65 * symsMapIndex[group][level] -> The index from which the syms for
66 * the level begin in the syms[group] array. Remember each keycode
67 * can have multiple keysyms in each level (that is, each key press
68 * can result in multiple keysyms).
70 darray(int) symsMapIndex[XkbNumKbdGroups];
72 * symsMapNumEntries[group][level] -> How many syms are in
73 * syms[group][symsMapIndex[group][level]].
75 darray(size_t) symsMapNumEntries[XkbNumKbdGroups];
77 darray_xkb_action acts[XkbNumKbdGroups];
79 xkb_atom_t types[XkbNumKbdGroups];
81 struct xkb_behavior behavior;
82 unsigned short vmodmap;
85 uint8_t out_of_range_group_action;
86 uint8_t out_of_range_group_number;
90 * Init the given key info to sane values.
93 InitKeyInfo(KeyInfo *keyi, unsigned file_id)
96 static const char dflt[4] = "*";
98 keyi->defs.defined = 0;
99 keyi->defs.file_id = file_id;
100 keyi->defs.merge = MERGE_OVERRIDE;
101 keyi->defs.next = NULL;
102 keyi->name = KeyNameToLong(dflt);
103 keyi->typesDefined = keyi->symsDefined = keyi->actsDefined = 0;
105 for (i = 0; i < XkbNumKbdGroups; i++) {
106 keyi->numLevels[i] = 0;
107 keyi->types[i] = XKB_ATOM_NONE;
108 darray_init(keyi->syms[i]);
109 darray_init(keyi->symsMapIndex[i]);
110 darray_init(keyi->symsMapNumEntries[i]);
111 darray_init(keyi->acts[i]);
114 keyi->dfltType = XKB_ATOM_NONE;
115 keyi->behavior.type = XkbKB_Default;
116 keyi->behavior.data = 0;
118 keyi->repeat = RepeatUndefined;
119 keyi->out_of_range_group_action = 0;
120 keyi->out_of_range_group_number = 0;
124 FreeKeyInfo(KeyInfo *keyi)
128 for (i = 0; i < XkbNumKbdGroups; i++) {
129 darray_free(keyi->syms[i]);
130 darray_free(keyi->symsMapIndex[i]);
131 darray_free(keyi->symsMapNumEntries[i]);
132 darray_free(keyi->acts[i]);
137 * Copy old into new, optionally reset old to 0.
138 * If old is reset, new simply re-uses old's memory. Otherwise, the memory is
139 * newly allocated and new points to the new memory areas.
142 CopyKeyInfo(KeyInfo * old, KeyInfo * new, bool clearOld)
147 new->defs.next = NULL;
150 for (i = 0; i < XkbNumKbdGroups; i++) {
151 old->numLevels[i] = 0;
152 darray_init(old->symsMapIndex[i]);
153 darray_init(old->symsMapNumEntries[i]);
154 darray_init(old->syms[i]);
155 darray_init(old->acts[i]);
159 for (i = 0; i < XkbNumKbdGroups; i++) {
160 darray_copy(new->syms[i], old->syms[i]);
161 darray_copy(new->symsMapIndex[i], old->symsMapIndex[i]);
162 darray_copy(new->symsMapNumEntries[i], old->symsMapNumEntries[i]);
163 darray_copy(new->acts[i], old->acts[i]);
170 /***====================================================================***/
172 typedef struct _ModMapEntry {
174 enum merge_mode merge;
178 unsigned long keyName;
183 typedef struct _SymbolsInfo {
184 char *name; /* e.g. pc+us+inet(evdev) */
187 enum merge_mode merge;
188 unsigned explicit_group;
189 darray(KeyInfo) keys;
193 xkb_atom_t groupNames[XkbNumKbdGroups];
199 InitSymbolsInfo(SymbolsInfo * info, struct xkb_keymap *keymap,
205 info->explicit_group = 0;
206 info->errorCount = 0;
207 info->file_id = file_id;
208 info->merge = MERGE_OVERRIDE;
209 darray_init(info->keys);
210 darray_growalloc(info->keys, 110);
211 list_init(&info->modMaps);
212 for (i = 0; i < XkbNumKbdGroups; i++)
213 info->groupNames[i] = XKB_ATOM_NONE;
214 InitKeyInfo(&info->dflt, file_id);
215 InitVModInfo(&info->vmods, keymap);
220 FreeSymbolsInfo(SymbolsInfo * info)
223 ModMapEntry *mm, *next;
226 darray_foreach(keyi, info->keys) {
229 darray_free(info->keys);
230 list_foreach_safe(mm, next, &info->modMaps, entry)
232 memset(info, 0, sizeof(SymbolsInfo));
236 ResizeKeyGroup(KeyInfo *keyi, unsigned int group, unsigned int numLevels,
237 unsigned sizeSyms, bool forceActions)
241 if (darray_size(keyi->syms[group]) < sizeSyms)
242 darray_resize0(keyi->syms[group], sizeSyms);
244 if (darray_empty(keyi->symsMapIndex[group]) ||
245 keyi->numLevels[group] < numLevels) {
246 darray_resize(keyi->symsMapIndex[group], numLevels);
247 for (i = keyi->numLevels[group]; i < numLevels; i++)
248 darray_item(keyi->symsMapIndex[group], i) = -1;
251 if (darray_empty(keyi->symsMapNumEntries[group]) ||
252 keyi->numLevels[group] < numLevels)
253 darray_resize0(keyi->symsMapNumEntries[group], numLevels);
255 if ((forceActions && (keyi->numLevels[group] < numLevels ||
256 darray_empty(keyi->acts[group]))) ||
257 (keyi->numLevels[group] < numLevels && !darray_empty(keyi->acts[group])))
258 darray_resize0(keyi->acts[group], numLevels);
260 if (keyi->numLevels[group] < numLevels)
261 keyi->numLevels[group] = numLevels;
266 enum key_group_selector {
273 MergeKeyGroups(SymbolsInfo * info,
274 KeyInfo * into, KeyInfo * from, unsigned group)
276 darray_xkb_keysym_t resultSyms;
277 enum key_group_selector using = NONE;
278 darray_xkb_action resultActs;
279 unsigned int resultWidth;
280 unsigned int resultSize = 0;
283 bool report, clobber;
285 clobber = (from->defs.merge != MERGE_AUGMENT);
287 report = (warningLevel > 9) ||
288 ((into->defs.file_id == from->defs.file_id) && (warningLevel > 0));
290 darray_init(resultSyms);
292 if (into->numLevels[group] >= from->numLevels[group]) {
293 resultActs = into->acts[group];
294 resultWidth = into->numLevels[group];
297 resultActs = from->acts[group];
298 resultWidth = from->numLevels[group];
299 darray_resize(into->symsMapIndex[group],
300 from->numLevels[group]);
301 darray_resize0(into->symsMapNumEntries[group],
302 from->numLevels[group]);
304 for (i = into->numLevels[group]; i < from->numLevels[group]; i++)
305 darray_item(into->symsMapIndex[group], i) = -1;
308 if (darray_empty(resultActs) && (!darray_empty(into->acts[group]) ||
309 !darray_empty(from->acts[group]))) {
310 darray_resize0(resultActs, resultWidth);
311 for (i = 0; i < resultWidth; i++) {
312 union xkb_action *fromAct = NULL, *toAct = NULL;
314 if (!darray_empty(from->acts[group]))
315 fromAct = &darray_item(from->acts[group], i);
317 if (!darray_empty(into->acts[group]))
318 toAct = &darray_item(into->acts[group], i);
320 if (((fromAct == NULL) || (fromAct->type == XkbSA_NoAction))
321 && (toAct != NULL)) {
322 darray_item(resultActs, i) = *toAct;
324 else if (((toAct == NULL) || (toAct->type == XkbSA_NoAction))
325 && (fromAct != NULL)) {
326 darray_item(resultActs, i) = *fromAct;
329 union xkb_action *use, *ignore;
340 ("Multiple actions for level %d/group %d on key %s\n",
341 i + 1, group + 1, longText(into->name));
342 ACTION("Using %s, ignoring %s\n",
343 XkbcActionTypeText(use->type),
344 XkbcActionTypeText(ignore->type));
347 darray_item(resultActs, i) = *use;
352 for (i = 0; i < resultWidth; i++) {
353 unsigned int fromSize = 0;
356 if (!darray_empty(from->symsMapNumEntries[group]) &&
357 i < from->numLevels[group])
358 fromSize = darray_item(from->symsMapNumEntries[group], i);
360 if (!darray_empty(into->symsMapNumEntries[group]) &&
361 i < into->numLevels[group])
362 toSize = darray_item(into->symsMapNumEntries[group], i);
365 resultSize += toSize;
368 else if (toSize == 0 || clobber) {
369 resultSize += fromSize;
373 resultSize += toSize;
382 resultSyms = from->syms[group];
383 darray_free(into->symsMapNumEntries[group]);
384 darray_free(into->symsMapIndex[group]);
385 into->symsMapNumEntries[group] = from->symsMapNumEntries[group];
386 into->symsMapIndex[group] = from->symsMapIndex[group];
387 darray_init(from->symsMapNumEntries[group]);
388 darray_init(from->symsMapIndex[group]);
391 else if (using == TO) {
392 resultSyms = into->syms[group];
396 darray_resize0(resultSyms, resultSize);
398 for (i = 0; i < resultWidth; i++) {
399 enum key_group_selector use = NONE;
400 unsigned int fromSize = 0;
401 unsigned int toSize = 0;
403 if (i < from->numLevels[group])
404 fromSize = darray_item(from->symsMapNumEntries[group], i);
406 if (i < into->numLevels[group])
407 toSize = darray_item(into->symsMapNumEntries[group], i);
409 if (fromSize == 0 && toSize == 0) {
410 darray_item(into->symsMapIndex[group], i) = -1;
411 darray_item(into->symsMapNumEntries[group], i) = 0;
417 else if (toSize == 0 || clobber)
422 if (toSize && fromSize && report) {
423 INFO("Multiple symbols for group %d, level %d on key %s\n",
424 group + 1, i + 1, longText(into->name));
425 ACTION("Using %s, ignoring %s\n",
426 (use == FROM ? "from" : "to"),
427 (use == FROM ? "to" : "from"));
431 memcpy(darray_mem(resultSyms, cur_idx),
432 darray_mem(from->syms[group],
433 darray_item(from->symsMapIndex[group], i)),
434 darray_item(from->symsMapNumEntries[group],
435 i) * sizeof(xkb_keysym_t));
436 darray_item(into->symsMapIndex[group], i) = cur_idx;
437 darray_item(into->symsMapNumEntries[group], i) =
438 darray_item(from->symsMapNumEntries[group], i);
441 memcpy(darray_mem(resultSyms, cur_idx),
442 darray_mem(into->syms[group],
443 darray_item(into->symsMapIndex[group], i)),
444 darray_item(into->symsMapNumEntries[group],
445 i) * sizeof(xkb_keysym_t));
446 darray_item(into->symsMapIndex[group], i) = cur_idx;
448 cur_idx += darray_item(into->symsMapNumEntries[group], i);
452 if (!darray_same(resultActs, into->acts[group]))
453 darray_free(into->acts[group]);
454 if (!darray_same(resultActs, from->acts[group]))
455 darray_free(from->acts[group]);
456 into->numLevels[group] = resultWidth;
457 if (!darray_same(resultSyms, into->syms[group]))
458 darray_free(into->syms[group]);
459 into->syms[group] = resultSyms;
460 if (!darray_same(resultSyms, from->syms[group]))
461 darray_free(from->syms[group]);
462 darray_init(from->syms[group]);
463 darray_free(from->symsMapIndex[group]);
464 darray_free(from->symsMapNumEntries[group]);
465 into->acts[group] = resultActs;
466 darray_init(from->acts[group]);
467 if (!darray_empty(into->syms[group]))
468 into->symsDefined |= (1 << group);
469 from->symsDefined &= ~(1 << group);
470 into->actsDefined |= (1 << group);
471 from->actsDefined &= ~(1 << group);
477 MergeKeys(SymbolsInfo *info, struct xkb_keymap *keymap,
478 KeyInfo *into, KeyInfo *from)
481 unsigned collide = 0;
484 if (from->defs.merge == MERGE_REPLACE) {
485 for (i = 0; i < XkbNumKbdGroups; i++) {
486 if (into->numLevels[i] != 0) {
487 darray_free(into->syms[i]);
488 darray_free(into->acts[i]);
492 memset(from, 0, sizeof(KeyInfo));
495 report = ((warningLevel > 9) ||
496 ((into->defs.file_id == from->defs.file_id)
497 && (warningLevel > 0)));
498 for (i = 0; i < XkbNumKbdGroups; i++) {
499 if (from->numLevels[i] > 0) {
500 if (into->numLevels[i] == 0) {
501 into->numLevels[i] = from->numLevels[i];
502 into->syms[i] = from->syms[i];
503 into->symsMapIndex[i] = from->symsMapIndex[i];
504 into->symsMapNumEntries[i] = from->symsMapNumEntries[i];
505 into->acts[i] = from->acts[i];
506 into->symsDefined |= (1 << i);
507 darray_init(from->syms[i]);
508 darray_init(from->symsMapIndex[i]);
509 darray_init(from->symsMapNumEntries[i]);
510 darray_init(from->acts[i]);
511 from->numLevels[i] = 0;
512 from->symsDefined &= ~(1 << i);
513 if (!darray_empty(into->syms[i]))
514 into->defs.defined |= _Key_Syms;
515 if (!darray_empty(into->acts[i]))
516 into->defs.defined |= _Key_Acts;
520 if (!darray_empty(into->syms[i]))
521 collide |= _Key_Syms;
522 if (!darray_empty(into->acts[i]))
523 collide |= _Key_Acts;
525 MergeKeyGroups(info, into, from, (unsigned) i);
528 if (from->types[i] != XKB_ATOM_NONE) {
529 if ((into->types[i] != XKB_ATOM_NONE) && report &&
530 (into->types[i] != from->types[i])) {
531 xkb_atom_t use, ignore;
532 collide |= _Key_Types;
533 if (from->defs.merge != MERGE_AUGMENT) {
534 use = from->types[i];
535 ignore = into->types[i];
538 use = into->types[i];
539 ignore = from->types[i];
542 ("Multiple definitions for group %d type of key %s\n",
543 i, longText(into->name));
544 ACTION("Using %s, ignoring %s\n",
545 xkb_atom_text(keymap->ctx, use),
546 xkb_atom_text(keymap->ctx, ignore));
548 if ((from->defs.merge != MERGE_AUGMENT)
549 || (into->types[i] == XKB_ATOM_NONE)) {
550 into->types[i] = from->types[i];
554 if (UseNewField(_Key_Behavior, &into->defs, &from->defs, &collide)) {
555 into->behavior = from->behavior;
556 into->defs.defined |= _Key_Behavior;
558 if (UseNewField(_Key_VModMap, &into->defs, &from->defs, &collide)) {
559 into->vmodmap = from->vmodmap;
560 into->defs.defined |= _Key_VModMap;
562 if (UseNewField(_Key_Repeat, &into->defs, &from->defs, &collide)) {
563 into->repeat = from->repeat;
564 into->defs.defined |= _Key_Repeat;
566 if (UseNewField(_Key_Type_Dflt, &into->defs, &from->defs, &collide)) {
567 into->dfltType = from->dfltType;
568 into->defs.defined |= _Key_Type_Dflt;
570 if (UseNewField(_Key_GroupInfo, &into->defs, &from->defs, &collide)) {
571 into->out_of_range_group_action = from->out_of_range_group_action;
572 into->out_of_range_group_number = from->out_of_range_group_number;
573 into->defs.defined |= _Key_GroupInfo;
576 WARN("Symbol map for key %s redefined\n",
577 longText(into->name));
578 ACTION("Using %s definition for conflicting fields\n",
579 (from->defs.merge == MERGE_AUGMENT ? "first" : "last"));
585 AddKeySymbols(SymbolsInfo *info, KeyInfo *keyi, struct xkb_keymap *keymap)
587 unsigned long real_name;
590 darray_foreach(iter, info->keys)
591 if (iter->name == keyi->name)
592 return MergeKeys(info, keymap, iter, keyi);
594 if (FindKeyNameForAlias(keymap, keyi->name, &real_name))
595 darray_foreach(iter, info->keys)
596 if (iter->name == real_name)
597 return MergeKeys(info, keymap, iter, keyi);
599 darray_resize0(info->keys, darray_size(info->keys) + 1);
600 new = &darray_item(info->keys, darray_size(info->keys) - 1);
601 return CopyKeyInfo(keyi, new, true);
605 AddModMapEntry(SymbolsInfo * info, ModMapEntry * new)
610 clobber = (new->merge != MERGE_AUGMENT);
611 list_foreach(mm, &info->modMaps, entry) {
612 if (new->haveSymbol && mm->haveSymbol
613 && (new->u.keySym == mm->u.keySym)) {
614 unsigned use, ignore;
615 if (mm->modifier != new->modifier) {
618 ignore = mm->modifier;
622 ignore = new->modifier;
625 ("%s added to symbol map for multiple modifiers\n",
626 XkbcKeysymText(new->u.keySym));
627 ACTION("Using %s, ignoring %s.\n",
628 XkbcModIndexText(use),
629 XkbcModIndexText(ignore));
634 if ((!new->haveSymbol) && (!mm->haveSymbol) &&
635 (new->u.keyName == mm->u.keyName)) {
636 unsigned use, ignore;
637 if (mm->modifier != new->modifier) {
640 ignore = mm->modifier;
644 ignore = new->modifier;
646 ERROR("Key %s added to map for multiple modifiers\n",
647 longText(new->u.keyName));
648 ACTION("Using %s, ignoring %s.\n",
649 XkbcModIndexText(use),
650 XkbcModIndexText(ignore));
657 mm = malloc(sizeof(*mm));
659 WSGO("Could not allocate modifier map entry\n");
660 ACTION("Modifier map for %s will be incomplete\n",
661 XkbcModIndexText(new->modifier));
666 list_add(&mm->entry, &info->modMaps);
670 /***====================================================================***/
673 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
674 enum merge_mode merge, struct xkb_keymap *keymap)
678 ModMapEntry *mm, *next;
680 if (from->errorCount > 0) {
681 into->errorCount += from->errorCount;
684 if (into->name == NULL) {
685 into->name = from->name;
688 for (i = 0; i < XkbNumKbdGroups; i++) {
689 if (from->groupNames[i] != XKB_ATOM_NONE) {
690 if ((merge != MERGE_AUGMENT) ||
691 (into->groupNames[i] == XKB_ATOM_NONE))
692 into->groupNames[i] = from->groupNames[i];
696 darray_foreach(keyi, from->keys) {
697 if (merge != MERGE_DEFAULT)
698 keyi->defs.merge = merge;
700 if (!AddKeySymbols(into, keyi, keymap))
704 list_foreach_safe(mm, next, &from->modMaps, entry) {
705 if (merge != MERGE_DEFAULT)
707 if (!AddModMapEntry(into, mm))
711 list_init(&from->modMaps);
715 HandleSymbolsFile(XkbFile *file, struct xkb_keymap *keymap,
716 enum merge_mode merge,
720 HandleIncludeSymbols(IncludeStmt *stmt, struct xkb_keymap *keymap,
723 enum merge_mode newMerge;
725 SymbolsInfo included;
729 if ((stmt->file == NULL) && (stmt->map == NULL)) {
732 memset(info, 0, sizeof(SymbolsInfo));
734 else if (ProcessIncludeFile(keymap->ctx, stmt, FILE_TYPE_SYMBOLS, &rtrn,
736 InitSymbolsInfo(&included, keymap, rtrn->id);
737 included.merge = included.dflt.defs.merge = MERGE_OVERRIDE;
738 if (stmt->modifier) {
739 included.explicit_group = atoi(stmt->modifier) - 1;
742 included.explicit_group = info->explicit_group;
744 HandleSymbolsFile(rtrn, keymap, MERGE_OVERRIDE, &included);
745 if (stmt->stmt != NULL) {
747 included.name = stmt->stmt;
753 info->errorCount += 10;
756 if ((stmt->next != NULL) && (included.errorCount < 1)) {
759 SymbolsInfo next_incl;
761 for (next = stmt->next; next != NULL; next = next->next) {
762 if ((next->file == NULL) && (next->map == NULL)) {
764 MergeIncludedSymbols(&included, info, next->merge, keymap);
765 FreeSymbolsInfo(info);
767 else if (ProcessIncludeFile(keymap->ctx, next, FILE_TYPE_SYMBOLS,
769 InitSymbolsInfo(&next_incl, keymap, rtrn->id);
770 next_incl.merge = next_incl.dflt.defs.merge = MERGE_OVERRIDE;
771 if (next->modifier) {
772 next_incl.explicit_group = atoi(next->modifier) - 1;
775 next_incl.explicit_group = info->explicit_group;
777 HandleSymbolsFile(rtrn, keymap, MERGE_OVERRIDE, &next_incl);
778 MergeIncludedSymbols(&included, &next_incl, op, keymap);
779 FreeSymbolsInfo(&next_incl);
783 info->errorCount += 10;
784 FreeSymbolsInfo(&included);
789 else if (stmt->next) {
790 info->errorCount += included.errorCount;
795 MergeIncludedSymbols(info, &included, newMerge, keymap);
796 FreeSymbolsInfo(&included);
798 return (info->errorCount == 0);
805 GetGroupIndex(KeyInfo *keyi, struct xkb_keymap *keymap,
806 ExprDef * arrayNdx, unsigned what, unsigned *ndx_rtrn)
816 if (arrayNdx == NULL) {
820 defined = keyi->symsDefined;
822 defined = keyi->actsDefined;
824 for (i = 0; i < XkbNumKbdGroups; i++) {
825 if ((defined & (1 << i)) == 0) {
830 ERROR("Too many groups of %s for key %s (max %d)\n", name,
831 longText(keyi->name), XkbNumKbdGroups + 1);
832 ACTION("Ignoring %s defined for extra groups\n", name);
835 if (!ExprResolveGroup(keymap->ctx, arrayNdx, &tmp)) {
836 ERROR("Illegal group index for %s of key %s\n", name,
837 longText(keyi->name));
838 ACTION("Definition with non-integer array index ignored\n");
841 *ndx_rtrn = tmp.uval - 1;
846 AddSymbolsToKey(KeyInfo *keyi, struct xkb_keymap *keymap,
847 ExprDef *arrayNdx, ExprDef *value, SymbolsInfo *info)
849 unsigned ndx, nSyms, nLevels;
853 if (!GetGroupIndex(keyi, keymap, arrayNdx, SYMBOLS, &ndx))
856 keyi->symsDefined |= (1 << ndx);
859 if (value->op != ExprKeysymList) {
860 ERROR("Expected a list of symbols, found %s\n", exprOpText(value->op));
861 ACTION("Ignoring symbols for group %d of %s\n", ndx + 1,
862 longText(keyi->name));
865 if (!darray_empty(keyi->syms[ndx])) {
866 ERROR("Symbols for key %s, group %d already defined\n",
867 longText(keyi->name), ndx + 1);
868 ACTION("Ignoring duplicate definition\n");
871 nSyms = darray_size(value->value.list.syms);
872 nLevels = darray_size(value->value.list.symsMapIndex);
873 if ((keyi->numLevels[ndx] < nSyms || darray_empty(keyi->syms[ndx])) &&
874 (!ResizeKeyGroup(keyi, ndx, nLevels, nSyms, false))) {
875 WSGO("Could not resize group %d of key %s to contain %d levels\n",
876 ndx + 1, longText(keyi->name), nSyms);
877 ACTION("Symbols lost\n");
880 keyi->symsDefined |= (1 << ndx);
881 for (i = 0; i < nLevels; i++) {
882 darray_item(keyi->symsMapIndex[ndx], i) =
883 darray_item(value->value.list.symsMapIndex, i);
884 darray_item(keyi->symsMapNumEntries[ndx], i) =
885 darray_item(value->value.list.symsNumEntries, i);
887 for (j = 0; j < darray_item(keyi->symsMapNumEntries[ndx], i); j++) {
888 /* FIXME: What's abort() doing here? */
889 if (darray_item(keyi->symsMapIndex[ndx], i) + j >= nSyms)
891 if (!LookupKeysym(darray_item(value->value.list.syms,
892 darray_item(value->value.list.symsMapIndex,
894 &darray_item(keyi->syms[ndx],
895 darray_item(keyi->symsMapIndex[ndx],
898 "Could not resolve keysym %s for key %s, group %d (%s), level %d\n",
899 darray_item(value->value.list.syms, i),
900 longText(keyi->name),
902 xkb_atom_text(keymap->ctx, info->groupNames[ndx]), nSyms);
904 darray_item(keyi->syms[ndx],
905 darray_item(keyi->symsMapIndex[ndx],
906 i) + j) = XKB_KEY_NoSymbol;
907 darray_item(keyi->symsMapIndex[ndx], i) = -1;
908 darray_item(keyi->symsMapNumEntries[ndx], i) = 0;
911 if (darray_item(keyi->symsMapNumEntries[ndx], i) == 1 &&
912 darray_item(keyi->syms[ndx],
913 darray_item(keyi->symsMapIndex[ndx],
914 i) + j) == XKB_KEY_NoSymbol) {
915 darray_item(keyi->symsMapIndex[ndx], i) = -1;
916 darray_item(keyi->symsMapNumEntries[ndx], i) = 0;
920 for (j = keyi->numLevels[ndx] - 1;
921 j >= 0 && darray_item(keyi->symsMapNumEntries[ndx], j) == 0; j--)
922 keyi->numLevels[ndx]--;
927 AddActionsToKey(KeyInfo *keyi, struct xkb_keymap *keymap, ExprDef *arrayNdx,
928 ExprDef *value, SymbolsInfo *info)
933 struct xkb_any_action *toAct;
935 if (!GetGroupIndex(keyi, keymap, arrayNdx, ACTIONS, &ndx))
939 keyi->actsDefined |= (1 << ndx);
942 if (value->op != ExprActionList) {
943 WSGO("Bad expression type (%d) for action list value\n", value->op);
944 ACTION("Ignoring actions for group %d of %s\n", ndx,
945 longText(keyi->name));
948 if (!darray_empty(keyi->acts[ndx])) {
949 WSGO("Actions for key %s, group %d already defined\n",
950 longText(keyi->name), ndx);
953 for (nActs = 0, act = value->value.child; act != NULL; nActs++) {
954 act = (ExprDef *) act->common.next;
957 WSGO("Action list but not actions in AddActionsToKey\n");
960 if ((keyi->numLevels[ndx] < nActs || darray_empty(keyi->acts[ndx])) &&
961 !ResizeKeyGroup(keyi, ndx, nActs, nActs, true)) {
962 WSGO("Could not resize group %d of key %s\n", ndx,
963 longText(keyi->name));
964 ACTION("Actions lost\n");
967 keyi->actsDefined |= (1 << ndx);
969 toAct = (struct xkb_any_action *) darray_mem(keyi->acts[ndx], 0);
970 act = value->value.child;
971 for (i = 0; i < nActs; i++, toAct++) {
972 if (!HandleActionDef(act, keymap, toAct, info->action)) {
973 ERROR("Illegal action definition for %s\n",
974 longText(keyi->name));
975 ACTION("Action for group %d/level %d ignored\n", ndx + 1, i + 1);
977 act = (ExprDef *) act->common.next;
982 static const LookupEntry lockingEntries[] = {
983 { "true", XkbKB_Lock },
984 { "yes", XkbKB_Lock },
985 { "on", XkbKB_Lock },
986 { "false", XkbKB_Default },
987 { "no", XkbKB_Default },
988 { "off", XkbKB_Default },
989 { "permanent", XkbKB_Lock | XkbKB_Permanent },
993 static const LookupEntry repeatEntries[] = {
994 { "true", RepeatYes },
995 { "yes", RepeatYes },
997 { "false", RepeatNo },
1000 { "default", RepeatUndefined },
1005 SetSymbolsField(KeyInfo *keyi, struct xkb_keymap *keymap, char *field,
1006 ExprDef *arrayNdx, ExprDef *value, SymbolsInfo *info)
1011 if (strcasecmp(field, "type") == 0) {
1013 if ((!ExprResolveString(keymap->ctx, value, &tmp))
1014 && (warningLevel > 0)) {
1015 WARN("The type field of a key symbol map must be a string\n");
1016 ACTION("Ignoring illegal type definition\n");
1018 if (arrayNdx == NULL) {
1019 keyi->dfltType = xkb_atom_intern(keymap->ctx, tmp.str);
1020 keyi->defs.defined |= _Key_Type_Dflt;
1022 else if (!ExprResolveGroup(keymap->ctx, arrayNdx, &ndx)) {
1023 ERROR("Illegal group index for type of key %s\n",
1024 longText(keyi->name));
1025 ACTION("Definition with non-integer array index ignored\n");
1030 keyi->types[ndx.uval - 1] = xkb_atom_intern(keymap->ctx, tmp.str);
1031 keyi->typesDefined |= (1 << (ndx.uval - 1));
1035 else if (strcasecmp(field, "symbols") == 0)
1036 return AddSymbolsToKey(keyi, keymap, arrayNdx, value, info);
1037 else if (strcasecmp(field, "actions") == 0)
1038 return AddActionsToKey(keyi, keymap, arrayNdx, value, info);
1039 else if ((strcasecmp(field, "vmods") == 0) ||
1040 (strcasecmp(field, "virtualmods") == 0) ||
1041 (strcasecmp(field, "virtualmodifiers") == 0)) {
1042 ok = ExprResolveVModMask(value, &tmp, keymap);
1044 keyi->vmodmap = (tmp.uval >> 8);
1045 keyi->defs.defined |= _Key_VModMap;
1048 ERROR("Expected a virtual modifier mask, found %s\n",
1049 exprOpText(value->op));
1050 ACTION("Ignoring virtual modifiers definition for key %s\n",
1051 longText(keyi->name));
1054 else if ((strcasecmp(field, "locking") == 0) ||
1055 (strcasecmp(field, "lock") == 0) ||
1056 (strcasecmp(field, "locks") == 0)) {
1057 ok = ExprResolveEnum(keymap->ctx, value, &tmp, lockingEntries);
1059 keyi->behavior.type = tmp.uval;
1060 keyi->defs.defined |= _Key_Behavior;
1062 else if ((strcasecmp(field, "radiogroup") == 0) ||
1063 (strcasecmp(field, "permanentradiogroup") == 0) ||
1064 (strcasecmp(field, "allownone") == 0)) {
1065 ERROR("Radio groups not supported\n");
1066 ACTION("Ignoring radio group specification for key %s\n",
1067 longText(keyi->name));
1070 else if (uStrCasePrefix("overlay", field) ||
1071 uStrCasePrefix("permanentoverlay", field)) {
1072 ERROR("Overlays not supported\n");
1073 ACTION("Ignoring overlay specification for key %s\n",
1074 longText(keyi->name));
1076 else if ((strcasecmp(field, "repeating") == 0) ||
1077 (strcasecmp(field, "repeats") == 0) ||
1078 (strcasecmp(field, "repeat") == 0)) {
1079 ok = ExprResolveEnum(keymap->ctx, value, &tmp, repeatEntries);
1081 ERROR("Illegal repeat setting for %s\n",
1082 longText(keyi->name));
1083 ACTION("Non-boolean repeat setting ignored\n");
1086 keyi->repeat = tmp.uval;
1087 keyi->defs.defined |= _Key_Repeat;
1089 else if ((strcasecmp(field, "groupswrap") == 0) ||
1090 (strcasecmp(field, "wrapgroups") == 0)) {
1091 ok = ExprResolveBoolean(keymap->ctx, value, &tmp);
1093 ERROR("Illegal groupsWrap setting for %s\n",
1094 longText(keyi->name));
1095 ACTION("Non-boolean value ignored\n");
1099 keyi->out_of_range_group_action = XkbWrapIntoRange;
1101 keyi->out_of_range_group_action = XkbClampIntoRange;
1102 keyi->defs.defined |= _Key_GroupInfo;
1104 else if ((strcasecmp(field, "groupsclamp") == 0) ||
1105 (strcasecmp(field, "clampgroups") == 0)) {
1106 ok = ExprResolveBoolean(keymap->ctx, value, &tmp);
1108 ERROR("Illegal groupsClamp setting for %s\n",
1109 longText(keyi->name));
1110 ACTION("Non-boolean value ignored\n");
1114 keyi->out_of_range_group_action = XkbClampIntoRange;
1116 keyi->out_of_range_group_action = XkbWrapIntoRange;
1117 keyi->defs.defined |= _Key_GroupInfo;
1119 else if ((strcasecmp(field, "groupsredirect") == 0) ||
1120 (strcasecmp(field, "redirectgroups") == 0)) {
1121 if (!ExprResolveGroup(keymap->ctx, value, &tmp)) {
1122 ERROR("Illegal group index for redirect of key %s\n",
1123 longText(keyi->name));
1124 ACTION("Definition with non-integer group ignored\n");
1127 keyi->out_of_range_group_action = XkbRedirectIntoRange;
1128 keyi->out_of_range_group_number = tmp.uval - 1;
1129 keyi->defs.defined |= _Key_GroupInfo;
1132 ERROR("Unknown field %s in a symbol interpretation\n", field);
1133 ACTION("Definition ignored\n");
1140 SetGroupName(SymbolsInfo *info, struct xkb_keymap *keymap, ExprDef *arrayNdx,
1143 ExprResult tmp, name;
1145 if ((arrayNdx == NULL) && (warningLevel > 0)) {
1146 WARN("You must specify an index when specifying a group name\n");
1147 ACTION("Group name definition without array subscript ignored\n");
1150 if (!ExprResolveGroup(keymap->ctx, arrayNdx, &tmp)) {
1151 ERROR("Illegal index in group name definition\n");
1152 ACTION("Definition with non-integer array index ignored\n");
1155 if (!ExprResolveString(keymap->ctx, value, &name)) {
1156 ERROR("Group name must be a string\n");
1157 ACTION("Illegal name for group %d ignored\n", tmp.uval);
1160 info->groupNames[tmp.uval - 1 + info->explicit_group] =
1161 xkb_atom_intern(keymap->ctx, name.str);
1168 HandleSymbolsVar(VarDef *stmt, struct xkb_keymap *keymap, SymbolsInfo *info)
1170 ExprResult elem, field;
1174 if (ExprResolveLhs(keymap, stmt->name, &elem, &field, &arrayNdx) == 0)
1175 return 0; /* internal error, already reported */
1176 if (elem.str && (strcasecmp(elem.str, "key") == 0)) {
1177 ret = SetSymbolsField(&info->dflt, keymap, field.str, arrayNdx,
1180 else if ((elem.str == NULL) && ((strcasecmp(field.str, "name") == 0) ||
1181 (strcasecmp(field.str, "groupname") ==
1183 ret = SetGroupName(info, keymap, arrayNdx, stmt->value);
1185 else if ((elem.str == NULL)
1186 && ((strcasecmp(field.str, "groupswrap") == 0) ||
1187 (strcasecmp(field.str, "wrapgroups") == 0))) {
1188 ERROR("Global \"groupswrap\" not supported\n");
1189 ACTION("Ignored\n");
1192 else if ((elem.str == NULL)
1193 && ((strcasecmp(field.str, "groupsclamp") == 0) ||
1194 (strcasecmp(field.str, "clampgroups") == 0))) {
1195 ERROR("Global \"groupsclamp\" not supported\n");
1196 ACTION("Ignored\n");
1199 else if ((elem.str == NULL)
1200 && ((strcasecmp(field.str, "groupsredirect") == 0) ||
1201 (strcasecmp(field.str, "redirectgroups") == 0))) {
1202 ERROR("Global \"groupsredirect\" not supported\n");
1203 ACTION("Ignored\n");
1206 else if ((elem.str == NULL) &&
1207 (strcasecmp(field.str, "allownone") == 0)) {
1208 ERROR("Radio groups not supported\n");
1209 ACTION("Ignoring \"allownone\" specification\n");
1213 ret = SetActionField(keymap, elem.str, field.str, arrayNdx,
1214 stmt->value, &info->action);
1223 HandleSymbolsBody(VarDef *def, struct xkb_keymap *keymap, KeyInfo *keyi,
1227 ExprResult tmp, field;
1230 for (; def != NULL; def = (VarDef *) def->common.next) {
1231 if ((def->name) && (def->name->type == ExprFieldRef)) {
1232 ok = HandleSymbolsVar(def, keymap, info);
1236 if (def->name == NULL) {
1237 if ((def->value == NULL)
1238 || (def->value->op == ExprKeysymList))
1239 field.str = strdup("symbols");
1241 field.str = strdup("actions");
1245 ok = ExprResolveLhs(keymap, def->name, &tmp, &field,
1249 ok = SetSymbolsField(keyi, keymap, field.str, arrayNdx,
1258 SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi)
1260 unsigned group = info->explicit_group;
1265 if ((keyi->typesDefined | keyi->symsDefined | keyi->actsDefined) & ~1) {
1267 WARN("For the map %s an explicit group specified\n", info->name);
1268 WARN("but key %s has more than one group defined\n",
1269 longText(keyi->name));
1270 ACTION("All groups except first one will be ignored\n");
1271 for (i = 1; i < XkbNumKbdGroups; i++) {
1272 keyi->numLevels[i] = 0;
1273 darray_free(keyi->syms[i]);
1274 darray_free(keyi->acts[i]);
1278 keyi->typesDefined = keyi->symsDefined = keyi->actsDefined = 1 << group;
1280 keyi->numLevels[group] = keyi->numLevels[0];
1281 keyi->numLevels[0] = 0;
1282 keyi->syms[group] = keyi->syms[0];
1283 darray_init(keyi->syms[0]);
1284 keyi->symsMapIndex[group] = keyi->symsMapIndex[0];
1285 darray_init(keyi->symsMapIndex[0]);
1286 keyi->symsMapNumEntries[group] = keyi->symsMapNumEntries[0];
1287 darray_init(keyi->symsMapNumEntries[0]);
1288 keyi->acts[group] = keyi->acts[0];
1289 darray_init(keyi->acts[0]);
1290 keyi->types[group] = keyi->types[0];
1296 HandleSymbolsDef(SymbolsDef *stmt, struct xkb_keymap *keymap,
1301 InitKeyInfo(&keyi, info->file_id);
1302 CopyKeyInfo(&info->dflt, &keyi, false);
1303 keyi.defs.merge = stmt->merge;
1304 keyi.name = KeyNameToLong(stmt->keyName);
1305 if (!HandleSymbolsBody((VarDef *) stmt->symbols, keymap, &keyi, info)) {
1310 if (!SetExplicitGroup(info, &keyi)) {
1315 if (!AddKeySymbols(info, &keyi, keymap)) {
1323 HandleModMapDef(ModMapDef *def, struct xkb_keymap *keymap, SymbolsInfo *info)
1330 if (!LookupModIndex(keymap->ctx, NULL, def->modifier, TypeInt, &rtrn)) {
1331 ERROR("Illegal modifier map definition\n");
1332 ACTION("Ignoring map for non-modifier \"%s\"\n",
1333 xkb_atom_text(keymap->ctx, def->modifier));
1337 tmp.modifier = rtrn.uval;
1338 for (key = def->keys; key != NULL; key = (ExprDef *) key->common.next) {
1339 if ((key->op == ExprValue) && (key->type == TypeKeyName)) {
1340 tmp.haveSymbol = false;
1341 tmp.u.keyName = KeyNameToLong(key->value.keyName);
1343 else if (ExprResolveKeySym(keymap->ctx, key, &rtrn)) {
1344 tmp.haveSymbol = true;
1345 tmp.u.keySym = rtrn.uval;
1348 ERROR("Modmap entries may contain only key names or keysyms\n");
1349 ACTION("Illegal definition for %s modifier ignored\n",
1350 XkbcModIndexText(tmp.modifier));
1354 ok = AddModMapEntry(info, &tmp) && ok;
1360 HandleSymbolsFile(XkbFile *file, struct xkb_keymap *keymap,
1361 enum merge_mode merge, SymbolsInfo *info)
1366 info->name = uDupString(file->name);
1370 switch (stmt->stmtType) {
1372 if (!HandleIncludeSymbols((IncludeStmt *) stmt, keymap, info))
1375 case StmtSymbolsDef:
1376 if (!HandleSymbolsDef((SymbolsDef *) stmt, keymap, info))
1380 if (!HandleSymbolsVar((VarDef *) stmt, keymap, info))
1384 if (!HandleVModDef((VModDef *) stmt, keymap, merge, &info->vmods))
1388 ERROR("Interpretation files may not include other types\n");
1389 ACTION("Ignoring definition of symbol interpretation\n");
1392 case StmtKeycodeDef:
1393 ERROR("Interpretation files may not include other types\n");
1394 ACTION("Ignoring definition of key name\n");
1398 if (!HandleModMapDef((ModMapDef *) stmt, keymap, info))
1402 WSGO("Unexpected statement type %d in HandleSymbolsFile\n",
1407 if (info->errorCount > 10) {
1409 ERROR("Too many errors\n");
1411 ACTION("Abandoning symbols file \"%s\"\n", file->topName);
1418 * Given a keysym @sym, return a key which generates it, or NULL.
1419 * This is used for example in a modifier map definition, such as:
1420 * modifier_map Lock { Caps_Lock };
1421 * where we want to add the Lock modifier to the modmap of the key
1422 * which matches the keysym Caps_Lock.
1423 * Since there can be many keys which generates the keysym, the key
1424 * is chosen first by lowest group in which the keysym appears, than
1425 * by lowest level and than by lowest key code.
1427 static struct xkb_key *
1428 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
1430 struct xkb_key *key, *ret = NULL;
1431 unsigned int group, level, min_group = UINT_MAX, min_level = UINT_MAX;
1433 xkb_foreach_key(key, keymap) {
1434 for (group = 0; group < key->num_groups; group++) {
1435 for (level = 0; level < XkbKeyGroupWidth(keymap, key, group);
1437 if (XkbKeyNumSyms(key, group, level) != 1 ||
1438 (XkbKeySymEntry(key, group, level))[0] != sym)
1442 * If the keysym was found in a group or level > 0, we must
1443 * keep looking since we might find a key in which the keysym
1444 * is in a lower group or level.
1446 if (group < min_group ||
1447 (group == min_group && level < min_level)) {
1449 if (group == 0 && level == 0) {
1465 * Find the given name in the keymap->map->types and return its index.
1467 * @param atom The atom to search for.
1468 * @param type_rtrn Set to the index of the name if found.
1470 * @return true if found, false otherwise.
1473 FindNamedType(struct xkb_keymap *keymap, xkb_atom_t atom, unsigned *type_rtrn)
1476 const char *name = xkb_atom_text(keymap->ctx, atom);
1477 struct xkb_key_type *type;
1480 darray_foreach(type, keymap->types) {
1481 if (strcmp(type->name, name) == 0) {
1492 * Assign a type to the given sym and return the Atom for the type assigned.
1495 * - ONE_LEVEL for width 0/1
1496 * - ALPHABETIC for 2 shift levels, with lower/upercase
1497 * - KEYPAD for keypad keys.
1498 * - TWO_LEVEL for other 2 shift level keys.
1499 * and the same for four level keys.
1501 * @param width Number of sysms in syms.
1502 * @param syms The keysyms for the given key (must be size width).
1503 * @param typeNameRtrn Set to the Atom of the type name.
1505 * @returns true if a type could be found, false otherwise.
1507 * FIXME: I need to take the KeyInfo so I can look at symsMapIndex and
1508 * all that fun stuff rather than just assuming there's always one
1512 FindAutomaticType(struct xkb_keymap *keymap, int width,
1513 const xkb_keysym_t *syms, xkb_atom_t *typeNameRtrn,
1517 if ((width == 1) || (width == 0)) {
1518 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ONE_LEVEL");
1521 else if (width == 2) {
1522 if (syms && xkb_keysym_is_lower(syms[0]) &&
1523 xkb_keysym_is_upper(syms[1])) {
1524 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ALPHABETIC");
1526 else if (syms && (xkb_keysym_is_keypad(syms[0]) ||
1527 xkb_keysym_is_keypad(syms[1]))) {
1528 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "KEYPAD");
1532 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "TWO_LEVEL");
1536 else if (width <= 4) {
1537 if (syms && xkb_keysym_is_lower(syms[0]) &&
1538 xkb_keysym_is_upper(syms[1]))
1539 if (xkb_keysym_is_lower(syms[2]) && xkb_keysym_is_upper(syms[3]))
1541 xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_ALPHABETIC");
1543 *typeNameRtrn = xkb_atom_intern(keymap->ctx,
1544 "FOUR_LEVEL_SEMIALPHABETIC");
1546 else if (syms && (xkb_keysym_is_keypad(syms[0]) ||
1547 xkb_keysym_is_keypad(syms[1])))
1548 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_KEYPAD");
1550 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL");
1551 /* XXX: why not set autoType here? */
1553 return ((width >= 0) && (width <= 4));
1557 * Ensure the given KeyInfo is in a coherent state, i.e. no gaps between the
1558 * groups, and reduce to one group if all groups are identical anyway.
1561 PrepareKeyDef(KeyInfo *keyi)
1563 int i, j, width, defined, lastGroup;
1566 defined = keyi->symsDefined | keyi->actsDefined | keyi->typesDefined;
1567 /* get highest group number */
1568 for (i = XkbNumKbdGroups - 1; i >= 0; i--) {
1569 if (defined & (1 << i))
1577 /* If there are empty groups between non-empty ones fill them with data */
1578 /* from the first group. */
1579 /* We can make a wrong assumption here. But leaving gaps is worse. */
1580 for (i = lastGroup; i > 0; i--) {
1581 if (defined & (1 << i))
1583 width = keyi->numLevels[0];
1584 if (keyi->typesDefined & 1) {
1585 for (j = 0; j < width; j++) {
1586 keyi->types[i] = keyi->types[0];
1588 keyi->typesDefined |= 1 << i;
1590 if ((keyi->actsDefined & 1) && !darray_empty(keyi->acts[0])) {
1591 darray_copy(keyi->acts[i], keyi->acts[0]);
1592 keyi->actsDefined |= 1 << i;
1594 if ((keyi->symsDefined & 1) && !darray_empty(keyi->syms[0])) {
1595 darray_copy(keyi->syms[i], keyi->syms[0]);
1596 darray_copy(keyi->symsMapIndex[i], keyi->symsMapIndex[0]);
1597 darray_copy(keyi->symsMapNumEntries[i],
1598 keyi->symsMapNumEntries[0]);
1599 keyi->symsDefined |= 1 << i;
1602 keyi->numLevels[i] = keyi->numLevels[0];
1605 /* If all groups are completely identical remove them all */
1606 /* exept the first one. */
1608 for (i = lastGroup; i > 0; i--) {
1609 if ((keyi->numLevels[i] != keyi->numLevels[0]) ||
1610 (keyi->types[i] != keyi->types[0])) {
1614 if (!darray_same(keyi->syms[i], keyi->syms[0]) &&
1615 (darray_empty(keyi->syms[i]) || darray_empty(keyi->syms[0]) ||
1616 darray_size(keyi->syms[i]) != darray_size(keyi->syms[0]) ||
1617 memcmp(darray_mem(keyi->syms[i], 0),
1618 darray_mem(keyi->syms[0], 0),
1619 sizeof(xkb_keysym_t) * darray_size(keyi->syms[0])))) {
1623 if (!darray_same(keyi->symsMapIndex[i], keyi->symsMapIndex[0]) &&
1624 (darray_empty(keyi->symsMapIndex[i]) ||
1625 darray_empty(keyi->symsMapIndex[0]) ||
1626 memcmp(darray_mem(keyi->symsMapIndex[i], 0),
1627 darray_mem(keyi->symsMapIndex[0], 0),
1628 keyi->numLevels[0] * sizeof(int)))) {
1632 if (!darray_same(keyi->symsMapNumEntries[i],
1633 keyi->symsMapNumEntries[0]) &&
1634 (darray_empty(keyi->symsMapNumEntries[i]) ||
1635 darray_empty(keyi->symsMapNumEntries[0]) ||
1636 memcmp(darray_mem(keyi->symsMapNumEntries[i], 0),
1637 darray_mem(keyi->symsMapNumEntries[0], 0),
1638 keyi->numLevels[0] * sizeof(size_t)))) {
1642 if (!darray_same(keyi->acts[i], keyi->acts[0]) &&
1643 (darray_empty(keyi->acts[i]) || darray_empty(keyi->acts[0]) ||
1644 memcmp(darray_mem(keyi->acts[i], 0),
1645 darray_mem(keyi->acts[0], 0),
1646 keyi->numLevels[0] * sizeof(union xkb_action)))) {
1652 for (i = lastGroup; i > 0; i--) {
1653 keyi->numLevels[i] = 0;
1654 darray_free(keyi->syms[i]);
1655 darray_free(keyi->symsMapIndex[i]);
1656 darray_free(keyi->symsMapNumEntries[i]);
1657 darray_free(keyi->acts[i]);
1660 keyi->symsDefined &= 1;
1661 keyi->actsDefined &= 1;
1662 keyi->typesDefined &= 1;
1667 * Copy the KeyInfo into the keyboard description.
1669 * This function recurses.
1672 CopySymbolsDef(struct xkb_keymap *keymap, KeyInfo *keyi, int start_from)
1676 struct xkb_key *key;
1677 unsigned int sizeSyms = 0;
1678 unsigned width, tmp, nGroups;
1679 struct xkb_key_type * type;
1680 bool haveActions, autoType, useAlias;
1681 unsigned types[XkbNumKbdGroups];
1682 union xkb_action *outActs;
1683 unsigned int symIndex = 0;
1685 useAlias = (start_from == 0);
1687 key = FindNamedKey(keymap, keyi->name, useAlias,
1688 CreateKeyNames(keymap), start_from);
1690 if (start_from == 0 && warningLevel >= 5) {
1691 WARN("Key %s not found in keycodes\n", longText(keyi->name));
1692 ACTION("Symbols ignored\n");
1696 kc = XkbKeyGetKeycode(keymap, key);
1698 haveActions = false;
1699 for (i = width = nGroups = 0; i < XkbNumKbdGroups; i++) {
1700 if (((i + 1) > nGroups)
1701 && (((keyi->symsDefined | keyi->actsDefined) & (1 << i))
1702 || (keyi->typesDefined) & (1 << i)))
1704 if (!darray_empty(keyi->acts[i]))
1707 /* Assign the type to the key, if it is missing. */
1708 if (keyi->types[i] == XKB_ATOM_NONE) {
1709 if (keyi->dfltType != XKB_ATOM_NONE)
1710 keyi->types[i] = keyi->dfltType;
1711 else if (FindAutomaticType(keymap, keyi->numLevels[i],
1712 darray_mem(keyi->syms[i], 0),
1713 &keyi->types[i], &autoType)) { }
1715 if (warningLevel >= 5) {
1716 WARN("No automatic type for %d symbols\n",
1717 (unsigned int) keyi->numLevels[i]);
1718 ACTION("Using %s for the %s key (keycode %d)\n",
1719 xkb_atom_text(keymap->ctx, keyi->types[i]),
1720 longText(keyi->name), kc);
1724 if (FindNamedType(keymap, keyi->types[i], &types[i])) {
1725 if (!autoType || keyi->numLevels[i] > 2)
1726 key->explicit |= (1 << i);
1729 if (warningLevel >= 3) {
1730 WARN("Type \"%s\" is not defined\n",
1731 xkb_atom_text(keymap->ctx, keyi->types[i]));
1732 ACTION("Using TWO_LEVEL for the %s key (keycode %d)\n",
1733 longText(keyi->name), kc);
1735 types[i] = XkbTwoLevelIndex;
1737 /* if the type specifies fewer levels than the key has, shrink the key */
1738 type = &darray_item(keymap->types, types[i]);
1739 if (type->num_levels < keyi->numLevels[i]) {
1740 if (warningLevel > 0) {
1741 WARN("Type \"%s\" has %d levels, but %s has %d symbols\n",
1742 type->name, type->num_levels,
1743 xkb_atom_text(keymap->ctx, keyi->name), keyi->numLevels[i]);
1744 ACTION("Ignoring extra symbols\n");
1746 keyi->numLevels[i] = type->num_levels;
1748 if (keyi->numLevels[i] > width)
1749 width = keyi->numLevels[i];
1750 if (type->num_levels > width)
1751 width = type->num_levels;
1752 sizeSyms += darray_size(keyi->syms[i]);
1755 darray_resize0(key->syms, sizeSyms);
1758 outActs = XkbcResizeKeyActions(keymap, key, width * nGroups);
1759 if (outActs == NULL) {
1760 WSGO("Could not enlarge actions for %s (key %d)\n",
1761 longText(keyi->name), kc);
1764 key->explicit |= XkbExplicitInterpretMask;
1769 key->num_groups = nGroups;
1770 if (keyi->defs.defined & _Key_GroupInfo) {
1771 key->out_of_range_group_number = keyi->out_of_range_group_number;
1772 key->out_of_range_group_action = keyi->out_of_range_group_action;
1775 key->sym_index = calloc(nGroups * width, sizeof(*key->sym_index));
1776 key->num_syms = calloc(nGroups * width, sizeof(*key->num_syms));
1778 for (i = 0; i < nGroups; i++) {
1779 /* assign kt_index[i] to the index of the type in map->types.
1780 * kt_index[i] may have been set by a previous run (if we have two
1781 * layouts specified). Let's not overwrite it with the ONE_LEVEL
1782 * default group if we dont even have keys for this group anyway.
1784 * FIXME: There should be a better fix for this.
1786 if (keyi->numLevels[i])
1787 key->kt_index[i] = types[i];
1788 if (!darray_empty(keyi->syms[i])) {
1789 /* fill key to "width" symbols*/
1790 for (tmp = 0; tmp < width; tmp++) {
1791 if (tmp < keyi->numLevels[i] &&
1792 darray_item(keyi->symsMapNumEntries[i], tmp) != 0) {
1793 memcpy(darray_mem(key->syms, symIndex),
1794 darray_mem(keyi->syms[i],
1795 darray_item(keyi->symsMapIndex[i], tmp)),
1796 darray_item(keyi->symsMapNumEntries[i],
1797 tmp) * sizeof(xkb_keysym_t));
1798 key->sym_index[(i * width) + tmp] = symIndex;
1799 key->num_syms[(i * width) + tmp] =
1800 darray_item(keyi->symsMapNumEntries[i], tmp);
1801 symIndex += key->num_syms[(i * width) + tmp];
1804 key->sym_index[(i * width) + tmp] = -1;
1805 key->num_syms[(i * width) + tmp] = 0;
1807 if (outActs != NULL && !darray_empty(keyi->acts[i])) {
1808 if (tmp < keyi->numLevels[i])
1809 outActs[tmp] = darray_item(keyi->acts[i], tmp);
1811 outActs[tmp].type = XkbSA_NoAction;
1816 switch (keyi->behavior.type & XkbKB_OpMask) {
1821 key->behavior = keyi->behavior;
1822 key->explicit |= XkbExplicitBehaviorMask;
1825 if (keyi->defs.defined & _Key_VModMap) {
1826 key->vmodmap = keyi->vmodmap;
1827 key->explicit |= XkbExplicitVModMapMask;
1829 if (keyi->repeat != RepeatUndefined) {
1830 key->repeats = keyi->repeat == RepeatYes;
1831 key->explicit |= XkbExplicitAutoRepeatMask;
1834 /* do the same thing for the next key */
1835 CopySymbolsDef(keymap, keyi, kc + 1);
1840 CopyModMapDef(struct xkb_keymap *keymap, ModMapEntry *entry)
1842 struct xkb_key *key;
1844 if (!entry->haveSymbol) {
1845 key = FindNamedKey(keymap, entry->u.keyName, true,
1846 CreateKeyNames(keymap), 0);
1848 if (warningLevel >= 5) {
1849 WARN("Key %s not found in keycodes\n",
1850 longText(entry->u.keyName));
1851 ACTION("Modifier map entry for %s not updated\n",
1852 XkbcModIndexText(entry->modifier));
1858 key = FindKeyForSymbol(keymap, entry->u.keySym);
1860 if (warningLevel > 5) {
1861 WARN("Key \"%s\" not found in symbol map\n",
1862 XkbcKeysymText(entry->u.keySym));
1863 ACTION("Modifier map entry for %s not updated\n",
1864 XkbcModIndexText(entry->modifier));
1870 key->modmap |= (1 << entry->modifier);
1875 * Handle the xkb_symbols section of an xkb file.
1877 * @param file The parsed xkb_symbols section of the xkb file.
1878 * @param keymap Handle to the keyboard description to store the symbols in.
1879 * @param merge Merge strategy (e.g. MERGE_OVERRIDE).
1882 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1883 enum merge_mode merge)
1886 struct xkb_key *key;
1891 InitSymbolsInfo(&info, keymap, file->id);
1892 info.dflt.defs.merge = merge;
1894 HandleSymbolsFile(file, keymap, merge, &info);
1896 if (darray_empty(info.keys))
1899 if (info.errorCount != 0)
1902 darray_resize0(keymap->acts, darray_size(keymap->acts) + 32 + 1);
1905 keymap->symbols_section_name = strdup(info.name);
1907 for (i = 0; i < XkbNumKbdGroups; i++) {
1908 if (info.groupNames[i] != XKB_ATOM_NONE) {
1909 free(keymap->group_names[i]);
1910 keymap->group_names[i] = xkb_atom_strdup(keymap->ctx,
1911 info.groupNames[i]);
1916 darray_foreach(keyi, info.keys)
1917 PrepareKeyDef(keyi);
1920 darray_foreach(keyi, info.keys)
1921 if (!CopySymbolsDef(keymap, keyi, 0))
1924 if (warningLevel > 3) {
1925 xkb_foreach_key(key, keymap) {
1926 if (key->name[0] == '\0')
1929 if (key->num_groups < 1)
1930 WARN("No symbols defined for <%.4s> (keycode %d)\n",
1931 key->name, XkbKeyGetKeycode(keymap, key));
1935 list_foreach(mm, &info.modMaps, entry)
1936 if (!CopyModMapDef(keymap, mm))
1939 FreeSymbolsInfo(&info);
1943 FreeSymbolsInfo(&info);