;	INCDIR	"INCLUDE:"
	INCLUDE	"exec/types.i"
	INCLUDE	"exec/io.i"
	INCLUDE	"devices/inputevent.i"
	INCLUDE "LVO3.0/exec_lib.i"
	INCLUDE "LVO3.0/dos_lib.i"

	section text,code

;	This file contains source for the handler signalling a disk has been
;	inserted or removed, plus the code to patch Delete and Close.

	xref	_diskSig
	xref	_thisTask
	xref	_DOSBase
	xref	_IntuitionBase

	xdef	_checkDisk
	xdef	_patchDOSlibrary
	xdef	_resetDOSlibrary


_checkDisk:
	move.l	a0,-(sp)
	moveq	#0,d0
.checkloop
	cmp.b	#IECLASS_DISKREMOVED,ie_Class(a0)
	bne.s	.try_inserted
	moveq	#1,d0
	bra	.end
.try_inserted
	cmp.b	#IECLASS_DISKINSERTED,ie_Class(a0)
	bne.s	.try_nextevent
	moveq	#1,d0
	bra .end
.try_nextevent
	move.l	(a0),d0
	move.l	d0,a0
	bne.s	.checkloop
.end
	tst	d0
	beq	.nosig
	move.l	$4,a6
	move.l	_thisTask,a1
	move.l	_diskSig,d0
	jsr	_LVOSignal(a6)
.nosig
	move.l	(sp)+,d0
	rts

;	calling this function from here will patch Delete and Close
;	with the new values; while "startpatch" label is a jump from
;	_resetDOSlibrary in case the patches were re-patched - so they're
;	set again with the re-patched value.

_patchDOSlibrary:
	move.l	$4,a6
	move.l	#_patchDelete,oldDelete
	move.l	#_patchClose,oldClose
	jsr	_LVOForbid(a6)
startpatch
	move.l	_DOSBase,a1
	move.l	oldDelete,d0
	move.w	#_LVODeleteFile,a0
	jsr	_LVOSetFunction(a6)
	move.l	d0,oldDelete
	move.l	_DOSBase,a1
	move.l	oldClose,d0
	move.w	#_LVOClose,a0
	jsr	_LVOSetFunction(a6)
	move.l	d0,oldClose
	jsr	_LVOPermit(a6)
	move.w	#0,d0			; this *IS* important (return FALSE
	rts				;	from resetDOSlibrary)

_resetDOSlibrary:
	move.l	$4,a6
	jsr	_LVOForbid(a6)
	move.l	_DOSBase,a1
	move.l	oldDelete,d0
	move.w	#_LVODeleteFile,a0
	jsr	_LVOSetFunction(a6)
	move.l	d0,patchReturn1		; a temporary value
	move.l	_DOSBase,a1
	move.l	oldClose,d0
	move.w	#_LVOClose,a0
	jsr	_LVOSetFunction(a6)
	cmp.l	#_patchClose,d0
	bne	.resetError
	move.l	patchReturn1,d0
	cmp.l	#_patchDelete,d0
	bne	.resetError
	move.w	#1,d0
	jsr	_LVOPermit(a6)
	rts
.resetError:
	move.l	d0,oldClose
	move.l	patchReturn1,oldDelete
	bra	startpatch

_patchClose:
	move.l	a2,-(sp)
	move.l	oldClose,a2
	bra.s	cont

_patchDelete:
	move.l	a2,-(sp)
	move.l	oldDelete,a2
cont:
	jsr	(a2)
	movem.l	d0/a6,-(sp)
	move.l	$4,a6
	move.l	_thisTask,a1
	move.l	_diskSig,d0
	jsr	_LVOSignal(a6)
	movem.l	(sp)+,d0/a6
	move.l	(sp)+,a2
	rts

oldClose:
	dc.l	0
oldDelete:
	dc.l	0
patchReturn1
	dc.l	0
	end
