; FRTest.asm
;
; A demo of how to call up my file requester from asm.
;
; By:  Khalid Aldoseri.
;
;
	include	'libraries/dosextens.i'
	include 'kdbase.i'

	XDEF	_LVOFindTask
	XDEF	_LVOOpenLibrary
	XDEF	_LVOCloseLibrary
	XDEF	_LVOGetMsg
	XDEF	_LVOWaitPort
	XDEF	_LVOReplyMsg

	SECTION main,CODE

	move.l	4,a6			;get Execbase
	move.l	a6,_ExecBase		;store execbase
	sub.l	a1,a1			;clear a1
	jsr	_LVOFindTask(a6)	;find address of process
	move.l	d0,a0			;store process address
	move.l	a0,_OurTask 		;in _OurTask

	tst.l	pr_CLI(a0)		;were we run from CLI?
	bne	.Start			;yes, jump to start

	move.l	pr_MsgPort(a0),a0	;no, get address of pr_MsgPort
	jsr	_LVOWaitPort(a6)	;WaitPort for the WBmsg
	move.l	_OurTask,a1		;load address of task
	lea	pr_MsgPort(a1),a0	;get address of pr_MsgPort
	jsr	_LVOGetMsg(a6)		;GetMsg the WBmsg
	move.l	d0,_WBMessage		;store WBmsg

.Start	move.l	#KDLIBVERSION,d0	;request lib version 3 or above
	lea	.kdname,a1		;load name of kdlib
	jsr	_LVOOpenLibrary(a6)	;openlibrary kdlib
	tst.l	d0			;check if lib opened
	beq	.Quit1			;no, quit
	move.l	d0,_KDBase 		;store KDBase

.Main	
	move.l	_KDBase,a6		;move KDBase into a6
	jsr	_LVOCreateFRequest(a6)	;initialize a FRequest struct
	tst.l	d0			;check for success
	beq	.Quit			;no, quit
	move.l	d0,_freq		;store FRequest address in _freq

	move.l	d0,a0			;move _freq into a0
	lea	.title,a1		;move .title into a1
	move.l	a1,kd_fr_reqtitle(a0)	;store it in kd_fr_reqtitle

					;set FR flags.. FR_STDFLAGS already
					;set by the CreateFRequest call.

	move.l	#FR_STDFLAGS,kd_fr_flags(a0)

	jsr	_LVONewFReq(a6)		;call up requester

	;d0 will contain either 0 for cancelled/failed or the address
	;of kd_fr_fullname if successful.

	tst.l	d0
	beq	.freefr

	; do your own stuff here with the filename

.freefr
	move.l	_freq,a0		;move _freq into a0
	jsr	_LVODeleteFRequest(a6)	;free FRequest struct

.Quit
	move.l	_KDBase,a1		;load KDBase into a1
	move.l	_ExecBase,a6		;get execbase
	jsr	_LVOCloseLibrary(a6)	;close library

.Quit1	move.l	_OurTask,a0		;get process pointer
	tst.l	pr_CLI(a0)		;were we run from WB?
	bne	.Quit2			;no, quit.
	move.l	_WBMessage,a1		;yes, load WBmsg into a1
	jsr	_LVOReplyMsg(a6)	;and reply to it.

.Quit2	clr.l	d0
	rts				;that's all folks!


	SECTION	info,DATA

_ExecBase	dc.l	0
_WBMessage	dc.l	0
_OurTask	dc.l	0
_KDBase		dc.l	0
_freq		dc.l	0

.title		dc.b	'File Requester Test',0

.kdname		dc.b	'kd_freq.library',0

	end
