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