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

* Includes
* -----------------------------------------------------------------------
	include "exec/types.i"
	include "intuition/intuition.i"

* External references
* -----------------------------------------------------------------------

	XREF	_AbsExecBase
	XREF	_LVOWrite
	XREF	_LVOMoveWindow
	XREF	_LVOSizeWindow
	XREF	_LVORethinkDisplay
	XREF	_LVOOpenLibrary
	XREF	_LVOCloseLibrary
	XREF	_LVOOutput
	XREF	_LVORawDoFmt

* Macros
* ------------------------------------------------------------------------

* Calls to dos.library, exec library, and intuition.library
DOSCall MACRO
	LINKLIB _LVO\1,_DOSBase
	ENDM

ExeCall MACRO
	LINKLIB _LVO\1,_AbsExecBase
	ENDM

IntCall MACRO
	LINKLIB _LVO\1,_IntBase
	ENDM

* On with the Program!
* ------------------------------------------------------------------------
WinSize:
	move.l	a0,a4
	lea.l	DOSName(PC),a1
	move.l	#LIBRARY_VERSION,d0
	ExeCall	OpenLibrary
	move.l	d0,_DOSBase
	DOSCall	Output
	move.l	d0,_stdout
	cmpi.b	#'?',(a4)
	bne.s	NoHelp
	move.l	_stdout,d1
	move.l	#Help,d2
	move.l	#HelpLen,d3
	DOSCall Write
	rts
NoHelp:
	move.l	#IntName,a1
	moveq	#0,d0
	ExeCall OpenLibrary
	move.l	d0,_IntBase
	bne.s	IntOk
	move.l	_stdout,d1
	move.l	#NoInt,d2
	move.l	#NoIntLen,d3
	DOSCall Write
	rts
IntOk:
	moveq	#0,d0
	move.b	(a4),d0
	andi.b	#95,d0
	cmpi.b	#'S',d0
	bne.s	NoShow
	move.l	_IntBase,a5
	move.l	ib_ActiveWindow(a5),a4
	move.w	wd_LeftEdge(a4),d0
	move.l	d0,left
	move.w	wd_TopEdge(a4),d0
	move.l	d0,top
	move.w	wd_Width(a4),d0
	move.l	d0,width
	move.w	wd_Height(a4),d0
	move.l	d0,height
	lea	Format,a0
	lea	left,a1
	lea	PutChar,a2
	lea	char,a3
	ExeCall	RawDoFmt
	rts
NoShow:
	cmpi.b	#' ',(a4)
	blt.s	RangeChk
	move.l	a4,a0
	bsr	atoi
	move.l	d0,left
	cmpi.b	#' ',d1
	blt.s	Arrggh
	bsr	atoi
	move.l	d0,top
	cmpi.b	#' ',d1
	blt.s	Arrggh
	bsr	atoi
	move.l	d0,width
	cmpi.b	#' ',d1
	blt.s	Arrggh
	bsr	atoi
	move.l	d0,height
	bra.s	RangeChk
Arrggh:
	move.l	_stdout,d1
	move.l	#BadArgs,d2
	move.l	#BadArgLen,d3
	DOSCall Write
	rts
RangeChk:
	move.l	_IntBase,a5
	move.l	ib_ActiveWindow(a5),a4
	move.l	ib_ActiveScreen(a5),a5
	moveq	#0,d0
	move.w	sc_Width(a5),d0
	move.l	width,d1
	cmp.l	d0,d1
	ble.s	RC1
	move.l	d0,width
	move.l	d0,d1
RC1:
	move.w	wd_MaxWidth(a4),d0
	cmp.l	d0,d1
	bls.s	RC2
	move.w	d1,wd_MaxWidth(a4)
RC2:
	move.w	wd_MinWidth(a4),d0
	cmp.l	d0,d1
	bge.s	RC3
	move.l	d0,width
RC3:
	move.l	height,d1
	move.w	sc_Height(a5),d0
	cmp.l	d0,d1
	ble.s	RC4
	move.l	d0,height
	move.l	d0,d1
RC4:
	move.w	wd_MaxHeight(a4),d0
	cmp.l	d0,d1
	bls.s	RC5
	move.w	d1,wd_MaxHeight(a4)
RC5:
	move.w	wd_MinHeight(a4),d0
	cmp.l	d0,d1
	bge.s	RC6
	move.l	d0,height
RC6:
	move.l	left,d1
	move.l	width,d2
	move.w	sc_Width(a5),d0
	sub.l	d2,d0
	cmp.l	d0,d1
	ble.s	RC7
	move.l	d0,left
RC7:
	move.l	top,d1
	move.l	height,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
DoIt:
	move.l	left,d1
	move.w	wd_LeftEdge(a4),d0
	cmp.l	d0,d1
	blt.s	MoveFirst
	move.l	top,d1
	move.w	wd_TopEdge(a4),d0
	cmp.l	d0,d1
	bge.s	SizeFirst
MoveFirst:
	bsr	MoveIt
	bsr.s	SizeIt
	bra.s	DoDisp
SizeFirst:
	bsr.s	SizeIt
	bsr	MoveIt
DoDisp:
	IntCall RethinkDisplay
	move.l	_IntBase,a1
	ExeCall CloseLibrary
	move.l	_DOSBase,a1
	ExeCall	CloseLibrary
	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	#'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	a4,a0			; window to re-size
	move.l	width,d0
	moveq	#0,d2
	move.w	wd_Width(a4),d2
	sub.l	d2,d0
	move.l	height,d1
	move.w	wd_Height(a4),d2
	sub.l	d2,d1
	IntCall SizeWindow
	rts

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

PutChar:
	movem.l	d0-d4/a0-a2,-(sp)
	move.l	count,d3
	move.b	d0,(a3)+
	bne.s	pexit
	move.l	_stdout,d1
	move.l	#char,d2
	DOSCall	Write
pexit:
	addq.l	#1,d3
	move.l	d3,count
	movem.l	(sp)+,d0-d4/a0-a2
	rts

* Data
* -----------------------------------------------------------------------
count:
	DC.L	0
_IntBase:
	DC.L	0
_DOSBase:
	DC.L	0
_stdout:
	DC.L	0
left:
	DC.L	$0fffffff
top:
	DC.L	$0fffffff
width:
	DC.L	$0fffffff
height:
	DC.L	$0fffffff

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
	DC.B	'	WinSize ? for this help',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
char:
	DS.B	64

	end
