;
; loadConfig.s
;
; Load a Sculptrix configuration file
;
;  1997-1998 Straylight
;

;----- Licensing note -------------------------------------------------------
;
; This file is part of Straylight's Sculptrix.
;
; Sculptrix 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, or (at your option)
; any later version.
;
; Sculptrix 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 Sculptrix.  If not, write to the Free Software Foundation,
; 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

;----- Revision history -----------------------------------------------------
;
; Version	By	Change
;
; 1.00		mdw	Initial version.

;----- Standard header ------------------------------------------------------

		GET	libs:header
		GET	libs:swis

		GET	libs:stream

;----- External dependencies ------------------------------------------------

		IMPORT	version
		IMPORT	|Image$$RW$$Limit|

;----- Main code ------------------------------------------------------------

		AREA	|App$$Code|,CODE,READONLY
		ENTRY

; --- main ---
;
; On entry:	--
;
; On exit:	--
;
; Use:		Loads a named Sculptrix file.  If we're currently in the
;		desktop, and Sculptrix thinks that it's necessary, redraw
;		the screen.

main		ROUT

		; --- Set up some workspace ---

		SWI	OS_GetEnv		;Read environment information
		ADR	R12,|Image$$RW$$Limit|	;Get end of the program
		ADD	R14,R12,#lc__wSize	;Add on my requirements
		CMP	R14,R1			;Do I have enough?
		ADRCS	R0,lc__noRoom		;No -- point to error
		SWICS	OS_GenerateError	;And report to the user

		; --- Read the arguments string ---

00		LDRB	R14,[R0],#1		;Load next byte out
		CMP	R14,#&20		;Is it a space or less?
		BHI	%b00			;No -- keep going then
00		CMP	R14,#&20		;Is it exactly a space?
		LDREQB	R14,[R0],#1		;Yes -- fetch another one
		BEQ	%b00			;And keep going
		SUB	R1,R0,#1		;Point back to last char

		ADR	R0,lc__argDef		;Point to description string
		ADR	R2,lc__args		;Point to the argument buffer
		MOV	R3,#256			;Use up lots of buffer space
		SWI	OS_ReadArgs		;Read the arguments in

		; --- Inspect the arguments ---

		LDR	R14,lc__wantHelp	;Load the help flag
		CMP	R14,#0			;Is it set?
		BNE	lc__help		;Yes -- give some help

		LDR	R1,lc__filename		;Load filename address
		CMP	R1,#0			;Is this defined?
		ADREQ	R0,lc__noFile		;No -- point to an error
		SWIEQ	OS_GenerateError	;And return the error

		; --- Check the file's size for sanity ---

		MOV	R0,#17			;Find out some file info
		SWI	OS_File			;Read all about the file
		TST	R0,#1			;Is this a file?
		MOVEQ	R2,R0			;No -- get the object type
		MOVEQ	R0,#19			;Return me an error please
		SWIEQ	OS_File			;And get the error

		CMP	R4,#256			;Is the file too big?
		ADRCS	R0,lc__tooBig		;Yes -- point to error
		SWICS	OS_GenerateError	;And report it to the user

		; --- Load the file into memory ---

		MOV	R0,#16			;Load the file into memory
		ADD	R2,R12,#512		;Point to appropriate chunk
		MOV	R3,#0			;Load where I said, please
		SWI	OS_File			;Do that, please

		; --- Check the file's contents for sanity ---

		LDR	R0,[R12,#512]		;Load the first word
		LDR	R14,lc__magic		;Load what it should be
		CMP	R0,R14			;These should be the same
		ADRNE	R0,lc__notConf		;Point to an error
		SWINE	OS_GenerateError	;And complain if they're not

		; --- Send the configuration to Sculptrix ---

		ADD	R0,R12,#512 + 4		;Point to actual config data
		SWI	XSculptrix_SetConfig	;Set the configuration

		; --- Error checking here ---
		;
		; I know for a fact, because I have the source code in front
		; of me, that Sculptrix_SetConfig never returns an error.
		; Therefore, if V is set right now, I know that Sculptrix
		; isn't actually there, and I can return a slightly more
		; useful error message.

		ADRVS	R0,lc__noSculpt		;Point to message
		SWIVS	OS_GenerateError	;And return it to caller

		; --- If C is clear, there's nothing to redraw ---
		;
		; Also, I may not have to do any redrawing because I was
		; told not to.

		SWICC	OS_Exit			;C is clear: we're finished
		LDR	R14,lc__noRedraw	;Load the no-redraw flag
		CMP	R14,#0			;Is it cleared?
		SWINE	OS_Exit			;No -- we're done then

		; --- Initialise as a Wimp task ---

		MOV	R0,#200			;Wimp version number
		LDR	R1,lc__task		;Load the magic number
		ADR	R2,lc__progName		;Point to program name
		SWI	XWimp_Initialise	;Start up as a Wimp app

		; --- Read the screen extents ---

		ADR	R0,lc__vduVars		;Point to VDU variables
		ADR	R1,lc__vduBuff		;And to the output buffer
		SWI	OS_ReadVduVariables	;Read them in please
		LDMIA	R1!,{R1-R4}		;Load the values out now
		ADD	R3,R3,#1		;Increment width
		ADD	R4,R4,#1		;And height
		MOV	R3,R3,LSL R1		;Shift width up to OS units
		MOV	R4,R4,LSL R2		;And the height too

		; --- Now do the redraw ---
		;
		; Note that I don't check for errors here.

		MOV	R0,#-1			;Redraw the whole screen
		MOV	R1,#0			;From the bottom left
		MOV	R2,#0			;So set that up
		SWI	XWimp_ForceRedraw	;Redraw everything

		; --- That's it: I'm done now ---

		SWI	OS_Exit			;That's it, then

lc__noRoom	DCD	1
		DCB	"Not enough memory for LoadConfig to initialise",0

		ALIGN
lc__argDef	DCB	"help/s,noredraw/s,",0

lc__noFile	DCD	1
		DCB	"You must specify a filename",0

lc__tooBig	DCD	1
		DCB	"Too large to be a Sculptrix configuration file",0

lc__notConf	DCD	1
		DCB	"This isn't a Sculptrix configuration file",0

lc__noSculpt	DCD	1
		DCB	"Sculptrix not loaded or too old",0

lc__magic	DCB	"SXCF"

lc__task	DCB	"TASK"

lc__progName	DCB	"LoadConfig",0

lc__vduVars	DCD	4,5,11,12,-1

		LTORG

; --- lc__help ---
;
; On entry:	--
;
; On exit:	--
;
; Use:		Reports a help message and quits.

lc__help	ROUT

		ADR	R0,lc__helpText		;Point to help text
		MOV	R1,#0			;Use system dictionary
		ADR	R2,version		;Point to version string
		SWI	OS_PrettyPrint		;Display the text
		SWI	OS_Exit			;And exit nicely

lc__helpText	DCB	"LoadConfig ",27,0,13
		DCB	13
		DCB	"Syntax: LoadConfig [<options>] <filename>",13
		DCB	13
		DCB	"Options (which may be abbreviated) are:",13
		DCB	13
		DCB	"-help",9,9,"Display this help text",13
		DCB	"-noRedraw",9,"Suppress screen redraw",13
		DCB	0

		LTORG

;----- Workspace layout -----------------------------------------------------

		^	0,R12
lc__wStart	#	0

		; --- VDU variables ---

lc__vduBuff	#	0			;VDU variables output buffer
lc__xEig	#	4			;X EIG factor
lc__yEig	#	4			;Y EIG factor
lc__scrWidth	#	4			;Screen width
lc__scrHeight	#	4			;Screen height

		; --- Arguments read from command line ---

lc__args	#	0			;Argument buffer start
lc__wantHelp	#	4			;Someone wants some help
lc__noRedraw	#	4			;Don't redraw the screen
lc__filename	#	4			;Address of the filename
lc__argBuff	#	256 - 12		;Rest of the argument buffer

		; --- Buffer for loading config files ---

lc__loadBuf	#	0			;Buffer start address
lc__confMagic	#	4			;Magic number on the front
lc__confData	#	252			;Rest of the data

lc__wSize	EQU	{VAR} - lc__wStart

;----- That's all, folks ----------------------------------------------------

		END
