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