* TypeFinder.s
* Copyright © 1992 SMITHWARE! 07-Feb-92
* by Mark W. Smith



										include		"INCLUDE:exec/types.i"
										include		"INCLUDE:exec/funcdef.i"
										include		"INCLUDE:exec/exec.i"
										include		"INCLUDE:libraries/dos.i"
										include		"INCLUDE:libraries/dosextens.i"
										include		"INCLUDE:intuition/intuition.i"
										include		"INCLUDE:inova/itools1.i"

										include		"INCLUDE:intuition.offsets"

* Library Vector Offsets

* Exec offsets

_AbsExecBase					equ		4

_LVOOpenLibrary				equ		$FFFFFDD8
_LVOWaitPort					equ		$FFFFFE80
_LVOGetMsg						equ		$FFFFFE8C
_LVOReplyMsg					equ		$FFFFFE86
_LVOCloseLibrary			equ		$FFFFFE62
_LVOCopyMemQuick			equ		$FFFFFD8A

* ARP offsets

_LVOOutput						equ		$FFFFFFC4
_LVOOpen							equ		$FFFFFFE2
_LVOClose							equ		$FFFFFFDC
_LVOLock							equ		$FFFFFFAC
_LVOUnLock						equ		$FFFFFFA6
_LVOSeek							equ		$FFFFFFBE
_LVORead							equ		$FFFFFFD6
_LVOWrite							equ		$FFFFFFD0
_LVOCurrentDir				equ		$FFFFFF82
_LVOParentDir					equ		$FFFFFF2E
_LVOAtol							equ		$FFFFFEFE				;ASCII to long int (32-bit)

* Graphics offsets

_LVOSetAPen						equ		$FFFFFEAA
_LVORectFill					equ		$FFFFFECE
_LVOMove							equ		$FFFFFF10
_LVODraw							equ		$FFFFFF0A

* Inovatools offsets

_LVOFlashyOpenWindow	equ		$FFFFFEA4
_LVOFlashyCloseWindow	equ		$FFFFFE9E
_LVOFileName					equ		$FFFFFEB0




* useful equates

TRUE			equ					1
FALSE			equ					0
NULL			equ					0


* macros

PUSH			macro
					move.l			\1,-(sp)
					endm

POP				macro
					move.l			(sp)+,\1
					endm

PUSHM			macro
					movem.l			\1,-(sp)
					endm

POPM			macro
					movem.l			(sp)+,\1
					endm

SEEKPOS		macro																;file, position, mode
					move.l			\1,d1
					move.l			\2,d2
					move.l			\3,d3
					move.l			_ArpBase,a6
					jsr					_LVOSeek(a6)
					endm

READBYTES	macro																;file, buffer, len
					move.l			\1,d1
					move.l			\2,d2
					move.l			\3,d3
					move.l			_ArpBase,a6
					jsr					_LVORead(a6)
					endm

WRITEBYTES	macro																;file,string,len
					move.l			\1,d1
					move.l			\2,d2
					move.l			\3,d3
					move.l			_ArpBase,a6
					jsr					_LVOWrite(a6)
					endm	
	




					section			code


					move.l			sp,InitialSP
					move.l			_AbsExecBase,a6

					moveq.l			#39,d0								;arp lib 39.1 minimum
					movea.l			#ArpName,a1
					jsr					_LVOOpenLibrary(a6)		;open arp.library
					move.l			d0,_ArpBase
					bne					main
					WRITEBYTES	StdOut,#ArpErr,#ArpErrLen
					bra					NoArpLib

main
					move.l			d0,a6
					jsr					_LVOOutput(a6)		;get stdout
					move.l			d0,StdOut

					movea.l			#PSDocuments,a0
					move.l			a0,d1
					move.l			#ACCESS_READ,d2
					jsr					_LVOLock(a6)
					beq					1$
					move.l			d0,d1
					jsr					_LVOCurrentDir(a6)		;cd to PAGESTREAM2:PSDocuments
					move.l			d0,DLock							;if it exists
1$
					moveq.l			#33,d0
					movea.l			#IntName,a1
					move.l			_AbsExecBase,a6
					jsr					_LVOOpenLibrary(a6)		;open intuition.library
					move.l			d0,_IntuitionBase

					moveq.l			#33,d0
					movea.l			#GfxName,a1
					jsr					_LVOOpenLibrary(a6)		;open graphics.library
					move.l			d0,_GraphicsBase

					moveq.l			#1,d0									;inovatools lib v1.1 minimum
					movea.l			#InovaName,a1
					jsr					_LVOOpenLibrary(a6)		;open inovatools1.library
					move.l			d0,_InovaTools1Base
					bne					2$
					WRITEBYTES	StdOut,#InovaErr,#InovaErrLen
					bra					NoInovaLib
2$
					moveq.l			#0,d0
					move.l			d0,RememberKey				;clear before the 1st call!

					move.l			#255,d0
					move.l			#MEMF_PUBLIC,d1
					move.l			RememberKey,a0
					move.l			_IntuitionBase,a6
					jsr					_LVOAllocRemember(a6)	;fast mem requested for filepath
					move.l			d0,FilePathMem

					move.l			#ImgDat1_SIZEOF,d0
					move.l			#MEMF_CHIP,d1
					move.l			RememberKey,a0
					move.l			_IntuitionBase,a6
					jsr					_LVOAllocRemember(a6)	;chip mem needed for icon
					move.l			d0,IconMem

					move.l			d0,a1
					move.l			#ImgDat1_SIZEOF,d0
					movea.l			#ImgDat1,a0
					move.l			_AbsExecBase,a6
					jsr					_LVOCopyMemQuick(a6)	;copy icon image to chip

					move.l			#ImgDat2_SIZEOF,d0		;512 bytes
					move.l			#MEMF_CHIP,d1
					move.l			RememberKey,a0
					move.l			_IntuitionBase,a6
					jsr					_LVOAllocRemember(a6)	;pot leaf needs chip too!
					move.l			d0,PotLeafMem

					move.l			d0,a1
					move.l			#ImgDat2_SIZEOF,d0		;8700 bytes
					movea.l			#ImgDat2,a0
					move.l			_AbsExecBase,a6
					jsr					_LVOCopyMemQuick(a6)	;copy potleaf image to chip

					move.l			#76,d0
					move.l			#MEMF_CHIP,d1
					move.l			RememberKey,a0
					move.l			_IntuitionBase,a6
					jsr					_LVOAllocRemember(a6)	;chip mem for awake pointer
					move.l			d0,AwakePtrMem
					move.l			d0,a1

					move.l			#76,d0
					movea.l			#ImgDat3,a0
					move.l			_AbsExecBase,a6
					jsr					_LVOCopyMemQuick(a6)	;copy the pointer image to chip

					movea.l			#Image1,a0
					move.l			IconMem,ig_ImageData(a0)
					move.l			#Image2,ig_NextImage(a0)	;tell gadget where images are

					movea.l			#Image2,a0
					move.l			PotLeafMem,ig_ImageData(a0)

					movea.l			#Image3,a0
					move.l			AwakePtrMem,ig_ImageData(a0)

					movea.l			#NewWindowStructure1,a0
					move.l			a0,d0
					movea.l			_InovaTools1Base,a6
					jsr					_LVOFlashyOpenWindow(a6)	;'ala MacIntosh open window
					move.l			d0,tfWindow								;save the window ptr
					move.l			d0,a0
					move.l			wd_RPort(a0),tfRPort			;get window's rastport

					movea.l			#Image3,a1
					move.l			ig_ImageData(a1),a1
					moveq.b			#16,d0
					move.b			d0,d1
					moveq.b			#-7,d2
					move.b			d2,d3
					movea.l			_IntuitionBase,a6
					jsr					_LVOSetPointer(a6)

					movea.l			_GraphicsBase,a6
					movea.l			tfRPort,a1								;and save it too

					moveq.l			#2,d0											;fg pen set to black
					jsr					_LVOSetAPen(a6)
					moveq.l			#13,d2
3$
					moveq.l			#10,d0										;this loop draws the horiz
					move.l			d2,d1											;lines of the boxes
					jsr					_LVOMove(a6)
					move.l			#200,d0
					jsr					_LVODraw(a6)
					addi.l			#12,d2
					cmp.l				#157,d2
					ble					3$

					moveq.l			#0,d0
					move.l			#176,d1
					jsr					_LVOMove(a6)
					move.l			#639,d0
					jsr					_LVODraw(a6)							;draw the bottom horiz line

					move.l			#10,d0
					moveq.l			#13,d1
					jsr					_LVOMove(a6)
					move.l			#157,d1
					jsr					_LVODraw(a6)							;draw left side of boxes

					move.l			#200,d0
					moveq.l			#13,d1
					jsr					_LVOMove(a6)
					move.l			#157,d1
					jsr					_LVODraw(a6)							;draw right side of boxes

					movea.l			tfRPort,a0
					movea.l			#Status,a1
					moveq.l			#0,d0
					moveq.l			#0,d1
					move.l			_IntuitionBase,a6
					jsr					_LVOPrintIText(a6)				;tell user we're initializing

					move.l			#tfdatName,d1
					move.w			#MODE_OLDFILE,d2
					move.l			_ArpBase,a6
					jsr					_LVOOpen(a6)							;open s:tf.dat, our font list
					tst.l				d0
					beq					NoTFdat
					move.l			d0,tfdatfh								;save a filehandle to it
					moveq.l			#0,d4
4$
					READBYTES		tfdatfh,#ch,#1
					tst.b				d0
					beq					5$
					move.b			ch,d0
					cmpi.b			#$0a,d0
					bne					4$
					addq.w			#1,d4										;count number of fonts we know
					bra					4$
5$
					move.l			#NumTypefaces,a0
					move.w			d4,(a0)
					move.l			d4,d0
					muls				#28,d0										;numfonts * 28 bytes each
					move.l			#MEMF_PUBLIC,d1
					move.l			RememberKey,a0
					move.l			_IntuitionBase,a6
					jsr					_LVOAllocRemember(a6)			;get mem for it
					move.l			d0,tfdatMem								;and save the address

					move.l			d0,a1
					PUSH				a1
					SEEKPOS			tfdatfh,#NULL,#OFFSET_BEGINNING	;start of s:tf.dat file
					POP					a1

6$
					PUSH				a1
					READBYTES		tfdatfh,#buffer,#9			;get id code for typeface
					tst.b				d0
					beq					10$
					movea.l			#buffer,a0
					move.l			_ArpBase,a6
					jsr					_LVOAtol(a6)						;convert id code to int
					POP					a1
					move.l			d0,(a1)+
					moveq.b			#0,d0										;separate id and name w/ NULL
					move.b			d0,(a1)+
					moveq.b			#22,d1									;len of typeface name field
					move.b			d1,d2										;max len of name
7$
					PUSHM				a1/d1-d2
					READBYTES		tfdatfh,#ch,#1					;get typeface name
					POPM				a1/d1-d2
					move.b			ch,d0
					cmpi.b			#$0a,d0									;end of line?
					beq					8$
					tst.b				d2											;has 22 chars been exceeded?
					ble					7$											;yep, ignore the comments
					move.b			d0,(a1)+								;no, copy typename then
					subq.b			#1,d1
					subq.b			#1,d2
					bra					7$
8$
					tst.b				d1
					beq					9$
					move.b			#' ',(a1)+							;expand field w/ spaces
					subq.b			#1,d1
					bra					8$
9$
					moveq.b			#0,d0										;add NULL to name to make string
					move.b			d0,(a1)+
					bra					6$
10$
					POP					a1
					move.l			tfdatfh,d1
					move.l			_ArpBase,a6
					jsr					_LVOClose(a6)						;close our font list s:tf.dat

					movea.l			tfRPort,a0
					movea.l			#IText1,a1
					moveq.l			#0,d0
					moveq.l			#0,d1
					move.l			_IntuitionBase,a6
					jsr					_LVOPrintIText(a6)				;tell user we're initializing





ExecLoop
					movea.l			tfWindow,a0
					move.l			_IntuitionBase,a6
					jsr					_LVOClearPointer(a6)
					bsr					ClearStatus
					move.l			a1,a0												;RPort to a0
					movea.l			#Status,a1									;tell user we're now
					move.l			#ReadyPrompt,it_IText(a1)		;done with initialization
					moveq.l			#0,d0
					moveq.l			#0,d1
					move.l			_IntuitionBase,a6
					jsr					_LVOPrintIText(a6)
1$
					movea.l			_AbsExecBase,a6
					movea.l			tfWindow,a0
					movea.l			wd_UserPort(a0),a0
					PUSH				a0
					jsr					_LVOWaitPort(a6)
					POP					a0
					jsr					_LVOGetMsg(a6)
					move.l			d0,a1
					move.l			im_Class(a1),d2
					move.w			im_Code(a1),d3
					jsr					_LVOReplyMsg(a6)
					cmp.l				#CLOSEWINDOW,d2				;user wants quit program
					beq					Adios
					cmp.l				#GADGETUP,d2					;user wants document opened
					bne					1$										;msg none of our business

					move.l			FilePathMem,a0
					move.l			a0,d0
					movea.l			#FileReqTitle,a1
					move.l			a1,d1
					moveq.l			#0,d2
					moveq.l			#0,d3
					move.l			#FILES_OPEN,d4
					move.l			#375,d5
					moveq.l			#25,d6
					movea.l			_InovaTools1Base,a6
					jsr					_LVOFileName(a6)			;req for user filename
					move.l			FilePathMem,a0				;get the full path to it
					tst.b				(a0)
					beq					1$										;no path or user selected CANCEL

					bsr					ClearBoxes
					bsr					ClearFilename
					move.l			a1,a0
					movea.l			#IText0,a1
					move.l			FilePathMem,it_IText(a1)
					moveq.l			#0,d0
					moveq.l			#0,d1
					move.l			_IntuitionBase,a6
					jsr					_LVOPrintIText(a6)			;write filename to screen

					move.l			FilePathMem,a0
					move.l			a0,d1
					move.w			#MODE_OLDFILE,d2
					move.l			_ArpBase,a6
					jsr					_LVOOpen(a6)						;open the user's file request
					tst.l				d0
					beq					ExecLoop
					move.l			d0,userfh								;save the filehandle to close

					bsr					ClearStatus
					move.l			a1,a0
					movea.l			#Status,a1
					move.l			#SearchPrompt,it_IText(a1)
					moveq.l			#0,d0
					moveq.l			#0,d1
					move.l			_IntuitionBase,a6
					jsr					_LVOPrintIText(a6)			;tell user we're looking

					movea.l			#Image3,a0
					move.l			AwakePtrMem,ig_ImageData(a0)
					movea.l			tfWindow,a0

					movea.l			#Image3,a1
					move.l			ig_ImageData(a1),a1
					moveq.b			#16,d0
					move.b			d0,d1
					moveq.b			#-7,d2
					move.b			d2,d3
					movea.l			_IntuitionBase,a6
					jsr					_LVOSetPointer(a6)


CompareTypes
					READBYTES		userfh,#ch,#1
					tst.b				d0
					beq					CloseUserFile
					move.b			ch,d0
					cmpi.b			#0,d0								;wait for $00 char
					bne					CompareTypes
					READBYTES		userfh,#ch,#1
					tst.b				d0
					beq					CloseUserFile
					move.b			ch,d0
					cmpi.b			#$20,d0							;$0020 is PGS flag for font change
					bne					CompareTypes
					READBYTES		userfh,#idcode,#4
					cmp.b				#4,d0
					bne					CloseUserFile
					move.l			#idcode,d0					;maybe a font!
					move.l			#NumTypefaces,a0
					move.w			(a0),d1
					move.l			tfdatMem,a0					;get data table of typeface codes
1$
					tst.w				d1									;have all fonts been searched?
					beq					CompareTypes
					subq.w			#1,d1								;one less font to compare to
					move.l			a0,d3								;save a0 ptr
					move.l			d0,a1
					move.l			#3,d4								;counter for db loop 2$
2$
					cmpm.b			(a0)+,(a1)+					;compare the idcodes
					dbne				d4,2$
					cmpi.b			#-1,d4
					beq					3$									;we know this font, go flag it

					addi.l			#24,d4
					add.l				d4,a0								;move pointer to next in our list
					bra					1$									;and go compare this one to it
3$
					move.l			d3,a0								;back up to start of idcode
					or.l				#$80000000,(a0)			;set bit so we recognize it later
					bra					CompareTypes				;and go compare this one to it

CloseUserFile
					move.l			userfh,d1
					move.l			_ArpBase,a6
					jsr					_LVOClose(a6)				;close the user's file



ShowTypes
					move.b			#12,d2							;only 12 fonts/document! too many!
					move.l			#16,d3							;TopEdge for first font name
					move.l			#NumTypefaces,a0
					move.w			(a0),d4
					move.l			tfdatMem,a0					;get our font list s:tf.dat
1$
					tst.w				d4									;end of our font list?
					beq					ExecLoop
					tst.b				d2									;max of 12 fonts reached?
					ble					3$
					move.l			(a0),d1
					cmpi.l			#$80000000,d1				;is it a flagged font?
					bpl					2$
					adda.l			#28,a0
					subq.w			#1,d4								;one less font to compare
					bra					1$

2$
					and.l				#$7FFFFFFF,d1				;unflag it
					move.l			d1,(a0)
					subq.b			#1,d2								;one box less to fill
					PUSHM				a0/d1-d4
					moveq.l			#1,d0
					movea.l			tfRPort,a1
					move.l			_GraphicsBase,a6
					jsr					_LVOSetAPen(a6)			;fg pen set to white
					POPM				a0/d1-d4

					addq.l			#5,a0
					movea.l			#Typeface,a1				;get our intuitext structure
					move.w			d3,it_TopEdge(a1)		;the box for current font name
					move.l			a0,it_IText(a1)			;name of current font
					PUSHM				a0/d1-d4
					moveq.l			#0,d0
					moveq.l			#0,d1
					movea.l			tfRPort,a0
					move.l			_IntuitionBase,a6
					jsr					_LVOPrintIText(a6)	;print the font found!

					POPM				a0/d1-d4
					addi.b			#12,d3							;move name's TopEdge to next slot
					adda.l			#23,a0							;move ptr to next idcode in list
					bra					1$
3$
					tst.w				d4
					beq					ExecLoop
					move.l			(a0),d1
					cmpi.l			#$80000000,d1				;is it a flagged font?
					bpl					4$									;changed from bgt - the bug ?
					adda.l			#28,a0
					subq.w			#1,d4								;one less font to reset
					bra					3$
4$
					and.l				#$7FFFFFFF,d1
					move.l			d1,(a0)
					bra					3$

NoTFdat
					WRITEBYTES	StdOut,#tfdatErr,#tfdatErrLen
Adios
					movea.l			tfWindow,a0
					move.l			a0,d0
					movea.l			_InovaTools1Base,a6
					jsr					_LVOFlashyCloseWindow(a6)

					move.l			#TRUE,d0
					move.l			RememberKey,a0
					move.l			_IntuitionBase,a6
					jsr					_LVOFreeRemember(a6)

					move.l			DLock,d1
					move.l			_ArpBase,a6
					jsr					_LVOUnLock(a6)

					move.l			_ArpBase,a1
					move.l			_AbsExecBase,a6
					jsr					_LVOCloseLibrary(a6)
NoArpLib
					move.l			_InovaTools1Base,a1
					move.l			_AbsExecBase,a6
					jsr					_LVOCloseLibrary(a6)

NoInovaLib
					move.l			_GraphicsBase,a1
					move.l			_AbsExecBase,a6
					jsr					_LVOCloseLibrary(a6)

					move.l			_IntuitionBase,a1
					move.l			_AbsExecBase,a6
					jsr					_LVOCloseLibrary(a6)

					moveq.l			#0,d0
					move.l			InitialSP,sp
					rts

* end of main code







* SUBROUTINES here

ClearStatus
					moveq.l			#0,d0
					movea.l			tfRPort,a1
					movea.l			_GraphicsBase,a6
					jsr					_LVOSetAPen(a6)				;fg pen set to bg color

					moveq.l			#10,d0
					move.l			#163,d1
					move.l			#157,d2
					move.l			#173,d3
					movea.l			tfRPort,a1
					jsr					_LVORectFill(a6)			;fill area with bg color


					moveq.l			#1,d0
					movea.l			tfRPort,a1
					jsr					_LVOSetAPen(a6)				;reset fg pen to white

					rts




ClearFilename
					moveq.l			#0,d0									;fg pen set to bg color
					movea.l			tfRPort,a1
					movea.l			_GraphicsBase,a6
					jsr					_LVOSetAPen(a6)

					moveq.l			#3,d0
					move.l			#177,d1
					move.l			#636,d2
					move.l			#186,d3
					movea.l			tfRPort,a1
					jsr					_LVORectFill(a6)			;fill area with bg color

					moveq.l			#3,d0
					movea.l			tfRPort,a1
					jsr					_LVOSetAPen(a6)				;reset fg pen to red

					rts


ClearBoxes
					moveq.l			#0,d0									;fg pen set to blue
					movea.l			tfRPort,a1
					movea.l			_GraphicsBase,a6
					jsr					_LVOSetAPen(a6)

					moveq.l			#14,d0								;xmin left edge - constant
					moveq.l			#15,d1								;ymin top edge - variable
					move.l			#197,d2								;xmax right edge - constant
					move.l			#12,d4								;num of boxes to fill
1$
					move.l			d1,d3
					addq.l			#8,d3									;ymax = ymin + 10
					PUSHM				a1/d0-d4
					jsr					_LVORectFill(a6)			;fill area with bg color
					POPM				a1/d0-d4
					addi.l			#12,d1
					subq.l			#1,d4
					bne					1$
					rts


MouseAsleep
					move.l			AwakePtrMem,a1
					move.l			#76,d0
					movea.l			#ImgDat3,a0
					move.l			_AbsExecBase,a6
					jsr					_LVOCopyMemQuick(a6)	;copy the image to chip

					movea.l			#Image3,a0
					move.l			AwakePtrMem,ig_ImageData(a0)

					movea.l			#Image3,a1
					move.l			ig_ImageData(a1),a1
					moveq.b			#16,d0
					move.b			d0,d1
					moveq.b			#-7,d2
					move.b			d2,d3
					movea.l			_IntuitionBase,a6
					jsr					_LVOSetPointer(a6)
					rts






								section			bss

InitialSP					dc.l			0
_IntuitionBase		dc.l			0
_GraphicsBase			dc.l			0
_ArpBase					dc.l			0
_InovaTools1Base	dc.l			0
tfWindow					dc.l			0
tfRPort						dc.l			0
RememberKey				dc.l			0
IconMem						dc.l			0
PotLeafMem				dc.l			0
FilePathMem				dc.l			0
tfdatMem					dc.l			0
AwakePtrMem				dc.l			0
AsleepPtrMem			dc.l			0
NumTypefaces			dc.l			0

StdOut						ds.l			1		;output file handle
ch 								ds.l			1		;one char at a time for Read
idcode						ds.l			1		;font idcode from document
buffer						ds.b			9
DLock 					 	ds.l			1		;PAGESTREAM2:PSDocuments dir lock
tfdatfh						ds.l			1		;s:tf.dat file handle
userfh						ds.l			1		;pagestream doc file handle





								section			data

IntName					dc.b				'intuition.library',0
								cnop				0,2
GfxName					dc.b				'graphics.library',0
								cnop				0,2
InovaName				dc.b				'inovatools1.library',0
								cnop				0,2
ArpName					dc.b				'arp.library',0
								cnop				0,2
PSDocuments			dc.b				'PAGESTREAM2:PSDocuments',0
								cnop				0,2							
FileReqTitle		dc.b				'Select a PAGESTREAM document:',0
								cnop				0,2
tfdatName				dc.b				's:tf.dat',0
								cnop				0,2
ReadyPrompt			dc.b				'Ready...',0
								cnop				0,2
SearchPrompt		dc.b				'Searching...',0
								cnop				0,2
tfdatErr				dc.b				'S:tf.dat not found',10,0
								cnop				0,2
tfdatErrLen			equ					*-tfdatErr
ArpErr					dc.b				'arp.library not found or < v39.x',10,0
								cnop				0,2
ArpErrLen				equ					*-ArpErr
InovaErr				dc.b				'inovatools1.library not found or < v1.x',10,0
								cnop				0,2
InovaErrLen			equ					*-InovaErr


								include			"INCLUDE:user/TypeFinder.i"


								end

