; SizeWin use MoveWindow, SizeWindow from CLI/Shell
; (c) 1992 by Michael Watzl
;
; written with DEVPAC


*	LVOs

OpenLib		= -552
Output		= -60
Write		= -48
CloseLib	= -414
MoveWin		= -168
SizeWin		= -288


*	some Offsets:

activewin	= $34
winleft		= 4
wintop		= 6
winwidth	= 8
winheight	= 10

_main	tst.l	d0
	cmpi.l	#1,d0		; more than 1 arg?
	beq.s	usage
	bsr	parse		; parse the args
				; and save them

	move	d0,d7		; d7=Height
	move	d1,d6		; d6=Width
	move	d2,d5		; d5=yPos
	move	d3,d4		; d4=xPos

	bsr	openi
	bsr.s	calc		; calc deltas for move/size window!

	movea.l	_IntuitionBase,a6
	movea.l	activewin(a6),a0
	movea.l	a0,a4		; save ActiveWindow

	move	d6,d0		; delta width
	move	d7,d1		; delta height
	jsr	SizeWin(a6)

	movea.l	a4,a0		; -> ActiveWindow

	move	d4,d0		; delta	x
	move	d5,d1		; delta y
	jsr	MoveWin(a6)

closei	move.l	_IntuitionBase,a1
	move.l	4.w,a6
	jsr	CloseLib(a6)
quit	rts

calc	movea.l	_IntuitionBase,a6
	movea.l	activewin(a6),a0

	sub.w	winheight(a0),d7
	sub.w	winwidth(a0),d6

	sub.w	wintop(a0),d5
	sub.w	winleft(a0),d4
	rts

usage	bsr	openi
	bsr	toasc
	lea	usgtxt,a0
	bsr.s	_print
	bsr 	closei
	moveq	#5,d0
	rts

error	lea	errtxt,a0
	bsr.s	_print
	moveq	#10,d0
	rts

_print	movem.l	R0-R15,-(sp)
	move.l	a0,d5		; save pointers
	lea	DosName,a1
	move.l	4.w,a6
	moveq	#0,d0
	jsr	OpenLib(a6)	; open dos.library
	tst.l	d0
	beq.s	openerr
	move.l	d0,_DosBase
	move.l	d0,a6
	jsr	Output(a6)	; get Output_handle
	tst.l	d0
	beq.s	outerr
	move.l	d0,_Output
	movea.l	d5,a0
	move.l	a0,d2		; save->Write
	moveq.b	#0,d3		; counter for length
.loop	addq.b	#1,d3
	cmp.b	#-1,(a0)+	; end of string?
	bne.s	.loop
	subq	#1,d3
	move.l	_Output,d1
	move.l	_DosBase,a6
	jsr	Write(a6)

outerr	move.l	_DosBase,a1
	move.l	4.w,a6
	jsr	CloseLib(a6)
openerr	movem.l	(sp)+,R0-R15
	rts

parse	moveq	#1,d2		; counter for args
	movea.l	a0,a4		; save pointer
loop	cmpi.b	#$20,(a4)	; next arg?
	beq.s	incr		; increase counter
	cmpi.b	#$0a,(a4)	; any more args?
	beq.s	stop
	cmpi.b	#'9',(a4)	; ASCII-value > '9'
	bhi.s	Gousage
	cmpi.b	#'0',(a4)+	; ASCII-value < '0'
	blo.s	Gousage
	bra.s	loop

incr	addq	#1,d2
	cmpi.b	#5,d2
	beq.s	Gousage
	adda.l	#1,a4
	bra.s	loop
stop	cmpi.b	#4,d2
	bne.s	Gousage

* Start PARSING
	bsr.s	getarg	; arg 4
	move.w	d6,d0
	bsr.s	getarg	; arg 3
	move.w	d6,d1
	bsr.s	getarg	; arg 2
	move.w	d6,d2
	bsr.s	getarg	; arg 1
	move.w	d6,d3
_ende	rts

Gousage	addq	#4,sp		; kill the adress for rts
	bra	usage

; 	interprets a argumentlist from back to front

getarg	moveq	#1,d4		; value of digit
	moveq	#0,d5		; temporary (für mulu)
	moveq	#0,d6		; result

.loop	cmpa.l	a0,a4
	beq.s	next		; last one?
	cmpi.b	#$20,-(a4)
	beq.s	next		; next ARg?
	sub.b	#48,(a4)	; ASCII -> register
	move.b	(a4),d5
	mulu	d4,d5
	add.l	d5,d6
	mulu	#10,d4		; increase digit value
	bra.s	.loop
	rts
next	rts

; 	 changes LeftEdge, Top... to asc and writes them into usage.txt

toasc	move.l	_IntuitionBase,a6	, init
	move.l	activewin(a6),a0

	move.l	winleft(a0),xposR	; read all 4 vals
	move.l	winwidth(a0),xposR+4

	moveq	#3,d6			; counter for 4 registers 0-3
	moveq	#3,d5			; counter for 4 word-digits
	moveq	#0,d0

	lea	xposR,a0		; Raw for ARI
	lea	asc+11,a1		; Temporary ascii

zahlen	move.l	#1000,d4		; load d4 with highest divisor
	asl	#1,d6			; double offset (word = 2 byte)
	move	(a0,d6.w),d0		; INTEGER
	asr	#1,d6

ziffern	divu	d4,d0
	move	d0,(a1,d5.w)		; write result
	tst.b	d0
	addi	#48,(a1,d5.w)		; and change to ascii
	swap	d0			; load the rest
	andi.l	#$0000ffff,d0		; kill rubish
	divu	#10,d4
	dbf	d5,ziffern
	moveq	#3,d5
	suba.l	#4,a1
	dbf	d6,zahlen

	moveq	#3,d0		; exchange: 4321 -> 1234
	lea	asc,a0
.loop	move.b	(a0),d1		; 4xx1 -> 1xx4
	move.b	3(a0),(a0)
	move.b	d1,3(a0)
	move.b	1(a0),d1	; x32x -> x23x
	move.b	2(a0),1(a0)
	move.b	d1,2(a0)
	addq	#4,a0
	dbf	d0,.loop

	move.l	asc,xpos	; write into the usagetxt
	move.l	asc+4,ypos
	move.l	asc+8,width
	move.l	asc+12,height

	rts

openi	moveq	#0,d0		; open intuition.library
	move.l	4.w,a6
	lea	IntName,a1
	jsr	OpenLib(a6)
	tst.l	d0
	beq.s	goerror
	move.l	d0,_IntuitionBase
	rts

goerror	addq	#4,sp			; kill address for rts
	bra	error			; and jump to "error"

IntName	dc.b	"intuition.library",0
DosName	dc.b	"dos.library",0
_DosBase	dc.l	0
_IntuitionBase	dc.l	0
_Output		dc.l	0

usgtxt	dc.b	"Usage:	",27,"[32mNewPos ",27,"[31m xpos ypos width height"
	dc.b	10,"	to place your window and size it"
	dc.b	10,"	(c) 1992 by Michael Watzl",10
	dc.b	10,"	Current Window: ",10,27,"[32m	LeftEdge: "
xpos	dc.b	0,0,0,0," Topedge: "
ypos	dc.b	0,0,0,0," Width: "
width	dc.b	0,0,0,0," Height:  "
height	dc.b	0,0,0,0,10,27,"[31m",-1

errtxt	dc.b	"Couldn't open intuition.library",10,-1

xposR	dc.w	0
yposR	dc.w	0
widthR	dc.w	0
heightR	dc.w	0
asc	ds.l	4	; 4*4 Bytes!
