/*
 * 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

static const char *VERSION        = "0.2";
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:
  void Recording(const cDevice * device, const char * name);
  void Replaying(const cControl * control, const char * name);
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;
}

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.
}

void cPluginVdrmon::Recording(const cDevice * device, const char * name)
{
  // Recording started or ended
  Thread->Recording(name);
}

void cPluginVdrmon::Replaying(const cControl * control, const char * name)
{
  // Replaying started or ended
  Thread->Replaying(name);
}

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