*
*	WinSize - a window sizing utility
*
*	Copyright (c) 1987 by Limited Reality, Inc.
*
*	Author:	Jim Cooper

* Includes
* ------------------------------------------------------------------------
	include "exec/types.i"
	include	"exec/exec_offsets.i"
	include "intuition/intuition.i"
	include	"libraries/dos_offsets.i"
	include	"intuition/intuition_offsets.i"

	ifd	CAPE
	EXEOBJ
	objfile	'ram:WinSize'
	endc

* Stack frame offsets
* ------------------------------------------------------------------------
count		EQU	-128
IntBase 	EQU	-124
DOSBase 	EQU	-120
stdout		EQU	-116
left		EQU	-112
top		EQU	-108
width		EQU	-104
height		EQU	-100
cmdline 	EQU	-96
buffer		EQU	-92

* On with the Program!
* ------------------------------------------------------------------------
WinSize:
	link	a4,#-128
	move.l	#$0fffffff,d0
	move.l	d0,left(a4)
	move.l	d0,top(a4)
	move.l	d0,width(a4)
	move.l	d0,height(a4)
	move.l	a0,cmdline(a4)
	lea.l	DOSName(PC),a1
	move.l	#LIBRARY_VERSION,d0
	move.l	_AbsExecBase,a6
	Call	OpenLibrary
	move.l	d0,DOSBase(a4)
	move.l	d0,a6
	Call	Output
	move.l	d0,stdout(a4)
	move.l	cmdline(a4),a0
	cmpi.b	#'?',(a0)
	bne.s	NoHelp
	move.l	stdout(a4),d1
	move.l	#Help,d2
	move.l	#HelpLen,d3
	Call	Write
	unlk	a4
	rts
NoHelp:
	move.l	#IntName,a1
	moveq	#0,d0
	move.l	_AbsExecBase,a6
	Call	OpenLibrary
	move.l	d0,IntBase(a4)
	bne.s	IntOk
	move.l	stdout(a4),d1
	move.l	#NoInt,d2
	move.l	#NoIntLen,d3
	move.l	DOSBase(a4),a6
	Call	Write
	unlk	a4
	rts
IntOk:
	move.l	cmdline(a4),a0
	moveq	#0,d0
	move.b	(a0),d0
	andi.b	#95,d0
	cmpi.b	#'S',d0
	bne.s	NoShow
	move.l	IntBase(a4),a5
	move.l	ib_ActiveWindow(a5),a0
	move.w	wd_LeftEdge(a0),d0
	move.l	d0,left(a4)
	move.w	wd_TopEdge(a0),d0
	move.l	d0,top(a4)
	move.w	wd_Width(a0),d0
	move.l	d0,width(a4)
	move.w	wd_Height(a0),d0
	move.l	d0,height(a4)
	lea	Format(pc),a0
	lea	left(a4),a1
	lea	store(pc),a2
	lea	buffer(a4),a3
	Call	RawDoFmt
	bsr	Print
	unlk	a4
	rts
NoShow:
	cmpi.b	#' ',(a0)
	blt.s	RangeChk
	bsr	atoi
	move.l	d0,left(a4)
	cmpi.b	#' ',d1
	blt.s	Arrggh
	bsr	atoi
	move.l	d0,top(a4)
	cmpi.b	#' ',d1
	blt.s	Arrggh
	bsr	atoi
	move.l	d0,width(a4)
	cmpi.b	#' ',d1
	blt.s	Arrggh
	bsr	atoi
	move.l	d0,height(a4)
	bra.s	RangeChk
Arrggh:
	move.l	stdout(a4),d1
	move.l	#BadArgs,d2
	move.l	#BadArgLen,d3
	move.l	DOSBase(a4),a6
	Call	Write
	unlk	a4
	rts
RangeChk:
	move.l	IntBase(a4),a5
	move.l	ib_ActiveWindow(a5),a0
	move.l	ib_ActiveScreen(a5),a5
	moveq	#0,d0
	move.w	sc_Width(a5),d0
	move.l	width(a4),d1
	cmp.l	d0,d1
	ble.s	RC1
	move.l	d0,width(a4)
	move.l	d0,d1
RC1:
	move.w	wd_MaxWidth(a0),d0
	cmp.l	d0,d1
	bls.s	RC2
	move.w	d1,wd_MaxWidth(a0)
RC2:
	move.w	wd_MinWidth(a0),d0
	cmp.l	d0,d1
	bge.s	RC3
	move.l	d0,width(a4)
RC3:
	move.l	height(a4),d1
	move.w	sc_Height(a5),d0
	cmp.l	d0,d1
	ble.s	RC4
	move.l	d0,height(a4)
	move.l	d0,d1
RC4:
	move.w	wd_MaxHeight(a0),d0
	cmp.l	d0,d1
	bls.s	RC5
	move.w	d1,wd_MaxHeight(a0)
RC5:
	move.w	wd_MinHeight(a0),d0
	cmp.l	d0,d1
	bge.s	RC6
	move.l	d0,height(a4)
RC6:
	move.l	left(a4),d1
	move.l	width(a4),d2
	move.w	sc_Width(a5),d0
	sub.l	d2,d0
	cmp.l	d0,d1
	ble.s	RC7
	move.l	d0,left(a4)
RC7:
	move.l	top(a4),d1
	move.l	height(a4),d2
	moveq	#0,d0
	move.w	sc_Height(a5),d0
	sub.l	d2,d0
	cmp.l	d0,d1
	ble.s	DoIt
	move.l	d0,top(a4)
DoIt:
	move.l	IntBase(a4),a6
	move.l	left(a4),d1
	move.w	wd_LeftEdge(a0),d0
	cmp.l	d0,d1
	blt.s	MoveFirst
	move.l	top(a4),d1
	move.w	wd_TopEdge(a0),d0
	cmp.l	d0,d1
	bge.s	SizeFirst
MoveFirst:
	bsr	MoveIt
	bsr.s	SizeIt
	bra.s	DoDisp
SizeFirst:
	bsr.s	SizeIt
	bsr	MoveIt
DoDisp:
	Call	RethinkDisplay
	move.l	a6,a1
	move.l	_AbsExecBase,a6
	Call	CloseLibrary
	move.l	DOSBase(a4),a1
	Call	CloseLibrary
	unlk	a4
	rts

* Subroutines
* -----------------------------------------------------------------------
atoi:
	moveq	#0,d0
	move.l	d0,d1
asplp:
	move.b	(a0),d1
	cmpi.b	#' ',d1
	bne.s	aloop
	addq.l	#1,a0
	bra.s	asplp
aloop:
	move.b	(a0)+,d1
	beq.s	afini
	cmpi.b	#'-',d1
	beq.s	aloop
	cmpi.b	#'0',d1
	blt.s	afini
	cmpi.b	#'9',d1
	bgt.s	afini
	subi.b	#'0',d1
	mulu	#10,d0
	add.l	d1,d0
	bra.s	aloop
afini:
	andi.l	#$0fffffff,d0		; "fix" for max sizes, and "kill" neg.
	rts

SizeIt:
	move.l	width(a4),d0
	moveq	#0,d2
	move.w	wd_Width(a0),d2
	sub.l	d2,d0
	move.l	height(a4),d1
	move.w	wd_Height(a0),d2
	sub.l	d2,d1
	Call	SizeWindow
	rts

MoveIt:
	move.l	left(a4),d0
	moveq	#0,d2
	move.w	wd_LeftEdge(a0),d2
	sub.l	d2,d0
	move.l	top(a4),d1
	move.w	wd_TopEdge(a0),d2
	sub.l	d2,d1
	Call	MoveWindow
	rts

Print:
	move.l	#buffer,d2
	add.l	a4,d2
	move.l	d2,a0
plp:
	tst.b	(a0)+
	bne.s	plp
	move.l	a0,d3
	sub.l	d2,d3
	move.l	stdout(a4),d1
	move.l	DOSBase(a4),a6
	Call	Write
	rts

store:
	move.b	d0,(a3)+
	rts

* Data
* -----------------------------------------------------------------------
IntName:
	DC.B	'intuition.library',0
DOSName:
	DC.B	'dos.library',0

BadArgs:
	DC.B	'Bad Arguments!',10,10
Help:
	DC.B	'Usage: WinSize [leftedge topedge width height]',10
	DC.B	'       WinSize s to show current size',10
HelpLen EQU	*-Help
BadArgLen EQU	*-BadArgs

NoInt:
	DC.B	'Couldn''t open Intuition!!!',10
NoIntLen EQU	*-NoInt

Format:
	DC.B	'Current parameters: '
	DC.B	'left %ld, top %ld, width %ld, height %ld.',10,0

	end
