
version	macro
	dc.b	' v0.9 '
	endm

keyboard	equ   $bfec01		; some constants
mouse0		equ   $dff00a
mouse1		equ   $dff00c
diskcrt1	equ   $bfd100

nappis		equ   0			; pointers to the interrupt 
hiiri0		equ   2			; data field. (defined in the
hiiri1		equ   4			; end of this file.)
levyas		equ   6
laskuri		equ   8
gfxbase		equ   12
intuitionbase	equ   16
oldview		equ   20
calc		equ   24
time		equ   28

INTB_VERTB	equ   5
CTRL_C		equ   12

	include include/macros.i
	include	libraries/execxref.i
	include	libraries/graphicsxref.i
	include	libraries/intuitionxref.i
	include	graphics/gfxbase.i

	clr.b	-1(a0,d0)
	movea.l	a0,a5

	move.l	4,a6
	lea	gfxname(pc),a1
	moveq	#0,d0
	call	OpenLibrary			; open graphics library
	move.l	d0,IData+gfxbase
	beq	cleanup

	lea	intuitionname(pc),a1
	moveq	#0,d0
	call	OpenLibrary			; and intuition library
	move.l	d0,IData+intuitionbase
	beq	cleanup

	bsr	parsecommandline

	moveq	#INTB_VERTB,d0
	lea	keskstruct(pc),a1		; enable our interrupt 
	call	AddIntServer			; server

	moveq	#0,d0
	bset	#CTRL_C,d0			; wait for ctrl + c
	call	Wait

	move.l	#INTB_VERTB,d0
	lea	keskstruct(pc),a1		; remove server
	call	RemIntServer

cleanup	move.l	IData+gfxbase(pc),d0
	beq	_100
	move.l	d0,a1
	call	CloseLibrary			; and cleanup libraries
_100	move.l	IData+intuitionbase(pc),d0
	beq	_101
	move.l	d0,a1
	call	CloseLibrary

_101	moveq	#0,d0				; return code 0 = no error
	rts

;	*****************************************************************
;	* - Parse command line taks the user defined time from the 	*
;	*   command line.						*
;	*****************************************************************

parsecommandline
	moveq	#0,d0
	moveq	#0,d1
	move.l	a5,a0
	tst.b	(a0)
	beq	pc_exit
	tst.b	1(a0)
	beq	pc_onenumber
	tst.b	2(a0)
	beq	pc_twonumbers
	move.b	(a0)+,d0
	sub.l	#48,d0
	bmi	pc_exit
	cmp.b	#$a,d0
	bpl	pc_exit
	mulu.w	#100,d0
pc_twonumbers
	move.b	(a0)+,d1
	sub.l	#48,d1
	bmi	pc_exit
	cmp.b	#$a,d1
	bpl	pc_exit
	mulu.w	#10,d1
	add.l	d1,d0
pc_onenumber
	move.b	(a0)+,d1
	sub.l	#48,d1
	bmi	pc_exit
	cmp.b	#$a,d1
	bpl	pc_exit
	add.l	d1,d0
	mulu.w	#50,d0
	move.l	d0,IData+time
	rts
pc_exit	move.l	#50*120,IData+time
	rts

;	*****************************************************************
;	* Here starts our interrupt routine.				*
;	* - Firstly it just waits for some time				*
;	* - Then it loads a view NULL and bangs in a copperlist that	*
;	*   blanks the screen.						*
;	* - Waits for user activity and removes view when user does	*
;	*   something.							*
;	*****************************************************************

introutine
	push	all

	lea	$dff000,a0			; take current values
	move.b	keyboard,d4			; of keys and mouse...
	move.w	mouse0,d5
	move.w	mouse1,d6
	move.b	diskcrt1,d7

	cmp.w	hiiri0(a1),d5			; compare them them to
	bne	touhuaa				; previous values and 
	cmp.b	levyas(a1),d7			; figure out has the user
	bne	touhuaa				; done anything since last
	cmp.b	nappis(a1),d4			; vertical blank interrupt
	bne	touhuaa
	cmp.w	hiiri1(a1),d6
	bne	touhuaa				; yes -> init values.

ei_puuhaa
	move.l	laskuri(a1),d0			; no -> check have we waited 
	addq.l	#1,d0				; for those 20 secs??
	move.l	time(a1),d1
	cmp.l	d1,d0
	beq	sammuta				; yes -> load a blank view

	move.l	d0,laskuri(A1)
	pull	all
	moveq	#0,d0
	rts

sammuta	addq.l	#1,d0
	move.l	d0,laskuri(a1)

	move.l	gfxbase(a1),a6
	move.l	gb_ActiView(a6),oldview(a1)
	push	a1
	move.l	#0,a1	
	call	LoadView			; load null view.
	pull	a1

	move.l	#coplist,$dff080		; and bang the copperlist
	pull	all				; in that blanks the
	moveq	#0,d0				; screen.
	rts



touhuaa	move.b	d4,nappis(a1)			; store new values into 
	move.w	d5,hiiri0(a1)			; variables.
	move.w	d6,hiiri1(a1)
	move.b	d7,levyas(a1)
	clr.l	laskuri(a1)			; clear counter

	move.l	oldview(a1),d0			; and restore view back to 
	beq	touhuaa2			; normal IF neccessary.

	move.l	gfxbase(A1),a6
	push	a1
	move.l	d0,a1
	call	LoadView			; restore previous view.
	pull	a1
	move.l	gb_copinit(a6),$dff080		: return the copper list also.
	move.l	#0,oldview(a1)

touhuaa2
	pull	all
	moveq	#0,d0
	rts


keskstruct
	dc.l  0			; structure used to inform
	dc.l  0			; AddIntServer() routine.
	dc.b  2
	dc.b  0
	dc.l  intname
	dc.l  IData
	dc.l  introutine

IData	dc.w  0		; nappis	these are the datas that our
	dc.w  0		; hiiri0	interrupt server uses for
	dc.w  0		; hiiri1	storage space.
	dc.w  0		; levyas
	dc.l  0		; laskuri
	dc.l  0		; gfxbase
	dc.l  0		; intui
      	dc.l  0		; oldview
	dc.l  0		; calc
	dc.l  0		; time
	dc.w  0

intname		dc.b	'Blanker_Interrupt',0
gfxname		dc.b	'graphics.library',0
intuitionname	dc.b	'intuition.library',0
		dc.b	'$VER: Blanker'
		version
		dc.b	'By: Jarkko Vatjus-Anttila',0

	section	coppr,data_c		; out copper list. Must be in 
					; CHIP ram..
coplist	dc.w    $0100,$0200
	dc.w	$180,$000		; COLOR00 = $000 (black)

	dc.w    $ffff,$fffe		; double "end command" to make 
	dc.w    $ffff,$fffe		; sure the copper really stops.

	end
