/*****************************************************************************/

/*
 *      ns2ktvgui_cb.c -- REALmagic NS2000TV setting application GTK+ callbacks
 *
 *      Copyright (C) 2000 Sigma Designs
 *                    written by Pascal Cannenterre <pascal_cannenterre@sdesigns.com>
 *
 *      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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#include "define.h"
#include "ns2ktvgui_cb.h"
#include "rmns2ktvset.h"


void Save_clicked( GtkWidget *widget, gpointer   data )
{
  DWORD result;
  G_MESSAGE("\nOK clicked !");
  result = ApplyToStartup();

  G_MESSAGE("\nStock into the EEPROM : %li\n",result);

  gtk_main_quit();
}


void Cancel_clicked( GtkWidget *widget, gpointer   data )
{
  G_MESSAGE("\nCancel clicked !");	
}

void Reset_clicked( GtkWidget *widget, gpointer   data )
{
  if (TvReset())
    G_PRINT("Reset to Default failure !");
  else
    refresh_all_cb(NULL,data);
   
  G_MESSAGE("\nReset clicked !");
}

void position_left_pressed( GtkWidget *widget, gpointer   data)
{
  
  GdkModifierType mask;
  long tvhpos;
  
  if(!get_TvHorizontalPosition(&tvhpos)) {
    if(!put_TvHorizontalPosition(tvhpos-1))
      G_PRINT("\nTv hpos : %li",tvhpos-1);
  }
 
  gdk_window_get_pointer (widget->window, NULL, NULL, &mask);
  
  while (mask & GDK_BUTTON1_MASK)
    {
      gdk_window_get_pointer (widget->window, NULL, NULL, &mask);
      
      G_MESSAGE("left still press");
      
      gtk_main_iteration_do (FALSE);
      usleep(50000);

      if(!get_TvHorizontalPosition(&tvhpos)) {
	if(!put_TvHorizontalPosition(tvhpos-1))
	  G_PRINT("\nTv hpos : %li",tvhpos-1);
      }
    }

  G_MESSAGE("Left pressed !");
}

void position_right_pressed( GtkWidget *widget, gpointer   data )
{
  // DWORD FMPreturn;
  GdkModifierType mask;
  long tvhpos;
  
  if(!get_TvHorizontalPosition(&tvhpos)) {
    if(!put_TvHorizontalPosition(tvhpos+1))
      G_PRINT("\nTv hpos : %li",tvhpos+1);
  }
  
  gdk_window_get_pointer (widget->window, NULL, NULL, &mask);
  
  while (mask & GDK_BUTTON1_MASK)
    {
      gdk_window_get_pointer (widget->window, NULL, NULL, &mask);
      
      G_MESSAGE("left still press");
      
      gtk_main_iteration_do (FALSE);
      usleep(50000);

      if(!get_TvHorizontalPosition(&tvhpos)) {
	if(!put_TvHorizontalPosition(tvhpos+1))
	  G_PRINT("\nTv hpos : %li",tvhpos+1);
      }
    }

  G_MESSAGE("Right pressed !");
}


void position_down_pressed( GtkWidget *widget, gpointer   data )
{
  // DWORD FMPreturn;
  GdkModifierType mask;
  long tvvpos;
  
  if(!get_TvVerticalPosition(&tvvpos)) {
    if(!put_TvVerticalPosition(tvvpos+1))
      G_PRINT("\nTv vpos : %li",tvvpos+1);
  }
  
  gdk_window_get_pointer (widget->window, NULL, NULL, &mask);
  
  while (mask & GDK_BUTTON1_MASK)
    {
      gdk_window_get_pointer (widget->window, NULL, NULL, &mask);
      
      G_MESSAGE("left still press");
      
      gtk_main_iteration_do (FALSE);
      usleep(50000);
      
      if(!get_TvVerticalPosition(&tvvpos)) {
	if(!put_TvVerticalPosition(tvvpos+1))
	  G_PRINT("\nTv vpos : %li",tvvpos+1);
      } 
    }
  
  G_MESSAGE("Down pressed !");
}


void position_up_pressed( GtkWidget *widget, gpointer   data )
{
  
  // DWORD FMPreturn;
  GdkModifierType mask;
  long tvvpos;

  if(!get_TvVerticalPosition(&tvvpos)) {
    if(!put_TvVerticalPosition(tvvpos-1))
      G_PRINT("\nTv vpos : %li",tvvpos-1);
  }
  
  gdk_window_get_pointer (widget->window, NULL, NULL, &mask);

  while (mask & GDK_BUTTON1_MASK)
    {
      gdk_window_get_pointer (widget->window, NULL, NULL, &mask);
      
      G_MESSAGE("left still press");
      
      gtk_main_iteration_do (FALSE);
      usleep(50000);

      if(!get_TvVerticalPosition(&tvvpos)) {
	if(!put_TvVerticalPosition(tvvpos-1))
	  G_PRINT("\nTv vpos : %li",tvvpos-1);
      }
      
    } 
  G_MESSAGE("Up pressed !");
}


void size_venlarge_pressed( GtkWidget *widget, gpointer   data )
{
  // DWORD FMPreturn;
  GdkModifierType mask;
  long tvheight;
  
  if(!get_TvHeight(&tvheight)) {
    if(!put_TvHeight(tvheight+1))
      G_PRINT("\nTv height : %li",tvheight+1);
    }
   
  gdk_window_get_pointer (widget->window, NULL, NULL, &mask);
  
  while (mask & GDK_BUTTON1_MASK)
    {
      gdk_window_get_pointer (widget->window, NULL, NULL, &mask);
      
      G_MESSAGE("left still press");
      
      gtk_main_iteration_do (FALSE);
      usleep(50000);

      if(!get_TvHeight(&tvheight)) {
	if(!put_TvHeight(tvheight+1))
	  G_PRINT("\nTv height : %li",tvheight+1);
      }
    }

  G_MESSAGE("Vertical Enlarge pressed !");
}

void size_henlarge_pressed( GtkWidget *widget, gpointer   data )
{
  // DWORD FMPreturn;
  GdkModifierType mask;
  long tvwidth;
  
  if(!get_TvWidth(&tvwidth)) {
    if(!put_TvWidth(tvwidth+1)) 
      G_PRINT("\nTv width : %li",tvwidth+1);
  }
  
  gdk_window_get_pointer (widget->window, NULL, NULL, &mask);
  
  while (mask & GDK_BUTTON1_MASK)
    {
      gdk_window_get_pointer (widget->window, NULL, NULL, &mask);
      
      G_MESSAGE("left still press");
      
      gtk_main_iteration_do (FALSE);
      usleep(50000);
      
      if(!get_TvWidth(&tvwidth)) {
	if(!put_TvWidth(tvwidth+1))
	  G_PRINT("\nTv width : %li",tvwidth+1);
      }
    }
  
  G_MESSAGE("Horizontal Enlarge pressed !");
}

void size_hreduce_pressed( GtkWidget *widget, gpointer   data )
{
  // DWORD FMPreturn;
  GdkModifierType mask;
  long tvwidth;
  
  if(!get_TvWidth(&tvwidth)) {
    if(!put_TvWidth(tvwidth-1)) 
      G_PRINT("\nTv width : %li",tvwidth-1);
  }

  gdk_window_get_pointer (widget->window, NULL, NULL, &mask);
 
  while (mask & GDK_BUTTON1_MASK)
    {
      gdk_window_get_pointer (widget->window, NULL, NULL, &mask);
      
      G_MESSAGE("left still press");
      
      gtk_main_iteration_do (FALSE);
      usleep(50000);
      
      if(!get_TvWidth(&tvwidth)) {
	if(!put_TvWidth(tvwidth-1))
	  G_PRINT("\nTv width : %li",tvwidth-1);
      }
    }
    
  G_MESSAGE("Horizontal Reduce pressed !");
}

void size_vreduce_pressed( GtkWidget *widget, gpointer   data )
{
  // DWORD FMPreturn;
  GdkModifierType mask;
  long tvheight;
  
  if(!get_TvHeight(&tvheight)) {
    if(!put_TvHeight(tvheight-1)) 
      G_PRINT("\nTv height : %li",tvheight-1);
  }
  
  gdk_window_get_pointer (widget->window, NULL, NULL, &mask);
  
  while (mask & GDK_BUTTON1_MASK)
    {
      gdk_window_get_pointer (widget->window, NULL, NULL, &mask);
      
      G_MESSAGE("left still press");
      
      gtk_main_iteration_do (FALSE);
      usleep(50000);

      if(!get_TvHeight(&tvheight)) {
	if(!put_TvHeight(tvheight-1)) 
	  G_PRINT("\nTv height : %li",tvheight-1);
      }
    }

  G_MESSAGE("Vertical Reduce pressed !");
}

void switch_TVstandard_cb( GtkWidget *widget, gpointer   data ) 
{
  G_MESSAGE("TV standard changed !");
  
  if (GTK_TOGGLE_BUTTON (widget)->active)
    {
      put_TvStandard((DWORD) GPOINTER_TO_UINT(data));
    } 
}
        
void switch_TVoutput_mode_cb( GtkWidget *widget, gpointer   data )
{
  G_MESSAGE("TV output mode changed !");
  
  if (GTK_TOGGLE_BUTTON (widget)->active)
    {
      put_TvOutputMode((DWORD) GPOINTER_TO_UINT(data));
    }  
}

void switch_Scart_RF_cb( GtkWidget *widget, gpointer   data )
{
  G_MESSAGE("Scart / Rf changed !");
  
  if (GTK_TOGGLE_BUTTON (widget)->active)
    {
      put_TvScart((DWORD) GPOINTER_TO_UINT(data));
    }  
}

void brightness_changed( GtkAdjustment *adj,gpointer   data )
{
  if( !put_TvBrightness( (BRT_MIN+BRT_MAX) - ((short)adj->value) ) )
    G_PRINT("\nSet new Br value : %hd",(BRT_MIN+BRT_MAX) - (short) adj->value);
}


void contrast_changed( GtkAdjustment *adj,gpointer   data )
{
  if(!put_TvContrast( (CON_MIN+CON_MAX) - ((short)adj->value) ) )
    G_PRINT("\nSet new Co value : %hd",(CON_MIN+CON_MAX) - (short) adj->value);
}

void saturation_changed( GtkAdjustment *adj,gpointer   data )
{
  if(!put_TvSaturation( (CSC_MIN+CSC_MAX) - ((short)adj->value) ) )
    G_PRINT("\nSet new Sa value : %d",(CSC_MIN+CSC_MAX)-(short) adj->value);
}


void sharpness_changed( GtkAdjustment *adj,gpointer   data )
{
  if(!put_TvSharpness( (SHP_MIN+SHP_MAX) - ((short)adj->value) ) )
    G_PRINT("\nSet new Sh value : %hd",(SHP_MIN+SHP_MAX)- (short) adj->value);
}

void flicker_changed( GtkAdjustment *adj,gpointer   data )
{
  if(!put_TvFlicker( (FLICKER_MIN+FLICKER_MAX) - ((short)adj->value) ) )
    G_PRINT("\nSet new Fl value : %hd",(FLICKER_MIN+FLICKER_MAX) - (short) adj->value);
}

void refresh_BSCShFl_frame_cb(GtkWidget *widget)
{
  GtkAdjustment	*obj;
  short val;
  G_MESSAGE("Affiche moi !");

  if(widget != NULL)
    {
  // refresh brightness
      obj = GTK_RANGE(gtk_object_get_data (GTK_OBJECT (widget),"brightness_scale"))->adjustment;
      if(!get_TvBrightness(&val))
	gtk_adjustment_set_value(obj,(short) (BRT_MIN+BRT_MAX - val) );
      else
	G_MESSAGE("Failure to get Brightness value !");
  // refresh Saturation
      obj = GTK_RANGE(gtk_object_get_data (GTK_OBJECT (widget),"saturation_scale"))->adjustment;
      if(!get_TvSaturation(&val))
	gtk_adjustment_set_value(obj,(short) (CSC_MIN+CSC_MAX - val) );
      else
	G_MESSAGE("Failure to get Saturation value !");
  // refresh Contrast
      obj = GTK_RANGE(gtk_object_get_data (GTK_OBJECT (widget),"contrast_scale"))->adjustment;
      if(!get_TvContrast(&val))
	gtk_adjustment_set_value(obj,(short) (CON_MIN+CON_MAX - val) );
      else
	G_MESSAGE("Failure to get Contrast value !");
  // refresh Sharpness
      obj = GTK_RANGE(gtk_object_get_data (GTK_OBJECT (widget),"sharpness_scale"))->adjustment;
      if(!get_TvSharpness(&val))
	gtk_adjustment_set_value(obj,(short) (SHP_MIN+SHP_MAX - val) );
      else
	G_MESSAGE("Failure to get Sharpness value !");
  // refresh Flicker
      obj = GTK_RANGE(gtk_object_get_data (GTK_OBJECT (widget),"flicker_scale"))->adjustment;
      if(!get_TvFlicker(&val))
	gtk_adjustment_set_value(obj,(short) (FLICKER_MIN+FLICKER_MAX - val) );
      else
	G_MESSAGE("Failure to get Flicher value !");
    }
  else
    G_MESSAGE("Trouble with obj BSCShFl");
}

void refresh_TV_standard_frame_cb( GtkWidget *widget )
{
  GtkToggleButton * obj;
  DWORD standard;
  
  if(!get_TvStandard(&standard))
    {
      switch(standard)
	{
	case SET_NTSC:
	  obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioNTSC"));
	  gtk_toggle_button_set_active (obj, TRUE);
	  break;
	case escNtscPedestal:
	  obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioNTSCjapan"));
	  gtk_toggle_button_set_active (obj, TRUE);
	  break;
	case SET_PAL:
	  obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioPAL"));
	  gtk_toggle_button_set_active (obj, TRUE);
	  break;	 
	default:
	  obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioHDTV480"));
	  gtk_toggle_button_set_active (obj, TRUE); 
	}
    }
  else
    G_MESSAGE("Couldn't refresh the TV standard !");
}


void refresh_Output_Mode_frame_cb( GtkWidget *widget )
{
  GtkToggleButton * obj;
  DWORD outputmode;
  
  if(!get_TvOutputMode(&outputmode))
    {
      switch(outputmode)
	{
	case OUTPUT_OFF:
	  obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioOFF"));
	  gtk_toggle_button_set_active (obj, TRUE);
	  break;
	case COMPOSITE:
	  obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioCOMPOSITE"));
	  gtk_toggle_button_set_active (obj, TRUE);
	    break;
	case COMPONENT_RGB:
	  obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioRGB"));
	  gtk_toggle_button_set_active (obj, TRUE);
	  break;
	default:
	  obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioYUV"));
	  gtk_toggle_button_set_active (obj, TRUE);
	}
    }
  else
    G_MESSAGE("Couldn't refresh the TV output mode !");
}

void refresh_Scart_RF_frame_cb( GtkWidget *widget )
{
  GtkToggleButton * obj;
  DWORD scartrf;

  if(!get_TvScart(&scartrf))
    {
      switch(scartrf)
	{
	case SCART_COMPOSITE:
	case SCART_RGB:
	  obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioON"));
	  gtk_toggle_button_set_active (obj, TRUE);
	  break;
	default:
	  obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioOFF"));
	  gtk_toggle_button_set_active (obj, TRUE);
	}
    }
  else
    G_MESSAGE("Couldn't refresh the Scart/Rf mode !");
}

void refresh_all_cb(GtkWidget *widget, gpointer   data)
{
  GtkWidget *obj;
 
  if(data != NULL)
    {
      obj = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (data),"tvstd_frame"));
      refresh_TV_standard_frame_cb(obj);
      
      obj = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (data),"tvoutmode_frame"));
      refresh_Output_Mode_frame_cb(obj);
      
      obj = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (data),"scart_rf_frame"));
      refresh_Scart_RF_frame_cb(obj);
      
      obj = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (data),"tvBSC_frame"));
      refresh_BSCShFl_frame_cb(obj);
      setOutputMode(FMPV_VIDEOOUT_VGA);
    }
  else
    G_MESSAGE("Trouble with DATA !");
  
}
