AmiTimeKeeper/library.c

319 lines
8.5 KiB
C
Raw Normal View History

2018-11-24 21:39:18 +00:00
/*-
2021-01-12 22:16:18 +00:00
* Copyright (c) 2017-2021 Carsten Sonne Larsen <cs@innolan.net>
2018-11-24 21:39:18 +00:00
* 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.
2021-01-12 22:00:49 +00:00
*
2018-11-24 21:39:18 +00:00
*/
#include "config.h"
2021-01-12 22:02:29 +00:00
#include "global.h"
#include "timer.h"
2021-01-31 19:10:59 +00:00
#include "text.h"
2021-01-12 22:00:49 +00:00
#include "ptz.h"
2018-11-24 21:39:18 +00:00
#include "mem.h"
2021-01-12 22:02:29 +00:00
#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>
2021-01-12 22:16:18 +00:00
#include <rexx/rxslib.h>
2021-01-12 22:02:29 +00:00
2021-01-12 22:16:18 +00:00
#if defined(SCREENNOTIFY)
2021-01-12 22:02:29 +00:00
#include "libraries/screennotify.h"
2021-01-12 22:16:18 +00:00
#endif
2021-01-12 22:02:29 +00:00
#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
2021-01-12 22:16:18 +00:00
#ifndef RXSNAME
#define RXSNAME "rexxsyslib.library"
#endif
2021-01-12 22:02:29 +00:00
#ifndef BATTCLOCKNAME
#define BATTCLOCKNAME "battclock.resource"
#endif
#ifndef TIMERNAME
#define TIMERNAME "timer.device"
#endif
#ifndef LIBREV
2021-01-12 22:16:18 +00:00
#define LIBREV 34L
2018-11-24 21:39:18 +00:00
#endif
2021-01-12 22:16:18 +00:00
#ifndef NOAREXX
#define NOAREXX "No ARexx library found"
#endif
2021-01-12 22:02:29 +00:00
#ifndef NORTCCLOCK
#define NORTCCLOCK "No RTC found"
#endif
struct DosLibrary *DOSBase2 = NULL;
2018-11-24 21:39:18 +00:00
struct UtilityBase *UtilityBase = NULL;
struct Library *CxBase = NULL;
2021-01-12 22:02:29 +00:00
struct IntuitionBase *IntuitionBase = NULL;
2018-11-24 21:39:18 +00:00
struct Library *GadToolsBase = NULL;
2021-01-12 22:02:29 +00:00
struct Library *IconBase = NULL;
struct GfxBase *GfxBase = NULL;
struct LocaleBase *LocaleBase = NULL;
2021-01-12 22:16:18 +00:00
struct RxsLib *RexxSysBase = NULL;
2021-01-12 22:02:29 +00:00
struct Library *ScreenNotifyBase = NULL;
2018-11-24 21:39:18 +00:00
struct Library *BattClockBase = NULL;
2021-01-12 22:02:29 +00:00
struct Device *TimerDevice = NULL;
2018-11-24 21:39:18 +00:00
2021-01-12 22:02:29 +00:00
static void OpenLibrarySuccess(char *name, char *ident)
2018-11-24 21:39:18 +00:00
{
2021-01-31 19:10:59 +00:00
LogDebug(Text2P, TextOpened, (ident == NULL ? name : ident));
2018-11-24 21:39:18 +00:00
}
2021-01-12 22:02:29 +00:00
static void ClosingLibrary(char *name, char *ident)
2018-11-24 21:39:18 +00:00
{
2021-01-31 19:10:59 +00:00
LogDebug(Text2P, TextClosing, (ident == NULL ? name : ident));
2018-11-24 21:39:18 +00:00
}
2021-01-31 19:10:59 +00:00
static void OpenLibraryError(char *name, long version)
2018-11-24 21:39:18 +00:00
{
2021-01-31 19:10:59 +00:00
char message[128];
SNPrintf(message, 127, "%s %s %ld.0", TextNoOpen, name, version);
Printf((STRPTR)message);
LogError(message);
2018-11-24 21:39:18 +00:00
}
int OpenLibraries(void)
{
2021-01-31 19:10:59 +00:00
LogInfo(Text2P, TextStarting, "up");
2021-01-12 21:53:12 +00:00
2018-11-24 21:39:18 +00:00
// DOS Library
2021-01-12 22:02:29 +00:00
if (!(DOSBase2 = (struct DosLibrary *)OpenLibrary((STRPTR)DOSNAME, LIBREV)))
2018-11-24 21:39:18 +00:00
{
2021-01-12 22:02:29 +00:00
OpenLibraryError(DOSNAME, LIBREV);
2018-11-24 21:39:18 +00:00
return LIB_ERROR;
}
2021-01-12 22:02:29 +00:00
OpenLibrarySuccess(DOSNAME, DOSBase2->dl_lib.lib_IdString);
2018-11-24 21:39:18 +00:00
2021-01-12 22:02:29 +00:00
// Utility Library
if (!(UtilityBase = (struct UtilityBase *)OpenLibrary((STRPTR)UTILITYNAME, LIBREV)))
2018-11-24 21:39:18 +00:00
{
2021-01-12 22:02:29 +00:00
OpenLibraryError(UTILITYNAME, LIBREV);
2018-11-24 21:39:18 +00:00
return LIB_ERROR;
}
2021-01-12 22:02:29 +00:00
OpenLibrarySuccess(UTILITYNAME, UtilityBase->ub_LibNode.lib_IdString);
2018-11-24 21:39:18 +00:00
2021-01-12 22:02:29 +00:00
// Commodity Library
if (!(CxBase = OpenLibrary((STRPTR)COMMODITIESNAME, LIBREV)))
2018-11-24 21:39:18 +00:00
{
2021-01-12 22:02:29 +00:00
OpenLibraryError(COMMODITIESNAME, LIBREV);
2018-11-24 21:39:18 +00:00
return LIB_ERROR;
}
2021-01-12 22:02:29 +00:00
OpenLibrarySuccess(COMMODITIESNAME, CxBase->lib_IdString);
2018-11-24 21:39:18 +00:00
// Intuition Library
2021-01-12 22:02:29 +00:00
if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary((STRPTR)INTUITIONNAME, LIBREV)))
2018-11-24 21:39:18 +00:00
{
2021-01-12 22:02:29 +00:00
OpenLibraryError(INTUITIONNAME, LIBREV);
2018-11-24 21:39:18 +00:00
return LIB_ERROR;
}
2021-01-12 22:02:29 +00:00
OpenLibrarySuccess(INTUITIONNAME, IntuitionBase->LibNode.lib_IdString);
2018-11-24 21:39:18 +00:00
2021-01-12 22:02:29 +00:00
// Gadget Library
if (!(GadToolsBase = OpenLibrary((STRPTR)GADTOOLSNAME, LIBREV)))
2018-11-24 21:39:18 +00:00
{
2021-01-12 22:02:29 +00:00
OpenLibraryError(GADTOOLSNAME, LIBREV);
2018-11-24 21:39:18 +00:00
return LIB_ERROR;
}
2021-01-12 22:02:29 +00:00
OpenLibrarySuccess(GADTOOLSNAME, GadToolsBase->lib_IdString);
2018-11-24 21:39:18 +00:00
// Icon Library
2021-01-12 22:02:29 +00:00
if (!(IconBase = OpenLibrary((STRPTR)ICONNAME, LIBREV)))
2018-11-24 21:39:18 +00:00
{
2021-01-12 22:02:29 +00:00
OpenLibraryError(ICONNAME, LIBREV);
2018-11-24 21:39:18 +00:00
return LIB_ERROR;
}
2021-01-12 22:02:29 +00:00
OpenLibrarySuccess(ICONNAME, IconBase->lib_IdString);
2018-11-24 21:39:18 +00:00
2021-01-12 22:02:29 +00:00
// Graphics Library
if (!(GfxBase = (struct GfxBase *)OpenLibrary((STRPTR)GRAPHICSNAME, LIBREV)))
2018-11-24 21:39:18 +00:00
{
2021-01-12 22:02:29 +00:00
OpenLibraryError(GRAPHICSNAME, LIBREV);
2018-11-24 21:39:18 +00:00
return LIB_ERROR;
}
2021-01-12 22:02:29 +00:00
OpenLibrarySuccess(GRAPHICSNAME, GfxBase->LibNode.lib_IdString);
2018-11-24 21:39:18 +00:00
2021-01-12 22:02:29 +00:00
// Locale Library
if (!(LocaleBase = (struct LocaleBase *)OpenLibrary((STRPTR)LOCALENAME, LIBREV)))
2018-11-24 21:39:18 +00:00
{
2021-01-12 22:02:29 +00:00
OpenLibraryError(LOCALENAME, LIBREV);
2018-11-24 21:39:18 +00:00
return LIB_ERROR;
}
2021-01-12 22:02:29 +00:00
OpenLibrarySuccess(LOCALENAME, LocaleBase->lb_LibNode.lib_IdString);
2018-11-24 21:39:18 +00:00
2021-01-12 22:16:18 +00:00
// ARexx Library
RexxSysBase = (struct RxsLib *)OpenLibrary((STRPTR)RXSNAME, 0);
if (RexxSysBase != NULL)
{
OpenLibrarySuccess(RXSNAME, RexxSysBase->rl_Node.lib_IdString);
}
else
{
LogWarn(NOAREXX);
}
2021-01-12 22:02:29 +00:00
// Timer Device
if (!(TimerDevice = OpenTimerBase()))
2021-01-12 21:50:54 +00:00
{
2021-01-31 19:10:59 +00:00
LogError(Text2P, TextNoOpen, TIMERNAME);
2021-01-12 21:50:54 +00:00
return LIB_ERROR;
}
2021-01-31 19:10:59 +00:00
LogDebug(Text2P, TextOpened, TIMERNAME);
2021-01-12 21:50:54 +00:00
2021-01-12 22:02:29 +00:00
// RTC Clock
BattClockBase = OpenResource((STRPTR)BATTCLOCKNAME);
if (BattClockBase != NULL)
2018-11-24 21:39:18 +00:00
{
2021-01-31 19:10:59 +00:00
LogDebug(Text2P, TextOpened, BATTCLOCKNAME);
2018-11-24 21:39:18 +00:00
}
2021-01-12 22:02:29 +00:00
else
2018-11-24 21:39:18 +00:00
{
2021-01-12 22:16:18 +00:00
LogNotice(NORTCCLOCK);
2018-11-24 21:39:18 +00:00
}
2021-01-12 22:16:18 +00:00
#if defined(SCREENNOTIFY)
2021-01-12 22:02:29 +00:00
ScreenNotifyBase = OpenLibrary((STRPTR)SCREENNOTIFY_NAME, SCREENNOTIFY_VERSION);
if (ScreenNotifyBase != NULL)
2018-11-24 21:39:18 +00:00
{
2021-01-31 19:10:59 +00:00
LogNotice(Text2P, TextFound, SCREENNOTIFY_NAME);
2018-11-24 21:39:18 +00:00
}
2021-01-12 22:16:18 +00:00
#endif
2018-11-24 21:39:18 +00:00
2021-01-12 21:55:34 +00:00
SeedRandom();
2018-11-24 21:39:18 +00:00
return LIB_OK;
}
void CloseLibraries(void)
{
if (TimerDevice != NULL)
{
2021-01-31 19:10:59 +00:00
LogDebug(Text2P, TextClosing, TIMERNAME);
2018-11-24 21:39:18 +00:00
CloseTimerBase();
TimerDevice = NULL;
}
2021-01-12 22:16:18 +00:00
#if defined(SCREENNOTIFY)
2021-01-12 22:02:29 +00:00
if (ScreenNotifyBase != NULL)
2018-11-24 21:39:18 +00:00
{
2021-01-12 22:02:29 +00:00
ClosingLibrary(SCREENNOTIFY_NAME, ScreenNotifyBase->lib_IdString);
CloseLibrary(ScreenNotifyBase);
ScreenNotifyBase = NULL;
}
2021-01-12 22:16:18 +00:00
#endif
if (RexxSysBase != NULL)
{
ClosingLibrary(RXSNAME, RexxSysBase->rl_Node.lib_IdString);
CloseLibrary((struct Library *)RexxSysBase);
RexxSysBase = NULL;
}
2021-01-12 22:02:29 +00:00
if (LocaleBase != NULL)
{
ClosingLibrary(LOCALENAME, LocaleBase->lb_LibNode.lib_IdString);
CloseLibrary((struct Library *)LocaleBase);
LocaleBase = NULL;
2018-11-24 21:39:18 +00:00
}
2021-01-12 22:02:29 +00:00
if (GfxBase != NULL)
2018-11-24 21:39:18 +00:00
{
2021-01-12 22:02:29 +00:00
ClosingLibrary(GRAPHICSNAME, GfxBase->LibNode.lib_IdString);
CloseLibrary((struct Library *)GfxBase);
GfxBase = NULL;
2018-11-24 21:39:18 +00:00
}
if (IconBase != NULL)
{
2021-01-12 22:02:29 +00:00
ClosingLibrary(ICONNAME, IconBase->lib_IdString);
2018-11-24 21:39:18 +00:00
CloseLibrary(IconBase);
IconBase = NULL;
}
2021-01-12 22:02:29 +00:00
if (GadToolsBase != NULL)
2018-11-24 21:39:18 +00:00
{
2021-01-12 22:02:29 +00:00
ClosingLibrary(GADTOOLSNAME, GadToolsBase->lib_IdString);
CloseLibrary(GadToolsBase);
GadToolsBase = NULL;
2018-11-24 21:39:18 +00:00
}
2021-01-12 22:02:29 +00:00
if (IntuitionBase != NULL)
2018-11-24 21:39:18 +00:00
{
2021-01-12 22:02:29 +00:00
ClosingLibrary(INTUITIONNAME, IntuitionBase->LibNode.lib_IdString);
CloseLibrary((struct Library *)IntuitionBase);
IntuitionBase = NULL;
2018-11-24 21:39:18 +00:00
}
2021-01-12 22:02:29 +00:00
if (CxBase != NULL)
2018-11-24 21:39:18 +00:00
{
2021-01-12 22:02:29 +00:00
ClosingLibrary(COMMODITIESNAME, CxBase->lib_IdString);
CloseLibrary(CxBase);
CxBase = NULL;
2018-11-24 21:39:18 +00:00
}
2021-01-12 22:02:29 +00:00
if (UtilityBase != NULL)
2021-01-12 21:50:54 +00:00
{
2021-01-12 22:02:29 +00:00
ClosingLibrary(UTILITYNAME, UtilityBase->ub_LibNode.lib_IdString);
CloseLibrary((struct Library *)UtilityBase);
UtilityBase = NULL;
2021-01-12 21:50:54 +00:00
}
2021-01-12 22:02:29 +00:00
if (DOSBase2 != NULL)
2018-11-24 21:39:18 +00:00
{
2021-01-12 22:02:29 +00:00
ClosingLibrary(DOSNAME, DOSBase2->dl_lib.lib_IdString);
CloseLibrary((struct Library *)DOSBase2);
DOSBase2 = NULL;
2018-11-24 21:39:18 +00:00
}
2021-01-12 22:02:29 +00:00
}