#ifdef MSDOS
#include <conio.h>
#endif
#include "global.h"
#ifdef SCREENSAVER
#include "commands.h"
#include "session.h"

#if !defined(_lint)
static char rcsid[] OPTIONAL = "$Id: ssaver.c,v 1.16 1997/07/31 00:44:20 root Exp $";
#endif

#if defined(TNOS_68K) || defined(UNIX)
extern unsigned char SCREENwidth;
#endif
extern unsigned char SCREENlength;
int32 LastIO;
static int32 ScreenSaver = 0;
static int ss_line, state, delayit, statline;

extern void statLineToggle (int clearmarquee);
#if !defined(TNOS_68K) && !defined(UNIX)
void setscreens (int back, int fore, int clr);
#endif


#if defined(TNOS_68K) || defined(UNIX)
#undef tputs
#undef newscreen
/*lint -save -e659 */
#undef newscreen
#ifdef HAVE_NCURSES_H
#include <ncurses.h>
#else
#include <curses.h>
#endif
/*lint -restore */
static WINDOW *saved;
#ifdef TNOS_68K
extern short UseCurses;
#endif
#ifdef TNOS_68K
extern chtype MY_NORMAL;
#endif
#endif

#ifdef UNIX
extern int16 intrace;
#endif


#define DELAYVAL 2

void
screensaver ()
{
#ifdef UNIX
static short back, fore;
#endif

	if (!ScreenSaver)
		return;
#ifdef TNOS_68K
	if (!UseCurses)
		return;
#endif
	if (secclock() > (LastIO + ScreenSaver))	{
		if (!state)	{
			statline = Current->screen->statline;
			if (statline)
				statLineToggle (1);
			state = 1;
/* next line is a problem for linux version */
			swapscreen (Current, NULLSESSION);
#if defined(TNOS_68K) || defined(UNIX)
			saved = newwin (SCREENlength, SCREENwidth, 0, 0);
#ifdef TNOS_68K
			wattrset (saved, MY_NORMAL);
#else
			(void) pair_content (1, &fore, &back);
			(void) init_pair (1, COLOR_WHITE, COLOR_BLACK);
			wattrset (saved, COLOR_PAIR(1));
			(void) wbkgd (saved, COLOR_PAIR(1));
#endif
			(void) leaveok (saved, 1);
#else
			_setcursortype (_NOCURSOR);
#endif
			ss_line = delayit = 0;
		}
		if (!delayit++)	{
#if !defined(TNOS_68K) && !defined(UNIX)
			setscreens (0, 7, 1);
			gotoxy (12, ++ss_line);
			ss_line %= SCREENlength;
			printf (" TNOS Screen Saver Active... Press any key to continue ");
			(void) fflush (stdout);
#else
			(void) werase (saved);
			(void) wmove (saved, ss_line++, 12);
			ss_line %= SCREENlength;
			(void) waddstr (saved, " TNOS Screen Saver Active... Press any key to continue ");
			(void) wrefresh (saved);
#endif
		}
		delayit %= DELAYVAL;
	} else if (state)	{
#if defined(TNOS_68K) || defined(UNIX)
		(void) delwin (saved);
#endif
		swapscreen (NULLSESSION, Current);
		state = 0;
#if defined(TNOS_68K) || defined(UNIX)
		_setcursortype (_NORMALCURSOR);
#endif
#ifdef UNIX
		(void) init_pair (1, fore, back);
#endif
		if (statline)
			statLineToggle(1);
	}
}

/* set the screen saver value */
int dossaver(int argc, char *argv[], void *p OPTIONAL)
{
	LastIO = secclock();
	return setlong(&ScreenSaver,"Screen Saver delay (seconds)",argc,argv);

}

void
sskick()
{
#ifdef UNIX

	if (!intrace && ScreenSaver)	{
#else
	if (ScreenSaver)	{
#endif
		LastIO = secclock();
		if (state)
			screensaver();
	}
}

int
ssenabled ()
{
	return state;
}

#endif /* SCREENSAVER */

