keycodes: rename computedMin/Max to min/max_key_code
[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 "text.h"
29 #include "expr.h"
30 #include "keycodes.h"
31 #include "include.h"
32
33 /*
34  * The xkb_keycodes section
35  * ========================
36  *
37  * This is the simplest section type, and is the first one to be
38  * compiled. The purpose of this is mostly to map between the
39  * hardware/evdev scancodes and xkb keycodes. Each key is given a name
40  * of up to 4 letters, by which it can be referred to later, e.g. in the
41  * symbols section.
42  *
43  * Keycode statements
44  * ------------------
45  * Statements of the form:
46  *      <TLDE> = 49;
47  *      <AE01> = 10;
48  *
49  * The above would let 49 and 10 be valid keycodes in the keymap, and
50  * assign them the names TLDE and AE01 respectively. The format <WXYZ> is
51  * always used to refer to a key by name.
52  *
53  * [ The naming convention <AE01> just denoted the position of the key
54  * in the main alphanumric section of the keyboard, with the top left key
55  * (usually "~") acting as origin. <AE01> is the key in the first row
56  * second column (which is usually "1"). ]
57  *
58  * In the common case this just maps to the evdev scancodes from
59  * /usr/include/linux/input.h, e.g. the following definitions:
60  *      #define KEY_GRAVE            41
61  *      #define KEY_1                2
62  * Similar definitions appear in the xf86-input-keyboard driver. Note
63  * that in all current keymaps there's a constant offset of 8 (for
64  * historical reasons).
65  *
66  * If there's a conflict, like the same name given to different keycodes,
67  * or same keycode given different names, it is resolved according to the
68  * merge mode which applies to the definitions.
69  *
70  * The reason for the 4 characters limit is that the name is sometimes
71  * converted to an unsigned long (in a direct mapping), instead of a char
72  * array (see KeyNameToLong, LongToKeyName).
73  *
74  * Alias statements
75  * ----------------
76  * Statements of the form:
77  *      alias <MENU> = <COMP>;
78  *
79  * Allows to refer to a previously defined key (here <COMP>) by another
80  * name (here <MENU>). Conflicts are handled similarly.
81  *
82  * Indicator name statements
83  * -------------------------
84  * Statements of the form:
85  *      indicator 1 = "Caps Lock";
86  *      indicator 2 = "Num Lock";
87  *      indicator 3 = "Scroll Lock";
88  *
89  * Assigns a name the indicator (i.e. keyboard LED) with the given index.
90  * The amount of possible indicators is predetermined (XKB_NUM_INDICATORS).
91  * The indicator may be referred by this name later in the compat section
92  * and by the user.
93  *
94  * Effect on the keymap
95  * --------------------
96  * After all of the xkb_keycodes sections have been compiled, the
97  * following members of struct xkb_keymap are finalized:
98  *      xkb_keycode_t min_key_code;
99  *      xkb_keycode_t max_key_code;
100  *      darray(struct xkb_key_alias) key_aliases;
101  *      char *keycodes_section_name;
102  * The 'name' field of indicators declared in xkb_keycodes:
103  *      struct xkb_indicator_map indicators[XKB_NUM_INDICATORS];
104  * Further, the array of keys:
105  *      darray(struct xkb_key) keys;
106  * had been resized to its final size (i.e. all of the xkb_key objects are
107  * referable by their keycode). However the objects themselves do not
108  * contain any useful information besides the key name at this point.
109  */
110
111 typedef struct _AliasInfo {
112     enum merge_mode merge;
113     unsigned file_id;
114
115     unsigned long alias;
116     unsigned long real;
117 } AliasInfo;
118
119 typedef struct _IndicatorNameInfo {
120     enum merge_mode merge;
121     unsigned file_id;
122
123     xkb_atom_t name;
124 } IndicatorNameInfo;
125
126 typedef struct _KeyNamesInfo {
127     char *name;     /* e.g. evdev+aliases(qwerty) */
128     int errorCount;
129     unsigned file_id;
130     enum merge_mode merge;
131
132     xkb_keycode_t min_key_code;
133     xkb_keycode_t max_key_code;
134     darray(unsigned long) names;
135     darray(unsigned int) files;
136     IndicatorNameInfo indicator_names[XKB_NUM_INDICATORS];
137     darray(AliasInfo) aliases;
138
139     struct xkb_context *ctx;
140 } KeyNamesInfo;
141
142 static void
143 ResizeKeyNameArrays(KeyNamesInfo *info, int newMax)
144 {
145     if (newMax < darray_size(info->names))
146         return;
147
148     darray_resize0(info->names, newMax + 1);
149     darray_resize0(info->files, newMax + 1);
150 }
151
152 static void
153 InitAliasInfo(AliasInfo *info, enum merge_mode merge, unsigned file_id,
154               char alias[XKB_KEY_NAME_LENGTH], char real[XKB_KEY_NAME_LENGTH])
155 {
156     memset(info, 0, sizeof(*info));
157     info->merge = merge;
158     info->file_id = file_id;
159     info->alias = KeyNameToLong(alias);
160     info->real = KeyNameToLong(real);
161 }
162
163 static IndicatorNameInfo *
164 FindIndicatorByName(KeyNamesInfo *info, xkb_atom_t name,
165                     xkb_led_index_t *idx_out)
166 {
167     xkb_led_index_t idx;
168
169     for (idx = 0; idx < XKB_NUM_INDICATORS; idx++) {
170         if (info->indicator_names[idx].name == name) {
171             *idx_out = idx;
172             return &info->indicator_names[idx];
173         }
174     }
175
176     return NULL;
177 }
178
179 static bool
180 AddIndicatorName(KeyNamesInfo *info, enum merge_mode merge,
181                  IndicatorNameInfo *new, xkb_led_index_t new_idx)
182 {
183     xkb_led_index_t old_idx;
184     IndicatorNameInfo *old;
185     bool replace, report;
186     int verbosity = xkb_get_log_verbosity(info->ctx);
187
188     replace = (merge == MERGE_REPLACE) || (merge == MERGE_OVERRIDE);
189
190     old = FindIndicatorByName(info, new->name, &old_idx);
191     if (old) {
192         report = ((old->file_id == new->file_id && verbosity > 0) ||
193                   verbosity > 9);
194
195         if (old_idx == new_idx) {
196             if (report)
197                 log_warn(info->ctx, "Multiple indicators named %s; "
198                          "Identical definitions ignored\n",
199                          xkb_atom_text(info->ctx, new->name));
200             return true;
201         }
202
203         if (report)
204             log_warn(info->ctx, "Multiple indicators named %s; "
205                      "Using %d, ignoring %d\n",
206                      xkb_atom_text(info->ctx, new->name),
207                      (replace ? old_idx + 1 : new_idx + 1),
208                      (replace ? new_idx + 1 : old_idx + 1));
209
210         /*
211          * XXX: If in the next check we ignore new, than we will have
212          * deleted this old for nothing!
213          */
214         if (replace)
215             memset(old, 0, sizeof(*old));
216     }
217
218     old = &info->indicator_names[new_idx];
219     if (old->name != XKB_ATOM_NONE) {
220         report = ((old->file_id == new->file_id && verbosity > 0) ||
221                   verbosity > 9);
222
223         if (old->name == new->name) {
224             if (report)
225                 log_warn(info->ctx, "Multiple names for indicator %d; "
226                          "Identical definitions ignored\n", new_idx + 1);
227         }
228         else if (replace) {
229             if (report)
230                 log_warn(info->ctx, "Multiple names for indicator %d; "
231                          "Using %s, ignoring %s\n", new_idx + 1,
232                          xkb_atom_text(info->ctx, new->name),
233                          xkb_atom_text(info->ctx, old->name));
234             old->name = new->name;
235         }
236         else {
237             if (report)
238                 log_warn(info->ctx, "Multiple names for indicator %d; "
239                          "Using %s, ignoring %s\n", new_idx + 1,
240                          xkb_atom_text(info->ctx, old->name),
241                          xkb_atom_text(info->ctx, new->name));
242         }
243
244         return true;
245     }
246
247     info->indicator_names[new_idx] = *new;
248     return true;
249 }
250
251 static void
252 ClearKeyNamesInfo(KeyNamesInfo *info)
253 {
254     free(info->name);
255     darray_free(info->names);
256     darray_free(info->files);
257     darray_free(info->aliases);
258 }
259
260 static void
261 InitKeyNamesInfo(KeyNamesInfo *info, struct xkb_context *ctx,
262                  unsigned file_id)
263 {
264     memset(info, 0, sizeof(*info));
265     info->ctx = ctx;
266     info->merge = MERGE_DEFAULT;
267     info->file_id = file_id;
268     info->min_key_code = XKB_KEYCODE_MAX;
269 }
270
271 static int
272 FindKeyByLong(KeyNamesInfo * info, unsigned long name)
273 {
274     xkb_keycode_t i;
275
276     for (i = info->min_key_code; i <= info->max_key_code; i++)
277         if (darray_item(info->names, i) == name)
278             return i;
279
280     return 0;
281 }
282
283 /**
284  * Store the name of the key as a long in the info struct under the given
285  * keycode. If the same keys is referred to twice, print a warning.
286  * Note that the key's name is stored as a long, the keycode is the index.
287  */
288 static bool
289 AddKeyName(KeyNamesInfo *info, xkb_keycode_t kc, unsigned long name,
290            enum merge_mode merge, unsigned file_id, bool reportCollisions)
291 {
292     xkb_keycode_t old;
293     int verbosity = xkb_get_log_verbosity(info->ctx);
294
295     ResizeKeyNameArrays(info, kc);
296
297     info->min_key_code = MIN(info->min_key_code, kc);
298     info->max_key_code = MAX(info->max_key_code, kc);
299
300     if (reportCollisions)
301         reportCollisions = (verbosity > 7 ||
302                             (verbosity > 0 &&
303                              file_id == darray_item(info->files, kc)));
304
305     if (darray_item(info->names, kc) != 0) {
306         const char *lname = LongKeyNameText(darray_item(info->names, kc));
307         const char *kname = LongKeyNameText(name);
308
309         if (darray_item(info->names, kc) == name && reportCollisions) {
310             log_warn(info->ctx, "Multiple identical key name definitions; "
311                      "Later occurences of \"%s = %d\" ignored\n", lname, kc);
312             return true;
313         }
314
315         if (merge == MERGE_AUGMENT) {
316             if (reportCollisions)
317                 log_warn(info->ctx, "Multiple names for keycode %d; "
318                          "Using %s, ignoring %s\n", kc, lname, kname);
319             return true;
320         }
321         else {
322             if (reportCollisions)
323                 log_warn(info->ctx, "Multiple names for keycode %d; "
324                          "Using %s, ignoring %s\n", kc, kname, lname);
325             darray_item(info->names, kc) = 0;
326             darray_item(info->files, kc) = 0;
327         }
328     }
329
330     old = FindKeyByLong(info, name);
331     if (old != 0 && old != kc) {
332         const char *kname = LongKeyNameText(name);
333
334         if (merge == MERGE_OVERRIDE) {
335             darray_item(info->names, old) = 0;
336             darray_item(info->files, old) = 0;
337             if (reportCollisions)
338                 log_warn(info->ctx, "Key name %s assigned to multiple keys; "
339                          "Using %d, ignoring %d\n", kname, kc, old);
340         }
341         else {
342             if (reportCollisions && verbosity > 3)
343                 log_warn(info->ctx, "Key name %s assigned to multiple keys; "
344                          "Using %d, ignoring %d\n", kname, old, kc);
345             return true;
346         }
347     }
348
349     darray_item(info->names, kc) = name;
350     darray_item(info->files, kc) = file_id;
351     return true;
352 }
353
354 /***====================================================================***/
355
356 static int
357 HandleAliasDef(KeyNamesInfo *info, KeyAliasDef *def, enum merge_mode merge,
358                unsigned file_id);
359
360 static bool
361 MergeAliases(KeyNamesInfo *into, KeyNamesInfo *from, enum merge_mode merge)
362 {
363     AliasInfo *alias;
364     KeyAliasDef def;
365
366     if (darray_empty(from->aliases))
367         return true;
368
369     if (darray_empty(into->aliases)) {
370         into->aliases = from->aliases;
371         darray_init(from->aliases);
372         return true;
373     }
374
375     memset(&def, 0, sizeof(def));
376
377     darray_foreach(alias, from->aliases) {
378         def.merge = (merge == MERGE_DEFAULT) ? alias->merge : merge;
379         LongToKeyName(alias->alias, def.alias);
380         LongToKeyName(alias->real, def.real);
381
382         if (!HandleAliasDef(into, &def, def.merge, alias->file_id))
383             return false;
384     }
385
386     return true;
387 }
388
389 static void
390 MergeIncludedKeycodes(KeyNamesInfo *into, KeyNamesInfo *from,
391                       enum merge_mode merge)
392 {
393     xkb_keycode_t i;
394     xkb_led_index_t idx;
395
396     if (from->errorCount > 0) {
397         into->errorCount += from->errorCount;
398         return;
399     }
400
401     if (into->name == NULL) {
402         into->name = from->name;
403         from->name = NULL;
404     }
405
406     ResizeKeyNameArrays(into, from->max_key_code);
407
408     for (i = from->min_key_code; i <= from->max_key_code; i++) {
409         unsigned long name = darray_item(from->names, i);
410         if (name == 0)
411             continue;
412
413         if (!AddKeyName(into, i, name, merge, from->file_id, false))
414             into->errorCount++;
415     }
416
417     for (idx = 0; idx < XKB_NUM_INDICATORS; idx++) {
418         IndicatorNameInfo *led = &from->indicator_names[idx];
419         if (led->name == XKB_ATOM_NONE)
420             continue;
421
422         led->merge = (merge == MERGE_DEFAULT ? led->merge : merge);
423         if (!AddIndicatorName(into, led->merge, led, idx))
424             into->errorCount++;
425     }
426
427     if (!MergeAliases(into, from, merge))
428         into->errorCount++;
429 }
430
431 static void
432 HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge);
433
434 static bool
435 HandleIncludeKeycodes(KeyNamesInfo *info, IncludeStmt *stmt)
436 {
437     enum merge_mode merge = MERGE_DEFAULT;
438     XkbFile *rtrn;
439     KeyNamesInfo included, next_incl;
440
441     InitKeyNamesInfo(&included, info->ctx, info->file_id);
442     if (stmt->stmt) {
443         free(included.name);
444         included.name = stmt->stmt;
445         stmt->stmt = NULL;
446     }
447
448     for (; stmt; stmt = stmt->next_incl) {
449         if (!ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_KEYCODES,
450                                 &rtrn, &merge)) {
451             info->errorCount += 10;
452             ClearKeyNamesInfo(&included);
453             return false;
454         }
455
456         InitKeyNamesInfo(&next_incl, info->ctx, rtrn->id);
457
458         HandleKeycodesFile(&next_incl, rtrn, MERGE_OVERRIDE);
459
460         MergeIncludedKeycodes(&included, &next_incl, merge);
461
462         ClearKeyNamesInfo(&next_incl);
463         FreeXkbFile(rtrn);
464     }
465
466     MergeIncludedKeycodes(info, &included, merge);
467     ClearKeyNamesInfo(&included);
468
469     return (info->errorCount == 0);
470 }
471
472 static int
473 HandleKeycodeDef(KeyNamesInfo *info, KeycodeDef *stmt, enum merge_mode merge)
474 {
475     if (stmt->merge != MERGE_DEFAULT) {
476         if (stmt->merge == MERGE_REPLACE)
477             merge = MERGE_OVERRIDE;
478         else
479             merge = stmt->merge;
480     }
481
482     return AddKeyName(info, stmt->value, KeyNameToLong(stmt->name), merge,
483                       info->file_id, true);
484 }
485
486 static void
487 HandleAliasCollision(KeyNamesInfo *info, AliasInfo *old, AliasInfo *new)
488 {
489     int verbosity = xkb_get_log_verbosity(info->ctx);
490     bool report = ((new->file_id == old->file_id && verbosity > 0) ||
491                    verbosity > 9);
492
493     if (new->real == old->real) {
494         if (report)
495             log_warn(info->ctx, "Alias of %s for %s declared more than once; "
496                      "First definition ignored\n",
497                      LongKeyNameText(new->alias), LongKeyNameText(new->real));
498     }
499     else {
500         unsigned long use, ignore;
501
502         use = (new->merge == MERGE_AUGMENT ? old->real : new->real);
503         ignore = (new->merge == MERGE_AUGMENT ? new->real : old->real);
504
505         if (report)
506             log_warn(info->ctx, "Multiple definitions for alias %s; "
507                      "Using %s, ignoring %s\n",
508                      LongKeyNameText(old->alias), LongKeyNameText(use),
509                      LongKeyNameText(ignore));
510
511         old->real = use;
512     }
513
514     old->file_id = new->file_id;
515     old->merge = new->merge;
516 }
517
518 static int
519 HandleAliasDef(KeyNamesInfo *info, KeyAliasDef *def, enum merge_mode merge,
520                unsigned file_id)
521 {
522     AliasInfo *alias, new;
523
524     darray_foreach(alias, info->aliases) {
525         if (alias->alias == KeyNameToLong(def->alias)) {
526             InitAliasInfo(&new, merge, file_id, def->alias, def->real);
527             HandleAliasCollision(info, alias, &new);
528             return true;
529         }
530     }
531
532     InitAliasInfo(&new, merge, file_id, def->alias, def->real);
533     darray_append(info->aliases, new);
534     return true;
535 }
536
537 static int
538 HandleKeyNameVar(KeyNamesInfo *info, VarDef *stmt)
539 {
540     const char *elem, *field;
541     ExprDef *arrayNdx;
542
543     if (!ExprResolveLhs(info->ctx, stmt->name, &elem, &field, &arrayNdx))
544         return false;
545
546     if (elem) {
547         log_err(info->ctx, "Unknown element %s encountered; "
548                 "Default for field %s ignored\n", elem, field);
549         return false;
550     }
551
552     if (!istreq(field, "minimum") && !istreq(field, "maximum")) {
553         log_err(info->ctx, "Unknown field encountered; "
554                 "Assigment to field %s ignored\n", field);
555         return false;
556     }
557
558     /* We ignore explicit min/max statements, we always use computed. */
559     return true;
560 }
561
562 static int
563 HandleIndicatorNameDef(KeyNamesInfo *info, IndicatorNameDef *def,
564                        enum merge_mode merge)
565 {
566     IndicatorNameInfo ii;
567     xkb_atom_t name;
568
569     if (def->ndx < 1 || def->ndx > XKB_NUM_INDICATORS) {
570         info->errorCount++;
571         log_err(info->ctx,
572                 "Name specified for illegal indicator index %d\n; Ignored\n",
573                 def->ndx);
574         return false;
575     }
576
577     if (!ExprResolveString(info->ctx, def->name, &name)) {
578         char buf[20];
579         snprintf(buf, sizeof(buf), "%d", def->ndx);
580         info->errorCount++;
581         return ReportBadType(info->ctx, "indicator", "name", buf,
582                              "string");
583     }
584
585     ii.merge = info->merge;
586     ii.file_id = info->file_id;
587     ii.name = name;
588     return AddIndicatorName(info, merge, &ii, def->ndx - 1);
589 }
590
591 static void
592 HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge)
593 {
594     ParseCommon *stmt;
595     bool ok;
596
597     free(info->name);
598     info->name = strdup_safe(file->name);
599
600     for (stmt = file->defs; stmt; stmt = stmt->next) {
601         switch (stmt->type) {
602         case STMT_INCLUDE:
603             ok = HandleIncludeKeycodes(info, (IncludeStmt *) stmt);
604             break;
605         case STMT_KEYCODE:
606             ok = HandleKeycodeDef(info, (KeycodeDef *) stmt, merge);
607             break;
608         case STMT_ALIAS:
609             ok = HandleAliasDef(info, (KeyAliasDef *) stmt, merge,
610                                 info->file_id);
611             break;
612         case STMT_VAR:
613             ok = HandleKeyNameVar(info, (VarDef *) stmt);
614             break;
615         case STMT_INDICATOR_NAME:
616             ok = HandleIndicatorNameDef(info, (IndicatorNameDef *) stmt,
617                                         merge);
618             break;
619         default:
620             log_err(info->ctx,
621                     "Keycode files may define key and indicator names only; "
622                     "Ignoring %s\n", stmt_type_to_string(stmt->type));
623             ok = false;
624             break;
625         }
626
627         if (!ok)
628             info->errorCount++;
629
630         if (info->errorCount > 10) {
631             log_err(info->ctx, "Abandoning keycodes file \"%s\"\n",
632                     file->topName);
633             break;
634         }
635     }
636 }
637
638 static void
639 ApplyAliases(KeyNamesInfo *info, struct xkb_keymap *keymap)
640 {
641     struct xkb_key *key;
642     struct xkb_key_alias *a, new;
643     AliasInfo *alias;
644
645     darray_foreach(alias, info->aliases) {
646         /* Check that ->real is a key. */
647         key = FindNamedKey(keymap, alias->real, false);
648         if (!key) {
649             log_vrb(info->ctx, 5,
650                     "Attempt to alias %s to non-existent key %s; Ignored\n",
651                     LongKeyNameText(alias->alias),
652                     LongKeyNameText(alias->real));
653             continue;
654         }
655
656         /* Check that ->alias is not a key. */
657         key = FindNamedKey(keymap, alias->alias, false);
658         if (key) {
659             log_vrb(info->ctx, 5,
660                     "Attempt to create alias with the name of a real key; "
661                     "Alias \"%s = %s\" ignored\n",
662                     LongKeyNameText(alias->alias),
663                     LongKeyNameText(alias->real));
664             continue;
665         }
666
667         /* Check that ->alias in not already an alias, and if so handle it. */
668         darray_foreach(a, keymap->key_aliases) {
669             AliasInfo old_alias;
670
671             if (KeyNameToLong(a->alias) != alias->alias)
672                 continue;
673
674             InitAliasInfo(&old_alias, MERGE_AUGMENT, 0, a->alias, a->real);
675             HandleAliasCollision(info, &old_alias, alias);
676             LongToKeyName(old_alias.alias, a->alias);
677             LongToKeyName(old_alias.real, a->real);
678             alias->alias = 0;
679         }
680         if (alias->alias == 0)
681             continue;
682
683         /* Add the alias. */
684         LongToKeyName(alias->alias, new.alias);
685         LongToKeyName(alias->real, new.real);
686         darray_append(keymap->key_aliases, new);
687     }
688
689     darray_free(info->aliases);
690 }
691
692 static bool
693 CopyKeyNamesToKeymap(struct xkb_keymap *keymap, KeyNamesInfo *info)
694 {
695     xkb_keycode_t kc;
696     xkb_led_index_t idx;
697
698     keymap->min_key_code = info->min_key_code;
699     keymap->max_key_code = info->max_key_code;
700
701     darray_resize0(keymap->keys, keymap->max_key_code + 1);
702     for (kc = info->min_key_code; kc <= info->max_key_code; kc++)
703         LongToKeyName(darray_item(info->names, kc),
704                       darray_item(keymap->keys, kc).name);
705
706     keymap->keycodes_section_name = strdup_safe(info->name);
707
708     for (idx = 0; idx < XKB_NUM_INDICATORS; idx++) {
709         IndicatorNameInfo *led = &info->indicator_names[idx];
710         if (led->name == XKB_ATOM_NONE)
711             continue;
712
713         keymap->indicators[idx].name = led->name;
714     }
715
716     ApplyAliases(info, keymap);
717
718     return true;
719 }
720
721 bool
722 CompileKeycodes(XkbFile *file, struct xkb_keymap *keymap,
723                 enum merge_mode merge)
724 {
725     KeyNamesInfo info;
726
727     InitKeyNamesInfo(&info, keymap->ctx, file->id);
728
729     HandleKeycodesFile(&info, file, merge);
730     if (info.errorCount != 0)
731         goto err_info;
732
733     if (!CopyKeyNamesToKeymap(keymap, &info))
734         goto err_info;
735
736     ClearKeyNamesInfo(&info);
737     return true;
738
739 err_info:
740     ClearKeyNamesInfo(&info);
741     return false;
742 }
743
744 struct xkb_key *
745 FindNamedKey(struct xkb_keymap *keymap, unsigned long name, bool use_aliases)
746 {
747     struct xkb_key *key;
748
749     xkb_foreach_key(key, keymap)
750         if (KeyNameToLong(key->name) == name)
751             return key;
752
753     if (use_aliases) {
754         unsigned long new_name;
755         if (FindKeyNameForAlias(keymap, name, &new_name))
756             return FindNamedKey(keymap, new_name, false);
757     }
758
759     return NULL;
760 }
761
762 bool
763 FindKeyNameForAlias(struct xkb_keymap *keymap, unsigned long lname,
764                     unsigned long *real_name)
765 {
766     char name[XKB_KEY_NAME_LENGTH];
767     struct xkb_key_alias *a;
768
769     LongToKeyName(lname, name);
770     darray_foreach(a, keymap->key_aliases) {
771         if (strncmp(name, a->alias, XKB_KEY_NAME_LENGTH) == 0) {
772             *real_name = KeyNameToLong(a->real);
773             return true;
774         }
775     }
776
777     return false;
778 }