; Support routines for nocare V1.2.1


REFRESHBIT0	equ	$06
REFRESHBIT1	equ	$07

WBENCHSCREEN	EQU	$0001

nw_Flags	EQU	$E	
nw_Type	EQU	$2E

	xref 	_LVOOpenLibrary	
	xref 	_LVOCloseLibrary	 
	xref 	_LVOOpenWindow
	xref 	_LVOSetFunction
	xref 	_LVOAllocMem
	xref	_LVOEnable
	xref	_LVODisable
	xref	_LVOFreeMem

PUSH	macro
	movem.l	a0-a6/d0-d6,-(sp)
	endm

PULL	macro
	movem.l	(sp)+,a0-a6/d0-d6
	endm

	xdef	_do_patch
	xdef	_undo_patch

	data
intuitionname	dc.b	'intuition.library',0
	even
intuitionbase	dc.l	0

	code
openint:
	lea.l	intuitionname,a1
	clr.l	d0
	move.l	$4,a6
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,intuitionbase
	rts

closeint
	move.l	$4,a6
	move.l	intuitionbase,a1
	jsr	_LVOCloseLibrary(a6)
	rts

_do_patch:
	PUSH
	bsr.s	openint
	move.l	#(codeend-newopenwindow),d0
	move.l	#0,d1
	jsr	_LVOAllocMem(a6)

	move.l	d0,base
	lea.l	newopenwindow,a0
	move.l	#(codeend-newopenwindow-1),d1
	move.l	d0,a1
1$	move.b	(a0)+,(a1)+
	dbf	d1,1$	

; Then patch the OpenWindow() function:

	jsr	_LVODisable(a6)
	move.l	intuitionbase,a1
	move.w	#_LVOOpenWindow,a0
	move.l	base,d0
	jsr	_LVOSetFunction(a6)
	move.l	base,a2
	move.l	d0,offset(a2)
	jsr	_LVOEnable(a6)
	bsr.s	closeint
	PULL
	rts


_undo_patch:
	PUSH
	bsr	openint
	move.l	d0,a1
	move.l	_LVOOpenWindow+2(a1),a2
	move.l	a2,base
	cmp.l	#'HAHA',matchoffset(a2)
	bne.s	out
	move.w	#_LVOOpenWindow,a0
	move.l	offset(a2),d0
	jsr	_LVOSetFunction(a6)
	move.l	base,a1
	move.l	#(codeend-newopenwindow),d0	
	jsr	_LVOFreeMem(a6)

out	bsr	 closeint
	PULL
	rts
	

;-----------------------------------------------------------------------------
; As OpenWindow() is called, then a0 points to a NewWindow structure.
; We patch some bits in the nw_Flags field and than jump to the
; original OpenWindow() code.
;
newopenwindow
	cmp.w	#WBENCHSCREEN,nw_Type(a0)
	bne.s	dontchange
	move.l	nw_Flags(a0),d0
	bclr	#REFRESHBIT0,d0		; We don't want SIMPLE_REFRESH ones.
	move.l	d0,nw_Flags(a0)
dontchange
	move.l	oldopenwindow(PC),a1
	jmp	(a1)			; Jump to original code.



	even
oldopenwindow	dc.l	0
base		dc.l	0
match		dc.l	'HAHA'
codeend

offset equ (oldopenwindow-newopenwindow)
matchoffset equ (match-newopenwindow)

	end
