1 /************************************************************
2 * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
3 *
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.
15 *
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.
24 *
25 ********************************************************/
26
27 #include "config.h"
28
29 #include "xkbcomp-priv.h"
30 #include "text.h"
31 #include "vmod.h"
32 #include "expr.h"
33 #include "include.h"
34
35 enum type_field {
36 TYPE_FIELD_MASK = (1 << 0),
37 TYPE_FIELD_MAP = (1 << 1),
38 TYPE_FIELD_PRESERVE = (1 << 2),
39 TYPE_FIELD_LEVEL_NAME = (1 << 3),
40 };
41
42 typedef struct {
43 enum type_field defined;
44 enum merge_mode merge;
45
46 xkb_atom_t name;
47 xkb_mod_mask_t mods;
48 xkb_level_index_t num_levels;
49 darray(struct xkb_key_type_entry) entries;
50 darray(xkb_atom_t) level_names;
51 } KeyTypeInfo;
52
53 typedef struct {
54 char *name;
55 int errorCount;
56
57 darray(KeyTypeInfo) types;
58 struct xkb_mod_set mods;
59
60 struct xkb_context *ctx;
61 } KeyTypesInfo;
62
63 /***====================================================================***/
64
65 static inline const char *
MapEntryTxt(KeyTypesInfo * info,struct xkb_key_type_entry * entry)66 MapEntryTxt(KeyTypesInfo *info, struct xkb_key_type_entry *entry)
67 {
68 return ModMaskText(info->ctx, &info->mods, entry->mods.mods);
69 }
70
71 static inline const char *
TypeTxt(KeyTypesInfo * info,KeyTypeInfo * type)72 TypeTxt(KeyTypesInfo *info, KeyTypeInfo *type)
73 {
74 return xkb_atom_text(info->ctx, type->name);
75 }
76
77 static inline const char *
TypeMaskTxt(KeyTypesInfo * info,KeyTypeInfo * type)78 TypeMaskTxt(KeyTypesInfo *info, KeyTypeInfo *type)
79 {
80 return ModMaskText(info->ctx, &info->mods, type->mods);
81 }
82
83 static inline bool
ReportTypeShouldBeArray(KeyTypesInfo * info,KeyTypeInfo * type,const char * field)84 ReportTypeShouldBeArray(KeyTypesInfo *info, KeyTypeInfo *type,
85 const char *field)
86 {
87 return ReportShouldBeArray(info->ctx, "key type", field,
88 TypeTxt(info, type));
89 }
90
91 static inline bool
ReportTypeBadType(KeyTypesInfo * info,KeyTypeInfo * type,const char * field,const char * wanted)92 ReportTypeBadType(KeyTypesInfo *info, KeyTypeInfo *type,
93 const char *field, const char *wanted)
94 {
95 return ReportBadType(info->ctx, "key type", field,
96 TypeTxt(info, type), wanted);
97 }
98
99 /***====================================================================***/
100
101 static void
InitKeyTypesInfo(KeyTypesInfo * info,struct xkb_context * ctx,const struct xkb_mod_set * mods)102 InitKeyTypesInfo(KeyTypesInfo *info, struct xkb_context *ctx,
103 const struct xkb_mod_set *mods)
104 {
105 memset(info, 0, sizeof(*info));
106 info->ctx = ctx;
107 info->mods = *mods;
108 }
109
110 static void
ClearKeyTypeInfo(KeyTypeInfo * type)111 ClearKeyTypeInfo(KeyTypeInfo *type)
112 {
113 darray_free(type->entries);
114 darray_free(type->level_names);
115 }
116
117 static void
ClearKeyTypesInfo(KeyTypesInfo * info)118 ClearKeyTypesInfo(KeyTypesInfo *info)
119 {
120 free(info->name);
121 darray_free(info->types);
122 }
123
124 static KeyTypeInfo *
FindMatchingKeyType(KeyTypesInfo * info,xkb_atom_t name)125 FindMatchingKeyType(KeyTypesInfo *info, xkb_atom_t name)
126 {
127 KeyTypeInfo *old;
128
129 darray_foreach(old, info->types)
130 if (old->name == name)
131 return old;
132
133 return NULL;
134 }
135
136 static bool
AddKeyType(KeyTypesInfo * info,KeyTypeInfo * new,bool same_file)137 AddKeyType(KeyTypesInfo *info, KeyTypeInfo *new, bool same_file)
138 {
139 KeyTypeInfo *old;
140 const int verbosity = xkb_context_get_log_verbosity(info->ctx);
141
142 old = FindMatchingKeyType(info, new->name);
143 if (old) {
144 if (new->merge == MERGE_REPLACE || new->merge == MERGE_OVERRIDE) {
145 if ((same_file && verbosity > 0) || verbosity > 9) {
146 log_warn(info->ctx,
147 "Multiple definitions of the %s key type; "
148 "Earlier definition ignored\n",
149 xkb_atom_text(info->ctx, new->name));
150 }
151
152 ClearKeyTypeInfo(old);
153 *old = *new;
154 darray_init(new->entries);
155 darray_init(new->level_names);
156 return true;
157 }
158
159 if (same_file)
160 log_vrb(info->ctx, 4,
161 "Multiple definitions of the %s key type; "
162 "Later definition ignored\n",
163 xkb_atom_text(info->ctx, new->name));
164
165 ClearKeyTypeInfo(new);
166 return true;
167 }
168
169 darray_append(info->types, *new);
170 return true;
171 }
172
173 /***====================================================================***/
174
175 static void
MergeIncludedKeyTypes(KeyTypesInfo * into,KeyTypesInfo * from,enum merge_mode merge)176 MergeIncludedKeyTypes(KeyTypesInfo *into, KeyTypesInfo *from,
177 enum merge_mode merge)
178 {
179 if (from->errorCount > 0) {
180 into->errorCount += from->errorCount;
181 return;
182 }
183
184 into->mods = from->mods;
185
186 if (into->name == NULL) {
187 into->name = from->name;
188 from->name = NULL;
189 }
190
191 if (darray_empty(into->types)) {
192 into->types = from->types;
193 darray_init(from->types);
194 }
195 else {
196 KeyTypeInfo *type;
197 darray_foreach(type, from->types) {
198 type->merge = (merge == MERGE_DEFAULT ? type->merge : merge);
199 if (!AddKeyType(into, type, false))
200 into->errorCount++;
201 }
202 }
203 }
204
205 static void
206 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge);
207
208 static bool
HandleIncludeKeyTypes(KeyTypesInfo * info,IncludeStmt * include)209 HandleIncludeKeyTypes(KeyTypesInfo *info, IncludeStmt *include)
210 {
211 KeyTypesInfo included;
212
213 InitKeyTypesInfo(&included, info->ctx, &info->mods);
214 included.name = include->stmt;
215 include->stmt = NULL;
216
217 for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
218 KeyTypesInfo next_incl;
219 XkbFile *file;
220
221 file = ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_TYPES);
222 if (!file) {
223 info->errorCount += 10;
224 ClearKeyTypesInfo(&included);
225 return false;
226 }
227
228 InitKeyTypesInfo(&next_incl, info->ctx, &included.mods);
229
230 HandleKeyTypesFile(&next_incl, file, stmt->merge);
231
232 MergeIncludedKeyTypes(&included, &next_incl, stmt->merge);
233
234 ClearKeyTypesInfo(&next_incl);
235 FreeXkbFile(file);
236 }
237
238 MergeIncludedKeyTypes(info, &included, include->merge);
239 ClearKeyTypesInfo(&included);
240
241 return (info->errorCount == 0);
242 }
243
244 /***====================================================================***/
245
246 static bool
SetModifiers(KeyTypesInfo * info,KeyTypeInfo * type,ExprDef * arrayNdx,ExprDef * value)247 SetModifiers(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
248 ExprDef *value)
249 {
250 xkb_mod_mask_t mods;
251
252 if (arrayNdx)
253 log_warn(info->ctx,
254 "The modifiers field of a key type is not an array; "
255 "Illegal array subscript ignored\n");
256
257 if (!ExprResolveModMask(info->ctx, value, MOD_BOTH, &info->mods, &mods)) {
258 log_err(info->ctx,
259 "Key type mask field must be a modifier mask; "
260 "Key type definition ignored\n");
261 return false;
262 }
263
264 if (type->defined & TYPE_FIELD_MASK) {
265 log_warn(info->ctx,
266 "Multiple modifier mask definitions for key type %s; "
267 "Using %s, ignoring %s\n",
268 xkb_atom_text(info->ctx, type->name),
269 TypeMaskTxt(info, type),
270 ModMaskText(info->ctx, &info->mods, mods));
271 return false;
272 }
273
274 type->mods = mods;
275 return true;
276 }
277
278 /***====================================================================***/
279
280 static struct xkb_key_type_entry *
FindMatchingMapEntry(KeyTypeInfo * type,xkb_mod_mask_t mods)281 FindMatchingMapEntry(KeyTypeInfo *type, xkb_mod_mask_t mods)
282 {
283 struct xkb_key_type_entry *entry;
284
285 darray_foreach(entry, type->entries)
286 if (entry->mods.mods == mods)
287 return entry;
288
289 return NULL;
290 }
291
292 static bool
AddMapEntry(KeyTypesInfo * info,KeyTypeInfo * type,struct xkb_key_type_entry * new,bool clobber,bool report)293 AddMapEntry(KeyTypesInfo *info, KeyTypeInfo *type,
294 struct xkb_key_type_entry *new, bool clobber, bool report)
295 {
296 struct xkb_key_type_entry *old;
297
298 old = FindMatchingMapEntry(type, new->mods.mods);
299 if (old) {
300 if (report && old->level != new->level) {
301 log_warn(info->ctx,
302 "Multiple map entries for %s in %s; "
303 "Using %d, ignoring %d\n",
304 MapEntryTxt(info, new), TypeTxt(info, type),
305 (clobber ? new->level : old->level) + 1,
306 (clobber ? old->level : new->level) + 1);
307 }
308 else {
309 log_vrb(info->ctx, 10,
310 "Multiple occurrences of map[%s]= %d in %s; Ignored\n",
311 MapEntryTxt(info, new), new->level + 1,
312 TypeTxt(info, type));
313 return true;
314 }
315
316 if (clobber) {
317 if (new->level >= type->num_levels)
318 type->num_levels = new->level + 1;
319 old->level = new->level;
320 }
321
322 return true;
323 }
324
325 if (new->level >= type->num_levels)
326 type->num_levels = new->level + 1;
327
328 darray_append(type->entries, *new);
329 return true;
330 }
331
332 static bool
SetMapEntry(KeyTypesInfo * info,KeyTypeInfo * type,ExprDef * arrayNdx,ExprDef * value)333 SetMapEntry(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
334 ExprDef *value)
335 {
336 struct xkb_key_type_entry entry;
337
338 if (arrayNdx == NULL)
339 return ReportTypeShouldBeArray(info, type, "map entry");
340
341 if (!ExprResolveModMask(info->ctx, arrayNdx, MOD_BOTH, &info->mods,
342 &entry.mods.mods))
343 return ReportTypeBadType(info, type, "map entry", "modifier mask");
344
345 if (entry.mods.mods & (~type->mods)) {
346 log_vrb(info->ctx, 1,
347 "Map entry for unused modifiers in %s; "
348 "Using %s instead of %s\n",
349 TypeTxt(info, type),
350 ModMaskText(info->ctx, &info->mods,
351 entry.mods.mods & type->mods),
352 MapEntryTxt(info, &entry));
353 entry.mods.mods &= type->mods;
354 }
355
356 if (!ExprResolveLevel(info->ctx, value, &entry.level)) {
357 log_err(info->ctx,
358 "Level specifications in a key type must be integer; "
359 "Ignoring malformed level specification\n");
360 return false;
361 }
362
363 entry.preserve.mods = 0;
364
365 return AddMapEntry(info, type, &entry, true, true);
366 }
367
368 /***====================================================================***/
369
370 static bool
AddPreserve(KeyTypesInfo * info,KeyTypeInfo * type,xkb_mod_mask_t mods,xkb_mod_mask_t preserve_mods)371 AddPreserve(KeyTypesInfo *info, KeyTypeInfo *type,
372 xkb_mod_mask_t mods, xkb_mod_mask_t preserve_mods)
373 {
374 struct xkb_key_type_entry *entry;
375 struct xkb_key_type_entry new;
376
377 darray_foreach(entry, type->entries) {
378 if (entry->mods.mods != mods)
379 continue;
380
381 /* Map exists without previous preserve (or "None"); override. */
382 if (entry->preserve.mods == 0) {
383 entry->preserve.mods = preserve_mods;
384 return true;
385 }
386
387 /* Map exists with same preserve; do nothing. */
388 if (entry->preserve.mods == preserve_mods) {
389 log_vrb(info->ctx, 10,
390 "Identical definitions for preserve[%s] in %s; "
391 "Ignored\n",
392 ModMaskText(info->ctx, &info->mods, mods),
393 TypeTxt(info, type));
394 return true;
395 }
396
397 /* Map exists with different preserve; latter wins. */
398 log_vrb(info->ctx, 1,
399 "Multiple definitions for preserve[%s] in %s; "
400 "Using %s, ignoring %s\n",
401 ModMaskText(info->ctx, &info->mods, mods),
402 TypeTxt(info, type),
403 ModMaskText(info->ctx, &info->mods, preserve_mods),
404 ModMaskText(info->ctx, &info->mods, entry->preserve.mods));
405
406 entry->preserve.mods = preserve_mods;
407 return true;
408 }
409
410 /*
411 * Map does not exist, i.e. preserve[] came before map[].
412 * Create a map with the specified mask mapping to Level1. The level
413 * may be overridden later with an explicit map[] statement.
414 */
415 new.level = 0;
416 new.mods.mods = mods;
417 new.preserve.mods = preserve_mods;
418 darray_append(type->entries, new);
419 return true;
420 }
421
422 static bool
SetPreserve(KeyTypesInfo * info,KeyTypeInfo * type,ExprDef * arrayNdx,ExprDef * value)423 SetPreserve(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
424 ExprDef *value)
425 {
426 xkb_mod_mask_t mods, preserve_mods;
427
428 if (arrayNdx == NULL)
429 return ReportTypeShouldBeArray(info, type, "preserve entry");
430
431 if (!ExprResolveModMask(info->ctx, arrayNdx, MOD_BOTH, &info->mods, &mods))
432 return ReportTypeBadType(info, type, "preserve entry",
433 "modifier mask");
434
435 if (mods & ~type->mods) {
436 const char *before, *after;
437
438 before = ModMaskText(info->ctx, &info->mods, mods);
439 mods &= type->mods;
440 after = ModMaskText(info->ctx, &info->mods, mods);
441
442 log_vrb(info->ctx, 1,
443 "Preserve for modifiers not used by the %s type; "
444 "Index %s converted to %s\n",
445 TypeTxt(info, type), before, after);
446 }
447
448 if (!ExprResolveModMask(info->ctx, value, MOD_BOTH, &info->mods,
449 &preserve_mods)) {
450 log_err(info->ctx,
451 "Preserve value in a key type is not a modifier mask; "
452 "Ignoring preserve[%s] in type %s\n",
453 ModMaskText(info->ctx, &info->mods, mods),
454 TypeTxt(info, type));
455 return false;
456 }
457
458 if (preserve_mods & ~mods) {
459 const char *before, *after;
460
461 before = ModMaskText(info->ctx, &info->mods, preserve_mods);
462 preserve_mods &= mods;
463 after = ModMaskText(info->ctx, &info->mods, preserve_mods);
464
465 log_vrb(info->ctx, 1,
466 "Illegal value for preserve[%s] in type %s; "
467 "Converted %s to %s\n",
468 ModMaskText(info->ctx, &info->mods, mods),
469 TypeTxt(info, type), before, after);
470 }
471
472 return AddPreserve(info, type, mods, preserve_mods);
473 }
474
475 /***====================================================================***/
476
477 static bool
AddLevelName(KeyTypesInfo * info,KeyTypeInfo * type,xkb_level_index_t level,xkb_atom_t name,bool clobber)478 AddLevelName(KeyTypesInfo *info, KeyTypeInfo *type,
479 xkb_level_index_t level, xkb_atom_t name, bool clobber)
480 {
481 /* New name. */
482 if (level >= darray_size(type->level_names)) {
483 darray_resize0(type->level_names, level + 1);
484 goto finish;
485 }
486
487 /* Same level, same name. */
488 if (darray_item(type->level_names, level) == name) {
489 log_vrb(info->ctx, 10,
490 "Duplicate names for level %d of key type %s; Ignored\n",
491 level + 1, TypeTxt(info, type));
492 return true;
493 }
494
495 /* Same level, different name. */
496 if (darray_item(type->level_names, level) != XKB_ATOM_NONE) {
497 const char *old, *new;
498 old = xkb_atom_text(info->ctx,
499 darray_item(type->level_names, level));
500 new = xkb_atom_text(info->ctx, name);
501 log_vrb(info->ctx, 1,
502 "Multiple names for level %d of key type %s; "
503 "Using %s, ignoring %s\n",
504 level + 1, TypeTxt(info, type),
505 (clobber ? new : old), (clobber ? old : new));
506
507 if (!clobber)
508 return true;
509 }
510
511 /* XXX: What about different level, same name? */
512
513 finish:
514 darray_item(type->level_names, level) = name;
515 return true;
516 }
517
518 static bool
SetLevelName(KeyTypesInfo * info,KeyTypeInfo * type,ExprDef * arrayNdx,ExprDef * value)519 SetLevelName(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
520 ExprDef *value)
521 {
522 xkb_level_index_t level;
523 xkb_atom_t level_name;
524
525 if (arrayNdx == NULL)
526 return ReportTypeShouldBeArray(info, type, "level name");
527
528 if (!ExprResolveLevel(info->ctx, arrayNdx, &level))
529 return ReportTypeBadType(info, type, "level name", "integer");
530
531 if (!ExprResolveString(info->ctx, value, &level_name)) {
532 log_err(info->ctx,
533 "Non-string name for level %d in key type %s; "
534 "Ignoring illegal level name definition\n",
535 level + 1, xkb_atom_text(info->ctx, type->name));
536 return false;
537 }
538
539 return AddLevelName(info, type, level, level_name, true);
540 }
541
542 /***====================================================================***/
543
544 static bool
SetKeyTypeField(KeyTypesInfo * info,KeyTypeInfo * type,const char * field,ExprDef * arrayNdx,ExprDef * value)545 SetKeyTypeField(KeyTypesInfo *info, KeyTypeInfo *type,
546 const char *field, ExprDef *arrayNdx, ExprDef *value)
547 {
548 bool ok = false;
549 enum type_field type_field = 0;
550
551 if (istreq(field, "modifiers")) {
552 type_field = TYPE_FIELD_MASK;
553 ok = SetModifiers(info, type, arrayNdx, value);
554 }
555 else if (istreq(field, "map")) {
556 type_field = TYPE_FIELD_MAP;
557 ok = SetMapEntry(info, type, arrayNdx, value);
558 }
559 else if (istreq(field, "preserve")) {
560 type_field = TYPE_FIELD_PRESERVE;
561 ok = SetPreserve(info, type, arrayNdx, value);
562 }
563 else if (istreq(field, "levelname") || istreq(field, "level_name")) {
564 type_field = TYPE_FIELD_LEVEL_NAME;
565 ok = SetLevelName(info, type, arrayNdx, value);
566 } else {
567 log_err(info->ctx,
568 "Unknown field %s in key type %s; Definition ignored\n",
569 field, TypeTxt(info, type));
570 }
571
572 type->defined |= type_field;
573 return ok;
574 }
575
576 static bool
HandleKeyTypeBody(KeyTypesInfo * info,VarDef * def,KeyTypeInfo * type)577 HandleKeyTypeBody(KeyTypesInfo *info, VarDef *def, KeyTypeInfo *type)
578 {
579 bool ok = true;
580 const char *elem, *field;
581 ExprDef *arrayNdx;
582
583 for (; def; def = (VarDef *) def->common.next) {
584 ok = ExprResolveLhs(info->ctx, def->name, &elem, &field,
585 &arrayNdx);
586 if (!ok)
587 continue;
588
589 if (elem && istreq(elem, "type")) {
590 log_err(info->ctx,
591 "Support for changing the default type has been removed; "
592 "Statement ignored\n");
593 continue;
594 }
595
596 ok = SetKeyTypeField(info, type, field, arrayNdx, def->value);
597 }
598
599 return ok;
600 }
601
602 static bool
HandleKeyTypeDef(KeyTypesInfo * info,KeyTypeDef * def,enum merge_mode merge)603 HandleKeyTypeDef(KeyTypesInfo *info, KeyTypeDef *def, enum merge_mode merge)
604 {
605 KeyTypeInfo type = {
606 .defined = 0,
607 .merge = (def->merge == MERGE_DEFAULT ? merge : def->merge),
608 .name = def->name,
609 .mods = 0,
610 .num_levels = 1,
611 .entries = darray_new(),
612 .level_names = darray_new(),
613 };
614
615 if (!HandleKeyTypeBody(info, def->body, &type)) {
616 info->errorCount++;
617 return false;
618 }
619
620 if (!AddKeyType(info, &type, true)) {
621 info->errorCount++;
622 return false;
623 }
624
625 return true;
626 }
627
628 static void
HandleKeyTypesFile(KeyTypesInfo * info,XkbFile * file,enum merge_mode merge)629 HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge)
630 {
631 bool ok;
632
633 free(info->name);
634 info->name = strdup_safe(file->name);
635
636 for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
637 switch (stmt->type) {
638 case STMT_INCLUDE:
639 ok = HandleIncludeKeyTypes(info, (IncludeStmt *) stmt);
640 break;
641 case STMT_TYPE:
642 ok = HandleKeyTypeDef(info, (KeyTypeDef *) stmt, merge);
643 break;
644 case STMT_VAR:
645 log_err(info->ctx,
646 "Support for changing the default type has been removed; "
647 "Statement ignored\n");
648 ok = true;
649 break;
650 case STMT_VMOD:
651 ok = HandleVModDef(info->ctx, &info->mods, (VModDef *) stmt, merge);
652 break;
653 default:
654 log_err(info->ctx,
655 "Key type files may not include other declarations; "
656 "Ignoring %s\n", stmt_type_to_string(stmt->type));
657 ok = false;
658 break;
659 }
660
661 if (!ok)
662 info->errorCount++;
663
664 if (info->errorCount > 10) {
665 log_err(info->ctx,
666 "Abandoning keytypes file \"%s\"\n", file->name);
667 break;
668 }
669 }
670 }
671
672 /***====================================================================***/
673
674 static bool
CopyKeyTypesToKeymap(struct xkb_keymap * keymap,KeyTypesInfo * info)675 CopyKeyTypesToKeymap(struct xkb_keymap *keymap, KeyTypesInfo *info)
676 {
677 unsigned num_types;
678 struct xkb_key_type *types;
679
680 num_types = darray_empty(info->types) ? 1 : darray_size(info->types);
681 types = calloc(num_types, sizeof(*types));
682 if (!types)
683 return false;
684
685 /*
686 * If no types were specified, a default unnamed one-level type is
687 * used for all keys.
688 */
689 if (darray_empty(info->types)) {
690 struct xkb_key_type *type = &types[0];
691
692 type->mods.mods = 0;
693 type->num_levels = 1;
694 type->entries = NULL;
695 type->num_entries = 0;
696 type->name = xkb_atom_intern_literal(keymap->ctx, "default");
697 type->level_names = NULL;
698 type->num_level_names = 0;
699 }
700 else {
701 for (unsigned i = 0; i < num_types; i++) {
702 KeyTypeInfo *def = &darray_item(info->types, i);
703 struct xkb_key_type *type = &types[i];
704
705 type->name = def->name;
706 type->mods.mods = def->mods;
707 type->num_levels = def->num_levels;
708 darray_steal(def->level_names, &type->level_names, &type->num_level_names);
709 darray_steal(def->entries, &type->entries, &type->num_entries);
710 }
711 }
712
713 keymap->types_section_name = strdup_safe(info->name);
714 XkbEscapeMapName(keymap->types_section_name);
715 keymap->num_types = num_types;
716 keymap->types = types;
717 keymap->mods = info->mods;
718 return true;
719 }
720
721 /***====================================================================***/
722
723 bool
CompileKeyTypes(XkbFile * file,struct xkb_keymap * keymap,enum merge_mode merge)724 CompileKeyTypes(XkbFile *file, struct xkb_keymap *keymap,
725 enum merge_mode merge)
726 {
727 KeyTypesInfo info;
728
729 InitKeyTypesInfo(&info, keymap->ctx, &keymap->mods);
730
731 HandleKeyTypesFile(&info, file, merge);
732 if (info.errorCount != 0)
733 goto err_info;
734
735 if (!CopyKeyTypesToKeymap(keymap, &info))
736 goto err_info;
737
738 ClearKeyTypesInfo(&info);
739 return true;
740
741 err_info:
742 ClearKeyTypesInfo(&info);
743 return false;
744 }
745