	opt d+

;	NotifyCenterwindows

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	 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 test,0
	STRUCT	ncw_Notify,IntNotifyRequest_SIZEOF
	LONG	ncw_SigBits
	LONG	ncw_Left
	LONG	ncw_Top
	LONG	ncw_Width
	LONG	ncw_Height
	LONG	ncw_Screen
	LONG	ncw_ScreenName
	LONG	ncw_Flags
	LABEL	test_SIZEOF

_main:	suba.l	a5,a5
	OPENDOS	0
	OPENUTI	0
	OPENINT	0
	lea	NotIName(pc),a1
	moveq	#1,d0
	CALLEXEC OpenLibrary
	lea	_NotIBase(pc),a0
	move.l	d0,(a0)
	beq	Need
	moveq	#test_SIZEOF,d0
	move.l	#MEMF_CLEAR!MEMF_PUBLIC,d1
	CALLLIB	AllocVec
	move.l	d0,a5
	tst.l	d0
	beq	Ende

	move.l	_NotIBase(pc),a6
	move.l	#'WA_P',d0
	CALLLIB	NotIFindUniqueTask
	beq.s	.Install
	move.l	d0,a1
	moveq	#0,d0
	bset	#12,d0
	CALLEXEC Signal
	moveq	#10,d1
	CALLDOS	Delay
.Install

	CALLEXEC CreateMsgPort
	lea	ncw_Notify(a5),a0
	move.l	d0,inr_Port(a0)
	move.b	#100,inr_Pri(a0)
	move.l	#'WA_P',inr_UniqueID(a0)
	move.l	#INRF_SEND_MESSAGE!INRF_WAIT_REPLY!INRF_BEFORE_OPENWINDOW!INRF_AFTER_OPENWINDOW,ncw_Notify+inr_Flags(a0)
	move.l	_NotIBase(pc),a6
	CALLLIB	NotIStartNotify
	tst.l	d0
	beq	TypeERR
	moveq	#0,d0
	moveq	#0,d1
	move.l	ncw_Notify+inr_Port(a5),a0
	move.b	MP_SIGBIT(a0),d1
	bset	d1,d0
	bset	#12,d0
	move.l	d0,ncw_SigBits(a5)
	bra	Mloop
Wait	move.l	ncw_SigBits(a5),d0
	CALLEXEC Wait
	btst	#12,d0
	bne.s	Ende
Mloop	move.l	ncw_Notify+inr_Port(a5),a0
	CALLEXEC GetMsg
	tst.l	d0
	beq.s	Wait
	move.l	d0,a2
	bsr	WorkOnMsg
Reply	move.l	a2,a1
	CALLEXEC ReplyMsg
	bra.s	Mloop
TypeERR:
	move.b	ncw_Notify+inr_Error(a5),d0
	lea	UniqueTXT(pc),a0
	cmp.b	#INRERROR_UNIQUE,d0
	beq.s	.type
	lea	LowMem(pc),a0
	cmp.b	#INRERROR_LOWMEM,d0
	bne	Ende
.type	move.l	a0,d1
	CALLDOS	PutStr

Ende:	move.l	a5,d0
	beq.s	.Closelibs
	tst.l	ncw_Notify+inr_ReqSemaphore(a5)
	beq.s	.NotInt
.Again	lea	ncw_Notify(a5),a0
	move.l	_NotIBase(pc),a6
	CALLLIB	NotIEndNotify
	tst.l	d0
	bne.s	.NotInt
	move.b	ncw_Notify+inr_Error(a5),d0
	cmp.b	#INRERROR_OUTSTANDING_MSGS,d0
	beq.s	.ClearPort
	cmp.b	#INRERROR_ISLOCKED,d0
	beq.s	.wait
	cmp.b	#INRERROR_UNKNOWN,d0
	beq.s	.NotInt
	illegal
.wait	moveq	#5,d1
	CALLDOS	Delay
.ClearPort
	move.l	ncw_Notify+inr_Port(a5),a0
	CALLEXEC GetMsg
	tst.l	d0
	beq.s	.Again
	move.l	d0,a1
	CALLLIB	ReplyMsg
	bra.s	.ClearPort
.NotInt	move.l	ncw_Notify+inr_Port(a5),d0
	beq.s	.NoPort
	move.l	d0,a0
	CALLEXEC DeleteMsgPort
.NoPort	move.l	a5,a1
	CALLEXEC FreeVec
.Closelibs
	CLOSEDOS	MACRO schließt die dos.lib wenn sie offen war
	CLOSEUTI
	CLOSEINT
	CLOSELIB _NotIBase
	moveq	#0,d0
	rts
After:
	cmp.w	#INRCODE_AFTER_OPENWINDOW,inm_Code(a2)
	bne	.x
	move.l	inm_TagListPtr(a2),d0
	beq.s	.x
	move.l	d0,a0
	cmp.l	#$80BADBAD,(a0)
	bne.s	.x
	move.l	a0,a1
	moveq	#TAG_MORE,d0
.1	cmp.l	(a0)+,d0
	lea	4(a0),a0
	bne.s	.1
	move.l	-4(a0),inm_TagListPtr(a2)
	CALLEXEC FreeVec
.x	rts
WorkOnMsg:
	cmp.w	#INRCODE_BEFORE_OPENWINDOW,inm_Code(a2)
	bne	After
	clr.l	ncw_Screen(a5)
	move.l	inm_IntObject(a2),d0
	beq.s	.tags
	move.l	d0,a0
	move.l	nw_Screen(a0),a1
	move.l	a1,ncw_Screen(a5)
	tst.w	nw_LeftEdge(a0)
	bpl.s	.1
	movem.w	sc_Width(a1),d0
	sub.w	nw_Width(a0),d0
	asr.w	#1,d0
	move.w	d0,nw_LeftEdge(a0)
.1	tst.w	nw_TopEdge(a0)
	bpl.s	.tags
	movem.w	sc_Height(a1),d0
	sub.w	nw_Height(a0),d0
	asr.w	#1,d0
	move.w	d0,nw_TopEdge(a0)
	bra	.x
.tags	tst.l	inm_TagListPtr(a2)
	beq	.x
	bsr	GetAllTags
	beq	.x
	moveq	#0,d2
	move.l	_UtiBase(pc),a6
	move.l	#WA_Left,d0
	move.l	inm_TagListPtr(a2),a0
	CALLLIB	FindTagItem
	tst.l	d0
	beq.s	.3
	move.l	d0,a0
	tst.l	4(a0)
	bne.s	.Top
	move.l	ncw_Left(a5),4(a0)
	bra.s	.Top
.3	bset	#1,d2
.Top	move.l	inm_TagListPtr(a2),a0
	move.l	#WA_Top,d0
	CALLLIB	FindTagItem
	tst.l	d0
	beq.s	.4
	move.l	d0,a0
	tst.l	4(a0)
	bne.s	.Test
	move.l	ncw_Top(a5),4(a0)
	bra.s	.Test
.4	bset	#2,d2
.Test	tst.w	d2
	beq	.x
	moveq	#4*8,d0
	CALLEXEC AllocVec
	move.l	#MEMF_CLEAR!MEMF_PUBLIC,d1
	move.l	d0,a3
	move.l	d0,a4
	tst.l	d0
	beq	.x
	move.l	#$80BADBAD,(a4)+
	moveq	#100,d0
	move.l	d0,(a4)+
	btst	#1,d2
	beq.s	.2
	move.l	#WA_Left,(a4)+
	move.l	ncw_Left(a5),(a4)+
.2	btst	#2,d2
	beq	.add
	move.l	#WA_Top,(a4)+
	move.l	ncw_Top(a5),(a4)+
.add	moveq	#TAG_MORE,d0
	move.l	d0,(a4)+
	move.l	inm_TagListPtr(a2),(a4)
	move.l	a3,inm_TagListPtr(a2)
.x	rts
GetLeftVal:
	moveq	#80,d0
	add.w	d0,d0
	rts
GetTopVal
	moveq	#50,d0
	rts
GetAllTags:
	move.l	_UtiBase(pc),a6
	move.l	#WA_Flags,d0
	moveq	#0,d1
	move.l	inm_TagListPtr(a2),a0
	CALLLIB	GetTagData
	andi.w	#WFLG_BORDERLESS!WFLG_BACKDROP,d0
	bne	.err

	move.l	#WA_Backdrop,d0
	moveq	#0,d1
	move.l	inm_TagListPtr(a2),a0
	CALLLIB	GetTagData
	tst.l	d0
	bne	.err

	move.l	#WA_Borderless,d0
	moveq	#0,d1
	move.l	inm_TagListPtr(a2),a0
	CALLLIB	GetTagData
	tst.l	d0
	bne	.err

	move.l	#WA_Left,d0
	moveq	#0,d1
	move.l	inm_TagListPtr(a2),a0
	CALLLIB	GetTagData
	move.l	d0,ncw_Left(a5)
	bne	.err

	move.l	#WA_Top,d0
	moveq	#0,d1
	move.l	inm_TagListPtr(a2),a0
	CALLLIB	GetTagData
	move.l	d0,ncw_Top(a5)
	bne	.err

	move.l	#WA_PubScreen,d0
	moveq	#0,d1
	move.l	inm_TagListPtr(a2),a0
	CALLLIB	GetTagData
	tst.l	d0
	beq.s	.ScNa
	move.l	d0,ncw_Screen(a5)
.ScNa
	move.l	#WA_CustomScreen,d0
	moveq	#0,d1
	move.l	inm_TagListPtr(a2),a0
	CALLLIB	GetTagData
	tst.l	d0
	beq.s	.NoCu
	move.l	d0,ncw_Screen(a5)
.NoCu
	move.l	#WA_PubScreenName,d0
	moveq	#0,d1
	move.l	inm_TagListPtr(a2),a0
	CALLLIB	GetTagData
	tst.l	d0
	beq.s	.ScOk
	move.l	d0,ncw_ScreenName(a5)
.ScOk
	move.l	#WA_Width,d0
	moveq	#0,d1
	move.l	inm_TagListPtr(a2),a0
	CALLLIB	GetTagData
	move.l	d0,ncw_Width(a5)
	bne.s	.2
	move.l	#WA_InnerWidth,d0
	moveq	#0,d1
	move.l	inm_TagListPtr(a2),a0
	CALLLIB	GetTagData
	move.l	d0,ncw_Width(a5)
	beq	.err
.2
	move.l	#WA_Height,d0
	moveq	#0,d1
	move.l	inm_TagListPtr(a2),a0
	CALLLIB	GetTagData
	move.l	d0,ncw_Height(a5)
	bne.s	.3
	move.l	#WA_InnerHeight,d0
	moveq	#0,d1
	move.l	inm_TagListPtr(a2),a0
	CALLLIB	GetTagData
	move.l	d0,ncw_Height(a5)
	beq	.err
.3
	move.l	ncw_Screen(a5),d0
	bne.s	.1
	move.l	ncw_ScreenName(a5),d0
	bne.s	.Lock
	move.l	inm_Task(a2),a0
	move.l	pr_WindowPtr(a0),d0
	ble.s	.Lock
	move.l	d0,a0
	move.l	wd_WScreen(a0),ncw_Screen(a5)
	bne.s	.4
.Lock	move.l	ncw_ScreenName(a5),a0
	CALLINT	LockPubScreen
	move.l	d0,ncw_Screen(a5)
	beq.s	.err
	move.l	d0,a1
	move.l	ncw_ScreenName(a5),a0
	CALLLIB	UnlockPubScreen
.4	move.l	ncw_Screen(a5),d0
.1	move.l	d0,a0
	movem.w	sc_Width(a0),d0-1
	sub.l	ncw_Width(a5),d0
	ble.s	.err
	sub.l	ncw_Height(a5),d1
	ble.s	.err
	asr	#1,d0
	asr	#1,d1
	movem.l	d0-1,ncw_Left(a5)
	bra.s	.x
.err	moveq	#0,d0
.x	rts
Need	lea	YouNeed(pc),a0
	move.l	a0,d1
	CALLDOS	PutStr
	bra	Ende

	INCLUDE	INC_NAMES.i
_NotIBase	dc.l	0
NotIName	dc.b	'notifyintuition.library',0
YouNeed		dc.b	'You need "notifyintuition.library" to run this programm.',10,0
UniqueTXT	dc.b	'Error : Ein Programm mit der ID "WA_P" läuft schon.',10,0
LowMem		dc.b	'Error : Speicherplatzmangel',10,0
