|##########|
|#MAGIC   #|CLABLMAF
|#PROJECT #|""
|#PATHS   #|"StdProject"
|#FLAGS   #|xx---x--x---xxx-----------------
|#USERSW  #|--------------------------------
|#USERMASK#|--------------------------------
|#SWITCHES#|x-xx-x----------
|##########|
DEFINITION MODULE KeyMap;
(* $A- *)
FROM Exec    IMPORT Node,List,LibraryPtr;
FROM System  IMPORT LONGSET,SysStringPtr,Regs;
FROM Input   IMPORT Qualifiers,InputEventPtr;

TYPE
  KeyMapTypes        = (shift,alt,control,downup,kmp4,dead,string,nop);
  KeyMapTypeSet      = SET OF KeyMapTypes;
  DeadPrefixBytes    = (dpbMod,dpb1,dpb2,dpbDead);
  DeadPrefixByteSet  = SET OF DeadPrefixBytes;

CONST
  dp2dIndexMax  = $0F;
  dp2dFacShift  = 4;
  maxKeys       = 64;
  noQual        = KeyMapTypeSet:{};
  vanilla       = KeyMapTypeSet:{shift,alt,control}

TYPE
  BitTable         = ARRAY [maxKeys DIV (8*LONGSET'SIZE)] OF LONGSET;
  BitTablePtr      = POINTER TO BitTable;

  KeyInfo          = RECORD
                       IF KEY : INTEGER
                         OF 0 THEN ch : ARRAY [0..3] OF CHAR;
                         OF 1 THEN st : SysStringPtr;
                        END
                     END;

  Types            = ARRAY [0..maxKeys-1] OF KeyMapTypeSet;
  TypesPtr         = POINTER TO Types;

  Info             = ARRAY [0..maxKeys-1] OF KeyInfo;
  InfoPtr          = POINTER TO Info;

  KeyMap           = RECORD
                       loKeyMapTypes   : TypesPtr;
                       loKeyMap        : InfoPtr;
                       loCapsable      : BitTablePtr;
                       loRepeatable    : BitTablePtr;
                       hiKeyMapTypes   : TypesPtr;
                       hiKeyMap        : InfoPtr;
                       hiCapsable      : BitTablePtr;
                       hiRepeatable    : BitTablePtr;
                     END;
  KeyMapPtr        = POINTER TO KeyMap;

  KeyMapNode       = RECORD OF Node
                       keyMap : KeyMap
                     END;

  KeyMapResource   = RECORD OF Node
                       keyMaps : List
                     END;
  CodeQualPair     = RECORD
                       code : SHORTCARD;
                       qual : SET OF [lShift..rCommand];
                     END;
  CodeQualArray    = ARRAY OF CodeQualPair;

VAR
  KeymapBase  : LibraryPtr;

LIBRARY KeymapBase BY -30
  PROCEDURE SetKeyMapDefault(keyMap IN A0 : KeyMapPtr);

LIBRARY KeymapBase BY -36
  PROCEDURE AskKeyMapDefault():KeyMapPtr;

LIBRARY KeymapBase BY -42
  PROCEDURE MapRawKey(event  IN A0 : InputEventPtr;
                      buffer IN A1 : ANYPTR;
                      length IN D1 : INTEGER;
                      keyMap IN A2 : KeyMapPtr):INTEGER;

LIBRARY KeymapBase BY -48
  PROCEDURE MapANSI(REF string IN A0 : STRING;
                        count  IN D0 : LONGINT;
                    VAR buffer IN A1 : CodeQualArray;
                        length IN D1 : LONGINT;
                        keyMap IN A2 : KeyMapPtr):LONGINT;

GROUP
  All    = KeyMapTypes,KeyMapTypeSet,DeadPrefixBytes,DeadPrefixByteSet,
           dp2dIndexMax,dp2dFacShift,maxKeys,noQual,vanilla,BitTable,
           BitTablePtr,KeyInfo,Types,TypesPtr,Info,InfoPtr,KeyMap,
           KeyMapPtr,KeyMapNode,KeyMapResource;

END KeyMap.
