1 /************************************************************
2 * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
4 * Permission to use, copy, modify, and distribute this
5 * software and its documentation for any purpose and without
6 * fee is hereby granted, provided that the above copyright
7 * notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting
9 * documentation, and that the name of Silicon Graphics not be
10 * used in advertising or publicity pertaining to distribution
11 * of the software without specific prior written permission.
12 * Silicon Graphics makes no representation about the suitability
13 * of this software for any purpose. It is provided "as is"
14 * without any express or implied warranty.
16 * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
23 * THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 ********************************************************/
27 #include "xkbcomp-priv.h"
35 /* Needed to work with the typechecker. */
36 typedef darray(xkb_keysym_t) darray_xkb_keysym_t;
37 typedef darray(union xkb_action) darray_xkb_action;
42 KEY_REPEAT_UNDEFINED = -1
46 KEY_FIELD_SYMS = (1 << 0),
47 KEY_FIELD_ACTS = (1 << 1),
48 KEY_FIELD_REPEAT = (1 << 2),
49 KEY_FIELD_TYPE_DFLT = (1 << 3),
50 KEY_FIELD_TYPES = (1 << 4),
51 KEY_FIELD_GROUPINFO = (1 << 5),
52 KEY_FIELD_VMODMAP = (1 << 6),
55 typedef struct _KeyInfo {
56 enum key_field defined;
58 enum merge_mode merge;
60 unsigned long name; /* the 4 chars of the key name, as long */
61 unsigned char typesDefined;
62 unsigned char symsDefined;
63 unsigned char actsDefined;
64 xkb_level_index_t numLevels[XkbNumKbdGroups];
66 /* syms[group] -> Single array for all the keysyms in the group. */
67 darray_xkb_keysym_t syms[XkbNumKbdGroups];
69 * symsMapIndex[group][level] -> The index from which the syms for
70 * the level begin in the syms[group] array. Remember each keycode
71 * can have multiple keysyms in each level (that is, each key press
72 * can result in multiple keysyms).
74 darray(int) symsMapIndex[XkbNumKbdGroups];
76 * symsMapNumEntries[group][level] -> How many syms are in
77 * syms[group][symsMapIndex[group][level]].
79 darray(size_t) symsMapNumEntries[XkbNumKbdGroups];
81 darray_xkb_action acts[XkbNumKbdGroups];
83 xkb_atom_t types[XkbNumKbdGroups];
84 enum key_repeat repeat;
85 xkb_mod_mask_t vmodmap;
88 uint8_t out_of_range_group_action;
89 xkb_group_index_t out_of_range_group_number;
93 * Init the given key info to sane values.
96 InitKeyInfo(KeyInfo *keyi, unsigned file_id)
99 static const char dflt[4] = "*";
102 keyi->file_id = file_id;
103 keyi->merge = MERGE_OVERRIDE;
104 keyi->name = KeyNameToLong(dflt);
105 keyi->typesDefined = keyi->symsDefined = keyi->actsDefined = 0;
107 for (i = 0; i < XkbNumKbdGroups; i++) {
108 keyi->numLevels[i] = 0;
109 keyi->types[i] = XKB_ATOM_NONE;
110 darray_init(keyi->syms[i]);
111 darray_init(keyi->symsMapIndex[i]);
112 darray_init(keyi->symsMapNumEntries[i]);
113 darray_init(keyi->acts[i]);
116 keyi->dfltType = XKB_ATOM_NONE;
118 keyi->repeat = KEY_REPEAT_UNDEFINED;
119 keyi->out_of_range_group_action = 0;
120 keyi->out_of_range_group_number = 0;
124 FreeKeyInfo(KeyInfo *keyi)
128 for (i = 0; i < XkbNumKbdGroups; i++) {
129 darray_free(keyi->syms[i]);
130 darray_free(keyi->symsMapIndex[i]);
131 darray_free(keyi->symsMapNumEntries[i]);
132 darray_free(keyi->acts[i]);
137 * Copy old into new, optionally reset old to 0.
138 * If old is reset, new simply re-uses old's memory. Otherwise, the memory is
139 * newly allocated and new points to the new memory areas.
142 CopyKeyInfo(KeyInfo * old, KeyInfo * new, bool clearOld)
149 for (i = 0; i < XkbNumKbdGroups; i++) {
150 old->numLevels[i] = 0;
151 darray_init(old->symsMapIndex[i]);
152 darray_init(old->symsMapNumEntries[i]);
153 darray_init(old->syms[i]);
154 darray_init(old->acts[i]);
158 for (i = 0; i < XkbNumKbdGroups; i++) {
159 darray_copy(new->syms[i], old->syms[i]);
160 darray_copy(new->symsMapIndex[i], old->symsMapIndex[i]);
161 darray_copy(new->symsMapNumEntries[i], old->symsMapNumEntries[i]);
162 darray_copy(new->acts[i], old->acts[i]);
169 /***====================================================================***/
171 typedef struct _ModMapEntry {
173 enum merge_mode merge;
177 unsigned long keyName;
182 typedef struct _SymbolsInfo {
183 char *name; /* e.g. pc+us+inet(evdev) */
186 enum merge_mode merge;
187 xkb_group_index_t explicit_group;
188 darray(KeyInfo) keys;
192 xkb_atom_t groupNames[XkbNumKbdGroups];
196 struct xkb_keymap *keymap;
200 InitSymbolsInfo(SymbolsInfo * info, struct xkb_keymap *keymap,
206 info->explicit_group = 0;
207 info->errorCount = 0;
208 info->file_id = file_id;
209 info->merge = MERGE_OVERRIDE;
210 darray_init(info->keys);
211 darray_growalloc(info->keys, 110);
212 list_init(&info->modMaps);
213 for (i = 0; i < XkbNumKbdGroups; i++)
214 info->groupNames[i] = XKB_ATOM_NONE;
215 InitKeyInfo(&info->dflt, file_id);
216 InitVModInfo(&info->vmods, keymap);
218 info->keymap = keymap;
222 FreeSymbolsInfo(SymbolsInfo * info)
225 ModMapEntry *mm, *next;
228 darray_foreach(keyi, info->keys) {
231 darray_free(info->keys);
232 list_foreach_safe(mm, next, &info->modMaps, entry)
234 memset(info, 0, sizeof(SymbolsInfo));
238 ResizeKeyGroup(KeyInfo *keyi, xkb_group_index_t group,
239 xkb_level_index_t numLevels, unsigned sizeSyms,
244 if (darray_size(keyi->syms[group]) < sizeSyms)
245 darray_resize0(keyi->syms[group], sizeSyms);
247 if (darray_empty(keyi->symsMapIndex[group]) ||
248 keyi->numLevels[group] < numLevels) {
249 darray_resize(keyi->symsMapIndex[group], numLevels);
250 for (i = keyi->numLevels[group]; i < numLevels; i++)
251 darray_item(keyi->symsMapIndex[group], i) = -1;
254 if (darray_empty(keyi->symsMapNumEntries[group]) ||
255 keyi->numLevels[group] < numLevels)
256 darray_resize0(keyi->symsMapNumEntries[group], numLevels);
258 if ((forceActions && (keyi->numLevels[group] < numLevels ||
259 darray_empty(keyi->acts[group]))) ||
260 (keyi->numLevels[group] < numLevels && !darray_empty(keyi->acts[group])))
261 darray_resize0(keyi->acts[group], numLevels);
263 if (keyi->numLevels[group] < numLevels)
264 keyi->numLevels[group] = numLevels;
269 enum key_group_selector {
276 MergeKeyGroups(SymbolsInfo * info,
277 KeyInfo * into, KeyInfo * from, xkb_group_index_t group)
279 darray_xkb_keysym_t resultSyms;
280 enum key_group_selector using = NONE;
281 darray_xkb_action resultActs;
282 xkb_level_index_t resultWidth;
283 unsigned int resultSize = 0;
286 bool report, clobber;
287 int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
289 clobber = (from->merge != MERGE_AUGMENT);
291 report = (verbosity > 9) ||
292 (into->file_id == from->file_id && verbosity > 0);
294 darray_init(resultSyms);
296 if (into->numLevels[group] >= from->numLevels[group]) {
297 resultActs = into->acts[group];
298 resultWidth = into->numLevels[group];
301 resultActs = from->acts[group];
302 resultWidth = from->numLevels[group];
303 darray_resize(into->symsMapIndex[group],
304 from->numLevels[group]);
305 darray_resize0(into->symsMapNumEntries[group],
306 from->numLevels[group]);
308 for (i = into->numLevels[group]; i < from->numLevels[group]; i++)
309 darray_item(into->symsMapIndex[group], i) = -1;
312 if (darray_empty(resultActs) && (!darray_empty(into->acts[group]) ||
313 !darray_empty(from->acts[group]))) {
314 darray_resize0(resultActs, resultWidth);
315 for (i = 0; i < resultWidth; i++) {
316 union xkb_action *fromAct = NULL, *toAct = NULL;
318 if (!darray_empty(from->acts[group]))
319 fromAct = &darray_item(from->acts[group], i);
321 if (!darray_empty(into->acts[group]))
322 toAct = &darray_item(into->acts[group], i);
324 if (((fromAct == NULL) || (fromAct->type == XkbSA_NoAction))
325 && (toAct != NULL)) {
326 darray_item(resultActs, i) = *toAct;
328 else if (((toAct == NULL) || (toAct->type == XkbSA_NoAction))
329 && (fromAct != NULL)) {
330 darray_item(resultActs, i) = *fromAct;
333 union xkb_action *use, *ignore;
343 log_warn(info->keymap->ctx,
344 "Multiple actions for level %d/group %u on key %s; "
345 "Using %s, ignoring %s\n",
346 i + 1, group + 1, LongKeyNameText(into->name),
347 ActionTypeText(use->type),
348 ActionTypeText(ignore->type));
350 darray_item(resultActs, i) = *use;
355 for (i = 0; i < resultWidth; i++) {
356 unsigned int fromSize = 0;
359 if (!darray_empty(from->symsMapNumEntries[group]) &&
360 i < from->numLevels[group])
361 fromSize = darray_item(from->symsMapNumEntries[group], i);
363 if (!darray_empty(into->symsMapNumEntries[group]) &&
364 i < into->numLevels[group])
365 toSize = darray_item(into->symsMapNumEntries[group], i);
368 resultSize += toSize;
371 else if (toSize == 0 || clobber) {
372 resultSize += fromSize;
376 resultSize += toSize;
385 resultSyms = from->syms[group];
386 darray_free(into->symsMapNumEntries[group]);
387 darray_free(into->symsMapIndex[group]);
388 into->symsMapNumEntries[group] = from->symsMapNumEntries[group];
389 into->symsMapIndex[group] = from->symsMapIndex[group];
390 darray_init(from->symsMapNumEntries[group]);
391 darray_init(from->symsMapIndex[group]);
394 else if (using == TO) {
395 resultSyms = into->syms[group];
399 darray_resize0(resultSyms, resultSize);
401 for (i = 0; i < resultWidth; i++) {
402 enum key_group_selector use = NONE;
403 unsigned int fromSize = 0;
404 unsigned int toSize = 0;
406 if (i < from->numLevels[group])
407 fromSize = darray_item(from->symsMapNumEntries[group], i);
409 if (i < into->numLevels[group])
410 toSize = darray_item(into->symsMapNumEntries[group], i);
412 if (fromSize == 0 && toSize == 0) {
413 darray_item(into->symsMapIndex[group], i) = -1;
414 darray_item(into->symsMapNumEntries[group], i) = 0;
420 else if (toSize == 0 || clobber)
425 if (toSize && fromSize && report) {
426 log_info(info->keymap->ctx,
427 "Multiple symbols for group %u, level %d on key %s; "
428 "Using %s, ignoring %s\n",
429 group + 1, i + 1, LongKeyNameText(into->name),
430 (use == FROM ? "from" : "to"),
431 (use == FROM ? "to" : "from"));
435 memcpy(darray_mem(resultSyms, cur_idx),
436 darray_mem(from->syms[group],
437 darray_item(from->symsMapIndex[group], i)),
438 darray_item(from->symsMapNumEntries[group],
439 i) * sizeof(xkb_keysym_t));
440 darray_item(into->symsMapIndex[group], i) = cur_idx;
441 darray_item(into->symsMapNumEntries[group], i) =
442 darray_item(from->symsMapNumEntries[group], i);
445 memcpy(darray_mem(resultSyms, cur_idx),
446 darray_mem(into->syms[group],
447 darray_item(into->symsMapIndex[group], i)),
448 darray_item(into->symsMapNumEntries[group],
449 i) * sizeof(xkb_keysym_t));
450 darray_item(into->symsMapIndex[group], i) = cur_idx;
452 cur_idx += darray_item(into->symsMapNumEntries[group], i);
456 if (!darray_same(resultActs, into->acts[group]))
457 darray_free(into->acts[group]);
458 if (!darray_same(resultActs, from->acts[group]))
459 darray_free(from->acts[group]);
460 into->numLevels[group] = resultWidth;
461 if (!darray_same(resultSyms, into->syms[group]))
462 darray_free(into->syms[group]);
463 into->syms[group] = resultSyms;
464 if (!darray_same(resultSyms, from->syms[group]))
465 darray_free(from->syms[group]);
466 darray_init(from->syms[group]);
467 darray_free(from->symsMapIndex[group]);
468 darray_free(from->symsMapNumEntries[group]);
469 into->acts[group] = resultActs;
470 darray_init(from->acts[group]);
471 if (!darray_empty(into->syms[group]))
472 into->symsDefined |= (1 << group);
473 from->symsDefined &= ~(1 << group);
474 into->actsDefined |= (1 << group);
475 from->actsDefined &= ~(1 << group);
481 UseNewKeyField(enum key_field field, KeyInfo *old, KeyInfo *new,
482 int verbosity, enum key_field *collide)
484 if (!(old->defined & field))
487 if (new->defined & field) {
488 if ((old->file_id == new->file_id && verbosity > 0) || verbosity > 9)
491 if (new->merge != MERGE_AUGMENT)
500 MergeKeys(SymbolsInfo *info, KeyInfo *into, KeyInfo *from)
503 enum key_field collide = 0;
505 int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
507 if (from->merge == MERGE_REPLACE) {
508 for (i = 0; i < XkbNumKbdGroups; i++) {
509 if (into->numLevels[i] != 0) {
510 darray_free(into->syms[i]);
511 darray_free(into->acts[i]);
515 memset(from, 0, sizeof(KeyInfo));
519 report = (verbosity > 9 ||
520 (into->file_id == from->file_id && verbosity > 0));
522 for (i = 0; i < XkbNumKbdGroups; i++) {
523 if (from->numLevels[i] > 0) {
524 if (into->numLevels[i] == 0) {
525 into->numLevels[i] = from->numLevels[i];
526 into->syms[i] = from->syms[i];
527 into->symsMapIndex[i] = from->symsMapIndex[i];
528 into->symsMapNumEntries[i] = from->symsMapNumEntries[i];
529 into->acts[i] = from->acts[i];
530 into->symsDefined |= (1 << i);
531 darray_init(from->syms[i]);
532 darray_init(from->symsMapIndex[i]);
533 darray_init(from->symsMapNumEntries[i]);
534 darray_init(from->acts[i]);
535 from->numLevels[i] = 0;
536 from->symsDefined &= ~(1 << i);
537 if (!darray_empty(into->syms[i]))
538 into->defined |= KEY_FIELD_SYMS;
539 if (!darray_empty(into->acts[i]))
540 into->defined |= KEY_FIELD_ACTS;
544 if (!darray_empty(into->syms[i]))
545 collide |= KEY_FIELD_SYMS;
546 if (!darray_empty(into->acts[i]))
547 collide |= KEY_FIELD_ACTS;
549 MergeKeyGroups(info, into, from, (unsigned) i);
552 if (from->types[i] != XKB_ATOM_NONE) {
553 if ((into->types[i] != XKB_ATOM_NONE) && report &&
554 (into->types[i] != from->types[i])) {
555 xkb_atom_t use, ignore;
556 collide |= KEY_FIELD_TYPES;
557 if (from->merge != MERGE_AUGMENT) {
558 use = from->types[i];
559 ignore = into->types[i];
562 use = into->types[i];
563 ignore = from->types[i];
566 log_warn(info->keymap->ctx,
567 "Multiple definitions for group %d type of key %s; "
568 "Using %s, ignoring %s\n",
569 i, LongKeyNameText(into->name),
570 xkb_atom_text(info->keymap->ctx, use),
571 xkb_atom_text(info->keymap->ctx, ignore));
574 if (from->merge != MERGE_AUGMENT ||
575 into->types[i] == XKB_ATOM_NONE) {
576 into->types[i] = from->types[i];
581 if (UseNewKeyField(KEY_FIELD_VMODMAP, into, from, verbosity, &collide)) {
582 into->vmodmap = from->vmodmap;
583 into->defined |= KEY_FIELD_VMODMAP;
585 if (UseNewKeyField(KEY_FIELD_REPEAT, into, from, verbosity, &collide)) {
586 into->repeat = from->repeat;
587 into->defined |= KEY_FIELD_REPEAT;
589 if (UseNewKeyField(KEY_FIELD_TYPE_DFLT, into, from, verbosity, &collide)) {
590 into->dfltType = from->dfltType;
591 into->defined |= KEY_FIELD_TYPE_DFLT;
593 if (UseNewKeyField(KEY_FIELD_GROUPINFO, into, from, verbosity, &collide)) {
594 into->out_of_range_group_action = from->out_of_range_group_action;
595 into->out_of_range_group_number = from->out_of_range_group_number;
596 into->defined |= KEY_FIELD_GROUPINFO;
600 log_warn(info->keymap->ctx,
601 "Symbol map for key %s redefined; "
602 "Using %s definition for conflicting fields\n",
603 LongKeyNameText(into->name),
604 (from->merge == MERGE_AUGMENT ? "first" : "last"));
610 AddKeySymbols(SymbolsInfo *info, KeyInfo *keyi)
612 unsigned long real_name;
615 darray_foreach(iter, info->keys)
616 if (iter->name == keyi->name)
617 return MergeKeys(info, iter, keyi);
619 if (FindKeyNameForAlias(info->keymap, keyi->name, &real_name))
620 darray_foreach(iter, info->keys)
621 if (iter->name == real_name)
622 return MergeKeys(info, iter, keyi);
624 darray_resize0(info->keys, darray_size(info->keys) + 1);
625 new = &darray_item(info->keys, darray_size(info->keys) - 1);
626 return CopyKeyInfo(keyi, new, true);
630 AddModMapEntry(SymbolsInfo * info, ModMapEntry * new)
635 clobber = (new->merge != MERGE_AUGMENT);
636 list_foreach(mm, &info->modMaps, entry) {
637 if (new->haveSymbol && mm->haveSymbol
638 && (new->u.keySym == mm->u.keySym)) {
639 unsigned use, ignore;
640 if (mm->modifier != new->modifier) {
643 ignore = mm->modifier;
647 ignore = new->modifier;
649 log_err(info->keymap->ctx,
650 "%s added to symbol map for multiple modifiers; "
651 "Using %s, ignoring %s.\n",
652 KeysymText(new->u.keySym), ModIndexText(use),
653 ModIndexText(ignore));
658 if ((!new->haveSymbol) && (!mm->haveSymbol) &&
659 (new->u.keyName == mm->u.keyName)) {
660 unsigned use, ignore;
661 if (mm->modifier != new->modifier) {
664 ignore = mm->modifier;
668 ignore = new->modifier;
670 log_err(info->keymap->ctx,
671 "Key %s added to map for multiple modifiers; "
672 "Using %s, ignoring %s.\n",
673 LongKeyNameText(new->u.keyName), ModIndexText(use),
674 ModIndexText(ignore));
681 mm = malloc(sizeof(*mm));
683 log_wsgo(info->keymap->ctx,
684 "Could not allocate modifier map entry; "
685 "Modifier map for %s will be incomplete\n",
686 ModIndexText(new->modifier));
691 list_add(&mm->entry, &info->modMaps);
695 /***====================================================================***/
698 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
699 enum merge_mode merge)
703 ModMapEntry *mm, *next;
705 if (from->errorCount > 0) {
706 into->errorCount += from->errorCount;
709 if (into->name == NULL) {
710 into->name = from->name;
713 for (i = 0; i < XkbNumKbdGroups; i++) {
714 if (from->groupNames[i] != XKB_ATOM_NONE) {
715 if ((merge != MERGE_AUGMENT) ||
716 (into->groupNames[i] == XKB_ATOM_NONE))
717 into->groupNames[i] = from->groupNames[i];
721 darray_foreach(keyi, from->keys) {
722 if (merge != MERGE_DEFAULT)
725 if (!AddKeySymbols(into, keyi))
729 list_foreach_safe(mm, next, &from->modMaps, entry) {
730 if (merge != MERGE_DEFAULT)
732 if (!AddModMapEntry(into, mm))
736 list_init(&from->modMaps);
740 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge);
743 HandleIncludeSymbols(SymbolsInfo *info, IncludeStmt *stmt)
745 enum merge_mode merge = MERGE_DEFAULT;
747 SymbolsInfo included, next_incl;
749 InitSymbolsInfo(&included, info->keymap, info->file_id);
752 included.name = stmt->stmt;
756 for (; stmt; stmt = stmt->next_incl) {
757 if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_SYMBOLS,
759 info->errorCount += 10;
760 FreeSymbolsInfo(&included);
764 InitSymbolsInfo(&next_incl, info->keymap, rtrn->id);
765 next_incl.merge = next_incl.dflt.merge = MERGE_OVERRIDE;
767 next_incl.explicit_group = atoi(stmt->modifier) - 1;
769 next_incl.explicit_group = info->explicit_group;
771 HandleSymbolsFile(&next_incl, rtrn, MERGE_OVERRIDE);
773 MergeIncludedSymbols(&included, &next_incl, merge);
775 FreeSymbolsInfo(&next_incl);
779 MergeIncludedSymbols(info, &included, merge);
780 FreeSymbolsInfo(&included);
782 return (info->errorCount == 0);
789 GetGroupIndex(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
790 unsigned what, xkb_group_index_t *ndx_rtrn)
799 if (arrayNdx == NULL) {
803 defined = keyi->symsDefined;
805 defined = keyi->actsDefined;
807 for (i = 0; i < XkbNumKbdGroups; i++) {
808 if ((defined & (1 << i)) == 0) {
814 log_err(info->keymap->ctx,
815 "Too many groups of %s for key %s (max %u); "
816 "Ignoring %s defined for extra groups\n",
817 name, LongKeyNameText(keyi->name), XkbNumKbdGroups + 1, name);
821 if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, ndx_rtrn)) {
822 log_err(info->keymap->ctx,
823 "Illegal group index for %s of key %s\n"
824 "Definition with non-integer array index ignored\n",
825 name, LongKeyNameText(keyi->name));
834 LookupKeysym(const char *str, xkb_keysym_t *sym_rtrn)
838 if (!str || istreq(str, "any") || istreq(str, "nosymbol")) {
839 *sym_rtrn = XKB_KEY_NoSymbol;
843 if (istreq(str, "none") || istreq(str, "voidsymbol")) {
844 *sym_rtrn = XKB_KEY_VoidSymbol;
848 sym = xkb_keysym_from_name(str);
849 if (sym != XKB_KEY_NoSymbol) {
858 AddSymbolsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
861 xkb_group_index_t ndx;
863 xkb_level_index_t nLevels;
867 if (!GetGroupIndex(info, keyi, arrayNdx, SYMBOLS, &ndx))
870 keyi->symsDefined |= (1 << ndx);
873 if (value->op != EXPR_KEYSYM_LIST) {
874 log_err(info->keymap->ctx,
875 "Expected a list of symbols, found %s; "
876 "Ignoring symbols for group %u of %s\n",
877 expr_op_type_to_string(value->op), ndx + 1,
878 LongKeyNameText(keyi->name));
881 if (!darray_empty(keyi->syms[ndx])) {
882 log_err(info->keymap->ctx,
883 "Symbols for key %s, group %u already defined; "
884 "Ignoring duplicate definition\n",
885 LongKeyNameText(keyi->name), ndx + 1);
888 nSyms = darray_size(value->value.list.syms);
889 nLevels = darray_size(value->value.list.symsMapIndex);
890 if ((keyi->numLevels[ndx] < nSyms || darray_empty(keyi->syms[ndx])) &&
891 (!ResizeKeyGroup(keyi, ndx, nLevels, nSyms, false))) {
892 log_wsgo(info->keymap->ctx,
893 "Could not resize group %u of key %s to contain %zu levels; "
895 ndx + 1, LongKeyNameText(keyi->name), nSyms);
898 keyi->symsDefined |= (1 << ndx);
899 for (i = 0; i < nLevels; i++) {
900 darray_item(keyi->symsMapIndex[ndx], i) =
901 darray_item(value->value.list.symsMapIndex, i);
902 darray_item(keyi->symsMapNumEntries[ndx], i) =
903 darray_item(value->value.list.symsNumEntries, i);
905 for (j = 0; j < darray_item(keyi->symsMapNumEntries[ndx], i); j++) {
906 /* FIXME: What's abort() doing here? */
907 if (darray_item(keyi->symsMapIndex[ndx], i) + j >= nSyms)
909 if (!LookupKeysym(darray_item(value->value.list.syms,
910 darray_item(value->value.list.symsMapIndex,
912 &darray_item(keyi->syms[ndx],
913 darray_item(keyi->symsMapIndex[ndx],
915 log_warn(info->keymap->ctx,
916 "Could not resolve keysym %s for key %s, group %u (%s), level %zu\n",
917 darray_item(value->value.list.syms, i),
918 LongKeyNameText(keyi->name),
920 xkb_atom_text(info->keymap->ctx,
921 info->groupNames[ndx]),
924 darray_item(keyi->syms[ndx],
925 darray_item(keyi->symsMapIndex[ndx],
926 i) + j) = XKB_KEY_NoSymbol;
927 darray_item(keyi->symsMapIndex[ndx], i) = -1;
928 darray_item(keyi->symsMapNumEntries[ndx], i) = 0;
931 if (darray_item(keyi->symsMapNumEntries[ndx], i) == 1 &&
932 darray_item(keyi->syms[ndx],
933 darray_item(keyi->symsMapIndex[ndx],
934 i) + j) == XKB_KEY_NoSymbol) {
935 darray_item(keyi->symsMapIndex[ndx], i) = -1;
936 darray_item(keyi->symsMapNumEntries[ndx], i) = 0;
940 for (j = keyi->numLevels[ndx] - 1;
941 j >= 0 && darray_item(keyi->symsMapNumEntries[ndx], j) == 0; j--)
942 keyi->numLevels[ndx]--;
947 AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
951 xkb_group_index_t ndx;
954 union xkb_action *toAct;
956 if (!GetGroupIndex(info, keyi, arrayNdx, ACTIONS, &ndx))
960 keyi->actsDefined |= (1 << ndx);
964 if (value->op != EXPR_ACTION_LIST) {
965 log_wsgo(info->keymap->ctx,
966 "Bad expression type (%d) for action list value; "
967 "Ignoring actions for group %u of %s\n",
968 value->op, ndx, LongKeyNameText(keyi->name));
972 if (!darray_empty(keyi->acts[ndx])) {
973 log_wsgo(info->keymap->ctx,
974 "Actions for key %s, group %u already defined\n",
975 LongKeyNameText(keyi->name), ndx);
979 for (nActs = 0, act = value->value.child; act != NULL; nActs++) {
980 act = (ExprDef *) act->common.next;
984 log_wsgo(info->keymap->ctx,
985 "Action list but not actions in AddActionsToKey\n");
989 if ((keyi->numLevels[ndx] < nActs || darray_empty(keyi->acts[ndx])) &&
990 !ResizeKeyGroup(keyi, ndx, nActs, nActs, true)) {
991 log_wsgo(info->keymap->ctx,
992 "Could not resize group %u of key %s; "
994 ndx, LongKeyNameText(keyi->name));
997 keyi->actsDefined |= (1 << ndx);
999 toAct = darray_mem(keyi->acts[ndx], 0);
1000 act = value->value.child;
1001 for (i = 0; i < nActs; i++, toAct++) {
1002 if (!HandleActionDef(act, info->keymap, toAct, info->action)) {
1003 log_err(info->keymap->ctx,
1004 "Illegal action definition for %s; "
1005 "Action for group %u/level %zu ignored\n",
1006 LongKeyNameText(keyi->name), ndx + 1, i + 1);
1008 act = (ExprDef *) act->common.next;
1014 static const LookupEntry repeatEntries[] = {
1015 { "true", KEY_REPEAT_YES },
1016 { "yes", KEY_REPEAT_YES },
1017 { "on", KEY_REPEAT_YES },
1018 { "false", KEY_REPEAT_NO },
1019 { "no", KEY_REPEAT_NO },
1020 { "off", KEY_REPEAT_NO },
1021 { "default", KEY_REPEAT_UNDEFINED },
1026 SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
1027 ExprDef *arrayNdx, ExprDef *value)
1030 struct xkb_context *ctx = info->keymap->ctx;
1032 if (istreq(field, "type")) {
1033 xkb_group_index_t ndx;
1036 if (!ExprResolveString(ctx, value, &str))
1037 log_lvl(info->keymap->ctx, 1,
1038 "The type field of a key symbol map must be a string; "
1039 "Ignoring illegal type definition\n");
1041 if (arrayNdx == NULL) {
1042 keyi->dfltType = xkb_atom_intern(ctx, str);
1043 keyi->defined |= KEY_FIELD_TYPE_DFLT;
1045 else if (!ExprResolveGroup(ctx, arrayNdx, &ndx)) {
1046 log_err(info->keymap->ctx,
1047 "Illegal group index for type of key %s; "
1048 "Definition with non-integer array index ignored\n",
1049 LongKeyNameText(keyi->name));
1054 keyi->types[ndx] = xkb_atom_intern(ctx, str);
1055 keyi->typesDefined |= (1 << ndx);
1058 else if (istreq(field, "symbols"))
1059 return AddSymbolsToKey(info, keyi, arrayNdx, value);
1060 else if (istreq(field, "actions"))
1061 return AddActionsToKey(info, keyi, arrayNdx, value);
1062 else if (istreq(field, "vmods") ||
1063 istreq(field, "virtualmods") ||
1064 istreq(field, "virtualmodifiers")) {
1065 xkb_mod_mask_t mask;
1067 ok = ExprResolveVModMask(info->keymap, value, &mask);
1069 keyi->vmodmap = (mask >> XkbNumModifiers) & 0xffff;
1070 keyi->defined |= KEY_FIELD_VMODMAP;
1073 log_err(info->keymap->ctx,
1074 "Expected a virtual modifier mask, found %s; "
1075 "Ignoring virtual modifiers definition for key %s\n",
1076 expr_op_type_to_string(value->op),
1077 LongKeyNameText(keyi->name));
1080 else if (istreq(field, "locking") ||
1081 istreq(field, "lock") ||
1082 istreq(field, "locks")) {
1083 log_err(info->keymap->ctx,
1084 "Key behaviors not supported; "
1085 "Ignoring locking specification for key %s\n",
1086 LongKeyNameText(keyi->name));
1088 else if (istreq(field, "radiogroup") ||
1089 istreq(field, "permanentradiogroup") ||
1090 istreq(field, "allownone")) {
1091 log_err(info->keymap->ctx,
1092 "Radio groups not supported; "
1093 "Ignoring radio group specification for key %s\n",
1094 LongKeyNameText(keyi->name));
1096 else if (istreq_prefix("overlay", field) ||
1097 istreq_prefix("permanentoverlay", field)) {
1098 log_err(info->keymap->ctx,
1099 "Overlays not supported; "
1100 "Ignoring overlay specification for key %s\n",
1101 LongKeyNameText(keyi->name));
1103 else if (istreq(field, "repeating") ||
1104 istreq(field, "repeats") ||
1105 istreq(field, "repeat")) {
1108 ok = ExprResolveEnum(ctx, value, &val, repeatEntries);
1110 log_err(info->keymap->ctx,
1111 "Illegal repeat setting for %s; "
1112 "Non-boolean repeat setting ignored\n",
1113 LongKeyNameText(keyi->name));
1117 keyi->defined |= KEY_FIELD_REPEAT;
1119 else if (istreq(field, "groupswrap") ||
1120 istreq(field, "wrapgroups")) {
1123 if (!ExprResolveBoolean(ctx, value, &set)) {
1124 log_err(info->keymap->ctx,
1125 "Illegal groupsWrap setting for %s; "
1126 "Non-boolean value ignored\n",
1127 LongKeyNameText(keyi->name));
1132 keyi->out_of_range_group_action = XkbWrapIntoRange;
1134 keyi->out_of_range_group_action = XkbClampIntoRange;
1136 keyi->defined |= KEY_FIELD_GROUPINFO;
1138 else if (istreq(field, "groupsclamp") ||
1139 istreq(field, "clampgroups")) {
1142 if (!ExprResolveBoolean(ctx, value, &set)) {
1143 log_err(info->keymap->ctx,
1144 "Illegal groupsClamp setting for %s; "
1145 "Non-boolean value ignored\n",
1146 LongKeyNameText(keyi->name));
1151 keyi->out_of_range_group_action = XkbClampIntoRange;
1153 keyi->out_of_range_group_action = XkbWrapIntoRange;
1155 keyi->defined |= KEY_FIELD_GROUPINFO;
1157 else if (istreq(field, "groupsredirect") ||
1158 istreq(field, "redirectgroups")) {
1159 xkb_group_index_t grp;
1161 if (!ExprResolveGroup(ctx, value, &grp)) {
1162 log_err(info->keymap->ctx,
1163 "Illegal group index for redirect of key %s; "
1164 "Definition with non-integer group ignored\n",
1165 LongKeyNameText(keyi->name));
1169 keyi->out_of_range_group_action = XkbRedirectIntoRange;
1170 keyi->out_of_range_group_number = grp - 1;
1171 keyi->defined |= KEY_FIELD_GROUPINFO;
1174 log_err(info->keymap->ctx,
1175 "Unknown field %s in a symbol interpretation; "
1176 "Definition ignored\n",
1185 SetGroupName(SymbolsInfo *info, ExprDef *arrayNdx, ExprDef *value)
1187 xkb_group_index_t grp;
1191 log_lvl(info->keymap->ctx, 1,
1192 "You must specify an index when specifying a group name; "
1193 "Group name definition without array subscript ignored\n");
1197 if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, &grp)) {
1198 log_err(info->keymap->ctx,
1199 "Illegal index in group name definition; "
1200 "Definition with non-integer array index ignored\n");
1204 if (!ExprResolveString(info->keymap->ctx, value, &name)) {
1205 log_err(info->keymap->ctx,
1206 "Group name must be a string; "
1207 "Illegal name for group %d ignored\n", grp);
1211 info->groupNames[grp - 1 + info->explicit_group] =
1212 xkb_atom_intern(info->keymap->ctx, name);
1218 HandleSymbolsVar(SymbolsInfo *info, VarDef *stmt)
1220 const char *elem, *field;
1224 if (ExprResolveLhs(info->keymap->ctx, stmt->name, &elem, &field,
1226 return 0; /* internal error, already reported */
1227 if (elem && istreq(elem, "key")) {
1228 ret = SetSymbolsField(info, &info->dflt, field, arrayNdx,
1231 else if (!elem && (istreq(field, "name") ||
1232 istreq(field, "groupname"))) {
1233 ret = SetGroupName(info, arrayNdx, stmt->value);
1235 else if (!elem && (istreq(field, "groupswrap") ||
1236 istreq(field, "wrapgroups"))) {
1237 log_err(info->keymap->ctx,
1238 "Global \"groupswrap\" not supported; Ignored\n");
1241 else if (!elem && (istreq(field, "groupsclamp") ||
1242 istreq(field, "clampgroups"))) {
1243 log_err(info->keymap->ctx,
1244 "Global \"groupsclamp\" not supported; Ignored\n");
1247 else if (!elem && (istreq(field, "groupsredirect") ||
1248 istreq(field, "redirectgroups"))) {
1249 log_err(info->keymap->ctx,
1250 "Global \"groupsredirect\" not supported; Ignored\n");
1253 else if (!elem && istreq(field, "allownone")) {
1254 log_err(info->keymap->ctx,
1255 "Radio groups not supported; "
1256 "Ignoring \"allownone\" specification\n");
1260 ret = SetActionField(info->keymap, elem, field, arrayNdx,
1261 stmt->value, &info->action);
1268 HandleSymbolsBody(SymbolsInfo *info, VarDef *def, KeyInfo *keyi)
1271 const char *elem, *field;
1274 for (; def; def = (VarDef *) def->common.next) {
1275 if (def->name && def->name->op == EXPR_FIELD_REF) {
1276 ok = HandleSymbolsVar(info, def);
1281 if (!def->value || def->value->op == EXPR_KEYSYM_LIST)
1288 ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
1293 ok = SetSymbolsField(info, keyi, field, arrayNdx, def->value);
1300 SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi)
1302 xkb_group_index_t group = info->explicit_group;
1307 if ((keyi->typesDefined | keyi->symsDefined | keyi->actsDefined) & ~1) {
1308 xkb_group_index_t i;
1309 log_warn(info->keymap->ctx,
1310 "For the map %s an explicit group specified, "
1311 "but key %s has more than one group defined; "
1312 "All groups except first one will be ignored\n",
1313 info->name, LongKeyNameText(keyi->name));
1314 for (i = 1; i < XkbNumKbdGroups; i++) {
1315 keyi->numLevels[i] = 0;
1316 darray_free(keyi->syms[i]);
1317 darray_free(keyi->acts[i]);
1321 keyi->typesDefined = keyi->symsDefined = keyi->actsDefined = 1 << group;
1323 keyi->numLevels[group] = keyi->numLevels[0];
1324 keyi->numLevels[0] = 0;
1325 keyi->syms[group] = keyi->syms[0];
1326 darray_init(keyi->syms[0]);
1327 keyi->symsMapIndex[group] = keyi->symsMapIndex[0];
1328 darray_init(keyi->symsMapIndex[0]);
1329 keyi->symsMapNumEntries[group] = keyi->symsMapNumEntries[0];
1330 darray_init(keyi->symsMapNumEntries[0]);
1331 keyi->acts[group] = keyi->acts[0];
1332 darray_init(keyi->acts[0]);
1333 keyi->types[group] = keyi->types[0];
1339 HandleSymbolsDef(SymbolsInfo *info, SymbolsDef *stmt)
1343 InitKeyInfo(&keyi, info->file_id);
1344 CopyKeyInfo(&info->dflt, &keyi, false);
1345 keyi.merge = stmt->merge;
1346 keyi.name = KeyNameToLong(stmt->keyName);
1347 if (!HandleSymbolsBody(info, (VarDef *) stmt->symbols, &keyi)) {
1352 if (!SetExplicitGroup(info, &keyi)) {
1357 if (!AddKeySymbols(info, &keyi)) {
1365 HandleModMapDef(SymbolsInfo *info, ModMapDef *def)
1369 xkb_mod_index_t ndx;
1371 struct xkb_context *ctx = info->keymap->ctx;
1373 if (!LookupModIndex(ctx, NULL, def->modifier, EXPR_TYPE_INT, &ndx)) {
1374 log_err(info->keymap->ctx,
1375 "Illegal modifier map definition; "
1376 "Ignoring map for non-modifier \"%s\"\n",
1377 xkb_atom_text(ctx, def->modifier));
1384 for (key = def->keys; key != NULL; key = (ExprDef *) key->common.next) {
1387 if (key->op == EXPR_VALUE && key->value_type == EXPR_TYPE_KEYNAME) {
1388 tmp.haveSymbol = false;
1389 tmp.u.keyName = KeyNameToLong(key->value.keyName);
1391 else if (ExprResolveKeySym(ctx, key, &sym)) {
1392 tmp.haveSymbol = true;
1396 log_err(info->keymap->ctx,
1397 "Modmap entries may contain only key names or keysyms; "
1398 "Illegal definition for %s modifier ignored\n",
1399 ModIndexText(tmp.modifier));
1403 ok = AddModMapEntry(info, &tmp) && ok;
1409 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge)
1415 info->name = strdup_safe(file->name);
1418 for (stmt = file->defs; stmt; stmt = stmt->next) {
1419 switch (stmt->type) {
1421 ok = HandleIncludeSymbols(info, (IncludeStmt *) stmt);
1424 ok = HandleSymbolsDef(info, (SymbolsDef *) stmt);
1427 ok = HandleSymbolsVar(info, (VarDef *) stmt);
1430 ok = HandleVModDef((VModDef *) stmt, info->keymap, merge,
1434 ok = HandleModMapDef(info, (ModMapDef *) stmt);
1437 log_err(info->keymap->ctx,
1438 "Interpretation files may not include other types; "
1439 "Ignoring %s\n", stmt_type_to_string(stmt->type));
1447 if (info->errorCount > 10) {
1448 log_err(info->keymap->ctx, "Abandoning symbols file \"%s\"\n",
1456 * Given a keysym @sym, return a key which generates it, or NULL.
1457 * This is used for example in a modifier map definition, such as:
1458 * modifier_map Lock { Caps_Lock };
1459 * where we want to add the Lock modifier to the modmap of the key
1460 * which matches the keysym Caps_Lock.
1461 * Since there can be many keys which generates the keysym, the key
1462 * is chosen first by lowest group in which the keysym appears, than
1463 * by lowest level and than by lowest key code.
1465 static struct xkb_key *
1466 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
1468 struct xkb_key *key, *ret = NULL;
1469 xkb_group_index_t group, min_group = UINT32_MAX;
1470 xkb_level_index_t level, min_level = UINT16_MAX;
1472 xkb_foreach_key(key, keymap) {
1473 for (group = 0; group < key->num_groups; group++) {
1474 for (level = 0; level < XkbKeyGroupWidth(keymap, key, group);
1476 if (XkbKeyNumSyms(key, group, level) != 1 ||
1477 (XkbKeySymEntry(key, group, level))[0] != sym)
1481 * If the keysym was found in a group or level > 0, we must
1482 * keep looking since we might find a key in which the keysym
1483 * is in a lower group or level.
1485 if (group < min_group ||
1486 (group == min_group && level < min_level)) {
1488 if (group == 0 && level == 0) {
1504 * Find the given name in the keymap->map->types and return its index.
1506 * @param name The name to search for.
1507 * @param type_rtrn Set to the index of the name if found.
1509 * @return true if found, false otherwise.
1512 FindNamedType(struct xkb_keymap *keymap, xkb_atom_t name, unsigned *type_rtrn)
1516 for (i = 0; i < keymap->num_types; i++) {
1517 if (keymap->types[i].name == name) {
1527 * Assign a type to the given sym and return the Atom for the type assigned.
1530 * - ONE_LEVEL for width 0/1
1531 * - ALPHABETIC for 2 shift levels, with lower/upercase
1532 * - KEYPAD for keypad keys.
1533 * - TWO_LEVEL for other 2 shift level keys.
1534 * and the same for four level keys.
1536 * @param width Number of sysms in syms.
1537 * @param syms The keysyms for the given key (must be size width).
1538 * @param typeNameRtrn Set to the Atom of the type name.
1540 * @returns true if a type could be found, false otherwise.
1542 * FIXME: I need to take the KeyInfo so I can look at symsMapIndex and
1543 * all that fun stuff rather than just assuming there's always one
1547 FindAutomaticType(struct xkb_keymap *keymap, xkb_level_index_t width,
1548 const xkb_keysym_t *syms, xkb_atom_t *typeNameRtrn,
1552 if ((width == 1) || (width == 0)) {
1553 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ONE_LEVEL");
1556 else if (width == 2) {
1557 if (syms && xkb_keysym_is_lower(syms[0]) &&
1558 xkb_keysym_is_upper(syms[1])) {
1559 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ALPHABETIC");
1561 else if (syms && (xkb_keysym_is_keypad(syms[0]) ||
1562 xkb_keysym_is_keypad(syms[1]))) {
1563 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "KEYPAD");
1567 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "TWO_LEVEL");
1571 else if (width <= 4) {
1572 if (syms && xkb_keysym_is_lower(syms[0]) &&
1573 xkb_keysym_is_upper(syms[1]))
1574 if (xkb_keysym_is_lower(syms[2]) && xkb_keysym_is_upper(syms[3]))
1576 xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_ALPHABETIC");
1578 *typeNameRtrn = xkb_atom_intern(keymap->ctx,
1579 "FOUR_LEVEL_SEMIALPHABETIC");
1581 else if (syms && (xkb_keysym_is_keypad(syms[0]) ||
1582 xkb_keysym_is_keypad(syms[1])))
1583 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_KEYPAD");
1585 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL");
1586 /* XXX: why not set autoType here? */
1592 * Ensure the given KeyInfo is in a coherent state, i.e. no gaps between the
1593 * groups, and reduce to one group if all groups are identical anyway.
1596 PrepareKeyDef(KeyInfo *keyi)
1598 xkb_group_index_t i, lastGroup;
1599 unsigned int defined;
1600 xkb_level_index_t j, width;
1603 defined = keyi->symsDefined | keyi->actsDefined | keyi->typesDefined;
1604 /* get highest group number */
1605 for (i = XkbNumKbdGroups - 1; i > 0; i--) {
1606 if (defined & (1 << i))
1614 /* If there are empty groups between non-empty ones fill them with data */
1615 /* from the first group. */
1616 /* We can make a wrong assumption here. But leaving gaps is worse. */
1617 for (i = lastGroup; i > 0; i--) {
1618 if (defined & (1 << i))
1620 width = keyi->numLevels[0];
1621 if (keyi->typesDefined & 1) {
1622 for (j = 0; j < width; j++) {
1623 keyi->types[i] = keyi->types[0];
1625 keyi->typesDefined |= 1 << i;
1627 if ((keyi->actsDefined & 1) && !darray_empty(keyi->acts[0])) {
1628 darray_copy(keyi->acts[i], keyi->acts[0]);
1629 keyi->actsDefined |= 1 << i;
1631 if ((keyi->symsDefined & 1) && !darray_empty(keyi->syms[0])) {
1632 darray_copy(keyi->syms[i], keyi->syms[0]);
1633 darray_copy(keyi->symsMapIndex[i], keyi->symsMapIndex[0]);
1634 darray_copy(keyi->symsMapNumEntries[i],
1635 keyi->symsMapNumEntries[0]);
1636 keyi->symsDefined |= 1 << i;
1639 keyi->numLevels[i] = keyi->numLevels[0];
1642 /* If all groups are completely identical remove them all */
1643 /* exept the first one. */
1645 for (i = lastGroup; i > 0; i--) {
1646 if ((keyi->numLevels[i] != keyi->numLevels[0]) ||
1647 (keyi->types[i] != keyi->types[0])) {
1651 if (!darray_same(keyi->syms[i], keyi->syms[0]) &&
1652 (darray_empty(keyi->syms[i]) || darray_empty(keyi->syms[0]) ||
1653 darray_size(keyi->syms[i]) != darray_size(keyi->syms[0]) ||
1654 memcmp(darray_mem(keyi->syms[i], 0),
1655 darray_mem(keyi->syms[0], 0),
1656 sizeof(xkb_keysym_t) * darray_size(keyi->syms[0])))) {
1660 if (!darray_same(keyi->symsMapIndex[i], keyi->symsMapIndex[0]) &&
1661 (darray_empty(keyi->symsMapIndex[i]) ||
1662 darray_empty(keyi->symsMapIndex[0]) ||
1663 memcmp(darray_mem(keyi->symsMapIndex[i], 0),
1664 darray_mem(keyi->symsMapIndex[0], 0),
1665 keyi->numLevels[0] * sizeof(int)))) {
1669 if (!darray_same(keyi->symsMapNumEntries[i],
1670 keyi->symsMapNumEntries[0]) &&
1671 (darray_empty(keyi->symsMapNumEntries[i]) ||
1672 darray_empty(keyi->symsMapNumEntries[0]) ||
1673 memcmp(darray_mem(keyi->symsMapNumEntries[i], 0),
1674 darray_mem(keyi->symsMapNumEntries[0], 0),
1675 keyi->numLevels[0] * sizeof(size_t)))) {
1679 if (!darray_same(keyi->acts[i], keyi->acts[0]) &&
1680 (darray_empty(keyi->acts[i]) || darray_empty(keyi->acts[0]) ||
1681 memcmp(darray_mem(keyi->acts[i], 0),
1682 darray_mem(keyi->acts[0], 0),
1683 keyi->numLevels[0] * sizeof(union xkb_action)))) {
1689 for (i = lastGroup; i > 0; i--) {
1690 keyi->numLevels[i] = 0;
1691 darray_free(keyi->syms[i]);
1692 darray_free(keyi->symsMapIndex[i]);
1693 darray_free(keyi->symsMapNumEntries[i]);
1694 darray_free(keyi->acts[i]);
1697 keyi->symsDefined &= 1;
1698 keyi->actsDefined &= 1;
1699 keyi->typesDefined &= 1;
1704 * Copy the KeyInfo into the keyboard description.
1706 * This function recurses.
1709 CopySymbolsDef(SymbolsInfo *info, KeyInfo *keyi,
1710 xkb_keycode_t start_from)
1712 struct xkb_keymap *keymap = info->keymap;
1714 struct xkb_key *key;
1715 size_t sizeSyms = 0;
1716 xkb_group_index_t i, nGroups;
1717 xkb_level_index_t width, tmp;
1718 struct xkb_key_type * type;
1719 bool haveActions, autoType, useAlias;
1720 unsigned types[XkbNumKbdGroups];
1721 unsigned int symIndex = 0;
1723 useAlias = (start_from == 0);
1725 key = FindNamedKey(keymap, keyi->name, useAlias, start_from);
1727 if (start_from == 0)
1728 log_lvl(info->keymap->ctx, 5,
1729 "Key %s not found in keycodes; Symbols ignored\n",
1730 LongKeyNameText(keyi->name));
1733 kc = XkbKeyGetKeycode(keymap, key);
1735 haveActions = false;
1737 for (i = nGroups = 0; i < XkbNumKbdGroups; i++) {
1738 if (((i + 1) > nGroups)
1739 && (((keyi->symsDefined | keyi->actsDefined) & (1 << i))
1740 || (keyi->typesDefined) & (1 << i)))
1742 if (!darray_empty(keyi->acts[i]))
1745 /* Assign the type to the key, if it is missing. */
1746 if (keyi->types[i] == XKB_ATOM_NONE) {
1747 if (keyi->dfltType != XKB_ATOM_NONE)
1748 keyi->types[i] = keyi->dfltType;
1749 else if (FindAutomaticType(keymap, keyi->numLevels[i],
1750 darray_mem(keyi->syms[i], 0),
1751 &keyi->types[i], &autoType)) { }
1753 log_lvl(info->keymap->ctx, 5,
1754 "No automatic type for %d symbols; "
1755 "Using %s for the %s key (keycode %d)\n",
1757 xkb_atom_text(keymap->ctx, keyi->types[i]),
1758 LongKeyNameText(keyi->name), kc);
1760 if (FindNamedType(keymap, keyi->types[i], &types[i])) {
1761 if (!autoType || keyi->numLevels[i] > 2)
1762 key->explicit |= (1 << i);
1765 log_lvl(info->keymap->ctx, 3,
1766 "Type \"%s\" is not defined; "
1767 "Using default type for the %s key (keycode %d)\n",
1768 xkb_atom_text(keymap->ctx, keyi->types[i]),
1769 LongKeyNameText(keyi->name), kc);
1771 * Index 0 is guaranteed to contain something, usually
1772 * ONE_LEVEL or at least some default one-level type.
1777 /* if the type specifies fewer levels than the key has, shrink the key */
1778 type = &keymap->types[types[i]];
1779 if (type->num_levels < keyi->numLevels[i]) {
1780 log_lvl(info->keymap->ctx, 1,
1781 "Type \"%s\" has %d levels, but %s has %d symbols; "
1782 "Ignoring extra symbols\n",
1783 xkb_atom_text(keymap->ctx, type->name),
1785 LongKeyNameText(keyi->name),
1786 keyi->numLevels[i]);
1787 keyi->numLevels[i] = type->num_levels;
1789 if (keyi->numLevels[i] > width)
1790 width = keyi->numLevels[i];
1791 if (type->num_levels > width)
1792 width = type->num_levels;
1793 sizeSyms += darray_size(keyi->syms[i]);
1796 darray_resize0(key->syms, sizeSyms);
1798 key->num_groups = nGroups;
1802 key->sym_index = calloc(nGroups * width, sizeof(*key->sym_index));
1804 key->num_syms = calloc(nGroups * width, sizeof(*key->num_syms));
1807 key->actions = calloc(nGroups * width, sizeof(*key->actions));
1808 key->explicit |= XkbExplicitInterpretMask;
1811 if (keyi->defined & KEY_FIELD_GROUPINFO) {
1812 key->out_of_range_group_number = keyi->out_of_range_group_number;
1813 key->out_of_range_group_action = keyi->out_of_range_group_action;
1816 for (i = 0; i < nGroups; i++) {
1817 /* assign kt_index[i] to the index of the type in map->types.
1818 * kt_index[i] may have been set by a previous run (if we have two
1819 * layouts specified). Let's not overwrite it with the ONE_LEVEL
1820 * default group if we dont even have keys for this group anyway.
1822 * FIXME: There should be a better fix for this.
1824 if (keyi->numLevels[i])
1825 key->kt_index[i] = types[i];
1826 if (!darray_empty(keyi->syms[i])) {
1827 /* fill key to "width" symbols*/
1828 for (tmp = 0; tmp < width; tmp++) {
1829 if (tmp < keyi->numLevels[i] &&
1830 darray_item(keyi->symsMapNumEntries[i], tmp) != 0) {
1831 memcpy(darray_mem(key->syms, symIndex),
1832 darray_mem(keyi->syms[i],
1833 darray_item(keyi->symsMapIndex[i], tmp)),
1834 darray_item(keyi->symsMapNumEntries[i],
1835 tmp) * sizeof(xkb_keysym_t));
1836 key->sym_index[(i * width) + tmp] = symIndex;
1837 key->num_syms[(i * width) + tmp] =
1838 darray_item(keyi->symsMapNumEntries[i], tmp);
1839 symIndex += key->num_syms[(i * width) + tmp];
1842 key->sym_index[(i * width) + tmp] = -1;
1843 key->num_syms[(i * width) + tmp] = 0;
1845 if (key->actions && !darray_empty(keyi->acts[i])) {
1846 if (tmp < keyi->numLevels[i])
1847 key->actions[tmp] = darray_item(keyi->acts[i], tmp);
1849 key->actions[tmp].type = XkbSA_NoAction;
1855 if (keyi->defined & KEY_FIELD_VMODMAP) {
1856 key->vmodmap = keyi->vmodmap;
1857 key->explicit |= XkbExplicitVModMapMask;
1860 if (keyi->repeat != KEY_REPEAT_UNDEFINED) {
1861 key->repeats = (keyi->repeat == KEY_REPEAT_YES);
1862 key->explicit |= XkbExplicitAutoRepeatMask;
1865 /* do the same thing for the next key */
1866 CopySymbolsDef(info, keyi, kc + 1);
1871 CopyModMapDef(SymbolsInfo *info, ModMapEntry *entry)
1873 struct xkb_key *key;
1874 struct xkb_keymap *keymap = info->keymap;
1876 if (!entry->haveSymbol) {
1877 key = FindNamedKey(keymap, entry->u.keyName, true, 0);
1879 log_lvl(info->keymap->ctx, 5,
1880 "Key %s not found in keycodes; "
1881 "Modifier map entry for %s not updated\n",
1882 LongKeyNameText(entry->u.keyName),
1883 ModIndexText(entry->modifier));
1888 key = FindKeyForSymbol(keymap, entry->u.keySym);
1890 log_lvl(info->keymap->ctx, 5,
1891 "Key \"%s\" not found in symbol map; "
1892 "Modifier map entry for %s not updated\n",
1893 KeysymText(entry->u.keySym),
1894 ModIndexText(entry->modifier));
1899 key->modmap |= (1 << entry->modifier);
1904 * Handle the xkb_symbols section of an xkb file.
1906 * @param file The parsed xkb_symbols section of the xkb file.
1907 * @param keymap Handle to the keyboard description to store the symbols in.
1908 * @param merge Merge strategy (e.g. MERGE_OVERRIDE).
1911 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1912 enum merge_mode merge)
1914 xkb_group_index_t i;
1915 struct xkb_key *key;
1920 InitSymbolsInfo(&info, keymap, file->id);
1921 info.dflt.merge = merge;
1923 HandleSymbolsFile(&info, file, merge);
1925 if (darray_empty(info.keys))
1928 if (info.errorCount != 0)
1932 keymap->symbols_section_name = strdup(info.name);
1934 for (i = 0; i < XkbNumKbdGroups; i++) {
1935 if (info.groupNames[i] != XKB_ATOM_NONE) {
1936 keymap->group_names[i] = xkb_atom_text(keymap->ctx,
1937 info.groupNames[i]);
1942 darray_foreach(keyi, info.keys)
1943 PrepareKeyDef(keyi);
1946 darray_foreach(keyi, info.keys)
1947 if (!CopySymbolsDef(&info, keyi, 0))
1950 if (xkb_get_log_verbosity(keymap->ctx) > 3) {
1951 xkb_foreach_key(key, keymap) {
1952 if (key->name[0] == '\0')
1955 if (key->num_groups < 1)
1956 log_info(info.keymap->ctx,
1957 "No symbols defined for %s (keycode %d)\n",
1958 KeyNameText(key->name),
1959 XkbKeyGetKeycode(keymap, key));
1963 list_foreach(mm, &info.modMaps, entry)
1964 if (!CopyModMapDef(&info, mm))
1967 FreeSymbolsInfo(&info);
1971 FreeSymbolsInfo(&info);