/**
 *  TouchTFT plugin for the Video Disk Recorder 
 * 
 *  setup.c - A plugin for the Video Disk Recorder
 *
 *  (c) 2004 Lars Tegeler, Sascha Volkenandt  
 *
 * This code is distributed under the terms and conditions of the
 * GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
 *
 * $Id: setup.c,v 1.6 2004/05/26 11:34:33 tegeler Exp $
 *
 **/

#include "setup.h"
#include "touch.h"
#include "debug.h"

#include <vdr/plugin.h>

#define TOUCH_DEVICE "/dev/input/event3"
#define MAX_DEVICE_LEN 256

static const char* logDevices[] = 
{
   "none",
   "stdout",
   "syslog",
   "file"
};

cTouchTFTSetup TouchTFTSetup;

// --- cTouchTFTSetup -----------------------------------------------------------

//Constructor
cTouchTFTSetup::cTouchTFTSetup(void)
{
  TouchDevice = new char[MAX_DEVICE_LEN];
  strcpy(TouchDevice,TOUCH_DEVICE);
  xmin = 0;
  xmax = 4000;
  ymin = 0;
  ymax = 4000;
  realx = 720;
  realy = 576;
  entprell_ms = 20;
  Level = 0;
  Logger = 0;
  _gtft.activate = 0;
  swap_xy = 0;
}

bool cTouchTFTSetup::SetupParse(const char *Name, const char *Value)
{
    // Take Cover Images
    d(9, "Setup parse:%s\n", Name);

    // Parse your own setup parameters and store their values.
    if (!strcasecmp(Name, "xmin")) xmin = atoi(Value);
    else if (!strcasecmp(Name, "xmax")) xmax = atoi(Value);
    else if (!strcasecmp(Name, "ymin")) ymin = atoi(Value);
    else if (!strcasecmp(Name, "ymax")) ymax = atoi(Value);
    else if (!strcasecmp(Name, "realx")) realx = atoi(Value);
    else if (!strcasecmp(Name, "realy")) realy = atoi(Value);
    else if (!strcasecmp(Name, "entprell_ms")) entprell_ms = atoi(Value);
    else if (!strcasecmp(Name, "Level")) Level = atoi(Value);
    else if (!strcasecmp(Name, "Logger")) Logger = atoi(Value);
    else if (!strcasecmp(Name, "SWAP_XY")) swap_xy = atoi(Value);
    else if (!strcasecmp(Name, "TouchDevice")) 
      {
	strcpy(TouchDevice, Value);
      }
    else
      {
        d(9, "Setup parse:%s\n done false\n", Name);
	return false;
      };
    d(9, "Setup parse:%s\n done:%d\n", Name, Value);
    return true;
}

// --- cMenuSetupTouchTFT --------------------------------------------------------

cMenuSetupTouchTFT::cMenuSetupTouchTFT()
{
  //the setupmenu
  _data = &TouchTFTSetup;

  SetSection(tr("TouchTFT"));
  
  Add( new cMenuEditIntItem( tr("Minimum x-axis (touch device)"), &_data->xmin,0,4000) );
  Add( new cMenuEditIntItem( tr("Maximum x-axis (touch device)"), &_data->xmax,0,4000) );
  Add( new cMenuEditIntItem( tr("Minimum y-axis (touch device)"), &_data->ymin,0,4000) );
  Add( new cMenuEditIntItem( tr("Maximum y-axis (touch device)"), &_data->ymax,0,4000) );
  Add( new cMenuEditIntItem( tr("Resolution x-axis (pixels)"), &_data->realx,0,1024) );
  Add( new cMenuEditIntItem( tr("Resolution y-axis (pixels)"), &_data->realy,0,1024) );    
  Add( new cMenuEditIntItem( tr("Entprellzeit [ms]"), &_data->entprell_ms,1,1000) );
  Add( new cMenuEditStrItem( tr("Touch device"), _data->TouchDevice,  MAX_DEVICE_LEN, NULL) );
  Add( new cMenuEditIntItem( tr("SWAP XY"), &_data->swap_xy,  0,1) );
  Add( new cMenuEditStraItem( tr("Log Device"), &_data->Logger, 4, logDevices));
  Add( new cMenuEditIntItem( tr("Debug Level"), &_data->Level, 0, 10) );

  SetHelp( _data->_gtft.activate? tr( "Stop" ): tr( "Calibrate" ) );
}

eOSState cMenuSetupTouchTFT::ProcessKey(eKeys Key)
{
  eOSState state = cOsdMenu::ProcessKey(Key);
  
  switch (Key) 
    {
    case kRed :
      {
	state = osUser1;
      } break;
      
    case kGreen  : state = osUser2;
      break;
      
    case kYellow : 
      state = osUser3;
      
      break;
      
    case kBlue   : state = osUser4;
      
      break;
      
    default      : break;
    }
  
  switch (state) 
    {
    case osUnknown:
      if (Key == kOk) 
	{
	  Store();
	  state = osBack;
	}
      break;
      
    case osUser1:   // Red
      {
	_data->_gtft.activate = ! _data->_gtft.activate;
	SetHelp( _data->_gtft.activate? tr( "Stop" ): tr( "Calibrate" ) );
	cPluginManager::CallAllServices( GRAPHTFT_CALIBRATION_ID, &(_data->_gtft) );

	if( _data->_gtft.activate )
	  { // Calibration started, reset values
	    _data->xmin = 9999;
	    _data->ymin = 9999;
	    _data->xmax = -9999;
	    _data->ymax = -9999;
	  }
	state = osContinue;
      } break;
      
    case osUser2:   //Green
      state = osContinue;
      break;
      
    case osUser3:   //Yellow
      state = osContinue;
      break;
      
    case osUser4:   //Blue
      state = osContinue;
      break;
      
    case osBack:    //Back
      break;
      
    default:
      break;
    }
  return state;
}

void cMenuSetupTouchTFT::Store(void)
{
  //store the setupdata
  SetupStore("xmin", _data->xmin);
  SetupStore("xmax", _data->xmax);
  SetupStore("ymin", _data->ymin);
  SetupStore("ymax", _data->ymax);
  SetupStore("realx", _data->realx);
  SetupStore("realy", _data->realy);
  SetupStore("entprell_ms", _data->entprell_ms);
  SetupStore("Level", TouchTFTSetup.Level);
  SetupStore("Logger", TouchTFTSetup.Logger);
  SetupStore("TouchDevice", _data->TouchDevice);
  SetupStore("SWAP_XY",_data->swap_xy);
}
