
RUNNER	equ	0		1 for .PRG, 0 for .ACC

* NOTE: with this particular program assembled stand-alone there is no way
* to exit from the program!

	IFEQ	RUNNER
	OUTPUT	.ACC
	ELSEIF
	opt x+				dump long labels for debugging
	OUTPUT	.PRG
	ENDC

	include	gemmacro.s

* the program proper
	IFEQ	RUNNER
start	move.l	#mystack,a7		must have a stack!
	ELSEIF
start	move.l	4(a7),a3		base page
	move.l	#mystack,a7
	move.l	$c(a3),d0		text len
	add.l	$14(a3),d0		data len
	add.l	$1c(a3),d0		BSS len
	add.l	#$100,d0		basepage
	move.l	d0,-(sp)
	move.l	a3,-(sp)
	clr.w	-(sp)
	move.w	#$4a,-(sp)
	trap	#1			shrink memory
	lea	12(sp),sp
	ENDC

	appl_init
	move.w	d0,ap_id		store the application id

	IFEQ	RUNNER
* start by installing me in the Desk menu
	menu_register	ap_id,#mymenu
	ELSEIF
* set the mouse to an arrow
	graf_mouse	#0
	bra	open_acc		then pretend an Open
	ENDC



* the main loop of the application
* the only interesting events are messages
waitforevent
	evnt_mesag	#messagebuf
	move.l	#messagebuf,a0
	move.w	(a0),d0			message type
	cmp.w	#40,d0
	beq	open_acc
	bra.s	waitforevent
open_acc:
	pea	findsa
	move.w	#38,-(sp)
	trap	#14
	addq.l	#6,sp
	tst.l	sndalert
	beq	donotinst
	bmi	dooff
	form_alert	#1,#saonalert
	move.w	int_out,d0
	cmpi.w	#2,d0
	beq	asis
	pea	turnsaoff
	move.w	#38,-(sp)
	trap	#14
	addq.l	#6,sp
	bra	waitforevent

dooff:
	form_alert	#1,#saoffalert
	move.w	int_out,d0
	cmpi.w	#2,d0
	beq	asis
	pea	turnsaon
	move.w	#38,-(sp)
	trap	#14
	addq.l	#6,sp
asis:
	bra	waitforevent

donotinst:
	form_alert	#1,#nialert
	bra	waitforevent

*********************************************************
*	Conducts a search for soundalert's cookie.	*
*	If found saves it's current state and turns	*
*	it off. sndalert=-1 for off 			*
*			 0 for not installed		*
*			 1 for on			*
*********************************************************
findsa:
	move.l	$5a0,a0		;get pointer to cookie jar
fsaloop:
	move.l	(a0)+,d0
	beq.s	fsaloopover
	cmpi.l	#'SMC1',d0
	beq.s	chksa
	addq.l	#4,a0
	bra.s	fsaloop
chksa:
	move.l	(a0),d0
	move.l	a0,savesa
	move.l	d0,sndalert	;save state
	rts
fsaloopover:
	move.l	#0,sndalert
	rts

turnsaoff:
	move.l	savesa,a0
	move.l	#-1,(a0)
	rts
turnsaon:
	move.l	savesa,a0
	move.l	#1,(a0)
	rts



	SECTION	DATA
mymenu	dc.b	'  Sound Alert',0

saonalert:
	dc.b	'[1][SMC Sound Alert is|currently'
	dc.b	' ON. Do you|wish to turn it OFF?][YES|NO]',0
saoffalert:
	dc.b	'[1][SMC Sound Alert is|currently'
	dc.b	' OFF. Do you|wish to turn it ON?][YES|NO]',0
nialert:
	dc.b	'[1][SMC Sound Alert is not|'
	dc.b	'currently installed in this|'
	dc.b	'machine.][SORRY]',0


* global constants
	SECTION	BSS

ap_id		ds.w 1
messagebuf	ds.b 16

	ds.l	100			stack space
mystack	ds.w	1			(stacks go backwards)
sndalert:	ds.l	1
savesa:		ds.l	1
* if not linking then include the run-times

	IFEQ	__LK
	include	aeslib.s
	ENDC
