/*
 * externalplayer.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 <unistd.h>
#include <getopt.h>

#include "externalplayer-player.h"

static const char *VERSION        = "0.0.1";
static const char *DESCRIPTION    = "launch external players from main menu";

class cPluginExternalplayer : public cPlugin {
private:
  char * playerCommand;
  char * mainMenuEntry;
  bool releaseRemotes;
  char * rcuDevice;
  char * lircDevice;
public:
  cPluginExternalplayer();
  virtual ~cPluginExternalplayer();
  virtual const char * Version() { return VERSION; }
  virtual const char * Description() { return DESCRIPTION; }
  virtual const char * CommandLineHelp();
  virtual bool ProcessArgs(int argc, char * argv[]);
  virtual bool Initialize();
  virtual bool Start();
  virtual void Stop();
  virtual void Housekeeping();
  virtual const char * MainMenuEntry() { return mainMenuEntry; }
  virtual cOsdObject * MainMenuAction();
  virtual cMenuSetupPage * SetupMenu();
  virtual bool SetupParse(const char * Name, const char * Value);
  virtual bool Service(const char * Id, void * Data = NULL);
};

cPluginExternalplayer::cPluginExternalplayer() {
  playerCommand  = NULL;
  mainMenuEntry  = NULL;
  releaseRemotes = false;
  rcuDevice  = "/dev/ttyS1";
  lircDevice = "/dev/lircd";
}

cPluginExternalplayer::~cPluginExternalplayer() {
}

const char *cPluginExternalplayer::CommandLineHelp() {
  return "  -c CMD,    --command=CMD     command to execute\n"
         "  -m ENTRY,  --menuentry=ENTRY main menu entry\n"
         "  -r,        --release-remotes release kbd, lirc and rcu devices\n"
         "  -L DEVICE, --lirc-device     lirc device (default: /dev/lircd)\n"
         "  -R DEVICE, --rcu-device      rcu device (default: /dev/ttyS1)\n";
}

bool cPluginExternalplayer::ProcessArgs(int argc, char *argv[]) {
  static struct option long_options[] = {
    { "command",         required_argument, NULL, 'c' },
    { "menuentry",       required_argument, NULL, 'm' },
    { "release-remotes", no_argument,       NULL, 'r' },
    { "lirc-device",     required_argument, NULL, 'L' },
    { "rcu-device",      required_argument, NULL, 'R' },
    { NULL }
  };

  int c;
  while ((c = getopt_long(argc, argv, "c:m:rL:R:", long_options, NULL)) != -1) {
    switch (c) {
      case 'c': playerCommand = strdup(optarg);
                break;
      case 'm': mainMenuEntry = strdup(optarg);
                break;
      case 'r': releaseRemotes = true;
                break;
      case 'L': lircDevice = strdup(optarg);
                break;
      case 'R': rcuDevice = strdup(optarg);
                break;
      default:  return false;
    }
  }

  if (playerCommand == NULL || mainMenuEntry == NULL) {
    return false;
  }

  return true;
}

bool cPluginExternalplayer::Initialize() {
  return true;
}

bool cPluginExternalplayer::Start() {
  return true;
}

void cPluginExternalplayer::Stop() {
}

void cPluginExternalplayer::Housekeeping() {
}

cOsdObject *cPluginExternalplayer::MainMenuAction() {
  isyslog("externalplayer-plugin: starting player");

  ePlayMode playMode = pmExtern_THIS_SHOULD_BE_AVOIDED;

  cRemotesDisable * remotesLock = NULL;

  if (releaseRemotes) {
    remotesLock = new cRemotesDisable(lircDevice, rcuDevice);
  }

  cControl::Launch(new cControlExternalplayer(playMode, remotesLock, playerCommand));
  return NULL;
}

cMenuSetupPage *cPluginExternalplayer::SetupMenu() {
  return NULL;
}

bool cPluginExternalplayer::SetupParse(const char *Name, const char *Value) {
  return false;
}

bool cPluginExternalplayer::Service(const char *Id, void *Data) {
  return false;
}

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