#ifndef NOIDENT
const static char RCSid[] = "$Header: qc:c-src/common/misclib/RCS/autotimer.c,v 1.1 1993/10/02 10:57:47 alph Exp $";
#endif

/*
 *----------------------------------------------------------------------
 *
 * $Source: qc:c-src/common/misclib/RCS/autotimer.c,v $
 * $Revision: 1.1 $
 * $Date: 1993/10/02 10:57:47 $
 * $State: Exp $
 * $Author: alph $
 * $Locker:  $
 *
 *----------------------------------------------------------------------
 *
 * alph --- SAS C auto initialization functions
 * 
 * This code is (C) Copyright 1993 by Carsten Heyl. All rights reserved.
 * This code is NOT in the Public Domain.
 * It may not be copied or distributed without a license.
 *
 *----------------------------------------------------------------------
 * $Log: autotimer.c,v $
 * Revision 1.1  1993/10/02  10:57:47  alph
 * Initial revision
 *
 *----------------------------------------------------------------------
 */

#include <exec/types.h>
#include <exec/memory.h>
#include <exec/libraries.h>
#include <devices/timer.h>

#include <intuition/intuition.h>

#include <proto/exec.h>
#include <proto/intuition.h>

#include <stdlib.h>

extern STRPTR _ProgramName;	/* SAS startup module defines this :-) */

struct Library *TimerBase = NULL;
struct timerequest *_STtr = NULL;

static void DoReq(UBYTE *errorStr)
{
    struct Library *IntuitionBase;

    IntuitionBase = OpenLibrary("intuition.library", 36);
  
    if (IntuitionBase != NULL) 
    {
	struct EasyStruct libraryES;
    
	libraryES.es_StructSize = sizeof(libraryES);
	libraryES.es_Flags = 0;
	libraryES.es_Title = _ProgramName;
	libraryES.es_TextFormat = errorStr;
	libraryES.es_GadgetFormat = "Exit %s";
    
	EasyRequestArgs(NULL, &libraryES, NULL, (APTR)&_ProgramName);

	CloseLibrary(IntuitionBase);
    }
}

/****** amiga.lib/autoinit *********************************************
*
*   NAME   
*       autoinit - SAS C Autoinitialization Functions 
*
*   SYNOPSIS
*       _STIopenTimerDev()
*
*       void _STIopenTimerDev(void)
*
*       _STDcloseTimerDev()
*
*       void _STDcloseTimerDev(void)
*
*   FUNCTION
*       These functions open and close the timer.device at the
*       startup and exit of the program, respectively. For a
*       program to use these functions, it must be linked with
*       autotimer.o.
*
*   NOTES
*
*       The autoinitialization and autotermination functions are
*       features specific to the SAS C6. However, these functions
*       can be used with other (ANSI) C compilers, too. Example
*       follows: 
*
*       \* at start of main() *\
*
*       atexit(_STDcloseTimerDev);
*       _STDopenTimerDev();
*
*   BUGS
*
*   SEE ALSO
*       bsdsocket.library/SetErrnoPtr(),
*       SAS/C 6 User's Guide p. 145 for details of
*       autoinitialization and autotermination functions.  
*****************************************************************************
*
*/

void
_STIopenTimerDev(void)
{
    static short done = 0;

    if (done++)		/* try only once (SAS/C 6.2 liked to call this twice) */
	return;

    /*
     * Check OS version
     */
    if ((*(struct Library **)4)->lib_Version < 37)
	exit(20);

    _STtr = (struct timerequest *) AllocMem(sizeof(struct timerequest),
					    MEMF_PUBLIC | MEMF_CLEAR);
    if(!_STtr)
	exit(20);

    if(!OpenDevice(TIMERNAME, UNIT_MICROHZ, 
		  (struct IORequest *) _STtr, 0L))
    {
	TimerBase = (struct Library *) _STtr->tr_node.io_Device;
#if 0
	DoReq("Open timer ok.");
#endif
	return;
    }
    else
	DoReq("Could not open "TIMERNAME".");
    exit(20);
}

void
_STDcloseTimerDev(void)
{
    if (TimerBase) {
	CloseDevice((struct IORequest *) _STtr);
	TimerBase = NULL;
    }
    if(_STtr)
    {
	FreeMem(_STtr, sizeof(struct timerequest));
	_STtr = NULL;
    }
}
