Remove the XKB_NUM_INDICATORS limit
[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  * by which it can be referred to later, e.g. in the symbols section.
41  *
42  * Keycode statements
43  * ------------------
44  * Statements of the form:
45  *      <TLDE> = 49;
46  *      <AE01> = 10;
47  *
48  * The above would let 49 and 10 be valid keycodes in the keymap, and
49  * assign them the names TLDE and AE01 respectively. The format <WXYZ> is
50  * always used to refer to a key by name.
51  *
52  * [ The naming convention <AE01> just denoted the position of the key
53  * in the main alphanumric section of the keyboard, with the two letters
54  * specifying the row and the two digits specifying the column, from
55  * the bottom left.]
56  *
57  * In the common case this just maps to the evdev scancodes from
58  * /usr/include/linux/input.h, e.g. the following definitions:
59  *      #define KEY_GRAVE            41
60  *      #define KEY_1                2
61  * Similar definitions appear in the xf86-input-keyboard driver. Note
62  * that in all current keymaps there's a constant offset of 8 (for
63  * historical reasons).
64  *
65  * If there's a conflict, like the same name given to different keycodes,
66  * or same keycode given different names, it is resolved according to the
67  * merge mode which applies to the definitions.
68  *
69  * Alias statements
70  * ----------------
71  * Statements of the form:
72  *      alias <MENU> = <COMP>;
73  *
74  * Allows to refer to a previously defined key (here <COMP>) by another
75  * name (here <MENU>). Conflicts are handled similarly.
76  *
77  * Indicator name statements
78  * -------------------------
79  * Statements of the form:
80  *      indicator 1 = "Caps Lock";
81  *      indicator 2 = "Num Lock";
82  *      indicator 3 = "Scroll Lock";
83  *
84  * Assigns a name the indicator (i.e. keyboard LED) with the given index.
85  * The amount of possible indicators is predetermined (XKB_NUM_INDICATORS).
86  * The indicator may be referred by this name later in the compat section
87  * and by the user.
88  *
89  * Effect on the keymap
90  * --------------------
91  * After all of the xkb_keycodes sections have been compiled, the
92  * following members of struct xkb_keymap are finalized:
93  *      xkb_keycode_t min_key_code;
94  *      xkb_keycode_t max_key_code;
95  *      darray(struct xkb_key_alias) key_aliases;
96  *      char *keycodes_section_name;
97  * The 'name' field of indicators declared in xkb_keycodes:
98  *      struct xkb_indicator_map indicators[XKB_NUM_INDICATORS];
99  * Further, the array of keys:
100  *      struct xkb_key *keys;
101  * had been resized to its final size (i.e. all of the xkb_key objects are
102  * referable by their keycode). However the objects themselves do not
103  * contain any useful information besides the key name at this point.
104  */
105
106 typedef struct {
107     enum merge_mode merge;
108     unsigned file_id;
109
110     xkb_atom_t alias;
111     xkb_atom_t real;
112 } AliasInfo;
113
114 typedef struct {
115     unsigned int file_id;
116     xkb_atom_t name;
117 } KeyNameInfo;
118
119 typedef struct {
120     enum merge_mode merge;
121     unsigned file_id;
122
123     xkb_atom_t name;
124 } IndicatorNameInfo;
125
126 typedef struct {
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(KeyNameInfo) key_names;
135     darray(IndicatorNameInfo) indicator_names;
136     darray(AliasInfo) aliases;
137
138     struct xkb_context *ctx;
139 } KeyNamesInfo;
140
141 static void
142 InitAliasInfo(AliasInfo *info, enum merge_mode merge, unsigned file_id,
143               xkb_atom_t alias, xkb_atom_t real)
144 {
145     memset(info, 0, sizeof(*info));
146     info->merge = merge;
147     info->file_id = file_id;
148     info->alias = alias;
149     info->real = real;
150 }
151
152 static IndicatorNameInfo *
153 FindIndicatorByName(KeyNamesInfo *info, xkb_atom_t name,
154                     xkb_led_index_t *idx_out)
155 {
156     IndicatorNameInfo *led;
157     xkb_led_index_t idx;
158
159     darray_enumerate(idx, led, info->indicator_names) {
160         if (led->name == name) {
161             *idx_out = idx;
162             return led;
163         }
164     }
165
166     return NULL;
167 }
168
169 static bool
170 AddIndicatorName(KeyNamesInfo *info, enum merge_mode merge,
171                  IndicatorNameInfo *new, xkb_led_index_t new_idx)
172 {
173     xkb_led_index_t old_idx;
174     IndicatorNameInfo *old;
175     bool replace;
176     int verbosity = xkb_context_get_log_verbosity(info->ctx);
177
178     replace = (merge == MERGE_REPLACE || merge == MERGE_OVERRIDE);
179
180     /* Inidicator with the same name already exists. */
181     old = FindIndicatorByName(info, new->name, &old_idx);
182     if (old) {
183         bool report = ((old->file_id == new->file_id && verbosity > 0) ||
184                        verbosity > 9);
185
186         if (old_idx == new_idx) {
187             log_warn(info->ctx,
188                      "Multiple indicators named \"%s\"; "
189                      "Identical definitions ignored\n",
190                      xkb_atom_text(info->ctx, new->name));
191             return true;
192         }
193
194         if (report) {
195             xkb_led_index_t use = (replace ? new_idx + 1 : old_idx + 1);
196             xkb_led_index_t ignore = (replace ? old_idx + 1 : new_idx + 1);
197             log_warn(info->ctx, "Multiple indicators named %s; "
198                      "Using %d, ignoring %d\n",
199                      xkb_atom_text(info->ctx, new->name), use, ignore);
200         }
201
202         if (replace)
203             *old = *new;
204
205         return true;
206     }
207
208     if (new_idx >= darray_size(info->indicator_names))
209         darray_resize0(info->indicator_names, new_idx + 1);
210
211     /* Inidicator with the same index already exists. */
212     old = &darray_item(info->indicator_names, new_idx);
213     if (old->name != XKB_ATOM_NONE) {
214         bool report = ((old->file_id == new->file_id && verbosity > 0) ||
215                        verbosity > 9);
216
217         /* Same name case already handled above. */
218
219         if (report) {
220             xkb_atom_t use = (replace ? new->name : old->name);
221             xkb_atom_t ignore = (replace ? old->name : new->name);
222             log_warn(info->ctx, "Multiple names for indicator %d; "
223                      "Using %s, ignoring %s\n", new_idx + 1,
224                      xkb_atom_text(info->ctx, use),
225                      xkb_atom_text(info->ctx, ignore));
226         }
227
228         if (replace)
229             *old = *new;
230
231         return true;
232     }
233
234     darray_item(info->indicator_names, new_idx) = *new;
235     return true;
236 }
237
238 static void
239 ClearKeyNamesInfo(KeyNamesInfo *info)
240 {
241     free(info->name);
242     darray_free(info->key_names);
243     darray_free(info->aliases);
244     darray_free(info->indicator_names);
245 }
246
247 static void
248 InitKeyNamesInfo(KeyNamesInfo *info, struct xkb_context *ctx,
249                  unsigned file_id)
250 {
251     memset(info, 0, sizeof(*info));
252     info->ctx = ctx;
253     info->merge = MERGE_DEFAULT;
254     info->file_id = file_id;
255     info->min_key_code = XKB_KEYCODE_MAX;
256 }
257
258 static xkb_keycode_t
259 FindKeyByName(KeyNamesInfo * info, xkb_atom_t name)
260 {
261     xkb_keycode_t i;
262
263     for (i = info->min_key_code; i <= info->max_key_code; i++)
264         if (darray_item(info->key_names, i).name == name)
265             return i;
266
267     return XKB_KEYCODE_INVALID;
268 }
269
270 static bool
271 AddKeyName(KeyNamesInfo *info, xkb_keycode_t kc, xkb_atom_t name,
272            enum merge_mode merge, unsigned file_id, bool report)
273 {
274     KeyNameInfo *namei;
275     xkb_keycode_t old;
276     int verbosity = xkb_context_get_log_verbosity(info->ctx);
277
278     if (kc >= darray_size(info->key_names))
279         darray_resize0(info->key_names, kc + 1);
280
281     info->min_key_code = MIN(info->min_key_code, kc);
282     info->max_key_code = MAX(info->max_key_code, kc);
283
284     namei = &darray_item(info->key_names, kc);
285
286     report = report && ((verbosity > 0 && file_id == namei->file_id) ||
287                         verbosity > 7);
288
289     if (namei->name != 0) {
290         const char *lname = KeyNameText(info->ctx, namei->name);
291         const char *kname = KeyNameText(info->ctx, name);
292
293         if (namei->name == name) {
294             if (report)
295                 log_warn(info->ctx,
296                          "Multiple identical key name definitions; "
297                          "Later occurences of \"%s = %d\" ignored\n",
298                          lname, kc);
299             return true;
300         }
301         else if (merge == MERGE_AUGMENT) {
302             if (report)
303                 log_warn(info->ctx,
304                          "Multiple names for keycode %d; "
305                          "Using %s, ignoring %s\n", kc, lname, kname);
306             return true;
307         }
308         else {
309             if (report)
310                 log_warn(info->ctx,
311                          "Multiple names for keycode %d; "
312                          "Using %s, ignoring %s\n", kc, kname, lname);
313             namei->name = 0;
314             namei->file_id = 0;
315         }
316     }
317
318     old = FindKeyByName(info, name);
319     if (old != XKB_KEYCODE_INVALID && old != kc) {
320         const char *kname = KeyNameText(info->ctx, name);
321
322         if (merge == MERGE_OVERRIDE) {
323             darray_item(info->key_names, old).name = 0;
324             darray_item(info->key_names, old).file_id = 0;
325             if (report)
326                 log_warn(info->ctx,
327                          "Key name %s assigned to multiple keys; "
328                          "Using %d, ignoring %d\n", kname, kc, old);
329         }
330         else {
331             if (report)
332                 log_vrb(info->ctx, 3,
333                         "Key name %s assigned to multiple keys; "
334                         "Using %d, ignoring %d\n", kname, old, kc);
335             return true;
336         }
337     }
338
339     namei->name = name;
340     namei->file_id = file_id;
341     return true;
342 }
343
344 /***====================================================================***/
345
346 static int
347 HandleAliasDef(KeyNamesInfo *info, KeyAliasDef *def, enum merge_mode merge,
348                unsigned file_id);
349
350 static bool
351 MergeAliases(KeyNamesInfo *into, KeyNamesInfo *from, enum merge_mode merge)
352 {
353     AliasInfo *alias;
354     KeyAliasDef def;
355
356     if (darray_empty(from->aliases))
357         return true;
358
359     if (darray_empty(into->aliases)) {
360         into->aliases = from->aliases;
361         darray_init(from->aliases);
362         return true;
363     }
364
365     memset(&def, 0, sizeof(def));
366
367     darray_foreach(alias, from->aliases) {
368         def.merge = (merge == MERGE_DEFAULT) ? alias->merge : merge;
369         def.alias = alias->alias;
370         def.real = alias->real;
371
372         if (!HandleAliasDef(into, &def, def.merge, alias->file_id))
373             return false;
374     }
375
376     return true;
377 }
378
379 static void
380 MergeIncludedKeycodes(KeyNamesInfo *into, KeyNamesInfo *from,
381                       enum merge_mode merge)
382 {
383     xkb_keycode_t i;
384     xkb_led_index_t idx;
385     IndicatorNameInfo *led;
386
387     if (from->errorCount > 0) {
388         into->errorCount += from->errorCount;
389         return;
390     }
391
392     if (into->name == NULL) {
393         into->name = from->name;
394         from->name = NULL;
395     }
396
397     if (darray_size(into->key_names) < darray_size(from->key_names))
398         darray_resize0(into->key_names, darray_size(from->key_names));
399
400     for (i = from->min_key_code; i <= from->max_key_code; i++) {
401         xkb_atom_t name = darray_item(from->key_names, i).name;
402         if (name == XKB_ATOM_NONE)
403             continue;
404
405         if (!AddKeyName(into, i, name, merge, from->file_id, false))
406             into->errorCount++;
407     }
408
409     darray_enumerate(idx, led, from->indicator_names) {
410         if (led->name == XKB_ATOM_NONE)
411             continue;
412
413         led->merge = (merge == MERGE_DEFAULT ? led->merge : merge);
414         if (!AddIndicatorName(into, led->merge, led, idx))
415             into->errorCount++;
416     }
417
418     if (!MergeAliases(into, from, merge))
419         into->errorCount++;
420 }
421
422 static void
423 HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge);
424
425 static bool
426 HandleIncludeKeycodes(KeyNamesInfo *info, IncludeStmt *stmt)
427 {
428     enum merge_mode merge = MERGE_DEFAULT;
429     XkbFile *rtrn;
430     KeyNamesInfo included, next_incl;
431
432     InitKeyNamesInfo(&included, info->ctx, info->file_id);
433     if (stmt->stmt) {
434         free(included.name);
435         included.name = stmt->stmt;
436         stmt->stmt = NULL;
437     }
438
439     for (; stmt; stmt = stmt->next_incl) {
440         if (!ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_KEYCODES,
441                                 &rtrn, &merge)) {
442             info->errorCount += 10;
443             ClearKeyNamesInfo(&included);
444             return false;
445         }
446
447         InitKeyNamesInfo(&next_incl, info->ctx, rtrn->id);
448
449         HandleKeycodesFile(&next_incl, rtrn, MERGE_OVERRIDE);
450
451         MergeIncludedKeycodes(&included, &next_incl, merge);
452
453         ClearKeyNamesInfo(&next_incl);
454         FreeXkbFile(rtrn);
455     }
456
457     MergeIncludedKeycodes(info, &included, merge);
458     ClearKeyNamesInfo(&included);
459
460     return (info->errorCount == 0);
461 }
462
463 static bool
464 HandleKeycodeDef(KeyNamesInfo *info, KeycodeDef *stmt, enum merge_mode merge)
465 {
466     if (stmt->merge != MERGE_DEFAULT) {
467         if (stmt->merge == MERGE_REPLACE)
468             merge = MERGE_OVERRIDE;
469         else
470             merge = stmt->merge;
471     }
472
473     if (stmt->value < 0 || stmt->value > XKB_KEYCODE_MAX) {
474         log_err(info->ctx,
475                 "Illegal keycode %lld: must be between 0..%u; "
476                 "Key ignored\n", stmt->value, XKB_KEYCODE_MAX);
477         return false;
478     }
479
480     return AddKeyName(info, stmt->value, stmt->name, merge,
481                       info->file_id, true);
482 }
483
484 static void
485 HandleAliasCollision(KeyNamesInfo *info, AliasInfo *old, AliasInfo *new)
486 {
487     int verbosity = xkb_context_get_log_verbosity(info->ctx);
488     bool report = ((new->file_id == old->file_id && verbosity > 0) ||
489                    verbosity > 9);
490
491     if (new->real == old->real) {
492         if (report)
493             log_warn(info->ctx, "Alias of %s for %s declared more than once; "
494                      "First definition ignored\n",
495                      KeyNameText(info->ctx, new->alias),
496                      KeyNameText(info->ctx, new->real));
497     }
498     else {
499         xkb_atom_t use, ignore;
500
501         use = (new->merge == MERGE_AUGMENT ? old->real : new->real);
502         ignore = (new->merge == MERGE_AUGMENT ? new->real : old->real);
503
504         if (report)
505             log_warn(info->ctx, "Multiple definitions for alias %s; "
506                      "Using %s, ignoring %s\n",
507                      KeyNameText(info->ctx, old->alias),
508                      KeyNameText(info->ctx, use),
509                      KeyNameText(info->ctx, 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 == 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_MAX_LEDS) {
570         info->errorCount++;
571         log_err(info->ctx,
572                 "Illegal indicator index (%d) specified; must be between 1 .. %d; "
573                 "Ignored\n", def->ndx, XKB_MAX_LEDS);
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                     KeyNameText(info->ctx, alias->alias),
652                     KeyNameText(info->ctx, 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                     KeyNameText(info->ctx, alias->alias),
663                     KeyNameText(info->ctx, 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 (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             a->alias = old_alias.alias;
677             a->real = old_alias.real;
678             alias->alias = 0;
679         }
680         if (alias->alias == 0)
681             continue;
682
683         /* Add the alias. */
684         new.alias = alias->alias;
685         new.real = alias->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     IndicatorNameInfo *led;
698
699     keymap->keys = calloc(info->max_key_code + 1, sizeof(*keymap->keys));
700     if (!keymap->keys)
701         return false;
702
703     keymap->min_key_code = info->min_key_code;
704     keymap->max_key_code = info->max_key_code;
705
706     for (kc = info->min_key_code; kc <= info->max_key_code; kc++) {
707         keymap->keys[kc].keycode = kc;
708         keymap->keys[kc].name = darray_item(info->key_names, kc).name;
709     }
710
711     keymap->keycodes_section_name = strdup_safe(info->name);
712
713     darray_resize0(keymap->indicators, darray_size(info->indicator_names));
714     darray_enumerate(idx, led, info->indicator_names) {
715         if (led->name == XKB_ATOM_NONE)
716             continue;
717
718         darray_item(keymap->indicators, idx).name = led->name;
719     }
720
721     ApplyAliases(info, keymap);
722
723     return true;
724 }
725
726 bool
727 CompileKeycodes(XkbFile *file, struct xkb_keymap *keymap,
728                 enum merge_mode merge)
729 {
730     KeyNamesInfo info;
731
732     InitKeyNamesInfo(&info, keymap->ctx, file->id);
733
734     HandleKeycodesFile(&info, file, merge);
735     if (info.errorCount != 0)
736         goto err_info;
737
738     if (!CopyKeyNamesToKeymap(keymap, &info))
739         goto err_info;
740
741     ClearKeyNamesInfo(&info);
742     return true;
743
744 err_info:
745     ClearKeyNamesInfo(&info);
746     return false;
747 }
748
749 struct xkb_key *
750 FindNamedKey(struct xkb_keymap *keymap, xkb_atom_t name, bool use_aliases)
751 {
752     struct xkb_key *key;
753
754     xkb_foreach_key(key, keymap)
755         if (key->name == name)
756             return key;
757
758     if (use_aliases) {
759         xkb_atom_t new_name;
760         if (FindKeyNameForAlias(keymap, name, &new_name))
761             return FindNamedKey(keymap, new_name, false);
762     }
763
764     return NULL;
765 }
766
767 bool
768 FindKeyNameForAlias(struct xkb_keymap *keymap, xkb_atom_t name,
769                     xkb_atom_t *real_name)
770 {
771     struct xkb_key_alias *a;
772
773     darray_foreach(a, keymap->key_aliases) {
774         if (name == a->alias) {
775             *real_name = a->real;
776             return true;
777         }
778     }
779
780     return false;
781 }