/* 
  Jerry J. Trantow
  1560 A. East Irving Place
  Milwaukee, Wi 53202-1460

He was prepared for a glorius life;
he had laid out an avenue through his head,eight rods wide;
he had got the world,-- much more, the flesh and the devil,
-- as it were by the nape of the neck, 
and held it under the tide of its own events,
and let it go down stream like a dead dog,
till he heard the hollow chambers of silence stretching away
on every side and his own soul expanded and filled them.
                                       VanWyck Brooks on HDT

  This program illustrates how to use the functions that
  spin the pointer.  To understand the task see description
  in spawn.c.
  This program executes the proper sequence of 
  SpawnSpinTask() which attaches the task to the window
  and then goes into a loop sending signals 
      Signal(taskPtr,SIGF_SPIN)
      Signal(taskPtr,SIGF_STOP)
  with the delay times and task priority specified by the
  user until the close box is hit.

  The variables which control the task are
      Spin_sec    ; seconds between images
      Spin_usec   ; microseconds between images
      Priority    ; Priority of task that spins the pointer

HISTORY
19 Jun 90 Made priority changeable, refreshed the Gadgets
20 Jun 90 Clipped priority to proper range
21 Jun 90 Changed delay to display milliseconds
25 Jun 90 Changed to 60 column spaces 
25 Jun 90 Removed GfxBase
30 Jun 90 Added code to handle GADGETUP
*/

#include "spin.h"   
#include "spin.gad" /* gadget definition file */

struct IntuitionBase *IntuitionBase;
struct Window *WPtr;

LONG Spin_sec;  /* seconds between images    */
LONG Spin_usec; /* useconds between images   */
                /* displayed as milliseconds */
VOID main()
{
  FAST wloop=TRUE;
  FAST struct IntuiMessage *MyIntuiMessage;
  FAST struct Gadget *GPtr;
  LONG Priority;  /* priority of spin task */

  Spin_sec=0L;
  Spin_usec=90000L;
  Priority=0L;
                        /* initialize the string gadgets */
  sprintf((char *)DelaySpecialString.Buffer,"%ld", 
          Spin_usec/1000L);
  sprintf((char *)PrioritySpecialString.Buffer,"%03ld",
          Priority);
  DelaySpecialString.LongInt=Spin_usec/1000L;
  PrioritySpecialString.LongInt=Priority;

  if ((IntuitionBase=(struct IntuitionBase *)
   OpenLibrary("intuition.library",33L))==NULL)
    CleanUp("Couldn't open Intuition");
  if ((WPtr=(struct Window *)OpenWindow(&NewWindow))==NULL)
    CleanUp("Couldn't open Window");

  if (SpawnSpinTask(WPtr)==NULL)
    CleanUp("UserData is NULL, Where is the Task?\n");
 
  ActivateGadget(&DelayGadget,WPtr,NULL);

  while (wloop==TRUE)
  {
    Wait((LONG)(1L<<WPtr->UserPort->mp_SigBit));
    while (MyIntuiMessage=(struct IntuiMessage *)
     GetMsg(WPtr->UserPort))
    {
      switch (MyIntuiMessage->Class)
      {
        case SIZEVERIFY:
        case NEWSIZE:
        case REFRESHWINDOW:
        case MOUSEBUTTONS:
        case MOUSEMOVE:
        case GADGETDOWN:
          break;
        case GADGETUP:         /* move to the next gadget */
          GPtr=(struct Gadget *)MyIntuiMessage->IAddress;
          switch ((USHORT)GPtr->GadgetID)
          {
           case GAD_START: /* NOTE: No sense refreshing */
                            /*       faster than 16msec  */
              Spin_usec=max(DelaySpecialString.LongInt,16L);
              sprintf((char *)DelaySpecialString.Buffer,
               "%ld",Spin_usec);
              Spin_usec*=1000L;     /* milli to micro sec */
              Priority=
               min(127L,PrioritySpecialString.LongInt);
              Priority=max(-128L,Priority);
              sprintf((char *)PrioritySpecialString.Buffer,
               "%03ld",Priority);
              SetTaskPri((struct Task *)WPtr->UserData,
               (LONG)Priority);
               Signal(
               (struct Task *)WPtr->UserData,SIGF_SPIN);
              RefreshGadgets(&StartGadget,WPtr,NULL);
              break;  
            case GAD_STOP:
              Signal((struct Task *)WPtr->UserData,
               SIGF_STOP);
              break;
            case GAD_DELAYVALUE:
              ActivateGadget(&PriorityGadget,WPtr,NULL);
              break;
            case GAD_PRIORITYVALUE:
              ActivateGadget(&DelayGadget,WPtr,NULL);
              break;
          }
          break;
        case REQSET:
        case MENUPICK:
          break;
        case CLOSEWINDOW:
          wloop=FALSE;	
          break;
        case RAWKEY:
        case REQVERIFY:
        case REQCLEAR:
        case MENUVERIFY:
        case NEWPREFS:
        case DISKINSERTED:
        case DISKREMOVED:
          break;
      }
      ReplyMsg(MyIntuiMessage);
    }
  }
  KillSpinTask((struct Task *)WPtr->UserData);

  about();  /* a little advertizing */

  CleanUp("Clean Finish");
}
VOID CleanUp(string)
REGISTER char *string;
{
  puts(string);
  if (WPtr!=NULL)
    CloseWindow(WPtr);
  if (IntuitionBase!=NULL)
    CloseLibrary((struct Library *)IntuitionBase);
  exit();
}