;----------------------------------------------------------------------
;
;     DiskReset            Copyright © 1993 by P-O Yliniemi
;
; Program to reset the computer when a disk is removed from any drive.
; If the user selects the close gadget or presses the ESC key while the
; window is active, the reset would be cancelled.
;
;----------------------------------------------------------------------

;--------------------------- LVO:s ------------------------------------

ExecBase:	equ	$4

OpenLibrary:	equ	-552		; Exec LVO:s
CloseLibrary:	equ	-414
ColdReboot:	equ	-726
Wait:		equ	-318
GetMsg:		equ	-372
ReplyMsg:	equ	-378

OpenWindowTags:	equ	-606		; Intuition LVO:s
CloseWindow:	equ	-72

;-------------------- System constants ------------------------------

UserPort:	equ	86		; Window->UserPort (MsgPort *)
mp_SigBit:	equ	15		; MsgPort->mp_SigBit
Class:		equ	20		; IntuiMessage->Class
Code:		equ	24		; IntuiMessage->Code

WA_Title:	equ	$8000006e	; Tag constants
WA_Left:	equ	$80000064
WA_Top:		equ	$80000065
WA_Width:	equ	$80000066
WA_Height:	equ	$80000067
WA_CloseGadget:	equ	$80000084
WA_DragBar:	equ	$80000082
WA_Activate:	equ	$80000089
WA_RMBTrap:	equ	$8000008a
WA_IDCMP:	equ	$8000006a

TAG_END:	equ	$00000000
TRUE:		equ	$00000001

IDCMP_DISKREMOVED: equ	$00010000	; IDCMP flags
IDCMP_CLOSEWINDOW: equ	$00000200
IDCMP_VANILLAKEY:  equ	$00200000

;--------------------------- The program ------------------------------

OpenIntuition:
	move.l	ExecBase,a6
	lea.l	IntuiName(pc),a1	; Open Intuition library version
	moveq.l	#37,d0			;  37 (OS 2.0) or higher
	jsr	OpenLibrary(a6)
	move.l	d0,IntuiBase		; Save base address and check for
	beq.s	IntuiError		;  error

OpenWin:
	move.l	d0,a6			; Open a little window on the
	move.l	#0,a0			;  Workbench screen...
	lea.l	WindowTags,a1
	jsr	OpenWindowTags(a6)
	move.l	d0,win			; Save pointer and check for
	beq.s	CloseIntui		;  error

	move.l	ExecBase,a6		; Get message from the windows
CheckSignals:
	bsr.s	MakeSignals		; Get signal mask for IDCMP flags
	jsr	Wait(a6)		; Wait until requested signals
	move.l	d0,signals		;  appear and save them

	bsr.s	MakeSignals		; Get signal mask
	cmp.l	signals,d0		; and compare to received signals
	bne.s	CheckSignals		; Continue checking if different
	bsr.s	handleIDCMP		; Handle all messages waiting
	tst.w	done			; Check DONE flag
	beq.s	CheckSignals		; Continue check the signals

CloseWin:
	move.l	win,a0			; Close the window
	move.l	IntuiBase(pc),a6
	jsr	CloseWindow(a6)
CloseIntui:
	move.l	IntuiBase(pc),a1	; Close Intuition library
	move.l	ExecBase,a6
	jsr	CloseLibrary(a6)

IntuiError:
	moveq.l	#0,d0			; Failat code
	rts

;------------ Subroutine to make the Signal mask for Wait() --------------

MakeSignals:
	moveq.l	#1,d0			; Make signal mask:
	moveq.l	#0,d1
	move.l	win(pc),a0		; win->UserPort->mp_SigBit to
	move.l	UserPort(a0),a0		; 
	move.b	mp_SigBit(a0),d1	; D1
	lsl.l	d1,d0			; Shift 1 D1 times
	rts

;----------------------- handleIDCMP subroutine --------------------------

handleIDCMP:
	move.l	win(pc),a0		;  port
	move.l	UserPort(a0),a0
	jsr	GetMsg(a6)
	move.l	d0,a1			; Message pointer to A1
	beq.s	nothing
	move.l	Class(a1),Mclass	; Get message class
	move.w	Code(a1),Mcode		; Get message code
	jsr	ReplyMsg(a6)		; and reply the message
	move.l	Mclass,d0
CheckMessage:
	cmp.l	#IDCMP_CLOSEWINDOW,d0	; Close gadget selected ?	
	bne.s	CheckDisk		; Nope!
	move.w	#-1,done		; Set done flag
	rts				; and return
CheckDisk:
	cmp.l	#IDCMP_DISKREMOVED,d0	; Is disk removed ?
	bne.s	CheckVanilla
	jsr	ColdReboot(a6)		; Reboot computer
CheckVanilla:
	cmp.l	#IDCMP_VANILLAKEY,d0	; Is any key pressed
	bne.s	nothing			; Nope!
	cmp.w	#27,Mcode		; Is ESC pressed
	bne.s	nothing			; Nope!
	move.w	#-1,done		; Set done flag
nothing:
	rts

;-------------------- Data and structures -------------------------

WindowTags:
	dc.l	WA_Title,	wintitle,
	dc.l	WA_Left,	160,
	dc.l	WA_Top,		120,
	dc.l	WA_Width,	315,
	dc.l	WA_Height,	11,
	dc.l	WA_CloseGadget,	TRUE,
	dc.l	WA_DragBar,	TRUE,
	dc.l	WA_Activate,	TRUE,
	dc.l	WA_RMBTrap,	TRUE,
	dc.l	WA_IDCMP,	IDCMP_DISKREMOVED ! IDCMP_CLOSEWINDOW ! IDCMP_VANILLAKEY,
	dc.l	TAG_END

wintitle:
		dc.b	"Remove a disk to reset the machine",0

		even
win:		dc.l	0
signals:	dc.l	0
Mclass:		dc.l	0
Mcode:		dc.l	0
done:		dc.w	0

IntuiBase:	dc.l	0
IntuiName:	dc.b	"intuition.library",0

end
