/*
 * led.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id: led.c,v 1.1.1.1 2003/06/24 12:07:17 tv Exp $
 */

#include <vdr/plugin.h>
#include <vdr/status.h>
#include <stdlib.h>

static const char *VERSION        = "0.3";
static const char *DESCRIPTION    = "Led controller";

/**
 *
 *
 **/
class Led : public cStatus {

public:
    // constructor
    Led ();
    
    
protected:
    virtual void Recording(const cDevice *Device, const char *Name);
    virtual void Replaying(const cControl *Control, const char *Name);

    // the number of recordings and replays going on
    unsigned int m_recordings;
    unsigned int m_replays;
};


/**
 * Creates the instance. Resets the internal data to a known good state.
 **/
Led::Led () {
    // no active recordings/replays yet
    m_recordings = 0;
    m_replays = 0;
}


/**
 *
 * @param Device 
 * @param Name
 **/
void Led::Recording(const cDevice *Device, const char *Name) {
    // so, what happened?
    if ( Name == NULL ) {
        // recording stopped, decrement the count
        m_recordings--;

        // log progress
        dsyslog("led: Led::Recording stopped, active now: %d", m_recordings );
    }
    else {
        // recording started, increment the count
        m_recordings++;

        // log progress
        dsyslog("led: Led::Recording started recording %s, now active: %d", Name, m_recordings );
    }

    
    // so, anything active anymore?
    if ( m_recordings == 0 ) {
        // no active recordings anymore, shut down the led using the parout application
        system ( "parout 1" );
    }

    else {
        // we have at least one going on, turn on the led using the parout application
        system ( "parout 3" );
    }
}


/**
 *
 * @param Control 
 * @param Name
 **/
void Led::Replaying(const cControl *Control, const char *Name) {
    // so, what happened?
    if ( Name == NULL ) {
        // replaying stopped, decrement the count
        m_replays--;

        // log progress
        dsyslog("led: Led::Replaying stopped, active now: %d", m_replays );
    }
    else {
        // replaying started, increment the count
        m_replays++;

        // log progress
        dsyslog("led: Led::Replaying started replaying %s, now active: %d", Name, m_replays );
    }

    
    // so, anything active anymore?
    if ( m_replays == 0 ) {
        // no active replays anymore, do something
        // system ( "parout 1" );
    }

    else {
        // we have at least one going on, do something
        //system ( "parout 3" );
    }
}


/**
 *
 *
 **/
class cPluginLed : public cPlugin {

public:
    cPluginLed(void);
    virtual ~cPluginLed();
    virtual const char *Version(void) { return VERSION; }
    virtual const char *Description(void) { return DESCRIPTION; }
    virtual bool Start(void);

    
private:
    // the Led instance
    Led * m_led;
};


cPluginLed::cPluginLed() {
    // 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!
    m_led = NULL;
}

cPluginLed::~cPluginLed() {
    // do we have anything to delete?
    if ( m_led ) {
        delete m_led;
    }
}


bool cPluginLed::Start() {
    // create the Led class
    m_led = new Led;
    return true;
}


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