;***************************** PopColor.asm ****************************
; This utility, when executed, waits a few seconds for the user to re-
; arrange a desired screen to the front of the display. Then the dissidents
; ColorTool is opened on that screen, allowing the user to adjust the
; colors. The dissidents color.library must be in the libs drawer of the
; boot disk.
;
; Assemble this file and link
; 1> blink PopColor.o small.lib nodebug to ram:PopColor

	SMALLOBJ
	OPTIMON
	OBJFILE "rad:Color.o"

	SECTION ColorCode,CODE

	INCLUDE	"rad:FileIO.i"

	XREF	_LVOOldOpenLibrary,_LVOCloseLibrary
	XREF	_LVOWaitPort,_LVOGetMsg,_LVOReplyMsg
	XREF	_LVODelay,_LVOOutput,_LVOWrite,_LVODisplayAlert,_LVORead
	XREF	_LVOOpen,_LVOClose

_LVODoColor	equ	-30

TDNestCnt	equ	295
ThisTask		equ	$114
pr_CLI		equ	$AC
pr_MsgPort	equ	$5C

	XDEF	_startup
_BUFFER:
_startup:
	;---Get Exec library base
		movea.l	$00000004,a6
		movea.l	a6,a4			;_SysBase
	;---Open the DOS library
		lea		DOSName,a1
		jsr		_LVOOldOpenLibrary(a6)
		movea.l	d0,a5
	;---Open the Intuition library
		lea		IntuiName,a1
		jsr		_LVOOldOpenLibrary(a6)
		movea.l  d0,a3
	;---get the address of our task
		movea.l	ThisTask(a6),a2 
	;---are we running as a son of Workbench?
		moveq		#0,d7
		move.l	pr_CLI(a2),d0
		bne.s		doMN
	;---WorkBench: wait for a message from our starter
		;[a2=ThisTask]
		;[a5=_DOSBase]
		lea		pr_MsgPort(a2),a0
		jsr		_LVOWaitPort(a6)
		lea		pr_MsgPort(a2),a0
		jsr		_LVOGetMsg(a6)
	;---save the message so we can return it later
		move.l	d0,d7
doMN	bra		domain

IntuiName	dc.b	'intuition.library',0
DOSName		dc.b	'dos.library',0
ColorErr		dc.b	'Need the dissidents '
ColorName	dc.b	'color.library',0
ReqErr		dc.b	'Need the dissidents '
ReqName		dc.b	'requester.library',0
NoMem			dc.b	'Out of memory',0

	CNOP	0,2
req_err:
		lea		ReqErr,a1
		bra.s		dsp_err
fil_err:
		lea		NoMem,a1
		bra.s		dsp_err
col_err:
		lea		ColorErr,a1
;This makes an alert message out of the passed string in a1
;and displays it as an alert or CLI msg. 79 chars MAX in passed string.
;wb msg in d7, DOSBase in a5, IntuitionBase in a3
dsp_err:
		suba.w	#256,sp
		movea.l	sp,a0
		addq.l	#2,a0			;for WB, skip X pos
		moveq		#20,d0
		move.b	d0,(a0)+		;y co-ordinate = 20
		move.l	a0,d2
  ;---copy passed string to BUFFER and get numOfChars
		movea.l	a1,a6
lenS	move.b	(a6)+,(a0)+
		bne.s		lenS
		subq.l	#1,a6
		suba.l	a1,a6			;numOfChars
  ;---If the CLI, write the msg there
		move.l	d7,d1			;wb msg
		bne.s		WBalert
		clr.b		(a0)
		moveq		#10,d0
		move.b	d0,-(a0)
		move.l	a6,d3
	;---get standard output handle
		movea.l	a5,a6
		jsr		_LVOOutput(a6)
		move.l	d0,d1
  ;---add new line char
		addq.l	#1,d3
		jsr		_LVOWrite(a6)
fail_out:
		adda.w	#256,sp
		bra		faIL
  ;---If WB, post an alert
WBalert:
		clr.b		(a0)			;continuation byte = 0
		moveq		#80-1,d1		;80 chars - 1 Dbra
		sub.l		a6,d1
		bcc.s		aler
		moveq		#1,d1
aler	lsr.l		#1,d1
		lsl.w		#3,d1
		movea.l	d2,a0
		subq.l	#3,a0
		move.w	d1,(a0)		;x co-ordinate = (numOfChars/2) * 8 (WORD)
    ;---display the alert
		moveq		#50,d1		;Height
		moveq		#0,d0			;RECOVERY_ALERT
		movea.l	a3,a6
		jsr		_LVODisplayAlert(a6)
		bra.s		fail_out

domain:
		suba.l	a2,a2
		moveq		#0,d5
	;---Open the dissidents color library
		lea		ColorName,a1
		jsr		_LVOOldOpenLibrary(a6)
		move.l	d0,d6
		beq.s		col_err
	;---Open the dissidents requester library
		lea		ReqName,a1
		jsr		_LVOOldOpenLibrary(a6)
		move.l	d0,d5
		beq		req_err
	;---Get a FileIO
		movea.l	d5,a6
		jsr		_LVOGetFileIO(a6)
		move.l	d0,d1
		beq		fil_err
		movea.l	d0,a2
		lea		_BUFFER,a1
		move.l	a1,FILEIO_BUFFER(a2)
		lea		TableToLoad,a1
		move.l	a1,FILEIO_TITLE(a2)
	;---give the user time to rearrange the screens
		moveq		#127,d1
		add.w		d1,d1
		movea.l	a5,a6
		jsr		_LVODelay(a6)
	;---See if user has a ColorTable to load
		bsr		DoFile
		beq.s		doDD
		move.l	FILEIO_FILESIZE(a2),d2
		beq.s		doDD
	;---Load ColorTable
		move.w	#1005,d2
		bsr		OpenFile
		move.l	d0,d2
		beq.s		doDD
		moveq		#32*2,d3
		lea		CTable,a1
		move.l	a1,d2
		jsr		_LVORead(a6)
		move.l	d4,d1
		jsr		_LVOClose(a6)
	;We get the address of front screen from Intuition. This will work until
	;1.4 (IntuitionBase disappears then).
	;NOTE - We assume that someone doesn't close that screen before we write
	;out its ColorTable. We really should do a Forbid/Permit around this whole
	;bloody thing. What the hell. We wouldn't need this utility if other
	;programmers used the color library anyway.
	;---open the color tool window on this screen
doDD	movea.l	60(a3),a1	;the dreaded screen
		movea.l	44+4(a1),a0
		addq.l	#4,a0
		move.l	(a0),d3		;its ColorTable
		movea.l	d2,a0			;0 or the loaded ColorTable
		movea.l	d6,a6
		jsr		_LVODoColor(a6)
	;---See if user wants to save the screen's ColorTable
		lea		TableToSave,a1
		move.l	a1,FILEIO_TITLE(a2)
		bsr.s		DoFile
		beq.s		faIL
		move.w	#1006,d2
		bsr		OpenFile
		beq.s		faIL
		moveq		#32*2,d2
		exg.l		d2,d3
		jsr		_LVOWrite(a6)
		move.l	d4,d1
		jsr		_LVOClose(a6)
	;---Free the FileIO
faIL	tst.l		d5
		beq.s		clC
		movea.l	a2,a1
		movea.l	d5,a6
		jsr		_LVOReleaseFileIO(a6)
		movea.l	a4,a6
		movea.l	d5,a1
		jsr		_LVOCloseLibrary(a6)
	;---close the color library
clC	movea.l	a4,a6
		tst.l		d6
		beq.s		clI
		movea.l	d6,a1
		jsr		_LVOCloseLibrary(a6)
	;---close Intuition library
clI	movea.l	a3,a1
		jsr		_LVOCloseLibrary(a6)
	;---close DOS library
		movea.l	a5,a1
		jsr		_LVOCloseLibrary(a6)
	;---if we ran from CLI, skip workbench cleanup
		move.l	d7,d0
		beq.s		exitToDOS
	;------ return the startup message to our parent
	;------ we forbid so workbench can't UnLoadSeg() us
	;------ before we are done
		addq.b	#1,TDNestCnt(a6)
		;[d0=returnMsg]
		movea.l	d0,a1
		jsr		_LVOReplyMsg(a6)
	;---this rts sends us back to DOS
exitToDOS:
		moveq		#0,d0
		rts

DoFile:
		movea.l	60(a3),a1
		movea.l	d5,a6
		movea.l	a2,a0
		jsr		_LVODoFileIOWindow(a6)
		move.l	d0,d2
		beq.s		fff
		addq.l	#1,d2
fff	rts

OpenFile:
		ext.l		d2
		move.l	d0,d1
		movea.l	a5,a6
		jsr		_LVOOpen(a6)
		move.l	d0,d1
		move.l	d0,d4
		rts

CTable			ds.w	32
TableToLoad		dc.b	'Load ColorTable. CANCEL if none',0
TableToSave		dc.b	'Save ColorTable. CANCEL if none',0

	END
