1 /************************************************************
2 * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
4 * Permission to use, copy, modify, and distribute this
5 * software and its documentation for any purpose and without
6 * fee is hereby granted, provided that the above copyright
7 * notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting
9 * documentation, and that the name of Silicon Graphics not be
10 * used in advertising or publicity pertaining to distribution
11 * of the software without specific prior written permission.
12 * Silicon Graphics makes no representation about the suitability
13 * of this software for any purpose. It is provided "as is"
14 * without any express or implied warranty.
16 * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
23 * THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 ********************************************************/
29 #include "xkbcomp-priv.h"
30 #include "parseutils.h"
34 /***====================================================================***/
36 /* Needed to work with the typechecker. */
37 typedef darray(xkb_keysym_t) darray_xkb_keysym_t;
38 typedef darray(union xkb_action) darray_xkb_action;
42 #define RepeatUndefined ~((unsigned) 0)
44 #define _Key_Syms (1 << 0)
45 #define _Key_Acts (1 << 1)
46 #define _Key_Repeat (1 << 2)
47 #define _Key_Behavior (1 << 3)
48 #define _Key_Type_Dflt (1 << 4)
49 #define _Key_Types (1 << 5)
50 #define _Key_GroupInfo (1 << 6)
51 #define _Key_VModMap (1 << 7)
53 static inline const char *
54 longText(unsigned long val)
58 LongToKeyName(val, buf);
59 return XkbcKeyNameText(buf);
62 typedef struct _KeyInfo {
63 unsigned short defined;
65 enum merge_mode merge;
67 unsigned long name; /* the 4 chars of the key name, as long */
68 unsigned char typesDefined;
69 unsigned char symsDefined;
70 unsigned char actsDefined;
71 unsigned int numLevels[XkbNumKbdGroups];
73 /* syms[group] -> Single array for all the keysyms in the group. */
74 darray_xkb_keysym_t syms[XkbNumKbdGroups];
76 * symsMapIndex[group][level] -> The index from which the syms for
77 * the level begin in the syms[group] array. Remember each keycode
78 * can have multiple keysyms in each level (that is, each key press
79 * can result in multiple keysyms).
81 darray(int) symsMapIndex[XkbNumKbdGroups];
83 * symsMapNumEntries[group][level] -> How many syms are in
84 * syms[group][symsMapIndex[group][level]].
86 darray(size_t) symsMapNumEntries[XkbNumKbdGroups];
88 darray_xkb_action acts[XkbNumKbdGroups];
90 xkb_atom_t types[XkbNumKbdGroups];
92 struct xkb_behavior behavior;
93 unsigned short vmodmap;
96 uint8_t out_of_range_group_action;
97 xkb_group_index_t out_of_range_group_number;
101 * Init the given key info to sane values.
104 InitKeyInfo(KeyInfo *keyi, unsigned file_id)
107 static const char dflt[4] = "*";
110 keyi->file_id = file_id;
111 keyi->merge = MERGE_OVERRIDE;
112 keyi->name = KeyNameToLong(dflt);
113 keyi->typesDefined = keyi->symsDefined = keyi->actsDefined = 0;
115 for (i = 0; i < XkbNumKbdGroups; i++) {
116 keyi->numLevels[i] = 0;
117 keyi->types[i] = XKB_ATOM_NONE;
118 darray_init(keyi->syms[i]);
119 darray_init(keyi->symsMapIndex[i]);
120 darray_init(keyi->symsMapNumEntries[i]);
121 darray_init(keyi->acts[i]);
124 keyi->dfltType = XKB_ATOM_NONE;
125 keyi->behavior.type = XkbKB_Default;
126 keyi->behavior.data = 0;
128 keyi->repeat = RepeatUndefined;
129 keyi->out_of_range_group_action = 0;
130 keyi->out_of_range_group_number = 0;
134 FreeKeyInfo(KeyInfo *keyi)
138 for (i = 0; i < XkbNumKbdGroups; i++) {
139 darray_free(keyi->syms[i]);
140 darray_free(keyi->symsMapIndex[i]);
141 darray_free(keyi->symsMapNumEntries[i]);
142 darray_free(keyi->acts[i]);
147 * Copy old into new, optionally reset old to 0.
148 * If old is reset, new simply re-uses old's memory. Otherwise, the memory is
149 * newly allocated and new points to the new memory areas.
152 CopyKeyInfo(KeyInfo * old, KeyInfo * new, bool clearOld)
159 for (i = 0; i < XkbNumKbdGroups; i++) {
160 old->numLevels[i] = 0;
161 darray_init(old->symsMapIndex[i]);
162 darray_init(old->symsMapNumEntries[i]);
163 darray_init(old->syms[i]);
164 darray_init(old->acts[i]);
168 for (i = 0; i < XkbNumKbdGroups; i++) {
169 darray_copy(new->syms[i], old->syms[i]);
170 darray_copy(new->symsMapIndex[i], old->symsMapIndex[i]);
171 darray_copy(new->symsMapNumEntries[i], old->symsMapNumEntries[i]);
172 darray_copy(new->acts[i], old->acts[i]);
179 /***====================================================================***/
181 typedef struct _ModMapEntry {
183 enum merge_mode merge;
187 unsigned long keyName;
192 typedef struct _SymbolsInfo {
193 char *name; /* e.g. pc+us+inet(evdev) */
196 enum merge_mode merge;
197 xkb_group_index_t explicit_group;
198 darray(KeyInfo) keys;
202 xkb_atom_t groupNames[XkbNumKbdGroups];
208 InitSymbolsInfo(SymbolsInfo * info, struct xkb_keymap *keymap,
214 info->explicit_group = 0;
215 info->errorCount = 0;
216 info->file_id = file_id;
217 info->merge = MERGE_OVERRIDE;
218 darray_init(info->keys);
219 darray_growalloc(info->keys, 110);
220 list_init(&info->modMaps);
221 for (i = 0; i < XkbNumKbdGroups; i++)
222 info->groupNames[i] = XKB_ATOM_NONE;
223 InitKeyInfo(&info->dflt, file_id);
224 InitVModInfo(&info->vmods, keymap);
229 FreeSymbolsInfo(SymbolsInfo * info)
232 ModMapEntry *mm, *next;
235 darray_foreach(keyi, info->keys) {
238 darray_free(info->keys);
239 list_foreach_safe(mm, next, &info->modMaps, entry)
241 memset(info, 0, sizeof(SymbolsInfo));
245 ResizeKeyGroup(KeyInfo *keyi, xkb_group_index_t group, unsigned int numLevels,
246 unsigned sizeSyms, bool forceActions)
250 if (darray_size(keyi->syms[group]) < sizeSyms)
251 darray_resize0(keyi->syms[group], sizeSyms);
253 if (darray_empty(keyi->symsMapIndex[group]) ||
254 keyi->numLevels[group] < numLevels) {
255 darray_resize(keyi->symsMapIndex[group], numLevels);
256 for (i = keyi->numLevels[group]; i < numLevels; i++)
257 darray_item(keyi->symsMapIndex[group], i) = -1;
260 if (darray_empty(keyi->symsMapNumEntries[group]) ||
261 keyi->numLevels[group] < numLevels)
262 darray_resize0(keyi->symsMapNumEntries[group], numLevels);
264 if ((forceActions && (keyi->numLevels[group] < numLevels ||
265 darray_empty(keyi->acts[group]))) ||
266 (keyi->numLevels[group] < numLevels && !darray_empty(keyi->acts[group])))
267 darray_resize0(keyi->acts[group], numLevels);
269 if (keyi->numLevels[group] < numLevels)
270 keyi->numLevels[group] = numLevels;
275 enum key_group_selector {
282 MergeKeyGroups(SymbolsInfo * info,
283 KeyInfo * into, KeyInfo * from, xkb_group_index_t group)
285 darray_xkb_keysym_t resultSyms;
286 enum key_group_selector using = NONE;
287 darray_xkb_action resultActs;
288 unsigned int resultWidth;
289 unsigned int resultSize = 0;
292 bool report, clobber;
294 clobber = (from->merge != MERGE_AUGMENT);
296 report = (warningLevel > 9) ||
297 (into->file_id == from->file_id && warningLevel > 0);
299 darray_init(resultSyms);
301 if (into->numLevels[group] >= from->numLevels[group]) {
302 resultActs = into->acts[group];
303 resultWidth = into->numLevels[group];
306 resultActs = from->acts[group];
307 resultWidth = from->numLevels[group];
308 darray_resize(into->symsMapIndex[group],
309 from->numLevels[group]);
310 darray_resize0(into->symsMapNumEntries[group],
311 from->numLevels[group]);
313 for (i = into->numLevels[group]; i < from->numLevels[group]; i++)
314 darray_item(into->symsMapIndex[group], i) = -1;
317 if (darray_empty(resultActs) && (!darray_empty(into->acts[group]) ||
318 !darray_empty(from->acts[group]))) {
319 darray_resize0(resultActs, resultWidth);
320 for (i = 0; i < resultWidth; i++) {
321 union xkb_action *fromAct = NULL, *toAct = NULL;
323 if (!darray_empty(from->acts[group]))
324 fromAct = &darray_item(from->acts[group], i);
326 if (!darray_empty(into->acts[group]))
327 toAct = &darray_item(into->acts[group], i);
329 if (((fromAct == NULL) || (fromAct->type == XkbSA_NoAction))
330 && (toAct != NULL)) {
331 darray_item(resultActs, i) = *toAct;
333 else if (((toAct == NULL) || (toAct->type == XkbSA_NoAction))
334 && (fromAct != NULL)) {
335 darray_item(resultActs, i) = *fromAct;
338 union xkb_action *use, *ignore;
349 ("Multiple actions for level %d/group %u on key %s\n",
350 i + 1, group + 1, longText(into->name));
351 ACTION("Using %s, ignoring %s\n",
352 XkbcActionTypeText(use->type),
353 XkbcActionTypeText(ignore->type));
356 darray_item(resultActs, i) = *use;
361 for (i = 0; i < resultWidth; i++) {
362 unsigned int fromSize = 0;
365 if (!darray_empty(from->symsMapNumEntries[group]) &&
366 i < from->numLevels[group])
367 fromSize = darray_item(from->symsMapNumEntries[group], i);
369 if (!darray_empty(into->symsMapNumEntries[group]) &&
370 i < into->numLevels[group])
371 toSize = darray_item(into->symsMapNumEntries[group], i);
374 resultSize += toSize;
377 else if (toSize == 0 || clobber) {
378 resultSize += fromSize;
382 resultSize += toSize;
391 resultSyms = from->syms[group];
392 darray_free(into->symsMapNumEntries[group]);
393 darray_free(into->symsMapIndex[group]);
394 into->symsMapNumEntries[group] = from->symsMapNumEntries[group];
395 into->symsMapIndex[group] = from->symsMapIndex[group];
396 darray_init(from->symsMapNumEntries[group]);
397 darray_init(from->symsMapIndex[group]);
400 else if (using == TO) {
401 resultSyms = into->syms[group];
405 darray_resize0(resultSyms, resultSize);
407 for (i = 0; i < resultWidth; i++) {
408 enum key_group_selector use = NONE;
409 unsigned int fromSize = 0;
410 unsigned int toSize = 0;
412 if (i < from->numLevels[group])
413 fromSize = darray_item(from->symsMapNumEntries[group], i);
415 if (i < into->numLevels[group])
416 toSize = darray_item(into->symsMapNumEntries[group], i);
418 if (fromSize == 0 && toSize == 0) {
419 darray_item(into->symsMapIndex[group], i) = -1;
420 darray_item(into->symsMapNumEntries[group], i) = 0;
426 else if (toSize == 0 || clobber)
431 if (toSize && fromSize && report) {
432 INFO("Multiple symbols for group %u, level %d on key %s\n",
433 group + 1, i + 1, longText(into->name));
434 ACTION("Using %s, ignoring %s\n",
435 (use == FROM ? "from" : "to"),
436 (use == FROM ? "to" : "from"));
440 memcpy(darray_mem(resultSyms, cur_idx),
441 darray_mem(from->syms[group],
442 darray_item(from->symsMapIndex[group], i)),
443 darray_item(from->symsMapNumEntries[group],
444 i) * sizeof(xkb_keysym_t));
445 darray_item(into->symsMapIndex[group], i) = cur_idx;
446 darray_item(into->symsMapNumEntries[group], i) =
447 darray_item(from->symsMapNumEntries[group], i);
450 memcpy(darray_mem(resultSyms, cur_idx),
451 darray_mem(into->syms[group],
452 darray_item(into->symsMapIndex[group], i)),
453 darray_item(into->symsMapNumEntries[group],
454 i) * sizeof(xkb_keysym_t));
455 darray_item(into->symsMapIndex[group], i) = cur_idx;
457 cur_idx += darray_item(into->symsMapNumEntries[group], i);
461 if (!darray_same(resultActs, into->acts[group]))
462 darray_free(into->acts[group]);
463 if (!darray_same(resultActs, from->acts[group]))
464 darray_free(from->acts[group]);
465 into->numLevels[group] = resultWidth;
466 if (!darray_same(resultSyms, into->syms[group]))
467 darray_free(into->syms[group]);
468 into->syms[group] = resultSyms;
469 if (!darray_same(resultSyms, from->syms[group]))
470 darray_free(from->syms[group]);
471 darray_init(from->syms[group]);
472 darray_free(from->symsMapIndex[group]);
473 darray_free(from->symsMapNumEntries[group]);
474 into->acts[group] = resultActs;
475 darray_init(from->acts[group]);
476 if (!darray_empty(into->syms[group]))
477 into->symsDefined |= (1 << group);
478 from->symsDefined &= ~(1 << group);
479 into->actsDefined |= (1 << group);
480 from->actsDefined &= ~(1 << group);
486 MergeKeys(SymbolsInfo *info, struct xkb_keymap *keymap,
487 KeyInfo *into, KeyInfo *from)
490 unsigned collide = 0;
493 if (from->merge == MERGE_REPLACE) {
494 for (i = 0; i < XkbNumKbdGroups; i++) {
495 if (into->numLevels[i] != 0) {
496 darray_free(into->syms[i]);
497 darray_free(into->acts[i]);
501 memset(from, 0, sizeof(KeyInfo));
504 report = ((warningLevel > 9) ||
505 ((into->file_id == from->file_id)
506 && (warningLevel > 0)));
507 for (i = 0; i < XkbNumKbdGroups; i++) {
508 if (from->numLevels[i] > 0) {
509 if (into->numLevels[i] == 0) {
510 into->numLevels[i] = from->numLevels[i];
511 into->syms[i] = from->syms[i];
512 into->symsMapIndex[i] = from->symsMapIndex[i];
513 into->symsMapNumEntries[i] = from->symsMapNumEntries[i];
514 into->acts[i] = from->acts[i];
515 into->symsDefined |= (1 << i);
516 darray_init(from->syms[i]);
517 darray_init(from->symsMapIndex[i]);
518 darray_init(from->symsMapNumEntries[i]);
519 darray_init(from->acts[i]);
520 from->numLevels[i] = 0;
521 from->symsDefined &= ~(1 << i);
522 if (!darray_empty(into->syms[i]))
523 into->defined |= _Key_Syms;
524 if (!darray_empty(into->acts[i]))
525 into->defined |= _Key_Acts;
529 if (!darray_empty(into->syms[i]))
530 collide |= _Key_Syms;
531 if (!darray_empty(into->acts[i]))
532 collide |= _Key_Acts;
534 MergeKeyGroups(info, into, from, (unsigned) i);
537 if (from->types[i] != XKB_ATOM_NONE) {
538 if ((into->types[i] != XKB_ATOM_NONE) && report &&
539 (into->types[i] != from->types[i])) {
540 xkb_atom_t use, ignore;
541 collide |= _Key_Types;
542 if (from->merge != MERGE_AUGMENT) {
543 use = from->types[i];
544 ignore = into->types[i];
547 use = into->types[i];
548 ignore = from->types[i];
551 ("Multiple definitions for group %d type of key %s\n",
552 i, longText(into->name));
553 ACTION("Using %s, ignoring %s\n",
554 xkb_atom_text(keymap->ctx, use),
555 xkb_atom_text(keymap->ctx, ignore));
557 if (from->merge != MERGE_AUGMENT ||
558 into->types[i] == XKB_ATOM_NONE) {
559 into->types[i] = from->types[i];
563 if (use_new_field(_Key_Behavior, into->defined, into->file_id,
564 from->defined, from->file_id, from->merge, &collide)) {
565 into->behavior = from->behavior;
566 into->defined |= _Key_Behavior;
568 if (use_new_field(_Key_VModMap, into->defined, into->file_id,
569 from->defined, from->file_id, from->merge, &collide)) {
570 into->vmodmap = from->vmodmap;
571 into->defined |= _Key_VModMap;
573 if (use_new_field(_Key_Repeat, into->defined, into->file_id,
574 from->defined, from->file_id, from->merge, &collide)) {
575 into->repeat = from->repeat;
576 into->defined |= _Key_Repeat;
578 if (use_new_field(_Key_Type_Dflt, into->defined, into->file_id,
579 from->defined, from->file_id, from->merge, &collide)) {
580 into->dfltType = from->dfltType;
581 into->defined |= _Key_Type_Dflt;
583 if (use_new_field(_Key_GroupInfo, into->defined, into->file_id,
584 from->defined, from->file_id, from->merge, &collide)) {
585 into->out_of_range_group_action = from->out_of_range_group_action;
586 into->out_of_range_group_number = from->out_of_range_group_number;
587 into->defined |= _Key_GroupInfo;
590 WARN("Symbol map for key %s redefined\n",
591 longText(into->name));
592 ACTION("Using %s definition for conflicting fields\n",
593 (from->merge == MERGE_AUGMENT ? "first" : "last"));
599 AddKeySymbols(SymbolsInfo *info, KeyInfo *keyi, struct xkb_keymap *keymap)
601 unsigned long real_name;
604 darray_foreach(iter, info->keys)
605 if (iter->name == keyi->name)
606 return MergeKeys(info, keymap, iter, keyi);
608 if (FindKeyNameForAlias(keymap, keyi->name, &real_name))
609 darray_foreach(iter, info->keys)
610 if (iter->name == real_name)
611 return MergeKeys(info, keymap, iter, keyi);
613 darray_resize0(info->keys, darray_size(info->keys) + 1);
614 new = &darray_item(info->keys, darray_size(info->keys) - 1);
615 return CopyKeyInfo(keyi, new, true);
619 AddModMapEntry(SymbolsInfo * info, ModMapEntry * new)
624 clobber = (new->merge != MERGE_AUGMENT);
625 list_foreach(mm, &info->modMaps, entry) {
626 if (new->haveSymbol && mm->haveSymbol
627 && (new->u.keySym == mm->u.keySym)) {
628 unsigned use, ignore;
629 if (mm->modifier != new->modifier) {
632 ignore = mm->modifier;
636 ignore = new->modifier;
639 ("%s added to symbol map for multiple modifiers\n",
640 XkbcKeysymText(new->u.keySym));
641 ACTION("Using %s, ignoring %s.\n",
642 XkbcModIndexText(use),
643 XkbcModIndexText(ignore));
648 if ((!new->haveSymbol) && (!mm->haveSymbol) &&
649 (new->u.keyName == mm->u.keyName)) {
650 unsigned use, ignore;
651 if (mm->modifier != new->modifier) {
654 ignore = mm->modifier;
658 ignore = new->modifier;
660 ERROR("Key %s added to map for multiple modifiers\n",
661 longText(new->u.keyName));
662 ACTION("Using %s, ignoring %s.\n",
663 XkbcModIndexText(use),
664 XkbcModIndexText(ignore));
671 mm = malloc(sizeof(*mm));
673 WSGO("Could not allocate modifier map entry\n");
674 ACTION("Modifier map for %s will be incomplete\n",
675 XkbcModIndexText(new->modifier));
680 list_add(&mm->entry, &info->modMaps);
684 /***====================================================================***/
687 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
688 enum merge_mode merge, struct xkb_keymap *keymap)
692 ModMapEntry *mm, *next;
694 if (from->errorCount > 0) {
695 into->errorCount += from->errorCount;
698 if (into->name == NULL) {
699 into->name = from->name;
702 for (i = 0; i < XkbNumKbdGroups; i++) {
703 if (from->groupNames[i] != XKB_ATOM_NONE) {
704 if ((merge != MERGE_AUGMENT) ||
705 (into->groupNames[i] == XKB_ATOM_NONE))
706 into->groupNames[i] = from->groupNames[i];
710 darray_foreach(keyi, from->keys) {
711 if (merge != MERGE_DEFAULT)
714 if (!AddKeySymbols(into, keyi, keymap))
718 list_foreach_safe(mm, next, &from->modMaps, entry) {
719 if (merge != MERGE_DEFAULT)
721 if (!AddModMapEntry(into, mm))
725 list_init(&from->modMaps);
729 HandleSymbolsFile(XkbFile *file, struct xkb_keymap *keymap,
730 enum merge_mode merge,
734 HandleIncludeSymbols(IncludeStmt *stmt, struct xkb_keymap *keymap,
737 enum merge_mode newMerge;
739 SymbolsInfo included;
743 if ((stmt->file == NULL) && (stmt->map == NULL)) {
746 memset(info, 0, sizeof(SymbolsInfo));
748 else if (ProcessIncludeFile(keymap->ctx, stmt, FILE_TYPE_SYMBOLS, &rtrn,
750 InitSymbolsInfo(&included, keymap, rtrn->id);
751 included.merge = included.dflt.merge = MERGE_OVERRIDE;
753 included.explicit_group = atoi(stmt->modifier) - 1;
755 included.explicit_group = info->explicit_group;
756 HandleSymbolsFile(rtrn, keymap, MERGE_OVERRIDE, &included);
757 if (stmt->stmt != NULL) {
759 included.name = stmt->stmt;
765 info->errorCount += 10;
768 if ((stmt->next != NULL) && (included.errorCount < 1)) {
771 SymbolsInfo next_incl;
773 for (next = stmt->next; next != NULL; next = next->next) {
774 if ((next->file == NULL) && (next->map == NULL)) {
776 MergeIncludedSymbols(&included, info, next->merge, keymap);
777 FreeSymbolsInfo(info);
779 else if (ProcessIncludeFile(keymap->ctx, next, FILE_TYPE_SYMBOLS,
781 InitSymbolsInfo(&next_incl, keymap, rtrn->id);
782 next_incl.merge = next_incl.dflt.merge = MERGE_OVERRIDE;
784 next_incl.explicit_group = atoi(next->modifier) - 1;
786 next_incl.explicit_group = info->explicit_group;
787 HandleSymbolsFile(rtrn, keymap, MERGE_OVERRIDE, &next_incl);
788 MergeIncludedSymbols(&included, &next_incl, op, keymap);
789 FreeSymbolsInfo(&next_incl);
793 info->errorCount += 10;
794 FreeSymbolsInfo(&included);
799 else if (stmt->next) {
800 info->errorCount += included.errorCount;
805 MergeIncludedSymbols(info, &included, newMerge, keymap);
806 FreeSymbolsInfo(&included);
808 return (info->errorCount == 0);
815 GetGroupIndex(KeyInfo *keyi, struct xkb_keymap *keymap,
816 ExprDef *arrayNdx, unsigned what, xkb_group_index_t *ndx_rtrn)
826 if (arrayNdx == NULL) {
830 defined = keyi->symsDefined;
832 defined = keyi->actsDefined;
834 for (i = 0; i < XkbNumKbdGroups; i++) {
835 if ((defined & (1 << i)) == 0) {
840 ERROR("Too many groups of %s for key %s (max %u)\n", name,
841 longText(keyi->name), XkbNumKbdGroups + 1);
842 ACTION("Ignoring %s defined for extra groups\n", name);
845 if (!ExprResolveGroup(keymap->ctx, arrayNdx, &tmp)) {
846 ERROR("Illegal group index for %s of key %s\n", name,
847 longText(keyi->name));
848 ACTION("Definition with non-integer array index ignored\n");
851 *ndx_rtrn = tmp.uval - 1;
856 AddSymbolsToKey(KeyInfo *keyi, struct xkb_keymap *keymap,
857 ExprDef *arrayNdx, ExprDef *value, SymbolsInfo *info)
859 xkb_group_index_t ndx;
860 size_t nSyms, nLevels;
863 if (!GetGroupIndex(keyi, keymap, arrayNdx, SYMBOLS, &ndx))
866 keyi->symsDefined |= (1 << ndx);
869 if (value->op != ExprKeysymList) {
870 ERROR("Expected a list of symbols, found %s\n", exprOpText(value->op));
871 ACTION("Ignoring symbols for group %u of %s\n", ndx + 1,
872 longText(keyi->name));
875 if (!darray_empty(keyi->syms[ndx])) {
876 ERROR("Symbols for key %s, group %u already defined\n",
877 longText(keyi->name), ndx + 1);
878 ACTION("Ignoring duplicate definition\n");
881 nSyms = darray_size(value->value.list.syms);
882 nLevels = darray_size(value->value.list.symsMapIndex);
883 if ((keyi->numLevels[ndx] < nSyms || darray_empty(keyi->syms[ndx])) &&
884 (!ResizeKeyGroup(keyi, ndx, nLevels, nSyms, false))) {
885 WSGO("Could not resize group %u of key %s to contain %zu levels\n",
886 ndx + 1, longText(keyi->name), nSyms);
887 ACTION("Symbols lost\n");
890 keyi->symsDefined |= (1 << ndx);
891 for (i = 0; i < nLevels; i++) {
892 darray_item(keyi->symsMapIndex[ndx], i) =
893 darray_item(value->value.list.symsMapIndex, i);
894 darray_item(keyi->symsMapNumEntries[ndx], i) =
895 darray_item(value->value.list.symsNumEntries, i);
897 for (j = 0; j < darray_item(keyi->symsMapNumEntries[ndx], i); j++) {
898 /* FIXME: What's abort() doing here? */
899 if (darray_item(keyi->symsMapIndex[ndx], i) + j >= nSyms)
901 if (!LookupKeysym(darray_item(value->value.list.syms,
902 darray_item(value->value.list.symsMapIndex,
904 &darray_item(keyi->syms[ndx],
905 darray_item(keyi->symsMapIndex[ndx],
908 "Could not resolve keysym %s for key %s, group %u (%s), level %zu\n",
909 darray_item(value->value.list.syms, i),
910 longText(keyi->name),
912 xkb_atom_text(keymap->ctx, info->groupNames[ndx]), nSyms);
914 darray_item(keyi->syms[ndx],
915 darray_item(keyi->symsMapIndex[ndx],
916 i) + j) = XKB_KEY_NoSymbol;
917 darray_item(keyi->symsMapIndex[ndx], i) = -1;
918 darray_item(keyi->symsMapNumEntries[ndx], i) = 0;
921 if (darray_item(keyi->symsMapNumEntries[ndx], i) == 1 &&
922 darray_item(keyi->syms[ndx],
923 darray_item(keyi->symsMapIndex[ndx],
924 i) + j) == XKB_KEY_NoSymbol) {
925 darray_item(keyi->symsMapIndex[ndx], i) = -1;
926 darray_item(keyi->symsMapNumEntries[ndx], i) = 0;
930 for (j = keyi->numLevels[ndx] - 1;
931 j >= 0 && darray_item(keyi->symsMapNumEntries[ndx], j) == 0; j--)
932 keyi->numLevels[ndx]--;
937 AddActionsToKey(KeyInfo *keyi, struct xkb_keymap *keymap, ExprDef *arrayNdx,
938 ExprDef *value, SymbolsInfo *info)
941 xkb_group_index_t ndx;
944 struct xkb_any_action *toAct;
946 if (!GetGroupIndex(keyi, keymap, arrayNdx, ACTIONS, &ndx))
950 keyi->actsDefined |= (1 << ndx);
953 if (value->op != ExprActionList) {
954 WSGO("Bad expression type (%d) for action list value\n", value->op);
955 ACTION("Ignoring actions for group %u of %s\n", ndx,
956 longText(keyi->name));
959 if (!darray_empty(keyi->acts[ndx])) {
960 WSGO("Actions for key %s, group %u already defined\n",
961 longText(keyi->name), ndx);
964 for (nActs = 0, act = value->value.child; act != NULL; nActs++) {
965 act = (ExprDef *) act->common.next;
968 WSGO("Action list but not actions in AddActionsToKey\n");
971 if ((keyi->numLevels[ndx] < nActs || darray_empty(keyi->acts[ndx])) &&
972 !ResizeKeyGroup(keyi, ndx, nActs, nActs, true)) {
973 WSGO("Could not resize group %u of key %s\n", ndx,
974 longText(keyi->name));
975 ACTION("Actions lost\n");
978 keyi->actsDefined |= (1 << ndx);
980 toAct = (struct xkb_any_action *) darray_mem(keyi->acts[ndx], 0);
981 act = value->value.child;
982 for (i = 0; i < nActs; i++, toAct++) {
983 if (!HandleActionDef(act, keymap, toAct, info->action)) {
984 ERROR("Illegal action definition for %s\n",
985 longText(keyi->name));
986 ACTION("Action for group %u/level %zu ignored\n", ndx + 1, i + 1);
988 act = (ExprDef *) act->common.next;
993 static const LookupEntry lockingEntries[] = {
994 { "true", XkbKB_Lock },
995 { "yes", XkbKB_Lock },
996 { "on", XkbKB_Lock },
997 { "false", XkbKB_Default },
998 { "no", XkbKB_Default },
999 { "off", XkbKB_Default },
1000 { "permanent", XkbKB_Lock | XkbKB_Permanent },
1004 static const LookupEntry repeatEntries[] = {
1005 { "true", RepeatYes },
1006 { "yes", RepeatYes },
1007 { "on", RepeatYes },
1008 { "false", RepeatNo },
1010 { "off", RepeatNo },
1011 { "default", RepeatUndefined },
1016 SetSymbolsField(KeyInfo *keyi, struct xkb_keymap *keymap, char *field,
1017 ExprDef *arrayNdx, ExprDef *value, SymbolsInfo *info)
1022 if (strcasecmp(field, "type") == 0) {
1024 if ((!ExprResolveString(keymap->ctx, value, &tmp))
1025 && (warningLevel > 0)) {
1026 WARN("The type field of a key symbol map must be a string\n");
1027 ACTION("Ignoring illegal type definition\n");
1029 if (arrayNdx == NULL) {
1030 keyi->dfltType = xkb_atom_intern(keymap->ctx, tmp.str);
1031 keyi->defined |= _Key_Type_Dflt;
1033 else if (!ExprResolveGroup(keymap->ctx, arrayNdx, &ndx)) {
1034 ERROR("Illegal group index for type of key %s\n",
1035 longText(keyi->name));
1036 ACTION("Definition with non-integer array index ignored\n");
1041 keyi->types[ndx.uval - 1] = xkb_atom_intern(keymap->ctx, tmp.str);
1042 keyi->typesDefined |= (1 << (ndx.uval - 1));
1046 else if (strcasecmp(field, "symbols") == 0)
1047 return AddSymbolsToKey(keyi, keymap, arrayNdx, value, info);
1048 else if (strcasecmp(field, "actions") == 0)
1049 return AddActionsToKey(keyi, keymap, arrayNdx, value, info);
1050 else if ((strcasecmp(field, "vmods") == 0) ||
1051 (strcasecmp(field, "virtualmods") == 0) ||
1052 (strcasecmp(field, "virtualmodifiers") == 0)) {
1053 ok = ExprResolveVModMask(value, &tmp, keymap);
1055 keyi->vmodmap = (tmp.uval >> 8);
1056 keyi->defined |= _Key_VModMap;
1059 ERROR("Expected a virtual modifier mask, found %s\n",
1060 exprOpText(value->op));
1061 ACTION("Ignoring virtual modifiers definition for key %s\n",
1062 longText(keyi->name));
1065 else if ((strcasecmp(field, "locking") == 0) ||
1066 (strcasecmp(field, "lock") == 0) ||
1067 (strcasecmp(field, "locks") == 0)) {
1068 ok = ExprResolveEnum(keymap->ctx, value, &tmp, lockingEntries);
1070 keyi->behavior.type = tmp.uval;
1071 keyi->defined |= _Key_Behavior;
1073 else if ((strcasecmp(field, "radiogroup") == 0) ||
1074 (strcasecmp(field, "permanentradiogroup") == 0) ||
1075 (strcasecmp(field, "allownone") == 0)) {
1076 ERROR("Radio groups not supported\n");
1077 ACTION("Ignoring radio group specification for key %s\n",
1078 longText(keyi->name));
1081 else if (uStrCasePrefix("overlay", field) ||
1082 uStrCasePrefix("permanentoverlay", field)) {
1083 ERROR("Overlays not supported\n");
1084 ACTION("Ignoring overlay specification for key %s\n",
1085 longText(keyi->name));
1087 else if ((strcasecmp(field, "repeating") == 0) ||
1088 (strcasecmp(field, "repeats") == 0) ||
1089 (strcasecmp(field, "repeat") == 0)) {
1090 ok = ExprResolveEnum(keymap->ctx, value, &tmp, repeatEntries);
1092 ERROR("Illegal repeat setting for %s\n",
1093 longText(keyi->name));
1094 ACTION("Non-boolean repeat setting ignored\n");
1097 keyi->repeat = tmp.uval;
1098 keyi->defined |= _Key_Repeat;
1100 else if ((strcasecmp(field, "groupswrap") == 0) ||
1101 (strcasecmp(field, "wrapgroups") == 0)) {
1102 ok = ExprResolveBoolean(keymap->ctx, value, &tmp);
1104 ERROR("Illegal groupsWrap setting for %s\n",
1105 longText(keyi->name));
1106 ACTION("Non-boolean value ignored\n");
1110 keyi->out_of_range_group_action = XkbWrapIntoRange;
1112 keyi->out_of_range_group_action = XkbClampIntoRange;
1113 keyi->defined |= _Key_GroupInfo;
1115 else if ((strcasecmp(field, "groupsclamp") == 0) ||
1116 (strcasecmp(field, "clampgroups") == 0)) {
1117 ok = ExprResolveBoolean(keymap->ctx, value, &tmp);
1119 ERROR("Illegal groupsClamp setting for %s\n",
1120 longText(keyi->name));
1121 ACTION("Non-boolean value ignored\n");
1125 keyi->out_of_range_group_action = XkbClampIntoRange;
1127 keyi->out_of_range_group_action = XkbWrapIntoRange;
1128 keyi->defined |= _Key_GroupInfo;
1130 else if ((strcasecmp(field, "groupsredirect") == 0) ||
1131 (strcasecmp(field, "redirectgroups") == 0)) {
1132 if (!ExprResolveGroup(keymap->ctx, value, &tmp)) {
1133 ERROR("Illegal group index for redirect of key %s\n",
1134 longText(keyi->name));
1135 ACTION("Definition with non-integer group ignored\n");
1138 keyi->out_of_range_group_action = XkbRedirectIntoRange;
1139 keyi->out_of_range_group_number = tmp.uval - 1;
1140 keyi->defined |= _Key_GroupInfo;
1143 ERROR("Unknown field %s in a symbol interpretation\n", field);
1144 ACTION("Definition ignored\n");
1151 SetGroupName(SymbolsInfo *info, struct xkb_keymap *keymap, ExprDef *arrayNdx,
1154 ExprResult tmp, name;
1156 if ((arrayNdx == NULL) && (warningLevel > 0)) {
1157 WARN("You must specify an index when specifying a group name\n");
1158 ACTION("Group name definition without array subscript ignored\n");
1161 if (!ExprResolveGroup(keymap->ctx, arrayNdx, &tmp)) {
1162 ERROR("Illegal index in group name definition\n");
1163 ACTION("Definition with non-integer array index ignored\n");
1166 if (!ExprResolveString(keymap->ctx, value, &name)) {
1167 ERROR("Group name must be a string\n");
1168 ACTION("Illegal name for group %d ignored\n", tmp.uval);
1171 info->groupNames[tmp.uval - 1 + info->explicit_group] =
1172 xkb_atom_intern(keymap->ctx, name.str);
1179 HandleSymbolsVar(VarDef *stmt, struct xkb_keymap *keymap, SymbolsInfo *info)
1181 ExprResult elem, field;
1185 if (ExprResolveLhs(keymap, stmt->name, &elem, &field, &arrayNdx) == 0)
1186 return 0; /* internal error, already reported */
1187 if (elem.str && (strcasecmp(elem.str, "key") == 0)) {
1188 ret = SetSymbolsField(&info->dflt, keymap, field.str, arrayNdx,
1191 else if ((elem.str == NULL) && ((strcasecmp(field.str, "name") == 0) ||
1192 (strcasecmp(field.str, "groupname") ==
1194 ret = SetGroupName(info, keymap, arrayNdx, stmt->value);
1196 else if ((elem.str == NULL)
1197 && ((strcasecmp(field.str, "groupswrap") == 0) ||
1198 (strcasecmp(field.str, "wrapgroups") == 0))) {
1199 ERROR("Global \"groupswrap\" not supported\n");
1200 ACTION("Ignored\n");
1203 else if ((elem.str == NULL)
1204 && ((strcasecmp(field.str, "groupsclamp") == 0) ||
1205 (strcasecmp(field.str, "clampgroups") == 0))) {
1206 ERROR("Global \"groupsclamp\" not supported\n");
1207 ACTION("Ignored\n");
1210 else if ((elem.str == NULL)
1211 && ((strcasecmp(field.str, "groupsredirect") == 0) ||
1212 (strcasecmp(field.str, "redirectgroups") == 0))) {
1213 ERROR("Global \"groupsredirect\" not supported\n");
1214 ACTION("Ignored\n");
1217 else if ((elem.str == NULL) &&
1218 (strcasecmp(field.str, "allownone") == 0)) {
1219 ERROR("Radio groups not supported\n");
1220 ACTION("Ignoring \"allownone\" specification\n");
1224 ret = SetActionField(keymap, elem.str, field.str, arrayNdx,
1225 stmt->value, &info->action);
1234 HandleSymbolsBody(VarDef *def, struct xkb_keymap *keymap, KeyInfo *keyi,
1238 ExprResult tmp, field;
1241 for (; def != NULL; def = (VarDef *) def->common.next) {
1242 if ((def->name) && (def->name->type == ExprFieldRef)) {
1243 ok = HandleSymbolsVar(def, keymap, info);
1247 if (def->name == NULL) {
1248 if ((def->value == NULL)
1249 || (def->value->op == ExprKeysymList))
1250 field.str = strdup("symbols");
1252 field.str = strdup("actions");
1256 ok = ExprResolveLhs(keymap, def->name, &tmp, &field,
1260 ok = SetSymbolsField(keyi, keymap, field.str, arrayNdx,
1269 SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi)
1271 xkb_group_index_t group = info->explicit_group;
1276 if ((keyi->typesDefined | keyi->symsDefined | keyi->actsDefined) & ~1) {
1277 xkb_group_index_t i;
1278 WARN("For the map %s an explicit group specified\n", info->name);
1279 WARN("but key %s has more than one group defined\n",
1280 longText(keyi->name));
1281 ACTION("All groups except first one will be ignored\n");
1282 for (i = 1; i < XkbNumKbdGroups; i++) {
1283 keyi->numLevels[i] = 0;
1284 darray_free(keyi->syms[i]);
1285 darray_free(keyi->acts[i]);
1289 keyi->typesDefined = keyi->symsDefined = keyi->actsDefined = 1 << group;
1291 keyi->numLevels[group] = keyi->numLevels[0];
1292 keyi->numLevels[0] = 0;
1293 keyi->syms[group] = keyi->syms[0];
1294 darray_init(keyi->syms[0]);
1295 keyi->symsMapIndex[group] = keyi->symsMapIndex[0];
1296 darray_init(keyi->symsMapIndex[0]);
1297 keyi->symsMapNumEntries[group] = keyi->symsMapNumEntries[0];
1298 darray_init(keyi->symsMapNumEntries[0]);
1299 keyi->acts[group] = keyi->acts[0];
1300 darray_init(keyi->acts[0]);
1301 keyi->types[group] = keyi->types[0];
1307 HandleSymbolsDef(SymbolsDef *stmt, struct xkb_keymap *keymap,
1312 InitKeyInfo(&keyi, info->file_id);
1313 CopyKeyInfo(&info->dflt, &keyi, false);
1314 keyi.merge = stmt->merge;
1315 keyi.name = KeyNameToLong(stmt->keyName);
1316 if (!HandleSymbolsBody((VarDef *) stmt->symbols, keymap, &keyi, info)) {
1321 if (!SetExplicitGroup(info, &keyi)) {
1326 if (!AddKeySymbols(info, &keyi, keymap)) {
1334 HandleModMapDef(ModMapDef *def, struct xkb_keymap *keymap, SymbolsInfo *info)
1341 if (!LookupModIndex(keymap->ctx, NULL, def->modifier, TypeInt, &rtrn)) {
1342 ERROR("Illegal modifier map definition\n");
1343 ACTION("Ignoring map for non-modifier \"%s\"\n",
1344 xkb_atom_text(keymap->ctx, def->modifier));
1348 tmp.modifier = rtrn.uval;
1349 for (key = def->keys; key != NULL; key = (ExprDef *) key->common.next) {
1350 if ((key->op == ExprValue) && (key->type == TypeKeyName)) {
1351 tmp.haveSymbol = false;
1352 tmp.u.keyName = KeyNameToLong(key->value.keyName);
1354 else if (ExprResolveKeySym(keymap->ctx, key, &rtrn)) {
1355 tmp.haveSymbol = true;
1356 tmp.u.keySym = rtrn.uval;
1359 ERROR("Modmap entries may contain only key names or keysyms\n");
1360 ACTION("Illegal definition for %s modifier ignored\n",
1361 XkbcModIndexText(tmp.modifier));
1365 ok = AddModMapEntry(info, &tmp) && ok;
1371 HandleSymbolsFile(XkbFile *file, struct xkb_keymap *keymap,
1372 enum merge_mode merge, SymbolsInfo *info)
1377 info->name = uDupString(file->name);
1381 switch (stmt->stmtType) {
1383 if (!HandleIncludeSymbols((IncludeStmt *) stmt, keymap, info))
1386 case StmtSymbolsDef:
1387 if (!HandleSymbolsDef((SymbolsDef *) stmt, keymap, info))
1391 if (!HandleSymbolsVar((VarDef *) stmt, keymap, info))
1395 if (!HandleVModDef((VModDef *) stmt, keymap, merge, &info->vmods))
1399 ERROR("Interpretation files may not include other types\n");
1400 ACTION("Ignoring definition of symbol interpretation\n");
1403 case StmtKeycodeDef:
1404 ERROR("Interpretation files may not include other types\n");
1405 ACTION("Ignoring definition of key name\n");
1409 if (!HandleModMapDef((ModMapDef *) stmt, keymap, info))
1413 WSGO("Unexpected statement type %d in HandleSymbolsFile\n",
1418 if (info->errorCount > 10) {
1420 ERROR("Too many errors\n");
1422 ACTION("Abandoning symbols file \"%s\"\n", file->topName);
1429 * Given a keysym @sym, return a key which generates it, or NULL.
1430 * This is used for example in a modifier map definition, such as:
1431 * modifier_map Lock { Caps_Lock };
1432 * where we want to add the Lock modifier to the modmap of the key
1433 * which matches the keysym Caps_Lock.
1434 * Since there can be many keys which generates the keysym, the key
1435 * is chosen first by lowest group in which the keysym appears, than
1436 * by lowest level and than by lowest key code.
1438 static struct xkb_key *
1439 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
1441 struct xkb_key *key, *ret = NULL;
1442 xkb_group_index_t group, min_group = UINT_MAX;
1443 unsigned int level, min_level = UINT_MAX;
1445 xkb_foreach_key(key, keymap) {
1446 for (group = 0; group < key->num_groups; group++) {
1447 for (level = 0; level < XkbKeyGroupWidth(keymap, key, group);
1449 if (XkbKeyNumSyms(key, group, level) != 1 ||
1450 (XkbKeySymEntry(key, group, level))[0] != sym)
1454 * If the keysym was found in a group or level > 0, we must
1455 * keep looking since we might find a key in which the keysym
1456 * is in a lower group or level.
1458 if (group < min_group ||
1459 (group == min_group && level < min_level)) {
1461 if (group == 0 && level == 0) {
1477 * Find the given name in the keymap->map->types and return its index.
1479 * @param atom The atom to search for.
1480 * @param type_rtrn Set to the index of the name if found.
1482 * @return true if found, false otherwise.
1485 FindNamedType(struct xkb_keymap *keymap, xkb_atom_t atom, unsigned *type_rtrn)
1488 const char *name = xkb_atom_text(keymap->ctx, atom);
1489 struct xkb_key_type *type;
1492 darray_foreach(type, keymap->types) {
1493 if (strcmp(type->name, name) == 0) {
1504 * Assign a type to the given sym and return the Atom for the type assigned.
1507 * - ONE_LEVEL for width 0/1
1508 * - ALPHABETIC for 2 shift levels, with lower/upercase
1509 * - KEYPAD for keypad keys.
1510 * - TWO_LEVEL for other 2 shift level keys.
1511 * and the same for four level keys.
1513 * @param width Number of sysms in syms.
1514 * @param syms The keysyms for the given key (must be size width).
1515 * @param typeNameRtrn Set to the Atom of the type name.
1517 * @returns true if a type could be found, false otherwise.
1519 * FIXME: I need to take the KeyInfo so I can look at symsMapIndex and
1520 * all that fun stuff rather than just assuming there's always one
1524 FindAutomaticType(struct xkb_keymap *keymap, int width,
1525 const xkb_keysym_t *syms, xkb_atom_t *typeNameRtrn,
1529 if ((width == 1) || (width == 0)) {
1530 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ONE_LEVEL");
1533 else if (width == 2) {
1534 if (syms && xkb_keysym_is_lower(syms[0]) &&
1535 xkb_keysym_is_upper(syms[1])) {
1536 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ALPHABETIC");
1538 else if (syms && (xkb_keysym_is_keypad(syms[0]) ||
1539 xkb_keysym_is_keypad(syms[1]))) {
1540 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "KEYPAD");
1544 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "TWO_LEVEL");
1548 else if (width <= 4) {
1549 if (syms && xkb_keysym_is_lower(syms[0]) &&
1550 xkb_keysym_is_upper(syms[1]))
1551 if (xkb_keysym_is_lower(syms[2]) && xkb_keysym_is_upper(syms[3]))
1553 xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_ALPHABETIC");
1555 *typeNameRtrn = xkb_atom_intern(keymap->ctx,
1556 "FOUR_LEVEL_SEMIALPHABETIC");
1558 else if (syms && (xkb_keysym_is_keypad(syms[0]) ||
1559 xkb_keysym_is_keypad(syms[1])))
1560 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_KEYPAD");
1562 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL");
1563 /* XXX: why not set autoType here? */
1565 return ((width >= 0) && (width <= 4));
1569 * Ensure the given KeyInfo is in a coherent state, i.e. no gaps between the
1570 * groups, and reduce to one group if all groups are identical anyway.
1573 PrepareKeyDef(KeyInfo *keyi)
1575 xkb_group_index_t i, lastGroup;
1576 int j, width, defined;
1579 defined = keyi->symsDefined | keyi->actsDefined | keyi->typesDefined;
1580 /* get highest group number */
1581 for (i = XkbNumKbdGroups - 1; i >= 0; i--) {
1582 if (defined & (1 << i))
1590 /* If there are empty groups between non-empty ones fill them with data */
1591 /* from the first group. */
1592 /* We can make a wrong assumption here. But leaving gaps is worse. */
1593 for (i = lastGroup; i > 0; i--) {
1594 if (defined & (1 << i))
1596 width = keyi->numLevels[0];
1597 if (keyi->typesDefined & 1) {
1598 for (j = 0; j < width; j++) {
1599 keyi->types[i] = keyi->types[0];
1601 keyi->typesDefined |= 1 << i;
1603 if ((keyi->actsDefined & 1) && !darray_empty(keyi->acts[0])) {
1604 darray_copy(keyi->acts[i], keyi->acts[0]);
1605 keyi->actsDefined |= 1 << i;
1607 if ((keyi->symsDefined & 1) && !darray_empty(keyi->syms[0])) {
1608 darray_copy(keyi->syms[i], keyi->syms[0]);
1609 darray_copy(keyi->symsMapIndex[i], keyi->symsMapIndex[0]);
1610 darray_copy(keyi->symsMapNumEntries[i],
1611 keyi->symsMapNumEntries[0]);
1612 keyi->symsDefined |= 1 << i;
1615 keyi->numLevels[i] = keyi->numLevels[0];
1618 /* If all groups are completely identical remove them all */
1619 /* exept the first one. */
1621 for (i = lastGroup; i > 0; i--) {
1622 if ((keyi->numLevels[i] != keyi->numLevels[0]) ||
1623 (keyi->types[i] != keyi->types[0])) {
1627 if (!darray_same(keyi->syms[i], keyi->syms[0]) &&
1628 (darray_empty(keyi->syms[i]) || darray_empty(keyi->syms[0]) ||
1629 darray_size(keyi->syms[i]) != darray_size(keyi->syms[0]) ||
1630 memcmp(darray_mem(keyi->syms[i], 0),
1631 darray_mem(keyi->syms[0], 0),
1632 sizeof(xkb_keysym_t) * darray_size(keyi->syms[0])))) {
1636 if (!darray_same(keyi->symsMapIndex[i], keyi->symsMapIndex[0]) &&
1637 (darray_empty(keyi->symsMapIndex[i]) ||
1638 darray_empty(keyi->symsMapIndex[0]) ||
1639 memcmp(darray_mem(keyi->symsMapIndex[i], 0),
1640 darray_mem(keyi->symsMapIndex[0], 0),
1641 keyi->numLevels[0] * sizeof(int)))) {
1645 if (!darray_same(keyi->symsMapNumEntries[i],
1646 keyi->symsMapNumEntries[0]) &&
1647 (darray_empty(keyi->symsMapNumEntries[i]) ||
1648 darray_empty(keyi->symsMapNumEntries[0]) ||
1649 memcmp(darray_mem(keyi->symsMapNumEntries[i], 0),
1650 darray_mem(keyi->symsMapNumEntries[0], 0),
1651 keyi->numLevels[0] * sizeof(size_t)))) {
1655 if (!darray_same(keyi->acts[i], keyi->acts[0]) &&
1656 (darray_empty(keyi->acts[i]) || darray_empty(keyi->acts[0]) ||
1657 memcmp(darray_mem(keyi->acts[i], 0),
1658 darray_mem(keyi->acts[0], 0),
1659 keyi->numLevels[0] * sizeof(union xkb_action)))) {
1665 for (i = lastGroup; i > 0; i--) {
1666 keyi->numLevels[i] = 0;
1667 darray_free(keyi->syms[i]);
1668 darray_free(keyi->symsMapIndex[i]);
1669 darray_free(keyi->symsMapNumEntries[i]);
1670 darray_free(keyi->acts[i]);
1673 keyi->symsDefined &= 1;
1674 keyi->actsDefined &= 1;
1675 keyi->typesDefined &= 1;
1680 * Copy the KeyInfo into the keyboard description.
1682 * This function recurses.
1685 CopySymbolsDef(struct xkb_keymap *keymap, KeyInfo *keyi,
1686 xkb_keycode_t start_from)
1689 struct xkb_key *key;
1690 size_t sizeSyms = 0;
1691 xkb_group_index_t i, nGroups;
1692 unsigned width, tmp;
1693 struct xkb_key_type * type;
1694 bool haveActions, autoType, useAlias;
1695 unsigned types[XkbNumKbdGroups];
1696 union xkb_action *outActs;
1697 unsigned int symIndex = 0;
1699 useAlias = (start_from == 0);
1701 key = FindNamedKey(keymap, keyi->name, useAlias,
1702 CreateKeyNames(keymap), start_from);
1704 if (start_from == 0 && warningLevel >= 5) {
1705 WARN("Key %s not found in keycodes\n", longText(keyi->name));
1706 ACTION("Symbols ignored\n");
1710 kc = XkbKeyGetKeycode(keymap, key);
1712 haveActions = false;
1714 for (i = nGroups = 0; i < XkbNumKbdGroups; i++) {
1715 if (((i + 1) > nGroups)
1716 && (((keyi->symsDefined | keyi->actsDefined) & (1 << i))
1717 || (keyi->typesDefined) & (1 << i)))
1719 if (!darray_empty(keyi->acts[i]))
1722 /* Assign the type to the key, if it is missing. */
1723 if (keyi->types[i] == XKB_ATOM_NONE) {
1724 if (keyi->dfltType != XKB_ATOM_NONE)
1725 keyi->types[i] = keyi->dfltType;
1726 else if (FindAutomaticType(keymap, keyi->numLevels[i],
1727 darray_mem(keyi->syms[i], 0),
1728 &keyi->types[i], &autoType)) { }
1730 if (warningLevel >= 5) {
1731 WARN("No automatic type for %d symbols\n",
1732 keyi->numLevels[i]);
1733 ACTION("Using %s for the %s key (keycode %d)\n",
1734 xkb_atom_text(keymap->ctx, keyi->types[i]),
1735 longText(keyi->name), kc);
1739 if (FindNamedType(keymap, keyi->types[i], &types[i])) {
1740 if (!autoType || keyi->numLevels[i] > 2)
1741 key->explicit |= (1 << i);
1744 if (warningLevel >= 3) {
1745 WARN("Type \"%s\" is not defined\n",
1746 xkb_atom_text(keymap->ctx, keyi->types[i]));
1747 ACTION("Using TWO_LEVEL for the %s key (keycode %d)\n",
1748 longText(keyi->name), kc);
1750 types[i] = XkbTwoLevelIndex;
1752 /* if the type specifies fewer levels than the key has, shrink the key */
1753 type = &darray_item(keymap->types, types[i]);
1754 if (type->num_levels < keyi->numLevels[i]) {
1755 if (warningLevel > 0) {
1756 WARN("Type \"%s\" has %d levels, but %s has %d symbols\n",
1757 type->name, type->num_levels,
1758 xkb_atom_text(keymap->ctx, keyi->name), keyi->numLevels[i]);
1759 ACTION("Ignoring extra symbols\n");
1761 keyi->numLevels[i] = type->num_levels;
1763 if (keyi->numLevels[i] > width)
1764 width = keyi->numLevels[i];
1765 if (type->num_levels > width)
1766 width = type->num_levels;
1767 sizeSyms += darray_size(keyi->syms[i]);
1770 darray_resize0(key->syms, sizeSyms);
1773 outActs = XkbcResizeKeyActions(keymap, key, width * nGroups);
1774 if (outActs == NULL) {
1775 WSGO("Could not enlarge actions for %s (key %d)\n",
1776 longText(keyi->name), kc);
1779 key->explicit |= XkbExplicitInterpretMask;
1784 key->num_groups = nGroups;
1785 if (keyi->defined & _Key_GroupInfo) {
1786 key->out_of_range_group_number = keyi->out_of_range_group_number;
1787 key->out_of_range_group_action = keyi->out_of_range_group_action;
1790 key->sym_index = calloc(nGroups * width, sizeof(*key->sym_index));
1791 key->num_syms = calloc(nGroups * width, sizeof(*key->num_syms));
1793 for (i = 0; i < nGroups; i++) {
1794 /* assign kt_index[i] to the index of the type in map->types.
1795 * kt_index[i] may have been set by a previous run (if we have two
1796 * layouts specified). Let's not overwrite it with the ONE_LEVEL
1797 * default group if we dont even have keys for this group anyway.
1799 * FIXME: There should be a better fix for this.
1801 if (keyi->numLevels[i])
1802 key->kt_index[i] = types[i];
1803 if (!darray_empty(keyi->syms[i])) {
1804 /* fill key to "width" symbols*/
1805 for (tmp = 0; tmp < width; tmp++) {
1806 if (tmp < keyi->numLevels[i] &&
1807 darray_item(keyi->symsMapNumEntries[i], tmp) != 0) {
1808 memcpy(darray_mem(key->syms, symIndex),
1809 darray_mem(keyi->syms[i],
1810 darray_item(keyi->symsMapIndex[i], tmp)),
1811 darray_item(keyi->symsMapNumEntries[i],
1812 tmp) * sizeof(xkb_keysym_t));
1813 key->sym_index[(i * width) + tmp] = symIndex;
1814 key->num_syms[(i * width) + tmp] =
1815 darray_item(keyi->symsMapNumEntries[i], tmp);
1816 symIndex += key->num_syms[(i * width) + tmp];
1819 key->sym_index[(i * width) + tmp] = -1;
1820 key->num_syms[(i * width) + tmp] = 0;
1822 if (outActs != NULL && !darray_empty(keyi->acts[i])) {
1823 if (tmp < keyi->numLevels[i])
1824 outActs[tmp] = darray_item(keyi->acts[i], tmp);
1826 outActs[tmp].type = XkbSA_NoAction;
1831 switch (keyi->behavior.type & XkbKB_OpMask) {
1836 key->behavior = keyi->behavior;
1837 key->explicit |= XkbExplicitBehaviorMask;
1840 if (keyi->defined & _Key_VModMap) {
1841 key->vmodmap = keyi->vmodmap;
1842 key->explicit |= XkbExplicitVModMapMask;
1844 if (keyi->repeat != RepeatUndefined) {
1845 key->repeats = keyi->repeat == RepeatYes;
1846 key->explicit |= XkbExplicitAutoRepeatMask;
1849 /* do the same thing for the next key */
1850 CopySymbolsDef(keymap, keyi, kc + 1);
1855 CopyModMapDef(struct xkb_keymap *keymap, ModMapEntry *entry)
1857 struct xkb_key *key;
1859 if (!entry->haveSymbol) {
1860 key = FindNamedKey(keymap, entry->u.keyName, true,
1861 CreateKeyNames(keymap), 0);
1863 if (warningLevel >= 5) {
1864 WARN("Key %s not found in keycodes\n",
1865 longText(entry->u.keyName));
1866 ACTION("Modifier map entry for %s not updated\n",
1867 XkbcModIndexText(entry->modifier));
1873 key = FindKeyForSymbol(keymap, entry->u.keySym);
1875 if (warningLevel > 5) {
1876 WARN("Key \"%s\" not found in symbol map\n",
1877 XkbcKeysymText(entry->u.keySym));
1878 ACTION("Modifier map entry for %s not updated\n",
1879 XkbcModIndexText(entry->modifier));
1885 key->modmap |= (1 << entry->modifier);
1890 * Handle the xkb_symbols section of an xkb file.
1892 * @param file The parsed xkb_symbols section of the xkb file.
1893 * @param keymap Handle to the keyboard description to store the symbols in.
1894 * @param merge Merge strategy (e.g. MERGE_OVERRIDE).
1897 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1898 enum merge_mode merge)
1900 xkb_group_index_t i;
1901 struct xkb_key *key;
1906 InitSymbolsInfo(&info, keymap, file->id);
1907 info.dflt.merge = merge;
1909 HandleSymbolsFile(file, keymap, merge, &info);
1911 if (darray_empty(info.keys))
1914 if (info.errorCount != 0)
1917 darray_resize0(keymap->acts, darray_size(keymap->acts) + 32 + 1);
1920 keymap->symbols_section_name = strdup(info.name);
1922 for (i = 0; i < XkbNumKbdGroups; i++) {
1923 if (info.groupNames[i] != XKB_ATOM_NONE) {
1924 free(keymap->group_names[i]);
1925 keymap->group_names[i] = xkb_atom_strdup(keymap->ctx,
1926 info.groupNames[i]);
1931 darray_foreach(keyi, info.keys)
1932 PrepareKeyDef(keyi);
1935 darray_foreach(keyi, info.keys)
1936 if (!CopySymbolsDef(keymap, keyi, 0))
1939 if (warningLevel > 3) {
1940 xkb_foreach_key(key, keymap) {
1941 if (key->name[0] == '\0')
1944 if (key->num_groups < 1)
1945 WARN("No symbols defined for <%.4s> (keycode %d)\n",
1946 key->name, XkbKeyGetKeycode(keymap, key));
1950 list_foreach(mm, &info.modMaps, entry)
1951 if (!CopyModMapDef(keymap, mm))
1954 FreeSymbolsInfo(&info);
1958 FreeSymbolsInfo(&info);