home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d548 / sandglass.lha / SandGlass / SandGlass_Sources.lzh / SandGlass.c < prev    next >
C/C++ Source or Header  |  1991-08-06  |  8KB  |  310 lines

  1. /* ========================================================================== */
  2. /* SandGlass V1.0 by Dirk O. Remmelt                                          */
  3. /* -------------------------------------------------------------------------- */
  4. /* This program animates the Workbench-busy-or-zz-mouse-pointer. After        */
  5. /* starting the program, and while a disk-device-access is taking place, the  */
  6. /* mouse-pointer represents itself as a sandglass, purling sand through it,   */
  7. /* turning around and starting all over again. ( A rather long sentence,      */
  8. /* Douglas N. Adams would be proud of me! )                                   */
  9. /* Please read also the SandGlass.doc file !                                  */
  10. /* ========================================================================== */
  11.  
  12.  
  13.  
  14. #include <exec/exec.h>                           /* includes                  */
  15. #include <graphics/gfx.h>
  16. #include <graphics/gfxbase.h>
  17. #include <intuition/intuition.h>
  18. #include <intuition/intuitionbase.h>
  19. #include <workbench/startup.h>
  20. #include <workbench/icon.h>
  21. #include <workbench/workbench.h>
  22. #include <stdio.h>
  23. #include "ImageDatas.h"
  24.  
  25.  
  26.  
  27. struct IntuitionBase *IntuitionBase;
  28. struct GfxBase       *GfxBase;
  29. struct IconBase      *IconBase;
  30.  
  31. struct Task          *ThisTask;
  32.  
  33. struct IntuiMessage  *IntuiMsg;
  34. struct Screen        *WBScreen;                  /* ptr to Workbench-Screen   */
  35. struct Window        *SandGlassWindow = NULL,    /* window to quit SandGlas   */
  36.                      *ActiveWindow;
  37.  
  38. struct NewWindow      nw =                       /* window-declaration of     */
  39. {                                                /* SandGlasWindow            */
  40.   0, 11,                                         /* LeftEdge, TopEdge         */
  41.   103, 10,                                       /* Width, Height             */
  42.   0, 1,                                          /* DetailPen, BlockPen       */
  43.   CLOSEWINDOW,                                   /* IDCMPFlags                */
  44.   WINDOWCLOSE | NOCAREREFRESH,                   /* Flags                     */
  45.   NULL, NULL,                                    /* FirstGadget, CheckMark    */
  46.   (UBYTE *) "SandGlass",                         /* Title                     */
  47.   NULL, NULL,                                    /* Screen, BitMap            */
  48.   103, 10,                                       /* MinWidth, MinHeight       */
  49.   103, 10,                                       /* MaxWidth, MaxHeight       */
  50.   WBENCHSCREEN                                   /* Type                      */
  51. };
  52.  
  53. USHORT *pointer;                                 /* start-address of and      */
  54. USHORT  old_pointer [48];                        /* buffer for original zz-   */
  55.                                                  /* mouse-pointer             */
  56. int     delay    = 5,
  57.         priority = -10,
  58.         old_priority;
  59.  
  60.  
  61.  
  62.  
  63. extern void CloseGraphix ();
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. void SetMyPointer ( int number )                 /* procedure to set one of   */
  71. {                                                /* the 14 images             */
  72.   int i;
  73.  
  74.   for ( i=0; i<32; i++ )
  75.     *(pointer+i+16) = ImageData [number][i];
  76.  
  77.   return ();
  78. }
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. void OpenGraphix ()
  86. {
  87.   int i;
  88.  
  89.   if ( ! ( GfxBase = (struct GfxBase *)
  90.     OpenLibrary ( "graphics.library", 0L ) ) )
  91.   {
  92.     printf ( "Can't open graphics.library !\n" );
  93.     CloseGraphix ();
  94.   }
  95.  
  96.   if ( ! ( IntuitionBase = (struct IntuitionBase *)
  97.     OpenLibrary ( "intuition.library", 0L ) ) )
  98.   {
  99.     printf ( "Can't open intuition.library !\n" );
  100.     CloseGraphix ();
  101.   }
  102.  
  103.   if ( ! ( SandGlassWindow = (struct Window *)
  104.     OpenWindow ( &nw ) ) )
  105.   {
  106.     printf ( "Can't open window !\n" );
  107.     CloseGraphix ();
  108.   }
  109.  
  110.   ThisTask = FindTask ( NULL );
  111.   if ( ThisTask ) old_priority = SetTaskPri ( ThisTask, priority );
  112.  
  113.   WBScreen     = IntuitionBase->FirstScreen;
  114.  
  115.   return ();
  116. }
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123. int compare ( char *str_ptr_1, char *str_ptr_2, int length )
  124. {
  125.   APTR ptr;
  126.  
  127.   if ( ! ( strncmp ( str_ptr_1, str_ptr_2, length ) ) )
  128.   {
  129.     if ( ( strlen ( str_ptr_2 ) ) > length )
  130.     {
  131.       ptr = (APTR)( (int)str_ptr_2 + (int)length );
  132.       return ( atoi ( ptr ) );
  133.     }
  134.   }
  135.  
  136.   return ( -1 );
  137. }
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144. void GetSetups ( int count, char *arg_ptr [], struct WBStartup *arg_wb_ptr )
  145. {
  146.   int    i, x;
  147.   struct WB_Arg     *wb_args;
  148.   struct DiskObject *disk_obj;
  149.   char **toolarray;
  150.   char  *s;
  151.  
  152.   if ( count )                                   /* started from CLI          */
  153.   {
  154.     for ( i=1; i<count; i++ )
  155.       *arg_ptr [i] = (char)( toupper ( (int)(*arg_ptr [i]) ) );
  156.  
  157.     for ( i=1; i<count; i++ )
  158.     {
  159.       if ( ! strcmp ( arg_ptr [i], "?" ) )
  160.       {
  161.         printf ( "Usage: SandGlass [?] [D=delay] [P=priority]\n" );
  162.         exit ( 0 );
  163.       }
  164.       else
  165.       if ( ( ( x = compare ( "D=", arg_ptr [i], 2 ) ) != -1 ) )
  166.       {
  167.         if ( x < 0 )
  168.         {
  169.           printf ( "Process Delay Time must be 0<=delay!\n" );
  170.           exit ( 0 );
  171.         }
  172.         else delay = x;
  173.       }
  174.       else
  175.       if ( ( ( x = compare ( "P=", arg_ptr [i], 2 ) ) != -1 ) )
  176.       {
  177.         if ( ( x < -128 ) || ( x > 127 ) )
  178.         {
  179.           printf ( "Task Priority must be -128<=priority<=127!\n" );
  180.           exit ( 0 );
  181.         }
  182.         else priority = x;
  183.       }
  184.     }
  185.   }
  186.   else                                           /* started from Workbench    */
  187.   {
  188.     if ( IconBase = ( struct IconBase *)
  189.       OpenLibrary ( "icon.library", 0L ) )
  190.     {
  191.       wb_args = (struct WBArg *)arg_wb_ptr->sm_ArgList;
  192.       if ( *wb_args->wa_Name )
  193.       {
  194.         if ( disk_obj = GetDiskObject ( wb_args->wa_Name ) )
  195.         {
  196.           toolarray = (char **)disk_obj->do_ToolTypes;
  197.           if ( s = (char *)FindToolType ( toolarray, "DELAY" ) )
  198.           {
  199.             delay = atoi ( s );
  200.             if ( delay < 0 ) delay = 5;
  201.           }
  202.           if ( s = (char *)FindToolType ( toolarray, "PRIORITY" ) )
  203.           {
  204.             priority = atoi ( s );
  205.             if ( ( priority < -128 ) || ( priority > 127 ) ) priority = -10;
  206.           }
  207.  
  208.           FreeDiskObject ( disk_obj );
  209.         }
  210.       }
  211.  
  212.       CloseLibrary ( IconBase );
  213.     }
  214.   }
  215.  
  216.   return ();
  217. }
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224. void ActOnIDCMP ()
  225. {
  226.   ULONG class;
  227.  
  228.   if ( IntuiMsg = (struct IntuiMessage *)        /* check for window-close-   */
  229.     GetMsg ( SandGlassWindow->UserPort ) )       /* message                   */
  230.   {
  231.     class = IntuiMsg->Class;
  232.  
  233.     ReplyMsg ( IntuiMsg );
  234.  
  235.     if ( class == CLOSEWINDOW ) CloseGraphix ();
  236.   }
  237.  
  238.   return ();
  239. }
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246. void CloseGraphix ()
  247. {
  248.   int i;
  249.  
  250.   if ( pointer )
  251.     for ( i=0; i<48; i++ )                       /* restore original mouse-   */
  252.       *(pointer+i) = old_pointer [i];            /* pointer                   */
  253.  
  254.   SetTaskPri ( ThisTask, old_priority );
  255.  
  256.   if ( SandGlassWindow ) CloseWindow  ( SandGlassWindow );
  257.   if ( GfxBase )         CloseLibrary ( GfxBase );
  258.   if ( IntuitionBase )   CloseLibrary ( IntuitionBase );
  259.  
  260.   exit ( 0 );
  261. }
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. void main ( int argc, char *argv[] )
  269. {
  270.   int i;
  271.  
  272.   GetSetups ( argc, argv, argv );
  273.  
  274.   OpenGraphix ();
  275.  
  276.   ActiveWindow = IntuitionBase->ActiveWindow;
  277.   while ( ( ! ActiveWindow->Pointer ) ||
  278.           ( ActiveWindow->WScreen != WBScreen ) )
  279.   {
  280.     ActOnIDCMP ();
  281.     ActiveWindow = IntuitionBase->ActiveWindow;
  282.  
  283.     Delay ( delay );
  284.   }
  285.  
  286.   pointer = ActiveWindow->Pointer;
  287.  
  288.   for ( i=0; i<48; i++ )                         /* save original mouse-      */
  289.     old_pointer [i] = *(pointer+i);              /* pointer                   */
  290.  
  291.   for ( i=0; i<16; i++ )                         /* clear first 8 rows        */
  292.     *(pointer+i) = 0x0000;
  293.  
  294.   SetMyPointer ( 0 );                            /* set first image           */
  295.  
  296.   for (;;)
  297.   {
  298.     ActOnIDCMP ();
  299.  
  300.     for ( i=0; i<14; i++ )
  301.     {
  302.       ActiveWindow = IntuitionBase->ActiveWindow;
  303.       if ( ( ActiveWindow->Pointer ) && ( ActiveWindow->WScreen == WBScreen ) )
  304.         SetMyPointer ( i );                      /* animate only Workbench-   */
  305.                                                  /* zz-mouse-pointer          */
  306.       Delay ( delay );
  307.     }
  308.   }
  309. }
  310.