;*******************************************************************************
; Director - MessageTrans (Messages)
;
; Copyright (C) 2003, Nick Craig-Wood and Philip Ludlam
;
;This program is free software; you can redistribute it and/or modify it under
;the terms of the GNU General Public License as published by the Free Software
;Foundation; either version 2 of the License, or (at your option) any later
;version.
;
;This program is distributed in the hope that it will be useful, but WITHOUT ANY
;WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
;PARTICULAR PURPOSE. See the GNU General Public License for more details.
;
;You should have received a copy of the GNU General Public License along with
;this program; if not, write to the Free Software Foundation, Inc., 59 Temple
;Place - Suite 330, Boston, MA 02111-1307, USA
;
;*******************************************************************************
;----h- Director.s.Messages
; Name
;   Messages
;
; Purpose
;   Setup a MessageTrans file for Director
;------
;*******************************************************************************


		TTL	> Messages

		GET	OSLib:oslib.hdr.MessageTrans
		GET	OSLib:oslib.hdr.OSFile
		GET	OSLib:oslib.hdr.Wimp
		GET	AsmLib2:hdr.RegsBoth
		GET	AsmLib2:hdr.MacrosBoth
		GET	h.WorkSpace
		GET	h.Constants
		GET	h.Memory
		GET	h.Task
		GET	h.Menus
		GET	h.BMG
		GET	h.ModuleHead

		AREA	|Message|, CODE, READONLY


;*******************************************************************************
; Variables local to the Message
; These are allocated from a block in the main workspace
;*******************************************************************************


		^	(:INDEX: MessageVars), wp

MessageBuffer	#	4		;  to contents of messages file
MessageDesc	#	4		;  to file desc. + filename

		ASSERT	@ <= EndMessageVars


;*******************************************************************************
;----f- Director.s.Messages.MessageInitialise
; Name
;   MessageInitialise
;
; Purpose
;   Initialise Director's use of MessageTrans
;
; Entry
;
;
; Exit
;
;------
;*******************************************************************************


MessageInitialise	ROUTINE	"r1-r4", EXPORT

		MOV	r0, #0
		ADR	r1, messagesfile$l
		SWI	XMessageTrans_FileInfo
		BVS	error$l

		CMP	r2, #0
		BEQ	skipcreatebuffer$l

		MOV	r0, r2				; r0 = size of buffer
		BL	malloc
		STR	r0, MessageBuffer		; store ptr
		MOV	r4, r0				; r4  MessageBuffer

skipcreatebuffer$l
		MOV	r0, #16 + 29			; r0 = space for file desc. + filename
		BL	malloc
		STR	r0, MessageDesc			; store ptr
		MOV	r3, r0				; r3  MessageDesc

		ADD	r1, r0, #16			; r1  filename in file desc.
		ADR	r0, messagesfile$l
		BL	strcpy

		MOV	r0, r3				; r0  MessageDesc
		ADD	r1, r0, #16			; r1  filename in MessageDesc
		MOV	r2, r4				; r2  MessageBuffer
		SWI	XMessageTrans_OpenFile
		BVS	error$l

error$l		EXIT

messagesfile$l	DCB	"<Director$Resources>Messages", 0
		ALIGN


;*******************************************************************************
;----f- Director.s.Messages.MessageFinalise
; Name
;   MessageFinalise
;
; Purpose
;   Finalise Director's use of MessageTrans
;
; Entry
;
;
; Exit
;
;------
;*******************************************************************************


MessageFinalise	ROUTINE	"r0", EXPORT

		LDR	r0, MessageDesc
		SWI	XMessageTrans_CloseFile		; ignore any errors

		LDR	r0, MessageBuffer		; deallocate space for the Messages file
		BL	free

		LDR	r0, MessageDesc			; deallocate space for the MessageTrans output buffer
		BL	free

exit$l		EXIT


;*******************************************************************************
;----f- Director.s.Messages.MessageLookupAddr
; Name
;   MessageLookupAddr
;
; Purpose
;   Call the MessageTrans Lookup SWI to return a pointer to text from a token.
;
; Entry
;   r0  token
;
; Exit
;   If an error occured:
;     r0 = pointer to valid error block
;     VS
;   Otherwise
;     VC
;     r0  read only string
;------
;*******************************************************************************


MessageLookupAddr ROUTINE "r1-r3", EXPORT

		MOV	r2, #0				; no output buffer
		MOV	r1, r0				; r1  token
		LDR	r0, MessageDesc

		SWI	XMessageTrans_Lookup
		MOVVC	r0, r2				; if OK, the return the output block

		EXIT


;*******************************************************************************
;----f- Director.s.Messages.MessageLookup
; Name
;   MessageLookup
;
; Purpose
;   Call the MessageTrans Lookup SWI to translate a token into text.
;
; Entry
;   r0  token
;   r1  ouput block
;   r2  size of output block (if r1 is NULL)
;
; Exit
;   If an error occured:
;     r0 = pointer to valid error block
;     VS
;   Otherwise
;     VC
;     r0 = output block
;     r1 = size of output
;------
;*******************************************************************************


MessageLookup	ROUTINE "r2-r7", EXPORT

		MOV	r3, r2				; r3 = output buffer size
		MOV	r2, r1				; r2  output buffer
		MOV	r1, r0				; r1  token
		LDR	r0, MessageDesc

		MOV	r4, #0
		MOV	r5, #0
		MOV	r6, #0
		MOV	r7, #0
		SWI	XMessageTrans_Lookup
		MOVVC	r0, r2				; if OK, the return the output block
		MOVVC	r1, r3

		EXIT


;*******************************************************************************
;----f- Director.s.Messages.MessageErrorLookup3
; Name
;   MessageErrorLookup3
;
; Purpose
;   Call the MessageTrans ErrorLookup SWI to translate an error block into text.
;
; Entry
;   r0  pointer to a valid error block (with MessageTrans token)
;   r1   not used
;   r2  pointer to the output error block
;   r3  size of output error block
;   r4  pointer to parameter 1
;   r5  pointer to parameter 2
;   r6  pointer to parameter 3
;
; Exit
;   VS
;   r0  Error_Block
;   r1 = size of output
;------
;*******************************************************************************


MessageErrorLookup3	ROUTINE "r1,r7", EXPORT

		LDR	r1, MessageDesc
		MOV	r7, #0
		SWI	XMessageTrans_ErrorLookup

		EXIT


;*******************************************************************************
;----f- Director.s.Messages.MessageErrorLookup
; Name
;   MessageErrorLookup
;
; Purpose
;   Call the MessageTrans ErrorLookup SWI to translate an error block into text.
;
; Entry
;   r0  pointer to a valid error block
;
; Exit
;   VS
;   r0  Error_Block
;   r1 = size of output
;------
;*******************************************************************************


MessageErrorLookup	ROUTINE "r1-r7", EXPORT

		LDR	r1, MessageDesc
		ADR	r2, Error_Block				; r2  output buffer
		MOV	r3, #StringSize				; r3 = size of Erro_Block
		MOV	r4, #0
		MOV	r5, #0
		MOV	r6, #0
		MOV	r7, #0
		SWI	XMessageTrans_ErrorLookup

		EXIT


;*******************************************************************************
; ReportErrorNew
;*******************************************************************************

;ReportErrorNew	ROUTINE "", EXPORT

;*******************************************************************************
; ReportErrorSWI
;
; Called when a SWI call returns an error which Director can continue from
;
; Entry
;   r0  valid error block
;
; Exit
;
; Typical use:
;	BL	ReportErrorSWI
;	EXIT_ERR
;*******************************************************************************

;ReportErrorSWI	ROUTINE "", EXPORT

;*******************************************************************************
; FatalErrorSWI
;
; Called when a SWI call returns an error which Director cannot continue from
;
; Entry
;   r0  valid error block
;
; Exit
;   this routine will not exit.
;*******************************************************************************

;FatalErrorSWI	ROUTINE "", EXPORT

;		SWI	OS_GenerateError	; an error while initialising is fatal


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

		END
