	opt o-,x-

	Incdir	"Include:" 
	Include "Misc/DevPacMacros.i"
	Include "Exec/Exec.i"
	Include "Intuition/Intuition.i"
	Include "Libraries/DOS.i"
	Include "Graphics/Text.i"
	Include "Graphics/GfxBase.i"
	Include	"Graphics/VideoControl.i"

******************************************************************************
*									     *
*    Written by Peter Kunath and Frank Riffel $Release 1.00/26.08.1991/$     *
*									     *
******************************************************************************
*									     *
*	THIS TOOL REQUIRES KICKSTART 2.0 AND A SUPER DENISE !!!		     *
*									     *
*   	BlackBorder patches the  OpenScreen() function under V2.0 and above  *
*   	to change the bordercolor of any opend Screen from 0 to black.       *
*   	Requires 2.x and a Superdenise.	You should run this Tool imediately  *
*	after IPrefs in your StartUp-Sequence.				     *
*									     *
* 	If you have an A3000 or a A2320 you probably noticed that the first  *
*	line on a de-interlaced screen flickers. This tool makes this ugly   *
*	effect non visible by turning the bordercolor to black.	             *
*									     *
*	DISCLAIMER							     *
*									     *
*	This program is FREELY DISTRIBUTABLE, but COPYRIGHTED. This means    *
*	that you can copy it freely as long as you don't ask for any more    *
*	money than a nominal fee for copying. This program may be put on PD  *
*       disks, especially on Fred Fish's AmigaLibDisks. If you want to       *
*       distribute this program you MUST keep the source code with it.       *
*       Program, document and source code must be distributed in their       *
*       original UNMODIFIED form. Of course you can use an archiver like     *
*       LHarc to make it available on mailboxes and FTP sites. This program  *
*	cannot be used for commercial purposes without written permission    *
*	from the authors. The authors can not be made responsible for any    *
*       damage which is caused by using this program.			     *
*									     *
******************************************************************************

Begin
	lea dosname(pc),a1			; Open DOS
	CALLEXEC OldOpenLibrary
	move.l	d0,_DOSBase
	beq	Exit

	CALLDOS Output				; Get Outputhandle
	move.l	d0,_Outhandle
	beq	CloseDOS			; We give up 

	move.l	d0,d1				; Write Info
	move.l	#Info,d2
	move.l	#InfoEnd-Info,d3
	CALLDOS Write

	lea gfxname(pc),a1			; Open Gfx	
	moveq	#36,d0
	CALLEXEC OpenLibrary
	move.l	d0,_GfxBase
	bne.s	Is_V36				; We are running under 2.x !

	move.l	d0,d1				; False Kickstart !
	move.l	#FalseKick,d2
	move.l	#FalseKickEnd-FalseKick,d3
	CALLDOS Write
	bra	CloseDOS

Is_V36	move.l 	d0,a0				; Is the Superhires denise here?
	btst	#GFXB_HR_DENISE,gb_ChipRevBits0(a0)
	bne.s	OpenInt				; yes !

	move.l	d0,d1				; No Superdenise !
	move.l	#NoSupDen,d2
	move.l	#NoSupDenEnd-NoSupDen,d3
	CALLDOS Write

	move.l	_GfxBase(pc),a1
	CALLEXEC CloseLibrary
	bra CloseDOS
	
OpenInt	lea intname(pc),a1			; Open Intuition
	CALLEXEC OldOpenLibrary
	move.l d0,_IntuitionBase
	beq	CloseDOS			; Should *NEVER* occour !!!

	move.l	#PatchEnd-Patchcode,d0		; Alloc Mem 
	move.l	#MEMF_PUBLIC!MEMF_CLEAR,d1
	CALLEXEC AllocMem
	move.l	d0,d2
	beq	CloseDOS

	CALLEXEC Forbid				; Install patch

	move.l	_IntuitionBase(pc),a1		; OpenWorkbench()
	move.w	#_LVOOpenWorkBench,a0		
	move.l	d2,d0
	CALLEXEC SetFunction
	move.l	d0,_OldFunc

	move.l	_IntuitionBase(pc),a1		; OpenScreen()
	move.w	#_LVOOpenScreen,a0
	move.l	d2,d0
	add.l	#Patchcode1-Patchcode,d0	; add offset
	CALLEXEC SetFunction
	move.l	d0,_OldFunc1

	move.l	_IntuitionBase(pc),a1		; OpenScreenTagList()
	move.w	#_LVOOpenScreenTagList,a0
	move.l	d2,d0
	add.l	#Patchcode2-Patchcode,d0	; add offset
	CALLEXEC SetFunction
	move.l	d0,_OldFunc2

	move.l	d2,a0				; Copy the Patchcode
	lea	Patchcode(pc),a1
	move.w	#PatchEnd-Patchcode-1,d0
Copy	move.b	(a1)+,(a0)+
	dbra	d0,Copy

	CALLEXEC Permit
	
	CALLINT OpenWorkBench			; apply patch to wb *NOW*

	move.l	_Outhandle(pc),d1		; Success !
	move.l	#Success,d2
	move.l	#SuccessEnd-Success,d3
	CALLDOS Write

CloseDOS
	move.l	_DOSBase(pc),a1
	CALLEXEC CloseLibrary
Exit
	moveq	#0,d0
	rts

*-----------------------------------------------------------------------*

Patchcode					; OpenWorkbench()
	move.l	a2,-(sp)			; save scratch
	move.l	_OldFunc(pc),a2			; get old callvector
	jsr	(a2)				; do the old OpenWorkbench()

	movem.l	d1/a0/a1,-(sp)			; lock WB Screen
	lea	wbname(pc),a0			; workbench name
	jsr	_LVOLockPubScreen(a6)		; screen => d0
	movem.l	(sp)+,d1/a0/a1		

	bsr.s	Patch				; apply the patch

	movem.l	d0/d1/a0/a1,-(sp)		; save reg's
	sub.l	a0,a0				; NULL
	move.l	d0,a1				; screen
	jsr	_LVOUnlockPubScreen(a6)		; unlock WB Screen
	movem.l	(sp)+,d0/d1/a0/a1		; restore reg's
	
	move.l	(sp)+,a2			; restore scratch
	rts					; bye


Patchcode1					; OpenScreen()
	move.l	a2,-(sp)			; save scratch register
	move.l	_OldFunc1(pc),a2		; get old routineaddress
	jsr	(a2)				; jump to old libraryroutine
	bsr.s	Patch				; do the patch 
	move.l	(sp)+,a2			; restore reg
	rts					; ciao

Patchcode2					; OpenScreenTagList()
	move.l	a2,-(sp)			; same as above 
	move.l	_OldFunc2(pc),a2		; but other Vector
	jsr	(a2)				; jump to old libraryroutine
	bsr.s	Patch				; do the patch
	move.l	(sp)+,a2			; same as above
	rts					; bye


;
; this is the actual patch (the routine that does the magic)
;

Patch	move.l	a2,-(sp)			; this is the actual patch
	move.l	d0,-(sp)			; in: d0 = screen
	beq.s	PatchExit			; screen not open !

	movem.l d1/a0/a1,-(sp)			; there should be no need BUT for
						; compatibility with shitty Stuff ...

	move.l	d0,a2				; Screen
	lea	sc_ViewPort(a2),a0		; Viewport
	move.l	vp_ColorMap(a0),a0		; Colormap
	lea	PatchTags(pc),a1		; Tag array	

	move.l	_GfxBase(pc),a6			;
	jsr	_LVOVideoControl(a6)		; set the tags

	move.l	a2,a0				; screen
	move.l	_IntuitionBase(pc),a6		;
	jsr	_LVOMakeScreen(a6)		; Re-Make the screen
	jsr	_LVORethinkDisplay(a6)		; Note: a6 is intlib again
 
	movem.l (sp)+,d1/a0/a1			; restore registers
PatchExit
	move.l	(sp)+,d0			; restore register
	move.l	(sp)+,a2			; restore register
	rts

;
;	Some data structures
;

_OldFunc	dc.l 0				; OpenWorkbench()
_OldFunc1	dc.l 0				; OpenScreen()
_OldFunc2	dc.l 0				; OpenScreenTagList()
_GfxBase	dc.l 0				; LibBase
_IntuitionBase	dc.l 0				; LibBase

PatchTags      	dc.l VTAG_BORDERBLANK_SET	; That makes the Magic
		dc.l 1				; TRUE
		dc.l TAG_DONE			; End of TAG ARRAY
		dc.l 0				; ti_Data unused

wbname		dc.b 'Workbench',0		; name of workbench screen	

PatchEnd

*-----------------------------------------------------------------------*

;
;	Other datastructures (used only at installtime)
;
	even

_DOSBase	dc.l 0				; LibBase
_Outhandle	dc.l 0				; filehandle

dosname 	DOSNAME				; Libname's
gfxname 	GFXNAME
intname 	INTNAME							

; 	some messages for the user
						
FalseKick
	dc.b	'Requires AmigaOS Release 2.0 or higher',10,0
FalseKickEnd

NoSupDen
	dc.b	'Requires a Superdenise',10,0
NoSupDenEnd

Success
	dc.b	'Patch installed!',10,0
SuccessEnd

Info
	dc.b	27,"[33mBlackBorder V1.0",27,"[31m © 1991 by Frank Riffel & Peter Kunath",10,0
InfoEnd
