/* include file to provide grafics
   AMIGA SPECIAL VERSION TO SUPPLY RASTPORTS ! */
/* functions supported:
   tekopen();
   vectoa(x,y) int x,y;
   posita(x,y) int x,y;
   dperas();
   gfxfont(number,size) int number,size;
   gfxtext(s,a) char s[]; float a;
   textlen(s) char s[];
   textheight()
   getxy(x,y) int *x,*y;
   gfxflush();
   textparam(sx,sy,tx,ty,w,namw) int sx,sy,tx,ty,w; char *name;
   rmtek();
   setcolour(n) int n;
   special_gfx(s) char *s;          ; do special commands like PostScript output
*/

#define TMPGCURSOR   "t:gcursor"
#define ASHBDRY      "t:ashbdry" 

#define _TEKXMAX 4096
#define _TEKYMAX 3120

#ifndef _SPECHDR
#include <spec.h>
#include <auxcfg.h>
#include <libraries/dos.h>
#endif

int  _tek4014,
     _tek4014_flg = 0,
     _gfxbufp = 0,
     _gfxfontnr=1,
     _gfxfontsz=1;

void  (*positax)(),
      (*vectoax)(),
      (*gfxtextx)(),
      (*dperasx)(),
      (*getxyx)(),
      (*setcolourx)(),
      (*rmtekx)(),
      (*textparamx)(),
      (*hcox)(),
      (*special_gfxx)(),
      (*textlenx)(),
      (*textheightx)();

void cvtxy(xx,yy,x,y)
int xx,yy,*x,*y;
{

   switch(_win_flg){
   case 0:
      *x=xx ; *y=yy; break;
   case 1:
      *x=xx ; *y=yy/2; break;
   case 2:
      *x=xx ; *y=yy/2 + (_TEKYMAX/2); break;
   case 3:
      *x=xx/2 ; *y=yy/2 + (_TEKYMAX/2); break;
   case 4:
      *x=xx/2 + (_TEKXMAX/2) ; *y=yy/2 + (_TEKYMAX/2); break;
   case 5:
      *x=xx/2 ; *y=yy/2; break;
   case 6:
      *x=xx/2 + (_TEKXMAX/2) ; *y=yy/2; break;
   }
}

void vectoa(x,y)
int x,y;
{
int xx,yy;

   if(x > _TEKXMAX) x = _TEKXMAX;
   if(x < 0) x = 0;
   if(y > _TEKYMAX) y = _TEKYMAX;
   if(y < 0) y = 0;
   cvtxy(x,y,&xx,&yy);
   (*vectoax)(xx,yy);
}

void posita(x,y)
int x,y;
{
int xx,yy;

   if(x > _TEKXMAX) x = _TEKXMAX;
   if(x < 0) x = 0;
   if(y > _TEKYMAX) y = _TEKYMAX;
   if(y < 0) y = 0;
   cvtxy(x,y,&xx,&yy);
   (*positax)(xx,yy);
}

void gfxtext(s,a)
char s[];
float a;
{
char *z;

   z = (char *) malloc(256);

   if(_win_flg==0) {
      strcpy(z,"\x1b"); /* normal text size */
      strcat(z,"9");
   } else {
      strcpy(z,"\x1b;"); /* small text size */
   }
   strcat(z,s);
   (*gfxtextx)(z);

   free(z);
}

void dperas()
{
   (*dperasx)();
}

void getxy(x,y) 
int *x,*y;
{
int xx,yy;

   (*getxyx)(&xx,&yy);
   cvtxy(xx,yy,x,y);
}

void setcolour(n)
int n;
{
   (*setcolourx)(n);
}

void rmtek()
{
   (*rmtekx)();
}

void textparam(sx,sy,tx,ty,w,name)
int sx,sy,tx,ty,w;
char name[];
{
   (*textparamx)(sx,sy,tx,ty,w,name);
}


void hco(str)
char str[];
{
   (*hcox)(str);
}

void special_gfx(s)
char *s;
{
   (*special_gfxx)(s);
}

tekopen()
{
char c,*s,*z,*str;
int n;
struct FileHandle *input, *output;

   s = (char *) malloc(80);
   z = (char *) malloc(80);
   str = (char *) malloc(80);

   strcpy(s,"gfx");
   c=_tekpipe+48;
   z[0]=c; z[1]=0;
   strcat(s,z);
   _tek4014=auxopen(s); /* _tekpipe is defined in spec.h */
   if(_tek4014 <= 0) {
      strcpy(str,"run >nil: <nil: lise:tekamiga -p "); strcat(str,z);
      input = (struct FileHandle *) Open("nil:",MODE_OLDFILE);
      output = (struct FileHandle *) Open("nil:",MODE_NEWFILE);
      Execute(str,input,output);
      n = 0;
      while(n < 60) {
         Delay(50L);
         _tek4014=auxopen(s); /* _tekpipe is defined in spec.h */
         if(_tek4014 > 0) {Delay(25L); break;}
      }
   }
   if(_tek4014 < 0) {
      fprintf(stderr,"failed to open graphic file\n");
      exit(-1);
   }
   n=read(_tek4014,&positax,4);
   n=read(_tek4014,&vectoax,4);
   n=read(_tek4014,&dperasx,4);
   n=read(_tek4014,&gfxtextx,4);
   n=read(_tek4014,&getxyx,4);
   n=read(_tek4014,&setcolourx,4);
   n=read(_tek4014,&rmtekx,4);
   n=read(_tek4014,&textparamx,4);
   n=read(_tek4014,&hcox,4);
   n=read(_tek4014,&special_gfxx,4);
   n=read(_tek4014,&textlenx,4);
   n=read(_tek4014,&textheightx,4);
   _tek4014_flg=1;
   close(_tek4014); /* open handle makes no sense on the Amiga */
   free(s); free(z); free(str);
   return(_tek4014);
}

void gfxflush()
{
   if(_tek4014_flg==0) tekopen();
}

textlen(s)
char s[];
{
int n;

   (*textlenx)(s,&n);
   return(n);
}

textheight()
{
int n;

   (*textheightx)(&n);
   return(n);
}

void gfxfont(number,size)
int number,size;
{
   _gfxfontnr=number;
   _gfxfontsz=size;
}
