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

#include <vdr/plugin.h>
#include <vdr/thread.h>
#include <vdr/status.h>
#include <vdr/device.h>
#include <vdr/player.h>
#include "vdrmonthread.h"

#define VDRMON_PORT		2011

const char *VERSION        = "0.6";
static const char *DESCRIPTION    = "kvdrmon support plugin";
static const char *MAINMENUENTRY  = "Vdrmon";

class cPluginVdrmon : public cPlugin, cStatus {
private:
  // Add any member variables or functions you may need here.
  cVdrmonThread * Thread;
  int port;
protected:
#if VDRVERSNUM >= 10338
  virtual void Recording(const cDevice *Device, const char *Name, const char *FileName, bool On);
#else
  virtual void Recording(const cDevice *Device, const char *Name);
#endif

#if VDRVERSNUM >= 10338
  void Replaying(const cControl *Control, const char *Name, const char *FileName, bool On);
#else
  void Replaying(const cControl * control, const char * name);
#endif
public:
  cPluginVdrmon(void);
  virtual ~cPluginVdrmon();
  virtual const char *Version(void) { return VERSION; }
  virtual const char *Description(void) { return DESCRIPTION; }
  virtual const char *CommandLineHelp(void);
  virtual bool Initialize(void);
  virtual bool Start(void);
  virtual void Stop(void);
  virtual void Housekeeping(void);
  virtual const char *MainMenuEntry(void) { return MAINMENUENTRY; }
  virtual cOsdObject *MainMenuAction(void);
  virtual cMenuSetupPage *SetupMenu(void);
  virtual bool ProcessArgs(int argc, char *argv[]);
};

cPluginVdrmon::cPluginVdrmon(void)
{
  // 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!
  Thread = NULL;
  port = VDRMON_PORT;
}

cPluginVdrmon::~cPluginVdrmon()
{
  // Clean up after yourself!
}

cOsdObject * cPluginVdrmon::MainMenuAction(void)
{
  return NULL;
}

cMenuSetupPage * cPluginVdrmon::SetupMenu(void)
{
  return NULL;
}
  
const char * cPluginVdrmon::CommandLineHelp(void)
{
  return "  -p port          port number to listen to";
}

bool cPluginVdrmon::ProcessArgs(int argc, char *argv[])
{
  // port given?
  if (argc == 2 && argv[0] == "-p")
    port = atoi(argv[1]);

  // default port
  if (port <= 0)
    port = VDRMON_PORT;

  return true;
}

bool cPluginVdrmon::Initialize(void)
{
  // Initialize any background activities the plugin shall perform.

  // Start any background activities the plugin shall perform.
  Thread = new cVdrmonThread(port);

  return Thread != NULL;
}

bool cPluginVdrmon::Start(void)
{
  Thread->Start();

  return true;
}

void cPluginVdrmon::Stop(void)
{
  // Stop any background activities the plugin shall perform.
  Thread->Shutdown();
}

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

#if VDRVERSNUM >= 10338
void cPluginVdrmon::Recording(const cDevice *Device, const char *Name, const char *FileName, bool On)
{
  // Recording started or ended
  Thread->Recording(On);
}
#else
void cPluginVdrmon::Recording(const cDevice * device, const char * Name)
{
  // Recording started or ended
  Thread->Recording(Name != NULL);
}
#endif

#if VDRVERSNUM >= 10338
void cPluginVdrmon::Replaying(const cControl *Control, const char *Name, const char *FileName, bool On)
{
  Thread->Replaying(On ? Name : NULL);
}
#else
void cPluginVdrmon::Replaying(const cControl * control, const char * Name)
{
  // Replaying started or ended
  Thread->Replaying(Name);
}
#endif

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