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

#include "device.h"
#include "player.h"
#include "common.h"
#include "setup.h"
#include "i18n.h"

#include <vdr/config.h>
#include <vdr/plugin.h>

static const char *VERSION        = "0.0.13";
static const char *DESCRIPTION    = "An output plugin for sigma em84xx chips";
static const char *MAINMENUENTRY  = "Play Disk";

// flag, if a dvd is connected and should be used
int sDvdConnected = 0;
// commandline options
bool sOsd4mb = 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);
   virtual const char *MainMenuEntry(void);
   virtual cOsdObject *MainMenuAction(void);
   virtual cMenuSetupPage *SetupMenu(void);
   virtual bool SetupParse(const char *Name, const char *Value);
};


cPluginEm84xx::cPluginEm84xx(void)
{
}

cPluginEm84xx::~cPluginEm84xx()
{

}

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

bool cPluginEm84xx::ProcessArgs(int argc, char *argv[])
{
   static struct option long_options[] = 
   {
         { "osd4mb",      no_argument,       NULL, 'o' },
         { NULL }
      };

   int c;
   while ((c = getopt_long(argc, argv, "o", long_options, NULL)) != -1)
   {
      switch (c)
      {
               case 'o':
            sOsd4mb = true;
         break;

         default:
            return false;
         }
   }

   return true;
}

const char* cPluginEm84xx::MainMenuEntry(void)
{
   if (sDvdConnected)
      return tr(MAINMENUENTRY);
   return NULL;
}

bool cPluginEm84xx::Initialize(void)
{
   // check for dvd drive
   RegisterI18n(em84xxPhrases);
   isyslog("em84xx: checking for a dvd drive");
   char* envUseDrive = getenv("USE_DRIVE");
   if (envUseDrive == NULL)
      isyslog("em84xx: no dvd drive detected (USE_DRIVE not set)");
   else
   {
      if (strncmp (envUseDrive, "/dev/", 5) == 0)
      {
         sDvdConnected = 1;
         isyslog("em84xx: dvd drive detected (device: %s)",envUseDrive);
      }
      else
         isyslog("em84xx: no dvd drive detected (unknown device: %s)",envUseDrive);
   }

#ifdef EM84_DEBUG
   LOGFD = fopen("em84xx.log", "a");
#endif

   cDeviceEm84xx::Initialize(sOsd4mb);
   cDeviceEm84xx::Configure();

   return true;
}

bool cPluginEm84xx::Start(void)
{
   if (!cDeviceEm84xx::GetInstance())
      return true;

   if ((!cDevice::PrimaryDevice()) || (cDevice::PrimaryDevice()->DeviceNumber() != cDeviceEm84xx::GetInstance()->DeviceNumber()))
   {
      if (cDevice::SetPrimaryDevice(cDeviceEm84xx::GetInstance()->DeviceNumber()+1))
         Setup.PrimaryDVB = cDeviceEm84xx::GetInstance()->DeviceNumber()+1;

   }
   return true;
}

void cPluginEm84xx::Stop(void)
{
   // reset dvd speed
   if (sDvdConnected && (cDeviceEm84xx::GetInstance()))
      cDeviceEm84xx::GetInstance()->SetControlDvdSpeed(0);
      
   cDeviceEm84xx::GetInstance()->SetUsePlayDisc(true);
   cDeviceEm84xx::Destruct();    
}

cOsdObject *cPluginEm84xx::MainMenuAction(void)
{
   cControl::Launch(new cControlEm84xx(cDeviceEm84xx::GetInstance()));
   if (cDeviceEm84xx::GetInstance())
   cDeviceEm84xx::GetInstance()->SetUsePlayDisc(true);
   return NULL;
}

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

bool cPluginEm84xx::SetupParse(const char *Name, const char *Value)
{
   return SetupEm84xx.SetupParse(Name, Value);
}

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