
		NOLIST
* This can only be assembled under DEVPAC V3.01 since there are many opcodes
* only DEVPAC 3.01 understands. As far as I know.

DEBUG		SET	0

		OPT	ow-,o+,ow6+,NODEBUG,NOLINE,NOCHKBIT,ALINK

*AUTO:		smake KCX



;* $Revision Header built automatically *************** (do not edit) ************
;*
;* © Copyright by GuntherSoft
;*
;* File             : SnakeSYS:DevPac/Sources/KCX/WBStartUp/KCX.Asm
;* Created on       : Sunday, 04-Oct-92 17:00:53
;* Created by       : Kai Iske
;* Current revision : V2.5
;*
;*
;* Purpose
;* -------
;*     Main KCommodity process, which is responsible for
;*     all activities
;*
;* Revision V2.5
;* --------------
;*     --- Initial release ---
;*
;*********************************************************************************

		XREF	_InvertString
		XREF	_FreeIEvents
		XREF	_HotKey
		XREF	_custom
		XDEF	_CxBase
		XDEF	_SysBase

**********************************************************************
*         Local parameters for font-sensitive window open up         *
**********************************************************************
		CARGS	#-(4+4+2+2+2+2),OffY.W,OffX.W,FontY.W,FontX.W,FontPtr.L

		IFGT	DEBUG
		XREF	KPrintF
		ENDC

		INCLUDE	KCXBase.i
		INCLUDE	KCX_Lib.i
		INCLUDE	KCX.i

		INCLUDE	Misc/MyMacros.i
		INCLUDE	Rexx/Errors.i
		INCLUDE	Rexx/Rxslib.i
		INCLUDE	Rexx/Storage.i
		INCLUDE	HardWare/custom.i

		IFGT	DEBUG
		INCLUDE	Misc/Debug.i
		ENDC



**********************************************************************
*                Macros for defining the patchtable                  *
**********************************************************************
PATCHES		MACRO
NumPatch	SET	1
		IFEQ	NARG-2
		dc.w	\2,_LVO\1
		dc.l	New\1
		dc.w	Old\1
		ELSE
		FAIL
		ENDC
		ENDM
PATCH		MACRO
NumPatch	SET	NumPatch+1
		IFEQ	NARG-2
		dc.w	\2,_LVO\1
		dc.l	New\1
		dc.w	Old\1
		ELSE
		FAIL
		ENDC
		ENDM


		SECTION	MyCode,CODE

Main		move.l	$4.w,_SysBase
		bra	START

**********************************************************************
*                  Versionstring for C:Version-Prg                   *
*       I place it right here, so that it can be found quickly       *
**********************************************************************
VersionString	dc.b	"$VER:KCommodity "
		REVISION
		dc.b	" ("
		REVDATE
		dc.b	")",0
		EVEN

**********************************************************************
*               Fade a layer explode/implode/size/move               *
*                     Vars are passed on stack.                      *
* ScreenAdr                                                          *
* LeftEdge                                                           *
* TopEdge                                                            *
* Width                                                              *
* Height                                                             *
* DeltaX                                                             *
* DeltaY                                                             *
* DeltaWidth                                                         *
* DeltaHeight                                                        *
* Number of Frames to be drawn                                       *
**********************************************************************
LayerFade	CARGS	#8,.Screen.L,.XPos.L,.YPos.L,.LWidth.L,.LHeight.L,.DX.L,.DY.L,.DW.L,.DH.L,.Frames.L
		CARGS	#-36,.Left.L,.Top.L,.Width.L,.Height.L,.LeftStep.L,.TopStep.L,.WidthStep.L,.HeightStep.L,.OldMask.W,.OldMode.W
		bra.s	.Start
.DoSDivMod	lsl.l	#7,d0
		move.l	.Frames(a5),d1
		jmp	_LVOSDivMod32(a6)
.Start		link	a5,#-36
		tst.l	.DX(a5)
		bne.s	.GoOn
		tst.l	.DY(a5)
		bne.s	.GoOn
		tst.l	.DW(a5)
		bne.s	.GoOn
		tst.l	.DH(a5)
		beq	.NoMove
.GoOn		addq.w	#1,SizeUse(a4)
		move.l	_UtilityBase(a4),a6
		move.l	.DX(a5),d0			; Calc X-Step Rate
		bsr.s	.DoSDivMod
		move.l	d0,.LeftStep(a5)

		move.l	.DY(a5),d0			; Y-Step Rate
		bsr.s	.DoSDivMod
		move.l	d0,.TopStep(a5)

		move.l	.DW(a5),d0			; Width Rate
		bsr.s	.DoSDivMod
		move.l	d0,.WidthStep(a5)

		move.l	.DH(a5),d0			; Height Rate
		bsr.s	.DoSDivMod
		move.l	d0,.HeightStep(a5)

		move.l	.Screen(a5),a0
		lea	sc_LayerInfo(a0),a0
		RELLIB	LockLayers,_LayersBase

		move.l	.Screen(a5),a0
		lea	sc_LayerInfo(a0),a0
		DOLIB	LockLayerInfo

		move.l	.Screen(a5),a3			; Get address for screen
		lea	sc_RastPort(a3),a0
		move.b	rp_Mask(a0),d0			; Get old BitPlane-Mask
		ext.w	d0
		move.w	d0,.OldMask(a5)			; Store Mask
		move.b	#2,rp_Mask(a0)			; Only allow BitPlane 2 (White color)
		move.b	rp_DrawMode(a0),d0		; Get old DrawMode
		ext.w	d0
		move.w	d0,.OldMode(a5)			; Store it
		move.l	a0,a1
		move.l	#RP_COMPLEMENT,d0
		RELLIB	SetDrMd,_GfxBase		; Set new DrawMode

		moveq	#1,d6
.Again		move.l	.XPos(a5),d0			; copy dims to workbuffer and
		move.l	.YPos(a5),d1			; calc to our values
		move.l	.LWidth(a5),d2
		move.l	.LHeight(a5),d3
		lsl.l	#7,d0
		lsl.l	#7,d1
		lsl.l	#7,d2
		lsl.l	#7,d3
		move.l	d0,.Left(a5)
		move.l	d1,.Top(a5)
		move.l	d2,.Width(a5)
		move.l	d3,.Height(a5)

		move.l	.Frames(a5),d7
.Loop		lea	sc_RastPort(a3),a1		; Draw Recs. It`s faster than
		move.l	.Left(a5),d0			; using DrawBorder
		move.l	.Top(a5),d1
		lsr.l	#7,d0
		lsr.l	#7,d1
		tst.w	d0
		bge.s	.X1Ok
		moveq	#0,d0
.X1Ok		tst.w	d1
		bge.s	.Y1Ok
		moveq	#0,d1
.Y1Ok		RELLIB	Move,_GfxBase

		lea	sc_RastPort(a3),a1
		move.l	.Left(a5),d0
		add.l	.Width(a5),d0
		move.l	.Top(a5),d1
		lsr.l	#7,d0
		lsr.l	#7,d1
		cmp.w	sc_Width(a3),d0
		blt.s	.W1Ok
		move.w	sc_Width(a3),d0
		subq.w	#1,d0
.W1Ok		tst.w	d1
		bge.s	.Y2Ok
		moveq	#0,d1
.Y2Ok		DOLIB	Draw

		lea	sc_RastPort(a3),a1
		move.l	.Left(a5),d0
		add.l	.Width(a5),d0
		move.l	.Top(a5),d1
		add.l	.Height(a5),d1
		lsr.l	#7,d0
		lsr.l	#7,d1
		cmp.w	sc_Width(a3),d0
		blt.s	.W2Ok
		move.w	sc_Width(a3),d0
		subq.w	#1,d0
.W2Ok		cmp.w	sc_Height(a3),d1
		blt.s	.H1Ok
		move.w	sc_Height(a3),d1
		subq.w	#1,d1
.H1Ok		DOLIB	Draw

		lea	sc_RastPort(a3),a1
		move.l	.Left(a5),d0
		move.l	.Top(a5),d1
		add.l	.Height(a5),d1
		lsr.l	#7,d0
		lsr.l	#7,d1
		tst.w	d0
		bge.s	.X2Ok
		moveq	#0,d0
.X2Ok		cmp.w	sc_Height(a3),d1
		blt.s	.H2Ok
		move.w	sc_Height(a3),d1
		subq.w	#1,d1
.H2Ok		DOLIB	Draw

		lea	sc_RastPort(a3),a1
		move.l	.Left(a5),d0
		move.l	.Top(a5),d1
		lsr.l	#7,d0
		lsr.l	#7,d1
		tst.w	d0
		bge.s	.X3Ok
		moveq	#0,d0
.X3Ok		tst.w	d1
		bge.s	.Y3Ok
		moveq	#0,d1
.Y3Ok		DOLIB	Draw

		move.l	.WidthStep(a5),d0
		move.l	.HeightStep(a5),d1
		add.l	d0,.Width(a5)			; Width/Height decreases
		add.l	d1,.Height(a5)
		move.l	.LeftStep(a5),d0
		move.l	.TopStep(a5),d1
		add.l	d0,.Left(a5)			; And Left/TopEdge moves
		add.l	d1,.Top(a5)			; half the way...
		dbf	d7,.Loop
		dbf	d6,.Again			; Clear frames again

		move.l	.Screen(a5),a0			; Remake Old Mode and Mask
		lea	sc_RastPort(a0),a1
		move.w	.OldMask(a5),d0
		move.b	d0,rp_Mask(a1)
		move.w	.OldMode(a5),d0
		ext.l	d0
		RELLIB	SetDrMd,_GfxBase
		move.l	.Screen(a5),a0
		lea	sc_LayerInfo(a0),a0
		RELLIB	UnlockLayers,_LayersBase

		move.l	.Screen(a5),a0
		lea	sc_LayerInfo(a0),a0
		DOLIB	UnlockLayerInfo

		subq.w	#1,SizeUse(a4)
.NoMove		unlk	a5
		rts
BackPortName	dc.b	"KCX-BackPort",0
PrefsPortName	dc.b	"KCX-PrefsPort",0
		EVEN


**********************************************************************
*                            Main program                            *
**********************************************************************
START		movem.l	d1-d7/a0-a6,-(sp)

		sub.l	a1,a1
		CALLSYS	FindTask
		move.l	d0,a5
		tst.l	pr_CLI(a5)
		bne.s	.FromCLI
		lea	pr_MsgPort(a5),a0
		DOLIB	WaitPort
		lea	pr_MsgPort(a5),a0
		DOLIB	GetMsg
		move.l	d0,MyWBMsg

.FromCLI	move.l	$4.w,a6				; Test, if we`re running under 2.x
		cmp.w	#NEED_LIBVER,LIB_VERSION(a6)
		bcc.s	.IsOver20

		jsr	OnlyKick20
		bra	.EndPrg

* First some needed initializations

.IsOver20	CALLSYS	Forbid

		move.l	#MP_SIZE+4,d0			; Get memory for our MsgPort
		move.l	#MEMF_CLEAR,d1
		DOLIB	AllocVec
		move.l	d0,d7
		tst.l	d0
		bne.s	.GotComBuff
		DOLIB	Permit
		bra	.OpenError
.GotComBuff	moveq	#-1,d0				; Try to allocate signal
		DOLIB	AllocSignal
		move.l	d0,d6
		tst.l	d0
		bge.s	.GotComSig
		move.l	d7,a1
		DOLIB	FreeVec
		DOLIB	Permit
		bra	.OpenError
.GotComSig	move.l	d7,a1
		move.b	#PA_SIGNAL,MP_FLAGS(a1)		; Set Up Message Port structure
		move.b	d6,MP_SIGBIT(a1)
		move.l	ThisTask(a6),MP_SIGTASK(a1)
		lea	MP_MSGLIST(a1),a0
		NEWLIST	a0
		lea	BackPortName(pc),a0
		move.l	a0,LN_NAME(a1)
		move.b	#NT_MSGPORT,LN_TYPE(a1)
		clr.b	LN_PRI(a1)
		moveq	#1,d1
		lsl.l	d6,d1
		move.l	d1,d6
		DOLIB	AddPort				; Add Port to list

		lea	PrefsPortName(pc),a1		; Buddy there ???
		DOLIB	FindPort
		tst.l	d0
		bne	.TakePrefs
		move.l	#_SIZEOF_PAGE,d0		; If not, create our ZeroPage
		move.l	#MEMF_CLEAR,d1
		DOLIB	AllocVec
		move.l	d0,ZeroBase
		bne.s	.GotZeroBase
		move.l	d7,a1
		DOLIB	RemPort
		move.l	d6,d0
		DOLIB	FreeSignal
		move.l	d7,a1
		DOLIB	FreeVec
		pea	10
		bra	.NoNothing
.GotZeroBase	move.l	d0,a4
		move.l	d7,BackPort(a4)			; Store pointer to BackPort
		move.l	d6,BackPortSig(a4)
		bsr	InitZeroPage			; Init Page
		bsr	InitWindows			; And windows
		bsr	MakeHotKeys			; Copy defaults to hotkey buffs
		lea	WindowBase(a4),a0		; Init Window list header
		NEWLIST	a0
		lea	TaskWinBase(a4),a0
		NEWLIST	a0
		lea	TagScrsTasks(a4),a0
		NEWLIST	a0
		CALLSYS	Permit
		bsr	OpenLibs			; Open libs
		tst.l	d0
		beq	.OpenError
		move.b	#1,BackFirst(a4)
		bra.s	.GoStart

.TakePrefs	move.l	d0,a0				; Otherwise get buddy`s ZeroPage
		move.l	MP_SIZE(a0),a4
		move.l	a4,ZeroBase
		move.l	d7,BackPort(a4)
		move.l	d6,BackPortSig(a4)
		CALLSYS	Permit
		move.l	PrefsPort(a4),a0
		move.l	BackPort(a4),a1
		move.l	#COM_BACKTHERE,d0
		RELLIB	ComPut,_KCXBase

* Patch references; Set up locale and open remaining library

.GoStart	move.l	a4,d0
		lea	FormWinWindowTags,a1
		add.l	d0,Zero2+4-FormWinWindowTags(a1)
		add.l	d0,Zero4+4-FormWinWindowTags(a1)

		bsr	SetUpLocale			; Set up the locale

		lea	ComName(pc),a1			; Open Commodities library
		move.l	a1,BackErrArg(a4)
		moveq	#NEED_LIBVER,d0
		CALLSYS	OpenLibrary
		move.l	d0,_CxBase
		beq	.OpenError

* Now to the ToolTypes/CommandLine parsing

		lea	DefPrefsPath(pc),a0		; Copy default pathes
		lea	PrefsPath(a4),a1
		bsr	StrCpy
		lea	DefPrintPath(pc),a0
		lea	PrintPath(a4),a1
		bsr	StrCpy

		CALLSYS	Forbid
		move.l	ThisTask(a6),a5
		move.l	a5,BackTask(a4)
		move.l	BackPort(a4),a0
		move.l	a4,MP_SIZE(a0)			; Store pointer to ZeroBase
		DOLIB	Permit
		tst.l	pr_CLI(a5)			; Did we come from WB ?
		bne.s	.GoOn				; Yep -> go on

		move.l	MyWBMsg(pc),a5			; Parse ToolTypes
		move.l	sm_ArgList(a5),a5		; Get pointer to ArgList
		move.l  a5,d0
		beq.s	.GoOn
		move.l	wa_Lock(a5),d1			; Get lock to Prg-Dir
		RELLIB	CurrentDir,_DOSBase
		move.l	wa_Name(a5),a0			; Get .info-File
		RELLIB	GetDiskObjectNew,_IconBase
		move.l	d0,MyDiskObj
		beq	.EndPrg
		bsr.s	.GadSub
		move.l	d0,a0				; Store pointer to ToolTypes
		move.l	do_ToolTypes(a0),MyToolTypes
		bsr	GetToolTypes
		bra.s	.WildThing
.GadSub		move.l	d0,a0				; Copy Icon`s info to our structure
		lea	do_Gadget(a0),a0
		lea	KCXAppIconDef,a1
		move.l	gg_GadgetRender(a0),AppIconPic-KCXAppIconDef(a1)
		move.w	gg_Width(a0),AppIconWidth-KCXAppIconDef(a1)
		move.w	gg_Height(a0),AppIconHeight-KCXAppIconDef(a1)
		rts
.GoOn		moveq	#WBTOOL,d0
		RELLIB	GetDefDiskObject,_IconBase
		bsr.s	.GadSub
		IFGT	DEBUG
		bra.s	.WildThing
		ENDC
		bsr	GetCommandLine			; Get command line
		tst.l	d7
		beq	.OpenError
		cmp.l	#1,d7
		beq.s	.WildThing
		moveq	#0,d0
		bra	.CloseDown

* Now initialize the main thingies

.WildThing	bsr	InitMem				; Init memory buffers
		tst.l	d0
		beq	.OpenError

		tst.b	BackFirst(a4)
		beq.s	.NoToolKey
		bsr	LoadPrefs			; Read in preferences
		lea	ToolPopUpKey(a4),a0
		tst.b	(a0)
		beq.s	.NoToolKey
		lea	PopUpKey(a4),a1
		bsr	StrCpy
.NoToolKey	lea	StrEditHook,a0			; Initialize the StringEdit-Hook
		move.l	#MyStrEditFunc,h_Entry(a0)
		clr.l	h_SubEntry(a0)
		move.l	a4,h_Data(a0)

		move.l	_GfxBase(a4),a0			; Check for Denise
		move.b	gb_ChipRevBits0(a0),d0
		btst	#GFXB_HR_DENISE,d0
		beq.s	.NoDenise
		ST	BlackBorderOk(a4)
		jsr	ReBlackBorder

.NoDenise	bsr	InitBroker			; Initialize broker
		tst.l	d0
		beq.s	.OpenError

		bsr	InitDrives			; Check for existing external drives
		bsr	SetNoClick			; Set/Clear click flag

		bsr	InitGfx				; Initialize graphics
		tst.l	d0
		beq.s	.OpenError

		bsr	InitAppIcon			; Init our AppIcon

		bsr	InitAppItem			; Init AppTools menuitem

		bsr	SetPatch			; Set library patches

		tst.b	FlickerOff(a4)			; Install FlickerIRQ ?
		beq.s	.NoFlicker
		bsr	InitFlickerIRQ			; Install anti flicker IRQ

.NoFlicker	bsr	InitChecker			; Init the Checker process
		tst.l	CheckerProc(a4)
		beq.s	.OpenError

		bsr	InitRexxProc			; Init Rexx process
		tst.l	RexxProc(a4)
		beq.s	.OpenError

.NoInitPop	clr.b	Disabled(a4)
		jsr	EnableBroker			; Start broker

		move.l	BackTask(a4),a1			; Set user`s priority
		move.l	ToolPri(a4),d0
		CALLSYS	SetTaskPri
		move.l	d0,-(sp)

		tst.b	DoPopUp(a4)
		beq.s	.NoPop
		bsr	OpenMainWin

.NoPop		bsr	CheckBroker			; Do the wild thing

		move.l	BackTask(a4),a1			; Reset old Task-Pri
		move.l	(sp)+,d0			; Necessary when launched from CLI
		CALLSYS	SetTaskPri
		bra.s	.EndPrg

.OpenError	tst.l	ErrorBack(a4)			; Error occured ?
		blt.s	.EndPrg
		jsr	DisplayError			; Display error
.EndPrg		moveq	#0,d0				; No error
.CloseDown	move.l	d0,-(sp)

		pea	SIGHANDSHAKE
		tst.l	RexxProc(a4)
		beq.s	.NoRexxDown
		move.l	RexxProc(a4),a1			; Tell Rexx-Proc to go away
		move.l	(sp),d0
		CALLSYS	Signal
		move.l	(sp),d0				; wait for reply
		DOLIB	Wait

.NoRexxDown	tst.l	CheckerProc(a4)
		beq.s	.NoCheckerDown
		move.l	CheckerProc(a4),a1		; Tell Time-Proc to go away
		move.l	(sp),d0
		CALLSYS	Signal
		move.l	(sp),d0				; wait for reply
		DOLIB	Wait

.NoCheckerDown	tst.l	PrintProc(a4)
		beq.s	.NoPrinterDown
		move.l	PrintProc(a4),a1		; Tell Printer to go away
		move.l	(sp),d0
		CALLSYS	Signal
		move.l	(sp),d0
		DOLIB	Wait

.NoPrinterDown	addq.w	#4,sp
		CALLSYS	Forbid
		tst.l	_KCXBase(a4)
		beq	.NoDiskObj
		bsr	RemFlickerIRQ
		bsr	UnPatch
		bsr	CloseWins			; Close all windows first
		bsr	RemoveDrives			; Remove drive structures
		bsr	RemoveAlarmSmp			; Remove Alarm Sample
		bsr	RemoveAppItems			; Remove AppMenu
		bsr	RemoveAppIcon			; Remove AppIcon-Stuff
		bsr	RemoveGfx			; Remove our gfx
		jsr	DeleteEnvs			; Remove environment vars
		bsr	RemoveBroker			; Close resources

		tst.l	BackPort(a4)
		beq.s	.NoBackPort
		tst.l	PrefsPort(a4)			; Prefs port still there ?
		beq.s	.NoPrefsPort
		move.l	PrefsPort(a4),a0		; Tell Prefs that we leave
		move.l	BackPort(a4),a1
		move.l	#COM_BACKGONE,d0
		RELLIB	ComPut,_KCXBase
.NoPrefsPort	move.l	BackPort(a4),a1			; Now remove Port
		CALLSYS	RemPort
		move.l	BackPort(a4),a0
		moveq	#0,d0
		move.b	MP_SIGBIT(a0),d0
		DOLIB	FreeSignal
		move.l	BackPort(a4),a1
		DOLIB	FreeVec
		clr.l	BackPort(a4)
		clr.l	BackPortSig(a4)

.NoBackPort	bsr	RemoveMem			; Remove those needed resources

		move.l	#NO_ERR,ErrorBack(a4)
		clr.w	QuitBroker(a4)
		clr.b	BackFirst(a4)

		move.l	MyDiskObj(pc),d0		; Got a Disk-Object ?
		beq.s	.NoDiskObj
		move.l	MyDiskObj(pc),a0		; release it
		RELLIB	FreeDiskObject,_IconBase

.NoDiskObj	bsr	ShutDownLocale

		lea	PrefsPortName(pc),a1		; Prefs-Program there ???
		CALLSYS	FindPort
		tst.l	d0
		bne.s	.EndAll
		tst.l	_KCXBase(a4)
		beq.s	.NoWins
		lea	WindowBase(a4),a0
		RELLIB	FreeWindows,_KCXBase		; Free list of windows
		lea	TaskWinBase(a4),a0
		DOLIB	FreeWindows
		lea	TagScrsTasks(a4),a0
		DOLIB	FreeWindows
.NoWins		bsr	CloseLibs			; Close libs
		move.l	ZeroBase,a1			; and remove Data-Block
		CALLSYS	FreeVec

.EndAll		move.l	_CxBase,d0
		beq.s	.NoNothing
		move.l	_CxBase,a1
		CALLSYS	CloseLibrary

.NoNothing	move.l	MyWBMsg(pc),d0
		beq.s	.NoWBMsg
		move.l	d0,a1
		CALLSYS	ReplyMsg
.NoWBMsg	move.l	(sp)+,d0
		movem.l	(sp)+,d1-d7/a0-a6
		rts
MyWBMsg		dc.l	0
MyToolTypes	dc.l	0
MyDiskObj	dc.l	0
MyRDArgs	dc.l	0
Args		dc.l	0
DefPrefsPath	dc.b	"SYS:Prefs/KCommodity",0
DefPrintPath	dc.b	"SYS:Utilities/KCXPrinter",0
		EVEN



**********************************************************************
*                      Attempt to load the libs                      *
**********************************************************************
OpenLibs	move.l	#NOTFOUND_ERR,ErrorBack(a4)
		lea	KCXName(pc),a1
		move.l	a1,BackErrArg(a4)
		move.l	#VERNUM,d0
		CALLSYS	OpenLibrary
		move.l	d0,_KCXBase(a4)
		tst.l	d0
		beq	.LibError

		lea	IntName(pc),a1
		bsr	.LibSub
		move.l	d0,_IntuitionBase(a4)

		lea	DosName(pc),a1
		bsr	.LibSub
		move.l	d0,_DOSBase(a4)

		lea	IconName(pc),a1
		bsr	.LibSub
		move.l	d0,_IconBase(a4)

		lea	LayerName(pc),a1
		bsr	.LibSub
		move.l	d0,_LayersBase(a4)

		lea	GadName(pc),a1
		bsr	.LibSub
		move.l	d0,_GadToolsBase(a4)

		lea	GfxName(pc),a1
		bsr.s	.LibSub
		move.l	d0,_GfxBase(a4)

		lea	RexxSysName(pc),a1
		move.l	a1,BackErrArg(a4)
		moveq	#0,d0
		DOLIB	OpenLibrary
		move.l	d0,_RexxSysBase(a4)
		beq.s	.LibError

		lea	AslName(pc),a1
		bsr.s	.LibSub
		move.l	d0,_AslBase(a4)
		move.l	d0,a0
		moveq	#37,d0
		cmp.w	LIB_VERSION(a0),d0
		blt.s	.NoReqTools
		lea	ReqToolsName(pc),a1
		move.l	a1,BackErrArg(a4)
		DOLIB	OpenLibrary
		move.l	d0,_ReqToolsBase(a4)
		beq.s	.LibError

.NoReqTools	lea	WBName(pc),a1
		bsr.s	.LibSub
		move.l	d0,_WorkBenchBase(a4)

		lea	UtilityName(pc),a1
		bsr.s	.LibSub
		move.l	d0,_UtilityBase(a4)

		lea	IFFParseName(pc),a1
		bsr.s	.LibSub
		move.l	d0,_IFFParseBase(a4)

		lea	KeyMapName(pc),a1
		bsr.s	.LibSub
		move.l	d0,_KeyMapBase(a4)

		lea	LocaleName(pc),a1
		move.l	#NEED_LIBVER,d0
		DOLIB	OpenLibrary
		move.l	d0,_LocaleBase(a4)
		moveq	#1,d0
.LibError	rts
.LibSub		move.l	a1,BackErrArg(a4)
		moveq	#NEED_LIBVER,d0
		CALLSYS	OpenLibrary
		tst.l	d0
		bne.s	.LibSubEnd
		addq.w	#4,sp
.LibSubEnd	rts
**********************************************************************
*                           Library names                            *
**********************************************************************
IntName		dc.b	"intuition.library",0
ComName		dc.b	"commodities.library",0
DosName		dc.b	"dos.library",0
IconName	dc.b	"icon.library",0
LayerName	dc.b	"layers.library",0
GadName		dc.b	"gadtools.library",0
GfxName		dc.b	"graphics.library",0
RexxSysName	dc.b	"rexxsyslib.library",0
WBName		dc.b	"workbench.library",0
AslName		dc.b	"asl.library",0
UtilityName	dc.b	"utility.library",0
IFFParseName	dc.b	"iffparse.library",0
LocaleName	dc.b	"locale.library",0
KeyMapName	dc.b	"keymap.library",0
ReqToolsName	dc.b	"reqtools.library",0
KCXName		KCXNAME
		EVEN





**********************************************************************
*                          Close Libs again                          *
**********************************************************************
CloseLibs	move.l	$4.w,a6
		tst.l	_KCXBase(a4)
		beq.s	.NoKCX
		move.l	_KCXBase(a4),a1
		DOLIB	CloseLibrary

.NoKCX		tst.l	_LocaleBase(a4)
		beq.s	.NoLocale
		move.l	_LocaleBase(a4),a1
		DOLIB	CloseLibrary

.NoLocale	tst.l	_KeyMapBase(a4)
		beq.s	.NoKeyMap
		move.l	_KeyMapBase(a4),a1
		DOLIB	CloseLibrary

.NoKeyMap	tst.l	_IFFParseBase(a4)
		beq.s	.NoIFFParseBase
		move.l	_IFFParseBase(a4),a1
		DOLIB	CloseLibrary

.NoIFFParseBase	tst.l	_UtilityBase(a4)
		beq.s	.NoUtilityBase
		move.l	_UtilityBase(a4),a1
		DOLIB	CloseLibrary

.NoUtilityBase	tst.l	_WorkBenchBase(a4)
		beq.s	.NoWBBase
		move.l	_WorkBenchBase(a4),a1
		DOLIB	CloseLibrary

.NoWBBase	tst.l	_AslBase(a4)
		beq.s	.NoAslBase
		move.l	_AslBase(a4),a1
		DOLIB	CloseLibrary

.NoAslBase	tst.l	_ReqToolsBase(a4)
		beq.s	.NoReqToolsBase
		move.l	_ReqToolsBase(a4),a1
		DOLIB	CloseLibrary

.NoReqToolsBase	tst.l	_RexxSysBase(a4)
		beq.s	.NoRexxSysBase
		move.l	_RexxSysBase(a4),a1
		DOLIB	CloseLibrary

.NoRexxSysBase	tst.l	_GfxBase(a4)
		beq.s	.NoGfxBase
		move.l	_GfxBase(a4),a1
		DOLIB	CloseLibrary

.NoGfxBase	tst.l	_GadToolsBase(a4)
		beq.s	.NoGadToolsBase
		move.l	_GadToolsBase(a4),a1
		DOLIB	CloseLibrary

.NoGadToolsBase	tst.l	_LayersBase(a4)
		beq.s	.NoLayersBase
		move.l	_LayersBase(a4),a1
		DOLIB	CloseLibrary

.NoLayersBase	tst.l	_IconBase(a4)
		beq.s	.NoIconBase
		move.l	_IconBase(a4),a1
		DOLIB	CloseLibrary

.NoIconBase	tst.l	_DOSBase(a4)
		beq.s	.NoDosBase
		move.l	_DOSBase(a4),a1
		DOLIB	CloseLibrary

.NoDosBase	tst.l	_IntuitionBase(a4)
		beq.s	.NoIntBase
		move.l	_IntuitionBase(a4),a1
		DOLIB	CloseLibrary
.NoIntBase	rts



**********************************************************************
*                   Set up things for localization                   *
**********************************************************************
SetUpLocale	tst.l	_LocaleBase(a4)			; Did we have a locale library ?
		beq.s	.NoLocale
		sub.l	a0,a0				; Open default locale
		RELLIB	OpenLocale,_LocaleBase
		move.l	d0,MyLocale
		sub.l	a0,a0				; Try to open our catalog
		lea	CatalogName(pc),a1
		sub.l	a2,a2
		DOLIB	OpenCatalogA
		move.l	d0,MyCatalog
		beq.s	.NoLocale
		LOCLEA	DefLabel,a0			; Copy default drive label to buffer
		lea	FormatDriveName(a4),a1
		bsr	StrCpy
.GotLocale
.NoLocale	rts
MyCatalog	dc.l	0
MyLocale	dc.l	0
CatalogName	dc.b	"kcxmain.catalog",0
		EVEN


**********************************************************************
*                       Shut down localization                       *
**********************************************************************
ShutDownLocale	tst.l	MyLocale			; Close default locale
		beq.s	.NoLocale
		move.l	MyLocale(pc),a0
		RELLIB	CloseLocale,_LocaleBase
.NoLocale	tst.l	MyCatalog			; Close catalog
		beq.s	.NoCatalog
		move.l	MyCatalog,a0
		RELLIB	CloseCatalog,_LocaleBase
.NoCatalog	rts



**********************************************************************
*           Initialize some structures from system-memory            *
**********************************************************************
InitMem		move.l	#AREXXBUFF_ERR,ErrorBack(a4)	; A small dummy buffer
		move.l	#AREXXBUFFSIZE,d0
		move.l	#MEMF_CLEAR,d1
		CALLSYS	AllocVec
		move.l	d0,ARexxBuffer(a4)
.Error		rts




**********************************************************************
*                       Remove used resources                        *
**********************************************************************
RemoveMem	tst.l	ARexxBuffer(a4)			;  Remove buffer for ARexx
		beq.s	.NoReadBuff
		move.l	ARexxBuffer(a4),a1
		CALLSYS	FreeVec
.NoReadBuff	clr.l	ARexxBuffer(a4)
		rts




**********************************************************************
*                Init the Rastport for the title bar                 *
**********************************************************************
InitGfx		link	a5,#-4				; Lock WB Screen
		bsr	LockScreen
		tst.l	d0
		beq	.NoGfx

		move.l	$4.w,a0				; Determine color of menubar
		cmp.w	#39,LIB_VERSION(a0)
		blt.s	.No30
		move.l	#2,MenuBackPen(a4)
		bra.s	.GotCol
.No30		move.l	#1,MenuBackPen(a4)

.GotCol		move.l	d0,a0				; Get font for titlebar
		move.l	sc_BarLayer(a0),a0
		move.l	lr_rp(a0),a0
		move.l	rp_Font(a0),a0			; A0 = Font used in bar layer-rastport
		move.l	a0,OldSysFont(a4)
		lea	TxtAttr,a1			; Store attributes of font
		movem.l	a0/a1,-(sp)
		move.l	LN_NAME(a0),a0
		lea	TxtAttrFont(a4),a2
		move.l	a2,ta_Name(a1)
		move.l	a2,a1
		bsr	StrCpy
		movem.l	(sp)+,a0/a1
		moveq	#0,d7
		move.w	tf_YSize(a0),d7			; Get height of font
		move.w	d7,ta_YSize(a1)			; and for bitplanes of title
		move.w	d7,BitMapHeight(a4)
		move.b	tf_Style(a0),ta_Style(a1)
		move.b	tf_Flags(a0),ta_Flags(a1)

		move.l	-4(a5),a0
		move.l	sc_RastPort+rp_BitMap(a0),a0	; Get depth of workbench
		moveq	#0,d0
		move.b	bm_Depth(a0),d0
		move.w	d0,BitMapDepth(a4)

		moveq	#0,d1
		move.l	-4(a5),a0
		move.w	sc_Width(a0),d1
		move.w	d1,BitMapWidth(a4)		; Store sizes
		moveq	#0,d1
		move.b	sc_BarHeight(a0),d1		; To get the full width and height
							; isn`t quite intelligent, but
							; it doesn`t take too much memory.
							; SO WHAT

		move.l	#BITMAP_ERR,ErrorBack(a4)	; Get mem for BitMap
		move.l	#bm_SIZEOF,d0
		move.l	#MEMF_CLEAR,d1
		CALLSYS	AllocVec
		move.l	d0,MyBitMap(a4)
		beq	.NoGfx

		move.l	#RASTPORT_ERR,ErrorBack(a4)	; Get mem for RastPort
		move.l	#rp_SIZEOF,d0
		move.l	#MEMF_CLEAR,d1
		CALLSYS	AllocVec
		move.l	d0,MyRastPort(a4)
		beq	.NoGfx

		move.l	MyBitMap(a4),a0			; Initialize our BitMap
		move.w	BitMapDepth(a4),d0
		move.w	BitMapWidth(a4),d1
		move.w	BitMapHeight(a4),d2
		RELLIB	InitBitMap,_GfxBase

		move.l	#BITPLANE_ERR,ErrorBack(a4)
		move.l	MyBitMap(a4),a3
		lea	bm_Planes(a3),a3		; Get address of Planes array
		move.w	BitMapDepth(a4),d7		; Make all planes
		subq.w	#1,d7
.MakeBitMaps	move.w	BitMapWidth(a4),d0
		move.w	BitMapHeight(a4),d1
		RELLIB	AllocRaster,_GfxBase
		tst.l	d0
		beq.s	.NoGfx
		move.l	d0,(a3)+
		dbf	d7,.MakeBitMaps

		move.l	MyRastPort(a4),a1		; Init RPort
		RELLIB	InitRastPort,_GfxBase

		move.l	MyRastPort(a4),a1
		move.l	MyBitMap(a4),a0
		move.l	a0,rp_BitMap(a1)		; Add pointer of BitMap to RPort
		move.l	MenuBackPen(a4),d0
		DOLIB	SetRast

		move.l	#FONT_ERR,ErrorBack(a4)
		lea	TxtAttr,a0			; Try to open font in RAM
		DOLIB	OpenFont
		move.l	d0,MyFont(a4)
		beq.s	.NoGfx				; THIS SHOULD NEVER HAPPEN

		move.l	MyRastPort(a4),a1		; Set font for rastport
		move.l	d0,a0
		DOLIB	SetFont

		move.l	MyRastPort(a4),a1		; Set draw mode
		move.l	#RP_JAM1,d0
		DOLIB	SetDrMd

		move.l	MyRastPort(a4),a1		; Set back pen
		moveq	#1,d0
		DOLIB	SetBPen
		move.l	MyRastPort(a4),a1
		moveq	#0,d0
		DOLIB	SetAPen

		move.l	#MEMF_TOTAL,d1			; Get total available mem
		CALLSYS	AvailMem
		move.l	d0,MaxMem(a4)

		moveq	#1,d0
.NoGfx		move.l	d0,-(sp)
		bsr	UnlockScreen
		move.l	(sp)+,d0
		unlk	a5
		rts



**********************************************************************
*                    Removes the Gfx areas again                     *
**********************************************************************
RemoveGfx	tst.l	MyFont(a4)			; Did we have a font
		beq.s	.GotNoFont

		move.l	MyFont(a4),a1			; Close font
		RELLIB	CloseFont,_GfxBase

.GotNoFont	tst.l	MyRastPort(a4)			; Free rastport
		beq.s	.GotNoRastPort
		move.l	MyRastPort(a4),a1
		CALLSYS	FreeVec

.GotNoRastPort	tst.l	MyBitMap(a4)
		beq.s	.AllFreed
		move.l	MyBitMap(a4),a5			; Free bitplanes of BitMap
		lea	bm_Planes(a5),a5
		move.w	BitMapDepth(a4),d7
		subq.w	#1,d7
.ClearBitPlanes	move.l	(a5)+,d0
		beq.s	.FreeBitMap
		move.l	d0,a0
		move.w	BitMapWidth(a4),d0
		move.w	BitMapHeight(a4),d1
		RELLIB	FreeRaster,_GfxBase
		dbf	d7,.ClearBitPlanes
.FreeBitMap	move.l	MyBitMap(a4),a1			; Free bitmap
		CALLSYS	FreeVec
.AllFreed	clr.l	MyFont(a4)
		clr.l	MyRastPort(a4)
		clr.l	MyBitMap(a4)
		rts




**********************************************************************
*                          Init Audio-Data                           *
**********************************************************************
InitAudio	move.l	#AUDIOPORT_ERR,ErrorBack(a4)
		CALLSYS	CreateMsgPort			; Create port
		move.l	d0,AudioPort(a4)
		beq.s	.AudioError
		move.l	#AUDIOREQ_ERR,ErrorBack(a4)	; Create Request
		move.l	d0,a0
		clr.l	LN_NAME(a0)
		clr.b	LN_PRI(a0)
		move.l	#ioa_SIZEOF,d0
		DOLIB	CreateIORequest
		move.l	d0,AudioReq(a4)
		beq.s	.AudioError
		move.l	d0,a1
		move.b	#-50,LN_PRI(a1)
.AudioError	rts



**********************************************************************
*                         Remove Audio-Stuff                         *
**********************************************************************
RemoveAudio	tst.l	AudioReq(a4)			; Remove request
		beq.s	.NoReq
		move.l	AudioReq(a4),a0
		CALLSYS	DeleteIORequest
.NoReq		tst.l	AudioPort(a4)			; Remove port
		beq.s	.NoAudioPort
		move.l	AudioPort(a4),a0
		CALLSYS	DeleteMsgPort
.NoAudioPort	clr.l	AudioReq(a4)
		clr.l	AudioPort(a4)
		rts



**********************************************************************
*                     Initialize the ARexx Port                      *
**********************************************************************
InitARexx	move.l	#AREXXPORT_ERR,ErrorBack(a4)
		CALLSYS	CreateMsgPort			; Create a Msg Port for ARexx
		move.l	d0,ARexxPort(a4)
		beq.s	.ARexxError
		move.l	d0,a1
		moveq	#0,d0
		moveq	#1,d1
		move.b	MP_SIGBIT(a1),d0		; Get Sig flag
		lsl.l	d0,d1
		move.l	d1,ARexxSigFlag(a4)
		lea	ARexxPortName(pc),a0
		move.l	a0,LN_NAME(a1)
		clr.b	LN_PRI(a1)
		DOLIB	AddPort				; Add port to system
		moveq	#1,d0
		clr.l	ErrorBack(a4)
.ARexxError	rts
ARexxPortName	dc.b	"KComm.1",0
		ds.b	10
		EVEN


**********************************************************************
*                 Remove our ARexx port from System                  *
**********************************************************************
RemoveARexx	tst.l	ARexxPort(a4)
		beq.s	.NoARexxPort
		move.l	ARexxPort(a4),a1
		bsr	KillMessages			; Kill remaining messages
		CALLSYS	RemPort				; Remove port
		move.l	ARexxPort(a4),a0
		DOLIB	DeleteMsgPort
.NoARexxPort	clr.l	ARexxPort(a4)
		rts




**********************************************************************
*                 This the table for all the patches                 *
**********************************************************************
PatchTable	PATCHES	OpenScreen,_IntuitionBase
		PATCH	OpenScreenTagList,_IntuitionBase
		PATCH	ScreenToFront,_IntuitionBase
		PATCH	ScreenToBack,_IntuitionBase
		PATCH	WBenchToFront,_IntuitionBase
		PATCH	WBenchToBack,_IntuitionBase
		PATCH	CloseScreen,_IntuitionBase
		PATCH	AutoRequest,_IntuitionBase
		PATCH	EasyRequestArgs,_IntuitionBase
		PATCH	CreateUpfrontLayer,_LayersBase
		PATCH	CreateUpfrontHookLayer,_LayersBase
		PATCH	DeleteLayer,_LayersBase
		PATCH	MoveSizeLayer,_LayersBase
		PATCH	DisplayBeep,_IntuitionBase
		PATCH	MatchFirst,_DOSBase
		PATCH	OpenWorkBench,_IntuitionBase
		PATCH	CloseWorkBench,_IntuitionBase


**********************************************************************
*                          Set all patches                           *
**********************************************************************
SetPatch	lea	PatchTable(pc),a2		; Work all patches
		moveq	#NumPatch-1,d7
.Loop		move.w	0(a2),d0
		move.l	0(a4,d0.w),a1
		move.w	2(a2),a0
		move.l	4(a2),d0
		move.l	a1,d1
		beq.s	.NoPatch
		CALLSYS	SetFunction
		move.w	8(a2),d1
		move.l	d0,0(a4,d1.w)
.NoPatch	lea	10(a2),a2
		dbf	d7,.Loop
		rts


**********************************************************************
*                     Remove all of our patches                      *
**********************************************************************
UnPatch		lea	PatchTable(pc),a2		; Unpatch system library vectors
		moveq	#NumPatch-1,d7
.Loop		move.w	0(a2),d0
		move.l	0(a4,d0.w),a1
		move.w	2(a2),a0
		move.w	8(a2),d1
		move.l	0(a4,d1.w),d0
		beq.s	.NoPatch
		clr.l	0(a4,d1.w)
		move.l	a1,d1
		beq.s	.NoPatch
		CALLSYS	SetFunction
.NoPatch	lea	10(a2),a2
		dbf	d7,.Loop
		rts


**********************************************************************
*            Install VertB interrupt to avoid flickering             *
**********************************************************************
InitFlickerIRQ	tst.b	IRQSet(a4)
		bne.s	.End
		lea	FlickerIRQ(a4),a1		; Start the flicker IRQ
		lea	FlickerName(pc),a0
		move.l	a0,LN_NAME(a1)
		move.b	#NT_INTERRUPT,LN_TYPE(a1)
		clr.b	LN_PRI(a1)
		lea	FlickerRout(pc),a0
		move.l	a0,IS_CODE(a1)
		move.l	a4,IS_DATA(a1)
		moveq	#INTB_VERTB,d0
		CALLSYS	AddIntServer
		ST	IRQSet(a4)
.End		rts
FlickerName	dc.b	"KCX Anti-Flicker IRQ",0
		EVEN



**********************************************************************
*                      Remove Anti-Flicker IRQ                       *
**********************************************************************
RemFlickerIRQ	tst.b	IRQSet(a4)
		beq.s	.End
		lea	FlickerIRQ(a4),a1
		moveq	#INTB_VERTB,d0
		CALLSYS	RemIntServer
		SF	IRQSet(a4)
.End		rts


**********************************************************************
*                 Small and simple, avoid flicker :)                 *
**********************************************************************
FlickerRout	tst.b	FlickerOff(a1)
		beq.s	.End
		OPT	NOCHKIMM
		lea	$dff000,a0
		OPT	CHKIMM
		clr.w	color(a0)			; Set background color to 0
.End		moveq	#0,d0
		rts



**********************************************************************
*                    Initialize and start broker                     *
**********************************************************************
InitBroker	bsr	RemoveBroker			; Begin from scratch

		move.l	#SIGNAL_ERR,ErrorBack(a4)

		moveq	#-1,d0				; Try to allocate signal for
		CALLSYS	AllocSignal			; alarm
		move.l	d0,AlarmSigBit(a4)
		blt	.NoBroker
		moveq	#1,d1
		lsl.l	d0,d1
		move.l	d1,AlarmSigFlag(a4)

		moveq	#-1,d0				; Allocate signal for formatter
		DOLIB	AllocSignal
		move.l	d0,FormatSigBit(a4)
		blt	.NoBroker
		moveq	#1,d1
		lsl.l	d0,d1
		move.l	d1,FormatSigFlag(a4)

		moveq	#-1,d0				; Allocate signal for dragging
		DOLIB	AllocSignal
		move.l	d0,DragSigBit(a4)
		blt	.NoBroker
		moveq	#1,d1
		lsl.l	d0,d1
		move.l	d1,DragSigFlag(a4)

		lea	MySemaphore(a4),a0		; Initialize our Semphore
		DOLIB	InitSemaphore

		lea	PortName(pc),a1			; Find port name
		DOLIB	FindPort			; This is for identification
		move.l	d0,DoublePort(a4)		; whether we are to quit

		move.l	#BROKERPORT_ERR,ErrorBack(a4)
		DOLIB	CreateMsgPort			; Try to set msg-port
		move.l	d0,BrokerPort(a4)
		beq	.NoBroker
		move.l	d0,a1
		moveq	#1,d0
		moveq	#0,d1
		move.b	MP_SIGBIT(a1),d1
		lsl.l	d1,d0
		move.l	d0,BrokerSigFlag(a4)		; Calc sig-flag
		lea	PortName(pc),a0
		move.l	a0,LN_NAME(a1)
		move.b	#5,LN_PRI(a1)
		DOLIB	AddPort				; Add port to system

		move.l	#BROKER_ERR,ErrorBack(a4)
		lea	MyBrokerDef,a0			; Create broker
		LOCLEA	BrokerTitle,nb_Title(a0)
		LOCLEA	BrokerDesc,nb_Descr(a0)
		move.l	BrokerPort(a4),nb_Port(a0)	; add Port-Address
		move.l	CxPri(a4),d0
		move.b	d0,nb_Pri(a0)			; and Priority
		moveq	#0,d0				; No errors
		LNKLIB	CxBroker,_CxBase		; install broker structure
		tst.l	d0
		beq.s	.NoBroker
		move.l	d0,MyBroker(a4)

		lea	DefaultHotKeys,a0		; Init HotKeys
		lea	HotKeyGadVars,a1
		lea	HotKeyGadStat,a2
		move.l	#POPUPKEYID,d6
		move.w	#HOTKEYNUM-1,d7
.InitHKs	move.l	8(a1),-(sp)
		move.l	d6,-(sp)
		move.l	(a0)+,-(sp)
		move.l	(a2)+,-(sp)
		bsr	InstallHotKey
		lea	16(sp),sp
		tst.l	d0
		beq.s	.NoBroker
		lea	12(a1),a1
		addq.l	#1,d6
		dbf	d7,.InitHKs

		move.l	#BROKERUP_ERR,ErrorBack(a4)
		move.l	MyBroker(a4),a0			; Check for an error in broker
		DOLIB	CxObjError
		tst.l	d0
		bne.s	.NoBroker

		jsr	DisableBroker			; Disable broker for now
		move.b	#1,Disabled(a4)
		moveq	#1,d0				; No error
		rts
.NoBroker	moveq	#0,d0				; error occured
		rts
PortName	dc.b	"KComm-BrokerPort",0
		EVEN



**********************************************************************
*                         Remove the broker                          *
**********************************************************************
RemoveBroker	tst.l	MyBroker(a4)
		beq.s	.NoBroker
		clr.b	Disabled(a4)			; Disable broker
		jsr	DisableBroker
		move.l	MyBroker(a4),a0			; Delete broker
		LNKLIB	DeleteCxObjAll,_CxBase
.NoBroker	tst.l	BrokerPort(a4)			; Remove broker and all
		beq.s	.NoPort				; attached objects
		move.l	BrokerPort(a4),a1
		bsr	KillMessages			; remove messages
		CALLSYS	RemPort
		move.l	BrokerPort(a4),a0		; Finally delete the
		DOLIB	DeleteMsgPort			; Messageport
.NoPort		tst.l	AlarmSigBit(a4)
		blt.s	.NoSigBit
		move.l	AlarmSigBit(a4),d0
		CALLSYS	FreeSignal			; Free Alarm-Signal
.NoSigBit	tst.l	FormatSigBit(a4)
		blt.s	.NoFormSig
		move.l	FormatSigBit(a4),d0
		CALLSYS	FreeSignal
.NoFormSig	tst.l	DragSigBit(a4)
		blt.s	.NoDragSig
		move.l	DragSigBit(a4),d0
		CALLSYS	FreeSignal
.NoDragSig	clr.l	MyBroker(a4)
		clr.l	BrokerPort(a4)
		moveq	#-1,d0
		move.l	d0,AlarmSigBit(a4)
		move.l	d0,DragSigBit(a4)
		move.l	d0,FormatSigBit(a4)
		rts



**********************************************************************
*                      Install the InputHandler                      *
**********************************************************************
InitHandler	move.l	#INPUTPORT_ERR,ErrorBack(a4)
		DOLIB	CreateMsgPort			; Create a Port
		move.l	d0,InputPort(a4)
		beq.s	.Error
		move.l	d0,a1
		clr.b	LN_PRI(a1)
		clr.l	LN_NAME(a1)

		move.l	InputPort(a4),a0
		move.l	#IOSTD_SIZE,d0
		DOLIB	CreateIORequest
		move.l	d0,InputReq(a4)
		tst.l	d0
		beq.s	.Error

		move.l	#NOTFOUND_ERR,ErrorBack(a4)
.LoopOpen	lea	InputName(pc),a0		; Try to open the device
		move.l	a0,BackErrArg(a4)
		move.l	InputReq(a4),a1
		moveq	#0,d0
		moveq	#0,d1
		DOLIB	OpenDevice
		tst.l	d0
		bne.s	.LoopOpen

		move.l	#HANDLER_ERR,ErrorBack(a4)
		lea	HandlerIRQ(a4),a0		; Set up Handler
		move.l	a4,IS_DATA(a0)
		move.l	#RawEventAction,IS_CODE(a0)
		move.l	HandlerPri(a4),d0		; Set priority
		move.b	d0,LN_PRI(a0)
		lea	HandlerName(pc),a2
		move.l	a2,LN_NAME(a0)
		move.l	InputReq(a4),a1
		move.l	a0,IO_DATA(a1)
		move.w	#IND_ADDHANDLER,IO_COMMAND(a1)
		DOLIB	DoIO
.End		moveq	#1,d0
		rts
.Error		moveq	#0,d0
		rts
InputName	dc.b	"input.device",0
HandlerName	dc.b	"KComm-InputHandler",0
		EVEN



**********************************************************************
*                        Remove InputHandler                         *
**********************************************************************
RemoveHandler	move.l	InputReq(a4),a1
		tst.l	IO_DEVICE(a1)
		beq.s	.NoDevice
		lea	HandlerIRQ(a4),a0
		move.l	a0,IO_DATA(a1)			; Remove Handler
		move.w	#IND_REMHANDLER,IO_COMMAND(a1)
		CALLSYS	DoIO
		move.l	InputReq(a4),a1			; Close device
		CALLSYS	CloseDevice
.NoDevice	tst.l	InputReq(a4)
		beq.s	.NoReq
		move.l	InputReq(a4),a0
		CALLSYS	DeleteIORequest
.NoReq		tst.l	InputPort(a4)
		beq.s	.NoPort				; Delete message
		move.l	InputPort(a4),a0
		CALLSYS	DeleteMsgPort
.NoPort		clr.l	InputPort(a4)
		clr.l	InputReq(a4)
		rts



**********************************************************************
*           Add an AppIcon. This one uses the Broker-Port            *
**********************************************************************
InitAppIcon	CALLSYS	CreateMsgPort			; Create Msg-Port
		move.l	d0,AppPort(a4)
		beq.s	.Error
		move.l	d0,a1
		clr.b	LN_PRI(a1)
		clr.l	LN_NAME(a1)
		moveq	#0,d0
		moveq	#1,d1
		move.b	MP_SIGBIT(a1),d0
		lsl.l	d0,d1
		move.l	d1,AppSigFlag(a4)		; Get SigFlag
		moveq	#1,d0
		tst.b	DispAppIcon(a4)
		beq.s	.End
		jsr	OnAppIcon
		bra.s	.End
.Error		move.l	#APPICON_ERR,ErrorBack(a4)
		jsr	DisplayError
.End		rts



**********************************************************************
*                         Remove our AppIcon                         *
**********************************************************************
RemoveAppIcon	jsr	OffAppIcon
		tst.l	AppPort(a4)
		beq.s	.NoPort
		move.l	AppPort(a4),a1			; Clear all pending messages
		bsr	KillMessages
		move.l	AppPort(a4),a0
		CALLSYS	DeleteMsgPort
.NoPort		clr.l	AppPort(a4)
		rts




**********************************************************************
*               Add some menu items to the tools menu                *
**********************************************************************
InitAppItem	tst.b	DispAppMenu(a4)
		beq.s	.End
		move.l	#MAINITEM,d0			; Create an item
		moveq	#0,d1
		LOCLEA	AppMenuTxt,a0
		move.l	AppPort(a4),a1
		sub.l	a2,a2
		RELLIB	AddAppMenuItemA,_WorkBenchBase
		move.l	d0,KCXAppMenuItem(a4)
		bne.s	.End
		move.l	#APPMENU_ERR,ErrorBack(a4)
		bsr	DisplayError
.End		rts




**********************************************************************
*                    Remove our menu items again                     *
**********************************************************************
RemoveAppItems	tst.l	KCXAppMenuItem(a4)
		beq.s	.NoRemove
		move.l	KCXAppMenuItem(a4),a0
		RELLIB	RemoveAppMenuItem,_WorkBenchBase
.NoRemove	clr.l	KCXAppMenuItem(a4)
		rts




**********************************************************************
*          Try to set a hotkey. Inputs are given on stack :          *
* 1. HotKey Definition                                               *
* 2. Default HotKey Definition                                       *
* 3. HotKey ID                                                       *
* 4. Address of filter                                               *
**********************************************************************
InstallHotKey	CARGS	#68,HotKey.l,DefHotKey.l,ID.l,Filter.l
		movem.l	RegsAll,-(sp)
		link	a5,#-HOTKEYSIZE
		move.l	#HOTKEY_ERR,ErrorBack(a4)
		move.l	ID(a5),-(sp)
		move.l	BrokerPort(a4),-(sp)
		move.l	HotKey(a5),a0
		add.l	a4,a0
		lea	NonName(pc),a1
		moveq	#-1,d7
		bsr	StrCmp
		tst.l	d0
		bne.s	.NormalHotKey
		lea	NonHotKey(pc),a0
.NormalHotKey	move.l	a0,-(sp)
		jsr	_HotKey
		lea	12(sp),sp
		tst.l	d0
		bne.s	.GotUserHotKey			; Got it -> go on
		move.l	HotKey(a5),a0			; Clone user-hotkey
		add.l	a4,a0
		lea	-HOTKEYSIZE(a5),a1
		bsr	StrCpy
		move.l	DefHotKey(a5),a0		; Try default
		move.l	a0,BackErrArg(a4)
		move.l	HotKey(a5),a1
		add.l	a4,a1
		bsr	StrCpy				; First copy it
		move.l	ID(a5),-(sp)
		move.l	BrokerPort(a4),-(sp)
		move.l	DefHotKey(a5),-(sp)
		jsr	_HotKey
		lea	12(sp),sp
		tst.l	d0
		bne.s	.GotDefHotKey			; Error ???
		bsr	DisplayError			; Display error
		bra.s	.Error
.GotDefHotKey	move.l	#USERHOTKEY_ERR,ErrorBack(a4)	; Tell user we`re using ours
		lea	-HOTKEYSIZE(a5),a0
		move.l	a0,BackErrArg(a4)
		move.l	DefHotKey(a5),BackErrArg2(a4)
		bsr	DisplayError
.GotUserHotKey	move.l	Filter(a5),a0
		add.l	a4,a0
		move.l	d0,(a0)
		move.l	MyBroker(a4),a0
		move.l	d0,a1
		LNKLIB	AttachCxObj,_CxBase		; Attach hotkey stuff
		unlk	a5
		movem.l	(sp)+,RegsAll
		moveq	#1,d0
		rts
.Error		unlk	a5
		movem.l	(sp)+,RegsAll
		moveq	#0,d0
		rts
NonName		dc.b	"none",0
NonHotKey	dc.b	"lshift lalt lcommand rshift ralt rcommand control numericpad esc",0
		EVEN





**********************************************************************
*                        Init Windowpositions                        *
**********************************************************************
InitWindows	lea	WinPosTab(pc),a2		; Copy default window pos/sizes to "BSS"
		lea	MainWinLeft(a4),a3
		moveq	#43,d0
.loop1		move.w	(a2)+,(a3)+
		dbf	d0,.loop1
		lea	WinSizeTab(pc),a2
		lea	MainWinWidth(a4),a3
		moveq	#43,d0
.loop2		move.w	(a2)+,(a3)+
		dbf	d0,.loop2
		rts
WinPosTab	dc.w	58,37,44,46,57,44,133,67
		dc.w	110,82,118,53,167,61,70,51
		dc.w	78,68,168,40,138,13,120,65
		dc.w	397,11,120,17,65,50,166,68
		dc.w	0,0,70,23,55,51,139,73
		dc.w	72,55,103,75
WinSizeTab	dc.w	551,126,574,45,583,126,484,79
		dc.w	421,35,424,94,348,78,527,147
		dc.w	562,95,356,111,364,173,507,100
		dc.w	319,106,485,180,511,100,309,23
		dc.w	0,0,608,162,592,96,405,82
		dc.w	523,180,450,101





**********************************************************************
*                      Init ZeroPage-Parameters                      *
**********************************************************************
InitZeroPage	moveq	#-1,d0
		move.l	d0,FCycleSecs(a4)		; Init some longs
		move.l	d0,BCycleSecs(a4)
		move.l	d0,MCycleSecs(a4)
		move.w	d0,OldX(a4)
		move.l	d0,ThisHour(a4)
		move.l	d0,AlarmSigBit(a4)
		move.l	d0,FormatSigBit(a4)
		move.l	d0,DragSigBit(a4)
		move.l	d0,LastSelScrSec(a4)
		move.l	d0,LastSelScrMin(a4)
		move.w	d0,LastSelScr(a4)

		moveq	#1,d0
		move.b	d0,DoPopUp(a4)			; Set some bool flags
		move.b	d0,TimeReset(a4)
		move.b	d0,DispAppIcon(a4)
		move.b	d0,DispAppMenu(a4)
		move.b	d0,CheckQuit(a4)

		move.b	d0,DoActivate(a4)		; Init default settings
		move.b	d0,ShowClock(a4)
		move.b	d0,DoShowTime(a4)
		move.b	d0,ShowSecs(a4)
		move.b	d0,DoShowAlarm(a4)
		move.b	d0,CycleWin(a4)
		move.b	d0,KeyClick(a4)
		move.b	d0,BillPopUp(a4)
		move.b	d0,WBFront(a4)
		move.b	d0,ESCClose(a4)

		move.b	d0,MakeIcons(a4)
		move.b	d0,QuickFormat(a4)
		move.b	d0,FFSDrive(a4)
		move.b	d0,WatchDisks(a4)
		move.b	d0,MMBShift(a4)
		move.b	d0,LogCalls(a4)
		move.b	d0,Saturday(a4)
		move.b	d0,Sunday(a4)
		move.b	d0,MouseEver(a4)
		move.b	d0,DriveNoClick(a4)
		move.b	d0,GfxCenter(a4)
		move.b	d0,GfxFormFeed(a4)
		move.b	d0,GfxKeepAspect(a4)
		move.b	d0,Watch0(a4)
		move.b	d0,WindowReAct(a4)

		moveq	#2,d1
		move.w	#300,ScreenTime+2(a4)
		move.b	#10,MouseTime+3(a4)
		move.b	#64,ClickVol+3(a4)
		move.b	#5,DimmValue+3(a4)
		move.b	#20,ExpFrames+3(a4)
		move.b	#20,ImpFrames+3(a4)
		move.b	#15,SizeFrames+3(a4)
		move.b	#15,MoveFrames+3(a4)
		move.b	#4,Accelerate+3(a4)
		move.b	d1,ThresHold+3(a4)
		move.w	#IECODE_LBUTTON,FrontCode(a4)
		move.w	#IECODE_RBUTTON,BackCode(a4)
		move.w	#IEQUALIFIER_LEFTBUTTON,BackQual(a4)
		move.w	#IEQUALIFIER_LALT,ESCQual(a4)
		move.w	d1,FrontClicks(a4)
		move.w	d1,BackClicks(a4)
		move.w	d1,MiddleClicks(a4)
		move.l	#8*6,CheapStop(a4)
		move.l	#18*6,CheapStart(a4)
		move.l	#MAXBARLEN,MaxBarLen(a4)

		move.l	$4.w,a6
		cmp.w	#39,LIB_VERSION(a6)
		blt.s	.No30
		move.l	d0,TextFrontPen(a4)
		move.l	d1,TextBackPen(a4)
		clr.l	GaugeFrontPen(a4)
		move.b	#3,GaugeBackPen+3(a4)
		bra.s	.GotCols
.No30		clr.l	TextFrontPen(a4)
		move.l	d0,TextBackPen(a4)
		move.b	#3,GaugeFrontPen+3(a4)
		clr.l	GaugeBackPen(a4)

.GotCols	move.w	#IEQUALIFIER_LSHIFT,FullDragQual(a4)
		move.w	#360,CostUnit1+2(a4)
		move.w	#720,CostUnit1+6(a4)
		move.b	#60,CostUnit2+3(a4)
		move.b	#120,CostUnit2+7(a4)
		move.b	#21,CostUnit3+3(a4)
		move.b	#42,CostUnit3+7(a4)
		move.b	#23,CostUnit+3(a4)
		move.b	#4,GfxScale+3(a4)
		move.b	#2,GfxDensity+3(a4)
		move.b	#4,GfxThreshold+3(a4)
		move.l	#NO_ERR,ErrorBack(a4)
		move.b	#55,HandlerPri+3(a4)
		move.w	#IEQUALIFIER_LALT,HoldXQual(a4)
		move.w	#IEQUALIFIER_CONTROL,HoldYQual(a4)

		move.w	#640-236,BillZoomSize(a4)
		move.w	#11,BillZoomSize+2(a4)

		lea	DefPubScrName(pc),a0		; Init default public screen list
		lea	PubScreenName(a4),a1
		bsr.s	GoStrCpy
		lea	DefSystemComm(pc),a0		; Init default system command
		lea	SystemComm(a4),a1
		bsr.s	GoStrCpy
		lea	DefLogFileName(pc),a0		; Init default LogFile name
		lea	LogFileName(a4),a1
		bsr.s	GoStrCpy
		lea	DefPrefsFName(pc),a0		; Init default Prefs filename
		lea	PrefsFileName(a4),a1
GoStrCpy	bra	StrCpy
DefPubScrName	dc.b	"TERM",0
DefLogFileName	dc.b	"S:KCXLOGFILE",0
DefPrefsFName	dc.b	"KCX.PREFS",0
		EVEN



**********************************************************************
*               Copy default hotkeys to "user" buffers               *
**********************************************************************
MakeHotKeys	lea	DefaultHotKeys,a2
		lea	HotKeyGadStat,a3
		move.w	#HOTKEYNUM-1,d7
.Loop		move.l	(a2)+,a0
		move.l	(a3)+,a1
		add.l	a4,a1
		bsr	StrCpy
		dbf	d7,.Loop
		rts



**********************************************************************
*                      Get the given tooltypes                       *
**********************************************************************
GetToolTypes	tst.l	MyToolTypes
		bne.s	GoOnToolTypes
		rts

GetToolSub1	move.l	MyToolTypes,a0
		RELLIB	FindToolType,_IconBase
		move.l	d0,d1
		rts

GetToolSub2	move.l	a0,d2
		RELLIB	StrToLong,_DOSBase
		tst.l	d0
		rts

GoOnToolTypes	lea	ToolPriStr(pc),a1		; Change Tool-Pri ?
		bsr.s	GetToolSub1
		beq.s	.NoToolPri			; No -> go on
		lea	ToolPri(a4),a0
		bsr.s	GetToolSub2
		bge.s	.NoToolPri
		clr.l	ToolPri(a4)
.NoToolPri	lea	HandlerPriStr(pc),a1
		bsr.s	GetToolSub1
		beq.s	.NoHandlerPri
		lea	HandlerPri(a4),a0
		bsr.s	GetToolSub2
		bge.s	.NoHandlerPri
		cmp.l	#51,HandlerPri(a4)
		bge.s	.NoHandlerPri
		move.l	#55,HandlerPri(a4)
.NoHandlerPri	lea	PriStr(pc),a1			; Change broker pri ?
		bsr.s	GetToolSub1
		beq.s	.NoCxPri			; No -> go on
		lea	CxPri(a4),a0
		bsr.s	GetToolSub2
		bge.s	.NoCxPri
		clr.l	CxPri(a4)
.NoCxPri	lea	PortNameStr(pc),a1
		bsr.s	GetToolSub1
		beq.s	.NoPortName
		move.l	d0,a0
		lea	ARexxPortName(pc),a1
		moveq	#15,d7				; Copy only 16 Chars
		bsr	StrNCpy
.NoPortName	lea	KeyStr(pc),a1
		bsr	GetToolSub1
		beq.s	.NoPopKey			; Nope -> go on
		move.l	d0,a0
		lea	ToolPopUpKey(a4),a1		; Copy PopUpKey
		bsr	StrCpy
.NoPopKey	lea	PopUpStr(pc),a1
		bsr	GetToolSub1
		beq.s	.DoNotPopUp
		move.l	d0,a0				; Do we have a NO flag
		lea	NoString(pc),a1
		DOLIB	MatchToolValue
		tst.l	d0
		beq.s	.DoNotPopUp
		clr.b	DoPopUp(a4)			; Let the window pop up
.DoNotPopUp	lea	IconStr(pc),a1			; No icon ?
		bsr	GetToolSub1
		beq.s	.NoAppIcon
		clr.b	DispAppIcon(a4)
.NoAppIcon	lea	MenuStr(pc),a1			; No menu ???
		bsr	GetToolSub1
		beq.s	.NoAppMenu
		clr.b	DispAppMenu(a4)
.NoAppMenu	lea	IconXStr(pc),a1			; Icon X-Position ?
		bsr	GetToolSub1
		beq.s	.NoIconX
		lea	IconXPos,a0
		bsr	GetToolSub2
.NoIconX	lea	IconYStr(pc),a1			; Icon Y-Position ?
		bsr	GetToolSub1
		beq.s	.NoIconY
		lea	IconYPos,a0
		bsr	GetToolSub2
.NoIconY	lea	NoReqStr(pc),a1			; Don`t show requester ?
		bsr	GetToolSub1
		beq.s	.NoReq
		SF	CheckQuit(a4)
.NoReq		lea	PrefsStr(pc),a1			; Alternative Prefs-File ?
		bsr	GetToolSub1
		beq.s	.NoPrefs
		tst.b	FirstPrefs(a4)
		bne.s	.NoPrefs
		move.l	d0,a0
		lea	PrefsFileName(a4),a1
		bsr	StrCpy
		ST	HadHardPrefs(a4)
.NoPrefs	lea	PrefsPathStr(pc),a1		; New path to Prefs-Prg ?
		bsr	GetToolSub1
		beq.s	.NoPrefsPath
		move.l	d0,a0
		lea	PrefsPath(a4),a1
		bsr	StrCpy
.NoPrefsPath	lea	PrintPathStr(pc),a1		; New path to printer ?
		bsr	GetToolSub1
		beq.s	.NoPrintPath
		move.l	d0,a0
		lea	PrintPath(a4),a1
		bsr	StrCpy
.NoPrintPath	rts
**********************************************************************
*                       Parse the command line                       *
**********************************************************************
GetCommandLine	move.l	#((HELP_ARG+1)*4),d0		; Get buffer for parsed arguments
		move.l	#MEMF_CLEAR,d1
		CALLSYS	AllocVec
		move.l	d0,Args
		moveq	#0,d7
		move.l	#MEMORY_ERR,ErrorBack(a4)	; Set Errornumber
		tst.l	d0
		beq	.HelpDone
		move.l	#CLITemplate,d1			; Parse commandline
		move.l	Args(pc),d2
		moveq	#0,d3
		RELLIB	ReadArgs,_DOSBase
		move.l	#NO_ERR,ErrorBack(a4)		; Set errornumber
		move.l	d0,MyRDArgs
		beq	.Error

		move.l	Args(pc),a5			; Now check args

		move.l	(CXPRI_ARG*4)(a5),d0		; Get Priority for broker ?
		beq.s	.NoCxPri
		move.l	d0,a0
		move.l	(a0),CxPri(a4)
.NoCxPri	move.l	(CXKEY_ARG*4)(a5),d0		; New PopKey ???
		beq.s	.NoCxKey
		move.l	d0,a0
		lea	ToolPopUpKey(a4),a1
		bsr	StrCpy
.NoCxKey	move.l	(CXPOP_ARG*4)(a5),d0		; Do popup ???
		beq.s	.CxPopUp
		move.l	d0,a0
		lea	NoString(pc),a1
		moveq	#-1,d7
		bsr	StrCmp
		tst.l	d0
		bne.s	.CxPopUp
		clr.b	DoPopUp(a4)			; Clear flag
.CxPopUp	move.l	(PORT_ARG*4)(a5),d0		; New Portname ???
		beq.s	.NoPortName
		move.l	d0,a0
		lea	ARexxPortName(pc),a1
		moveq	#15,d7				; Only 16 chars allowed
		bsr	StrNCpy
.NoPortName	move.l	(PRGPRI_ARG*4)(a5),d0		; New ToolPri(a4) ???
		beq.s	.NoToolPri
		move.l	d0,a0
		move.l	(a0),ToolPri(a4)
.NoToolPri	move.l	(HANDPRI_ARG*4)(a5),d0		; New Handler priority ?
		beq.s	.NoHandlerPri
		move.l	d0,a0
		move.l	(a0),d0
		cmp.l	#51,d0
		blt.s	.NoHandlerPri
		move.l	d0,HandlerPri(a4)
.NoHandlerPri	tst.l	(ICON_ARG*4)(a5)		; Display Icon ???
		beq.s	.Icon
		clr.b	DispAppIcon(a4)
.Icon		tst.l	(MENU_ARG*4)(a5)		; Attach menu ???
		beq.s	.Menu
		clr.b	DispAppMenu(a4)
.Menu		move.l	(PREFS_ARG*4)(a5),d0		; Alternative Prefs-File ?
		beq.s	.NoPrefs
		move.l	d0,a0
		lea	PrefsFileName(a4),a1
		bsr	StrCpy
		ST	HadHardPrefs(a4)
.NoPrefs	move.l	(PREFSPATH_ARG*4)(a5),d0	; New Prefs path ?
		beq.s	.NoPrefsPath
		move.l	d0,a0
		lea	PrefsPath(a4),a1
		bsr	StrCpy
.NoPrefsPath	move.l	(PRINTPATH_ARG*4)(a5),d0	; New Printer path ?
		beq.s	.NoPrinterPath
		move.l	d0,a0
		lea	PrintPath(a4),a1
		bsr	StrCpy
.NoPrinterPath	move.l	(ICONX_ARG*4)(a5),d0		; Icon X-Position ?
		beq.s	.NoIconX
		move.l	d0,a0
		move.l	(a0),IconXPos
.NoIconX	move.l	(ICONY_ARG*4)(a5),d0		; Icon Y-Position ?
		beq.s	.NoIconY
		move.l	d0,a0
		move.l	(a0),IconYPos
.NoIconY	tst.l	(NOREQ_ARG*4)(a5)		; Disable Quit-Requester ?
		beq.s	.NoQuit
		SF	CheckQuit(a4)
.NoQuit		tst.l	(HELP_ARG*4)(a5)		; Display Help ???
		beq.s	.NoCommand
		RELLIB	Output,_DOSBase
		move.l	d0,d1
		LOCLEA	CliHelpPage,a0
		move.l	a0,d2
		DOLIB	FPuts
		moveq	#2,d7
		move.l	#NO_ERR,ErrorBack(a4)
		bra.s	.HelpDone
.NoCommand	moveq	#1,d7
		bra.s	.HelpDone
.Error		DOLIB	IoErr				; On error get code
		move.l	d0,d1				; ... and display the message
		LOCLEA	CommandHeader,a0
		move.l	a0,d2
		DOLIB	PrintFault
		moveq	#0,d7
.HelpDone	move.l	MyRDArgs(pc),d1
		beq.s	.NoRDArgs
		RELLIB	FreeArgs,_DOSBase
.NoRDArgs	move.l	Args(pc),d0
		beq.s	.NoArgs
		move.l	d0,a1			; Free Vector-Table
		CALLSYS	FreeVec
.NoArgs		rts
**********************************************************************
*             Definition for the template and ToolTypes              *
**********************************************************************
NoString	dc.b	"NO",0
PriStr		dc.b	"CX_PRIORITY",0
KeyStr		dc.b	"CX_POPKEY",0
PopUpStr	dc.b	"CX_POPUP",0
PortNameStr	dc.b	"PORTNAME",0
ToolPriStr	dc.b	"TOOLPRI",0
HandlerPriStr	dc.b	"HANDLERPRI",0
IconStr		dc.b	"NOICON",0
MenuStr		dc.b	"NOMENU",0
PrefsStr	dc.b	"SETTINGS",0
PrefsPathStr	dc.b	"PREFSPATH",0
PrintPathStr	dc.b	"PRINTERPATH",0
IconXStr	dc.b	"ICONX",0
IconYStr	dc.b	"ICONY",0
NoReqStr	dc.b	"NOREQ",0
CLITemplate	dc.b	"CP=CX_PRIORITY/N,CK=CX_POPKEY/K,CU=CX_POPUP/K,P=PORTNAME/K,T=TOOLPRI/N,H=HANDLERPRI/N,N=NOICON/S,M=NOMENU/S,S=SETTINGS/K,E=PREFSPATH/K,R=PRINTERPATH/R,X=ICONX/N,Y=ICONY/N,NOREQ/S,HELP/S",0
		EVEN
CXPRI_ARG	EQU	0
CXKEY_ARG	EQU	1
CXPOP_ARG	EQU	2
PORT_ARG	EQU	3
PRGPRI_ARG	EQU	4
HANDPRI_ARG	EQU	5
ICON_ARG	EQU	6
MENU_ARG	EQU	7
PREFS_ARG	EQU	8
PREFSPATH_ARG	EQU	9
PRINTPATH_ARG	EQU	10
ICONX_ARG	EQU	11
ICONY_ARG	EQU	12
NOREQ_ARG	EQU	13
HELP_ARG	EQU	14




**********************************************************************
*   This routine checks for existing drives and initializes stuff    *
*                       for each drive present                       *
**********************************************************************
InitDrives	move.l	#TRACKPORT_ERR,ErrorBack(a4)	; Create a port for trackdisk device
		CALLSYS	CreateMsgPort
		move.l	d0,TrackPort(a4)
		beq	.Error
		move.l	d0,a0
		clr.l	LN_NAME(a0)
		clr.b	LN_PRI(a0)

		clr.w	NumDrives(a4)
		lea	DriveNames(pc),a0
		lea	DriveList(a4),a2
		lea	DriveRequests(a4),a3
		lea	DriveChange(a4),a5
		moveq	#0,d7				; Drive number
.LoopDrives	movem.l	a0-a3,-(sp)			; Create an IORequest for the drive in question
		move.l	TrackPort(a4),a0
		move.l	#IOTD_SIZE,d0
		CALLSYS	CreateIORequest
		movem.l	(sp)+,a0-a3
		tst.l	d0
		beq.s	.Error
		move.l	d0,(a3)
		movem.l	a0-a3,-(sp)
		lea	TrackName(pc),a0		; Try to open device for that drive
		move.l	d0,a1
		move.l	d7,d0
		move.l	#TDF_ALLOW_NON_3_5,d1
		DOLIB	OpenDevice
		movem.l	(sp)+,a0-a3
		tst.l	d0
		beq.s	.GotDrive
		move.l	a0,-(sp)
		move.l	(a3),a0				; If not found -> delete IORequest
		DOLIB	DeleteIORequest
		move.l	(sp)+,a0
		clr.l	(a3)
		bra.s	.NextDrive
.GotDrive	move.l	(a0),(a2)+			; Copy pointer to name to CycleGadget`s list
		move.l	d7,d0				; Make gadget available
		mulu	#28,d0
		lea	WatchGads,a1
		clr.l	4(a1,d0)
		lea	DriveThere(a4),a1		; Store, which drivenumber is within this slot
		move.w	NumDrives(a4),d0
		move.b	d7,0(a1,d0)
		move.l	(a3),a1
		move.w	#TD_CHANGENUM,IO_COMMAND(a1)	; Get actual number of diskchanges for that drive
		clr.b	IO_FLAGS(a1)
		movem.l	a0-a3,-(sp)
		DOLIB	DoIO
		movem.l	(sp)+,a0-a3
		move.l	IO_ACTUAL(a1),(a5)		; Store number within our buffer
		addq.l	#4,a3
		addq.l	#4,a5
		addq.w	#1,NumDrives(a4)		; We had another one -> increase number of drives
.NextDrive	addq.l	#4,a0
		addq.l	#1,d7
		cmp.l	#4,d7
		bne	.LoopDrives
.Error		rts
**********************************************************************
*       These are the names for external drives to be checked        *
**********************************************************************
DriveNames	dc.l	.Txt1,.Txt2,.Txt3,.Txt4
.Txt1	dc.b	"DF0:",0
.Txt2	dc.b	"DF1:",0
.Txt3	dc.b	"DF2:",0
.Txt4	dc.b	"DF3:",0
TrackName	dc.b	"trackdisk.device",0
		EVEN



**********************************************************************
*            Remove occupied resources for drive checking            *
**********************************************************************
RemoveDrives	lea	DriveRequests(a4),a3		; Close devices for all IORequests
		moveq	#3,d7
.FreeRequests	tst.l	(a3)
		beq.s	.NoReq
		move.l	(a3),a1
		tst.l	IO_DEVICE(a1)
		beq.s	.NoDevice
		CALLSYS	CloseDevice
.NoDevice	move.l	(a3),a0				; And remove Request itself
		CALLSYS	DeleteIORequest
.NoReq		clr.l	(a3)
		addq.l	#4,a3
		dbf	d7,.FreeRequests

		tst.l	TrackPort(a4)			; Shut down MsgPort
		beq.s	.NoPort
		move.l	TrackPort(a4),a0
		CALLSYS	DeleteMsgPort
.NoPort		clr.l	TrackPort(a4)
		rts




**********************************************************************
*                Try to read in the default PrefsFile                *
**********************************************************************
LoadPrefs	move.l	#NOIFF_ERR,ErrorBack(a4)
		RELLIB	AllocIFF,_IFFParseBase
		move.l	d0,MyIFFHandle(a4)
		tst.l	d0
		beq	.LoadError1

		lea	PrefsFileName(a4),a0
		move.l	a0,d1			; Try to open user-defined file
		move.l	d1,BackErrArg(a4)
		move.l	#MODE_OLDFILE,d2
		RELLIB	Open,_DOSBase
		tst.l	d0
		bne.s	.GotPrefFile
		tst.b	FirstPrefs(a4)
		bne	.DosLoadError
		tst.b	HadHardPrefs(a4)
		bne	.EndLoad

		lea	DirPrefsName,a0
		lea	PrefsFileName(a4),a1
		bsr	StrCpy

		move.l	#DirPrefsName,d1	; Try to open file in current directory
		move.l	d1,BackErrArg(a4)
		move.l	#MODE_OLDFILE,d2
		DOLIB	Open
		tst.l	d0
		bne.s	.GotPrefFile

		lea	EnvPrefsName,a0
		lea	PrefsFileName(a4),a1
		bsr	StrCpy

		move.l	#EnvPrefsName,d1	; Try to open File within env:kcx/
		move.l	d1,BackErrArg(a4)
		move.l	#MODE_OLDFILE,d2
		DOLIB	Open
		tst.l	d0
		beq	.EndLoad

.GotPrefFile	move.l	MyIFFHandle(a4),a0
		move.l	d0,iff_Stream(a0)

		RELLIB	InitIFFasDOS,_IFFParseBase

		move.l	MyIFFHandle(a4),a0
		move.l	#IFFF_READ,d0
		DOLIB	OpenIFF
		tst.l	d0
		bne	.LoadError

		move.l	MyIFFHandle(a4),a0	; Find version ID
		move.l	#'KCXM',d0
		move.l	#'VERS',d1
		DOLIB	PropChunk
		tst.l	d0
		bne	.LoadError

		move.l	MyIFFHandle(a4),a0		; Get to 'KCXM' -> 'PREF'
		move.l	#'KCXM',d0
		move.l	#'PREF',d1
		DOLIB	StopChunk
		tst.l	d0
		bne	.LoadError

		move.l	MyIFFHandle(a4),a0		; Scan to chunk
		move.l	#IFFPARSE_SCAN,d0
		DOLIB	ParseIFF
		tst.l	d0
		bne	.LoadError

.ScannedFile	move.l	MyIFFHandle(a4),a0		; Did we have a VERSion chunk ?
		move.l	#'KCXM',d0
		move.l	#'VERS',d1
		DOLIB	FindProp
		tst.l	d0
		beq.s	.PrefsError
		move.l	d0,a0				; Correct version ?
		move.l	spr_Data(a0),a0
		lea	VersChunk,a1
		move.w	(a0)+,d0
		move.w	(a0),d1
		cmp.w	(a1),d0
		bne.s	.PrefsError
		cmp.w	2(a1),d1
		beq.s	.IsPrefs
.PrefsError	move.l	#PREFS_ERR,ErrorBack(a4)
		lea	VersionStr,a0
		move.l	a0,BackErrArg(a4)
		bsr	DisplayError
		bra	.LoadError

.IsPrefs	move.l	MyIFFHandle(a4),a0		; Read Bool qualifiers
		lea	BOOLQUALSTART(a4),a1
		move.l	#FLAGSET,d0
		DOLIB	ReadChunkBytes
		cmp.l	#FLAGSET,d0
		bne	.LoadError

		move.l	MyIFFHandle(a4),a0		; Read Long qualifiers
		lea	LONGQUALSTART(a4),a1
		move.l	#LONGQUAL,d0
		DOLIB	ReadChunkBytes
		cmp.l	#LONGQUAL,d0
		bne	.LoadError

		lea	HotKeyGadStat,a3		; Read HotKey definitions
		move.l	#HOTKEYNUM-1,d7
.LoopHKeys	move.l	MyIFFHandle(a4),a0
		move.l	(a3)+,a1
		add.l	a4,a1
		move.l	#40,d0
		DOLIB	ReadChunkBytes
		cmp.l	#40,d0
		bne	.LoadError
		dbf	d7,.LoopHKeys

		move.l	MyIFFHandle(a4),a0		; Read User-Shell command
		lea	SystemComm(a4),a1
		move.l	#256,d0
		DOLIB	ReadChunkBytes
		cmp.l	#256,d0
		bne	.LoadError

		move.l	MyIFFHandle(a4),a0		; Read Logfilename
		lea	LogFileName(a4),a1
		move.l	#300,d0
		DOLIB	ReadChunkBytes
		cmp.l	#300,d0
		bne	.LoadError

		move.l	MyIFFHandle(a4),a0		; Read Public Screen list
		lea	PubScreenName(a4),a1
		move.l	#256,d0
		DOLIB	ReadChunkBytes
		cmp.l	#256,d0
		bne	.LoadError

		move.l	MyIFFHandle(a4),a0		; Read FKeys
		lea	FKeys(a4),a1
		move.l	#(MAXFKEYWIDTH+2)*10*4,d0
		DOLIB	ReadChunkBytes
		cmp.l	#(MAXFKEYWIDTH+2)*10*4,d0
		bne	.LoadError

		move.l	MyIFFHandle(a4),a0		; Read sample name
		lea	AlSampleName(a4),a1
		move.l	#256,d0
		DOLIB	ReadChunkBytes
		cmp.l	#256,d0
		bne	.LoadError

		move.l	MyIFFHandle(a4),a0		; Read format drive name
		lea	FormatDriveName(a4),a1
		move.l	#32,d0
		DOLIB	ReadChunkBytes
		cmp.l	#32,d0
		bne	.LoadError

		move.l	MyIFFHandle(a4),a0		; Read window positions
		lea	MainWinLeft(a4),a1
		move.l	#WINPOSLEN,d0
		DOLIB	ReadChunkBytes
		cmp.l	#WINPOSLEN,d0
		bne	.LoadError

		clr.w	NumLockEntries(a4)
		clr.l	GlobalTagAdr(a4)
		lea	WindowBase(a4),a0		; Read in window names
		lea	NumWindows(a4),a1
		move.l	#lw_Name,d6
		move.l	#lw_SIZEOF,d5
		move.l	#82,d4
		bsr.s	.ReadNameChunk
		tst.l	d0
		bne	.LoadError
		lea	TaskWinBase(a4),a0		; Read task names
		lea	NumTaskWins(a4),a1
		move.l	#lw_Name,d6
		move.l	#lw_SIZEOF,d5
		move.l	#82,d4
		bsr.s	.ReadNameChunk
		tst.l	d0
		bne	.LoadError
		lea	TagScrsTasks(a4),a0		; Read tag screen task names
		lea	TagScrNum(a4),a1
		move.l	#tagscr_Name,d6
		move.l	#tagscr_SIZEOF,d5
		move.l	#tagscr_SIZEOF-LN_SIZE,d4
		bsr.s	.ReadNameChunk
		tst.l	d0
		bne	.LoadError
		lea	TagScrsTasks(a4),a2		; Search for Global entry
		TSTLIST	a2
		beq.s	.GotEnd
		move.l	LH_HEAD(a2),a2
.FindGlobal	move.l	a2,d0
		beq.s	.GotEnd
		tst.b	tagscr_Type(a2)
		bne.s	.GotGlobal
		SUCC	a2,a2
		bra.s	.FindGlobal
.GotGlobal	move.l	a2,GlobalTagAdr(a4)
		LOCLEA	GlobalTagName,a0
		lea	tagscr_Name(a2),a1
		bsr	StrCpy
.GotEnd		moveq	#0,d0
		bra	.LoadError

.ReadNameChunk	move.l	a0,a3				; Copy pointer to other register
		move.l	a1,a2
		NEWLIST	a0
		move.l	MyIFFHandle(a4),a0
		moveq	#2,d0
		RELLIB	ReadChunkBytes,_IFFParseBase
		cmp.l	#2,d0
		bne.s	.NameLoadError
		move.w	(a2),d7				; Get number of entries
		subq.w	#1,d7
		blt.s	.EndReadChunk
.LoopWins	move.l	d5,d0				; Read in window names
		move.l	#MEMF_CLEAR,d1
		CALLSYS	AllocVec
		tst.l	d0
		bne.s	.GotEntry
		move.l	#MEMORY_ERR,ErrorBack(a4)
		bsr	DisplayError
		moveq	#0,d0
		bra.s	.NameLoadError
.GotEntry	move.l	a3,a0				; Add entry
		move.l	d0,a1
		move.l	d0,a2
		ADDTAIL
		move.l	MyIFFHandle(a4),a0
		lea	0(a2,d6),a1
		move.l	a1,LN_NAME(a2)
		move.l	d4,d0
		RELLIB	ReadChunkBytes,_IFFParseBase
		tst.l	d0
		bne.s	.GotName
		move.l	a2,a1
		REMOVE
		move.l	a2,a1
		CALLSYS	FreeVec
		bra.s	.EndLoad
.GotName	cmp.l	d4,d0
		bne.s	.NameLoadError
		dbf	d7,.LoopWins
.EndReadChunk	moveq	#0,d0
.NameLoadError	rts

.LoadError	tst.l	d0				; Handle errors ...
		beq.s	.EndLoad
		neg.l	d0
		add.l	#400,d0
		move.l	d0,ErrorBack(a4)
.LoadError1	bsr	DisplayError
		bra.s	.EndLoad
.DosLoadError	RELLIB	IoErr,_DOSBase
		move.l	d0,ErrorBack(a4)
		bsr	DisplayError
.EndLoad	tst.l	MyIFFHandle(a4)			; and close resources
		beq.s	.NoHandle
		move.l	MyIFFHandle(a4),a0
		RELLIB	CloseIFF,_IFFParseBase
		move.l	MyIFFHandle(a4),a0
		tst.l	iff_Stream(a0)
		beq.s	.NoStream
		move.l	iff_Stream(a0),d1
		RELLIB	Close,_DOSBase
.NoStream	move.l	MyIFFHandle(a4),a0
		RELLIB	FreeIFF,_IFFParseBase

.NoHandle	move.l	SavedPage(a4),d0
		move.l	d0,PageNum(a4)

		bsr	InitAlarmSmp
		move.b	#1,FirstPrefs(a4)
		move.b	ShowClock(a4),d0
		move.b	d0,OldClockState(a4)
		rts
**********************************************************************
*                Chunkdefinitions for the Prefs file                 *
**********************************************************************
VersChunk	dc.w	VERNUM,REVNUM
VersChunkSize	EQU	*-VersChunk




**********************************************************************
*  This routine initializes the process to handle all check events   *
**********************************************************************
InitChecker	CALLSYS	Forbid
		lea	CheckerTags(pc),a0
		move.l	a0,d1				; Start Process with tags
		RELLIB	CreateNewProc,_DOSBase
		CALLSYS	Permit
		tst.l	d0
		beq.s	.Error
		move.l	#SIGHANDSHAKE,d0		; Wait for reply
		CALLSYS	Wait
.Error		rts
**********************************************************************
*               Tags for new process of checker events               *
**********************************************************************
CheckerTags	dc.l	NP_Entry			; We only supply an entry point
		dc.l	TimeChecker
		dc.l	NP_Name				; Name of process
		dc.l	CheckerName
		dc.l	NP_Priority			; We run at a higher proirity
		dc.l	10				; so that we`re quite fast
		dc.l	NP_StackSize			; Stack-Size to be used
		dc.l	6000
		dc.l	TAG_DONE




**********************************************************************
*                     Install the ARexx-Process                      *
**********************************************************************
InitRexxProc	CALLSYS	Forbid
		lea	RexxTags(pc),a0
		move.l	a0,d1				; Start Process
		RELLIB	CreateNewProc,_DOSBase
		CALLSYS	Permit
		tst.l	d0
		beq.s	.Error
		move.l	#SIGHANDSHAKE,d0		; Wait for reply
		CALLSYS	Wait
.Error		rts
**********************************************************************
*                Tags for new process of ARexx events                *
**********************************************************************
RexxTags	dc.l	NP_Entry			; We only supply an entry point
		dc.l	RexxChecker
		dc.l	NP_Name				; Name of process
		dc.l	RexxName
		dc.l	NP_Priority			; We run at a higher proirity
		dc.l	0				; so that we`re quite fast
		dc.l	NP_StackSize			; Stack-Size to be used
		dc.l	4096
		dc.l	TAG_DONE





**********************************************************************
*          This is the small process to handle timer events          *
**********************************************************************
TimeChecker	movem.l	RegsAll,-(sp)

		move.l	ZeroBase(pc),a4

		moveq	#-1,d0				; Get signal for Events
		CALLSYS	AllocSignal
		move.l	d0,EventSigBit(a4)
		blt	CheckerError
		moveq	#1,d1
		lsl.l	d0,d1
		move.l	d1,EventSigFlag(a4)

		bsr	InitAudio			; Init Audio-Stuff
		tst.l	d0
		beq	CheckerError

		bsr	InitHandler			; Install the InputHandler
		tst.l	d0
		beq	CheckerError

		sub.l	a1,a1
		DOLIB	FindTask
		move.l	d0,CheckerProc(a4)

		move.l	BackTask(a4),a1			; Signal mother
		move.l	#SIGHANDSHAKE,d0
		DOLIB	Signal

.Loop		move.l	#SIGHANDSHAKE|SIGTIMEREMAKE|SIGBILLWIN,d0		; for signal to go away
		or.l	BillSigFlag(a4),d0		; message from bill window
		or.l	ClockSigFlag(a4),d0		; message from clock window
		or.l	EventSigFlag(a4),d0		; for input events
		CALLSYS	Wait
		move.l	d0,CheckerProcSig(a4)

		move.l	CheckerProcSig(a4),d0		; Got signal to cycle ?
		and.l	EventSigFlag(a4),d0
		beq.s	.NoEventSig

		bsr	EventMsgLoop			; Check for pending "events"

.NoEventSig	move.l	CheckerProcSig(a4),d0		; Are we to go away ?
		and.l	#SIGHANDSHAKE,d0
		bne	CheckerError			; Yep -> do so

		move.l	CheckerProcSig(a4),d0		; Did it come from the
		and.l	BillSigFlag(a4),d0		; Bill window ?
		beq.s	.NoBillSig
		bsr	CheckBillWindow

.NoBillSig	move.l	CheckerProcSig(a4),d0		; Only remake time display ?
		and.l	#SIGTIMEREMAKE,d0
		beq.s	.NoTimeRemake
		bsr	SetTime				; Yep -> do so
		bsr	RemakeCosts			; Also redisplay billstuff

.NoTimeRemake	move.l	CheckerProcSig(a4),d0		; Pop up bill window ?
		and.l	#SIGBILLWIN,d0
		beq.s	.NoBillOpenUp
		tst.l	BillHandle(a4)			; Window already open ?
		bne.s	.CloseBill			; yep -> close it
		bsr	OpenBillWin
		bra.s	.NoBillOpenUp
.CloseBill	bsr	CloseBillWin

.NoBillOpenUp	move.l	CheckerProcSig(a4),d0		; Message from clock window ?
		and.l	ClockSigFlag(a4),d0
		beq.s	.NoClockMsg

.LoopClockMsg	move.l	ClockHandle(a4),a0
		move.l	wd_UserPort(a0),a0
		CALLSYS	GetMsg
		tst.l	d0
		beq.s	.NoClockMsg

		move.l	d0,a1
		move.l	im_Class(a1),d7

		DOLIB	ReplyMsg

		cmp.l	#IDCMP_CLOSEWINDOW,d7		; Clock window closed ?
		bne.s	.NoClockClose
		bsr	CloseClockWin
		clr.b	ClockWin(a4)
		CALLSYS	Forbid
		moveq	#0,d0
		move.l	#GD_DisplayWin,d1
		lea	ClockDefGads(a4),a0
		move.l	ClockDefHandle(a4),a1
		RELLIB	SetCheckBox,_KCXBase
		CALLSYS	Permit
		bra.s	.NoClockMsg

.NoClockClose	cmp.l	#IDCMP_CHANGEWINDOW,d7		; Window moved ?
		bne.s	.LoopClockMsg
		move.l	ClockHandle(a4),a0
		lea	ClockWinLeft(a4),a1
		bsr	StoreWinPos
		bra	.LoopClockMsg
.NoClockMsg	bra	.Loop

CheckerError	bsr	RemoveHandler			; Remove Handler
		bsr	RemoveAudio			; Remove Audio-Stuff
		bsr	CloseBillWin			; Close the billwindow

		tst.l	EventSigBit(a4)			; Clear SigBit
		blt.s	.NoEventSig
		move.l	EventSigBit(a4),d0
		CALLSYS	FreeSignal

.NoEventSig	move.l	#-1,EventSigBit(a4)
		bsr	OnMouse
		CALLSYS	Forbid
		bsr	OnScreen
		CALLSYS	Permit
		move.l	BackTask(a4),a1			; Signal mother that we`re fine
		move.l	#SIGHANDSHAKE,d0
		CALLSYS	Signal
		movem.l	(sp)+,RegsAll
		rts



**********************************************************************
*                  Handler for incoming InputEvents                  *
**********************************************************************
EventMsgLoop	lea	EventPool(a4),a0		; Find an event
		moveq	#MAXEVENTS-1,d7
.Find		tst.l	LN_NAME(a0)
		bne.s	.GoOn
		lea	req_SIZEOF(a0),a0
		dbf	d7,.Find
		rts

.GoOn		move.l	a0,EventMsg(a4)			; Store pointer to event

		move.l	EventMsg(a4),a0			; Activate window ?
		cmp.l	#REQ_ACTIVATE,LN_NAME(a0)
		bne	NoActiveEvent
		bsr	Activate
		move.l	EventMsg(a4),a0
		lea	req_IEvent(a0),a0
		cmp.b	#IECLASS_RAWKEY,ie_Class(a0)
		bne	EventDone
		tst.b	MapUmlaut(a4)			; Map "Umlauts" ?
		beq	NoUmlaut
		move.l	EventMsg(a4),a1			; Check for "Umlauts"
		lea	req_IEvent(a1),a1
		move.w	ie_Qualifier(a1),d1		; Only accept plain and shifted keys
		and.w	#~(IEQUALIFIER_REPEAT|IEQUALIFIER_NUMERICPAD|IEQUALIFIER_INTERRUPT|IEQUALIFIER_RELATIVEMOUSE|IEQUALIFIER_MULTIBROADCAST),d1
		move.w	d1,d2
		and.w	#(IEQUALIFIER_LALT|IEQUALIFIER_RALT|IEQUALIFIER_LCOMMAND|IEQUALIFIER_RCOMMAND),d2
		bne	NoUmlaut
		movem.l	d1/a1,-(sp)
		link	a5,#-20
		move.l	a1,a0
		lea	-20(a5),a1
		clr.b	(a1)
		move.l	#19,d1
		sub.l	a2,a2
		RELLIB	MapRawKey,_KeyMapBase
		moveq	#0,d0
		move.b	-20(a5),d0
		unlk	a5
		movem.l	(sp)+,d1/a1
.UmlautOk	lea	UmlautTab(pc),a3		; Get UmlautTab
		moveq	#7,d7
.GetUmlaut	cmp.w	(a3),d0				; Got "Umlaut" ?
		beq.s	.GotUmlaut			; Yep -> go on
		addq.l	#6,a3
		dbf	d7,.GetUmlaut
		bra.s	NoUmlaut			; No "Umlaut"
.GotUmlaut	cmp.b	#'ß',d0
		bne.s	.NoSS
		move.w	d1,d0
		and.w	#(IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT),d0
		bne.s	NoUmlaut
		bra.s	.NoShift
.NoSS		tst.w	(a3)				; Skip shifted ß
		beq.s	NoUmlaut

.NoShift	link	a5,#-ie_SIZEOF			; Get space for one IEs
		move.b	ie_SubClass(a1),d0		; Get subclass
		move.l	ie_EventAddress(a1),d2		; Address

		lea	-ie_SIZEOF(a5),a0		; Create new event -> Clear link to next
		clr.l	ie_NextEvent(a0)
		move.b	#IECLASS_RAWKEY,ie_Class(a0)
		move.b	d0,ie_SubClass(a0)
		move.w	2(a3),ie_Code(a0)		; Get first char for "Umlaut"
		move.w	d1,ie_Qualifier(a0)
		move.l	d2,ie_EventAddress(a0)
		LNKLIB	AddIEvents,_CxBase		; Add events
		lea	-ie_SIZEOF(a5),a0
		move.w	4(a3),ie_Code(a0)		; Get second char for "Umlaut"
		and.w	#~(IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT),ie_Qualifier(a0)
		DOLIB	AddIEvents
		unlk	a5
		bra.s	HadUmlaut
NoUmlaut	clr.l	ie_NextEvent(a0)
		LNKLIB	AddIEvents,_CxBase
HadUmlaut	tst.b	MouseEver(a4)			; Blank mouse ?
		beq.s	.NoMouseOff
		move.l	EventMsg(a4),a1
		lea	req_IEvent(a1),a1
		move.w	ie_Code(a1),d0
		move.w	ie_Qualifier(a1),d1
		and.w	#IEQUALIFIER_LCOMMAND,d1
		beq.s	.NoMouseOff
		cmp.w	#$4c,d0				; Check for cursor keys
		blt.s	.DoOff
		cmp.w	#$4f,d0
		bgt.s	.DoOff
		bra.s	.NoMouseOff
.DoOff		bsr	OffMouse			; On key-press allways blank mouse
.NoMouseOff	bra	EventDone

NoActiveEvent	move.l	EventMsg(a4),a0
		cmp.l	#REQ_RAWKEY,LN_NAME(a0)
		bne	NoKeyEvent

		tst.b	Disabled(a4)
		bne.s	.End1
		tst.b	MouseEver(a4)			; Blank mouse ?
		beq.s	.End1
		move.l	EventMsg(a4),a1
		lea	req_IEvent(a1),a1
		move.w	ie_Code(a1),d0
		move.w	ie_Qualifier(a1),d1
		and.w	#~(IEQUALIFIER_REPEAT|IEQUALIFIER_NUMERICPAD|IEQUALIFIER_INTERRUPT|IEQUALIFIER_RELATIVEMOUSE|IEQUALIFIER_MULTIBROADCAST),d1
		bsr	FilterDouble
		cmp.w	#IEQUALIFIER_LCOMMAND,d1
		bne.s	.DoOff
		cmp.w	#$4c,d0				; Check for cursor keys
		blt.s	.DoOff
		cmp.w	#$4f,d0
		bgt.s	.DoOff
		bra.s	.End1
.DoOff		bsr	OffMouse			; On key-press allways blank mouse

.End1		tst.b	Disabled(a4)
		bne	.NoClickYet
		tst.b	KeyClick(a4)
		beq	.NoClickYet
		lea	AudioName(pc),a0		; Try to open device and
		moveq	#0,d0				; allocate a channel
		move.l	AudioReq(a4),a1
		move.w	#ADCMD_ALLOCATE,IO_COMMAND(a1)
		move.b	#ADIOF_NOWAIT,IO_FLAGS(a1)
		clr.w	ioa_AllocKey(a1)
		move.l	#AnyChannel,ioa_Data(a1)
		move.l	#4,ioa_Length(a1)
		move.l	AudioPort(a4),MN_REPLYPORT(a1)
		move.b	#-50,LN_PRI(a1)
		moveq	#0,d1				; suggested by UISG
		CALLSYS	OpenDevice
		tst.l	d0
		beq.s	.NoError
		moveq	#0,d0				; Disable
		move.b	d0,KeyClick(a4)			; ... the click function
		bra.s	.NoClickYet
.NoError	move.l	AudioReq(a4),a1			; Let`s hear it for the click
		move.w	#CMD_WRITE,IO_COMMAND(a1)
		move.b	#ADIOF_PERVOL,IO_FLAGS(a1)
		move.w	#450,ioa_Period(a1)
		move.l	ClickVol(a4),d0
		move.w	d0,ioa_Volume(a1)
		move.l	#CLICKLEN,ioa_Length(a1)
		move.l	#ClickData,ioa_Data(a1)
		move.w	#1,ioa_Cycles(a1)
		move.l	AudioPort(a4),MN_REPLYPORT(a1)
		move.b	#-50,LN_PRI(a1)
		move.l	IO_DEVICE(a1),a6
		jsr	DEV_BEGINIO(a6)
.WaitClick	move.l	AudioReq(a4),a1			; Wait until request is finished
		CALLSYS	CheckIO
		tst.l	d0
		beq.s	.WaitClick
		move.l	 AudioReq(a4),a1		; Close device again and
		DOLIB	CloseDevice			; free channels

.NoClickYet	tst.b	ScrTimeOut(a4)			; Screen blanked ?
		beq.s	.NoScr
		bsr	GetSemaphore
		move.l	EventMsg(a4),a0
		lea	req_IEvent(a0),a0
		lea	ie_TimeStamp(a0),a0		; Reset screen delay
		move.l	TV_SECS(a0),d0
		add.l	ScreenTime(a4),d0
		move.l	d0,ScreenOff(a4)
		clr.b	ScrTimeOut(a4)			; Screen timed out
		bsr	FreeSemaphore
		bsr	OnScreen			; UnBlank it
.NoScr		bra	EventDone

NoKeyEvent	move.l	EventMsg(a4),a0
		cmp.l	#REQ_RAWMOUSE,LN_NAME(a0)
		bne.s	NoMouseEvent

		tst.b	ScrTimeOut(a4)
		beq.s	.NoScr
		bsr	OnScreen			; Activate screen if not blanked
.NoScr		clr.b	ScrTimeOut(a4)			; out anymore
		bsr	OnMouse
		bra	EventDone

NoMouseEvent	move.l	EventMsg(a4),a0			; Work window cycling
		cmp.l	#REQ_CYCLE,LN_NAME(a0)
		bne	NoCycleEvent

		move.l	EventMsg(a4),a1
		lea	req_IEvent(a1),a1
		move.w	ie_Code(a1),d0
		move.w	ie_Qualifier(a1),d1
		and.w	#~(IEQUALIFIER_REPEAT|IEQUALIFIER_NUMERICPAD|IEQUALIFIER_INTERRUPT|IEQUALIFIER_RELATIVEMOUSE|IEQUALIFIER_MULTIBROADCAST),d1
		bsr	FilterDouble
		cmp.w	FrontCode(a4),d0
		bne.s	.NotFront
		cmp.w	FrontQual(a4),d1
		beq	.CheckCycle
.NotFront	cmp.w	BackCode(a4),d0
		bne.s	.NotBack
		cmp.w	BackQual(a4),d1
		beq	.NoPopFront
.NotBack	cmp.w	#IECODE_MBUTTON,d0
		bne	.EndCycle
		cmp.w	MiddleQual(a4),d1
		bne	.EndCycle

		cmp.w	#1,MiddleClicks(a4)
		beq.s	.DoBackM
		tst.l	MCycleSecs(a4)
		bge.s	.CheckDoubleM
.RemakeDoubleM	move.l	EventMsg(a4),a1
		lea	req_IEvent(a1),a1
		lea	ie_TimeStamp(a1),a1
		move.l	TV_SECS(a1),MCycleSecs(a4)	; Copy current system ticks
		move.l	TV_MICRO(a1),MCycleMicro(a4)
		bra	.RemakeCounters
.CheckDoubleM	subq.w	#1,ThisMiddleClick(a4)
		bgt	.EndCycle
		move.l	EventMsg(a4),a1
		lea	req_IEvent(a1),a1
		lea	ie_TimeStamp(a1),a1
		movem.l	MCycleSecs(a4),d0-d1
		move.l	TV_SECS(a1),d2
		move.l	TV_MICRO(a1),d3
		move.l	#-1,MCycleSecs(a4)
		RELLIB	DoubleClick,_IntuitionBase
		tst.l	d0
		beq.s	.RemakeDoubleM
.DoBackM	move.l	_IntuitionBase(a4),a0
		move.l	ib_FirstScreen(a0),a0
		bra	.PopScrBack

.CheckCycle	moveq	#-1,d0
		move.l	d0,BCycleSecs(a4)
		move.l	d0,MCycleSecs(a4)
		cmp.w	#1,FrontClicks(a4)
		beq	.OneFront
		tst.l	FCycleSecs(a4)			; Double click to front ?
		bge.s	.CheckDoubleF
.RemakeDouble	move.l	EventMsg(a4),a1
		lea	req_IEvent+ie_TimeStamp(a1),a1
		move.l	TV_SECS(a1),FCycleSecs(a4)	; Copy current system ticks
		move.l	TV_MICRO(a1),FCycleMicro(a4)
		clr.l	CycledWin(a4)			; Clear window pointer
		clr.l	CycledScreen(a4)		; Clear screen pointer
		RELLIB	FindWindow,_KCXBase		; Find a layer
		tst.l	d0				; Got one ?
		beq	.RemakeCounters			; Nope -> No window
		move.l	a0,CycledScreen(a4)
		move.l	d0,a0
		move.l	lr_Window(a0),CycledWin(a4)	; Store pointer to window
		bra	.RemakeCounters
.CheckDoubleF	RELLIB	FindWindow,_KCXBase		; Check if window is still the same
		cmp.l	CycledScreen(a4),a0		; Same screen ?
		bne.s	.RemakeDouble			; Nope -> remake
		tst.l	d0				; Got a window ?
		beq.s	.NoWindow			; Nope -> go on
		move.l	d0,a1				; Window the same a last one ?
		move.l	lr_Window(a1),d1		; Layer with a window ?
		beq.s	.NoWindow			; Nope -> Could be a screen
		cmp.l	CycledWin(a4),d1		; Window the same ? Nope -> go on
		bne.s	.RemakeDouble
.NoWindow	subq.w	#1,ThisFrontClick(a4)
		bgt	.EndCycle
		movem.l	d0/a0,-(sp)
		move.l	EventMsg(a4),a1
		movem.l	FCycleSecs(a4),d0-d1		; Check for double click
		lea	req_IEvent(a1),a1
		lea	ie_TimeStamp(a1),a1
		move.l	TV_SECS(a1),d2
		move.l	TV_MICRO(a1),d3
		move.l	#-1,FCycleSecs(a4)
		RELLIB	DoubleClick,_IntuitionBase
		movem.l	(sp)+,d1/a0
		tst.l	d0				; Click OK ?
		beq	.RemakeDouble			; Nope -> Remake ticks
		tst.l	d1				; Did we have a layer ?
		beq.s	.PopScrFront			; Nope -> check for screen
		move.l	d1,d0				; Check window
		bra.s	.SpecToFront
.OneFront	RELLIB	FindWindow,_KCXBase		; Find window underneith layer
		tst.l	d0
		beq.s	.PopScrFront			; No layer, no window
.SpecToFront	move.l	d0,a5
		move.l	lr_Window(a5),d1		; Layer with window ?
		beq.s	.PopScrFront			; Nope -> screen
		move.l	lr_ClipRect(a5),a1		; Window hidden by others ?
		move.l	a1,d0
		beq	.EndCycle
		tst.l	cr_Next(a1)
		beq	.EndCycle
		move.l	lr_Window(a5),a5
		move.l	wd_Flags(a5),d1
		move.l	d1,d2
		and.l	#WFLG_BACKDROP,d1		; BackDrop window -> Pop Screen
		bne.s	.PopScrFront
		and.l	#WFLG_DEPTHGADGET,d2		; Does the window have a DEPTH-Gad ???
		beq	.EndCycle			; Nope -> no pop to front
		move.l	a5,a0
		RELLIB	WindowToFront,_IntuitionBase
		bra.s	.EndFront
.PopScrFront	move.l	a0,d0				; Got Screen address ???
		beq	.EndCycle
		move.l	a0,-(sp)
		moveq	#0,d0
		RELLIB	LockIBase,_IntuitionBase
		move.l	ib_FirstScreen(a6),a2
		move.l	d0,a0
		DOLIB	UnlockIBase
		move.l	(sp)+,a0
		cmp.l	a2,a0
		beq.s	.EndFront
		DOLIB	ScreenToFront			; Pop screen to front
.EndFront	bra	.EndCycle
.NoPopFront	moveq	#-1,d0
		move.l	d0,FCycleSecs(a4)
		move.l	d0,MCycleSecs(a4)


		cmp.w	#1,BackClicks(a4)
		beq	.OneBack
		tst.l	BCycleSecs(a4)			; Double click to front ?
		bge.s	.CheckDoubleB
.RemakeDoubleB	move.l	EventMsg(a4),a1
		lea	req_IEvent(a1),a1
		lea	ie_TimeStamp(a1),a1
		move.l	TV_SECS(a1),BCycleSecs(a4)	; Copy current system ticks
		move.l	TV_MICRO(a1),BCycleMicro(a4)
		clr.l	CycledWin(a4)			; Clear window pointer
		clr.l	CycledScreen(a4)		; Clear screen pointer
		RELLIB	FindWindow,_KCXBase		; Find a layer
		tst.l	d0				; Got one ?
		beq	.RemakeCounters			; Nope -> No window
		move.l	a0,CycledScreen(a4)
		move.l	d0,a0
		move.l	lr_Window(a0),CycledWin(a4)	; Store pointer to window
		bra	.RemakeCounters
.CheckDoubleB	RELLIB	FindWindow,_KCXBase		; Check if window is still the same
		cmp.l	CycledScreen(a4),a0		; Same screen ?
		bne.s	.RemakeDoubleB			; Nope -> remake
		tst.l	d0				; Got a window ?
		beq.s	.NoWindowB			; Nope -> go on
		move.l	d0,a1				; Window the same a last one ?
		move.l	lr_Window(a1),d1		; Layer with a window ?
		beq.s	.NoWindowB			; Nope -> Could be a screen
		cmp.l	CycledWin(a4),d1		; Window the same ? Nope -> go on
		bne.s	.RemakeDoubleB
.NoWindowB	subq.w	#1,ThisBackClick(a4)
		bgt	.EndCycle
		movem.l	d0/a0,-(sp)
		move.l	EventMsg(a4),a1
		movem.l	BCycleSecs(a4),d0-d1		; Check for double click
		lea	req_IEvent(a1),a1
		lea	ie_TimeStamp(a1),a1
		move.l	TV_SECS(a1),d2
		move.l	TV_MICRO(a1),d3
		move.l	#-1,BCycleSecs(a4)
		RELLIB	DoubleClick,_IntuitionBase
		movem.l	(sp)+,d1/a0
		tst.l	d0				; Click OK ?
		beq	.RemakeDoubleB			; Nope -> Remake ticks
		tst.l	d1				; Did we have a layer ?
		beq.s	.PopScrBack			; Nope -> check for screen
		move.l	d1,d0				; Check window
		bra.s	.SpecToBack
.OneBack	RELLIB	FindWindow,_KCXBase		; Find layer
		tst.l	d0
		beq.s	.PopScrBack			; No layer, no window
.SpecToBack	move.l	d0,a1
		move.l	lr_Window(a1),a1
		move.l	a1,d0				; Got a window ?
		beq.s	.PopScrBack
		move.l	wd_Flags(a1),d0
		move.l	d0,d1
		and.l	#WFLG_BACKDROP,d0		; BackDrop window ?
		bne.s	.PopScrBack
		and.l	#WFLG_DEPTHGADGET,d1		; DEPTH-Gad there ?
		beq	EventDone			; Nope -> go on
		tst.l	wd_NextWindow(a1)
		bne.s	.ToBack
		move.l	wd_WScreen(a1),a0
		cmp.l	sc_FirstWindow(a0),a1
		beq	EventDone
.ToBack		move.l	a1,-(sp)
		RELLIB	WaitTOF,_GfxBase		; Wait a bit so that
							; the ButtonUp message can arrive
		move.l	(sp)+,a0
		RELLIB	WindowToBack,_IntuitionBase
		bra	EventDone
.PopScrBack	move.l	a0,d0				; If we have a screen ->
		beq	EventDone			; Let it pop
		RELLIB	ScreenToBack,_IntuitionBase
.EndCycle	bra	EventDone
.RemakeCounters	move.w	FrontClicks(a4),d0		; Reset click counter
		subq.w	#1,d0
		move.w	d0,ThisFrontClick(a4)
		move.w	MiddleClicks(a4),d0		; Reset click counter
		subq.w	#1,d0
		move.w	d0,ThisMiddleClick(a4)
		move.w	BackClicks(a4),d0		; Reset click counter
		subq.w	#1,d0
		move.w	d0,ThisBackClick(a4)
		bra.s	.EndCycle

NoCycleEvent	move.l	EventMsg(a4),a0			; Disk Event ?
		cmp.l	#REQ_DISKCHANGE,LN_NAME(a0)
		bne	NoDiskChEvent

		bsr	OnScreen

		link	a5,#-4

		move.l	#id_SIZEOF,d0			; Allocate InfoData Buffer
		move.l	#MEMF_CLEAR,d1
		CALLSYS	AllocVec
		move.l	d0,-4(a5)
		tst.l	d0
		beq	.Error

		lea	DriveList(a4),a1		; Check, which drive was changed
		lea	DriveChange(a4),a2
		lea	DriveRequests(a4),a3
		moveq	#0,d7
.FindDrive	tst.l	(a3)
		beq	.NotThisOne
		movem.l	a1/a2,-(sp)
		move.l	(a3),a1
		move.w	#TD_CHANGENUM,IO_COMMAND(a1)	; Get number of changes for one drive
		clr.b	IO_FLAGS(a1)
		CALLSYS	DoIO
		movem.l	(sp)+,a1/a2
		move.l	(a3),a0
		move.l	IO_ACTUAL(a0),d0
		move.l	(a2),d1
		move.l	d0,(a2)
		cmp.l	d1,d0				; Check with previous number of changes
		ble	.NotThisOne
		move.l	a1,-(sp)			; If that`s our drive, get address of handler
		move.l	(a1),d1
		RELLIB	DeviceProc,_DOSBase
		move.l	(sp)+,a1
		tst.l	d0
		beq.s	.NotThisOne
		movem.l	d7/a1,-(sp)			; Ask DOS to tell us the state of the disk
		move.l	d0,d1
		move.l	#ACTION_DISK_INFO,d2
		move.l	-4(a5),d3
		lsr.l	#2,d3
		moveq	#0,d4
		moveq	#0,d5
		moveq	#0,d6
		moveq	#0,d7
		DOLIB	DoPkt
		movem.l	(sp)+,d7/a1
		tst.l	d0
		beq.s	.NotThisOne
		move.l	-4(a5),a0			; Check for Disk-Status
		cmp.l	#ID_UNREADABLE_DISK,id_DiskType(a0)
		beq.s	.ThisOne
		cmp.l	#ID_NOT_REALLY_DOS,id_DiskType(a0)
		bne.s	.NotThisOne
.ThisOne	move.w	d7,FormatDrive(a4)		; This is the number of drive to be formatted
		lea	DriveThere(a4),a0		; Which drive is within this slot ???
		moveq	#0,d0
		move.b	0(a0,d7.w),d0			; Get Drive Number
		lea	Watch0(a4),a0			; Check whether drive is to be checked
		tst.b	0(a0,d0.w)			; Flag clear -> no check
		beq.s	.NotThisOne
		move.l	-4(a5),a1
		CALLSYS	FreeVec
		tst.l	FormHandle(a4)			; Formatter window open ???
		bne.s	.EndDisk
		unlk	a5				; Nope, tell mother to ask user
		move.l	BackTask(a4),a1
		move.l	FormatSigFlag(a4),d0
		DOLIB	Signal
		bra	EventDone
.NotThisOne	addq.l	#4,a1				; Loop for drives
		addq.l	#4,a2
		addq.l	#4,a3
		addq.w	#1,d7
		cmp.w	NumDrives(a4),d7
		bne	.FindDrive

.Error		tst.l	-4(a5)				; Free buffer
		beq.s	.EndDisk
		move.l	-4(a5),a1
		CALLSYS	FreeVec
.EndDisk	unlk	a5
		bra	EventDone

NoDiskChEvent	move.l	EventMsg(a4),a0			; Scr to back ?
		cmp.l	#REQ_SCRBACK,LN_NAME(a0)
		bne.s	NoScrBackEvent
		CALLSYS	Forbid				; Send active screen to back
		moveq	#0,d0
		RELLIB	LockIBase,_IntuitionBase
		move.l	ib_FirstScreen(a6),a2
		move.l	d0,a0
		DOLIB	UnlockIBase
		CALLSYS	Permit
		move.l	a2,d0
		beq	EventDone
		move.l	a2,a0
		RELLIB	ScreenToBack,_IntuitionBase
		bra	EventDone

NoScrBackEvent	move.l	EventMsg(a4),a0			; WB to front ?
		cmp.l	#REQ_WBFRONT,LN_NAME(a0)
		bne.s	NoWBFrontEvent			; Pick up WB-Screen
		RELLIB	WBenchToFront,_IntuitionBase
		bra	EventDone

NoWBFrontEvent	move.l	EventMsg(a4),a0			; Unblank screen ???
		cmp.l	#REQ_ONSCREEN,LN_NAME(a0)
		bne.s	NoOnScreen
		bsr	OnScreen
		bra	EventDone

NoOnScreen	move.l	EventMsg(a4),a0			; Timer Event ?
		cmp.l	#REQ_TIMER,LN_NAME(a0)
		bne	EventDone

		CALLSYS	Forbid
		tst.b	ShowClock(a4)
		beq.s	.NoRemakeFont
		link	a5,#-4				; Check for system font
		moveq	#0,d7				; within titlebar
		bsr	LockScreen
		tst.l	d0
		beq.s	.NoScreen
		move.l	d0,a0
		move.l	sc_BarLayer(a0),a1
		move.l	lr_rp(a1),a1
		move.l	rp_Font(a1),d1
		cmp.l	OldSysFont(a4),d1		; Still the same font used ?
		bne.s	.NewFont			; Yep -> go on
		move.l	a0,a1
		move.l	sc_RastPort+rp_BitMap(a1),a1
		moveq	#0,d0
		move.b	bm_Depth(a1),d0
		cmp.w	BitMapDepth(a4),d0
		beq.s	.NoFont
.NewFont	moveq	#1,d7
.NoFont		bsr	UnlockScreen
.NoScreen	unlk	a5
		tst.b	d7
		beq.s	.NoRemakeFont
.Lock		bsr	ClearTitleBar
		bsr	RemoveGfx
		bsr	InitGfx

.NoRemakeFont	CALLSYS	Permit
		bsr	CheckOnline			; Set new online time
		bsr	SetTime				; Set new time

		tst.b	TimeReset(a4)			; Reset timers ?
		beq.s	.NoTime				; (if times were altered)
		move.l	EventMsg(a4),a0
		lea	req_IEvent(a0),a0
		bsr	ResetTimer
		bra	.NoMouseOff
.NoTime		tst.b	Disabled(a4)			; Only blank when not disabled
		bne	.NoMouseOff
		move.l	EventMsg(a4),a0
		lea	req_IEvent(a0),a0
		lea	ie_TimeStamp(a0),a0
		tst.b	DoBlankNow(a4)
		bne.s	.BlankNow
		tst.b	ScrTimeOut(a4)
		bne.s	.NoScreenOff
		tst.l	ScreenTime(a4)			; Screen to be blanked ?
		beq.s	.NoScreenOff			; nope -> go on
		move.l	ScreenOff(a4),d0		; Screen timeout ?
		sub.l	TV_SECS(a0),d0
		bge.s	.NoScreenOff			; Nope -> go on
.BlankNow	move.l	TV_SECS(a0),d1			; Set time for next timeout
		add.l	ScreenTime(a4),d1
		add.l	#10,d1
		move.l	d1,ScreenOff(a4)
		tst.b	DoBlankNow(a4)
		bne.s	.ComeOnBlank
		cmp.l	#-10,d0
		blt.s	.NoScreenOff
.ComeOnBlank	clr.b	DoBlankNow(a4)
		ST	ScrTimeOut(a4)			; Screen and mouse are timed out
		CALLSYS	Forbid
		bsr	OffScreen
		CALLSYS	Permit
.NoScreenOff	tst.l	MouseTime(a4)			; Mouse to be blanked ?
		beq.s	.NoMouseOff			; Nope -> go on
		move.l	EventMsg(a4),a0			; Check, whether time for
		lea	req_IEvent(a0),a0
		lea	ie_TimeStamp(a0),a0		; blank has come
		move.l	MouseOff(a4),d0
		sub.l	TV_SECS(a0),d0
		bge.s	.NoMouseOff			; Nope -> go on
		move.l	TV_SECS(a0),d1			; Store new values for next
		add.l	MouseTime(a4),d1		; blank.
		addq.l	#1,d1
		move.l	d1,MouseOff(a4)
		cmp.l	#-10,d0
		blt.s	.NoMouseOff
		bsr	OffMouse			; Blank mouse

.NoMouseOff
EventDone	move.l	EventMsg(a4),a1
		clr.l	LN_NAME(a1)
		bra	EventMsgLoop			; Loop for remaining messages
**********************************************************************
*                   Table for converting "Umlauts"                   *
**********************************************************************
UmlautTab	dc.w	'ä',$20,$12		; Ae
		dc.w	'Ä',$20,$12
		dc.w	'ö',$18,$12		; Oe
		dc.w	'Ö',$18,$12
		dc.w	'ü',$16,$12		; Ue
		dc.w	'Ü',$16,$12
		dc.w	'ß',$21,$21		; ss
		dc.w	0,0,0





**********************************************************************
*                   Check the BillWindow-Messages                    *
**********************************************************************
CheckBillWindow	move.l	BillHandle(a4),a0		; Get message
		move.l	wd_UserPort(a0),a0
		RELLIB	GT_GetIMsg,_GadToolsBase
		tst.l	d0
		beq	NoBillMsg
		move.l	d0,a1

		move.l	im_Class(a1),d6
		move.w	im_Qualifier(a1),d4
		moveq	#0,d5
		move.w	im_Code(a1),d5
		move.l	d6,d0
		and.l	#(IDCMP_GADGETUP|IDCMP_GADGETDOWN),d0
		beq.s	.NoGad
		move.l	im_IAddress(a1),a0
		move.w	gg_GadgetID(a0),d7

.NoGad		DOLIB	GT_ReplyIMsg

		cmp.l	#IDCMP_VANILLAKEY,d6		; Key hit ?
		bne.s	.NoVKey
		cmp.b	#ESCKEY,d5			; ESC
		bne.s	.NoEsc
		move.l	#IDCMP_CLOSEWINDOW,d6		; Promote to CLOSEWINDOW
		bra.s	.NoVKey
.NoEsc		lea	BillGads(a4),a3			; Check, which gadget`s ke was hit
		moveq	#0,d1
.FindGad	move.l	(a3),d2
		beq.s	.NoVKey
		move.l	d5,d0
		bsr	GetUpperCase
		move.l	d2,a0
		move.l	gg_UserData(a0),d2
		cmp.b	d0,d2
		bne.s	.NextGad
		cmp.l	#GD_LogCall,d1
		blt.s	.NoVKey
		move.l	#IDCMP_GADGETUP,d6		; Promote to gadgetup message
		moveq	#-1,d5
		move.l	d1,d7
		bra.s	.NoVKey
.NextGad	addq.l	#4,a3
		addq.l	#1,d1
		bra.s	.FindGad

.NoVKey		cmp.l	#IDCMP_CLOSEWINDOW,d6		; Window close ???
		bne.s	NoBillClose
BillClose	bsr	CloseBillWin
		bra	NoBillMsg

NoBillClose	cmp.l	#IDCMP_CHANGEWINDOW,d6		; Window moved/zoomed ?
		bne.s	NoBillChange
		move.l	BillHandle(a4),a0
		move.l	wd_Flags(a0),d0
		and.l	#WFLG_ZOOMED,d0
		bne.s	.IsZoomed
		clr.b	Zipped(a4)
		move.l	BillHandle(a4),a0
		lea	BillWinLeft(a4),a1		; Store pos for normal size
		bsr	StoreWinPos
		move.l	BillHandle(a4),a0
		lea	BillTitle(a4),a1
		suba.l	a2,a2
		subq.w	#1,a2
		RELLIB	SetWindowTitles,_IntuitionBase
		bsr	RemakeCosts
		bra	CheckBillWindow
.IsZoomed	move.b	#1,Zipped(a4)			; Store pos for zoomed size
		move.w	wd_LeftEdge(a0),BillZoomSize(a4)
		move.w	wd_TopEdge(a0),BillZoomSize+2(a4)
		bsr	RemakeCosts
		bra	CheckBillWindow

NoBillChange	cmp.l	#IDCMP_GADGETDOWN,d6
		bne.s	NoBillGadDown

		cmp.w	#GD_BillTimeMX,d7		; Changed timezone ?
		bne.s	NoTimeZoneGad
		move.b	#1,DoRemakeCosts(a4)
		ext.l	d5
		move.l	d5,TimeZone(a4)
		bsr	RemakeCosts
		bra	CheckBillWindow

NoTimeZoneGad	cmp.w	#GD_BillCheap,d7		; Changed timemode
		bne	CheckBillWindow
		move.b	#1,DoRemakeCosts(a4)
		move.b	#1,DontCheckTime(a4)
		move.b	d5,CheapMode(a4)
		bsr	RemakeCosts
		bra	CheckBillWindow

NoBillGadDown	cmp.l	#IDCMP_GADGETUP,d6		; GadgetUp ?
		bne.s	NoBillGadUp

		cmp.w	#GD_LogCall,d7			; Enable/Disable phone logs ?
		bne.s	NoLogCallGad
		bchg	#0,LogCalls(a4)
		move.b	LogCalls(a4),d0
		move.l	#GD_LogCall,d1
		lea	BillGads(a4),a0
		move.l	BillHandle(a4),a1
		RELLIB	SetCheckBox,_KCXBase
		bsr	RemakeCosts
		bra	CheckBillWindow

NoLogCallGad	cmp.w	#GD_BillStart,d7		; Start bill counting ?
		bne.s	NoBillStartGad
		tst.l	d5
		bge.s	.NoKey
		move.l	d7,d0
		lea	BillGads(a4),a0
		move.l	BillHandle(a4),a5
		bsr	ButtonCheck
.NoKey		ST	BillRunning(a4)
		bra	CheckBillWindow

NoBillStartGad	cmp.w	#GD_BillStop,d7			; Stop bill counting ?
		bne	CheckBillWindow
		tst.l	d5
		bge.s	.NoKey
		move.l	d7,d0
		lea	BillGads(a4),a0
		move.l	BillHandle(a4),a5
		bsr	ButtonCheck
.NoKey		SF	BillRunning(a4)
		bra	CheckBillWindow

NoBillGadUp	cmp.l	#IDCMP_VANILLAKEY,d6		; Key pressed ?
		bne	CheckBillWindow

		cmp.w	#KD_BillTime1,d5		; Change time zone ?
		blt.s	NoTimeKeys
		cmp.w	#KD_BillTime3,d5
		bgt.s	NoTimeKeys
		sub.w	#'1',d5				; Calc new zone
		move.b	#1,DoRemakeCosts(a4)
		ext.l	d5
		move.l	d5,TimeZone(a4)
		move.l	#GD_BillTimeMX,d0
		move.l	d5,d1
		lea	BillGads(a4),a0
		move.l	BillHandle(a4),a1
		RELLIB	SetMXGad,_KCXBase
		bsr	RemakeCosts
		bra	CheckBillWindow

NoTimeKeys	cmp.w	#KD_BillNormal,d5		; Activate normal time mode ?
		bne.s	NoNormalKey
		clr.b	CheapMode(a4)
SetNewTariff	move.b	#1,DoRemakeCosts(a4)
		move.b	#1,DontCheckTime(a4)
		move.l	#GD_BillCheap,d0
		moveq	#0,d1
		move.b	CheapMode(a4),d1
		lea	BillGads(a4),a0
		move.l	BillHandle(a4),a1
		RELLIB	SetMXGad,_KCXBase
		bsr	RemakeCosts
		bra	CheckBillWindow

NoNormalKey	cmp.w	#KD_BillCheap,d5			; Activate cheap time mode ?
		bne	CheckBillWindow
		move.b	#1,CheapMode(a4)
		bra	SetNewTariff
NoBillMsg	rts




**********************************************************************
*          This is the process to check for ARexx-Commands           *
**********************************************************************
RexxChecker	movem.l	RegsAll,-(sp)

		move.l	ZeroBase(pc),a4

		bsr	InitARexx			; Init ARexx-Port
		tst.l	d0
		beq.s	NoRexx

		move.l	BackTask(a4),a1			; Signal mother A.Ok
		move.l	#SIGHANDSHAKE,d0
		CALLSYS	Signal

		sub.l	a1,a1				; Store address to this proc
		CALLSYS	FindTask
		move.l	d0,RexxProc(a4)

CheckRexx	move.l	#SIGHANDSHAKE,d0		; Wait for signals
		or.l	ARexxSigFlag(a4),d0
		CALLSYS	Wait
		move.l	d0,RexxSig(a4)

		move.l	RexxSig(a4),d0			; Remove Process ?
		and.l	#SIGHANDSHAKE,d0
		bne.s	NoRexx

		move.l	RexxSig(a4),d0			; Did it come from ARexx ?
		and.l	ARexxSigFlag(a4),d0
		beq.s	CheckRexx
		bsr	HandleARexx			; Handle ARexx commands
		bra	CheckRexx

NoRexx		bsr	RemoveARexx			; Remove ARexx-Port

		move.l	BackTask(a4),a1			; Signal mother A.Ok
		move.l	#SIGHANDSHAKE,d0
		CALLSYS	Signal

		movem.l	(sp)+,RegsAll
		rts




**********************************************************************
*                 Check the main part of this proggy                 *
**********************************************************************
CheckBroker	tst.w	QuitBroker(a4)			; Quit our broker ?
		bne	AbortBroker

		move.l	#SIGBREAKF_CTRL_C,d0		; Accept a BREAK (like from c:break)
		or.l	BrokerSigFlag(a4),d0		; Wait for sig on port of broker
		or.l	AlarmSigFlag(a4),d0		; ... Display alarm
		or.l	FormatSigFlag(a4),d0		; ... Formatter
		or.l	AppSigFlag(a4),d0		; ... of App-Icon
		or.l	DragSigFlag(a4),d0		; ... do dragging
		or.l	FormWinSig(a4),d0		; ... Format window
		or.l	SelScrWinSig(a4),d0		; ... Select Screen window
		or.l	BackPortSig(a4),d0		; ... Communications Port
		CALLSYS	Wait
		move.l	d0,ThisSig(a4)

		and.l	#SIGBREAKF_CTRL_C,d0
		bne	AbortBroker

		move.l	ThisSig(a4),d0			; Got signal from child to
		and.l	AlarmSigFlag(a4),d0		; alarm
		beq.s	NoAlarmDisp

		link	a5,#-4
		bsr	LockScreen
		tst.l	d0
		beq.s	.Error

		tst.b	AlarmSample(a4)
		beq.s	.PlayBeep
		tst.b	SampleLoaded(a4)
		bne.s	.PlaySample
.PlayBeep	move.l	-4(a5),a0			; Make it beep
		DOLIB	DisplayBeep
		bra.s	.BeepDone

.PlaySample	bsr	PlayAlarm

.BeepDone	tst.b	DoShowAlarm(a4)			; Show the requester ???
		beq.s	.Error

		CALLSYS	Forbid
		bsr	OnScreen			; Turn on everything, so that
		CALLSYS	Permit
		bsr	OnMouse				; User can see what`s up

		lea	MyEasyRequest,a1		; Display Easy-Requester
		move.l	#EasyTitle,es_Title(a1)
		LOCLEA	AlarmReqText,es_TextFormat(a1)
		move.l	#EasyGadget,es_GadgetFormat(a1)
		sub.l	a0,a0
		sub.l	a2,a2
		sub.l	a3,a3
		RELLIB	EasyRequestArgs,_IntuitionBase

.Error		bsr	UnlockScreen
		unlk	a5

NoAlarmDisp	move.l	ThisSig(a4),d0
		and.l	BrokerSigFlag(a4),d0		; If it was on the Msg-Port
		beq	NoCxMsgs

WorkAllMsgs	move.l	BrokerPort(a4),a0		; Check the msgs
		CALLSYS	GetMsg
		tst.l	d0
		beq	NoCxMsgs
		move.l	d0,a3

		move.l	a3,a0				; Get Msg-Type
		LNKLIB	CxMsgType,_CxBase
		move.l	d0,MsgType(a4)

		move.l	a3,a0				; Get ID of msg
		DOLIB	CxMsgID
		move.l	d0,MsgID(a4)

		move.l	a3,a1
		CALLSYS	ReplyMsg

		cmp.l	#CXM_COMMAND,MsgType(a4)	; Commodities command ?
		bne	NoXCommand			; Yep -> go on

		move.l	MsgID(a4),d0

		cmp.l	#CXCMD_KILL,d0			; Are we to go away ?
		bne.s	NoKill
		move.w	#1,QuitBroker(a4)
		bra	WorkAllMsgs

NoKill		cmp.l	#CXCMD_APPEAR,d0		; Mainwindow openuo ?
		bne.s	NoMainOpenUp
		bsr	OpenMainWin
		bra	WorkAllMsgs

NoMainOpenUp	cmp.l	#CXCMD_DISAPPEAR,d0		; Mainwindow closedown ?
		bne.s	NoMainCloseDown
		move.l	PrefsPort(a4),a0
		move.l	BackPort(a4),a1
		move.l	#COM_REMOVEPREFS,d0
		RELLIB	ComPut,_KCXBase
		bra	WorkAllMsgs

NoMainCloseDown	cmp.l	#CXCMD_DISABLE,d0		; Disable broker ?
		bne.s	DontDisable
		bsr	DisableBroker
		move.b	#1,Disabled(a4)
		bra	WorkAllMsgs

DontDisable	cmp.l	#CXCMD_ENABLE,d0		; Enable ???
		bne.s	DontEnable
		move.b	#1,TimeReset(a4)
		clr.b	Disabled(a4)
		bsr	EnableBroker
		bra	WorkAllMsgs

DontEnable	cmp.l	#CXCMD_UNIQUE,d0		; Another broker with same name
		bne.s	NoUnique			; -> Quit program
		move.w	#1,QuitBroker(a4)
NoUnique	bra	WorkAllMsgs

NoXCommand	cmp.l	#CXM_IEVENT,MsgType(a4)		; Pop up key used ???
		bne	WorkAllMsgs			; Nope -> Loop for msgs

		cmp.l	#POPUPKEYID,MsgID(a4)		; Open external Prefs proggy ??
		bne.s	NoOpenPrefs
		bsr	OpenMainWin
		bra	WorkAllMsgs

NoOpenPrefs	cmp.l	#PAGEKEYID,MsgID(a4)		; Change display mode of time ?
		bne.s	NoPageChange
		cmp.l	#SNAPMEMPAGE,PageNum(a4)
		bne.s	.GoOn
		move.l	OldPageNum(a4),d0			; Restore old Page
		move.l	d0,PageNum(a4)
		bra.s	.End1
.GoOn		addq.l	#1,PageNum(a4)
		cmp.l	#MAXPAGES,PageNum(a4)
		blt.s	.End
		clr.l	PageNum(a4)
.End		move.l	PageNum(a4),d0
		move.l	d0,OldPageNum(a4)
.End1		CALLSYS	Forbid
		bsr	ClearTitleBar
		bsr	SigTime
		CALLSYS	Permit
		bra	WorkAllMsgs

NoPageChange	cmp.l	#BILLKEYID,MsgID(a4)		; Pop Up Bill window ?
		bne.s	NoBillHotKey
		move.l	CheckerProc(a4),a1
		move.l	#SIGBILLWIN,d0
		CALLSYS	Signal
		bra	WorkAllMsgs

NoBillHotKey	cmp.l	#SHELLKEYID,MsgID(a4)		; Pop Up Shell ?
		bne	NoShellPop
		lea	SystemInOut(pc),a0
		move.l	a0,d1				; Open the dummy Files NIL:
		move.l	#MODE_NEWFILE,d2
		RELLIB	Open,_DOSBase
		move.l	d0,SysInput
		move.l	#SystemInOut,d1
		move.l	#MODE_NEWFILE,d2
		DOLIB	Open
		move.l	d0,SysOutput
		move.l	BackTask(a4),a0			; Make Path-List, only if NOT
		tst.l	pr_CLI(a0)			; Startet from CLI
		beq.s	.NoPath
		move.l	#TAG_DONE,SysCLI		; Clear remaining tags
		bra	.GotPath
.NoPath		move.l	#NP_Path,SysCLI			; Allow Process tags
		move.l	pr_CurrentDir(a0),d1		; Clone mother`s current dir
		RELLIB	DupLock,_DOSBase
		tst.l	d0
		beq	.NoProc
		move.l	$4.w,a6
		move.l	d0,SysDir
		lea	WBPName(pc),a1			; Try to get a pointer to a process
		bsr	.FindTaskSub			; we can clone the pathlist from
							; Try to get the Workbench,
							; AmigaShell, InitialCLI,
		lea	ICLIName(pc),a1			; Background CLI or New CLI processes
		bsr	.FindTaskSub			; This is needed, so that the new
							; shell may execute Prgs, which
							; are located in dirs you
		lea	NCLIName(pc),a1			; worked a PATH on.
		bsr	.FindTaskSub			; We have to arrange a pathlist
							; which looks like this :
							; Size of block, Link to next, Lock-BPTR
		lea	AShellName(pc),a1
		bsr	.FindTaskSub

		lea	BCLIName(pc),a1
		bsr	.FindTaskSub

		bra	WorkAllMsgs
.GotProc	DOLIB	Forbid
		move.l	d0,a0				; So we have a process -> go on
		move.l	pr_CLI(a0),d0			; Get pointer to it`s CLI-Structure
		lsl.l	#2,d0				; Convert the BPTR
		move.l	d0,a0				; Get pointer to A0
		move.l	cli_CommandDir(a0),d0		; Get pointer to CommandDir
		lsl.l	#2,d0				; Convert to normal Pointer
		move.l	d0,a2				; Store pointer
		lea	SysPath,a5			; Get Pointer to Tags-Entry
.GetPath	move.l	a2,d0				; No (more) path ?
		beq.s	.GotPath			; -> Start Shell
		moveq	#12,d0				; Allocate a small link-structure
		move.l	#MEMF_CLEAR,d1
		CALLSYS	AllocMem
		move.l	d0,d1				; Could not get mem for one more
		beq.s	.GotPath			; path lock -> start Shell
		addq.l	#4,d1				; Point to middle of these 3 LongWords
		lsr.l	#2,d1				; Convert to BPTR
		move.l	d1,(a5)				; Store Pointer to structure
		move.l	d0,a5				; Let A5 point to new memory block
		addq.l	#4,a5				; -> right to the middle
		move.l	d0,a3				; Store pointer
		move.l	4(a2),d1			; Duplicate Lock to Path
		RELLIB	DupLock,_DOSBase
		move.l	d0,8(a3)			; Store LockPointer
		clr.l	4(a3)				; Clear link-pointer
		move.l	#12,0(a3)			; Store length of one Path-Entry
		move.l	(a2),d0				; Get pointer to mother`s next path
		lsl.l	#2,d0				; Convert to normal pointer
		move.l	d0,a2				; Now restart for next path
		bra.s	.GetPath
.GotPath	CALLSYS	Permit
		lea	SystemComm(a4),a0
		move.l	a0,d1				; Start Shell...
		lea	SystemTags(pc),a0
		move.l	a0,d2
		RELLIB	SystemTagList,_DOSBase
		tst.l	d0
		bge.s	.NoProc
		move.l	SysInput(pc),d1
		DOLIB	Close
		move.l	SysOutput(pc),d1
		DOLIB	Close
.NoProc		bra	WorkAllMsgs
.FindTaskSub	DOLIB	FindTask
		tst.l	d0
		bne.s	.FoundTask
		rts
.FoundTask	addq.w	#4,sp
		bra	.GotProc

NoShellPop	cmp.l	#UMLAUTKEYID,MsgID(a4)		; Change "Umlaut" mode ?
		bne.s	NoUmlautEx
		CALLSYS	Forbid
		bchg	#0,MapUmlaut(a4)
		move.b	MapUmlaut(a4),d0
		moveq	#2,d1
		lea	SetGads(a4),a0
		move.l	SetHandle(a4),a1
		RELLIB	SetCheckBox,_KCXBase
		CALLSYS	Permit
		bra	WorkAllMsgs

NoUmlautEx	cmp.l	#SNAPKEYID,MsgID(a4)		; Snap memory ?
		bne	NoSnapHotKey
		tst.b	Snapped(a4)
		bne.s	.DisplaySnap
		sub.l	a0,a0				; Give feedback
		RELLIB	DisplayBeep,_IntuitionBase
		CALLSYS	Forbid
		move.l	#MEMF_CHIP,d1			; Get current memory settings
		DOLIB	AvailMem
		move.l	d0,ThisChip
		move.l	#MEMF_FAST,d1
		DOLIB	AvailMem
		move.l	d0,ThisFast
		move.b	#1,Snapped(a4)
		cmp.l	#SNAPMEMPAGE,PageNum(a4)
		bne.s	.End
		bsr	ClearTitleBar
		move.l	OldPageNum(a4),d0
		move.l	d0,PageNum(a4)
.End		CALLSYS	Permit
		bra	WorkAllMsgs
.DisplaySnap	CALLSYS	Forbid
		move.l	#MEMF_CHIP,d1			; Get current memory settings
		DOLIB	AvailMem
		sub.l	ThisChip,d0			; Get delta mem usage
		move.l	d0,ThisChip
		move.l	#MEMF_FAST,d1
		DOLIB	AvailMem
		sub.l	ThisFast,d0			; Get delta mem usage
		move.l	d0,ThisFast
		move.l	PageNum(a4),d0
		move.l	d0,OldPageNum(a4)
		move.l	#SNAPMEMPAGE,PageNum(a4)	; Switch to correct display
		clr.b	Snapped(a4)			; Clear flag
		bsr	ClearTitleBar
		bsr	SigTime
		CALLSYS	Permit
		bra	WorkAllMsgs

NoSnapHotKey	cmp.l	#DOBLANKKEYID,MsgID(a4)		; Blank screen ?
		bne.s	NoDoBlankHKey
		move.b	#1,DoBlankNow(a4)
		bra	WorkAllMsgs

NoDoBlankHKey	cmp.l	#FORMKEYID,MsgID(a4)		; Format window ?
		bne.s	NoFormHKey
		bsr	OpenFormWin
		bra	WorkAllMsgs

NoFormHKey	cmp.l	#DUMPKEYID,MsgID(a4)		; Dump screen ?
		bne.s	NoDumpHKey
		tst.l	PrintProc(a4)
		bne.s	.End
		bsr	StartPrinter
.End		bra	WorkAllMsgs

NoDumpHKey	cmp.l	#CENTERKEYID,MsgID(a4)		; Recenter a screen ?
		bne.s	NoCenterHKey
		CALLSYS	Forbid
		moveq	#0,d0
		RELLIB	LockIBase,_IntuitionBase
		move.l	d0,-(sp)
		bsr	GetDispCols
		move.l	ib_FirstScreen(a6),a3
		move.l	(sp)+,a0
		DOLIB	UnlockIBase
		move.l	a3,d0
		beq.s	.End
		move.l	a3,a0
		move.w	sc_ViewPort+vp_Modes(a0),d1
		andi.w	#V_HIRES|V_SUPERHIRES,d1
		bne.s	.SuperHigh
		lsr.w	#1,d5
		bra.s	.MoveOK
.SuperHigh	andi.w	#V_SUPERHIRES,d1
		beq.s	.MoveOK
		add.w	d5,d5
.MoveOK		sub.w	sc_Width(a0),d5
		bls.s	.End
		lsr.w	#1,d5
		sub.w	sc_LeftEdge(a0),d5
		beq.s	.End
		moveq	#0,d1
		move.w	d5,d0
		RELLIB	MoveScreen,_IntuitionBase
.End		CALLSYS	Permit
		bra	WorkAllMsgs

NoCenterHKey	cmp.l	#SELECTSCRKEYID,MsgID(a4)	; Pop a screen to front ?
		bne.s	NoSelScrHKey
		bsr	OpenSelScrWin
		bra	WorkAllMsgs

NoSelScrHKey	cmp.l	#CLOCKKEYID,MsgID(a4)		; On/Off clock ???
		bne.s	NoClockHKey
		CALLSYS	Forbid
		bchg	#0,ShowClock(a4)
		move.b	ShowClock(a4),d0
		move.l	#0,d1				; Set gadget within clock def window
		lea	ClockDefGads(a4),a0
		move.l	ClockDefHandle(a4),a1
		RELLIB	SetCheckBox,_KCXBase
		bsr	ClearTitleBar
		bsr	CloseClockWin
		bsr	SigTime
		CALLSYS	Permit
		bra	WorkAllMsgs

NoClockHKey	cmp.l	#FKEYKEYID,MsgID(a4)		; On/Off FKeys ???
		bne.s	NoFKeyHKey
		CALLSYS	Forbid
		bchg	#0,FKeysOn(a4)
		move.b	FKeysOn(a4),d0
		move.l	#11,d1				; Set gadget within clock def window
		lea	FKeyGads(a4),a0
		move.l	FKeyHandle(a4),a1
		RELLIB	SetCheckBox,_KCXBase
		CALLSYS	Permit
		bra	WorkAllMsgs

NoFKeyHKey	cmp.l	#SUNKEYID,MsgID(a4)		; On/Off SunMouse ???
		bne.s	NoSunHKey
		CALLSYS	Forbid
		bchg	#0,SunMouse(a4)
		move.b	SunMouse(a4),d0
		moveq	#1,d1
		lea	MouseGads(a4),a0
		move.l	MouseHandle(a4),a1
		RELLIB	SetCheckBox,_KCXBase
		CALLSYS	Permit
NoSunHKey	bra	WorkAllMsgs

NoCxMsgs	move.l	ThisSig(a4),d0			; Formatter window ?
		and.l	FormWinSig(a4),d0
		beq	NoFormWinSig
.Loop		move.l	FormHandle(a4),a0
		move.l	wd_UserPort(a0),a0
		RELLIB	GT_GetIMsg,_GadToolsBase
		tst.l	d0
		beq	CheckBroker

		move.l	d0,a1
		move.l	im_Class(a1),d6			; Get class
		moveq	#0,d5
		move.w	im_Code(a1),d5			; Get code
		cmp.l	#IDCMP_VANILLAKEY,d6
		bne.s	.End
		move.w	im_Qualifier(a1),d4
		move.l	d5,d0
		bsr	GetUpperCase
		move.l	d0,d5
.End		move.l	d6,d0
		and.l	#(IDCMP_GADGETUP|IDCMP_GADGETDOWN),d0
		beq.s	.NoGad
		move.l	im_IAddress(a1),a2		; Get gadget id
		move.w	gg_GadgetID(a2),d7

.NoGad		DOLIB	GT_ReplyIMsg			; Reply gadtools msg
		cmp.l	#IDCMP_VANILLAKEY,d6
		bne.s	.NoVKey
		cmp.b	#ESCKEY,d5
		bne.s	.NoEsc
		move.l	#IDCMP_CLOSEWINDOW,d6
		bra.s	.NoVKey
.NoEsc		lea	FormGads(a4),a3
		moveq	#0,d1
.FindGad	move.l	(a3),d0
		beq.s	.NoVKey
		move.l	d0,a0
		move.l	gg_UserData(a0),d0
		cmp.b	d0,d5
		bne.s	.NextGad
		move.l	#IDCMP_GADGETUP,d6
		moveq	#-1,d5
		move.l	d1,d7
		bra.s	.NoVKey
.NextGad	addq.l	#4,a3
		addq.l	#1,d1
		bra.s	.FindGad
.NoVKey		bsr	HandleFormWin
		tst.l	FormHandle(a4)
		beq	CheckBroker
		bra	.Loop

NoFormWinSig	move.l	ThisSig(a4),d0			; Select Screen window ?
		and.l	SelScrWinSig(a4),d0
		beq	NoSelScrWinSig
.Loop		move.l	SelScrHandle(a4),a0
		move.l	wd_UserPort(a0),a0
		RELLIB	GT_GetIMsg,_GadToolsBase
		tst.l	d0
		beq	CheckBroker

		move.l	d0,a1
		move.l	im_Class(a1),d6			; Get class
		moveq	#0,d5
		move.w	im_Code(a1),d5			; Get code
		cmp.l	#IDCMP_VANILLAKEY,d6
		bne.s	.End
		move.w	im_Qualifier(a1),d4
		move.l	d5,d0
		bsr	GetUpperCase
		move.l	d0,d5
.End		move.l	d6,d0
		and.l	#(IDCMP_GADGETUP|IDCMP_GADGETDOWN),d0
		beq.s	.NoGad
		move.l	im_IAddress(a1),a2		; Get gadget id
		move.w	gg_GadgetID(a2),d7

.NoGad		move.l	a1,a3				; For current time
		DOLIB	GT_ReplyIMsg			; Reply gadtools msg
		cmp.l	#IDCMP_VANILLAKEY,d6
		bne.s	.NoVKey
		cmp.b	#ESCKEY,d5
		bne.s	.NoEsc
		move.l	#IDCMP_CLOSEWINDOW,d6
		bra.s	.NoVKey
.NoEsc		lea	SelScrGads(a4),a3
		moveq	#0,d1
.FindGad	move.l	(a3),d0
		beq.s	.NoVKey
		move.l	d0,a0
		move.l	gg_UserData(a0),d0
		cmp.b	d0,d5
		bne.s	.NextGad
		move.l	#IDCMP_GADGETUP,d6
		moveq	#-1,d5
		move.l	d1,d7
		bra.s	.NoVKey
.NextGad	addq.l	#4,a3
		addq.l	#1,d1
		bra.s	.FindGad
.NoVKey		bsr	HandleSelScrWin
		tst.l	SelScrHandle(a4)
		beq	CheckBroker
		bra	.Loop

NoSelScrWinSig
		move.l	ThisSig(a4),d0			; App-Message ?
		and.l	AppSigFlag(a4),d0
		beq.s	NoAppMsg

.LoopAppMsg	move.l	AppPort(a4),a0			; Get message
		CALLSYS	GetMsg
		tst.l	d0
		beq.s	NoAppMsg
		move.l	d0,a1
		cmp.w	#MTYPE_APPICON,am_Type(a1)	; Did it come from window ?
		bne.s	.NoAppIcon
		tst.l	am_NumArgs(a1)
		bne.s	.ReplyAppMsg
		tst.l	am_ArgList(a1)
		bne.s	.ReplyAppMsg
		move.l	a1,-(sp)
		bsr	OpenMainWin			; Open our window
		move.l	(sp)+,a1
		bra.s	.ReplyAppMsg
.NoAppIcon	cmp.w	#MTYPE_APPMENUITEM,am_Type(a1)	; Was a menu item ?
		bne.s	.ReplyAppMsg
		cmp.l	#MAINITEM,am_ID(a1)		; The control-window ?
		bne.s	.ReplyAppMsg
		move.l	a1,-(sp)
		bsr	OpenMainWin			; Open it
		move.l	(sp)+,a1
.ReplyAppMsg	CALLSYS	ReplyMsg
		bra	.LoopAppMsg

NoAppMsg	move.l	ThisSig(a4),d0			; Signal for formatter ?
		and.l	FormatSigFlag(a4),d0
		beq.s	NoFormSig
		bsr	CheckFormat

NoFormSig	move.l	ThisSig(a4),d0			; Signal for dragging ?
		and.l	DragSigFlag(a4),d0
		beq.s	NoDragSig
		bsr	DoDragging

NoDragSig	move.l	ThisSig(a4),d0			; Signal from Com-Port ?
		and.l	BackPortSig(a4),d0
		beq	CheckBroker
		move.l	BackPort(a4),a0
		CALLSYS	GetMsg
		tst.l	d0
		beq	CheckBroker
		move.l	d0,a1
		move.w	cmsg_Type(a1),d0		; Get Type

		IFGT	DEBUG				; Some debug information
		DBUG	.ComOut,d0,#0
		bra.s	.End
.ComOut		dc.b	"COM-MESSAGE %.2lx",10,0
		EVEN
.End
		ENDC
		move.l	a1,-(sp)
		cmp.w	#COM_DISABLE,d0			; Disable Broker ???
		bne.s	.NoDisable
		bsr	DisableBroker
		bra	.DoneCom
.NoDisable	cmp.w	#COM_ENABLE,d0			; Enable Broker ???
		bne.s	.NoEnable
		bsr	EnableBroker
		bra	.DoneCom
.NoEnable	cmp.w	#COM_CLEARTITLE,d0		; Clear titlebar ???
		bne.s	.NoClearTitle
		bsr	ClearTitleBar
		bra	.DoneCom
.NoClearTitle	cmp.w	#COM_CLOSECLOCK,d0		; Close clock window ???
		bne.s	.NoCloseClock
		bsr	CloseClockWin
		bra	.DoneCom
.NoCloseClock	cmp.w	#COM_SETNOCLICK,d0		; Set click ?
		bne.s	.NoSetClick
		bsr	SetNoClick
		bra	.DoneCom
.NoSetClick	cmp.w	#COM_OPENFORM,d0		; Open Formatter window ?
		bne.s	.NoOpenForm
		bsr	OpenFormWin
		bra	.DoneCom
.NoOpenForm	cmp.w	#COM_REMOVE,d0			; Remove background stuff ?
		bne.s	.NoBackRem
		move.w	#1,QuitBroker(a4)
		bra	.DoneCom
.NoBackRem	cmp.w	#COM_APPICONON,d0		; Show AppIcon ?
		bne.s	.NoAppOn
		bsr	OnAppIcon
		bra	.DoneCom
.NoAppOn	cmp.w	#COM_APPICONOFF,d0		; Hide AppIcon ?
		bne.s	.NoAppOff
		bsr	OffAppIcon
		bra	.DoneCom
.NoAppOff	cmp.w	#COM_DELENVS,d0			; Delete Environment vars ?
		bne.s	.NoDelEnvs
		bsr	DeleteEnvs
		bra	.DoneCom
.NoDelEnvs	cmp.w	#COM_NEWPREFS,d0		; New prefs loaded ???
		bne.s	.NoNewPrefs
		bsr	SetBillGads
		bsr	SetFormGads
		move.b	#1,DoRemakeCosts(a4)
		bsr	RemakeCosts
		bra.s	.NewAlarmSmp
.NoNewPrefs	cmp.w	#COM_NEWALARM,d0		; New alarm sample ???
		bne.s	.NoNewAlarmSmp
.NewAlarmSmp	bsr	RemoveAlarmSmp
		bsr	InitAlarmSmp
		bra.s	.DoneCom
.NoNewAlarmSmp	cmp.w	#COM_PLAYALARM,d0		; Play alarm sample ???
		bne.s	.NoPlayAlarm
		bsr	PlayAlarm
		bra.s	.DoneCom
.NoPlayAlarm	cmp.w	#COM_REMAKECOSTS,d0		; Remake costs ???
		bne.s	.NoRemakeCosts
		bsr	RemakeCosts
		bra.s	.DoneCom
.NoRemakeCosts	cmp.w	#COM_PRINTER,d0			; Start printer ???
		bne.s	.NoStartPrint
		bsr	StartPrinter
		bra.s	.DoneCom
.NoStartPrint	cmp.w	#COM_REBLACK,d0			; Reblack Border ???
		bne.s	.NoBlackBorder
		move.l	GlobalTagAdr(a4),d0
		beq.s	.DoneCom
		move.l	d0,a0
		move.l	#VTAG_BORDERBLANK_SET,d0
		tst.b	tagscr_BlackBorder(a0)
		bne.s	.GoOn
		move.l	#VTAG_BORDERBLANK_CLR,d0
.GoOn		move.l	d0,BlackTags
		bsr	ReBlackBorder2
		bra.s	.DoneCom
.NoBlackBorder	cmp.w	#COM_FLICKER,d0			; Remove/Install Flicker IRQ ?
		bne.s	.NoFlicker
		tst.b	FlickerOff(a4)
		beq.s	.Remove
		bsr	InitFlickerIRQ
		bra.s	.DoneCom
.Remove		bsr	RemFlickerIRQ
		bra.s	.DoneCom
.NoFlicker
.DoneCom	move.l	(sp)+,a1
		CALLSYS	ReplyMsg
		bra	CheckBroker			; Loop for all stuff
AbortBroker	tst.l	PrintProc(a4)
		beq.s	.NoPrinter
		move.l	#STILLPRINT_ERR,ErrorBack(a4)
		bsr	DisplayError
		clr.w	QuitBroker(a4)
		bra	CheckBroker

.NoPrinter	tst.b	CheckQuit(a4)
		beq.s	.NoQuit
		lea	MyEasyRequest(pc),a1
		move.l	#EasyTitle,es_Title(a1)
		LOCLEA	QuitText,a0
		move.l	a0,es_TextFormat(a1)
		LOCLEA	QuitGads,a0
		move.l	a0,es_GadgetFormat(a1)
		sub.l	a0,a0
		sub.l	a2,a2
		sub.l	a3,a3
		RELLIB	EasyRequestArgs,_IntuitionBase
		clr.w	QuitBroker(a4)
		tst.l	d0
		beq	CheckBroker
.NoQuit		rts
**********************************************************************
*                  Tags for use to open a new shell                  *
**********************************************************************
SystemTags	dc.l	SYS_Input
SysInput	dc.l	0
		dc.l	SYS_Output
SysOutput	dc.l	0
		dc.l	SYS_Asynch
		dc.l	1
		dc.l	NP_Priority
		dc.l	0
SysCLI		dc.l	NP_Path
SysPath		dc.l	0
		dc.l	NP_CurrentDir
SysDir		dc.l	0
		dc.l	TAG_DONE
DefSystemComm	dc.b	"NewSHELL CON:0/11//200/KCX-Shell/CLOSE/ALT///50/50",0
SystemInOut	dc.b	"NIL:",0
WBPName		dc.b	"Workbench",0			; Names of processes we want
							; to clone path-lists from :-)
NCLIName	dc.b	"New CLI",0
ICLIName	dc.b	"Initial CLI",0
AShellName	dc.b	"AmigaShell",0
BCLIName	dc.b	"Background CLI",0
		EVEN




**********************************************************************
*                    Handle the formatter window                     *
**********************************************************************
HandleFormWin	cmp.l	#IDCMP_CLOSEWINDOW,d6		; Close window ?
		beq	CloseFormWin

		cmp.l	#IDCMP_CHANGEWINDOW,d6		; Window moved ?
		bne.s	NoFormMove
		move.l	FormHandle(a4),a0
		lea	FormWinLeft(a4),a1
		bra	StoreWinPos

NoFormMove	cmp.l	#IDCMP_GADGETUP,d6		; Gadget picked ?
		bne	NoFormGadUp

		cmp.w	#GD_Label,d7			; Changed Label ?
		bne.s	NoLabelGad
		tst.l	d5
		bge.s	NoLabelKey
LabelKey	move.l	#GD_Label,d0
		lea	FormGads(a4),a0
		move.l	FormHandle(a4),a1
		RELLIB	ActStringGad,_KCXBase
		rts
NoLabelKey	move.l	d7,d0				; Copy new string
		lea	FormGads(a4),a0
		RELLIB	GetGadgetPtr,_KCXBase
		move.l	gg_SpecialInfo(a0),a0
		move.l	si_Buffer(a0),a0
		lea	FormatDriveName(a4),a1
		jmp	StrCpy

NoLabelGad	cmp.w	#GD_Trashcan,d7			; Trashcan ?
		bne.s	NoTrashGad
		bchg	#0,MakeIcons(a4)
		move.b	MakeIcons(a4),d0
		move.l	#GD_Trashcan,d1
		bra	SetFormBox

NoTrashGad	cmp.w	#GD_Quick,d7			; Quick format ?
		bne.s	NoQuickGad
		bchg	#0,QuickFormat(a4)
		move.b	QuickFormat(a4),d0
		move.l	#GD_Quick,d1
		bra	SetFormBox

NoQuickGad	cmp.w	#GD_FFS,d7			; FFS format ?
		bne.s	NoFFSGad
		bchg	#0,FFSDrive(a4)
		move.b	FFSDrive(a4),d0
		move.l	#GD_FFS,d1
		bra	SetFormBox

NoFFSGad	cmp.w	#GD_NoVerify,d7			; No verify ?
		bne.s	NoVerifyGad
		bchg	#0,NoVerify(a4)
		move.b	NoVerify(a4),d0
		move.l	#GD_NoVerify,d1
		bra	SetFormBox

NoVerifyGad	cmp.w	#GD_Install,d7			; Install disk ?
		bne.s	NoInstallGad
		bchg	#0,Install(a4)
		move.b	Install(a4),d0
		move.l	#GD_Install,d1
		bra	SetFormBox

NoInstallGad	cmp.w	#GD_Eject,d7			; Eject disk ?
		bne.s	NoEjectGad
		bchg	#0,EjectDrive(a4)
		move.b	EjectDrive(a4),d0
		move.l	#GD_Eject,d1
		bra	SetFormBox

NoEjectGad	cmp.w	#GD_IntMode,d7			; International mode ???
		bne.s	NoIntModeGad
		tst.b	DirCache(a4)
		bne.s	.End
		bchg	#0,InterMode(a4)
		move.b	InterMode(a4),d0
		move.b	d0,OldInterMode(a4)
		move.l	#GD_IntMode,d1
		bra	SetFormBox
.End		rts

NoIntModeGad	cmp.w	#GD_DiskCache,d7		; Dir Caching ?
		bne.s	NoDirCacheGad
		bchg	#0,DirCache(a4)
		move.b	DirCache(a4),d0
		beq.s	.UseOldInt
		moveq	#1,d0
		move.b	d0,InterMode(a4)
		moveq	#1,d2
		bra.s	.SetGad
.UseOldInt	move.b	OldInterMode(a4),d0
		move.b	d0,InterMode(a4)
		moveq	#0,d2
.SetGad		move.l	#GD_IntMode,d1
		bsr	SetFormBox
		move.l	#GD_IntMode,d0
		move.l	d2,d1
		DOLIB	GhostGadget
		move.b	DirCache(a4),d0
		move.l	#GD_DiskCache,d1
		bra	SetFormBox

NoDirCacheGad	cmp.w	#GD_WatchDisks,d7		; Watch disks ?
		bne.s	NoWatchGad
		bchg	#0,WatchDisks(a4)
		move.b	WatchDisks(a4),d0
		move.l	#GD_WatchDisks,d1
		bra	SetFormBox

NoWatchGad	cmp.w	#GD_Watch0,d7			; Changed one of the watches ?
		blt.s	NoWatchesGad
		cmp.w	#GD_Watch3,d7
		bgt.s	NoWatchesGad
		move.w	d7,d6
		sub.w	#GD_Watch0,d7
		move.w	d7,d0
		mulu	#28,d0
		lea	WatchGads(pc),a0
		tst.l	4(a0,d0)
		bne.s	.End
		lea	Watch0(a4),a0
		bchg	#0,0(a0,d7.w)
		move.b	0(a0,d7.w),d0
		move.l	d6,d1
		bra	SetFormBox
.End		rts

NoWatchesGad	cmp.w	#GD_FormOk,d7			; Format ?
		bne.s	NoFormOkGad
		tst.l	d5
		bge.s	StartFormat
		move.l	d7,d0
		lea	FormGads(a4),a0
		move.l	FormHandle(a4),a5
		bsr	ButtonCheck
		bra.s	StartFormat

NoFormOkGad	cmp.w	#GD_FormCancel,d7		; Cancel ?
		bne.s	NoFormClGad
		tst.l	d5
		bge	CloseFormWin
		move.l	d7,d0
		lea	FormGads(a4),a0
		move.l	FormHandle(a4),a5
		bsr	ButtonCheck
		bra	CloseFormWin

NoFormClGad	cmp.w	#GD_Drive,d7			; New drive selected ?
		bne.s	NoDriveGad
		tst.l	d5
		bge.s	.NoKey
		move.w	FormatDrive(a4),d5
		and.w	#IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT,d4
		beq.s	.Up
		addq.w	#1,d5
		cmp.w	NumDrives(a4),d5
		blt.s	.NoKey
		moveq	#0,d5
		bra.s	.NoKey
.Up		subq.w	#1,d5
		bge.s	.NoKey
		move.w	NumDrives(a4),d5
		subq.w	#1,d5
.NoKey		move.w	d5,FormatDrive(a4)
		move.l	#GD_Drive,d0
		move.w	FormatDrive(a4),d1
		ext.l	d1
		lea	FormGads(a4),a0
		move.l	FormHandle(a4),a1
		RELLIB	SetCycleGad,_KCXBase
NoDriveGad	rts
StartFormat	bsr	GoGoGoFormat			; Start format
		bra	CloseFormWin
NoFormGadUp	rts






**********************************************************************
*                     Handle SelectScreen window                     *
**********************************************************************
HandleSelScrWin	cmp.l	#IDCMP_CLOSEWINDOW,d6		; Close window ?
		beq	CloseSelScrWin

		cmp.l	#IDCMP_CHANGEWINDOW,d6		; Window moved ?
		bne.s	NoSelScrMove
		move.l	SelScrHandle(a4),a0
		lea	SelScrWinLeft(a4),a1
		bra	StoreWinPos

NoSelScrMove	cmp.l	#IDCMP_GADGETUP,d6		; Gadget clicked ???
		bne	NoSelScrGadUp

		cmp.w	#GD_TaskList,d7			; New screen selected ???
		bne	NoTaskListGad
		tst.l	d5
		bge.s	.NoKey
		move.w	SelectedScreen(a4),d0
		and.w	#IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT,d4
		bne.s	.Up
		move.w	NumScreens(a4),d1
		subq.w	#1,d1
		cmp.w	d1,d0
		bge.s	.Done
		addq.w	#1,d0
		bra.s	.Done
.Up		tst.w	d0
		ble.s	.Done
		subq.w	#1,d0
.Done		move.w	d0,SelectedScreen(a4)
		moveq	#0,d1
		move.w	d0,d1
		move.l	d1,d2
		move.l	#GD_TaskList,d0
		lea	SelScrGads(a4),a0
		move.l	SelScrHandle(a4),a1
		RELLIB	SetListViewGad,_KCXBase
		rts
.NoKey		move.w	d5,SelectedScreen(a4)
		tst.w	LastSelScr(a4)
		bge.s	.CheckDouble
.Remake		move.w	d5,LastSelScr(a4)
		move.l	im_Seconds(a3),LastSelScrSec(a4)
		move.l	im_Micros(a3),LastSelScrMin(a4)
		bra.s	.End
.CheckDouble	cmp.w	LastSelScr(a4),d5
		bne.s	.Remake
		move.l	LastSelScrSec(a4),d0
		move.l	LastSelScrMin(a4),d1
		move.l	im_Seconds(a3),d2
		move.l	im_Micros(a3),d3
		RELLIB	DoubleClick,_IntuitionBase
		move.w	#-1,LastSelScr(a4)
		tst.l	d0
		bne.s	SelScrActKey
.End		rts

NoTaskListGad	cmp.w	#GD_SelScrAct,d7		; Activate screen
		bne.s	NoSelScrActGad
		tst.l	d5
		bge.s	.NoKey
		move.l	#GD_SelScrAct,d0
		lea	SelScrGads(a4),a0
		move.l	SelScrHandle(a4),a5
		bsr	ButtonCheck
.NoKey
SelScrActKey	move.w	SelectedScreen(a4),d0
		lea	SelScrList(a4),a0
		move.l	MLH_HEAD(a0),a0
		sub.l	a1,a1
		tst.w	d0
		blt.s	.Done
.Loop		TSTLST2	a0,a2
		beq.s	.Done
		tst.w	d0
		beq.s	.GotIt
		move.l	a0,d1
		beq.s	.Done
		SUCC	a0,a0
		subq.w	#1,d0
		bra.s	.Loop
.GotIt		move.l	a0,a1
.Done		move.l	a1,d0
		beq.s	.End
		move.l	selscr_Address(a1),a0
		RELLIB	ScreenToFront,_IntuitionBase
		bra	CloseSelScrWin
.End		rts

NoSelScrActGad	cmp.w	#GD_SelScrCancel,d7		; Cancel ???
		bne.s	NoSelScrCancel
		tst.l	d5
		bge.s	.NoKey
		move.l	#GD_SelScrCancel,d0
		lea	SelScrGads(a4),a0
		move.l	SelScrHandle(a4),a5
		bsr	ButtonCheck
.NoKey		bsr	CloseSelScrWin
		move.l	LastSelectedScr(a4),a0
		RELLIB	ScreenToFront,_IntuitionBase
		rts

NoSelScrCancel	cmp.w	#GD_SelScrUpdate,d7		; Update Screen List ?
		bne	NoSelScrUpGad
		tst.l	d5
		bge.s	.NoKey
		move.l	#GD_SelScrUpdate,d0
		lea	SelScrGads(a4),a0
		move.l	SelScrHandle(a4),a5
		bsr	ButtonCheck
.NoKey		move.l	#GD_TaskList,d0
		lea	SelScrGads(a4),a0
		move.l	SelScrHandle(a4),a1
		move.l	#~0,a2
		RELLIB	SetListViewLst,_KCXBase
		lea	SelScrList(a4),a0
		DOLIB	FreeWindows
		bsr	CollectScrs
		move.l	#GD_TaskList,d0
		lea	SelScrGads(a4),a0
		move.l	SelScrHandle(a4),a1
		lea	SelScrList(a4),a2
		RELLIB	SetListViewLst,_KCXBase
		moveq	#0,d1
		move.w	SelectedScreen(a4),d1
		move.l	d1,d2
		DOLIB	SetListViewGad
		move.l	#GD_SelScrNumScr,d0
		move.l	ScrNumTag+4,d1
		DOLIB	SetNumberGad
		move.l	#GD_SelScrDefPub,d0
		lea	DefPubScreenName(pc),a2
		DOLIB	SetTextGad
NoSelScrUpGad
NoSelScrGadUp	rts




**********************************************************************
*                       Play the alarm sample                        *
**********************************************************************
PlayAlarm	tst.b	SampleLoaded(a4)
		beq	.BeepDone

		move.l	_GfxBase(a4),a0			; Get clock rates
		move.w	gb_DisplayFlags(a0),d0
		and.w	#PAL,d0
		beq.s	.IsNTSC
		move.l	#3546895,d7
		bra.s	.DoSample
.IsNTSC		move.l	#3579545,d7

.DoSample	lea	AudioName(pc),a0		; Try to open device
		moveq	#0,d0
		move.l	AlarmReq(a4),a1
		move.w	#ADCMD_ALLOCATE,IO_COMMAND(a1)
		move.b	#ADIOF_NOWAIT,IO_FLAGS(a1)
		clr.w	ioa_AllocKey(a1)
		move.l	#AnyChannel,ioa_Data(a1)
		move.l	#4,ioa_Length(a1)
		move.l	AlarmPort(a4),MN_REPLYPORT(a1)
		move.b	#-50,LN_PRI(a1)
		moveq	#0,d1				; suggested by UISG
		CALLSYS	OpenDevice
		tst.l	d0
		bne.s	.BeepDone

		move.l	d7,d0				; Calculate period
		moveq	#0,d1
		move.w	AlarmSmpSec(a4),d1
		RELLIB	UDivMod32,_UtilityBase

		move.l	AlarmReq(a4),a1			; Play alarm
		move.w	#CMD_WRITE,IO_COMMAND(a1)
		move.b	#ADIOF_PERVOL,IO_FLAGS(a1)
		move.l	AlarmSmpStart(a4),ioa_Data(a1)
		move.l	AlarmSmpLen(a4),ioa_Length(a1)
		move.w	d0,ioa_Period(a1)
		move.w	#64,ioa_Volume(a1)
		move.w	#1,ioa_Cycles(a1)
		move.l	AlarmPort(a4),MN_REPLYPORT(a1)
		move.b	#-50,LN_PRI(a1)
		move.l	IO_DEVICE(a1),a6
		jsr	DEV_BEGINIO(a6)
		move.l	AlarmReq(a4),a1
		CALLSYS	WaitIO
		move.l	AlarmReq(a4),a1
		DOLIB	CloseDevice
.BeepDone	rts



**********************************************************************
*                  Start the external Prefs-Proggy                   *
**********************************************************************
OpenMainWin	tst.l	MainHandle(a4)			; Main window already open ?
		bne.s	.NoPrefsOpen
		lea	PrefsPath(a4),a0
		move.l	a0,d1
		move.l	#ACCESS_READ,d2
		RELLIB	Lock,_DOSBase
		tst.l	d0
		beq.s	.End
		move.l	d0,d1
		DOLIB	UnLock
		lea	SystemInOut(pc),a0
		move.l	a0,d1
		move.l	#MODE_OLDFILE,d2
		DOLIB	Open
		move.l	d0,PrefsTags+12
		lea	SystemInOut(pc),a0
		move.l	a0,d1
		move.l	#MODE_OLDFILE,d2
		DOLIB	Open
		move.l	d0,PrefsTags+20
		lea	PrefsPath(a4),a0
		move.l	a0,d1
		lea	PrefsTags(pc),a0
		move.l	a0,d2
		DOLIB	SystemTagList
.End		rts
.NoPrefsOpen	CALLSYS	Forbid
		move.l	MainHandle(a4),a0
		move.l	wd_WScreen(a0),a0
		RELLIB	ScreenToFront,_IntuitionBase
		move.l	MainHandle(a4),a0
		DOLIB	WindowToFront
		CALLSYS	Permit
		rts
PrefsTags	dc.l	SYS_Asynch,1
		dc.l	SYS_Input,0
		dc.l	SYS_Output,0
		dc.l	NP_Priority,0
		dc.l	NP_StackSize,8192
		dc.l	TAG_DONE



**********************************************************************
*                 Start the external printer program                 *
**********************************************************************
StartPrinter	tst.l	PrintProc(a4)
		bne.s	.End1
		lea	PrintPath(a4),a0
		move.l	a0,d1
		move.l	#ACCESS_READ,d2
		RELLIB	Lock,_DOSBase
		tst.l	d0
		beq.s	.End1
		move.l	d0,d1
		DOLIB	UnLock
		lea	SystemInOut(pc),a0		; Start Printer Process
		move.l	a0,d1
		move.l	#MODE_OLDFILE,d2
		DOLIB	Open
		move.l	d0,PrintTags+12
		lea	SystemInOut(pc),a0
		move.l	a0,d1
		move.l	#MODE_OLDFILE,d2
		DOLIB	Open
		move.l	d0,PrintTags+20
		lea	PrintPath(a4),a0
		move.l	a0,d1
		lea	PrintTags(pc),a1
		move.l	a1,d2
		DOLIB	SystemTagList
		tst.l	d0
		bge.s	.End
		move.l	PrintTags+12(pc),d1
		DOLIB	Close
		move.l	PrintTags+20(pc),d1
		DOLIB	Close
		bra.s	.End1
.End		move.l	#SIGHANDSHAKE,d0
		CALLSYS	Wait
.End1		rts
PrintTags	dc.l	SYS_Asynch,1
		dc.l	SYS_Input,0
		dc.l	SYS_Output,0
		dc.l	NP_Priority,0
		dc.l	TAG_DONE




**********************************************************************
*                     Open the formatter window                      *
**********************************************************************
OpenFormWin	tst.l	FormHandle(a4)
		beq.s	.GoOn
		move.l	FormHandle(a4),a0
		RELLIB	WindowToFront,_IntuitionBase
		rts
.GoOn		link	a5,#-(4+4+2+2+2+2+gng_SIZEOF)
		lea	FormTxt(a4),a0
		move.l	a0,FontPtr(a5)
		bsr	LockScreen
		tst.l	d0
		beq	.FormWinError
		moveq	#0,d2
		moveq	#0,d3
		RELLIB	ComputeFont,_KCXBase
		move.l	-4(a5),a0
		sub.l	a1,a1
		RELLIB	GetVisualInfoA,_GadToolsBase
		move.l	d0,FormVisInfo(a4)
		beq	.FormWinError

		move.l	-4(a5),a0
		moveq	#0,d3
		moveq	#0,d2
		move.w	FormWinWidth(a4),d2
		move.w	FormWinHeight(a4),d3
		RELLIB	ComputeFont,_KCXBase
		move.l	-4(a5),a0
		move.l	d2,d0
		DOLIB	ComputeX
		move.l	d0,d4
		move.w	FormWinLeft(a4),d2
		move.l	-4(a5),a3
		move.w	sc_LeftEdge(a3),d5
		neg.w	d5
		add.w	d5,d2
		add.w	d2,d0
		add.w	OffX(a5),d0
		moveq	#0,d1
		move.b	sc_WBorRight(a0),d1
		add.w	d1,d0
		cmp.w	sc_Width(a0),d0
		bls.s	.WOk
		move.w	sc_Width(a0),d0
		sub.w	d4,d0
		move.w	d0,d2
.WOk		move.l	d3,d0
		DOLIB	ComputeY
		move.l	d0,d4
		move.w	FormWinTop(a4),d3
		move.l	-4(a5),a3
		move.w	sc_TopEdge(a3),d5
		neg.w	d5
		add.w	d5,d3
		add.w	d3,d0
		add.w	OffY(a5),d0
		moveq	#0,d1
		move.b	sc_WBorBottom(a0),d1
		add.w	d1,d0
		cmp.w	sc_Height(a0),d0
		bls.s	.HOk
		move.w	sc_Height(a0),d0
		sub.w	d4,d0
		move.w	d0,d3
.HOk		lea.l	FormGadget(a4),a0
		RELLIB	CreateContext,_GadToolsBase
		move.l	d0,a3
		beq	.FormWinError

		movem.l d2-d3/a4,-(sp)
		moveq	#0,d3
		lea.l	FormWinGTags,a4
.FormWinGL	lea.l	FormWinNGads,a0
		move.l	d3,d0
		mulu	#gng_SIZEOF,d0
		add.l	d0,a0
		lea.l	-(4+4+2+2+2+2+gng_SIZEOF)(a5),a1
		moveq	#gng_SIZEOF,d0
		CALLSYS	CopyMem
		lea.l	-(4+4+2+2+2+2+gng_SIZEOF)(a5),a0
		move.l	a4,-(sp)
		move.l	ZeroBase(pc),a4
		move.l	gng_GadgetText(a0),-(sp)
		bsr	GetLocStr
		move.l	(sp)+,d7
		move.l	d7,gng_GadgetText(a0)
		move.l	FormVisInfo(a4),gng_VisualInfo(a0)
		move.l	FontPtr(a5),gng_TextAttr(a0)
		move.w	gng_LeftEdge(a0),d0
		RELLIB	ComputeX,_KCXBase
		move.l	(sp)+,a4
		add.w	OffX(a5),d0
		move.w	d0,gng_LeftEdge(a0)
		move.w	gng_TopEdge(a0),d0
		DOLIB	ComputeY
		add.w	OffY(a5),d0
		move.w	d0,gng_TopEdge(a0)
		move.w	gng_Width(a0),d0
		DOLIB	ComputeX
		move.w	d0,gng_Width(a0)
		move.w	gng_Height(a0),d0
		DOLIB	ComputeY
		move.w	d0,gng_Height(a0)
		lea.l	FormWinGTypes,a0
		moveq	#0,d0
		move.l	d3,d1
		lsl.l	#1,d1
		move.w	0(a0,d1.w),d0
		move.l	a3,a0
		lea.l	-(4+4+2+2+2+2+gng_SIZEOF)(a5),a1
		move.l	a4,a2
		move.l	a4,-(sp)
		move.l	ZeroBase(pc),a4
		RELLIB	CreateGadgetA,_GadToolsBase
		move.l	(sp)+,a4
		tst.l	d0
		beq	.FormWinError
		move.l	d0,a3
		move.l	d7,a0
		bsr	LocalizeGadKey
		move.l	d1,gg_UserData(a3)
		move.l	d3,d0
		lsl.l	#2,d0
		move.l	a4,-(sp)
		move.l	ZeroBase(pc),a4
		lea.l	FormGads(a4),a0
		move.l	(sp)+,a4
		move.l	a3,0(a0,d0.w)
.FormWinTL	tst.l	(a4)
		beq.s	.FormWinDN
		addq.w	#8,a4
		bra.s	.FormWinTL
.FormWinDN	addq.w	#4,a4
		addq.w	#1,d3
		cmp.w	#FormWin_CNT,d3
		bmi	.FormWinGL
		movem.l (sp)+,d2-d3/a4
		move.l	FormGadget(a4),FormWinWG+4

		lea	FormGads(a4),a0
		bsr	SetStrIntGads

		LOCLEA	DefFormTitle,a0
		lea	FormTitle(a4),a1
		bsr	StrCpy
		lea	FormatHotKey(a4),a0
		lea	FormTitle(a4),a1
		move.l	a1,FormWinWG+12
		bsr	StrCat
		LOCLEA	WindowScrTitle,FormWinWG+20
		move.l	-4(a5),FormWinSC+4		; Open window
		ext.l	d2
		ext.l	d3
		move.l	d2,FormWinL+4
		move.l	d3,FormWinT+4
		move.l	-4(a5),a0
		move.w	FormWinWidth(a4),d0
		RELLIB	ComputeX,_KCXBase
		add.w	OffX(a5),d0
		moveq	#0,d1
		move.b	sc_WBorRight(a0),d1
		add.w	d1,d0
		move.l	d0,FormWinW+4
		move.w	FormWinHeight(a4),d0
		DOLIB	ComputeY
		add.w	OffY(a5),d0
		moveq	#0,d1
		move.b	sc_WBorBottom(a0),d1
		add.w	d1,d0
		move.l	d0,FormWinH+4
		suba.l	a0,a0
		lea.l	FormWinWindowTags,a1
		RELLIB	OpenWindowTagList,_IntuitionBase
		move.l	d0,FormHandle(a4)
		beq.s	.FormWinError
		move.l	d0,a0
		moveq	#0,d0
		moveq	#1,d1
		move.l	wd_UserPort(a0),a0
		move.b	MP_SIGBIT(a0),d0
		lsl.l	d0,d1
		move.l	d1,FormWinSig(a4)

		move.l	FormHandle(a4),a0
		move.l	wd_RPort(a0),a2
		move.l	a2,a0
		lea.l	FormBevelTag(pc),a1
		move.l	FormVisInfo(a4),4(a1)
		move.l	#20,d0
		RELLIB	ComputeY,_KCXBase
		add.w	OffY(a5),d0
		move.l	d0,d1
		move.l	#502,d0
		DOLIB	ComputeX
		move.l	d0,d2
		move.l	#46,d0
		DOLIB	ComputeY
		move.l	d0,d3
		move.l	#2,d0
		DOLIB	ComputeX
		add.w	OffX(a5),d0
		RELLIB	DrawBevelBoxA,_GadToolsBase

		move.l	FormHandle(a4),a0
		suba.l	a1,a1
		DOLIB	GT_RefreshWindow

		bsr.s	SetFormGads

		move.l	-4(a5),a0
		RELLIB	ScreenToFront,_IntuitionBase

		bsr	UnlockScreen
		unlk	a5
		rts
.FormWinError	bsr	CloseFormWin
		move.l	#MEMORY_ERR,ErrorBack(a4)
		bsr	DisplayError
		bsr	UnlockScreen
		unlk	a5
		rts
FormBevelTag	dc.l	GT_VisualInfo,0,TAG_DONE



**********************************************************************
*                        Set Formatter gadget                        *
**********************************************************************
SetFormGads	move.b	MakeIcons(a4),d0
		move.l	#GD_Trashcan,d1
		bsr	SetFormBox
		move.b	QuickFormat(a4),d0
		move.l	#GD_Quick,d1
		bsr	SetFormBox
		move.b	FFSDrive(a4),d0
		move.l	#GD_FFS,d1
		bsr	SetFormBox
		move.b	NoVerify(a4),d0
		move.l	#GD_NoVerify,d1
		bsr	SetFormBox
		move.b	Install(a4),d0
		move.l	#GD_Install,d1
		bsr	SetFormBox
		move.b	EjectDrive(a4),d0
		move.l	#GD_Eject,d1
		bsr	SetFormBox
		move.b	WatchDisks(a4),d0
		move.l	#GD_WatchDisks,d1
		bsr	SetFormBox
		move.b	Watch0(a4),d0
		move.l	#GD_Watch0,d1
		bsr	SetFormBox
		move.b	Watch1(a4),d0
		move.l	#GD_Watch1,d1
		bsr	SetFormBox
		move.b	Watch2(a4),d0
		move.l	#GD_Watch2,d1
		bsr.s	SetFormBox
		move.b	Watch3(a4),d0
		move.l	#GD_Watch3,d1
		bsr.s	SetFormBox
		move.b	DirCache(a4),d0
		beq.s	.UseOldInt
		move.b	InterMode(a4),d0
		move.b	d0,OldInterMode(a4)
		moveq	#1,d0
		move.b	d0,InterMode(a4)
		moveq	#1,d2
		bra.s	.SetDirInt
.UseOldInt	move.b	OldInterMode(a4),d0
		move.b	d0,InterMode(a4)
		moveq	#0,d2
.SetDirInt	move.l	#GD_IntMode,d1
		bsr.s	SetFormBox
		move.l	#GD_IntMode,d0
		move.l	d2,d1
		RELLIB	GhostGadget,_KCXBase
		move.b	DirCache(a4),d0
		move.l	#GD_DiskCache,d1
		bsr.s	SetFormBox

		move.l	#GD_Drive,d0
		move.w	FormatDrive(a4),d1
		RELLIB	SetCycleGad,_KCXBase

		move.l	#GD_Label,d0
		lea	FormatDriveName(a4),a2
		DOLIB	SetStringGad
		rts
SetFormBox	lea	FormGads(a4),a0
		move.l	FormHandle(a4),a1
		RELLIB	SetCheckBox,_KCXBase
		rts



**********************************************************************
*                       Close formatter window                       *
**********************************************************************
CloseFormWin	lea	FormHandle(a4),a3
		bsr	CloseNormWindow
		clr.l	FormWinSig(a4)
		rts




**********************************************************************
*                        Open the bill window                        *
**********************************************************************
OpenBillWin	tst.l	BillHandle(a4)
		beq.s	.End
		move.l	BillHandle(a4),a0
		RELLIB	WindowToFront,_IntuitionBase
		rts
.End		link	a5,#-(4+4+2+2+2+2+gng_SIZEOF)	; Try to lock public screen
		lea	BillTxt(a4),a0
		move.l	a0,FontPtr(a5)
		lea	PubScreenName(a4),a3		; named in prefs
.FindScreen	tst.b	(a3)
		beq.s	.WBScreen
		move.l	a3,a2
.FindEnd	tst.b	(a2)
		beq.s	.GoOn
		cmp.b	#';',(a2)
		beq.s	.GoOn
		addq.l	#1,a2
		bra.s	.FindEnd
.GoOn		move.b	(a2),d6
		clr.b	(a2)
		move.l	a3,a0
		lea	BillPubScr(a4),a1
		bsr	StrCpy
		move.l	a3,a0
		RELLIB	LockPubScreen,_IntuitionBase
		move.l	d0,-4(a5)
		move.b	d6,(a2)
		tst.l	d0
		bne.s	.GotPubScreen
.FindNext	tst.b	(a3)
		beq.s	.WBScreen
		cmp.b	#';',(a3)+
		bne.s	.FindNext
		bra.s	.FindScreen

.WBScreen	sub.l	a0,a0				; On error -> Try to lock WB
		RELLIB	LockPubScreen,_IntuitionBase
		move.l	d0,-4(a5)
		beq	.BillWinError

.GotPubScreen	moveq	#0,d2
		moveq	#0,d3
		RELLIB	ComputeFont,_KCXBase
		move.l	-4(a5),a0
		sub.l	a1,a1
		RELLIB	GetVisualInfoA,_GadToolsBase
		move.l	d0,BillVisInfo(a4)
		beq	.BillWinError

		move.l	-4(a5),a0
		moveq	#0,d3
		moveq	#0,d2
		move.b	sc_BarHeight(a0),d3
		addq.w	#1,d3
		move.w	d3,BillZoomSize+6(a4)
		move.w	BillWinWidth(a4),d2
		move.w	BillWinHeight(a4),d3
		RELLIB	ComputeFont,_KCXBase
		move.l	-4(a5),a0
		move.l	d2,d0
		DOLIB	ComputeX
		move.l	d0,d4
		move.w	BillWinLeft(a4),d2
		add.w	d2,d0
		add.w	OffX(a5),d0
		moveq	#0,d1
		move.b	sc_WBorRight(a0),d1
		add.w	d1,d0
		cmp.w	sc_Width(a0),d0
		bls.s	.WOk
		move.w	sc_Width(a0),d0
		sub.w	d4,d0
		move.w	d0,d2
.WOk		move.l	d3,d0
		DOLIB	ComputeY
		move.l	d0,d4
		move.w	BillWinTop(a4),d3
		add.w	d3,d0
		add.w	OffY(a5),d0
		moveq	#0,d1
		move.b	sc_WBorBottom(a0),d1
		add.w	d1,d0
		cmp.w	sc_Height(a0),d0
		bls.s	.HOk
		move.w	sc_Height(a0),d0
		sub.w	d4,d0
		move.w	d0,d3
.HOk		lea.l	BillGadget(a4),a0
		RELLIB	CreateContext,_GadToolsBase
		move.l	d0,a3
		beq	.BillWinError

		movem.l d2-d3/a4,-(sp)
		moveq	#0,d3
		lea.l	BillWinGTags,a4
.BillWinGL	lea.l	BillWinNGads,a0
		move.l	d3,d0
		mulu	#gng_SIZEOF,d0
		add.l	d0,a0
		lea.l	-(4+4+2+2+2+2+gng_SIZEOF)(a5),a1
		moveq	#gng_SIZEOF,d0
		CALLSYS	CopyMem
		lea.l	-(4+4+2+2+2+2+gng_SIZEOF)(a5),a0
		move.l	a4,-(sp)
		move.l	ZeroBase(pc),a4
		move.l	gng_GadgetText(a0),-(sp)
		bsr	GetLocStr
		move.l	(sp)+,d7
		move.l	d7,gng_GadgetText(a0)
		move.l	BillVisInfo(a4),gng_VisualInfo(a0)
		move.l	FontPtr(a5),gng_TextAttr(a0)
		move.w	gng_LeftEdge(a0),d0
		RELLIB	ComputeX,_KCXBase
		move.l	(sp)+,a4
		add.w	OffX(a5),d0
		move.w	d0,gng_LeftEdge(a0)
		move.w	gng_TopEdge(a0),d0
		DOLIB	ComputeY
		add.w	OffY(a5),d0
		move.w	d0,gng_TopEdge(a0)
		move.w	gng_Width(a0),d0
		DOLIB	ComputeX
		move.w	d0,gng_Width(a0)
		move.w	gng_Height(a0),d0
		DOLIB	ComputeY
		move.w	d0,gng_Height(a0)
		lea.l	BillWinGTypes,a0
		moveq	#0,d0
		move.l	d3,d1
		lsl.l	#1,d1
		move.w	0(a0,d1.w),d0
		move.l	a3,a0
		lea.l	-(4+4+2+2+2+2+gng_SIZEOF)(a5),a1
		move.l	a4,a2
		move.l	a4,-(sp)
		move.l	ZeroBase(pc),a4
		bsr	LocalizeGadTags
		RELLIB	CreateGadgetA,_GadToolsBase
		move.l	(sp)+,a4
		tst.l	d0
		beq	.BillWinError
		move.l	d0,a3
		move.l	d7,a0
		bsr	LocalizeGadKey
		move.l	d1,gg_UserData(a3)
		move.l	d3,d0
		lsl.l	#2,d0
		move.l	a4,-(sp)
		move.l	ZeroBase(pc),a4
		lea.l	BillGads(a4),a0
		move.l	(sp)+,a4
		move.l	a3,0(a0,d0.w)
.BillWinTL	tst.l	(a4)
		beq.s	.BillWinDN
		addq.w	#8,a4
		bra.s	.BillWinTL
.BillWinDN	addq.w	#4,a4
		addq.w	#1,d3
		cmp.w	#BillWin_CNT,d3
		bmi	.BillWinGL
		movem.l (sp)+,d2-d3/a4
		move.l	BillGadget(a4),BillWinWG+4

		LOCLEA	DefBillTitle,a0
		lea	BillTitle(a4),a1
		move.l	a1,BillWinWG+12
		bsr	StrCpy
		lea	BillHotKey(a4),a0
		lea	BillTitle(a4),a1
		bsr	StrCat
		LOCLEA	WindowScrTitle,BillWinWG+20
		move.l	-4(a5),BillWinSC+4		; Open window
		ext.l	d2
		ext.l	d3
		move.l	d2,BillWinL+4
		move.l	d3,BillWinT+4
		move.l	-4(a5),a0
		move.w	BillWinWidth(a4),d0
		RELLIB	ComputeX,_KCXBase
		add.w	OffX(a5),d0
		moveq	#0,d1
		move.b	sc_WBorRight(a0),d1
		add.w	d1,d0
		move.l	d0,BillWinW+4
		move.w	d0,BillZoomSize+4(a4)
		move.w	BillWinHeight(a4),d0
		DOLIB	ComputeY
		add.w	OffY(a5),d0
		moveq	#0,d1
		move.b	sc_WBorBottom(a0),d1
		add.w	d1,d0
		move.l	d0,BillWinH+4
		suba.l	a0,a0
		tst.b	ActiveBill(a4)
		beq.s	.NoActiveBill
		or.l	#WFLG_ACTIVATE,BillWinF+4
		bra.s	.OpenBill
.NoActiveBill	and.l	#~WFLG_ACTIVATE,BillWinF+4
.OpenBill	lea.l	BillWinWindowTags,a1
		RELLIB	OpenWindowTagList,_IntuitionBase
		move.l	d0,BillHandle(a4)
		beq.s	.BillWinError
		move.l	d0,a0
		moveq	#0,d0
		moveq	#1,d1
		move.l	wd_UserPort(a0),a0
		move.b	MP_SIGBIT(a0),d0
		lsl.l	d0,d1
		move.l	d1,BillSigFlag(a4)

		move.l	BillHandle(a4),a0
		suba.l	a1,a1
		RELLIB	GT_RefreshWindow,_GadToolsBase

		bsr.s	SetBillGads

		tst.b	Zipped(a4)
		beq.s	.NoBillClose
		move.l	BillHandle(a4),a0
		RELLIB	ZipWindow,_IntuitionBase
		bsr	RemakeCosts
		bra.s	.NoBillClose

.BillWinError	bsr.s	CloseBillWin
		move.l	#MEMORY_ERR,ErrorBack(a4)
		bsr	DisplayError
.NoBillClose	bsr	UnlockScreen
		unlk	a5
		rts


**********************************************************************
*                       Set bill window gadget                       *
**********************************************************************
SetBillGads	move.l	#GD_LogCall,d1			; Set LogCalls gadget
		lea	BillGads(a4),a0
		move.l	BillHandle(a4),a1
		moveq	#0,d0
		move.b	LogCalls(a4),d0
		RELLIB	SetCheckBox,_KCXBase

		move.l	#GD_BillTimeMX,d0
		move.l	TimeZone(a4),d1
		lea	BillGads(a4),a0
		move.l	BillHandle(a4),a1
		DOLIB	SetMXGad

		move.l	#GD_BillCheap,d0
		moveq	#0,d1
		move.b	CheapMode(a4),d1
		lea	BillGads(a4),a0
		move.l	BillHandle(a4),a1
		DOLIB	SetMXGad
		rts


**********************************************************************
*                       Close the bill window                        *
**********************************************************************
CloseBillWin	lea	BillHandle(a4),a3
		bra	CloseNormWindow





**********************************************************************
*                        Select a new screen                         *
**********************************************************************
OpenSelScrWin	tst.l	SelScrHandle(a4)
		beq.s	.GoOn
		move.l	SelScrHandle(a4),a0
		RELLIB	WindowToFront,_IntuitionBase
		rts
.GoOn		bsr	CollectScrs
		link	a5,#-(4+4+2+2+2+2+gng_SIZEOF)
		lea	SelScrTxt(a4),a0
		move.l	a0,FontPtr(a5)

		moveq	#0,d0
		RELLIB	LockIBase,_IntuitionBase
		move.l	d0,a0
		move.l	_IntuitionBase(a4),a2
		move.l	ib_ActiveScreen(a2),a2
		move.l	a2,LastSelectedScr(a4)
		DOLIB	UnlockIBase
		move.w	sc_Flags(a2),d0
		and.w	#SCREENTYPE,d0
		cmp.w	#PUBLICSCREEN,d0
		bne.s	.UseWB
		DOLIB	LockPubScreenList
		tst.l	d0
		beq.s	.UseWB
		move.l	d0,a3
		move.l	LH_HEAD(a3),a3
.GetPubScr	cmp.l	psn_Screen(a3),a2
		beq.s	.GotIt
		SUCC	a3,a3
		bra.s	.GetPubScr
.GotIt		link	a5,#-(MAXPUBSCREENNAME+1)
		move.l	LN_NAME(a3),a0
		lea	-(MAXPUBSCREENNAME+1)(a5),a1
		bsr	StrCpy
		DOLIB	UnlockPubScreenList
		lea	-(MAXPUBSCREENNAME+1)(a5),a0
		DOLIB	LockPubScreen
		unlk	a5
		move.l	d0,-4(a5)
		bra.s	.GotMyScr
.UseWB		bsr	LockScreen
.GotMyScr	tst.l	d0
		beq	.SelScrWinError
		move.l	d0,a0
		RELLIB	ScreenToFront,_IntuitionBase
		moveq	#0,d2
		moveq	#0,d3
		RELLIB	ComputeFont,_KCXBase
		move.l	-4(a5),a0
		sub.l	a1,a1
		RELLIB	GetVisualInfoA,_GadToolsBase
		move.l	d0,SelScrVisInfo(a4)
		beq	.SelScrWinError

		move.l	-4(a5),a0
		moveq	#0,d3
		moveq	#0,d2
		move.w	SelScrWinWidth(a4),d2
		move.w	SelScrWinHeight(a4),d3
		RELLIB	ComputeFont,_KCXBase
		move.l	-4(a5),a0
		move.l	d2,d0
		DOLIB	ComputeX
		move.l	d0,d4
		move.w	SelScrWinLeft(a4),d2
		move.l	-4(a5),a3
		move.w	sc_LeftEdge(a3),d5
		neg.w	d5
		add.w	d5,d2
		add.w	d2,d0
		add.w	OffX(a5),d0
		moveq	#0,d1
		move.b	sc_WBorRight(a0),d1
		add.w	d1,d0
		cmp.w	sc_Width(a0),d0
		bls.s	.WOk
		move.w	sc_Width(a0),d0
		sub.w	d4,d0
		move.w	d0,d2
.WOk		move.l	d3,d0
		DOLIB	ComputeY
		move.l	d0,d4
		move.w	SelScrWinTop(a4),d3
		move.l	-4(a5),a3
		move.w	sc_TopEdge(a3),d5
		neg.w	d5
		add.w	d5,d3
		add.w	d3,d0
		add.w	OffY(a5),d0
		moveq	#0,d1
		move.b	sc_WBorBottom(a0),d1
		add.w	d1,d0
		cmp.w	sc_Height(a0),d0
		bls.s	.HOk
		move.w	sc_Height(a0),d0
		sub.w	d4,d0
		move.w	d0,d3
.HOk		lea.l	SelScrGadget(a4),a0
		RELLIB	CreateContext,_GadToolsBase
		move.l	d0,a3
		beq	.SelScrWinError

		movem.l d2-d3/a4,-(sp)
		moveq	#0,d3
		lea.l	SelScrWinGTags,a4
.SelScrWinGL	lea.l	SelScrWinNGads,a0
		move.l	d3,d0
		mulu	#gng_SIZEOF,d0
		add.l	d0,a0
		lea.l	-(4+4+2+2+2+2+gng_SIZEOF)(a5),a1
		moveq	#gng_SIZEOF,d0
		CALLSYS	CopyMem
		lea.l	-(4+4+2+2+2+2+gng_SIZEOF)(a5),a0
		move.l	a4,-(sp)
		move.l	ZeroBase(pc),a4
		move.l	gng_GadgetText(a0),-(sp)
		bsr	GetLocStr
		move.l	(sp)+,d7
		move.l	d7,gng_GadgetText(a0)
		move.l	SelScrVisInfo(a4),gng_VisualInfo(a0)
		move.l	FontPtr(a5),gng_TextAttr(a0)
		move.w	gng_LeftEdge(a0),d0
		RELLIB	ComputeX,_KCXBase
		move.l	(sp)+,a4
		add.w	OffX(a5),d0
		move.w	d0,gng_LeftEdge(a0)
		move.w	gng_TopEdge(a0),d0
		DOLIB	ComputeY
		add.w	OffY(a5),d0
		move.w	d0,gng_TopEdge(a0)
		move.w	gng_Width(a0),d0
		DOLIB	ComputeX
		move.w	d0,gng_Width(a0)
		move.w	gng_Height(a0),d0
		DOLIB	ComputeY
		move.w	d0,gng_Height(a0)
		lea.l	SelScrWinGTypes,a0
		moveq	#0,d0
		move.l	d3,d1
		lsl.l	#1,d1
		move.w	0(a0,d1.w),d0
		move.l	a3,a0
		lea.l	-(4+4+2+2+2+2+gng_SIZEOF)(a5),a1
		move.l	a4,a2
		move.l	a4,-(sp)
		move.l	ZeroBase(pc),a4
		RELLIB	CreateGadgetA,_GadToolsBase
		move.l	(sp)+,a4
		tst.l	d0
		beq	.SelScrWinError
		move.l	d0,a3
		move.l	d7,a0
		bsr	LocalizeGadKey
		move.l	d1,gg_UserData(a3)
		move.l	d3,d0
		lsl.l	#2,d0
		move.l	a4,-(sp)
		move.l	ZeroBase(pc),a4
		lea.l	SelScrGads(a4),a0
		move.l	(sp)+,a4
		move.l	a3,0(a0,d0.w)
.SelScrWinTL	tst.l	(a4)
		beq.s	.SelScrWinDN
		addq.w	#8,a4
		bra.s	.SelScrWinTL
.SelScrWinDN	addq.w	#4,a4
		addq.w	#1,d3
		cmp.w	#SelectScrWin_CNT,d3
		bmi	.SelScrWinGL
		movem.l (sp)+,d2-d3/a4
		move.l	SelScrGadget(a4),SelScrWinWG+4

		LOCLEA	DefSelScrTitle,a0
		lea	SelScrTitle(a4),a1
		move.l	a1,SelScrWinWG+12
		bsr	StrCpy
		lea	SelScrHotKey(a4),a0
		lea	SelScrTitle(a4),a1
		bsr	StrCat
		LOCLEA	WindowScrTitle,SelScrWinWG+20
		move.l	-4(a5),SelScrWinSC+4		; Open window
		ext.l	d2
		ext.l	d3
		move.l	d2,SelScrWinL+4
		move.l	d3,SelScrWinT+4
		move.l	-4(a5),a0
		move.w	SelScrWinWidth(a4),d0
		RELLIB	ComputeX,_KCXBase
		add.w	OffX(a5),d0
		moveq	#0,d1
		move.b	sc_WBorRight(a0),d1
		add.w	d1,d0
		move.l	d0,SelScrWinW+4
		move.w	SelScrWinHeight(a4),d0
		DOLIB	ComputeY
		add.w	OffY(a5),d0
		moveq	#0,d1
		move.b	sc_WBorBottom(a0),d1
		add.w	d1,d0
		move.l	d0,SelScrWinH+4
		suba.l	a0,a0
		lea.l	SelScrWinWindowTags,a1
		RELLIB	OpenWindowTagList,_IntuitionBase
		move.l	d0,SelScrHandle(a4)
		beq.s	.SelScrWinError
		move.l	d0,a0
		moveq	#0,d0
		moveq	#1,d1
		move.l	wd_UserPort(a0),a0
		move.b	MP_SIGBIT(a0),d0
		lsl.l	d0,d1
		move.l	d1,SelScrWinSig(a4)

		move.l	SelScrHandle(a4),a0
		suba.l	a1,a1
		RELLIB	GT_RefreshWindow,_GadToolsBase

		bsr	UnlockScreen
		unlk	a5
		move.l	SelScrHandle(a4),a0
		RELLIB	ActivateWindow,_IntuitionBase
		rts

.SelScrWinError	bsr.s	CloseSelScrWin
		move.l	#MEMORY_ERR,ErrorBack(a4)
		bsr	DisplayError
		bsr	UnlockScreen
		unlk	a5
		rts

CloseSelScrWin	lea	SelScrList(a4),a0
		tst.l	(a0)
		beq.s	.End
		RELLIB	FreeWindows,_KCXBase
.End		lea	SelScrHandle(a4),a3
		bsr.s	CloseNormWindow
		clr.l	SelScrWinSig(a4)
		rts
PubScrName	dc.b	"(PUB) ",0
		EVEN


**********************************************************************
*            Close down a window with a non-shared IDCMP             *
**********************************************************************
CloseNormWindow	tst.l	(a3)
		beq.s	.NoWindow
		move.l	(a3),a0
		RELLIB	CloseWindow,_IntuitionBase	; Close Window
.NoWindow	tst.l	4(a3)
		beq.s	.NoGadgets
		move.l	4(a3),a0			; Free GadTools-Gadgets
		RELLIB	FreeGadgets,_GadToolsBase
.NoGadgets	tst.l	8(a3)				; Free Visual-Info
		beq.s	.NoVisInfo
		move.l	8(a3),a0
		RELLIB	FreeVisualInfo,_GadToolsBase
.NoVisInfo	clr.l	(a3)				; Clear flags for later
		clr.l	4(a3)				; identification
		clr.l	8(a3)
		rts




**********************************************************************
*                   Collect all available screens                    *
**********************************************************************
CollectScrs	link	a5,#-8
		moveq	#0,d0
		RELLIB	LockIBase,_IntuitionBase
		move.l	d0,-4(a5)
		DOLIB	LockPubScreenList
		move.l	d0,-8(a5)

		clr.w	NumScreens(a4)			; Create Screen list
		clr.w	SelectedScreen(a4)
		lea	SelScrList(a4),a0
		NEWLIST	a0

		move.l	-8(a5),a2			; Collect all PubScreens
		move.l	LH_HEAD(a2),a2
		TSTLIST	a2
		beq.s	.NoMorePubs
.GetPubScrs	tst.l	(a2)
		beq.s	.NoMorePubs
		move.l	#selscr_SIZEOF,d0
		move.l	#MEMF_CLEAR,d1
		CALLSYS	AllocVec
		tst.l	d0
		beq	.ErrorScrs
		lea	SelScrList(a4),a0
		move.l	d0,a1
		move.l	d0,a3
		ADDTAIL
		move.l	psn_Screen(a2),selscr_Address(a3)
		lea	PubScrName(pc),a0
		lea	selscr_Name(a3),a1
		move.l	a1,LN_NAME(a3)
		bsr	StrCpy
		move.l	LN_NAME(a2),a0
		lea	selscr_Name(a3),a1
		bsr	StrCat
		addq.w	#1,NumScreens(a4)
		SUCC	a2,a2
		bra.s	.GetPubScrs

.NoMorePubs	move.l	_IntuitionBase(a4),a2
		move.l	ib_FirstScreen(a2),a2
.GetNormScrs	move.l	a2,d0
		beq.s	.ErrorScrs
		move.w	sc_Flags(a2),d0
		and.w	#SCREENTYPE,d0
		cmp.w	#CUSTOMSCREEN,d0
		bne.s	.IsPubScr
		move.l	#selscr_SIZEOF,d0
		move.l	#MEMF_CLEAR,d1
		CALLSYS	AllocVec
		tst.l	d0
		beq.s	.ErrorScrs
		lea	SelScrList(a4),a0
		move.l	d0,a1
		move.l	d0,a3
		ADDTAIL
		move.l	a2,selscr_Address(a3)
		move.l	sc_Title(a2),d0
		bne.s	.GotAName
		LOCLEA	NoScrName,d0
.GotAName	move.l	d0,a0
		lea	selscr_Name(a3),a1
		move.l	a1,LN_NAME(a3)
		bsr	StrCpy
		addq.w	#1,NumScreens(a4)
.IsPubScr	move.l	sc_NextScreen(a2),a2
		bra.s	.GetNormScrs

.ErrorScrs	RELLIB	UnlockPubScreenList,_IntuitionBase
		move.l	-4(a5),a0
		DOLIB	UnlockIBase
		lea	DefPubScreenName(pc),a0
		DOLIB	GetDefaultPubScreen
		lea	SelScrList(a4),a0
		move.l	a0,SelScrWinGTags+4
		moveq	#0,d0
		move.w	NumScreens(a4),d0
		move.l	d0,ScrNumTag+4
		unlk	a5
		rts



**********************************************************************
*                    Close all remaining windows                     *
**********************************************************************
CloseWins	bsr	ClearTitleBar			; Clear title bar
		bsr	CloseClockWin			; Close clock window
		bsr	CloseFormWin
		bsr	CloseSelScrWin
		rts



**********************************************************************
*           Store new position of window within NewWindow            *
**********************************************************************
StoreWinPos	move.w	wd_LeftEdge(a0),0(a1)
		move.w	wd_TopEdge(a0),2(a1)
		rts



**********************************************************************
*                        Compare two strings                         *
**********************************************************************
StrCmp		movem.l	a0/a1,-(sp)			; Store pointers
.Loop		moveq	#0,d0
		move.b	(a1)+,d0
		bsr	GetUpperCase
		move.b	d0,d1
		moveq	#0,d0
		move.b	(a0)+,d0			; get chars
		bsr	GetUpperCase
		tst.b	d0				; Source string ended ?
		beq.s	.EndSrc				; yep -> final check
		cmp.b	#$0a,d0				; a newline ?
		beq.s	.EndSrc				; yep -> final check
		cmp.b	d0,d1				; equal ?
		bne.s	.Invalid
		dbf	d7,.Loop
		movem.l	(sp)+,a0/a1
		moveq	#0,d0
		rts
.Invalid	moveq	#1,d0				; Error flag
		movem.l	(sp)+,a0/a1
		rts
.EndSrc		cmp.b	d0,d1				; last chars equal ?
		bne.s	.Invalid			; Nope -> error
		moveq	#0,d0				; a ok
		movem.l	(sp)+,a0/a1
		rts


**********************************************************************
*               Compare two strings (case sensitive)                 *
**********************************************************************
StrCmp2		moveq	#0,d0
		moveq	#0,d1
		movem.l	a0/a1,-(sp)			; Store pointers
.Loop		move.b	(a0)+,d0			; get chars
		move.b	(a1)+,d1
		tst.b	d0				; Source string ended ?
		beq.s	.EndSrc				; yep -> final check
		cmp.b	#$0a,d0				; a newline ?
		beq.s	.EndSrc				; yep -> final check
		cmp.b	d0,d1				; equal ?
		bne.s	.Invalid
		dbf	d7,.Loop
		movem.l	(sp)+,a0/a1
		moveq	#0,d0
		rts
.Invalid	moveq	#1,d0				; Error flag
		movem.l	(sp)+,a0/a1
		rts
.EndSrc		cmp.b	d0,d1				; last chars equal ?
		bne.s	.Invalid			; Nope -> error
		moveq	#0,d0				; a ok
		movem.l	(sp)+,a0/a1
		rts

**********************************************************************
*                           Copy a string                            *
**********************************************************************
StrCpy		move.b	(a0)+,d0
		move.b	d0,(a1)+
		tst.b	d0
		beq.s	.End
		bra.s	StrCpy
.End		rts



**********************************************************************
*                          Concat 2 string                           *
**********************************************************************
StrCat		tst.b	(a1)+
		beq.s	.Loop
		bra	StrCat
.Loop		subq.l	#1,a1
		bra	StrCpy



**********************************************************************
*            Copy a string up to x chars or end of string            *
**********************************************************************
StrNCpy		move.b	(a0)+,d0
		move.b	d0,(a1)+
		tst.b	d0
		beq.s	.End
		dbf	d7,StrNCpy
.End		rts



**********************************************************************
*                      Calc length of a string                       *
**********************************************************************
StrLen		move.l	a0,-(sp)
		moveq	#0,d0
.Loop		tst.b	(a0)+
		beq.s	.End
		addq.w	#1,d0
		bne.s	.Loop
.End		move.l	(sp)+,a0
		rts



**********************************************************************
*           This part removes all waiting msgs for a port            *
**********************************************************************
KillMessages	CARGS	#0,.Wnd.L,.Port.L,.Base.L
		movem.l	a0/a1/a6,-(sp)			; Store pointers
.Loop		move.l	.Port(sp),a0			; Get pointer to port
		CALLSYS	GetMsg
		tst.l	d0
		beq.s	NoMoreMsgs
		move.l	d0,a1				; Reply msg
		DOLIB	ReplyMsg
		bra	.Loop
NoMoreMsgs	movem.l	(sp)+,a0/a1/a6			; Reget pointers
		rts




**********************************************************************
*                 String/Integer Gadget pens setten                  *
**********************************************************************
SetStrIntGads	movem.l	RegsAll,-(sp)

.Loop		move.l	(a0),d0
		beq.s	.End
		move.l	(a0)+,a1
		move.w	gg_GadgetType(a1),d0
		and.w	#GTYP_STRGADGET,d0
		beq.s	.NoStrGad
		move.l	gg_SpecialInfo(a1),a1
		move.l	si_Extension(a1),a1
		move.w	#$0102,sex_ActivePens(a1)
.NoStrGad	bra.s	.Loop
.End		movem.l	(sp)+,RegsAll
		rts


**********************************************************************
*                     Lock the Workbench-Screen                      *
**********************************************************************
LockScreen	lea	WBScrName(pc),a0
		RELLIB	LockPubScreen,_IntuitionBase
		move.l	d0,-4(a5)
		rts
WBScrName	dc.b	"Workbench",0
		EVEN


**********************************************************************
*                      Unlock the locked screen                      *
**********************************************************************
UnlockScreen	tst.l	-4(a5)
		beq.s	.End
		sub.l	a0,a0
		move.l	-4(a5),a1
		RELLIB	UnlockPubScreen,_IntuitionBase
.End		rts



**********************************************************************
*                         Lock our semaphore                         *
**********************************************************************
GetSemaphore	movem.l	a0/a6,-(sp)
		move.l	ZeroBase(pc),a0
		lea	MySemaphore(a0),a0
		CALLSYS	ObtainSemaphore
		movem.l	(sp)+,a0/a6
		rts



**********************************************************************
*                       Unlock semaphore again                       *
**********************************************************************
FreeSemaphore	movem.l	a0/a6,-(sp)
		move.l	ZeroBase(pc),a0
		lea	MySemaphore(a0),a0
		CALLSYS	ReleaseSemaphore
		movem.l	(sp)+,a0/a6
		rts



**********************************************************************
*                    Get a string from a locale                      *
**********************************************************************
GetLocStr	movem.l	d0-d1/a0-a1/a6,-(sp)
		move.l	MyCatalog,d1
		beq.s	.End
		move.l	24(sp),d0
		beq.s	.End
		move.l	d0,a1
		moveq	#0,d0
		move.l	a1,d1
		lea	LOCALE_END(pc),a0
		cmp.l	a0,d1
		bgt.s	.End
		lea	LOCALE_START(pc),a0
		cmp.l	a0,d1
		blt.s	.End
		sub.l	a0,d1
		beq.s	.GetIt
.Loop		tst.b	(a0)+
		bne.s	.Ok
		addq.l	#1,d0
.Ok		subq.l	#1,d1
		bne.s	.Loop

.GetIt		move.l	MyCatalog,a0
		RELLIB	GetCatalogStr,_LocaleBase
		move.l	d0,24(sp)
.End		movem.l	(sp)+,d0-d1/a0-a1/a6
		rts


**********************************************************************
*                    Localize a gadget`s shortcut                    *
*  A0 = Pointer to text                                              *
*  RETURN :                                                          *
*  D1 = Shortcut                                                     *
**********************************************************************
LocalizeGadKey	move.l	d0,-(sp)
		moveq	#0,d1
		move.l	a0,d2
		beq.s	.End
.GetShort	move.b	(a0)+,d2
		beq.s	.End
		cmp.b	#'_',d2
		bne.s	.GetShort
		moveq	#0,d0
		move.b	(a0),d0
		bsr	GetUpperCase
		move.l	d0,d1
.End		move.l	(sp)+,d0
		rts



**********************************************************************
*         Localizes tags of a ListView, Cycle and MX gadget          *
* D0 = Type of Gadget                                                *
* A2 = Pointer to TagList                                            *
**********************************************************************
LocalizeGadTags	movem.l	RegsAll,-(sp)
		cmp.w	#MX_KIND,d0
		bne.s	.NoCycle
		move.l	#GTMX_Labels,d0
		move.l	a2,a0
		RELLIB	FindTagItem,_UtilityBase
		tst.l	d0
		beq.s	.End
		move.l	d0,a2
		move.l	ti_Data(a2),a2
.PatchCycles	move.l	(a2),d0
		beq.s	.End
		move.l	d0,-(sp)
		bsr	GetLocStr
		move.l	(sp)+,(a2)
		addq.l	#4,a2
		bra.s	.PatchCycles
.NoCycle
.End		movem.l	(sp)+,RegsAll
		rts



**********************************************************************
*                   Open clock window. Width in D6                   *
**********************************************************************
OpenClockWin	add.w	#DEPTHWIDTH*2+8,d6		; approximate width of close/depth gadgets
		ext.l	d6
		move.l	d6,ClockWidth			; Store for compare

		tst.l	ClockHandle(a4)			; Window already open ?
		bne.s	ClockWinOpen

		bsr	ClearTitleBar			; Clear titlebar first

		link	a5,#-4
		bsr	LockScreen
		tst.l	d0
		beq	ClockError

		sub.l	a0,a0				; Open clock window
		move.l	-4(a5),a1
		moveq	#0,d1
		move.b	sc_WBorTop(a1),d1
		moveq	#0,d0
		moveq	#0,d1
		move.w	ClockWinLeft(a4),d0
		move.w	ClockWinTop(a4),d1
		move.l	d0,ClockLeft
		move.l	d1,ClockTop
		lea	ClockWindowTags,a1
		RELLIB	OpenWindowTagList,_IntuitionBase
		move.l	d0,ClockHandle(a4)
		bne.s	GotClockWin
		clr.b	ClockWin(a4)			; On failure close
		bra.s	ClockError
GotClockWin	move.l	ClockHandle(a4),a0		; Get sig flag
		moveq	#0,d0
		move.l	wd_UserPort(a0),a0
		move.b	MP_SIGBIT(a0),d0
		moveq	#1,d1
		lsl.l	d0,d1
		move.l	d1,ClockSigFlag(a4)
		bsr	UnlockScreen
		unlk	a5
ClockWinOpen	move.l	ClockHandle(a4),a0		; Current width not equal
		moveq	#0,d0				; to width needed
		moveq	#0,d1
		moveq	#0,d2
		moveq	#0,d3
		move.w	wd_LeftEdge(a0),d0
		move.w	wd_TopEdge(a0),d1
		move.l	ClockWidth,d2
		cmp.w	wd_Width(a0),d2
		beq.s	CWidthOk
		moveq	#0,d3
		move.b	wd_BorderTop(a0),d3
		RELLIB	ChangeWindowBox,_IntuitionBase	; so readjust width
		bsr	ClockRemake			; Wait until window has been remade
CWidthOk	moveq	#1,d0
		rts
ClockError	bsr	UnlockScreen
		moveq	#0,d0
		unlk	a5
		rts



**********************************************************************
*                Close clockwindow and remove pointer                *
**********************************************************************
GD_DisplayWin                          EQU    1
CloseClockWin	tst.l	ClockHandle(a4)
		beq.s	.NoCWOpen
		move.l	ClockHandle(a4),a0		; Store new coords in structure
		RELLIB	CloseWindow,_IntuitionBase	; and now....CLOSE IT
		clr.l	ClockHandle(a4)
		clr.l	ClockSigFlag(a4)
.NoCWOpen	rts




**********************************************************************
*          This is the routine to handle RAW-Event actions           *
**********************************************************************
RawEventAction	movem.l	RegsAll,-(sp)

		move.l	a1,a4
		move.l	a0,IEvent(a4)

EventLoop	move.l	IEvent(a4),a0
		moveq	#0,d0
		move.b	ie_Class(a0),d0
		move.w	d0,IEClass(a4)			; Store class

		tst.b	Disabled(a4)
		bne.s	.NoJoy
		opt	NOCHKIMM
		move.w	$dff00c,d0			; I believe, a dirty code to check the JoyStick :)
		opt	CHKIMM
		btst	#1,d0
		bne.s	.Joy
		btst	#9,d0
		bne.s	.Joy
		move.w	d0,d1
		lsr.w	#1,d1
		eor.w	d0,d1
		btst	#0,d1
		bne.s	.Joy
		btst	#8,d1
		beq.s	.NoJoy
.Joy		tst.b	ScrTimeOut(a4)
		beq.s	.NoScr
		move.l	IEvent(a4),a0
		move.l	#REQ_ONSCREEN,d0
		bsr	SendEvent
.NoScr		SF	ScrTimeOut(a4)
		move.l	IEvent(a4),a0
		bsr	ResetTimer

.NoJoy		tst.b	Disabled(a4)
		bne.s	.NoLeftyAll
		tst.b	LeftyMouse(a4)			; We have to swap Buttonqualifiers
		beq.s	.NoLeftyAll			; for every message if needed
		move.l	IEvent(a4),a0
		move.w	ie_Qualifier(a0),d0		; Change qualifiers for all events
		move.w	d0,d1
		btst	#IEQUALIFIERB_RBUTTON,d1
		beq.s	.NoRight
		bset	#IEQUALIFIERB_LEFTBUTTON,d0
		beq.s	.CheckLeft
.NoRight	bclr	#IEQUALIFIERB_LEFTBUTTON,d0
.CheckLeft	btst	#IEQUALIFIERB_LEFTBUTTON,d1
		beq.s	.NoLeft
		bset	#IEQUALIFIERB_RBUTTON,d0
		beq.s	.SaveQual
.NoLeft		bclr	#IEQUALIFIERB_RBUTTON,d0
.SaveQual	move.w	d0,ie_Qualifier(a0)

.NoLeftyAll	cmp.w	#IECLASS_RAWKEY,IEClass(a4)	; Work on RawKeys and RawMous
		beq.s	.DoSpec				; as well
		cmp.w	#IECLASS_RAWMOUSE,IEClass(a4)
		bne	NoSpec

.DoSpec		move.l	IEvent(a4),a0
		bsr	ResetTimer

		tst.b	Disabled(a4)			; Swap for LeftyMouse
		bne.s	.CheckCycle
		tst.b	LeftyMouse(a4)
		beq.s	.CheckCycle
		move.l	IEvent(a4),a0
		move.w	ie_Code(a0),d0
		move.w	d0,d1
		and.w	#~IECODE_UP_PREFIX,d0
		cmp.w	#IECODE_LBUTTON,d0
		beq.s	.SwapThem
		cmp.w	#IECODE_RBUTTON,d0
		bne.s	.CheckCycle
.SwapThem	eor.w	#1,d1
		move.w	d1,ie_Code(a0)

.CheckCycle	cmp.w	#IECLASS_RAWMOUSE,IEClass(a4)	; Mouse movement ?
		bne	.NoHold

		tst.b	Disabled(a4)
		bne.s	.NoYAcc
		tst.b	MouseAccel(a4)			; Accelerate mouse ?
		beq.s	.NoYAcc
		move.l	IEvent(a4),a3
		move.w	ie_X(a3),d4			; Get current X Position of mouse
		ext.l	d4
		moveq	#1,d5				; Get direction to be moved
		tst.l	d4
		bge.s	.XBig
		neg.l	d4				; Make position positive
		moveq	#-1,d5				; And invert direction
.XBig		cmp.l	ThresHold(a4),d4		; Check for threshold
		ble.s	.NoXAcc
		move.l	d4,d0
		sub.l	ThresHold(a4),d0		; Sub threshold
		move.l	Accelerate(a4),d1		; Accelerate
		RELLIB	SMult32,_UtilityBase
		move.l	d5,d1				; Now for the direction
		DOLIB	SMult32
		move.w	d0,ie_X(a3)

.NoXAcc		move.w	ie_Y(a3),d4			; Do the same for Y Position
		ext.l	d4
		moveq	#1,d5
		tst.l	d4
		bge.s	.YBig
		neg.l	d4
		moveq	#-1,d5
.YBig		cmp.l	ThresHold(a4),d4		; Check for threshold
		ble.s	.NoYAcc
		move.l	d4,d0
		sub.l	ThresHold(a4),d0		; Sub threshold
		move.l	Accelerate(a4),d1		; Acceleration
		RELLIB	SMult32,_UtilityBase
		move.l	d5,d1
		DOLIB	SMult32
		move.w	d0,ie_Y(a3)

.NoYAcc		tst.b	Disabled(a4)
		bne.s	.NoHold
		tst.b	HoldMouse(a4)			; Ready to hold mouse ???
		beq.s	.NoHold

		move.l	IEvent(a4),a2			; Check for "Hold" keys
		move.w	ie_Qualifier(a2),d1
		move.w	d1,d0
		and.w	HoldXQual(a4),d0		; Hold qualifier pressed ?
		cmp.w	HoldXQual(a4),d0
		bne.s	.NoVertHold
		clr.w	ie_X(a2)			; Clear delta-movement
.NoVertHold	and.w	HoldYQual(a4),d1
		cmp.w	HoldYQual(a4),d1
		bne.s	.NoHold
		clr.w	ie_Y(a2)

.NoHold		tst.b	GfxGetting(a4)			; Printer process getting new area ?
		beq.s	.NoGfxGetting
		cmp.w	#IECLASS_RAWMOUSE,IEClass(a4)
		bne.s	.NoGfxGetting
		move.l	IEvent(a4),a0
		move.w	ie_Code(a0),d0
		move.w	ie_Qualifier(a0),d1
		move.w	d1,d3
		and.w	#~(IEQUALIFIER_REPEAT|IEQUALIFIER_NUMERICPAD|IEQUALIFIER_INTERRUPT|IEQUALIFIER_RELATIVEMOUSE|IEQUALIFIER_MULTIBROADCAST),d1
		bsr	FilterDouble
		cmp.w	#IECODE_LBUTTON,d0
		beq.s	.LButton
		cmp.w	#IECODE_RBUTTON,d0
		beq.s	.RButton
		tst.w	d0
		move.l	#SIGMOUSE,d0
		bra.s	.SendSig2
.LButton	move.l	#SIGLBUTTON,d0
		bra.s	.SendSig
.RButton	move.l	#SIGRBUTTON,d0
.SendSig	clr.b	ie_Class(a0)
.SendSig2	move.l	PrintProc(a4),a1
		CALLSYS	Signal
		bra	NoTimerEvent

.NoGfxGetting	tst.b	ImDragging(a4)			; Full dragging mode
		beq.s	.NoDragging
		cmp.w	#IECLASS_RAWMOUSE,IEClass(a4)
		bne.s	.NoDragging
		move.l	IEvent(a4),a0
		move.w	ie_Code(a0),d0
		move.w	ie_Qualifier(a0),d1
		and.w	#~(IEQUALIFIER_REPEAT|IEQUALIFIER_NUMERICPAD|IEQUALIFIER_INTERRUPT|IEQUALIFIER_RELATIVEMOUSE|IEQUALIFIER_MULTIBROADCAST),d1
		bsr	FilterDouble
		btst	#10,$dff016
		beq.s	.EndDrag
		cmp.w	#IECODE_LBUTTON|IECODE_UP_PREFIX,d0
		beq.s	.EndDrag
		move.l	#SIGMOUSE,d0
		bra.s	.SigDrag
.EndDrag	move.l	#SIGLBUTTON,d0
.SigDrag	move.l	BackTask(a4),a1
		CALLSYS	Signal
		bra	NoTimerEvent

.NoDragging	tst.b	GetSource(a4)			; Printer process looking for new source ?
		beq.s	.NoGetSource
		cmp.w	#IECLASS_RAWMOUSE,IEClass(a4)
		bne.s	.NoGetSource
		move.l	IEvent(a4),a0
		move.w	ie_Code(a0),d0
		move.w	ie_Qualifier(a0),d1
		and.w	#~(IEQUALIFIER_REPEAT|IEQUALIFIER_NUMERICPAD|IEQUALIFIER_INTERRUPT|IEQUALIFIER_RELATIVEMOUSE|IEQUALIFIER_MULTIBROADCAST),d1
		bsr	FilterDouble
		cmp.w	#IECODE_LBUTTON,d0
		bne.s	.NoNewWin
		clr.b	ie_Class(a0)
		move.l	#SIGLBUTTON,d0
		move.l	PrintProc(a4),a1
		CALLSYS	Signal
		bra	NoTimerEvent

.NoGetSource	tst.b	GetNewWindow(a4)		; Getting new window for lock ?
		beq.s	.NoNewWin
		cmp.w	#IECLASS_RAWMOUSE,IEClass(a4)
		bne.s	.NoNewWin
		move.l	IEvent(a4),a0
		move.w	ie_Code(a0),d0
		move.w	ie_Qualifier(a0),d1
		move.w	d1,d3
		and.w	#~(IEQUALIFIER_REPEAT|IEQUALIFIER_NUMERICPAD|IEQUALIFIER_INTERRUPT|IEQUALIFIER_RELATIVEMOUSE|IEQUALIFIER_MULTIBROADCAST),d1
		bsr	FilterDouble
		cmp.w	#IECODE_LBUTTON,d0
		bne.s	.NoNewWin
		clr.b	ie_Class(a0)
		move.l	#SIGLBUTTON,d0
		move.l	PrefsTask(a4),a1
		CALLSYS	Signal
		bra	NoTimerEvent

.NoNewWin	tst.b	Disabled(a4)
		bne	.NoCycle
		tst.b	CycleWin(a4)			; Are we to cycle windows ?
		beq	.NoCycle
		move.l	IEvent(a4),a0
		move.w	ie_Code(a0),d0
		move.w	d0,d1
		and.w	#IECODE_UP_PREFIX,d1
		bne	.NoCycle
		move.w	ie_Qualifier(a0),d1
		and.w	#~(IEQUALIFIER_REPEAT|IEQUALIFIER_NUMERICPAD|IEQUALIFIER_INTERRUPT|IEQUALIFIER_RELATIVEMOUSE|IEQUALIFIER_MULTIBROADCAST),d1
		bsr	FilterDouble
		move.w	d1,d3
		cmp.w	BackCode(a4),d0			; Check for Back key
		bne.s	.NotBack
		cmp.w	BackQual(a4),d1
		bne.s	.NotBack
		cmp.w	#1,BackClicks(a4)
		beq.s	.IsFront
		cmp.w	#1,ThisBackClick(a4)
		bne	.EndCycle
		bra.s	.IsFront
.NotBack	cmp.w	#IECODE_MBUTTON,d0
		bne.s	.NotMiddle
		cmp.w	MiddleQual(a4),d1
		bne.s	.NotMiddle
		cmp.w	#1,MiddleClicks(a4)
		beq.s	.IsFront
		cmp.w	#1,ThisMiddleClick(a4)
		bne.s	.EndCycle
		bra.s	.IsFront
.NotMiddle	cmp.w	FrontCode(a4),d0		; Check for Front key
		bne.s	.NoCycling
		cmp.w	FrontQual(a4),d1
		bne.s	.NoCycling
		cmp.w	#1,FrontClicks(a4)
		beq.s	.IsFront
		cmp.w	#1,ThisFrontClick(a4)
		bne.s	.EndCycle
.IsFront	cmp.w	#IECLASS_RAWKEY,IEClass(a4)
		beq.s	.Clear
		cmp.w	#IECODE_RBUTTON,d0
		beq.s	.Clear
		cmp.w	#IECODE_MBUTTON,d0
		bne.s	.EndCycle
.Clear		clr.b	ie_Class(a0)			; Clear event
		and.w	#IEQUALIFIER_LEFTBUTTON,d3	; Left button pressed
		beq.s	.EndCycle
		move.l	ie_EventAddress(a0),d2		; WB windows won`t take this combination
		link	a5,#-ie_SIZEOF
		lea	-ie_SIZEOF(a5),a0
		clr.l	ie_NextEvent(a0)		; Send LeftButtonUp, so that
		move.b	#IECLASS_RAWMOUSE,ie_Class(a0)
		clr.b	ie_SubClass(a0)
		move.w	#IECODE_LBUTTON|IECODE_UP_PREFIX,ie_Code(a0)
		clr.w	ie_Qualifier(a0)
		move.l	d2,ie_EventAddress(a0)
		LNKLIB	AddIEvents,_CxBase		; Add events
		unlk	a5
.EndCycle	move.l	IEvent(a4),a0
		move.l	#REQ_CYCLE,d0
		bsr	SendEvent
		ST	Cycled(a4)
		bra.s	.NoCycle
.NoCycling	SF	Cycled(a4)

.NoCycle	tst.b	Disabled(a4)
		bne.s	NoSpec
		tst.b	FullDrag(a4)			; Full drag a window ?
		beq.s	NoSpec
		move.l	IEvent(a4),a0			; Check for qualifier
		move.w	ie_Code(a0),d0
		move.w	ie_Qualifier(a0),d1
		move.w	d1,d3
		and.w	#~(IEQUALIFIER_REPEAT|IEQUALIFIER_NUMERICPAD|IEQUALIFIER_INTERRUPT|IEQUALIFIER_RELATIVEMOUSE|IEQUALIFIER_MULTIBROADCAST),d1
		bsr	FilterDouble
		cmp.w	#IECODE_LBUTTON,d0		; Check if mouse was pressed
		bne.s	NoSpec
		cmp.w	FullDragQual(a4),d1
		bne.s	NoSpec
		move.l	BackTask(a4),a1			; Signal mother that button was hit
		move.l	DragSigFlag(a4),d0
		CALLSYS	Signal
		move.l	IEvent(a4),a0
		clr.b	ie_Class(a0)
		bra	NoTimerEvent

NoSpec		cmp.w	#IECLASS_DISKINSERTED,IEClass(a4)
		bne.s	NoDiskEvent			; Disk inserted ?
		tst.b	WatchDisks(a4)
		beq	NoTimerEvent
		move.l	IEvent(a4),a0			; Tell process
		move.l	#REQ_DISKCHANGE,d0
		bsr	SendEvent
		bra	NoTimerEvent

NoDiskEvent	cmp.w	#IECLASS_DISKREMOVED,IEClass(a4)
		bne.s	NoDiskOut
		tst.b	ScrTimeOut(a4)
		beq.s	.NoScr
		move.l	IEvent(a4),a0
		move.l	#REQ_ONSCREEN,d0
		bsr	SendEvent
.NoScr		SF	ScrTimeOut(a4)
		bra	NoTimerEvent

NoDiskOut	cmp.w	#IECLASS_RAWMOUSE,IEClass(a4)	; Mouse moved ???
		bne	NoRawMouse

		tst.b	ShowMouse(a4)
		beq.s	.NoMouse
		bsr	SigTime

.NoMouse	move.l	IEvent(a4),a0			; Get current micro seconds
		lea	ie_TimeStamp(a0),a0		; so that checking of stopped
		move.l	TV_MICRO(a0),OldCheckMicro(a4)	; mouse takes the same amount of time

		tst.b	Disabled(a4)
		bne.s	.NoMMBShift
		tst.b	MMBShift(a4)
		beq.s	.NoMMBShift
		move.l	IEvent(a4),a0
		move.w	ie_Code(a0),d0
		and.w	#~IECODE_UP_PREFIX,d0
		cmp.w	#IECODE_LBUTTON,d0
		bne.s	.NoMMBShift
		move.w	ie_Qualifier(a0),d0
		move.w	d0,d1
		and.w	#IEQUALIFIER_MIDBUTTON,d0
		beq.s	.NoMMBShift
		and.w	#~IEQUALIFIER_MIDBUTTON,d1
		or.w	#IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT,d1
		move.w	d1,ie_Qualifier(a0)

.NoMMBShift	tst.b	Disabled(a4)
		bne	.NoMouse2Menu
		tst.b	Cycled(a4)
		bne	.NoMouse2Menu
		tst.b	MenuWrap(a4)
		beq	.NoMouse2Menu
		move.l	IEvent(a4),a1			; Check for mouse button
		move.w	ie_Code(a1),d0
		move.w	d0,d1
		and.w	#~IECODE_UP_PREFIX,d1
		cmp.w	#IECODE_RBUTTON,d1
		bne	.NoMouse2Menu
		and.w	#IECODE_UP_PREFIX,d0		; Button released ??
		bne	.LeftMenu
		tst.b	EnteredMenu(a4)
		bne	.NoMouse2Menu
		move.l	_IntuitionBase(a4),a2		; Check, if active window`s got a menu
		move.l	ib_ActiveWindow(a2),d0
		beq	.NoMouse2Menu
		move.l	d0,a2
		move.l	wd_MenuStrip(a2),d0
		beq	.NoMouse2Menu
		move.l	wd_Flags(a2),d0
		and.l	#WFLG_RMBTRAP,d0
		bne	.NoMouse2Menu
		ST	EnteredMenu(a4)			; Set flag, that we`re within a menu
		link	a5,#-(ie_SIZEOF)
		lea	-(ie_SIZEOF)(a5),a0
		lea	.IePixelHin(pc),a3		; Get address of PointerPixel
		clr.l	ie_NextEvent(a0)
		move.b	#IECLASS_NEWPOINTERPOS,ie_Class(a0)
		move.b	#IESUBCLASS_PIXEL,ie_SubClass(a0)
		clr.w	ie_Code(a0)
		clr.w	ie_Qualifier(a0)
		move.l	wd_WScreen(a2),a2
		move.w	sc_MouseX(a2),d0		; Get mouseposition on window`s screen
		move.w	sc_MouseY(a2),d2
		move.w	d0,OldMenuX(a4)			; Store it
		move.w	d2,OldMenuY(a4)
		move.l	a3,ie_EventAddress(a0)
		move.l	a2,iepp_Screen(a3)		; Store new position of mouse
		move.w	d0,iepp_PositionX(a3)
		move.w	#1,iepp_PositionY(a3)
		move.l	ie_TimeStamp+TV_SECS(a1),ie_TimeStamp+TV_SECS(a0)
		move.l	ie_TimeStamp+TV_MICRO(a1),ie_TimeStamp+TV_MICRO(a0)
		LNKLIB	AddIEvents,_CxBase
		unlk	a5
		bra	NoTimerEvent
.IePixelHin	ds.b	IEPointerPixel_SIZEOF
.IePixelHer	ds.b	IEPointerPixel_SIZEOF
.LeftMenu	tst.b	EnteredMenu(a4)			; Have been visited a menu ?
		beq	.NoMouse2Menu
		SF	EnteredMenu(a4)
		clr.b	ie_Class(a1)
		link	a5,#-ie_SIZEOF
		lea	-ie_SIZEOF(a5),a0
		clr.l	ie_NextEvent(a0)
		move.b	#IECLASS_RAWMOUSE,ie_Class(a0)
		clr.b	ie_SubClass(a0)
		move.w	#IECODE_RBUTTON|IECODE_UP_PREFIX,ie_Code(a0)
		clr.w	ie_Qualifier(a0)
		clr.l	ie_EventAddress(a0)
		move.l	ie_TimeStamp+TV_SECS(a1),ie_TimeStamp+TV_SECS(a0)
		move.l	ie_TimeStamp+TV_MICRO(a1),ie_TimeStamp+TV_MICRO(a0)
		move.l	a1,-(sp)
		LNKLIB	AddIEvents,_CxBase
		move.l	(sp)+,a1
		lea	-ie_SIZEOF(a5),a0
		lea	.IePixelHer(pc),a3
		move.b	#IECLASS_NEWPOINTERPOS,ie_Class(a0)
		move.b	#IESUBCLASS_PIXEL,ie_SubClass(a0)
		clr.w	ie_Code(a0)
		clr.w	ie_Qualifier(a0)
		move.l	_IntuitionBase(a4),a2		; Get current screen
		move.l	ib_ActiveScreen(a2),a2
		move.l	a3,ie_EventAddress(a0)
		move.l	a2,iepp_Screen(a3)		; Store old position
		move.w	OldMenuX(a4),iepp_PositionX(a3)
		move.w	OldMenuY(a4),iepp_PositionY(a3)
		move.l	ie_TimeStamp+TV_SECS(a1),ie_TimeStamp+TV_SECS(a0)
		move.l	ie_TimeStamp+TV_MICRO(a1),ie_TimeStamp+TV_MICRO(a0)
		DOLIB	AddIEvents
		unlk	a5
		bra	NoTimerEvent

.NoMouse2Menu	tst.b	Disabled(a4)			; Menu to mouse ???
		bne	.NoWrap
		tst.b	EnteredMenu(a4)
		beq	.NoWrap
.CheckWrap	move.l	_IntuitionBase(a4),a2
		move.l	ib_ActiveWindow(a2),a2
		move.l	wd_MenuStrip(a2),a0		; Now calc width of menu strip
.LoopStrip	tst.l	mu_NextMenu(a0)
		beq.s	.GotLast
		move.l	mu_NextMenu(a0),a0
		bra.s	.LoopStrip
.GotLast	move.w	mu_LeftEdge(a0),d7
		move.l	mu_FirstItem(a0),a0
		moveq	#0,d6
.GetLargest	move.l	a0,d0
		beq.s	.GotLargest
		cmp.w	mi_Width(a0),d6
		bge.s	.NotLarger
		move.w	mi_Width(a0),d6
.NotLarger	move.l	mi_NextItem(a0),a0
		bra.s	.GetLargest
.GotLargest	add.w	d6,d7				; d7 =  width of menustrip
		subq.w	#2,d7
		move.l	wd_WScreen(a2),a2		; Get address of screen
		move.w	sc_Width(a2),d0
		subq.w	#1,d0
		cmp.w	sc_MouseX(a2),d0
		ble.s	.DoRightWrap
		cmp.w	sc_MouseX(a2),d7		; Mouse position "over" menu ?
		bgt.s	.NoRightWrap
.DoRightWrap	moveq	#2,d7				; Yep -> store new pos
		bra.s	.DoWrap
.NoRightWrap	tst.w	sc_MouseX(a2)			; Mouse under left bound ?
		bgt.s	.NoWrap
		subq.w	#2,d7				; Yep Store new pos
.DoWrap		link	a5,#-ie_SIZEOF
		lea	-(ie_SIZEOF)(a5),a0
		lea	.IePixelZip(pc),a3
		clr.l	ie_NextEvent(a0)
		move.b	#IECLASS_NEWPOINTERPOS,ie_Class(a0)
		move.b	#IESUBCLASS_PIXEL,ie_SubClass(a0)
		clr.w	ie_Code(a0)
		clr.w	ie_Qualifier(a0)
		move.l	a3,ie_EventAddress(a0)
		move.l	a2,iepp_Screen(a3)		; Store old position
		move.w	d7,iepp_PositionX(a3)
		move.w	#1,iepp_PositionY(a3)
		move.w	ie_TimeStamp+TV_SECS(a1),ie_TimeStamp+TV_SECS(a0)
		move.w	ie_TimeStamp+TV_MICRO(a1),ie_TimeStamp+TV_MICRO(a0)
		LNKLIB	AddIEvents,_CxBase		; And move
		unlk	a5
		bra	NoTimerEvent
.IePixelZip	ds.b	IEPointerPixel_SIZEOF

.NoWrap		tst.b	Disabled(a4)
		bne	NoTimerEvent
		tst.b	DoActivate(a4)			; Activation of Windows ?
		beq	.End1
		tst.b	SunMouse(a4)			; MousMode active ?
		beq	.End1
		move.l	IEvent(a4),a0			; Get current event
		move.w	ie_Code(a0),d0
		cmp.w	#IECODE_NOBUTTON,d0		; Any button pressed ?
		bne	.End1				; Yep -> no activation
		move.w	ie_Qualifier(a0),d0		; Any button pressed ?
		and.w	#(IEQUALIFIER_LEFTBUTTON|IEQUALIFIER_RBUTTON|IEQUALIFIER_MIDBUTTON),d0
		bne	.End1				; No activation
		ST	StartSunMouse(a4)
		move.l	_IntuitionBase(a4),a6
		move.w	ib_MouseX(a6),d0		; Get current mouse-position
		move.w	ib_MouseY(a6),d1
		move.w	OldX(a4),d2
		move.w	OldY(a4),d3    
		tst.w	d2
		blt.s	.NoEnd1
		sub.w	d0,d2
		bge.s	.XPos
		neg.w	d2
.XPos		cmp.w	#15,d2
		bgt.s	.End1
		sub.w	d1,d3
		bge.s	.YPos
		neg.w	d3
.YPos		cmp.w	#15,d3
		bgt.s	.End1
.NoEnd1		move.w	d0,OldX(a4)			; Store new one
		move.w	d1,OldY(a4)
		RELLIB	FindWindow,_KCXBase
		tst.l	d0
		beq.s	.End1
		move.l	d0,a0
		move.l	lr_Window(a0),d0
		beq.s	.End1
		move.l	_IntuitionBase(a4),a0
		cmp.l	ib_ActiveWindow(a0),d0
		beq.s	.End1
		move.l	ib_ActiveWindow(a0),a0
		move.l	wd_Flags(a0),d0
		and.l	#WFLG_MENUSTATE,d0
		bne.s	.End1
		move.l	_IntuitionBase(a4),a0
		move.l	ib_ActiveWindow(a0),a0
		bsr	CheckWinGads
		move.l	a0,d0
		beq.s	.End1
		move.l	IEvent(a4),a0
		move.l	#REQ_ACTIVATE,d0
		bsr	SendEvent

.End1		move.l	IEvent(a4),a0
		move.l	#REQ_RAWMOUSE,d0
		bsr	SendEvent
		bra	NoTimerEvent

NoRawMouse	cmp.w	#IECLASS_RAWKEY,IEClass(a4)	; Key pressed ???
		bne	NoRawKey

		tst.b	Disabled(a4)			; Check for AltMode
		bne	.NoAltCodes
		tst.b	AltModeOn(a4)
		beq	.NoAltCodes
		move.l	IEvent(a4),a1			; Get current key
		moveq	#0,d0
		moveq	#0,d1
		move.w	ie_Code(a1),d0
		move.w	ie_Qualifier(a1),d1
		move.w	d1,d2
		move.w	d0,d3
		and.w	#~(IEQUALIFIER_REPEAT|IEQUALIFIER_NUMERICPAD|IEQUALIFIER_INTERRUPT|IEQUALIFIER_RELATIVEMOUSE|IEQUALIFIER_MULTIBROADCAST),d1

		tst.w	AltQual(a4)			; Which qualifier to use ?
		beq.s	.CheckForAlt
		move.w	#$63,d4				; Get CONTROL CODE
		move.w	#IEQUALIFIER_CONTROL,d5		; Get CONTROL QUALIFIER
		bra.s	.Check
.CheckForAlt	move.w	#$64,d4				; Get ALT CODE
		move.w	#IEQUALIFIER_LALT,d5		; Get ALT QUALIFIER

.Check		and.w	#~IECODE_UP_PREFIX,d3
		and.w	#IECODE_UP_PREFIX,d0
		beq.s	.DownKey
		cmp.w	d4,d3				; Qualifier hit ?
		bne	.NoAltCodes
		tst.w	NumAltCodes(a4)
		bne	.LeftAltMode
		bra	.NoAltCodes

.DownKey	cmp.w	d0,d4				; CODE Hit ?
		beq.s	.NoCode				; Yep -> Start Sequence

.GotAltQual	cmp.w	d5,d1				; Check for Qualifier
		bne.s	.NoCode
		tst.b	EnteredAltMode(a4)		; Alread in ALTMode ?
		bne.s	.CheckCode
.NoCode		ST	EnteredAltMode(a4)		; Set flag, that we`re within AltMode
		clr.w	AltCode(a4)			; Reset counters
		clr.w	NumAltCodes(a4)
		bra	.NoAltCodes			; Skip this now

.CheckCode	and.w	#IEQUALIFIER_NUMERICPAD,d2	; Key from Numeric Pad ?
		beq	.NoCode
		link	a5,#-20				; Get code
		move.l	IEvent(a4),a0			; Convert RawKey to ASCII
		move.w	ie_Qualifier(a0),d6
		clr.w	ie_Qualifier(a0)
		lea	-20(a5),a1
		clr.b	(a1)
		move.l	#19,d1
		sub.l	a2,a2
		RELLIB	MapRawKey,_KeyMapBase
		moveq	#0,d7				; Get code
		move.b	-20(a5),d7
		unlk	a5
		move.l	IEvent(a4),a0
		move.w	d6,ie_Qualifier(a0)
		cmp.b	#'0',d7				; Is it a digit ???
		blt	.NoCode
		cmp.b	#'9',d7
		bgt	.NoCode
		sub.b	#'0',d7				; Calc value of digit
		move.w	AltCode(a4),d0
		add.w	d7,d0
		mulu	#10,d0				; shift within decimals
		move.w	d0,AltCode(a4)
		move.l	IEvent(a4),a0			; Clear this event
		clr.b	ie_Class(a0)
		addq.w	#1,NumAltCodes(a4)		; One digit done
		cmp.w	#3,NumAltCodes(a4)		; All three entered ??
		bne	.NoAltCodes			; Nope -> go on
.LeftAltMode	moveq	#0,d0				; Get current code
		move.w	AltCode(a4),d0
		divu	#10,d0
		lsl.w	#8,d0
		and.w	#$ff00,d0			; D0 = Value with trailing Zero
		move.w	d0,AltCode(a4)
		link	a5,#-12				; Reconvert code to RawKey
		lea	AltCode(a4),a0
		moveq	#1,d0
		lea	-12(a5),a1
		moveq	#6,d1
		sub.l	a2,a2
		RELLIB	MapANSI,_KeyMapBase
		tst.l	d0
		bge.s	.GotCode
		unlk	a5
		bra	.NoCode
.GotCode	move.l	IEvent(a4),a0			; Generate Event out of old one
		lea	-12(a5),a1			; with all previous keys needed
		move.b	#IECLASS_RAWKEY,ie_Class(a0)
		clr.b	ie_SubClass(a0)
		clr.b	ie_Prev2DownCode(a0)
		clr.b	ie_Prev2DownQual(a0)
		clr.b	ie_Prev1DownCode(a0)
		clr.b	ie_Prev1DownQual(a0)
		cmp.w	#3,d0				; Check for "length" of new Event
		bne.s	.No3
		move.b	(a1)+,ie_Prev2DownCode(a0)	; Create new event
		move.b	(a1)+,ie_Prev2DownQual(a0)
		bra.s	.Do2
.No3		cmp.w	#2,d0
		bne.s	.No2
.Do2		move.b	(a1)+,ie_Prev1DownCode(a0)
		move.b	(a1)+,ie_Prev1DownQual(a0)
		bra.s	.Do1
.No2		cmp.w	#1,d0
		bne.s	.No1
.Do1		moveq	#0,d1
		moveq	#0,d2
		move.b	(a1)+,d1
		move.b	(a1),d2
		move.w	d1,ie_Code(a0)
		move.w	d2,ie_Qualifier(a0)
.No1		unlk	a5
		bra	.NoCode

.NoAltCodes	move.l	IEvent(a4),a1			; If key released -> break
		move.w	ie_Code(a1),d0
		and.w	#IECODE_UP_PREFIX,d0
		bne	NoTimerEvent

		tst.b	ScrTimeOut(a4)
		beq.s	.NotDimmed
		tst.l	DimmBase(a4)
		bne.s	.NotDimmed
		move.l	IEvent(a4),a0			; Only kill char if not dimmed
		clr.b	ie_Class(a0)

.NotDimmed	tst.b	Disabled(a4)
		bne.s	.NoKey

		move.l	IEvent(a4),a1			; Check for "patched" amiga m/n keys
		move.w	ie_Qualifier(a1),d0
		move.w	d0,d1
		and.w	#IEQUALIFIER_REPEAT,d1		; only act on non-repeated keys
		bne.s	.NoLAmiga
		and.w	#IEQUALIFIER_LCOMMAND,d0
		beq.s	.NoLAmiga
		move.l	_IntuitionBase(a4),a0
		cmp.w	#$37,ie_Code(a1)
		bne.s	.NoM
		clr.b	ie_Class(a1)
		move.l	#REQ_SCRBACK,d0			; Send a screen to back
		bsr	SendEvent
		bra	NoTimerEvent
.NoM		cmp.w	#$36,ie_Code(a1)
		bne.s	.NoLAmiga
		clr.b	ie_Class(a1)			; Bring WB to front
		move.l	#REQ_WBFRONT,d0
		bsr	SendEvent
		bra	NoTimerEvent

.NoLAmiga	move.l	IEvent(a4),a0
		move.l	#REQ_RAWKEY,d0
		bsr	SendEvent

.NoKey		tst.b	Disabled(a4)
		bne.s	.NoClickYet
		tst.b	KeyClick(a4)			; Enabled Key-Click ?
		beq.s	.NoClickYet
		link	a5,#-128
		move.l	IEvent(a4),a0			; Convert RAW-Key
		lea	-128(a5),a1
		clr.b	(a1)				; Clear previous keys
		moveq	#127,d1
		sub.l	a2,a2
		RELLIB	MapRawKey,_KeyMapBase		; Convert raw key
		lea	-128(a5),a1
		move.b	(a1),d0
		unlk	a5
		tst.b	d0				; No keys ?
		beq.s	.NoClickYet
		cmp.b	#$9b,d0				; Initial ANSI-Code
		beq.s	.NoClickYet			; No click
		cmp.b	#$09,d0				; TAB-Key pressed ?
		beq.s	.NoClickYet

.NoClickYet	tst.b	Disabled(a4)			; Allow Shortcuts from within String gadgets ???
		bne	.NoRAmiga
		tst.b	StringMenu(a4)
		beq	.NoRAmiga
		move.l	IEvent(a4),a0
		move.w	ie_Code(a0),d0
		and.w	#IECODE_UP_PREFIX,d0
		bne	.NoRAmiga
		move.w	ie_Qualifier(a0),d0
		and.w	#IEQUALIFIER_REPEAT,d0
		bne	.NoRAmiga
		move.w	ie_Qualifier(a0),d0
		and.w	#IEQUALIFIER_RCOMMAND,d0
		beq	.NoRAmiga
		move.l	_IntuitionBase(a4),a3
		move.l	ib_ActiveWindow(a3),a3
		tst.l	wd_MenuStrip(a3)
		beq	.NoRAmiga
		link	a5,#-2				; Get code pressed and convert to vanillakay
		move.l	IEvent(a4),a0
		move.w	ie_Qualifier(a0),d6
		and.w	#IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT|IEQUALIFIER_LALT|IEQUALIFIER_RALT,ie_Qualifier(a0)
		lea	-2(a5),a1
		clr.b	(a1)
		moveq	#1,d1
		sub.l	a2,a2
		RELLIB	MapRawKey,_KeyMapBase
		moveq	#0,d7				; Get code
		move.b	-2(a5),d7
		unlk	a5
		move.l	IEvent(a4),a0
		move.w	d6,ie_Qualifier(a0)
		cmp.b	#'x',d7
		beq	.NoRAmiga
		cmp.b	#'q',d7
		beq	.NoRAmiga
		move.l	_IntuitionBase(a4),a0		; Check for active str-gadgets within window
		move.l	ib_ActiveWindow(a0),a0
		move.l	a0,-(sp)
		bsr	CheckWinGads
		move.l	(sp)+,a1
		move.l	a0,d0
		bne	.NoRAmiga			; No active String Menu -> Go On
		move.l	a1,a3
		moveq	#1,d6				; Set number for checks
.CheckForUpper	move.l	wd_MenuStrip(a3),a0
		tst.b	d6
		bne.s	.NoUpperInput
		move.l	d7,d0
		bsr	GetUpperCase
		move.l	d0,d7
.NoUpperInput	moveq	#0,d1				; Menu number
.LoopMenus	move.l	a0,d0				; Loop for menus
		beq.s	.CheckLoop
		move.l	mu_FirstItem(a0),a1		; Loop for items
		moveq	#0,d2				; Item number
.LoopItems	move.l	a1,d0
		beq.s	.NoItems
		move.l	mi_SubItem(a1),a2		; Loop for subitems
		moveq	#0,d3				; SubItem number
.LoopSubs	move.l	a2,d0
		beq.s	.NoSubs
		move.w	mi_Flags(a2),d0
		and.w	#COMMSEQ,d0			; Check for Command-Key within SubItem
		beq.s	.NoSubCommand
		moveq	#0,d0
		move.b	mi_Command(a2),d0
		tst.b	d6
		bne.s	.NoUpperItem
		bsr	GetUpperCase
.NoUpperItem	cmp.b	d0,d7
		bne.s	.NoSubCommand
		move.w	#-1,mi_NextSelect(a2)
		bra.s	.GotMenu
.NoSubCommand	move.l	mi_NextItem(a2),a2		; Get Pointer to next subitem
		addq.w	#1,d3
		bra.s	.LoopSubs
.NoSubs		move.w	mi_Flags(a1),d0
		and.w	#COMMSEQ,d0			; Check for Command-Key within MenuItem
		beq.s	.NoItemCommand
		moveq	#0,d0
		move.b	mi_Command(a1),d0
		tst.b	d6
		bne.s	.NoUpperSub
		bsr	GetUpperCase
.NoUpperSub	cmp.b	d0,d7
		bne.s	.NoItemCommand
		move.w	#-1,mi_NextSelect(a1)
		bra.s	.GotMenu
.NoItemCommand	move.l	mi_NextItem(a1),a1		; Get Pointer to next item
		addq.w	#1,d2
		bra	.LoopItems
.NoItems	move.l	mu_NextMenu(a0),a0		; Get Pointer to next menu
		addq.w	#1,d1
		bra	.LoopMenus
.CheckLoop	dbf	d6,.CheckForUpper
		bra.s	.NoRAmiga
.GotMenu	and.w	#$01f,d1			; Calc correct menu number
		and.w	#$02f,d2
		and.w	#$01f,d3
		lsl.w	#5,d2
		lsl.w	#8,d3
		lsl.w	#3,d3
		or.w	d2,d1
		or.w	d3,d1
		move.l	IEvent(a4),a0			; Convert RawKey-Event to
		move.b	#IECLASS_MENULIST,ie_Class(a0)	; Set item type to be menupick
		clr.b	ie_SubClass(a0)			; Clear subclass
		move.w	d1,ie_Code(a0)			; Set menunumber
		clr.w	ie_Qualifier(a0)		; No qualifier
		move.l	a3,ie_EventAddress(a0)		; Set destination window

.NoRAmiga	tst.b	Disabled(a4)
		bne	.NoFKeys
		tst.b	FKeysOn(a4)			; FKeys enabled ?
		beq	.NoFKeys
		move.l	IEvent(a4),a1
		move.w	ie_Code(a1),d0
		cmp.w	#$50,d0
		blt	.NoFKeys
		cmp.w	#$59,d0
		bgt	.NoFKeys
		move.w	ie_Qualifier(a1),d1
		and.w	#~(IEQUALIFIER_REPEAT|IEQUALIFIER_NUMERICPAD|IEQUALIFIER_INTERRUPT|IEQUALIFIER_RELATIVEMOUSE|IEQUALIFIER_MULTIBROADCAST),d1
		bsr	FilterDouble
		moveq	#0,d7
		moveq	#3,d6
		lea	FKeyQualFlags,a0
.CheckQual	cmp.w	(a0)+,d1
		beq.s	.GotQual
		cmp.w	(a0)+,d1
		beq.s	.GotQual
		add.l	#(MAXFKEYWIDTH+2)*10,d7
		dbf	d6,.CheckQual
		bra	.NoFKeys
.GotQual	lea	FKeys(a4),a0
		lea	0(a0,d7),a0
		sub.w	#$50,d0
		mulu	#(MAXFKEYWIDTH+2),d0
		lea	0(a0,d0.w),a0
		lea	FKeyCmds,a2			; Check for any commands entered
		lea	FKeyRouts,a3
		move.w	#FKEYCMDNUM-1,d6
.CheckCmds	move.l	(a2)+,a1
		moveq	#-1,d7
		bsr	StrCmp2
		tst.l	d0
		bne.s	.NoCmd
		move.l	(a3),a3
		jsr	(a3)
		bra	.NoFKeys
.NoCmd		addq.l	#4,a3
		dbf	d6,.CheckCmds
		clr.l	-(a7)
		move.l	a0,-(a7)
		jsr	_InvertString			; Invert String "inverts" the string
		addq.l	#8,sp				; but we can`t simply reverse the string
		tst.l	d0				; otherwise all special codes will get lost
		beq	.NoFKeys			; so we flip the list of events now
		move.l	d0,a0
		move.l	d0,a5				; Store pointer to events
		moveq	#4,d0				; Calc number of events
.Loop		tst.l	ie_NextEvent(a0)
		beq.s	.Done
		addq.l	#4,d0
		move.l	ie_NextEvent(a0),a0
		bra.s	.Loop
.Done		move.l	d0,d7
		lsr.l	#2,d7				; Alloc table for pointers
		move.l	#MEMF_CLEAR,d1
		CALLSYS	AllocVec
		tst.l	d0
		beq.s	.NoTable
		move.l	a5,a0				; Copy adresses of events
		move.l	d0,a3
		move.l	d0,a1
		move.l	d7,d0
		subq.w	#1,d0
.CopyPtrs	move.l	a0,(a1)+			; Store all addresses of events
		move.l	ie_NextEvent(a0),a0		; within table
		dbf	d0,.CopyPtrs
		move.l	a3,a0				; Remake pointers
		subq.w	#1,d7				; sub one
		move.l	d7,d0
		lsl.l	#2,d0				; Calc offset
		lea	0(a0,d0),a0			; a0 points to end of list
		move.l	(a0),a5				; Get address of "last" event
		subq.w	#1,d7				; sub one for loop
		blt.s	.OnlyOne			; Ok -> There only way one
.Remake		move.l	(a0),a1				; Get address of event
		move.l	-4(a0),ie_NextEvent(a1)		; Create link to previous one
		subq.l	#4,a0				; Next event
		dbf	d7,.Remake
.OnlyOne	move.l	(a0),a0				; Get last event
		clr.l	ie_NextEvent(a0)		; Clear old link
		move.l	a5,a0				; Add events
		LNKLIB	AddIEvents,_CxBase
		move.l	IEvent(a4),a0
		clr.b	ie_Class(a0)
.NoEvents	move.l	a3,a1				; Free table
		CALLSYS	FreeVec
.NoTable	move.l	a5,-(sp)			; Free events
		jsr	_FreeIEvents
		addq.l	#4,sp

.NoFKeys	tst.b	Disabled(a4)
		bne.s	.NoEscape
		tst.b	ESCClose(a4)			; Allow closing of windows
		beq.s	.NoEscape			; via ESC-Key ???
		move.l	IEvent(a4),a1
		cmp.w	#RESCKEY,ie_Code(a1)
		bne.s	.NoEscape
		move.w	ie_Qualifier(a1),d1
		and.w	#~(IEQUALIFIER_REPEAT|IEQUALIFIER_NUMERICPAD|IEQUALIFIER_INTERRUPT|IEQUALIFIER_RELATIVEMOUSE|IEQUALIFIER_MULTIBROADCAST),d1
		move.w	#RESCKEY,d0
		bsr	FilterDouble
		cmp.w	ESCQual(a4),d1
		bne.s	.NoEscape
		move.l	_IntuitionBase(a4),a0
		move.l	ib_ActiveWindow(a0),a3
		move.l	a3,d0
		beq.s	.NoEscape			; Yep -> no close
		move.l	wd_Flags(a3),d0			; Window has a closegadget ?
		and.l	#WFLG_CLOSEGADGET,d0
		beq.s	.NoEscape			; Nope -> go on
		tst.l	wd_FirstRequest(a3)		; Requester visible ???
		bne.s	.NoEscape
.IsAct		link	a5,#-ie_SIZEOF
		lea	-ie_SIZEOF(a5),a0		; fill in Event structure
		clr.l	ie_NextEvent(a0)
		move.b	#IECLASS_CLOSEWINDOW,ie_Class(a0)
		clr.b	ie_SubClass(a0)
		clr.w	ie_Code(a0)
		clr.w	ie_Qualifier(a0)
		move.l	a3,ie_EventAddress(a0)
		LNKLIB	AddIEvents,_CxBase		; Add event
		unlk	a5
		bra	NoTimerEvent

.NoEscape	tst.b	Disabled(a4)
		bne.s	.CheckMouseOff
		tst.b	DoActivate(a4)			; Window activation set ?
		beq.s	.CheckMouseOff
		tst.b	SunMouse(a4)
		bne.s	.CheckMouseOff
		RELLIB	FindWindow,_KCXBase		; Check window underneith mouse
		tst.l	d0
		beq.s	.CheckMouseOff
		move.l	d0,a0
		move.l	lr_Window(a0),d0
		beq.s	.CheckMouseOff
		move.l	_IntuitionBase(a4),a0		; Still the same ?
		cmp.l	ib_ActiveWindow(a0),d0
		beq.s	.CheckMouseOff
		move.l	ib_ActiveWindow(a0),a0
		move.l	wd_Flags(a0),d0
		and.l	#WFLG_MENUSTATE,d0
		bne.s	.CheckMouseOff
		move.l	_IntuitionBase(a4),a0
		move.l	ib_ActiveWindow(a0),a0		; Check for active gadgets
		bsr	CheckWinGads			; on current window
		move.l	a0,d0
		beq.s	.CheckMouseOff
		move.l	IEvent(a4),a0
		move.l	#REQ_ACTIVATE,d0
		bsr	SendEvent
		move.l	IEvent(a4),a0
		clr.b	ie_Class(a0)

.CheckMouseOff	tst.b	Disabled(a4)
		bne	.NoUmlaut
		tst.b	MapUmlaut(a4)			; Map "Umlauts" ?
		beq	.NoUmlaut
		move.l	IEvent(a4),a1			; Check for "Umlauts"
		move.w	ie_Qualifier(a1),d1		; Only accept plain and shifted keys
		and.w	#~(IEQUALIFIER_REPEAT|IEQUALIFIER_NUMERICPAD|IEQUALIFIER_INTERRUPT|IEQUALIFIER_RELATIVEMOUSE|IEQUALIFIER_MULTIBROADCAST),d1
		move.w	d1,d2
		and.w	#(IEQUALIFIER_LALT|IEQUALIFIER_RALT|IEQUALIFIER_LCOMMAND|IEQUALIFIER_RCOMMAND),d2
		bne	.NoUmlaut
		movem.l	d1/a1,-(sp)
		link	a5,#-20
		move.l	a1,a0
		lea	-20(a5),a1
		clr.b	(a1)
		move.l	#19,d1
		sub.l	a2,a2
		RELLIB	MapRawKey,_KeyMapBase
		moveq	#0,d0
		move.b	-20(a5),d0
		unlk	a5
		movem.l	(sp)+,d1/a1
.UmlautOk	lea	UmlautTab(pc),a3		; Get UmlautTab
		moveq	#7,d7
.GetUmlaut	cmp.w	(a3),d0				; Got "Umlaut" ?
		beq.s	.GotUmlaut			; Yep -> go on
		addq.l	#6,a3
		dbf	d7,.GetUmlaut
		bra.s	.NoUmlaut			; No "Umlaut"
.GotUmlaut	cmp.b	#'ß',d0
		bne.s	.NoSS
		move.w	d1,d0
		and.w	#(IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT),d0
		bne.s	.NoUmlaut
		bra.s	.NoShift
.NoSS		tst.w	(a3)				; Skip shifted ß
		beq.s	.NoUmlaut

.NoShift	link	a5,#-ie_SIZEOF			; Get space for one IEs
		move.b	ie_SubClass(a1),d0		; Get subclass
		move.l	ie_EventAddress(a1),d2		; Address

		clr.b	ie_Class(a1)			; Clear current event

		lea	-ie_SIZEOF(a5),a0		; Create new event -> Clear link to next
		clr.l	ie_NextEvent(a0)
		move.b	#IECLASS_RAWKEY,ie_Class(a0)
		move.b	d0,ie_SubClass(a0)
		move.w	2(a3),ie_Code(a0)		; Get first char for "Umlaut"
		move.w	d1,ie_Qualifier(a0)
		move.l	d2,ie_EventAddress(a0)
		LNKLIB	AddIEvents,_CxBase		; Add events
		lea	-ie_SIZEOF(a5),a0
		move.w	4(a3),ie_Code(a0)		; Get second char for "Umlaut"
		and.w	#~(IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT),ie_Qualifier(a0)
		DOLIB	AddIEvents
		unlk	a5

.NoUmlaut	bra	NoTimerEvent


NoRawKey	cmp.w	#IECLASS_TIMER,IEClass(a4)	; Timer event ?
		bne	NoTimerEvent

		tst.b	MouseIsOff(a4)
		beq.s	.NoMouseOff
		move.w	MouseOffCount(a4),d0
		addq.w	#1,d0
		move.w	d0,MouseOffCount(a4)
		cmp.w	#10,d0
		bne.s	.NoMouseOff
		clr.b	MouseIsOff(a4)
		bsr	OffMouse
		clr.w	MouseOffCount(a4)

.NoMouseOff	tst.l	BlankScrHandle(a4)		; Do we have a "blank screen" ?
		bne.s	.CheckScreen
		tst.l	DimmBase(a4)
		beq.s	.NoOnScreen

.CheckScreen	move.l	_IntuitionBase(a4),a0
		move.l	ib_FirstScreen(a0),a0

		tst.b	ScreenDimm(a4)
		bne.s	.NoOnScreen
		cmp.l	BlankScrHandle(a4),a0		; If not close it
		beq.s	.NoOnScreen
		move.l	IEvent(a4),a0
		move.l	#REQ_ONSCREEN,d0
		bsr	SendEvent

.NoOnScreen	tst.b	Disabled(a4)
		bne	.NoCheck
		move.l	IEvent(a4),a0			; A little delay for check
		lea	ie_TimeStamp(a0),a0		; Only check every half a second
		move.l	TV_MICRO(a0),d0			; If mouse was moved
		move.l	d0,d1				; so that SUN-Mouse may
		move.l	OldCheckMicro(a4),d2		; activate windows if the
		sub.l	d2,d0				; mouse has come to a complete
		bge.s	.IsPos				; stop
		neg.l	d0
.IsPos		cmp.l	#100000,d0
		blt	.NoCheck
		move.l	d1,OldCheckMicro(a4)
		move.w	#-1,OldX(a4)
		tst.b	DoActivate(a4)			; WindowActivation active ?
		beq	.NoCheck
		tst.b	SunMouse(a4)			; Mouse mode active ?
		beq	.NoCheck
		move.l	_IntuitionBase(a4),a6
		move.w	ib_MouseX(a6),d5		; Get current mouse position
		move.w	ib_MouseY(a6),d6
		move.w	CheckX(a4),d2			; Get old position
		move.w	CheckY(a4),d3
		move.w	d5,CheckX(a4)			; Store new one
		move.w	d6,CheckY(a4)
		cmp.w	d5,d2				; Mouse moved since last change ?
		bne.s	.NoCheck1
		cmp.w	d6,d3
		bne.s	.NoCheck1
		tst.b	Moved(a4)			; Already activated on that position ?
		bne.s	.NoCheck
		RELLIB	FindWindow,_KCXBase
		tst.l	d0
		beq.s	.NoCheck
		move.l	d0,a0
		move.l	lr_Window(a0),d0
		beq.s	.NoCheck
		move.l	_IntuitionBase(a4),a0
		cmp.l	ib_ActiveWindow(a0),d0
		beq.s	.NoCheck
		move.l	ib_ActiveWindow(a0),a0
		move.l	wd_Flags(a0),d0
		and.l	#WFLG_MENUSTATE,d0
		bne.s	.NoCheck
		move.l	_IntuitionBase(a4),a0
		move.l	ib_ActiveWindow(a0),a0
		bsr	CheckWinGads
		move.l	a0,d0
		beq.s	.NoCheck
		tst.b	StartSunMouse(a4)
		beq.s	.NoCheck
		SF	StartSunMouse(a4)
		move.b	#1,Moved(a4)			; Set flag that already activated
		move.l	IEvent(a4),a0
		move.l	#REQ_ACTIVATE,d0
		bsr.s	SendEvent
		bra.s	.NoCheck
.NoCheck1	clr.b	Moved(a4)			; Clear flag

.NoCheck	move.l	IEvent(a4),a0			; Check if one second
		lea	ie_TimeStamp(a0),a1		; has elapsed since last
		move.l	TV_SECS(a1),d1			; signal
		cmp.l	OldTimeSig(a4),d1
		beq.s	NoTimerEvent
		move.l	d1,OldTimeSig(a4)		; put the message
		move.l	#REQ_TIMER,d0
		bsr.s	SendEvent

NoTimerEvent	move.l	IEvent(a4),a0			; Try to get next event linked
		move.l	ie_NextEvent(a0),d0		; to this one
		beq.s	NoEvent				; No other one -> that`s it
		move.l	d0,IEvent(a4)			; Store address
		bra	EventLoop			; Loop

NoEvent		SF	Cycled(a4)
		movem.l	(sp)+,RegsAll
		move.l	a0,d0				; Rstore pointer to "new" events
		rts
FKeyQualFlags	dc.w	0,0
		dc.w	IEQUALIFIER_LSHIFT,IEQUALIFIER_RSHIFT
		dc.w	IEQUALIFIER_LALT,IEQUALIFIER_RALT
		dc.w	IEQUALIFIER_CONTROL,-1
AudioName	dc.b	"audio.device",0
		EVEN



**********************************************************************
*      Append the current event to a message and send that one       *
*                        to the event handler                        *
**********************************************************************
SendEvent	movem.l	d0/a0,-(sp)
		move.l	CheckerProc(a4),a1
		move.l	#31,d0
		CALLSYS	SetTaskPri
		move.l	d0,d6
		movem.l	(sp)+,d0/a0
		lea	EventPool(a4),a1		; Find free entry
		moveq	#MAXEVENTS-1,d7
.Find		tst.l	LN_NAME(a1)
		bne.s	.GoOn
		move.l	d0,LN_NAME(a1)
		lea	req_IEvent(a1),a1
		moveq	#(ie_SIZEOF>>1)-1,d5
.CopyEvent	move.w	(a0)+,(a1)+
		dbf	d5,.CopyEvent
		move.l	CheckerProc(a4),a1
		move.l	EventSigFlag(a4),d0
		CALLSYS	Signal
		bra.s	.End
.GoOn		lea	req_SIZEOF(a1),a1
		dbf	d7,.Find
.End		move.l	d6,d0
		move.l	CheckerProc(a4),a1
		CALLSYS	SetTaskPri
		rts





**********************************************************************
*             Wait until remakes on clockwindow are made             *
**********************************************************************
ClockRemake	move.l	ClockHandle(a4),a0		; Wait until window popped up
		move.l	wd_UserPort(a0),a0
		CALLSYS	WaitPort
.Loop		move.l	ClockHandle(a4),a0
		move.l	wd_UserPort(a0),a0
		DOLIB	GetMsg
		tst.l	d0
		beq.s	.End
		move.l	d0,a1
		DOLIB	ReplyMsg
		bra.s	.Loop
.End		rts



**********************************************************************
*                          Reset the timers                          *
**********************************************************************
ResetTimer	lea	ie_TimeStamp(a0),a0
		move.l	TV_SECS(a0),d0			; Get current seconds
		move.l	d0,d1
		add.l	ScreenTime(a4),d0		; add delay for screen blank
		move.l	d0,ScreenOff(a4)
		add.l	MouseTime(a4),d1		; add delay for mouse blank
		move.l	d1,MouseOff(a4)
		clr.b	TimeReset(a4)
		rts



**********************************************************************
*                          Blank the mouse                           *
**********************************************************************
OffMouse	tst.b	MouseIsOff(a4)
		bne.s	.AlreadyOff
		sub.l	a0,a0
		lea	MySprite(pc),a1
		lea	NoSpriteDat,a2
		RELLIB	ChangeSprite,_GfxBase
		move.b	#1,MouseIsOff(a4)
.AlreadyOff	rts
MySprite	ds.l	3




**********************************************************************
*                           UnBlank mouse                            *
**********************************************************************
OnMouse		tst.b	MouseIsOff(a4)
		beq.s	.End
		RELLIB	RethinkDisplay,_IntuitionBase
		clr.b	MouseIsOff(a4)
.End		rts





**********************************************************************
*                     UnBlank/UnDimm the Screen                      *
**********************************************************************
OnScreen	bsr	GetSemaphore
		tst.b	ScreenDimm(a4)
		beq.s	.UnDimm
		tst.l	DimmBase(a4)
		beq.s	.End

		moveq	#0,d0				; Check if screen to be undimmed
		RELLIB	LockIBase,_IntuitionBase	; is still there
		move.l	d0,d7

		move.l	ib_FirstScreen(a6),a3		; Get frontmost screen

		move.l	DimmBase(a4),a2
.ScreenLoop	move.l	dm_Screen(a2),a0
		move.l	a0,d0
		beq.s	.NoMoreScrs
		move.l	a3,a1
.CheckScreen	move.l	a1,d0
		beq.s	.ScreenNotThere
		cmp.l	a0,a1
		bne.s	.NotEqual
		lea	sc_ViewPort(a1),a0
		lea	dm_Colors(a2),a1
		moveq	#32,d0
		RELLIB	LoadRGB4,_GfxBase
		bra.s	.ScreenNotThere
.NotEqual	move.l	sc_NextScreen(a1),a1
		bra.s	.CheckScreen
.ScreenNotThere	lea	dm_SIZEOF(a2),a2
		bra.s	.ScreenLoop

.NoMoreScrs	move.l	d7,a0
		RELLIB	UnlockIBase,_IntuitionBase

		move.l	DimmBase(a4),a1
		CALLSYS	FreeVec
		clr.l	DimmBase(a4)
		bra.s	.End

.UnDimm		tst.l	BlankScrHandle(a4)			; Close blank-screen if there
		beq.s	.End
		move.l	BlankScrHandle(a4),a0
		RELLIB	CloseScreen,_IntuitionBase
		clr.l	BlankScrHandle(a4)
.End		bsr	FreeSemaphore
		rts




**********************************************************************
*                       Blank/Dimm the screen                        *
**********************************************************************
OffScreen	bsr	GetSemaphore
		tst.b	ScreenDimm(a4)			; To be dimmed ?
		beq	.NoDimm
		tst.l	BlankScrHandle(a4)		; Blank Screen there ?
		beq.s	.NoBlankOff
		move.l	BlankScrHandle(a4),a0		; Close it first
		RELLIB	CloseScreen,_IntuitionBase
		clr.l	BlankScrHandle(a4)
.NoBlankOff	tst.l	DimmBase(a4)			; Already dimmed ?
		bne	.EndDimm

		moveq	#0,d0				; Lock Base
		RELLIB	LockIBase,_IntuitionBase
		move.l	d0,-(sp)
		move.l	ib_FirstScreen(a6),a3
		move.l	a3,a0

		moveq	#1,d0				; Calc number of screens
.CalcScreens	move.l	a0,d1
		beq.s	.NoMoreScreens
		addq.l	#1,d0
		move.l	sc_NextScreen(a0),a0
		bra.s	.CalcScreens

.NoMoreScreens	mulu	#dm_SIZEOF,d0			; Get buffer for screens
		move.l	#MEMF_CLEAR,d1
		CALLSYS	AllocVec
		move.l	d0,DimmBase(a4)
		beq	.DimmError

		move.l	d0,a2				; Get pointer to buffer

.GetColors	move.l	a3,d0
		beq.s	.GotColors			; available to the structure
		move.l	a3,dm_Screen(a2)		; allocated
		lea	sc_ViewPort(a3),a3
		move.l	vp_ColorMap(a3),a3
		move.w	cm_Count(a3),dm_NumCols(a2)
		move.l	a2,a1
		moveq	#31,d7
		moveq	#0,d6
.LoopColors	move.l	a3,a0
		move.l	d6,d0
		move.l	a1,-(sp)
		RELLIB	GetRGB4,_GfxBase
		move.l	(sp)+,a1
		move.w	d0,dm_Colors(a1)
		move.w	d0,dm_Dimmed(a1)
		addq.l	#2,a1
		addq.l	#1,d6
		dbf	d7,.LoopColors
		move.l	dm_Screen(a2),a3
		lea	dm_SIZEOF(a2),a2
		move.l	sc_NextScreen(a3),a3
		bra.s	.GetColors

.GotColors	move.l	DimmBase(a4),a2			; Dimm the colors by the given amount
.ScreenLoop	move.w	dm_NumCols(a2),d7
		subq.w	#1,d7
		lea	dm_Dimmed(a2),a3
.RemakeAll	move.l	DimmValue(a4),d4
.RemakeCols	move.w	(a3),d0
		move.w	d0,d1
		move.w	d0,d2
		and.w	#$0f00,d0
		and.w	#$00f0,d1
		and.w	#$000f,d2
		lsr.w	#8,d0
		lsr.w	#4,d1
		subq.w	#1,d0				; Sub one from the red part
		bge.s	.GoRed
		moveq	#0,d0				; If <0 clear red
.GoRed		subq.w	#1,d4				; On value done
		ble.s	.GoBlue				; Noe more left ? -> Quit
		subq.w	#1,d1				; Now go for greem
		bge.s	.GoGreen
		moveq	#0,d1
.GoGreen	subq.w	#1,d4				; Done ???
		ble.s	.GoBlue
		subq.w	#1,d2				; Go for blue
		bge.s	.GoBlue
		moveq	#0,d2
.GoBlue		lsl.w	#8,d0				; reconstruct new color
		lsl.w	#4,d1
		or.w	d1,d2
		or.w	d0,d2
		move.w	d2,(a3)				; and write it back
		subq.w	#1,d4
		bgt.s	.RemakeCols
		addq.l	#2,a3
		dbf	d7,.RemakeAll
		move.l	dm_Screen(a2),a0		; Load new colors
		lea	sc_ViewPort(a0),a0
		lea	dm_Dimmed(a2),a1
		move.w	dm_NumCols(a2),d0
		RELLIB	LoadRGB4,_GfxBase
		lea	dm_SIZEOF(a2),a2
		tst.l	dm_Screen(a2)
		bne.s	.ScreenLoop

.DimmError	move.l	(sp)+,a0
		RELLIB	UnlockIBase,_IntuitionBase
.EndDimm	bsr	FreeSemaphore
		rts

.NoDimm		tst.l	BlankScrHandle(a4)		; Screen already blanked ?
		bne.s	.End				; Yep -> pop it to front
		moveq	#0,d0
		RELLIB	LockIBase,_IntuitionBase	; Get DisplayID of active screen
		move.l	ib_ActiveScreen(a6),a2
		move.l	d0,a0
		DOLIB	UnlockIBase
		lea	sc_ViewPort(a2),a0
		RELLIB	GetVPModeID,_GfxBase
		cmp.l	#INVALID_ID,d0
		beq.s	.EndIt
		move.l	d0,BlankScrTags+4
		sub.l	a0,a0
		lea	BlankScrTags(pc),a1
		RELLIB	OpenScreenTagList,_IntuitionBase
		move.l	d0,BlankScrHandle(a4)
		beq.s	.EndIt
		bsr	OffMouse			; Disable mouse
		bsr	FreeSemaphore
		rts
.End		move.l	BlankScrHandle(a4),a0		; Get screen to front :-)
		RELLIB	ScreenToFront,_IntuitionBase
		bsr	OffMouse
.EndIt		bsr	FreeSemaphore
		rts
BlankScrTags	dc.l	SA_DisplayID,DEFAULT_MONITOR_ID
		dc.l	SA_Depth,1
		dc.l	SA_Colors,BlankCols
		dc.l	SA_Quiet,-1
		dc.l	TAG_DONE
BlankCols	dc.w	0,0,0,0,0,0,0,0,-1,0,0,0




**********************************************************************
*                   This is the activation-routine                   *
**********************************************************************
Activate	bsr	GetSemaphore
		movem.l	d0-d7/a0-a6,-(sp)
		link	a5,#-(180+80)
		tst.w	RequestsOpen(a4)
		bne	.NotAct

		RELLIB	FindWindow,_KCXBase
		tst.l	d0
		beq	.NotAct

		movem.l	d0/a0,-(sp)
		CALLSYS	Forbid
		moveq	#0,d0
		RELLIB	LockIBase,_IntuitionBase

		move.l	ib_ActiveWindow(a6),a2		; First look if it`s a "locked" window

		move.l	d0,a0
		DOLIB	UnlockIBase

		move.l	a2,a0				; Active window has an active
		bsr	CheckWinGads			; String Gadget ?
		move.l	a0,d0
		beq	.GotWindow

.NoStrGad	move.l	wd_UserPort(a2),d0		; Try to get UserPort
		beq	.NoWinPort
		move.l	d0,a0
		move.l	MP_SIGTASK(a0),d0		; Get task of window
		beq	.NoWinPort
		move.l	d0,a3
		moveq	#-1,d7
		moveq	#1,d5
		cmp.b	#NT_PROCESS,LN_TYPE(a3)		; Is it a process ?
		bne.s	.IsTask				; Nope go on
		moveq	#0,d5
		tst.l	pr_TaskNum(a3)			; Got a task number ?
		beq.s	.IsTask				; Nope -> is task
		move.l	pr_CLI(a3),d0			; Have a cli ?
		beq.s	.IsTask				; Nope -> is task
		lsl.l	#2,d0
		move.l	d0,a0
		tst.l	cli_Module(a0)			; Got a module ?
		beq.s	.IsTask				; Nope -> is task
		move.l	cli_CommandName(a0),d0		; Get name of process
		lsl.l	#2,d0
		move.l	d0,a0
		moveq	#0,d7
		move.b	(a0)+,d7			; Get Length of taskname
		lea	-(180+80)(a5),a1
		bsr	StrNCpy				; Copy name
		bra.s	.GoOnName
.IsTask		move.l	LN_NAME(a3),a0			; Get name of task
		lea	-(180+80)(a5),a1
		bsr	StrCpy				; Copy name
.GoOnName	lea	TaskWinBase(a4),a3
		TSTLIST	a3
		beq.s	.NoWinPort
		move.l	LH_HEAD(a3),a3
.LoopTasks	move.l	a3,d0
		beq.s	.NoWinPort
		lea	lw_Name(a3),a0			; Get name
		tst.b	d5
		beq.s	.IsProc
		lea	-(180+80)(a5),a1
		bsr	StrCmp
		tst.l	d0
		bne.s	.NotSameTask
		movem.l	(sp)+,d0/a0
		sub.l	a0,a0
		bra	.GotWindow
.IsProc		move.l	a0,d1
		lea	-180(a5),a0			; Get buffer for pattern
		move.l	a0,d2
		move.l	#180,d3
		RELLIB	ParsePatternNoCase,_DOSBase	; Tokenize pattern
		lea	-180(a5),a0			; Check if name of task matches
		move.l	a0,d1
		lea	-(180+80)(a5),a0
		move.l	a0,d2
		DOLIB	MatchPatternNoCase
		tst.l	d0
		beq.s	.NotSameTask
		movem.l	(sp)+,d0/a0
		sub.l	a0,a0
		bra.s	.GotWindow
.NotSameTask	SUCC	a3,a3
		bra.s	.LoopTasks

.NoWinPort	move.l	wd_Title(a2),d0			; Get name of window
		beq.s	.DoAct
		move.l	d0,a2
		lea	WindowBase(a4),a3		; Get address of window list
		TSTLIST	a3
		beq.s	.DoAct
		move.l	LH_HEAD(a3),a3
.LoopWins	move.l	a3,d0
		beq.s	.DoAct
		lea	lw_Name(a3),a0			; Get name
		move.l	a0,d1
		lea	-180(a5),a0			; Get buffer for pattern
		move.l	a0,d2
		move.l	#180,d3
		RELLIB	ParsePatternNoCase,_DOSBase	; Tokenize pattern
		lea	-180(a5),a0			; Check if title of window matches
		move.l	a0,d1
		move.l	a2,d2
		DOLIB	MatchPatternNoCase
		tst.l	d0
		beq.s	.NotSameWin
		movem.l	(sp)+,d0/a0
		sub.l	a0,a0
		bra.s	.GotWindow
.NotSameWin	SUCC	a3,a3
		bra.s	.LoopWins

.DoAct		movem.l	(sp)+,d0/a0
		move.l	d0,a1
		move.l	lr_Window(a1),a1
		move.l	_IntuitionBase(a4),a2
		cmp.l	ib_ActiveWindow(a2),a1
		bne.s	.GoOn
		sub.l	a0,a0
		bra.s	.GotWindow
.GoOn		move.l	d0,a0
		move.l	lr_Window(a0),a0
.GotWindow	CALLSYS	Permit
		move.l	a0,d0
		beq.s	.NotAct
		RELLIB	ActivateWindow,_IntuitionBase	; activate window
.NotAct		unlk	a5
		movem.l	(sp)+,d0-d7/a0-a6
		bra	FreeSemaphore




**********************************************************************
*           Checks for any active gadgets within a window            *
**********************************************************************
CheckWinGads	movem.l	d1/a1,-(sp)
		move.l	wd_FirstGadget(a0),a1		; First gadget of window

.CheckGadgets	move.l	a1,d1
		beq.s	.GadgetsChecked

		move.w	gg_GadgetType(a1),d1		; Is it a string-gadget ?
		and.w	#STRGADGET,d1
		beq.s	.NoStrGad			; No -> go on

		move.w	gg_Flags(a1),d1			; Is it selected ?
		and.w	#GFLG_SELECTED,d1
		bne.s	.NoWindow			; Yep -> No more actions

.NoStrGad	move.l	gg_NextGadget(a1),a1		; Check all gads in window
		bra	.CheckGadgets
.NoWindow	sub.l	a0,a0
.GadgetsChecked	movem.l	(sp)+,d1/a1
		rts


**********************************************************************
*            Signal the timer process to remake the time             *
**********************************************************************
SigTime		move.l	CheckerProc(a4),a1
		move.l	#SIGTIMEREMAKE,d0
		CALLSYS	Signal
		rts


**********************************************************************
*                          Display new time                          *
**********************************************************************
SetTime		lea	MyDateTime(a4),a0		; Get datestamp
		move.l	a0,d1
		RELLIB	DateStamp,_DOSBase

		lea	MyDateTime(a4),a0		; Do the string formatings
		clr.b	dat_Flags(a0)
		lea	MyDateTime(a4),a0		; Check for englich/german format
		cmp.w	#4,DateFormat(a4)
		blt.s	.NoDos
		move.b	#FORMAT_DOS,dat_Format(a0)
		bra.s	.GotFormat
.NoDos		tst.w	DateFormat(a4)
		beq.s	.English
		cmp.w	#2,DateFormat(a4)
		beq.s	.English
		move.b	#FORMAT_CDN,dat_Format(a0)
		bra.s	.GotFormat
.English	move.b	#FORMAT_USA,dat_Format(a0)

.GotFormat	clr.b	AllDateString
		lea	AllDayString(pc),a1
		move.l	a1,dat_StrDay(a0)		; First create the whole string
		lea	AllDateString(pc),a1
		move.l	a1,dat_StrDate(a0)		; for environment variables
		lea	AllTimeString(pc),a1
		move.l	a1,dat_StrTime(a0)
		move.l	a0,d1
		DOLIB	DateToStr

		lea	AllDateString(pc),a0
		bsr	CheckPoints

		tst.b	SetEnv(a4)			; Write env vars
		beq.s	NoEnvTime

		lea	EnvDayString(pc),a1
		move.l	a1,d1				; set env vars
		lea	AllDayString(pc),a1
		move.l	a1,d2
		moveq	#-1,d3
		move.l	#GVF_GLOBAL_ONLY,d4
		DOLIB	SetVar
		tst.l	d0
		beq.s	.Error

		lea	EnvDateString(pc),a1
		move.l	a1,d1
		lea	AllDateString(pc),a1
		move.l	a1,d2
		moveq	#-1,d3
		move.l	#GVF_GLOBAL_ONLY,d4
		DOLIB	SetVar
		tst.l	d0
		beq.s	.Error

		lea	EnvTimeString(pc),a1
		move.l	a1,d1
		lea	AllTimeString(pc),a1
		move.l	a1,d2
		moveq	#-1,d3
		move.l	#GVF_GLOBAL_ONLY,d4
		DOLIB	SetVar
		tst.l	d0
		bne.s	NoEnvTime

.Error		clr.b	SetEnv(a4)		; Disable setenv if errors
NoEnvTime	clr.b	DayString		; clear previous strings
		clr.b	DateString
		clr.b	TimeString

		lea	MyDateTime(a4),a0
		tst.b	ShowDay(a4)		; Now create the rest for clock
		beq.s	.Loop3			; display
		lea	DayString(pc),a1
		move.l	a1,dat_StrDay(a0)
		bra.s	.Loop4
.Loop3		clr.l	dat_StrDay(a0)

.Loop4		tst.b	ShowDate(a4)		; Are we to show a date ?
		beq.s	.Loop5
		lea	DateString(pc),a1
		move.l	a1,dat_StrDate(a0)	; Yep
		bra.s	.Loop6
.Loop5		clr.l	dat_StrDate(a0)		; Nope
.Loop6		tst.b	DoShowTime(a4)		; Show time ?
		beq.s	.Loop6a
		lea	TimeString(pc),a1
		move.l	a1,dat_StrTime(a0)
		bra.s	.Loop6b
.Loop6a		clr.l	dat_StrTime(a0)
.Loop6b		move.l	a0,d1
		DOLIB	DateToStr
		tst.l	d0
		beq	NoTime
		lea	DateString(pc),a0
		bsr	CheckPoints

		bsr	CheckForAlarm			; Are we to alarm ?

		tst.b	ShowClock(a4)			; Show clock ?
		beq	NoTime

		cmp.l	#TIMEPAGE,PageNum(a4)		; Display normal time ?
		bgt	NoClockDisp

		lea	TimeFormats,a0			; Change to different
		tst.b	ShortDay(a4)			; time format strings
		beq.s	.Loop7
		addq.l	#8,a0				; Use the ones with long days
.Loop7		tst.b	ShowSecs(a4)
		bne.s	.Loop8
		addq.l	#4,a0				; use the one without secs
.Loop8		move.l	(a0),a0
		lea	FormatData(pc),a1
		lea	PutChProc(pc),a2
		lea	FinalString(a4),a3
		CALLSYS	RawDoFmt

		tst.b	ShowMouse(a4)			; Show Mouse positions ?
		beq.s	.NoMousePos
		link	a5,#-(4+30)
		moveq	#0,d0
		RELLIB	LockIBase,_IntuitionBase
		move.l	d0,d7
		move.l	ib_ActiveScreen(a6),a0
		move.w	sc_MouseX(a0),-4(a5)
		move.w	sc_MouseY(a0),-2(a5)
		move.l	d7,a0
		DOLIB	UnlockIBase
		lea	MousePosForm,a0
		lea	-4(a5),a1
		lea	PutChProc(pc),a2
		lea	-(4+30)(a5),a3
		CALLSYS	RawDoFmt
		lea	-(4+30)(a5),a0
		lea	FinalString(a4),a1
		bsr	StrCat
		unlk	a5
.NoMousePos	bsr	MakeDisplay			; Display the stuff
		bra	NoTime

NoClockDisp	cmp.l	#DMEMPAGE,PageNum(a4)		; Display Chip/Fast mem seperately ?
		bne.s	NoDoubleMem

		move.l	#MEMF_CHIP,d1			; Get available memory
		CALLSYS	AvailMem
		move.l	d0,ChipMem
		move.l	#MEMF_FAST,d1
		DOLIB	AvailMem
		move.l	d0,FastMem

		lea	FormMemString,a0		; Format string
		lea	ChipMem,a1
		lea	PutChProc(pc),a2
		lea	FinalString(a4),a3
		DOLIB	RawDoFmt

		bsr	MakeDisplay			; Display
		bra	NoTime

NoDoubleMem	cmp.l	#TMEMPAGE,PageNum(a4)		; Display total memory ?
		bne.s	NoTotalMem

		move.l	#MEMF_CHIP,d1			; Calc total memory
		CALLSYS	AvailMem
		move.l	d0,d7
		move.l	#MEMF_FAST,d1
		DOLIB	AvailMem
		add.l	d0,d7
		move.l	d7,TotalMem

		LOCLEA	FormTMemString,a0
		lea	TotalMem,a1
		lea	PutChProc(pc),a2
		lea	FinalString(a4),a3
		DOLIB	RawDoFmt

		bsr	MakeDisplay
		bra	NoTime

NoTotalMem	cmp.l	#TIMEMEMPAGE,PageNum(a4)	; Display time and memory ?
		bne	NoDoublePage

		lea	TimeFormats,a0			; Change to different
		tst.b	ShortDay(a4)			; time format strings
		beq.s	.Loop7
		addq.l	#8,a0				; Use the ones with long days
.Loop7		tst.b	ShowSecs(a4)
		bne.s	.Loop8
		addq.l	#4,a0				; use the one without secs
.Loop8		move.l	(a0),a0
		lea	FormatData(pc),a1
		lea	PutChProc(pc),a2
		lea	FinalString(a4),a3
		CALLSYS	RawDoFmt

		move.l	#MEMF_CHIP,d1			; Get available memory
		CALLSYS	AvailMem
		lsr.l	#8,d0
		lsr.l	#2,d0
		move.l	d0,ChipMem
		move.l	#MEMF_FAST,d1
		DOLIB	AvailMem
		lsr.l	#8,d0
		lsr.l	#2,d0
		move.l	d0,FastMem

		link	a5,#-80

		lea	DoubleMemString,a0		; Format string
		lea	ChipMem,a1
		lea	PutChProc(pc),a2
		lea	-80(a5),a3
		DOLIB	RawDoFmt

		lea	-80(a5),a0
		lea	FinalString(a4),a1
		bsr	StrCat
		unlk	a5

		bsr	MakeDisplay
		bra	NoTime

NoDoublePage	cmp.l	#GMEMPAGE,PageNum(a4)		; Display fillgauge
		bne	NoGfxMem

		link	a5,#-4
		bsr	LockScreen
		tst.l	d0
		bne.s	.GoOn
		clr.l	PageNum(a4)
		bra	NoGfxMem

.GoOn		move.l	MyRastPort(a4),a1
		move.l	TextBackPen(a4),d0
		RELLIB	SetRast,_GfxBase

		move.l	#MEMF_CHIP,d1			; Calc total memory
		CALLSYS	AvailMem
		move.l	d0,d7
		move.l	#MEMF_FAST,d1
		DOLIB	AvailMem
		add.l	d7,d0
		move.l	MaxMem(a4),d7
		sub.l	d0,d7

		move.l	MyRastPort(a4),a1		; Get length of E and F
		LOCLEA	EString(pc),a0			; chars
		moveq	#1,d0
		RELLIB	TextLength,_GfxBase
		move.l	d0,EWidth(a4)
		move.l	d0,d5
		move.l	MyRastPort(a4),a1
		LOCLEA	FString(pc),a0
		moveq	#1,d0
		DOLIB	TextLength
		move.l	d0,FWidth(a4)
		add.l	d5,d0				; d0 length of both
		add.l	MaxBarLen(a4),d0
		addq.l	#4,d0
		move.l	d0,BlitWidth(a4)
		moveq	#0,d2
		move.l	-4(a5),a0
		move.w	sc_Width(a0),d2
		sub.l	#DEPTHWIDTH,d2
		sub.l	d0,d2
		move.l	d2,LeftEdge(a4)
		move.l	MaxBarLen(a4),d0		; Get maximum length of bar
		move.l	d7,d1
		RELLIB	UMult32,_UtilityBase
		move.l	MaxMem(a4),d1
		DOLIB	UDivMod32
		move.l	d0,d5				; D5 = Length of gauge

		move.l	MyRastPort(a4),a1
		move.l	TextFrontPen(a4),d0
		RELLIB	SetAPen,_GfxBase

		move.l	MyRastPort(a4),a1		; Write E
		moveq	#0,d1
		move.l	MyFont(a4),a0
		move.w	tf_Baseline(a0),d1
		moveq	#0,d0
		DOLIB	Move
		move.l	MyRastPort(a4),a1
		LOCLEA	EString(pc),a0
		moveq	#1,d0
		DOLIB	Text

		move.l	MyRastPort(a4),a1		; Draw "non-filled" rec
		move.l	GaugeBackPen(a4),d0
		DOLIB	SetAPen

		move.l	MyRastPort(a4),a1
		move.l	EWidth(a4),d0
		addq.l	#2,d0
		moveq	#0,d1
		move.l	d0,d2
		add.l	MaxBarLen(a4),d2
		moveq	#0,d3
		move.l	MyFont(a4),a0
		move.w	tf_YSize(a0),d3
		subq.l	#1,d3
		DOLIB	RectFill

		move.l	MyRastPort(a4),a1		; Draw gauge
		move.l	GaugeFrontPen(a4),d0
		DOLIB	SetAPen

		move.l	MyRastPort(a4),a1
		move.l	EWidth(a4),d0
		addq.l	#2,d0
		moveq	#0,d1
		move.l	d5,d2
		add.l	d0,d2
		moveq	#0,d3
		move.l	MyFont(a4),a0
		move.w	tf_YSize(a0),d3
		subq.l	#1,d3
		DOLIB	RectFill

		move.l	MyRastPort(a4),a1		; Draw F
		move.l	TextFrontPen(a4),d0
		DOLIB	SetAPen

		move.l	MyRastPort(a4),a1
		move.l	EWidth(a4),d0
		addq.l	#4,d0
		add.l	MaxBarLen(a4),d0
		move.l	MyFont(a4),a0
		moveq	#0,d1
		move.w	tf_Baseline(a0),d1
		DOLIB	Move
		move.l	MyRastPort(a4),a1
		LOCLEA	FString(pc),a0
		moveq	#1,d0
		DOLIB	Text

		move.l	MyRastPort(a4),a1		; Reset color for our RastPort
		moveq	#0,d0
		DOLIB	SetAPen

		tst.b	ClockWin(a4)			; Window active ?
		beq.s	TitleGauge

		move.l	BlitWidth(a4),d6
		bsr	OpenClockWin
		tst.l	d0
		beq	NoTime
		bra.s	.NoClear
.Clear		move.l	ClockHandle(a4),a0
		tst.l	wd_Title(a0)			; Window-Title already cleard ?
		beq.s	.NoClear
		sub.l	a1,a1				; Clear window-title
		suba.l	a2,a2
		subq.w	#1,a2
		RELLIB	SetWindowTitles,_IntuitionBase

.NoClear	move.l	ClockHandle(a4),a1		; Blit gauge into window-title
		move.l	wd_RPort(a1),a1
		move.l	#DEPTHWIDTH,d2
		bra.s	DoGauge

TitleGauge	bsr	CloseClockWin
		move.l	-4(a5),a1			; Blit gauge into title/window
		move.l	sc_BarLayer(a1),a1
		move.l	lr_rp(a1),a1
		move.l	LeftEdge(a4),d2

DoGauge		move.l	MyRastPort(a4),a0
		moveq	#0,d0
		moveq	#0,d1
		moveq	#1,d3
		move.l	BlitWidth(a4),d4
		moveq	#0,d5
		move.w	BitMapHeight(a4),d5
		move.l	#$c0,d6
		RELLIB	ClipBlit,_GfxBase

		bsr	UnlockScreen
		unlk	a5
		bra.s	NoTime

NoGfxMem	cmp.l	#ONLINEPAGE,PageNum(a4)		; Display online-time ?
		bne.s	NoOnlinePage			; Nope -> go on
		LOCLEA	OnlineFormStr(pc),a0		; Copy online string
		lea	FinalString(a4),a1
		bsr	StrCpy
		lea	OnlineString(pc),a0		; Concat time string
		lea	FinalString(a4),a1
		bsr	StrCat
		bsr.s	MakeDisplay
		bra.s	NoTime

NoOnlinePage	cmp.l	#SNAPMEMPAGE,PageNum(a4)	; Display snapped memory ?
		bne.s	NoTime
		LOCLEA	FormSnapMemStr,a0		; Format string
		lea	ThisChip,a1
		lea	PutChProc(pc),a2
		lea	FinalString(a4),a3
		CALLSYS	RawDoFmt
		bsr.s	MakeDisplay			; Display
NoTime		rts
EnvDayString	dc.b	"DAY",0
EnvDateString	dc.b	"DATE",0
EnvTimeString	dc.b	"TIME",0
		EVEN


**********************************************************************
*              Convert "-" within date to "." if needed              *
**********************************************************************
CheckPoints	tst.b	(a0)
		beq.s	.NoFormat
		cmp.w	#4,DateFormat(a4)
		beq.s	.NoFormat
		cmp.w	#5,DateFormat(a4)
		beq.s	.DosPoint
		cmp.w	#1,DateFormat(a4)
		ble.s	.NoFormat
		move.b	#'.',2(a0)
		move.b	#'.',5(a0)
		bra.s	.NoFormat
.DosPoint	move.b	#'.',2(a0)
		move.b	#'.',6(a0)
.NoFormat	rts




**********************************************************************
*                Make up the display for clock/memory                *
**********************************************************************
MakeDisplay	lea	FinalString(a4),a0		; Get length of time string
		bsr	StrLen
		move.l	d0,d7
		move.l	MyRastPort(a4),a1		; Get width of string (pixels)
		RELLIB	TextLength,_GfxBase
		move.l	d0,d6

		tst.b	ClockWin(a4)			; Display in window ?
		beq.s	DisplayInTitle			; Nope -> do it in title

		bsr	OpenClockWin
		tst.l	d0
		beq.s	.NoDisplay

		move.l	ClockHandle(a4),a0		; Set new title of window
		lea	FinalString(a4),a1
		suba.l	a2,a2
		subq.w	#1,a2
		RELLIB	SetWindowTitles,_IntuitionBase
.NoDisplay	rts

DisplayInTitle	link	a5,#-4
		bsr	LockScreen
		tst.l	d0
		beq	NoDisplay

		bsr	CloseClockWin

		move.l	MyRastPort(a4),a1		; Clear rastport
		move.l	TextBackPen(a4),d0
		RELLIB	SetRast,_GfxBase

		move.l	MyRastPort(a4),a1		; Set text pen
		move.l	TextFrontPen(a4),d0
		DOLIB	SetAPen

		moveq	#0,d1				; Move cursor to position
		move.l	MyFont(a4),a0
		move.w	tf_Baseline(a0),d1
		move.l	MyRastPort(a4),a1
		moveq	#0,d0
		DOLIB	Move

		move.l	MyRastPort(a4),a1		; Draw string
		lea	FinalString(a4),a0
		move.l	d7,d0
		DOLIB	Text

		moveq	#0,d2
		move.l	-4(a5),a0
		move.w	sc_Width(a0),d2			; Get width of screen
		sub.l	#DEPTHWIDTH,d2			; Skip depth gadget
		sub.l	d6,d2				; Skip width of string
		move.l	d2,LeftEdge(a4)			; Store new X-Position in TitleBar
		move.l	d6,d4
		add.w	#DEPTHWIDTH,d6
		cmp.w	BitMapWidth(a4),d6		; If width to be blitted is
		ble.s	.End				; > Plane width : Skip some pixels
		move.w	BitMapWidth(a4),d4
		sub.w	#DEPTHWIDTH,d4
.End		move.l	d4,BlitWidth(a4)			; Store width of string

		move.l	MyRastPort(a4),a0		; Blit our rastport into
		moveq	#0,d0				; rastport of layer which
		moveq	#0,d1				; is the titlebar
		move.l	-4(a5),a1
		move.l	sc_BarLayer(a1),a1
		move.l	lr_rp(a1),a1
		move.l	#1,d3
		moveq	#0,d5
		move.w	BitMapHeight(a4),d5
		move.l	#$c0,d6
		DOLIB	ClipBlit

NoDisplay	bsr	UnlockScreen
		unlk	a5
		rts




**********************************************************************
*                   Clears the workbench titlebar                    *
**********************************************************************
ClearTitleBar	tst.l	CheckerProc(a4)
		beq.s	.Error
		link	a5,#-4
		bsr	LockScreen
		tst.l	d0
		beq.s	.End

		RELLIB	WaitTOF,_GfxBase

		move.l	MyRastPort(a4),a1
		move.l	MenuBackPen(a4),d0		; Clear rastport
		DOLIB	SetRast

		move.l	MyRastPort(a4),a0		; Blit
		moveq	#0,d0				; to restore titlebar shape
		moveq	#0,d1
		move.l	-4(a5),a1
		move.l	sc_BarLayer(a1),a1
		move.l	lr_rp(a1),a1
		move.l	LeftEdge(a4),d2
		move.l	#1,d3
		moveq	#0,d5
		move.w	BitMapHeight(a4),d5
		move.l	BlitWidth(a4),d4
		move.l	#$c0,d6
		DOLIB	ClipBlit

.End		bsr	UnlockScreen
		unlk	a5
.Error		rts



**********************************************************************
*                  Load the specified Alarm sample                   *
**********************************************************************
InitAlarmSmp	tst.b	AlarmSample(a4)
		beq	.End
		tst.b	HadASample(a4)
		beq	.End

		SF	SampleLoaded(a4)
		SF	SampleLoaded(a4)

		move.l	#NOIFF_ERR,ErrorBack(a4)	; Open IFF handle
		RELLIB	AllocIFF,_IFFParseBase
		move.l	d0,MyIFFHandle(a4)
		beq	.AlarmError1

		lea	AlSampleName(a4),a0		; Open file
		move.l	a0,d1
		move.l	d1,BackErrArg(a4)
		move.l	#MODE_OLDFILE,d2
		RELLIB	Open,_DOSBase
		move.l	MyIFFHandle(a4),a0
		move.l	d0,iff_Stream(a0)
		beq	.DosAlarmError

		RELLIB	InitIFFasDOS,_IFFParseBase

		move.l	MyIFFHandle(a4),a0		; Open for IFF read
		move.l	#IFFF_READ,d0
		DOLIB	OpenIFF
		tst.l	d0
		bne	.AlarmError

		move.l	MyIFFHandle(a4),a0		; Search for these two
		move.l	#'8SVX',d0			; chunks
		move.l	#'VHDR',d1
		DOLIB	PropChunk
		tst.l	d0
		bne	.AlarmError

		move.l	MyIFFHandle(a4),a0
		move.l	#'8SVX',d0
		move.l	#'BODY',d1
		DOLIB	StopChunk
		tst.l	d0
		bne	.AlarmError

		move.l	MyIFFHandle(a4),a0		; Scan file
		move.l	#IFFPARSE_SCAN,d0
		DOLIB	ParseIFF
		tst.l	d0
		bne	.AlarmError

		move.l	MyIFFHandle(a4),a0		; Check for VHDR
		move.l	#'8SVX',d0
		move.l	#'VHDR',d1
		DOLIB	FindProp
		tst.l	d0
		beq	.AlarmError
		move.l	d0,a0				; Read out VHDR chunk
		move.l	spr_Data(a0),a0
		move.l	oneShotHiSamples(a0),AlarmHiSmp(a4)
		move.l	repeatHiSamples(a0),AlarmHiRep(a4)
		move.l	samplesPerHiCycle(a0),AlarmHiCyc(a4)
		move.w	samplesPerSec(a0),AlarmSmpSec(a4)
		move.b	ctOctave(a0),AlarmOctave(a4)
		move.l	Volume(a0),AlarmVolume(a4)
		move.l	AlarmHiSmp(a4),d0
		add.l	AlarmHiRep(a4),d0
		move.l	d0,AlarmSmpLen(a4)

		move.l	MyIFFHandle(a4),a0		; Check for BODY
		DOLIB	CurrentChunk
		move.l	#MEMORY_ERR,ErrorBack(a4)	; Try to get memory block
		move.l	d0,a3
		move.l	cn_Size(a3),d0
		move.l	#MEMF_CHIP,d1
		CALLSYS	AllocVec
		move.l	d0,AlarmSmpStart(a4)
		beq.s	.AlarmError1

		move.l	MyIFFHandle(a4),a0		; Read sample
		move.l	d0,a1
		move.l	cn_Size(a3),d0
		RELLIB	ReadChunkBytes,_IFFParseBase
		cmp.l	cn_Size(a3),d0
		bne.s	.AlarmError

		move.l	#AUDIOPORT_ERR,ErrorBack(a4)	; Get Message Port
		CALLSYS	CreateMsgPort
		move.l	d0,AlarmPort(a4)
		beq.s	.AlarmError1

		move.l	#AUDIOREQ_ERR,ErrorBack(a4)	; Create Request
		move.l	AlarmPort(a4),a0
		clr.l	LN_NAME(a0)
		clr.b	LN_PRI(a0)
		move.l	#ioa_SIZEOF,d0
		CALLSYS	CreateIORequest
		move.l	d0,AlarmReq(a4)
		beq.s	.AlarmError1

		move.b	#1,SampleLoaded(a4)		; Set flags
		move.b	#1,HadASample(a4)
		bra.s	.EndAlarm

.AlarmError	tst.l	d0
		beq.s	.EndAlarm
		neg.l	d0
		add.l	#400,d0
		move.l	d0,ErrorBack(a4)
.AlarmError1	bsr	DisplayError
		bra.s	.EndAlarm
.DosAlarmError	DOLIB	IoErr
		move.l	d0,ErrorBack(a4)
		bsr	DisplayError
.EndAlarm	tst.l	MyIFFHandle(a4)
		beq.s	.NoHandle
		move.l	MyIFFHandle(a4),a0
		RELLIB	CloseIFF,_IFFParseBase
		move.l	MyIFFHandle(a4),a0
		move.l	iff_Stream(a0),d1
		beq.s	.NoFile
		RELLIB	Close,_DOSBase
.NoFile		move.l	MyIFFHandle(a4),a0
		RELLIB	FreeIFF,_IFFParseBase
.NoHandle
.End		rts



**********************************************************************
*                     Remove Alarm sample stuff                      *
**********************************************************************
RemoveAlarmSmp	tst.l	AlarmSmpStart(a4)
		beq.s	.NoSample
		move.l	AlarmSmpStart(a4),a1
		CALLSYS	FreeVec
.NoSample	tst.l	AlarmPort(a4)
		beq.s	.NoPort
		move.l	AlarmPort(a4),a0
		CALLSYS	DeleteMsgPort
.NoPort		tst.l	AlarmReq(a4)
		beq.s	.NoReq
		move.l	AlarmReq(a4),a0
		CALLSYS	DeleteIORequest
.NoReq		clr.l	AlarmPort(a4)
		clr.l	AlarmReq(a4)
		clr.l	AlarmSmpStart(a4)
		clr.b	SampleLoaded(a4)
		rts



**********************************************************************
*                      Set drive noclick/click                       *
**********************************************************************
SetNoClick	movem.l	RegsAll,-(sp)
		lea	DriveRequests(a4),a3
		moveq	#3,d7
.Loop		move.l	(a3)+,d0
		beq.s	.GoOn
		move.l	d0,a0
		move.l	IO_UNIT(a0),a0
		tst.b	DriveNoClick(a4)
		beq.s	.ClickOFF
		or.b	#TDPF_NOCLICK,TDU_PUBFLAGS(a0)
		bra.s	.GoOn
.ClickOFF	and.b	#~TDPF_NOCLICK,TDU_PUBFLAGS(a0)
.GoOn		dbf	d7,.Loop
		movem.l	(sp)+,RegsAll
		rts






**********************************************************************
*           Routine for keeping up the correct online time           *
**********************************************************************
CheckOnline	tst.b	Online(a4)			; Already online ?
		bne.s	StillOnline

		tst.b	BillRunning(a4)			; Started bill manually ?
		beq.s	.NoStartBill
		ST	NoBillPDown(a4)
		bra.s	.StartBill

.NoStartBill	lea	$bfd000,a5
		btst	#CIAB_COMCD,ciapra(a5)
		bne	NotOnline

.StartBill	move.b	#1,Online(a4)			; Reset flags
		clr.b	DontCheckTime(a4)
		clr.l	OnlineHour
		clr.l	OnlineMinute
		move.l	#5,OnlineSeconds		; We already have a connect of about 3 secs
		move.l	#-5,NextUnit			; Store for next unit calc
		move.l	#5,UnitsDone
		clr.l	TotalCost(a4)
		lea	AllTimeString(pc),a0		; Copy current time and date
		lea	LogInTime(pc),a1
		bsr	StrCpy
		lea	AllDateString(pc),a0
		lea	LogInDate(pc),a1
		bsr	StrCpy
		lea	AllTimeString+6(pc),a0
		lea	CopyTime(pc),a1
		bsr	StrCpy

StillOnline	lea	$bfd000,a5
		btst	#CIAB_COMCD,ciapra(a5)		; Still online ?
		beq	.GoOn

		tst.b	BillRunning(a4)			; User selected manual count ?
		bne	.GoOn

		clr.b	Online(a4)			; Nope....
		clr.b	DontCheckTime(a4)

		tst.b	LogCalls(a4)			; Save log to file ?
		beq	.NoReenable

		cmp.l	#6,UnitsDone			; Skip for extreme short calls (like 3 secs)
		ble	.NoReenable

		move.w	#'N',d0				; Set char for either normal
		tst.b	CheapMode(a4)
		beq.s	.NoCheap
		move.w	#'C',d0				; ... or cheap mode
.NoCheap	move.w	d0,LogEntryTariff
		clr.l	CostMajor			; Calc total costs
		clr.l	CostMinor
		move.l	TotalCost(a4),d0
.CostLoop	cmp.l	#100,d0
		blt.s	.CostEnd
		sub.l	#100,d0
		addq.l	#1,CostMajor
		bra.s	.CostLoop
.CostEnd	move.l	d0,CostMinor

		link	a5,#-LOGENTRYSIZE		; Get Buffer for log-entry

		move.l	TimeZone(a4),d0
		move.l	d0,LogTimeZone

		LOCLEA	LogEntryFormat,a0		; Format log entry
		lea	LogEntryDat,a1
		lea	PutChProc(pc),a2
		lea	-LOGENTRYSIZE(a5),a3
		CALLSYS	RawDoFmt

		lea	LogFileName(a4),a0
		move.l	a0,d1				; Open log file
		move.l	#MODE_READWRITE,d2
		RELLIB	Open,_DOSBase
		tst.l	d0
		beq.s	.NoLogWrite
		move.l	d0,d7
		move.l	d0,d1
		moveq	#0,d2
		move.l	#OFFSET_END,d3			; Seek to end
		DOLIB	Seek
		lea	-LOGENTRYSIZE(a5),a0
		move.l	d7,d1
		move.l	a0,d2				; Write new logentry
		DOLIB	FPuts
		move.l	d7,d1
		DOLIB	Close
.NoLogWrite	unlk	a5

.NoReenable	tst.b	BillPopUp(a4)
		beq.s	.EndLogOff
		tst.b	NoBillPDown(a4)
		bne.s	.EndLogOff
		bsr	CloseBillWin
.EndLogOff	clr.b	NoBillPDown(a4)
		clr.b	BillPopedUp(a4)
		bra	NotOnline

.GoOn		move.b	#1,Online(a4)			; Yep, we`re online

		cmp.l	#6,UnitsDone			; Open Bill window ?
		ble.s	.NoOpen

		tst.b	BillPopUp(a4)
		beq.s	.NoOpen
		tst.b	BillPopedUp(a4)
		bne.s	.NoOpen
		tst.l	BillHandle(a4)
		beq.s	.DoOpen
		move.b	#1,NoBillPDown(a4)
.DoOpen		move.b	#1,BillPopedUp(a4)
		bsr	OpenBillWin

.NoOpen		link	a5,#-4
		lea	-4(a5),a3
		lea	AllTimeString+6(pc),a0		; Get current seconds
		move.l	a0,d1
		move.l	a3,d2
		RELLIB	StrToLong,_DOSBase
		move.l	d1,d7				; Convert to long
		lea	CopyTime(pc),a0			; Get last seconds
		move.l	a0,d1
		move.l	a3,d2
		DOLIB	StrToLong			; Convert to long
		unlk	a5
		moveq	#0,d5
.GetValue	cmp.l	d1,d7				; Got seconds elapsed ?
		beq.s	.GotValue			; Yep -> go on
		addq.l	#1,d5				; One more second done
		addq.l	#1,d1
		cmp.l	#60,d1				; Over 60 boundary ?
		bne.s	.GetValue
		moveq	#0,d1				; Restart from scratch
		bra.s	.GetValue
.GotValue	add.l	d5,OnlineSeconds		; Remake online time
		cmp.l	#60,OnlineSeconds		; One minute done ?
		blt.s	.End				; Nope -> go on
		sub.l	#60,OnlineSeconds
		addq.l	#1,OnlineMinute
		cmp.l	#60,OnlineMinute		; One hour online ? GOSH
		bne.s	.End				; Nope -> go on
		clr.l	OnlineMinute
		addq.l	#1,OnlineHour

.End		lea	AllTimeString+6(pc),a0		; Copy current time
		lea	CopyTime(pc),a1
		bsr	StrCpy

		sub.l	d5,NextUnit
		add.l	d5,UnitsDone
NotOnline
**********************************************************************
*                  Remake texts within Bill window                   *
**********************************************************************
RemakeCosts	tst.b	DontCheckTime(a4)		; Check for time ?
		bne	.NoRemake
		lea	MyDateTime(a4),a0		; Get current datestamp
		move.l	ds_Days(a0),d0			; Calc daynumber
		divu	#7,d0
		clr.w	d0
		swap	d0
		lea	Sunday(a4),a0
		tst.b	0(a0,d0.w)			; Bit for "always cheap" set ?
		beq.s	.NormalDay			; Nope -> normal day
		link	a5,#-12
		moveq	#1,d3				; Set flag and treat as cheap
		bra	.Done

.NormalDay	link	a5,#-12
		lea	MyDateTime(a4),a0		; Get datestamp
		move.l	ds_Minute(a0),d0		; Get current minute
		divu	#60,d0
		moveq	#0,d1
		move.w	d0,d1				; D1 = minutes
		clr.w	d0
		swap	d0				; D0 = hours
		move.l	d1,-8(a5)
		move.l	d0,-4(a5)

		move.l	-8(a5),d2			; Calc number of ten minutes done
		mulu	#6,d2
		and.l	#$0000ffff,d2
		move.l	-4(a5),d3
		divu	#10,d3
		and.l	#$0000ffff,d3
		addq.l	#1,d3
		add.l	d3,d2				; d2 = Current time

		move.l	CheapStart(a4),d0		; Check for normal/cheap time
		move.l	CheapStop(a4),d1

		cmp.l	d0,d2
		blt.s	.NoCheap1
		cmp.l	d1,d2
		bgt.s	.NoCheap1
		moveq	#1,d3
		bra.s	.Done
.NoCheap1	cmp.l	d1,d2
		blt.s	.NoCheap2
		cmp.l	d0,d2
		bgt.s	.NoCheap2
		moveq	#0,d3
		bra.s	.Done
.NoCheap2	cmp.l	d0,d2
		blt.s	.NoCheap3
		cmp.l	d1,d2
		blt.s	.NoCheap3
		cmp.l	d0,d1
		bgt.s	.NoCheap4
		moveq	#1,d3
		bra.s	.Done
.NoCheap4	moveq	#0,d3
		bra.s	.Done
.NoCheap3	cmp.l	d0,d2
		bgt.s	.Done
		cmp.l	d1,d2
		bgt.s	.Done
		cmp.l	d0,d1
		bgt.s	.NoCheap6
		moveq	#1,d3
		bra.s	.Done
.NoCheap6	moveq	#0,d3
.Done		cmp.b	CheapMode(a4),d3
		beq.s	.TimeDone
		move.b	d3,CheapMode(a4)
		move.l	#GD_BillCheap,d0
		move.l	d3,d1
		lea	BillGads(a4),a0
		move.l	BillHandle(a4),a1
		RELLIB	SetMXGad,_KCXBase
		clr.b	DoRemakeCosts(a4)
.TimeDone	unlk	a5

.NoRemake	tst.b	Online(a4)			; Are we online ?
		beq.s	.NotOnline
		tst.l	NextUnit			; Next unit
		bgt.s	.NotOnline			; Nope...
		move.l	NextUnit,d1
		neg.l	d1
		move.l	TimeZone(a4),d0			; Get timezone
		lsl.l	#3,d0
		lea	CostUnit1(a4),a0
		tst.b	CheapMode(a4)
		beq.s	.Normal
		move.l	cu_Cheap(a0,d0),d0		; Get costs for cheap mode
		bra.s	.GoOn
.Normal		move.l	cu_Normal(a0,d0),d0		; or for normal mode

.GoOn		move.l	d0,d2
		sub.l	d1,d0				; Skip "over" seconds
		move.l	d0,NextUnit			; Store for next unit calc

		tst.l	d2
		bgt.s	.DIV0
		move.l	#23,TotalCost(a4)
		bra.s	.NoNewCost1
.DIV0		move.l	CostUnit(a4),d0			; Remake costs
		add.l	d0,TotalCost(a4)
.NoNewCost1	bsr	DisplayCost

.NotOnline	tst.b	DoRemakeCosts(a4)
		beq.s	.NoCostRemake
		clr.b	DoRemakeCosts(a4)
		move.l	TimeZone(a4),d0			; Changed time/cheap mode ?
		lea	CostUnit1(a4),a0
		lsl.l	#3,d0
		tst.b	CheapMode(a4)
		beq.s	.RemNormal
		move.l	cu_Cheap(a0,d0),d0		; Get new time
		bra.s	.GoOnRemake
.RemNormal	move.l	cu_Normal(a0,d0),d0
.GoOnRemake	tst.l	d0				; Check for infinite costs
		bgt.s	.DIV1
		move.l	CostUnit(a4),d2
		bra.s	.NoInitial2
.DIV1		move.l	UnitsDone(pc),d2
		divu	d0,d2
		and.l	#$0000ffff,d2
		tst.b	Online(a4)
		beq.s	.NoInitial
		tst.l	TotalCost(a4)
		beq.s	.NoInitial
		addq.l	#1,d2				; D2 = Number of units done for new time
.NoInitial	move.l	CostUnit(a4),d3
		mulu	d3,d2				; Get new costs for new time
.NoInitial2	move.l	d2,TotalCost(a4)		; Store costs
		tst.l	d0				; Infinite costs
		bgt.s	.DIV2
		move.l	#1,NextUnit			; Store calc for next unit
		bra.s	.NoDIV2
.DIV2		move.l	UnitsDone(pc),d2		; Units done
		divu	d0,d2				; Calc overlap for next unit
		clr.w	d2
		swap	d2
		sub.l	d2,d0				; D0 = Event for next unit
		bge.s	.NoSubUnit			; On negative value
		neg.l	d0				; make it positive
.NoSubUnit	move.l	d0,NextUnit
.NoDIV2		bsr	DisplayCost
.NoCostRemake	clr.l	CostMajor
		clr.l	CostMinor
		move.l	TimeZone(a4),d0			; Calc total costs
		lsl.l	#3,d0
		lea	CostUnit1(a4),a0
		lea	0(a0,d0),a0
		tst.b	CheapMode(a4)
		beq.s	.StillNormal
		move.l	cu_Cheap(a0),d1			; either for cheap mode
		bra.s	.StillCheap
.StillNormal	move.l	cu_Normal(a0),d1		; or for normal mode
.StillCheap	tst.l	d1				; Check for infinite costs
		bgt.s	.DIV3
		move.l	CostUnit(a4),d0
		bra.s	.TotLoop
.DIV3		move.l	#3600,d0
		divu	d1,d0
		and.l	#$0000ffff,d0			; d0 = Number of units/hour
		move.l	CostUnit(a4),d1
		mulu	d1,d0				; d0 = costs/hour
.TotLoop	cmp.l	#100,d0				; convert value
		blt.s	.EndLoop
		sub.l	#100,d0
		addq.l	#1,CostMajor
		bra.s	.TotLoop
.EndLoop	move.l	d0,CostMinor

		lea	FormLongSStr,a0			; Create Onlinetime
		lea	OnlineHour,a1
		lea	PutChProc(pc),a2
		lea	OnlineString(pc),a3
		CALLSYS	RawDoFmt

		tst.l	BillHandle(a4)
		beq	NoCostCalc
		tst.b	Zipped(a4)
		bne.s	.ZippedTitle

		LOCLEA	PerHourStr,a0
		lea	PerHourBuff(a4),a1
		bsr	StrCpy
		subq.l	#1,a1
		move.l	a1,a3
		lea	PerHourForm,a0			; Convert values to string
		lea	CostMajor,a1
		lea	PutChProc(pc),a2
		DOLIB	RawDoFmt

		move.l	#GD_BillTime,d0			; Set text gadgets
		lea	BillGads(a4),a0
		move.l	BillHandle(a4),a1
		lea	AllTimeString(pc),a2
		RELLIB	SetTextGad,_KCXBase

		move.l	#GD_BillOnline,d0
		lea	OnlineString(pc),a2
		DOLIB	SetTextGad

		move.l	#GD_BillCost,d0
		lea	CostString(pc),a2
		DOLIB	SetTextGad

		move.l	#GD_BillHour,d0
		lea	PerHourBuff(a4),a2
		DOLIB	SetTextGad
		bra	NoCostCalc

.ZippedTitle	lea	OnlineString(pc),a0		; IF window is zipped, create
		lea	FinalString2(a4),a1		; appropriate title
		bsr	StrCpy
		lea	CostString(pc),a0
		lea	FinalString2(a4),a1
		bsr	StrCat

		link	a5,#-6
		lea	-6(a5),a0
		move.b	#',',(a0)
		move.l	TimeZone(a4),d0
		add.b	#'1',d0
		move.b	d0,1(a0)
		tst.b	CheapMode(a4)
		beq.s	.NoCheap
		LOCLEA	CheapChar,a1
		bra.s	.EndZone
.NoCheap	LOCLEA	NormChar,a1
.EndZone	move.b	(a1),2(a0)
		tst.b	LogCalls(a4)
		beq.s	.NoLog
		move.l	a0,-(sp)
		LOCLEA	LogCallText,a0
		bsr	LocalizeGadKey
		move.l	(sp)+,a0
		move.b	d1,3(a0)
		bra.s	.EndLog
.NoLog		move.b	#' ',3(a0)
.EndLog		clr.b	4(a0)
		lea	FinalString2(a4),a1
		bsr	StrCat
		unlk	a5

		move.l	BillHandle(a4),a0		; Display / OnlineString
		lea	FinalString2(a4),a1
		suba.l	a2,a2
		subq.w	#1,a2
		RELLIB	SetWindowTitles,_IntuitionBase
NoCostCalc	rts



**********************************************************************
*                Display new costs of telephone call                 *
**********************************************************************
DisplayCost	move.l	TotalCost(a4),d0
		clr.l	CostMajor
		clr.l	CostMinor

.Loop		cmp.l	#100,d0				; Calc costs
		blt.s	.LoopEnd
		sub.l	#100,d0
		addq.l	#1,CostMajor
		bra.s	.Loop
.LoopEnd	move.l	d0,CostMinor

		lea	CostFormString,a0		; Convert values
		lea	CostMajor,a1
		lea	PutChProc(pc),a2
		lea	CostString,a3
		CALLSYS	RawDoFmt
		rts


**********************************************************************
*                      Check if we`re to alarm                       *
**********************************************************************
CheckForAlarm	tst.b	DoAlarm(a4)			; Are we to alarm ?
		beq.s	NoAlarm

		tst.b	EveryHour(a4)
		beq.s	.NoEveryHour
		link	a5,#-12
		lea	AllTimeString(pc),a0
		lea	-4(a5),a1
		move.b	(a0)+,(a1)
		move.b	(a0)+,1(a1)
		clr.b	2(a1)
		clr.b	3(a1)
		move.l	a1,d1
		lea	-12(a5),a0
		move.l	a0,d2
		RELLIB	StrToLong,_DOSBase
		move.l	-12(a5),d0
		unlk	a5
		tst.l	ThisHour(a4)
		bge.s	.GoOn
		move.l	d0,ThisHour(a4)
		bra.s	.NoEveryHour
.GoOn		move.l	ThisHour(a4),d1
		move.l	d0,ThisHour(a4)
		cmp.l	d0,d1
		bne.s	.DoAlarm
.NoEveryHour	lea	AllTimeString(pc),a0
		bsr.s	GetAlarmCode
		cmp.l	AlarmTime(a4),d0
		bne.s	NoAlarm
.DoAlarm	lea	AllTimeString(pc),a0
		cmp.b	#'0',6(a0)
		bne.s	NoAlarm
		cmp.b	#'0',7(a0)
		bne.s	NoAlarm
		move.l	BackTask(a4),a1			; Signal mother to alarm
		move.l	AlarmSigFlag(a4),d0
		CALLSYS	Signal
NoAlarm		rts


**********************************************************************
*                         Get number of time                         *
**********************************************************************
GetAlarmCode	link	a5,#-(8+LEN_DATSTRING)
		lea	-(8+LEN_DATSTRING)(a5),a1
		bsr	StrCpy
		lea	-(8+LEN_DATSTRING)(a5),a3
		clr.b	2(a3)
		clr.b	5(a3)
		move.l	a3,d1
		lea	-4(a5),a0
		move.l	a0,d2
		RELLIB	StrToLong,_DOSBase
		tst.l	d0
		ble.s	.Error
		move.l	a3,d1
		addq.l	#3,d1
		lea	-8(a5),a0
		move.l	a0,d2
		RELLIB	StrToLong,_DOSBase
		tst.l	d0
		ble.s	.Error
		move.l	-4(a5),d0
		mulu	#60,d0
		add.l	-8(a5),d0
		unlk	a5
		rts
.Error		moveq	#-1,d0
		unlk	a5
		rts



**********************************************************************
*                        Do the full dragging                        *
**********************************************************************
DoDragging	movem.l	RegsAll,-(sp)

		link	a5,#-38
		RELLIB	FindWindow,_KCXBase		; Get window underneith mouse
		tst.l	d0
		beq	.NoDrag
		move.l	d0,a0
		tst.l	lr_Window(a0)
		beq	.NoDrag
		move.l	lr_Window(a0),a0		; Window equipped with a dragbar ?
		move.l	wd_Flags(a0),d0
		and.l	#WFLG_DRAGBAR,d0
		beq	.NoDrag
		move.l	a0,-4(a5)			; Store pointer to window
		move.l	wd_WScreen(a0),-8(a5)		; Store Pointer to screen

		move.l	-8(a5),a0			; Set DrawMode
		lea	sc_RastPort(a0),a1
		move.l	a1,-12(a5)
		moveq	#0,d0				; Store old Drawmode
		move.b	rp_DrawMode(a1),d0
		move.w	d0,-14(a5)
		move.l	#RP_COMPLEMENT,d0
		RELLIB	SetDrMd,_GfxBase

		move.l	-4(a5),a0			; Get Window dimensions
		move.w	wd_LeftEdge(a0),-16(a5)
		move.w	wd_TopEdge(a0),-18(a5)
		move.w	wd_Width(a0),-20(a5)
		move.w	wd_Height(a0),-22(a5)
		subq.w	#1,-20(a5)
		subq.w	#1,-22(a5)

		move.l	-8(a5),a0			; Get Screen dimensions
		move.w	sc_Width(a0),d0
		sub.w	-20(a5),d0
		subq.w	#1,d0
		move.w	d0,-24(a5)
		move.w	sc_Height(a0),d0
		sub.w	-22(a5),d0
		subq.w	#1,d0
		move.w	d0,-26(a5)

		move.l	-8(a5),a1			; Get ViewModes to check how to move
		bsr	GetCoords			; Get current mouse coordinates
		move.w	d2,-28(a5)
		move.w	d3,-30(a5)
		move.w	d2,-32(a5)			; Double position
		move.w	d3,-34(a5)
		sub.w	-16(a5),d2
		sub.w	-18(a5),d3
		move.w	d2,-16(a5)
		move.w	d3,-18(a5)

		move.b	#1,ImDragging(a4)
		bsr	DrawWndRec			; Draw first rectangle
.DragLoop	move.l	#SIGLBUTTON|SIGMOUSE,d0
		CALLSYS	Wait
		and.l	#SIGLBUTTON,d0
		bne.s	.DragLoopEnd
		move.l	-8(a5),a1			; Get ViewModes to check how to move
		bsr	GetCoords			; Get mouse coordinates
		cmp.w	-28(a5),d2			; Position changed ?
		bne.s	.NewPos
		cmp.w	-30(a5),d3
		beq.s	.DragLoop
.NewPos		bsr.s	DrawWndRec			; Clear last rec
		move.w	d2,-28(a5)			; Store new positions
		move.w	d3,-30(a5)
		bsr.s	DrawWndRec			; Draw new rec
		bra.s	.DragLoop
.DragLoopEnd	bsr.s	DrawWndRec
		clr.b	ImDragging(a4)

.EndDrag	move.l	-8(a5),a0			; Reset old drawmode
		lea	sc_RastPort(a0),a1
		move.w	-14(a5),d0
		RELLIB	SetDrMd,_GfxBase

		move.w	-28(a5),d0			; Mouse position changed ?
		move.w	-30(a5),d1
		cmp.w	-32(a5),d0
		bne.s	.NewWinPos
		cmp.w	-34(a5),d1
		bne.s	.NewWinPos
.NewWinPos	move.l	-4(a5),a0			; Yep
		bsr	TestPosition			; Get new left/top edge
		move.w	d4,d0
		move.w	d5,d1
		move.w	wd_Width(a0),d2			; Get width
		move.w	wd_Height(a0),d3
		RELLIB	ChangeWindowBox,_IntuitionBase	; Move window
.NoDrag		unlk	a5
		movem.l	(sp)+,RegsAll
		rts



**********************************************************************
*                    Draw the window`s rectangle                     *
**********************************************************************
DrawWndRec	move.l	-8(a5),a0			; Lock layers
		lea	sc_LayerInfo(a0),a0
		RELLIB	LockLayers,_LayersBase
		bsr.s	TestPosition
		move.l	-12(a5),a1
		move.w	d4,d0
		move.w	d5,d1
		RELLIB	Move,_GfxBase

		move.w	-20(a5),d0
		moveq	#0,d1
		bsr.s	.DoDraw

		move.w	-20(a5),d0
		move.w	-22(a5),d1
		bsr.s	.DoDraw

		moveq	#0,d0
		move.w	-22(a5),d1
		bsr.s	.DoDraw

		moveq	#0,d0
		moveq	#0,d1
		bsr.s	.DoDraw

		move.l	-8(a5),a0			; Unlock layers again
		lea	sc_LayerInfo(a0),a0
		RELLIB	UnlockLayers,_LayersBase
		rts

.DoDraw		move.l	-12(a5),a1
		add.w	d4,d0
		add.w	d5,d1
		jmp	_LVODraw(a6)


**********************************************************************
*                      Get correct coordinates                       *
**********************************************************************
GetCoords	move.l	a1,-(sp)
		moveq	#0,d0
		RELLIB	LockIBase,_IntuitionBase
		move.l	d0,d5
		move.l	(sp)+,a1
		move.l	ib_ActiveScreen(a6),a0
		moveq	#0,d2
		moveq	#0,d3
		move.w	sc_MouseX(a0),d2
		move.w	sc_MouseY(a0),d3
		movem.l	RegsAll,-(sp)
		move.l	d5,a0
		DOLIB	UnlockIBase
		movem.l	(sp)+,RegsAll
		rts


**********************************************************************
*                     Test the current positions                     *
**********************************************************************
TestPosition	move.w	-28(a5),d4
		sub.w	-16(a5),d4
		bge.s	.XBig
		moveq	#0,d4
		bra.s	.CheckY
.XBig		cmp.w	-24(a5),d4
		ble.s	.CheckY
		move.w	-24(a5),d4
.CheckY		move.w	-30(a5),d5
		sub.w	-18(a5),d5
		bge.s	.YBig
		moveq	#0,d5
		bra.s	.End
.YBig		cmp.w	-26(a5),d5
		ble.s	.End
		move.w	-26(a5),d5
.End		rts



**********************************************************************
*          This is the routine for handleing ARexx command           *
**********************************************************************
GD_WindowAct                           EQU    0
GD_SunMouse                            EQU    1
GD_WriteEnv                            EQU    0
GD_ScrTime                             EQU    1
GD_MouseTime                           EQU    2
GD_CycleOn                             EQU    0
GD_PopUpBill                           EQU    9
GD_ESCClose                            EQU    0
GD_MapUmlauts                          EQU    2
GD_FKeyAct                             EQU    11
GD_MenuWrap                            EQU    6
HandleARexx	move.l	ARexxPort(a4),a0		; Get ARexx msg
		CALLSYS	GetMsg
		move.l	d0,MyARexxMsg(a4)
		beq	AllARexxDone
		move.l	d0,a0

		move.l	ACTION(a0),d0			; Get action to be taken
		move.l	d0,ARexxAction(a4)
		move.l	ARG0(a0),ARexxCommand(a4)

		move.l	#RC_OK,AResult1(a4)
		clr.l	AResult2(a4)

		and.l	#RXCOMM,d0			; Is it a command ?
		beq	ARexxCommDone

		lea	ActivateStr(pc),a1		; Window activation ?
		bsr	CheckARexxComm
		bne.s	ANoActivate

		bsr	CheckABool
		tst.l	d0
		blt	ACommFail
		move.b	d0,DoActivate(a4)
		CALLSYS	Forbid
		move.l	#GD_WindowAct,d1
		lea	MouseGads(a4),a0
		move.l	MouseHandle(a4),a1
		RELLIB	SetCheckBox,_KCXBase
		CALLSYS	Permit
		bra	ARexxCommDone

ANoActivate	lea	MouseStr(pc),a1			; Sunmouse ?
		bsr	CheckARexxComm
		bne.s	ANoSunMouse

		bsr	CheckABool
		tst.l	d0
		blt	ACommFail
		move.b	d0,SunMouse(a4)
		CALLSYS	Forbid
		move.l	#GD_SunMouse,d1
		lea	MouseGads(a4),a0
		move.l	MouseHandle(a4),a1
		RELLIB	SetCheckBox,_KCXBase
		CALLSYS	Permit
		bra	ARexxCommDone

ANoSunMouse	lea	MenuWrapStr(pc),a1		; On/Off MenuWrap ?
		bsr	CheckARexxComm
		bne.s	ANoMenuWrap

		bsr	CheckABool
		tst.l	d0
		blt	ACommFail
		move.b	d0,MenuWrap(a4)
		CALLSYS	Forbid
		move.l	#GD_MenuWrap,d1
		lea	SetGads(a4),a0
		move.l	SetHandle(a4),a1
		RELLIB	SetCheckBox,_KCXBase
		CALLSYS	Permit
		bra	ARexxCommDone

ANoMenuWrap	lea	PageNumStr(pc),a1		; New pagenumber ?
		bsr	CheckARexxComm
		bne.s	ANoPageNum

		move.l	PageNum(a4),d7
		move.l	a0,d1
		lea	PageNum(a4),a0
		move.l	a0,d2
		RELLIB	StrToLong,_DOSBase
		tst.l	d0
		ble.s	.Error
		tst.l	PageNum(a4)
		blt.s	.Error
		cmp.l	#MAXPAGES,PageNum(a4)
		blt.s	.End
.Error		clr.l	PageNum(a4)
.End		move.l	PageNum(a4),d0
		move.l	d0,OldPageNum(a4)
		CALLSYS	Forbid
		bsr	ClearTitleBar
		bsr	SigTime
		CALLSYS	Permit
		bra	ARexxCommDone

ANoPageNum	lea	SetEnvStr(pc),a1		; Write env-vars?
		bsr	CheckARexxComm
		bne.s	ANoSetEnv

		bsr	CheckABool			; Bool flag ???
		tst.l	d0
		blt	ACommFail
		move.b	d0,SetEnv(a4)
		tst.b	SetEnv(a4)
		bne.s	.NoDel
		bsr	DeleteEnvs			; Delete vars if setenv is off
.NoDel		CALLSYS	Forbid
		bsr	SigTime				; Set the time
		move.b	SetEnv(a4),d0
		move.l	#GD_WriteEnv,d1
		lea	SetGads(a4),a0
		move.l	SetHandle(a4),a1
		RELLIB	SetCheckBox,_KCXBase
		CALLSYS	Permit
		bra	ARexxCommDone

ANoSetEnv	lea	ScreenTStr(pc),a1		; New screen time ?
		bsr	CheckARexxComm
		bne.s	ANoScreenTime
		move.l	ScreenTime(a4),d7
		move.l	ARexxBuffer(a4),a1
		bsr	StrCpy
		move.l	ARexxBuffer(a4),d1
		lea	ScreenTime(a4),a0
		move.l	a0,d2
		RELLIB	StrToLong,_DOSBase		; Get new time
		tst.l	ScreenTime(a4)			; negative ?
		blt.s	.Error				; disable blank
		tst.l	d0
		bge.s	.GoOn
.Error		move.l	d7,ScreenTime(a4)
.GoOn		move.b	#1,TimeReset(a4)
		CALLSYS	Forbid
		move.l	#GD_ScrTime,d0
		move.l	ScreenTime(a4),d1
		lea	BlankGads(a4),a0
		move.l	BlankHandle(a4),a1
		RELLIB	SetCheckBox,_KCXBase
		CALLSYS	Permit
		bra	ARexxCommDone

ANoScreenTime	lea	MouseTStr(pc),a1		; New mouse time ?
		bsr	CheckARexxComm
		bne.s	ANoMouseTime
		move.l	MouseTime(a4),d7
		move.l	ARexxBuffer(a4),a1
		bsr	StrCpy
		move.l	ARexxBuffer(a4),d1
		lea	MouseTime(a4),a0
		move.l	a0,d2
		RELLIB	StrToLong,_DOSBase		; Get new time
		tst.l	MouseTime(a4)			; Negative -> disable blank
		blt.s	.Error
		tst.l	d0
		bge.s	.GoOn
.Error		move.l	d7,MouseTime(a4)
.GoOn		move.b	#1,TimeReset(a4)
		CALLSYS	Forbid
		move.l	#GD_MouseTime,d0
		move.l	MouseTime(a4),d1
		lea	BlankGads(a4),a0
		move.l	BlankHandle(a4),a1
		RELLIB	SetCheckBox,_KCXBase
		CALLSYS	Permit
		bra	ARexxCommDone

ANoMouseTime	lea	CycleWStr(pc),a1		; Cycling enabled ?
		bsr	CheckARexxComm
		bne.s	ANoCycleWin

		bsr	CheckABool
		tst.l	d0
		blt	ACommFail
		move.b	d0,CycleWin(a4)
		CALLSYS	Forbid
		move.l	#GD_CycleOn,d1
		lea	CycleGads(a4),a0
		move.l	CycleHandle(a4),a1
		RELLIB	SetCheckBox,_KCXBase
		CALLSYS	Permit
		bra	ARexxCommDone

ANoCycleWin	lea	LogCallStr(pc),a1		; Log calls ??
		bsr	CheckARexxComm
		bne.s	ANoLogCalls

		bsr	CheckABool
		tst.l	d0
		blt	ACommFail
		move.b	d0,LogCalls(a4)			; Set mode
		CALLSYS	Forbid
		move.l	#GD_LogCall,d1
		lea	BillGads(a4),a0
		move.l	BillHandle(a4),a1
		RELLIB	SetCheckBox,_KCXBase
		CALLSYS	Permit
		bra	ARexxCommDone

ANoLogCalls	lea	BillWinStr(pc),a1		; Popup billwindow on carrier detect ?
		bsr	CheckARexxComm
		bne.s	ANoBillPopUp

		bsr	CheckABool
		tst.l	d0
		blt	ACommFail
		move.b	d0,BillPopUp(a4)		; set new state
		CALLSYS	Forbid
		move.l	#GD_PopUpBill,d1
		lea	PrefsGads(a4),a0
		move.l	PrefsHandle(a4),a1
		RELLIB	SetCheckBox,_KCXBase
		CALLSYS	Permit
		bra	ARexxCommDone

ANoBillPopUp	lea	ESCCloseStr(pc),a1		; ESCClose active ?
		bsr	CheckARexxComm
		bne.s	ANoESCClose

		bsr	CheckABool
		tst.l	d0
		blt	ACommFail
		move.b	d0,ESCClose(a4)
		CALLSYS	Forbid
		move.l	#GD_ESCClose,d1
		lea	MiscGads(a4),a0
		move.l	MiscHandle(a4),a1
		RELLIB	SetCheckBox,_KCXBase
		CALLSYS	Permit
		bra	ARexxCommDone

ANoESCClose	lea	UmlautOnStr(pc),a1		; Map Umlauts ?
		bsr	CheckARexxComm
		bne.s	ANoUmlautOn

		bsr	CheckABool
		tst.l	d0
		blt	ACommFail
		move.b	d0,MapUmlaut(a4)
		CALLSYS	Forbid
		move.l	#GD_MapUmlauts,d1
		lea	SetGads(a4),a0
		move.l	SetHandle(a4),a1
		RELLIB	SetCheckBox,_KCXBase
		CALLSYS	Permit
		bra	ARexxCommDone

ANoUmlautOn	lea	FKeysStr(pc),a1			; Map FKeys ?
		bsr	CheckARexxComm
		bne.s	ANoFKeys

		bsr	CheckABool
		tst.l	d0
		blt	ACommFail
		move.b	d0,FKeysOn(a4)
		CALLSYS	Forbid
		move.l	#GD_FKeyAct,d1
		lea	FKeyGads(a4),a0
		move.l	FKeyHandle(a4),a1
		RELLIB	SetCheckBox,_KCXBase
		CALLSYS	Permit
		bra	ARexxCommDone

ANoFKeys	lea	EnableStr(pc),a1		; Enable ?
		bsr	CheckARexxComm
		bne.s	ANoEnable
		clr.b	Disabled(a4)
		move.b	#1,TimeReset(a4)
		bsr	EnableBroker
		bra	ARexxCommDone

ANoEnable	lea	DisableStr(pc),a1		; Disable ?
		bsr	CheckARexxComm
		bne.s	ANoDisable
		bsr	DisableBroker
		CALLSYS	Forbid
		bsr	OnScreen
		CALLSYS	Permit
		bsr	OnMouse
		move.b	#1,Disabled(a4)
		bra	ARexxCommDone

ANoDisable	lea	AReviseStr(pc),a1		; Do revision ?
		bsr	CheckARexxComm
		bne	ANoRevise
		move.l	ARexxBuffer(a4),a1
		bsr	StrCpy
		move.l	ARexxBuffer(a4),a1
		move.l	a1,a0
.Loop		tst.b	(a1)				; Get filename
		beq	ACommFail
		cmp.b	#' ',(a1)
		beq.s	GotFileName
		cmp.b	#'"',(a1)
		beq.s	GotFileName
		cmp.b	#"'",(a1)
		beq.s	GotFileName
		addq.l	#1,a1
		bra	.Loop
GotFileName	clr.b	(a1)+
		move.l	a1,a3
		lea	FileName(pc),a1			; Copy filename
		bsr	StrCpy
		move.l	a3,a0
		bsr	SkipBlanks
		move.l	a0,a3
		lea	AReviseCStr(pc),a1		; C or ASM revision ?
		bsr	CheckARexxComm2
		tst.l	d0
		bne.s	NoCMode
		move.l	#CRevDefine1,RevDefine		; Set Pointer to macro text
		move.b	#'/',d0				; First char of header
		move.b	#'*',d1				; Line introducer
		move.b	d0,d2				; Trailing char
		bra.s	GotMode
NoCMode		move.l	a3,a0
		lea	AReviseAsmStr(pc),a1
		bsr	CheckARexxComm2
		tst.l	d0
		bne.s	NoAsmMode
		move.l	#AsmRevDefine1,RevDefine	; Pointer to defines
		move.b	#';',d0
		move.b	d0,d1
		move.b	#'*',d2
		bra.s	GotMode
NoAsmMode	move.l	a3,a0
		lea	ARevisePasStr(pc),a1
		bsr	CheckARexxComm2
		tst.l	d0
		bne.s	NoPasMode
		move.l	#PASRevDefine1,RevDefine	; Pointer to defines
		move.b	#'{',d0
		move.b	#'*',d1
		move.b	#'}',d2
		bra.s	GotMode
NoPasMode	move.l	a3,a0
		lea	AReviseBasStr(pc),a1
		bsr	CheckARexxComm2
		tst.l	d0
		bne	ACommFail
		move.l	#BASRevDefine1,RevDefine	; Pointer to defines
		move.b	#"'",d0
		move.b	d0,d1
		move.b	#'*',d2
GotMode		bsr	DoRevision
		tst.l	d0
		beq	ACommFail
		bra	ARexxCommDone

ANoRevise	lea	DriveStr(pc),a1			; Set new drive ?
		bsr	CheckARexxComm
		bne.s	ANoDrive

		link	a5,#-4
		move.l	a0,d1
		lea	-4(a5),a0
		move.l	a0,d2
		RELLIB	StrToLong,_DOSBase
		move.l	-4(a5),d1
		unlk	a5
		tst.l	d0
		beq	ACommFail
		move.l	d1,d0
		lea	DriveRequests(a4),a0
		lsl.l	#2,d0
		tst.l	0(a0,d0.w)
		beq	ACommFail
		move.w	d1,FormatDrive(a4)

		CALLSYS	Forbid
		move.l	#GD_Drive,d0			; Set Drive gadget
		moveq	#0,d1
		move.w	FormatDrive(a4),d1
		lea	FormGads(a4),a0
		move.l	FormHandle(a4),a1
		lea	DriveList(a4),a2
		RELLIB	SetCycleGad,_KCXBase
		CALLSYS	Permit
		bra	ARexxCommDone

ANoDrive	lea	LabelStr(pc),a1			; new label ?
		bsr	CheckARexxComm
		bne.s	ANoLabel

		lea	FormatDriveName(a4),a1
		move.l	#29,d7
		bsr	StrNCpy
		CALLSYS	Forbid
		move.l	#GD_Label,d0
		lea	FormGads(a4),a0
		move.l	FormHandle(a4),a1
		move.l	#30,d1
		lea	FormatDriveName(a4),a2
		RELLIB	SetStringGad,_KCXBase
		CALLSYS	Permit
		bra	ARexxCommDone

ANoLabel	lea	AIconStr(pc),a1			; Create Trashcan ?
		bsr	CheckARexxComm
		bne.s	ANoTrash

		bsr	CheckABool
		tst.l	d0
		blt	ACommFail
		CALLSYS	Forbid
		move.b	d0,MakeIcons(a4)
		move.l	#GD_Trashcan,d1
		bsr	SetFormBox
		CALLSYS	Permit
		bra	ARexxCommDone

ANoTrash	lea	QuickStr(pc),a1			; Quick format ?
		bsr	CheckARexxComm
		bne.s	ANoQuick

		bsr	CheckABool
		tst.l	d0
		blt	ACommFail
		CALLSYS	Forbid
		move.b	d0,QuickFormat(a4)
		move.l	#GD_Quick,d1
		bsr	SetFormBox
		CALLSYS	Permit
		bra	ARexxCommDone

ANoQuick	lea	FFSStr(pc),a1			; Format FFS ?
		bsr	CheckARexxComm
		bne.s	ANoFFS

		bsr	CheckABool
		tst.l	d0
		blt	ACommFail
		CALLSYS	Forbid
		move.b	d0,FFSDrive(a4)
		move.l	#GD_FFS,d1
		bsr	SetFormBox
		CALLSYS	Permit
		bra	ARexxCommDone

ANoFFS		lea	VerifyStr(pc),a1		; Verify format ?
		bsr	CheckARexxComm
		bne.s	ANoVerify

		bsr	CheckABool
		tst.l	d0
		blt	ACommFail
		CALLSYS	Forbid
		move.b	d0,NoVerify(a4)
		move.l	#GD_NoVerify,d1
		bsr	SetFormBox
		CALLSYS	Permit
		bra	ARexxCommDone

ANoVerify	lea	InstallStr(pc),a1		; Install disk ?
		bsr	CheckARexxComm
		bne.s	ANoInstall

		bsr	CheckABool
		tst.l	d0
		blt	ACommFail
		CALLSYS	Forbid
		move.b	d0,Install(a4)
		move.l	#GD_Install,d1
		bsr	SetFormBox
		CALLSYS	Permit
		bra	ARexxCommDone

ANoInstall	lea	EjectStr(pc),a1			; Eject disk ?
		bsr	CheckARexxComm
		bne.s	ANoEject

		bsr	CheckABool
		tst.l	d0
		blt	ACommFail
		CALLSYS	Forbid
		move.b	d0,EjectDrive(a4)
		move.l	#GD_Eject,d1
		bsr	SetFormBox
		CALLSYS	Permit
		bra	ARexxCommDone

ANoEject	lea	InterStr(pc),a1			; International mode ?
		bsr	CheckARexxComm
		bne.s	ANoInter

		bsr	CheckABool
		tst.l	d0
		blt	ACommFail
		CALLSYS	Forbid
		tst.b	DirCache(a4)
		bne.s	.End
		move.b	d0,InterMode(a4)
		move.b	d0,OldInterMode(a4)
		move.l	#GD_IntMode,d1
		bsr	SetFormBox
.End		CALLSYS	Permit
		bra	ARexxCommDone

ANoInter	lea	CacheStr(pc),a1			; Caching ?
		bsr	CheckARexxComm
		bne.s	ANoCache

		bsr	CheckABool
		tst.l	d0
		blt	ACommFail
		CALLSYS	Forbid
		move.b	d0,DirCache(a4)
		tst.b	d0
		beq.s	.UseOldInt
		moveq	#1,d0
		move.b	d0,InterMode(a4)
		moveq	#1,d2
		bra.s	.SetGad
.UseOldInt	move.b	OldInterMode(a4),d0
		move.b	d0,InterMode(a4)
		moveq	#0,d2
.SetGad		move.l	#GD_IntMode,d1
		bsr	SetFormBox
		move.l	#GD_IntMode,d0
		move.l	d2,d1
		DOLIB	GhostGadget
		move.b	DirCache(a4),d0
		move.l	#GD_DiskCache,d1
		bsr	SetFormBox
		CALLSYS	Permit
		bra	ARexxCommDone

ANoCache	lea	WatchStr(pc),a1			; Watch disks ?
		bsr	CheckARexxComm
		bne.s	ANoWatch

		bsr	CheckABool
		tst.l	d0
		blt	ACommFail
		CALLSYS	Forbid
		move.b	d0,WatchDisks(a4)
		move.l	#GD_WatchDisks,d1
		bsr	SetFormBox
		CALLSYS	Permit
		bra	ARexxCommDone

ANoWatch	lea	FormatStr(pc),a1		; Do format ???
		bsr	CheckARexxComm
		bne.s	ANoFormat
		bsr	GoGoGoFormat
		bra.s	ARexxCommDone

ANoFormat	lea	TimeZoneStr(pc),a1		; Change timezone ?
		bsr	CheckARexxComm
		bne.s	ARexxCommDone
		move.l	TimeZone(a4),d7
		move.l	ARexxBuffer(a4),a1
		bsr	StrCpy
		move.l	ARexxBuffer(a4),d1
		lea	TimeZone(a4),a0
		move.l	a0,d2
		RELLIB	StrToLong,_DOSBase		; Get new time
		tst.l	TimeZone(a4)			; negative ?
		blt.s	.Error				; disable blank
		cmp.l	#3,d0
		blt.s	.GoOn
.Error		move.l	d7,TimeZone(a4)
.GoOn		CALLSYS	Forbid
		move.l	#GD_BillTimeMX,d0
		move.l	TimeZone(a4),d1
		lea	BillGads(a4),a0
		move.l	BillHandle(a4),a1
		RELLIB	SetMXGad,_KCXBase
		CALLSYS	Permit
		bra.s	ARexxCommDone

ACommFail	move.l	#RC_FAIL,AResult1(a4)		; Command failed
		clr.l	AResult2(a4)
ARexxCommDone	move.l	MyARexxMsg(a4),a1
		move.l	AResult1(a4),rm_Result1(a1)
		move.l	AResult2(a4),rm_Result2(a1)
		CALLSYS	ReplyMsg
		bra	HandleARexx
AllARexxDone	rts

**********************************************************************
*                          ARexx variables                           *
**********************************************************************
ActivateStr	dc.b	"ACTIVATE",0
MouseStr	dc.b	"SUNMOUSE",0
MenuWrapStr	dc.b	"MENUWRAP",0
FKeysStr	dc.b	"FKEYS",0
PageNumStr	dc.b	"PAGENUM",0
SetEnvStr	dc.b	"SETENV",0
ScreenTStr	dc.b	"SCREENTIME",0
MouseTStr	dc.b	"MOUSETIME",0
CycleWStr	dc.b	"CYCLING",0
BillWinStr	dc.b	"BILLPOPUP",0
LogCallStr	dc.b	"LOGCALLS",0
ESCCloseStr	dc.b	"ESCCLOSE",0
UmlautOnStr	dc.b	"MAPUMLAUT",0
DriveStr	dc.b	"DRIVE",0
LabelStr	dc.b	"LABEL",0
AIconStr	dc.b	"TRASHCAN",0
QuickStr	dc.b	"QUICK",0
FFSStr		dc.b	"FFS",0
VerifyStr	dc.b	"NOVERIFY",0
InstallStr	dc.b	"INSTALL",0
EjectStr	dc.b	"EJECT",0
InterStr	dc.b	"INTERNATIONAL",0
CacheStr	dc.b	"DIRCACHE",0
WatchStr	dc.b	"WATCHDISKS",0
FormatStr	dc.b	"FORMAT",0
TimeZoneStr	dc.b	"TIMEZONE",0
EnableStr	dc.b	"ENABLE",0
DisableStr	dc.b	"DISABLE",0
AReviseStr	dc.b	"REVISE",0
AReviseAsmStr	dc.b	"ASM",0
AReviseCStr	dc.b	"C",0
ARevisePasStr	dc.b	"PAS",0
AReviseBasStr	dc.b	"BAS",0
		EVEN



**********************************************************************
*                Checks, if we know the ARexx command                *
**********************************************************************
CheckARexxComm	move.l	ARexxCommand(a4),a0
CheckARexxComm2	moveq	#0,d0
		moveq	#0,d1
.Loop		move.b	(a0)+,d0			; Get chars from buffer
		move.b	(a1)+,d1
		or.b	#$20,d0				; Make it case in-sensitive
		or.b	#$20,d1
		cmp.b	#$20,d0				; Argstring end ?
		beq.s	.StringOk
		cmp.b	d0,d1
		beq.s	.Loop
.StringFalse	moveq	#1,d0				; Error flag
		rts
.StringOk	cmp.b	#$20,d1				; Our string also ending ?
		bne	.StringFalse			; Nope -> go on
		bsr	SkipBlanks
		moveq	#0,d0				; Command OK
		tst.l	d0
		rts


**********************************************************************
*             Checks for flags (0/1) in an ARexx-Command             *
**********************************************************************
CheckABool	cmp.b	#'0',(a0)
		beq.s	.False
		cmp.b	#'1',(a0)
		beq.s	.True
		moveq	#-1,d0
		rts
.False		moveq	#0,d0
		rts
.True		moveq	#1,d0
		rts




**********************************************************************
*              Enter a log message and revise the text               *
**********************************************************************
DoRevision	clr.l	OurLock(a4)

		move.b	d0,CHeader			; Write chars to Header
		move.b	d1,CLineNL			; for different languages
		move.b	d1,CLine
		move.b	d1,CCopyRight
		move.b	d1,CCR1
		move.b	d1,CFile
		move.b	d1,CCreatedOn
		move.b	d1,CAuthor
		move.b	d1,CRevision
		move.b	d1,CPurpose
		move.b	d1,CPurpose+3
		move.b	d1,CPurpose+6
		move.b	d1,CPurpose+17
		move.b	d1,CLog
		move.b	d1,CL1
		move.b	d1,CL2
		move.b	d1,CL3
		move.b	d1,CLogInit
		move.b	d1,CLI1
		move.b	d1,CLI2
		move.b	d1,CLI3
		move.b	d1,CLI4
		move.b	d1,CLI5
		move.b	d1,CEnd
		move.b	d2,CEnd+81
		move.b	d1,CNewLog

		lea	RevDateTime(a4),a0
		move.l	a0,d1
		RELLIB	DateStamp,_DOSBase

		lea	RevDateTime(a4),a0		; Get day string
		move.l	ds_Days(a0),d0
		divu	#7,d0
		clr.w	d0
		swap	d0
		lsl.l	#2,d0
		lea	RevDays(pc),a0
		move.l	0(a0,d0.w),a0			; Copy day name to buffer
		lea	RevDayString(pc),a1
		bsr	StrCpy

		lea	RevDateTime(a4),a0		; Do the string formatings
		move.b	#FORMAT_CDN,dat_Format(a0)
		clr.l	dat_StrDay(a0)			; First create the whole string
		lea	RevDateString(pc),a1
		move.l	a1,dat_StrDate(a0)		; for revision
		lea	RevTimeString(pc),a1
		move.l	a1,dat_StrTime(a0)
		move.l	a0,d1
		DOLIB	DateToStr
		lea	RevDateString(pc),a0
		move.b	#'.',2(a0)
		move.b	#'.',5(a0)

		lea	FileName(pc),a0
.LoopDev	cmp.b	#':',(a0)			; Find out whether filename
		beq.s	.GotDev				; is given with complete path
		tst.b	(a0)
		beq.s	.NoDev
		addq.l	#1,a0
		bra.s	.LoopDev

.NoDev		move.l	MyARexxMsg(a4),a0		; Lock to current Dir of
		move.l	MN_REPLYPORT(a0),a0		; ARexx. Normaly the workdirectory
		move.l	MP_SIGTASK(a0),a0		; your editor is running on.
		move.l	pr_CurrentDir(a0),d1		; If a path is specified
		RELLIB	DupLock,_DOSBase
		move.l	d0,OurLock(a4)
		beq	NoRevision
		move.l	d0,d1
		DOLIB	CurrentDir
		move.l	d0,PastLock(a4)
		beq	NoRevision

.GotDev		bsr	CheckRevision			; Check if Revision already there
		tst.l	d7
		blt	NoRevision

		link	a5,#-(4+4+2+2+2+2+gng_SIZEOF)
		lea	LogTxt(a4),a0
		move.l	a0,FontPtr(a5)
		bsr	LockScreen
		tst.l	d0
		beq	.LogWinError
		move.l	d0,a0
		RELLIB	ScreenToFront,_IntuitionBase
		moveq	#0,d2
		moveq	#0,d3
		RELLIB	ComputeFont,_KCXBase
		move.l	-4(a5),a0
		sub.l	a1,a1
		RELLIB	GetVisualInfoA,_GadToolsBase
		move.l	d0,LogVisInfo(a4)
		beq	.LogWinError

		move.l	-4(a5),a0
		moveq	#0,d3
		moveq	#0,d2
		move.w	LogWinWidth(a4),d2
		move.w	LogWinHeight(a4),d3
		RELLIB	ComputeFont,_KCXBase
		move.l	-4(a5),a0
		move.l	d2,d0
		DOLIB	ComputeX
		move.l	d0,d4
		move.w	LogWinLeft(a4),d2
		move.l	-4(a5),a3
		move.w	sc_LeftEdge(a3),d5
		neg.w	d5
		add.w	d5,d2
		add.w	d2,d0
		add.w	OffX(a5),d0
		moveq	#0,d1
		move.b	sc_WBorRight(a0),d1
		add.w	d1,d0
		cmp.w	sc_Width(a0),d0
		bls.s	.WOk
		move.w	sc_Width(a0),d0
		sub.w	d4,d0
		move.w	d0,d2
.WOk		move.l	d3,d0
		DOLIB	ComputeY
		move.l	d0,d4
		move.w	LogWinTop(a4),d3
		move.l	-4(a5),a3
		move.w	sc_TopEdge(a3),d5
		neg.w	d5
		add.w	d5,d3
		add.w	d3,d0
		add.w	OffY(a5),d0
		moveq	#0,d1
		move.b	sc_WBorBottom(a0),d1
		add.w	d1,d0
		cmp.w	sc_Height(a0),d0
		bls.s	.HOk
		move.w	sc_Height(a0),d0
		sub.w	d4,d0
		move.w	d0,d3
.HOk		lea.l	LogGadget(a4),a0
		RELLIB	CreateContext,_GadToolsBase
		move.l	d0,a3
		beq	.LogWinError

		movem.l d2-d3/a4,-(sp)
		moveq	#0,d3
		lea.l	LogWinGTags,a4
.LogWinGL	lea.l	LogWinNGads,a0
		move.l	d3,d0
		mulu	#gng_SIZEOF,d0
		add.l	d0,a0
		lea.l	-(4+4+2+2+2+2+gng_SIZEOF)(a5),a1
		moveq	#gng_SIZEOF,d0
		CALLSYS	CopyMem
		lea.l	-(4+4+2+2+2+2+gng_SIZEOF)(a5),a0
		move.l	a4,-(sp)
		move.l	ZeroBase(pc),a4
		move.l	gng_GadgetText(a0),-(sp)
		bsr	GetLocStr
		move.l	(sp)+,d7
		move.l	d7,gng_GadgetText(a0)
		move.l	LogVisInfo(a4),gng_VisualInfo(a0)
		move.l	FontPtr(a5),gng_TextAttr(a0)
		move.w	gng_LeftEdge(a0),d0
		RELLIB	ComputeX,_KCXBase
		move.l	(sp)+,a4
		add.w	OffX(a5),d0
		move.w	d0,gng_LeftEdge(a0)
		move.w	gng_TopEdge(a0),d0
		DOLIB	ComputeY
		add.w	OffY(a5),d0
		move.w	d0,gng_TopEdge(a0)
		move.w	gng_Width(a0),d0
		DOLIB	ComputeX
		move.w	d0,gng_Width(a0)
		move.w	gng_Height(a0),d0
		DOLIB	ComputeY
		move.w	d0,gng_Height(a0)
		lea.l	LogWinGTypes,a0
		moveq	#0,d0
		move.l	d3,d1
		lsl.l	#1,d1
		move.w	0(a0,d1.w),d0
		move.l	a3,a0
		lea.l	-(4+4+2+2+2+2+gng_SIZEOF)(a5),a1
		move.l	a4,a2
		move.l	a4,-(sp)
		move.l	ZeroBase(pc),a4
		RELLIB	CreateGadgetA,_GadToolsBase
		move.l	(sp)+,a4
		tst.l	d0
		beq	.LogWinError
		move.l	d0,a3
		move.l	d7,a0
		bsr	LocalizeGadKey
		move.l	d1,gg_UserData(a3)
		move.l	d3,d0
		lsl.l	#2,d0
		move.l	a4,-(sp)
		move.l	ZeroBase(pc),a4
		lea.l	LogGads(a4),a0
		move.l	(sp)+,a4
		move.l	a3,0(a0,d0.w)
.LogWinTL	tst.l	(a4)
		beq.s	.LogWinDN
		addq.w	#8,a4
		bra.s	.LogWinTL
.LogWinDN	addq.w	#4,a4
		addq.w	#1,d3
		cmp.w	#LogWin_CNT,d3
		bmi	.LogWinGL
		movem.l (sp)+,d2-d3/a4
		move.l	LogGadget(a4),LogWinWG+4

		lea	LogGads(a4),a0
		bsr	SetStrIntGads

		move.l	-4(a5),LogWinSC+4		; Open window
		ext.l	d2
		ext.l	d3
		move.l	d2,LogWinL+4
		move.l	d3,LogWinT+4
		move.l	-4(a5),a0
		move.w	LogWinWidth(a4),d0
		RELLIB	ComputeX,_KCXBase
		add.w	OffX(a5),d0
		moveq	#0,d1
		move.b	sc_WBorRight(a0),d1
		add.w	d1,d0
		move.l	d0,LogWinW+4
		move.w	LogWinHeight(a4),d0
		DOLIB	ComputeY
		add.w	OffY(a5),d0
		moveq	#0,d1
		move.b	sc_WBorBottom(a0),d1
		add.w	d1,d0
		move.l	d0,LogWinH+4
		suba.l	a0,a0
		lea.l	LogWinWindowTags,a1
		RELLIB	OpenWindowTagList,_IntuitionBase
		move.l	d0,LogHandle(a4)
		beq.s	.LogWinError

		move.l	LogHandle(a4),a0
		suba.l	a1,a1
		RELLIB	GT_RefreshWindow,_GadToolsBase

		move.l	#GD_LogVer,d0
		move.w	RevRevisionDat(pc),d1
		lea	LogGads(a4),a0
		move.l	LogHandle(a4),a1
		RELLIB	SetIntegerGad,_KCXBase
		move.l	#GD_LogRev,d0
		move.w	RevRevisionDat+2(pc),d1
		DOLIB	SetIntegerGad
		move.l	#GD_LogOnlyText,d0
		moveq	#0,d1
		move.b	OnlyLog(a4),d1
		bchg	#0,d1
		DOLIB	GhostGadget

		bsr	UnlockScreen
		unlk	a5
		bra.s	.GotLogWin
.LogWinError	bsr	UnlockScreen
		unlk	a5
		bra	NoLogWin

.GotLogWin	lea	NewMsg(a4),a1			; and messages
		move.w	#(62*5)-1,d7
.Loop1		clr.b	(a1)+
		dbf	d7,.Loop1

		tst.b	OnlyLog(a4)			; Enter a new LogMessage ?
		bne.s	DoOnlyLog
		move.l	LogHandle(a4),a0		; Set window title for purpose
		LOCLEA	PurposeWinTitle,a1
		suba.l	a2,a2
		subq.w	#1,a2
		RELLIB	SetWindowTitles,_IntuitionBase
		bsr	CheckRevInput
		cmp.b	#2,LogInputEnd(a4)
		beq	NoErrorFound
		lea	NewMsg(a4),a3
		bsr	CopyMessage			; Copy entered message
		bra.s	PurpDone

DoOnlyLog	move.l	LogHandle(a4),a0		; Change to log title
		LOCLEA	LogWinTitle,a1
		suba.l	a2,a2
		subq.w	#1,a2
		RELLIB	SetWindowTitles,_IntuitionBase
		bsr	CheckRevInput
		cmp.b	#2,LogInputEnd(a4)
		beq	NoErrorFound
		lea	NewMsg(a4),a3
		bsr	CopyMessage			; Copy entered message

PurpDone	link	a5,#-(4+4+2+2+2+2+20+gng_SIZEOF)
		lea	ProgTxt(a4),a0
		move.l	a0,FontPtr(a5)
		bsr	LockScreen
		tst.l	d0
		beq	.ProgWinError
		moveq	#0,d2
		moveq	#0,d3
		RELLIB	ComputeFont,_KCXBase
		move.l	-4(a5),a0
		sub.l	a1,a1
		RELLIB	GetVisualInfoA,_GadToolsBase
		move.l	d0,ProgVisInfo(a4)
		beq	.ProgWinError

		move.l	-4(a5),a0
		moveq	#0,d3
		moveq	#0,d2
		move.w	ProgWinWidth(a4),d2
		move.w	ProgWinHeight(a4),d3
		RELLIB	ComputeFont,_KCXBase
		move.l	-4(a5),a0
		move.l	d2,d0
		DOLIB	ComputeX
		move.l	d0,d4
		move.w	ProgWinLeft(a4),d2
		move.l	-4(a5),a3
		move.w	sc_LeftEdge(a3),d5
		neg.w	d5
		add.w	d5,d2
		add.w	d2,d0
		add.w	OffX(a5),d0
		moveq	#0,d1
		move.b	sc_WBorRight(a0),d1
		add.w	d1,d0
		cmp.w	sc_Width(a0),d0
		bls.s	.WOk
		move.w	sc_Width(a0),d0
		sub.w	d4,d0
		move.w	d0,d2
.WOk		move.l	d3,d0
		DOLIB	ComputeY
		move.l	d0,d4
		move.w	ProgWinTop(a4),d3
		move.l	-4(a5),a3
		move.w	sc_TopEdge(a3),d5
		neg.w	d5
		add.w	d5,d3
		add.w	d3,d0
		add.w	OffY(a5),d0
		moveq	#0,d1
		move.b	sc_WBorBottom(a0),d1
		add.w	d1,d0
		cmp.w	sc_Height(a0),d0
		bls.s	.HOk
		move.w	sc_Height(a0),d0
		sub.w	d4,d0
		move.w	d0,d3
.HOk		lea.l	ProgGadget(a4),a0
		RELLIB	CreateContext,_GadToolsBase
		move.l	d0,a3
		beq	.ProgWinError

		movem.l d2-d3/a4,-(sp)
		moveq	#0,d3
		lea.l	ProgWinGTags,a4
.ProgWinGL	lea.l	ProgWinNGads,a0
		move.l	d3,d0
		mulu	#gng_SIZEOF,d0
		add.l	d0,a0
		lea.l	-(4+4+2+2+2+2+gng_SIZEOF)(a5),a1
		moveq	#gng_SIZEOF,d0
		CALLSYS	CopyMem
		lea.l	-(4+4+2+2+2+2+gng_SIZEOF)(a5),a0
		move.l	a4,-(sp)
		move.l	ZeroBase(pc),a4
		move.l	ProgVisInfo(a4),gng_VisualInfo(a0)
		move.l	FontPtr(a5),gng_TextAttr(a0)
		move.w	gng_LeftEdge(a0),d0
		RELLIB	ComputeX,_KCXBase
		move.l	(sp)+,a4
		add.w	OffX(a5),d0
		move.w	d0,gng_LeftEdge(a0)
		move.w	gng_TopEdge(a0),d0
		DOLIB	ComputeY
		add.w	OffY(a5),d0
		move.w	d0,gng_TopEdge(a0)
		move.w	gng_Width(a0),d0
		DOLIB	ComputeX
		move.w	d0,gng_Width(a0)
		move.w	gng_Height(a0),d0
		DOLIB	ComputeY
		move.w	d0,gng_Height(a0)
		lea.l	ProgWinGTypes,a0
		moveq	#0,d0
		move.l	d3,d1
		lsl.l	#1,d1
		move.w	0(a0,d1.w),d0
		move.l	a3,a0
		lea.l	-(4+4+2+2+2+2+gng_SIZEOF)(a5),a1
		move.l	a4,a2
		move.l	a4,-(sp)
		move.l	ZeroBase(pc),a4
		RELLIB	CreateGadgetA,_GadToolsBase
		move.l	(sp)+,a4
		tst.l	d0
		beq	.ProgWinError
		move.l	d0,a3
.ProgWinTL	tst.l	(a4)
		beq.s	.ProgWinDN
		addq.w	#8,a4
		bra.s	.ProgWinTL
.ProgWinDN	addq.w	#4,a4
		addq.w	#1,d3
		cmp.w	#ProgWin_CNT,d3
		bmi	.ProgWinGL
		movem.l (sp)+,d2-d3/a4
		move.l	ProgGadget(a4),ProgWinWG+4

		LOCLEA	ProgWinWTitle,ProgWinWG+12
		LOCLEA	WindowScrTitle,ProgWinWG+20
		move.l	-4(a5),ProgWinSC+4		; Open window
		ext.l	d2
		ext.l	d3
		move.l	d2,ProgWinL+4
		move.l	d3,ProgWinT+4
		move.l	-4(a5),a0
		move.w	ProgWinWidth(a4),d0
		RELLIB	ComputeX,_KCXBase
		add.w	OffX(a5),d0
		moveq	#0,d1
		move.b	sc_WBorRight(a0),d1
		add.w	d1,d0
		move.l	d0,ProgWinW+4
		move.w	ProgWinHeight(a4),d0
		DOLIB	ComputeY
		add.w	OffY(a5),d0
		moveq	#0,d1
		move.b	sc_WBorBottom(a0),d1
		add.w	d1,d0
		move.l	d0,ProgWinH+4
		suba.l	a0,a0
		lea.l	ProgWinWindowTags,a1
		RELLIB	OpenWindowTagList,_IntuitionBase
		move.l	d0,ProgHandle(a4)
		beq	.ProgWinError
		move.l	d0,a0
		move.l	wd_RPort(a0),ProgRPort(a4)
		suba.l	a1,a1
		RELLIB	GT_RefreshWindow,_GadToolsBase

		moveq	#0,d2
		moveq	#0,d3
		move.w	ProgWinWidth(a4),d2
		move.w	ProgWinHeight(a4),d3
		RELLIB	ComputeFont,_KCXBase

		move.l	ProgRPort(a4),a0
		lea.l	-(4+4+2+2+2+2+20)(a5),a1	; Set Bevel tag
		move.l	#GTBB_Recessed,(a1)
		move.l	#-1,4(a1)
		move.l	#GT_VisualInfo,8(a1)
		move.l	ProgVisInfo(a4),12(a1)
		move.l	#TAG_DONE,16(a1)
		moveq	#4,d0
		DOLIB	ComputeY
		add.w	OffY(a5),d0
		move.l	d0,d1
		move.l	#221,d0
		DOLIB	ComputeX
		move.l	d0,d2
		move.l	#16,d0
		DOLIB	ComputeY
		move.l	d0,d3
		move.l	#39,d0
		DOLIB	ComputeX
		add.w	OffX(a5),d0
		RELLIB	DrawBevelBoxA,_GadToolsBase

		bsr	UnlockScreen
		unlk	a5
		bra.s	.ProgOpen

.ProgWinError	bsr	UnlockScreen
		unlk	a5
		bra	NoLogWin

.ProgOpen	move.l	LogHandle(a4),a0
		RELLIB	BusyWindow,_KCXBase

		lea	Author(pc),a0
		move.l	a0,RevAuthorDat			; Set strings to defaults
		lea	Company(pc),a0
		move.l	a0,RevCompanyDat

		lea	AuthorString(pc),a0
		move.l	a0,d1				; Try to get Author(a4) env
		lea	Author(pc),a0
		move.l	a0,d2
		move.l	#40,d3
		move.l	#GVF_GLOBAL_ONLY,d4
		RELLIB	GetVar,_DOSBase
		tst.l	d0
		bgt.s	GotAuthor
		move.l	#DefaultName,RevAuthorDat

GotAuthor	lea	CompanyString(pc),a0
		move.l	a0,d1				; Try to get Company(a4) env
		lea	Company(pc),a0
		move.l	a0,d2
		move.l	#40,d3
		move.l	#GVF_GLOBAL_ONLY,d4
		RELLIB	GetVar,_DOSBase
		tst.l	d0
		bgt.s	GotCompany
		move.l	#DefaultName,RevCompanyDat

GotCompany	cmp.b	#3,LogInputEnd(a4)		; Only text ???
		beq.s	.NoNewVerNum

		moveq	#GD_LogVer,d0			; Get version number
		lea	LogGads(a4),a0
		RELLIB	GetGadgetPtr,_KCXBase
		move.l	gg_SpecialInfo(a0),a0
		move.l	si_LongInt(a0),d0
		move.w	d0,RevRevisionDat

		move.l	#GD_LogRev,d0			; Get revision number
		lea	LogGads(a4),a0
		DOLIB	GetGadgetPtr
		move.l	gg_SpecialInfo(a0),a0
		move.l	si_LongInt(a0),d0
		move.w	d0,RevRevisionDat+2
		bra.s	.GotNewVerNum

.NoNewVerNum	move.w	RevRevisionDat(pc),d0
		move.w	RevRevisionDat+2(pc),d1
		subq.w	#1,d1
		bge.s	.CalcedOldNum
		moveq	#0,d1
		subq.w	#1,d0
		bge.s	.CalcedOldNum
		moveq	#0,d0
.CalcedOldNum	move.w	d0,RevRevisionDat
		move.w	d1,RevRevisionDat+2

.GotNewVerNum	lea	FileName(pc),a0
		move.l	a0,d1				; Open File
		move.l	#MODE_OLDFILE,d2
		RELLIB	Open,_DOSBase
		move.l	d0,InHand(a4)
		beq	NoRevision

		lea	TempName(pc),a0
		move.l	a0,d1
		move.l	#MODE_NEWFILE,d2		; Open temporaryfile
		DOLIB	Open
		move.l	d0,OutHand(a4)
		beq	NoRevision

		tst.b	OnlyLog(a4)			; Enter new LogMessage ?
		beq	WriteHeader

		clr.l	ProgDone(a4)
		move.l	#(PROGWINWIDTH-92)/13,ProgStep(a4)

		bsr	ProgMore

.Loop		bsr	DoFGets
		ble	NoRevision
		bsr.s	DoFPuts
		blt	NoRevision
		lea	CHeader(pc),a0
		move.l	ARexxBuffer(a4),a1
		moveq	#-1,d7
		bsr	StrCmp
		tst.l	d0
		beq.s	GotHeader
		bra	.Loop

GotHeader	bsr	ProgMore

		moveq	#5,d7				; Skip comming 6 lines
		cmp.b	#3,LogInputEnd(a4)
		bne.s	.NotOnlyLog
		addq.w	#1,d7				; ... or 7 lines
.NotOnlyLog	bsr	RSkipLines
		tst.l	d0
		beq	NoRevision
		cmp.b	#3,LogInputEnd(a4)
		beq.s	.SkipRev
		bsr.s	DoFGets
		ble	NoRevision
		lea	CRevision(pc),a0		; Convert revision to new string
		lea	RevRevisionDat(pc),a1
		bsr	PrintF
		bsr.s	DoFPuts
		blt	NoRevision
.SkipRev	moveq	#3,d7				; Skip the next 4 lines
		bsr	RSkipLines
		tst.l	d0
		blt	NoRevision
		bsr	ProgMore
		bra.s	SkipFPuts

DoFPutsC	move.l	#CHeader,d2
		bra.s	DoFPuts0
DoFPutsNL	move.l	#CLineNL,d2
		bra.s	DoFPuts0
DoFPuts		move.l	ARexxBuffer(a4),d2
DoFPuts0	move.l	OutHand(a4),d1
		DOLIB	FPuts
		tst.l	d0
		rts
DoFGets		move.l	InHand(a4),d1
		move.l	ARexxBuffer(a4),d2
		move.l	#AREXXBUFFSIZE,d3
		DOLIB	FGets
		tst.l	d0
		rts
SkipFPuts
.Loop		bsr.s	DoFGets
		ble	NoRevision
		move.l	ARexxBuffer(a4),a0
		bsr	StrLen
		cmp.l	#3,d0
		beq.s	.GotIt
		bsr.s	DoFPuts
		blt	NoRevision
		bra	.Loop
.GotIt		bsr	ProgMore
		cmp.b	#3,LogInputEnd(a4)		; Add complete entry ???
		bne	.StoreCompLog
		bsr	DoFPuts
		blt	NoRevision
		moveq	#2,d7				; Skip old header
		bsr	RSkipLines
		tst.l	d0
		blt	NoRevision
		link	a5,#-128			; Get next line, to see, whether there already
		lea	-128(a5),a0
		move.l	InHand(a4),d1			; Check next line if it`s an extended message
		move.l	a0,d2
		DOLIB	FGets
		tst.l	d0
		bgt.s	.Ok1
		unlk	a5
		bra	NoRevision
.Ok1		lea	-128(a5),a0			; Reposition file pointer
		bsr	StrLen
		move.l	InHand(a4),d1
		move.l	d0,d2
		neg.l	d2
		move.l	#OFFSET_CURRENT,d3
		DOLIB	Seek
		lea	-128(a5),a0			; Check read line for extension
		moveq	#0,d7
		cmp.b	#'-',4(a0)
		bne.s	.DoCopy
		cmp.b	#'*',5(a0)
		bne.s	.DoCopy
		cmp.b	#'-',6(a0)
		beq.s	.NoCopy
.DoCopy		move.l	ARexxBuffer(a4),a0
		lea	-128(a5),a1			; Copy header
		moveq	#1,d7
		bsr	StrCpy
		lea	-(128-2)(a5),a1
		move.b	#' ',(a1)+
		move.b	#' ',(a1)+
		move.b	#'-',(a1)+
		move.b	#'*',(a1)+
		move.b	#'-',(a1)+
		move.b	#' ',(a1)+
		move.b	#' ',(a1)+
		move.l	ARexxBuffer(a4),a0
		addq.l	#3,a0
		bsr	StrCpy
		moveq	#1,d7
.NoCopy		lea	CNewLog(pc),a0			; Write changed log-header
		lea	RevCreatedOnDat(pc),a1
		bsr	PrintF
		bsr	DoFPuts
		blt	NoRevision
		lea	NewMsg(a4),a3			; First insert new message
		move.l	d7,-(sp)
		bsr	RWriteMessage
		move.l	(sp)+,d7
		tst.l	d0
		blt	NoRevision
		tst.b	d7
		beq.s	.Ok2
		move.l	OutHand(a4),d1			; Insert old header
		lea	-128(a5),a0
		move.l	a0,d2
		DOLIB	FPuts
		tst.l	d0
		bge.s	.Ok2
		unlk	a5
		bra	NoRevision
.Ok2		unlk	a5
.FindLogEnd	bsr	DoFGets				; Insert old Logmessage
		ble	NoRevision
		move.l	ARexxBuffer(a4),a0
		bsr	StrLen
		cmp.l	#3,d0
		beq.s	.GotLogEnd
		bsr	DoFPuts
		blt	NoRevision
		bra.s	.FindLogEnd
.GotLogEnd	bsr	DoFPutsNL
		blt	NoRevision
		bsr	ProgMore
		bra.s	.LoopHeader
.StoreCompLog	lea	CLog(pc),a0			; generate new log entry
		lea	RevRevisionDat(pc),a1
		bsr	PrintF
		bsr	DoFPuts
		blt	NoRevision
		lea	NewMsg(a4),a3
		bsr	RWriteMessage			; Write new logmessage
		tst.l	d0
		blt	NoRevision
		bsr	DoFPutsNL
		blt	NoRevision
		bsr	ProgMore
.LoopHeader	bsr	DoFGets
		ble	NoRevision
		bsr	DoFPuts
		blt	NoRevision
		lea	CEnd(pc),a0
		move.l	ARexxBuffer(a4),a1
		moveq	#-1,d7
		bsr	StrCmp
		tst.l	d0
		beq.s	GotHeaderEnd
		bra	.LoopHeader

GotHeaderEnd	bsr	ProgMore
		bsr	DoFGets
		ble	NoRevision
		move.l	ARexxBuffer(a4),a0
		bsr	StrLen
		move.l	InHand(a4),d1			; Reset filepointer to last read
		move.l	d0,d2				; position
		neg.l	d2
		move.l	#OFFSET_CURRENT,d3
		DOLIB	Seek
		move.l	ARexxBuffer(a4),a0		; Did we read in a define ?
		move.l	RevDefine(pc),a1
		move.l	#9,d7
		bsr	StrCmp				; Check for the first 9 chars
		tst.l	d0
		bne	WriteRest			; Nope -> go on

		move.w	RevRevisionDat(pc),d0
		move.w	RevRevisionDat+2(pc),d1
		move.w	d0,RevDefineDat
		move.w	d1,RevDefineDat+2
		move.w	d0,RevVersionDat
		move.w	d1,RevVersionDat+2
		move.l	RevDefine(pc),a0		; Write new define
		lea	RevDefineDat(pc),a1
		bsr	PrintF
		bsr	DoFPuts
		blt	NoRevision
		moveq	#5,d7				; Skip C lines
		cmp.l	#AsmRevDefine1,RevDefine	; Revise Asm source
		bne.s	NoAsmSkip
		add.l	#8,d7				; skip one more line
		bra.s	DoSkip
NoAsmSkip	cmp.l	#PASRevDefine1,RevDefine
		bne.s	DoSkip
		addq.l	#1,d7
DoSkip		bsr	DoFGets
		ble	NoRevision
		dbf	d7,DoSkip
		bra	WriteRest			; Append rest of file

WriteHeader	clr.l	ProgDone(a4)
		move.l	#((PROGWINWIDTH-92)/11),ProgStep(a4)

		bsr	ProgMore
		bsr	ProgMore

		bsr	DoFPutsC
		blt	NoRevision
		lea	CCopyRight(pc),a0		; Convert Company(a4) string
		lea	RevCompanyDat(pc),a1
		bsr	PrintF
		bsr	DoFPuts
		blt	NoRevision
		bsr	ProgMore
		lea	CFile(pc),a0			; convert and write filename
		lea	RevFileDat(pc),a1
		bsr	PrintF
		bsr	DoFPuts
		blt	NoRevision
		bsr	ProgMore
		lea	CCreatedOn(pc),a0		; do creation date
		lea	RevCreatedOnDat(pc),a1
		bsr	PrintF
		bsr	DoFPuts
		blt	NoRevision
		bsr	ProgMore
		lea	CAuthor(pc),a0			; do Author(a4)
		lea	RevAuthorDat(pc),a1
		bsr	PrintF
		bsr	DoFPuts
		blt	NoRevision
		bsr	ProgMore
		lea	CRevision(pc),a0		; Do initial revision
		lea	RevRevisionDat(pc),a1
		bsr	PrintF
		bsr	DoFPuts
		blt	NoRevision
		move.l	OutHand(a4),d1			; Write purpose header
		move.l	#CPurpose,d2
		DOLIB	FPuts
		tst.l	d0
		blt	NoRevision
		bsr	ProgMore
		lea	NewMsg(a4),a3			; Write purpose message
		bsr	RWriteMessage
		tst.l	d0
		blt	NoRevision
		lea	CLogInit(pc),a0
		lea	RevRevisionDat(pc),a1
		bsr	PrintF
		bsr	DoFPuts
		blt	NoRevision
		bsr	ProgMore
		move.w	RevRevisionDat(pc),d0
		move.w	RevRevisionDat+2(pc),d1
		move.w	d0,RevDefineDat
		move.w	d1,RevDefineDat+2
		move.w	d0,RevVersionDat
		move.w	d1,RevVersionDat+2
		move.l	RevDefine(pc),a0		; Write initial define
		lea	RevDefineDat(pc),a1
		bsr	PrintF
		bsr	DoFPuts
		blt	NoRevision

WriteRest	bsr	ProgMore
		move.l	OutHand(a4),d1			; Write the remaining part of file
		DOLIB	Flush
		move.l	InHand(a4),d1
		DOLIB	Flush
.Loop		move.l	InHand(a4),d1			; Read a maximum of 4096 bytes
		move.l	ARexxBuffer(a4),d2
		move.l	#AREXXBUFFSIZE,d3
		DOLIB	Read
		tst.l	d0
		beq.s	RestWritten			; End of file -> go on
		blt	NoRevision			; Error -> quit
		move.l	d0,d3
		move.l	OutHand(a4),d1			; Write buffer to temp file
		move.l	ARexxBuffer(a4),d2
		DOLIB	Write
		tst.l	d0
		ble	NoRevision
		bra	.Loop

RestWritten	bsr	ProgMore

		move.l	OutHand(a4),d1			; Close files
		RELLIB	Close,_DOSBase
		move.l	InHand(a4),d1
		DOLIB	Close

		clr.l	OutHand(a4)
		clr.l	InHand(a4)

		lea	FileName(pc),a0
		move.l	a0,d1				; Copy file back
		move.l	#MODE_NEWFILE,d2
		DOLIB	Open
		move.l	d0,InHand(a4)
		beq.s	NoRevision

		lea	TempName(pc),a0
		move.l	a0,d1
		move.l	#MODE_OLDFILE,d2
		DOLIB	Open
		move.l	d0,OutHand(a4)
		beq.s	NoRevision

		bsr	ProgMore

.Loop		move.l	OutHand(a4),d1			; Read in 4096 bytes
		move.l	ARexxBuffer(a4),d2
		move.l	#AREXXBUFFSIZE,d3
		DOLIB	Read
		tst.l	d0				; 0 read -> end it
		beq.s	.EndWrite
		blt.s	NoRevision
		move.l	d0,d3				; Store read bytes to new file
		move.l	InHand(a4),d1
		move.l	ARexxBuffer(a4),d2
		DOLIB	Write
		tst.l	d0
		blt.s	NoRevision
		bra	.Loop

.EndWrite	bsr	ProgMore
		bra.s	NoErrorFound

NoRevision	RELLIB	IoErr,_DOSBase			; Get error number
		tst.l	d0
		beq.s	NoErrorFound
		move.l	d0,ErrorBack(a4)
		bsr	DisplayError			; Display error

		moveq	#0,d7
		bra.s	GotError
NoErrorFound	moveq	#1,d7
GotError	lea	TempName(pc),a0
		move.l	a0,d1				; Delete file, if possible
		RELLIB	DeleteFile,_DOSBase
		tst.l	OutHand(a4)			; Close files
		beq.s	NoTempFile
		move.l	OutHand(a4),d1
		DOLIB	Close
		lea	TempName(pc),a0
		move.l	a0,d1				; Delete file, if possible
		DOLIB	DeleteFile
NoTempFile	tst.l	InHand(a4)			; Close input handle
		beq.s	NoLogWin
		move.l	InHand(a4),d1
		DOLIB	Close

NoLogWin	tst.l	OurLock(a4)
		beq.s	.NoLock
		move.l	PastLock(a4),d1
		RELLIB	CurrentDir,_DOSBase
		move.l	OurLock(a4),d1
		DOLIB	UnLock
.NoLock		clr.l	InHand(a4)
		clr.l	OutHand(a4)

		lea	ProgHandle(a4),a3
		bsr	CloseNormWindow

		lea	LogHandle(a4),a3
		bsr	CloseNormWindow			; Close our window (AGAIN ???)

		move.l	ARexxAction(a4),d1
		and.l	#RXFF_RESULT,d1
		beq.s	.End
		cmp.b	#2,LogInputEnd(a4)
		bne.s	.NoCancel
		lea	FalseStr(pc),a0			; Create BOOL for error
		bsr	StrLen
		RELLIB	CreateArgstring,_RexxSysBase
		tst.l	d0
		beq.s	.End
		move.l	d0,AResult2(a4)
		bra.s	.End
.NoCancel	lea	TrueStr(pc),a0			; Create BOOL for ok
		bsr	StrLen
		RELLIB	CreateArgstring,_RexxSysBase
		tst.l	d0
		beq.s	.End
		move.l	d0,AResult2(a4)

.End		move.l	d7,d0
		rts
RevDays		dc.l	.Txt1,.Txt2,.Txt3,.Txt4,.Txt5,.Txt6,.Txt7
.Txt1		dc.b	"Sunday",0
.Txt2		dc.b	"Monday",0
.Txt3		dc.b	"Tuesday",0
.Txt4		dc.b	"Wednesday",0
.Txt5		dc.b	"Thursday",0
.Txt6		dc.b	"Friday",0
.Txt7		dc.b	"Saturday",0
TempName	dc.b	"T:KCX.TEMPFILE",0
AuthorString	dc.b	"AUTHOR",0
CompanyString	dc.b	"COMPANY",0
		EVEN


**********************************************************************
*             Increase process indicator by given amount             *
**********************************************************************
ProgMore	movem.l	RegsAll,-(sp)			; Set draw-pen first
		move.l	ProgRPort(a4),a1
		moveq	#3,d0
		RELLIB	SetAPen,_GfxBase

		move.l	ProgDone(a4),d2			; Get old ratio
		add.l	ProgStep(a4),d2			; Calc new one
		cmp.l	#219,d2				; Keep gauge within bounds
		ble.s	.NoRemake
		move.l	#219,d2
.NoRemake	move.l	d2,ProgDone(a4)

		link	a5,#-(4+4+2+2+2+2)

		lea	ProgTxt(a4),a0
		move.l	a0,FontPtr(a5)
		move.l	d2,-(sp)
		move.l	ProgHandle(a4),a0
		move.l	wd_WScreen(a0),-4(a5)
		moveq	#0,d2
		moveq	#0,d3
		move.w	ProgWinWidth(a4),d2
		move.w	ProgWinHeight(a4),d3
		RELLIB	ComputeFont,_KCXBase
		move.l	(sp)+,d2

		move.l	#5,d0
		DOLIB	ComputeY
		add.w	OffY(a5),d0
		move.l	d0,d1
		move.l	d2,d0
		DOLIB	ComputeX
		move.l	d0,d2
		moveq	#14,d0
		DOLIB	ComputeY
		move.l	d0,d3
		moveq	#40,d0
		DOLIB	ComputeX
		add.w	OffX(a5),d0
		move.l	ProgRPort(a4),a1
		add.l	d0,d2
		add.l	d1,d3
		RELLIB	RectFill,_GfxBase

		unlk	a5
		movem.l	(sp)+,RegsAll
		rts



**********************************************************************
*           Checks for inputs from Purpose/Log msg window            *
**********************************************************************
CheckRevInput	clr.b	LogInputEnd(a4)			; Set end flag for quit
LoopLog		tst.b	LogInputEnd(a4)			; Are we to quit
		bne	LogInputOver
		move.l	LogHandle(a4),a0		; Wait for a message
		move.l	wd_UserPort(a0),a0
		CALLSYS	WaitPort
LoopLogMsg	move.l	LogHandle(a4),a0		; get it
		move.l	wd_UserPort(a0),a0
		RELLIB	GT_GetIMsg,_GadToolsBase
		tst.l	d0
		beq.s	LoopLog
		move.l	d0,a1
		move.l	im_Class(a1),d6
		move.l	d6,d0
		and.l	#(IDCMP_GADGETUP|IDCMP_GADGETDOWN),d0
		beq.s	.NoGad
		move.l	im_IAddress(a1),a3
		move.w	gg_GadgetID(a3),d7
.NoGad		moveq	#0,d0
		move.w	im_Code(a1),d0
		bsr	GetUpperCase
		move.l	d0,d5
		DOLIB	GT_ReplyIMsg

		cmp.l	#IDCMP_VANILLAKEY,d6
		bne.s	.NoVKey
		cmp.b	#ESCKEY,d5
		beq	LogCancelKey
		lea	LogGads(a4),a3
		moveq	#0,d1
.FindGad	move.l	(a3),d0
		beq.s	.NoVKey
		move.l	d0,a0
		move.l	gg_UserData(a0),d0
		cmp.b	d0,d5
		bne.s	.NextGad
		move.l	#IDCMP_GADGETUP,d6
		moveq	#-1,d5
		move.l	d1,d7
		bra.s	.NoVKey
.NextGad	addq.l	#4,a3
		addq.l	#1,d1
		bra.s	.FindGad

.NoVKey		cmp.l	#IDCMP_ACTIVEWINDOW,d6		; Window activated ?
		bne.s	LogWinInAct

		moveq	#GD_Line1,d0
		lea	LogGads(a4),a0
		move.l	LogHandle(a4),a1
		RELLIB	ActStringGad,_KCXBase
		bra	LoopLogMsg

LogWinInAct	cmp.l	#IDCMP_GADGETUP,d6		; Gadget released
		bne	LogNoGadUp

		cmp.w	#GD_LogVer,d7
		bne.s	NoLogVerGad
		tst.l	d5
		bge	LoopLogMsg
		move.l	#GD_LogVer,d0
		lea	LogGads(a4),a0
		move.l	LogHandle(a4),a1
		RELLIB	ActStringGad,_KCXBase
		bra	LoopLogMsg

NoLogVerGad	cmp.w	#GD_LogOnlyText,d7		; Only add text ???
		bne.s	LogNoText
		tst.l	d5
		bge.s	.NoKey
		tst.b	OnlyLog(a4)
		beq	LoopLogMsg
		move.l	d7,d0
		lea	LogGads(a4),a0
		move.l	LogHandle(a4),a5
		bsr	ButtonCheck
.NoKey		move.b	#3,LogInputEnd(a4)
		bra	LoopLogMsg

LogNoText	cmp.w	#GD_LogOk,d7			; Ok gadget ?
		bne.s	LogNoOk
		tst.l	d5
		bge.s	.NoKey
		move.l	d7,d0
		lea	LogGads(a4),a0
		move.l	LogHandle(a4),a5
		bsr	ButtonCheck
.NoKey		move.b	#1,LogInputEnd(a4)		; Yep -> quit input
		bra	LoopLogMsg

LogNoOk		cmp.w	#GD_LogCancel,d7		; Cancel gadget ?
		bne	LoopLogMsg
		tst.l	d5
		bge.s	NoLogCanKey
LogCancelKey	move.l	#GD_LogCancel,d0
		lea	LogGads(a4),a0
		move.l	LogHandle(a4),a5
		bsr	ButtonCheck
NoLogCanKey	move.b	#2,LogInputEnd(a4)
		bra	LoopLogMsg

LogNoGadUp	cmp.l	#IDCMP_CHANGEWINDOW,d6		; Window moved ?
		bne	LoopLogMsg
		move.l	LogHandle(a4),a0		; Store new position
		lea	LogWinLeft(a4),a1
		bsr	StoreWinPos
		bra	LoopLogMsg
LogInputOver	rts




**********************************************************************
*                    Checks for existing revision                    *
**********************************************************************
CheckRevision	move.w	#1,RevRevisionDat
		clr.w	RevRevisionDat+2
		lea	FileName(pc),a0
		move.l	a0,d1				; Try to open file
		move.l	#MODE_OLDFILE,d2
		RELLIB	Open,_DOSBase
		move.l	d0,InHand(a4)
		beq	FileNotFound

		moveq	#14,d6
CheckLines	bsr	DoFGets
		beq.s	FileError
		blt	FileNotFound
		lea	CHeader(pc),a0
		move.l	ARexxBuffer(a4),a1
		moveq	#-1,d7
		bsr	StrCmp				; current line our header ?
		tst.l	d0
		beq.s	IsMine				; Yep -> go on
		dbf	d6,CheckLines			; Loop
FileError	moveq	#0,d7				; Nothing found
		bra.s	EndCheckRev
IsMine		moveq	#7,d6
.GetRevision	bsr	DoFGets
		ble	FileError
		dbf	d6,.GetRevision
		link	a5,#-4
		move.l	ARexxBuffer(a4),a0
		lea	23(a0),a0
		move.l	a0,a2
		move.l	a0,d1
		lea	-4(a5),a0
		move.l	a0,d2
		RELLIB	StrToLong,_DOSBase
		move.l	-4(a5),d0
		move.w	d0,RevRevisionDat
.FindRev	cmp.b	#'.',(a2)+
		bne.s	.FindRev
		move.l	a2,d1
		lea	-4(a5),a0
		move.l	a0,d2
		DOLIB	StrToLong
		move.l	-4(a5),d0
		move.w	d0,RevRevisionDat+2
		unlk	a5
		move.w	RevRevisionDat(pc),d0
		move.w	RevRevisionDat+2(pc),d1
		addq.w	#1,d1
		cmp.w	#100,d1
		blt.s	.GoOn
		addq.w	#1,d0
		moveq	#0,d1
.GoOn		move.w	d0,RevRevisionDat
		move.w	d1,RevRevisionDat+2
		moveq	#1,d7				; Got it
		bra.s	EndCheckRev
FileNotFound	moveq	#-1,d7
EndCheckRev	move.b	d7,OnlyLog(a4)			; store flag
		tst.l	InHand(a4)			; Close file again
		beq.s	.End
		move.l	InHand(a4),d1
		DOLIB	Close
		clr.l	InHand(a4)
.End		rts




**********************************************************************
*                      Skip the following bytes                      *
**********************************************************************
RSkipLines	bsr	DoFGets
		ble.s	.Error
		bsr	DoFPuts
		blt.s	.Error
		dbf	d7,RSkipLines			; Loop
		bsr	ProgMore
		moveq	#1,d0
		rts
.Error		moveq	#0,d0
		rts





**********************************************************************
*                 Writes Log/Purpose message to file                 *
**********************************************************************
RWriteMessage	link	a5,#-8
		lea	CLine(pc),a0
		lea	-8(a5),a1
		bsr	StrCpy
		move.b	#'-',-3(a5)
		moveq	#4,d7				; Now loop for message lines
.LoopPurp	tst.b	(a3)				; line ok ?
		beq.s	.EndPurp
		move.l	OutHand(a4),d1			; Write initial chars
		cmp.b	#4,d7
		bne.s	.NormalLine
		lea	-8(a5),a0
		bra.s	.WriteLine
.NormalLine	lea	CLine(pc),a0
.WriteLine	move.l	a0,d2
		DOLIB	FPuts
		tst.l	d0
		blt.s	.Error
		move.l	a3,a0
		bsr	StrLen
		move.b	#$0a,0(a0,d0)
		move.l	OutHand(a4),d1
		move.l	a3,d2
		DOLIB	FPuts
		tst.l	d0
		blt.s	.Error
.EndPurp	lea	62(a3),a3			; Loop
		dbf	d7,.LoopPurp
		bsr	ProgMore
.Error		unlk	a5
		rts




**********************************************************************
*              Copy message eneterd to desired location              *
**********************************************************************
CopyMessage	lea	LogGads(a4),a2			; Copy contents of string gads
		move.l	(a2),a2
		moveq	#4,d7				; Loop for 5 gadgets
.Loop		move.l	gg_SpecialInfo(a2),a0
		move.l	si_Buffer(a0),a0
		move.l	a3,a1
		bsr	StrCpy				; Copy string to buffer
		move.l	gg_NextGadget(a2),a2
		lea	62(a3),a3			; Next buffer
		dbf	d7,.Loop
		rts




**********************************************************************
*                 Deletes our environment variables                  *
**********************************************************************
DeleteEnvs	tst.l	_DOSBase(a4)
		beq.s	.End
		lea	EnvDayString(pc),a1
		move.l	a1,d1
		move.l	#GVF_GLOBAL_ONLY,d2
		RELLIB	DeleteVar,_DOSBase
		lea	EnvDateString(pc),a1
		move.l	a1,d1
		move.l	#GVF_GLOBAL_ONLY,d2
		DOLIB	DeleteVar
		lea	EnvTimeString(pc),a1
		move.l	a1,d1
		move.l	#GVF_GLOBAL_ONLY,d2
		DOLIB	DeleteVar
.End		rts





**********************************************************************
*           Display Msg that only to be run under Kick2.x            *
**********************************************************************
OnlyKick20	sub.l	a1,a1
		CALLSYS	FindTask
		move.l	d0,a0
		tst.l	pr_CLI(a0)
		beq.s	.FromWB
		lea	DosName,a1
		moveq	#0,d0
		DOLIB	OpenLibrary
		move.l	d0,a6
		DOLIB	Output
		move.l	d0,d1
		move.l	#Only20Txt,d2
		move.l	#Only20TxtEnd-Only20Txt,d3
		DOLIB	Write
		move.l	a6,a1
		CALLSYS	CloseLibrary
		rts
.FromWB		lea	IntName,a1			; try to open IntBase
		moveq	#0,d0
		CALLSYS	OpenLibrary
		tst.l	d0
		beq.s	OnlyKick20Ret
		move.l	d0,a6

		sub.l	a0,a0				; Display requester
		lea	Only20ITxt(pc),a1
		lea	OkITxt(pc),a2
		move.l	a2,a3
		moveq	#0,d0
		moveq	#0,d1
		move.l	#300,d2
		moveq	#60,d3
		DOLIB	AutoRequest

		move.l	a6,a1
		CALLSYS	CloseLibrary
OnlyKick20Ret	rts



**********************************************************************
*                          Display an error                          *
**********************************************************************
DisplayError	tst.l	ErrorBack(a4)
		bge.s	.GoOn
		rts
.GoOn		tst.l	_KCXBase(a4)
		beq.s	.End
		movem.l	RegsAll,-(sp)
		move.l	ErrorBack(a4),d0		; Get error num
		cmp.l	#BROKER_ERR,d0			; Is it the broker set up error ?
		bne.s	NoDoubleMean			; Nope -> go on
		tst.l	DoublePort(a4)			; Did we already had a port ?
		beq.s	NoDoubleMean			; Nope -> go on
		movem.l	(sp)+,RegsAll
.End		rts
NoDoubleMean	move.l	ErrorBack(a4),d0
		lea	BackErrArg(a4),a3
		RELLIB	DisplayError,_KCXBase
		movem.l	(sp)+,RegsAll
		rts



**********************************************************************
*                  Just skip the blanks in a string                  *
**********************************************************************
SkipBlanks	cmp.b	#' ',(a0)			; Loop while there are spaces
		beq.s	.NoEnd				; and tabs
		cmp.b	#$09,(a0)
		beq.s	.NoEnd
		cmp.b	#'"',(a0)
		beq.s	.NoEnd
		cmp.b	#"'",(a0)
		beq.s	.NoEnd
		tst.b	(a0)
		beq.s	.GotEnd
		bra.s	.GotEnd
.NoEnd		addq.l	#1,a0
		bra.s	SkipBlanks
.GotEnd		rts



**********************************************************************
*                           Just a printf                            *
**********************************************************************
PrintF		movem.l	RegsAll,-(sp)
		lea	PutChProc(pc),a2
		move.l	ARexxBuffer(a4),a3
		CALLSYS	RawDoFmt
		movem.l	(sp)+,RegsAll
		rts


**********************************************************************
*                      Small dummy for RawDoFmt                      *
**********************************************************************
PutChProc	move.b	d0,(a3)+
		rts


**********************************************************************
*                         Enable the broker                          *
**********************************************************************
EnableBroker	movem.l	RegsAll,-(sp)
		tst.b	Disabled(a4)			; Don`t enable when disabled
		bne.s	.End
		move.l	MyBroker(a4),a0
		moveq	#1,d0
		LNKLIB	ActivateCxObj,_CxBase
.End		movem.l	(sp)+,RegsAll
		rts



**********************************************************************
*                        Disable broker again                        *
**********************************************************************
DisableBroker	movem.l	RegsAll,-(sp)
		tst.b	Disabled(a4)
		bne.s	.End
		move.l	MyBroker(a4),a0
		moveq	#0,d0
		LNKLIB	ActivateCxObj,_CxBase
		CALLSYS	Forbid
		bsr	OnScreen
		CALLSYS	Permit
		bsr	OnMouse
.End		movem.l	(sp)+,RegsAll
		rts



**********************************************************************
*                        Display the AppIcon                         *
**********************************************************************
OnAppIcon	tst.l	KCXAppIcon(a4)
		bne.s	.End
		lea	MyAppIconText,a0		; Display AppIcon
		move.l	AppPort(a4),a1
		sub.l	a2,a2
		lea	KCXAppIconDef,a3
		move.l	a4,-(sp)
		move.l	_WorkBenchBase(a4),a6
		sub.l	a4,a4
		DOLIB	AddAppIconA
		move.l	(sp)+,a4
		move.l	d0,KCXAppIcon(a4)
		bne.s	.End
.Error		move.l	#APPICON_ERR,ErrorBack(a4)
		bsr	DisplayError
		clr.b	DispAppIcon(a4)
.End		rts






**********************************************************************
*                 Remove the AppIcon from WorkBench                  *
**********************************************************************
OffAppIcon	tst.l	KCXAppIcon(a4)
		beq.s	.NoIcon
		move.l	KCXAppIcon(a4),a0
		RELLIB	RemoveAppIcon,_WorkBenchBase
		clr.l	KCXAppIcon(a4)
.NoIcon		rts




**********************************************************************
*           Checks if the inserted disk should be formated           *
**********************************************************************
CheckFormat	tst.l	FormHandle(a4)			; Formatter window open?
		bne.s	.End				; Yep -> skip question
		sub.l	a0,a0				; PopUp Requester
		lea	MyEasyRequest(pc),a1
		move.l	#EasyTitle,es_Title(a1)
		LOCLEA	DoFormatText,es_TextFormat(a1)
		LOCLEA	FormatGads,es_GadgetFormat(a1)
		sub.l	a2,a2
		move.w	FormatDrive(a4),d0		; Get number of slot
		lea	DriveList(a4),a3
		lsl.w	#2,d0
		lea	0(a3,d0.w),a3			; Get address of string
		RELLIB	EasyRequestArgs,_IntuitionBase
		cmp.l	#1,d0
		bne.s	.End
		bsr	OpenFormWin			; Yep, handle the formatter
.End		rts



**********************************************************************
*                        Start the formatting                        *
**********************************************************************
GoGoGoFormat	lea	MainCom(pc),a0			; Generate main commandline
		lea	FormatCommand(a4),a1
		bsr	StrCpy
		lea	DriveArg,a0
		subq.l	#1,a1
		bsr	StrCpy
		move.w	FormatDrive(a4),d0
		lea	DriveList(a4),a0
		lsl.w	#2,d0
		move.l	0(a0,d0.w),a0
		subq.l	#1,a1
		bsr	StrCpy
		lea	NameArg,a0
		subq.l	#1,a1
		bsr	StrCpy
		move.b	#'"',-1(a1)
		lea	FormatDriveName(a4),a0
		bsr	StrCpy
		move.b	#'"',-1(a1)
		addq.l	#1,a1
		tst.b	MakeIcons(a4)			; Add switches
		bne.s	.NoIcons
		lea	IconsArg,a0
		subq.l	#1,a1
		bsr	StrCpy
.NoIcons	tst.b	QuickFormat(a4)
		beq.s	.NotQuick
		lea	QuickArg,a0
		subq.l	#1,a1
		bsr	StrCpy
.NotQuick	tst.b	FFSDrive(a4)
		beq.s	.NotFFS
		lea	FFSArg,a0
		subq.l	#1,a1
		bsr	StrCpy
.NotFFS		tst.b	NoVerify(a4)
		beq.s	.NoVerify
		lea	VerifyArg,a0
		subq.l	#1,a1
		bsr	StrCpy
.NoVerify	tst.b	Install(a4)
		beq.s	.NoInstall
		lea	InstallArg,a0
		subq.l	#1,a1
		bsr	StrCpy
.NoInstall	tst.b	EjectDrive(a4)
		beq.s	.NoEject
		lea	EjectArg,a0
		subq.l	#1,a1
		bsr	StrCpy
.NoEject	tst.b	DirCache(a4)
		beq.s	.NoCacheMode
		lea	IntModeArg(pc),a0
		subq.l	#1,a1
		bsr	StrCpy
		lea	DirCacheArg(pc),a0
		subq.l	#1,a1
		bsr	StrCpy
		bra.s	.GotCache
.NoCacheMode	tst.b	InterMode(a4)
		beq.s	.GotCache
		lea	IntModeArg(pc),a0
		subq.l	#1,a1
		bsr	StrCpy
.GotCache	LOCLEA	FormatWin,a0			; Open new outhandle
		move.l	a0,d1
		move.l	#MODE_NEWFILE,d2
		RELLIB	Open,_DOSBase
		move.l	d0,FormatIn
		tst.l	d0
		beq.s	.End
		move.l	d0,a0
		add.l	a0,a0
		add.l	a0,a0
		move.l	fh_Type(a0),FormConTask		; Get pointer to console task
		lea	FormatTags(pc),a0		; Start process
		move.l	a0,d1
		DOLIB	CreateNewProc
.End		rts
FormatTags	dc.l	NP_Entry
		dc.l	Formatter
		dc.l	NP_Name
		dc.l	FormSlave
		dc.l	NP_ConsoleTask
FormConTask	dc.l	0
		dc.l	NP_Input
FormatIn	dc.l	0
		dc.l	NP_Output
		dc.l	0
		dc.l	NP_Priority
		dc.l	0
		dc.l	NP_Cli
		dc.l	1
		dc.l	TAG_DONE
MainCom		dc.b	"SYS:System/Format",0
FormSlave	dc.b	"KCX Formatter Slave",0
NewCOSName	dc.b	"*",0
		EVEN

**********************************************************************
*                This is the dummy Formatter process                 *
**********************************************************************
Formatter	link	a5,#-8			; Start the formatter
		move.l	ZeroBase(pc),a4
		move.l	$4.w,a6
		move.l	ThisTask(a6),a3
		tst.l	pr_COS(a3)		; Has this proc got an OutHandle
		bne.s	.End
		tst.l	pr_ConsoleTask(a3)	; Console Task ?
		beq.s	.End
		move.l	pr_COS(a3),-4(a5)	; Open new OutHandle and store old OutHandle
		lea	NewCOSName(pc),a0
		move.l	a0,d1
		move.l	#MODE_NEWFILE,d2
		RELLIB	Open,_DOSBase
		move.l	d0,-8(a5)
		move.l	d0,pr_COS(a3)		; Start command
		lea	FormatCommand(a4),a0
		move.l	a0,d1
		moveq	#0,d2
		DOLIB	SystemTagList
		CALLSYS	Forbid
		tst.l	-8(a5)			; Restore old OutHandle
		beq.s	.End
		move.l	-4(a5),pr_COS(a3)	; Hand close ours
		move.l	-8(a5),d1
		RELLIB	Close,_DOSBase
.End		unlk	a5
		rts






**********************************************************************
*               Filter double Code/Qualifier occurance               *
* D0 = Code                                                          *
* D1 = Qualifier                                                     *
**********************************************************************
FilterDouble	cmp.w	#IECODE_LBUTTON,d0		; If one of the buttons
		bne.s	.NotLeft			; is defined within code and qualifier
		and.w	#~IEQUALIFIER_LEFTBUTTON,d1	; clear the button out of the qualifier
.NotLeft	cmp.w	#IECODE_MBUTTON,d0
		bne.s	.NotMid
		and.w	#~IEQUALIFIER_MIDBUTTON,d1
.NotMid		cmp.w	#IECODE_RBUTTON,d0
		bne.s	.NotRight
		and.w	#~IEQUALIFIER_RBUTTON,d1
.NotRight	rts







**********************************************************************
*                      New OpenScreen function                       *
**********************************************************************
NewOpenScreen	movem.l	d0/a0-a1,-(sp)
		move.l	ZeroBase(pc),a0
		tst.b	Disabled(a0)
		bne.s	.NormalOpen
		tst.b	TagScreens(a0)
		beq.s	.NormalOpen
		movem.l	(sp)+,d0/a0-a1
		sub.l	a1,a1				;  "No" tags
		move.w	ns_Type(a0),d0			; Get type of screen
		move.w	d0,d1
		and.w	#NS_EXTENDED,d1			; Check for extension
		beq.s	.DoPatch
		move.w	d0,d1
		and.w	#~NS_EXTENDED,d0		; Remove extension field
		move.w	d0,ns_Type(a0)
		move.w	d1,d0				; Restore original type field
		move.l	ens_Extension(a0),a1		; Get extension pointer to TagList
		clr.l	ens_Extension(a0)		; Clear extension field
.DoPatch	movem.l	d0/a0-a1,-(sp)			; Same type/adr of newscreen/and ptr to TagList
		bsr.s	DoTaskTagPatch			; Patch TagList
		movem.l	(sp)+,d1/a0-a1			; Reget pointers
		move.w	d1,ns_Type(a0)			; Restore old settings
		and.w	#NS_EXTENDED,d1
		beq.s	.NoEns
		move.l	a1,ens_Extension(a0)		; and Extensionpointer
.NoEns		rts
.NormalOpen	movem.l	(sp)+,d0/a0-a1
		move.l	#OldOpenScreen,d0
		bra	DoRemember




**********************************************************************
*                   New OpenScreenTagList function                   *
**********************************************************************
NewOpenScreenTagList
**********************************************************************
*              Ask for calling task and patch if needed              *
**********************************************************************
DoTaskTagPatch	link	a5,#-768
		movem.l	d2-d7/a0-a6,-(sp)
		move.l	ZeroBase(pc),a4
		sub.l	a3,a3
		tst.b	Disabled(a4)
		bne	.NoTagTsks
		tst.b	TagScreens(a4)
		beq	.NoTagTsks

		move.l	28(sp),a1
		lea	-768(a5),a2			; Copy taglist
		clr.l	ti_Tag(a2)
		move.l	a1,d0
		beq.s	.TagsCopied
.CopyTags	move.l	a1,a0
		moveq	#(ti_SIZEOF/4)-1,d7
.CopyLoop	move.l	(a0)+,(a2)+
		dbf	d7,.CopyLoop
		addq.l	#ti_SIZEOF,a1
		tst.l	(-ti_SIZEOF+ti_Tag)(a1)
		bne.s	.CopyTags
		subq.l	#ti_SIZEOF,a2

.TagsCopied	sub.l	a1,a1				; Find calling task
		CALLSYS	FindTask
		move.l	d0,a3
		moveq	#79,d7
		moveq	#1,d5
		cmp.b	#NT_PROCESS,LN_TYPE(a3)
		bne.s	.IsTask
		moveq	#0,d5
		tst.l	pr_TaskNum(a3)
		beq.s	.IsTask
		move.l	pr_CLI(a3),d0			; Cli ?
		beq.s	.IsTask
		lsl.l	#2,d0
		move.l	d0,a0
		tst.l	cli_Module(a0)			; CLI Module
		beq.s	.IsTask
		move.l	cli_CommandName(a0),d0		; Get CommandName of process
		lsl.l	#2,d0
		move.l	d0,a0
		moveq	#0,d7
		move.b	(a0)+,d7
		bra.s	.GoOnName
.IsTask		move.l	LN_NAME(a3),a0			; It`s a normal Task
		lea	TermName(pc),a1
		bsr	StrCmp
		tst.l	d0
		bne.s	.GoOnName
		sub.l	a3,a3
		bra	.NoTagTsks
.GoOnName	lea	-(170+80)(a5),a1
		bsr	StrNCpy

		lea	TagScrsTasks(a4),a3
		TSTLIST	a3
		beq.s	.NoSpecialTask
		move.l	LH_HEAD(a3),a3			; Get address of first entry
.LoopTasks	move.l	a3,d0
		beq.s	.NoSpecialTask
		lea	tagscr_Name(a3),a0		; Get name
		moveq	#0,d6
		cmp.b	#'-',(a0)
		bne.s	.NoSkipTask
		moveq	#1,d6
		addq.l	#1,a0
.NoSkipTask	tst.b	d5
		beq.s	.IsProc
		lea	-(170+80)(a5),a1
		bsr	StrCmp
		tst.l	d0
		beq.s	.GotTask
		bra.s	.NotSameTask
.IsProc		move.l	a0,d1
		lea	-170(a5),a0			; Get buffer for pattern
		move.l	a0,d2
		move.l	#170,d3
		RELLIB	ParsePatternNoCase,_DOSBase	; Tokenize pattern
		lea	-170(a5),a0			; Check if name of task matches
		move.l	a0,d1
		lea	-(170+80)(a5),a0
		move.l	a0,d2
		DOLIB	MatchPatternNoCase
		tst.l	d0
		bne.s	.GotTask
.NotSameTask	SUCC	a3,a3
		bra.s	.LoopTasks

.GotTask	tst.b	d6				; Task to be excluded ?
		bne.s	.NoTagTsks			; Yep -> no patch
		bsr	DoTagPatches			; Patch list
		tst.b	d0				; Patches made ?
		beq.s	.NoTagTsks			; Nope -> normal openup
		bra.s	.DonePatches

.NoSpecialTask	move.l	GlobalTagAdr(a4),d0		; Check for global settings
		beq.s	.NoTagTsks			; No settings -> No Patch
		move.l	d0,a3				; Do Patch
		bsr	DoTagPatches
		tst.b	d0				; Changes made ?
		beq.s	.NoTagTsks			; Nope -> Normal openup

.DonePatches	move.l	#TAG_DONE,(a2)+
		clr.l	(a2)
		lea	-768(a5),a0
		move.l	a0,28(sp)			; Store pointer to new TagList
.NoTagTsks	move.l	a3,d0
		movem.l	(sp)+,d2-d7/a0-a6
		move.l	d0,-(sp)
		move.l	#OldOpenScreenTagList,d0	; Open Screen
		bsr	DoRemember
		move.l	(sp)+,d1
		unlk	a5
		movem.l	RegsAll,-(sp)
		tst.l	d1
		beq.s	.End
		move.l	d1,a3
		tst.l	d0
		beq.s	.End
		move.l	ZeroBase(pc),a4
		tst.b	TagScreens(a4)
		beq.s	.End
		movem.l	d0/a3,-(sp)
		tst.b	tagscr_CenterScr(a3)		; recenter screen ?
		beq.s	.NoCenter
		move.l	d0,a0				; Go on
		bsr	GetDispCols
		move.w	sc_ViewPort+vp_Modes(a0),d1
		andi.w	#V_HIRES|V_SUPERHIRES,d1
		bne.s	.SuperHigh
		lsr.w	#1,d5
		bra.s	.MoveOK
.SuperHigh	andi.w	#V_SUPERHIRES,d1
		beq.s	.MoveOK
		add.w	d5,d5
.MoveOK		sub.w	sc_Width(a0),d5
		bls.s	.NoCenter
		lsr.w	#1,d5
		sub.w	sc_LeftEdge(a0),d5
		beq.s	.NoCenter
		moveq	#0,d1
		move.w	d5,d0
		RELLIB	MoveScreen,_IntuitionBase
.NoCenter	movem.l	(sp)+,d0/a3
		tst.b	tagscr_BlackBorder(a3)		; Add BlackBorder to screen?
		beq.s	.End
		bsr	DoBlackBorder			; Yep
.End		movem.l	(sp)+,RegsAll
		rts
TermName	dc.b	"term Beta Task",0
		EVEN


**********************************************************************
*                       Add a tag to an array                        *
* D0 = TagItem                                                       *
* D1 = TagValue                                                      *
**********************************************************************
AddTag		bset	#31,d0
		move.l	d0,ti_Tag(a2)
		move.l	d1,ti_Data(a2)
		addq.l	#ti_SIZEOF,a2
		rts


**********************************************************************
*   Some fine routine to find a tag with shorter calling procedure   *
*                  Same as FindTagItem()/UtilityLib                  *
**********************************************************************
FindTag		bset	#31,d0
		lea	-768(a5),a0
		RELLIB	FindTagItem,_UtilityBase
		tst.l	d0
		rts



**********************************************************************
*               Do the patches for a given list-entry                *
* A2 = Current address within array                                  *
* A3 = Pointer to entry                                              *
* A5 = Pointer to buffer for array on stack                          *
**********************************************************************
DoTagPatches	tst.b	tagscr_HardPatch(a3)		; Patch existing Tags/ens_ ???
		bne.s	.DoPens
		move.l	32(sp),d0			; Get pointer to old TagList
		bne	.HardDone			; Yep had one, no patch
		move.l	28(sp),d0			; Get pointer to screen
		beq.s	.DoPens				; No Screen, no nothing -> go on
		move.l	d0,a0
		btst	#4,ns_Type(a0)			; Extended screen ?
		beq.s	.DoPens				; Nope -> patch
		tst.l	ens_Extension(a0)		; Tags ???
		bne	.HardDone			; Yep -> no patch
		bclr	#4,ns_Type(a0)			; Clear extended field (using tags now)

.DoPens		tst.b	tagscr_Pens(a3)			; Check for pens and patch, if needed
		beq.s	.HasPens
		moveq	#SA_Pens-$80000000,d0
		bsr.s	FindTag
		bne.s	.HasPens
		tst.b	tagscr_SystemPens(a3)
		beq.s	.Use20Pens
		move.l	$4.w,a0				; Check for 3.0 and set pens accordingly
		cmp.w	#39,LIB_VERSION(a0)
		beq.s	.Get30Pens
.Use20Pens	lea	PenDatas(pc),a0
		bra.s	.SetPens
.Get30Pens	lea	PenDatas30(pc),a0
.SetPens	move.l	a0,d1
		moveq	#SA_Pens-$80000000,d0
		bsr	AddTag

.HasPens	tst.b	tagscr_AutoScroll(a3)		; AutoScroll ???
		beq.s	.NoAutoScroll
		moveq	#SA_Overscan-$80000000,d0
		bsr.s	FindTag
		beq.s	.NoCompAuto
		move.l	d0,a0				; Patch Overscan-Tag to match requirements
		move.l	#OSCAN_STANDARD,ti_Data(a0)
		bra.s	.CheckAutoScr
.NoCompAuto	moveq	#SA_Overscan-$80000000,d0
		move.l	#OSCAN_STANDARD,d1
		bsr	AddTag

.CheckAutoScr	moveq	#SA_AutoScroll-$80000000,d0	; Check for AutoScroll and patch if needed
		bsr	FindTag
		beq.s	.AddAutoScr
		move.l	d0,a0
		move.l	#-1,ti_Data(a0)
		bra.s	.NoAutoScroll
.AddAutoScr	moveq	#SA_AutoScroll-$80000000,d0
		moveq	#-1,d1
		bsr	AddTag

.NoAutoScroll	tst.b	tagscr_Font(a3)			; NoFont ?
		beq.s	.NoFont
		moveq	#SA_Font-$80000000,d0
		bsr	FindTag
		bne.s	.GotFontTag
		moveq	#SA_Font-$80000000,d0
		lea	Topaz80(pc),a0
		move.l	a0,d1
		bsr	AddTag
		bra.s	.NoFont
.GotFontTag	move.l	d0,a0
		lea	Topaz80(pc),a1
		move.l	a1,ti_Data(a0)

.NoFont		move.l	$4.w,a0				; Check for OS3
		cmp.w	#39,LIB_VERSION(a0)
		blt.s	.NoLikeWB

		tst.b	tagscr_Interleave(a3)		; Interleaved BitMaps ?
		beq.s	.NoInterleave
		moveq	#SA_Interleaved-$80000000,d0
		bsr	FindTag
		bne.s	.GotInterleave
		moveq	#SA_Interleaved-$80000000,d0
		moveq	#-1,d1
		bsr	AddTag
		bra.s	.NoInterleave
.GotInterleave	move.l	d0,a0
		move.l	#-1,ti_Data(a0)

.NoInterleave	tst.b	tagscr_LikeWB(a3)		; Like WB ?
		beq.s	.NoLikeWB
		moveq	#SA_LikeWorkbench-$80000000,d0
		bsr	FindTag
		bne.s	.GotLikeWB
		moveq	#SA_LikeWorkbench-$80000000,d0
		moveq	#-1,d1
		bsr	AddTag
		bra	.NoScrWidth
.GotLikeWB	move.l	d0,a0
		move.l	#-1,ti_Data(a0)
		bra	.NoScrWidth

.NoLikeWB	tst.b	tagscr_Monitor(a3)		; Set DisplayID ?
		beq	.NoScrWidth
		link	a5,#-dis_SIZEOF
		sub.l	a0,a0
		lea	-dis_SIZEOF(a5),a1
		move.l	#dis_SIZEOF,d0
		move.l	#DTAG_DISP,d1
		move.l	tagscr_DisplayID(a3),d2
		RELLIB	GetDisplayInfoData,_GfxBase
		tst.l	d0
		bne.s	.CheckInfo
		unlk	a5
		bra.s	.NoScrWidth
.CheckInfo	lea	-dis_SIZEOF(a5),a0
		tst.w	dis_NotAvailable(a0)
		beq.s	.DoIt
		unlk	a5
		bra.s	.NoScrWidth
.DoIt		unlk	a5
		moveq	#SA_DisplayID-$80000000,d0
		bsr	FindTag
		beq.s	.NoIDFound
		move.l	d0,a0
		move.l	tagscr_DisplayID(a3),d0
		move.l	d0,ti_Data(a0)
		bra.s	.NoDisplayID
.NoIDFound	moveq	#SA_DisplayID-$80000000,d0
		move.l	tagscr_DisplayID(a3),d1
		bsr	AddTag

.NoDisplayID	move.l	tagscr_Height(a3),d7		; Height specified ???
		beq.s	.NoScrHeight
		moveq	#SA_Height-$80000000,d0
		bsr	FindTag
		bne.s	.GotScrHeight
		moveq	#SA_Height-$80000000,d0
		move.l	d7,d1
		bsr	AddTag
		bra.s	.NoScrHeight
.GotScrHeight	move.l	d0,a0
		move.l	d7,ti_Data(a0)

.NoScrHeight	move.l	tagscr_Width(a3),d7		; Width specified ???
		beq.s	.NoScrWidth
		moveq	#SA_Width-$80000000,d0
		bsr	FindTag
		bne.s	.GotScrWidth
		moveq	#SA_Width-$80000000,d0
		move.l	d7,d1
		bsr	AddTag
		bra.s	.NoScrWidth
.GotScrWidth	move.l	d0,a0
		move.l	d7,ti_Data(a0)

.NoScrWidth	moveq	#1,d0
		rts
.HardDone	moveq	#0,d0
		rts
PenDatas	dc.w	3,1,1,2,1,3,1,0,2,-1
PenDatas30	dc.w	3,1,1,2,1,3,1,0,2,1,2,1,-1
Topaz80		dc.l	TopazName
		dc.w	8
		dc.b	FS_NORMAL,FPF_ROMFONT
TopazName	dc.b	"topaz.font",0
		even



**********************************************************************
*                     Install/Remove BlackBorder                     *
**********************************************************************
DoBlackBorder	tst.b	BlackBorderOk(a4)
		beq.s	NoBlackOK
		tst.l	d0
		beq.s	NoBlackOK
DoBlackBorderOk	movem.l	d0-d1/a0-a3,-(sp)		; Add BlackBorder tag to ViewPort
		move.l	d0,a2
		move.l	sc_ViewPort+vp_ColorMap(a2),a0
		lea	BlackTags(pc),a1
		RELLIB	VideoControl,_GfxBase
		move.l	a2,a0
		RELLIB	MakeScreen,_IntuitionBase
		DOLIB	ViewAddress			; I don`t use RethinDisplay() `cause
		lea	sc_ViewPort(a2),a1		; it makeweird thinks on a OpenWorkBench() call
		move.l	d0,a2				; just calc new copper-lists for ColorMap
		move.l	d0,a0
		RELLIB	MakeVPort,_GfxBase
		move.l	a2,a1
		DOLIB	MrgCop
		move.l	a2,a1
		DOLIB	LoadView
		movem.l	(sp)+,d0-d1/a0-a3
NoBlackOK	rts
BlackTags	dc.l	VTAG_BORDERBLANK_SET,-1,TAG_DONE,0



**********************************************************************
*                  Redo all screens for BlackBorder                  *
**********************************************************************
ReBlackBorder	move.l	GlobalTagAdr(a4),d0
		beq.s	NoReBlack
		move.l	d0,a0
		tst.b	tagscr_BlackBorder(a0)
		beq.s	NoReBlack
		move.l	#VTAG_BORDERBLANK_SET,BlackTags
ReBlackBorder2	tst.b	BlackBorderOk(a4)
		beq.s	NoReBlack
		moveq	#0,d0
		RELLIB	LockIBase,_IntuitionBase
		move.l	ib_FirstScreen(a6),a2
		move.l	d0,a0
		DOLIB	UnlockIBase
		lea	WBScrName(pc),a0
		RELLIB	LockPubScreen,_IntuitionBase
		move.l	d0,a3
		CALLSYS	Forbid
.LoopScreens	move.l	a2,d0
		beq.s	.NoScrs
		movem.l	a2/a3,-(sp)
		bsr	DoBlackBorderOk
		movem.l	(sp)+,a2/a3
		move.l	sc_NextScreen(a2),a2
		bra.s	.LoopScreens
.NoScrs		CALLSYS	Permit
		move.l	a3,d0
		beq.s	NoReBlack
		move.l	d0,a1
		lea	WBScrName(pc),a0
		RELLIB	UnlockPubScreen,_IntuitionBase
NoReBlack	rts



**********************************************************************
*               Get currently available DisplayColumns               *
**********************************************************************
GetDispCols	move.l	_GfxBase(a4),a3
		move.w	gb_NormalDisplayColumns(a3),d5
		rts



**********************************************************************
*                      Screen to front routine                       *
**********************************************************************
NewScreenToFront
		move.l	#OldScreenToFront,d0
		bsr.s	DoRemember
		bra	RestoreWindow


**********************************************************************
*                           Screen to back                           *
**********************************************************************
NewScreenToBack	move.l	#OldScreenToBack,d0
		bsr.s	DoRemember
		bra	RestoreWindow


**********************************************************************
*                         Workbench to front                         *
**********************************************************************
NewWBenchToFront
		move.l	#OldWBenchToFront,d0
		bsr.s	DoRemember
		bra	RestoreWindow


**********************************************************************
*                         Workbench to back                          *
**********************************************************************
NewWBenchToBack	move.l	#OldWBenchToBack,d0
		bsr.s	DoRemember
		bra	RestoreWindow


**********************************************************************
*                            Close screen                            *
**********************************************************************
NewCloseScreen	bsr	GetSemaphore
		move.l	ZeroBase(pc),a1
		lea	ScreenList(a1),a1
		moveq	#MAXSCREENS-1,d0
.Loop		cmp.l	(a1),a0
		beq.s	.GotIt
		addq.l	#8,a1
		dbf	d0,.Loop
.End		move.l	ZeroBase(pc),a1
		pea	RestoreWindow(pc)
		move.l	OldCloseScreen(a1),-(sp)
		bsr	FreeSemaphore
		rts
.GotIt		clr.l	(a1)
		bra.s	.End




**********************************************************************
*                  Stub for calling system routines                  *
**********************************************************************
DoRemember	bsr	GetSemaphore
		bsr.s	RememberWindow			; Remember the active window
		move.l	a0,-(sp)
		move.l	ZeroBase(pc),a0
		move.l	0(a0,d0.w),d0
		move.l	(sp)+,a0
		move.l	d0,-(sp)
		bsr	FreeSemaphore
		rts



**********************************************************************
*                   Store screen/window addresses                    *
**********************************************************************
RememberWindow	movem.l	RegsAll,-(sp)
		move.l	ZeroBase(pc),a4
		CALLSYS	Forbid
		move.l	_IntuitionBase(a4),a0
		move.l	ib_ActiveWindow(a0),a1
		move.l	ib_ActiveScreen(a0),a0
		CALLSYS	Permit
		move.l	a0,d0
		beq.s	.End
		move.l	a1,d0
		beq.s	.End
		lea	ScreenList(a4),a2		; Check if screen`s already in list
		moveq	#MAXSCREENS-1,d0
.Find		cmp.l	(a2),a0
		beq.s	.GotIt
		addq.l	#8,a2
		dbf	d0,.Find
		lea	ScreenList(a4),a2		; Store screen
		moveq	#MAXSCREENS-1,d0
.Stick		tst.l	(a2)
		beq.s	.Save
		addq.l	#8,a2
		dbf	d0,.Stick
		bra.s	.End
.Save		move.l	a0,(a2)+
		move.l	a1,(a2)
		bra.s	.End
.GotIt		move.l	a1,4(a2)
.End		movem.l	(sp)+,RegsAll
		rts




**********************************************************************
*       This routine reactivates the previously active window        *
**********************************************************************
RestoreWindow	bsr	GetSemaphore
		movem.l	RegsAll,-(sp)			; Search for active screen in list
		move.l	ZeroBase(pc),a4
		lea	ScreenList(a4),a1		; Get ScreenList
		move.l	_IntuitionBase(a4),a6
		move.l	ib_FirstScreen(a6),a0		; Get first screen
		move.l	a0,d0
		beq.s	.End
		moveq	#MAXSCREENS-1,d0
.Loop		cmp.l	(a1),a0				; Hunt for screen in list
		beq.s	.GotIt
		addq.l	#8,a1
		dbf	d0,.Loop
		move.l	a0,a2				; Copy pointer to screen
		bra.s	.FindWin			; This screen isn`t within the lis
							; So activate the window underneith the mouse
.GotIt		move.l	4(a1),a0			; ReActivate it`s window
		move.l	(a1),a1				; Get pointer to screen
		move.l	a1,a2				; Copy pointer to screen
		move.l	sc_FirstWindow(a1),a1		; Window still living ???
.CheckWinEx	move.l	a1,d0
		beq.s	.FindWin
		cmp.l	a1,a0				; Yep -> activate it
		beq.s	.Skip
		move.l	wd_NextWindow(a1),a1
		bra.s	.CheckWinEx
.FindWin	RELLIB	FindWindow,_KCXBase		; Otherwise activate window underneith the mouse
		tst.l	d0				; Got a window ?
		beq.s	.TakeFirst			; Nope -> Take first available
		move.l	d0,a0
		move.l	lr_Window(a0),d0		; Window for layer ???
		beq.s	.TakeFirst			; Nope -> Take first window
		move.l	d0,a0
		bra.s	.Skip
.TakeFirst	move.l	sc_FirstWindow(a2),d0		; Get first window
		beq.s	.End				; None there -> Quit
		move.l	d0,a0				; Activate window
.Skip		tst.b	WindowReAct(a4)
		beq.s	.End
		RELLIB	ActivateWindow,_IntuitionBase
.End		movem.l	(sp)+,RegsAll
		bsr	FreeSemaphore
		rts




**********************************************************************
*                       New CreateUpfrontLayer                       *
**********************************************************************
NewCreateUpfrontLayer
		movem.l	d0-d7/a0-a6,-(sp)
		move.l	ZeroBase(pc),a4
		tst.b	Disabled(a4)
		bne	.NoExplode
		tst.b	ExpWindows(a4)
		beq	.NoExplode
		tst.l	ExpFrames(a4)
		beq	.NoExplode
		and.l	#LAYERBACKDROP,d4		; Backdrop-Layer ?
		bne	.NoExplode			; No explode
		move.l	a0,a3
		bsr	GetScreen
		move.l	a3,d7
		beq	.NoExplode
		move.l	ExpFrames(a4),-(sp)
		sub.l	d0,d2				; Get dims
		sub.l	d1,d3
		tst.b	MouseCenter(a4)
		bne.s	.GetMousePos
		movem.l	d0-d3/d6-d7/a0-a6,-(sp)
		link	a5,#-dim_SIZEOF
		lea	sc_ViewPort(a3),a0
		RELLIB	GetVPModeID,_GfxBase
		sub.l	a0,a0
		lea	-dim_SIZEOF(a5),a1
		move.l	d0,d2
		move.l	#dim_SIZEOF,d0
		move.l	#DTAG_DIMS,d1
		DOLIB	GetDisplayInfoData
		lea	-dim_SIZEOF+dim_TxtOScan(a5),a0
		move.w	ra_MaxX(a0),d4
		lsr.w	#1,d4
		move.w	ra_MaxY(a0),d5
		lsr.w	#1,d5
		move.w	sc_LeftEdge(a3),d0
		neg.w	d0
		add.w	d0,d4
		move.w	sc_TopEdge(a3),d0
		neg.w	d0
		add.w	d0,d5
		ext.l	d4
		ext.l	d5
		unlk	a5
		movem.l	(sp)+,d0-d3/d6-d7/a0-a6
		bra.s	.GotPos
.GetMousePos	move.w	sc_MouseX(a3),d4
		bge.s	.MouseX
		moveq	#0,d4
.MouseX		cmp.w	sc_Width(a3),d4
		blt.s	.MouseXa
		move.w	sc_Width(a3),d4
		subq.w	#1,d4
.MouseXa	move.w	sc_MouseY(a3),d5
		bge.s	.MouseY
		moveq	#0,d5
.MouseY		cmp.w	sc_Height(a3),d5
		blt.s	.MouseYa
		move.w	sc_Height(a3),d5
		subq.w	#1,d5
.MouseYa	ext.l	d4
		ext.l	d5
.GotPos		movem.l	d2-d3,-(sp)
		sub.l	d5,d1
		move.l	d1,-(sp)
		sub.l	d4,d0
		move.l	d0,-(sp)
		pea	1.W
		pea	1.W
		movem.l	d4-d5,-(sp)
		move.l	a3,-(sp)
		jsr	LayerFade
		lea	40(sp),sp
.NoExplode	movem.l	(sp)+,d0-d7/a0-a6
		move.l	a5,-(sp)
		move.l	ZeroBase(pc),a5
		move.l	OldCreateUpfrontLayer(a5),a5
		jsr	(a5)
		move.l	(sp)+,a5
		rts


**********************************************************************
*                     New CreateUpfrontHookLayer                     *
**********************************************************************
NewCreateUpfrontHookLayer
		movem.l	d0-d7/a0-a6,-(sp)
		move.l	ZeroBase(pc),a4
		tst.b	Disabled(a4)
		bne	.NoExplode
		tst.b	ExpWindows(a4)
		beq	.NoExplode
		tst.l	ExpFrames(a4)
		beq	.NoExplode
		and.l	#LAYERBACKDROP,d4		; BackDrop-Layer ?
		bne	.NoExplode			; No explode
		move.l	a0,a3
		bsr	GetScreen
		move.l	a3,d7
		beq	.NoExplode
		move.l	ExpFrames(a4),-(sp)
		sub.l	d0,d2				; Get dims
		sub.l	d1,d3
		tst.b	MouseCenter(a4)
		bne.s	.GetMouse
		movem.l	d0-d3/d6-d7/a0-a6,-(sp)
		link	a5,#-dim_SIZEOF
		lea	sc_ViewPort(a3),a0
		RELLIB	GetVPModeID,_GfxBase
		sub.l	a0,a0
		lea	-dim_SIZEOF(a5),a1
		move.l	d0,d2
		move.l	#dim_SIZEOF,d0
		move.l	#DTAG_DIMS,d1
		DOLIB	GetDisplayInfoData
		lea	-dim_SIZEOF+dim_TxtOScan(a5),a0
		move.w	ra_MaxX(a0),d4
		lsr.w	#1,d4
		move.w	ra_MaxY(a0),d5
		lsr.w	#1,d5
		move.w	sc_LeftEdge(a3),d0
		neg.w	d0
		add.w	d0,d4
		move.w	sc_TopEdge(a3),d0
		neg.w	d0
		add.w	d0,d5
		ext.l	d4
		ext.l	d5
		unlk	a5
		movem.l	(sp)+,d0-d3/d6-d7/a0-a6
		bra.s	.GotPos
.GetMouse	move.w	sc_MouseX(a3),d4
		bge.s	.MouseX
		moveq	#0,d4
.MouseX		cmp.w	sc_Width(a3),d4
		blt.s	.MouseXa
		move.w	sc_Width(a3),d4
		subq.w	#1,d4
.MouseXa	move.w	sc_MouseY(a3),d5
		bge.s	.MouseY
		moveq	#0,d5
.MouseY		cmp.w	sc_Height(a3),d5
		blt.s	.MouseYa
		move.w	sc_Height(a3),d5
		subq.w	#1,d5
.MouseYa	ext.l	d4
		ext.l	d5
.GotPos		move.l	d3,-(sp)
		move.l	d2,-(sp)
		sub.l	d5,d1
		move.l	d1,-(sp)
		sub.l	d4,d0
		move.l	d0,-(sp)
		pea	1.W
		pea	1.W
		move.l	d5,-(sp)
		move.l	d4,-(sp)
		move.l	a3,-(sp)
		jsr	LayerFade
		lea	40(sp),sp
.NoExplode	movem.l	(sp)+,d0-d7/a0-a6
		move.l	a5,-(sp)
		move.l	ZeroBase(pc),a5
		move.l	OldCreateUpfrontHookLayer(a5),a5
		jsr	(a5)
		move.l	(sp)+,a5
		rts





**********************************************************************
*                      This is our DeleteLayer                       *
**********************************************************************
NewDeleteLayer	movem.l	d2-d7/a2-a6,-(sp)
		move.l	ZeroBase(pc),a4
		moveq	#0,d4				; Clear screen pointer
		move.w	lr_Flags(a1),d0			; BACKDROP layer ?
		and.w	#LAYERBACKDROP,d0
		bne.s	.BackLayer			; Yep -> no frames
		move.l	a1,a3
		move.l	lr_LayerInfo(a3),a3
		bsr	GetScreen			; Get layer`s screen
		move.l	a3,d4
		move.w	lr_MinX(a1),d7			; Get layers dims
		move.w	lr_MinY(a1),d1
		move.w	lr_MaxX(a1),d2
		move.w	lr_MaxY(a1),d3
		ext.l	d7
		ext.l	d1
		ext.l	d2
		ext.l	d3
		sub.l	d7,d2
		sub.l	d1,d3
.BackLayer	movem.l	d1-d7,-(sp)
		move.l	OldDeleteLayer(a4),a3		; Delete layer
		jsr	(a3)
		movem.l	(sp)+,d1-d7
		move.l	d0,a3
		beq	.NoImplode
		tst.b	Disabled(a4)
		bne	.NoImplode
		tst.b	ExpWindows(a4)
		beq	.NoImplode
		tst.l	ImpFrames(a4)
		beq	.NoImplode
		move.l	d7,d0

		tst.l	d4				; Do we have a screen ?
		beq	.NoImplode
		move.l	a3,-(sp)
		move.l	ImpFrames(a4),-(sp)
		move.l	d4,a3
		move.l	d2,d5
		move.l	d3,d6
		neg.l	d5
		neg.l	d6
		move.l	d6,-(sp)
		move.l	d5,-(sp)
		tst.b	MouseCenter(a4)
		bne.s	.GetMouse
		movem.l	d0-d4/d7/a0-a6,-(sp)
		link	a5,#-dim_SIZEOF
		lea	sc_ViewPort(a3),a0
		RELLIB	GetVPModeID,_GfxBase
		sub.l	a0,a0
		lea	-dim_SIZEOF(a5),a1
		move.l	d0,d2
		move.l	#dim_SIZEOF,d0
		move.l	#DTAG_DIMS,d1
		DOLIB	GetDisplayInfoData
		lea	-dim_SIZEOF+dim_TxtOScan(a5),a0
		move.w	ra_MaxX(a0),d5
		lsr.w	#1,d5
		move.w	ra_MaxY(a0),d6
		lsr.w	#1,d6
		move.w	sc_LeftEdge(a3),d0
		neg.w	d0
		add.w	d0,d5
		move.w	sc_TopEdge(a3),d0
		neg.w	d0
		add.w	d0,d6
		ext.l	d5
		ext.l	d6
		unlk	a5
		movem.l	(sp)+,d0-d4/d7/a0-a6
		sub.l	d1,d6
		move.l	d6,-(sp)
		sub.l	d7,d5
		move.l	d5,-(sp)
		bra.s	.GotPos
.GetMouse	move.w	sc_MouseY(a3),d5
		bge.s	.MouseY
		moveq	#0,d5
.MouseY		cmp.w	sc_Height(a3),d5
		blt.s	.MouseYa
		move.w	sc_Height(a3),d5
		subq.w	#1,d5
.MouseYa	ext.l	d5
		sub.l	d1,d5
		move.l	d5,-(sp)
		move.w	sc_MouseX(a3),d5
		bge.s	.MouseX
		moveq	#0,d5
.MouseX		cmp.w	sc_Width(a3),d5
		blt.s	.MouseXa
		move.w	sc_Width(a3),d5
		subq.w	#1,d5
.MouseXa	ext.l	d5
		sub.l	d7,d5
		move.l	d5,-(sp)
.GotPos		move.l	d3,-(sp)
		move.l	d2,-(sp)
		move.l	d1,-(sp)
		move.l	d7,-(sp)
		move.l	d4,-(sp)
		jsr	LayerFade			; Implode
		lea	40(sp),sp
		move.l	(sp)+,a3
.NoImplode	move.l	a3,d0				; Restore returnvalue
		movem.l	(sp)+,d2-d7/a2-a6
		rts



**********************************************************************
*                    New Move/Size Layer function                    *
**********************************************************************
NewMoveSizeLayer
		movem.l	d1-d7/a0-a6,-(sp)
		move.l	ZeroBase(pc),a4
		movem.l	RegsAll,-(sp)
		tst.b	Disabled(a4)
		bne.s	.NoMove
		tst.b	ExpWindows(a4)
		beq.s	.NoMove
		move.l	a0,a3
		move.l	lr_LayerInfo(a3),a3
		bsr.s	GetScreen
		move.l	a3,d4				; Get Screen pointer
		beq.s	.NoMove
		tst.w	d2
		bne.s	.NoMoveFrames
		tst.w	d3
		bne.s	.NoMoveFrames
		move.l	MoveFrames(a4),d7
		beq.s	.NoMove
		bra.s	.DoSize
.NoMoveFrames	move.l	SizeFrames(a4),d7
		beq.s	.NoMove
.DoSize		move.l	d7,-(sp)
		move.l	d3,-(sp)
		move.l	d2,-(sp)
		move.l	d1,-(sp)
		move.l	d0,-(sp)
		movem.w	lr_MinX(a0),d0-d3
		ext.l	d0
		ext.l	d1
		ext.l	d2
		ext.l	d3
		sub.l	d0,d2
		sub.l	d1,d3
		move.l	d3,-(sp)
		move.l	d2,-(sp)
		move.l	d1,-(sp)
		move.l	d0,-(sp)
		move.l	d4,-(sp)
		jsr	LayerFade
		lea	40(sp),sp
.NoMove		movem.l	(sp)+,RegsAll
		move.l	ZeroBase(pc),a3
		move.l	OldMoveSizeLayer(a3),a3		; Move layer
		jsr	(a3)
		movem.l	(sp)+,d1-d7/a0-a6
		rts


**********************************************************************
*             Get address of corresponding to Layer_Info             *
* A3 = Address of Layer_Info                                         *
**********************************************************************
GetScreen	movem.l	d0-d7/a0-a1,-(sp)
		move.l	_IntuitionBase(a4),a0
		move.l	ib_FirstScreen(a0),a0
.FindScreen	move.l	a0,d0
		beq.s	.GotScreen
		move.l	a0,a1
		lea	sc_LayerInfo(a1),a1
		cmp.l	a1,a3
		beq.s	.GotScreen
		move.l	sc_NextScreen(a0),a0
		bra.s	.FindScreen
.GotScreen	move.l	a0,a3
		movem.l	(sp)+,d0-d7/a0-a1
		rts



**********************************************************************
*                      New autorequest routine                       *
**********************************************************************
NewAutoRequest	pea	OldAutoRequest
		bra.s	DoRequest


**********************************************************************
*                      New Easyrequest routine                       *
**********************************************************************
NewEasyRequestArgs
		pea	OldEasyRequestArgs
		bra.s	DoRequest




**********************************************************************
*                  Do stuff for calling a requester                  *
**********************************************************************
DoRequest	movem.l	d1-d7/a0-a6,-(sp)
		move.l	ZeroBase(pc),a4
		move.l	d0,-(sp)
		bsr	OnScreen
		bsr	GetSemaphore
		move.l	60(sp),d0
		addq.w	#1,RequestsOpen(a4)
		move.b	#1,TimeReset(a4)
		clr.b	ScrTimeOut(a4)
		move.l	0(a4,d0.w),d7
		bsr	FreeSemaphore
		move.l	(sp)+,d0
		pea	.End
		move.l	d7,-(sp)
		movem.l	8(sp),d1-d7/a0-a6
		rts
.End		bsr	GetSemaphore
		move.l	ZeroBase(pc),a4
		subq.w	#1,RequestsOpen(a4)
		movem.l	(sp)+,d1-d7/a0-a6
		addq.l	#4,sp
		bra	FreeSemaphore


**********************************************************************
*                          New DisplayBeep                           *
**********************************************************************
NewDisplayBeep	bsr	GetSemaphore
		move.l	a4,-(sp)
		move.l	ZeroBase(pc),a4
		move.l	OldDisplayBeep(a4),d0
		move.b	NoDisplayBeep(a4),d1
		move.l	(sp)+,a4
		tst.b	d1
		bne.s	.End
		pea	.End(pc)
		move.l	d0,-(sp)
		rts
.End		bra	FreeSemaphore




**********************************************************************
*       New MatchFirst routine, for creating a "real" *-Joker        *
**********************************************************************
NewMatchFirst	movem.l	a4/a5,-(sp)
		link	a5,#-$102
		move.l	d1,a0
		lea	-$100(a5),a1
		move.l	a1,d1
.CopyName	move.b	(a0)+,d0
		cmp.b	#'*',d0
		bne.s	.GoOn
		move.b	#'#',(a1)+
		move.b	#'?',d0
.GoOn		move.b	d0,(a1)+
		tst.b	d0
		bne.s	.CopyName
		move.l	ZeroBase(pc),a4
		move.l	OldMatchFirst(a4),a4
		jsr	(a4)
		unlk	a5
		movem.l	(sp)+,a4/a5
		rts



**********************************************************************
*                 New Open WorkBench for BlackBorder                 *
**********************************************************************
GD_Display                             EQU    0
NewOpenWorkBench
		move.l	#OldOpenWorkBench,d0
		bsr	DoRemember
		movem.l	RegsAll,-(sp)
		CALLSYS	Forbid
		move.l	ZeroBase(pc),a4
		move.b	OldClockState(a4),d0
		move.b	d0,ShowClock(a4)
		move.l	#GD_Display,d1
		lea	ClockDefGads(a4),a0
		move.l	ClockDefHandle(a4),a1
		RELLIB	SetCheckBox,_KCXBase
		CALLSYS	Permit
		movem.l	(sp)+,RegsAll
		rts



**********************************************************************
*                  Patched close workbench routine                   *
**********************************************************************
NewCloseWorkBench
		movem.l	d1-d7/a0-a6,-(sp)
		CALLSYS	Forbid
		move.l	ZeroBase(pc),a4
		move.b	ShowClock(a4),d0
		move.b	d0,OldClockState(a4)
		clr.b	ShowClock(a4)
		moveq	#0,d0
		move.l	#GD_Display,d1
		lea	ClockDefGads(a4),a0
		move.l	ClockDefHandle(a4),a1
		RELLIB	SetCheckBox,_KCXBase
		CALLSYS	Permit
		move.l	(13*4)(sp),a6
		move.l	OldCloseWorkBench(a4),a0
		jsr	(a0)
		tst.l	d0
		bne.s	.NoReAct
		move.l	d0,-(sp)
		CALLSYS	Forbid
		move.b	OldClockState(a4),d0
		move.b	d0,ShowClock(a4)
		move.l	#GD_Display,d1
		lea	ClockDefGads(a4),a0
		move.l	ClockDefHandle(a4),a1
		RELLIB	SetCheckBox,_KCXBase
		CALLSYS	Permit
		move.l	(sp)+,d0
.NoReAct	movem.l	(sp)+,d1-d7/a0-a6
		rts





ZeroBase	dc.l	0




**********************************************************************
*              Stub for calling the library`s EditHook               *
**********************************************************************
MyStrEditFunc	movem.l	RegsAll,-(sp)
		move.l	h_Data(a0),a4
		RELLIB	StrEditFunc,_KCXBase
		movem.l	(sp)+,RegsAll
		rts




**********************************************************************
*              FKey-Command : Minimize a window`s size               *
**********************************************************************
MinimizeWin	movem.l	RegsAll,-(sp)
		move.l	_IntuitionBase(a4),a6
		move.l	ib_ActiveWindow(a6),d0
		beq.s	.End
		move.l	d0,a0				; Get window
		move.l	wd_Flags(a0),d0
		and.l	#WFLG_SIZEGADGET,d0
		beq.s	.End
		move.w	wd_LeftEdge(a0),d0
		move.w	wd_TopEdge(a0),d1
		neg.w	d0
		neg.w	d1
		move.w	wd_Width(a0),d2
		move.w	wd_Height(a0),d3
		sub.w	wd_MinWidth(a0),d2
		sub.w	wd_MinHeight(a0),d3
		neg.w	d2
		neg.w	d3
		DOLIB	ChangeWindowBox
.End		movem.l	(sp)+,RegsAll
		rts



**********************************************************************
*              FKey-Command : Maximize a window`s size               *
**********************************************************************
MaximizeWin	movem.l	RegsAll,-(sp)
		move.l	_IntuitionBase(a4),a6
		move.l	ib_ActiveWindow(a6),d0
		beq.s	.End
		move.l	d0,a0				; Get window
		move.l	wd_Flags(a0),d0
		and.l	#WFLG_SIZEGADGET,d0
		beq.s	.End
		move.w	wd_LeftEdge(a0),d0
		move.w	wd_TopEdge(a0),d1
		move.l	wd_WScreen(a0),a1
		move.w	sc_Width(a1),d2
		move.w	sc_Height(a1),d3
		neg.w	d0
		neg.w	d1
		DOLIB	ChangeWindowBox
.End		movem.l	(sp)+,RegsAll
		rts



**********************************************************************
*                   FKey-Command : Zip a window                      *
**********************************************************************
ZipWin		movem.l	RegsAll,-(sp)
		move.l	_IntuitionBase(a4),a6
		move.l	ib_ActiveWindow(a6),d0
		beq.s	.End
		move.l	d0,a0				; Get window
		move.l	wd_Flags(a0),d0
		and.l	#WFLG_HASZOOM,d0
		beq.s	.End
		DOLIB	ZipWindow
.End		movem.l	(sp)+,RegsAll
		rts






**********************************************************************
*               Give visual feedback for button press                *
* D0 = Number of gadget                                              *
* A0 = Pointer to first gadget                                       *
* A5 = Pointer to window                                             *
**********************************************************************
ButtonCheck	movem.l	RegsAll,-(sp)
		move.l	a5,d1
		beq	.End
		RELLIB	GetGadgetPtr,_KCXBase
		move.l	a0,a3
		move.l	a0,a1
		or.w	#GFLG_SELECTED,gg_Flags(a3)	; Set new state of gadget
		move.l	a3,a0
		move.l	a5,a1
		sub.l	a2,a2
		moveq	#1,d0
		RELLIB	RefreshGList,_IntuitionBase
		move.l	wd_IDCMPFlags(a5),d5		; Only allow RAWKEYS
		move.l	a5,a0
		move.l	#IDCMP_RAWKEY,d0
		DOLIB	ModifyIDCMP
.WaitMsg	move.l	wd_UserPort(a5),a0		; wait for release msg
		CALLSYS	WaitPort
.LoopMsgs	move.l	wd_UserPort(a5),a0
		RELLIB	GT_GetIMsg,_GadToolsBase
		tst.l	d0
		beq.s	.WaitMsg
		move.l	d0,a1
		move.l	im_Class(a1),d6
		move.w	im_Code(a1),d7
		DOLIB	GT_ReplyIMsg
		cmp.l	#IDCMP_RAWKEY,d6
		bne.s	.LoopMsgs
		and.w	#IECODE_UP_PREFIX,d7
		beq.s	.LoopMsgs
		move.l	a5,a0				; reset idcmp-flags
		move.l	d5,d0
		RELLIB	ModifyIDCMP,_IntuitionBase
		and.w	#~GFLG_SELECTED,gg_Flags(a3)	; Restore gadget image
		move.l	a3,a0
		move.l	a5,a1
		sub.l	a2,a2
		moveq	#1,d0
		DOLIB	RefreshGList
.End		movem.l	(sp)+,RegsAll
		rts



**********************************************************************
*          Convert a char to uppercase using Locale or Util          *
**********************************************************************
GetUpperCase	movem.l	d1-d7/a0-a6,-(sp)		; Convert a char to upper case
		move.l	ZeroBase(pc),a4
		cmp.l	#48,d0
		blt.s	.NormChar
		cmp.l	#57,d0
		bgt.s	.NormChar
		lea	ConvTable(pc),a0
		sub.w	#48,d0
		moveq	#0,d1
		move.b	0(a0,d0),d1
		move.l	d1,d0
.NormChar	tst.l	MyLocale
		beq.s	.UseUtil
		move.l	MyLocale,a0			; Using Locale
		RELLIB	ConvToUpper,_LocaleBase
		bra.s	.End
.UseUtil	RELLIB	ToUpper,_UtilityBase		; or utility.library
.End		movem.l	(sp)+,d1-d7/a0-a6
		rts
ConvTable	dc.b	"=","!",'"',"§","$","%","&","/","(",")"
		EVEN



**********************************************************************
*                             Variables                              *
**********************************************************************
_SysBase	dc.l	0
_CxBase		dc.l	0
StrEditHook	ds.b	h_SIZEOF
FalseStr	dc.b	"0",0
TrueStr		dc.b	"1",0
		EVEN
CheckerName	dc.b	"KCOMM.CHECKER-PROCESS",0
RexxName	dc.b	"KCOMM.REXX-PROCESS",0
		EVEN




**********************************************************************
*                            Audio-Stuff                             *
**********************************************************************
LEFTCH0		EQU	2
RIGHTCH0	EQU	1
LEFTCH1		EQU	4
RIGHTCH1	EQU	8
AnyChannel	dc.b	LEFTCH0|RIGHTCH0
		dc.b	LEFTCH0|RIGHTCH1
		dc.b	LEFTCH1|RIGHTCH0
		dc.b	LEFTCH1|RIGHTCH1



**********************************************************************
*           Main format-definition for the Revision Header           *
**********************************************************************
CHeader		dc.b	"/* $Revision Header built automatically *************** (do not edit) ************",$0a
CLineNL		dc.b	"**",$0a,0
CLine		dc.b	"**     ",0,0
CCopyRight	dc.b	"** © Copyright by %s",$0a
CCR1		dc.b	"**",$0a,0
CFile		dc.b	"** File             : %s",$0a,0
CCreatedOn	dc.b	"** Created on       : %s, %s %s",$0a,0
CAuthor		dc.b	"** Created by       : %s",$0a,0
CRevision	dc.b	"** Current revision : V%d.%d",$0a,0
CPurpose	dc.b	"**",$0a
CP1		dc.b	"**",$0a
CP2		dc.b	"** Purpose",$0a
CP3		dc.b	"** -------",$0a,0
CLog		dc.b	"**",$0a
CL1		dc.b	"** Revision V%d.%d",$0a
CL2		dc.b	"** --------------",$0a
CL3		dc.b	"** created on %s, %s %s  by  %s.   LogMessage :",$0a,0
CLogInit	dc.b	"**",$0a
CLI1		dc.b	"** Revision V%d.%d",$0a
CLI2		dc.b	"** --------------",$0a
CLI3		dc.b	"** created on %s, %s %s  by  %s.   LogMessage :",$0a
CLI4		dc.b	"**     --- Initial release ---",$0a
CLI5		dc.b	"**",$0a
CEnd		dc.b	"*********************************************************************************/",$0a,0
CNewLog		dc.b	"**  -*-  changed on %s, %s %s  by  %s.   LogMessage :",$0a,0
		EVEN


**********************************************************************
*                           Defines for C                            *
**********************************************************************
CRevDefine1	dc.b	'#define REVISION "%d.%d"',$0a
		dc.b	'#define REVDATE  "%s"',$0a
		dc.b	'#define REVTIME  "%s"',$0a
		dc.b	'#define AUTHOR   "%s"',$0a
		dc.b	'#define VERNUM   %d',$0a
		dc.b	'#define REVNUM   %d',$0a,0
		EVEN


**********************************************************************
*                        Macros for Assembler                        *
**********************************************************************
AsmRevDefine1	dc.b	"REVISION  MACRO",$0a
		dc.b	'          dc.b "%d.%d"',$0a
		dc.b	"          ENDM",$0a
		dc.b	"REVDATE   MACRO",$0a
		dc.b	'          dc.b "%s"',$0a
		dc.b	"          ENDM",$0a
		dc.b	"REVTIME   MACRO",$0a
		dc.b	'          dc.b "%s"',$0a
		dc.b	"          ENDM",$0a
		dc.b	"AUTHOR    MACRO",$0a
		dc.b	'          dc.b "%s"',$0a
		dc.b	"          ENDM",$0a
		dc.b	"VERNUM    EQU  %d",$0a
		dc.b	"REVNUM    EQU  %d",$0a,0
		EVEN


**********************************************************************
*                    Constants for Pascal/Modula                     *
**********************************************************************
PASRevDefine1	dc.b	"CONST {* Place all your constants beneith these ones *}",$0a
		dc.b	'        REVISION = "%d.%d"',$0a
		dc.b	'         REVDATE = "%s"',$0a
		dc.b	'         REVTIME = "%s"',$0a
		dc.b	'          AUTHOR = "%s"',$0a
		dc.b	'          VERNUM = %d',$0a
		dc.b	'          REVNUM = %d',$0a,0
		EVEN



**********************************************************************
*                         Strings for Basic                          *
**********************************************************************
BASRevDefine1	dc.b	'REVISION$ = "%d.%d"',$0a
		dc.b	'REVDATE$ = "%s"',$0a
		dc.b	'REVTIME$ = "%s"',$0a
		dc.b	'AUTHOR$ = "%s"',$0a
		dc.b	'VERNUM = %d',$0a
		dc.b	'REVNUM = %d',$0a,0
		EVEN



**********************************************************************
*                   Datablock for Revision Header                    *
**********************************************************************
RevDefine	dc.l	0
RevCompanyDat	dc.l	Company
RevFileDat	dc.l	FileName
RevRevisionDat	dc.w	0
		dc.w	0
RevCreatedOnDat	dc.l	RevDayString
		dc.l	RevDateString
		dc.l	RevTimeString
RevAuthorDat	dc.l	Author
RevDefineDat	dc.w	0
		dc.w	0
		dc.l	RevDateString
		dc.l	RevTimeString
		dc.l	Author
RevVersionDat	dc.w	0
		dc.w	0
DefaultName	dc.b	"--- Unknown ---",0
		EVEN
Author		ds.b	42
Company		ds.b	42
RevDayString	ds.b	LEN_DATSTRING
RevDateString	ds.b	LEN_DATSTRING
RevTimeString	ds.b	LEN_DATSTRING
FileName	ds.b	256



**********************************************************************
*                   Formats for displaying the day                   *
**********************************************************************
FormatData	dc.l	DayString
		dc.l	DateString
		dc.l	TimeString
DayString	ds.b	LEN_DATSTRING
DateString	ds.b	LEN_DATSTRING
TimeString	ds.b	LEN_DATSTRING
OnlineString	ds.b	LEN_DATSTRING
AllDayString	ds.b	LEN_DATSTRING
AllDateString	ds.b	LEN_DATSTRING
AllTimeString	ds.b	LEN_DATSTRING
CopyTime	ds.b	LEN_DATSTRING
LogInDate	ds.b	LEN_DATSTRING
LogInTime	ds.b	LEN_DATSTRING


**********************************************************************
*               The main structure of all...The Broker               *
**********************************************************************
		CNOP	0,4
MyBrokerDef	dc.b	NB_VERSION			; Commodities-Version (NEEDED)
		dc.b	0
		dc.l	BrokerName			; Name of broker (For Exchange-Prg)
		dc.l	0				; Title   (For Exchange-Prg)
		dc.l	0				; Broker-Description (For Exchange-Prg)
		dc.w	NBU_NOTIFY!NBU_UNIQUE		; Notify broker, we are unique
		dc.w	COF_SHOW_HIDE			; We can be hidden/shown
		dc.w	0				; Pri of broker plus an alignment byte
		dc.l	0				; Port-Pointer
		dc.w	0				; Dummy
BrokerName	dc.b	"KCommodity",0
		EVEN


**********************************************************************
*                         AutoRequester-Texts                        *
**********************************************************************
Only20ITxt	dc.b	0,1
		dc.b	RP_JAM1
		dc.b	0
		dc.w	10,10
		dc.l	0
		dc.l	Only20Txt
		dc.l	0
Only20Txt	dc.b	"KCommodity V"
		REVISION
		dc.b	" still only runs under OS 2.x !",$0a,0
Only20TxtEnd
		EVEN
OkITxt		dc.b	2,1
		dc.b	RP_JAM1
		dc.b	0
		dc.w	6,3
		dc.l	0
		dc.l	OkTxt
		dc.l	0
OkTxt		dc.b	"OK",0
		EVEN


**********************************************************************
*                            Error-Texts                             *
**********************************************************************
EasyTitle	dc.b	"KCommodity V"
VersionStr	REVISION
		dc.b	0
		EVEN


**********************************************************************
*                       Easy-Requeststructure                        *
**********************************************************************
MyEasyRequest	dc.l	es_SIZEOF
		dc.l	0
		dc.l	0
		dc.l	0
		dc.l	0
EasyGadget	dc.b	"OK",0
		EVEN


**********************************************************************
*                 Some defines fo easy customization                 *
**********************************************************************
PROGWINWIDTH	EQU	309
CLOCKWINIDCMP	EQU	IDCMP_CHANGEWINDOW|IDCMP_CLOSEWINDOW
CLOCKWINFLAGS	EQU	WFLG_CLOSEGADGET|WFLG_DEPTHGADGET|WFLG_DRAGBAR|WFLG_RMBTRAP



**********************************************************************
*                   Settings for Formatter window                    *
**********************************************************************
FormWinWindowTags:
FormWinL:
    DC.L    WA_Left,0
FormWinT:
    DC.L    WA_Top,0
FormWinW:
    DC.L    WA_Width,0
FormWinH:
    DC.L    WA_Height,0
    DC.L    WA_IDCMP,CYCLEIDCMP!STRINGIDCMP!CHECKBOXIDCMP!BUTTONIDCMP!IDCMP_CLOSEWINDOW!IDCMP_VANILLAKEY!IDCMP_CHANGEWINDOW
    DC.L    WA_Flags,WFLG_DRAGBAR!WFLG_DEPTHGADGET!WFLG_CLOSEGADGET!WFLG_SMART_REFRESH!WFLG_ACTIVATE
FormWinWG:
    DC.L    WA_Gadgets,0
    DC.L    WA_Title,FormTitle
    DC.L    WA_ScreenTitle,WindowScrTitle
FormWinSC:
    DC.L    WA_PubScreen,0
    DC.L    TAG_DONE


FormWinGTypes:
    DC.W    CYCLE_KIND
    DC.W    STRING_KIND
    DC.W    CHECKBOX_KIND
    DC.W    CHECKBOX_KIND
    DC.W    CHECKBOX_KIND
    DC.W    CHECKBOX_KIND
    DC.W    CHECKBOX_KIND
    DC.W    CHECKBOX_KIND
    DC.W    BUTTON_KIND
    DC.W    BUTTON_KIND
    DC.W    CHECKBOX_KIND
    DC.W    CHECKBOX_KIND
    DC.W    CHECKBOX_KIND
    DC.W    CHECKBOX_KIND
    DC.W    CHECKBOX_KIND
    DC.W    CHECKBOX_KIND
    DC.W    CHECKBOX_KIND

FormWinNGads:
    DC.W    106,3,69,13
    DC.L    DriveText,0
    DC.W    GD_Drive
    DC.L    PLACETEXT_LEFT,0,0
    DC.W    177,3,235,13
    DC.L    LabelText,0
    DC.W    GD_Label
    DC.L    PLACETEXT_RIGHT,0,0
    DC.W    152,23,26,11
    DC.L    TrashcanText,0
    DC.W    GD_Trashcan
    DC.L    PLACETEXT_LEFT,0,0
    DC.W    298,23,26,11
    DC.L    QuickText,0
    DC.W    GD_Quick
    DC.L    PLACETEXT_LEFT,0,0
    DC.W    473,23,26,11
    DC.L    FFSText,0
    DC.W    GD_FFS
    DC.L    PLACETEXT_LEFT,0,0
    DC.W    152,51,26,11
    DC.L    NoVerifyText,0
    DC.W    GD_NoVerify
    DC.L    PLACETEXT_LEFT,0,0
    DC.W    298,51,26,11
    DC.L    InstallText,0
    DC.W    GD_Install
    DC.L    PLACETEXT_LEFT,0,0
    DC.W    473,51,26,11
    DC.L    EjectText,0
    DC.W    GD_Eject
    DC.L    PLACETEXT_LEFT,0,0
    DC.W    5,85,104,12
    DC.L    FormOkText,0
    DC.W    GD_FormOk
    DC.L    PLACETEXT_IN,0,0
    DC.W    395,85,104,12
    DC.L    FormCancelText,0
    DC.W    GD_FormCancel
    DC.L    PLACETEXT_IN,0,0
    DC.W    152,71,26,11
    DC.L    WatchDisksText,0
    DC.W    GD_WatchDisks
    DC.L    PLACETEXT_LEFT,0,0
    DC.W    231,71,26,11
    DC.L    WatchTxt1,0
    DC.W    GD_Watch0
    DC.L    PLACETEXT_LEFT,0,0
    DC.W    313,71,26,11
    DC.L    WatchTxt2,0
    DC.W    GD_Watch1
    DC.L    PLACETEXT_LEFT,0,0
    DC.W    392,71,26,11
    DC.L    WatchTxt3,0
    DC.W    GD_Watch2
    DC.L    PLACETEXT_LEFT,0,0
    DC.W    473,71,26,11
    DC.L    WatchTxt4,0
    DC.W    GD_Watch3
    DC.L    PLACETEXT_LEFT,0,0
    DC.W    152,37,26,11
    DC.L    DiskCacheText,0
    DC.W    GD_DiskCache
    DC.L    PLACETEXT_LEFT,0,0
    DC.W    473,37,26,11
    DC.L    IntModeText,0
    DC.W    GD_IntMode
    DC.L    PLACETEXT_LEFT,0,0

FormWinGTags:
Zero2    DC.L    GTCY_Labels,DriveList
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
    DC.L    GTST_EditHook,StrEditHook
    DC.L    STRINGA_ExitHelp,1
    DC.L    GTST_MaxChars,256
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
    DC.L    GTCB_Scaled,-1
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
    DC.L    GTCB_Scaled,-1
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
    DC.L    GTCB_Scaled,-1
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
    DC.L    GTCB_Scaled,-1
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
    DC.L    GTCB_Scaled,-1
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
    DC.L    GTCB_Scaled,-1
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
    DC.L    GTCB_Scaled,-1
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
WatchGads
    DC.L    GA_Disabled,1
    DC.L    GTCB_Scaled,-1
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
    DC.L    GA_Disabled,1
    DC.L    GTCB_Scaled,-1
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
    DC.L    GA_Disabled,1
    DC.L    GTCB_Scaled,-1
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
    DC.L    GA_Disabled,1
    DC.L    GTCB_Scaled,-1
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
    DC.L    GTCB_Scaled,-1
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
    DC.L    GTCB_Scaled,-1
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE



GD_Drive                               EQU    0
GD_Label                               EQU    1
GD_Trashcan                            EQU    2
GD_Quick                               EQU    3
GD_FFS                                 EQU    4
GD_NoVerify                            EQU    5
GD_Install                             EQU    6
GD_Eject                               EQU    7
GD_FormOk                              EQU    8
GD_FormCancel                          EQU    9
GD_WatchDisks                          EQU    10
GD_Watch0                              EQU    11
GD_Watch1                              EQU    12
GD_Watch2                              EQU    13
GD_Watch3                              EQU    14
GD_DiskCache                           EQU    15
GD_IntMode                             EQU    16




**********************************************************************
*                      Settings for bill window                      *
**********************************************************************

BillWinWindowTags:
BillWinL:
    DC.L    WA_Left,0
BillWinT:
    DC.L    WA_Top,0
BillWinW:
    DC.L    WA_Width,0
BillWinH:
    DC.L    WA_Height,0
    DC.L    WA_IDCMP,TEXTIDCMP!MXIDCMP!CHECKBOXIDCMP!IDCMP_CLOSEWINDOW!IDCMP_VANILLAKEY!IDCMP_CHANGEWINDOW
BillWinF:
    DC.L    WA_Flags,WFLG_DRAGBAR!WFLG_DEPTHGADGET!WFLG_CLOSEGADGET!WFLG_SMART_REFRESH
BillWinWG:
    DC.L    WA_Gadgets,0
    DC.L    WA_Title,BillTitle
    DC.L    WA_ScreenTitle,WindowScrTitle
BillWinSC:
    DC.L    WA_PubScreen,0
Zero4    DC.L    WA_Zoom,BillZoomSize
    DC.L    TAG_DONE


BillWinGTypes:
    DC.W    TEXT_KIND
    DC.W    TEXT_KIND
    DC.W    TEXT_KIND
    DC.W    MX_KIND
    DC.W    MX_KIND
    DC.W    TEXT_KIND
    DC.W    CHECKBOX_KIND
    DC.W    BUTTON_KIND
    DC.W    BUTTON_KIND

BillWinNGads:
    DC.W    7,13,72,13
    DC.L    BillTimeText,0
    DC.W    GD_BillTime
    DC.L    PLACETEXT_ABOVE,0,0
    DC.W    125,13,72,13
    DC.L    BillOnlineText,0
    DC.W    GD_BillOnline
    DC.L    PLACETEXT_ABOVE,0,0
    DC.W    241,13,72,13
    DC.L    BillCostText,0
    DC.W    GD_BillCost
    DC.L    PLACETEXT_ABOVE,0,0
    DC.W    7,30,17,9
    DC.L    0,0
    DC.W    GD_BillTimeMX
    DC.L    PLACETEXT_RIGHT,0,0
    DC.W    296,30,17,9
    DC.L    0,0
    DC.W    GD_BillCheap
    DC.L    PLACETEXT_LEFT,0,0
    DC.W    6,73,305,13
    DC.L    0,0
    DC.W    GD_BillHour
    DC.L    0,0,0
    DC.W    6,89,26,11
    DC.L    LogCallText,0
    DC.W    GD_LogCall
    DC.L    PLACETEXT_RIGHT,0,0
    DC.W    125,30,72,13
    DC.L    BillStartText,0
    DC.W    GD_BillStart
    DC.L    PLACETEXT_IN,0,0
    DC.W    125,47,72,13
    DC.L    BillStopText,0
    DC.W    GD_BillStop
    DC.L    PLACETEXT_IN,0,0

BillWinGTags:
    DC.L    GTTX_Text,TimeString
    DC.L    GTTX_Border,1
    DC.L    TAG_DONE
    DC.L    GTTX_Text,OnlineString
    DC.L    GTTX_Border,1
    DC.L    TAG_DONE
    DC.L    GTTX_Text,CostString
    DC.L    GTTX_Border,1
    DC.L    TAG_DONE
    DC.L    GTMX_Scaled,-1
    DC.L    GTMX_Labels,BillTimeMXLabels
    DC.L    GTMX_Spacing,2
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
    DC.L    GTMX_Scaled,-1
    DC.L    GTMX_Labels,BillCheapLabels
    DC.L    GTMX_Spacing,2
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
    DC.L    GTTX_Border,1
    DC.L    TAG_DONE
    DC.L    GTCB_Scaled,-1
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE


BillTimeMXTags:
    DC.L    GTMX_Labels,BillTimeMXLabels
    DC.L    GTMX_Spacing,2
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
BillCheapTags:
    DC.L    GTMX_Labels,BillCheapLabels
    DC.L    GTMX_Spacing,2
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
BillTimeMXLabels:
    DC.L    BillTimeMXLab0
    DC.L    BillTimeMXLab1
    DC.L    BillTimeMXLab2
    DC.L    0
BillCheapLabels:
    DC.L    BillCheapLab0
    DC.L    BillCheapLab1
    DC.L    0


GD_BillTime                            EQU    0
GD_BillOnline                          EQU    1
GD_BillCost                            EQU    2
GD_BillTimeMX                          EQU    3
GD_BillCheap                           EQU    4
GD_BillHour                            EQU    5
GD_LogCall                             EQU    6
GD_BillStart                           EQU    7
GD_BillStop                            EQU    8

KD_BillTime1	EQU	'1'
KD_BillTime2	EQU	'2'
KD_BillTime3	EQU	'3'
KD_BillNormal	EQU	'4'
KD_BillCheap	EQU	'5'



**********************************************************************
*                      Settings for Log window                       *
**********************************************************************
LogWinWindowTags:
LogWinL:
    DC.L    WA_Left,0
LogWinT:
    DC.L    WA_Top,0
LogWinW:
    DC.L    WA_Width,0
LogWinH:
    DC.L    WA_Height,0
    DC.L    WA_IDCMP,STRINGIDCMP!BUTTONIDCMP!INTEGERIDCMP!IDCMP_CLOSEWINDOW!IDCMP_ACTIVEWINDOW!IDCMP_VANILLAKEY!IDCMP_CHANGEWINDOW!IDCMP_REFRESHWINDOW
    DC.L    WA_Flags,WFLG_DRAGBAR!WFLG_DEPTHGADGET!WFLG_CLOSEGADGET!WFLG_SMART_REFRESH!WFLG_ACTIVATE
LogWinWG:
    DC.L    WA_Gadgets,0
    DC.L    WA_ScreenTitle,WindowScrTitle
LogWinSC:
    DC.L    WA_PubScreen,0
    DC.L    TAG_DONE


LogWinGTypes:
    DC.W    STRING_KIND
    DC.W    STRING_KIND
    DC.W    STRING_KIND
    DC.W    STRING_KIND
    DC.W    STRING_KIND
    DC.W    BUTTON_KIND
    DC.W    BUTTON_KIND
    DC.W    INTEGER_KIND
    DC.W    INTEGER_KIND
    DC.W    BUTTON_KIND

LogWinNGads:
    DC.W    5,4,502,14
    DC.L    0,0
    DC.W    GD_Line1
    DC.L    0,0,0
    DC.W    5,20,502,14
    DC.L    0,0
    DC.W    GD_Line2
    DC.L    0,0,0
    DC.W    5,36,502,14
    DC.L    0,0
    DC.W    GD_Line3
    DC.L    0,0,0
    DC.W    5,52,502,14
    DC.L    0,0
    DC.W    GD_Line4
    DC.L    0,0,0
    DC.W    5,68,502,14
    DC.L    0,0
    DC.W    GD_Line5
    DC.L    0,0,0
    DC.W    5,86,99,12
    DC.L    LogOkText,0
    DC.W    GD_LogOk
    DC.L    PLACETEXT_IN,0,0
    DC.W    408,86,99,12
    DC.L    LogCancelText,0
    DC.W    GD_LogCancel
    DC.L    PLACETEXT_IN,0,0
    DC.W    307,86,36,13
    DC.L    LogVerText,0
    DC.W    GD_LogVer
    DC.L    PLACETEXT_LEFT,0,0
    DC.W    366,86,36,13
    DC.L    LogRevText,0
    DC.W    GD_LogRev
    DC.L    PLACETEXT_LEFT,0,0
    DC.W    116,86,99,12
    DC.L    LogOnlyTextText,0
    DC.W    GD_LogOnlyText
    DC.L    PLACETEXT_IN,0,0

LogWinGTags:
    DC.L    GTST_EditHook,StrEditHook
    DC.L    STRINGA_ExitHelp,1
    DC.L    GTST_MaxChars,60
    DC.L    TAG_DONE
    DC.L    GTST_EditHook,StrEditHook
    DC.L    STRINGA_ExitHelp,1
    DC.L    GTST_MaxChars,60
    DC.L    TAG_DONE
    DC.L    GTST_EditHook,StrEditHook
    DC.L    STRINGA_ExitHelp,1
    DC.L    GTST_MaxChars,60
    DC.L    TAG_DONE
    DC.L    GTST_EditHook,StrEditHook
    DC.L    STRINGA_ExitHelp,1
    DC.L    GTST_MaxChars,60
    DC.L    TAG_DONE
    DC.L    GTST_EditHook,StrEditHook
    DC.L    STRINGA_ExitHelp,1
    DC.L    GTST_MaxChars,60
    DC.L    TAG_DONE
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
    DC.L    GTST_EditHook,StrEditHook
    DC.L    STRINGA_ExitHelp,1
    DC.L    GTIN_Number,0
    DC.L    GTIN_MaxChars,2
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
    DC.L    GTST_EditHook,StrEditHook
    DC.L    STRINGA_ExitHelp,1
    DC.L    GTIN_Number,0
    DC.L    GTIN_MaxChars,2
    DC.L    TAG_DONE
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
LogRevText:
    DC.B    '.',0
    CNOP    0,2

GD_Line1                               EQU    0
GD_Line2                               EQU    1
GD_Line3                               EQU    2
GD_Line4                               EQU    3
GD_Line5                               EQU    4
GD_LogOk                               EQU    5
GD_LogCancel                           EQU    6
GD_LogVer                              EQU    7
GD_LogRev                              EQU    8
GD_LogOnlyText                         EQU    9

KD_LogOk	EQU	'O'
KD_LogCancel	EQU	'C'
KD_LogVer	EQU	'R'




**********************************************************************
*                    Settings for Progress window                    *
**********************************************************************
ProgWinWindowTags:
ProgWinL:
    DC.L    WA_Left,0
ProgWinT:
    DC.L    WA_Top,0
ProgWinW:
    DC.L    WA_Width,0
ProgWinH:
    DC.L    WA_Height,0
    DC.L    WA_IDCMP,TEXTIDCMP
    DC.L    WA_Flags,WFLG_DRAGBAR!WFLG_SMART_REFRESH
ProgWinWG:
    DC.L    WA_Gadgets,0
    DC.L    WA_Title,ProgWinWTitle
    DC.L    WA_ScreenTitle,WindowScrTitle
ProgWinSC:
    DC.L    WA_PubScreen,0
    DC.L    TAG_DONE

ProgWinGTypes:
    DC.W    TEXT_KIND
    DC.W    TEXT_KIND

ProgWinNGads:
    DC.W    12,6,26,12
    DC.L    0,0
    DC.W    GD_Prog0
    DC.L    0,0,0
    DC.W    265,6,42,12
    DC.L    0,0
    DC.W    GD_Prog100
    DC.L    0,0,0

ProgWinGTags:
    DC.L    GTTX_Text,Prog0String
    DC.L    TAG_DONE
    DC.L    GTTX_Text,Prog100String
    DC.L    TAG_DONE

Prog0String:
    DC.B    '0%',0
    CNOP     0,2
Prog100String:
    DC.B    '100%',0
    CNOP     0,2

GD_Prog0                               EQU    0
GD_Prog100                             EQU    1
ProgWin_CNT        EQU 2



**********************************************************************
*                    Definitions for Clock-Window                    *
**********************************************************************
ClockWindowTags	dc.l	WA_Left
ClockLeft	dc.l	0
		dc.l	WA_Top
ClockTop	dc.l	0
		dc.l	WA_InnerWidth
ClockWidth	dc.l	0
		dc.l	WA_InnerHeight
		dc.l	0
		dc.l	WA_ScreenTitle
		dc.l	WindowScrTitle
		dc.l	WA_IDCMP
		dc.l	CLOCKWINIDCMP
		dc.l	WA_Flags
		dc.l	CLOCKWINFLAGS
		dc.l	WA_Title
		dc.l	0
		dc.l	TAG_DONE





**********************************************************************
*                Definitions for Select Screen window                *
**********************************************************************
SelScrWinWindowTags:
SelScrWinL:
    DC.L    WA_Left,0
SelScrWinT:
    DC.L    WA_Top,0
SelScrWinW:
    DC.L    WA_Width,0
SelScrWinH:
    DC.L    WA_Height,0
    DC.L    WA_IDCMP,LISTVIEWIDCMP!CYCLEIDCMP!BUTTONIDCMP!NUMBERIDCMP!TEXTIDCMP!IDCMP_CLOSEWINDOW!IDCMP_VANILLAKEY!IDCMP_CHANGEWINDOW!IDCMP_REFRESHWINDOW
    DC.L    WA_Flags,WFLG_DRAGBAR!WFLG_DEPTHGADGET!WFLG_CLOSEGADGET!WFLG_SMART_REFRESH!WFLG_ACTIVATE
SelScrWinWG:
    DC.L    WA_Gadgets,0
    DC.L    WA_Title,SelScrTitle
    DC.L    WA_ScreenTitle,WindowScrTitle
SelScrWinSC:
    DC.L    WA_PubScreen,0
    DC.L    TAG_DONE


SelScrWinGTypes:
    DC.W    LISTVIEW_KIND
    DC.W    BUTTON_KIND
    DC.W    BUTTON_KIND
    DC.W    NUMBER_KIND
    DC.W    TEXT_KIND
    DC.W    BUTTON_KIND

SelScrWinNGads:
    DC.W    9,13,242,84
    DC.L    TaskListText,0
    DC.W    GD_TaskList
    DC.L    PLACETEXT_ABOVE,0,0
    DC.W    256,86,93,12
    DC.L    SelScrActText,0
    DC.W    GD_SelScrAct
    DC.L    PLACETEXT_IN,0,0
    DC.W    353,86,93,12
    DC.L    SelScrCancelText,0
    DC.W    GD_SelScrCancel
    DC.L    PLACETEXT_IN,0,0
    DC.W    256,40,190,12
    DC.L    SelScrNumScrText,0
    DC.W    GD_SelScrNumScr
    DC.L    PLACETEXT_ABOVE,0,0
    DC.W    256,68,190,12
    DC.L    SelScrDefPubText,0
    DC.W    GD_SelScrDefPub
    DC.L    PLACETEXT_ABOVE,0,0
    DC.W    256,13,190,12
    DC.L    SelScrUpdateText,0
    DC.W    GD_SelScrUpdate
    DC.L    PLACETEXT_IN,0,0


SelScrWinGTags:
    DC.L    GTLV_Labels,0
    DC.L    GTLV_ShowSelected,0
    DC.L    GTLV_Selected,0
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
ScrNumTag
    DC.L    GTNM_Number,0
    DC.L    GTNM_Border,1
    DC.L    TAG_DONE
    DC.L    GTTX_Text,DefPubScreenName
    DC.L    GTTX_Border,1
    DC.L    TAG_DONE
    DC.L    GT_Underscore,'_'
    DC.L    TAG_DONE
DefPubScreenName	ds.b	MAXPUBSCREENNAME+1


GD_TaskList                            EQU    0
GD_SelScrAct                           EQU    1
GD_SelScrCancel                        EQU    2
GD_SelScrNumScr                        EQU    3
GD_SelScrDefPub                        EQU    4
GD_SelScrUpdate                        EQU    5

KD_TaskList		EQU	'L'
KD_SelScrAct		EQU	'A'
KD_SelScrCancel		EQU	'C'
KD_SelScrUpdate		EQU	'U'




**********************************************************************
*                    Standad string for the Costs                    *
**********************************************************************
CostString	dc.b	"    0.00",0
**********************************************************************
*              Definitions for userdefinable filenames               *
**********************************************************************
DirPrefsName	dc.b	"KCX.PREFS",0
EnvPrefsName	dc.b	"ENV:KCX/KCX.PREFS",0
EnvArcPrefsName	dc.b	"ENVARC:KCX/KCX.PREFS",0
		EVEN



**********************************************************************
*                         HotKey definitions                         *
**********************************************************************
DefaultHotKeys	dc.l	DefPopUpKey,DefBillHotKey,DefFormHotKey
		dc.l	DefPageHotKey,DefShellHotKey
		dc.l	DefUmlautHotKey,DefSnapHotKey,DefDoBlankHotKey
		dc.l	DefDumpHotKey,DefCenterHotKey,DefSelScrHotKey
		dc.l	DefClockHotKey,DefFKeyHotKey,DefSunHotKey
DefPopUpKey	dc.b	"LCOMMAND HELP",0
DefBillHotKey	dc.b	"ALT SHIFT B",0
DefFormHotKey	dc.b	"ALT SHIFT F",0
DefPageHotKey	dc.b	"RALT HELP",0
DefShellHotKey	dc.b	"ALT SHIFT S",0
DefUmlautHotKey	dc.b	"ALT SHIFT U",0
DefSnapHotKey	dc.b	"ALT SHIFT G",0
DefDoBlankHotKey	dc.b	"ALT SHIFT D",0
DefDumpHotKey	dc.b	"ALT NUMERICPAD *",0
DefCenterHotKey	dc.b	"ALT SHIFT X",0
DefSelScrHotKey	dc.b	"LSHIFT ESC",0
DefClockHotKey	dc.b	"ALT SHIFT C",0
DefFKeyHotKey	dc.b	"ALT SHIFT K",0
DefSunHotKey	dc.b	"ALT SHIFT M",0
		EVEN
POPUPKEYID	EQU	1			; Definitions for IDs
BILLKEYID	EQU	2
FORMKEYID	EQU	3
PAGEKEYID	EQU	4
SHELLKEYID	EQU	5
UMLAUTKEYID	EQU	6
SNAPKEYID	EQU	7
DOBLANKKEYID	EQU	8
DUMPKEYID	EQU	9
CENTERKEYID	EQU	10
SELECTSCRKEYID	EQU	11
CLOCKKEYID	EQU	12
FKEYKEYID	EQU	13
SUNKEYID	EQU	14


**********************************************************************
*                     Defines for HotKey-Window                      *
**********************************************************************
HotKeyGadStat	dc.l	PopUpKey		; CX_PopKey
		dc.l	BillHotKey		; Bill
		dc.l	FormatHotKey		; Formatter
		dc.l	PageHotKey		; Page
		dc.l	ShellHotKey		; UserShell
		dc.l	UmlautHotKey		; Map Umlauts
		dc.l	SnapHotKey		; Snap memory
		dc.l	DoBlankHotKey		; Immediate Blank
		dc.l	DumpHotKey		; Display dump
		dc.l	CenterHotKey		; Center a screen
		dc.l	SelScrHotKey		; Select Screen to pop up
		dc.l	ClockHotKey		; On/Off clock
		dc.l	FKeyHotKey		; On/Off FKeys
		dc.l	SunHotKey		; On/Off SunMouse
HOTKEYNUM	EQU	((*-HotKeyGadStat)/4)
HotKeyGadVars	dc.l	MainHandle,ControlTitle,PopUpFilter
		dc.l	BillHandle,BillTitle,BillFilter
		dc.l	FormHandle,FormTitle,FormFilter
		dc.l	0,0,PageFilter
		dc.l	0,0,ShellFilter
		dc.l	0,0,UmlautFilter
		dc.l	0,0,SnapFilter
		dc.l	0,0,DoBlankFilter
		dc.l	PrintHandle,PrintTitle,DumpFilter
		dc.l	0,0,CenterFilter
		dc.l	SelScrHandle,SelScrTitle,SelScrFilter
		dc.l	0,0,ClockFilter
		dc.l	0,0,FKeyFilter
		dc.l	0,0,SunFilter


**********************************************************************
*                      Variables for formatter                       *
**********************************************************************
DriveArg	dc.b	" DRIVE ",0
NameArg		dc.b	" NAME ",0
IconsArg	dc.b	" NOICONS",0
QuickArg	dc.b	" QUICK",0
FFSArg		dc.b	" FFS",0
VerifyArg	dc.b	" NOVERIFY",0
InstallArg	dc.b	" INSTALL",0
EjectArg	dc.b	" EJECT",0
DirCacheArg	dc.b	" DIRCACHE",0
IntModeArg	dc.b	" INTL",0
		EVEN


**********************************************************************
*                   Command definitions for FKeys                    *
**********************************************************************
FKeyCmds	dc.l	MinimizeCmd,MaximizeCmd,ZipWinCmd
FKEYCMDNUM	EQU	(*-FKeyCmds)/4
FKeyRouts	dc.l	MinimizeWin,MaximizeWin,ZipWin
MinimizeCmd	dc.b	"MINWIN",0
MaximizeCmd	dc.b	"MAXWIN",0
ZipWinCmd	dc.b	"ZIPWIN",0
		EVEN




**********************************************************************
*                 Text attr for the text in titlebar                 *
**********************************************************************
TxtAttr		dc.l	TxtAttrFont
		dc.w	0
		dc.b	FS_NORMAL
		dc.b	0



**********************************************************************
*                     Formatstrings for RawDoFmt                     *
**********************************************************************
TimeFormats	dc.l	FormLSec,FormLNSec,FormSSec,FormSNSec
FormLSec	dc.b	" %s %s %s ",0
FormLNSec	dc.b	" %s %s %.5s ",0
FormSSec	dc.b	" %.3s %s %s ",0
FormSNSec	dc.b	" %.3s %s %.5s ",0
CostFormString	dc.b	"%5ld.%02ld",0
PerHourForm	dc.b	"%4ld.%02ld",0
LIntFormat	dc.b	"%ld",0
StringFormat	dc.b	"%s",0
FormLongSStr	dc.b	"%02ld:%02ld:%02ld",0
FormLongNSStr	dc.b	"%02ld:%02ld",0
ScrDimmFormat	dc.b	"%2.2ld",0
FormMemString	dc.b	" C: %7ld   F: %8ld ",0
DoubleMemString	dc.b	"  C: %ld   F: %ld ",0
MousePosForm	dc.b	"  X: %4.4d  Y: %4.4d",0
ChipMem		dc.l	0
FastMem		dc.l	0
TotalMem	dc.l	0
NextUnit	dc.l	0
UnitsDone	dc.l	0
LogEntryDat	dc.l	LogInDate
		dc.l	LogInTime
		dc.l	AllTimeString
LogTimeZone	dc.l	0
LogEntryTariff	dc.w	0
OnlineHour	dc.l	0
OnlineMinute	dc.l	0
OnlineSeconds	dc.l	0
CostMajor	dc.l	0
CostMinor	dc.l	0
BillTotalCost	dc.l	0
BillDayCost	dc.l	0
BillDayPtr	dc.l	BillDayStr
BillMajor	dc.l	0
BillMinor	dc.l	0
BillDayStr	ds.b	10
ThisChip	dc.l	0
ThisFast	dc.l	0




**********************************************************************
*                          The AppIcon-Icon                          *
**********************************************************************
KCXAppIconDef	dc.w	0,0
		dc.l	0				; Gadget structure
		dc.w	0,0
AppIconWidth	dc.w	0
AppIconHeight	dc.w	0
		dc.w	0,0,0
AppIconPic	dc.l	0
		dc.l	0,0,0,0
		dc.w	0
		dc.l	0
		dc.b	0,0
		dc.l	0
		dc.l	0
IconXPos	dc.l	NO_ICON_POSITION		; Position of icon
IconYPos	dc.l	NO_ICON_POSITION
		dc.l	0
		dc.l	0
		dc.l	0
MyAppIconText	dc.b	"KCommodity",0
		EVEN



**********************************************************************
*                 Structures for the Tools-MenuItem                  *
**********************************************************************
MAINITEM	EQU	1



**********************************************************************
*                       Texts for localization                       *
**********************************************************************
LOCALE_START
* CATALOG kcxmain
* VERSION 2.5
* DATE 01.02.93
DefFormTitle		dc.b	"Formatter : HotKey=",0
* D				"Formatierer : HotKey=",0
DefBillTitle		dc.b	"Bill : HotKey=",0
* D				"Rng. : HotKey=",0
DefSelScrTitle		dc.b	"Screen Select : HotKey=",0
* D				"Screen Wahl : HotKey=",0
DefLabel		dc.b	"Empty",0
* D				"Leer",0
CommandHeader		dc.b	"KCommodity, error occured ",0
* D				"KCommodity, Fehler aufgetreten",0
GlobalTagName		dc.b	"*** GLOBALTAG ***",0
* D				"*** GLOBALER TAG ***",0
NoScrName		dc.b	"? NO NAME ?",0
* D				"? KEIN NAME ?",0
QuitText		dc.b	"Do you really want to quit KCommodity ?",0
* D				"Wollen Sie KCommodity wirklich beenden ?",0
QuitGads		dc.b	"Sure|No",0
* D				"Sicher|Nein",0
FormatWin		dc.b	"CON:0/11//90/KCX-Formatter Shell",0
* D				"CON:0/11//90/KCX-Formatier Shell",0
OnlineFormStr		dc.b	"Online ",0
* D				"Anschluß ",0
EString			dc.b	"E",0
* D				"L",0
FString			dc.b	"F",0
* D				"V",0
BrokerTitle		dc.b	"KCommodity by Kai Iske",0
* D				"KCommodity von Kai Iske",0
BrokerDesc		dc.b	"Multifunction commodity. ShareWare",0
* D				"Multifunktionale Commodity. ShareWare",0
DoFormatText		dc.b	"You have inserted an unformatted disk",10
			dc.b	"in drive %s",10
			dc.b	"Would you like to format the disk ?",0
* D				"Sie haben eine unformatierte Diskette",10
* D				"in Laufwerk %s eingelegt.",10
* D				"Wollen Sie die Diskette formatieren ?",0
FormatGads		dc.b	"YES|NO",0
* D				"JA|NEIN",0
WindowScrTitle		dc.b	"KCommodity by Kai Iske. ShareWare",0
* D				"KCommodity von Kai Iske. ShareWare",0
LogWinTitle		dc.b	"Enter a Log Message",0
* D				"Geben Sie eine Log Nachricht ein",0
PurposeWinTitle		dc.b	"Enter a Purpose Message",0
* D				"Geben Sie eine Zweck-Nachricht ein",0
AlarmReqText		dc.b	"Hey guy,",10,"It`s time for you to wake up !",0
* D				"Hey Du,",10,"Es ist Zeit aufzuwachen !",0
PerHourStr		dc.b	"Costs per hour : ",0
* D				"Kosten pro Stunde : ",0
FormTMemString		dc.b	" Total Memory : %8ld ",0
* D				" Ges. Speicher : %8ld ",0
FormSnapMemStr		dc.b	" Snapped  C: %7ld   F: %8ld ",0
* D				" Schnap.  C: %7ld   F: %8ld ",0
LogEntryFormat		dc.b	"%9.9s: %8.8s - %8.8s / Zone %ld Tariff %c / Time %02ld:%02ld:%02ld / Cost %ld.%02ld",10,0
* D				"%9.9s: %8.8s - %8.8s / Zone %ld Tarif  %c / Zeit %02ld:%02ld:%02ld / Geld %ld.%02ld",10,0
NormChar		dc.b	"N",0
* D				"N",0
CheapChar		dc.b	"C",0
* D				"B",0
AppMenuTxt		dc.b	"KCX Preferences",0
* D				"KCX Einstellungen",0
DriveText		dc.b	"_Drive",0
* D				"_Laufwerk",0
LabelText		dc.b	"_Label",0
* D				"_Name",0
TrashcanText		dc.b	"_Trashcan",0
* D				"_Papierkorb",0
QuickText		dc.b	"_Quick",0
* D				"_Schnell",0
FFSText			dc.b	"_FFS",0
* D				"_FFS",0
NoVerifyText		dc.b	"No _Verify",0
* D				"Kein _Verify",0
InstallText		dc.b	"_Install",0
* D				"_Installieren",0
EjectText		dc.b	"_Eject",0
* D				"A_uswerfen",0
FormOkText		dc.b	"F_ormat",0
* D				"F_ormatieren",0
FormCancelText		dc.b	"_Cancel",0
* D				"_Abbruch",0
WatchDisksText		dc.b	"_Watch disks",0
* D				"_Überwache Disk.",0
DiskCacheText		dc.b	"Di_rectory-Cache",0
* D				"Ve_rzeichnis-Cache",0
IntModeText		dc.b	"Intern. _Mode",0
* D				"Intern. Mo_dus",0
WatchTxt1		dc.b	"DF_0:",0
* D				"DF_0:",0
WatchTxt2		dc.b	"DF_1:",0
* D				"DF_1:",0
WatchTxt3		dc.b	"DF_2:",0
* D				"DF_2:",0
WatchTxt4		dc.b	"DF_3:",0
* D				"DF_3:",0
BillTimeMXLab0		dc.b	"Time _1",0
* D				"Zeit _1",0
BillTimeMXLab1		dc.b	"Time _2",0
* D				"Zeit _2",0
BillTimeMXLab2		dc.b	"Time _3",0
* D				"Zeit _3",0
BillCheapLab0		dc.b	"Normal _4",0
* D				"Normal _4",0
BillCheapLab1		dc.b	"Cheap _5",0
* D				"Billig _5",0
BillTimeText		dc.b	"Time",0
* D				"Zeit",0
BillOnlineText		dc.b	"Online",0
* D				"Anschluß",0
BillCostText		dc.b	"Cost",0
* D				"Kosten",0
BillStartText		dc.b	"_Start",0
* D				"_Start",0
BillStopText		dc.b	"Sto_p",0
* D				"Sto_p",0
LogCallText		dc.b	"_Log call",0
* D				"_Anruf speichern",0
LogOkText		dc.b	"_Ok",0
* D				"_Ok",0
LogCancelText		dc.b	"_Cancel",0
* D				"_Abbruch",0
LogVerText		dc.b	"_Revision",0
* D				"_Revision",0
LogOnlyTextText		dc.b	"_Only Text",0
* D				"_Nur Text",0
ProgWinWTitle		dc.b	"Revision in progress",0
* D				"Revision in Arbeit",0
SelScrWinWTitle		dc.b	"Screen-List : Select new screen",0
* D				"Screen-Liste : Wählen Sie einen neuen Screen",0
TaskListText		dc.b	"_List",0
* D				"_Liste",0
SelScrActText		dc.b	"_Activate",0
* D				"A_ktivieren",0
SelScrCancelText	dc.b	"_Cancel",0
* D				"_Abbruch",0
SelScrNumScrText	dc.b	"Number of Screens",0
* D				"Anzahl Screens",0
SelScrDefPubText	dc.b	"Default Pub-Screen",0
* D				"Voreingest. Pub-Screen",0
SelScrUpdateText	dc.b	"_Update list",0
* D				"Liste erne_uern",0
CliHelpPage		dc.b	10,10,"KCommodity by Kai Iske. ShareWare",10
			dc.b	"USAGE : KCommodity",10
			dc.b	"  ToolType :          Description :              Default :",10
			dc.b	"  -----------------------------------------------------------------------",10
			dc.b	"  [CX_PRIORITY=n]     Priority of Broker         5",10
			dc.b    "  [CX_POPKEY=s]       PopUpKey for Window        LCOMMAND HELP",10
			dc.b	"  [CX_POPUP=YES|NO]   PopUp on startup           YES",10
			dc.b	"  [PORTNAME=s]        Set name for ARexx-Port    KComm.1",10
			dc.b	"  [TOOLPRI=n]         Priority for Program       0",10
			dc.b	"  [HANDLERPRI=n]      Priority for Handler       55; must be >= 51",10
			dc.b	"  [SETTINGS=s]        New filename for Prefs     ENV:KCX/KCX.PREFS",10
			dc.b	"  [PREFSPATH=s]       New path for Prefs-Prg     SYS:PREFS/KCommodity",10
			dc.b	"  [PRINTERPATH=s]     New path for printer       SYS:Utilities/KCXPrinter",10
			dc.b	"  [NOICON]            Don`t display AppIcon      ---",10
			dc.b	"  [NOMENU]            Don`t add AppMenuItem      ---",10
			dc.b	"  [ICONX]             X-Position for AppIcon     FINDBEST",10
			dc.b	"  [ICONY]             Y-Position for AppIcon     FINDBEST",10
			dc.b	"  [NOREQ]             Disable Quit-Requester     ---",10,10,10,0
* D				10,10,"KCommodity von Kai Iske. ShareWare",10
* D				"Benutzung : KCommodity",10
* D				"  ToolType :          Beschreibung :             Voreingestellt :",10
* D				"  -----------------------------------------------------------------------",10
* D				"  [CX_PRIORITY=n]     Priorität des Brokers      5",10
* D				"  [CX_POPKEY=s]       PopUpKey für Window        LCOMMAND HELP",10
* D				"  [CX_POPUP=YES|NO]   PopUp beim Starten         YES",10
* D				"  [PORTNAME=s]        Name des ARexx-Ports       KComm.1",10
* D				"  [TOOLPRI=n]         Priorität des Programms    0",10
* D				"  [HANDLERPRI=n]      Priorität des Handlers     55; muß >= 51 sein",10
* D				"  [SETTINGS=s]        Neuer Filename für Prefs   ENV:KCX/KCX.PREFS",10
* D				"  [PREFSPATH=s]       Neuer Pfad für Prefs-Prg   SYS:PREFS/KCommodity",10
* D				"  [PRINTERPATH=s]     Neuer Pfad für Printer     SYS:Utilities/KCXPrinter",10
* D				"  [NOICON]            Kein AppIcon zeigen        ---",10
* D				"  [NOMENU]            Kein AppMenuItem zeigen    ---",10
* D				"  [ICONX]             X-Position des AppIcon     BESTE FINDEN",10
* D				"  [ICONY]             Y-Position des AppIcon     BESTE FINDEN",10
* D				"  [NOREQ]             Ende-Requester unterbinden ---",10,10,10,0
LOCALE_END




		SECTION	Chips,DATA_C


**********************************************************************
*                           Sound of click                           *
**********************************************************************
ClickData	dc.w	-512,-256,-128,-64,-32,0,32,64,128,256,512
CLICKLEN	EQU	(*-ClickData)


**********************************************************************
*                         Clear sprite data                          *
**********************************************************************
NoSpriteDat	ds.l	1
		ds.l	1
		ds.l	1


		INCLUDE	KCX.BSS
		END
