*  Here three routines are defined: MakeOffswitch, TestOffSwitch and
*  RemOffSwitch. They exist to replace the keyboard abort ^C by a kind of
*  push button, labeled with the programname. So programs running in the
*  background are easy reachable and you can/must use your own closing
*  down. Must perhaps, because you MUST call RemOffSwitch before
*  termination, otherwise the window exits forever and every push on the
*  offswitch withdraws 56 bytes of the pool.
*  MakeOffSwitch() may be used to make one window. Subsequent calls just
*  return the (already existing) window pointer.
*  

*	nolist		
	ifnd	ifiles
ifiles	set	1
	include	"exec/types.i"
	include	"intuition/intuition.i"
	endc
*	list

LEFT	equ	640/2			Middle of the screen
TOP	equ	0
WIDTH	equ	640/2			Half a screen
HEIGHT	equ	10			Normal dragbar height

	xdef	_MakeOffSwitch,MakeOffSwitch
	xdef	_TestOffSwitch,TestOffSwitch
	xdef	_RemOffSwitch,RemOffSwitch

	xref	_LVOOpenLibrary
	xref	_LVOOpenWindow
	xref	_LVOGetMsg
	xref	_LVOReplyMsg
	xref	_LVOCloseWindow
	xref	_LVOCloseLibrary

_AbsExecBase	equ	4

	csect	OffSwitch,0,0,1,2

*
*  From C:
*  if (MakeOffSwitch(Title)!=NULL)  /* then there is a window! */
*  else  /* there is no window, but further no problem */
*  Title is a pointer to a '\0' terminated string.
*  MakeOffSwitch returns a pointer to the Window structure. Ignore it
*  or use it, perhaps to change the window. However you may not remove it.
*
*  Intuition is opened, but when the OpenWindow fails, intuition is closed
*  immediate. So if this routine returns NULL, it is not necesary to call
*  RemOffSwitch, although this would cause no problem.
*

_MakeOffSwitch:
	move.l	4(a7),a0
MakeOffSwitch:
	move.l	a0,Title(a4)		Text in bar into NewWindow-$
	move.l	a6,-(a7)		Keep callers a6
	move.l	Wind(a4),d0		See if there is already a window
	bne.s	QMOS			Yes, return its pointer
	move.l	IBase(a4),d0		See if Intuition is already open
	bne.s	GotI
	move.l	_AbsExecBase,a6		Get Exec-function pointer
	lea.l	IntuName(pc),a1		"intuition.library"
	moveq.l	#0,d0			Any version is OK
	jsr	_LVOOpenLibrary(a6)	Get Intuition library
	move.l	d0,IBase(a4)		Keep for later
	beq.s	QMOS			No intu; no window
GotI	move.l	d0,a6			Get intu pointer
	lea.l	NewWind(a4),a0		Get NewWindow structure
	jsr	_LVOOpenWindow(a6)	Open a window
	move.l	d0,Wind(a4)		Keep pointer, so we can close it
	beq.s	CloseI			No window, Intu of no use
QMOS	move.l	(a7)+,a6		Restore callers a6
	rts

*
*  From C:
*  if (TestOffSwitch()!=0)  /* then close gadget was stirred */
*  else  /* close gadged was passed */
*  This may be done more then once, the message is just passed.
*
*  Even if there is no window,  it is allowed to call this routine. 
*  Then it just returns zero.
*

_TestOffSwitch:
TestOffSwitch:
	move.l	a6,-(a7)		Keep callers a6
	move.l	_AbsExecBase,a6		Get Exec-function pointer
	move.l	Wind(a4),d0		Get Window-$
	beq.s	QTOS			If none quit
	move.l	d0,a1			We need a member so use a-reg
	move.l	wd_UserPort(a1),a0	Get connection to our window
	jsr	_LVOGetMsg(a6)		See if ther's a message
	tst.l	d0			non-zero is truly a message
	beq.s	QTOS			zero means no message
	move.l	d0,a1			a1 is used to reply
	move.l	im_Class(a1),-(a7)	Keep interesting part of mess
	jsr	_LVOReplyMsg(a6)	Return message ownership
	move.l	(a7)+,d0		Get interesting part
	cmp.l	#CLOSEWINDOW,d0		We look only for closewindow
	bne.s	QZero			If not we return zero
QTOS	move.l	(a7)+,a6		If true we return CLOSEWINDOW
	rts

*
*  From C:
*  RemOffSwitch();   /* removes the window */
*
*  Even if there was no window, it is allowed to call this routine.
*
	
_RemOffSwitch:
RemOffSwitch:
	move.l	a6,-(a7)		Save callers a6
	move.l	IBase(a4),d0		Get key to intuition
	beq.s	QROS			If none asume ready
	move.l	d0,a6			a6 is used to enter intuition
	move.l	Wind(a4),d0		Get window structure
	beq.s	CloseI			If none close intuition
	move.l	d0,a0			A0 is parameter port
	jsr	_LVOCloseWindow(a6)	Close our window
	clr.l	Wind(a4)		Aint that nice?
CloseI	move.l	a6,a1			A1 is parameter port
	move.l	_AbsExecBase,a6		Get key to Exec
	jsr	_LVOCloseLibrary(a6)	Close intuition
	clr.l	IBase(a4)		There is NO intuition anymore..
QZero	moveq.l	#0,d0			This routine always returns 0
QROS	move.l	(a7)+,a6		Callers a6
	rts
	
IntuName dc.b 'intuition.library',0

	csect	__MERGED,1,,2,2

***  The NewWindow structure

NewWind dc.w	LEFT,TOP,WIDTH,HEIGHT
	dc.b	-1,-1
	dc.l	CLOSEWINDOW
	dc.l	WINDOWDRAG!WINDOWCLOSE!WINDOWDEPTH
	dc.l	0,0
Title	dc.l	0
	dc.l	0,0
	dc.w	0,0,0,0
	dc.w	WBENCHSCREEN
	
Wind	ds.l	1
IBase	ds.l	1

	end
