#include <exec/types.h>
#include <exec/ports.h>
#include <exec/memory.h>
#include <exec/io.h>
#include <exec/tasks.h>
#include <exec/interrupts.h>
#include <intuition/intuition.h>
#include <graphics/gfxbase.h>
#include <graphics/gfxmacros.h>
#include <devices/input.h>
#include <exec/devices.h>
#include <devices/inputevent.h>
#include <hardware/custom.h>
#include <hardware/dmabits.h>

#define TIMEINTERVAL  1
#define LIMIT 60
#define CONTROL_C ((SetSignal(0,0x1000)&0x1000))

extern struct MsgPort *CreatePort();
extern struct IOStdReq *CreateStdIO();
extern void HandlerInterface();
extern struct Custom custom;

struct MsgPort *inputDevPort,*timerport;
struct IOStdReq *inputRequestBlock;
struct Interrupt handlerStuff;
struct MemEntry me[10];
struct timerequest *timerreq;
struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Screen *darkscreen;

struct NewScreen dark =
 {0,0,320,30,1,0,1,NULL,CUSTOMSCREEN,NULL,NULL,NULL,NULL};

LONG ereigniss;
ULONG sekunden;
BOOL  Bild=TRUE;

void WaitForTimer(tr,seconds)
struct timerequest *tr;
ULONG seconds;
{
   tr->tr_node.io_Command=TR_ADDREQUEST;
   tr->tr_time.tv_secs=seconds;
   tr->tr_time.tv_micro=0;
   DoIO(tr);
}

void dunkel()
{
   if((darkscreen=(struct Screen *)OpenScreen(&dark))!=0)
     { 
       OFF_DISPLAY;
       SetRGB4(&(darkscreen->ViewPort),0,0,0,0);
       Bild=FALSE;
     } 
}

void hell()
{
   if(darkscreen!=0)CloseScreen(darkscreen);
   ON_DISPLAY;
   Bild=TRUE;
}

void _main()
{
        if((IntuitionBase=(struct IntuitionBase *)
            OpenLibrary("intuition.library",0))==NULL)XCEXIT(10);
        if((GfxBase=(struct GfxBase *)
            OpenLibrary("graphics.library",0))==NULL)goto abbruch6;

	if((inputDevPort = CreatePort(0,0))==NULL)goto abbruch5;
	if((inputRequestBlock = CreateStdIO(inputDevPort))==NULL)goto abbruch4;
	if(OpenDevice("input.device",0,inputRequestBlock,0)!=0)goto abbruch3;

        if((timerport=CreatePort(0,0))==NULL)goto abbruch2;
        if((timerreq=(struct timerequest *)
            AllocMem(sizeof(struct timerequest),MEMF_PUBLIC))==NULL)
            goto abbruch1;
        timerreq->tr_node.io_Message.mn_Node.ln_Type=NT_MESSAGE;
        timerreq->tr_node.io_Message.mn_Node.ln_Pri=0;
        timerreq->tr_node.io_Message.mn_ReplyPort=timerport;
        if(OpenDevice(TIMERNAME,UNIT_VBLANK,timerreq,0)!=0)goto abbruch0;

        handlerStuff.is_Data = (APTR)&me[0];
	handlerStuff.is_Code = HandlerInterface;
	handlerStuff.is_Node.ln_Pri = 51;
        inputRequestBlock->io_Command = IND_ADDHANDLER;
	inputRequestBlock->io_Data = (APTR)&handlerStuff;
	DoIO(inputRequestBlock);  

        while(!CONTROL_C)
         {
           WaitForTimer(timerreq,TIMEINTERVAL);
           if(ereigniss==0)
               {
                  if(Bild && (++sekunden > LIMIT)) 
                     dunkel();
               }
           else
               {
                  ereigniss=0;
                  sekunden=0;
                  if(!Bild)
                     hell();
               }
         }
  
         inputRequestBlock->io_Command = IND_REMHANDLER;
	 inputRequestBlock->io_Data = (APTR)&handlerStuff;
	 DoIO(inputRequestBlock);

         CloseDevice(timerreq);
 abbruch0:
         FreeMem(timerreq,sizeof(struct timerequest));
 abbruch1:       
         DeletePort(timerport);
 abbruch2:
	 CloseDevice(inputRequestBlock);
 abbruch3: 
         DeleteStdIO(inputRequestBlock);
 abbruch4:        
         DeletePort(inputDevPort);
 abbruch5:
         CloseLibrary(GfxBase);
 abbruch6:
         CloseLibrary(IntuitionBase);
         XCEXIT(20);
}

