/* Unfinished. */

#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <string.h>

#include "../common/config.h"
#include "../common/args.h"

#include "main.h"
#include "dface.h"
/* For \\, \r, and \n.	*/
void DecodeSlash(char *src)
{
 char *dest=src;

 while(*src)
 {
  if(*src=='\\')
  {
   src++;
   if(!*src) break;
   if(*src=='n') *dest='\n';
   else if(*src=='r') *dest='\r';
   else if(*src=='\\') *dest='\\';
  }
  
  src++;
  dest++;
 }
}

static int dissylength=0;
static int Dissy(uint16 a, char *s)
{
 printf("%04x %s\n",a,s);
 return(--dissylength);
}
void FCEUDP_CheckInterface(void)
{
	int check;
  
        if(!ioctl(fileno(stdin),FIONREAD,&check) && check)
	{
	 uint8 buffer[1024],cmd[1024],args[1024];
	 int l;

	 fgets(buffer,1024,stdin);
	 l=sscanf(buffer,"%s %[^\r\n]",cmd,args);
	 if(l==1)
	 {
	  if(!strcmp(cmd,"reset")) FCEUI_ResetNES();
	  else if(!strcmp(cmd,"power")) FCEUI_PowerNES();
	  else if(!strcmp(cmd,"close")) FCEUI_CloseGame();
	 }
	 else if(l==2)
	 {
	  DecodeSlash(args);
	  if(!strcmp(cmd,"setvidsys")) 
	  {
	   KillVideo();
	   FCEUI_SetVidSystem(atoi(args));
	   InitVideo();
	  }
	  else if(!strcmp(cmd,"setgg")) FCEUI_SetGameGenie(atoi(args));
	  else if(!strcmp(cmd,"load")) 
	  {
	   if(DoGameLoad(args)) puts("1");
	   else puts("0");
	  }
	  else if(!strcmp(cmd,"disassemble"))
	  {
	   unsigned int a;
	   if(sscanf(args,"%04x %d",&a,&dissylength)==2 && dissylength>0)
	    FCEUI_Disassemble(a,Dissy);
	  }
	 }
	 // printf("Args: %s Cmd: %s\n",args,cmd);
	}
}
