home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / util / gblanker-38.8.lha / GBlanker / GSource / Blankers / debug.c < prev    next >
C/C++ Source or Header  |  1994-11-12  |  6KB  |  278 lines

  1. /*
  2.  *    Copyright (c) 1994 Michael D. Bayne.
  3.  *    All rights reserved.
  4.  *
  5.  *    Please see the documentation accompanying the distribution for distribution
  6.  *  and disclaimer information.
  7.  */
  8.  
  9. #include <exec/memory.h>
  10. #include <intuition/intuitionbase.h>
  11. #include <graphics/gfxbase.h>
  12. #include <string.h>
  13.  
  14. #include "includes.h"
  15.  
  16. LONG PortRemoved = FALSE, Blanking = FALSE, *ServerBlanking = 0L;
  17. ULONG InitSecs, InitMicros, CheckCPU = FALSE, StopIt = FALSE;
  18. struct Library *IntuitionBase, *GarshnelibBase, *GfxBase;
  19. BYTE PortName[] = "GarshneClient";
  20. struct MsgPort *ClientPort, *TimerPort;
  21. struct timerequest *TimeOutIO;
  22. PrefObject *CurPrefs;
  23. BYTE PrefsPath[128];
  24.  
  25. extern __far LONG RangeSeed;
  26.  
  27. PrefObject *LoadPrefs( STRPTR Path )
  28. {
  29.     PrefObject *Prefs;
  30.     BPTR PrefFile;
  31.     LONG Objects;
  32.  
  33.     if( Prefs = AllocVec( sizeof( PrefObject ) * 25, MEMF_CLEAR ))
  34.     {
  35.         if( PrefFile = Open( Path, MODE_OLDFILE ))
  36.         {
  37.             Read( PrefFile, &Objects, sizeof( LONG ));
  38.             Read( PrefFile, Prefs, sizeof( PrefObject ) * Objects );
  39.             Close( PrefFile );
  40.         }
  41.         else
  42.             Defaults( Prefs );
  43.     }
  44.  
  45.     return Prefs;
  46. }
  47.  
  48. int main( void )
  49. {
  50.     struct Process *Proc = ( struct Process * )FindTask( 0L );
  51.     BlankMsg *FreeMsg;
  52.     LONG ReturnVal = 0;
  53.     
  54.     if( Proc->pr_CLI )
  55.     {
  56.         STRPTR Str = BADDR((( struct CommandLineInterface * )
  57.                             BADDR( Proc->pr_CLI ))->cli_CommandName );
  58.         CopyMem( Str + 1, PrefsPath, *Str );
  59.         PrefsPath[*Str] = '\0';
  60.         strcat( PrefsPath, ".prefs" );
  61.     }
  62.     
  63.     IntuitionBase = OpenLibrary( "intuition.library", 37L );
  64.     GfxBase = OpenLibrary( GRAPHICSNAME, 37L );
  65.     GarshnelibBase = OpenLibrary( "Garshnelib.library", 37L );
  66.     
  67.     if( IntuitionBase && GfxBase && GarshnelibBase )
  68.     {
  69.         ClientPort = CreateMsgPort();
  70.         TimerPort = CreateMsgPort();
  71.         
  72.         if( ClientPort && TimerPort )
  73.         {
  74.             TimeOutIO = ( struct timerequest * )
  75.                 CreateExtIO( TimerPort, sizeof( struct timerequest ));
  76.             
  77.             if( TimeOutIO && !OpenDevice( "timer.device", UNIT_VBLANK,
  78.                                          ( struct IORequest * )TimeOutIO, 0L ))
  79.             {
  80.                 ClientPort->mp_Node.ln_Name = PortName;
  81.                 ClientPort->mp_Node.ln_Pri = 0L;
  82.                 AddPort( ClientPort );
  83.                 CurPrefs = LoadPrefs( PrefsPath );
  84.                 
  85.                 CurrentTime( &InitSecs, &InitMicros );
  86.                 RangeSeed = InitSecs + InitMicros;
  87.                 
  88.                 Blank( CurPrefs );
  89.  
  90.                 while( FreeMsg = ( BlankMsg * )GetMsg( ClientPort ))
  91.                 {
  92.                     if( FreeMsg->bm_Type & BF_REPLY )
  93.                         FreeVec( FreeMsg );
  94.                     else
  95.                     {
  96.                         FreeMsg->bm_Type |= BF_REPLY;
  97.                         ReplyMsg(( struct Message * )FreeMsg );
  98.                     }
  99.                 }
  100.                 
  101.                 if( CurPrefs )
  102.                     FreeVec( CurPrefs );
  103.             
  104.                 if( !PortRemoved )
  105.                     RemPort( ClientPort );
  106.             }
  107.             
  108.             if( TimeOutIO )
  109.             {
  110.                 if( TimeOutIO->tr_node.io_Device )
  111.                     CloseDevice(( struct IORequest * )TimeOutIO );
  112.                 DeleteExtIO(( struct IORequest * )TimeOutIO );
  113.             }
  114.         }
  115.         else
  116.         {
  117.             Complain( "ClientPort or TimerPort failed to open." );
  118.             ReturnVal = 2;
  119.         }
  120.  
  121.         if( ClientPort )
  122.         {
  123.             BlankMsg *TmpMsg;
  124.             while( TmpMsg = ( BlankMsg * )GetMsg( ClientPort ))
  125.             {
  126.                 TmpMsg->bm_Flags |= BF_REPLY;
  127.                 ReplyMsg(( struct Message * )TmpMsg );
  128.             }
  129.             DeleteMsgPort( ClientPort );
  130.         }
  131.  
  132.         if( TimerPort )
  133.             DeleteMsgPort( TimerPort );
  134.     }
  135.     else
  136.     {
  137.         Complain( "A library failed to open." );
  138.         ReturnVal = 1;
  139.     }
  140.     
  141.     if( GarshnelibBase )
  142.         CloseLibrary( GarshnelibBase );
  143.     
  144.     if( GfxBase )
  145.         CloseLibrary( GfxBase );
  146.     
  147.     if( IntuitionBase )
  148.         CloseLibrary( IntuitionBase );
  149.     
  150.     return ReturnVal;
  151. }
  152.  
  153. LONG MessageServer( LONG Type )
  154. {
  155.     struct MsgPort *ServerPort;
  156.     BlankMsg *ClientMsg;
  157.     
  158.     if( ServerPort = FindPort( "GarshneServer" ))
  159.     {
  160.         if( ClientMsg = AllocVec( sizeof( BlankMsg ), MEMF_PUBLIC|MEMF_CLEAR ))
  161.         {
  162.             ClientMsg->bm_Mess.mn_ReplyPort = ClientPort;
  163.             ClientMsg->bm_Mess.mn_Length = sizeof( BlankMsg );
  164.             ClientMsg->bm_Type = Type;
  165.             PutMsg( ServerPort, ( struct Message * )ClientMsg );
  166.             
  167.             return OK;
  168.         }
  169.     }
  170.     
  171.     return QUIT;
  172. }
  173.  
  174. LONG HandleSignal( LONG Signal )
  175. {
  176.     BlankMsg *CurMsg;
  177.     LONG RetVal = OK;
  178.     
  179.     if( Signal & SIG_TIMER )
  180.     {
  181.         MessageServer( BM_FAILED );
  182.         RetVal = UNBLANK;
  183.     }
  184.     
  185.     if( Signal & SIG_PORT )
  186.     {
  187.         while( CurMsg = ( BlankMsg * )GetMsg( ClientPort ))
  188.         {
  189.             LONG Type = CurMsg->bm_Type;
  190.             LONG Flags = CurMsg->bm_Flags;
  191.             
  192.             if( Type != BM_PING )
  193.                 ServerBlanking = ( LONG * )CurMsg->bm_Mess.mn_Node.ln_Name;
  194.             
  195.             if( Flags & BF_REPLY )
  196.                 FreeVec( CurMsg );
  197.             else
  198.             {
  199.                 CurMsg->bm_Flags |= BF_REPLY;
  200.                 ReplyMsg(( struct Message * )CurMsg );
  201.             }
  202.             
  203.             switch( Type )
  204.             {
  205.             case BM_DOBLANK:
  206.             case BM_DOTESTBLANK:
  207.                 if( !Blanking )
  208.                 {
  209.                     PrefObject *TmpPrefs;
  210.  
  211.                     Blanking = TRUE;
  212.                     CurrentTime( &InitSecs, &InitMicros );
  213.                     if( Type == BM_DOTESTBLANK )
  214.                         TmpPrefs = LoadPrefs( "T:GBlankerTmpPrefs" );
  215.                     switch( Blank( Type == BM_DOBLANK ? CurPrefs : TmpPrefs ))
  216.                     {
  217.                     case FAILED:
  218.                         /* In this case the Blank() function failed to init */
  219.                         /* (ie. no memory or something). So we yell. */
  220.                         MessageServer( BM_FAILED );
  221.                         break;
  222.                     case DELAYEDQUIT:
  223.                     case QUIT:
  224.                         RetVal = QUIT;
  225.                         break;
  226.                     }
  227.                     Blanking = FALSE;
  228.                     if( Type == BM_DOTESTBLANK )
  229.                         FreeVec( TmpPrefs );
  230.                 }
  231.                 break;
  232.             case BM_RELOADPREFS:
  233.                 if( CurPrefs )
  234.                     FreeVec( CurPrefs );
  235.                 CurPrefs = LoadPrefs( PrefsPath );
  236.                 break;
  237.             case BM_DELAYEDQUIT:
  238.                 RemPort( ClientPort );
  239.                 PortRemoved = TRUE;
  240.                 RetVal = DELAYEDQUIT;
  241.                 break;
  242.             case BM_DOQUIT:
  243.                 RetVal = QUIT;
  244.                 break;
  245.             case BM_UNBLANK:
  246.                 break;
  247.             }
  248.         }
  249.     }
  250.     
  251.     if( Signal & SIGBREAKF_CTRL_C )
  252.         RetVal = QUIT;
  253.  
  254.     return RetVal;
  255. }
  256.  
  257. LONG ContinueBlanking( VOID )
  258. {
  259.     if( SetSignal( 0L, SIGBREAKF_CTRL_C ) & SIGBREAKF_CTRL_C )
  260.         return QUIT;
  261.  
  262.     if( StopIt )
  263.         return QUIT;
  264.  
  265.     return OK;
  266. }
  267.  
  268. VOID Complain( STRPTR Bitch )
  269. {
  270.     if( IntuitionBase )
  271.     {
  272.         struct EasyStruct ErrorReq = {
  273.             sizeof( struct EasyStruct ), 0, "Error", 0L, "Ok" };
  274.         ErrorReq.es_TextFormat = Bitch;
  275.         EasyRequestArgs( 0L, &ErrorReq, 0L, 0L );
  276.     }
  277. }
  278.