/*
 * em84xx.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */

#include "device.h"
#include "player.h"
#include "common.h"
#include <vdr/plugin.h>


static const char *VERSION        = "0.1.0";
static const char *DESCRIPTION    = "An output plugin for sigma em84xx chips";
#ifndef EM_HASNO_DVD
static const char *MAINMENUENTRY  = "Play Disk";
#endif

// config-parms
int sTvIs169 = false;
int sTv169HandleOther = 0;
int sTv43HandleOther = 0;
int sSpdifForward = false;
int sOSDHas4MB = false;

class cPluginEm84xx : public cPlugin {
public:
   cPluginEm84xx(void);
   virtual ~cPluginEm84xx();
   virtual const char *Version(void) { return VERSION;}
   virtual const char *Description(void) { return DESCRIPTION;}
   virtual const char *CommandLineHelp(void);
   virtual bool ProcessArgs(int argc, char *argv[]);
   virtual bool Initialize(void);
   virtual bool Start(void);
   virtual void Stop(void);
#ifdef EM_HASNO_DVD
   virtual const char *MainMenuEntry(void) { return NULL;}
#else 
   virtual const char *MainMenuEntry(void) { return MAINMENUENTRY;}
#endif   
   virtual cOsdObject *MainMenuAction(void);
   virtual cMenuSetupPage *SetupMenu(void);
   virtual bool SetupParse(const char *Name, const char *Value);
};

class cMenuSetupEm84 : public cMenuSetupPage {
private:
   int mTvIs169;
   int mTv169HandleOther;
   int mTv43HandleOther;
   int mSpdifForward;
   int mOSDHas4MB;

   const char* tTvIs169[2];
   const char* tTv169HandleOther[3];
   const char* tTv43HandleOther[2];
   const char* tSpdifForward[2];

protected:
   virtual void Store(void);
public:
   cMenuSetupEm84(void);
};

cMenuSetupEm84::cMenuSetupEm84(void)
{
   mTvIs169     = sTvIs169;
   mTv169HandleOther  = sTv169HandleOther;
   mTv43HandleOther   = sTv43HandleOther;
   mSpdifForward   = sSpdifForward;
   mOSDHas4MB = sOSDHas4MB;

   tTvIs169[0] = tr("4:3");
   tTvIs169[1] = tr("16:9");

   tTv43HandleOther[0] = tr("Letterbox");
   tTv43HandleOther[1] = tr("Pan & scan");

   tTv169HandleOther[0] = tr("Zoom on");
   tTv169HandleOther[1] = tr("Zoom of");
   tTv169HandleOther[2] = tr("Nothing");

   tSpdifForward[0] = tr("Analog + Spdif Downmix");
   tSpdifForward[1] = tr("AC3/DTS on Spdif");

   Add(new cMenuEditBoolItem( tr("OSD 4MB"), (int *)&mOSDHas4MB ));
   Add(new cMenuEditStraItem( tr("TV is"),       &mTvIs169, 2, tTvIs169));
   Add(new cMenuEditStraItem( tr("Handle 16:9 on 4:3 TV"),      &mTv43HandleOther, 2, tTv43HandleOther));
   Add(new cMenuEditStraItem( tr("Handle 4:3 on 16:9 TV"),      &mTv169HandleOther, 3, tTv169HandleOther));
   Add(new cMenuEditStraItem( tr("Audio output"),      &mSpdifForward, 2, tSpdifForward));

}

void cMenuSetupEm84::Store(void)
{
   SetupStore("TV is", sTvIs169 = mTvIs169);
   SetupStore("Handle 16:9 on 4:3 TV", sTv43HandleOther= mTv43HandleOther);
   SetupStore("Handle 4:3 on 16:9 TV", sTv169HandleOther= mTv169HandleOther);
   SetupStore("Audio output", sSpdifForward= mSpdifForward);
   SetupStore("OSD4MB", sOSDHas4MB = mOSDHas4MB);
   

   if (cDeviceEm84xx::GetInstance() != NULL) {
      cDeviceEm84xx::GetInstance()->SetOutputAspectRatio(sTvIs169);
      cDeviceEm84xx::GetInstance()->SetOutputHandle169on43(sTv43HandleOther);
      cDeviceEm84xx::GetInstance()->SetOutputHandle43on169(sTv169HandleOther);
      cDeviceEm84xx::GetInstance()->SetAudioOutput(sSpdifForward);
   }
}


cPluginEm84xx::cPluginEm84xx(void)
{
}

cPluginEm84xx::~cPluginEm84xx()
{
   cDeviceEm84xx::Destruct();
}

const char *cPluginEm84xx::CommandLineHelp(void)
{
   return NULL;
}

bool cPluginEm84xx::ProcessArgs(int argc, char *argv[])
{
   return true;
}

bool cPluginEm84xx::Initialize(void)
{
   LOGFD = fopen("/tmp/em84xx.log", "a");

   cDeviceEm84xx::Initialize();
   if (cDeviceEm84xx::GetInstance() != NULL) {
      cDeviceEm84xx::GetInstance()->SetOutputAspectRatio(sTvIs169);
      cDeviceEm84xx::GetInstance()->SetOutputHandle169on43(sTv43HandleOther);
      cDeviceEm84xx::GetInstance()->SetOutputHandle43on169(sTv169HandleOther);
      cDeviceEm84xx::GetInstance()->SetAudioOutput(sSpdifForward);
   }

   return true;
}

bool cPluginEm84xx::Start(void)
{
   return true;
}

void cPluginEm84xx::Stop(void)
{
}

cOsdObject *cPluginEm84xx::MainMenuAction(void)
{
   cControl::Launch(new cControlEm84xx());
   return NULL;
}

cMenuSetupPage *cPluginEm84xx::SetupMenu(void)
{
   return new cMenuSetupEm84;
}

bool cPluginEm84xx::SetupParse(const char *Name, const char *Value)
{
   if (!strcasecmp(Name, "TV is")) {
      sTvIs169  = atoi(Value);
   }
   else if (!strcasecmp(Name, "Handle 16:9 on 4:3 TV")) {
      sTv43HandleOther = atoi(Value);
   }
   else if (!strcasecmp(Name, "Handle 4:3 on 16:9 TV")) {
      sTv169HandleOther = atoi(Value);
   }
   else if (!strcasecmp(Name, "Audio output")) {
      sSpdifForward = atoi(Value);
   }
   else if( !strcasecmp(Name, "OSD4MB") ) {
      sOSDHas4MB = atoi(Value);
   }
   else
      return false;

   return true;
}

VDRPLUGINCREATOR(cPluginEm84xx); // Don't touch this!
