/*
 * ambilight.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/device.h>
static const char *VERSION        = "0.0.1";
static const char *DESCRIPTION    = "Enter description for 'ambilight' plugin";
static const char *MAINMENUENTRY  = "Ambilight";

class cAmbilight : public cThread {
protected:
    virtual void Action(void);
public:
    cAmbilight(void);
    ~cAmbilight();
};

class cPluginAmbilight : public cPlugin {
private:
  // Add any member variables or functions you may need here.
  cAmbilight *ambilight;
public:
  cPluginAmbilight(void);
  virtual ~cPluginAmbilight();
  virtual const char *Version(void) { return VERSION; }
  virtual const char *Description(void) { return 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) { return MAINMENUENTRY; }
  virtual cOsdObject *MainMenuAction(void);
  virtual cMenuSetupPage *SetupMenu(void);
  virtual bool SetupParse(const char *Name, const char *Value);
  virtual bool Service(const char *Id, void *Data = NULL);
  virtual const char **SVDRPHelpPages(void);
  virtual cString SVDRPCommand(const char *Command, const char *Option, int &ReplyCode);
  };



cPluginAmbilight::cPluginAmbilight(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!
}

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

const char *cPluginAmbilight::CommandLineHelp(void)
{
  // Return a string that describes all known command line options.
  return NULL;
}

bool cPluginAmbilight::ProcessArgs(int argc, char *argv[])
{
  // Implement command line argument processing here if applicable.
  return true;
}

bool cPluginAmbilight::Initialize(void)
{
  // Initialize any background activities the plugin shall perform.
  return true;
}

bool cPluginAmbilight::Start(void)
{
  // Start any background activities the plugin shall perform.
  printf("Ambilight Plugin started\n");
  ambilight = new cAmbilight();
  ambilight->Start();
  return true;
}

void cPluginAmbilight::Stop(void)
{
  delete(ambilight);
  // Stop any background activities the plugin shall perform.
}

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

cOsdObject *cPluginAmbilight::MainMenuAction(void)
{
  // Perform the action when selected from the main VDR menu.
  return NULL;
}

cMenuSetupPage *cPluginAmbilight::SetupMenu(void)
{
  // Return a setup menu in case the plugin supports one.
  return NULL;
}

bool cPluginAmbilight::SetupParse(const char *Name, const char *Value)
{
  // Parse your own setup parameters and store their values.
  return false;
}

bool cPluginAmbilight::Service(const char *Id, void *Data)
{
  // Handle custom service requests from other plugins
  return false;
}

const char **cPluginAmbilight::SVDRPHelpPages(void)
{
  // Return help text for SVDRP commands this plugin implements
  return NULL;
}

cString cPluginAmbilight::SVDRPCommand(const char *Command, const char *Option, int &ReplyCode)
{
  // Process SVDRP commands this plugin implements
  return NULL;
}

// Constructor of cAmbilight Thread
cAmbilight::cAmbilight(void) {

}

cAmbilight::~cAmbilight() {

}


void cAmbilight::Action(void) {
    int size;
    int x,y;
    int red, green, blue, offset;
    while (Running()) {
	uchar *pnmImage=cDevice::PrimaryDevice()->GrabImage(size,false,100,48,32);
	printf("Ambilight grabbed %d bytes: ",size);
	offset=13;// vom pnmbild
	red=0;
	green=0;
	blue=0;
	// addiere alle Werte im Bild auf
	for (y=0; y <32; y++) {
	    for (x=0; x <48; x++) {
		red += pnmImage[offset + (x +  y*48) *3 + 0];	    
		green += pnmImage[offset + (x +  y*48) *3 + 1];	    
		blue += pnmImage[offset + (x +  y*48) *3 + 2];	    

	    }
	}
	red = red / 32 / 48;
	green = green / 32 / 48;
	blue = blue / 32 / 48;
	
	printf ("RGB: %d %d %d\n", red, green, blue);
	free(pnmImage);
	usleep(100000);
	
    }
}


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