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 ********************************************************/
34 #include "parseutils.h"
37 longText(unsigned long val)
41 LongToKeyName(val, buf);
42 return XkbcKeyNameText(buf);
45 /***====================================================================***/
48 LongToKeyName(unsigned long val, char *name)
50 name[0] = ((val >> 24) & 0xff);
51 name[1] = ((val >> 16) & 0xff);
52 name[2] = ((val >> 8) & 0xff);
53 name[3] = (val & 0xff);
56 /***====================================================================***/
58 typedef struct _IndicatorNameInfo
66 typedef struct _KeyNamesInfo
68 char *name; /* e.g. evdev+aliases(qwerty) */
72 xkb_keycode_t computedMin; /* lowest keycode stored */
73 xkb_keycode_t computedMax; /* highest keycode stored */
74 xkb_keycode_t explicitMin;
75 xkb_keycode_t explicitMax;
76 xkb_keycode_t arraySize;
79 unsigned char *has_alt_forms;
80 IndicatorNameInfo *leds;
84 static void HandleKeycodesFile(XkbFile * file,
85 struct xkb_desc * xkb,
90 ResizeKeyNameArrays(KeyNamesInfo *info, int newMax)
95 tmp = _XkbTypedRealloc(info->names, newMax + 1, unsigned long);
98 ("Couldn't reallocate for larger maximum keycode (%d)\n",
100 ACTION("Maximum key value not changed\n");
104 for (i = info->arraySize + 1; i <= newMax; i++)
107 tmp = _XkbTypedRealloc(info->files, newMax + 1, unsigned);
110 ("Couldn't reallocate for larger maximum keycode (%d)\n",
112 ACTION("Maximum key value not changed\n");
116 for (i = info->arraySize + 1; i <= newMax; i++)
119 tmp = _XkbTypedRealloc(info->has_alt_forms, newMax + 1, unsigned char);
122 ("Couldn't reallocate for larger maximum keycode (%d)\n",
124 ACTION("Maximum key value not changed\n");
127 info->has_alt_forms = tmp;
128 for (i = info->arraySize + 1; i <= newMax; i++)
129 info->has_alt_forms[i] = 0;
131 info->arraySize = newMax;
137 InitIndicatorNameInfo(IndicatorNameInfo * ii, KeyNamesInfo * info)
139 ii->defs.defined = 0;
140 ii->defs.merge = info->merge;
141 ii->defs.fileID = info->fileID;
142 ii->defs.next = NULL;
149 ClearIndicatorNameInfo(IndicatorNameInfo * ii, KeyNamesInfo * info)
151 if (ii == info->leds)
153 ClearCommonInfo(&ii->defs);
158 static IndicatorNameInfo *
159 NextIndicatorName(KeyNamesInfo * info)
161 IndicatorNameInfo *ii;
163 ii = uTypedAlloc(IndicatorNameInfo);
166 InitIndicatorNameInfo(ii, info);
167 info->leds = (IndicatorNameInfo *) AddCommonInfo(&info->leds->defs,
173 static IndicatorNameInfo *
174 FindIndicatorByIndex(KeyNamesInfo * info, int ndx)
176 IndicatorNameInfo *old;
178 for (old = info->leds; old != NULL;
179 old = (IndicatorNameInfo *) old->defs.next)
187 static IndicatorNameInfo *
188 FindIndicatorByName(KeyNamesInfo * info, uint32_t name)
190 IndicatorNameInfo *old;
192 for (old = info->leds; old != NULL;
193 old = (IndicatorNameInfo *) old->defs.next)
195 if (old->name == name)
202 AddIndicatorName(KeyNamesInfo * info, IndicatorNameInfo * new)
204 IndicatorNameInfo *old;
207 replace = (new->defs.merge == MergeReplace) ||
208 (new->defs.merge == MergeOverride);
209 old = FindIndicatorByName(info, new->name);
212 if (((old->defs.fileID == new->defs.fileID) && (warningLevel > 0))
213 || (warningLevel > 9))
215 WARN("Multiple indicators named %s\n", XkbcAtomText(new->name));
216 if (old->ndx == new->ndx)
218 if (old->virtual != new->virtual)
221 old->virtual = new->virtual;
222 ACTION("Using %s instead of %s\n",
223 (old->virtual ? "virtual" : "real"),
224 (old->virtual ? "real" : "virtual"));
228 ACTION("Identical definitions ignored\n");
235 ACTION("Ignoring %d, using %d\n", old->ndx, new->ndx);
237 ACTION("Using %d, ignoring %d\n", old->ndx, new->ndx);
241 if (info->leds == old)
242 info->leds = (IndicatorNameInfo *) old->defs.next;
245 IndicatorNameInfo *tmp;
248 tmp = (IndicatorNameInfo *) tmp->defs.next)
250 if (tmp->defs.next == (CommonInfo *) old)
252 tmp->defs.next = old->defs.next;
261 old = FindIndicatorByIndex(info, new->ndx);
264 if (((old->defs.fileID == new->defs.fileID) && (warningLevel > 0))
265 || (warningLevel > 9))
267 WARN("Multiple names for indicator %d\n", new->ndx);
268 if ((old->name == new->name) && (old->virtual == new->virtual))
269 ACTION("Identical definitions ignored\n");
272 const char *oldType, *newType;
273 uint32_t using, ignoring;
275 oldType = "virtual indicator";
277 oldType = "real indicator";
279 newType = "virtual indicator";
281 newType = "real indicator";
285 ignoring = old->name;
290 ignoring = new->name;
292 ACTION("Using %s %s, ignoring %s %s\n",
293 oldType, XkbcAtomText(using),
294 newType, XkbcAtomText(ignoring));
299 old->name = new->name;
300 old->virtual = new->virtual;
305 new = NextIndicatorName(info);
308 WSGO("Couldn't allocate name for indicator %d\n", old->ndx);
312 new->name = old->name;
314 new->virtual = old->virtual;
319 ClearKeyNamesInfo(KeyNamesInfo * info)
323 info->computedMax = info->explicitMax = info->explicitMin = 0;
324 info->computedMin = XKB_KEYCODE_MAX;
330 free(info->has_alt_forms);
331 info->has_alt_forms = NULL;
333 ClearIndicatorNameInfo(info->leds, info);
335 ClearAliases(&info->aliases);
339 InitKeyNamesInfo(KeyNamesInfo * info)
343 info->aliases = NULL;
346 info->has_alt_forms = NULL;
347 ClearKeyNamesInfo(info);
348 info->errorCount = 0;
352 FindKeyByLong(KeyNamesInfo * info, unsigned long name)
356 for (i = info->computedMin; i <= info->computedMax; i++)
358 if (info->names[i] == name)
365 * Store the name of the key as a long in the info struct under the given
366 * keycode. If the same keys is referred to twice, print a warning.
367 * Note that the key's name is stored as a long, the keycode is the index.
370 AddKeyName(KeyNamesInfo * info,
372 char *name, unsigned merge, unsigned fileID, Bool reportCollisions)
377 if (kc > info->arraySize && !ResizeKeyNameArrays(info, kc)) {
378 ERROR("Couldn't resize KeyNames arrays for keycode %d\n", kc);
379 ACTION("Ignoring key %d\n", kc);
382 if (kc < info->computedMin)
383 info->computedMin = kc;
384 if (kc > info->computedMax)
385 info->computedMax = kc;
386 lval = KeyNameToLong(name);
388 if (reportCollisions)
390 reportCollisions = ((warningLevel > 7) ||
392 && (fileID == info->files[kc])));
395 if (info->names[kc] != 0)
399 LongToKeyName(info->names[kc], buf);
401 if (info->names[kc] == lval)
403 if (info->has_alt_forms[kc] || (merge == MergeAltForm))
405 info->has_alt_forms[kc] = True;
407 else if (reportCollisions)
409 WARN("Multiple identical key name definitions\n");
410 ACTION("Later occurences of \"<%s> = %d\" ignored\n",
415 if (merge == MergeAugment)
417 if (reportCollisions)
419 WARN("Multiple names for keycode %d\n", kc);
420 ACTION("Using <%s>, ignoring <%s>\n", buf, name);
426 if (reportCollisions)
428 WARN("Multiple names for keycode %d\n", kc);
429 ACTION("Using <%s>, ignoring <%s>\n", name, buf);
435 old = FindKeyByLong(info, lval);
436 if ((old != 0) && (old != kc))
438 if (merge == MergeOverride)
440 info->names[old] = 0;
441 info->files[old] = 0;
442 info->has_alt_forms[old] = True;
443 if (reportCollisions)
445 WARN("Key name <%s> assigned to multiple keys\n", name);
446 ACTION("Using %d, ignoring %d\n", kc, old);
449 else if (merge != MergeAltForm)
451 if ((reportCollisions) && (warningLevel > 3))
453 WARN("Key name <%s> assigned to multiple keys\n", name);
454 ACTION("Using %d, ignoring %d\n", old, kc);
456 ("Use 'alternate' keyword to assign the same name to multiple keys\n");
462 info->has_alt_forms[old] = True;
465 info->names[kc] = lval;
466 info->files[kc] = fileID;
467 info->has_alt_forms[kc] = (merge == MergeAltForm);
471 /***====================================================================***/
474 MergeIncludedKeycodes(KeyNamesInfo * into, KeyNamesInfo * from,
480 if (from->errorCount > 0)
482 into->errorCount += from->errorCount;
485 if (into->name == NULL)
487 into->name = from->name;
490 if (from->computedMax > into->arraySize &&
491 !ResizeKeyNameArrays(into, from->computedMax)) {
492 ERROR("Couldn't resize KeyNames arrays for key %d\n",
494 ACTION("Ignoring include file %s\n", from->name);
495 into->errorCount += 10;
498 for (i = from->computedMin; i <= from->computedMax; i++)
501 if (from->names[i] == 0)
503 LongToKeyName(from->names[i], buf);
505 if (from->has_alt_forms[i])
506 thisMerge = MergeAltForm;
509 if (!AddKeyName(into, i, buf, thisMerge, from->fileID, False))
514 IndicatorNameInfo *led, *next;
515 for (led = from->leds; led != NULL; led = next)
517 if (merge != MergeDefault)
518 led->defs.merge = merge;
519 if (!AddIndicatorName(into, led))
521 next = (IndicatorNameInfo *) led->defs.next;
524 if (!MergeAliases(&into->aliases, &from->aliases, merge))
526 if (from->explicitMin != 0)
528 if ((into->explicitMin == 0)
529 || (into->explicitMin > from->explicitMin))
530 into->explicitMin = from->explicitMin;
532 if (from->explicitMax > 0)
534 if ((into->explicitMax == 0)
535 || (into->explicitMax < from->explicitMax))
536 into->explicitMax = from->explicitMax;
541 * Handle the given include statement (e.g. "include "evdev+aliases(qwerty)").
543 * @param stmt The include statement from the keymap file.
544 * @param xkb Unused for all but the xkb->flags.
545 * @param info Struct to store the key info in.
548 HandleIncludeKeycodes(IncludeStmt * stmt, struct xkb_desc * xkb, KeyNamesInfo * info)
552 KeyNamesInfo included = {NULL};
556 if ((stmt->file == NULL) && (stmt->map == NULL))
560 memset(info, 0, sizeof(KeyNamesInfo));
562 else if (stmt->file && strcmp(stmt->file, "computed") == 0)
564 xkb->flags |= AutoKeyNames;
565 info->explicitMin = 0;
566 info->explicitMax = XKB_KEYCODE_MAX;
567 return (info->errorCount == 0);
568 } /* parse file, store returned info in the xkb struct */
569 else if (ProcessIncludeFile(stmt, XkmKeyNamesIndex, &rtrn, &newMerge))
571 InitKeyNamesInfo(&included);
572 HandleKeycodesFile(rtrn, xkb, MergeOverride, &included);
573 if (stmt->stmt != NULL)
576 included.name = stmt->stmt;
583 info->errorCount += 10; /* XXX: why 10?? */
586 /* Do we have more than one include statement? */
587 if ((stmt->next != NULL) && (included.errorCount < 1))
591 KeyNamesInfo next_incl;
593 for (next = stmt->next; next != NULL; next = next->next)
595 if ((next->file == NULL) && (next->map == NULL))
598 MergeIncludedKeycodes(&included, info, next->merge);
599 ClearKeyNamesInfo(info);
601 else if (ProcessIncludeFile(next, XkmKeyNamesIndex, &rtrn, &op))
603 InitKeyNamesInfo(&next_incl);
604 HandleKeycodesFile(rtrn, xkb, MergeOverride, &next_incl);
605 MergeIncludedKeycodes(&included, &next_incl, op);
606 ClearKeyNamesInfo(&next_incl);
611 info->errorCount += 10; /* XXX: Why 10?? */
612 ClearKeyNamesInfo(&included);
621 MergeIncludedKeycodes(info, &included, newMerge);
622 ClearKeyNamesInfo(&included);
624 return (info->errorCount == 0);
628 * Parse the given statement and store the output in the info struct.
632 HandleKeycodeDef(KeycodeDef * stmt, unsigned merge, KeyNamesInfo * info)
634 if ((info->explicitMin != 0 && stmt->value < info->explicitMin) ||
635 (info->explicitMax != 0 && stmt->value > info->explicitMax))
637 ERROR("Illegal keycode %lu for name <%s>\n", stmt->value, stmt->name);
638 ACTION("Must be in the range %d-%d inclusive\n",
640 info->explicitMax ? info->explicitMax : XKB_KEYCODE_MAX);
643 if (stmt->merge != MergeDefault)
645 if (stmt->merge == MergeReplace)
646 merge = MergeOverride;
650 return AddKeyName(info, stmt->value, stmt->name, merge, info->fileID,
654 #define MIN_KEYCODE_DEF 0
655 #define MAX_KEYCODE_DEF 1
658 * Handle the minimum/maximum statement of the xkb file.
659 * Sets explicitMin/Max of the info struct.
661 * @return 1 on success, 0 otherwise.
664 HandleKeyNameVar(VarDef * stmt, KeyNamesInfo * info)
666 ExprResult tmp, field;
670 if (ExprResolveLhs(stmt->name, &tmp, &field, &arrayNdx) == 0)
671 return 0; /* internal error, already reported */
675 ERROR("Unknown element %s encountered\n", tmp.str);
676 ACTION("Default for field %s ignored\n", field.str);
679 if (uStrCaseCmp(field.str, "minimum") == 0)
680 which = MIN_KEYCODE_DEF;
681 else if (uStrCaseCmp(field.str, "maximum") == 0)
682 which = MAX_KEYCODE_DEF;
685 ERROR("Unknown field encountered\n");
686 ACTION("Assigment to field %s ignored\n", field.str);
689 if (arrayNdx != NULL)
691 ERROR("The %s setting is not an array\n", field.str);
692 ACTION("Illegal array reference ignored\n");
696 if (ExprResolveKeyCode(stmt->value, &tmp) == 0)
698 ACTION("Assignment to field %s ignored\n", field.str);
701 if (tmp.uval > XKB_KEYCODE_MAX)
704 ("Illegal keycode %d (must be in the range %d-%d inclusive)\n",
705 tmp.uval, 0, XKB_KEYCODE_MAX);
706 ACTION("Value of \"%s\" not changed\n", field.str);
709 if (which == MIN_KEYCODE_DEF)
711 if ((info->explicitMax > 0) && (info->explicitMax < tmp.uval))
714 ("Minimum key code (%d) must be <= maximum key code (%d)\n",
715 tmp.uval, info->explicitMax);
716 ACTION("Minimum key code value not changed\n");
719 if ((info->computedMax > 0) && (info->computedMin < tmp.uval))
722 ("Minimum key code (%d) must be <= lowest defined key (%d)\n",
723 tmp.uval, info->computedMin);
724 ACTION("Minimum key code value not changed\n");
727 info->explicitMin = tmp.uval;
729 if (which == MAX_KEYCODE_DEF)
731 if ((info->explicitMin > 0) && (info->explicitMin > tmp.uval))
733 ERROR("Maximum code (%d) must be >= minimum key code (%d)\n",
734 tmp.uval, info->explicitMin);
735 ACTION("Maximum code value not changed\n");
738 if ((info->computedMax > 0) && (info->computedMax > tmp.uval))
741 ("Maximum code (%d) must be >= highest defined key (%d)\n",
742 tmp.uval, info->computedMax);
743 ACTION("Maximum code value not changed\n");
746 info->explicitMax = tmp.uval;
752 HandleIndicatorNameDef(IndicatorNameDef * def,
753 unsigned merge, KeyNamesInfo * info)
755 IndicatorNameInfo ii;
758 if ((def->ndx < 1) || (def->ndx > XkbNumIndicators))
761 ERROR("Name specified for illegal indicator index %d\n", def->ndx);
765 InitIndicatorNameInfo(&ii, info);
767 if (!ExprResolveString(def->name, &tmp))
770 snprintf(buf, sizeof(buf), "%d", def->ndx);
772 return ReportBadType("indicator", "name", buf, "string");
774 ii.name = xkb_intern_atom(tmp.str);
776 ii.virtual = def->virtual;
777 if (!AddIndicatorName(info, &ii))
783 * Handle the xkb_keycodes section of a xkb file.
784 * All information about parsed keys is stored in the info struct.
786 * Such a section may have include statements, in which case this function is
787 * semi-recursive (it calls HandleIncludeKeycodes, which may call
788 * HandleKeycodesFile again).
790 * @param file The input file (parsed xkb_keycodes section)
791 * @param xkb Necessary to pass down, may have flags changed.
792 * @param merge Merge strategy (MergeOverride, etc.)
793 * @param info Struct to contain the fully parsed key information.
796 HandleKeycodesFile(XkbFile * file,
797 struct xkb_desc * xkb, unsigned merge, KeyNamesInfo * info)
801 info->name = _XkbDupString(file->name);
805 switch (stmt->stmtType)
807 case StmtInclude: /* e.g. include "evdev+aliases(qwerty)" */
808 if (!HandleIncludeKeycodes((IncludeStmt *) stmt, xkb, info))
811 case StmtKeycodeDef: /* e.g. <ESC> = 9; */
812 if (!HandleKeycodeDef((KeycodeDef *) stmt, merge, info))
815 case StmtKeyAliasDef: /* e.g. alias <MENU> = <COMP>; */
816 if (!HandleAliasDef((KeyAliasDef *) stmt,
817 merge, info->fileID, &info->aliases))
820 case StmtVarDef: /* e.g. minimum, maximum */
821 if (!HandleKeyNameVar((VarDef *) stmt, info))
824 case StmtIndicatorNameDef: /* e.g. indicator 1 = "Caps Lock"; */
825 if (!HandleIndicatorNameDef((IndicatorNameDef *) stmt,
833 ERROR("Keycode files may define key and indicator names only\n");
834 ACTION("Ignoring definition of %s\n",
836 StmtInterpDef) ? "a symbol interpretation" :
837 "virtual modifiers"));
841 WSGO("Unexpected statement type %d in HandleKeycodesFile\n",
846 if (info->errorCount > 10)
849 ERROR("Too many errors\n");
851 ACTION("Abandoning keycodes file \"%s\"\n", file->topName);
858 * Compile the xkb_keycodes section, parse it's output, return the results.
860 * @param file The parsed XKB file (may have include statements requiring
862 * @param result The effective keycodes, as gathered from the file.
863 * @param merge Merge strategy.
865 * @return True on success, False otherwise.
868 CompileKeycodes(XkbFile *file, struct xkb_desc * xkb, unsigned merge)
870 KeyNamesInfo info; /* contains all the info after parsing */
872 InitKeyNamesInfo(&info);
873 HandleKeycodesFile(file, xkb, merge, &info);
875 /* all the keys are now stored in info */
877 if (info.errorCount == 0)
879 if (info.explicitMin > 0) /* if "minimum" statement was present */
880 xkb->min_key_code = info.explicitMin;
882 xkb->min_key_code = info.computedMin;
883 if (info.explicitMax > 0) /* if "maximum" statement was present */
884 xkb->max_key_code = info.explicitMax;
886 xkb->max_key_code = info.computedMax;
887 if (XkbcAllocNames(xkb, XkbKeyNamesMask | XkbIndicatorNamesMask, 0, 0)
891 xkb->names->keycodes = xkb_intern_atom(info.name);
892 for (i = info.computedMin; i <= info.computedMax; i++)
894 LongToKeyName(info.names[i], xkb->names->keys[i].name);
899 WSGO("Cannot create struct xkb_names in CompileKeycodes\n");
904 IndicatorNameInfo *ii;
905 if (XkbcAllocIndicatorMaps(xkb) != Success)
907 WSGO("Couldn't allocate IndicatorRec in CompileKeycodes\n");
908 ACTION("Physical indicators not set\n");
910 for (ii = info.leds; ii != NULL;
911 ii = (IndicatorNameInfo *) ii->defs.next)
913 xkb->names->indicators[ii->ndx - 1] = ii->name;
914 if (xkb->indicators != NULL)
917 bit = 1 << (ii->ndx - 1);
919 xkb->indicators->phys_indicators &= ~bit;
921 xkb->indicators->phys_indicators |= bit;
926 ApplyAliases(xkb, False, &info.aliases);
929 ClearKeyNamesInfo(&info);