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