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

#include <linux/dvb/frontend.h>
#include <sys/ioctl.h>
#include <vdr/diseqc.h>
#include <vdr/plugin.h>
#include <vdr/dvbdevice.h>
#include "i18n.h"
#include "menu.h"
#include "status.h" 

static const char *VERSION        = "0.1.0";
static const char *DESCRIPTION    = "Steuerung eines Rotors";
static const char *MAINMENUENTRY  = "Rotor";

// --- cPluginRotor ---------------------------------------------------------

class cPluginRotor : public cPlugin {
private:
  // Add any member variables or functions you may need here.
  cStatusMonitor *statusMonitor;
public:
  cPluginRotor(void);
  virtual ~cPluginRotor();
  virtual const char *Version(void) { return VERSION; }
  virtual const char *Description(void) { return tr(DESCRIPTION); }
  virtual const char *CommandLineHelp(void);
  virtual bool ProcessArgs(int argc, char *argv[]);
  virtual bool Initialize(void);
  virtual bool Start(void);
  virtual void Housekeeping(void);
  virtual const char *MainMenuEntry(void) { return tr(MAINMENUENTRY); }
  virtual cOsdObject *MainMenuAction(void);
  virtual cMenuSetupPage *SetupMenu(void);
  virtual bool SetupParse(const char *Name, const char *Value);
};

cPluginRotor::cPluginRotor(void)
{
  statusMonitor = NULL;
  // Initialize any member variables here.
  // DON'T DO ANYTHING ELSE THAT MAY HAVE SIDE EFFECTS, REQUIRE GLOBAL
  // VDR OBJECTS TO EXIST OR PRODUCE ANY OUTPUT!
}

cPluginRotor::~cPluginRotor()
{
  // Clean up after yourself!
  delete statusMonitor;
}

const char *cPluginRotor::CommandLineHelp(void)
{
  // Return a string that describes all known command line options.
  return NULL;
}

bool cPluginRotor::ProcessArgs(int argc, char *argv[])
{
  // Implement command line argument processing here if applicable.
  return true;
}

bool cPluginRotor::Initialize(void)
{
  // Initialize any background activities the plugin shall perform.
  return true;
}

bool cPluginRotor::Start(void)
{
  // Start any background activities the plugin shall perform.
  statusMonitor = new cStatusMonitor;
  RegisterI18n(Phrases);
  List.Add(tr("Not in DISEQC.CONF"),"",0,0);
  cDiseqc *diseqc;
  uchar *codes;
  int n;
  for(const cSource *source=Sources.First();source;source=(cSource *)source->Next())
  {
    if (diseqc=Diseqcs.Get(source->Code(),12000,'h')) 
    {
      char *CurrentAction = NULL;
      while ((diseqc->Execute(&CurrentAction))!= cDiseqc::daNone)  
      {
        codes = diseqc->Codes(n);
        if (n==4 && (*codes==0xe0 || *codes==0xe1) && (*(codes+1)==0x30 || *(codes+1)==0x31) && *(codes+2)==0x6b)
        {
          List.Add(cSource::ToString(source->Code()),source->Description(),*(codes+3),source->Code());
          break;
        }   
      } 
    }
    if (!List.GetfromSource(source->Code()) && ((source->Code() & 0xC000) == 0x8000))
      List.Add(cSource::ToString(source->Code()),source->Description(),0,source->Code());
  }
  char buffer[PATH_MAX];
  snprintf(buffer, sizeof(buffer), "/dev/dvb/adapter%d/frontend0", data.DvbCard);
  data.fd_frontend = open(buffer,0);
  return true;
}

void cPluginRotor::Housekeeping(void)
{
  // Perform any cleanup or other regular tasks.
}

cOsdObject *cPluginRotor::MainMenuAction(void)
{
  // Perform the action when selected from the main VDR menu.
  return new cMainMenuRotor;
}

cMenuSetupPage *cPluginRotor::SetupMenu(void)
{
  // Return a setup menu in case the plugin supports one.
  return new cMenuSetupRotor;
}

bool cPluginRotor::SetupParse(const char *Name, const char *Value)
{
  // Parse your own setup parameters and store their values.
  if      (!strcasecmp(Name, "DVB-Karte"))      data.DvbCard = atoi(Value);
//  else if (!strcasecmp(Name, "AdressByte"))     data.AByte = atoi(Value);
  else if (!strcasecmp(Name, "Wiederholen"))    data.repeat = atoi(Value);
  else if (!strcasecmp(Name, "Latitude"))       data.Lat = atoi(Value);
  else if (!strcasecmp(Name, "SouthNorth"))     data.SN = atoi(Value);
  else if (!strcasecmp(Name, "Longitude"))      data.Long = atoi(Value);
  else if (!strcasecmp(Name, "EastWest"))       data.EW = atoi(Value);
  else if (!strcasecmp(Name, "SLOF"))           data.SLOF = atoi(Value);
  else if (!strcasecmp(Name, "FrequLo"))        data.FrequLo = atoi(Value);
  else if (!strcasecmp(Name, "FrequHi"))        data.FrequHi = atoi(Value);
  else if (!strcasecmp(Name, "UseGotoX"))       data.UseGotoX = atoi(Value);
  else if (!strcasecmp(Name, "ShowInfo"))       data.ShowInfo = atoi(Value);
  else
    return false;
  return true;
}

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

