LibPriority = 0
Version = 1
Revision = 1

OpenLibrary = -552
CloseLibrary = -414
Remove = -252
FreeMem = -210
DisplayBeep = -96
Delay = -198

	SECTION LibraryCode,CODE
	Include "Exec/Libraries.i"
	Include "Exec/Initializers.i"
	Include "Exec/Resident.i"

	STRUCTURE AduBase,LIB_SIZE
	UBYTE adu_Flags
	UBYTE adu_Pad
	ULONG adu_SegList
	ULONG adu_IntuitionBase
	ULONG adu_DOSBase
	LABEL AduBase_SIZEOF

	XDEF Init
	XDEF Open
	XDEF Close
	XDEF Expunge
	XDEF Null
	XDEF FuncOne
	XDEF LibName

ExitCleanly:
	moveq #0,d0
	rts

InitDDescrip:
	dc.w RTC_MATCHWORD
	dc.l InitDDescrip,EndCode
	dc.b RTF_AUTOINIT,Version,NT_LIBRARY,LibPriority
	dc.l LibName,IDString,Init
LibName: dc.b 'adu.library',0
VersionString: dc.b '$VER: '
IDString: dc.b 'adu.library 1.1 (27.02.94)',13,10,0
	EVEN
Init: dc.l AduBase_SIZEOF,FunctionTable,DataTable,InitRoutine
FunctionTable:
	dc.l Open,Close,Expunge,Null
	dc.l ADU_LibFuncOne
	dc.l -1
DataTable:
	INITBYTE LN_TYPE,NT_LIBRARY
	INITLONG LN_NAME,LibName
	INITBYTE LIB_FLAGS,LIBF_SUMUSED!LIBF_CHANGED
	INITWORD LIB_VERSION,Version
	INITWORD LIB_REVISION,Revision
	INITLONG LIB_IDSTRING,IDString
	dc.l 0

InitRoutine:
* d0 contains a pointer to our library
* a0 contains its SegList
* a6 is SYSBASE
* If we return 0 in d0, it means this function has failed for some reason.
	move.l a5,-(sp)
	move.l d0,a5
	move.l a0,adu_SegList(a5)
	lea INTUITIONNAME,a1
	move.l #0,d0
	CALLLIB OpenLibrary
	move.l d0,adu_IntuitionBase(a5)
	tst.l d0
	beq .NoIntuition
	lea DOSNAME,a1
	move.l #0,d0
	CALLLIB OpenLibrary
	move.l d0,adu_DOSBase(a5)
	tst.l d0
	beq .NoDOS
	move.l a5,d0
	bra .Exit
.NoDOS:
	move.l adu_IntuitionBase(a5),a1
	CALLLIB CloseLibrary
	move.l #0,adu_IntuitionBase(a5)
.NoIntuition:
	move.l #0,d0
.Exit: move.l (sp)+,a5
	rts

Open:
* a6 contains our library's address, which we must return in d0
* REMEMBER : The OS has turned off multitasking, so be quick in here
	addq.w #1,LIB_OPENCNT(a6)
	bclr #LIBB_DELEXP,adu_Flags(a6)
	move.l a6,d0
	rts

Close:
* a6 contains our library's address.
* This routine should return NULL unless the library is now fully
* closed, and there is an expunge request pending, in which case
* the system expects the SegList which we received in Init.
* REMEMBER : The OS has turned off multitasking, so be quick in here:
	moveq.l #0,d0
	subq.w #1,LIB_OPENCNT(a6)
	bne.s .ExitWithoutExpunge
	btst #LIBB_DELEXP,adu_Flags(a6)
	beq .ExitWithoutExpunge
	bsr Expunge
.ExitWithoutExpunge:
	rts

Expunge:
* a6 contains our library's address.
* If the library is no longer open, Expunge should return the SegList.
* Otherwise, the delayed expunge flag should be set, and NULL returned.
* REMEMBER : Never use Wait() or take a long time in this routine.
* You may cause a deadlock or other unwelcome behaviour.
	movem.l d2/a5/a6,-(sp)
	move.l a6,a5
	tst.w LIB_OPENCNT(a5)
	beq .FreeLibMemory
	bset #LIBB_DELEXP,adu_Flags(a5)
	moveq.l #0,d0
	bra .End
.FreeLibMemory:
	move.l 4.w,a6
	move.l adu_IntuitionBase(a5),a1
	move.l a1,d0
	beq .NoIntuition
	CALLLIB CloseLibrary
.NoIntuition:
	move.l adu_DOSBase(a5),a1
	move.l a1,d0
	beq .NoDOS
	CALLLIB CloseLibrary
.NoDOS:
	move.l adu_SegList(a5),d2
	move.l a5,a1
	CALLLIB Remove
	moveq.l #0,d0
	move.l a5,a1
	move.w LIB_NEGSIZE(a5),d0
	sub.l d0,a1
	add.w LIB_POSSIZE(a5),d0
	CALLLIB FreeMem
	move.l d2,d0
.End: movem.l (sp)+,d2/a5/a6
	rts

Null: moveq.l #0,d0
	rts

ADU_LibFuncOne:
* Takes a long int in d0, and flashes the screen that many times.
* Since this function has been called in our library, we can assume
* that a6 points to our library. This is true of any function in
* our library.
	movem.l d2/a5-a6,-(sp)
	move.l a6,a5
	move.l d0,d2
.BeepLoop:
	subq.l #1,d2
	blt.s .FinishedBeeping
	suba.l a0,a0
	move.l adu_IntuitionBase(a5),a6
	CALLLIB DisplayBeep
	move.l #25,d1
	move.l adu_DOSBase(a5),a6
	CALLLIB Delay
	bra.s .BeepLoop
.FinishedBeeping
	movem.l (sp)+,d2/a5-a6
	rts

INTUITIONNAME: dc.b 'intuition.library',0
DOSNAME: dc.b 'dos.library',0
	EVEN

	SECTION EndOfLibrary,CODE
EndCode: END
