AmiTimeKeeper/library.c

342 lines
9.5 KiB
C

/*-
* Copyright (c) 2017-2018 Carsten Sonne Larsen <cs@innolan.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "config.h"
#include "time.h"
#include "mem.h"
/* Library identifiers */
static const char *dosName = "dos.library";
static const char *aslName = "asl.library";
static const char *iconName = "icon.library";
static const char *commoditiesName = "commodities.library";
static const char *localeName = "locale.library";
static const char *utilityName = "utility.library";
static const char *gadgetName = "gadtools.library";
static const char *intuitionName = "intuition.library";
static const char *mathName = "mathieeedoubbas.library";
#if !defined(LIB_HOST)
/* Use fake bases */
#define DOSBase __FX_DOSBase
#define LocaleBase __FX_LocaleBase
#define UtilityBase __FX_UtilityBase
#define IntuitionBase __FX_IntuitionBase
#define CxBase __FX_CxBase
#define AslBase __FX_AslBase
#define IconBase __FX_IconBase
#define GadToolsBase __FX_GadToolsBase
#define __MathIeeeDoubBasBase __FX__MathIeeeDoubBasBase
#define __UtilityBase __FX__UtilityBase
#endif
/* Library addresses */
//struct ExecBase *SysBase = (struct ExecBase *)4L;
struct DosLibrary *DOSBase = NULL;
struct LocaleBase *LocaleBase = NULL;
struct UtilityBase *UtilityBase = NULL;
struct IntuitionBase *IntuitionBase = NULL;
struct Library *CxBase = NULL;
struct Library *AslBase = NULL;
struct Library *IconBase = NULL;
struct Library *GadToolsBase = NULL;
struct Library *__MathIeeeDoubBasBase = NULL;
struct UtilityBase *__UtilityBase = NULL;
#define DOSLIB_NAME ((STRPTR)dosName)
#define DOSLIB_REV 36L
#define ASLLIB_NAME ((STRPTR)aslName)
#define ASLLIB_REV 37L
#define ICONLIB_NAME ((STRPTR)iconName)
#define ICONLIB_REV 36L
#define COMMODLIB_NAME ((STRPTR)commoditiesName)
#define COMMODLIB_REV 37L
#define INTUITIONLIB_NAME ((STRPTR)intuitionName)
#define INTUITIONLIB_REV 37L
#define LOCALELIB_NAME ((STRPTR)localeName)
#define LOCALELIB_REV 37L
#define UTILLIB_NAME ((STRPTR)utilityName)
#define UTILLIB_REV 37L
#define GADGETLIB_NAME ((STRPTR)gadgetName)
#define GADGETLIB_REV 37L
#define MATHLIB_NAME ((STRPTR)mathName)
#define MATHLIB_REV 34L
// RTC Clock
static const char *batteryClockName = "battclock.resource";
#define BATTCLOCK_NAME ((STRPTR)batteryClockName)
struct Library *BattClockBase = NULL;
static struct Device *TimerDevice = NULL;
static const char *currentLocale = "Current Locale";
static void OpenLibrarySuccess(STRPTR name)
{
LogTrace("Opened %s", name);
}
static void OpenLibraryError(STRPTR name, long version)
{
LogError("Cannot open %s %ld.0", name, version);
}
static void ClosingLibrary(STRPTR name)
{
LogTrace("Closing %s", name);
}
static void OpenResourceSuccess(const char *name)
{
LogTrace("Opened %s", name);
}
static void OpenResourceError(const char *name)
{
LogError("Cannot open %s", name);
}
static void ClosingResource(const char *name)
{
LogTrace("Closing %s", name);
}
int OpenLibraries(void)
{
// DOS Library
if (!(DOSBase = (struct DosLibrary *)OpenLibrary((STRPTR)DOSLIB_NAME, DOSLIB_REV)))
{
OpenLibraryError(DOSLIB_NAME, DOSLIB_REV);
return LIB_ERROR;
}
#ifdef AOS3
// Library does not always provide IdString
OpenResourceSuccess(DOSLIB_NAME);
#else
OpenLibrarySuccess(((struct Library *)DOSBase)->lib_IdString);
#endif
// Math Library
if (!(__MathIeeeDoubBasBase = OpenLibrary((STRPTR)MATHLIB_NAME, MATHLIB_REV)))
{
OpenLibraryError(MATHLIB_NAME, MATHLIB_REV);
return LIB_ERROR;
}
#ifdef AOS3
// Library does not always provide IdString
OpenResourceSuccess(MATHLIB_NAME);
#else
OpenLibrarySuccess(((struct Library *)__MathIeeeDoubBasBase)->lib_IdString);
#endif
// Commodities Library
if (!(CxBase = OpenLibrary((STRPTR)COMMODLIB_NAME, COMMODLIB_REV)))
{
OpenLibraryError(COMMODLIB_NAME, COMMODLIB_REV);
return LIB_ERROR;
}
OpenLibrarySuccess(CxBase->lib_IdString);
// Intuition Library
if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary((STRPTR)INTUITIONLIB_NAME, INTUITIONLIB_REV)))
{
OpenLibraryError(INTUITIONLIB_NAME, INTUITIONLIB_REV);
return LIB_ERROR;
}
OpenLibrarySuccess(((struct Library *)IntuitionBase)->lib_IdString);
// Icon Library
if (!(IconBase = OpenLibrary((STRPTR)ICONLIB_NAME, ICONLIB_REV)))
{
OpenLibraryError(ICONLIB_NAME, ICONLIB_REV);
return LIB_ERROR;
}
OpenLibrarySuccess(IconBase->lib_IdString);
// Icon Library
if (!(AslBase = OpenLibrary((STRPTR)ASLLIB_NAME, ASLLIB_REV)))
{
OpenLibraryError(ASLLIB_NAME, ASLLIB_REV);
return LIB_ERROR;
}
OpenLibrarySuccess(AslBase->lib_IdString);
// Locale Library
if (!(LocaleBase = (struct LocaleBase *)OpenLibrary((STRPTR)LOCALELIB_NAME, LOCALELIB_REV)))
{
OpenLibraryError(LOCALELIB_NAME, LOCALELIB_REV);
return LIB_ERROR;
}
OpenLibrarySuccess(((struct Library *)LocaleBase)->lib_IdString);
// Utility Library
if (!(UtilityBase = (struct UtilityBase *)OpenLibrary((STRPTR)UTILLIB_NAME, UTILLIB_REV)))
{
OpenLibraryError(UTILLIB_NAME, UTILLIB_REV);
return LIB_ERROR;
}
__UtilityBase = UtilityBase;
OpenLibrarySuccess(((struct Library *)UtilityBase)->lib_IdString);
// Gadget Library
if (!(GadToolsBase = OpenLibrary((STRPTR)GADGETLIB_NAME, GADGETLIB_REV)))
{
OpenLibraryError(GADGETLIB_NAME, GADGETLIB_REV);
return LIB_ERROR;
}
OpenLibrarySuccess(GadToolsBase->lib_IdString);
// Locale
if (!(Globals->Locale = OpenLocale(NULL)))
{
OpenResourceError(currentLocale);
return LIB_ERROR;
}
OpenResourceSuccess((const char *)Globals->Locale->loc_LocaleName);
// RTC Clock
if (!(BattClockBase = OpenResource((STRPTR)BATTCLOCK_NAME)))
{
OpenResourceError((const char *)BATTCLOCK_NAME);
return LIB_ERROR;
}
OpenResourceSuccess((const char *)BATTCLOCK_NAME);
// Timer Device
if (!(TimerDevice = OpenTimerBase()))
{
OpenResourceError(TIMERNAME);
return LIB_ERROR;
}
OpenResourceSuccess(TIMERNAME);
InitUtcOffset();
return LIB_OK;
}
void CloseLibraries(void)
{
if (TimerDevice != NULL)
{
ClosingResource((const char *)TIMERNAME);
CloseTimerBase();
TimerDevice = NULL;
}
if (Globals->Locale != NULL)
{
ClosingResource((const char *)Globals->Locale->loc_LocaleName);
CloseLocale(Globals->Locale);
Globals->Locale = NULL;
}
if (CxBase != NULL)
{
ClosingLibrary(CxBase->lib_IdString);
CloseLibrary(CxBase);
CxBase = NULL;
}
if (AslBase != NULL)
{
ClosingLibrary(AslBase->lib_IdString);
CloseLibrary(AslBase);
AslBase = NULL;
}
if (IconBase != NULL)
{
ClosingLibrary(IconBase->lib_IdString);
CloseLibrary(IconBase);
IconBase = NULL;
}
if (LocaleBase != NULL)
{
ClosingLibrary(((struct Library *)LocaleBase)->lib_IdString);
CloseLibrary((struct Library *)LocaleBase);
LocaleBase = NULL;
}
if (UtilityBase != NULL)
{
ClosingLibrary(((struct Library *)UtilityBase)->lib_IdString);
CloseLibrary((struct Library *)UtilityBase);
UtilityBase = NULL;
__UtilityBase = NULL;
}
if (GadToolsBase != NULL)
{
ClosingLibrary(GadToolsBase->lib_IdString);
CloseLibrary(GadToolsBase);
GadToolsBase = NULL;
}
if (IntuitionBase != NULL)
{
ClosingLibrary(((struct Library *)IntuitionBase)->lib_IdString);
CloseLibrary((struct Library *)IntuitionBase);
IntuitionBase = NULL;
}
if (DOSBase != NULL)
{
#ifdef AOS3
// Library does not always provide IdString
ClosingResource(DOSLIB_NAME);
#else
ClosingLibrary(((struct Library *)DOSBase)->lib_IdString);
#endif
CloseLibrary((struct Library *)DOSBase);
DOSBase = NULL;
}
if (__MathIeeeDoubBasBase != NULL)
{
#ifdef AOS3
// Library does not always provide IdString
ClosingResource(MATHLIB_NAME);
#else
ClosingLibrary(((struct Library *)__MathIeeeDoubBasBase)->lib_IdString);
#endif
CloseLibrary((struct Library *)__MathIeeeDoubBasBase);
__MathIeeeDoubBasBase = NULL;
}
}