
;	+---------------------------------------------------------------+
;	| Play v1.0							|
;	| ---------							|
;	|   Play is an example program to show you how easy it's to use |
;	| the multiformat replayer library *BFBPlayMaster.library* in   |
;	| you own applications.						|
;	|   This simple program is meant to be used only from CLI and it|
;	| takes one argument from commandline, the filename. It uses    |
;	| bfbplaymaster.library to check if the given file was a module |
;	| or not and if yes, then play it if correct subreplayer.library|
;	| is available.							|
;	|   Yes, this really is true. Bfbplaymaster.library makes	|
;	| possible for everyone to build for example an own multiformat	|
;	| moduleplayer (like this or the more powerful BFBPlayer	|
;	| included in this package.)					|
;	|   Please refer to autodocs for further information about the	|
;	| available library routines and the main documents about	|
;	| the copyrights and other more boring stuff.			|
;	|								|
;	|   Enjoy with this piece of work:	Jarkko Vatjus-Anttila	|
;	+---------------------------------------------------------------+

	include	exec/memory.i		; change these paths if your
	include utility/tagitem.i
	include	libraries/execxref.i	; includes are somewhere else.
	include	libraries/dosxref.i
	include	libraries/intuitionxref.i

	include	asm:libs/bfbplaymaster/include/asm/bfbplay.i
	include asm:libs/bfbplaymaster/include/asm/bfbplaybase.i
	include	asm:libs/bfbplaymaster/include/asm/bfbplayxref.i

	mc68000
	snmaopt	 e+

call	macro
	jsr	_LVO\1(a6)
	endm

	structure	maindatas,0
		aptr	dosbase
		aptr	bfbplaymasterbase

		aptr	modulebuffer
		aptr	filenameptr
	
		label	maindatas_SIZEOF

	section	maincode,code

	clr.b	-1(a0,d0)
	move.l	a0,a5			; store commandline ptr.

	move.l	4,a6			; allocate variable space.
	move.l	#maindatas_SIZEOF,d0
	move.l	#MEMF_ANY!MEMF_CLEAR,d1
	call	AllocMem
	move.l	d0,a4
	tst.l	d0
	beq	cleanup

	lea	dosname,a1		; and open libraries.
	moveq	#0,d0
	call	OpenLibrary
	move.l	d0,dosbase(A4)
	beq	cleanup

	lea	bfbplaymastername,a1
	moveq	#0,d0
	call	OpenLibrary
	move.l	d0,bfbplaymasterbase(a4)
	beq	cleanup

;	***********************************************************************
;	NOTE! that we do not allocate the modinfo structure. Because we are
;	      going to pass NULL as modinfo ptr to the master library, it
;	      allocates the structure for us. This saves our afforts, but we
;	      need to remember that because we didn't allocate the structure
;	      we must NOT try to free it either!
;	***********************************************************************

	bsr	parsecommandline	; parse commandline..
	bsr	operatemodule		; ..and load module.
	tst.l	d0			; error??
	bmi	cleanup

	bsr	playmodule		; play the module.
	tst.l	d0			; check return code.
	bmi	cleanup

	move.l	4,a6
	moveq	#0,d0
	bset	#12,d0			; SIGBIT CTRL_C
	call	Wait

;	***********************************************************************

cleanup	move.l	a4,d0			; if no variable space, then exit the
	beq	_999			; whole thing.

	move.l	modulebuffer(a4),d0
	beq	_101
	move.l	d0,a0
	move.l	bfbplaymasterbase(A4),a6
	call	BFBUnLoadModule		; unloadmodule frees *all* resources.
;	move.l	#0,bb_libinuse(a6)

_101	move.l	4,a6
	move.l	dosbase(A4),d0
	beq	_102
	move.l	d0,a1
	call	CloseLibrary

_102	move.l	bfbplaymasterbase(a4),d0
	beq	_104
	move.l	d0,a1
	call	CloseLibrary

_104	move.l	a4,a1
	move.l	#maindatas_SIZEOF,d0
	call	FreeMem

_999	moveq	#0,d0
	rts

;	***********************************************************************
	* Here begin our subroutines.					      *
;	***********************************************************************

parsecommandline
	move.l	a5,a0
	cmp.b	#34,(a0)		; check if there's a quote.
	bne	pcl_findspace
	adda.l	#1,a0
	move.l	a0,filenameptr(A4)	; store start of filename
pcl_lp1	tst.b	(a0)
	beq	pcl_exit		; loop until EOF or other quote
	cmp.b	#34,(a0)+
	bne	pcl_lp1
	move.b	#0,-1(a0)
	bra	pcl_exit

pcl_findspace
	move.l	a0,filenameptr(a4)	; loop until EOF or space.
pcl_lp2	tst.b	(a0)
	beq	pcl_exit
	cmp.b	#" ",(a0)+
	bne	pcl_lp2
	move.b	#0,-1(a0)
pcl_exit
	rts

;	***********************************************************************

printclimsg
	move.l	a0,a5
	moveq	#0,d3
pcm_lp1	addq.l	#1,d3
	tst.b	(a0)+
	bne	pcm_lp1
	move.b	-1(a0),d7
	move.b	#10,-1(a0)
	lea	-1(a0),a3
	move.l	dosbase(A4),a6
	call	Output
	move.l	d0,d1
	move.l	a5,d2
	call	Write
	move.b	d7,(a3)
	rts

;	***********************************************************************

operatemodule
	lea	playertags,a0
	move.l	filenameptr(a4),4(a0)

;	move.l	modulebuffer(a4),12(a0)
;	move.l	filenameptr(a4),a0
;	move.l	#0,a1			; no separate sampledata
;	move.l	modulebuffer(a4),a2

	move.l	bfbplaymasterbase(A4),a6
	call	BFBLoadModule
	tst.l	d0
	bpl	om_c0

	call	BFBGetError		; convert the errorcode into string
	move.l	d0,a0
	bsr	printclimsg
	bra	om_fail

om_c0	lea	playertags,a0		; fetch the moduleinfo buffer ptr,
	move.l	12(a0),modulebuffer(a4)	; because the library allocated it
	moveq	#0,d0			; for us.
	rts

om_fail	moveq	#-1,d0
	rts

;	***********************************************************************

playmodule
	move.l	modulebuffer(a4),a0
	move.l	bfbplaymasterbase(a4),a6
	call	BFBPlayModule
	tst.l	d0
	bpl	pm_c1

	call	BFBGetError
	move.l	d0,a0
	bsr	printclimsg
	moveq	#-1,d0
	rts

pm_c1	;move.l	#1,bb_libinuse(a6)
	lea	tempbuffer,a1
	move.l	modulebuffer(a4),a0

	move.l	mi_modulename(a0),d0		; just pick the information
	bne	pm_c0				; you need from modinfo
	move.l	filenameptr(a4),d0		; structure.
pm_c0	move.l	d0,(a1)
	move.l	mi_moduletypename(a0),4(a1)
	move.l	mi_bufferlen(a0),8(a1)
	move.l	mi_bufferptr(a0),12(a1)
	move.l	mi_bufferptr(a0),d0
	add.l	mi_bufferlen(a0),d0
	move.l	d0,16(a1)

	lea	formatstring,a0
	lea	pcp(pc),a2			; format it into understandable
	lea	charbuffer,a3			; form.
	move.l	4,a6
	call	RawDoFmt

	lea	charbuffer,a0			; and disply.
	bsr	printclimsg
	moveq	#0,d0
pm_exit	rts

pcp	move.b	d0,(a3)+
	rts

;	***********************************************************************

	section	datas,data

playertags
	dc.l	BFBTAG_SongName,0
	dc.l	BFBTAG_ModInfo,0
;	dc.l	BFBTAG_ModType,MODTYPE_PROTRACKER
	dc.l	TAG_DONE

dosname	dc.b	'dos.library',0
intuitionname
	dc.b	'intuition.library',0
bfbplaymastername
	dc.b	'bfbplaymaster.library',0

formatstring
	dc.b	'Name:   %s',10
	dc.b	'Format: %s',10
	dc.b	'Length: %ld ($%lx -> $%lx)',10
	dc.b	'Status: Replaying. Press CTRL+C to quit.',0

	section	tempdatas,bss

tempbuffer
	ds.l	5
charbuffer
	ds.b	256
	even
