(* Used in DuM2.mod to allow icon startup - thanks Richie !
    FOR SOME REASON MY COMPILER DOESN'T LIKE AMIGAX.sym
    and freezes every time I import ExecBase, so I kludged it
 *)

(*

        This module determines if a task is running from
        Workbench. If so, a procedure is provided to exit
        properly without loosing memory.

        Created: 10/27/86 by Richie Bielak

        Modified:

        11/29/86 by Richie Bielak
        Changed to use fixed "FindTask". The new "FindTask" was
        kindly provided by TDI. However, I still don't understand
        why the new one works, and the old one doesn't. The code
        is nearly the same!

        Copyright (c) 1986 by Richie Bielak.

        This program may be freely copied, but please leave
        my name in. Thanks. Richie.

*)
IMPLEMENTATION MODULE WBStart;

  FROM SYSTEM               IMPORT  ADDRESS,NULL,ADR,CODE,SETREG,REGISTER;
  FROM DOSExtensions        IMPORT  ProcessPtr;
  FROM Ports                IMPORT  WaitPort, GetMsg, ReplyMsg;
  FROM Interrupts           IMPORT  Forbid;
  FROM DOSLibrary           IMPORT  DOSBase;
  FROM Libraries            IMPORT  CloseLibrary;
(*  FROM AMIGAX               IMPORT  ExecBase;
  *)


  (* +++++++++++++ NEW FIND TASK ++++++++++ *)

  CONST
    D0 = 0; A1 = 9; A6 = 14;
    JSRA6 = 4EAEH; SAVEA6 = 2F0EH; RESTOREA6 = 2C5FH;
    FindTaskVec = -30-264;
  VAR
    ExecBase[4] : ADDRESS;
  (* Tasks.FindTask has a bug.  Use this instead *)
  PROCEDURE FindTask(name: ADDRESS): ADDRESS;
    (* find a task with a given name, or find oneself. *)
  BEGIN
    CODE(SAVEA6);
    SETREG(A1, name);
    SETREG(A6,ExecBase);
    CODE(JSRA6,FindTaskVec);
    CODE(RESTOREA6);
    RETURN REGISTER(D0);
  END FindTask;

  (* ++++++++++++++++++++++++++++++++++++++++++ *)

  VAR
    WBStartUpMsg : ADDRESS;

  (* Get WB Startup message - this procedure will return
    NULL if the program is running from CLI.
  *)
  PROCEDURE GetWBStartUpMsg () : ADDRESS;
    VAR
      ThisProcess : ProcessPtr;
    BEGIN
      WBStartUpMsg := NULL;
      (* Get ID of our task *)
      (* Task record is imbedded in the Process record *)
      ThisProcess := FindTask(0);

      (* See if we are running from CLI *)
      IF ThisProcess^.prCLI <> 0 THEN
        RETURN NULL
      (* OK we're running from WB, get our startup msg *)
      ELSE
        WITH ThisProcess^ DO
          (* Wait for the message, and remove it from the port *)
          WBStartUpMsg := WaitPort (ADR(prMsgPort));
          WBStartUpMsg := GetMsg (ADR(prMsgPort));
          RETURN (WBStartUpMsg)
        END;
      END;
    END GetWBStartUpMsg;

  (* Call to this procedure returns the startup message.
     Call this procedure just before exiting your program.
  *)
  PROCEDURE ReturnWBStartUpMsg ();
    BEGIN
      IF WBStartUpMsg <> NULL THEN
        (* Close DOS Library, if it's still open *)
        IF DOSBase <> NULL THEN
          CloseLibrary (DOSBase)
        END;
        Forbid ();
        ReplyMsg (WBStartUpMsg);
      END;
    END ReturnWBStartUpMsg;

BEGIN
END WBStart.
