/* GNUPLOT - linux.trm */
/*
 * Copyright (C) 1990
 *
 * Permission to use, copy, and distribute this software and its
 * documentation for any purpose with or without fee is hereby granted, 
 * provided that the above copyright notice appear in all copies and 
 * that both that copyright notice and this permission notice appear 
 * in supporting documentation.
 *
 * Permission to modify the software is granted, but not the right to
 * distribute the modified code.  Modifications are to be distributed 
 * as patches to released version.
 *
 * This software  is provided "as is" without express or implied warranty.
 *
 * This file is included by ../term.c.
 *
 * This terminal driver supports:
 *  VGA 640x480x16 for PC's running the Linux Operating System 
 *
 * AUTHOR
 *  Tommy Frandsen (frandsen@diku.dk) - original
 *  div - D.R.Denholm@southampton.ac.uk
 *      - this is now a seperate program, spawned by linux.trm
 *        linux.trm sends commands by pipe to stdin
 *        only this image need be installed with sticky bit
 *        [ is that the right unix lingo ? ]
 *
 * send your comments or suggestions to (pixar!info-gnuplot@sun.com).
 *
 */

#include "plot.h"
#include "fonts.h"  /* ripped out of bitmap.h */


#include <stdio.h>
#include <stdlib.h>
#include <vga.h>


#define LINUX_XMAX 640
#define LINUX_YMAX 480

#define LINUX_XLAST (LINUX_XMAX - 1)
#define LINUX_YLAST (LINUX_YMAX - 1)

#define LINUX_VCHAR FNT5X9_VCHAR
#define LINUX_HCHAR FNT5X9_HCHAR

#define LINUX_VTIC 5
#define LINUX_HTIC 5

static int graphics_on = 0;

int startx, starty;
int linux_angle=0;


static int vgacolor[] = {7,8,2,3,4,5,9,14,12,15,13,10,11,1,6};

static char buffer[1024];


LINUX_putc(x,y,c,ang,line_func)
unsigned int x,y;
char c;
int ang;
FUNC_PTR line_func;
{
int i, j, k;
unsigned int pixelon;
	i = (int)(c) - 32;
	for (j=0; j<FNT5X9_VBITS; j++) {
		for (k=0; k<FNT5X9_HBITS; k++) {
			pixelon = (((unsigned int)(fnt5x9[i][j])) >> k & 1);
			if (pixelon) {
				switch(ang) {
					case 0 : (*line_func)(x+k+1,y-j,x+k+1,y-j);
							break;
					case 1 : (*line_func)(x-j,y-k-1,x-j,y-k-1);
							break;
				}
			}
		}
	}
}



LINUX_put_text(x,y,str)
unsigned int x, y;
char *str;
{
int i;
	switch(linux_angle) {
		case 0 : y -= LINUX_VCHAR/2;
				break;
		case 1 : x += LINUX_VCHAR/2;
				break;
	}
	for (i=0;str[i];i++) {
		LINUX_putc(x,LINUX_YLAST-y,str[i],linux_angle,vga_drawline); 
		switch(linux_angle) {
			case 0 : x+=LINUX_HCHAR ;
					break;
			case 1 : y+=LINUX_HCHAR ;
					break;
		}
	}
}


main()
{
	int x,y;  /* current pixel */
	int p,q;
	while (gets(buffer))
		switch(*buffer)
		{
			case 'G' :	vga_setmode(G640x480x16); break;
			case 'L' :  sscanf(buffer+1, "%4d", &p);
									if (p >= 13)
										p %= 13;
									vga_setcolor(vgacolor[p+2]); 
									break;
			case 'M' :  sscanf(buffer+1,"%4d%4d", &x, &y); break;
			case 'V' :  sscanf(buffer+1,"%4d%4d", &p, &q);
							vga_drawline(x,LINUX_YLAST-y,p,LINUX_YLAST-q); 	
							x=p; y=q;
							break;
			case 'T' :  sscanf(buffer+1,"%4d%4d", &p, &q);
                  	LINUX_put_text(p,q,buffer+9);
                  	break;
			case 'A' :  sscanf(buffer+1, "%4d", &linux_angle);
							break;
			case 'E' :	vga_setmode(TEXT); break;
			case 'R' :	exit(0);
		}

	/* NOT REACHED */
	return 0;
}

