#include <stdlib.h>
#include <string.h>
#include <stream.h>
#include "cgidbg.h"

#include "usercom.h"
#include "dsp_app.h"

#include "plotdatg.h"

#include "remcom.h"


#include "grphio.h"
#include "shared.h"
#include <stdio.h>
#include "dyntextg.h"

char * GraphicsString[InputEndOfInputTypes] ;

const BufSize = 4096 ;

void LocalCommCleanUp()
{
	if (!WriteSeg) return ;
	PacketHeader Head(PacketControl,PacketControlExit);
	SharedMemoryExitState = 1 ;
	WriteSeg->WritePacket(Head);
	for (int i =0 ; i < 1000000;i++) ; // spin your wheels
	// sleep (1) ; // allow enough time for message to be sent before
		    // deleting buffers
}



void ExitMinimalCleanUp()
{
	// TheWindowsManager->CloseCgi();
	LocalCommCleanUp() ;
	exit(1);
}

void ExitAndCleanUp()
{
	ExitCleanUp();
	LocalCommCleanUp();
}

void SignalCleanUp()
{
	ExitCleanUp();
}


int WindowWrite(enum OutputType type,char c)
{
	DbgError("WindowWrite","no manager");

/*
 *	int WindowIndex = TheWindowsManager->OutWindowIndex(type);
 *	static char * buf = "X";
 *	buf[0] = c;
 *	LogOut << "WindowWrite(" << type << ", " << buf << "):" <<
 *		WindowIndex << "\n" ;
 */

	// return TheWindowsManager->WriteCharacter(type,c) ;
	return 0 ;
}

extern char * GetGraphicsRawBufLine(enum InputType type,const char * )
{
	static char RawBuf[BufSize] ;
	if (GraphicsStringAvailable[type]) {
		// LogMsg("GetGraphicsRawBufLine::", GraphicsString[type]);
		strcpy(RawBuf,GraphicsString[type]);
		GraphicsStringAvailable[type] = 0 ;
		return RawBuf;
	}
	return 0 ;
}

int GraphicsReturnOrQuit(OutputType typ)
{
	DbgError("GraphicsReturnOrQuit","called");
	return 0 ;
/*
 *	// LogOut << "GraphicsReturnOrQuit\n" ;
 *	if (typ == OutputUndefined) typ = Output->GetOutputType();
 *	TextWindow * Win = TheWindowsManager->GetOutWindow(typ);
 *	int ReSuspend = 0 ;
 *	if (Win->IsDrawSuspended()) {
 *		Win->ClearSuspend() ;
 *		// LogOut << "GraphicsReturnOrQuit Redraw\n" ;
 *		Win->Redraw();
 *		ReSuspend = 1 ;
 *	}
 *	// LogForm("ReturnToContinue(%d) called", typ);
 *	int Return = TheWindowsManager->ReturnOrQuit(typ);
 *	if (ReSuspend) Win->SetSuspend(); 
 *	Win->NewPage() ;
 *	return Return ;
 */
}


void GraphicsReturnToContinue(OutputType typ)
{
/*
 *	// LogOut << "GraphicsReturnToContinue\n" ;
 *	if (typ == InputUndefined) typ = Output->GetOutputType();
 *	TextWindow * Win = TheWindowsManager->GetOutWindow(typ);
 *	int ReSuspend = 0 ;
 *	if (Win->IsDrawSuspended()) {
 *		Win->ClearSuspend() ;
 *		ReSuspend = 1 ;
 *	}
 *	// LogForm("ReturnToContinue(%d) called", typ);
 *	TheWindowsManager->ReturnToContinue(typ);
 *	Win->NewPage() ;
 *	if (ReSuspend) Win->SetSuspend(); 
 */
}


