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