AmiTimeKeeper/library.c

356 lines
9.0 KiB
C

/*-
* Copyright (c) 2017-2020 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 "global.h"
#include "timer.h"
#include "ptz.h"
#include "mem.h"
#include <graphics/gfxbase.h>
#include <libraries/dos.h>
#include <libraries/commodities.h>
#include <intuition/intuitionbase.h>
#include <graphics/gfxbase.h>
#include <utility/utility.h>
#include <workbench/icon.h>
#include <resources/battclock.h>
#include "libraries/screennotify.h"
#include "logmod.h"
#define MODULENAME "Library"
/* Library and resource identifiers */
#ifndef DOSNAME
#define DOSNAME "dos.library"
#endif
#ifndef UTILITYNAME
#define UTILITYNAME "utility.library"
#endif
#ifndef COMMODITIESNAME
#define COMMODITIESNAME "commodities.library"
#endif
#ifndef INTUITIONNAME
#define INTUITIONNAME "intuition.library"
#endif
#ifndef GADTOOLSNAME
#define GADTOOLSNAME "gadtools.library"
#endif
#ifndef ICONNAME
#define ICONNAME "icon.library"
#endif
#ifndef GRAPHICSNAME
#define GRAPHICSNAME "graphics.library"
#endif
#ifndef LOCALENAME
#define LOCALENAME "locale.library"
#endif
#ifndef BATTCLOCKNAME
#define BATTCLOCKNAME "battclock.resource"
#endif
#ifndef TIMERNAME
#define TIMERNAME "timer.device"
#endif
#ifndef LIBREV
#define LIBREV 38L
#endif
#ifndef CURRENTLOCALE
#define CURRENTLOCALE "Current Locale Info"
#endif
#ifndef NORTCCLOCK
#define NORTCCLOCK "No RTC found"
#endif
struct DosLibrary *DOSBase2 = NULL;
struct UtilityBase *UtilityBase = NULL;
struct Library *CxBase = NULL;
struct IntuitionBase *IntuitionBase = NULL;
struct Library *GadToolsBase = NULL;
struct Library *IconBase = NULL;
struct GfxBase *GfxBase = NULL;
struct LocaleBase *LocaleBase = NULL;
struct Library *ScreenNotifyBase = NULL;
struct Library *BattClockBase = NULL;
struct Device *TimerDevice = NULL;
static void OpenLibrarySuccess(char *name, char *ident)
{
if (ident == NULL)
{
LogDebug("Opened %s", name);
}
else
{
LogDebug("Opened %s", ident);
}
}
static void OpenLibraryError(char *name, long version)
{
char message[64];
SNPrintf(message, 63, "Cannot open %s %ld.0", name, version);
Printf((STRPTR)message);
LogError(message);
}
static void ClosingLibrary(char *name, char *ident)
{
if (ident == NULL)
{
LogDebug("Closing %s", name);
}
else
{
LogDebug("Closing %s", ident);
}
}
static void OpenResourceSuccess(char *name)
{
LogDebug("Opened %s", name);
}
static void OpenResourceError(char *name)
{
LogError("Cannot open %s", name);
}
static void ClosingResource(char *name)
{
LogDebug("Closing %s", name);
}
int OpenLibraries(void)
{
LogInfo("Starting up");
// DOS Library
if (!(DOSBase2 = (struct DosLibrary *)OpenLibrary((STRPTR)DOSNAME, LIBREV)))
{
OpenLibraryError(DOSNAME, LIBREV);
return LIB_ERROR;
}
OpenLibrarySuccess(DOSNAME, DOSBase2->dl_lib.lib_IdString);
// Utility Library
if (!(UtilityBase = (struct UtilityBase *)OpenLibrary((STRPTR)UTILITYNAME, LIBREV)))
{
OpenLibraryError(UTILITYNAME, LIBREV);
return LIB_ERROR;
}
OpenLibrarySuccess(UTILITYNAME, UtilityBase->ub_LibNode.lib_IdString);
// Commodity Library
if (!(CxBase = OpenLibrary((STRPTR)COMMODITIESNAME, LIBREV)))
{
OpenLibraryError(COMMODITIESNAME, LIBREV);
return LIB_ERROR;
}
OpenLibrarySuccess(COMMODITIESNAME, CxBase->lib_IdString);
// Intuition Library
if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary((STRPTR)INTUITIONNAME, LIBREV)))
{
OpenLibraryError(INTUITIONNAME, LIBREV);
return LIB_ERROR;
}
OpenLibrarySuccess(INTUITIONNAME, IntuitionBase->LibNode.lib_IdString);
// Gadget Library
if (!(GadToolsBase = OpenLibrary((STRPTR)GADTOOLSNAME, LIBREV)))
{
OpenLibraryError(GADTOOLSNAME, LIBREV);
return LIB_ERROR;
}
OpenLibrarySuccess(GADTOOLSNAME, GadToolsBase->lib_IdString);
// Icon Library
if (!(IconBase = OpenLibrary((STRPTR)ICONNAME, LIBREV)))
{
OpenLibraryError(ICONNAME, LIBREV);
return LIB_ERROR;
}
OpenLibrarySuccess(ICONNAME, IconBase->lib_IdString);
// Graphics Library
if (!(GfxBase = (struct GfxBase *)OpenLibrary((STRPTR)GRAPHICSNAME, LIBREV)))
{
OpenLibraryError(GRAPHICSNAME, LIBREV);
return LIB_ERROR;
}
OpenLibrarySuccess(GRAPHICSNAME, GfxBase->LibNode.lib_IdString);
// Locale Library
if (!(LocaleBase = (struct LocaleBase *)OpenLibrary((STRPTR)LOCALENAME, LIBREV)))
{
OpenLibraryError(LOCALENAME, LIBREV);
return LIB_ERROR;
}
OpenLibrarySuccess(LOCALENAME, LocaleBase->lb_LibNode.lib_IdString);
// Locale
if (!(AppLocale = OpenAppLocale(NULL)))
{
OpenResourceError(CURRENTLOCALE);
return LIB_ERROR;
}
OpenResourceSuccess((char *)AppLocale->AmigaLocale->loc_LocaleName);
// Timer Device
if (!(TimerDevice = OpenTimerBase()))
{
OpenResourceError(TIMERNAME);
return LIB_ERROR;
}
OpenResourceSuccess(TIMERNAME);
// RTC Clock
BattClockBase = OpenResource((STRPTR)BATTCLOCKNAME);
if (BattClockBase != NULL)
{
OpenResourceSuccess(BATTCLOCKNAME);
}
else
{
LogDebug(NORTCCLOCK);
}
ScreenNotifyBase = OpenLibrary((STRPTR)SCREENNOTIFY_NAME, SCREENNOTIFY_VERSION);
if (ScreenNotifyBase != NULL)
{
LogDebug("Found %s", SCREENNOTIFY_NAME);
}
SeedRandom();
return LIB_OK;
}
void CloseLibraries(void)
{
if (TimerDevice != NULL)
{
ClosingResource(TIMERNAME);
CloseTimerBase();
TimerDevice = NULL;
}
if (AppLocale != NULL)
{
ClosingResource((char *)AppLocale->AmigaLocale->loc_LocaleName);
CloseAppLocale(AppLocale);
AppLocale = NULL;
}
if (ScreenNotifyBase != NULL)
{
ClosingLibrary(SCREENNOTIFY_NAME, ScreenNotifyBase->lib_IdString);
CloseLibrary(ScreenNotifyBase);
ScreenNotifyBase = NULL;
}
if (LocaleBase != NULL)
{
ClosingLibrary(LOCALENAME, LocaleBase->lb_LibNode.lib_IdString);
CloseLibrary((struct Library *)LocaleBase);
LocaleBase = NULL;
}
if (GfxBase != NULL)
{
ClosingLibrary(GRAPHICSNAME, GfxBase->LibNode.lib_IdString);
CloseLibrary((struct Library *)GfxBase);
GfxBase = NULL;
}
if (IconBase != NULL)
{
ClosingLibrary(ICONNAME, IconBase->lib_IdString);
CloseLibrary(IconBase);
IconBase = NULL;
}
if (GadToolsBase != NULL)
{
ClosingLibrary(GADTOOLSNAME, GadToolsBase->lib_IdString);
CloseLibrary(GadToolsBase);
GadToolsBase = NULL;
}
if (IntuitionBase != NULL)
{
ClosingLibrary(INTUITIONNAME, IntuitionBase->LibNode.lib_IdString);
CloseLibrary((struct Library *)IntuitionBase);
IntuitionBase = NULL;
}
if (CxBase != NULL)
{
ClosingLibrary(COMMODITIESNAME, CxBase->lib_IdString);
CloseLibrary(CxBase);
CxBase = NULL;
}
if (UtilityBase != NULL)
{
ClosingLibrary(UTILITYNAME, UtilityBase->ub_LibNode.lib_IdString);
CloseLibrary((struct Library *)UtilityBase);
UtilityBase = NULL;
}
if (DOSBase2 != NULL)
{
ClosingLibrary(DOSNAME, DOSBase2->dl_lib.lib_IdString);
CloseLibrary((struct Library *)DOSBase2);
DOSBase2 = NULL;
}
LogWarn("Shutdown complete");
}
void ReopenLocale(void)
{
struct AppLocale *locale, *temp;
if (!(locale = OpenAppLocale(NULL)))
{
OpenResourceError(CURRENTLOCALE);
}
else
{
temp = AppLocale;
AppLocale = locale;
if (temp != NULL)
{
ClosingResource((char *)temp->AmigaLocale->loc_LocaleName);
CloseAppLocale(temp);
}
}
}