1 /************************************************************
2 * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
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.
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.
25 ********************************************************/
29 #include "parseutils.h"
33 longText(unsigned long val)
37 LongToKeyName(val, buf);
38 return XkbcKeyNameText(buf);
41 /***====================================================================***/
44 LongToKeyName(unsigned long val, char *name)
46 name[0] = ((val >> 24) & 0xff);
47 name[1] = ((val >> 16) & 0xff);
48 name[2] = ((val >> 8) & 0xff);
49 name[3] = (val & 0xff);
52 /***====================================================================***/
54 typedef struct _IndicatorNameInfo {
61 typedef struct _KeyNamesInfo {
62 char *name; /* e.g. evdev+aliases(qwerty) */
65 enum merge_mode merge;
66 xkb_keycode_t computedMin; /* lowest keycode stored */
67 xkb_keycode_t computedMax; /* highest keycode stored */
68 xkb_keycode_t explicitMin;
69 xkb_keycode_t explicitMax;
70 darray(unsigned long) names;
71 darray(unsigned int) files;
72 IndicatorNameInfo *leds;
77 HandleKeycodesFile(XkbFile *file, struct xkb_keymap *keymap,
78 enum merge_mode merge,
82 ResizeKeyNameArrays(KeyNamesInfo *info, int newMax)
84 if (newMax < darray_size(info->names))
87 darray_resize0(info->names, newMax + 1);
88 darray_resize0(info->files, newMax + 1);
92 InitIndicatorNameInfo(IndicatorNameInfo * ii, KeyNamesInfo * info)
95 ii->defs.merge = info->merge;
96 ii->defs.file_id = info->file_id;
99 ii->name = XKB_ATOM_NONE;
104 ClearIndicatorNameInfo(IndicatorNameInfo * ii, KeyNamesInfo * info)
106 if (ii == info->leds) {
107 ClearCommonInfo(&ii->defs);
112 static IndicatorNameInfo *
113 NextIndicatorName(KeyNamesInfo * info)
115 IndicatorNameInfo *ii;
117 ii = uTypedAlloc(IndicatorNameInfo);
119 InitIndicatorNameInfo(ii, info);
120 info->leds = AddCommonInfo(&info->leds->defs, &ii->defs);
125 static IndicatorNameInfo *
126 FindIndicatorByIndex(KeyNamesInfo * info, int ndx)
128 IndicatorNameInfo *old;
130 for (old = info->leds; old != NULL;
131 old = (IndicatorNameInfo *) old->defs.next) {
138 static IndicatorNameInfo *
139 FindIndicatorByName(KeyNamesInfo * info, xkb_atom_t name)
141 IndicatorNameInfo *old;
143 for (old = info->leds; old != NULL;
144 old = (IndicatorNameInfo *) old->defs.next) {
145 if (old->name == name)
152 AddIndicatorName(KeyNamesInfo *info, struct xkb_keymap *keymap,
153 enum merge_mode merge,
154 IndicatorNameInfo *new)
156 IndicatorNameInfo *old;
159 replace = (merge == MERGE_REPLACE) || (merge == MERGE_OVERRIDE);
160 old = FindIndicatorByName(info, new->name);
162 if (((old->defs.file_id == new->defs.file_id) && (warningLevel > 0))
163 || (warningLevel > 9)) {
164 WARN("Multiple indicators named %s\n",
165 xkb_atom_text(keymap->ctx, new->name));
166 if (old->ndx == new->ndx) {
167 if (old->virtual != new->virtual) {
169 old->virtual = new->virtual;
170 ACTION("Using %s instead of %s\n",
171 (old->virtual ? "virtual" : "real"),
172 (old->virtual ? "real" : "virtual"));
175 ACTION("Identical definitions ignored\n");
181 ACTION("Ignoring %d, using %d\n", old->ndx, new->ndx);
183 ACTION("Using %d, ignoring %d\n", old->ndx, new->ndx);
186 if (info->leds == old)
187 info->leds = (IndicatorNameInfo *) old->defs.next;
189 IndicatorNameInfo *tmp;
192 tmp = (IndicatorNameInfo *) tmp->defs.next) {
193 if (tmp->defs.next == (CommonInfo *) old) {
194 tmp->defs.next = old->defs.next;
203 old = FindIndicatorByIndex(info, new->ndx);
205 if (((old->defs.file_id == new->defs.file_id) && (warningLevel > 0))
206 || (warningLevel > 9)) {
207 WARN("Multiple names for indicator %d\n", new->ndx);
208 if ((old->name == new->name) && (old->virtual == new->virtual))
209 ACTION("Identical definitions ignored\n");
211 const char *oldType, *newType;
212 xkb_atom_t using, ignoring;
214 oldType = "virtual indicator";
216 oldType = "real indicator";
218 newType = "virtual indicator";
220 newType = "real indicator";
223 ignoring = old->name;
227 ignoring = new->name;
229 ACTION("Using %s %s, ignoring %s %s\n",
230 oldType, xkb_atom_text(keymap->ctx, using),
231 newType, xkb_atom_text(keymap->ctx, ignoring));
235 old->name = new->name;
236 old->virtual = new->virtual;
241 new = NextIndicatorName(info);
243 WSGO("Couldn't allocate name for indicator %d\n", old->ndx);
247 new->name = old->name;
249 new->virtual = old->virtual;
254 ClearKeyNamesInfo(KeyNamesInfo * info)
258 info->computedMax = info->explicitMax = info->explicitMin = 0;
259 info->computedMin = XKB_KEYCODE_MAX;
260 darray_free(info->names);
261 darray_free(info->files);
263 ClearIndicatorNameInfo(info->leds, info);
265 ClearAliases(&info->aliases);
269 InitKeyNamesInfo(KeyNamesInfo * info, unsigned file_id)
273 info->aliases = NULL;
274 info->file_id = file_id;
275 darray_init(info->names);
276 darray_init(info->files);
277 ClearKeyNamesInfo(info);
278 info->errorCount = 0;
282 FindKeyByLong(KeyNamesInfo * info, unsigned long name)
286 for (i = info->computedMin; i <= info->computedMax; i++)
287 if (darray_item(info->names, i) == name)
294 * Store the name of the key as a long in the info struct under the given
295 * keycode. If the same keys is referred to twice, print a warning.
296 * Note that the key's name is stored as a long, the keycode is the index.
299 AddKeyName(KeyNamesInfo * info,
300 xkb_keycode_t kc, char *name, enum merge_mode merge,
301 unsigned file_id, bool reportCollisions)
306 ResizeKeyNameArrays(info, kc);
308 if (kc < info->computedMin)
309 info->computedMin = kc;
310 if (kc > info->computedMax)
311 info->computedMax = kc;
312 lval = KeyNameToLong(name);
314 if (reportCollisions) {
315 reportCollisions = (warningLevel > 7 ||
317 file_id == darray_item(info->files, kc)));
320 if (darray_item(info->names, kc) != 0) {
323 LongToKeyName(darray_item(info->names, kc), buf);
325 if (darray_item(info->names, kc) == lval && reportCollisions) {
326 WARN("Multiple identical key name definitions\n");
327 ACTION("Later occurences of \"<%s> = %d\" ignored\n",
331 if (merge == MERGE_AUGMENT) {
332 if (reportCollisions) {
333 WARN("Multiple names for keycode %d\n", kc);
334 ACTION("Using <%s>, ignoring <%s>\n", buf, name);
339 if (reportCollisions) {
340 WARN("Multiple names for keycode %d\n", kc);
341 ACTION("Using <%s>, ignoring <%s>\n", name, buf);
343 darray_item(info->names, kc) = 0;
344 darray_item(info->files, kc) = 0;
347 old = FindKeyByLong(info, lval);
348 if ((old != 0) && (old != kc)) {
349 if (merge == MERGE_OVERRIDE) {
350 darray_item(info->names, old) = 0;
351 darray_item(info->files, old) = 0;
352 if (reportCollisions) {
353 WARN("Key name <%s> assigned to multiple keys\n", name);
354 ACTION("Using %d, ignoring %d\n", kc, old);
358 if ((reportCollisions) && (warningLevel > 3)) {
359 WARN("Key name <%s> assigned to multiple keys\n", name);
360 ACTION("Using %d, ignoring %d\n", old, kc);
365 darray_item(info->names, kc) = lval;
366 darray_item(info->files, kc) = file_id;
370 /***====================================================================***/
373 MergeIncludedKeycodes(KeyNamesInfo *into, struct xkb_keymap *keymap,
374 KeyNamesInfo *from, enum merge_mode merge)
379 if (from->errorCount > 0) {
380 into->errorCount += from->errorCount;
383 if (into->name == NULL) {
384 into->name = from->name;
388 ResizeKeyNameArrays(into, from->computedMax);
390 for (i = from->computedMin; i <= from->computedMax; i++) {
391 if (darray_item(from->names, i) == 0)
393 LongToKeyName(darray_item(from->names, i), buf);
395 if (!AddKeyName(into, i, buf, merge, from->file_id, false))
399 IndicatorNameInfo *led, *next;
400 for (led = from->leds; led != NULL; led = next) {
401 if (merge != MERGE_DEFAULT)
402 led->defs.merge = merge;
403 if (!AddIndicatorName(into, keymap, led->defs.merge, led))
405 next = (IndicatorNameInfo *) led->defs.next;
408 if (!MergeAliases(&into->aliases, &from->aliases, merge))
410 if (from->explicitMin != 0) {
411 if ((into->explicitMin == 0)
412 || (into->explicitMin > from->explicitMin))
413 into->explicitMin = from->explicitMin;
415 if (from->explicitMax > 0) {
416 if ((into->explicitMax == 0)
417 || (into->explicitMax < from->explicitMax))
418 into->explicitMax = from->explicitMax;
423 * Handle the given include statement (e.g. "include "evdev+aliases(qwerty)").
425 * @param stmt The include statement from the keymap file.
426 * @param keymap Unused for all but the keymap->flags.
427 * @param info Struct to store the key info in.
430 HandleIncludeKeycodes(IncludeStmt *stmt, struct xkb_keymap *keymap,
433 enum merge_mode newMerge;
435 KeyNamesInfo included;
438 memset(&included, 0, sizeof(included));
441 if ((stmt->file == NULL) && (stmt->map == NULL)) {
444 memset(info, 0, sizeof(KeyNamesInfo));
446 else if (stmt->file && strcmp(stmt->file, "computed") == 0) {
447 keymap->flags |= AutoKeyNames;
448 info->explicitMin = 0;
449 info->explicitMax = XKB_KEYCODE_MAX;
450 return (info->errorCount == 0);
451 } /* parse file, store returned info in the xkb struct */
452 else if (ProcessIncludeFile(keymap->ctx, stmt, FILE_TYPE_KEYCODES, &rtrn,
454 InitKeyNamesInfo(&included, rtrn->id);
455 HandleKeycodesFile(rtrn, keymap, MERGE_OVERRIDE, &included);
456 if (stmt->stmt != NULL) {
458 included.name = stmt->stmt;
464 info->errorCount += 10; /* XXX: why 10?? */
467 /* Do we have more than one include statement? */
468 if ((stmt->next != NULL) && (included.errorCount < 1)) {
471 KeyNamesInfo next_incl;
473 for (next = stmt->next; next != NULL; next = next->next) {
474 if ((next->file == NULL) && (next->map == NULL)) {
476 MergeIncludedKeycodes(&included, keymap, info, next->merge);
477 ClearKeyNamesInfo(info);
479 else if (ProcessIncludeFile(keymap->ctx, next, FILE_TYPE_KEYCODES,
481 InitKeyNamesInfo(&next_incl, rtrn->id);
482 HandleKeycodesFile(rtrn, keymap, MERGE_OVERRIDE, &next_incl);
483 MergeIncludedKeycodes(&included, keymap, &next_incl, op);
484 ClearKeyNamesInfo(&next_incl);
488 info->errorCount += 10; /* XXX: Why 10?? */
489 ClearKeyNamesInfo(&included);
497 MergeIncludedKeycodes(info, keymap, &included, newMerge);
498 ClearKeyNamesInfo(&included);
500 return (info->errorCount == 0);
504 * Parse the given statement and store the output in the info struct.
508 HandleKeycodeDef(KeycodeDef *stmt, enum merge_mode merge, KeyNamesInfo *info)
510 if ((info->explicitMin != 0 && stmt->value < info->explicitMin) ||
511 (info->explicitMax != 0 && stmt->value > info->explicitMax)) {
512 ERROR("Illegal keycode %lu for name <%s>\n", stmt->value, stmt->name);
513 ACTION("Must be in the range %d-%d inclusive\n",
515 info->explicitMax ? info->explicitMax : XKB_KEYCODE_MAX);
518 if (stmt->merge != MERGE_DEFAULT) {
519 if (stmt->merge == MERGE_REPLACE)
520 merge = MERGE_OVERRIDE;
524 return AddKeyName(info, stmt->value, stmt->name, merge, info->file_id,
528 #define MIN_KEYCODE_DEF 0
529 #define MAX_KEYCODE_DEF 1
532 * Handle the minimum/maximum statement of the xkb file.
533 * Sets explicitMin/Max of the info struct.
535 * @return 1 on success, 0 otherwise.
538 HandleKeyNameVar(VarDef *stmt, struct xkb_keymap *keymap, KeyNamesInfo *info)
540 ExprResult tmp, field;
544 if (ExprResolveLhs(keymap, stmt->name, &tmp, &field, &arrayNdx) == 0)
545 return 0; /* internal error, already reported */
547 if (tmp.str != NULL) {
548 ERROR("Unknown element %s encountered\n", tmp.str);
549 ACTION("Default for field %s ignored\n", field.str);
552 if (strcasecmp(field.str, "minimum") == 0)
553 which = MIN_KEYCODE_DEF;
554 else if (strcasecmp(field.str, "maximum") == 0)
555 which = MAX_KEYCODE_DEF;
557 ERROR("Unknown field encountered\n");
558 ACTION("Assigment to field %s ignored\n", field.str);
561 if (arrayNdx != NULL) {
562 ERROR("The %s setting is not an array\n", field.str);
563 ACTION("Illegal array reference ignored\n");
567 if (ExprResolveKeyCode(keymap->ctx, stmt->value, &tmp) == 0) {
568 ACTION("Assignment to field %s ignored\n", field.str);
571 if (tmp.uval > XKB_KEYCODE_MAX) {
573 ("Illegal keycode %d (must be in the range %d-%d inclusive)\n",
574 tmp.uval, 0, XKB_KEYCODE_MAX);
575 ACTION("Value of \"%s\" not changed\n", field.str);
578 if (which == MIN_KEYCODE_DEF) {
579 if ((info->explicitMax > 0) && (info->explicitMax < tmp.uval)) {
581 ("Minimum key code (%d) must be <= maximum key code (%d)\n",
582 tmp.uval, info->explicitMax);
583 ACTION("Minimum key code value not changed\n");
586 if ((info->computedMax > 0) && (info->computedMin < tmp.uval)) {
588 ("Minimum key code (%d) must be <= lowest defined key (%d)\n",
589 tmp.uval, info->computedMin);
590 ACTION("Minimum key code value not changed\n");
593 info->explicitMin = tmp.uval;
595 if (which == MAX_KEYCODE_DEF) {
596 if ((info->explicitMin > 0) && (info->explicitMin > tmp.uval)) {
597 ERROR("Maximum code (%d) must be >= minimum key code (%d)\n",
598 tmp.uval, info->explicitMin);
599 ACTION("Maximum code value not changed\n");
602 if ((info->computedMax > 0) && (info->computedMax > tmp.uval)) {
604 ("Maximum code (%d) must be >= highest defined key (%d)\n",
605 tmp.uval, info->computedMax);
606 ACTION("Maximum code value not changed\n");
609 info->explicitMax = tmp.uval;
621 HandleIndicatorNameDef(IndicatorNameDef *def, struct xkb_keymap *keymap,
622 enum merge_mode merge, KeyNamesInfo *info)
624 IndicatorNameInfo ii;
627 if ((def->ndx < 1) || (def->ndx > XkbNumIndicators)) {
629 ERROR("Name specified for illegal indicator index %d\n", def->ndx);
633 InitIndicatorNameInfo(&ii, info);
635 if (!ExprResolveString(keymap->ctx, def->name, &tmp)) {
637 snprintf(buf, sizeof(buf), "%d", def->ndx);
639 return ReportBadType("indicator", "name", buf, "string");
641 ii.name = xkb_atom_intern(keymap->ctx, tmp.str);
643 ii.virtual = def->virtual;
644 if (!AddIndicatorName(info, keymap, merge, &ii))
650 * Handle the xkb_keycodes section of a xkb file.
651 * All information about parsed keys is stored in the info struct.
653 * Such a section may have include statements, in which case this function is
654 * semi-recursive (it calls HandleIncludeKeycodes, which may call
655 * HandleKeycodesFile again).
657 * @param file The input file (parsed xkb_keycodes section)
658 * @param xkb Necessary to pass down, may have flags changed.
659 * @param merge Merge strategy (MERGE_OVERRIDE, etc.)
660 * @param info Struct to contain the fully parsed key information.
663 HandleKeycodesFile(XkbFile *file, struct xkb_keymap *keymap,
664 enum merge_mode merge, KeyNamesInfo *info)
669 info->name = uDupString(file->name);
673 switch (stmt->stmtType) {
674 case StmtInclude: /* e.g. include "evdev+aliases(qwerty)" */
675 if (!HandleIncludeKeycodes((IncludeStmt *) stmt, keymap, info))
678 case StmtKeycodeDef: /* e.g. <ESC> = 9; */
679 if (!HandleKeycodeDef((KeycodeDef *) stmt, merge, info))
682 case StmtKeyAliasDef: /* e.g. alias <MENU> = <COMP>; */
683 if (!HandleAliasDef((KeyAliasDef *) stmt, merge, info->file_id,
687 case StmtVarDef: /* e.g. minimum, maximum */
688 if (!HandleKeyNameVar((VarDef *) stmt, keymap, info))
691 case StmtIndicatorNameDef: /* e.g. indicator 1 = "Caps Lock"; */
692 if (!HandleIndicatorNameDef((IndicatorNameDef *) stmt, keymap,
698 ERROR("Keycode files may define key and indicator names only\n");
699 ACTION("Ignoring definition of %s\n",
701 StmtInterpDef) ? "a symbol interpretation" :
702 "virtual modifiers"));
706 WSGO("Unexpected statement type %d in HandleKeycodesFile\n",
711 if (info->errorCount > 10) {
713 ERROR("Too many errors\n");
715 ACTION("Abandoning keycodes file \"%s\"\n", file->topName);
722 * Compile the xkb_keycodes section, parse it's output, return the results.
724 * @param file The parsed XKB file (may have include statements requiring
726 * @param result The effective keycodes, as gathered from the file.
727 * @param merge Merge strategy.
729 * @return true on success, false otherwise.
732 CompileKeycodes(XkbFile *file, struct xkb_keymap *keymap,
733 enum merge_mode merge)
736 KeyNamesInfo info; /* contains all the info after parsing */
738 InitKeyNamesInfo(&info, file->id);
740 HandleKeycodesFile(file, keymap, merge, &info);
742 /* all the keys are now stored in info */
744 if (info.errorCount != 0)
747 if (info.explicitMin > 0) /* if "minimum" statement was present */
748 keymap->min_key_code = info.explicitMin;
750 keymap->min_key_code = info.computedMin;
752 if (info.explicitMax > 0) /* if "maximum" statement was present */
753 keymap->max_key_code = info.explicitMax;
755 keymap->max_key_code = info.computedMax;
757 darray_resize0(keymap->key_names, keymap->max_key_code + 1);
758 for (i = info.computedMin; i <= info.computedMax; i++)
759 LongToKeyName(darray_item(info.names, i),
760 darray_item(keymap->key_names, i).name);
763 keymap->keycodes_section_name = strdup(info.name);
766 IndicatorNameInfo *ii;
768 for (ii = info.leds; ii; ii = (IndicatorNameInfo *) ii->defs.next) {
769 free(keymap->indicator_names[ii->ndx - 1]);
770 keymap->indicator_names[ii->ndx - 1] =
771 xkb_atom_strdup(keymap->ctx, ii->name);
775 ApplyAliases(keymap, &info.aliases);
777 ClearKeyNamesInfo(&info);
781 ClearKeyNamesInfo(&info);