	opt		l-,c+,a+
*********************************
*	TABSIZE 4					*
*********************************
*	executable					*
*	case dependent				*
*	pc relative					*
*********************************
	output 	RAM:WBcol
*********************************
*	program name				*
*********************************
	incdir	I:
*********************************
*		author:					*
*	Duponcheel Marc	© 1989		*
*	Varenlaan 13				*
*	B-2610 Wilrijk				*
*	--------------				*
*	BELGIUM						*
*	tel: (03) 830.48.27			*
*********************************
*	DevPac Macro Assembler		*
*	- version 2.13				*
*********************************
*	WBcol copies				*
*	Workbench colors			*
*	to the front screen			*
*********************************

*********************************
	include	exec/exec_lib.i
	include	libraries/dosextens.i
	include	graphics/graphics_lib.i
	include	intuition/intuition_lib.i
	include	intuition/intuition.i
*********************************

screen		equr	a0
firstview	equr	a5					viewport of frontmost screen
workview	equr	a4					viewport of workbench screen
COLORS		equ		$20					size of color table
colors		equr	a3					table of colors
index		equr	d3					index in color table

*	shared library stuff
libbase		equr	a6
execbase	equr	d4
intbase		equr	d5
gfxbase		equr	d6

return		equr	d7					return code for amigados
OK			equ		$0
KO			equ		$1

	moveq.l	#KO,return					expect trouble
	move.l	#_SysBase,a0
	move.l	(a0),execbase
	move.l	execbase,libbase

	sub.l	a1,a1
	jsr		_LVOFindTask(libbase)
	move.l	d0,a0						don't test...
	move.l	pr_CLI(a0),FromWB
	bne.s	FromCLI
	lea		pr_MsgPort(a0),a0
	jsr		_LVOWaitPort(libbase)
	jsr		_LVOGetMsg(libbase)			workbench startup message
	move.l	d0,MsgPtr
FromCLI

ReadyGfx
	lea		GfxName,a1
	moveq.l	#0,d0
	jsr		_LVOOpenLibrary(libbase)
	move.l	d0,gfxbase					graphics library
	beq.s	PurgeGfx

ReadyInt
	lea		IntName,a1
	moveq.l	#0,d0
	jsr		_LVOOpenLibrary(libbase)
	move.l	d0,intbase					intuition library
	beq.s	PurgeInt

	move.l	d0,libbase					d0 = intbase
	move.l	ib_FirstScreen(libbase),screen
	lea		sc_ViewPort(screen),a1
	move.l	a1,firstview
LoopScreen
	move.w	sc_Flags(screen),d0
	and.w	#SCREENTYPE,d0
	cmp.w	#WBENCHSCREEN,d0			look for workbench screen
	beq.s	Workbench
	move.l	sc_NextScreen(screen),screen
*	tst.l	screen
	bne.s	LoopScreen
	bra.s	NoWorkbench					not found ???
Workbench
	lea		sc_ViewPort(screen),workview

	bsr		CopyColors

	moveq.l	#OK,return
NoWorkbench

	move.l	execbase,libbase
	move.l	intbase,a1
	jsr		_LVOCloseLibrary(libbase)
PurgeInt

	move.l	gfxbase,a1
	jsr		_LVOCloseLibrary(libbase)
PurgeGfx

	tst.l	FromWB
	bne.s	NotFromWB
	jsr		_LVOForbid(libbase)
	move.l	MsgPtr,a1
	jsr		_LVOReplyMsg(libbase)
NotFromWB

	move.l return,d0
	rts

CopyColors
	lea		Colors,colors
	moveq.w	#$0,index
	move.l	gfxbase,libbase
NextColor
	move.l	vp_ColorMap(workview),a0
	move.w	index,d0
	jsr		_LVOGetRGB4(libbase)
	lsl.w	#$1,index
	move.w	d0,(colors,index)
	lsr.w	#$1,index
	addq.w	#$1,index
	cmp.w	#COLORS,index
	bne.s	NextColor

	move.l	firstview,a0
	move.l	colors,a1
	move.l	vp_ColorMap(a0),a2
	move.w	cm_Count(a2),d0
	jsr		_LVOLoadRGB4(libbase)
	rts

Colors
	ds.w	COLORS
FromWB
	dc.l	$0
MsgPtr
	dc.l	$0
GfxName
	GRAFNAME
	even
IntName
	INTNAME
	even
