(*************************************************************************

     $RCSfile: LowLevel.mod $
  Description: Interface to lowlevel.library

   Created by: fjc (Frank Copeland)
    $Revision: 3.1 $
      $Author: fjc $
        $Date: 1994/08/08 01:18:15 $

  Includes Release 40.15

  (C) Copyright 1993 Commodore-Amiga, Inc.
      All Rights Reserved

  Oberon-A Interface Copyright © 1994, Frank Copeland.
  This file is part of the Oberon-A Interface.
  See Oberon-A.doc for conditions of use and distribution.

*************************************************************************)

MODULE LowLevel;

(*
** $C- CaseChk       $I- IndexChk  $L+ LongAdr   $N- NilChk
** $P- PortableCode  $R- RangeChk  $S- StackChk  $T- TypeChk
** $V- OvflChk       $Z- ZeroVars
*)

IMPORT
  E   := Exec,
  U   := Utility,
  T   := Timer,
  SYS := SYSTEM;

(*
**      $VER: lowlevel.h 40.6 (30.7.93)
**
**      lowlevel.library interface structures and definitions.
*)

(*****************************************************************************)

TYPE

(* structure for use with QueryKeys() *)
  KeyQueryPtr * = CPOINTER TO KeyQuery;
  KeyQuery * = RECORD
    keyCode * : E.UWORD;
    pressed * : E.BOOL;
  END;


(*****************************************************************************)

CONST

(* bits in the return value of GetKey() *)
  kbLShift * = 16;
  kbRShift * = 17;
  kbCapsLock * = 18;
  kbControl * = 19;
  kbLAlt * = 20;
  kbRAlt * = 21;
  kbLAmiga * = 22;
  kbRAmiga * = 23;


(*****************************************************************************)

CONST

(* Tags for SetJoyPortAttrs() *)
  sjaDummy * = U.tagUser + 0C00100H;
  sjaType * = sjaDummy+1;             (* force type to mouse, joy, game cntrlr *)
  sjaReinitialize * = sjaDummy+2;     (* free potgo bits, reset to autosense   *)

(* Controller types for sjaType tag *)
  sjaTypeAutoSense * = 0;
  sjaTypeGameCtlr * = 1;
  sjaTypeMouse * = 2;
  sjaTypeJoystk * = 3;


(*****************************************************************************)

CONST

(* ReadJoyPort() return value definitions *)

(* Port types *)
  jpTypeNotAvail * = 0;               (* port data unavailable    *)
  jpTypeGameCtlr * = 010000000H;               (* port has game controller *)
  jpTypeMouse * = 020000000H;                 (* port has mouse           *)
  jpTypeJoyStk * = 030000000H;                 (* port has joystick        *)
  jpTypeUnknown * = 040000000H;               (* port has unknown device  *)
  jpTypeMask * = 0F0000000H;                  (* controller type          *)

(* Button types, valid for all types except JP_TYPE_NOTAVAIL *)
  jpButtonBlue * = 23;           (* Blue - Stop; Right Mouse                *)
  jpButtonRed * = 22;            (* Red - Select; Left Mouse; Joystick Fire *)
  jpButtonYellow * = 21;         (* Yellow - Repeat                         *)
  jpButtonGreen * = 20;          (* Green - Shuffle                         *)
  jpButtonForward * = 19;        (* Charcoal - Forward              *)
  jpButtonReverse * = 18;        (* Charcoal - Reverse              *)
  jpButtonPlay * = 17;           (* Grey - Play/Pause; Middle Mouse         *)
  jpButtonMask * = { jpButtonBlue, jpButtonRed, jpButtonYellow,
                     jpButtonGreen, jpButtonForward, jpButtonReverse,
                     jpButtonPlay };

(* Direction types, valid for jpTypeGamectlr and jpTypeJoystk *)
  jpJoyUp * = 3;
  jpJoyDown * = 2;
  jpJoyLeft * = 1;
  jpJoyRight * = 0;
  jpDirectionMask * = {jpJoyUp, jpJoyDown, jpJoyLeft, jpJoyRight};

(* Mouse position reports, valid for jpTypeMouse *)
  jpMHorzMask * = {0..7};                (* horzizontal position *)
  jpMVertMask * = {8..15};                (* vertical position    *)
  jpMouseMask * = jpMHorzMask + jpMVertMask;

(* Obsolete ReadJoyPort() definitions, here for source code compatibility only.
 * Please do NOT use in new code.
 *)
  jpBtn1 * = jpButtonBlue;
  jpBtn2 * = jpButtonRed;
  jpBtn3 * = jpButtonYellow;
  jpBtn4 * = jpButtonGreen;
  jpBtn5 * = jpButtonForward;
  jpBtn6 * = jpButtonReverse;
  jpBtn7 * = jpButtonPlay;
  jpUp * = jpJoyUp;
  jpDown * = jpJoyDown;
  jpLeft * = jpJoyLeft;
  jpRight * = jpJoyRight;


(*****************************************************************************)

CONST

(* Tags for SystemControl() *)
  sconDummy * = U.tagUser + 000C00000H;
  sconTakeOverSys * = sconDummy+0;
  sconKillReq * = sconDummy+1;
  sconCDReboot * = sconDummy+2;
  sconStopInput * = sconDummy+3;
  sconAddCreateKeys * = sconDummy+4;
  sconRemCreateKeys * = sconDummy+5;

(* Reboot control values for use with SCON_CDReboot tag *)
  cdRebootOn * = 1;
  cdRebootOff * = 0;
  cdRebootDefault * = 2;


(*****************************************************************************)

CONST

(* Rawkey codes returned when using sconAddCreateKeys with SystemControl() *)

  rawkeyPort0ButtonBlue * = 072H;
  rawkeyPort0ButtonRed * = 078H;
  rawkeyPort0ButtonYellow * = 077H;
  rawkeyPort0ButtonGreen * = 076H;
  rawkeyPort0ButtonForward * = 075H;
  rawkeyPort0ButtonReverse * = 074H;
  rawkeyPort0ButtonPlay * = 073H;
  rawkeyPort0JoyUp * = 079H;
  rawkeyPort0JoyDown * = 07AH;
  rawkeyPort0JoyLeft * = 07CH;
  rawkeyPort0JoyRight * = 07BH;

  rawkeyPort1ButtonBlue * = 0172H;
  rawkeyPort1ButtonRed * = 0178H;
  rawkeyPort1ButtonYellow * = 0177H;
  rawkeyPort1ButtonGreen * = 0176H;
  rawkeyPort1ButtonForward * = 0175H;
  rawkeyPort1ButtonReverse * = 0174H;
  rawkeyPort1ButtonPlay * = 0173H;
  rawkeyPort1JoyUp * = 0179H;
  rawkeyPort1JoyDown * = 017AH;
  rawkeyPort1JoyLeft * = 017CH;
  rawkeyPort1JoyRight * = 017BH;

  rawkeyPort2ButtonBlue * = 0272H;
  rawkeyPort2ButtonRed * = 0278H;
  rawkeyPort2ButtonYellow * = 0277H;
  rawkeyPort2ButtonGreen * = 0276H;
  rawkeyPort2ButtonForward * = 0275H;
  rawkeyPort2ButtonReverse * = 0274H;
  rawkeyPort2ButtonPlay * = 0273H;
  rawkeyPort2JoyUp * = 0279H;
  rawkeyPort2JoyDown * = 027AH;
  rawkeyPort2JoyLeft * = 027CH;
  rawkeyPort2JoyRight * = 027BH;

  rawkeyPort3ButtonBlue * = 0372H;
  rawkeyPort3ButtonRed * = 0378H;
  rawkeyPort3ButtonYellow * = 0377H;
  rawkeyPort3ButtonGreen * = 0376H;
  rawkeyPort3ButtonForward * = 0375H;
  rawkeyPort3ButtonReverse * = 0374H;
  rawkeyPort3ButtonPlay * = 0373H;
  rawkeyPort3JoyUp * = 0379H;
  rawkeyPort3JoyDown * = 037AH;
  rawkeyPort3JoyLeft * = 037CH;
  rawkeyPort3JoyRight * = 037BH;


(*****************************************************************************)

CONST

(* Return values for GetLanguageSelection() *)
  langUnknown * = 0;
  langAmerican * = 1;           (* American English *)
  langEnglish * = 2;            (* British English  *)
  langGerman * = 3;
  langFrench * = 4;
  langSpanish * = 5;
  langItalian * = 6;
  langPortuguese * = 7;
  langDanish * = 8;
  langDutch * = 9;
  langNorwegian * = 10;
  langFinnish * = 11;
  langSwedish * = 12;
  langJapanese * = 13;
  langChinese * = 14;
  langArabic * = 15;
  langGreek * = 16;
  langHebrew * = 17;
  langKorean * = 18;


(*****************************************************************************)

(*-- Library Base variable --------------------------------------------*)

CONST

  name * = "lowlevel.library";

TYPE

  LowLevelBasePtr* = CPOINTER TO LowLevelBase;
  LowLevelBase* = RECORD (E.Library) END;

VAR

  base* : LowLevelBasePtr;


(*-- Library Functions ------------------------------------------------*)

(*
**      $VER: lowlevel_protos.h 40.6 (30.7.93)
*)

(*--- functions in V40 or higher (Release 3.1) ---*)

(* CONTROLLER HANDLING *)

LIBCALL (base : LowLevelBasePtr) ReadJoyPort *
  ( port [0] : E.ULONG )
  : E.ULONG;
  -30;

(* LANGUAGE HANDLING *)

LIBCALL (base : LowLevelBasePtr) GetLanguageSelection * ()
  : E.UBYTE;
  -36;

(* KEYBOARD HANDLING *)

LIBCALL (base : LowLevelBasePtr) GetKey * ()
  : E.ULONG;
  -48;
LIBCALL (base : LowLevelBasePtr) QueryKeys *
  ( queryArray [8] : KeyQueryPtr;
    arraySize  [1] : E.ULONG );
  -54;
LIBCALL (base : LowLevelBasePtr) AddKBInt *
  ( intRoutine [8] : E.APTR;
    intData    [9] : E.APTR )
  : E.APTR;
  -60;
LIBCALL (base : LowLevelBasePtr) RemKBInt *
  ( intHandle [9] : E.APTR );
  -66;

(* SYSTEM HANDLING *)

LIBCALL (base : LowLevelBasePtr) SystemControlA *
  ( tagList [9] : ARRAY OF U.TagItem )
  : E.ULONG;
  -72;
LIBCALL (base : LowLevelBasePtr) SystemControl *
  ( tagList [9]..: U.Tag )
  : E.ULONG;
  -72;

(* TIMER HANDLING *)

LIBCALL (base : LowLevelBasePtr) AddTimerInt *
  ( intRoutine [8] : E.APTR;
    intData    [9] : E.APTR )
  : E.APTR;
  -78;
LIBCALL (base : LowLevelBasePtr) RemTimerInt *
  ( intHandle [9] : E.APTR );
  -84;
LIBCALL (base : LowLevelBasePtr) StopTimerInt *
  ( intHandle [9] : E.APTR );
  -90;
LIBCALL (base : LowLevelBasePtr) StartTimerInt *
  ( intHandle    [9] : E.APTR;
    timeInterval [0] : E.ULONG;
    continuous   [1] : LONGINT );
  -96;
LIBCALL (base : LowLevelBasePtr) ElapsedTime *
  ( VAR context [8] : T.EClockVal )
  : E.ULONG;
  -102;

(* VBLANK HANDLING *)

LIBCALL (base : LowLevelBasePtr) AddVBlankInt *
  ( intRoutine [8] : E.APTR;
    intData    [9] : E.APTR )
  : E.APTR;
  -108;
LIBCALL (base : LowLevelBasePtr) RemVBlankInt *
  ( intHandle [9] : E.APTR );
  -114;

(* MORE CONTROLLER HANDLING *)

LIBCALL (base : LowLevelBasePtr) SetJoyPortAttrsA *
  ( portNumber [0] : E.ULONG;
    tagList    [9] : ARRAY OF U.TagItem )
  : BOOLEAN;
  -132;
LIBCALL (base : LowLevelBasePtr) SetJoyPortAttrs *
  ( portNumber [0]  : E.ULONG;
    tagList    [9]..: U.Tag )
  : BOOLEAN;
  -132;

(*-- Library Base variable --------------------------------------------*)

(*-----------------------------------*)
PROCEDURE* CloseLib ();

BEGIN (* CloseLib *)
  IF base # NIL THEN E.base.CloseLibrary (base) END
END CloseLib;

(*-----------------------------------*)
PROCEDURE OpenLib * (mustOpen : BOOLEAN);

BEGIN (* OpenLib *)
  IF base = NIL THEN
    base :=
      SYS.VAL (
        LowLevelBasePtr,
        E.base.OpenLibrary (name, E.libraryMinimum));
    IF base # NIL THEN SYS.SETCLEANUP (CloseLib)
    ELSIF mustOpen THEN HALT (100)
    END;
  END;
END OpenLib;

BEGIN
  base := NIL
END LowLevel.
