* quick hack by A.D.Webber (e-mail: adw@ukc.ac.uk) for LPAK 0.5(beta)
* release.
*
* based on accessory demo sources from HiSoft (provided with GenST2)
* will need tweaking for other assemblers.
*
* But the program is so simple that I'm sure a better version could be
* knocked up without any difficulty.
*
*					Lloyd. (28/9/1993)
*

RUNNER	equ	0		1 for .PRG, 0 for .ACC
X_ID	equ	'XBRA'
P_ID	equ	'LPAK'

* 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
* check others here
	bra.s	waitforevent

* here when I have to Open
open_acc:
	pea.l	find_lpak
	move.w	#$26,-(sp)
	trap	#14
	addq.l	#6,sp
	move.l	lpak_addr,a6
	beq.s	no_lalert
lalert:	tst.w	(a6)
	beq.s	lpak_offa
lpak_ona:
	form_alert	#3,#onalert
	bra.s	handle_response
lpak_offa:
	form_alert	#3,#offalert
handle_response:
	cmp.w	#3,d0
	beq.s	waitforevent
	cmp.w	#1,d0
	beq.s	slpak_on
	cmp.w	#2,d0
	bne	waitforevent
slpak_off:
	clr.w	(a6)
	bra	waitforevent
slpak_on:
	move.w	#-1,(a6)
	bra	waitforevent

no_lalert:
	form_alert	#1,#no_lpak
	bra	waitforevent

find_lpak:
	move.l	$84.w,a0
fl_loop:
	cmp.l	#X_ID,-12(a0)
	bne.s	no_flpak
	cmp.l	#P_ID,-8(a0)
	beq.s	found_lpak
	move.l	-4(a0),a0
	bra.s	fl_loop
found_lpak:
	lea.l	-14(a0),a0
	move.l	a0,lpak_addr
no_flpak:
	rts

	SECTION	DATA
* all strings must end in a null
mymenu	dc.b	'  LPAK Control',0

no_lpak	dc.b	'[1][LPAK is not currently|installed][ Rats ]',0
onalert	dc.b	'[1][LPAK version 0.5(beta)|',189,' A.D.Webber (Lloyd!)|is currently on|Please select:][ On | Off | Cancel ]',0
offalert dc.b	'[1][LPAK version 0.5(beta)|',189,' A.D.Webber (Lloyd!)|is currently off|Please select:][ On | Off | Cancel ]',0

* global constants
	SECTION	BSS

lpak_addr	ds.l	1
ap_id		ds.w	1
messagebuf	ds.b	16

	ds.l	100			stack space
mystack	ds.w	1			(stacks go backwards)


* if not linking then include the run-times

	IFEQ	__LK
	include	aeslib.s
	ENDC
	end