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 ********************************************************/
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);
57 /***====================================================================***/
59 typedef struct _IndicatorNameInfo
67 typedef struct _KeyNamesInfo
69 char *name; /* e.g. evdev+aliases(qwerty) */
73 int computedMin; /* lowest keycode stored */
74 int computedMax; /* highest keycode stored */
79 unsigned long names[XkbMaxLegalKeyCode + 1]; /* 4-letter name of key, keycode is the index */
80 unsigned files[XkbMaxLegalKeyCode + 1];
81 unsigned char has_alt_forms[XkbMaxLegalKeyCode + 1];
82 IndicatorNameInfo *leds;
86 static void HandleKeycodesFile(XkbFile * file,
92 InitIndicatorNameInfo(IndicatorNameInfo * ii, KeyNamesInfo * info)
95 ii->defs.merge = info->merge;
96 ii->defs.fileID = info->fileID;
105 ClearIndicatorNameInfo(IndicatorNameInfo * ii, KeyNamesInfo * info)
107 if (ii == info->leds)
109 ClearCommonInfo(&ii->defs);
115 static IndicatorNameInfo *
116 NextIndicatorName(KeyNamesInfo * info)
118 IndicatorNameInfo *ii;
120 ii = uTypedAlloc(IndicatorNameInfo);
123 InitIndicatorNameInfo(ii, info);
124 info->leds = (IndicatorNameInfo *) AddCommonInfo(&info->leds->defs,
130 static IndicatorNameInfo *
131 FindIndicatorByIndex(KeyNamesInfo * info, int ndx)
133 IndicatorNameInfo *old;
135 for (old = info->leds; old != NULL;
136 old = (IndicatorNameInfo *) old->defs.next)
144 static IndicatorNameInfo *
145 FindIndicatorByName(KeyNamesInfo * info, CARD32 name)
147 IndicatorNameInfo *old;
149 for (old = info->leds; old != NULL;
150 old = (IndicatorNameInfo *) old->defs.next)
152 if (old->name == name)
159 AddIndicatorName(KeyNamesInfo * info, IndicatorNameInfo * new)
161 IndicatorNameInfo *old;
164 replace = (new->defs.merge == MergeReplace) ||
165 (new->defs.merge == MergeOverride);
166 old = FindIndicatorByName(info, new->name);
169 if (((old->defs.fileID == new->defs.fileID) && (warningLevel > 0))
170 || (warningLevel > 9))
172 WARN("Multiple indicators named %s\n", XkbcAtomText(new->name));
173 if (old->ndx == new->ndx)
175 if (old->virtual != new->virtual)
178 old->virtual = new->virtual;
179 ACTION("Using %s instead of %s\n",
180 (old->virtual ? "virtual" : "real"),
181 (old->virtual ? "real" : "virtual"));
185 ACTION("Identical definitions ignored\n");
192 ACTION("Ignoring %d, using %d\n", old->ndx, new->ndx);
194 ACTION("Using %d, ignoring %d\n", old->ndx, new->ndx);
198 if (info->leds == old)
199 info->leds = (IndicatorNameInfo *) old->defs.next;
202 IndicatorNameInfo *tmp;
205 tmp = (IndicatorNameInfo *) tmp->defs.next)
207 if (tmp->defs.next == (CommonInfo *) old)
209 tmp->defs.next = old->defs.next;
218 old = FindIndicatorByIndex(info, new->ndx);
221 if (((old->defs.fileID == new->defs.fileID) && (warningLevel > 0))
222 || (warningLevel > 9))
224 WARN("Multiple names for indicator %d\n", new->ndx);
225 if ((old->name == new->name) && (old->virtual == new->virtual))
226 ACTION("Identical definitions ignored\n");
229 const char *oldType, *newType;
230 CARD32 using, ignoring;
232 oldType = "virtual indicator";
234 oldType = "real indicator";
236 newType = "virtual indicator";
238 newType = "real indicator";
242 ignoring = old->name;
247 ignoring = new->name;
249 ACTION("Using %s %s, ignoring %s %s\n",
250 oldType, XkbcAtomText(using),
251 newType, XkbcAtomText(ignoring));
256 old->name = new->name;
257 old->virtual = new->virtual;
262 new = NextIndicatorName(info);
265 WSGO("Couldn't allocate name for indicator %d\n", new->ndx);
269 new->name = old->name;
271 new->virtual = old->virtual;
276 ClearKeyNamesInfo(KeyNamesInfo * info)
278 if (info->name != NULL)
281 info->computedMax = info->explicitMax = info->explicitMin = -1;
282 info->computedMin = 256;
283 info->effectiveMin = 8;
284 info->effectiveMax = 255;
285 bzero((char *) info->names, sizeof(info->names));
286 bzero((char *) info->files, sizeof(info->files));
287 bzero((char *) info->has_alt_forms, sizeof(info->has_alt_forms));
289 ClearIndicatorNameInfo(info->leds, info);
291 ClearAliases(&info->aliases);
296 InitKeyNamesInfo(KeyNamesInfo * info)
300 info->aliases = NULL;
301 ClearKeyNamesInfo(info);
302 info->errorCount = 0;
307 FindKeyByLong(KeyNamesInfo * info, unsigned long name)
311 for (i = info->effectiveMin; i <= info->effectiveMax; i++)
313 if (info->names[i] == name)
320 * Store the name of the key as a long in the info struct under the given
321 * keycode. If the same keys is referred to twice, print a warning.
322 * Note that the key's name is stored as a long, the keycode is the index.
325 AddKeyName(KeyNamesInfo * info,
327 char *name, unsigned merge, unsigned fileID, Bool reportCollisions)
332 if ((kc < info->effectiveMin) || (kc > info->effectiveMax))
334 ERROR("Illegal keycode %d for name <%s>\n", kc, name);
335 ACTION("Must be in the range %d-%d inclusive\n",
336 info->effectiveMin, info->effectiveMax);
339 if (kc < info->computedMin)
340 info->computedMin = kc;
341 if (kc > info->computedMax)
342 info->computedMax = kc;
343 lval = KeyNameToLong(name);
345 if (reportCollisions)
347 reportCollisions = ((warningLevel > 7) ||
349 && (fileID == info->files[kc])));
352 if (info->names[kc] != 0)
356 LongToKeyName(info->names[kc], buf);
358 if (info->names[kc] == lval)
360 if (info->has_alt_forms[kc] || (merge == MergeAltForm))
362 info->has_alt_forms[kc] = True;
364 else if (reportCollisions)
366 WARN("Multiple identical key name definitions\n");
367 ACTION("Later occurences of \"<%s> = %d\" ignored\n",
372 if (merge == MergeAugment)
374 if (reportCollisions)
376 WARN("Multiple names for keycode %d\n", kc);
377 ACTION("Using <%s>, ignoring <%s>\n", buf, name);
383 if (reportCollisions)
385 WARN("Multiple names for keycode %d\n", kc);
386 ACTION("Using <%s>, ignoring <%s>\n", name, buf);
392 old = FindKeyByLong(info, lval);
393 if ((old != 0) && (old != kc))
395 if (merge == MergeOverride)
397 info->names[old] = 0;
398 info->files[old] = 0;
399 info->has_alt_forms[old] = True;
400 if (reportCollisions)
402 WARN("Key name <%s> assigned to multiple keys\n", name);
403 ACTION("Using %d, ignoring %d\n", kc, old);
406 else if (merge != MergeAltForm)
408 if ((reportCollisions) && (warningLevel > 3))
410 WARN("Key name <%s> assigned to multiple keys\n", name);
411 ACTION("Using %d, ignoring %d\n", old, kc);
413 ("Use 'alternate' keyword to assign the same name to multiple keys\n");
419 info->has_alt_forms[old] = True;
422 info->names[kc] = lval;
423 info->files[kc] = fileID;
424 info->has_alt_forms[kc] = (merge == MergeAltForm);
428 /***====================================================================***/
431 MergeIncludedKeycodes(KeyNamesInfo * into, KeyNamesInfo * from,
437 if (from->errorCount > 0)
439 into->errorCount += from->errorCount;
442 if (into->name == NULL)
444 into->name = from->name;
447 for (i = from->computedMin; i <= from->computedMax; i++)
450 if (from->names[i] == 0)
452 LongToKeyName(from->names[i], buf);
454 if (from->has_alt_forms[i])
455 thisMerge = MergeAltForm;
458 if (!AddKeyName(into, i, buf, thisMerge, from->fileID, False))
463 IndicatorNameInfo *led, *next;
464 for (led = from->leds; led != NULL; led = next)
466 if (merge != MergeDefault)
467 led->defs.merge = merge;
468 if (!AddIndicatorName(into, led))
470 next = (IndicatorNameInfo *) led->defs.next;
473 if (!MergeAliases(&into->aliases, &from->aliases, merge))
475 if (from->explicitMin > 0)
477 if ((into->explicitMin < 0)
478 || (into->explicitMin > from->explicitMin))
479 into->effectiveMin = into->explicitMin = from->explicitMin;
481 if (from->explicitMax > 0)
483 if ((into->explicitMax < 0)
484 || (into->explicitMax < from->explicitMax))
485 into->effectiveMax = into->explicitMax = from->explicitMax;
491 * Handle the given include statement (e.g. "include "evdev+aliases(qwerty)").
493 * @param stmt The include statement from the keymap file.
494 * @param xkb Unused for all but the xkb->flags.
495 * @param info Struct to store the key info in.
498 HandleIncludeKeycodes(IncludeStmt * stmt, XkbcDescPtr xkb, KeyNamesInfo * info)
502 KeyNamesInfo included = {NULL};
506 if ((stmt->file == NULL) && (stmt->map == NULL))
510 bzero(info, sizeof(KeyNamesInfo));
512 else if (strcmp(stmt->file, "computed") == 0)
514 xkb->flags |= AutoKeyNames;
515 info->explicitMin = XkbMinLegalKeyCode;
516 info->explicitMax = XkbMaxLegalKeyCode;
517 return (info->errorCount == 0);
518 } /* parse file, store returned info in the xkb struct */
519 else if (ProcessIncludeFile(stmt, XkmKeyNamesIndex, &rtrn, &newMerge))
521 InitKeyNamesInfo(&included);
522 HandleKeycodesFile(rtrn, xkb, MergeOverride, &included);
523 if (stmt->stmt != NULL)
525 if (included.name != NULL)
526 uFree(included.name);
527 included.name = stmt->stmt;
533 info->errorCount += 10; /* XXX: why 10?? */
536 /* Do we have more than one include statement? */
537 if ((stmt->next != NULL) && (included.errorCount < 1))
541 KeyNamesInfo next_incl;
543 for (next = stmt->next; next != NULL; next = next->next)
545 if ((next->file == NULL) && (next->map == NULL))
548 MergeIncludedKeycodes(&included, info, next->merge);
549 ClearKeyNamesInfo(info);
551 else if (ProcessIncludeFile(next, XkmKeyNamesIndex, &rtrn, &op))
553 InitKeyNamesInfo(&next_incl);
554 HandleKeycodesFile(rtrn, xkb, MergeOverride, &next_incl);
555 MergeIncludedKeycodes(&included, &next_incl, op);
556 ClearKeyNamesInfo(&next_incl);
560 info->errorCount += 10; /* XXX: Why 10?? */
569 MergeIncludedKeycodes(info, &included, newMerge);
570 ClearKeyNamesInfo(&included);
572 return (info->errorCount == 0);
576 * Parse the given statement and store the output in the info struct.
580 HandleKeycodeDef(KeycodeDef * stmt, unsigned merge, KeyNamesInfo * info)
585 if (!ExprResolveInteger(stmt->value, &result, NULL, NULL))
587 ACTION("No value keycode assigned to name <%s>\n", stmt->name);
591 if ((code < info->effectiveMin) || (code > info->effectiveMax))
593 ERROR("Illegal keycode %d for name <%s>\n", code, stmt->name);
594 ACTION("Must be in the range %d-%d inclusive\n",
595 info->effectiveMin, info->effectiveMax);
598 if (stmt->merge != MergeDefault)
600 if (stmt->merge == MergeReplace)
601 merge = MergeOverride;
605 return AddKeyName(info, code, stmt->name, merge, info->fileID, True);
608 #define MIN_KEYCODE_DEF 0
609 #define MAX_KEYCODE_DEF 1
612 * Handle the minimum/maximum statement of the xkb file.
613 * Sets explicitMin/Max and effectiveMin/Max of the info struct.
615 * @return 1 on success, 0 otherwise.
618 HandleKeyNameVar(VarDef * stmt, KeyNamesInfo * info)
620 ExprResult tmp, field;
624 if (ExprResolveLhs(stmt->name, &tmp, &field, &arrayNdx) == 0)
625 return 0; /* internal error, already reported */
629 ERROR("Unknown element %s encountered\n", tmp.str);
630 ACTION("Default for field %s ignored\n", field.str);
633 if (uStrCaseCmp(field.str, "minimum") == 0)
634 which = MIN_KEYCODE_DEF;
635 else if (uStrCaseCmp(field.str, "maximum") == 0)
636 which = MAX_KEYCODE_DEF;
639 ERROR("Unknown field encountered\n");
640 ACTION("Assigment to field %s ignored\n", field.str);
643 if (arrayNdx != NULL)
645 ERROR("The %s setting is not an array\n", field.str);
646 ACTION("Illegal array reference ignored\n");
650 if (ExprResolveInteger(stmt->value, &tmp, NULL, NULL) == 0)
652 ACTION("Assignment to field %s ignored\n", field.str);
655 if ((tmp.ival < XkbMinLegalKeyCode) || (tmp.ival > XkbMaxLegalKeyCode))
658 ("Illegal keycode %d (must be in the range %d-%d inclusive)\n",
659 tmp.ival, XkbMinLegalKeyCode, XkbMaxLegalKeyCode);
660 ACTION("Value of \"%s\" not changed\n", field.str);
663 if (which == MIN_KEYCODE_DEF)
665 if ((info->explicitMax > 0) && (info->explicitMax < tmp.ival))
668 ("Minimum key code (%d) must be <= maximum key code (%d)\n",
669 tmp.ival, info->explicitMax);
670 ACTION("Minimum key code value not changed\n");
673 if ((info->computedMax > 0) && (info->computedMin < tmp.ival))
676 ("Minimum key code (%d) must be <= lowest defined key (%d)\n",
677 tmp.ival, info->computedMin);
678 ACTION("Minimum key code value not changed\n");
681 info->explicitMin = tmp.ival;
682 info->effectiveMin = tmp.ival;
684 if (which == MAX_KEYCODE_DEF)
686 if ((info->explicitMin > 0) && (info->explicitMin > tmp.ival))
688 ERROR("Maximum code (%d) must be >= minimum key code (%d)\n",
689 tmp.ival, info->explicitMin);
690 ACTION("Maximum code value not changed\n");
693 if ((info->computedMax > 0) && (info->computedMax > tmp.ival))
696 ("Maximum code (%d) must be >= highest defined key (%d)\n",
697 tmp.ival, info->computedMax);
698 ACTION("Maximum code value not changed\n");
701 info->explicitMax = tmp.ival;
702 info->effectiveMax = tmp.ival;
708 HandleIndicatorNameDef(IndicatorNameDef * def,
709 unsigned merge, KeyNamesInfo * info)
711 IndicatorNameInfo ii;
714 if ((def->ndx < 1) || (def->ndx > XkbNumIndicators))
717 ERROR("Name specified for illegal indicator index %d\n", def->ndx);
721 InitIndicatorNameInfo(&ii, info);
723 if (!ExprResolveString(def->name, &tmp, NULL, NULL))
726 snprintf(buf, sizeof(buf), "%d", def->ndx);
728 return ReportBadType("indicator", "name", buf, "string");
730 ii.name = XkbcInternAtom(tmp.str, False);
732 ii.virtual = def->virtual;
733 if (!AddIndicatorName(info, &ii))
739 * Handle the xkb_keycodes section of a xkb file.
740 * All information about parsed keys is stored in the info struct.
742 * Such a section may have include statements, in which case this function is
743 * semi-recursive (it calls HandleIncludeKeycodes, which may call
744 * HandleKeycodesFile again).
746 * @param file The input file (parsed xkb_keycodes section)
747 * @param xkb Necessary to pass down, may have flags changed.
748 * @param merge Merge strategy (MergeOverride, etc.)
749 * @param info Struct to contain the fully parsed key information.
752 HandleKeycodesFile(XkbFile * file,
753 XkbcDescPtr xkb, unsigned merge, KeyNamesInfo * info)
757 info->name = _XkbDupString(file->name);
761 switch (stmt->stmtType)
763 case StmtInclude: /* e.g. include "evdev+aliases(qwerty)" */
764 if (!HandleIncludeKeycodes((IncludeStmt *) stmt, xkb, info))
767 case StmtKeycodeDef: /* e.g. <ESC> = 9; */
768 if (!HandleKeycodeDef((KeycodeDef *) stmt, merge, info))
771 case StmtKeyAliasDef: /* e.g. alias <MENU> = <COMP>; */
772 if (!HandleAliasDef((KeyAliasDef *) stmt,
773 merge, info->fileID, &info->aliases))
776 case StmtVarDef: /* e.g. minimum, maximum */
777 if (!HandleKeyNameVar((VarDef *) stmt, info))
780 case StmtIndicatorNameDef: /* e.g. indicator 1 = "Caps Lock"; */
781 if (!HandleIndicatorNameDef((IndicatorNameDef *) stmt,
789 ERROR("Keycode files may define key and indicator names only\n");
790 ACTION("Ignoring definition of %s\n",
792 StmtInterpDef) ? "a symbol interpretation" :
793 "virtual modifiers"));
797 WSGO("Unexpected statement type %d in HandleKeycodesFile\n",
802 if (info->errorCount > 10)
805 ERROR("Too many errors\n");
807 ACTION("Abandoning keycodes file \"%s\"\n", file->topName);
815 * Compile the xkb_keycodes section, parse it's output, return the results.
817 * @param file The parsed XKB file (may have include statements requiring
819 * @param result The effective keycodes, as gathered from the file.
820 * @param merge Merge strategy.
822 * @return True on success, False otherwise.
825 CompileKeycodes(XkbFile *file, XkbcDescPtr xkb, unsigned merge)
827 KeyNamesInfo info; /* contains all the info after parsing */
829 InitKeyNamesInfo(&info);
830 HandleKeycodesFile(file, xkb, merge, &info);
832 /* all the keys are now stored in info */
834 if (info.errorCount == 0)
836 if (info.explicitMin > 0) /* if "minimum" statement was present */
837 xkb->min_key_code = info.effectiveMin;
839 xkb->min_key_code = info.computedMin;
840 if (info.explicitMax > 0) /* if "maximum" statement was present */
841 xkb->max_key_code = info.effectiveMax;
843 xkb->max_key_code = info.computedMax;
844 if (XkbcAllocNames(xkb, XkbKeyNamesMask | XkbIndicatorNamesMask, 0, 0)
848 xkb->names->keycodes = XkbcInternAtom(info.name, False);
849 uDEBUG2(1, "key range: %d..%d\n", xkb->min_key_code,
851 for (i = info.computedMin; i <= info.computedMax; i++)
853 LongToKeyName(info.names[i], xkb->names->keys[i].name);
854 uDEBUG2(2, "key %d = %s\n", i,
855 XkbcKeyNameText(xkb->names->keys[i].name));
860 WSGO("Cannot create XkbcNamesRec in CompileKeycodes\n");
865 IndicatorNameInfo *ii;
866 if (XkbcAllocIndicatorMaps(xkb) != Success)
868 WSGO("Couldn't allocate IndicatorRec in CompileKeycodes\n");
869 ACTION("Physical indicators not set\n");
871 for (ii = info.leds; ii != NULL;
872 ii = (IndicatorNameInfo *) ii->defs.next)
874 xkb->names->indicators[ii->ndx - 1] = ii->name;
875 if (xkb->indicators != NULL)
877 register unsigned bit;
878 bit = 1 << (ii->ndx - 1);
880 xkb->indicators->phys_indicators &= ~bit;
882 xkb->indicators->phys_indicators |= bit;
887 ApplyAliases(xkb, False, &info.aliases);
890 ClearKeyNamesInfo(&info);