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"
36 /***====================================================================***/
38 /* Needed to work with the typechecker. */
39 typedef darray (xkb_keysym_t) darray_xkb_keysym_t;
40 typedef darray (union xkb_action) darray_xkb_action;
44 #define RepeatUndefined ~((unsigned) 0)
46 #define _Key_Syms (1 << 0)
47 #define _Key_Acts (1 << 1)
48 #define _Key_Repeat (1 << 2)
49 #define _Key_Behavior (1 << 3)
50 #define _Key_Type_Dflt (1 << 4)
51 #define _Key_Types (1 << 5)
52 #define _Key_GroupInfo (1 << 6)
53 #define _Key_VModMap (1 << 7)
55 typedef struct _KeyInfo {
57 unsigned long name; /* the 4 chars of the key name, as long */
58 unsigned char groupInfo;
59 unsigned char typesDefined;
60 unsigned char symsDefined;
61 unsigned char actsDefined;
62 unsigned int numLevels[XkbNumKbdGroups];
64 /* syms[group] -> Single array for all the keysyms in the group. */
65 darray_xkb_keysym_t syms[XkbNumKbdGroups];
67 * symsMapIndex[group][level] -> The index from which the syms for
68 * the level begin in the syms[group] array. Remember each keycode
69 * can have multiple keysyms in each level (that is, each key press
70 * can result in multiple keysyms).
72 darray(int) symsMapIndex[XkbNumKbdGroups];
74 * symsMapNumEntries[group][level] -> How many syms are in
75 * syms[group][symsMapIndex[group][level]].
77 darray(size_t) symsMapNumEntries[XkbNumKbdGroups];
79 darray_xkb_action acts[XkbNumKbdGroups];
81 xkb_atom_t types[XkbNumKbdGroups];
83 struct xkb_behavior behavior;
84 unsigned short vmodmap;
89 * Init the given key info to sane values.
92 InitKeyInfo(KeyInfo * info, unsigned file_id)
95 static const char dflt[4] = "*";
97 info->defs.defined = 0;
98 info->defs.file_id = file_id;
99 info->defs.merge = MERGE_OVERRIDE;
100 info->defs.next = NULL;
101 info->name = KeyNameToLong(dflt);
103 info->typesDefined = info->symsDefined = info->actsDefined = 0;
104 for (i = 0; i < XkbNumKbdGroups; i++) {
105 info->numLevels[i] = 0;
106 info->types[i] = XKB_ATOM_NONE;
107 darray_init(info->syms[i]);
108 darray_init(info->symsMapIndex[i]);
109 darray_init(info->symsMapNumEntries[i]);
110 darray_init(info->acts[i]);
112 info->dfltType = XKB_ATOM_NONE;
113 info->behavior.type = XkbKB_Default;
114 info->behavior.data = 0;
116 info->repeat = RepeatUndefined;
120 * Free memory associated with this key info and reset to sane values.
123 FreeKeyInfo(KeyInfo * info)
127 info->defs.defined = 0;
128 info->defs.file_id = 0;
129 info->defs.merge = MERGE_OVERRIDE;
130 info->defs.next = NULL;
132 info->typesDefined = info->symsDefined = info->actsDefined = 0;
133 for (i = 0; i < XkbNumKbdGroups; i++) {
134 info->numLevels[i] = 0;
135 info->types[i] = XKB_ATOM_NONE;
136 darray_free(info->syms[i]);
137 darray_free(info->symsMapIndex[i]);
138 darray_free(info->symsMapNumEntries[i]);
139 darray_free(info->acts[i]);
141 info->dfltType = XKB_ATOM_NONE;
142 info->behavior.type = XkbKB_Default;
143 info->behavior.data = 0;
145 info->repeat = RepeatUndefined;
149 * Copy old into new, optionally reset old to 0.
150 * If old is reset, new simply re-uses old's memory. Otherwise, the memory is
151 * newly allocated and new points to the new memory areas.
154 CopyKeyInfo(KeyInfo * old, KeyInfo * new, bool clearOld)
159 new->defs.next = NULL;
162 for (i = 0; i < XkbNumKbdGroups; i++) {
163 old->numLevels[i] = 0;
164 darray_init(old->symsMapIndex[i]);
165 darray_init(old->symsMapNumEntries[i]);
166 darray_init(old->syms[i]);
167 darray_init(old->acts[i]);
171 for (i = 0; i < XkbNumKbdGroups; i++) {
172 darray_copy(new->syms[i], old->syms[i]);
173 darray_copy(new->symsMapIndex[i], old->symsMapIndex[i]);
174 darray_copy(new->symsMapNumEntries[i], old->symsMapNumEntries[i]);
175 darray_copy(new->acts[i], old->acts[i]);
182 /***====================================================================***/
184 typedef struct _ModMapEntry {
189 unsigned long keyName;
194 typedef struct _SymbolsInfo {
195 char *name; /* e.g. pc+us+inet(evdev) */
198 enum merge_mode merge;
199 unsigned explicit_group;
200 darray(KeyInfo) keys;
204 xkb_atom_t groupNames[XkbNumKbdGroups];
211 InitSymbolsInfo(SymbolsInfo * info, struct xkb_keymap *keymap,
217 info->explicit_group = 0;
218 info->errorCount = 0;
219 info->file_id = file_id;
220 info->merge = MERGE_OVERRIDE;
221 darray_init(info->keys);
222 darray_growalloc(info->keys, 110);
224 for (i = 0; i < XkbNumKbdGroups; i++)
225 info->groupNames[i] = XKB_ATOM_NONE;
226 InitKeyInfo(&info->dflt, file_id);
227 InitVModInfo(&info->vmods, keymap);
229 info->aliases = NULL;
233 FreeSymbolsInfo(SymbolsInfo * info)
238 darray_foreach(key, info->keys)
240 darray_free(info->keys);
242 ClearCommonInfo(&info->modMap->defs);
244 ClearAliases(&info->aliases);
245 memset(info, 0, sizeof(SymbolsInfo));
249 ResizeKeyGroup(KeyInfo * key, unsigned int group, unsigned int numLevels,
250 unsigned sizeSyms, bool forceActions)
254 if (darray_size(key->syms[group]) < sizeSyms)
255 darray_resize0(key->syms[group], sizeSyms);
257 if (darray_empty(key->symsMapIndex[group]) ||
258 key->numLevels[group] < numLevels) {
259 darray_resize(key->symsMapIndex[group], numLevels);
260 for (i = key->numLevels[group]; i < numLevels; i++)
261 darray_item(key->symsMapIndex[group], i) = -1;
264 if (darray_empty(key->symsMapNumEntries[group]) ||
265 key->numLevels[group] < numLevels)
266 darray_resize0(key->symsMapNumEntries[group], numLevels);
268 if ((forceActions && (key->numLevels[group] < numLevels ||
269 darray_empty(key->acts[group]))) ||
270 (key->numLevels[group] < numLevels && !darray_empty(key->acts[group])))
271 darray_resize0(key->acts[group], numLevels);
273 if (key->numLevels[group] < numLevels)
274 key->numLevels[group] = numLevels;
279 enum key_group_selector {
286 MergeKeyGroups(SymbolsInfo * info,
287 KeyInfo * into, KeyInfo * from, unsigned group)
289 darray_xkb_keysym_t resultSyms;
290 enum key_group_selector using = NONE;
291 darray_xkb_action resultActs;
292 unsigned int resultWidth;
293 unsigned int resultSize = 0;
296 bool report, clobber;
298 clobber = (from->defs.merge != MERGE_AUGMENT);
300 report = (warningLevel > 9) ||
301 ((into->defs.file_id == from->defs.file_id) && (warningLevel > 0));
303 darray_init(resultSyms);
305 if (into->numLevels[group] >= from->numLevels[group]) {
306 resultActs = into->acts[group];
307 resultWidth = into->numLevels[group];
310 resultActs = from->acts[group];
311 resultWidth = from->numLevels[group];
312 darray_resize(into->symsMapIndex[group],
313 from->numLevels[group]);
314 darray_resize0(into->symsMapNumEntries[group],
315 from->numLevels[group]);
317 for (i = into->numLevels[group]; i < from->numLevels[group]; i++)
318 darray_item(into->symsMapIndex[group], i) = -1;
321 if (darray_empty(resultActs) && (!darray_empty(into->acts[group]) ||
322 !darray_empty(from->acts[group]))) {
323 darray_resize0(resultActs, resultWidth);
324 for (i = 0; i < resultWidth; i++) {
325 union xkb_action *fromAct = NULL, *toAct = NULL;
327 if (!darray_empty(from->acts[group]))
328 fromAct = &darray_item(from->acts[group], i);
330 if (!darray_empty(into->acts[group]))
331 toAct = &darray_item(into->acts[group], i);
333 if (((fromAct == NULL) || (fromAct->type == XkbSA_NoAction))
334 && (toAct != NULL)) {
335 darray_item(resultActs, i) = *toAct;
337 else if (((toAct == NULL) || (toAct->type == XkbSA_NoAction))
338 && (fromAct != NULL)) {
339 darray_item(resultActs, i) = *fromAct;
342 union xkb_action *use, *ignore;
353 ("Multiple actions for level %d/group %d on key %s\n",
354 i + 1, group + 1, longText(into->name));
355 ACTION("Using %s, ignoring %s\n",
356 XkbcActionTypeText(use->type),
357 XkbcActionTypeText(ignore->type));
360 darray_item(resultActs, i) = *use;
365 for (i = 0; i < resultWidth; i++) {
366 unsigned int fromSize = 0;
369 if (!darray_empty(from->symsMapNumEntries[group]) &&
370 i < from->numLevels[group])
371 fromSize = darray_item(from->symsMapNumEntries[group], i);
373 if (!darray_empty(into->symsMapNumEntries[group]) &&
374 i < into->numLevels[group])
375 toSize = darray_item(into->symsMapNumEntries[group], i);
378 resultSize += toSize;
381 else if (toSize == 0 || clobber) {
382 resultSize += fromSize;
386 resultSize += toSize;
395 resultSyms = from->syms[group];
396 darray_free(into->symsMapNumEntries[group]);
397 darray_free(into->symsMapIndex[group]);
398 into->symsMapNumEntries[group] = from->symsMapNumEntries[group];
399 into->symsMapIndex[group] = from->symsMapIndex[group];
400 darray_init(from->symsMapNumEntries[group]);
401 darray_init(from->symsMapIndex[group]);
404 else if (using == TO) {
405 resultSyms = into->syms[group];
409 darray_resize0(resultSyms, resultSize);
411 for (i = 0; i < resultWidth; i++) {
412 enum key_group_selector use = NONE;
413 unsigned int fromSize = 0;
414 unsigned int toSize = 0;
416 if (i < from->numLevels[group])
417 fromSize = darray_item(from->symsMapNumEntries[group], i);
419 if (i < into->numLevels[group])
420 toSize = darray_item(into->symsMapNumEntries[group], i);
422 if (fromSize == 0 && toSize == 0) {
423 darray_item(into->symsMapIndex[group], i) = -1;
424 darray_item(into->symsMapNumEntries[group], i) = 0;
430 else if (toSize == 0 || clobber)
435 if (toSize && fromSize && report) {
436 INFO("Multiple symbols for group %d, level %d on key %s\n",
437 group + 1, i + 1, longText(into->name));
438 ACTION("Using %s, ignoring %s\n",
439 (use == FROM ? "from" : "to"),
440 (use == FROM ? "to" : "from"));
444 memcpy(darray_mem(resultSyms, cur_idx),
445 darray_mem(from->syms[group],
446 darray_item(from->symsMapIndex[group], i)),
447 darray_item(from->symsMapNumEntries[group],
448 i) * sizeof(xkb_keysym_t));
449 darray_item(into->symsMapIndex[group], i) = cur_idx;
450 darray_item(into->symsMapNumEntries[group], i) =
451 darray_item(from->symsMapNumEntries[group], i);
454 memcpy(darray_mem(resultSyms, cur_idx),
455 darray_mem(into->syms[group],
456 darray_item(into->symsMapIndex[group], i)),
457 darray_item(into->symsMapNumEntries[group],
458 i) * sizeof(xkb_keysym_t));
459 darray_item(into->symsMapIndex[group], i) = cur_idx;
461 cur_idx += darray_item(into->symsMapNumEntries[group], i);
465 if (!darray_same(resultActs, into->acts[group]))
466 darray_free(into->acts[group]);
467 if (!darray_same(resultActs, from->acts[group]))
468 darray_free(from->acts[group]);
469 into->numLevels[group] = resultWidth;
470 if (!darray_same(resultSyms, into->syms[group]))
471 darray_free(into->syms[group]);
472 into->syms[group] = resultSyms;
473 if (!darray_same(resultSyms, from->syms[group]))
474 darray_free(from->syms[group]);
475 darray_init(from->syms[group]);
476 darray_free(from->symsMapIndex[group]);
477 darray_free(from->symsMapNumEntries[group]);
478 into->acts[group] = resultActs;
479 darray_init(from->acts[group]);
480 if (!darray_empty(into->syms[group]))
481 into->symsDefined |= (1 << group);
482 from->symsDefined &= ~(1 << group);
483 into->actsDefined |= (1 << group);
484 from->actsDefined &= ~(1 << group);
490 MergeKeys(SymbolsInfo *info, struct xkb_keymap *keymap,
491 KeyInfo *into, KeyInfo *from)
494 unsigned collide = 0;
497 if (from->defs.merge == MERGE_REPLACE) {
498 for (i = 0; i < XkbNumKbdGroups; i++) {
499 if (into->numLevels[i] != 0) {
500 darray_free(into->syms[i]);
501 darray_free(into->acts[i]);
505 memset(from, 0, sizeof(KeyInfo));
508 report = ((warningLevel > 9) ||
509 ((into->defs.file_id == from->defs.file_id)
510 && (warningLevel > 0)));
511 for (i = 0; i < XkbNumKbdGroups; i++) {
512 if (from->numLevels[i] > 0) {
513 if (into->numLevels[i] == 0) {
514 into->numLevels[i] = from->numLevels[i];
515 into->syms[i] = from->syms[i];
516 into->symsMapIndex[i] = from->symsMapIndex[i];
517 into->symsMapNumEntries[i] = from->symsMapNumEntries[i];
518 into->acts[i] = from->acts[i];
519 into->symsDefined |= (1 << i);
520 darray_init(from->syms[i]);
521 darray_init(from->symsMapIndex[i]);
522 darray_init(from->symsMapNumEntries[i]);
523 darray_init(from->acts[i]);
524 from->numLevels[i] = 0;
525 from->symsDefined &= ~(1 << i);
526 if (!darray_empty(into->syms[i]))
527 into->defs.defined |= _Key_Syms;
528 if (!darray_empty(into->acts[i]))
529 into->defs.defined |= _Key_Acts;
533 if (!darray_empty(into->syms[i]))
534 collide |= _Key_Syms;
535 if (!darray_empty(into->acts[i]))
536 collide |= _Key_Acts;
538 MergeKeyGroups(info, into, from, (unsigned) i);
541 if (from->types[i] != XKB_ATOM_NONE) {
542 if ((into->types[i] != XKB_ATOM_NONE) && report &&
543 (into->types[i] != from->types[i])) {
544 xkb_atom_t use, ignore;
545 collide |= _Key_Types;
546 if (from->defs.merge != MERGE_AUGMENT) {
547 use = from->types[i];
548 ignore = into->types[i];
551 use = into->types[i];
552 ignore = from->types[i];
555 ("Multiple definitions for group %d type of key %s\n",
556 i, longText(into->name));
557 ACTION("Using %s, ignoring %s\n",
558 xkb_atom_text(keymap->ctx, use),
559 xkb_atom_text(keymap->ctx, ignore));
561 if ((from->defs.merge != MERGE_AUGMENT)
562 || (into->types[i] == XKB_ATOM_NONE)) {
563 into->types[i] = from->types[i];
567 if (UseNewField(_Key_Behavior, &into->defs, &from->defs, &collide)) {
568 into->behavior = from->behavior;
569 into->defs.defined |= _Key_Behavior;
571 if (UseNewField(_Key_VModMap, &into->defs, &from->defs, &collide)) {
572 into->vmodmap = from->vmodmap;
573 into->defs.defined |= _Key_VModMap;
575 if (UseNewField(_Key_Repeat, &into->defs, &from->defs, &collide)) {
576 into->repeat = from->repeat;
577 into->defs.defined |= _Key_Repeat;
579 if (UseNewField(_Key_Type_Dflt, &into->defs, &from->defs, &collide)) {
580 into->dfltType = from->dfltType;
581 into->defs.defined |= _Key_Type_Dflt;
583 if (UseNewField(_Key_GroupInfo, &into->defs, &from->defs, &collide)) {
584 into->groupInfo = from->groupInfo;
585 into->defs.defined |= _Key_GroupInfo;
588 WARN("Symbol map for key %s redefined\n",
589 longText(into->name));
590 ACTION("Using %s definition for conflicting fields\n",
591 (from->defs.merge == MERGE_AUGMENT ? "first" : "last"));
597 AddKeySymbols(SymbolsInfo *info, KeyInfo *key, struct xkb_keymap *keymap)
599 unsigned long real_name;
602 darray_foreach(iter, info->keys)
603 if (iter->name == key->name)
604 return MergeKeys(info, keymap, iter, key);
606 if (FindKeyNameForAlias(keymap, key->name, &real_name))
607 darray_foreach(iter, info->keys)
608 if (iter->name == real_name)
609 return MergeKeys(info, keymap, iter, key);
611 darray_resize0(info->keys, darray_size(info->keys) + 1);
612 new = &darray_item(info->keys, darray_size(info->keys) - 1);
613 return CopyKeyInfo(key, new, true);
617 AddModMapEntry(SymbolsInfo * info, ModMapEntry * new)
622 clobber = (new->defs.merge != MERGE_AUGMENT);
623 for (mm = info->modMap; mm != NULL; mm = (ModMapEntry *) mm->defs.next) {
624 if (new->haveSymbol && mm->haveSymbol
625 && (new->u.keySym == mm->u.keySym)) {
626 unsigned use, ignore;
627 if (mm->modifier != new->modifier) {
630 ignore = mm->modifier;
634 ignore = new->modifier;
637 ("%s added to symbol map for multiple modifiers\n",
638 XkbcKeysymText(new->u.keySym));
639 ACTION("Using %s, ignoring %s.\n",
640 XkbcModIndexText(use),
641 XkbcModIndexText(ignore));
646 if ((!new->haveSymbol) && (!mm->haveSymbol) &&
647 (new->u.keyName == mm->u.keyName)) {
648 unsigned use, ignore;
649 if (mm->modifier != new->modifier) {
652 ignore = mm->modifier;
656 ignore = new->modifier;
658 ERROR("Key %s added to map for multiple modifiers\n",
659 longText(new->u.keyName));
660 ACTION("Using %s, ignoring %s.\n",
661 XkbcModIndexText(use),
662 XkbcModIndexText(ignore));
668 mm = uTypedAlloc(ModMapEntry);
670 WSGO("Could not allocate modifier map entry\n");
671 ACTION("Modifier map for %s will be incomplete\n",
672 XkbcModIndexText(new->modifier));
676 mm->defs.next = &info->modMap->defs;
681 /***====================================================================***/
684 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
685 enum merge_mode merge, struct xkb_keymap *keymap)
690 if (from->errorCount > 0) {
691 into->errorCount += from->errorCount;
694 if (into->name == NULL) {
695 into->name = from->name;
698 for (i = 0; i < XkbNumKbdGroups; i++) {
699 if (from->groupNames[i] != XKB_ATOM_NONE) {
700 if ((merge != MERGE_AUGMENT) ||
701 (into->groupNames[i] == XKB_ATOM_NONE))
702 into->groupNames[i] = from->groupNames[i];
706 darray_foreach(key, from->keys) {
707 if (merge != MERGE_DEFAULT)
708 key->defs.merge = merge;
710 if (!AddKeySymbols(into, key, keymap))
714 if (from->modMap != NULL) {
715 ModMapEntry *mm, *next;
716 for (mm = from->modMap; mm != NULL; mm = next) {
717 if (merge != MERGE_DEFAULT)
718 mm->defs.merge = merge;
719 if (!AddModMapEntry(into, mm))
721 next = (ModMapEntry *) mm->defs.next;
726 if (!MergeAliases(&into->aliases, &from->aliases, merge))
731 HandleSymbolsFile(XkbFile *file, struct xkb_keymap *keymap,
732 enum merge_mode merge,
736 HandleIncludeSymbols(IncludeStmt *stmt, struct xkb_keymap *keymap,
739 enum merge_mode newMerge;
741 SymbolsInfo included;
745 if ((stmt->file == NULL) && (stmt->map == NULL)) {
748 memset(info, 0, sizeof(SymbolsInfo));
750 else if (ProcessIncludeFile(keymap->ctx, stmt, FILE_TYPE_SYMBOLS, &rtrn,
752 InitSymbolsInfo(&included, keymap, rtrn->id);
753 included.merge = included.dflt.defs.merge = MERGE_OVERRIDE;
754 if (stmt->modifier) {
755 included.explicit_group = atoi(stmt->modifier) - 1;
758 included.explicit_group = info->explicit_group;
760 HandleSymbolsFile(rtrn, keymap, MERGE_OVERRIDE, &included);
761 if (stmt->stmt != NULL) {
763 included.name = stmt->stmt;
769 info->errorCount += 10;
772 if ((stmt->next != NULL) && (included.errorCount < 1)) {
775 SymbolsInfo next_incl;
777 for (next = stmt->next; next != NULL; next = next->next) {
778 if ((next->file == NULL) && (next->map == NULL)) {
780 MergeIncludedSymbols(&included, info, next->merge, keymap);
781 FreeSymbolsInfo(info);
783 else if (ProcessIncludeFile(keymap->ctx, next, FILE_TYPE_SYMBOLS,
785 InitSymbolsInfo(&next_incl, keymap, rtrn->id);
786 next_incl.merge = next_incl.dflt.defs.merge = MERGE_OVERRIDE;
787 if (next->modifier) {
788 next_incl.explicit_group = atoi(next->modifier) - 1;
791 next_incl.explicit_group = info->explicit_group;
793 HandleSymbolsFile(rtrn, keymap, MERGE_OVERRIDE, &next_incl);
794 MergeIncludedSymbols(&included, &next_incl, op, keymap);
795 FreeSymbolsInfo(&next_incl);
799 info->errorCount += 10;
800 FreeSymbolsInfo(&included);
805 else if (stmt->next) {
806 info->errorCount += included.errorCount;
811 MergeIncludedSymbols(info, &included, newMerge, keymap);
812 FreeSymbolsInfo(&included);
814 return (info->errorCount == 0);
821 GetGroupIndex(KeyInfo *key, struct xkb_keymap *keymap,
822 ExprDef * arrayNdx, unsigned what, unsigned *ndx_rtrn)
832 if (arrayNdx == NULL) {
836 defined = key->symsDefined;
838 defined = key->actsDefined;
840 for (i = 0; i < XkbNumKbdGroups; i++) {
841 if ((defined & (1 << i)) == 0) {
846 ERROR("Too many groups of %s for key %s (max %d)\n", name,
847 longText(key->name), XkbNumKbdGroups + 1);
848 ACTION("Ignoring %s defined for extra groups\n", name);
851 if (!ExprResolveGroup(keymap->ctx, arrayNdx, &tmp)) {
852 ERROR("Illegal group index for %s of key %s\n", name,
853 longText(key->name));
854 ACTION("Definition with non-integer array index ignored\n");
857 *ndx_rtrn = tmp.uval - 1;
862 AddSymbolsToKey(KeyInfo *key, struct xkb_keymap *keymap,
863 ExprDef *arrayNdx, ExprDef *value, SymbolsInfo *info)
865 unsigned ndx, nSyms, nLevels;
869 if (!GetGroupIndex(key, keymap, arrayNdx, SYMBOLS, &ndx))
872 key->symsDefined |= (1 << ndx);
875 if (value->op != ExprKeysymList) {
876 ERROR("Expected a list of symbols, found %s\n", exprOpText(value->op));
877 ACTION("Ignoring symbols for group %d of %s\n", ndx + 1,
878 longText(key->name));
881 if (!darray_empty(key->syms[ndx])) {
882 ERROR("Symbols for key %s, group %d already defined\n",
883 longText(key->name), ndx + 1);
884 ACTION("Ignoring duplicate definition\n");
887 nSyms = darray_size(value->value.list.syms);
888 nLevels = darray_size(value->value.list.symsMapIndex);
889 if ((key->numLevels[ndx] < nSyms || darray_empty(key->syms[ndx])) &&
890 (!ResizeKeyGroup(key, ndx, nLevels, nSyms, false))) {
891 WSGO("Could not resize group %d of key %s to contain %d levels\n",
892 ndx + 1, longText(key->name), nSyms);
893 ACTION("Symbols lost\n");
896 key->symsDefined |= (1 << ndx);
897 for (i = 0; i < nLevels; i++) {
898 darray_item(key->symsMapIndex[ndx], i) =
899 darray_item(value->value.list.symsMapIndex, i);
900 darray_item(key->symsMapNumEntries[ndx], i) =
901 darray_item(value->value.list.symsNumEntries, i);
903 for (j = 0; j < darray_item(key->symsMapNumEntries[ndx], i); j++) {
904 /* FIXME: What's abort() doing here? */
905 if (darray_item(key->symsMapIndex[ndx], i) + j >= nSyms)
907 if (!LookupKeysym(darray_item(value->value.list.syms,
908 darray_item(value->value.list.
909 symsMapIndex, i) + j),
910 &darray_item(key->syms[ndx],
911 darray_item(key->symsMapIndex[ndx],
914 "Could not resolve keysym %s for key %s, group %d (%s), level %d\n",
915 darray_item(value->value.list.syms, i),
918 xkb_atom_text(keymap->ctx, info->groupNames[ndx]), nSyms);
920 darray_item(key->syms[ndx],
921 darray_item(key->symsMapIndex[ndx],
922 i) + j) = XKB_KEY_NoSymbol;
923 darray_item(key->symsMapIndex[ndx], i) = -1;
924 darray_item(key->symsMapNumEntries[ndx], i) = 0;
927 if (darray_item(key->symsMapNumEntries[ndx], i) == 1 &&
928 darray_item(key->syms[ndx],
929 darray_item(key->symsMapIndex[ndx],
930 i) + j) == XKB_KEY_NoSymbol) {
931 darray_item(key->symsMapIndex[ndx], i) = -1;
932 darray_item(key->symsMapNumEntries[ndx], i) = 0;
936 for (j = key->numLevels[ndx] - 1;
937 j >= 0 && darray_item(key->symsMapNumEntries[ndx], j) == 0; j--)
938 key->numLevels[ndx]--;
943 AddActionsToKey(KeyInfo *key, struct xkb_keymap *keymap, ExprDef *arrayNdx,
944 ExprDef *value, SymbolsInfo *info)
949 struct xkb_any_action *toAct;
951 if (!GetGroupIndex(key, keymap, arrayNdx, ACTIONS, &ndx))
955 key->actsDefined |= (1 << ndx);
958 if (value->op != ExprActionList) {
959 WSGO("Bad expression type (%d) for action list value\n", value->op);
960 ACTION("Ignoring actions for group %d of %s\n", ndx,
961 longText(key->name));
964 if (!darray_empty(key->acts[ndx])) {
965 WSGO("Actions for key %s, group %d already defined\n",
966 longText(key->name), ndx);
969 for (nActs = 0, act = value->value.child; act != NULL; nActs++) {
970 act = (ExprDef *) act->common.next;
973 WSGO("Action list but not actions in AddActionsToKey\n");
976 if ((key->numLevels[ndx] < nActs || darray_empty(key->acts[ndx])) &&
977 !ResizeKeyGroup(key, ndx, nActs, nActs, true)) {
978 WSGO("Could not resize group %d of key %s\n", ndx,
979 longText(key->name));
980 ACTION("Actions lost\n");
983 key->actsDefined |= (1 << ndx);
985 toAct = (struct xkb_any_action *) darray_mem(key->acts[ndx], 0);
986 act = value->value.child;
987 for (i = 0; i < nActs; i++, toAct++) {
988 if (!HandleActionDef(act, keymap, toAct, info->action)) {
989 ERROR("Illegal action definition for %s\n",
990 longText(key->name));
991 ACTION("Action for group %d/level %d ignored\n", ndx + 1, i + 1);
993 act = (ExprDef *) act->common.next;
998 static const LookupEntry lockingEntries[] = {
999 { "true", XkbKB_Lock },
1000 { "yes", XkbKB_Lock },
1001 { "on", XkbKB_Lock },
1002 { "false", XkbKB_Default },
1003 { "no", XkbKB_Default },
1004 { "off", XkbKB_Default },
1005 { "permanent", XkbKB_Lock | XkbKB_Permanent },
1009 static const LookupEntry repeatEntries[] = {
1010 { "true", RepeatYes },
1011 { "yes", RepeatYes },
1012 { "on", RepeatYes },
1013 { "false", RepeatNo },
1015 { "off", RepeatNo },
1016 { "default", RepeatUndefined },
1021 SetSymbolsField(KeyInfo *key, struct xkb_keymap *keymap, char *field,
1022 ExprDef *arrayNdx, ExprDef *value, SymbolsInfo *info)
1027 if (strcasecmp(field, "type") == 0) {
1029 if ((!ExprResolveString(keymap->ctx, value, &tmp))
1030 && (warningLevel > 0)) {
1031 WARN("The type field of a key symbol map must be a string\n");
1032 ACTION("Ignoring illegal type definition\n");
1034 if (arrayNdx == NULL) {
1035 key->dfltType = xkb_atom_intern(keymap->ctx, tmp.str);
1036 key->defs.defined |= _Key_Type_Dflt;
1038 else if (!ExprResolveGroup(keymap->ctx, arrayNdx, &ndx)) {
1039 ERROR("Illegal group index for type of key %s\n",
1040 longText(key->name));
1041 ACTION("Definition with non-integer array index ignored\n");
1046 key->types[ndx.uval - 1] = xkb_atom_intern(keymap->ctx, tmp.str);
1047 key->typesDefined |= (1 << (ndx.uval - 1));
1051 else if (strcasecmp(field, "symbols") == 0)
1052 return AddSymbolsToKey(key, keymap, arrayNdx, value, info);
1053 else if (strcasecmp(field, "actions") == 0)
1054 return AddActionsToKey(key, keymap, arrayNdx, value, info);
1055 else if ((strcasecmp(field, "vmods") == 0) ||
1056 (strcasecmp(field, "virtualmods") == 0) ||
1057 (strcasecmp(field, "virtualmodifiers") == 0)) {
1058 ok = ExprResolveVModMask(value, &tmp, keymap);
1060 key->vmodmap = (tmp.uval >> 8);
1061 key->defs.defined |= _Key_VModMap;
1064 ERROR("Expected a virtual modifier mask, found %s\n",
1065 exprOpText(value->op));
1066 ACTION("Ignoring virtual modifiers definition for key %s\n",
1067 longText(key->name));
1070 else if ((strcasecmp(field, "locking") == 0) ||
1071 (strcasecmp(field, "lock") == 0) ||
1072 (strcasecmp(field, "locks") == 0)) {
1073 ok = ExprResolveEnum(keymap->ctx, value, &tmp, lockingEntries);
1075 key->behavior.type = tmp.uval;
1076 key->defs.defined |= _Key_Behavior;
1078 else if ((strcasecmp(field, "radiogroup") == 0) ||
1079 (strcasecmp(field, "permanentradiogroup") == 0) ||
1080 (strcasecmp(field, "allownone") == 0)) {
1081 ERROR("Radio groups not supported\n");
1082 ACTION("Ignoring radio group specification for key %s\n",
1083 longText(key->name));
1086 else if (uStrCasePrefix("overlay", field) ||
1087 uStrCasePrefix("permanentoverlay", field)) {
1088 ERROR("Overlays not supported\n");
1089 ACTION("Ignoring overlay specification for key %s\n",
1090 longText(key->name));
1092 else if ((strcasecmp(field, "repeating") == 0) ||
1093 (strcasecmp(field, "repeats") == 0) ||
1094 (strcasecmp(field, "repeat") == 0)) {
1095 ok = ExprResolveEnum(keymap->ctx, value, &tmp, repeatEntries);
1097 ERROR("Illegal repeat setting for %s\n",
1098 longText(key->name));
1099 ACTION("Non-boolean repeat setting ignored\n");
1102 key->repeat = tmp.uval;
1103 key->defs.defined |= _Key_Repeat;
1105 else if ((strcasecmp(field, "groupswrap") == 0) ||
1106 (strcasecmp(field, "wrapgroups") == 0)) {
1107 ok = ExprResolveBoolean(keymap->ctx, value, &tmp);
1109 ERROR("Illegal groupsWrap setting for %s\n",
1110 longText(key->name));
1111 ACTION("Non-boolean value ignored\n");
1115 key->groupInfo = XkbWrapIntoRange;
1117 key->groupInfo = XkbClampIntoRange;
1118 key->defs.defined |= _Key_GroupInfo;
1120 else if ((strcasecmp(field, "groupsclamp") == 0) ||
1121 (strcasecmp(field, "clampgroups") == 0)) {
1122 ok = ExprResolveBoolean(keymap->ctx, value, &tmp);
1124 ERROR("Illegal groupsClamp setting for %s\n",
1125 longText(key->name));
1126 ACTION("Non-boolean value ignored\n");
1130 key->groupInfo = XkbClampIntoRange;
1132 key->groupInfo = XkbWrapIntoRange;
1133 key->defs.defined |= _Key_GroupInfo;
1135 else if ((strcasecmp(field, "groupsredirect") == 0) ||
1136 (strcasecmp(field, "redirectgroups") == 0)) {
1137 if (!ExprResolveGroup(keymap->ctx, value, &tmp)) {
1138 ERROR("Illegal group index for redirect of key %s\n",
1139 longText(key->name));
1140 ACTION("Definition with non-integer group ignored\n");
1144 XkbSetGroupInfo(0, XkbRedirectIntoRange, tmp.uval - 1);
1145 key->defs.defined |= _Key_GroupInfo;
1148 ERROR("Unknown field %s in a symbol interpretation\n", field);
1149 ACTION("Definition ignored\n");
1156 SetGroupName(SymbolsInfo *info, struct xkb_keymap *keymap, ExprDef *arrayNdx,
1159 ExprResult tmp, name;
1161 if ((arrayNdx == NULL) && (warningLevel > 0)) {
1162 WARN("You must specify an index when specifying a group name\n");
1163 ACTION("Group name definition without array subscript ignored\n");
1166 if (!ExprResolveGroup(keymap->ctx, arrayNdx, &tmp)) {
1167 ERROR("Illegal index in group name definition\n");
1168 ACTION("Definition with non-integer array index ignored\n");
1171 if (!ExprResolveString(keymap->ctx, value, &name)) {
1172 ERROR("Group name must be a string\n");
1173 ACTION("Illegal name for group %d ignored\n", tmp.uval);
1176 info->groupNames[tmp.uval - 1 + info->explicit_group] =
1177 xkb_atom_intern(keymap->ctx, name.str);
1184 HandleSymbolsVar(VarDef *stmt, struct xkb_keymap *keymap, SymbolsInfo *info)
1186 ExprResult elem, field;
1190 if (ExprResolveLhs(keymap, stmt->name, &elem, &field, &arrayNdx) == 0)
1191 return 0; /* internal error, already reported */
1192 if (elem.str && (strcasecmp(elem.str, "key") == 0)) {
1193 ret = SetSymbolsField(&info->dflt, keymap, field.str, arrayNdx,
1196 else if ((elem.str == NULL) && ((strcasecmp(field.str, "name") == 0) ||
1197 (strcasecmp(field.str, "groupname") ==
1199 ret = SetGroupName(info, keymap, arrayNdx, stmt->value);
1201 else if ((elem.str == NULL)
1202 && ((strcasecmp(field.str, "groupswrap") == 0) ||
1203 (strcasecmp(field.str, "wrapgroups") == 0))) {
1204 ERROR("Global \"groupswrap\" not supported\n");
1205 ACTION("Ignored\n");
1208 else if ((elem.str == NULL)
1209 && ((strcasecmp(field.str, "groupsclamp") == 0) ||
1210 (strcasecmp(field.str, "clampgroups") == 0))) {
1211 ERROR("Global \"groupsclamp\" not supported\n");
1212 ACTION("Ignored\n");
1215 else if ((elem.str == NULL)
1216 && ((strcasecmp(field.str, "groupsredirect") == 0) ||
1217 (strcasecmp(field.str, "redirectgroups") == 0))) {
1218 ERROR("Global \"groupsredirect\" not supported\n");
1219 ACTION("Ignored\n");
1222 else if ((elem.str == NULL) &&
1223 (strcasecmp(field.str, "allownone") == 0)) {
1224 ERROR("Radio groups not supported\n");
1225 ACTION("Ignoring \"allownone\" specification\n");
1229 ret = SetActionField(keymap, elem.str, field.str, arrayNdx,
1230 stmt->value, &info->action);
1239 HandleSymbolsBody(VarDef *def, struct xkb_keymap *keymap, KeyInfo *key,
1243 ExprResult tmp, field;
1246 for (; def != NULL; def = (VarDef *) def->common.next) {
1247 if ((def->name) && (def->name->type == ExprFieldRef)) {
1248 ok = HandleSymbolsVar(def, keymap, info);
1252 if (def->name == NULL) {
1253 if ((def->value == NULL)
1254 || (def->value->op == ExprKeysymList))
1255 field.str = strdup("symbols");
1257 field.str = strdup("actions");
1261 ok = ExprResolveLhs(keymap, def->name, &tmp, &field,
1265 ok = SetSymbolsField(key, keymap, field.str, arrayNdx,
1274 SetExplicitGroup(SymbolsInfo *info, KeyInfo *key)
1276 unsigned group = info->explicit_group;
1281 if ((key->typesDefined | key->symsDefined | key->actsDefined) & ~1) {
1283 WARN("For the map %s an explicit group specified\n", info->name);
1284 WARN("but key %s has more than one group defined\n",
1285 longText(key->name));
1286 ACTION("All groups except first one will be ignored\n");
1287 for (i = 1; i < XkbNumKbdGroups; i++) {
1288 key->numLevels[i] = 0;
1289 darray_free(key->syms[i]);
1290 darray_free(key->acts[i]);
1294 key->typesDefined = key->symsDefined = key->actsDefined = 1 << group;
1296 key->numLevels[group] = key->numLevels[0];
1297 key->numLevels[0] = 0;
1298 key->syms[group] = key->syms[0];
1299 darray_init(key->syms[0]);
1300 key->symsMapIndex[group] = key->symsMapIndex[0];
1301 darray_init(key->symsMapIndex[0]);
1302 key->symsMapNumEntries[group] = key->symsMapNumEntries[0];
1303 darray_init(key->symsMapNumEntries[0]);
1304 key->acts[group] = key->acts[0];
1305 darray_init(key->acts[0]);
1306 key->types[group] = key->types[0];
1312 HandleSymbolsDef(SymbolsDef *stmt, struct xkb_keymap *keymap,
1317 InitKeyInfo(&key, info->file_id);
1318 CopyKeyInfo(&info->dflt, &key, false);
1319 key.defs.merge = stmt->merge;
1320 key.name = KeyNameToLong(stmt->keyName);
1321 if (!HandleSymbolsBody((VarDef *) stmt->symbols, keymap, &key, info)) {
1326 if (!SetExplicitGroup(info, &key)) {
1331 if (!AddKeySymbols(info, &key, keymap)) {
1339 HandleModMapDef(ModMapDef *def, struct xkb_keymap *keymap, SymbolsInfo *info)
1346 if (!LookupModIndex(keymap->ctx, NULL, def->modifier, TypeInt, &rtrn)) {
1347 ERROR("Illegal modifier map definition\n");
1348 ACTION("Ignoring map for non-modifier \"%s\"\n",
1349 xkb_atom_text(keymap->ctx, def->modifier));
1353 tmp.modifier = rtrn.uval;
1354 for (key = def->keys; key != NULL; key = (ExprDef *) key->common.next) {
1355 if ((key->op == ExprValue) && (key->type == TypeKeyName)) {
1356 tmp.haveSymbol = false;
1357 tmp.u.keyName = KeyNameToLong(key->value.keyName);
1359 else if (ExprResolveKeySym(keymap->ctx, key, &rtrn)) {
1360 tmp.haveSymbol = true;
1361 tmp.u.keySym = rtrn.uval;
1364 ERROR("Modmap entries may contain only key names or keysyms\n");
1365 ACTION("Illegal definition for %s modifier ignored\n",
1366 XkbcModIndexText(tmp.modifier));
1370 ok = AddModMapEntry(info, &tmp) && ok;
1376 HandleSymbolsFile(XkbFile *file, struct xkb_keymap *keymap,
1377 enum merge_mode merge, SymbolsInfo *info)
1382 info->name = uDupString(file->name);
1386 switch (stmt->stmtType) {
1388 if (!HandleIncludeSymbols((IncludeStmt *) stmt, keymap, info))
1391 case StmtSymbolsDef:
1392 if (!HandleSymbolsDef((SymbolsDef *) stmt, keymap, info))
1396 if (!HandleSymbolsVar((VarDef *) stmt, keymap, info))
1400 if (!HandleVModDef((VModDef *) stmt, keymap, merge, &info->vmods))
1404 ERROR("Interpretation files may not include other types\n");
1405 ACTION("Ignoring definition of symbol interpretation\n");
1408 case StmtKeycodeDef:
1409 ERROR("Interpretation files may not include other types\n");
1410 ACTION("Ignoring definition of key name\n");
1414 if (!HandleModMapDef((ModMapDef *) stmt, keymap, info))
1418 WSGO("Unexpected statement type %d in HandleSymbolsFile\n",
1423 if (info->errorCount > 10) {
1425 ERROR("Too many errors\n");
1427 ACTION("Abandoning symbols file \"%s\"\n", file->topName);
1434 * Given a keysym @sym, find the keycode which generates it
1435 * (returned in @kc_rtrn). This is used for example in a modifier
1436 * map definition, such as:
1437 * modifier_map Lock { Caps_Lock };
1438 * where we want to add the Lock modifier to the modmap of the key
1439 * which matches the keysym Caps_Lock.
1440 * Since there can be many keys which generates the keysym, the key
1441 * is chosen first by lowest group in which the keysym appears, than
1442 * by lowest level and than by lowest key code.
1445 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym,
1446 xkb_keycode_t *kc_rtrn)
1449 unsigned int group, level, min_group = UINT_MAX, min_level = UINT_MAX;
1451 for (key = keymap->min_key_code; key <= keymap->max_key_code; key++) {
1452 for (group = 0; group < XkbKeyNumGroups(keymap, key); group++) {
1453 for (level = 0; level < XkbKeyGroupWidth(keymap, key, group);
1455 if (XkbKeyNumSyms(keymap, key, group, level) != 1 ||
1456 (XkbKeySymEntry(keymap, key, group, level))[0] != sym)
1460 * If the keysym was found in a group or level > 0, we must
1461 * keep looking since we might find a key in which the keysym
1462 * is in a lower group or level.
1464 if (group < min_group ||
1465 (group == min_group && level < min_level)) {
1467 if (group == 0 && level == 0) {
1479 return min_group != UINT_MAX;
1483 * Find the given name in the keymap->map->types and return its index.
1485 * @param atom The atom to search for.
1486 * @param type_rtrn Set to the index of the name if found.
1488 * @return true if found, false otherwise.
1491 FindNamedType(struct xkb_keymap *keymap, xkb_atom_t atom, unsigned *type_rtrn)
1494 const char *name = xkb_atom_text(keymap->ctx, atom);
1495 struct xkb_key_type *type;
1498 darray_foreach(type, keymap->types) {
1499 if (strcmp(type->name, name) == 0) {
1510 * Assign a type to the given sym and return the Atom for the type assigned.
1513 * - ONE_LEVEL for width 0/1
1514 * - ALPHABETIC for 2 shift levels, with lower/upercase
1515 * - KEYPAD for keypad keys.
1516 * - TWO_LEVEL for other 2 shift level keys.
1517 * and the same for four level keys.
1519 * @param width Number of sysms in syms.
1520 * @param syms The keysyms for the given key (must be size width).
1521 * @param typeNameRtrn Set to the Atom of the type name.
1523 * @returns true if a type could be found, false otherwise.
1525 * FIXME: I need to take the KeyInfo so I can look at symsMapIndex and
1526 * all that fun stuff rather than just assuming there's always one
1530 FindAutomaticType(struct xkb_keymap *keymap, int width,
1531 const xkb_keysym_t *syms, xkb_atom_t *typeNameRtrn,
1535 if ((width == 1) || (width == 0)) {
1536 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ONE_LEVEL");
1539 else if (width == 2) {
1540 if (syms && xkb_keysym_is_lower(syms[0]) &&
1541 xkb_keysym_is_upper(syms[1])) {
1542 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ALPHABETIC");
1544 else if (syms && (xkb_keysym_is_keypad(syms[0]) ||
1545 xkb_keysym_is_keypad(syms[1]))) {
1546 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "KEYPAD");
1550 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "TWO_LEVEL");
1554 else if (width <= 4) {
1555 if (syms && xkb_keysym_is_lower(syms[0]) &&
1556 xkb_keysym_is_upper(syms[1]))
1557 if (xkb_keysym_is_lower(syms[2]) && xkb_keysym_is_upper(syms[3]))
1559 xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_ALPHABETIC");
1561 *typeNameRtrn = xkb_atom_intern(keymap->ctx,
1562 "FOUR_LEVEL_SEMIALPHABETIC");
1564 else if (syms && (xkb_keysym_is_keypad(syms[0]) ||
1565 xkb_keysym_is_keypad(syms[1])))
1566 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_KEYPAD");
1568 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL");
1569 /* XXX: why not set autoType here? */
1571 return ((width >= 0) && (width <= 4));
1575 * Ensure the given KeyInfo is in a coherent state, i.e. no gaps between the
1576 * groups, and reduce to one group if all groups are identical anyway.
1579 PrepareKeyDef(KeyInfo * key)
1581 int i, j, width, defined, lastGroup;
1584 defined = key->symsDefined | key->actsDefined | key->typesDefined;
1585 /* get highest group number */
1586 for (i = XkbNumKbdGroups - 1; i >= 0; i--) {
1587 if (defined & (1 << i))
1595 /* If there are empty groups between non-empty ones fill them with data */
1596 /* from the first group. */
1597 /* We can make a wrong assumption here. But leaving gaps is worse. */
1598 for (i = lastGroup; i > 0; i--) {
1599 if (defined & (1 << i))
1601 width = key->numLevels[0];
1602 if (key->typesDefined & 1) {
1603 for (j = 0; j < width; j++) {
1604 key->types[i] = key->types[0];
1606 key->typesDefined |= 1 << i;
1608 if ((key->actsDefined & 1) && !darray_empty(key->acts[0])) {
1609 darray_copy(key->acts[i], key->acts[0]);
1610 key->actsDefined |= 1 << i;
1612 if ((key->symsDefined & 1) && !darray_empty(key->syms[0])) {
1613 darray_copy(key->syms[i], key->syms[0]);
1614 darray_copy(key->symsMapIndex[i], key->symsMapIndex[0]);
1615 darray_copy(key->symsMapNumEntries[i], key->symsMapNumEntries[0]);
1616 key->symsDefined |= 1 << i;
1619 key->numLevels[i] = key->numLevels[0];
1622 /* If all groups are completely identical remove them all */
1623 /* exept the first one. */
1625 for (i = lastGroup; i > 0; i--) {
1626 if ((key->numLevels[i] != key->numLevels[0]) ||
1627 (key->types[i] != key->types[0])) {
1631 if (!darray_same(key->syms[i], key->syms[0]) &&
1632 (darray_empty(key->syms[i]) || darray_empty(key->syms[0]) ||
1633 darray_size(key->syms[i]) != darray_size(key->syms[0]) ||
1634 memcmp(darray_mem(key->syms[i], 0),
1635 darray_mem(key->syms[0], 0),
1636 sizeof(xkb_keysym_t) * darray_size(key->syms[0])))) {
1640 if (!darray_same(key->symsMapIndex[i], key->symsMapIndex[0]) &&
1641 (darray_empty(key->symsMapIndex[i]) ||
1642 darray_empty(key->symsMapIndex[0]) ||
1643 memcmp(darray_mem(key->symsMapIndex[i], 0),
1644 darray_mem(key->symsMapIndex[0], 0),
1645 key->numLevels[0] * sizeof(int)))) {
1649 if (!darray_same(key->symsMapNumEntries[i],
1650 key->symsMapNumEntries[0]) &&
1651 (darray_empty(key->symsMapNumEntries[i]) ||
1652 darray_empty(key->symsMapNumEntries[0]) ||
1653 memcmp(darray_mem(key->symsMapNumEntries[i], 0),
1654 darray_mem(key->symsMapNumEntries[0], 0),
1655 key->numLevels[0] * sizeof(size_t)))) {
1659 if (!darray_same(key->acts[i], key->acts[0]) &&
1660 (darray_empty(key->acts[i]) || darray_empty(key->acts[0]) ||
1661 memcmp(darray_mem(key->acts[i], 0),
1662 darray_mem(key->acts[0], 0),
1663 key->numLevels[0] * sizeof(union xkb_action)))) {
1669 for (i = lastGroup; i > 0; i--) {
1670 key->numLevels[i] = 0;
1671 darray_free(key->syms[i]);
1672 darray_free(key->symsMapIndex[i]);
1673 darray_free(key->symsMapNumEntries[i]);
1674 darray_free(key->acts[i]);
1677 key->symsDefined &= 1;
1678 key->actsDefined &= 1;
1679 key->typesDefined &= 1;
1684 * Copy the KeyInfo into the keyboard description.
1686 * This function recurses.
1689 CopySymbolsDef(struct xkb_keymap *keymap, KeyInfo *key, int start_from)
1693 unsigned int sizeSyms = 0;
1694 unsigned width, tmp, nGroups;
1695 struct xkb_key_type * type;
1696 bool haveActions, autoType, useAlias;
1697 unsigned types[XkbNumKbdGroups];
1698 union xkb_action *outActs;
1699 unsigned int symIndex = 0;
1700 struct xkb_sym_map *sym_map;
1702 useAlias = (start_from == 0);
1704 /* get the keycode for the key. */
1705 if (!FindNamedKey(keymap, key->name, &kc, useAlias,
1706 CreateKeyNames(keymap), start_from)) {
1707 if ((start_from == 0) && (warningLevel >= 5)) {
1708 WARN("Key %s not found in keycodes\n", longText(key->name));
1709 ACTION("Symbols ignored\n");
1714 haveActions = false;
1715 for (i = width = nGroups = 0; i < XkbNumKbdGroups; i++) {
1716 if (((i + 1) > nGroups)
1717 && (((key->symsDefined | key->actsDefined) & (1 << i))
1718 || (key->typesDefined) & (1 << i)))
1720 if (!darray_empty(key->acts[i]))
1723 /* Assign the type to the key, if it is missing. */
1724 if (key->types[i] == XKB_ATOM_NONE) {
1725 if (key->dfltType != XKB_ATOM_NONE)
1726 key->types[i] = key->dfltType;
1727 else if (FindAutomaticType(keymap, key->numLevels[i],
1728 darray_mem(key->syms[i], 0),
1729 &key->types[i], &autoType)) { }
1731 if (warningLevel >= 5) {
1732 WARN("No automatic type for %d symbols\n",
1733 (unsigned int) key->numLevels[i]);
1734 ACTION("Using %s for the %s key (keycode %d)\n",
1735 xkb_atom_text(keymap->ctx, key->types[i]),
1736 longText(key->name), kc);
1740 if (FindNamedType(keymap, key->types[i], &types[i])) {
1741 if (!autoType || key->numLevels[i] > 2)
1742 keymap->explicit[kc] |= (1 << i);
1745 if (warningLevel >= 3) {
1746 WARN("Type \"%s\" is not defined\n",
1747 xkb_atom_text(keymap->ctx, key->types[i]));
1748 ACTION("Using TWO_LEVEL for the %s key (keycode %d)\n",
1749 longText(key->name), kc);
1751 types[i] = XkbTwoLevelIndex;
1753 /* if the type specifies fewer levels than the key has, shrink the key */
1754 type = &darray_item(keymap->types, types[i]);
1755 if (type->num_levels < key->numLevels[i]) {
1756 if (warningLevel > 0) {
1757 WARN("Type \"%s\" has %d levels, but %s has %d symbols\n",
1758 type->name, type->num_levels,
1759 xkb_atom_text(keymap->ctx, key->name), key->numLevels[i]);
1760 ACTION("Ignoring extra symbols\n");
1762 key->numLevels[i] = type->num_levels;
1764 if (key->numLevels[i] > width)
1765 width = key->numLevels[i];
1766 if (type->num_levels > width)
1767 width = type->num_levels;
1768 sizeSyms += darray_size(key->syms[i]);
1771 if (!XkbcResizeKeySyms(keymap, kc, sizeSyms)) {
1772 WSGO("Could not enlarge symbols for %s (keycode %d)\n",
1773 longText(key->name), kc);
1777 outActs = XkbcResizeKeyActions(keymap, kc, width * nGroups);
1778 if (outActs == NULL) {
1779 WSGO("Could not enlarge actions for %s (key %d)\n",
1780 longText(key->name), kc);
1783 keymap->explicit[kc] |= XkbExplicitInterpretMask;
1788 sym_map = &darray_item(keymap->key_sym_map, kc);
1790 if (key->defs.defined & _Key_GroupInfo)
1793 i = sym_map->group_info;
1795 sym_map->group_info = XkbSetNumGroups(i, nGroups);
1796 sym_map->width = width;
1797 sym_map->sym_index = uTypedCalloc(nGroups * width, int);
1798 sym_map->num_syms = uTypedCalloc(nGroups * width, unsigned int);
1800 for (i = 0; i < nGroups; i++) {
1801 /* assign kt_index[i] to the index of the type in map->types.
1802 * kt_index[i] may have been set by a previous run (if we have two
1803 * layouts specified). Let's not overwrite it with the ONE_LEVEL
1804 * default group if we dont even have keys for this group anyway.
1806 * FIXME: There should be a better fix for this.
1808 if (key->numLevels[i])
1809 sym_map->kt_index[i] = types[i];
1810 if (!darray_empty(key->syms[i])) {
1811 /* fill key to "width" symbols*/
1812 for (tmp = 0; tmp < width; tmp++) {
1813 if (tmp < key->numLevels[i] &&
1814 darray_item(key->symsMapNumEntries[i], tmp) != 0) {
1815 memcpy(darray_mem(sym_map->syms, symIndex),
1816 darray_mem(key->syms[i],
1817 darray_item(key->symsMapIndex[i], tmp)),
1818 darray_item(key->symsMapNumEntries[i],
1819 tmp) * sizeof(xkb_keysym_t));
1820 sym_map->sym_index[(i * width) + tmp] = symIndex;
1821 sym_map->num_syms[(i * width) + tmp] =
1822 darray_item(key->symsMapNumEntries[i], tmp);
1823 symIndex += sym_map->num_syms[(i * width) + tmp];
1826 sym_map->sym_index[(i * width) + tmp] = -1;
1827 sym_map->num_syms[(i * width) + tmp] = 0;
1829 if (outActs != NULL && !darray_empty(key->acts[i])) {
1830 if (tmp < key->numLevels[i])
1831 outActs[tmp] = darray_item(key->acts[i], tmp);
1833 outActs[tmp].type = XkbSA_NoAction;
1838 switch (key->behavior.type & XkbKB_OpMask) {
1843 keymap->behaviors[kc] = key->behavior;
1844 keymap->explicit[kc] |= XkbExplicitBehaviorMask;
1847 if (key->defs.defined & _Key_VModMap) {
1848 keymap->vmodmap[kc] = key->vmodmap;
1849 keymap->explicit[kc] |= XkbExplicitVModMapMask;
1851 if (key->repeat != RepeatUndefined) {
1852 if (key->repeat == RepeatYes)
1853 keymap->ctrls->per_key_repeat[kc / 8] |= (1 << (kc % 8));
1855 keymap->ctrls->per_key_repeat[kc / 8] &= ~(1 << (kc % 8));
1856 keymap->explicit[kc] |= XkbExplicitAutoRepeatMask;
1859 /* do the same thing for the next key */
1860 CopySymbolsDef(keymap, key, kc + 1);
1865 CopyModMapDef(struct xkb_keymap *keymap, ModMapEntry *entry)
1869 if (!entry->haveSymbol &&
1870 !FindNamedKey(keymap, entry->u.keyName, &kc, true,
1871 CreateKeyNames(keymap), 0)) {
1872 if (warningLevel >= 5) {
1873 WARN("Key %s not found in keycodes\n",
1874 longText(entry->u.keyName));
1875 ACTION("Modifier map entry for %s not updated\n",
1876 XkbcModIndexText(entry->modifier));
1880 else if (entry->haveSymbol &&
1881 !FindKeyForSymbol(keymap, entry->u.keySym, &kc)) {
1882 if (warningLevel > 5) {
1883 WARN("Key \"%s\" not found in symbol map\n",
1884 XkbcKeysymText(entry->u.keySym));
1885 ACTION("Modifier map entry for %s not updated\n",
1886 XkbcModIndexText(entry->modifier));
1890 keymap->modmap[kc] |= (1 << entry->modifier);
1895 * Handle the xkb_symbols section of an xkb file.
1897 * @param file The parsed xkb_symbols section of the xkb file.
1898 * @param keymap Handle to the keyboard description to store the symbols in.
1899 * @param merge Merge strategy (e.g. MERGE_OVERRIDE).
1902 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1903 enum merge_mode merge)
1909 InitSymbolsInfo(&info, keymap, file->id);
1910 info.dflt.defs.merge = merge;
1912 HandleSymbolsFile(file, keymap, merge, &info);
1914 if (darray_empty(info.keys))
1917 if (info.errorCount != 0)
1920 /* alloc memory in the xkb struct */
1921 if (XkbcAllocNames(keymap, XkbGroupNamesMask, 0) != Success) {
1922 WSGO("Can not allocate names in CompileSymbols\n");
1923 ACTION("Symbols not added\n");
1927 darray_resize0(keymap->key_sym_map, keymap->max_key_code + 1);
1928 keymap->modmap = calloc(keymap->max_key_code + 1,
1929 sizeof(*keymap->modmap));
1930 if (!keymap->modmap)
1933 i = keymap->max_key_code + 1;
1934 keymap->explicit = calloc(keymap->max_key_code + 1,
1935 sizeof(*keymap->explicit));
1936 if (!keymap->explicit)
1939 darray_resize0(keymap->acts, darray_size(keymap->acts) + 32 + 1);
1940 darray_resize0(keymap->key_acts, keymap->max_key_code + 1);
1942 keymap->behaviors = calloc(keymap->max_key_code + 1,
1943 sizeof(*keymap->behaviors));
1944 if (!keymap->behaviors)
1947 keymap->vmodmap = calloc(keymap->max_key_code + 1,
1948 sizeof(*keymap->vmodmap));
1949 if (!keymap->vmodmap)
1952 if (XkbcAllocControls(keymap) != Success) {
1953 WSGO("Could not allocate controls in CompileSymbols\n");
1954 ACTION("Symbols not added\n");
1959 keymap->names->symbols = strdup(info.name);
1961 /* now copy info into xkb. */
1962 ApplyAliases(keymap, &info.aliases);
1964 for (i = 0; i < XkbNumKbdGroups; i++) {
1965 if (info.groupNames[i] != XKB_ATOM_NONE) {
1966 free(keymap->names->groups[i]);
1967 keymap->names->groups[i] = xkb_atom_strdup(keymap->ctx,
1968 info.groupNames[i]);
1973 darray_foreach(key, info.keys)
1977 darray_foreach(key, info.keys)
1978 if (!CopySymbolsDef(keymap, key, 0))
1981 if (warningLevel > 3) {
1982 for (i = keymap->min_key_code; i <= keymap->max_key_code; i++) {
1983 if (darray_item(keymap->names->keys, i).name[0] == '\0')
1986 if (XkbKeyNumGroups(keymap, i) < 1) {
1988 memcpy(buf, darray_item(keymap->names->keys, i).name, 4);
1990 WARN("No symbols defined for <%s> (keycode %d)\n", buf, i);
1996 ModMapEntry *mm, *next;
1997 for (mm = info.modMap; mm != NULL; mm = next) {
1998 if (!CopyModMapDef(keymap, mm))
2000 next = (ModMapEntry *) mm->defs.next;
2004 FreeSymbolsInfo(&info);
2008 FreeSymbolsInfo(&info);