home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / util / blank / gblanker / source / blankers / main.c < prev    next >
C/C++ Source or Header  |  1994-10-18  |  5KB  |  270 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.  
  11. #include <dos/dos.h>
  12.  
  13. #include <intuition/intuitionbase.h>
  14. #include <graphics/gfxbase.h>
  15.  
  16. #include <clib/exec_protos.h>
  17. #include <clib/intuition_protos.h>
  18. #include <clib/dos_protos.h>
  19.  
  20. #include <string.h>
  21.  
  22. #include "/defs.h"
  23. #include "main.h"
  24.  
  25. LONG PortRemoved = FALSE, Blanking = FALSE, *ServerBlanking, GotPing;
  26. ULONG  Seconds, Micros, LastTime;
  27. struct IntuitionBase *IntuitionBase = 0L;
  28. struct GfxBase *GfxBase = 0L;
  29. struct Library *GarshnelibBase = 0L;
  30. struct MsgPort *ClientPort;
  31. VOID *CurPrefs;
  32.  
  33. extern __far LONG RangeSeed;
  34.  
  35. #define PREFSIZE 512
  36.  
  37. BYTE PrefsPath[128];
  38.  
  39. STRPTR NameSansParens( STRPTR Name )
  40. {
  41.     BYTE Buf[64], *Ptr;
  42.     LONG Len = strlen( Name );
  43.  
  44.     strcpy( Buf, Name );
  45.  
  46.     if( Buf[Len - 1] == ')' )
  47.     {
  48.         for( Ptr = Buf; *Ptr != '('; Ptr++ );
  49.         strcpy( Ptr, Ptr + 1 );
  50.         Buf[Len - 2] = '\0';
  51.     }
  52.  
  53.     return Buf;
  54. }
  55.  
  56. VOID SavePrefs( VOID *Prefs )
  57. {
  58.     BPTR PrefFile;
  59.     
  60.     if( PrefFile = Open( PrefsPath, MODE_NEWFILE ))
  61.     {
  62.         Write( PrefFile, Prefs, PREFSIZE );
  63.         Close( PrefFile );
  64.     }
  65. }
  66.  
  67. VOID *LoadPrefs( BYTE *PrefsName )
  68. {
  69.     BPTR PrefFile;
  70.     VOID *Prefs;
  71.     
  72.     strcpy( PrefsPath, PrefsName );
  73.     strcat( PrefsPath, ".prefs" );
  74.     
  75.     if( Prefs = AllocVec( PREFSIZE, MEMF_CLEAR ))
  76.     {
  77.         if( PrefFile = Open( PrefsPath, MODE_OLDFILE ))
  78.         {
  79.             if( Read( PrefFile, Prefs, PREFSIZE ) != PREFSIZE )
  80.                 FillDefaults( Prefs );
  81.             Close( PrefFile );
  82.         }
  83.         else
  84.             FillDefaults( Prefs );
  85.     }
  86.     
  87.     return Prefs;
  88. }
  89.  
  90. LONG MessageServer( LONG Type )
  91. {
  92.     struct MsgPort *ServerPort;
  93.     BlankMsg *ClientMsg;
  94.     
  95.     if( ServerPort = FindPort( "GarshneServer" ))
  96.     {
  97.         if( ClientMsg = AllocVec( sizeof( BlankMsg ), MEMF_PUBLIC|MEMF_CLEAR ))
  98.         {
  99.             ClientMsg->bm_Mess.mn_ReplyPort = ClientPort;
  100.             ClientMsg->bm_Mess.mn_Length = sizeof( BlankMsg );
  101.             ClientMsg->bm_Type = Type;
  102.             PutMsg( ServerPort, ( struct Message * )ClientMsg );
  103.             
  104.             return OK;
  105.         }
  106.     }
  107.     
  108.     return QUIT;
  109. }
  110.  
  111. LONG HandleSignal( LONG Signal )
  112. {
  113.     BlankMsg *CurMsg;
  114.     
  115.     if( Signal & SIG_WIN )
  116.         DoPrefs( IDCMP, CurPrefs );
  117.     
  118.     if( Signal & SIG_PORT )
  119.     {
  120.         while( CurMsg = ( BlankMsg * )GetMsg( ClientPort ))
  121.         {
  122.             LONG Type = CurMsg->bm_Type;
  123.             LONG Flags = CurMsg->bm_Flags;
  124.             
  125.             if( Type == BM_DELAYEDQUIT )
  126.             {
  127.                 RemPort( ClientPort );
  128.                 PortRemoved = TRUE;
  129.                 Delay( 60 );
  130.             }
  131.             
  132.             ServerBlanking = ( LONG * )CurMsg->bm_Mess.mn_Node.ln_Name;
  133.             
  134.             if( Flags & BF_REPLY )
  135.                 FreeVec( CurMsg );
  136.             else
  137.             {
  138.                 CurMsg->bm_Flags |= BF_REPLY;
  139.                 ReplyMsg(( struct Message * )CurMsg );
  140.             }
  141.             
  142.             switch( Type )
  143.             {
  144.             case BM_DOBLANK:
  145.                 if( !Blanking )
  146.                 {
  147.                     Blanking = TRUE;
  148.                     LastTime = 0L;
  149.                     switch( Blank( CurPrefs ))
  150.                     {
  151.                     case FAILED:
  152.                         MessageServer( BM_FAILED );
  153.                     case QUIT:
  154.                         return QUIT;
  155.                     default:
  156.                         Blanking = FALSE;
  157.                     }
  158.                 }
  159.                 return OK;
  160.             case BM_DOPREFS:
  161.                 if( !SIG_WIN )
  162.                     DoPrefs( STARTUP, CurPrefs );
  163.                 break;
  164.             case BM_UNBLANK:
  165.                 return UNBLANK;
  166.             case BM_DELAYEDQUIT:
  167.             case BM_DOQUIT:
  168.                 return QUIT;
  169.             case BM_PING:
  170.                 GotPing = TRUE;
  171.             default:
  172.                 break;
  173.             }
  174.         }
  175.     }
  176.     
  177.     if( Signal & SIGBREAKF_CTRL_C )
  178.     {
  179.         MessageServer( BM_FAILED );
  180.         return QUIT;
  181.     }
  182.     
  183.     return OK;
  184. }
  185.  
  186. LONG ContinueBlanking( VOID )
  187. {
  188.     LONG Sigs = SetSignal( 0L, SIG_PORT | SIGBREAKF_CTRL_C ), RetVal = OK;
  189.  
  190.     if( Sigs )
  191.         RetVal = HandleSignal( Sigs );
  192.     
  193.     if( RetVal == OK && !*ServerBlanking )
  194.         RetVal = UNBLANK;
  195.     
  196.     return RetVal;
  197. }
  198.  
  199. int main( int argc, char *argv[] )
  200. {
  201.     BlankMsg *FreeMsg;
  202.     static BYTE PortName[] = "GarshneClient";
  203.     ULONG Seconds, Micros;
  204.     LONG RetVal = 0;
  205.     
  206.     if( FindPort( PortName ))
  207.         return 1;
  208.     
  209.     IntuitionBase =
  210.         ( struct IntuitionBase * )OpenLibrary( "intuition.library", 37L );
  211.     GfxBase = ( struct GfxBase * )OpenLibrary( GRAPHICSNAME, 37L );
  212.     GarshnelibBase = OpenLibrary( "Garshnelib.library", 37L );
  213.     
  214.     if( IntuitionBase && GfxBase && GarshnelibBase )
  215.     {
  216.         ClientPort = CreateMsgPort();
  217.         
  218.         if( ClientPort )
  219.         {
  220.             ClientPort->mp_Node.ln_Name = PortName;
  221.             ClientPort->mp_Node.ln_Pri = 0L;
  222.             AddPort( ClientPort );
  223.             CurPrefs = LoadPrefs( NameSansParens( argv[0] ));
  224.             
  225.             CurrentTime( &Seconds, &Micros );
  226.             RangeSeed = Seconds + Micros;
  227.             
  228.             if( MessageServer( BM_INITMSG ) == OK )
  229.                 while( HandleSignal( Wait( SIG_PORT | SIG_WIN |
  230.                                           SIGBREAKF_CTRL_C )) == OK );
  231.             
  232.             if( SIG_WIN )
  233.                 DoPrefs( KILL, CurPrefs );
  234.             
  235.             while( FreeMsg = ( BlankMsg * )GetMsg( ClientPort ))
  236.             {
  237.                 if( FreeMsg->bm_Type & BF_REPLY )
  238.                     FreeVec( FreeMsg );
  239.                 else
  240.                 {
  241.                     FreeMsg->bm_Type |= BF_REPLY;
  242.                     ReplyMsg(( struct Message * )FreeMsg );
  243.                 }
  244.             }
  245.             
  246.             if( CurPrefs )
  247.                 FreeVec( CurPrefs );
  248.             
  249.             if( !PortRemoved )
  250.                 RemPort( ClientPort );
  251.             DeleteMsgPort( ClientPort );
  252.         }
  253.         else
  254.             RetVal = 2;
  255.     }
  256.     else
  257.         RetVal = 1;
  258.     
  259.     if( GarshnelibBase )
  260.         CloseLibrary( GarshnelibBase );
  261.     
  262.     if( GfxBase )
  263.         CloseLibrary(( struct Library * )GfxBase );
  264.     
  265.     if( IntuitionBase )
  266.         CloseLibrary(( struct Library * )IntuitionBase );
  267.     
  268.     return RetVal;
  269. }
  270.