/*
 * extb plugin to VDR (C++)
 *
 * (C) 2004 - 2007 Andreas Brachold <vdr04-at-deltab-dot-de>
 *
 * This code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
 */

#include <getopt.h>
#include "extboardio.h"
#include "extb.h"
#include "extb_status.h"
#include "extb_setup_menu.h"
#include "extb_osd_menu.h"
#include "i18n.h"




static const char *VERSION        = "0.2.14";
static const char *DESCRIPTION    = "control the extb board";
static const char *MAINMENUENTRY  = "Extension board";
static const char *DEFAULT_LIRCD  = "/dev/lircd";

cExtBLircWatch *gLircWatch = NULL;



class cPluginExtB
 : public cPlugin {
  cExtBStatusMonitor *statusMonitor;
  cExtBoardIO        m_ext;
  char*              m_szDevice;
public:
  cPluginExtB(void);
  virtual ~cPluginExtB();
  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 Stop(void);
  virtual void Housekeeping(void);
  virtual const char *MainMenuEntry(void);
  virtual cOsdObject *MainMenuAction(void);
  virtual cMenuSetupPage *SetupMenu(void);
  virtual bool SetupParse(const char *Name, const char *Value);
};

cPluginExtB::cPluginExtB(void)
{
  statusMonitor = NULL;
  m_szDevice = NULL;
}

cPluginExtB::~cPluginExtB()
{
  //Cleanup if'nt throw housekeeping
  if(statusMonitor) {
    delete statusMonitor;
    statusMonitor = NULL;
  }

  if(gLircWatch) {
    delete gLircWatch;
    gLircWatch = NULL;
  }
 
  if(m_szDevice) {
    free(m_szDevice);
    m_szDevice = NULL;
  }  
}

const char *cPluginExtB::CommandLineHelp(void)
{
  // Return a string that describes all known command line options.
  return  "  -d DEV,   --device=DEV   sets the lirc-device to other device than /dev/lircd\n";
}

bool cPluginExtB::ProcessArgs(int argc, char *argv[])
{
  static struct option long_options[] =
  {
    { "device",   required_argument, NULL, 'd'},
    { NULL}
  };

  int c;
  int option_index = 0;
  while ((c = getopt_long(argc, argv, "d:", long_options, &option_index)) != -1)
  {
    switch (c)
    {
      case 'd':
      {
        if(m_szDevice) {
          free(m_szDevice);
          m_szDevice = NULL;
        }
        m_szDevice = strdup(optarg);
        break;
      }
      default:
        return false;
    }
  }

  if (NULL == m_szDevice)
  {
    // neither Device given:
    // => set "/dev/lircd" it to default
    m_szDevice = strdup(DEFAULT_LIRCD);
  }

  return true;
}

bool cPluginExtB::Initialize(void)
{
  if(NULL == m_szDevice || !m_ext.open(m_szDevice)) {
    esyslog("extb: lirc_open() failed!");
    return (false);
  }
  
  statusMonitor = new cExtBStatusMonitor(&m_ext);
  if(NULL == statusMonitor){
    esyslog("extb: can't create cExtBStatusMonitor!");
    return (false);
  }
  
  gLircWatch = new cExtBLircWatch(m_szDevice);
  if(NULL == gLircWatch){
    esyslog("extb: can't create cExtBLircWatch!");
    return (false);
  }
  return true;
}

bool cPluginExtB::Start(void)
{
  RegisterI18n(Phrases);

  if(!m_ext.isopen())
    return false;
  /* active RGB + audio output */
  m_ext.sendmore(theSetup.m_szStartUp);
  m_ext.Update(true);
  return true;
}

void cPluginExtB::Stop(void)
{
  if(m_ext.isopen()) {
    m_ext.sendmore(theSetup.m_szShutDown);
  }
  
  //Cleanup if'nt throw housekeeping
  if(statusMonitor) {
    delete statusMonitor;
    statusMonitor = NULL;
  }

  if(gLircWatch) {
    delete gLircWatch;
    gLircWatch = NULL;
  }
 
  if(m_ext.isopen()) {
    m_ext.flush();
    m_ext.close();
  }
  
  if(m_szDevice) {
    free(m_szDevice);
    m_szDevice = NULL;
  }  
}

void cPluginExtB::Housekeeping(void)
{
  if(m_ext.isopen())
    m_ext.Update(true); //Keep alive
}

const char *cPluginExtB::MainMenuEntry(void) 
{ 
  if(theSetup.m_bHideMainMenuEntry)
    return NULL;
  else
    return tr(MAINMENUENTRY); 
}

cOsdObject *cPluginExtB::MainMenuAction(void)
{
  return (new cExtBOsdMenu(&m_ext));
}

cMenuSetupPage *cPluginExtB::SetupMenu(void)
{
  return (new cExtBMenuSetup(&m_ext));
}

bool cPluginExtB::SetupParse(const char *szName, const char *szValue)
{
  // Parse your own setup parameters and store their values.
  return theSetup.SetupParse(szName,szValue);
}

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