MODULE SlowDown;

(*======================================================================*)
(*                             Slow Down!                               *)
(*======================================================================*)
(*         © Copyright 1989 Robert Salesas, All Rights Reserved         *)
(*        Re-Distribute as you wish but DO NOT alter the contents       *)
(*            of this file.  Moral Rights remain my property.           *)
(*======================================================================*)
(*      Version: 1.00           Author : Robert Salesas                 *)
(*      Date   : 24-Dec-89      Changes: Original                       *)
(*======================================================================*)

(*
    This program was written to slow down the Amiga.
    The original purpose of this program was to permit
    mentally handicapped users to play games that multi-task
    properly.

    If you use the -N (No Window) flag, a CTRL-C will stop the program once
    it has been started.
    You can use the break command to send it a CTRL-C.
*)

FROM SYSTEM           IMPORT  ADR, STRPTR;
FROM RunTime          IMPORT  WBMsg, CurrentProcess;
FROM CmdLineUtils     IMPORT  argc, argv;
FROM Conversions      IMPORT  ConvStrToNum;
FROM Tasks            IMPORT  SetTaskPri;
FROM DOSProcess       IMPORT  Delay;
FROM Ports            IMPORT  GetMsg, ReplyMsg;
FROM Intuition        IMPORT  WindowPtr, ScreenPtr, GadgetPtr,
                              IDCMPFlags, IDCMPFlagSet, IntuiMessagePtr,
                              CloseWindow;
FROM ReqWinUtils      IMPORT  StartReqWinGList, StartReqWin,
                              AddBoolText, IntCombo,
                              AddIntCombo, HandleIntCombo;
FROM EasyGadgets      IMPORT  listFailed, DisposeList, currentList;
FROM FindWB           IMPORT  FirstWBScreen;
FROM Pens             IMPORT  Move;
FROM Text             IMPORT  Text;
FROM Puts             IMPORT  PutString;
FROM TrapCtrlC        IMPORT  On;

VAR
  Wp          :  WindowPtr;
  Sp          :  ScreenPtr;
  Gad,
  GList       :  GadgetPtr;
  GadID       :  CARDINAL;
  Msg         :  IntuiMessagePtr;
  PrioCombo   :  IntCombo;
  SlowCombo   :  IntCombo;
  Temp        :  LONGINT;
  Prio,
  Slow,
  OldPri      :  INTEGER;
  Par1,
  Par2, Par3  :  STRPTR;


  PROCEDURE BusyWait(DelayCount : CARDINAL);
  VAR
    L1,
    Counter  :  CARDINAL;
  BEGIN
    INC(DelayCount);
    Counter := 0;
    REPEAT
      FOR L1 := 0 TO 2000 DO
      END;
      INC(Counter);
    UNTIL (Counter = DelayCount);
  END BusyWait;

BEGIN
  Prio := 0;  Slow := 0;
  IF (WBMsg = NIL) THEN
    IF (argc = 1) OR (argc > 4) THEN
      PutString("Slow Down! - © Copyright 1989 Robert Salesas");
      PutString("");
      PutString("USAGE:  SlowDown  PRIORITY  SPEED    -N");
      PutString("                  (-9..20)  (0..99)  (No Window)");
      RETURN
    ELSE
      Par1 := argv[1];  Par2 := argv[2];  Par3 := argv[3];
      IF ConvStrToNum(Par1^, Temp, 10, TRUE) THEN
        IF (Temp >= -9) AND (Temp <= 20) THEN
          Prio := INTEGER(Temp);
        END;
      END;
      IF (argc > 2) AND ConvStrToNum(Par2^, Temp, 10, TRUE) THEN
        IF (Temp >= 0) AND (Temp <= 99) THEN
          Slow := INTEGER(Temp);
        END;
      END;
      IF (argc = 4) AND ((Par3^[0] = "-") AND (Par3^[1] = "N")) THEN
        On;
        LOOP
          BusyWait(Slow);
          Delay(2);
        END;
      END;
    END;
  END;
  
  OldPri := SetTaskPri(CurrentProcess, Prio);
  
  StartReqWinGList;
  WITH PrioCombo DO
    MaxDigs := 2;
    Value := Prio;
    MinVal := -9;
    MaxVal := 20;
  END;
  AddIntCombo(20, 16, 160, "Task Priority", ADR(PrioCombo));
  WITH SlowCombo DO
    MaxDigs := 2;
    Value := Slow;
    MinVal := 0;
    MaxVal := 99;
  END;
  AddIntCombo(20, 28, 160, "Slow Down Delay", ADR(SlowCombo));
  AddBoolText(86, 42, 60, "Quit");
  IF NOT listFailed THEN
    GList := currentList;
    Sp := FirstWBScreen();
    IF (Sp # NIL) THEN
      Wp := StartReqWin(Sp, 230, 74, "Slow Down!", IDCMPFlagSet{MouseButtons, GadgetUp, GadgetDown}, GList);
      IF (Wp # NIL) THEN
        Move(Wp^.RPort, 32, 68);  Text(Wp^.RPort, ADR("© 1989 Robert Salesas"), 21);
        LOOP
          Msg := GetMsg(Wp^.UserPort);
          IF (Msg # NIL) THEN
            IF NOT (MouseButtons IN Msg^.Class) THEN
              Gad:= Msg^.IAddress;  GadID := Gad^.GadgetID;
              ReplyMsg(Msg);
              IF (GadID = 0) THEN
                HandleIntCombo(Wp, Gad);
                OldPri := SetTaskPri(CurrentProcess, PrioCombo.Value);
              ELSIF (GadID = 1) THEN
                HandleIntCombo(Wp, Gad);
              ELSIF (GadID = 2) THEN
                EXIT;
              END;
            ELSE
              ReplyMsg(Msg);
            END;
          ELSE
            BusyWait(SlowCombo.Value);
            Delay(2);
          END;
        END;
        CloseWindow(Wp);
      END;
    END;
    DisposeList(GList);
  END;
END SlowDown.