;
; config.s
;
; Configuration interface for Sculptrix
;
;  1995-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.

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

		GET	libs:header
		GET	libs:swis

		GET	libs:stream

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

		GET	sh.wSpace

		GET	sh.messages

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

		AREA	|Module$$Code|,CODE,READONLY

; --- config_set ---
;
; On entry:	R0 == pointer to config buffer
;
; On exit:	CS if config has changed, else CC
;
; Use:		Reads the config buffer into workspace.

		EXPORT	config_set
config_set	ROUT

		STMFD	R13!,{R0-R4,R14}	;Save some registers
		MOV	R2,#0			;Clear some flags

		; --- Copy over the flags ---

		LDRB	R14,[R0],#4		;Load the flags out
		LDR	R1,sculpt_flags		;Load our current flags too
		AND	R3,R1,#&FF		;Get the appearance flags
		CMP	R3,R14			;Is there a change here?
		ORRNE	R2,R2,#1		;Yes -- set the flag
		BICNE	R1,R1,#&FF		;And some of our own
		ORRNE	R14,R14,R1		;Merge the two sets together
		STRNE	R14,sculpt_flags	;And store back in workspace

		; --- Now deal with the colour sets ---

		ADR	R1,sculpt_colours	;Point to the colour tables
		MOV	R3,#4			;There are four to do
00		LDR	R14,[R0],#2		;Load the next pair
		MOV	R14,R14,LSL #16		;Shift up to zero low half
		ORR	R14,R14,R14,LSR #16	;And shift down to copy
		LDR	R4,[R1,#0]		;Load the old word
		CMP	R4,R14			;Do these match?
		ORRNE	R2,R2,#1		;No -- set the flag
		STR	R14,[R1],#4		;Store in my table
		SUBS	R3,R3,#1		;Decrement the counter
		BNE	%b00			;Loop back for some more

		; --- Finally do the other pair ---

		LDR	R14,[R0,#0]		;Load the colours
		LDR	R1,sculpt_hilight	;Load the old ones
		MOV	R1,R1,LSL #16		;Shift up nicely
		CMP	R1,R14,LSL #16		;Do these match?
		ORRNE	R2,R2,#1		;No -- set the flag
		STRNE	R14,sculpt_hilight	;A hack, but it works

		TST	R2,#1			;Did anything change
		LDMFD	R13!,{R0-R4,R14}	;Return when done
		ORRNES	PC,R14,#C_flag
		BICEQS	PC,R14,#C_flag

		LTORG

; --- config_read ---
;
; On entry:	R0 == pointer to a buffer
;		R1 == size of the buffer
;
; On exit:	--
;
; Use:		Writes the config into a buffer

		EXPORT	config_read
config_read	ROUT

		CMP	R1,#cBuff_size		;Is the buffer big enough?
		ADRCCL	R0,msg_errCfgBuff	;No -- find the error
		ORRCCS	PC,R14,#V_flag		;And return it to caller

		STMFD	R13!,{R0-R2,R14}	;Save some registers

		; --- Copy the flags ---

		LDRB	R14,sculpt_flags	;Load our flags
		STR	R14,[R0],#4		;Store in output block

		; --- Copy the colour tables ---

		ADR	R1,sculpt_colours	;Point to the table
		MOV	R2,#4			;There are four to collect
00		LDR	R14,[R1],#4		;Load the next pair
		STRB	R14,[R0],#1		;Store low byte in block
		MOV	R14,R14,LSR #8		;Shift down by a byte
		STRB	R14,[R0],#1		;Store next byte away
		SUBS	R2,R2,#1		;Decrement counter
		BNE	%b00			;And loop back until done

		; --- Finally do the other pair ---

		LDR	R14,sculpt_hilight	;A hack, but it works
		STR	R14,[R0,#0]		;Load the colours

		LDMFD	R13!,{R0-R2,PC}^	;Return when done

		LTORG

; --- config_load ---
;
; On entry:	R0 == pointer to filename
;
; On exit:	--
;
; Use:		Loads a configuration file.

		EXPORT	config_load
config_load	ROUT

		STMFD	R13!,{R14}		;Save some registers
		LDR	R12,[R12,#0]		;Load my workspace pointer

		; --- Find out if the file is suitable ---

		MOV	R1,R0			;Point to the filename
		MOV	R0,#17			;Read file information
		SWI	XOS_File		;Read the information, then
		BVS	%99config_load		;If failed, return the error
		TST	R0,#1			;Is it a real file?
		BEQ	%98config_load		;No -- build and error then

		CMP	R4,#256			;Will it fit in my buffer?
		ADRCSL	R0,msg_errBadConfig	;No -- point to the error
		BCS	%99config_load		;And report the error

		; --- Load the file into my buffer ---

		MOV	R0,#16			;Load the file, please
		ADR	R2,sculpt_misc		;Point to a buffer
		MOV	R3,#0			;Please load it here
		SWI	XOS_File		;Do the file load op
		BVS	%99config_load		;If failed, return the error

		ADR	R0,sculpt_misc		;Point to the buffer
		LDR	R1,[R0],#4		;Load the first word out
		LDR	R14,config__magic	;Load a magic number
		CMP	R1,R14			;Do these match up?
		ADRNEL	R0,msg_errBadConfig	;No -- point to the error
		BNE	%99config_load		;And report the error

		BL	config_set		;Set the configuration
		LDMFD	R13!,{PC}^		;Return when done

		; --- Returning errors ---

98config_load	MOV	R2,R0			;Get the botched object type
		MOV	R0,#19			;Return me a sensible error
		SWI	XOS_File		;Get the error message

99		LDMFD	R13!,{R14}		;Restore the return address
		ORRS	PC,R14,#V_flag		;And return with V set

config__magic	DCB	"SXCF",0

		LTORG

;----- Config buffer structure ----------------------------------------------

		^	0
cBuff_flags	#	4			;Various nice flags
cBuff_stdCols	#	2			;Standard border colours
cBuff_grpCols	#	2			;Group box colours
cBuff_shdCols	#	2			;Shaded border colours
cBuff_shgCols	#	2			;Shaded group colours
cBuff_hilight	#	1			;Highlight colour for default
cBuff_slab	#	1			;Slab background colour
		#	2			;Alignment padding
cBuff_size	#	0

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

		END
