
; ShutDwnPower - by Joseph Fenton
;
; Copyright (C) 1997 By Microcode Solutions, all rights reserved!
;
; This is a demonstartion of how to use the InnerCommunications Port
;
; Assemble with Devpac.  Other assemblers will require modifications
; to the code.


; Includes ----------------------------------------------------------

		incdir	DEVPAC:includes

		include	exec/exec.i
		include	dos/dosextens.i
		include	workbench/startup.i
		include	workbench/workbench.i
		include	intuition/intuition.i

		include	macinterface.i

		include	offsets.lib	; all the LVO's

		output	FUSION:ICP/ShutDwnPower

; Macros ------------------------------------------------------------

CALLSYS		MACRO
		jsr	_LVO\1(a6)
		ENDM

CALLEXE		MACRO
		movea.l	_ExecBase,a6
		CALLSYS	\1
		ENDM

CALLDOS		MACRO
		movea.l	_DOSBase,a6
		CALLSYS	\1
		ENDM

CALLINT		MACRO
		movea.l	_IntuitionBase,a6
		CALLSYS	\1
		ENDM

;--------------------------------------------------------------------

entry_pt	bra.b	main

		dc.b	0,'$VER:ShutDwnPower 1.0 (21.6.97)',0

		cnop	0,4

main		movea.l	4.w,a6
		move.l	a6,_ExecBase

		suba.l	a1,a1
		CALLSYS	FindTask
		movea.l	d0,a5
		tst.l	pr_CLI(a5)
		bne.b	.fromdos		; not run from WB

		lea	pr_MsgPort(a5),a0
		CALLSYS	WaitPort		; wait for startup message
		lea	pr_MsgPort(a5),a0
		CALLSYS	GetMsg			; get startup message
		move.l	d0,StartupMsg

.fromdos	lea	err_not_running,a4

		lea	icpName,a1
		CALLSYS	FindPort		; get ICP address
		move.l	d0,ICPort
		beq.b	.done			; FUSION not running

		bsr.w	doShutDown

.done		move.l	a4,d0
		beq.b	.exit			; no error message

		tst.l	pr_CLI(a5)
		bne.b	.doserr			; print error to StdOut

		moveq	#37,d0
		lea	IntuitionName,a1
		CALLSYS	OpenLibrary
		move.l	d0,_IntuitionBase
		beq.b	.done			; for safety

		suba.l	a0,a0			; appear on Workbench
		movea.l	a4,a1			; easyStruct for error
		suba.l	a2,a2			; no extra IDCMP flags for termination
		suba.l	a3,a3			; no arguments
		CALLINT	EasyRequestArgs

		move.l	a6,a1
		CALLEXE	CloseLibrary
		bra.b	.exit

.doserr		moveq	#0,d0
		lea	DOSName,a1
		CALLSYS	OpenLibrary
		move.l	d0,_DOSBase
		beq.b	.exit			; for safety

		move.l	es_TextFormat(a4),d1	; error string
		CALLDOS	PutStr

		move.l	#newline,d1		; CR and LF
		CALLSYS	PutStr

		movea.l	a6,a1
		CALLEXE	CloseLibrary

.exit		move.l	StartupMsg,d0
		bne.b	.wbexit
		rts				; return with 0 in d0 to DOS

.wbexit		CALLSYS	Forbid
		movea.l	StartupMsg,a1
		CALLSYS	ReplyMsg
		moveq	#0,d0
		rts

;--------------------------------------------------------------------
; Found FUSION's public port, create msgport/message for innercommunication

		cnop	0,4

doShutDown	lea	err_cant_setup,a4	; in case of error

		CALLSYS	CreateMsgPort
		move.l	d0,SDPort
		beq.w	.error1			; couldn't create port

		movea.l	d0,a0
		moveq	#FM_SIZE,d0
		CALLSYS	CreateIORequest
		move.l	d0,SDMsg
		beq.w	.error2			; couldn't create message

; bring MAC screen to the front

		movea.l	d0,a1
		move.l	#FMCMD_FRONT,FM_OP(a1)
		movea.l	ICPort,a0
		CALLSYS	PutMsg
		movea.l	SDPort,a0
		CALLSYS	WaitPort
		movea.l	SDPort,a0
		CALLSYS	GetMsg

; execute ShutDwnPower function

		movea.l	SDMsg,a1
		move.l	#FMCMD_EXECUTE,FM_OP(a1)
		move.l	#ShutDwnPower,FM_ARG1(a1)
		clr.l	FM_ARG2(a1)
		move.l	#FMFF_Switch,FM_FLAGS(a1)
		move.l	#600,FM_TIMEOUT(a1)	; 10 sec
		movea.l	ICPort,a0
		CALLSYS	PutMsg
		movea.l	SDPort,a0
		CALLSYS	WaitPort
		movea.l	SDPort,a0
		CALLSYS	GetMsg
		movea.l	d0,a1
		suba.l	a4,a4			; no error message
		cmpi.l	#ERR_SHUTDOWN,FM_RES1(a1)
		beq.b	.cleanup
		lea	err_cant_shutdn,a4

.cleanup	movea.l	SDMsg,a0
		CALLSYS	DeleteIORequest

.error2		movea.l	SDPort,a0
		CALLSYS	DeleteMsgPort

.error1		rts


; Emulator-Side Function Code ---------------------------------------

; This code runs on the emulator's task.  No registers need to be saved.

		cnop	0,4

ShutDwnPower	movea.l	ICPort,a0
		lea	MP_MSGLIST(a0),a0
		movea.l	SDMsg,a1
		ADDHEAD				; to get ERR_SHUTDOWN

		move.w	#1,-(a7)		; sdPowerOff
		dc.w	$A895			; _ShutDown

; Emulation didn't shut down, remove entry and reply it.

		movea.l	SDMsg,a1
		REMOVE
		moveq	#0,d0
		rts


; Variables ---------------------------------------------------------

		cnop	0,4

_ExecBase	dc.l	0
_IntuitionBase	dc.l	0
_DOSBase	dc.l	0

StartupMsg	dc.l	0

SDPort		dc.l	0
SDMsg		dc.l	0
ICPort		dc.l	0

; Name Strings ------------------------------------------------------

		cnop	0,2

icpName		FusionPortName
		even

IntuitionName	dc.b	'intuition.library',0
		even

DOSName		dc.b	'dos.library',0
		even

; Error Messages ----------------------------------------------------

		cnop	0,4

err_not_running	dc.l	es_SIZEOF
		dc.l	0
		dc.l	errTitle
		dc.l	not_running_msg
		dc.l	errOkay

err_cant_setup	dc.l	es_SIZEOF
		dc.l	0
		dc.l	errTitle
		dc.l	cant_setup_msg
		dc.l	errOkay

err_cant_shutdn	dc.l	es_SIZEOF
		dc.l	0
		dc.l	errTitle
		dc.l	cant_shutdn_msg
		dc.l	errOkay

errTitle	dc.b	'Error while trying to shut down FUSION',0
		even

errOkay		dc.b	'Okay',0
		even

not_running_msg	dc.b	'FUSION is not running!',0
		even

cant_setup_msg	dc.b	'InnerCommunications Port interface could not be setup.',0
		even

cant_shutdn_msg	dc.b	'FUSION could not be shut down.',0
		even

newline		dc.b	10,13,0
		even


		END

