/*****************************************************************************/
/*
 *     fmpovlctrl.c -- REALmagic FMP Overlay Config and Control
 *
 *      Copyright (C) 2000 Sigma Designs
 *                    written by Pascal Cannenterre <pascal_cannenterre@sdesigns.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*****************************************************************************/
#include "define.h"
#include "allblackbut.h"
#include "headerdetection.h"

const char * strStreamType[] = {
  "UNKNOWN",
  "MPEG_VIDEO",
  "MPEG_AUDIO",
  "MPEG_SYSTEM",
  "MPEG_AVI",
  "MPEG2_VIDEO",
  "MPEG2_SYSTEM",
  "MPEG2_TRANSPORT",
  "DVD_VMG",
  "DVD_VTS",
  "AC3_AUDIO",
  "PES",
  "MPEG4_VIDEO",
  "DTS_AUDIO",
  "MPEG4_SYSTEM",
  "MPEG4_SYSTEMAUDIO",
  "MPEG4_SYSTEMVIDEO",
  "QUICKTIME",
  "DVD_VOB"
};

DWORD getStreamType(char * filename)
{
  DWORD rmdrivermode = UNDETERMINED_STREAM_TYPE;
  FileStreamType streamType;
  GetFileProperties(filename, &streamType);
  
  g_message("Stream type is %s", strStreamType[streamType.fileType]); 
  
  switch(streamType.fileType)
    {
    case FT_UNKNOWN:
      rmdrivermode = UNDETERMINED_STREAM_TYPE;
      break;
    case FT_MPEG_AVI:
    case FT_MPEG4_VIDEO:
    case FT_MPEG4_SYSTEMAUDIO:
    case FT_MPEG4_SYSTEMVIDEO:
    case FT_MPEG4_SYSTEM:
    case FT_QUICKTIME:
      rmdrivermode = UNSUPPORTED_STREAM_TYPE;
      break;
    case FT_MPEG_VIDEO:
    case FT_MPEG2_VIDEO:
      rmdrivermode = FMPF_VIDEO;
      break;
    case FT_MPEG_AUDIO:
      rmdrivermode = FMPF_MPEG_AUDIO;
      break;
    case FT_MPEG_SYSTEM:
    case FT_MPEG2_SYSTEM:
      rmdrivermode = FMPF_SYSTEM;
      break;
    case FT_MPEG2_TRANSPORT:
      rmdrivermode = FMPF_TRANSPORT;
      break;
    case FT_DVD_VOB:
    case FT_DVD_VMG:
    case FT_DVD_VTS:
      rmdrivermode = FMPF_DVD; 
      break;
    case FT_AC3_AUDIO:
    case FT_DTS_AUDIO:
      rmdrivermode = FMPF_AC3;
      break;
    case FT_PES:
      rmdrivermode = FMPF_PES;
      break;
    default:
      break;
    }
  return rmdrivermode;
}

BOOL loadOverlayConfig(FMPOVERLAYVDO * ovl)
{  
  DWORD FMPreturn;  
  getres(&(ovl->xres), &(ovl->yres), &(ovl->depth), &(ovl->freq));
  FMPreturn = LoadParamForThisResolution(ovl->xres,ovl->yres,ovl->depth,ovl->freq);

  if(FMPreturn > 0)
    {
      ovl->b_cfg_loaded = FALSE;
      return FALSE;
    }
  else
    {
      ovl->b_cfg_loaded = TRUE; 
      return TRUE;
    }
}




void screen1(Display *dpy)
{
	AllBlackButPattern(dpy, MY_DEEPBLUE,0,0,355,1);
}

void screen2(Display *dpy)
{
	AllBlackButPattern(dpy, MY_DEEPBLUE,0,0,2,288);
}

void screen3(Display *dpy)
{
	AllBlackButPattern(dpy, MY_DEEPBLUE,355,0,2,288);
}

void screen4(Display *dpy, DWORD xresolution, DWORD yresolution)
{
	AllBlackButPattern(dpy, MY_WHITE,(xresolution-200)/2,0,200,yresolution);
}

void screen5(Display *dpy, DWORD xresolution, DWORD yresolution)
{
	AllBlackButPattern(dpy, MY_GREY,(xresolution-200)/2,0,200,yresolution);
}


DWORD autocalibrate(FMPOVERLAYVDO * ovl)
{
/*         char autocalibrate_msg[] = */
/* 	{ "Unable to get the current resolution\n",*/
/* 	  "Autocalibration needs depth > 8 bpp\n",*/
/* 	  "Minimum resolution supported : 640x480\n" };*/

	calibrationInit();
        // switch output to VGA
	setOutputMode(FMPV_VIDEOOUT_VGA);
	AllBlackButInit(ovl->dpy);

 	if(getres(&(ovl->xres), &(ovl->yres), &(ovl->depth), &(ovl->freq)))
	  {
	    
	    return FMPE_ERROR;
	  }
			
	if (ovl->depth<16)
	  {
	    
	    return FMPE_ERROR;
	  }

	if (ovl->xres<640 && ovl->yres<480)
	  {
	    
	    return FMPE_ERROR;
	  }
	
	// set the display properties
	LoadParamForThisResolution( ovl->xres, ovl->yres, ovl->depth, ovl->freq);
	
	setOverlayResolution(ovl->xres,ovl->yres, ovl->depth);

	// Set mode to overlay
	setOverlay(2,UNUSED_ARG);				
	screen1(ovl->dpy); /* display pattern 1 */
	setOverlayCalibration(0x88);		
        /* Autocalibration 'YOffset' is done */
		
	screen2(ovl->dpy); /* display pattern 2 */
	setOverlayCalibration(0x89);		
	/* Autocalibration 'XOffset' is done */
				
	screen3(ovl->dpy); /* display pattern 3 */
	setOverlayCalibration(0x8a);		
	/* Autocalibration 'Correction' is done */

	screen4(ovl->dpy,ovl->xres, ovl->yres); /* display pattern 4 */
	setOverlayCalibration(0x80);		
	/* Autocalibration 'white' is done */

	screen5(ovl->dpy,ovl->xres, ovl->yres); /* display pattern 5 */
	setOverlayCalibration(0x82);		
	/* Autocalibration 'grey' is done */
	
	// Set mode to none
	setOverlay(0,UNUSED_ARG);                         	
	AllBlackButClose(ovl->dpy);
	calibrationEnd();

	return SaveParamForThisResolution(ovl->xres,ovl->yres,ovl->depth,ovl->freq);

}

void initialize_info_structure_disc(INFO * info)
{
        memset (info,0,sizeof(INFO));
	// info->pDiscTimer = NULL;
	// info->b_stop_timer = 0;
        info->symbol		= 0;
	info->bDiscType = FMPC_UNKNOWN;
	info->playing	= CLOSED;
	info->test	= 0;
	info->maddie		= 0;
// should get this info from navigator
	info->bFullScreen = 1;
	info->tv_format = TV_4x3;

	info->CDDAcurrenttrack = 1;
	
// Position and Size of the window
	info->dwX	= 320;
	info->dwY	= 0;
	info->dwcX	= 320;
	info->dwcY	= 240;
	info->bStep	= 10;
	info->bPosAction = POS_NO_ACTION;
//	info->pIRma = NULL;
//	info->IHdtv_AR = 0;
	info->dwTime = 0;
	info->totalTime = 0;
	info->osdFlag = 0;
	info->blk = 0;
	info->bMode = 0;
	info->bSearch = 0;
}

void reinitialize_info_structure_disc(INFO * info)
{
        info->symbol		= 0;
	info->bDiscType = FMPC_UNKNOWN;
	info->bVCDsubtype = 0;
	info->playing	= CLOSED;
	info->test	= 0;
	info->maddie		= 0;
// should get this info from navigator
	info->bFullScreen = 1;
	info->tv_format = TV_4x3;
	info->CDDATrackDuration = NULL;
        info->previous_trackTime = 0;
	info->CDDAcurrenttrack = 1;
	info->CDDAtracknumber = 0;
	info->bABRepeat = 0;
	info->bRepeat	= 0;
	
	info->bTitleTotal = 0;
	memset (info->titleAngle,0,sizeof(info->titleAngle));
	memset (info->titleChapter,0,sizeof(info->titleChapter));

	info->bAudioStreamTotal = 0;
	memset (info->AudioStreamLangage,0,sizeof(info->AudioStreamLangage));
	memset (info->AudioStreamLangageExt,0,sizeof(info->AudioStreamLangageExt));
	memset (info->AudioStreamCodingMode, 0,sizeof(info->AudioStreamCodingMode));
	memset (info->AudioStreamChannel, 0, sizeof(info->AudioStreamChannel));
	memset (info->AudioStreamType, 0, sizeof(info->AudioStreamType));
	memset (info->AudioStreamAvailable, 0, sizeof(info->AudioStreamAvailable));

	info->bSPStreamTotal = 0;
	memset (info->SPStreamLangage, 0, sizeof(info->SPStreamLangage));
	memset (info->SPStreamLangageExt, 0, sizeof(info->SPStreamLangageExt));
	memset (info->SPStreamAvailable, 0, sizeof(info->SPStreamAvailable));
	
	info->bTitle = 0;
	info->bChapter = 0;
	info->bAudioStream = 0;
	info->bAngleNumber = 0;
	info->bSPStream = 0;
	info->SPStreamOn = FALSE;
	
// Position and Size of the window
	info->dwX	= 320;
	info->dwY	= 0;
	info->dwcX	= 320;
	info->dwcY	= 240;
	info->bStep	= 10;
	info->bPosAction = POS_NO_ACTION;
//	info->pIRma = NULL;
//	info->IHdtv_AR = 0;
	info->dwTime = 0;
	info->totalTime = SEEK_BAR_DEFAULT_LENGTH;
	info->previous_totalTime = SEEK_BAR_DEFAULT_LENGTH;
	info->osdFlag = 0;
	info->blk = 0;
	info->bMode = 0;
	info->bSearch = 0;
}

void initialize_info_structure_file(INFO * info)
{
	info->symbol		= 0;
	info->bDiscType = LOCALFILE;
	info->playing	= CLOSED;
	info->test	= 0;
	info->maddie		= 0;

	info->dwBufferSize = 32*1024;
	info->dwNBuffer    = 10;
	info->b_stop = 0;
	info->pFileplayer = NULL;
	/* info->sFilename = NULL; */
	info->b_resume = 0;
	info->bRepeat = -1;
	info->dwStreamtype = FMPF_SYSTEM;
	
	info->dwTime = 0;
	info->totalTime = SEEK_BAR_DEFAULT_LENGTH;
	info->previous_totalTime = 0;

	info->osdFlag = 0;
	info->blk = 0;
	info->bMode = 0;
	info->bSearch = 0;
}

void initialize_info_structure_rtsp(INFO * info)
{
	info->bDiscType = RTSPURL;
	info->playing	= CLOSED;
	/* info->sFilename = NULL; */
	/* info->sUrl      = NULL; */
	info->b_resume  = 0;
	info->bRepeat   = -1;
	
	info->dwTime = 0;
	info->totalTime = SEEK_BAR_DEFAULT_LENGTH;
	info->previous_totalTime = 0;

	info->osdFlag = 0;
	info->blk = 0;
	info->bMode = 0;
	info->bSearch = 0;
}

void reinitialize_gui_state(FMPOVERLAYVDO * ovl)
{
  ovl->b_overlay_access = FALSE;
  ovl->b_vga_cable_present = FALSE;
  ovl->b_cfg_loaded = FALSE;
  ovl->b_mute = FALSE;
  /* ovl->b_fullscreen = FALSE; */
}

void initialize_gui_state(FMPOVERLAYVDO * ovl)
{
  memset (ovl,0,sizeof(FMPOVERLAYVDO));
  
  ovl->key_color = 0x004000;
  ovl->b_overlay_access = FALSE;
  ovl->b_vga = TRUE;
  ovl->b_vga_cable_present = FALSE;
  ovl->b_cfg_loaded = FALSE;
  ovl->b_mute = FALSE;
  ovl->b_fullscreen = FALSE;
  
  ovl->video_output_mode = FMPV_VIDEOOUT_VGA;
  ovl->tv_standard = FMPV_VIDEOOUT_NTSC;
  ovl->tv_component = FMPV_VIDEOOUT_COMPOSITE;
  ovl->brt = BRT_RESET;
  ovl->sat = CSC_RESET;
  ovl->con = CON_RESET;
  
  ovl->volume = VOL_RESET;
  ovl->balance = BAL_RESET;
  ovl->audio_mode = FMPV_AUDIO_MODE_STEREO;
  ovl->audio_output = PCM;
  
  ovl->dpy = XOpenDisplay (NULL);

  if (!ovl->dpy) 
    _exit(1);  
  
  ovl->scr = DefaultScreen(ovl->dpy);
}
