	opt d+

; NotifyFollowYou

INC_DOS		EQU	0 1
INC_INT		EQU	1
INC_GFX		EQU	0 1
INC_GAD		EQU	0 1
INC_ASL		EQU	0 1
INC_UTI		EQU	0 1
INC_WB		EQU	0 1

	incdir	INCLUDE:
	include	Automacros.i
	include	libraries/notifyintuition.i


	; StartUp Nr,TaskName,Stack     Taskname + Stack only for Async_Run
	STARTUP	1,<'Async TaskName'>,4096	; 0=OFF 1=WB+CLI 2=ONLY_WB 3=ONLY_CLI 4=ASYNC_RUN

 STRUCTURE Var,0
	STRUCT	var_Notify,IntNotifyRequest_SIZEOF
	APTR	var_WindowPtr
	LONG	var_WaitMask
	APTR	var_LastScreenPtr
	LABEL	var_SIZEOF

_main:	suba.l	a5,a5
	OPENDOS	0
	OPENINT	0
	OPENLIB	notifyname,_NotiBase(pc),2
	beq	NoLib
	move.l	#var_SIZEOF,d0
	move.l	#MEMF_CLEAR!MEMF_PUBLIC,d1
	CALLEXEC AllocVec
	move.l	d0,a5
	tst.l	d0
	beq	Ende
	CALLEXEC CreateMsgPort
	move.l	d0,var_Notify+inr_Port(a5)
	beq	Ende
	move.l	d0,a0
	moveq	#0,d0
	moveq	#0,d1
	move.b	MP_SIGBIT(a0),d1
	bset	d1,d0
	bset	#12,d0
	move.l	d0,var_WaitMask(a5)

	move.l	_NotiBase(pc),a6
	move.l	#'FOLO',d0
	CALLLIB	NotIFindUniqueTask
	beq.s	.Install
	move.l	d0,a1
	moveq	#0,d0
	bset	#12,d0
	CALLEXEC Signal
	moveq	#10,d1
	CALLDOS	Delay
	bra	Ende
.Install
	lea	var_Notify(a5),a0
	clr.l	inr_Name(a0)		wan't get all screen's
	or.l	#INRF_SEND_MESSAGE!INRF_NOTIFY_INITIAL!INRF_WAIT_REPLY!INRF_NOT_MYOWNREQUEST,inr_Flags(a0)
	or.l	#INRF_AFTER_OPENSCREEN!INRF_AFTER_OPENWINDOW!INRF_BEFORE_CLOSESCREEN!INRF_BEFORE_CLOSEWINDOW!INRF_BEFORE_CLOSEWORKBENCH,inr_Flags(a0)
	move.l	#'FOLO',inr_UniqueID(a0)
	move.l	_NotiBase(pc),a6
	CALLLIB	NotIStartNotify
	tst.l	d0
	beq	Ende

***********************************************************
*
***********************************************************
Wait	move.l	var_WaitMask(a5),d0
	CALLEXEC Wait
	btst	#12,d0
	bne	Ende
Mloop	move.l	var_Notify+inr_Port(a5),a0
	CALLEXEC GetMsg
	move.l	d0,a2
	tst.l	d0
	bne.s	.wmsg
	move.l	var_WindowPtr(a5),d0
	beq.s	Wait
	move.l	d0,a0
	move.l	wd_UserPort(a0),a0
	CALLLIB	GetMsg
	move.l	d0,a2
	tst.l	d0
	beq.s	Wait
.wmsg
	move.l	inm_Class(a2),d2
	movem.w	inm_Code(a2),d3
	move.l	inm_IntObject(a2),a3

	cmp.l	#INTNOTIFY_CLASS,d2
	bne	IntuitionMsg
	cmp.w	#INRCODE_AFTER_OPENSCREEN,d3
	beq	A_OpenScreen
	cmp.w	#INRCODE_BEFORE_CLOSESCREEN,d3
	beq	B_CloseScreen
	cmp.w	#INRCODE_BEFORE_CLOSEWORKBENCH,d3
	beq	B_CloseScreen
	cmp.w	#INRCODE_BEFORE_CLOSEWINDOW,d3
	beq	B_CloseWindow
	cmp.w	#INRCODE_AFTER_OPENWINDOW,d3
	beq	A_OpenWindow
Reply	move.l	a2,a1
	CALLEXEC ReplyMsg
	bra	Mloop
***********************************************************
*
***********************************************************
A_OpenWindow
	move.l	a2,a1
	CALLLIB	ReplyMsg
	move.l	var_LastScreenPtr(a5),d2
	beq.s	.x
	move.l	a3,d0
	beq.s	.x
	cmp.l	wd_WScreen(a3),d2
	bne.s	.x
	move.l	var_WindowPtr(a5),d1		Fenster offen ?
	beq.s	.OpenWin
	move.l	d1,a0
	cmp.l	wd_WScreen(a0),d2
	beq.s	.x
	bsr	CloseWindow			dann zumachen
.OpenWin
	lea	WindowTags(pc),a0
	move.l	d2,4(a0)
	bsr	OpenWindow
.x	bra	Mloop
***********************************************************
*
***********************************************************
A_OpenScreen:
	move.l	a3,var_LastScreenPtr(a5)
	bra	Reply
***********************************************************
*
***********************************************************
B_CloseScreen:
	move.l	var_WindowPtr(a5),d0		Fenster offen ?
	beq.s	.x
	move.l	d0,a0
	cmp.l	wd_WScreen(a0),a3		ist das mein Screen ?
	bne.s	.x
	bsr	CloseWindow
	lea	WindowTags(pc),a0
	move.l	sc_NextScreen(a3),4(a0)		ist ein Screen dahinter ?
	bne.s	.do
	move.l	_IntBase(pc),a1
	move.l	ib_FirstScreen(a1),4(a0)
	beq.s	.x
.do	cmp.l	4(a0),a3
	beq.s	.x
	move.l	a2,a1
	CALLEXEC ReplyMsg
	bsr	OpenWindow
	bra	Mloop
.x	bra	Reply
***********************************************************
*
***********************************************************
B_CloseWindow:
	move.l	var_WindowPtr(a5),d0		Fenster offen ?
	beq.s	.x
	cmp.l	a3,d0				schließt jemand mein Fenster ?
	bne.s	.x
	clr.l	var_WindowPtr(a5)
	move.l	a2,a1
	CALLEXEC ReplyMsg
	moveq	#25,d1
	CALLDOS	Delay
	lea	WindowTags(pc),a0
	move.l	_IntBase(pc),a1
	move.l	ib_FirstScreen(a1),4(a0)
	beq	Mloop
	bsr	OpenWindow
	bra	Mloop
.x	bra	Reply
***********************************************************
*
***********************************************************
IntuitionMsg:
	move.l	a2,a1
	CALLEXEC ReplyMsg
	cmp.w	#IDCMP_CLOSEWINDOW,d2
	bne	Mloop
	bsr	CloseWindow
***********************************************************
*
***********************************************************
Ende:	move.l	a5,d0
	beq	.closelibs
	tst.l	var_WindowPtr(a5)
	beq.s	.NoWin
	bsr	CloseWindow
.NoWin	tst.l	var_Notify+inr_ReqSemaphore(a5)
	beq.s	.1
.new	lea	var_Notify(a5),a0
	move.l	_NotiBase(pc),a6
	CALLLIB	NotIEndNotify
	tst.l	d0
	bne.s	.1
	move.b	var_Notify+inr_Error(a5),d0
	cmp.b	#INRERROR_OUTSTANDING_MSGS,d0
	bne.s	.3
.lo1	move.l	var_Notify+inr_Port(a5),a0
	CALLEXEC GetMsg
	tst.l	d0
	beq.s	.new
	move.l	d0,a1
	CALLLIB	ReplyMsg
	bra.s	.lo1
.3	cmp.b	#INRERROR_ISLOCKED,d0
	bne.s	.4
	moveq	#30,d1
	CALLDOS	Delay
	bra.s	.new
.4	cmp.b	#INRERROR_UNKNOWN,d0
	bne.s	.new
.1	move.l	var_Notify+inr_Port(a5),d0
	beq.s	.2
	move.l	d0,a0
	CALLEXEC DeleteMsgPort
.2
.closelibs
	CLOSELIB _NotiBase
	CLOSEDOS	MACRO schließt die dos.lib wenn sie offen war
	CLOSEINT
	moveq	#0,d0
	rts
OpenWindow:
	movem.l	a6,-(sp)
	tst.l	var_WindowPtr(a5)
	bne.s	.x
	move.l	var_Notify+inr_ReqSemaphore(a5),a0
	CALLEXEC AttemptSemaphore
	move.w	d0,d4
	lea	WindowTags(pc),a1
	suba.l	a0,a0
	CALLINT	OpenWindowTagList
	move.l	d0,var_WindowPtr(a5)
	beq.s	.x
	move.l	d0,a0
	move.l	wd_UserPort(a0),a0
	moveq	#0,d0
	moveq	#0,d1
	move.b	MP_SIGBIT(a0),d1
	bset	d1,d0
	move.l	var_Notify+inr_Port(a5),a0
	move.b	MP_SIGBIT(a0),d1
	bset	d1,d0
	bset	#12,d0
	move.l	d0,var_WaitMask(a5)
	tst.w	d4
	beq.s	.x
	move.l	var_Notify+inr_ReqSemaphore(a5),a0
	CALLEXEC ReleaseSemaphore
.x	movem.l	(sp)+,a6
	rts
CloseWindow:
	movem.l	a6,-(sp)
	move.l	var_Notify+inr_ReqSemaphore(a5),a0
	CALLEXEC AttemptSemaphore
	move.w	d0,d4
	move.l	var_WindowPtr(a5),d0
	beq.s	.x
	move.l	d0,a0
	clr.l	var_WindowPtr(a5)
	CALLINT	CloseWindow
	tst.w	d4
	beq.s	.x
	move.l	var_Notify+inr_ReqSemaphore(a5),a0
	CALLEXEC ReleaseSemaphore
.x	movem.l	(sp)+,a6
	rts
NoLib:	lea	YouNeed(pc),a0
	move.l	a0,d1
	CALLDOS	PutStr
	bra	Ende

	INCLUDE	INC_NAMES.i
_NotiBase	dc.l	0
WindowTags	dc.l	WA_CustomScreen,0
		dc.l	WA_Title,Follow
		dc.l	WA_Left,20
		dc.l	WA_Top,20
		dc.l	WA_Width,300
		dc.l	WA_Height,16
		dc.l	WA_CloseGadget,1
		dc.l	WA_DragBar,1
		dc.l	WA_DepthGadget,1
		dc.l	WA_RMBTrap,1
		dc.l	WA_IDCMP,IDCMP_CLOSEWINDOW
		dc.l	0
notifyname	dc.b	'notifyintuition.library',0
YouNeed		dc.b	"You need 'notifyintuition.library' to run this demo.",10,0
Follow		dc.b	'I follow you to new screens.',0
