#include "plplot.h"
#include <stdio.h>
#include <limits.h>

/* This file contains the tektronix dependent routines for use with plplot. */

#define TEKX   1023
#define TEKY    779

/* Define graphics control characters. */
#define FF   12
#define CAN  24
#define ESC  27
#define GS   29
#define US   31

static FILE *OutFile;
static int select=0;
static int porient;
static char FileName[80];

void tektsetup(xdpi,ydpi,xwid,ywid)
PLFLT xdpi, ydpi;
PLINT xwid, ywid;
{
   /* ignore */
}

void tekfsetup(xdpi,ydpi,xwid,ywid)
PLFLT xdpi, ydpi;
PLINT xwid, ywid;
{
}

void tektinit()
{
      smod(1);   /* an interactive device */
      OutFile = stdout;
      teksetup(0);
}

void tekfinit()
{
      char response[80];
      int ori;

      smod(0);  /* not an interactive terminal */
      if(!select) {
         printf("Landscape or portrait orientation? (0 or 1): ");
         fgets(response,sizeof(response),stdin);
         if(sscanf(response,"%d",&ori) != 1)
            ori = 0;   /* carriage return defaults to landscape */
      }
      
      OutFile = NULL;
      while(!OutFile) {
         if(!select) {
            printf("Enter graphics command storage file name. ");
            fgets(response,sizeof(response),stdin);
            if(sscanf(response,"%s",FileName) != 1)  {
                printf("Invalid entry.n");
                continue;
            }
         }
         select =0;
         if ((OutFile = fopen(FileName,"w")) == NULL) 
             printf("Can't open %s.\n",FileName);
      }
      teksetup(ori);
}

teksetup(portrait)
int portrait;
{
      /* set default pen color*/
      scol(1);
      porient = portrait;

      if(!portrait) {
         /* set device resolution in dots/mm */
         setpxl(4.771*16,4.653*16);
         /* set page size */
         setphy(0,TEKX*16,0,TEKY*16);
      }
      else {
         /* set device resolution in dots/mm */
         setpxl(4.653*16,4.771*16);
         /* set page size */
         setphy(0,TEKY*16,0,TEKX*16);
      }
      fprintf(OutFile,"%c",GS);
}

/* Sets the tektronix to text mode */
void tektext()
{
    fprintf(OutFile,"%c",US);
}

/* Sets the tektronix to graphics mode */
void tekgraph()
{
    /* Nothing has to be done here */
}

/* Clears the tektronix screen */
void tektclear()
{
     putchar('\007');
     fflush(stdout);
     while(getchar() != '\n')
        ;
     fprintf(OutFile,"%c%c",ESC,FF);
}

void tekfclear()
{
     fprintf(OutFile,"%c%c",ESC,FF);
}

void tektselect(ori,file)
PLINT ori;
char *file;
{
}

void tekfselect(ori,file)
PLINT ori;
char *file;
{
   porient = ori;
   strncpy(FileName,file,sizeof(FileName)-1);
   FileName[sizeof(FileName)-1] = '\0';
   select = 1;
}

/* Change color */
void tekcolor(colour)
PLINT colour;
{
}

static PLINT xold, yold;

void tekpage()
{
   xold = -100000;
   yold = -100000;
}


/* Change pen width */
void tekwidth(width)
PLINT width;
{
}

/* Draws a line in the current colour from (x1,y1) to (x2,y2) */
void tekline(x1a,y1a,x2a,y2a)
PLINT x1a,y1a,x2a,y2a;
{
    int x1,y1,x2,y2,hy,ly,hx,lx;

    x1a >>= 4;
    y1a >>= 4;
    x2a >>= 4;
    y2a >>= 4;

    if(!porient) {
       x1 = x1a;
       y1 = y1a;
       x2 = x2a;
       y2 = y2a;
    }
    else {
       x1 = TEKX - y1a;
       y1 = x1a;
       x2 = TEKX - y2a;
       y2 = x2a;
    }

    /* If continuation of previous line just send new point */
    if(x1 == xold && y1 == yold) {
       hy = y2/32 + 32;
       ly = y2 - (y2/32)*32 + 96;
       hx = x2/32 + 32;
       lx = x2 - (x2/32)*32 + 64;
       fprintf(OutFile,"%c%c%c%c",hy,ly,hx,lx);
    }
    else {
       fprintf(OutFile,"%c",GS);
       hy = y1/32 + 32;
       ly = y1 - (y1/32)*32 + 96;
       hx = x1/32 + 32;
       lx = x1 - (x1/32)*32 + 64;
       fprintf(OutFile,"%c%c%c%c",hy,ly,hx,lx);
       hy = y2/32 + 32;
       ly = y2 - (y2/32)*32 + 96;
       hx = x2/32 + 32;
       lx = x2 - (x2/32)*32 + 64;
       fprintf(OutFile,"%c%c%c%c",hy,ly,hx,lx);
    }
    xold = x2;
    yold = y2;
}

/* Close file */
void tekftidy()
{
   fclose(OutFile);
}

void tekttidy()
{
   tektclear();
   fprintf(OutFile,"%c%c",US,CAN);
   fflush(OutFile);
}

