/*
 * STevie - ST editor for VI enthusiasts.    ...Tim Thompson...twitch!tjt...
 * vt100 mode + reading of LINES/COLUMNS for ATARI added by Kees Lemmens
 */

#include "stevie.h"
#include <stdio.h>

#ifdef ATARI
# include <osbind.h>
# define EscSeq(x) Cconout('\033');Cconout(x);
# define VT52		52
# define VT100		100
  int Termtype;
#endif
#ifdef UNIXPC
# include <sys/window.h>
#endif
#ifdef TCAP
# include <curses.h>
#endif

windinit()
{
#ifdef ATARI
	char *getenv();
	char *p = getenv("TERM");
    char *c = getenv("COLUMNS");
    char *l = getenv("LINES");
    
	Termtype=VT52;
	Columns=80;
	Rows=25;

	if ( p != NULL )
		if (strncmp(p,"vt100",5) == 0)
			Termtype = VT100;
	if ( c != NULL )
		Columns = atoi(c);
	if ( l != NULL )
		Rows = atoi(l);
#endif
#ifdef UNIXPC
        struct uwdata uw;
        
	winit();
	if ( ioctl(0,WIOCGETD,&uw) == -1
	  && ioctl(1,WIOCGETD,&uw) == -1
	  && ioctl(2,WIOCGETD,&uw) == -1 ) {
		fprintf(stderr,"*** ERROR *** Not a window!\n");
		windexit(1);
	}
	Columns = uw.uw_width / uw.uw_hs;
	Rows = uw.uw_height / uw.uw_vs;
	cbreak();
	nonl();
	noecho();
#endif
#ifdef TCAP
	char *getenv();
	char *p = getenv("TERM");

	initscr();
	Columns = 80;
	if ( strncmp(p,"vt52",4)==0 )
		Rows = 25;
	else
		Rows = 24;
	cbreak();
	nonl();
	noecho();
#endif
}

windgoto(r,c)
int r,c;
{
#ifdef UNIXPC
	printf("\033[%d;%dH",r+1,c+1);
#endif
#ifdef ATARI
	char *itoa();
	
	if (Termtype == VT100)
	{	printf("\033[%d;%dH",r+1,c+1);
	}
	else
	{	EscSeq('Y');
		Cconout(r+040);
		Cconout(c+040);
	}
#endif
#ifdef TCAP
	move(r,c);
#endif
}

windexit(r)
int r;
{
#ifdef UNIXPC
	nocbreak();
	nl();
	echo();
	wexit();
#endif
#ifdef TCAP
	nocbreak();
	nl();
	echo();
	endwin();
#endif
	exit(r);
}

windclear()
{
#ifdef UNIXPC
	printf("\033[H\033[J");
#endif
#ifdef ATARI
	if (Termtype == VT100)
		Cconws("\033[H\033[J");
	else
		Cconws("\033H\033J");
#endif
#ifdef TCAP
	clear();
	refresh();
#endif
}

windgetc()
{
#ifdef ATARI
	return(Cnecin());
#else
	return(getchar());
#endif
}

windstr(s)
char *s;
{
#ifdef ATARI
	Cconws(s);
#endif
#ifdef UNIXPC
	printf("%s",s);
#endif
#ifdef TCAP
	addstr(s);
	refresh();
#endif
}

windputc(c)
int c;
{
#ifdef ATARI
	Cconout(c);
#endif
#ifdef UNIXPC
	putchar(c);
#endif
#ifdef TCAP
	addch(c);
#endif
}

windrefresh()
{
#ifdef TCAP
	refresh();
#endif
}

beep()
{
#ifdef ATARI
	Cconout('\007');
#else
	putchar('\007');
#endif
}
