/*
 *  FILE: stdstuff.c
 *  Support routines for creating and dealing with standard libraries
 *  and devices.
 *
 *  Public Domain, but keep my name in it as the original author.
 *  30-Sep-88	Jan Sven Trabandt   added to gimme.lib
 *  31-Oct-88	Jan Sven Trabandt   added new options
 *				    made getRidOfStdStuff more controllable
 */


#define I_AM_STDSTUFF
#include "gimmelib/gimmefuncs.h"
#include "gimmelib/stdstuff.h"

#ifdef AZTEC_C
extern int Enable_Abort;
#endif AZTEC_C

struct IntuitionBase	*IntuitionBase = NULL;
struct GfxBase		*GfxBase = NULL;
struct DiskfontBase	*DiskfontBase = NULL;
struct Device		*TimerBase = NULL;

struct MsgPort	    *gimTimerPort = NULL;
struct timerequest  *gimMasterTR = NULL;
struct timerequest  *gimSlaveTR = NULL;

struct MsgPort	    *gimMainPort = NULL;
SHORT		    gimSubTasks = 0;

void		    *gimChainMem = NULL;


ULONG gimmeStdStuff( myflags, rev )
    ULONG   myflags;
    SHORT   rev;
{
    if( myflags & GSS_DISABLEABORT ) {
#ifdef AZTEC_C
	Enable_Abort = 0;
#endif
    }
    if( myflags & GSS_INTUITION ) {
	if( !(IntuitionBase = (struct IntuitionBase *)
			    OpenLibrary("intuition.library", (long) rev)) ) {
	    return( GSS_INTUITION );
	}
    }
    if( myflags & GSS_GFX ) {
	if( !(GfxBase = (struct GfxBase *)
			    OpenLibrary("graphics.library", (long) rev)) ) {
	    return( GSS_GFX );
	}
    }
    if( myflags & GSS_CLOSEWBENCH ) {
	if( CloseWorkBench() ) {
	    return( GSS_CLOSEWBENCH );
	}
    }
    if( myflags & GSS_DISKFONT ) {
	if( !(DiskfontBase = (struct DiskfontBase *)
			    OpenLibrary("diskfont.library", (long) rev)) ) {
	    return( GSS_DISKFONT );
	}
    }
    if( myflags & GSS_TIMER ) {
	if( !(gimMasterTR = accessTimer(UNIT_MICROHZ, &gimTimerPort)) ) {
	    return( GSS_TIMER );
	}
	if( !(gimSlaveTR = gimmeTimeRequest(gimMasterTR)) ) {
	    return( GSS_TIMER );
	}
    }
    if( myflags & GSS_SUBTASK ) {
	if( initSubTasker(&gimSubTasks, &gimMainPort) ) {
	    return( GSS_SUBTASK );
	}
    }
    return( 0L );
} /* gimmeStdStuff */


ULONG getRidOfStdStuff( myflags )
    ULONG   myflags;
{
    if( myflags & GSS_MEMCHAIN ) {
	if( gimChainMem ) {
	    chainFreeMem( gimChainMem );
	    gimChainMem = NULL;
	}
    }
    if( myflags & GSS_SUBTASK ) {
	if( gimMainPort ) {
	    handleSpecialSubTaskMsg( &gimSubTasks, &gimMainPort );
	    if( doneSubTasker(&gimSubTasks, &gimMainPort)
			&& !(myflags & GSS_RUTHLESS) ) {
		return( GSS_SUBTASK );
	    }
	    gimSubTasks = 0;
	    gimMainPort = NULL;
	}
    }
    if( myflags & GSS_TIMER ) {
	if( gimSlaveTR ) {
	    if( getRidOfTimeRequest(gimSlaveTR) && !(myflags & GSS_RUTHLESS) ) {
		return( GSS_TIMER );
	    }
	    gimSlaveTR = NULL;
	}
	if( gimMasterTR ) {
	    if( releaseTimer(gimMasterTR, NULL) && !(myflags & GSS_RUTHLESS) ) {
		return( GSS_TIMER );
	    }
	    gimMasterTR = NULL;
	    gimTimerPort = NULL;
	}
    }
    if( myflags & GSS_DISKFONT ) {
	if( DiskfontBase ) {
	    CloseLibrary( DiskfontBase );
	    DiskfontBase = NULL;
	}
    }
    if( myflags & GSS_OPENWBENCH ) {
	if( OpenWorkBench() && !(myflags & GSS_RUTHLESS) ) {
	    return( GSS_OPENWBENCH );
	}
    }
    if( myflags & GSS_GFX ) {
	if( GfxBase ) {
	    CloseLibrary( GfxBase );
	    GfxBase = NULL;
	}
    }
    if( myflags & GSS_INTUITION ) {
	if( IntuitionBase ) {
	    CloseLibrary( IntuitionBase );
	    IntuitionBase = NULL;
	}
    }
    if( myflags & GSS_ENABLEABORT ) {
#ifdef AZTEC_C
	Enable_Abort = 1;
#endif
    }
    return( 0L );
} /* getRidOfStdStuff */
