Use xkb_led_index_t throughout
[platform/upstream/libxkbcommon.git] / src / xkbcomp / keycodes.c
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 "xkbcomp-priv.h"
28 #include "expr.h"
29 #include "parseutils.h"
30
31 typedef struct _AliasInfo {
32     enum merge_mode merge;
33     unsigned file_id;
34     struct list entry;
35
36     char alias[XkbKeyNameLength + 1];
37     char real[XkbKeyNameLength + 1];
38 } AliasInfo;
39
40 typedef struct _IndicatorNameInfo {
41     enum merge_mode merge;
42     unsigned file_id;
43     struct list entry;
44
45     xkb_led_index_t ndx;
46     xkb_atom_t name;
47     bool virtual;
48 } IndicatorNameInfo;
49
50 typedef struct _KeyNamesInfo {
51     char *name;     /* e.g. evdev+aliases(qwerty) */
52     int errorCount;
53     unsigned file_id;
54     enum merge_mode merge;
55     xkb_keycode_t computedMin; /* lowest keycode stored */
56     xkb_keycode_t computedMax; /* highest keycode stored */
57     xkb_keycode_t explicitMin;
58     xkb_keycode_t explicitMax;
59     darray(unsigned long) names;
60     darray(unsigned int) files;
61     struct list leds;
62     struct list aliases;
63
64     struct xkb_keymap *keymap;
65 } KeyNamesInfo;
66
67 static void
68 ResizeKeyNameArrays(KeyNamesInfo *info, int newMax)
69 {
70     if (newMax < darray_size(info->names))
71         return;
72
73     darray_resize0(info->names, newMax + 1);
74     darray_resize0(info->files, newMax + 1);
75 }
76
77 static void
78 InitAliasInfo(AliasInfo *info, enum merge_mode merge, unsigned file_id,
79               char *alias, char *real)
80 {
81     memset(info, 0, sizeof(*info));
82     info->merge = merge;
83     info->file_id = file_id;
84     strncpy(info->alias, alias, XkbKeyNameLength);
85     strncpy(info->real, real, XkbKeyNameLength);
86 }
87
88 static void
89 InitIndicatorNameInfo(IndicatorNameInfo * ii, KeyNamesInfo * info)
90 {
91     ii->merge = info->merge;
92     ii->file_id = info->file_id;
93     ii->ndx = 0;
94     ii->name = XKB_ATOM_NONE;
95     ii->virtual = false;
96 }
97
98 static IndicatorNameInfo *
99 NextIndicatorName(KeyNamesInfo * info)
100 {
101     IndicatorNameInfo *ii;
102
103     ii = malloc(sizeof(*ii));
104     if (!ii)
105         return NULL;
106
107     InitIndicatorNameInfo(ii, info);
108     list_append(&ii->entry, &info->leds);
109
110     return ii;
111 }
112
113 static IndicatorNameInfo *
114 FindIndicatorByIndex(KeyNamesInfo * info, xkb_led_index_t ndx)
115 {
116     IndicatorNameInfo *old;
117
118     list_foreach(old, &info->leds, entry)
119         if (old->ndx == ndx)
120             return old;
121
122     return NULL;
123 }
124
125 static IndicatorNameInfo *
126 FindIndicatorByName(KeyNamesInfo * info, xkb_atom_t name)
127 {
128     IndicatorNameInfo *old;
129
130     list_foreach(old, &info->leds, entry)
131         if (old->name == name)
132             return old;
133
134     return NULL;
135 }
136
137 static bool
138 AddIndicatorName(KeyNamesInfo *info, enum merge_mode merge,
139                  IndicatorNameInfo *new)
140 {
141     IndicatorNameInfo *old;
142     bool replace;
143     int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
144
145     replace = (merge == MERGE_REPLACE) || (merge == MERGE_OVERRIDE);
146
147     old = FindIndicatorByName(info, new->name);
148     if (old) {
149         if ((old->file_id == new->file_id && verbosity > 0) ||
150             verbosity > 9) {
151             if (old->ndx == new->ndx) {
152                 if (old->virtual != new->virtual) {
153                     if (replace)
154                         old->virtual = new->virtual;
155                     log_warn(info->keymap->ctx, "Multiple indicators named %s; "
156                              "Using %s instead of %s\n",
157                              xkb_atom_text(info->keymap->ctx, new->name),
158                              (old->virtual ? "virtual" : "real"),
159                              (old->virtual ? "real" : "virtual"));
160                 }
161                 else {
162                     log_warn(info->keymap->ctx, "Multiple indicators named %s; "
163                              "Identical definitions ignored\n",
164                              xkb_atom_text(info->keymap->ctx, new->name));
165                 }
166                 return true;
167             }
168             else {
169                 log_warn(info->keymap->ctx, "Multiple indicators named %s; "
170                          "Using %d, ignoring %d\n",
171                          xkb_atom_text(info->keymap->ctx, new->name),
172                          (replace ? old->ndx : new->ndx),
173                          (replace ? new->ndx : old->ndx));
174             }
175
176             if (replace) {
177                 list_del(&old->entry);
178                 free(old);
179             }
180         }
181     }
182
183     old = FindIndicatorByIndex(info, new->ndx);
184     if (old) {
185         if ((old->file_id == new->file_id && verbosity > 0) ||
186             verbosity > 9) {
187             if (old->name == new->name && old->virtual == new->virtual) {
188                 log_warn(info->keymap->ctx,
189                          "Multiple names for indicator %d; "
190                          "Identical definitions ignored\n", new->ndx);
191             } else {
192                 const char *oldType, *newType;
193                 xkb_atom_t using, ignoring;
194                 if (old->virtual)
195                     oldType = "virtual indicator";
196                 else
197                     oldType = "real indicator";
198                 if (new->virtual)
199                     newType = "virtual indicator";
200                 else
201                     newType = "real indicator";
202                 if (replace) {
203                     using = new->name;
204                     ignoring = old->name;
205                 }
206                 else {
207                     using = old->name;
208                     ignoring = new->name;
209                 }
210                 log_warn(info->keymap->ctx,
211                          "Multiple names for indicator %d; "
212                          "Using %s %s, ignoring %s %s\n",
213                          new->ndx,
214                          oldType, xkb_atom_text(info->keymap->ctx, using),
215                          newType, xkb_atom_text(info->keymap->ctx, ignoring));
216             }
217         }
218         if (replace) {
219             old->name = new->name;
220             old->virtual = new->virtual;
221         }
222         return true;
223     }
224     old = new;
225     new = NextIndicatorName(info);
226     if (!new) {
227         log_wsgo(info->keymap->ctx,
228                  "Couldn't allocate name for indicator %d; Ignored\n",
229                  old->ndx);
230         return false;
231     }
232     new->name = old->name;
233     new->ndx = old->ndx;
234     new->virtual = old->virtual;
235     return true;
236 }
237
238 static void
239 ClearKeyNamesInfo(KeyNamesInfo * info)
240 {
241     AliasInfo *alias, *next_alias;
242     IndicatorNameInfo *ii, *next_ii;
243
244     free(info->name);
245     info->name = NULL;
246     info->merge = MERGE_DEFAULT;
247     info->computedMax = info->explicitMax = info->explicitMin = 0;
248     info->computedMin = XKB_KEYCODE_MAX;
249     darray_free(info->names);
250     darray_free(info->files);
251     list_foreach_safe(ii, next_ii, &info->leds, entry)
252         free(ii);
253     list_init(&info->leds);
254     list_foreach_safe(alias, next_alias, &info->aliases, entry)
255         free(alias);
256     list_init(&info->aliases);
257 }
258
259 static void
260 InitKeyNamesInfo(KeyNamesInfo *info, struct xkb_keymap *keymap,
261                  unsigned file_id)
262 {
263     info->name = NULL;
264     info->merge = MERGE_DEFAULT;
265     list_init(&info->leds);
266     list_init(&info->aliases);
267     info->file_id = file_id;
268     darray_init(info->names);
269     darray_init(info->files);
270     ClearKeyNamesInfo(info);
271     info->errorCount = 0;
272     info->keymap = keymap;
273 }
274
275 static int
276 FindKeyByLong(KeyNamesInfo * info, unsigned long name)
277 {
278     uint64_t i;
279
280     for (i = info->computedMin; i <= info->computedMax; i++)
281         if (darray_item(info->names, i) == name)
282             return i;
283
284     return 0;
285 }
286
287 /**
288  * Store the name of the key as a long in the info struct under the given
289  * keycode. If the same keys is referred to twice, print a warning.
290  * Note that the key's name is stored as a long, the keycode is the index.
291  */
292 static bool
293 AddKeyName(KeyNamesInfo * info,
294            xkb_keycode_t kc, char *name, enum merge_mode merge,
295            unsigned file_id, bool reportCollisions)
296 {
297     xkb_keycode_t old;
298     unsigned long lval;
299     int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
300
301     ResizeKeyNameArrays(info, kc);
302
303     if (kc < info->computedMin)
304         info->computedMin = kc;
305     if (kc > info->computedMax)
306         info->computedMax = kc;
307     lval = KeyNameToLong(name);
308
309     if (reportCollisions)
310         reportCollisions = (verbosity > 7 ||
311                             (verbosity > 0 &&
312                              file_id == darray_item(info->files, kc)));
313
314     if (darray_item(info->names, kc) != 0) {
315         char buf[6];
316
317         LongToKeyName(darray_item(info->names, kc), buf);
318         buf[4] = '\0';
319         if (darray_item(info->names, kc) == lval && reportCollisions) {
320             log_warn(info->keymap->ctx,
321                      "Multiple identical key name definitions; "
322                      "Later occurences of \"<%s> = %d\" ignored\n", buf, kc);
323             return true;
324         }
325
326         if (merge == MERGE_AUGMENT) {
327             if (reportCollisions)
328                 log_warn(info->keymap->ctx,
329                          "Multiple names for keycode %d; "
330                          "Using <%s>, ignoring <%s>\n", kc, buf, name);
331             return true;
332         }
333         else {
334             if (reportCollisions)
335                 log_warn(info->keymap->ctx,
336                          "Multiple names for keycode %d; "
337                          "Using <%s>, ignoring <%s>\n", kc, name, buf);
338             darray_item(info->names, kc) = 0;
339             darray_item(info->files, kc) = 0;
340         }
341     }
342
343     old = FindKeyByLong(info, lval);
344     if ((old != 0) && (old != kc)) {
345         if (merge == MERGE_OVERRIDE) {
346             darray_item(info->names, old) = 0;
347             darray_item(info->files, old) = 0;
348             if (reportCollisions)
349                 log_warn(info->keymap->ctx,
350                          "Key name <%s> assigned to multiple keys; "
351                          "Using %d, ignoring %d\n", name, kc, old);
352         }
353         else {
354             if (reportCollisions && verbosity > 3)
355                 log_warn(info->keymap->ctx,
356                          "Key name <%s> assigned to multiple keys; "
357                          "Using %d, ignoring %d\n", name, old, kc);
358             return true;
359         }
360     }
361     darray_item(info->names, kc) = lval;
362     darray_item(info->files, kc) = file_id;
363     return true;
364 }
365
366 /***====================================================================***/
367
368 static int
369 HandleAliasDef(KeyNamesInfo *info, KeyAliasDef *def, enum merge_mode merge,
370                unsigned file_id);
371
372 static bool
373 MergeAliases(KeyNamesInfo *into, KeyNamesInfo *from, enum merge_mode merge)
374 {
375     AliasInfo *alias, *next;
376     KeyAliasDef def;
377
378     if (list_empty(&from->aliases))
379         return true;
380
381     if (list_empty(&into->aliases)) {
382         list_replace(&from->aliases, &into->aliases);
383         list_init(&from->aliases);
384         return true;
385     }
386
387     memset(&def, 0, sizeof(def));
388
389     list_foreach_safe(alias, next, &from->aliases, entry) {
390         def.merge = (merge == MERGE_DEFAULT) ? alias->merge : merge;
391         memcpy(def.alias, alias->alias, XkbKeyNameLength);
392         memcpy(def.real, alias->real, XkbKeyNameLength);
393
394         if (!HandleAliasDef(into, &def, def.merge, alias->file_id))
395             return false;
396     }
397
398     return true;
399 }
400
401 static void
402 MergeIncludedKeycodes(KeyNamesInfo *into, KeyNamesInfo *from,
403                       enum merge_mode merge)
404 {
405     uint64_t i;
406     char buf[5];
407     IndicatorNameInfo *led;
408
409     if (from->errorCount > 0) {
410         into->errorCount += from->errorCount;
411         return;
412     }
413     if (into->name == NULL) {
414         into->name = from->name;
415         from->name = NULL;
416     }
417
418     ResizeKeyNameArrays(into, from->computedMax);
419
420     for (i = from->computedMin; i <= from->computedMax; i++) {
421         if (darray_item(from->names, i) == 0)
422             continue;
423         LongToKeyName(darray_item(from->names, i), buf);
424         buf[4] = '\0';
425         if (!AddKeyName(into, i, buf, merge, from->file_id, false))
426             into->errorCount++;
427     }
428
429     list_foreach(led, &from->leds, entry) {
430         led->merge = (merge == MERGE_DEFAULT ? led->merge : merge);
431         if (!AddIndicatorName(into, led->merge, led))
432             into->errorCount++;
433     }
434
435     if (!MergeAliases(into, from, merge))
436         into->errorCount++;
437     if (from->explicitMin != 0) {
438         if ((into->explicitMin == 0)
439             || (into->explicitMin > from->explicitMin))
440             into->explicitMin = from->explicitMin;
441     }
442     if (from->explicitMax > 0) {
443         if ((into->explicitMax == 0)
444             || (into->explicitMax < from->explicitMax))
445             into->explicitMax = from->explicitMax;
446     }
447 }
448
449 static void
450 HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge);
451
452 /**
453  * Handle the given include statement (e.g. "include "evdev+aliases(qwerty)").
454  *
455  * @param info Struct to store the key info in.
456  * @param stmt The include statement from the keymap file.
457  */
458 static bool
459 HandleIncludeKeycodes(KeyNamesInfo *info, IncludeStmt *stmt)
460 {
461     enum merge_mode merge = MERGE_DEFAULT;
462     XkbFile *rtrn;
463     KeyNamesInfo included, next_incl;
464
465     /* XXX: What's that? */
466     if (stmt->file && streq(stmt->file, "computed")) {
467         info->keymap->flags |= AutoKeyNames;
468         info->explicitMin = 0;
469         info->explicitMax = XKB_KEYCODE_MAX;
470         return (info->errorCount == 0);
471     }
472
473     InitKeyNamesInfo(&included, info->keymap, info->file_id);
474     if (stmt->stmt) {
475         free(included.name);
476         included.name = stmt->stmt;
477         stmt->stmt = NULL;
478     }
479
480     for (; stmt; stmt = stmt->next_incl) {
481         if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_KEYCODES,
482                                 &rtrn, &merge)) {
483             info->errorCount += 10;
484             ClearKeyNamesInfo(&included);
485             return false;
486         }
487
488         InitKeyNamesInfo(&next_incl, info->keymap, rtrn->id);
489
490         HandleKeycodesFile(&next_incl, rtrn, MERGE_OVERRIDE);
491
492         MergeIncludedKeycodes(&included, &next_incl, merge);
493
494         ClearKeyNamesInfo(&next_incl);
495         FreeXKBFile(rtrn);
496     }
497
498     MergeIncludedKeycodes(info, &included, merge);
499     ClearKeyNamesInfo(&included);
500
501     return (info->errorCount == 0);
502 }
503
504 /**
505  * Parse the given statement and store the output in the info struct.
506  * e.g. <ESC> = 9
507  */
508 static int
509 HandleKeycodeDef(KeyNamesInfo *info, KeycodeDef *stmt, enum merge_mode merge)
510 {
511     if ((info->explicitMin != 0 && stmt->value < info->explicitMin) ||
512         (info->explicitMax != 0 && stmt->value > info->explicitMax)) {
513         log_err(info->keymap->ctx,
514                 "Illegal keycode %lu for name <%s>; "
515                 "Must be in the range %d-%d inclusive\n",
516                 stmt->value, stmt->name, info->explicitMin,
517                 info->explicitMax ? info->explicitMax : XKB_KEYCODE_MAX);
518         return 0;
519     }
520     if (stmt->merge != MERGE_DEFAULT) {
521         if (stmt->merge == MERGE_REPLACE)
522             merge = MERGE_OVERRIDE;
523         else
524             merge = stmt->merge;
525     }
526     return AddKeyName(info, stmt->value, stmt->name, merge, info->file_id,
527                       true);
528 }
529
530 static void
531 HandleAliasCollision(KeyNamesInfo *info, AliasInfo *old, AliasInfo *new)
532 {
533     int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
534
535     if (strncmp(new->real, old->real, XkbKeyNameLength) == 0) {
536         if ((new->file_id == old->file_id && verbosity > 0) || verbosity > 9)
537             log_warn(info->keymap->ctx,
538                      "Alias of %s for %s declared more than once; "
539                      "First definition ignored\n",
540                      KeyNameText(new->alias), KeyNameText(new->real));
541     }
542     else {
543         char *use, *ignore;
544
545         if (new->merge == MERGE_AUGMENT) {
546             use = old->real;
547             ignore = new->real;
548         }
549         else {
550             use = new->real;
551             ignore = old->real;
552         }
553
554         if ((old->file_id == new->file_id && verbosity > 0) || verbosity > 9)
555             log_warn(info->keymap->ctx,
556                      "Multiple definitions for alias %s; "
557                      "Using %s, ignoring %s\n",
558                      KeyNameText(old->alias), KeyNameText(use),
559                      KeyNameText(ignore));
560
561         if (use != old->real)
562             memcpy(old->real, use, XkbKeyNameLength);
563     }
564
565     old->file_id = new->file_id;
566     old->merge = new->merge;
567 }
568
569 static int
570 HandleAliasDef(KeyNamesInfo *info, KeyAliasDef *def, enum merge_mode merge,
571                unsigned file_id)
572 {
573     AliasInfo *alias;
574
575     list_foreach(alias, &info->aliases, entry) {
576         if (strncmp(alias->alias, def->alias, XkbKeyNameLength) == 0) {
577             AliasInfo new;
578             InitAliasInfo(&new, merge, file_id, def->alias, def->real);
579             HandleAliasCollision(info, alias, &new);
580             return true;
581         }
582     }
583
584     alias = calloc(1, sizeof(*alias));
585     if (!alias) {
586         log_wsgo(info->keymap->ctx, "Allocation failure in HandleAliasDef\n");
587         return false;
588     }
589
590     alias->file_id = file_id;
591     alias->merge = merge;
592     memcpy(alias->alias, def->alias, XkbKeyNameLength);
593     memcpy(alias->real, def->real, XkbKeyNameLength);
594     list_append(&alias->entry, &info->aliases);
595
596     return true;
597 }
598
599 #define MIN_KEYCODE_DEF 0
600 #define MAX_KEYCODE_DEF 1
601
602 /**
603  * Handle the minimum/maximum statement of the xkb file.
604  * Sets explicitMin/Max of the info struct.
605  *
606  * @return 1 on success, 0 otherwise.
607  */
608 static int
609 HandleKeyNameVar(KeyNamesInfo *info, VarDef *stmt)
610 {
611     const char *elem, *field;
612     xkb_keycode_t kc;
613     ExprDef *arrayNdx;
614     int which;
615
616     if (!ExprResolveLhs(info->keymap->ctx, stmt->name, &elem, &field,
617                         &arrayNdx))
618         return false;               /* internal error, already reported */
619
620     if (elem) {
621         log_err(info->keymap->ctx,
622                 "Unknown element %s encountered; "
623                 "Default for field %s ignored\n", elem, field);
624         return false;
625     }
626
627     if (istreq(field, "minimum"))
628         which = MIN_KEYCODE_DEF;
629     else if (istreq(field, "maximum"))
630         which = MAX_KEYCODE_DEF;
631     else {
632         log_err(info->keymap->ctx,
633                 "Unknown field encountered; "
634                 "Assigment to field %s ignored\n", field);
635         return false;
636     }
637
638     if (arrayNdx != NULL) {
639         log_err(info->keymap->ctx,
640                 "The %s setting is not an array; "
641                 "Illegal array reference ignored\n", field);
642         return false;
643     }
644
645     if (ExprResolveKeyCode(info->keymap->ctx, stmt->value, &kc) == 0) {
646         log_err(info->keymap->ctx,
647                 "Illegal keycode encountered; "
648                 "Assignment to field %s ignored\n", field);
649         return false;
650     }
651
652     if (kc > XKB_KEYCODE_MAX) {
653         log_err(info->keymap->ctx,
654                 "Illegal keycode %d (must be in the range %d-%d inclusive); "
655                 "Value of \"%s\" not changed\n",
656                 kc, 0, XKB_KEYCODE_MAX, field);
657         return false;
658     }
659
660     if (which == MIN_KEYCODE_DEF) {
661         if (info->explicitMax > 0 && info->explicitMax < kc) {
662             log_err(info->keymap->ctx,
663                     "Minimum key code (%d) must be <= maximum key code (%d); "
664                     "Minimum key code value not changed\n",
665                     kc, info->explicitMax);
666             return false;
667         }
668
669         if (info->computedMax > 0 && info->computedMin < kc) {
670             log_err(info->keymap->ctx,
671                     "Minimum key code (%d) must be <= lowest defined key (%d); "
672                     "Minimum key code value not changed\n",
673                     kc, info->computedMin);
674             return false;
675         }
676
677         info->explicitMin = kc;
678     }
679
680     if (which == MAX_KEYCODE_DEF) {
681         if (info->explicitMin > 0 && info->explicitMin > kc) {
682             log_err(info->keymap->ctx,
683                     "Maximum code (%d) must be >= minimum key code (%d); "
684                     "Maximum code value not changed\n",
685                     kc, info->explicitMin);
686             return false;
687         }
688
689         if (info->computedMax > 0 && info->computedMax > kc) {
690             log_err(info->keymap->ctx,
691                     "Maximum code (%d) must be >= highest defined key (%d); "
692                     "Maximum code value not changed\n",
693                     kc, info->computedMax);
694             return false;
695         }
696
697         info->explicitMax = kc;
698     }
699
700     return true;
701 }
702
703 static int
704 HandleIndicatorNameDef(KeyNamesInfo *info, IndicatorNameDef *def,
705                        enum merge_mode merge)
706 {
707     IndicatorNameInfo ii;
708     const char *str;
709
710     if (def->ndx < 1 || def->ndx > XkbNumIndicators) {
711         info->errorCount++;
712         log_err(info->keymap->ctx,
713                 "Name specified for illegal indicator index %d\n; Ignored\n",
714                 def->ndx);
715         return false;
716     }
717
718     InitIndicatorNameInfo(&ii, info);
719     ii.ndx = (xkb_led_index_t) def->ndx;
720
721     if (!ExprResolveString(info->keymap->ctx, def->name, &str)) {
722         char buf[20];
723         snprintf(buf, sizeof(buf), "%d", def->ndx);
724         info->errorCount++;
725         return ReportBadType(info->keymap, "indicator", "name", buf,
726                              "string");
727     }
728
729     ii.name = xkb_atom_intern(info->keymap->ctx, str);
730     ii.virtual = def->virtual;
731
732     return AddIndicatorName(info, merge, &ii);
733 }
734
735 /**
736  * Handle the xkb_keycodes section of a xkb file.
737  * All information about parsed keys is stored in the info struct.
738  *
739  * Such a section may have include statements, in which case this function is
740  * semi-recursive (it calls HandleIncludeKeycodes, which may call
741  * HandleKeycodesFile again).
742  *
743  * @param info Struct to contain the fully parsed key information.
744  * @param file The input file (parsed xkb_keycodes section)
745  * @param merge Merge strategy (MERGE_OVERRIDE, etc.)
746  */
747 static void
748 HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge)
749 {
750     ParseCommon *stmt;
751
752     free(info->name);
753     info->name = strdup_safe(file->name);
754     stmt = file->defs;
755     while (stmt)
756     {
757         switch (stmt->type) {
758         case STMT_INCLUDE:    /* e.g. include "evdev+aliases(qwerty)" */
759             if (!HandleIncludeKeycodes(info, (IncludeStmt *) stmt))
760                 info->errorCount++;
761             break;
762         case STMT_KEYCODE: /* e.g. <ESC> = 9; */
763             if (!HandleKeycodeDef(info, (KeycodeDef *) stmt, merge))
764                 info->errorCount++;
765             break;
766         case STMT_ALIAS: /* e.g. alias <MENU> = <COMP>; */
767             if (!HandleAliasDef(info, (KeyAliasDef *) stmt, merge,
768                                 info->file_id))
769                 info->errorCount++;
770             break;
771         case STMT_VAR: /* e.g. minimum, maximum */
772             if (!HandleKeyNameVar(info, (VarDef *) stmt))
773                 info->errorCount++;
774             break;
775         case STMT_INDICATOR_NAME: /* e.g. indicator 1 = "Caps Lock"; */
776             if (!HandleIndicatorNameDef(info, (IndicatorNameDef *) stmt,
777                                         merge))
778                 info->errorCount++;
779             break;
780         case STMT_INTERP:
781         case STMT_VMOD:
782             log_err(info->keymap->ctx,
783                     "Keycode files may define key and indicator names only; "
784                     "Ignoring definition of %s\n",
785                     (stmt->type == STMT_INTERP ?
786                      "a symbol interpretation" : "virtual modifiers"));
787             info->errorCount++;
788             break;
789         default:
790             log_wsgo(info->keymap->ctx,
791                      "Unexpected statement type %d in HandleKeycodesFile\n",
792                      stmt->type);
793             break;
794         }
795         stmt = stmt->next;
796         if (info->errorCount > 10) {
797             log_err(info->keymap->ctx, "Abandoning keycodes file \"%s\"\n",
798                     file->topName);
799             break;
800         }
801     }
802 }
803
804 static int
805 ApplyAliases(KeyNamesInfo *info)
806 {
807     int i;
808     struct xkb_key *key;
809     struct xkb_key_alias *old, *a;
810     AliasInfo *alias, *next;
811     int nNew = 0, nOld;
812     struct xkb_keymap *keymap = info->keymap;
813
814     nOld = darray_size(keymap->key_aliases);
815     old = &darray_item(keymap->key_aliases, 0);
816
817     list_foreach(alias, &info->aliases, entry) {
818         unsigned long lname;
819
820         lname = KeyNameToLong(alias->real);
821         key = FindNamedKey(keymap, lname, false, CreateKeyNames(keymap), 0);
822         if (!key) {
823             log_lvl(info->keymap->ctx, 5,
824                     "Attempt to alias %s to non-existent key %s; Ignored\n",
825                     KeyNameText(alias->alias), KeyNameText(alias->real));
826             alias->alias[0] = '\0';
827             continue;
828         }
829
830         lname = KeyNameToLong(alias->alias);
831         key = FindNamedKey(keymap, lname, false, false, 0);
832         if (key) {
833             log_lvl(info->keymap->ctx, 5,
834                     "Attempt to create alias with the name of a real key; "
835                     "Alias \"%s = %s\" ignored\n",
836                     KeyNameText(alias->alias), KeyNameText(alias->real));
837             alias->alias[0] = '\0';
838             continue;
839         }
840
841         nNew++;
842
843         if (!old)
844             continue;
845
846         for (i = 0, a = old; i < nOld; i++, a++) {
847             AliasInfo old_alias;
848
849             if (strncmp(a->alias, alias->alias, XkbKeyNameLength) != 0)
850                 continue;
851
852             InitAliasInfo(&old_alias, MERGE_AUGMENT, 0, a->alias, a->real);
853             HandleAliasCollision(info, &old_alias, alias);
854             memcpy(old_alias.real, a->real, XkbKeyNameLength);
855             alias->alias[0] = '\0';
856             nNew--;
857             break;
858         }
859     }
860
861     if (nNew == 0)
862         goto out;
863
864     darray_resize0(keymap->key_aliases, nOld + nNew);
865
866     a = &darray_item(keymap->key_aliases, nOld);
867     list_foreach(alias, &info->aliases, entry) {
868         if (alias->alias[0] != '\0') {
869             strncpy(a->alias, alias->alias, XkbKeyNameLength);
870             strncpy(a->real, alias->real, XkbKeyNameLength);
871             a++;
872         }
873     }
874
875 out:
876     list_foreach_safe(alias, next, &info->aliases, entry)
877         free(alias);
878     list_init(&info->aliases);
879     return true;
880 }
881
882 /**
883  * Compile the xkb_keycodes section, parse it's output, return the results.
884  *
885  * @param file The parsed XKB file (may have include statements requiring
886  * further parsing)
887  * @param result The effective keycodes, as gathered from the file.
888  * @param merge Merge strategy.
889  *
890  * @return true on success, false otherwise.
891  */
892 bool
893 CompileKeycodes(XkbFile *file, struct xkb_keymap *keymap,
894                 enum merge_mode merge)
895 {
896     xkb_keycode_t kc;
897     KeyNamesInfo info; /* contains all the info after parsing */
898     IndicatorNameInfo *ii;
899
900     InitKeyNamesInfo(&info, keymap, file->id);
901
902     HandleKeycodesFile(&info, file, merge);
903
904     /* all the keys are now stored in info */
905
906     if (info.errorCount != 0)
907         goto err_info;
908
909     if (info.explicitMin > 0) /* if "minimum" statement was present */
910         keymap->min_key_code = info.explicitMin;
911     else
912         keymap->min_key_code = info.computedMin;
913
914     if (info.explicitMax > 0) /* if "maximum" statement was present */
915         keymap->max_key_code = info.explicitMax;
916     else
917         keymap->max_key_code = info.computedMax;
918
919     darray_resize0(keymap->keys, keymap->max_key_code + 1);
920     for (kc = info.computedMin; kc <= info.computedMax; kc++)
921         LongToKeyName(darray_item(info.names, kc),
922                       XkbKey(keymap, kc)->name);
923
924     if (info.name)
925         keymap->keycodes_section_name = strdup(info.name);
926
927     list_foreach(ii, &info.leds, entry) {
928         keymap->indicator_names[ii->ndx - 1] =
929             xkb_atom_text(keymap->ctx, ii->name);
930     }
931
932     ApplyAliases(&info);
933
934     ClearKeyNamesInfo(&info);
935     return true;
936
937 err_info:
938     ClearKeyNamesInfo(&info);
939     return false;
940 }