keycodes: add KeyNameInfo
[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 two letters
55  * specifying the row and the two digits specifying the column, from
56  * the bottom left.]
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 {
120     unsigned int file_id;
121     unsigned long name;
122 } KeyNameInfo;
123
124 typedef struct _IndicatorNameInfo {
125     enum merge_mode merge;
126     unsigned file_id;
127
128     xkb_atom_t name;
129 } IndicatorNameInfo;
130
131 typedef struct _KeyNamesInfo {
132     char *name;     /* e.g. evdev+aliases(qwerty) */
133     int errorCount;
134     unsigned file_id;
135     enum merge_mode merge;
136
137     xkb_keycode_t min_key_code;
138     xkb_keycode_t max_key_code;
139     darray(KeyNameInfo) key_names;
140     IndicatorNameInfo indicator_names[XKB_NUM_INDICATORS];
141     darray(AliasInfo) aliases;
142
143     struct xkb_context *ctx;
144 } KeyNamesInfo;
145
146 static void
147 InitAliasInfo(AliasInfo *info, enum merge_mode merge, unsigned file_id,
148               char alias[XKB_KEY_NAME_LENGTH], char real[XKB_KEY_NAME_LENGTH])
149 {
150     memset(info, 0, sizeof(*info));
151     info->merge = merge;
152     info->file_id = file_id;
153     info->alias = KeyNameToLong(alias);
154     info->real = KeyNameToLong(real);
155 }
156
157 static IndicatorNameInfo *
158 FindIndicatorByName(KeyNamesInfo *info, xkb_atom_t name,
159                     xkb_led_index_t *idx_out)
160 {
161     xkb_led_index_t idx;
162
163     for (idx = 0; idx < XKB_NUM_INDICATORS; idx++) {
164         if (info->indicator_names[idx].name == name) {
165             *idx_out = idx;
166             return &info->indicator_names[idx];
167         }
168     }
169
170     return NULL;
171 }
172
173 static bool
174 AddIndicatorName(KeyNamesInfo *info, enum merge_mode merge,
175                  IndicatorNameInfo *new, xkb_led_index_t new_idx)
176 {
177     xkb_led_index_t old_idx;
178     IndicatorNameInfo *old;
179     bool replace, report;
180     int verbosity = xkb_get_log_verbosity(info->ctx);
181
182     replace = (merge == MERGE_REPLACE) || (merge == MERGE_OVERRIDE);
183
184     old = FindIndicatorByName(info, new->name, &old_idx);
185     if (old) {
186         report = ((old->file_id == new->file_id && verbosity > 0) ||
187                   verbosity > 9);
188
189         if (old_idx == new_idx) {
190             if (report)
191                 log_warn(info->ctx, "Multiple indicators named %s; "
192                          "Identical definitions ignored\n",
193                          xkb_atom_text(info->ctx, new->name));
194             return true;
195         }
196
197         if (report)
198             log_warn(info->ctx, "Multiple indicators named %s; "
199                      "Using %d, ignoring %d\n",
200                      xkb_atom_text(info->ctx, new->name),
201                      (replace ? old_idx + 1 : new_idx + 1),
202                      (replace ? new_idx + 1 : old_idx + 1));
203
204         /*
205          * XXX: If in the next check we ignore new, than we will have
206          * deleted this old for nothing!
207          */
208         if (replace)
209             memset(old, 0, sizeof(*old));
210     }
211
212     old = &info->indicator_names[new_idx];
213     if (old->name != XKB_ATOM_NONE) {
214         report = ((old->file_id == new->file_id && verbosity > 0) ||
215                   verbosity > 9);
216
217         if (old->name == new->name) {
218             if (report)
219                 log_warn(info->ctx, "Multiple names for indicator %d; "
220                          "Identical definitions ignored\n", new_idx + 1);
221         }
222         else if (replace) {
223             if (report)
224                 log_warn(info->ctx, "Multiple names for indicator %d; "
225                          "Using %s, ignoring %s\n", new_idx + 1,
226                          xkb_atom_text(info->ctx, new->name),
227                          xkb_atom_text(info->ctx, old->name));
228             old->name = new->name;
229         }
230         else {
231             if (report)
232                 log_warn(info->ctx, "Multiple names for indicator %d; "
233                          "Using %s, ignoring %s\n", new_idx + 1,
234                          xkb_atom_text(info->ctx, old->name),
235                          xkb_atom_text(info->ctx, new->name));
236         }
237
238         return true;
239     }
240
241     info->indicator_names[new_idx] = *new;
242     return true;
243 }
244
245 static void
246 ClearKeyNamesInfo(KeyNamesInfo *info)
247 {
248     free(info->name);
249     darray_free(info->key_names);
250     darray_free(info->aliases);
251 }
252
253 static void
254 InitKeyNamesInfo(KeyNamesInfo *info, struct xkb_context *ctx,
255                  unsigned file_id)
256 {
257     memset(info, 0, sizeof(*info));
258     info->ctx = ctx;
259     info->merge = MERGE_DEFAULT;
260     info->file_id = file_id;
261     info->min_key_code = XKB_KEYCODE_MAX;
262 }
263
264 static xkb_keycode_t
265 FindKeyByLong(KeyNamesInfo * info, unsigned long name)
266 {
267     xkb_keycode_t i;
268
269     for (i = info->min_key_code; i <= info->max_key_code; i++)
270         if (darray_item(info->key_names, i).name == name)
271             return i;
272
273     return XKB_KEYCODE_INVALID;
274 }
275
276 static bool
277 AddKeyName(KeyNamesInfo *info, xkb_keycode_t kc, unsigned long name,
278            enum merge_mode merge, unsigned file_id, bool report)
279 {
280     KeyNameInfo *namei;
281     xkb_keycode_t old;
282     int verbosity = xkb_get_log_verbosity(info->ctx);
283
284     if (kc >= darray_size(info->key_names))
285         darray_resize0(info->key_names, kc + 1);
286
287     info->min_key_code = MIN(info->min_key_code, kc);
288     info->max_key_code = MAX(info->max_key_code, kc);
289
290     namei = &darray_item(info->key_names, kc);
291
292     report = report && ((verbosity > 0 && file_id == namei->file_id) ||
293                         verbosity > 7);
294
295     if (namei->name != 0) {
296         const char *lname = LongKeyNameText(namei->name);
297         const char *kname = LongKeyNameText(name);
298
299         if (namei->name == name) {
300             if (report)
301                 log_warn(info->ctx,
302                          "Multiple identical key name definitions; "
303                          "Later occurences of \"%s = %d\" ignored\n",
304                          lname, kc);
305             return true;
306         }
307         else if (merge == MERGE_AUGMENT) {
308             if (report)
309                 log_warn(info->ctx,
310                          "Multiple names for keycode %d; "
311                          "Using %s, ignoring %s\n", kc, lname, kname);
312             return true;
313         }
314         else {
315             if (report)
316                 log_warn(info->ctx,
317                          "Multiple names for keycode %d; "
318                          "Using %s, ignoring %s\n", kc, kname, lname);
319             namei->name = 0;
320             namei->file_id = 0;
321         }
322     }
323
324     old = FindKeyByLong(info, name);
325     if (old != XKB_KEYCODE_INVALID && old != kc) {
326         const char *kname = LongKeyNameText(name);
327
328         if (merge == MERGE_OVERRIDE) {
329             darray_item(info->key_names, old).name = 0;
330             darray_item(info->key_names, old).file_id = 0;
331             if (report)
332                 log_warn(info->ctx,
333                          "Key name %s assigned to multiple keys; "
334                          "Using %d, ignoring %d\n", kname, kc, old);
335         }
336         else {
337             if (report)
338                 log_vrb(info->ctx, 3,
339                         "Key name %s assigned to multiple keys; "
340                         "Using %d, ignoring %d\n", kname, old, kc);
341             return true;
342         }
343     }
344
345     namei->name = name;
346     namei->file_id = file_id;
347     return true;
348 }
349
350 /***====================================================================***/
351
352 static int
353 HandleAliasDef(KeyNamesInfo *info, KeyAliasDef *def, enum merge_mode merge,
354                unsigned file_id);
355
356 static bool
357 MergeAliases(KeyNamesInfo *into, KeyNamesInfo *from, enum merge_mode merge)
358 {
359     AliasInfo *alias;
360     KeyAliasDef def;
361
362     if (darray_empty(from->aliases))
363         return true;
364
365     if (darray_empty(into->aliases)) {
366         into->aliases = from->aliases;
367         darray_init(from->aliases);
368         return true;
369     }
370
371     memset(&def, 0, sizeof(def));
372
373     darray_foreach(alias, from->aliases) {
374         def.merge = (merge == MERGE_DEFAULT) ? alias->merge : merge;
375         LongToKeyName(alias->alias, def.alias);
376         LongToKeyName(alias->real, def.real);
377
378         if (!HandleAliasDef(into, &def, def.merge, alias->file_id))
379             return false;
380     }
381
382     return true;
383 }
384
385 static void
386 MergeIncludedKeycodes(KeyNamesInfo *into, KeyNamesInfo *from,
387                       enum merge_mode merge)
388 {
389     xkb_keycode_t i;
390     xkb_led_index_t idx;
391
392     if (from->errorCount > 0) {
393         into->errorCount += from->errorCount;
394         return;
395     }
396
397     if (into->name == NULL) {
398         into->name = from->name;
399         from->name = NULL;
400     }
401
402     if (darray_size(into->key_names) < darray_size(from->key_names))
403         darray_resize0(into->key_names, darray_size(from->key_names));
404
405     for (i = from->min_key_code; i <= from->max_key_code; i++) {
406         unsigned long name = darray_item(from->key_names, i).name;
407         if (name == 0)
408             continue;
409
410         if (!AddKeyName(into, i, name, merge, from->file_id, false))
411             into->errorCount++;
412     }
413
414     for (idx = 0; idx < XKB_NUM_INDICATORS; idx++) {
415         IndicatorNameInfo *led = &from->indicator_names[idx];
416         if (led->name == XKB_ATOM_NONE)
417             continue;
418
419         led->merge = (merge == MERGE_DEFAULT ? led->merge : merge);
420         if (!AddIndicatorName(into, led->merge, led, idx))
421             into->errorCount++;
422     }
423
424     if (!MergeAliases(into, from, merge))
425         into->errorCount++;
426 }
427
428 static void
429 HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge);
430
431 static bool
432 HandleIncludeKeycodes(KeyNamesInfo *info, IncludeStmt *stmt)
433 {
434     enum merge_mode merge = MERGE_DEFAULT;
435     XkbFile *rtrn;
436     KeyNamesInfo included, next_incl;
437
438     InitKeyNamesInfo(&included, info->ctx, info->file_id);
439     if (stmt->stmt) {
440         free(included.name);
441         included.name = stmt->stmt;
442         stmt->stmt = NULL;
443     }
444
445     for (; stmt; stmt = stmt->next_incl) {
446         if (!ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_KEYCODES,
447                                 &rtrn, &merge)) {
448             info->errorCount += 10;
449             ClearKeyNamesInfo(&included);
450             return false;
451         }
452
453         InitKeyNamesInfo(&next_incl, info->ctx, rtrn->id);
454
455         HandleKeycodesFile(&next_incl, rtrn, MERGE_OVERRIDE);
456
457         MergeIncludedKeycodes(&included, &next_incl, merge);
458
459         ClearKeyNamesInfo(&next_incl);
460         FreeXkbFile(rtrn);
461     }
462
463     MergeIncludedKeycodes(info, &included, merge);
464     ClearKeyNamesInfo(&included);
465
466     return (info->errorCount == 0);
467 }
468
469 static int
470 HandleKeycodeDef(KeyNamesInfo *info, KeycodeDef *stmt, enum merge_mode merge)
471 {
472     if (stmt->merge != MERGE_DEFAULT) {
473         if (stmt->merge == MERGE_REPLACE)
474             merge = MERGE_OVERRIDE;
475         else
476             merge = stmt->merge;
477     }
478
479     return AddKeyName(info, stmt->value, KeyNameToLong(stmt->name), merge,
480                       info->file_id, true);
481 }
482
483 static void
484 HandleAliasCollision(KeyNamesInfo *info, AliasInfo *old, AliasInfo *new)
485 {
486     int verbosity = xkb_get_log_verbosity(info->ctx);
487     bool report = ((new->file_id == old->file_id && verbosity > 0) ||
488                    verbosity > 9);
489
490     if (new->real == old->real) {
491         if (report)
492             log_warn(info->ctx, "Alias of %s for %s declared more than once; "
493                      "First definition ignored\n",
494                      LongKeyNameText(new->alias), LongKeyNameText(new->real));
495     }
496     else {
497         unsigned long use, ignore;
498
499         use = (new->merge == MERGE_AUGMENT ? old->real : new->real);
500         ignore = (new->merge == MERGE_AUGMENT ? new->real : old->real);
501
502         if (report)
503             log_warn(info->ctx, "Multiple definitions for alias %s; "
504                      "Using %s, ignoring %s\n",
505                      LongKeyNameText(old->alias), LongKeyNameText(use),
506                      LongKeyNameText(ignore));
507
508         old->real = use;
509     }
510
511     old->file_id = new->file_id;
512     old->merge = new->merge;
513 }
514
515 static int
516 HandleAliasDef(KeyNamesInfo *info, KeyAliasDef *def, enum merge_mode merge,
517                unsigned file_id)
518 {
519     AliasInfo *alias, new;
520
521     darray_foreach(alias, info->aliases) {
522         if (alias->alias == KeyNameToLong(def->alias)) {
523             InitAliasInfo(&new, merge, file_id, def->alias, def->real);
524             HandleAliasCollision(info, alias, &new);
525             return true;
526         }
527     }
528
529     InitAliasInfo(&new, merge, file_id, def->alias, def->real);
530     darray_append(info->aliases, new);
531     return true;
532 }
533
534 static int
535 HandleKeyNameVar(KeyNamesInfo *info, VarDef *stmt)
536 {
537     const char *elem, *field;
538     ExprDef *arrayNdx;
539
540     if (!ExprResolveLhs(info->ctx, stmt->name, &elem, &field, &arrayNdx))
541         return false;
542
543     if (elem) {
544         log_err(info->ctx, "Unknown element %s encountered; "
545                 "Default for field %s ignored\n", elem, field);
546         return false;
547     }
548
549     if (!istreq(field, "minimum") && !istreq(field, "maximum")) {
550         log_err(info->ctx, "Unknown field encountered; "
551                 "Assigment to field %s ignored\n", field);
552         return false;
553     }
554
555     /* We ignore explicit min/max statements, we always use computed. */
556     return true;
557 }
558
559 static int
560 HandleIndicatorNameDef(KeyNamesInfo *info, IndicatorNameDef *def,
561                        enum merge_mode merge)
562 {
563     IndicatorNameInfo ii;
564     xkb_atom_t name;
565
566     if (def->ndx < 1 || def->ndx > XKB_NUM_INDICATORS) {
567         info->errorCount++;
568         log_err(info->ctx,
569                 "Name specified for illegal indicator index %d\n; Ignored\n",
570                 def->ndx);
571         return false;
572     }
573
574     if (!ExprResolveString(info->ctx, def->name, &name)) {
575         char buf[20];
576         snprintf(buf, sizeof(buf), "%d", def->ndx);
577         info->errorCount++;
578         return ReportBadType(info->ctx, "indicator", "name", buf,
579                              "string");
580     }
581
582     ii.merge = info->merge;
583     ii.file_id = info->file_id;
584     ii.name = name;
585     return AddIndicatorName(info, merge, &ii, def->ndx - 1);
586 }
587
588 static void
589 HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge)
590 {
591     ParseCommon *stmt;
592     bool ok;
593
594     free(info->name);
595     info->name = strdup_safe(file->name);
596
597     for (stmt = file->defs; stmt; stmt = stmt->next) {
598         switch (stmt->type) {
599         case STMT_INCLUDE:
600             ok = HandleIncludeKeycodes(info, (IncludeStmt *) stmt);
601             break;
602         case STMT_KEYCODE:
603             ok = HandleKeycodeDef(info, (KeycodeDef *) stmt, merge);
604             break;
605         case STMT_ALIAS:
606             ok = HandleAliasDef(info, (KeyAliasDef *) stmt, merge,
607                                 info->file_id);
608             break;
609         case STMT_VAR:
610             ok = HandleKeyNameVar(info, (VarDef *) stmt);
611             break;
612         case STMT_INDICATOR_NAME:
613             ok = HandleIndicatorNameDef(info, (IndicatorNameDef *) stmt,
614                                         merge);
615             break;
616         default:
617             log_err(info->ctx,
618                     "Keycode files may define key and indicator names only; "
619                     "Ignoring %s\n", stmt_type_to_string(stmt->type));
620             ok = false;
621             break;
622         }
623
624         if (!ok)
625             info->errorCount++;
626
627         if (info->errorCount > 10) {
628             log_err(info->ctx, "Abandoning keycodes file \"%s\"\n",
629                     file->topName);
630             break;
631         }
632     }
633 }
634
635 static void
636 ApplyAliases(KeyNamesInfo *info, struct xkb_keymap *keymap)
637 {
638     struct xkb_key *key;
639     struct xkb_key_alias *a, new;
640     AliasInfo *alias;
641
642     darray_foreach(alias, info->aliases) {
643         /* Check that ->real is a key. */
644         key = FindNamedKey(keymap, alias->real, false);
645         if (!key) {
646             log_vrb(info->ctx, 5,
647                     "Attempt to alias %s to non-existent key %s; Ignored\n",
648                     LongKeyNameText(alias->alias),
649                     LongKeyNameText(alias->real));
650             continue;
651         }
652
653         /* Check that ->alias is not a key. */
654         key = FindNamedKey(keymap, alias->alias, false);
655         if (key) {
656             log_vrb(info->ctx, 5,
657                     "Attempt to create alias with the name of a real key; "
658                     "Alias \"%s = %s\" ignored\n",
659                     LongKeyNameText(alias->alias),
660                     LongKeyNameText(alias->real));
661             continue;
662         }
663
664         /* Check that ->alias in not already an alias, and if so handle it. */
665         darray_foreach(a, keymap->key_aliases) {
666             AliasInfo old_alias;
667
668             if (KeyNameToLong(a->alias) != alias->alias)
669                 continue;
670
671             InitAliasInfo(&old_alias, MERGE_AUGMENT, 0, a->alias, a->real);
672             HandleAliasCollision(info, &old_alias, alias);
673             LongToKeyName(old_alias.alias, a->alias);
674             LongToKeyName(old_alias.real, a->real);
675             alias->alias = 0;
676         }
677         if (alias->alias == 0)
678             continue;
679
680         /* Add the alias. */
681         LongToKeyName(alias->alias, new.alias);
682         LongToKeyName(alias->real, new.real);
683         darray_append(keymap->key_aliases, new);
684     }
685
686     darray_free(info->aliases);
687 }
688
689 static bool
690 CopyKeyNamesToKeymap(struct xkb_keymap *keymap, KeyNamesInfo *info)
691 {
692     xkb_keycode_t kc;
693     xkb_led_index_t idx;
694
695     keymap->min_key_code = info->min_key_code;
696     keymap->max_key_code = info->max_key_code;
697
698     darray_resize0(keymap->keys, keymap->max_key_code + 1);
699     for (kc = info->min_key_code; kc <= info->max_key_code; kc++)
700         LongToKeyName(darray_item(info->key_names, kc).name,
701                       darray_item(keymap->keys, kc).name);
702
703     keymap->keycodes_section_name = strdup_safe(info->name);
704
705     for (idx = 0; idx < XKB_NUM_INDICATORS; idx++) {
706         IndicatorNameInfo *led = &info->indicator_names[idx];
707         if (led->name == XKB_ATOM_NONE)
708             continue;
709
710         keymap->indicators[idx].name = led->name;
711     }
712
713     ApplyAliases(info, keymap);
714
715     return true;
716 }
717
718 bool
719 CompileKeycodes(XkbFile *file, struct xkb_keymap *keymap,
720                 enum merge_mode merge)
721 {
722     KeyNamesInfo info;
723
724     InitKeyNamesInfo(&info, keymap->ctx, file->id);
725
726     HandleKeycodesFile(&info, file, merge);
727     if (info.errorCount != 0)
728         goto err_info;
729
730     if (!CopyKeyNamesToKeymap(keymap, &info))
731         goto err_info;
732
733     ClearKeyNamesInfo(&info);
734     return true;
735
736 err_info:
737     ClearKeyNamesInfo(&info);
738     return false;
739 }
740
741 struct xkb_key *
742 FindNamedKey(struct xkb_keymap *keymap, unsigned long name, bool use_aliases)
743 {
744     struct xkb_key *key;
745
746     xkb_foreach_key(key, keymap)
747         if (KeyNameToLong(key->name) == name)
748             return key;
749
750     if (use_aliases) {
751         unsigned long new_name;
752         if (FindKeyNameForAlias(keymap, name, &new_name))
753             return FindNamedKey(keymap, new_name, false);
754     }
755
756     return NULL;
757 }
758
759 bool
760 FindKeyNameForAlias(struct xkb_keymap *keymap, unsigned long lname,
761                     unsigned long *real_name)
762 {
763     char name[XKB_KEY_NAME_LENGTH];
764     struct xkb_key_alias *a;
765
766     LongToKeyName(lname, name);
767     darray_foreach(a, keymap->key_aliases) {
768         if (strncmp(name, a->alias, XKB_KEY_NAME_LENGTH) == 0) {
769             *real_name = KeyNameToLong(a->real);
770             return true;
771         }
772     }
773
774     return false;
775 }