/***************************************************************************
 *   Copyright (C) 2006 by Frank Simon   *
 *   frank@linux   *
 *                                                                         *
 *   This program 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 program 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.             *
 ***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <iostream>
#include <cstdlib>
#include <vdr/remote.h>
#include <vdr/plugin.h>

#include <touch.h>
#include <debug.h>
#include <service.h>

cTouchScreenEnhancer::cTouchScreenEnhancer(void)
{
  if (oncestarted) d(0,"cTouchScreenEnhancer started again");
  oncestarted =true;
}

/// und Destruktor
cTouchScreenEnhancer::~cTouchScreenEnhancer(void)
{
  mTouchDriver->Stop();
  delete mTouchDriver;
  mTouchDriver=0;
}


void cTouchScreenEnhancer::Housekeeping()
{
  isyslog("TouchTFT: Housekeeping... Stopping task...");
  mTouchDriver->Stop();
}

bool  cTouchScreenEnhancer::IsOk(void)
{
  return(mTouchDriver->IsOk());
}

/**
 * Initialisiert den Touch-Device usw.
 */
void cTouchScreenEnhancer::Initialize(void)
{
  mTouchDriver = new cTouchDriver(this);
  mTouchDriver->Start();
}

/**
 * Der Touch wurde betätigt. Touched liefert :
 * true, falls gedrückt, 
 * false, falls losgelassen
 */
void cTouchScreenEnhancer::TouchEvent(int x, int y, bool Touched)
{  
  // Check whether we are in calibration mode and adapt accordingly
  if( TouchTFTSetup._gtft.activate )
    {
      if( x < TouchTFTSetup.xmin ) TouchTFTSetup.xmin = x;
      if( y < TouchTFTSetup.ymin ) TouchTFTSetup.ymin = y;
      if( x > TouchTFTSetup.xmax ) TouchTFTSetup.xmax = x;
      if( y > TouchTFTSetup.ymax ) TouchTFTSetup.ymax = y;
    }

  int xs, ys;
  scaletoscreen(x,y,xs,ys);        
  d( 1, "Touched at: %d:%d\n", xs, ys );

  // SEND TO GRAPHTFT! 
  cGraphTftService::GraphTftTouchEvent data;

  data.x = xs;
  data.y = ys;
  data.button = cGraphTftService::mbLeft;
  data.flag = cGraphTftService::efNone;

  cPluginManager::CallAllServices( GRAPHTFT_TOUCH_EVENT_ID, &data );
}


void cTouchScreenEnhancer::scaletoscreen(int x, int y, int& rx, int& ry)
{
  float dx = (x - TouchTFTSetup.xmin);
  float dx_0 = (TouchTFTSetup.xmax-TouchTFTSetup.xmin);
  float dy = (y - TouchTFTSetup.ymin);
  float dy_0 = (TouchTFTSetup.ymax-TouchTFTSetup.ymin);
  rx = (int) (dx/dx_0 * TouchTFTSetup.realx);
  ry = (int) (TouchTFTSetup.realy - dy/dy_0 *TouchTFTSetup.realy);
}

