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