/*
**  WBScreen.c
**
**  Open or close the Workbench. Can also make another screen the default
**  public screen.
*/


#include <exec/types.h>
#include <intuition/intuition.h>
#include <intuition/screens.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/graphics.h>
#include <proto/intuition.h>
#include <string.h>


#define  OPEN   1L << 0
#define  CLOSE  1L << 1

#define  ERROR   5
#define  WARN   10
#define  FAIL   20


UBYTE *Version = { "$VER: WBScreen 2.13 (05.08.98)" };


BOOL CheckWB( void );



int main( int argc, char *argv[] )
{
  BOOL wb;
  char *ptr, **ttypes;
  struct List *psList;
  struct PubScreenNode *psNode;
  UBYTE name[ MAXPUBSCREENNAME + 1 ];
  ULONG secs, ticks = GfxBase->DisplayFlags & PAL ? 50 : 60;
  WORD command = 0, ret = 0;
  
      
  if ( IntuitionBase = ( struct IntuitionBase * )
                                   OpenLibrary( "intuition.library", 37L ))
    {
      if ( ttypes = ArgArrayInit( argc, argv ) )
        {
          wb = CheckWB();
          
          
          ptr = ArgString( ttypes, "CLOSE", 0 );
          
          if ( ptr)
            if ( wb == TRUE )
              {
                CloseWorkBench();
                
                command = CLOSE;
              }
            else
              ret = ERROR;
          
          
          ptr = ArgString( ttypes, "OPEN", 0 );
          
          if ( ptr )
            if ( wb == FALSE )
              {
                OpenWorkBench();
                
                command = OPEN;
              }
            else
              ret = WARN;
          
          
          secs = ticks * ( ArgInt( ttypes, "DELAY", 0 ));
          
          if ( secs )
            Delay( secs );      // Wait before trying to find the screen. 
            
          
          ptr = ArgString( ttypes, "DEFAULT", 0 );
          
          if ( ptr )                //  If a pub screen name is specified 
            {                       //  we'll make that one the default.  
              struct Screen *scr;   //  If AUTO is specified, we'll make  
                                    //  the frontmost screen the default. 
              
              if ( stricmp( ptr, "AUTO" ) == NULL )
                {
                  ULONG lock;
                  
                  lock = LockIBase( NULL );   //  Get a ptr to first scr. 
                  
                    scr = IntuitionBase->FirstScreen;
                    
                  UnlockIBase( lock );  
                    
                  
                  psList = LockPubScreenList();
                  
                  psNode = ( struct PubScreenNode * ) psList->lh_Head;
                  
                  while ( psNode )
                    {
                      if ( scr == psNode->psn_Screen )
                        {
                          strcpy( name, psNode->psn_Node.ln_Name );
                          
                          ptr = name;
                          
                          break;            //  Find the pub screen name  
                        }                   //  of this frontmost screen. 
                      
                      psNode = ( struct PubScreenNode * )
                                                  psNode->psn_Node.ln_Succ;
                    }
                  
                  UnlockPubScreenList();
                }
              
              
              if ( scr = LockPubScreen( ptr ))
                {
                  SetDefaultPubScreen( ptr );
                  
                  SetPubScreenModes( POPPUBSCREEN | SHANGHAI );
                  
                  UnlockPubScreen( NULL, scr );
                }
            }
          
          ArgArrayDone();
          
          
          if ( command )
            {
              if (! secs )            //  Wait until Workbench is removed 
                Delay( ticks / 6 );   //  from or added to PubScreenList. 
              
              
              wb = CheckWB();
              
              
              if ( command == CLOSE )
                ret = ( wb == TRUE ) ? FAIL : 0;
              
              else if ( command == OPEN )
                ret = ( wb == FALSE ) ? FAIL : 0;
            }
        }
        
      
        CloseLibrary( ( struct Library * ) IntuitionBase );
    }
  
  
  return ret;
}


/*  Check if the Workbench is open or closed.
**
**  Return TRUE if open, FALSE if closed.
**
**  Can't use LockWorkbench() in this function, cause that would open the
**  Workbench automatically.
*/

BOOL CheckWB( void )
{
  BOOL ret = FALSE;
  struct List *psList;
  struct PubScreenNode *psNode;
  
  psList = LockPubScreenList();
  
  psNode = ( struct PubScreenNode * ) psList->lh_Head;
  
  while ( psNode )
    {
      if ( strcmp( psNode->psn_Node.ln_Name, "Workbench" ) == NULL )
        {
          ret = TRUE;           //  Because of a bug in CloseWorkbench(), 
                                //  which always returns 0, we check here 
          break;                //  if the Workbench is opened or not.    
        }
      
      psNode = ( struct PubScreenNode * ) psNode->psn_Node.ln_Succ;
    }
  
  UnlockPubScreenList();
  
  return ret;
}
