;	TagScreens v1.5 (6.3.92)
;	(c) 1992 by Martin Berndt
;	This program is placed in the Public Domain!
;
;	- SA_Pens & SA_FullPalette will be added to every new screen
;	- NTSC/PAL will be selected automatically
;	- SA_AutoScroll will be added to every new screen
;	- Special patch for CygnusEd included
;	- 2nd activation removes all patches
;	- Option "NOMON" disables PAL/NTSC switching
;	- Option "NOSCROLL" disables AutoScroll
;	- Option "SIZE" allows to specify the max. NTSC-Height (NoLace)

	include	"exec/funcdef.i"
	include	"exec/types.i"
	include	"exec/initializers.i"
	include	"exec/resident.i"
	include	"exec/libraries.i"
	include	"exec/memory.i"
	include	"exec/execbase.i"
	include	"exec/exec_lib.i"
	include	"intuition/intuition_lib.i"
	include	"intuition/intuition.i"
	include	"graphics/displayinfo.i"
	include	"dos/dos_lib.i"
	include	"utility/tagitem.i"

START	move.l	$4.w,a6
	lea	intuiname(pc),a1
	bsr.s	OpenLib
	move.l	d0,a5
	lea	dosname(pc),a1
	bsr.s	OpenLib
	move.l	d0,a4
	exg	a4,a6
	jsr	_LVOOutput(a6)
	move.l	d0,d6
	cmpi.w	#37,LIB_VERSION(a4)
	bcs	badkick
	move.l	#template,d1
	move.l	#argument,d2
	moveq	#0,d3
	jsr	_LVOReadArgs(a6)
	tst.l	d0
	bne.s	ArgsOk
	jsr	_LVOIoErr(a6)
	move.l	d0,d1
	move.l	d3,d2
	jsr	_LVOPrintFault(a6)
	bra	CloseLibs

OpenLib	moveq	#0,d0
	jmp	_LVOOpenLibrary(a6)

ArgsOk	lea	argument+8(pc),a0
	move.l	(a0),d1
	beq.s	NoSArg
	move.l	d1,a1
	move.l	(a1),(a0)
NoSArg	move.l	d0,d1
	jsr	_LVOFreeArgs(a6)
	exg	a4,a6
	move.l	_LVOOpenScreenTagList+2(a5),a0
	move.l	a0,a2
	lea	Cygnus-Second(a0),a0
	lea	Cygnus(pc),a1
	moveq	#8,d0
loop1	cmp.b	(a0)+,(a1)+
	bne.s	Patch
	dbf	d0,loop1
	jsr	_LVOForbid(a6)
	move.l	#_LVOOpenScreenTagList,d0
	move.l	-4(a2),a0
	bsr.s	SetFunc
	move.l	#_LVOOpenScreen,d0
	move.l	Code_Start+4-Second(a2),a0
	bsr.s	SetFunc
	jsr	_LVOPermit(a6)
	lea	Code_Start-Second(a2),a1
	jsr	_LVOFreeVec(a6)
	lea	text3(pc),a1
	bra.s	ErrorTxt

badkick	exg	a4,a6
	lea	text2(pc),a1
	bra.s	ErrorTxt
badmem	lea	text4(pc),a1
	bra.s	ErrorTxt

SetFunc	exg	d0,a0
	move.l	a5,a1
	jmp	_LVOSetFunction(a6)

Patch	move.l	#Code_End-Code_Start,d0
	move.l	d0,d5
	move.l	#MEMF_CLEAR|MEMF_PUBLIC,d1
	jsr	_LVOAllocVec(a6)
	move.l	d0,d7
	beq.s	badmem
	move.l	d0,a1
	lea	Code_Start(pc),a0
	move.l	d5,d0
	jsr	_LVOCopyMem(a6)
	jsr	_LVOForbid(a6)
	move.l	d7,a0
	addq.w	#4,a0
	move.l	#_LVOOpenScreen,d0	
	move.l	2(a5,d0.w),(a0)+
	bsr.s	SetFunc
	move.l	d7,a0
	lea	Second-Code_Start-4(a0),a0
	move.l	#_LVOOpenScreenTagList,d0
	move.l	2(a5,d0.w),(a0)+
	bsr.s	SetFunc
	jsr	_LVOPermit(a6)
	lea	text1(pc),a1
ErrorTxt	moveq	#0,d3
	move.b	(a1)+,d3
	move.l	a1,d2
	move.l	d6,d1
	exg	a4,a6
	jsr	_LVOWrite(a6)
CloseLibs	exg	a4,a6
	move.l	a4,a1
	jsr	_LVOCloseLibrary(a6)
	move.l	a5,a1
	jsr	_LVOCloseLibrary(a6)
	moveq	#0,d0
	rts

text1	dc.b	text2-text1-1
	dc.b	$9b,"0;33;40m>>> TagScreens v1.5 <<<",$9b
	dc.b	"0;31;40m  (c) 1992 by Martin Berndt",10
	dc.b	"_LVOOpenScreen and _LVOOpenScreenTagList patched.",10
text2	dc.b	text3-text2-1
	dc.b	"This tool needs Kickstart v37 or higher",10
text3	dc.b	text4-text3-1
	dc.b	$9b,"0;33;40mTagScreens"
	dc.b	$9b,"0;31;40m removed.",10
text4	dc.b	dosname-text4-1
	dc.b	"Oops... Not enough memory!",10
dosname	dc.b	"dos.library",0
intuiname	dc.b	"intuition.library",0
template	dc.b	"NOMON/S,NOSCROLL/S,SIZE/N",0
	dc.b	"$VER: TagScreens v1.5 (6.3.92)",0
	even

Code_Start
	move.l	(sp)+,a0
	jmp	$f80000
	btst	#4,ns_Type(a0)
	beq.s	DoPatch
	move.l	a0,-(sp)
	move.l	ens_Extension(a0),a0
	cmpi.l	#SA_Pens,(a0)
	bne.s	Code_Start
	tst.l	8(a0)
	bne.s	Code_Start
	move.l	(sp)+,a0

DoPatch	link	a5,#-$30
	move.l	sp,a1
	movem.l	d0-d1/a0,-(sp)
	move.l	#SA_Pens,(a1)+
	bclr	#4,ns_Type(a0)
	beq.s	SkipExt
	move.l	ens_Extension(a0),a0
	move.l	4(a0),a0
	bra.s	SkipPens
SkipExt	lea	PenDatas(pc),a0
SkipPens	move.l	a0,(a1)+
	move.l	#SA_FullPalette,(a1)+
	moveq	#-1,d0
	move.l	d0,(a1)+
	move.l	argument(pc),d0
	bne.s	NoMon
	move.l	8(sp),a0
	tst.w	ns_Height(a0)
	bmi.s	NoMon
	move.l	#SA_DisplayID,(a1)+
	move.l	argument+8(pc),d0
	bne.s	SizeArg
	move.w	#200,d0
SizeArg	moveq	#0,d1
	move.w	ns_ViewModes(a0),d1
	andi.w	#%1001110011100101,d1
	btst	#2,d1
	beq.s	SkipLace
	add.w	d0,d0
SkipLace	sub.w	ns_Height(a0),d0
	bcs.s	MonPAL
	sub.w	ns_TopEdge(a0),d0
	bcs.s	MonPAL
	ori.l	#NTSC_MONITOR_ID,d1
	bra.s	SetMon
MonPAL	ori.l	#PAL_MONITOR_ID,d1
SetMon	move.l	d1,(a1)+
NoMon	move.l	argument+4(pc),d0
	bne.s	NoScroll
	lea	ScrData(pc),a0
	moveq	#3,d0
ScrLoop	move.l	(a0)+,(a1)+
	dbf	d0,ScrLoop
NoScroll	clr.l	(a1)
	movem.l	(sp)+,d0-d1/a0
	move.l	sp,a1
	jsr	_LVOOpenScreenTagList(a6)
	unlk	a5
	rts

Cygnus	dc.b	"CygnusEd",0
	even

UseOld	movem.l	(sp)+,a0/a1/a6
UseOld0	jmp	$f80000
Second	movem.l	a0/a1/a6,-(sp)
	move.l	$4.w,a6
	suba.l	a1,a1
	jsr	_LVOFindTask(a6)
	lea	Cygnus(pc),a0
	move.l	d0,a1
	move.l	10(a1),a1
	moveq	#8,d0
loop2	cmp.b	(a0)+,(a1)+
	bne.s	UseOld
	dbf	d0,loop2
	movem.l	(sp)+,a0/a1/a6
	link	a5,#-$50
	move.l	a0,-(sp)
	lea	4(sp),a0
loop3	tst.l	(a1)
	beq.s	gettag
	move.l	(a1)+,(a0)+
	move.l	(a1)+,(a0)+
	bra.s	loop3
gettag	move.l	#SA_Pens,(a0)+
	lea	PenDatas(pc),a1
	move.l	a1,(a0)+
	move.l	argument+4(pc),d0
	bne.s	noscrtag
	lea	ScrData(pc),a1
	moveq	#3,d0
scrtlop	move.l	(a1)+,(a0)+
	dbf	d0,scrtlop
noscrtag	clr.l	(a0)
	move.l	(sp)+,a0
	move.l	sp,a1
	bsr.s	UseOld0
	unlk	a5
	rts

PenDatas	dc.w	3,1,1,2,1,3,1,0,2,-1
ScrData	dc.l	SA_Overscan,OSCAN_STANDARD,SA_AutoScroll,-1
argument	dc.l	0,0,0
Code_End
	end
