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

/* This file contains the xterm 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
#define ETX  003

void xtesetup(xdpi, ydpi, xwid, ywid)
PLINT xwid, ywid;
PLFLT xdpi, ydpi;
{
   /* the user doesn't know what he's talking about. ignore this stuff */
}

void xteselect(ori, name)
PLINT ori;
char *name;
{
}

void xteinit()
{
      /* tell plplot that this is an interactive device (so pause after */
      /* drawing graph).  use if smod(0) if sending grphx to a file. */
      smod(1);   /* interactive device */

      /* set default pen color (this should be the desired pen number) */
      /* plplot will actually tell the device to use this pen by */
      /* making a call to plcolor. */
      scol(1);

      swid(1);
      /* set device resolution in dots/mm */
      setpxl(4.771*16,4.653*16);

      /* set page size using setphy(xmin,xmax,ymin,ymax) */
      /* plplot assumes that the min coordinates are in the lower left */
      setphy(0,16*TEKX,0,16*TEKY);

      printf("%c[?38h",ESC); /* open graphics window */
      printf("%c",GS);       /* set to vector mode */
      printf("%c%c",ESC,FF);
}

/* Sets to text mode */
void xtetext()
{
    printf("%c",US);
}

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

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

static PLINT xold, yold;

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

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

void xtewidth(width)
PLINT width;
{
}

/* Draws a line in the current colour from (x1,y1) to (x2,y2) */
void xteline(x1,y1,x2,y2)
PLINT x1,y1,x2,y2;
{
    PLINT hy,ly,hx,lx;

    x1 >>= 4;
    y1 >>= 4;
    x2 >>= 4;
    y2 >>= 4;
    /* 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;
       printf("%c%c%c%c",hy,ly,hx,lx);
    }
    else {
       printf("%c",GS);
       hy = y1/32 + 32;
       ly = y1 - (y1/32)*32 + 96;
       hx = x1/32 + 32;
       lx = x1 - (x1/32)*32 + 64;
       printf("%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;
       printf("%c%c%c%c",hy,ly,hx,lx);
    }
    xold = x2;
    yold = y2;
}

void xtetidy()
{
   putchar('\007');
   fflush(stdout);
   while(getchar() != '\n')
      ;
   printf("%c%c",US,CAN);
   printf("%c%c",ESC,ETX);
   fflush(stdout);
}

