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