#include <stdlib.h>
#include <intuition/intuitionbase.h>
#include <intuition/screens.h>
#include <exec/lists.h>
#include <exec/nodes.h>
#include <exec/memory.h>
#include <exec/interrupts.h>
#include <dos/dos.h>
#include <hardware/custom.h>
#include <hardware/intbits.h>
#include <libraries/retina.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/retina_protos.h>
#include <pragmas/retina_pragmas.h>

long __stack = 4096;
char *__procname = "RetinaSTF";
long __priority = 1;

extern void VertBServer();

struct IntuitionBase *IntuitionBase=NULL;
struct _xy_RetinaBase *RetinaBase=NULL;

LONG sig = -1;

ULONG mainsig;

struct Task *maintask = NULL;

#ifdef LATTICE
int CXBRK(void)  { return(0); }
void chkabort(void) { return; }
#endif

void main(int argc, char *argv[])
{
   struct Interrupt *vbint;
   ULONG screen = 0,sigs;
   
   if (argc==4) {
      
      *(((unsigned short *)VertBServer) + 63) = (unsigned short)atoi(argv[1]);
      *(((unsigned short *)VertBServer) + 68) = (unsigned short)atoi(argv[2]);
      *(((unsigned short *)VertBServer) + 73) = (unsigned short)atoi(argv[3]);
      
      IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",NULL);
      
      if (IntuitionBase!=NULL) {
   
         RetinaBase=(struct _xy_RetinaBase *)OpenLibrary("retina.library",NULL);
      
         if (RetinaBase!=NULL) {
         
            sig = AllocSignal(-1);
            if (sig!=-1) {
   
               mainsig = 1L << sig;
               maintask = FindTask(NULL);
               
               if (vbint = AllocMem(sizeof(struct Interrupt), MEMF_PUBLIC|MEMF_CLEAR)) {
                  vbint->is_Node.ln_Type = NT_INTERRUPT;
                  vbint->is_Node.ln_Pri = -60;
                  vbint->is_Node.ln_Name = "Retina_ScreenToFront";
                  vbint->is_Data = (APTR)&screen;
                  vbint->is_Code = VertBServer;
                     
                     
                  AddIntServer(INTB_VERTB, vbint);
                     
                  for (;;) {
                     sigs = Wait(SIGBREAKF_CTRL_C | mainsig);
                     if (sigs==SIGBREAKF_CTRL_C) {
                        break;
                     }
                     if (sigs==mainsig) {
                        Retina_ScreenToFront((struct RetinaScreen *)screen);
                     }
                  }
                    
                    
                  RemIntServer(INTB_VERTB, vbint);
                  FreeMem(vbint, sizeof(struct Interrupt));
               }
                
               FreeSignal(sig);
            }
            CloseLibrary((struct Library *)RetinaBase);
         }
         CloseLibrary((struct Library *)IntuitionBase);
      }
   }
}
