; ***** BEGIN LICENSE BLOCK *****
; Version: MPL 1.1
;
; The contents of this file are subject to the Mozilla Public License Version
; 1.1 (the "License"); you may not use this file except in compliance with
; the License. You may obtain a copy of the License at
; http://www.mozilla.org/MPL/
;
; Software distributed under the License is distributed on an "AS IS" basis,
; WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
; for the specific language governing rights and limitations under the
; License.
;
; The Original Code is the Printers+ front-end.
;
; The Initial Developer of the Printers front-end is Acorn Computers Ltd.
; The Initial Developer of the Printers+ extensions is RISCOS Ltd.
;
; Portions created by the Initial Developers are Copyright (C) 2003
; the Initial Developers. All Rights Reserved.
;
; Contributor(s):
;
; ***** END LICENSE BLOCK ***** */

; Bodge around Printer: in SystemDevs. Need to do the following:
;
; Set a code variable (Printer$Path) which returns a value for the fx5,n
;
; Won't behave quite properly as printers without a PrinterType$N will print
; to null:

	GET     s.Proc
	GET	s.Macros

	AREA	SetPrint,CODE,READONLY
	ENTRY
; Set up constants for SWIs

OSByte_ReadPrinterDestination * 245

X_Bit * &20000
XOS_SetVarVal * &24+X_Bit
XOS_ReadVarVal * &23+X_Bit
XOS_ReadUnsigned * &21+X_Bit
XOS_Byte * &06+X_Bit
XOS_ConvertCardinal1 * &D5+X_Bit

VarType_Code * 16
VarType_Expanded * 3

Start
        ROUT
        ADR     r0, PrintVarName
        ADR     r1, PrintVarCode
        LDR     r2, =PrintVarCodeEnd - PrintVarCode
        MOV     r3, #0
        MOV     r4, #VarType_Code
        SWI     XOS_SetVarVal
        MOV     pc, lr

        LTORG

PrintVarCode
        MOV     pc, lr          ; Write
VarRead Entry                   ; Read

        BL      SussSpecialPrinter

        BL      SussPrinterVar

        EXIT

PrintVarName    DCB     "Printer$Path",0
SpecialVarName  DCB     "FileSwitch$SpecialField",0
nullplace       DCB     "null:"
        DCB     0
        ALIGN

;
; Returns r0=printer type to use (number)
;
SussSpecialPrinter Entry "r1-r4"

        ADR     r0, SpecialVarName
        ADR     r1, VarValBuffer
        MOV     r2, #?VarValBuffer
        MOV     r3, #0
        MOV     r4, #3
        SWI     XOS_ReadVarVal
        BVS     SpecialNotThere
        TEQ     r2, #0
        BEQ     SpecialNotThere

        MOV     lr, #0
        STRB    lr, [r1, r2]            ; terminate 'cos not already

        LDRB    lr, [r1]                ; Is it numeric ?
        TEQ     lr, #"&"
        BEQ     SpecialDuff
        CMP     lr, #"0"
        RSBHSS  lr, lr, #"9"
        BHS     SpecialNumeric

; Special field is non-numeric

        ADR     r0, printer_Types       ; Point to first name in list

10      MOV     r2, r1                  ; Restore search name each time

15      LDRB    lr, [r0], #1            ; Get char from stored name
        TEQ     lr, #&FF                ; Ended all stored names ?
        BEQ     SpecialDuff
        LDRB    r3, [r2], #1            ; Get char from special buffer
        CMP     lr, #" "                ; Ended both names together ?
        CMPLS   r3, #0
        BLS     %FT30                   ; Found it if so
        LowerCase r3, r4                ; Name stored Lowercased
        CMP     lr, r3                  ; Loop if still matching
        BEQ     %BT15

        SUB     r0, r0, #1              ; Point back one char in stored name
20      LDRB    lr, [r0], #1            ; Skip to start of next stored name
        CMP     lr, #" "
        BHI     %BT20
        B       %BT10                   ; r0 -> next name to try

30
        MOV     r0, lr
        EXIT


SpecialNumeric
        MOV     r0, #10 + (2_110 :SHL: 29) ; No bad terms, restrict to byte
        SWI     XOS_ReadUnsigned
        MOVVC   r0, r2
        EXIT    VC

SpecialDuff
        MOV     r0, #0
        EXIT

SpecialNotThere
        MOV     r0, #OSByte_ReadPrinterDestination
        MOV     r1, #0
        MOV     r2, #&FF
        SWI     XOS_Byte
        MOV     r0, r1                  ; r0 => printer type

        EXIT

printer_Types ; Allow expansion (may need extra synonyms, for instance)

        DCB     "null",       0
        DCB     "sink",       0
        DCB     "parallel",   1
        DCB     "centronics", 1
        DCB     "rs423",      2
        DCB     "serial",     2
        DCB     "user",       3

; no printer#net: - use netprint: instead!

        DCB     &FF             ; End of table
        ALIGN

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; SussPrinterVar.
;
; This routine will suss the given printer variable,, eg. it will look
; for a printer variable for the current printer destination.  Once it
; has found this any routine interested in performing anytype of
; output should then indirect to the returned filename.
;
; in:   r0 = printer type to try
; out:  r0 = pointer to value
;       r1 corrupted
;       r2 = length of value
;

SussPrinterVar Entry    "r3,r4"

        ADR     r1, varsuffixplace
10      MOV     r2, #?varsuffixplace
        SWI     XOS_ConvertCardinal1

        ADRVC   r0, varprefix           ; r0 -> var in buffer
        ADRVC   r1, VarValBuffer        ; r1 -> return buffer
        MOVVC   r2, #?VarValBuffer      ; r2  = size of buffer
        MOVVC   r3, #0                  ; r3  = 0, first read
        MOVVC   r4, #VarType_Expanded
        SWIVC   XOS_ReadVarVal

        MOVVC   r0, r1
        ADRVSL  r0, nullplace
        MOVVS   r2, #?nullplace

        EXIT

varprefix
        = "PrinterType$"
varsuffixplace % 8

VarValBuffer % 256

        ALIGN
PrintVarCodeEnd

        ALIGN
        END
